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