Trailing whitespace deleted.
[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 Free Software Foundation, Inc.
3
4 ;; Author: Carsten Dominik <dominik@science.uva.nl>
5 ;; Version: 4.18
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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
38 (defun reftex-toc-mode ()
39 "Major mode for managing Table of Contents for LaTeX files.
40 This buffer was created with RefTeX.
41 Press `?' for a summary of important key bindings.
42
43 Here are all local bindings.
44
45 \\{reftex-toc-map}"
46 (interactive)
47 (kill-all-local-variables)
48 (setq major-mode 'reftex-toc-mode
49 mode-name "TOC")
50 (use-local-map reftex-toc-map)
51 (set (make-local-variable 'revert-buffer-function) 'reftex-toc-revert)
52 (set (make-local-variable 'reftex-toc-include-labels-indicator) "")
53 (set (make-local-variable 'reftex-toc-max-level-indicator)
54 (if (= reftex-toc-max-level 100)
55 "ALL"
56 (int-to-string reftex-toc-max-level)))
57 (setq mode-line-format
58 (list "---- " 'mode-line-buffer-identification
59 " " 'global-mode-string " (" mode-name ")"
60 " L<" 'reftex-toc-include-labels-indicator ">"
61 " I<" 'reftex-toc-include-index-indicator ">"
62 " T<" 'reftex-toc-max-level-indicator ">"
63 " -%-"))
64 (setq truncate-lines t)
65 (when (featurep 'xemacs)
66 ;; XEmacs needs the call to make-local-hook
67 (make-local-hook 'post-command-hook)
68 (make-local-hook 'pre-command-hook))
69 (make-local-variable 'reftex-last-follow-point)
70 (add-hook 'post-command-hook 'reftex-toc-post-command-hook nil t)
71 (add-hook 'pre-command-hook 'reftex-toc-pre-command-hook nil t)
72 (easy-menu-add reftex-toc-menu reftex-toc-map)
73 (run-hooks 'reftex-toc-mode-hook))
74
75 (defvar reftex-last-toc-file nil
76 "Stores the file name from which `reftex-toc' was called. For redo command.")
77
78 (defvar reftex-last-window-height nil)
79 (defvar reftex-last-window-width nil)
80 (defvar reftex-toc-include-labels-indicator nil)
81 (defvar reftex-toc-include-index-indicator nil)
82 (defvar reftex-toc-max-level-indicator nil)
83
84 (defvar reftex-toc-return-marker (make-marker)
85 "Marker which makes it possible to return from toc to old position.")
86
87 (defconst reftex-toc-help
88 " AVAILABLE KEYS IN TOC BUFFER
89 ============================
90 n / p next-line / previous-line
91 SPC Show the corresponding location of the LaTeX document.
92 TAB Goto the location and keep the *toc* window.
93 RET Goto the location and hide the *toc* window (also on mouse-2).
94 C-c > Display Index. With prefix arg, restrict index to current section.
95 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
96 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
97 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
98 f / a / g Toggle follow mode / toggle auto recenter / Refresh *toc* buffer.
99 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
100 . In other window, show position from where `reftex-toc' was called.
101 x Switch to TOC of external document (with LaTeX package `xr').
102 z Jump to a specific section (e.g. '3 z' goes to section 3")
103
104 (defun reftex-toc (&optional rebuild)
105 "Show the table of contents for the current document.
106 When called with a raw C-u prefix, rescan the document first."
107
108 (interactive)
109
110 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
111 current-prefix-arg)
112 (reftex-erase-buffer "*toc*"))
113
114 (setq reftex-last-toc-file (buffer-file-name))
115 (setq reftex-last-toc-master (reftex-TeX-master-file))
116
117 (set-marker reftex-toc-return-marker (point))
118
119 ;; If follow mode is active, arrange to delay it one command
120 (if reftex-toc-follow-mode
121 (setq reftex-toc-follow-mode 1))
122
123 (and reftex-toc-include-index-entries
124 (reftex-ensure-index-support))
125 (or reftex-support-index
126 (setq reftex-toc-include-index-entries nil))
127
128 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
129 (reftex-access-scan-info current-prefix-arg)
130
131 (let* ((this-buf (current-buffer))
132 (docstruct-symbol reftex-docstruct-symbol)
133 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
134 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
135 (here-I-am (if (boundp 'reftex-rebuilding-toc)
136 (get 'reftex-toc :reftex-data)
137 (car (reftex-where-am-I))))
138 offset)
139
140 (if (get-buffer-window "*toc*")
141 (select-window (get-buffer-window "*toc*"))
142 (when (or (not reftex-toc-keep-other-windows)
143 (< (window-height) (* 2 window-min-height)))
144 (delete-other-windows))
145
146 (setq reftex-last-window-width (window-width)
147 reftex-last-window-height (window-height)) ; remember
148 (if reftex-toc-split-windows-horizontally
149 (split-window-horizontally
150 (floor (* (frame-width) reftex-toc-split-windows-horizontally-fraction)))
151 (split-window))
152
153 (let ((default-major-mode 'reftex-toc-mode))
154 (switch-to-buffer "*toc*")))
155
156 (or (eq major-mode 'reftex-toc-mode) (reftex-toc-mode))
157 (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol)
158 (setq reftex-toc-include-labels-indicator
159 (if (eq reftex-toc-include-labels t)
160 "ALL"
161 reftex-toc-include-labels))
162 (setq reftex-toc-include-index-indicator
163 (if (eq reftex-toc-include-index-entries t)
164 "ALL"
165 reftex-toc-include-index-entries))
166
167 (cond
168 ((= (buffer-size) 0)
169 ;; buffer is empty - fill it with the table of contents
170 (message "Building *toc* buffer...")
171
172 (setq buffer-read-only nil)
173 (insert (format
174 "TABLE-OF-CONTENTS on %s
175 SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
176 ------------------------------------------------------------------------------
177 " (abbreviate-file-name reftex-last-toc-master)))
178
179 (if (reftex-use-fonts)
180 (put-text-property 1 (point) 'face reftex-toc-header-face))
181 (put-text-property 1 (point) 'intangible t)
182 (put-text-property 1 2 'xr-alist xr-alist)
183
184 (setq offset
185 (reftex-insert-docstruct
186 this-buf
187 t ; include toc
188 reftex-toc-include-labels
189 reftex-toc-include-index-entries
190 reftex-toc-include-file-boundaries
191 reftex-toc-include-context
192 nil ; counter
193 nil ; commented
194 here-I-am
195 "" ; xr-prefix
196 t ; a toc buffer
197 ))
198
199 (run-hooks 'reftex-display-copied-context-hook)
200 (message "Building *toc* buffer...done.")
201 (setq buffer-read-only t))
202 (t
203 ;; Only compute the offset
204 (setq offset
205 (or (reftex-get-offset this-buf here-I-am
206 (if reftex-toc-include-labels " " nil)
207 t
208 reftex-toc-include-index-entries
209 reftex-toc-include-file-boundaries)
210 (reftex-last-assoc-before-elt
211 'toc here-I-am
212 (symbol-value reftex-docstruct-symbol))))
213 (put 'reftex-toc :reftex-line 3)
214 (goto-line 3)
215 (beginning-of-line)))
216
217 ;; Find the correct starting point
218 (reftex-find-start-point (point) offset (get 'reftex-toc :reftex-line))
219 (setq reftex-last-follow-point (point))))
220
221 (defun reftex-toc-recenter (&optional arg)
222 "Display the TOC window and highlight line corresponding to current position."
223 (interactive "P")
224 (let ((buf (current-buffer)))
225 (reftex-toc arg)
226 (if (= (count-lines 1 (point)) 2)
227 (let ((current-prefix-arg nil))
228 (select-window (get-buffer-window buf))
229 (reftex-toc nil)))
230 (and (> (point) 1)
231 (not (get-text-property (point) 'intangible))
232 (memq reftex-highlight-selection '(cursor both))
233 (reftex-highlight 2
234 (or (previous-single-property-change
235 (min (point-max) (1+ (point))) :data)
236 (point-min))
237 (or (next-single-property-change (point) :data)
238 (point-max))))
239 (select-window (get-buffer-window buf))))
240
241 (defun reftex-toc-pre-command-hook ()
242 ;; used as pre command hook in *toc* buffer
243 (reftex-unhighlight 0)
244 ;; (reftex-unhighlight 1) ;; remove highlight on leaving buffer.
245 )
246
247 (defun reftex-toc-post-command-hook ()
248 ;; used in the post-command-hook for the *toc* buffer
249 (when (get-text-property (point) :data)
250 (put 'reftex-toc :reftex-data (get-text-property (point) :data))
251 (and (> (point) 1)
252 (not (get-text-property (point) 'intangible))
253 (memq reftex-highlight-selection '(cursor both))
254 (reftex-highlight 2
255 (or (previous-single-property-change (1+ (point)) :data)
256 (point-min))
257 (or (next-single-property-change (point) :data)
258 (point-max)))))
259 (if (integerp reftex-toc-follow-mode)
260 ;; remove delayed action
261 (setq reftex-toc-follow-mode t)
262 (and reftex-toc-follow-mode
263 (not (equal reftex-last-follow-point (point)))
264 ;; show context in other window
265 (setq reftex-last-follow-point (point))
266 (condition-case nil
267 (reftex-toc-visit-location nil (not reftex-revisit-to-follow))
268 (error t)))))
269
270 (defun reftex-re-enlarge ()
271 ;; Enlarge windiw to a remembered size
272 (if reftex-toc-split-windows-horizontally
273 (enlarge-window-horizontally
274 (max 0 (- (or reftex-last-window-width (window-width))
275 (window-width))))
276 (enlarge-window
277 (max 0 (- (or reftex-last-window-height (window-height))
278 (window-height))))))
279
280 (defun reftex-toc-show-help ()
281 "Show a summary of special key bindings."
282 (interactive)
283 (with-output-to-temp-buffer "*RefTeX Help*"
284 (princ reftex-toc-help))
285 (reftex-enlarge-to-fit "*RefTeX Help*" t)
286 ;; If follow mode is active, arrange to delay it one command
287 (if reftex-toc-follow-mode
288 (setq reftex-toc-follow-mode 1)))
289
290 (defun reftex-toc-next (&optional arg)
291 "Move to next selectable item."
292 (interactive "p")
293 (setq reftex-callback-fwd t)
294 (or (eobp) (forward-char 1))
295 (goto-char (or (next-single-property-change (point) :data)
296 (point))))
297 (defun reftex-toc-previous (&optional arg)
298 "Move to previous selectable item."
299 (interactive "p")
300 (setq reftex-callback-fwd nil)
301 (goto-char (or (previous-single-property-change (point) :data)
302 (point))))
303 (defun reftex-toc-next-heading (&optional arg)
304 "Move to next table of contentes line."
305 (interactive "p")
306 (end-of-line)
307 (re-search-forward "^ " nil t arg)
308 (beginning-of-line))
309 (defun reftex-toc-previous-heading (&optional arg)
310 "Move to previous table of contentes line."
311 (interactive "p")
312 (re-search-backward "^ " nil t arg))
313 (defun reftex-toc-toggle-follow ()
314 "Toggle follow (other window follows with context)."
315 (interactive)
316 (setq reftex-last-follow-point -1)
317 (setq reftex-toc-follow-mode (not reftex-toc-follow-mode)))
318 (defun reftex-toc-toggle-file-boundary ()
319 "Toggle inclusion of file boundaries in *toc* buffer."
320 (interactive)
321 (setq reftex-toc-include-file-boundaries
322 (not reftex-toc-include-file-boundaries))
323 (reftex-toc-revert))
324 (defun reftex-toc-toggle-labels (arg)
325 "Toggle inclusion of labels in *toc* buffer.
326 With prefix ARG, prompt for a label type and include only labels of
327 that specific type."
328 (interactive "P")
329 (setq reftex-toc-include-labels
330 (if arg (reftex-query-label-type)
331 (not reftex-toc-include-labels)))
332 (reftex-toc-revert))
333 (defun reftex-toc-toggle-index (arg)
334 "Toggle inclusion of index in *toc* buffer.
335 With prefix arg, prompt for an index tag and include only entries of that
336 specific index."
337 (interactive "P")
338 (setq reftex-toc-include-index-entries
339 (if arg (reftex-index-select-tag)
340 (not reftex-toc-include-index-entries)))
341 (reftex-toc-revert))
342 (defun reftex-toc-toggle-context ()
343 "Toggle inclusion of label context in *toc* buffer.
344 Label context is only displayed when the labels are there as well."
345 (interactive)
346 (setq reftex-toc-include-context (not reftex-toc-include-context))
347 (reftex-toc-revert))
348 (defun reftex-toc-max-level (arg)
349 "Set the maximum level of toc lines in this buffer to value of prefix ARG.
350 When no prefix is given, set the max level to a large number, so that all
351 levels are shown. For eaxample, to set the level to 3, type `3 m'."
352 (interactive "P")
353 (setq reftex-toc-max-level (if arg
354 (prefix-numeric-value arg)
355 100))
356 (setq reftex-toc-max-level-indicator
357 (if arg (int-to-string reftex-toc-max-level) "ALL"))
358 (reftex-toc-revert))
359 (defun reftex-toc-view-line ()
360 "View document location in other window."
361 (interactive)
362 (reftex-toc-visit-location))
363 (defun reftex-toc-goto-line-and-hide ()
364 "Go to document location in other window. Hide the *toc* window."
365 (interactive)
366 (reftex-toc-visit-location 'hide))
367 (defun reftex-toc-goto-line ()
368 "Go to document location in other window. *toc* window stays."
369 (interactive)
370 (reftex-toc-visit-location t))
371 (defun reftex-toc-mouse-goto-line-and-hide (ev)
372 "Go to document location in other window. Hide the *toc* window."
373 (interactive "e")
374 (mouse-set-point ev)
375 (reftex-toc-visit-location 'hide))
376 (defun reftex-toc-show-calling-point ()
377 "Show point where reftex-toc was called from."
378 (interactive)
379 (let ((this-window (selected-window)))
380 (unwind-protect
381 (progn
382 (switch-to-buffer-other-window
383 (marker-buffer reftex-toc-return-marker))
384 (goto-char (marker-position reftex-toc-return-marker))
385 (recenter '(4)))
386 (select-window this-window))))
387 (defun reftex-toc-quit ()
388 "Hide the *toc* window and do not move point."
389 (interactive)
390 (or (one-window-p) (delete-window))
391 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
392 (reftex-re-enlarge)
393 (goto-char (or (marker-position reftex-toc-return-marker) (point))))
394 (defun reftex-toc-quit-and-kill ()
395 "Kill the *toc* buffer."
396 (interactive)
397 (kill-buffer "*toc*")
398 (or (one-window-p) (delete-window))
399 (switch-to-buffer (marker-buffer reftex-toc-return-marker))
400 (reftex-re-enlarge)
401 (goto-char (marker-position reftex-toc-return-marker)))
402 (defun reftex-toc-display-index (&optional arg)
403 "Display the index buffer for the current document.
404 This works just like `reftex-display-index' from a LaTeX buffer.
405 With prefix arg 1, restrict index to the section at point."
406 (interactive "P")
407 (let ((data (get-text-property (point) :data))
408 (docstruct (symbol-value reftex-docstruct-symbol))
409 bor eor restr)
410 (when (equal arg 2)
411 (setq bor (reftex-last-assoc-before-elt 'toc data docstruct)
412 eor (assoc 'toc (cdr (memq bor docstruct)))
413 restr (list (nth 6 bor) bor eor)))
414 (reftex-toc-goto-line)
415 (reftex-display-index (if restr nil arg) restr)))
416 (defun reftex-toc-rescan (&rest ignore)
417 "Regenerate the *toc* buffer by reparsing file of section at point."
418 (interactive)
419 (if (and reftex-enable-partial-scans
420 (null current-prefix-arg))
421 (let* ((data (get-text-property (point) :data))
422 (what (car data))
423 (file (cond ((eq what 'toc) (nth 3 data))
424 ((memq what '(eof bof file-error)) (nth 1 data))
425 ((stringp what) (nth 3 data))
426 ((eq what 'index) (nth 3 data))))
427 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
428 (if (not file)
429 (error "Don't know which file to rescan. Try `C-u r'")
430 (put 'reftex-toc :reftex-line line)
431 (switch-to-buffer-other-window
432 (reftex-get-file-buffer-force file))
433 (setq current-prefix-arg '(4))
434 (let ((reftex-rebuilding-toc t))
435 (reftex-toc))))
436 (reftex-toc-Rescan))
437 (reftex-kill-temporary-buffers))
438 (defun reftex-toc-Rescan (&rest ignore)
439 "Regenerate the *toc* buffer by reparsing the entire document."
440 (interactive)
441 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
442 (put 'reftex-toc :reftex-line line))
443 (switch-to-buffer-other-window
444 (reftex-get-file-buffer-force reftex-last-toc-file))
445 (setq current-prefix-arg '(16))
446 (let ((reftex-rebuilding-toc t))
447 (reftex-toc)))
448 (defun reftex-toc-revert (&rest ignore)
449 "Regenerate the *toc* from the internal lists."
450 (interactive)
451 (switch-to-buffer-other-window
452 (reftex-get-file-buffer-force reftex-last-toc-file))
453 (reftex-erase-buffer "*toc*")
454 (setq current-prefix-arg nil)
455 (let ((reftex-rebuilding-toc t))
456 (reftex-toc t)))
457 (defun reftex-toc-external (&rest ignore)
458 "Switch to table of contents of an external document."
459 (interactive)
460 (let* ((old-buf (current-buffer))
461 (xr-alist (get-text-property 1 'xr-alist))
462 (xr-index (reftex-select-external-document
463 xr-alist 0)))
464 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
465 (cdr (nth xr-index xr-alist)))
466 (error "Cannot switch document")))
467 (reftex-toc)
468 (if (equal old-buf (current-buffer))
469 (message "")
470 (message "Switched document"))))
471
472 (defun reftex-toc-jump (arg)
473 "Jump to a specific section. E.g. '3 z' jumps to section 3.
474 Useful for large TOC's."
475 (interactive "P")
476 (goto-char (point-min))
477 (re-search-forward
478 (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
479 nil t)
480 (beginning-of-line))
481
482 (defun reftex-toc-visit-location (&optional final no-revisit)
483 ;; Visit the tex file corresponding to the toc entry on the current line.
484 ;; If FINAL is t, stay there
485 ;; If FINAL is 'hide, hide the *toc* window.
486 ;; Otherwise, move cursor back into *toc* window.
487 ;; NO-REVISIT means don't visit files, just use live biffers.
488 ;; This function is pretty clever about finding back a section heading,
489 ;; even if the buffer is not live, or things like outline, x-symbol etc.
490 ;; have been active.
491
492 (let* ((toc (get-text-property (point) :data))
493 (toc-window (selected-window))
494 show-window show-buffer match)
495
496 (unless toc (error "Don't know which toc line to visit"))
497
498 (cond
499
500 ((eq (car toc) 'toc)
501 ;; a toc entry
502 (setq match (reftex-toc-find-section toc no-revisit)))
503
504 ((eq (car toc) 'index)
505 ;; an index entry
506 (setq match (reftex-index-show-entry toc no-revisit)))
507
508 ((memq (car toc) '(bof eof))
509 ;; A file entry
510 (setq match
511 (let ((where (car toc))
512 (file (nth 1 toc)))
513 (if (or (not no-revisit) (reftex-get-buffer-visiting file))
514 (progn
515 (switch-to-buffer-other-window
516 (reftex-get-file-buffer-force file nil))
517 (goto-char (if (eq where 'bof) (point-min) (point-max))))
518 (message reftex-no-follow-message) nil))))
519
520 ((stringp (car toc))
521 ;; a label
522 (setq match (reftex-show-label-location toc reftex-callback-fwd
523 no-revisit t))))
524
525 (setq show-window (selected-window)
526 show-buffer (current-buffer))
527
528 (unless match
529 (select-window toc-window)
530 (error "Cannot find location"))
531
532 (select-window toc-window)
533
534 ;; use the `final' parameter to decide what to do next
535 (cond
536 ((eq final t)
537 (reftex-unhighlight 0)
538 (select-window show-window))
539 ((eq final 'hide)
540 (reftex-unhighlight 0)
541 (or (one-window-p) (delete-window))
542 (switch-to-buffer show-buffer)
543 (reftex-re-enlarge))
544 (t nil))))
545
546 (defun reftex-toc-find-section (toc &optional no-revisit)
547 (let* ((file (nth 3 toc))
548 (marker (nth 4 toc))
549 (level (nth 5 toc))
550 (literal (nth 7 toc))
551 (emergency-point (nth 8 toc))
552 (match
553 (cond
554 ((and (markerp marker) (marker-buffer marker))
555 ;; Buffer is still live and we have the marker. Should be easy.
556 (switch-to-buffer-other-window (marker-buffer marker))
557 (goto-char (marker-position marker))
558 (or (looking-at (regexp-quote literal))
559 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal))
560 (looking-at (reftex-make-desperate-section-regexp literal))
561 (looking-at (concat "\\\\"
562 (regexp-quote
563 (car
564 (rassq level
565 reftex-section-levels-all)))
566 "[[{]?"))))
567 ((or (not no-revisit)
568 (reftex-get-buffer-visiting file))
569 ;; Marker is lost. Use the backup method.
570 (switch-to-buffer-other-window
571 (reftex-get-file-buffer-force file nil))
572 (goto-char (or emergency-point (point-min)))
573 (or (looking-at (regexp-quote literal))
574 (let ((len (length literal)))
575 (or (reftex-nearest-match (regexp-quote literal) len)
576 (reftex-nearest-match
577 (reftex-make-regexp-allow-for-ctrl-m literal) len)
578 (reftex-nearest-match
579 (reftex-make-desperate-section-regexp literal) len)))))
580 (t (message reftex-no-follow-message) nil))))
581 (when match
582 (goto-char (match-beginning 0))
583 (if (not (= (point) (point-max))) (recenter 1))
584 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
585 match))
586
587 (defun reftex-make-desperate-section-regexp (old)
588 ;; Return a regexp which will still match a section statement even if
589 ;; x-symbol or isotex or the like have been at work in the mean time.
590 (let* ((n (1+ (string-match "[[{]" old)))
591 (new (regexp-quote (substring old 0 (1+ (string-match "[[{]" old)))))
592 (old (substring old n)))
593 (while (string-match
594 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
595 old)
596 (if (match-beginning 1)
597 (setq new (concat new "[^\n\r]*[\n\r]"))
598 (setq new (concat new "[^\n\r]*" (match-string 3 old))))
599 (setq old (substring old (match-end 0))))
600 new))
601
602
603 (defun reftex-recenter-toc-when-idle ()
604 (and (> (buffer-size) 5)
605 reftex-mode
606 (not (active-minibuffer-window))
607 (fboundp 'reftex-toc-mode)
608 (get-buffer-window "*toc*")
609 (string= reftex-last-toc-master (reftex-TeX-master-file))
610 (reftex-toc-recenter)))
611
612 (defun reftex-toggle-auto-toc-recenter ()
613 "Toggle the automatic recentering of the toc window.
614 When active, leaving point idle will make the toc window jump to the correct
615 section."
616 (interactive)
617 (if reftex-toc-auto-recenter-timer
618 (progn
619 (if (featurep 'xemacs)
620 (delete-itimer reftex-toc-auto-recenter-timer)
621 (cancel-timer reftex-toc-auto-recenter-timer))
622 (setq reftex-toc-auto-recenter-timer nil)
623 (message "Automatic recentering of toc buffer was turned off"))
624 (setq reftex-toc-auto-recenter-timer
625 (if (featurep 'xemacs)
626 (start-itimer "RefTeX Idle Timer for recenter"
627 'reftex-recenter-toc-when-idle
628 reftex-idle-time reftex-idle-time t)
629 (run-with-idle-timer
630 reftex-idle-time t 'reftex-recenter-toc-when-idle)))
631 (message "Automatic recentering of toc window was turned on")))
632
633
634 ;; Table of Contents map
635 (define-key reftex-toc-map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
636 'reftex-toc-mouse-goto-line-and-hide)
637
638 (substitute-key-definition
639 'next-line 'reftex-toc-next reftex-toc-map global-map)
640 (substitute-key-definition
641 'previous-line 'reftex-toc-previous reftex-toc-map global-map)
642
643 (loop for x in
644 '(("n" . reftex-toc-next)
645 ("p" . reftex-toc-previous)
646 ("?" . reftex-toc-show-help)
647 (" " . reftex-toc-view-line)
648 ("\C-m" . reftex-toc-goto-line-and-hide)
649 ("\C-i" . reftex-toc-goto-line)
650 ("\C-c>". reftex-toc-display-index)
651 ("r" . reftex-toc-rescan)
652 ("R" . reftex-toc-Rescan)
653 ("g" . revert-buffer)
654 ("q" . reftex-toc-quit)
655 ("k" . reftex-toc-quit-and-kill)
656 ("f" . reftex-toc-toggle-follow)
657 ("a" . reftex-toggle-auto-toc-recenter)
658 ("F" . reftex-toc-toggle-file-boundary)
659 ("i" . reftex-toc-toggle-index)
660 ("l" . reftex-toc-toggle-labels)
661 ("t" . reftex-toc-max-level)
662 ("c" . reftex-toc-toggle-context)
663 ("%" . reftex-toc-toggle-commented)
664 ("x" . reftex-toc-external)
665 ("z" . reftex-toc-jump)
666 ("." . reftex-toc-show-calling-point)
667 ("\C-c\C-n" . reftex-toc-next-heading)
668 ("\C-c\C-p" . reftex-toc-previous-heading))
669 do (define-key reftex-toc-map (car x) (cdr x)))
670
671 (loop for key across "0123456789" do
672 (define-key reftex-toc-map (vector (list key)) 'digit-argument))
673 (define-key reftex-toc-map "-" 'negative-argument)
674
675 (easy-menu-define
676 reftex-toc-menu reftex-toc-map
677 "Menu for Table of Contents buffer"
678 '("TOC"
679 ["Show Location" reftex-toc-view-line t]
680 ["Go To Location" reftex-toc-goto-line t]
681 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t]
682 ["Index" reftex-toc-display-index t]
683 ["Quit" reftex-toc-quit t]
684 "--"
685 ["External Document TOC " reftex-toc-external t]
686 "--"
687 ("Update"
688 ["Rebuilt *toc* Buffer" revert-buffer t]
689 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans]
690 ["Rescan Entire Document" reftex-toc-Rescan t])
691 ("Options"
692 "TOC Items"
693 ["File Boundaries" reftex-toc-toggle-file-boundary :style toggle
694 :selected reftex-toc-include-file-boundaries]
695 ["Labels" reftex-toc-toggle-labels :style toggle
696 :selected reftex-toc-include-labels]
697 ["Index Entries" reftex-toc-toggle-index :style toggle
698 :selected reftex-toc-include-index-entries]
699 ["Context" reftex-toc-toggle-context :style toggle
700 :selected reftex-toc-include-context]
701 "--"
702 ["Follow Mode" reftex-toc-toggle-follow :style toggle
703 :selected reftex-toc-follow-mode])
704 "--"
705 ["Help" reftex-toc-show-help t]))
706
707
708 ;;; reftex-toc.el ends here