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