Source

reftex / reftex-toc.el

Diff from to

File reftex-toc.el

 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').
 	 (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)
     (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
     (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)
           (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)
    (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)
       (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)
 	("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)