]> code.delx.au - gnu-emacs/blob - lisp/textmodes/reftex-toc.el
7f922ce85a4a764123bcb78e0189e947dcad73aa
[gnu-emacs] / lisp / textmodes / reftex-toc.el
1 ;;; reftex-toc.el --- RefTeX's table of contents mode
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5
6 ;; Author: Carsten Dominik <dominik@science.uva.nl>
7 ;; Version: 4.28
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31 (provide 'reftex-toc)
32 (require 'reftex)
33 ;;;
34
35 (defvar reftex-toc-map (make-sparse-keymap)
36 "Keymap used for *toc* buffer.")
37
38 (defvar reftex-toc-menu)
39
40 (defun reftex-toc-mode ()
41 "Major mode for managing Table of Contents for LaTeX files.
42 This buffer was created with RefTeX.
43 Press `?' for a summary of important key bindings.
44
45 Here are all local bindings.
46
47 \\{reftex-toc-map}"
48 (interactive)
49 (kill-all-local-variables)
50 (setq major-mode 'reftex-toc-mode
51 mode-name "TOC")
52 (use-local-map reftex-toc-map)
53 (set (make-local-variable 'transient-mark-mode) t)
54 (set (make-local-variable 'zmacs-regions) t)
55 (set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert)
56 (set (make-local-variable 'reftex-toc-include-labels-indicator) "")
57 (set (make-local-variable 'reftex-toc-max-level-indicator)
58 (if (= reftex-toc-max-level 100)
59 "ALL"
60 (int-to-string reftex-toc-max-level)))
61 (setq mode-line-format
62 (list "---- " 'mode-line-buffer-identification
63 " " 'global-mode-string " (" mode-name ")"
64 " L<" 'reftex-toc-include-labels-indicator ">"
65 " I<" 'reftex-toc-include-index-indicator ">"
66 " T<" 'reftex-toc-max-level-indicator ">"
67 " -%-"))
68 (setq truncate-lines t)
69 (when (featurep 'xemacs)
70 ;; XEmacs needs the call to make-local-hook
71 (make-local-hook 'post-command-hook)
72 (make-local-hook 'pre-command-hook))
73 (make-local-variable 'reftex-last-follow-point)
74 (add-hook 'post-command-hook 'reftex-toc-post-command-hook nil t)
75 (add-hook 'pre-command-hook 'reftex-toc-pre-command-hook nil t)
76 (easy-menu-add reftex-toc-menu reftex-toc-map)
77 (run-mode-hooks 'reftex-toc-mode-hook))
78
79 (defvar reftex-last-toc-file nil
80 "Stores the file name from which `reftex-toc' was called. For redo command.")
81
82 (defvar reftex-last-window-height nil)
83 (defvar reftex-last-window-width nil)
84 (defvar reftex-toc-include-labels-indicator nil)
85 (defvar reftex-toc-include-index-indicator nil)
86 (defvar reftex-toc-max-level-indicator nil)
87
88 (defvar reftex-toc-return-marker (make-marker)
89 "Marker which makes it possible to return from toc to old position.")
90
91 (defconst reftex-toc-help
92 " AVAILABLE KEYS IN TOC BUFFER
93 ============================
94 n / p next-line / previous-line
95 SPC Show the corresponding location of the LaTeX document.
96 TAB Goto the location and keep the *toc* window.
97 RET Goto the location and hide the *toc* window (also on mouse-2).
98 < / > Promote / Demote section, or all sections in region.
99 C-c > Display Index. With prefix arg, restrict index to current section.
100 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
101 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
102 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
103 f / g Toggle follow mode / Refresh *toc* buffer.
104 a / d Toggle auto recenter / Toggle dedicated frame
105 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
106 . In other window, show position from where `reftex-toc' was called.
107 M-% Global search and replace to rename label at point.
108 x Switch to TOC of external document (with LaTeX package `xr').
109 z Jump to a specific section (e.g. '3 z' goes to section 3).")
110
111 (defun reftex-toc (&optional rebuild reuse)
112 "Show the table of contents for the current document.
113 When called with a raw C-u prefix, rescan the document first."
114
115 ;; The REUSE argument means, search all visible frames for a window
116 ;; displaying the toc window. If yes, reuse this window.
117
118 (interactive)
119
120 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
121 current-prefix-arg)
122 (reftex-erase-buffer "*toc*"))
123
124 (setq reftex-last-toc-file (buffer-file-name))
125 (setq reftex-last-toc-master (reftex-TeX-master-file))
126
127 (set-marker reftex-toc-return-marker (point))
128
129 ;; If follow mode is active, arrange to delay it one command
130 (if reftex-toc-follow-mode
131 (setq reftex-toc-follow-mode 1))
132
133 (and reftex-toc-include-index-entries
134 (reftex-ensure-index-support))
135 (or reftex-support-index
136 (setq reftex-toc-include-index-entries nil))
137
138 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
139 (reftex-access-scan-info current-prefix-arg)
140
141 (let* ((this-buf (current-buffer))
142 (docstruct-symbol reftex-docstruct-symbol)
143 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
144 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
145 (here-I-am (if (boundp 'reftex-rebuilding-toc)
146 (get 'reftex-toc :reftex-data)
147 (car (reftex-where-am-I))))
148 (unsplittable (if (fboundp 'frame-property)
149 (frame-property (selected-frame) 'unsplittable)
150 (frame-parameter (selected-frame) 'unsplittable)))
151 offset toc-window)
152
153 (if (setq toc-window (get-buffer-window
154 "*toc*"
155 (if reuse 'visible)))
156 (select-window toc-window)
157 (when (or (not reftex-toc-keep-other-windows)
158 (< (window-height) (* 2 window-min-height)))
159 (delete-other-windows))
160
161 (setq reftex-last-window-width (window-width)
162 reftex-last-window-height (window-height)) ; remember
163
164 (unless unsplittable
165 (if reftex-toc-split-windows-horizontally
166 (split-window-horizontally
167 (floor (* (window-width)
168 reftex-toc-split-windows-fraction)))
169 (split-window-vertically
170 (floor (* (window-height)
171 reftex-toc-split-windows-fraction)))))
172
173 (let ((default-major-mode 'reftex-toc-mode))
174 (switch-to-buffer "*toc*")))
175
176 (or (eq major-mode 'reftex-toc-mode) (reftex-toc-mode))
177 (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol)
178 (setq reftex-toc-include-labels-indicator
179 (if (eq reftex-toc-include-labels t)
180 "ALL"
181 reftex-toc-include-labels))
182 (setq reftex-toc-include-index-indicator
183 (if (eq reftex-toc-include-index-entries t)
184 "ALL"
185 reftex-toc-include-index-entries))
186
187 (cond
188 ((= (buffer-size) 0)
189 ;; buffer is empty - fill it with the table of contents
190 (message "Building *toc* buffer...")
191
192 (setq buffer-read-only nil)
193 (insert (format
194 "TABLE-OF-CONTENTS on %s
195 SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
196 ------------------------------------------------------------------------------
197 " (abbreviate-file-name reftex-last-toc-master)))
198
199 (if (reftex-use-fonts)
200 (put-text-property (point-min) (point) 'face reftex-toc-header-face))
201 (put-text-property (point-min) (point) 'intangible t)
202 (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist)
203
204 (setq offset
205 (reftex-insert-docstruct
206 this-buf
207 t ; include toc
208 reftex-toc-include-labels
209 reftex-toc-include-index-entries
210 reftex-toc-include-file-boundaries
211 reftex-toc-include-context
212 nil ; counter
213 nil ; commented
214 here-I-am
215 "" ; xr-prefix
216 t ; a toc buffer
217 ))
218
219 (run-hooks 'reftex-display-copied-context-hook)
220 (message "Building *toc* buffer...done.")
221 (setq buffer-read-only t))
222 (t
223 ;; Only compute the offset
224 (setq offset
225 (or (reftex-get-offset this-buf here-I-am
226 (if reftex-toc-include-labels " " nil)
227 t
228 reftex-toc-include-index-entries
229 reftex-toc-include-file-boundaries)
230 (reftex-last-assoc-before-elt
231 'toc here-I-am
232 (symbol-value reftex-docstruct-symbol))))
233 (put 'reftex-toc :reftex-line 3)
234 (goto-line 3)
235 (beginning-of-line)))
236
237 ;; Find the correct starting point
238 (reftex-find-start-point (point) offset (get 'reftex-toc :reftex-line))
239 (setq reftex-last-follow-point (point))))
240
241 (defun reftex-toc-recenter (&optional arg)
242 "Display the TOC window and highlight line corresponding to current position."
243 (interactive "P")
244 (let ((buf (current-buffer))
245 (frame (selected-frame)))
246 (reftex-toc arg t)
247 (if (= (count-lines 1 (point)) 2)
248 (let ((current-prefix-arg nil))
249 (select-window (get-buffer-window buf frame))
250 (reftex-toc nil t)))
251 (and (> (point) 1)
252 (not (get-text-property (point) 'intangible))
253 (memq reftex-highlight-selection '(cursor both))
254 (reftex-highlight 2
255 (or (previous-single-property-change
256 (min (point-max) (1+ (point))) :data)
257 (point-min))
258 (or (next-single-property-change (point) :data)
259 (point-max))))
260 (select-window (get-buffer-window buf frame))))
261
262 (defun reftex-toc-pre-command-hook ()
263 ;; used as pre command hook in *toc* buffer
264 (reftex-unhighlight 0)
265 )
266
267 (defun reftex-toc-post-command-hook ()
268 ;; used in the post-command-hook for the *toc* buffer
269 (when (get-text-property (point) :data)
270 (put 'reftex-toc :reftex-data (get-text-property (point) :data))
271 (and (> (point) 1)
272 (not (get-text-property (point) 'intangible))
273 (memq reftex-highlight-selection '(cursor both))
274 (reftex-highlight 2
275 (or (previous-single-property-change (1+ (point)) :data)
276 (point-min))
277 (or (next-single-property-change (point) :data)
278 (point-max)))))
279 (if (integerp reftex-toc-follow-mode)
280 ;; remove delayed action
281 (setq reftex-toc-follow-mode t)
282 (and (not (reftex-toc-dframe-p))
283 reftex-toc-follow-mode
284 (not (equal reftex-last-follow-point (point)))
285 ;; show context in other window
286 (setq reftex-last-follow-point (point))
287 (condition-case nil
288 (reftex-toc-visit-location nil (not reftex-revisit-to-follow))
289 (error t)))))
290
291 (defun reftex-re-enlarge ()
292 ;; Enlarge window to a remembered size.
293 (if reftex-toc-split-windows-horizontally
294 (enlarge-window-horizontally
295 (max 0 (- (or reftex-last-window-width (window-width))
296 (window-width))))
297 (enlarge-window
298 (max 0 (- (or reftex-last-window-height (window-height))
299 (window-height))))))
300
301 (defun reftex-toc-dframe-p (&optional frame error)
302 ;; Check if FRAME is the dedicated TOC frame.
303 ;; If yes, and ERROR is non-nil, throw an error.
304 (setq frame (or frame (selected-frame)))
305 (let ((res (equal
306 (if (fboundp 'frame-property)
307 (frame-property frame 'name)
308 (frame-parameter frame 'name))
309 "RefTeX TOC Frame")))
310 (if (and res error)
311 (error "This frame is view-only. Use `C-c =' to create toc window for commands"))
312 res))
313
314 (defun reftex-toc-show-help ()
315 "Show a summary of special key bindings."
316 (interactive)
317 (reftex-toc-dframe-p nil 'error)
318 (with-output-to-temp-buffer "*RefTeX Help*"
319 (princ reftex-toc-help))
320 (reftex-enlarge-to-fit "*RefTeX Help*" t)
321 ;; If follow mode is active, arrange to delay it one command
322 (if reftex-toc-follow-mode
323 (setq reftex-toc-follow-mode 1)))
324
325 (defun reftex-toc-next (&optional arg)
326 "Move to next selectable item."
327 (interactive "p")
328 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
329 (setq reftex-callback-fwd t)
330 (or (eobp) (forward-char 1))
331 (goto-char (or (next-single-property-change (point) :data)
332 (point))))
333 (defun reftex-toc-previous (&optional arg)
334 "Move to previous selectable item."
335 (interactive "p")
336 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
337 (setq reftex-callback-fwd nil)
338 (goto-char (or (previous-single-property-change (point) :data)
339 (point))))
340 (defun reftex-toc-next-heading (&optional arg)
341 "Move to next table of contentes line."
342 (interactive "p")
343 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
344 (end-of-line)
345 (re-search-forward "^ " nil t arg)
346 (beginning-of-line))
347 (defun reftex-toc-previous-heading (&optional arg)
348 "Move to previous table of contentes line."
349 (interactive "p")
350 (if (boundp 'zmacs-region-stays) (setq zmacs-region-stays t))
351 (re-search-backward "^ " nil t arg))
352 (defun reftex-toc-toggle-follow ()
353 "Toggle follow (other window follows with context)."
354 (interactive)
355 (setq reftex-last-follow-point -1)
356 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
357 (defun reftex-toc-toggle-file-boundary ()
358 "Toggle inclusion of file boundaries in *toc* buffer."
359 (interactive)
360 (setq reftex-toc-include-file-boundaries
361 (not reftex-toc-include-file-boundaries))
362 (reftex-toc-revert))
363 (defun reftex-toc-toggle-labels (arg)
364 "Toggle inclusion of labels in *toc* buffer.
365 With prefix ARG, prompt for a label type and include only labels of
366 that specific type."
367 (interactive "P")
368 (setq reftex-toc-include-labels
369 (if arg (reftex-query-label-type)
370 (not reftex-toc-include-labels)))
371 (reftex-toc-revert))
372 (defun reftex-toc-toggle-index (arg)
373 "Toggle inclusion of index in *toc* buffer.
374 With prefix arg, prompt for an index tag and include only entries of that
375 specific index."
376 (interactive "P")
377 (setq reftex-toc-include-index-entries
378 (if arg (reftex-index-select-tag)
379 (not reftex-toc-include-index-entries)))
380 (reftex-toc-revert))
381 (defun reftex-toc-toggle-context ()
382 "Toggle inclusion of label context in *toc* buffer.
383 Label context is only displayed when the labels are there as well."
384 (interactive)
385 (setq reftex-toc-include-context (not reftex-toc-include-context))
386 (reftex-toc-revert))
387 (defun reftex-toc-max-level (arg)
388 "Set the maximum level of toc lines in this buffer to value of prefix ARG.
389 When no prefix is given, set the max level to a large number, so that all
390 levels are shown. For eaxample, to set the level to 3, type `3 m'."
391 (interactive "P")
392 (setq reftex-toc-max-level (if arg
393 (prefix-numeric-value arg)
394 100))
395 (setq reftex-toc-max-level-indicator
396 (if arg (int-to-string reftex-toc-max-level) "ALL"))
397 (reftex-toc-revert))
398 (defun reftex-toc-view-line ()
399 "View document location in other window."
400 (interactive)
401 (reftex-toc-dframe-p nil 'error)
402 (reftex-toc-visit-location))
403 (defun reftex-toc-goto-line-and-hide ()
404 "Go to document location in other window. Hide the *toc* window."
405 (interactive)
406 (reftex-toc-dframe-p nil 'error)
407 (reftex-toc-visit-location 'hide))
408 (defun reftex-toc-goto-line ()
409 "Go to document location in other window. *toc* window stays."
410 (interactive)
411 (reftex-toc-dframe-p nil 'error)
412 (reftex-toc-visit-location t))
413 (defun reftex-toc-mouse-goto-line-and-hide (ev)
414 "Go to document location in other window. Hide the *toc* window."
415 (interactive "e")
416 (mouse-set-point ev)
417 (reftex-toc-dframe-p nil 'error)
418 (reftex-toc-visit-location 'hide))
419 (defun reftex-toc-show-calling-point ()
420 "Show point where reftex-toc was called from."
421 (interactive)
422 (reftex-toc-dframe-p nil 'error)
423 (let ((this-window (selected-window)))
424 (unwind-protect
425 (progn
426 (switch-to-buffer-other-window
427 (marker-buffer reftex-toc-return-marker))
428 (goto-char (marker-position reftex-toc-return-marker))
429 (recenter '(4)))
430 (select-window this-window))))
431 (defun reftex-toc-quit ()
432 "Hide the *toc* window and do not move point.
433 If the toc window is the only window on the dedicated TOC frame, the frame
434 is destroyed."
435 (interactive)
436 (if (and (one-window-p)
437 (reftex-toc-dframe-p)
438 (> (length (frame-list)) 1))
439 (delete-frame)
440 (or (one-window-p) (delete-window))
441 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
442 (reftex-re-enlarge)
443 (goto-char (or (marker-position reftex-toc-return-marker) (point)))))
444 (defun reftex-toc-quit-and-kill ()
445 "Kill the *toc* buffer."
446 (interactive)
447 (kill-buffer "*toc*")
448 (or (one-window-p) (delete-window))
449 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
450 (reftex-re-enlarge)
451 (goto-char (marker-position reftex-toc-return-marker)))
452 (defun reftex-toc-display-index (&optional arg)
453 "Display the index buffer for the current document.
454 This works just like `reftex-display-index' from a LaTeX buffer.
455 With prefix arg 1, restrict index to the section at point."
456 (interactive "P")
457 (reftex-toc-dframe-p nil 'error)
458 (let ((data (get-text-property (point) :data))
459 (docstruct (symbol-value reftex-docstruct-symbol))
460 bor eor restr)
461 (when (equal arg 2)
462 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
463 eor (assoc 'toc (cdr (memq bor docstruct)))
464 restr (list (nth 6 bor) bor eor)))
465 (reftex-toc-goto-line)
466 (reftex-display-index (if restr nil arg) restr)))
467
468 ;; Rescanning the document and rebuilding the TOC buffer.
469 (defun reftex-toc-rescan (&rest ignore)
470 "Regenerate the *toc* buffer by reparsing file of section at point."
471 (interactive)
472 (if (and reftex-enable-partial-scans
473 (null current-prefix-arg))
474 (let* ((data (get-text-property (point) :data))
475 (what (car data))
476 (file (cond ((eq what 'toc) (nth 3 data))
477 ((memq what '(eof bof file-error)) (nth 1 data))
478 ((stringp what) (nth 3 data))
479 ((eq what 'index) (nth 3 data))))
480 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
481 (if (not file)
482 (error "Don't know which file to rescan. Try `C-u r'")
483 (put 'reftex-toc :reftex-line line)
484 (switch-to-buffer-other-window
485 (reftex-get-file-buffer-force file))
486 (setq current-prefix-arg '(4))
487 (let ((reftex-rebuilding-toc t))
488 (reftex-toc))))
489 (reftex-toc-Rescan))
490 (reftex-kill-temporary-buffers))
491
492 (defun reftex-toc-Rescan (&rest ignore)
493 "Regenerate the *toc* buffer by reparsing the entire document."
494 (interactive)
495 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
496 (put 'reftex-toc :reftex-line line))
497 (switch-to-buffer-other-window
498 (reftex-get-file-buffer-force reftex-last-toc-file))
499 (setq current-prefix-arg '(16))
500 (let ((reftex-rebuilding-toc t))
501 (reftex-toc)))
502
503 (defun reftex-toc-revert (&rest ignore)
504 "Regenerate the *toc* from the internal lists."
505 (interactive)
506 (let ((unsplittable
507 (if (fboundp 'frame-property)
508 (frame-property (selected-frame) 'unsplittable)
509 (frame-parameter (selected-frame) 'unsplittable)))
510 (reftex-rebuilding-toc t))
511 (if unsplittable
512 (switch-to-buffer
513 (reftex-get-file-buffer-force reftex-last-toc-file))
514 (switch-to-buffer-other-window
515 (reftex-get-file-buffer-force reftex-last-toc-file))))
516 (reftex-erase-buffer "*toc*")
517 (setq current-prefix-arg nil)
518 (reftex-toc t))
519
520 (defun reftex-toc-external (&rest ignore)
521 "Switch to table of contents of an external document."
522 (interactive)
523 (reftex-toc-dframe-p nil 'error)
524 (let* ((old-buf (current-buffer))
525 (xr-alist (get-text-property 1 'xr-alist))
526 (xr-index (reftex-select-external-document
527 xr-alist 0)))
528 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
529 (cdr (nth xr-index xr-alist)))
530 (error "Cannot switch document")))
531 (reftex-toc)
532 (if (equal old-buf (current-buffer))
533 (message "")
534 (message "Switched document"))))
535
536 (defun reftex-toc-jump (arg)
537 "Jump to a specific section. E.g. '3 z' jumps to section 3.
538 Useful for large TOC's."
539 (interactive "P")
540 (goto-char (point-min))
541 (re-search-forward
542 (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
543 nil t)
544 (beginning-of-line))
545
546 ;; Promotion/Demotion stuff
547
548 (defun reftex-toc-demote (&optional arg)
549 "Demote section at point. If region is active, apply to all in region."
550 (interactive "p")
551 (reftex-toc-do-promote 1))
552 (defun reftex-toc-promote (&optional arg)
553 "Promote section at point. If region is active, apply to all in region."
554 (interactive "p")
555 (reftex-toc-do-promote -1))
556 (defun reftex-toc-do-promote (delta)
557 "Workhorse for reftex-toc-promote and reftex-to-demote.
558 Changes the level of sections in the current region, or just the section at
559 point."
560 ;; We should not do anything unless we are sure this is going to work for
561 ;; each section in the region. Therefore we first collect information and
562 ;; test.
563 (let* ((start-line (+ (count-lines (point-min) (point))
564 (if (bolp) 1 0)))
565 (mark-line (if (reftex-region-active-p)
566 (save-excursion (goto-char (mark))
567 (+ (count-lines (point-min) (point))
568 (if (bolp) 1 0)))))
569 (start-pos (point))
570 (pro-or-de (if (> delta 0) "de" "pro"))
571 beg end entries data sections nsec mpos msg)
572 (setq msg
573 (catch 'exit
574 (if (reftex-region-active-p)
575 ;; A region is dangerous, check if we have a brandnew scan,
576 ;; to make sure we are not missing any section statements.
577 (if (not (reftex-toc-check-docstruct))
578 (reftex-toc-load-all-files-for-promotion) ;; exits
579 (setq beg (min (point) (mark))
580 end (max (point) (mark))))
581 (setq beg (point) end (point)))
582 (goto-char beg)
583 (while (and (setq data (get-text-property (point) :data))
584 (<= (point) end))
585 (if (eq (car data) 'toc) (push (cons data (point)) entries))
586 (goto-char (or (next-single-property-change (point) :data)
587 (point-max))))
588 (setq entries (nreverse entries))
589 ;; Get the relevant section numbers, for an informative message
590 (goto-char start-pos)
591 (setq sections (reftex-toc-extract-section-number (car entries)))
592 (if (> (setq nsec (length entries)) 1)
593 (setq sections
594 (concat sections "-"
595 (reftex-toc-extract-section-number
596 (nth (1- nsec) entries)))))
597 ;; Run through the list and prepare the changes.
598 (setq entries (mapcar 'reftex-toc-promote-prepare entries))
599 ;; Ask for permission
600 (if (or (not reftex-toc-confirm-promotion) ; never confirm
601 (and (integerp reftex-toc-confirm-promotion) ; confirm if many
602 (< nsec reftex-toc-confirm-promotion))
603 (yes-or-no-p ; ask
604 (format "%s %d toc-entr%s (section%s %s)? "
605 (if (< delta 0) "Promote" "Demote")
606 nsec
607 (if (= 1 nsec) "y" "ies")
608 (if (= 1 nsec) "" "s")
609 sections)))
610 nil ; we have permission, do nothing
611 (error "Abort")) ; abort, we don't have permission
612 ;; Do the changes
613 (mapcar 'reftex-toc-promote-action entries)
614 ;; Rescan the document and rebuilt the toc buffer
615 (save-window-excursion
616 (reftex-toc-Rescan))
617 (reftex-toc-restore-region start-line mark-line)
618 (message "%d section%s %smoted"
619 nsec (if (= 1 nsec) "" "s") pro-or-de)
620 nil))
621 (if msg (progn (ding) (message msg)))))
622
623 (defvar delta)
624 (defvar mpos)
625 (defvar pro-or-de)
626 (defvar start-pos)
627 (defvar start-line)
628 (defvar mark-line)
629
630 (defun reftex-toc-restore-region (point-line &optional mark-line)
631 (if mark-line
632 (progn (goto-line mark-line)
633 (setq mpos (point))))
634 (if point-line (goto-line point-line))
635 (if mark-line
636 (progn
637 (set-mark mpos)
638 (if (fboundp 'zmacs-activate-region)
639 (zmacs-activate-region)
640 (setq mark-active t
641 deactivate-mark nil)))))
642
643 (defun reftex-toc-promote-prepare (x)
644 "Look at a toc entry and see if we could pro/demote it.
645 Expects the level change DELTA to be dynamically scoped into this function.
646 This function prepares everything for the changes, but does not do it.
647 The return value is a list with information needed when doing the
648 promotion/demotion later."
649 (let* ((data (car x))
650 (toc-point (cdr x))
651 (marker (nth 4 data))
652 (literal (nth 7 data))
653 (load nil)
654 (name nil)
655 ;; Here follows some paranoid code to make very sure we are not
656 ;; going to break anything
657 (name1 ; dummy
658 (if (and (markerp marker) (marker-buffer marker))
659 ;; Buffer is still live and we have the marker.
660 (progn
661 (save-excursion
662 ;; Goto the buffer and check of section is unchanged
663 (set-buffer (marker-buffer marker))
664 (goto-char (marker-position marker))
665 (if (looking-at (regexp-quote literal))
666 ;; OK, get the makro name
667 (progn
668 (beginning-of-line 1)
669 (if (looking-at reftex-section-regexp)
670 (setq name (reftex-match-string 2))
671 (error "Something is wrong! Contact maintainer!")))
672 ;; Section has changed, request scan and loading
673 ;; We use a variable to delay until after the safe-exc.
674 ;; because otherwise we loose the region.
675 (setq load t)))
676 ;; Scan document and load all files, this exits command
677 (if load (reftex-toc-load-all-files-for-promotion))) ; exits
678 ;; We don't have a live marker: scan and load files.
679 (reftex-toc-load-all-files-for-promotion)))
680 (level (cdr (assoc name reftex-section-levels-all)))
681 (dummy (if (not (integerp level))
682 (progn
683 (goto-char toc-point)
684 (error "Cannot %smote special sections" pro-or-de))))
685 ;; Delta is dynamically scoped into here...
686 (newlevel (if (>= level 0) (+ delta level) (- level delta)))
687 (dummy2 (if (or (and (>= level 0) (= newlevel -1))
688 (and (< level 0) (= newlevel 0)))
689 (error "Cannot %smote \\%s" pro-or-de name)))
690 (newname (reftex-toc-newhead-from-alist newlevel name
691 reftex-section-levels-all)))
692 (if (and name newname)
693 (list data name newname toc-point)
694 (goto-char toc-point)
695 (error "Cannot %smote \\%s" pro-or-de name))))
696
697 (defun reftex-toc-promote-action (x)
698 "Change the level of a toc entry.
699 DELTA and PRO-OR-DE are assumed to be dynamically scoped into this function."
700 (let* ((data (car x))
701 (name (nth 1 x))
702 (newname (nth 2 x))
703 (marker (nth 4 data)))
704 (save-excursion
705 (set-buffer (marker-buffer marker))
706 (goto-char (marker-position marker))
707 (if (looking-at (concat "\\([ \t]*\\\\\\)" (regexp-quote name)))
708 (replace-match (concat "\\1" newname))
709 (error "Fatal error during %smotion" pro-or-de)))))
710
711 (defun reftex-toc-extract-section-number (entry)
712 "Get the numbering of a toc entry, for message purposes."
713 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry)))
714 (match-string 1 (nth 2 (car entry)))
715 "?"))
716
717 (defun reftex-toc-newhead-from-alist (nlevel head alist)
718 "Get new heading with level NLEVEL from ALIST.
719 If there are no such entries, return nil.
720 If there are several different entries with same new level, choose
721 the one with the smallest distance to the assocation of HEAD in the alist.
722 This makes it possible for promotion to work several sets of headings,
723 if these sets are sorted blocks in the alist."
724 (let* ((al alist)
725 (ass (assoc head al))
726 (pos (length (memq ass al)))
727 dist (mindist 1000) newhead)
728 (while al
729 (if (equal (cdar al) nlevel)
730 (progn
731 (setq dist (abs (- (length al) pos)))
732 (if (< dist mindist)
733 (setq newhead (car (car al))
734 mindist dist))))
735 (setq al (cdr al)))
736 newhead))
737
738 (defun reftex-toc-check-docstruct ()
739 "Check if the current docstruct is fully up to date and all files visited."
740 ;; We do this by checking if all sections are on the right position
741 (let ((docstruct (symbol-value reftex-docstruct-symbol))
742 entry marker empoint)
743 (catch 'exit
744 (while (setq entry (pop docstruct))
745 (if (eq (car entry) 'toc)
746 (progn
747 (setq marker (nth 4 entry)
748 empoint (nth 8 entry))
749 (if (not (and (markerp marker)
750 (marker-buffer marker)
751 (= (marker-position marker) empoint)))
752 (throw 'exit nil)))))
753 t)))
754
755 (defun reftex-toc-load-all-files-for-promotion ()
756 "Make sure all files of the document are being visited by buffers,
757 and that the scanning info is absolutely up to date.
758 We do this by rescanning with reftex-keep-temporary-buffers bound to t.
759 The variable PRO-OR-DE is assumed to be dynamically scoped into this function.
760 When finished, we exit with an error message."
761 (let ((reftex-keep-temporary-buffers t))
762 (reftex-toc-Rescan)
763 (reftex-toc-restore-region start-line mark-line)
764 (throw 'exit
765 "TOC had to be updated first. Please check selection and repeat the command.")))
766
767 (defun reftex-toc-rename-label ()
768 "Rename the currently selected label in the *TOC* buffer.
769 This launches a global search and replace in order to rename a label.
770 Renaming a label is hardly ever necessary - the only exeption is after
771 promotion/demotion in connection with a package like fancyref, where the
772 label prefix determines the wording of a reference."
773 (interactive)
774 (let* ((toc (get-text-property (point) :data))
775 (label (car toc)) newlabel)
776 (if (not (stringp label))
777 (error "This is not a label entry."))
778 (setq newlabel (read-string (format "Rename label \"%s\" to:" label)))
779 (if (assoc newlabel (symbol-value reftex-docstruct-symbol))
780 (if (not (y-or-n-p
781 (format "Label '%s' exists. Use anyway? " label)))
782 (error "Abort")))
783 (save-excursion
784 (save-window-excursion
785 (reftex-toc-visit-location t)
786 (condition-case nil
787 (reftex-query-replace-document
788 (concat "{" (regexp-quote label) "}")
789 (format "{%s}" newlabel))
790 (error t))))
791 (reftex-toc-rescan)))
792
793
794 (defun reftex-toc-visit-location (&optional final no-revisit)
795 ;; Visit the tex file corresponding to the toc entry on the current line.
796 ;; If FINAL is t, stay there
797 ;; If FINAL is 'hide, hide the *toc* window.
798 ;; Otherwise, move cursor back into *toc* window.
799 ;; NO-REVISIT means don't visit files, just use live buffers.
800 ;; This function is pretty clever about finding back a section heading,
801 ;; even if the buffer is not live, or things like outline, x-symbol etc.
802 ;; have been active.
803
804 (let* ((toc (get-text-property (point) :data))
805 (toc-window (selected-window))
806 show-window show-buffer match)
807
808 (unless toc (error "Don't know which toc line to visit"))
809
810 (cond
811
812 ((eq (car toc) 'toc)
813 ;; a toc entry
814 (setq match (reftex-toc-find-section toc no-revisit)))
815
816 ((eq (car toc) 'index)
817 ;; an index entry
818 (setq match (reftex-index-show-entry toc no-revisit)))
819
820 ((memq (car toc) '(bof eof))
821 ;; A file entry
822 (setq match
823 (let ((where (car toc))
824 (file (nth 1 toc)))
825 (if (or (not no-revisit) (reftex-get-buffer-visiting file))
826 (progn
827 (switch-to-buffer-other-window
828 (reftex-get-file-buffer-force file nil))
829 (goto-char (if (eq where 'bof) (point-min) (point-max))))
830 (message reftex-no-follow-message) nil))))
831
832 ((stringp (car toc))
833 ;; a label
834 (setq match (reftex-show-label-location toc reftex-callback-fwd
835 no-revisit t))))
836
837 (setq show-window (selected-window)
838 show-buffer (current-buffer))
839
840 (unless match
841 (select-window toc-window)
842 (error "Cannot find location"))
843
844 (select-window toc-window)
845
846 ;; use the `final' parameter to decide what to do next
847 (cond
848 ((eq final t)
849 (reftex-unhighlight 0)
850 (select-window show-window))
851 ((eq final 'hide)
852 (reftex-unhighlight 0)
853 (or (one-window-p) (delete-window))
854 ;; If `show-window' is still live, show-buffer is already visible
855 ;; so let's not make it visible in yet-another-window.
856 (if (window-live-p show-window)
857 (set-buffer show-buffer)
858 (switch-to-buffer show-buffer))
859 (reftex-re-enlarge))
860 (t nil))))
861
862 (defun reftex-toc-find-section (toc &optional no-revisit)
863 (let* ((file (nth 3 toc))
864 (marker (nth 4 toc))
865 (level (nth 5 toc))
866 (literal (nth 7 toc))
867 (emergency-point (nth 8 toc))
868 (match
869 (cond
870 ((and (markerp marker) (marker-buffer marker))
871 ;; Buffer is still live and we have the marker. Should be easy.
872 (switch-to-buffer-other-window (marker-buffer marker))
873 (push-mark nil)
874 (goto-char (marker-position marker))
875 (or (looking-at (regexp-quote literal))
876 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal))
877 (looking-at (reftex-make-desperate-section-regexp literal))
878 (looking-at (concat "\\\\"
879 (regexp-quote
880 (car
881 (rassq level
882 reftex-section-levels-all)))
883 "[[{]?"))))
884 ((or (not no-revisit)
885 (reftex-get-buffer-visiting file))
886 ;; Marker is lost. Use the backup method.
887 (switch-to-buffer-other-window
888 (reftex-get-file-buffer-force file nil))
889 (goto-char (or emergency-point (point-min)))
890 (or (looking-at (regexp-quote literal))
891 (let ((len (length literal)))
892 (or (reftex-nearest-match (regexp-quote literal) len)
893 (reftex-nearest-match
894 (reftex-make-regexp-allow-for-ctrl-m literal) len)
895 (reftex-nearest-match
896 (reftex-make-desperate-section-regexp literal) len)))))
897 (t (message reftex-no-follow-message) nil))))
898 (when match
899 (goto-char (match-beginning 0))
900 (if (not (= (point) (point-max))) (recenter 1))
901 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
902 match))
903
904 (defun reftex-make-desperate-section-regexp (old)
905 ;; Return a regexp which will still match a section statement even if
906 ;; x-symbol or isotex or the like have been at work in the mean time.
907 (let* ((n (1+ (string-match "[[{]" old)))
908 (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
909 (old (substring old n)))
910 (while (string-match
911 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
912 old)
913 (if (match-beginning 1)
914 (setq new (concat new "[^\n\r]*[\n\r]"))
915 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
916 (setq old (substring old (match-end 0))))
917 new))
918
919 ;; Auto recentering of TOC window
920
921 (defun reftex-recenter-toc-when-idle ()
922 (and (> (buffer-size) 5)
923 reftex-mode
924 (not (active-minibuffer-window))
925 (fboundp 'reftex-toc-mode)
926 (get-buffer-window "*toc*" 'visible)
927 (string= reftex-last-toc-master (reftex-TeX-master-file))
928 (let (current-prefix-arg)
929 (reftex-toc-recenter))))
930
931 (defun reftex-toggle-auto-toc-recenter ()
932 "Toggle the automatic recentering of the toc window.
933 When active, leaving point idle will make the toc window jump to the correct
934 section."
935 (interactive)
936 (if reftex-toc-auto-recenter-timer
937 (progn
938 (if (featurep 'xemacs)
939 (delete-itimer reftex-toc-auto-recenter-timer)
940 (cancel-timer reftex-toc-auto-recenter-timer))
941 (setq reftex-toc-auto-recenter-timer nil)
942 (message "Automatic recentering of toc windwo was turned off"))
943 (setq reftex-toc-auto-recenter-timer
944 (if (featurep 'xemacs)
945 (start-itimer "RefTeX Idle Timer for recenter"
946 'reftex-recenter-toc-when-idle
947 reftex-idle-time reftex-idle-time t)
948 (run-with-idle-timer
949 reftex-idle-time t 'reftex-recenter-toc-when-idle)))
950 (message "Automatic recentering of toc window was turned on")))
951
952 (defun reftex-toc-toggle-dedicated-frame ()
953 "Toggle the display of a separate frame for the TOC.
954 This frame is not used by the `reftex-toc' commands, but it is useful to
955 always show the current section in connection with the option
956 `reftex-auto-recenter-toc'."
957 (interactive)
958 (catch 'exit
959 (let* ((frames (frame-list)) frame
960 (get-frame-prop-func (if (fboundp 'frame-property)
961 'frame-property
962 'frame-parameter)))
963 (while (setq frame (pop frames))
964 (if (equal (funcall get-frame-prop-func frame 'name)
965 "RefTeX TOC Frame")
966 (progn
967 (delete-frame frame)
968 (throw 'exit nil))))
969 (reftex-make-separate-toc-frame))))
970
971 (defun reftex-make-separate-toc-frame ()
972 ;; Create a new fame showing only the toc buffer.
973 (let ((current-frame (selected-frame))
974 (current-window (selected-window))
975 (current-toc-window (get-buffer-window "*toc*" 'visible))
976 current-toc-frame)
977 (if (and current-toc-window
978 (not (equal (selected-frame) (window-frame current-toc-window))))
979 nil
980 (setq current-toc-frame
981 (make-frame
982 '((name . "RefTeX TOC Frame")
983 (height . 20) (width . 60)
984 (unsplittable . t)
985 (minibuffer . nil)
986 (default-toolbar-visible-p . nil)
987 (menubar-visible-p . nil)
988 (horizontal-scrollbar-visible-p . nil))))
989 (select-frame current-toc-frame)
990 (switch-to-buffer "*toc*")
991 (select-frame current-frame)
992 (if (fboundp 'focus-frame) (focus-frame current-frame)
993 (if (fboundp 'x-focus-frame) (x-focus-frame current-frame)))
994 (select-window current-window)
995 (when (eq reftex-auto-recenter-toc 'frame)
996 (unless reftex-toc-auto-recenter-timer
997 (reftex-toggle-auto-toc-recenter))
998 (add-hook 'delete-frame-hook 'reftex-toc-delete-frame-hook)))))
999
1000 (defun reftex-toc-delete-frame-hook (frame)
1001 (if (and reftex-toc-auto-recenter-timer
1002 (reftex-toc-dframe-p frame))
1003 (progn
1004 (reftex-toggle-auto-toc-recenter))))
1005
1006 ;; Table of Contents map
1007 (define-key reftex-toc-map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
1008 'reftex-toc-mouse-goto-line-and-hide)
1009
1010 (substitute-key-definition
1011 'next-line 'reftex-toc-next reftex-toc-map global-map)
1012 (substitute-key-definition
1013 'previous-line 'reftex-toc-previous reftex-toc-map global-map)
1014
1015 (loop for x in
1016 '(("n" . reftex-toc-next)
1017 ("p" . reftex-toc-previous)
1018 ("?" . reftex-toc-show-help)
1019 (" " . reftex-toc-view-line)
1020 ("\C-m" . reftex-toc-goto-line-and-hide)
1021 ("\C-i" . reftex-toc-goto-line)
1022 ("\C-c>" . reftex-toc-display-index)
1023 ("r" . reftex-toc-rescan)
1024 ("R" . reftex-toc-Rescan)
1025 ("g" . revert-buffer)
1026 ("q" . reftex-toc-quit);
1027 ("k" . reftex-toc-quit-and-kill)
1028 ("f" . reftex-toc-toggle-follow);
1029 ("a" . reftex-toggle-auto-toc-recenter)
1030 ("d" . reftex-toc-toggle-dedicated-frame)
1031 ("F" . reftex-toc-toggle-file-boundary)
1032 ("i" . reftex-toc-toggle-index)
1033 ("l" . reftex-toc-toggle-labels)
1034 ("t" . reftex-toc-max-level)
1035 ("c" . reftex-toc-toggle-context)
1036 ; ("%" . reftex-toc-toggle-commented)
1037 ("\M-%" . reftex-toc-rename-label)
1038 ("x" . reftex-toc-external)
1039 ("z" . reftex-toc-jump)
1040 ("." . reftex-toc-show-calling-point)
1041 ("\C-c\C-n" . reftex-toc-next-heading)
1042 ("\C-c\C-p" . reftex-toc-previous-heading)
1043 (">" . reftex-toc-demote)
1044 ("<" . reftex-toc-promote))
1045 do (define-key reftex-toc-map (car x) (cdr x)))
1046
1047 (loop for key across "0123456789" do
1048 (define-key reftex-toc-map (vector (list key)) 'digit-argument))
1049 (define-key reftex-toc-map "-" 'negative-argument)
1050
1051 (easy-menu-define
1052 reftex-toc-menu reftex-toc-map
1053 "Menu for Table of Contents buffer"
1054 '("TOC"
1055 ["Show Location" reftex-toc-view-line t]
1056 ["Go To Location" reftex-toc-goto-line t]
1057 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t]
1058 ["Show Calling Point" reftex-toc-show-calling-point t]
1059 ["Quit" reftex-toc-quit t]
1060 "--"
1061 ("Edit"
1062 ["Promote" reftex-toc-promote t]
1063 ["Demote" reftex-toc-demote t]
1064 ["Rename Label" reftex-toc-rename-label t])
1065 "--"
1066 ["Index" reftex-toc-display-index t]
1067 ["External Document TOC " reftex-toc-external t]
1068 "--"
1069 ("Update"
1070 ["Rebuilt *toc* Buffer" revert-buffer t]
1071 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans]
1072 ["Rescan Entire Document" reftex-toc-Rescan t])
1073 ("Options"
1074 "TOC Items"
1075 ["File Boundaries" reftex-toc-toggle-file-boundary :style toggle
1076 :selected reftex-toc-include-file-boundaries]
1077 ["Labels" reftex-toc-toggle-labels :style toggle
1078 :selected reftex-toc-include-labels]
1079 ["Index Entries" reftex-toc-toggle-index :style toggle
1080 :selected reftex-toc-include-index-entries]
1081 ["Context" reftex-toc-toggle-context :style toggle
1082 :selected reftex-toc-include-context]
1083 "--"
1084 ["Follow Mode" reftex-toc-toggle-follow :style toggle
1085 :selected reftex-toc-follow-mode]
1086 ["Auto Recenter" reftex-toggle-auto-toc-recenter :style toggle
1087 :selected reftex-toc-auto-recenter-timer]
1088 ["Dedicated Frame" reftex-toc-toggle-dedicated-frame t])
1089 "--"
1090 ["Help" reftex-toc-show-help t]))
1091
1092
1093 ;;; arch-tag: 92400ce2-0b86-4c89-a606-4ed71acea17e
1094 ;;; reftex-toc.el ends here