aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2015-04-27 12:59:11 -0400
committerStefan Monnier2015-04-27 12:59:11 -0400
commit1c7f0cdc4c1f8479fcf929cfb2446432b1cc19de (patch)
tree3e11deb745bdc2a383d1c30ed89752d9bf213a64
parent08dad2f84ddc2934ced2b1e4ec891a581d59d617 (diff)
downloademacs-1c7f0cdc4c1f8479fcf929cfb2446432b1cc19de.tar.gz
emacs-1c7f0cdc4c1f8479fcf929cfb2446432b1cc19de.zip
* lisp/textmodes/reftex-toc.el: Improve multi-frame behavior
* lisp/textmodes/reftex-toc.el (reftex-toc-revert): Avoid displaying the buffer in yet another frame. (reftex-toc-visit-location): Make sure toc-window has focus at the end when `final' is nil. (reftex--rebuilding-toc): Defvar to avoid `boundp' and silence warnings. Use `--' to clarify that it's internal. (reftex-toc-next, reftex-toc-previous, reftex-toc-demote) (reftex-toc-promote): Clarify unused argument. (reftex--pro-or-de, reftex--start-line, reftex--mark-line): Add `reftex--' prefix. Fix all users. (reftex-toc-promote-prepare): Use _ for dummy variable. (reftex-toc-restore-region): Rename `m
-rw-r--r--lisp/textmodes/reftex-toc.el145
1 files changed, 77 insertions, 68 deletions
diff --git a/lisp/textmodes/reftex-toc.el b/lisp/textmodes/reftex-toc.el
index 085f2d7bdf9..d51fb9b2d4a 100644
--- a/lisp/textmodes/reftex-toc.el
+++ b/lisp/textmodes/reftex-toc.el
@@ -190,8 +190,11 @@ M-% Global search and replace to rename label at point.
190x Switch to TOC of external document (with LaTeX package `xr'). 190x Switch to TOC of external document (with LaTeX package `xr').
191z Jump to a specific section (e.g. '3 z' goes to section 3).") 191z Jump to a specific section (e.g. '3 z' goes to section 3).")
192 192
193(defvar reftex--rebuilding-toc nil)
194
193;;;###autoload 195;;;###autoload
194(defun reftex-toc (&optional rebuild reuse) 196(defun reftex-toc (&optional _rebuild reuse)
197 ;; FIXME: Get rid of the `rebuild' argument.
195 "Show the table of contents for the current document. 198 "Show the table of contents for the current document.
196When called with a raw C-u prefix, rescan the document first." 199When called with a raw C-u prefix, rescan the document first."
197 200
@@ -201,6 +204,9 @@ When called with a raw C-u prefix, rescan the document first."
201 (interactive) 204 (interactive)
202 205
203 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file))) 206 (if (or (not (string= reftex-last-toc-master (reftex-TeX-master-file)))
207 ;; FIXME: use (interactive "P") to receive current-prefix-arg as
208 ;; an argument instead of using the var here, which forces us to set
209 ;; current-prefix-arg in the callers.
204 current-prefix-arg) 210 current-prefix-arg)
205 (reftex-erase-buffer "*toc*")) 211 (reftex-erase-buffer "*toc*"))
206 212
@@ -225,7 +231,7 @@ When called with a raw C-u prefix, rescan the document first."
225 (docstruct-symbol reftex-docstruct-symbol) 231 (docstruct-symbol reftex-docstruct-symbol)
226 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol))) 232 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
227 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data))) 233 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
228 (here-I-am (if (boundp 'reftex-rebuilding-toc) 234 (here-I-am (if reftex--rebuilding-toc
229 (get 'reftex-toc :reftex-data) 235 (get 'reftex-toc :reftex-data)
230 (car (reftex-where-am-I)))) 236 (car (reftex-where-am-I))))
231 (unsplittable (if (fboundp 'frame-property) 237 (unsplittable (if (fboundp 'frame-property)
@@ -414,17 +420,17 @@ SPC=view TAB=goto RET=goto+hide [q]uit [r]escan [l]abels [f]ollow [x]r [?]Help
414 (if reftex-toc-follow-mode 420 (if reftex-toc-follow-mode
415 (setq reftex-toc-follow-mode 1))) 421 (setq reftex-toc-follow-mode 1)))
416 422
417(defun reftex-toc-next (&optional arg) 423(defun reftex-toc-next (&optional _arg)
418 "Move to next selectable item." 424 "Move to next selectable item."
419 (interactive "p") 425 (interactive)
420 (when (featurep 'xemacs) (setq zmacs-region-stays t)) 426 (when (featurep 'xemacs) (setq zmacs-region-stays t))
421 (setq reftex-callback-fwd t) 427 (setq reftex-callback-fwd t)
422 (or (eobp) (forward-char 1)) 428 (or (eobp) (forward-char 1))
423 (goto-char (or (next-single-property-change (point) :data) 429 (goto-char (or (next-single-property-change (point) :data)
424 (point)))) 430 (point))))
425(defun reftex-toc-previous (&optional arg) 431(defun reftex-toc-previous (&optional _arg)
426 "Move to previous selectable item." 432 "Move to previous selectable item."
427 (interactive "p") 433 (interactive)
428 (when (featurep 'xemacs) (setq zmacs-region-stays t)) 434 (when (featurep 'xemacs) (setq zmacs-region-stays t))
429 (setq reftex-callback-fwd nil) 435 (setq reftex-callback-fwd nil)
430 (goto-char (or (previous-single-property-change (point) :data) 436 (goto-char (or (previous-single-property-change (point) :data)
@@ -558,7 +564,7 @@ With prefix arg 1, restrict index to the section at point."
558 (reftex-display-index (if restr nil arg) restr))) 564 (reftex-display-index (if restr nil arg) restr)))
559 565
560;; Rescanning the document and rebuilding the TOC buffer. 566;; Rescanning the document and rebuilding the TOC buffer.
561(defun reftex-toc-rescan (&rest ignore) 567(defun reftex-toc-rescan (&rest _)
562 "Regenerate the *toc* buffer by reparsing file of section at point." 568 "Regenerate the *toc* buffer by reparsing file of section at point."
563 (interactive) 569 (interactive)
564 (if (and reftex-enable-partial-scans 570 (if (and reftex-enable-partial-scans
@@ -576,12 +582,12 @@ With prefix arg 1, restrict index to the section at point."
576 (switch-to-buffer-other-window 582 (switch-to-buffer-other-window
577 (reftex-get-file-buffer-force file)) 583 (reftex-get-file-buffer-force file))
578 (setq current-prefix-arg '(4)) 584 (setq current-prefix-arg '(4))
579 (let ((reftex-rebuilding-toc t)) 585 (let ((reftex--rebuilding-toc t))
580 (reftex-toc)))) 586 (reftex-toc))))
581 (reftex-toc-Rescan)) 587 (reftex-toc-Rescan))
582 (reftex-kill-temporary-buffers)) 588 (reftex-kill-temporary-buffers))
583 589
584(defun reftex-toc-Rescan (&rest ignore) 590(defun reftex-toc-Rescan (&rest _)
585 "Regenerate the *toc* buffer by reparsing the entire document." 591 "Regenerate the *toc* buffer by reparsing the entire document."
586 (interactive) 592 (interactive)
587 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))) 593 (let* ((line (+ (count-lines (point-min) (point)) (if (bolp) 1 0))))
@@ -589,17 +595,17 @@ With prefix arg 1, restrict index to the section at point."
589 (switch-to-buffer-other-window 595 (switch-to-buffer-other-window
590 (reftex-get-file-buffer-force reftex-last-toc-file)) 596 (reftex-get-file-buffer-force reftex-last-toc-file))
591 (setq current-prefix-arg '(16)) 597 (setq current-prefix-arg '(16))
592 (let ((reftex-rebuilding-toc t)) 598 (let ((reftex--rebuilding-toc t))
593 (reftex-toc))) 599 (reftex-toc)))
594 600
595(defun reftex-toc-revert (&rest ignore) 601(defun reftex-toc-revert (&rest _)
596 "Regenerate the TOC from the internal lists." 602 "Regenerate the TOC from the internal lists."
597 (interactive) 603 (interactive)
598 (let ((unsplittable 604 (let ((unsplittable
599 (if (fboundp 'frame-property) 605 (if (fboundp 'frame-property)
600 (frame-property (selected-frame) 'unsplittable) 606 (frame-property (selected-frame) 'unsplittable)
601 (frame-parameter nil 'unsplittable))) 607 (frame-parameter nil 'unsplittable)))
602 (reftex-rebuilding-toc t)) 608 (reftex--rebuilding-toc t))
603 (if unsplittable 609 (if unsplittable
604 (switch-to-buffer 610 (switch-to-buffer
605 (reftex-get-file-buffer-force reftex-last-toc-file)) 611 (reftex-get-file-buffer-force reftex-last-toc-file))
@@ -607,9 +613,9 @@ With prefix arg 1, restrict index to the section at point."
607 (reftex-get-file-buffer-force reftex-last-toc-file)))) 613 (reftex-get-file-buffer-force reftex-last-toc-file))))
608 (reftex-erase-buffer "*toc*") 614 (reftex-erase-buffer "*toc*")
609 (setq current-prefix-arg nil) 615 (setq current-prefix-arg nil)
610 (reftex-toc t)) 616 (reftex-toc t t))
611 617
612(defun reftex-toc-external (&rest ignore) 618(defun reftex-toc-external (&rest _)
613 "Switch to table of contents of an external document." 619 "Switch to table of contents of an external document."
614 (interactive) 620 (interactive)
615 (reftex-toc-dframe-p nil 'error) 621 (reftex-toc-dframe-p nil 'error)
@@ -637,18 +643,17 @@ Useful for large TOCs."
637 643
638;; Promotion/Demotion stuff 644;; Promotion/Demotion stuff
639 645
640(defvar pro-or-de) 646(defvar reftex--pro-or-de)
641(defvar start-pos) 647(defvar reftex--start-line)
642(defvar start-line) 648(defvar reftex--mark-line)
643(defvar mark-line)
644 649
645(defun reftex-toc-demote (&optional arg) 650(defun reftex-toc-demote (&optional _arg)
646 "Demote section at point. If region is active, apply to all in region." 651 "Demote section at point. If region is active, apply to all in region."
647 (interactive "p") 652 (interactive)
648 (reftex-toc-do-promote 1)) 653 (reftex-toc-do-promote 1))
649(defun reftex-toc-promote (&optional arg) 654(defun reftex-toc-promote (&optional _arg)
650 "Promote section at point. If region is active, apply to all in region." 655 "Promote section at point. If region is active, apply to all in region."
651 (interactive "p") 656 (interactive)
652 (reftex-toc-do-promote -1)) 657 (reftex-toc-do-promote -1))
653(defun reftex-toc-do-promote (delta) 658(defun reftex-toc-do-promote (delta)
654 "Workhorse for `reftex-toc-promote' and `reftex-toc-demote'. 659 "Workhorse for `reftex-toc-promote' and `reftex-toc-demote'.
@@ -657,14 +662,15 @@ point."
657 ;; We should not do anything unless we are sure this is going to work for 662 ;; We should not do anything unless we are sure this is going to work for
658 ;; each section in the region. Therefore we first collect information and 663 ;; each section in the region. Therefore we first collect information and
659 ;; test. 664 ;; test.
660 (let* ((start-line (+ (count-lines (point-min) (point)) 665 (let* ((reftex--start-line (+ (count-lines (point-min) (point))
661 (if (bolp) 1 0))) 666 (if (bolp) 1 0)))
662 (mark-line (if (reftex-region-active-p) 667 (reftex--mark-line
663 (save-excursion (goto-char (mark)) 668 (if (reftex-region-active-p)
664 (+ (count-lines (point-min) (point)) 669 (save-excursion (goto-char (mark))
665 (if (bolp) 1 0))))) 670 (+ (count-lines (point-min) (point))
671 (if (bolp) 1 0)))))
666 (start-pos (point)) 672 (start-pos (point))
667 (pro-or-de (if (> delta 0) "de" "pro")) 673 (reftex--pro-or-de (if (> delta 0) "de" "pro"))
668 beg end entries data sections nsec msg) 674 beg end entries data sections nsec msg)
669 (setq msg 675 (setq msg
670 (catch 'exit 676 (catch 'exit
@@ -713,23 +719,23 @@ point."
713 ;; Rescan the document and rebuilt the toc buffer 719 ;; Rescan the document and rebuilt the toc buffer
714 (save-window-excursion 720 (save-window-excursion
715 (reftex-toc-Rescan)) 721 (reftex-toc-Rescan))
716 (reftex-toc-restore-region start-line mark-line) 722 (reftex-toc-restore-region reftex--start-line reftex--mark-line)
717 (message "%d section%s %smoted" 723 (message "%d section%s %smoted"
718 nsec (if (= 1 nsec) "" "s") pro-or-de) 724 nsec (if (= 1 nsec) "" "s") reftex--pro-or-de)
719 nil)) 725 nil))
720 (if msg (progn (ding) (message "%s" msg))))) 726 (if msg (progn (ding) (message "%s" msg)))))
721 727
722 728
723(defun reftex-toc-restore-region (point-line &optional mark-line) 729(defun reftex-toc-restore-region (point-line &optional mark-line)
724 (let (mpos) 730 (let ((mpos
725 (when mark-line 731 (when mark-line
726 (goto-char (point-min)) 732 (goto-char (point-min))
727 (forward-line (1- mark-line)) 733 (forward-line (1- mark-line))
728 (setq mpos (point))) 734 (point))))
729 (when point-line 735 (when point-line
730 (goto-char (point-min)) 736 (goto-char (point-min))
731 (forward-line (1- point-line))) 737 (forward-line (1- point-line)))
732 (when mark-line 738 (when mpos
733 (set-mark mpos) 739 (set-mark mpos)
734 (if (featurep 'xemacs) 740 (if (featurep 'xemacs)
735 (zmacs-activate-region) 741 (zmacs-activate-region)
@@ -749,7 +755,7 @@ promotion/demotion later. DELTA is the level change."
749 (name nil) 755 (name nil)
750 ;; Here follows some paranoid code to make very sure we are not 756 ;; Here follows some paranoid code to make very sure we are not
751 ;; going to break anything 757 ;; going to break anything
752 (name1 ; dummy 758 (_
753 (if (and (markerp marker) (marker-buffer marker)) 759 (if (and (markerp marker) (marker-buffer marker))
754 ;; Buffer is still live and we have the marker. 760 ;; Buffer is still live and we have the marker.
755 (progn 761 (progn
@@ -772,24 +778,24 @@ promotion/demotion later. DELTA is the level change."
772 ;; We don't have a live marker: scan and load files. 778 ;; We don't have a live marker: scan and load files.
773 (reftex-toc-load-all-files-for-promotion))) 779 (reftex-toc-load-all-files-for-promotion)))
774 (level (cdr (assoc name reftex-section-levels-all))) 780 (level (cdr (assoc name reftex-section-levels-all)))
775 (dummy (if (not (integerp level)) 781 (_ (if (not (integerp level))
776 (progn 782 (progn
777 (goto-char toc-point) 783 (goto-char toc-point)
778 (error "Cannot %smote special sections" pro-or-de)))) 784 (error "Cannot %smote special sections" reftex--pro-or-de))))
779 (newlevel (if (>= level 0) (+ delta level) (- level delta))) 785 (newlevel (if (>= level 0) (+ delta level) (- level delta)))
780 (dummy2 (if (or (and (>= level 0) (= newlevel -1)) 786 (_ (if (or (and (>= level 0) (= newlevel -1))
781 (and (< level 0) (= newlevel 0))) 787 (and (< level 0) (= newlevel 0)))
782 (error "Cannot %smote \\%s" pro-or-de name))) 788 (error "Cannot %smote \\%s" reftex--pro-or-de name)))
783 (newname (reftex-toc-newhead-from-alist newlevel name 789 (newname (reftex-toc-newhead-from-alist newlevel name
784 reftex-section-levels-all))) 790 reftex-section-levels-all)))
785 (if (and name newname) 791 (if (and name newname)
786 (list data name newname toc-point) 792 (list data name newname toc-point)
787 (goto-char toc-point) 793 (goto-char toc-point)
788 (error "Cannot %smote \\%s" pro-or-de name)))) 794 (error "Cannot %smote \\%s" reftex--pro-or-de name))))
789 795
790(defun reftex-toc-promote-action (x) 796(defun reftex-toc-promote-action (x)
791 "Change the level of a TOC entry. 797 "Change the level of a TOC entry.
792PRO-OR-DE is assumed to be dynamically scoped into this function." 798`reftex--pro-or-de' is assumed to be dynamically scoped into this function."
793 (let* ((data (car x)) 799 (let* ((data (car x))
794 (name (nth 1 x)) 800 (name (nth 1 x))
795 (newname (nth 2 x)) 801 (newname (nth 2 x))
@@ -798,7 +804,7 @@ PRO-OR-DE is assumed to be dynamically scoped into this function."
798 (goto-char (marker-position marker)) 804 (goto-char (marker-position marker))
799 (if (looking-at (concat "\\([ \t]*" reftex-section-pre-regexp "\\)" (regexp-quote name))) 805 (if (looking-at (concat "\\([ \t]*" reftex-section-pre-regexp "\\)" (regexp-quote name)))
800 (replace-match (concat "\\1" newname)) 806 (replace-match (concat "\\1" newname))
801 (error "Fatal error during %smotion" pro-or-de))))) 807 (error "Fatal error during %smotion" reftex--pro-or-de)))))
802 808
803(defun reftex-toc-extract-section-number (entry) 809(defun reftex-toc-extract-section-number (entry)
804 "Get the numbering of a TOC entry, for message purposes." 810 "Get the numbering of a TOC entry, for message purposes."
@@ -848,11 +854,11 @@ if these sets are sorted blocks in the alist."
848 "Make sure all files of the document are being visited by buffers, 854 "Make sure all files of the document are being visited by buffers,
849and that the scanning info is absolutely up to date. 855and that the scanning info is absolutely up to date.
850We do this by rescanning with `reftex-keep-temporary-buffers' bound to t. 856We do this by rescanning with `reftex-keep-temporary-buffers' bound to t.
851The variable PRO-OR-DE is assumed to be dynamically scoped into this function. 857The variable `reftex--pro-or-de' is assumed to be dynamically scoped into this function.
852When finished, we exit with an error message." 858When finished, we exit with an error message."
853 (let ((reftex-keep-temporary-buffers t)) 859 (let ((reftex-keep-temporary-buffers t))
854 (reftex-toc-Rescan) 860 (reftex-toc-Rescan)
855 (reftex-toc-restore-region start-line mark-line) 861 (reftex-toc-restore-region reftex--start-line reftex--mark-line)
856 (throw 'exit 862 (throw 'exit
857 "TOC had to be updated first. Please check selection and repeat the command."))) 863 "TOC had to be updated first. Please check selection and repeat the command.")))
858 864
@@ -895,7 +901,7 @@ label prefix determines the wording of a reference."
895 901
896 (let* ((toc (get-text-property (point) :data)) 902 (let* ((toc (get-text-property (point) :data))
897 (toc-window (selected-window)) 903 (toc-window (selected-window))
898 show-window show-buffer match) 904 match)
899 905
900 (unless toc (error "Don't know which TOC line to visit")) 906 (unless toc (error "Don't know which TOC line to visit"))
901 907
@@ -926,30 +932,33 @@ label prefix determines the wording of a reference."
926 (setq match (reftex-show-label-location toc reftex-callback-fwd 932 (setq match (reftex-show-label-location toc reftex-callback-fwd
927 no-revisit t)))) 933 no-revisit t))))
928 934
929 (setq show-window (selected-window)
930 show-buffer (current-buffer))
931
932 (unless match 935 (unless match
933 (select-window toc-window) 936 (select-window toc-window)
934 (error "Cannot find location")) 937 (error "Cannot find location"))
935 938
936 (select-window toc-window) 939 ;; Use the `final' parameter to decide what to do next.
937
938 ;; use the `final' parameter to decide what to do next
939 (cond 940 (cond
940 ((eq final t) 941 ((eq final t)
941 (reftex-unhighlight 0) 942 (with-selected-window toc-window
942 (select-window show-window)) 943 (reftex-unhighlight 0)))
943 ((eq final 'hide) 944 ((eq final 'hide)
944 (reftex-unhighlight 0) 945 (let ((show-window (selected-window))
945 (or (one-window-p) (delete-window)) 946 (show-buffer (window-buffer)))
946 ;; If `show-window' is still live, show-buffer is already visible 947 (unless (eq show-window toc-window) ;FIXME: Can this happen?
947 ;; so let's not make it visible in yet-another-window. 948 (with-selected-window toc-window
948 (if (window-live-p show-window) 949 (reftex-unhighlight 0)
949 (set-buffer show-buffer) 950 (or (one-window-p) (delete-window))))
950 (switch-to-buffer show-buffer)) 951 ;; If `show-window' is still live, show-buffer is already visible
951 (reftex-re-enlarge)) 952 ;; so let's not make it visible in yet-another-window.
952 (t nil)))) 953 (unless (window-live-p show-window)
954 ;; FIXME: How could show-window not be live?
955 (switch-to-buffer show-buffer))
956 (reftex-re-enlarge)))
957 (t
958 (unless (eq (selected-frame) (window-frame toc-window))
959 ;; Make sure `toc-window' is not just selected but has focus.
960 (select-frame-set-input-focus (window-frame toc-window)))
961 (select-window toc-window)))))
953 962
954(defun reftex-toc-find-section (toc &optional no-revisit) 963(defun reftex-toc-find-section (toc &optional no-revisit)
955 (let* ((file (nth 3 toc)) 964 (let* ((file (nth 3 toc))