1 ;;; reftex-sel.el --- the selection modes for RefTeX
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <dominik@science.uva.nl>
7 ;; Maintainer: auctex-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (eval-when-compile (require 'cl
))
34 (defvar reftex-select-label-map nil
35 "Keymap used for *RefTeX Select* buffer, when selecting a label.
36 This keymap can be used to configure the label selection process which is
37 started with the command \\[reftex-reference].")
39 (defun reftex-select-label-mode ()
40 "Major mode for selecting a label in a LaTeX document.
41 This buffer was created with RefTeX.
42 It only has a meaningful keymap when you are in the middle of a
44 To select a label, move the cursor to it and press RET.
45 Press `?' for a summary of important key bindings.
47 During a selection process, these are the local bindings.
49 \\{reftex-select-label-map}"
52 (kill-all-local-variables)
53 (when (featurep 'xemacs
)
54 ;; XEmacs needs the call to make-local-hook
55 (make-local-hook 'pre-command-hook
)
56 (make-local-hook 'post-command-hook
))
57 (setq major-mode
'reftex-select-label-mode
59 (set (make-local-variable 'reftex-select-marked
) nil
)
60 (when (syntax-table-p reftex-latex-syntax-table
)
61 (set-syntax-table reftex-latex-syntax-table
))
62 ;; We do not set a local map - reftex-select-item does this.
63 (run-hooks 'reftex-select-label-mode-hook
))
65 (defvar reftex-select-bib-map nil
66 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
67 This keymap can be used to configure the BibTeX selection process which is
68 started with the command \\[reftex-citation].")
70 (defun reftex-select-bib-mode ()
71 "Major mode for selecting a citation key in a LaTeX document.
72 This buffer was created with RefTeX.
73 It only has a meaningful keymap when you are in the middle of a
75 In order to select a citation, move the cursor to it and press RET.
76 Press `?' for a summary of important key bindings.
78 During a selection process, these are the local bindings.
80 \\{reftex-select-label-map}"
82 (kill-all-local-variables)
83 (when (featurep 'xemacs
)
84 ;; XEmacs needs the call to make-local-hook
85 (make-local-hook 'pre-command-hook
)
86 (make-local-hook 'post-command-hook
))
87 (setq major-mode
'reftex-select-bib-mode
89 (set (make-local-variable 'reftex-select-marked
) nil
)
90 ;; We do not set a local map - reftex-select-item does this.
91 (run-hooks 'reftex-select-bib-mode-hook
))
93 ;; (defun reftex-get-offset (buf here-am-I &optional typekey toc index file)
94 ;; ;; Find the correct offset data, like insert-docstruct would, but faster.
95 ;; ;; Buffer BUF knows the correct docstruct to use.
96 ;; ;; Basically this finds the first docstruct entry after HERE-I-AM which
97 ;; ;; is of allowed type. The optional arguments specify what is allowed.
99 ;; (with-current-buffer buf
100 ;; (reftex-access-scan-info)
101 ;; (let* ((rest (memq here-am-I (symbol-value reftex-docstruct-symbol)))
103 ;; (while (setq entry (pop rest))
104 ;; (if (or (and typekey
105 ;; (stringp (car entry))
106 ;; (or (equal typekey " ")
107 ;; (equal typekey (nth 1 entry))))
108 ;; (and toc (eq (car entry) 'toc))
109 ;; (and index (eq (car entry) 'index))
111 ;; (memq (car entry) '(bof eof file-error))))
112 ;; (throw 'exit entry)))
115 (defun reftex-get-offset (buf here-am-I
&optional typekey toc index file
)
116 ;; Find the correct offset data, like insert-docstruct would, but faster.
117 ;; Buffer BUF knows the correct docstruct to use.
118 ;; Basically this finds the first docstruct entry before HERE-I-AM which
119 ;; is of allowed type. The optional arguments specify what is allowed.
121 (with-current-buffer buf
122 (reftex-access-scan-info)
123 (let* ((rest (symbol-value reftex-docstruct-symbol
))
125 (while (setq entry
(pop rest
))
127 (stringp (car entry
))
128 (or (equal typekey
" ")
129 (equal typekey
(nth 1 entry
))))
130 (and toc
(eq (car entry
) 'toc
))
131 (and index
(eq (car entry
) 'index
))
133 (memq (car entry
) '(bof eof file-error
))))
134 (setq lastentry entry
))
135 (if (eq entry here-am-I
)
136 (throw 'exit
(or lastentry entry
))))
139 (defun reftex-insert-docstruct
140 (buf toc labels index-entries files context counter show-commented
141 here-I-am xr-prefix toc-buffer
)
142 ;; Insert an excerpt of the docstruct list.
143 ;; Return the data property of the entry corresponding to HERE-I-AM.
144 ;; BUF is the buffer which has the correct docstruct-symbol.
145 ;; LABELS non-nil means to include labels into the list.
146 ;; When a string, indicates the label type to include
147 ;; FILES non-nil means to display file boundaries.
148 ;; CONTEXT non-nil means to include label context.
149 ;; COUNTER means to count the labels.
150 ;; SHOW-COMMENTED means to include also labels which are commented out.
151 ;; HERE-I-AM is a member of the docstruct list. The function will return
152 ;; a used member near to this one, as a possible starting point.
153 ;; XR-PREFIX is the prefix to put in front of labels.
154 ;; TOC-BUFFER means this is to fill the toc buffer.
155 (let* ((font (reftex-use-fonts))
161 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
164 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
166 (if (memq reftex-highlight-selection
'(mouse both
))
167 reftex-mouse-selected-face
169 (label-face (reftex-verified-face reftex-label-face
170 'font-lock-constant-face
171 'font-lock-reference-face
))
172 (index-face (reftex-verified-face reftex-index-face
173 'font-lock-constant-face
174 'font-lock-reference-face
))
175 all cell text label typekey note comment master-dir-re
176 prev-inserted offset from to index-tag docstruct-symbol
)
178 ;; Pop to buffer buf to get the correct buffer-local variables
179 (with-current-buffer buf
181 ;; Ensure access to scanning info
182 (reftex-access-scan-info)
184 (setq docstruct-symbol reftex-docstruct-symbol
185 all
(symbol-value reftex-docstruct-symbol
)
186 reftex-active-toc nil
188 (concat "\\`" (regexp-quote
189 (file-name-directory (reftex-TeX-master-file))))))
191 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
192 (set (make-local-variable 'reftex-prefix
)
193 (cdr (assoc labels reftex-typekey-to-prefix-alist
)))
194 (if (equal reftex-prefix
" ") (setq reftex-prefix nil
))
196 ;; Walk the docstruct and insert the appropriate stuff
197 (while (setq cell
(pop all
))
204 ((memq (car cell
) '(bib thebib label-numbers appendix
205 master-dir bibview-cache is-multi xr xr-doc
)))
206 ;; These are currently ignored
208 ((memq (car cell
) '(bof eof file-error
))
209 ;; Beginning or end of a file
211 (setq prev-inserted cell
)
212 ; (if (eq offset 'attention) (setq offset cell))
214 " File " (if (string-match master-dir-re
(nth 1 cell
))
215 (substring (nth 1 cell
) (match-end 0))
217 (cond ((eq (car cell
) 'bof
) " starts here\n")
218 ((eq (car cell
) 'eof
) " ends here\n")
219 ((eq (car cell
) 'file-error
) " was not found\n")))
222 (put-text-property from to
223 'face reftex-file-boundary-face
))
226 (put-text-property from
(1- to
)
227 'mouse-face mouse-face
))
228 (put-text-property from to
:data cell
))))
230 ((eq (car cell
) 'toc
)
231 ;; a table of contents entry
233 (<= (nth 5 cell
) reftex-toc-max-level
))
234 (setq prev-inserted cell
)
235 ; (if (eq offset 'attention) (setq offset cell))
236 (setq reftex-active-toc cell
)
237 (insert (concat toc-indent
(nth 2 cell
) "\n"))
240 (put-text-property from to
241 'face reftex-section-heading-face
))
244 (put-text-property from
(1- to
)
245 'mouse-face mouse-face
))
246 (put-text-property from to
:data cell
))
249 ((stringp (car cell
))
251 (when (null (nth 2 cell
))
252 ;; No context yet. Quick update.
253 (setcdr cell
(cdr (reftex-label-info-update cell
)))
254 (put docstruct-symbol
'modified t
))
256 (setq label
(car cell
)
264 (string= typekey labels
)
265 (string= labels
" "))
266 (or show-commented
(null comment
)))
268 ;; Yes we want this one
270 (setq prev-inserted cell
)
271 ; (if (eq offset 'attention) (setq offset cell))
273 (setq label
(concat xr-prefix label
))
274 (when comment
(setq label
(concat "% " label
)))
275 (insert label-indent label
)
279 (- (point) (length label
)) to
281 'font-lock-comment-face
285 (insert (if counter
(format " (%d) " cnt
) "")
286 (if comment
" LABEL IS COMMENTED OUT " "")
287 (if (stringp note
) (concat " " note
) "")
292 (insert context-indent text
"\n")
294 (put-text-property from to
:data cell
)
296 (put-text-property from
(1- to
)
297 'mouse-face mouse-face
))
300 ((eq (car cell
) 'index
)
302 (when (and index-entries
303 (or (eq t index-entries
)
304 (string= index-entries
(nth 1 cell
))))
305 (setq prev-inserted cell
)
306 ; (if (eq offset 'attention) (setq offset cell))
307 (setq index-tag
(format "<%s>" (nth 1 cell
)))
309 (put-text-property 0 (length index-tag
)
310 'face reftex-index-tag-face index-tag
))
311 (insert label-indent index-tag
" " (nth 7 cell
))
316 (- (point) (length (nth 7 cell
))) to
323 (insert context-indent
(nth 2 cell
) "\n")
325 (put-text-property from to
:data cell
)
327 (put-text-property from
(1- to
)
328 'mouse-face mouse-face
))
331 (if (eq cell here-I-am
)
332 (setq offset
'attention
))
333 (if (and prev-inserted
(eq offset
'attention
))
334 (setq offset prev-inserted
))
337 (when (reftex-refontify)
338 ;; we need to fontify the buffer
339 (reftex-fontify-select-label-buffer buf
))
340 (run-hooks 'reftex-display-copied-context-hook
)
343 (defun reftex-find-start-point (fallback &rest locations
)
344 ;; Set point to the first available LOCATION. When a LOCATION is a list,
345 ;; search for such a :data text property. When it is an integer,
346 ;; use is as line number. FALLBACK is a buffer position used if everything
349 (goto-char (point-min))
352 (setq loc
(pop locations
))
356 (setq pos
(text-property-any (point-min) (point-max) :data loc
))
361 (when (<= loc
(count-lines (point-min) (point-max)))
362 (goto-char (point-min))
363 (forward-line (1- loc
))
365 (goto-char fallback
))))
367 (defvar reftex-last-data nil
)
368 (defvar reftex-last-line nil
)
369 (defvar reftex-select-marked nil
)
371 (defun reftex-select-item (prompt help-string keymap
374 ;; Select an item, using PROMPT. The function returns a key indicating
375 ;; an exit status, along with a data structure indicating which item was
377 ;; HELP-STRING contains help. KEYMAP is a keymap with the available
378 ;; selection commands.
379 ;; OFFSET can be a label list item which will be selected at start.
380 ;; When it is t, point will start out at the beginning of the buffer.
381 ;; Any other value will cause restart where last selection left off.
382 ;; When CALL-BACK is given, it is a function which is called with the index
384 ;; CB-FLAG is the initial value of that flag.
386 (let* (ev data last-data
(selection-buffer (current-buffer)))
388 (setq reftex-select-marked nil
)
392 (save-window-excursion
393 (setq truncate-lines t
)
395 ;; Find a good starting point
396 (reftex-find-start-point
397 (point-min) offset reftex-last-data reftex-last-line
)
398 (beginning-of-line 1)
399 (set (make-local-variable 'reftex-last-follow-point
) (point))
403 (use-local-map keymap
)
404 (add-hook 'pre-command-hook
'reftex-select-pre-command-hook nil t
)
405 (add-hook 'post-command-hook
'reftex-select-post-command-hook nil t
)
407 (set-marker reftex-recursive-edit-marker
(point))
408 ;; XEmacs does not run post-command-hook here
409 (and (featurep 'xemacs
) (run-hooks 'post-command-hook
))
412 (set-marker reftex-recursive-edit-marker nil
)
413 (with-current-buffer selection-buffer
415 (remove-hook 'pre-command-hook
'reftex-select-pre-command-hook t
)
416 (remove-hook 'post-command-hook
417 'reftex-select-post-command-hook t
))
418 ;; Kill the mark overlays
419 (mapc (lambda (c) (reftex-delete-overlay (nth 1 c
)))
420 reftex-select-marked
)))))
422 (set (make-local-variable 'reftex-last-line
)
423 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
424 (set (make-local-variable 'reftex-last-data
) last-data
)
425 (reftex-kill-buffer "*RefTeX Help*")
426 (setq reftex-callback-fwd
(not reftex-callback-fwd
)) ;; ;-)))
428 (list ev data last-data
)))
430 ;; The following variables are all bound dynamically in `reftex-select-item'.
431 ;; The defvars are here only to silence the byte compiler.
442 ;; The selection commands
444 (defun reftex-select-pre-command-hook ()
445 (reftex-unhighlight 1)
446 (reftex-unhighlight 0))
448 (defun reftex-select-post-command-hook ()
450 (setq data
(get-text-property (point) :data
))
451 (setq last-data
(or data last-data
))
453 (when (and data cb-flag
454 (not (equal reftex-last-follow-point
(point))))
455 (setq reftex-last-follow-point
(point))
456 (funcall call-back data reftex-callback-fwd
457 (not reftex-revisit-to-follow
)))
459 (setq b
(or (previous-single-property-change
462 e
(or (next-single-property-change
465 (setq b
(point) e
(point)))
466 (and (memq reftex-highlight-selection
'(cursor both
))
467 (reftex-highlight 1 b e
))
468 (if (or (not (pos-visible-in-window-p b
))
469 (not (pos-visible-in-window-p e
)))
471 (unless (current-message)
474 (defun reftex-select-next (&optional arg
)
475 "Move to next selectable item."
477 (setq reftex-callback-fwd t
)
478 (or (eobp) (forward-char 1))
479 (re-search-forward "^[^. \t\n\r]" nil t arg
)
480 (beginning-of-line 1))
481 (defun reftex-select-previous (&optional arg
)
482 "Move to previous selectable item."
484 (setq reftex-callback-fwd nil
)
485 (re-search-backward "^[^. \t\n\r]" nil t arg
))
486 (defun reftex-select-jump (arg)
487 "Jump to a specific section. E.g. '3 z' jumps to section 3.
488 Useful for large TOC's."
490 (goto-char (point-min))
492 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
495 (defun reftex-select-next-heading (&optional arg
)
496 "Move to next table of contentes line."
499 (re-search-forward "^ " nil t arg
)
501 (defun reftex-select-previous-heading (&optional arg
)
502 "Move to previous table of contentes line."
504 (re-search-backward "^ " nil t arg
))
505 (defun reftex-select-quit ()
506 "Abort selection process."
509 (defun reftex-select-keyboard-quit ()
510 "Abort selection process."
513 (defun reftex-select-jump-to-previous ()
514 "Jump back to where previous selection process left off."
518 ((and (local-variable-p 'reftex-last-data
(current-buffer))
520 (setq pos
(text-property-any (point-min) (point-max)
521 :data reftex-last-data
)))
523 ((and (local-variable-p 'reftex-last-line
(current-buffer))
524 (integerp reftex-last-line
))
525 (goto-char (point-min))
526 (forward-line (1- reftex-last-line
)))
528 (defun reftex-select-toggle-follow ()
529 "Toggle follow mode: Other window follows with full context."
531 (setq reftex-last-follow-point -
1)
532 (setq cb-flag
(not cb-flag
)))
533 (defun reftex-select-toggle-varioref ()
534 "Toggle the macro used for referencing the label between \\ref and \\vref."
536 (if (string= refstyle
"\\ref")
537 (setq refstyle
"\\vref")
538 (setq refstyle
"\\ref"))
539 (force-mode-line-update))
540 (defun reftex-select-toggle-fancyref ()
541 "Toggle the macro used for referencing the label between \\ref and \\vref."
544 (cond ((string= refstyle
"\\ref") "\\fref")
545 ((string= refstyle
"\\fref") "\\Fref")
547 (force-mode-line-update))
548 (defun reftex-select-show-insertion-point ()
549 "Show the point from where selection was started in another window."
551 (let ((this-window (selected-window)))
554 (switch-to-buffer-other-window
555 (marker-buffer reftex-select-return-marker
))
556 (goto-char (marker-position reftex-select-return-marker
))
558 (select-window this-window
))))
559 (defun reftex-select-callback ()
560 "Show full context in another window."
562 (if data
(funcall call-back data reftex-callback-fwd nil
) (ding)))
563 (defun reftex-select-accept ()
564 "Accept the currently selected item."
566 (throw 'myexit
'return
))
567 (defun reftex-select-mouse-accept (ev)
568 "Accept the item at the mouse click."
571 (setq data
(get-text-property (point) :data
))
572 (setq last-data
(or data last-data
))
573 (throw 'myexit
'return
))
574 (defun reftex-select-read-label ()
575 "Use minibuffer to read a label to reference, with completion."
577 (let ((label (completing-read
578 "Label: " (symbol-value reftex-docstruct-symbol
)
579 nil nil reftex-prefix
)))
580 (unless (or (equal label
"") (equal label reftex-prefix
))
581 (throw 'myexit label
))))
582 (defun reftex-select-read-cite ()
583 "Use minibuffer to read a citation key with completion."
585 (let* ((key (completing-read "Citation key: " found-list
))
586 (entry (assoc key found-list
)))
588 ((or (null key
) (equal key
"")))
591 (setq last-data data
)
592 (throw 'myexit
'return
))
593 (t (throw 'myexit key
)))))
595 (defun reftex-select-mark (&optional separator
)
598 (let* ((data (get-text-property (point) :data
))
600 (or data
(error "No entry to mark at point"))
601 (if (assq data reftex-select-marked
)
602 (error "Entry is already marked"))
603 (setq boe
(or (previous-single-property-change (1+ (point)) :data
)
605 eoe
(or (next-single-property-change (point) :data
) (point-max)))
606 (setq ovl
(reftex-make-overlay boe eoe
))
607 (push (list data ovl separator
) reftex-select-marked
)
608 (reftex-overlay-put ovl
'face reftex-select-mark-face
)
609 (reftex-overlay-put ovl
'before-string
611 (format "*%c%d* " separator
612 (length reftex-select-marked
))
613 (format "*%d* " (length reftex-select-marked
))))
614 (message "Entry has mark no. %d" (length reftex-select-marked
))))
616 (defun reftex-select-mark-comma ()
617 "Mark the entry and store the `comma' separator."
619 (reftex-select-mark ?
,))
620 (defun reftex-select-mark-to ()
621 "Mark the entry and store the `to' separator."
623 (reftex-select-mark ?-
))
624 (defun reftex-select-mark-and ()
625 "Mark the entry and store `and' to separator."
627 (reftex-select-mark ?
+))
629 (defun reftex-select-unmark ()
632 (let* ((data (get-text-property (point) :data
))
633 (cell (assq data reftex-select-marked
))
638 (error "No marked entry at point"))
639 (and ovl
(reftex-delete-overlay ovl
))
640 (setq reftex-select-marked
(delq cell reftex-select-marked
))
641 (setq cnt
(1+ (length reftex-select-marked
)))
644 (reftex-overlay-put (nth 1 c
) 'before-string
646 (format "*%c%d* " sep
(decf cnt
))
647 (format "*%d* " (decf cnt
)))))
648 reftex-select-marked
)
649 (message "Entry no longer marked")))
651 (defun reftex-select-help ()
652 "Display a summary of the special key bindings."
654 (with-output-to-temp-buffer "*RefTeX Help*"
656 (reftex-enlarge-to-fit "*RefTeX Help*" t
))
658 ;; Common bindings in reftex-select-label-map and reftex-select-bib-map
659 (let ((map (make-sparse-keymap)))
660 (substitute-key-definition
661 'next-line
'reftex-select-next map global-map
)
662 (substitute-key-definition
663 'previous-line
'reftex-select-previous map global-map
)
664 (substitute-key-definition
665 'keyboard-quit
'reftex-select-keyboard-quit map global-map
)
666 (substitute-key-definition
667 'newline
'reftex-select-accept map global-map
)
670 '((" " . reftex-select-callback
)
671 ("n" . reftex-select-next
)
672 ([(down)] . reftex-select-next
)
673 ("p" . reftex-select-previous
)
674 ([(up)] . reftex-select-previous
)
675 ("f" . reftex-select-toggle-follow
)
676 ("\C-m" . reftex-select-accept
)
677 ([(return)] . reftex-select-accept
)
678 ("q" . reftex-select-quit
)
679 ("." . reftex-select-show-insertion-point
)
680 ("?" . reftex-select-help
))
681 do
(define-key map
(car x
) (cdr x
)))
683 ;; The mouse-2 binding
684 (if (featurep 'xemacs
)
685 (define-key map
[(button2)] 'reftex-select-mouse-accept
)
686 (define-key map
[(mouse-2)] 'reftex-select-mouse-accept
)
687 (define-key map
[follow-link
] 'mouse-face
))
691 (loop for key across
"0123456789" do
692 (define-key map
(vector (list key
)) 'digit-argument
))
693 (define-key map
"-" 'negative-argument
)
696 (setq reftex-select-label-map map
)
697 (setq reftex-select-bib-map
(copy-keymap map
)))
699 ;; Specific bindings in reftex-select-label-map
700 (loop for key across
"aAcgFlrRstx#%" do
701 (define-key reftex-select-label-map
(vector (list key
))
703 "Press `?' during selection to find out about this key."
704 '(interactive) (list 'throw
'(quote myexit
) key
))))
707 '(("b" . reftex-select-jump-to-previous
)
708 ("z" . reftex-select-jump
)
709 ("v" . reftex-select-toggle-varioref
)
710 ("V" . reftex-select-toggle-fancyref
)
711 ("m" . reftex-select-mark
)
712 ("u" . reftex-select-unmark
)
713 ("," . reftex-select-mark-comma
)
714 ("-" . reftex-select-mark-to
)
715 ("+" . reftex-select-mark-and
)
716 ([(tab)] . reftex-select-read-label
)
717 ("\C-i" . reftex-select-read-label
)
718 ("\C-c\C-n" . reftex-select-next-heading
)
719 ("\C-c\C-p" . reftex-select-previous-heading
))
721 (define-key reftex-select-label-map
(car x
) (cdr x
)))
723 ;; Specific bindings in reftex-select-bib-map
724 (loop for key across
"grRaAeE" do
725 (define-key reftex-select-bib-map
(vector (list key
))
727 "Press `?' during selection to find out about this key."
728 '(interactive) (list 'throw
'(quote myexit
) key
))))
731 '(("\C-i" . reftex-select-read-cite
)
732 ([(tab)] . reftex-select-read-cite
)
733 ("m" . reftex-select-mark
)
734 ("u" . reftex-select-unmark
))
735 do
(define-key reftex-select-bib-map
(car x
) (cdr x
)))
738 ;; arch-tag: 842078ff-0586-4e0b-957e-536e08218464
739 ;;; reftex-sel.el ends here