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