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