1 ;;; reftex-toc.el --- RefTeX's table of contents mode
2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <dominik@science.uva.nl>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
28 (eval-when-compile (require 'cl
))
33 (defvar reftex-toc-map
(make-sparse-keymap)
34 "Keymap used for *toc* buffer.")
36 (defvar reftex-toc-menu
)
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.
43 Here are all local bindings.
47 (kill-all-local-variables)
48 (setq major-mode
'reftex-toc-mode
50 (use-local-map reftex-toc-map
)
51 (set (make-local-variable 'transient-mark-mode
) t
)
52 (set (make-local-variable 'zmacs-regions
) t
)
53 (set (make-local-variable 'revert-buffer-function
) 'reftex-toc-revert
)
54 (set (make-local-variable 'reftex-toc-include-labels-indicator
) "")
55 (set (make-local-variable 'reftex-toc-max-level-indicator
)
56 (if (= reftex-toc-max-level
100)
58 (int-to-string reftex-toc-max-level
)))
59 (setq mode-line-format
60 (list "---- " 'mode-line-buffer-identification
61 " " 'global-mode-string
" (" mode-name
")"
62 " L<" 'reftex-toc-include-labels-indicator
">"
63 " I<" 'reftex-toc-include-index-indicator
">"
64 " T<" 'reftex-toc-max-level-indicator
">"
66 (setq truncate-lines t
)
67 (when (featurep 'xemacs
)
68 ;; XEmacs needs the call to make-local-hook
69 (make-local-hook 'post-command-hook
)
70 (make-local-hook 'pre-command-hook
))
71 (make-local-variable 'reftex-last-follow-point
)
72 (add-hook 'post-command-hook
'reftex-toc-post-command-hook nil t
)
73 (add-hook 'pre-command-hook
'reftex-toc-pre-command-hook nil t
)
74 (easy-menu-add reftex-toc-menu reftex-toc-map
)
75 (run-hooks 'reftex-toc-mode-hook
))
77 (defvar reftex-last-toc-file nil
78 "Stores the file name from which `reftex-toc' was called. For redo command.")
80 (defvar reftex-last-window-height nil
)
81 (defvar reftex-last-window-width nil
)
82 (defvar reftex-toc-include-labels-indicator nil
)
83 (defvar reftex-toc-include-index-indicator nil
)
84 (defvar reftex-toc-max-level-indicator nil
)
86 (defvar reftex-toc-return-marker
(make-marker)
87 "Marker which makes it possible to return from toc to old position.")
89 (defconst reftex-toc-help
90 " AVAILABLE KEYS IN TOC BUFFER
91 ============================
92 n / p next-line / previous-line
93 SPC Show the corresponding location of the LaTeX document.
94 TAB Goto the location and keep the *toc* window.
95 RET Goto the location and hide the *toc* window (also on mouse-2).
96 < / > Promote / Demote section, or all sections in region.
97 C-c > Display Index. With prefix arg, restrict index to current section.
98 q / k Hide/Kill *toc* buffer, return to position of reftex-toc command.
99 l i c F Toggle display of [l]abels, [i]ndex, [c]ontext, [F]ile borders.
100 t Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
101 f / g Toggle follow mode / Refresh *toc* buffer.
102 a / d Toggle auto recenter / Toggle dedicated frame
103 r / C-u r Reparse the LaTeX document / Reparse entire LaTeX document.
104 . In other window, show position from where `reftex-toc' was called.
105 M-% Global search and replace to rename label at point.
106 x Switch to TOC of external document (with LaTeX package `xr').
107 z Jump to a specific section (e.g. '3 z' goes to section 3).")
109 (defun reftex-toc (&optional rebuild reuse
)
110 "Show the table of contents for the current document.
111 When called with a raw C-u prefix, rescan the document first."
113 ;; The REUSE argument means, search all visible frames for a window
114 ;; displaying the toc window. If yes, reuse this window.
118 (if (or (not (string= reftex-last-toc-master
(reftex-TeX-master-file)))
120 (reftex-erase-buffer "*toc*"))
122 (setq reftex-last-toc-file
(buffer-file-name))
123 (setq reftex-last-toc-master
(reftex-TeX-master-file))
125 (set-marker reftex-toc-return-marker
(point))
127 ;; If follow mode is active, arrange to delay it one command
128 (if reftex-toc-follow-mode
129 (setq reftex-toc-follow-mode
1))
131 (and reftex-toc-include-index-entries
132 (reftex-ensure-index-support))
133 (or reftex-support-index
134 (setq reftex-toc-include-index-entries nil
))
136 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
137 (reftex-access-scan-info current-prefix-arg
)
139 (let* ((this-buf (current-buffer))
140 (docstruct-symbol reftex-docstruct-symbol
)
141 (xr-data (assq 'xr
(symbol-value reftex-docstruct-symbol
)))
142 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data
)))
143 (here-I-am (if (boundp 'reftex-rebuilding-toc
)
144 (get 'reftex-toc
:reftex-data
)
145 (car (reftex-where-am-I))))
146 (unsplittable (if (fboundp 'frame-property
)
147 (frame-property (selected-frame) 'unsplittable
)
148 (frame-parameter (selected-frame) 'unsplittable
)))
151 (if (setq toc-window
(get-buffer-window
153 (if reuse
'visible
)))
154 (select-window toc-window
)
155 (when (or (not reftex-toc-keep-other-windows
)
156 (< (window-height) (* 2 window-min-height
)))
157 (delete-other-windows))
159 (setq reftex-last-window-width
(window-width)
160 reftex-last-window-height
(window-height)) ; remember
163 (if reftex-toc-split-windows-horizontally
164 (split-window-horizontally
165 (floor (* (window-width)
166 reftex-toc-split-windows-fraction
)))
167 (split-window-vertically
168 (floor (* (window-height)
169 reftex-toc-split-windows-fraction
)))))
171 (let ((default-major-mode 'reftex-toc-mode
))
172 (switch-to-buffer "*toc*")))
174 (or (eq major-mode
'reftex-toc-mode
) (reftex-toc-mode))
175 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
176 (setq reftex-toc-include-labels-indicator
177 (if (eq reftex-toc-include-labels t
)
179 reftex-toc-include-labels
))
180 (setq reftex-toc-include-index-indicator
181 (if (eq reftex-toc-include-index-entries t
)
183 reftex-toc-include-index-entries
))
187 ;; buffer is empty - fill it with the table of contents
188 (message "Building *toc* buffer...")
190 (setq buffer-read-only nil
)
192 "TABLE-OF-CONTENTS on %s
193 SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
194 ------------------------------------------------------------------------------
195 " (abbreviate-file-name reftex-last-toc-master
)))
197 (if (reftex-use-fonts)
198 (put-text-property (point-min) (point) 'face reftex-toc-header-face
))
199 (put-text-property (point-min) (point) 'intangible t
)
200 (put-text-property (point-min) (1+ (point-min)) 'xr-alist xr-alist
)
203 (reftex-insert-docstruct
206 reftex-toc-include-labels
207 reftex-toc-include-index-entries
208 reftex-toc-include-file-boundaries
209 reftex-toc-include-context
217 (run-hooks 'reftex-display-copied-context-hook
)
218 (message "Building *toc* buffer...done.")
219 (setq buffer-read-only t
))
221 ;; Only compute the offset
223 (or (reftex-get-offset this-buf here-I-am
224 (if reftex-toc-include-labels
" " nil
)
226 reftex-toc-include-index-entries
227 reftex-toc-include-file-boundaries
)
228 (reftex-last-assoc-before-elt
230 (symbol-value reftex-docstruct-symbol
))))
231 (put 'reftex-toc
:reftex-line
3)
233 (beginning-of-line)))
235 ;; Find the correct starting point
236 (reftex-find-start-point (point) offset
(get 'reftex-toc
:reftex-line
))
237 (setq reftex-last-follow-point
(point))))
239 (defun reftex-toc-recenter (&optional arg
)
240 "Display the TOC window and highlight line corresponding to current position."
242 (let ((buf (current-buffer))
243 (frame (selected-frame)))
245 (if (= (count-lines 1 (point)) 2)
246 (let ((current-prefix-arg nil
))
247 (select-window (get-buffer-window buf frame
))
250 (not (get-text-property (point) 'intangible
))
251 (memq reftex-highlight-selection
'(cursor both
))
253 (or (previous-single-property-change
254 (min (point-max) (1+ (point))) :data
)
256 (or (next-single-property-change (point) :data
)
258 (select-window (get-buffer-window buf frame
))))
260 (defun reftex-toc-pre-command-hook ()
261 ;; used as pre command hook in *toc* buffer
262 (reftex-unhighlight 0)
265 (defun reftex-toc-post-command-hook ()
266 ;; used in the post-command-hook for the *toc* buffer
267 (when (get-text-property (point) :data
)
268 (put 'reftex-toc
:reftex-data
(get-text-property (point) :data
))
270 (not (get-text-property (point) 'intangible
))
271 (memq reftex-highlight-selection
'(cursor both
))
273 (or (previous-single-property-change (1+ (point)) :data
)
275 (or (next-single-property-change (point) :data
)
277 (if (integerp reftex-toc-follow-mode
)
278 ;; remove delayed action
279 (setq reftex-toc-follow-mode t
)
280 (and (not (reftex-toc-dframe-p))
281 reftex-toc-follow-mode
282 (not (equal reftex-last-follow-point
(point)))
283 ;; show context in other window
284 (setq reftex-last-follow-point
(point))
286 (reftex-toc-visit-location nil
(not reftex-revisit-to-follow
))
289 (defun reftex-re-enlarge ()
290 ;; Enlarge window to a remembered size.
291 (if reftex-toc-split-windows-horizontally
292 (enlarge-window-horizontally
293 (max 0 (- (or reftex-last-window-width
(window-width))
296 (max 0 (- (or reftex-last-window-height
(window-height))
299 (defun reftex-toc-dframe-p (&optional frame error
)
300 ;; Check if FRAME is the dedicated TOC frame.
301 ;; If yes, and ERROR is non-nil, throw an error.
302 (setq frame
(or frame
(selected-frame)))
304 (if (fboundp 'frame-property
)
305 (frame-property frame
'name
)
306 (frame-parameter frame
'name
))
307 "RefTeX TOC Frame")))
309 (error "This frame is view-only. Use `C-c =' to create toc window for commands."))
312 (defun reftex-toc-show-help ()
313 "Show a summary of special key bindings."
315 (reftex-toc-dframe-p nil
'error
)
316 (with-output-to-temp-buffer "*RefTeX Help*"
317 (princ reftex-toc-help
))
318 (reftex-enlarge-to-fit "*RefTeX Help*" t
)
319 ;; If follow mode is active, arrange to delay it one command
320 (if reftex-toc-follow-mode
321 (setq reftex-toc-follow-mode
1)))
323 (defun reftex-toc-next (&optional arg
)
324 "Move to next selectable item."
326 (if (boundp 'zmacs-region-stays
) (setq zmacs-region-stays t
))
327 (setq reftex-callback-fwd t
)
328 (or (eobp) (forward-char 1))
329 (goto-char (or (next-single-property-change (point) :data
)
331 (defun reftex-toc-previous (&optional arg
)
332 "Move to previous selectable item."
334 (if (boundp 'zmacs-region-stays
) (setq zmacs-region-stays t
))
335 (setq reftex-callback-fwd nil
)
336 (goto-char (or (previous-single-property-change (point) :data
)
338 (defun reftex-toc-next-heading (&optional arg
)
339 "Move to next table of contentes line."
341 (if (boundp 'zmacs-region-stays
) (setq zmacs-region-stays t
))
343 (re-search-forward "^ " nil t arg
)
345 (defun reftex-toc-previous-heading (&optional arg
)
346 "Move to previous table of contentes line."
348 (if (boundp 'zmacs-region-stays
) (setq zmacs-region-stays t
))
349 (re-search-backward "^ " nil t arg
))
350 (defun reftex-toc-toggle-follow ()
351 "Toggle follow (other window follows with context)."
353 (setq reftex-last-follow-point -
1)
354 (setq reftex-toc-follow-mode
(not reftex-toc-follow-mode
)))
355 (defun reftex-toc-toggle-file-boundary ()
356 "Toggle inclusion of file boundaries in *toc* buffer."
358 (setq reftex-toc-include-file-boundaries
359 (not reftex-toc-include-file-boundaries
))
361 (defun reftex-toc-toggle-labels (arg)
362 "Toggle inclusion of labels in *toc* buffer.
363 With prefix ARG, prompt for a label type and include only labels of
366 (setq reftex-toc-include-labels
367 (if arg
(reftex-query-label-type)
368 (not reftex-toc-include-labels
)))
370 (defun reftex-toc-toggle-index (arg)
371 "Toggle inclusion of index in *toc* buffer.
372 With prefix arg, prompt for an index tag and include only entries of that
375 (setq reftex-toc-include-index-entries
376 (if arg
(reftex-index-select-tag)
377 (not reftex-toc-include-index-entries
)))
379 (defun reftex-toc-toggle-context ()
380 "Toggle inclusion of label context in *toc* buffer.
381 Label context is only displayed when the labels are there as well."
383 (setq reftex-toc-include-context
(not reftex-toc-include-context
))
385 (defun reftex-toc-max-level (arg)
386 "Set the maximum level of toc lines in this buffer to value of prefix ARG.
387 When no prefix is given, set the max level to a large number, so that all
388 levels are shown. For eaxample, to set the level to 3, type `3 m'."
390 (setq reftex-toc-max-level
(if arg
391 (prefix-numeric-value arg
)
393 (setq reftex-toc-max-level-indicator
394 (if arg
(int-to-string reftex-toc-max-level
) "ALL"))
396 (defun reftex-toc-view-line ()
397 "View document location in other window."
399 (reftex-toc-dframe-p nil
'error
)
400 (reftex-toc-visit-location))
401 (defun reftex-toc-goto-line-and-hide ()
402 "Go to document location in other window. Hide the *toc* window."
404 (reftex-toc-dframe-p nil
'error
)
405 (reftex-toc-visit-location 'hide
))
406 (defun reftex-toc-goto-line ()
407 "Go to document location in other window. *toc* window stays."
409 (reftex-toc-dframe-p nil
'error
)
410 (reftex-toc-visit-location t
))
411 (defun reftex-toc-mouse-goto-line-and-hide (ev)
412 "Go to document location in other window. Hide the *toc* window."
415 (reftex-toc-dframe-p nil
'error
)
416 (reftex-toc-visit-location 'hide
))
417 (defun reftex-toc-show-calling-point ()
418 "Show point where reftex-toc was called from."
420 (reftex-toc-dframe-p nil
'error
)
421 (let ((this-window (selected-window)))
424 (switch-to-buffer-other-window
425 (marker-buffer reftex-toc-return-marker
))
426 (goto-char (marker-position reftex-toc-return-marker
))
428 (select-window this-window
))))
429 (defun reftex-toc-quit ()
430 "Hide the *toc* window and do not move point.
431 If the toc window is the only window on the dedicated TOC frame, the frame
434 (if (and (one-window-p)
435 (reftex-toc-dframe-p)
436 (> (length (frame-list)) 1))
438 (or (one-window-p) (delete-window))
439 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
441 (goto-char (or (marker-position reftex-toc-return-marker
) (point)))))
442 (defun reftex-toc-quit-and-kill ()
443 "Kill the *toc* buffer."
445 (kill-buffer "*toc*")
446 (or (one-window-p) (delete-window))
447 (switch-to-buffer (marker-buffer reftex-toc-return-marker
))
449 (goto-char (marker-position reftex-toc-return-marker
)))
450 (defun reftex-toc-display-index (&optional arg
)
451 "Display the index buffer for the current document.
452 This works just like `reftex-display-index' from a LaTeX buffer.
453 With prefix arg 1, restrict index to the section at point."
455 (reftex-toc-dframe-p nil
'error
)
456 (let ((data (get-text-property (point) :data
))
457 (docstruct (symbol-value reftex-docstruct-symbol
))
460 (setq bor
(reftex-last-assoc-before-elt 'toc data docstruct
)
461 eor
(assoc 'toc
(cdr (memq bor docstruct
)))
462 restr
(list (nth 6 bor
) bor eor
)))
463 (reftex-toc-goto-line)
464 (reftex-display-index (if restr nil arg
) restr
)))
466 ;; Rescanning the document and rebuilding the TOC buffer.
467 (defun reftex-toc-rescan (&rest ignore
)
468 "Regenerate the *toc* buffer by reparsing file of section at point."
470 (if (and reftex-enable-partial-scans
471 (null current-prefix-arg
))
472 (let* ((data (get-text-property (point) :data
))
474 (file (cond ((eq what
'toc
) (nth 3 data
))
475 ((memq what
'(eof bof file-error
)) (nth 1 data
))
476 ((stringp what
) (nth 3 data
))
477 ((eq what
'index
) (nth 3 data
))))
478 (line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
480 (error "Don't know which file to rescan. Try `C-u r'")
481 (put 'reftex-toc
:reftex-line line
)
482 (switch-to-buffer-other-window
483 (reftex-get-file-buffer-force file
))
484 (setq current-prefix-arg
'(4))
485 (let ((reftex-rebuilding-toc t
))
488 (reftex-kill-temporary-buffers))
490 (defun reftex-toc-Rescan (&rest ignore
)
491 "Regenerate the *toc* buffer by reparsing the entire document."
493 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
494 (put 'reftex-toc
:reftex-line line
))
495 (switch-to-buffer-other-window
496 (reftex-get-file-buffer-force reftex-last-toc-file
))
497 (setq current-prefix-arg
'(16))
498 (let ((reftex-rebuilding-toc t
))
501 (defun reftex-toc-revert (&rest ignore
)
502 "Regenerate the *toc* from the internal lists."
505 (if (fboundp 'frame-property
)
506 (frame-property (selected-frame) 'unsplittable
)
507 (frame-parameter (selected-frame) 'unsplittable
)))
508 (reftex-rebuilding-toc t
))
511 (reftex-get-file-buffer-force reftex-last-toc-file
))
512 (switch-to-buffer-other-window
513 (reftex-get-file-buffer-force reftex-last-toc-file
))))
514 (reftex-erase-buffer "*toc*")
515 (setq current-prefix-arg nil
)
518 (defun reftex-toc-external (&rest ignore
)
519 "Switch to table of contents of an external document."
521 (reftex-toc-dframe-p nil
'error
)
522 (let* ((old-buf (current-buffer))
523 (xr-alist (get-text-property 1 'xr-alist
))
524 (xr-index (reftex-select-external-document
526 (switch-to-buffer-other-window (or (reftex-get-file-buffer-force
527 (cdr (nth xr-index xr-alist
)))
528 (error "Cannot switch document")))
530 (if (equal old-buf
(current-buffer))
532 (message "Switched document"))))
534 (defun reftex-toc-jump (arg)
535 "Jump to a specific section. E.g. '3 z' jumps to section 3.
536 Useful for large TOC's."
538 (goto-char (point-min))
540 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
544 ;; Promotion/Demotion stuff
546 (defun reftex-toc-demote (&optional arg
)
547 "Demote section at point. If region is active, apply to all in region."
549 (reftex-toc-do-promote 1))
550 (defun reftex-toc-promote (&optional arg
)
551 "Promote section at point. If region is active, apply to all in region."
553 (reftex-toc-do-promote -
1))
554 (defun reftex-toc-do-promote (delta)
555 "Workhorse for reftex-toc-promote and reftex-to-demote.
556 Changes the level of sections in the current region, or just the section at
558 ;; We should not do anything unless we are sure this is going to work for
559 ;; each section in the region. Therefore we first collect information and
561 (let* ((start-line (+ (count-lines (point-min) (point))
563 (mark-line (if (reftex-region-active-p)
564 (save-excursion (goto-char (mark))
565 (+ (count-lines (point-min) (point))
568 (pro-or-de (if (> delta
0) "de" "pro"))
569 beg end entries data sections nsec mpos msg
)
572 (if (reftex-region-active-p)
573 ;; A region is dangerous, check if we have a brandnew scan,
574 ;; to make sure we are not missing any section statements.
575 (if (not (reftex-toc-check-docstruct))
576 (reftex-toc-load-all-files-for-promotion) ;; exits
577 (setq beg
(min (point) (mark))
578 end
(max (point) (mark))))
579 (setq beg
(point) end
(point)))
581 (while (and (setq data
(get-text-property (point) :data
))
583 (if (eq (car data
) 'toc
) (push (cons data
(point)) entries
))
584 (goto-char (or (next-single-property-change (point) :data
)
586 (setq entries
(nreverse entries
))
587 ;; Get the relevant section numbers, for an informative message
588 (goto-char start-pos
)
589 (setq sections
(reftex-toc-extract-section-number (car entries
)))
590 (if (> (setq nsec
(length entries
)) 1)
593 (reftex-toc-extract-section-number
594 (nth (1- nsec
) entries
)))))
595 ;; Run through the list and prepare the changes.
596 (setq entries
(mapcar 'reftex-toc-promote-prepare entries
))
597 ;; Ask for permission
598 (if (or (not reftex-toc-confirm-promotion
) ; never confirm
599 (and (integerp reftex-toc-confirm-promotion
) ; confirm if many
600 (< nsec reftex-toc-confirm-promotion
))
602 (format "%s %d toc-entr%s (section%s %s)? "
603 (if (< delta
0) "Promote" "Demote")
605 (if (= 1 nsec
) "y" "ies")
606 (if (= 1 nsec
) "" "s")
608 nil
; we have permission, do nothing
609 (error "Abort")) ; abort, we don't have permission
611 (mapcar 'reftex-toc-promote-action entries
)
612 ;; Rescan the document and rebuilt the toc buffer
613 (save-window-excursion
615 (reftex-toc-restore-region start-line mark-line
)
616 (message "%d section%s %smoted"
617 nsec
(if (= 1 nsec
) "" "s") pro-or-de
)
619 (if msg
(progn (ding) (message msg
)))))
628 (defun reftex-toc-restore-region (point-line &optional mark-line
)
630 (progn (goto-line mark-line
)
631 (setq mpos
(point))))
632 (if point-line
(goto-line point-line
))
636 (if (fboundp 'zmacs-activate-region
)
637 (zmacs-activate-region)
639 deactivate-mark nil
)))))
641 (defun reftex-toc-promote-prepare (x)
642 "Look at a toc entry and see if we could pro/demote it.
643 Expects the level change DELTA to be dynamically scoped into this function.
644 This function prepares everything for the changes, but does not do it.
645 The return value is a list with information needed when doing the
646 promotion/demotion later."
647 (let* ((data (car x
))
649 (marker (nth 4 data
))
650 (literal (nth 7 data
))
653 ;; Here follows some paranoid code to make very sure we are not
654 ;; going to break anything
656 (if (and (markerp marker
) (marker-buffer marker
))
657 ;; Buffer is still live and we have the marker.
660 ;; Goto the buffer and check of section is unchanged
661 (set-buffer (marker-buffer marker
))
662 (goto-char (marker-position marker
))
663 (if (looking-at (regexp-quote literal
))
664 ;; OK, get the makro name
666 (beginning-of-line 1)
667 (if (looking-at reftex-section-regexp
)
668 (setq name
(reftex-match-string 2))
669 (error "Something is wrong! Contact maintainer!")))
670 ;; Section has changed, request scan and loading
671 ;; We use a variable to delay until after the safe-exc.
672 ;; because otherwise we loose the region.
674 ;; Scan document and load all files, this exits command
675 (if load
(reftex-toc-load-all-files-for-promotion))) ; exits
676 ;; We don't have a live marker: scan and load files.
677 (reftex-toc-load-all-files-for-promotion)))
678 (level (cdr (assoc name reftex-section-levels-all
)))
679 (dummy (if (not (integerp level
))
681 (goto-char toc-point
)
682 (error "Cannot %smote special sections" pro-or-de
))))
683 ;; Delta is dynamically scoped into here...
684 (newlevel (if (>= level
0) (+ delta level
) (- level delta
)))
685 (dummy2 (if (or (and (>= level
0) (= newlevel -
1))
686 (and (< level
0) (= newlevel
0)))
687 (error "Cannot %smote \\%s" pro-or-de name
)))
688 (newname (reftex-toc-newhead-from-alist newlevel name
689 reftex-section-levels-all
)))
690 (if (and name newname
)
691 (list data name newname toc-point
)
692 (goto-char toc-point
)
693 (error "Cannot %smote \\%s" pro-or-de name
))))
695 (defun reftex-toc-promote-action (x)
696 "Change the level of a toc entry.
697 DELTA and PRO-OR-DE are assumed to be dynamically scoped into this function."
698 (let* ((data (car x
))
701 (marker (nth 4 data
)))
703 (set-buffer (marker-buffer marker
))
704 (goto-char (marker-position marker
))
705 (if (looking-at (concat "\\([ \t]*\\\\\\)" (regexp-quote name
)))
706 (replace-match (concat "\\1" newname
))
707 (error "Fatal error during %smotion" pro-or-de
)))))
709 (defun reftex-toc-extract-section-number (entry)
710 "Get the numbering of a toc entry, for message purposes."
711 (if (string-match "\\s-*\\(\\S-+\\)" (nth 2 (car entry
)))
712 (match-string 1 (nth 2 (car entry
)))
715 (defun reftex-toc-newhead-from-alist (nlevel head alist
)
716 "Get new heading with level NLEVEL from ALIST.
717 If there are no such entries, return nil.
718 If there are several different entries with same new level, choose
719 the one with the smallest distance to the assocation of HEAD in the alist.
720 This makes it possible for promotion to work several sets of headings,
721 if these sets are sorted blocks in the alist."
723 (ass (assoc head al
))
724 (pos (length (memq ass al
)))
725 dist
(mindist 1000) newhead
)
727 (if (equal (cdar al
) nlevel
)
729 (setq dist
(abs (- (length al
) pos
)))
731 (setq newhead
(car (car al
))
736 (defun reftex-toc-check-docstruct ()
737 "Check if the current docstruct is fully up to date and all files visited."
738 ;; We do this by checking if all sections are on the right position
739 (let ((docstruct (symbol-value reftex-docstruct-symbol
))
740 entry marker empoint
)
742 (while (setq entry
(pop docstruct
))
743 (if (eq (car entry
) 'toc
)
745 (setq marker
(nth 4 entry
)
746 empoint
(nth 8 entry
))
747 (if (not (and (markerp marker
)
748 (marker-buffer marker
)
749 (= (marker-position marker
) empoint
)))
750 (throw 'exit nil
)))))
753 (defun reftex-toc-load-all-files-for-promotion ()
754 "Make sure all files of the document are being visited by buffers,
755 and that the scanning info is absolutely up to date.
756 We do this by rescanning with reftex-keep-temporary-buffers bound to t.
757 The variable PRO-OR-DE is assumed to be dynamically scoped into this function.
758 When finished, we exit with an error message."
759 (let ((reftex-keep-temporary-buffers t
))
761 (reftex-toc-restore-region start-line mark-line
)
763 "TOC had to be updated first. Please check selection and repeat the command.")))
765 (defun reftex-toc-rename-label ()
766 "Rename the currently selected label in the *TOC* buffer.
767 This launches a global search and replace in order to rename a label.
768 Renaming a label is hardly ever necessary - the only exeption is after
769 promotion/demotion in connection with a package like fancyref, where the
770 label prefix determines the wording of a reference."
772 (let* ((toc (get-text-property (point) :data
))
773 (label (car toc
)) newlabel
)
774 (if (not (stringp label
))
775 (error "This is not a label entry."))
776 (setq newlabel
(read-string (format "Rename label \"%s\" to:" label
)))
777 (if (assoc newlabel
(symbol-value reftex-docstruct-symbol
))
779 (format "Label '%s' exists. Use anyway? " label
)))
782 (save-window-excursion
783 (reftex-toc-visit-location t
)
785 (reftex-query-replace-document
786 (concat "{" (regexp-quote label
) "}")
787 (format "{%s}" newlabel
))
789 (reftex-toc-rescan)))
792 (defun reftex-toc-visit-location (&optional final no-revisit
)
793 ;; Visit the tex file corresponding to the toc entry on the current line.
794 ;; If FINAL is t, stay there
795 ;; If FINAL is 'hide, hide the *toc* window.
796 ;; Otherwise, move cursor back into *toc* window.
797 ;; NO-REVISIT means don't visit files, just use live buffers.
798 ;; This function is pretty clever about finding back a section heading,
799 ;; even if the buffer is not live, or things like outline, x-symbol etc.
802 (let* ((toc (get-text-property (point) :data
))
803 (toc-window (selected-window))
804 show-window show-buffer match
)
806 (unless toc
(error "Don't know which toc line to visit"))
812 (setq match
(reftex-toc-find-section toc no-revisit
)))
814 ((eq (car toc
) 'index
)
816 (setq match
(reftex-index-show-entry toc no-revisit
)))
818 ((memq (car toc
) '(bof eof
))
821 (let ((where (car toc
))
823 (if (or (not no-revisit
) (reftex-get-buffer-visiting file
))
825 (switch-to-buffer-other-window
826 (reftex-get-file-buffer-force file nil
))
827 (goto-char (if (eq where
'bof
) (point-min) (point-max))))
828 (message reftex-no-follow-message
) nil
))))
832 (setq match
(reftex-show-label-location toc reftex-callback-fwd
835 (setq show-window
(selected-window)
836 show-buffer
(current-buffer))
839 (select-window toc-window
)
840 (error "Cannot find location"))
842 (select-window toc-window
)
844 ;; use the `final' parameter to decide what to do next
847 (reftex-unhighlight 0)
848 (select-window show-window
))
850 (reftex-unhighlight 0)
851 (or (one-window-p) (delete-window))
852 ;; If `show-window' is still live, show-buffer is already visible
853 ;; so let's not make it visible in yet-another-window.
854 (if (window-live-p show-window
)
855 (set-buffer show-buffer
)
856 (switch-to-buffer show-buffer
))
860 (defun reftex-toc-find-section (toc &optional no-revisit
)
861 (let* ((file (nth 3 toc
))
864 (literal (nth 7 toc
))
865 (emergency-point (nth 8 toc
))
868 ((and (markerp marker
) (marker-buffer marker
))
869 ;; Buffer is still live and we have the marker. Should be easy.
870 (switch-to-buffer-other-window (marker-buffer marker
))
871 (goto-char (marker-position marker
))
872 (or (looking-at (regexp-quote literal
))
873 (looking-at (reftex-make-regexp-allow-for-ctrl-m literal
))
874 (looking-at (reftex-make-desperate-section-regexp literal
))
875 (looking-at (concat "\\\\"
879 reftex-section-levels-all
)))
881 ((or (not no-revisit
)
882 (reftex-get-buffer-visiting file
))
883 ;; Marker is lost. Use the backup method.
884 (switch-to-buffer-other-window
885 (reftex-get-file-buffer-force file nil
))
886 (goto-char (or emergency-point
(point-min)))
887 (or (looking-at (regexp-quote literal
))
888 (let ((len (length literal
)))
889 (or (reftex-nearest-match (regexp-quote literal
) len
)
890 (reftex-nearest-match
891 (reftex-make-regexp-allow-for-ctrl-m literal
) len
)
892 (reftex-nearest-match
893 (reftex-make-desperate-section-regexp literal
) len
)))))
894 (t (message reftex-no-follow-message
) nil
))))
896 (goto-char (match-beginning 0))
897 (if (not (= (point) (point-max))) (recenter 1))
898 (reftex-highlight 0 (match-beginning 0) (match-end 0) (current-buffer)))
901 (defun reftex-make-desperate-section-regexp (old)
902 ;; Return a regexp which will still match a section statement even if
903 ;; x-symbol or isotex or the like have been at work in the mean time.
904 (let* ((n (1+ (string-match "[[{]" old
)))
905 (new (regexp-quote (substring old
0 (1+ (string-match "[[{]" old
)))))
906 (old (substring old n
)))
908 "\\([\r\n]\\)\\|\\(\\`\\|[ \t\n\r]\\)\\([a-zA-Z0-9]+\\)\\([ \t\n\r]\\|}\\'\\)"
910 (if (match-beginning 1)
911 (setq new
(concat new
"[^\n\r]*[\n\r]"))
912 (setq new
(concat new
"[^\n\r]*" (match-string 3 old
))))
913 (setq old
(substring old
(match-end 0))))
916 ;; Auto recentering of TOC window
918 (defun reftex-recenter-toc-when-idle ()
919 (and (> (buffer-size) 5)
921 (not (active-minibuffer-window))
922 (fboundp 'reftex-toc-mode
)
923 (get-buffer-window "*toc*" 'visible
)
924 (string= reftex-last-toc-master
(reftex-TeX-master-file))
925 (let (current-prefix-arg)
926 (reftex-toc-recenter))))
928 (defun reftex-toggle-auto-toc-recenter ()
929 "Toggle the automatic recentering of the toc window.
930 When active, leaving point idle will make the toc window jump to the correct
933 (if reftex-toc-auto-recenter-timer
935 (if (featurep 'xemacs
)
936 (delete-itimer reftex-toc-auto-recenter-timer
)
937 (cancel-timer reftex-toc-auto-recenter-timer
))
938 (setq reftex-toc-auto-recenter-timer nil
)
939 (message "Automatic recentering of toc windwo was turned off"))
940 (setq reftex-toc-auto-recenter-timer
941 (if (featurep 'xemacs
)
942 (start-itimer "RefTeX Idle Timer for recenter"
943 'reftex-recenter-toc-when-idle
944 reftex-idle-time reftex-idle-time t
)
946 reftex-idle-time t
'reftex-recenter-toc-when-idle
)))
947 (message "Automatic recentering of toc window was turned on")))
949 (defun reftex-toc-toggle-dedicated-frame ()
950 "Toggle the display of a separate frame for the TOC.
951 This frame is not used by the `reftex-toc' commands, but it is useful to
952 always show the current section in connection with the option
953 `reftex-auto-recenter-toc'."
956 (let* ((frames (frame-list)) frame
957 (get-frame-prop-func (if (fboundp 'frame-property
)
960 (while (setq frame
(pop frames
))
961 (if (equal (funcall get-frame-prop-func frame
'name
)
966 (reftex-make-separate-toc-frame))))
968 (defun reftex-make-separate-toc-frame ()
969 ;; Create a new fame showing only the toc buffer.
970 (let ((current-frame (selected-frame))
971 (current-window (selected-window))
972 (current-toc-window (get-buffer-window "*toc*" 'visible
))
974 (if (and current-toc-window
975 (not (equal (selected-frame) (window-frame current-toc-window
))))
977 (setq current-toc-frame
979 '((name .
"RefTeX TOC Frame")
980 (height .
20) (width .
60)
983 (default-toolbar-visible-p . nil
)
984 (menubar-visible-p . nil
)
985 (horizontal-scrollbar-visible-p . nil
))))
986 (select-frame current-toc-frame
)
987 (switch-to-buffer "*toc*")
988 (select-frame current-frame
)
989 (if (fboundp 'focus-frame
) (focus-frame current-frame
)
990 (if (fboundp 'x-focus-frame
) (x-focus-frame current-frame
)))
991 (select-window current-window
)
992 (when (eq reftex-auto-recenter-toc
'frame
)
993 (unless reftex-toc-auto-recenter-timer
994 (reftex-toggle-auto-toc-recenter))
995 (add-hook 'delete-frame-hook
'reftex-toc-delete-frame-hook
)))))
997 (defun reftex-toc-delete-frame-hook (frame)
998 (if (and reftex-toc-auto-recenter-timer
999 (reftex-toc-dframe-p frame
))
1001 (reftex-toggle-auto-toc-recenter))))
1003 ;; Table of Contents map
1004 (define-key reftex-toc-map
(if (featurep 'xemacs
) [(button2)] [(mouse-2)])
1005 'reftex-toc-mouse-goto-line-and-hide
)
1007 (substitute-key-definition
1008 'next-line
'reftex-toc-next reftex-toc-map global-map
)
1009 (substitute-key-definition
1010 'previous-line
'reftex-toc-previous reftex-toc-map global-map
)
1013 '(("n" . reftex-toc-next
)
1014 ("p" . reftex-toc-previous
)
1015 ("?" . reftex-toc-show-help
)
1016 (" " . reftex-toc-view-line
)
1017 ("\C-m" . reftex-toc-goto-line-and-hide
)
1018 ("\C-i" . reftex-toc-goto-line
)
1019 ("\C-c>" . reftex-toc-display-index
)
1020 ("r" . reftex-toc-rescan
)
1021 ("R" . reftex-toc-Rescan
)
1022 ("g" . revert-buffer
)
1023 ("q" . reftex-toc-quit
);
1024 ("k" . reftex-toc-quit-and-kill
)
1025 ("f" . reftex-toc-toggle-follow
);
1026 ("a" . reftex-toggle-auto-toc-recenter
)
1027 ("d" . reftex-toc-toggle-dedicated-frame
)
1028 ("F" . reftex-toc-toggle-file-boundary
)
1029 ("i" . reftex-toc-toggle-index
)
1030 ("l" . reftex-toc-toggle-labels
)
1031 ("t" . reftex-toc-max-level
)
1032 ("c" . reftex-toc-toggle-context
)
1033 ; ("%" . reftex-toc-toggle-commented)
1034 ("\M-%" . reftex-toc-rename-label
)
1035 ("x" . reftex-toc-external
)
1036 ("z" . reftex-toc-jump
)
1037 ("." . reftex-toc-show-calling-point
)
1038 ("\C-c\C-n" . reftex-toc-next-heading
)
1039 ("\C-c\C-p" . reftex-toc-previous-heading
)
1040 (">" . reftex-toc-demote
)
1041 ("<" . reftex-toc-promote
))
1042 do
(define-key reftex-toc-map
(car x
) (cdr x
)))
1044 (loop for key across
"0123456789" do
1045 (define-key reftex-toc-map
(vector (list key
)) 'digit-argument
))
1046 (define-key reftex-toc-map
"-" 'negative-argument
)
1049 reftex-toc-menu reftex-toc-map
1050 "Menu for Table of Contents buffer"
1052 ["Show Location" reftex-toc-view-line t
]
1053 ["Go To Location" reftex-toc-goto-line t
]
1054 ["Exit & Go To Location" reftex-toc-goto-line-and-hide t
]
1055 ["Show Calling Point" reftex-toc-show-calling-point t
]
1056 ["Quit" reftex-toc-quit t
]
1059 ["Promote" reftex-toc-promote t
]
1060 ["Demote" reftex-toc-demote t
]
1061 ["Rename Label" reftex-toc-rename-label t
])
1063 ["Index" reftex-toc-display-index t
]
1064 ["External Document TOC " reftex-toc-external t
]
1067 ["Rebuilt *toc* Buffer" revert-buffer t
]
1068 ["Rescan One File" reftex-toc-rescan reftex-enable-partial-scans
]
1069 ["Rescan Entire Document" reftex-toc-Rescan t
])
1072 ["File Boundaries" reftex-toc-toggle-file-boundary
:style toggle
1073 :selected reftex-toc-include-file-boundaries
]
1074 ["Labels" reftex-toc-toggle-labels
:style toggle
1075 :selected reftex-toc-include-labels
]
1076 ["Index Entries" reftex-toc-toggle-index
:style toggle
1077 :selected reftex-toc-include-index-entries
]
1078 ["Context" reftex-toc-toggle-context
:style toggle
1079 :selected reftex-toc-include-context
]
1081 ["Follow Mode" reftex-toc-toggle-follow
:style toggle
1082 :selected reftex-toc-follow-mode
]
1083 ["Auto Recenter" reftex-toggle-auto-toc-recenter
:style toggle
1084 :selected reftex-toc-auto-recenter-timer
]
1085 ["Dedicated Frame" reftex-toc-toggle-dedicated-frame t
])
1087 ["Help" reftex-toc-show-help t
]))
1090 ;;; arch-tag: 92400ce2-0b86-4c89-a606-4ed71acea17e
1091 ;;; reftex-toc.el ends here