]> code.delx.au - gnu-emacs/blobdiff - lisp/textmodes/reftex-toc.el
Add new maintainer (deego).
[gnu-emacs] / lisp / textmodes / reftex-toc.el
index 9f5cb0ce9ac437658d5f0338f0c446deb4be711d..d1b8fee760ad853b1693dd2217d05fb0384945f2 100644 (file)
@@ -1,9 +1,8 @@
-;;; reftex-toc.el - RefTeX's table of contents mode
+;;; reftex-toc.el --- RefTeX's table of contents mode
 ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
 
-;; Author:     Carsten Dominik <dominik@strw.LeidenUniv.nl>
-;; Version: 4.11
-;;
+;; Author: Carsten Dominik <dominik@science.uva.nl>
+;; Version: 4.18
 
 ;; This file is part of GNU Emacs.
 
 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
+;;; Commentary:
+
+;;; Code:
+
 (eval-when-compile (require 'cl))
 (provide 'reftex-toc)
 (require 'reftex)
@@ -59,8 +62,10 @@ Here are all local bindings.
              "  T<" 'reftex-toc-max-level-indicator ">"
              " -%-"))
   (setq truncate-lines t)
-  (make-local-hook 'post-command-hook)
-  (make-local-hook 'pre-command-hook)
+  (when (featurep 'xemacs)
+    ;; XEmacs needs the call to make-local-hook
+    (make-local-hook 'post-command-hook)
+    (make-local-hook 'pre-command-hook))
   (make-local-variable 'reftex-last-follow-point)
   (add-hook 'post-command-hook 'reftex-toc-post-command-hook nil t)
   (add-hook 'pre-command-hook  'reftex-toc-pre-command-hook nil t)
@@ -71,6 +76,7 @@ Here are all local bindings.
   "Stores the file name from which `reftex-toc' was called.  For redo command.")
 
 (defvar reftex-last-window-height nil)
+(defvar reftex-last-window-width nil)
 (defvar reftex-toc-include-labels-indicator nil)
 (defvar reftex-toc-include-index-indicator nil)
 (defvar reftex-toc-max-level-indicator nil)
@@ -89,10 +95,11 @@ C-c >      Display Index. With prefix arg, restrict index to current section.
 q / k      Hide/Kill *toc* buffer, return to position of reftex-toc command.
 l i c F    Toggle display of  [l]abels,  [i]ndex,  [c]ontext,  [F]ile borders.
 t          Change maximum toc depth (e.g. `3 t' hides levels greater than 3).
-f / g      Toggle follow mode on and off  / Refresh *toc* buffer.
+f / a / g  Toggle follow mode / toggle auto recenter / Refresh *toc* buffer.
 r / C-u r  Reparse the LaTeX document     / Reparse entire LaTeX document.
 .          In other window, show position from where `reftex-toc' was called.
-x          Switch to TOC of external document (with LaTeX package `xr').")
+x          Switch to TOC of external document (with LaTeX package `xr').
+z          Jump to a specific section (e.g. '3 z' goes to section 3")
 
 (defun reftex-toc (&optional rebuild)
   "Show the table of contents for the current document.
@@ -125,7 +132,7 @@ When called with a raw C-u prefix, rescan the document first."
         (docstruct-symbol reftex-docstruct-symbol)
         (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
         (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
-        (here-I-am (if rebuild 
+        (here-I-am (if (boundp 'reftex-rebuilding-toc)
                        (get 'reftex-toc :reftex-data)
                      (car (reftex-where-am-I))))
         offset)
@@ -135,8 +142,14 @@ When called with a raw C-u prefix, rescan the document first."
       (when (or (not reftex-toc-keep-other-windows)
                (< (window-height) (* 2 window-min-height)))
        (delete-other-windows))
-      (setq reftex-last-window-height (window-height))  ; remember
-      (split-window)
+
+      (setq reftex-last-window-width (window-width)
+           reftex-last-window-height (window-height))  ; remember
+      (if reftex-toc-split-windows-horizontally
+         (split-window-horizontally
+          (floor (* (frame-width) reftex-toc-split-windows-horizontally-fraction)))
+       (split-window))
+
       (let ((default-major-mode 'reftex-toc-mode))
        (switch-to-buffer "*toc*")))
 
@@ -205,10 +218,31 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
     (reftex-find-start-point (point) offset (get 'reftex-toc :reftex-line))
     (setq reftex-last-follow-point (point))))
 
+(defun reftex-toc-recenter (&optional arg)
+  "Display the TOC window and highlight line corresponding to current position."
+  (interactive "P")
+  (let ((buf (current-buffer)))
+    (reftex-toc arg)
+    (if (= (count-lines 1 (point)) 2)
+       (let ((current-prefix-arg nil))
+         (select-window (get-buffer-window buf))
+         (reftex-toc nil)))
+    (and (> (point) 1)
+        (not (get-text-property (point) 'intangible))
+        (memq reftex-highlight-selection '(cursor both))
+        (reftex-highlight 2
+          (or (previous-single-property-change 
+               (min (point-max) (1+ (point))) :data)
+              (point-min))
+          (or (next-single-property-change (point) :data)
+              (point-max))))
+    (select-window (get-buffer-window buf))))
+
 (defun reftex-toc-pre-command-hook ()
   ;; used as pre command hook in *toc* buffer
   (reftex-unhighlight 0)
-  (reftex-unhighlight 1))
+;;  (reftex-unhighlight 1)  ;; remove highlight on leaving buffer.
+  )
 
 (defun reftex-toc-post-command-hook ()
   ;; used in the post-command-hook for the *toc* buffer
@@ -217,7 +251,7 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
     (and (> (point) 1)
         (not (get-text-property (point) 'intangible))
         (memq reftex-highlight-selection '(cursor both))
-        (reftex-highlight 1
+        (reftex-highlight 2
           (or (previous-single-property-change (1+ (point)) :data)
               (point-min))
           (or (next-single-property-change (point) :data)
@@ -235,9 +269,13 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
 
 (defun reftex-re-enlarge ()
   ;; Enlarge windiw to a remembered size
-  (enlarge-window
-   (max 0 (- (or reftex-last-window-height (window-height))
-            (window-height)))))
+  (if reftex-toc-split-windows-horizontally
+      (enlarge-window-horizontally
+       (max 0 (- (or reftex-last-window-width (window-width))
+                (window-width))))
+    (enlarge-window
+     (max 0 (- (or reftex-last-window-height (window-height))
+              (window-height))))))
 
 (defun reftex-toc-show-help ()
   "Show a summary of special key bindings."
@@ -393,16 +431,20 @@ With prefix arg 1, restrict index to the section at point."
           (switch-to-buffer-other-window
            (reftex-get-file-buffer-force file))
          (setq current-prefix-arg '(4))
-          (reftex-toc t)))
+         (let ((reftex-rebuilding-toc t))
+           (reftex-toc))))
     (reftex-toc-Rescan))
   (reftex-kill-temporary-buffers))
 (defun reftex-toc-Rescan (&rest ignore)
   "Regenerate the *toc* buffer by reparsing the entire document."
   (interactive)
+  (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
+    (put 'reftex-toc :reftex-line line))
   (switch-to-buffer-other-window
    (reftex-get-file-buffer-force reftex-last-toc-file))
   (setq current-prefix-arg '(16))
-  (reftex-toc t))
+  (let ((reftex-rebuilding-toc t))
+    (reftex-toc)))
 (defun reftex-toc-revert (&rest ignore)
   "Regenerate the *toc* from the internal lists."
   (interactive)
@@ -410,7 +452,8 @@ With prefix arg 1, restrict index to the section at point."
    (reftex-get-file-buffer-force reftex-last-toc-file))
   (reftex-erase-buffer "*toc*")
   (setq current-prefix-arg nil)
-  (reftex-toc t))
+  (let ((reftex-rebuilding-toc t))
+    (reftex-toc t)))
 (defun reftex-toc-external (&rest ignore)
   "Switch to table of contents of an external document."
   (interactive)
@@ -426,6 +469,16 @@ With prefix arg 1, restrict index to the section at point."
        (message "")
       (message "Switched document"))))
 
+(defun reftex-toc-jump (arg)
+  "Jump to a specific section.  E.g. '3 z' jumps to section 3.
+Useful for large TOC's."
+  (interactive "P")
+  (goto-char (point-min))
+  (re-search-forward
+   (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
+   nil t)
+  (beginning-of-line))
+
 (defun reftex-toc-visit-location (&optional final no-revisit)
   ;; Visit the tex file corresponding to the toc entry on the current line.
   ;; If FINAL is t, stay there
@@ -546,6 +599,38 @@ With prefix arg 1, restrict index to the section at point."
       (setq old (substring old (match-end 0))))
     new))
 
+
+(defun reftex-recenter-toc-when-idle ()
+  (and (> (buffer-size) 5)
+       reftex-mode
+       (not (active-minibuffer-window))
+       (fboundp 'reftex-toc-mode)
+       (get-buffer-window "*toc*")
+       (string= reftex-last-toc-master (reftex-TeX-master-file))
+       (reftex-toc-recenter)))
+
+(defun reftex-toggle-auto-toc-recenter ()
+  "Toggle the automatic recentering of the toc window.
+When active, leaving point idle will make the toc window jump to the correct
+section."
+  (interactive)
+  (if reftex-toc-auto-recenter-timer
+      (progn
+       (if (featurep 'xemacs)
+           (delete-itimer reftex-toc-auto-recenter-timer)
+         (cancel-timer reftex-toc-auto-recenter-timer))
+       (setq reftex-toc-auto-recenter-timer nil)
+       (message "Automatic recentering of toc buffer was turned off"))
+    (setq reftex-toc-auto-recenter-timer
+         (if (featurep 'xemacs)
+             (start-itimer "RefTeX Idle Timer for recenter"
+                           'reftex-recenter-toc-when-idle
+                           reftex-idle-time reftex-idle-time t)
+           (run-with-idle-timer
+            reftex-idle-time t 'reftex-recenter-toc-when-idle)))
+    (message "Automatic recentering of toc window was turned on")))
+
+
 ;; Table of Contents map
 (define-key reftex-toc-map (if (featurep 'xemacs) [(button2)] [(mouse-2)])
   'reftex-toc-mouse-goto-line-and-hide)
@@ -569,6 +654,7 @@ With prefix arg 1, restrict index to the section at point."
        ("q"    . reftex-toc-quit)
        ("k"    . reftex-toc-quit-and-kill)
        ("f"    . reftex-toc-toggle-follow)
+       ("a"    . reftex-toggle-auto-toc-recenter)
        ("F"    . reftex-toc-toggle-file-boundary)
        ("i"    . reftex-toc-toggle-index)
        ("l"    . reftex-toc-toggle-labels)
@@ -576,6 +662,7 @@ With prefix arg 1, restrict index to the section at point."
        ("c"    . reftex-toc-toggle-context)
        ("%"    . reftex-toc-toggle-commented)
        ("x"    . reftex-toc-external)
+       ("z"    . reftex-toc-jump)
        ("."    . reftex-toc-show-calling-point)
        ("\C-c\C-n" . reftex-toc-next-heading)
        ("\C-c\C-p" . reftex-toc-previous-heading))