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