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