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 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.
101 ;;; (reftex-access-scan-info)
102 ;;; (let* ((rest (memq here-am-I (symbol-value reftex-docstruct-symbol)))
104 ;;; (while (setq entry (pop rest))
105 ;;; (if (or (and typekey
106 ;;; (stringp (car entry))
107 ;;; (or (equal typekey " ")
108 ;;; (equal typekey (nth 1 entry))))
109 ;;; (and toc (eq (car entry) 'toc))
110 ;;; (and index (eq (car entry) 'index))
112 ;;; (memq (car entry) '(bof eof file-error))))
113 ;;; (throw 'exit entry)))
116 (defun reftex-get-offset (buf here-am-I
&optional typekey toc index file
)
117 ;; Find the correct offset data, like insert-docstruct would, but faster.
118 ;; Buffer BUF knows the correct docstruct to use.
119 ;; Basically this finds the first docstruct entry before HERE-I-AM which
120 ;; is of allowed type. The optional arguments specify what is allowed.
124 (reftex-access-scan-info)
125 (let* ((rest (symbol-value reftex-docstruct-symbol
))
127 (while (setq entry
(pop rest
))
129 (stringp (car entry
))
130 (or (equal typekey
" ")
131 (equal typekey
(nth 1 entry
))))
132 (and toc
(eq (car entry
) 'toc
))
133 (and index
(eq (car entry
) 'index
))
135 (memq (car entry
) '(bof eof file-error
))))
136 (setq lastentry entry
))
137 (if (eq entry here-am-I
)
138 (throw 'exit
(or lastentry entry
))))
141 (defun reftex-insert-docstruct
142 (buf toc labels index-entries files context counter show-commented
143 here-I-am xr-prefix toc-buffer
)
144 ;; Insert an excerpt of the docstruct list.
145 ;; Return the data property of the entry corresponding to HERE-I-AM.
146 ;; BUF is the buffer which has the correct docstruct-symbol.
147 ;; LABELS non-nil means to include labels into the list.
148 ;; When a string, indicates the label type to include
149 ;; FILES non-nil means to display file boundaries.
150 ;; CONTEXT non-nil means to include label context.
151 ;; COUNTER means to count the labels.
152 ;; SHOW-COMMENTED means to include also labels which are commented out.
153 ;; HERE-I-AM is a member of the docstruct list. The function will return
154 ;; a used member near to this one, as a possible starting point.
155 ;; XR-PREFIX is the prefix to put in front of labels.
156 ;; TOC-BUFFER means this is to fill the toc buffer.
157 (let* ((font (reftex-use-fonts))
163 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
166 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
168 (if (memq reftex-highlight-selection
'(mouse both
))
169 reftex-mouse-selected-face
171 (label-face (reftex-verified-face reftex-label-face
172 'font-lock-constant-face
173 'font-lock-reference-face
))
174 (index-face (reftex-verified-face reftex-index-face
175 'font-lock-constant-face
176 'font-lock-reference-face
))
177 all cell text label typekey note comment master-dir-re
178 prev-inserted offset from to index-tag docstruct-symbol
)
180 ;; Pop to buffer buf to get the correct buffer-local variables
184 ;; Ensure access to scanning info
185 (reftex-access-scan-info)
187 (setq docstruct-symbol reftex-docstruct-symbol
188 all
(symbol-value reftex-docstruct-symbol
)
189 reftex-active-toc nil
191 (concat "\\`" (regexp-quote
192 (file-name-directory (reftex-TeX-master-file))))))
194 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
195 (set (make-local-variable 'reftex-prefix
)
196 (cdr (assoc labels reftex-typekey-to-prefix-alist
)))
197 (if (equal reftex-prefix
" ") (setq reftex-prefix nil
))
199 ;; Walk the docstruct and insert the appropriate stuff
200 (while (setq cell
(pop all
))
207 ((memq (car cell
) '(bib thebib label-numbers appendix
208 master-dir bibview-cache is-multi xr xr-doc
)))
209 ;; These are currently ignored
211 ((memq (car cell
) '(bof eof file-error
))
212 ;; Beginning or end of a file
214 (setq prev-inserted cell
)
215 ; (if (eq offset 'attention) (setq offset cell))
217 " File " (if (string-match master-dir-re
(nth 1 cell
))
218 (substring (nth 1 cell
) (match-end 0))
220 (cond ((eq (car cell
) 'bof
) " starts here\n")
221 ((eq (car cell
) 'eof
) " ends here\n")
222 ((eq (car cell
) 'file-error
) " was not found\n")))
225 (put-text-property from to
226 'face reftex-file-boundary-face
))
229 (put-text-property from
(1- to
)
230 'mouse-face mouse-face
))
231 (put-text-property from to
:data cell
))))
233 ((eq (car cell
) 'toc
)
234 ;; a table of contents entry
236 (<= (nth 5 cell
) reftex-toc-max-level
))
237 (setq prev-inserted cell
)
238 ; (if (eq offset 'attention) (setq offset cell))
239 (setq reftex-active-toc cell
)
240 (insert (concat toc-indent
(nth 2 cell
) "\n"))
243 (put-text-property from to
244 'face reftex-section-heading-face
))
247 (put-text-property from
(1- to
)
248 'mouse-face mouse-face
))
249 (put-text-property from to
:data cell
))
252 ((stringp (car cell
))
254 (when (null (nth 2 cell
))
255 ;; No context yet. Quick update.
256 (setcdr cell
(cdr (reftex-label-info-update cell
)))
257 (put docstruct-symbol
'modified t
))
259 (setq label
(car cell
)
267 (string= typekey labels
)
268 (string= labels
" "))
269 (or show-commented
(null comment
)))
271 ;; Yes we want this one
273 (setq prev-inserted cell
)
274 ; (if (eq offset 'attention) (setq offset cell))
276 (setq label
(concat xr-prefix label
))
277 (when comment
(setq label
(concat "% " label
)))
278 (insert label-indent label
)
282 (- (point) (length label
)) to
284 'font-lock-comment-face
288 (insert (if counter
(format " (%d) " cnt
) "")
289 (if comment
" LABEL IS COMMENTED OUT " "")
290 (if (stringp note
) (concat " " note
) "")
295 (insert context-indent text
"\n")
297 (put-text-property from to
:data cell
)
299 (put-text-property from
(1- to
)
300 'mouse-face mouse-face
))
303 ((eq (car cell
) 'index
)
305 (when (and index-entries
306 (or (eq t index-entries
)
307 (string= index-entries
(nth 1 cell
))))
308 (setq prev-inserted cell
)
309 ; (if (eq offset 'attention) (setq offset cell))
310 (setq index-tag
(format "<%s>" (nth 1 cell
)))
312 (put-text-property 0 (length index-tag
)
313 'face reftex-index-tag-face index-tag
))
314 (insert label-indent index-tag
" " (nth 7 cell
))
319 (- (point) (length (nth 7 cell
))) to
326 (insert context-indent
(nth 2 cell
) "\n")
328 (put-text-property from to
:data cell
)
330 (put-text-property from
(1- to
)
331 'mouse-face mouse-face
))
334 (if (eq cell here-I-am
)
335 (setq offset
'attention
))
336 (if (and prev-inserted
(eq offset
'attention
))
337 (setq offset prev-inserted
))
340 (when (reftex-refontify)
341 ;; we need to fontify the buffer
342 (reftex-fontify-select-label-buffer buf
))
343 (run-hooks 'reftex-display-copied-context-hook
)
346 (defun reftex-find-start-point (fallback &rest locations
)
347 ;; Set point to the first available LOCATION. When a LOCATION is a list,
348 ;; search for such a :data text property. When it is an integer,
349 ;; use is as line number. FALLBACK is a buffer position used if everything
352 (goto-char (point-min))
355 (setq loc
(pop locations
))
359 (setq pos
(text-property-any (point-min) (point-max) :data loc
))
364 (when (<= loc
(count-lines (point-min) (point-max)))
367 (goto-char fallback
))))
369 (defvar reftex-last-data nil
)
370 (defvar reftex-last-line nil
)
371 (defvar reftex-select-marked nil
)
373 (defun reftex-select-item (prompt help-string keymap
376 ;; Select an item, using PROMPT. The function returns a key indicating
377 ;; an exit status, along with a data structure indicating which item was
379 ;; HELP-STRING contains help. KEYMAP is a keymap with the available
380 ;; selection commands.
381 ;; OFFSET can be a label list item which will be selected at start.
382 ;; When it is t, point will start out at the beginning of the buffer.
383 ;; Any other value will cause restart where last selection left off.
384 ;; When CALL-BACK is given, it is a function which is called with the index
386 ;; CB-FLAG is the initial value of that flag.
388 (let* (ev data last-data
(selection-buffer (current-buffer)))
390 (setq reftex-select-marked nil
)
394 (save-window-excursion
395 (setq truncate-lines t
)
397 ;; Find a good starting point
398 (reftex-find-start-point
399 (point-min) offset reftex-last-data reftex-last-line
)
400 (beginning-of-line 1)
401 (set (make-local-variable 'reftex-last-follow-point
) (point))
405 (use-local-map keymap
)
406 (add-hook 'pre-command-hook
'reftex-select-pre-command-hook nil t
)
407 (add-hook 'post-command-hook
'reftex-select-post-command-hook nil t
)
409 (set-marker reftex-recursive-edit-marker
(point))
410 ;; XEmacs does not run post-command-hook here
411 (and (featurep 'xemacs
) (run-hooks 'post-command-hook
))
414 (set-marker reftex-recursive-edit-marker nil
)
416 (set-buffer selection-buffer
)
418 (remove-hook 'pre-command-hook
'reftex-select-pre-command-hook t
)
419 (remove-hook 'post-command-hook
420 'reftex-select-post-command-hook t
))
421 ;; Kill the mark overlays
422 (mapc (lambda (c) (reftex-delete-overlay (nth 1 c
)))
423 reftex-select-marked
)))))
425 (set (make-local-variable 'reftex-last-line
)
426 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
427 (set (make-local-variable 'reftex-last-data
) last-data
)
428 (reftex-kill-buffer "*RefTeX Help*")
429 (setq reftex-callback-fwd
(not reftex-callback-fwd
)) ;; ;-)))
431 (list ev data last-data
)))
433 ;; The following variables are all bound dynamically in `reftex-select-item'.
434 ;; The defvars are here only to silence the byte compiler.
445 ;; The selection commands
447 (defun reftex-select-pre-command-hook ()
448 (reftex-unhighlight 1)
449 (reftex-unhighlight 0))
451 (defun reftex-select-post-command-hook ()
453 (setq data
(get-text-property (point) :data
))
454 (setq last-data
(or data last-data
))
456 (when (and data cb-flag
457 (not (equal reftex-last-follow-point
(point))))
458 (setq reftex-last-follow-point
(point))
459 (funcall call-back data reftex-callback-fwd
460 (not reftex-revisit-to-follow
)))
462 (setq b
(or (previous-single-property-change
465 e
(or (next-single-property-change
468 (setq b
(point) e
(point)))
469 (and (memq reftex-highlight-selection
'(cursor both
))
470 (reftex-highlight 1 b e
))
471 (if (or (not (pos-visible-in-window-p b
))
472 (not (pos-visible-in-window-p e
)))
474 (unless (current-message)
477 (defun reftex-select-next (&optional arg
)
478 "Move to next selectable item."
480 (setq reftex-callback-fwd t
)
481 (or (eobp) (forward-char 1))
482 (re-search-forward "^[^. \t\n\r]" nil t arg
)
483 (beginning-of-line 1))
484 (defun reftex-select-previous (&optional arg
)
485 "Move to previous selectable item."
487 (setq reftex-callback-fwd nil
)
488 (re-search-backward "^[^. \t\n\r]" nil t arg
))
489 (defun reftex-select-jump (arg)
490 "Jump to a specific section. E.g. '3 z' jumps to section 3.
491 Useful for large TOC's."
493 (goto-char (point-min))
495 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
498 (defun reftex-select-next-heading (&optional arg
)
499 "Move to next table of contentes line."
502 (re-search-forward "^ " nil t arg
)
504 (defun reftex-select-previous-heading (&optional arg
)
505 "Move to previous table of contentes line."
507 (re-search-backward "^ " nil t arg
))
508 (defun reftex-select-quit ()
509 "Abort selection process."
512 (defun reftex-select-keyboard-quit ()
513 "Abort selection process."
516 (defun reftex-select-jump-to-previous ()
517 "Jump back to where previous selection process left off."
521 ((and (local-variable-p 'reftex-last-data
(current-buffer))
523 (setq pos
(text-property-any (point-min) (point-max)
524 :data reftex-last-data
)))
526 ((and (local-variable-p 'reftex-last-line
(current-buffer))
527 (integerp reftex-last-line
))
528 (goto-line reftex-last-line
))
530 (defun reftex-select-toggle-follow ()
531 "Toggle follow mode: Other window follows with full context."
533 (setq reftex-last-follow-point -
1)
534 (setq cb-flag
(not cb-flag
)))
535 (defun reftex-select-toggle-varioref ()
536 "Toggle the macro used for referencing the label between \\ref and \\vref."
538 (if (string= refstyle
"\\ref")
539 (setq refstyle
"\\vref")
540 (setq refstyle
"\\ref"))
541 (force-mode-line-update))
542 (defun reftex-select-toggle-fancyref ()
543 "Toggle the macro used for referencing the label between \\ref and \\vref."
546 (cond ((string= refstyle
"\\ref") "\\fref")
547 ((string= refstyle
"\\fref") "\\Fref")
549 (force-mode-line-update))
550 (defun reftex-select-show-insertion-point ()
551 "Show the point from where selection was started in another window."
553 (let ((this-window (selected-window)))
556 (switch-to-buffer-other-window
557 (marker-buffer reftex-select-return-marker
))
558 (goto-char (marker-position reftex-select-return-marker
))
560 (select-window this-window
))))
561 (defun reftex-select-callback ()
562 "Show full context in another window."
564 (if data
(funcall call-back data reftex-callback-fwd nil
) (ding)))
565 (defun reftex-select-accept ()
566 "Accept the currently selected item."
568 (throw 'myexit
'return
))
569 (defun reftex-select-mouse-accept (ev)
570 "Accept the item at the mouse click."
573 (setq data
(get-text-property (point) :data
))
574 (setq last-data
(or data last-data
))
575 (throw 'myexit
'return
))
576 (defun reftex-select-read-label ()
577 "Use minibuffer to read a label to reference, with completion."
579 (let ((label (completing-read
580 "Label: " (symbol-value reftex-docstruct-symbol
)
581 nil nil reftex-prefix
)))
582 (unless (or (equal label
"") (equal label reftex-prefix
))
583 (throw 'myexit label
))))
584 (defun reftex-select-read-cite ()
585 "Use minibuffer to read a citation key with completion."
587 (let* ((key (completing-read "Citation key: " found-list
))
588 (entry (assoc key found-list
)))
590 ((or (null key
) (equal key
"")))
593 (setq last-data data
)
594 (throw 'myexit
'return
))
595 (t (throw 'myexit key
)))))
597 (defun reftex-select-mark (&optional separator
)
600 (let* ((data (get-text-property (point) :data
))
602 (or data
(error "No entry to mark at point"))
603 (if (assq data reftex-select-marked
)
604 (error "Entry is already marked"))
605 (setq boe
(or (previous-single-property-change (1+ (point)) :data
)
607 eoe
(or (next-single-property-change (point) :data
) (point-max)))
608 (setq ovl
(reftex-make-overlay boe eoe
))
609 (push (list data ovl separator
) reftex-select-marked
)
610 (reftex-overlay-put ovl
'face reftex-select-mark-face
)
611 (reftex-overlay-put ovl
'before-string
613 (format "*%c%d* " separator
614 (length reftex-select-marked
))
615 (format "*%d* " (length reftex-select-marked
))))
616 (message "Entry has mark no. %d" (length reftex-select-marked
))))
618 (defun reftex-select-mark-comma ()
619 "Mark the entry and store the `comma' separator."
621 (reftex-select-mark ?
,))
622 (defun reftex-select-mark-to ()
623 "Mark the entry and store the `to' separator."
625 (reftex-select-mark ?-
))
626 (defun reftex-select-mark-and ()
627 "Mark the entry and store `and' to separator."
629 (reftex-select-mark ?
+))
631 (defun reftex-select-unmark ()
634 (let* ((data (get-text-property (point) :data
))
635 (cell (assq data reftex-select-marked
))
640 (error "No marked entry at point"))
641 (and ovl
(reftex-delete-overlay ovl
))
642 (setq reftex-select-marked
(delq cell reftex-select-marked
))
643 (setq cnt
(1+ (length reftex-select-marked
)))
646 (reftex-overlay-put (nth 1 c
) 'before-string
648 (format "*%c%d* " sep
(decf cnt
))
649 (format "*%d* " (decf cnt
)))))
650 reftex-select-marked
)
651 (message "Entry no longer marked")))
653 (defun reftex-select-help ()
654 "Display a summary of the special key bindings."
656 (with-output-to-temp-buffer "*RefTeX Help*"
658 (reftex-enlarge-to-fit "*RefTeX Help*" t
))
660 ;; Common bindings in reftex-select-label-map and reftex-select-bib-map
661 (let ((map (make-sparse-keymap)))
662 (substitute-key-definition
663 'next-line
'reftex-select-next map global-map
)
664 (substitute-key-definition
665 'previous-line
'reftex-select-previous map global-map
)
666 (substitute-key-definition
667 'keyboard-quit
'reftex-select-keyboard-quit map global-map
)
668 (substitute-key-definition
669 'newline
'reftex-select-accept map global-map
)
672 '((" " . reftex-select-callback
)
673 ("n" . reftex-select-next
)
674 ([(down)] . reftex-select-next
)
675 ("p" . reftex-select-previous
)
676 ([(up)] . reftex-select-previous
)
677 ("f" . reftex-select-toggle-follow
)
678 ("\C-m" . reftex-select-accept
)
679 ([(return)] . reftex-select-accept
)
680 ("q" . reftex-select-quit
)
681 ("." . reftex-select-show-insertion-point
)
682 ("?" . reftex-select-help
))
683 do
(define-key map
(car x
) (cdr x
)))
685 ;; The mouse-2 binding
686 (if (featurep 'xemacs
)
687 (define-key map
[(button2)] 'reftex-select-mouse-accept
)
688 (define-key map
[(mouse-2)] 'reftex-select-mouse-accept
)
689 (define-key map
[follow-link
] 'mouse-face
))
693 (loop for key across
"0123456789" do
694 (define-key map
(vector (list key
)) 'digit-argument
))
695 (define-key map
"-" 'negative-argument
)
698 (setq reftex-select-label-map map
)
699 (setq reftex-select-bib-map
(copy-keymap map
)))
701 ;; Specific bindings in reftex-select-label-map
702 (loop for key across
"aAcgFlrRstx#%" do
703 (define-key reftex-select-label-map
(vector (list key
))
705 "Press `?' during selection to find out about this key."
706 '(interactive) (list 'throw
'(quote myexit
) key
))))
709 '(("b" . reftex-select-jump-to-previous
)
710 ("z" . reftex-select-jump
)
711 ("v" . reftex-select-toggle-varioref
)
712 ("V" . reftex-select-toggle-fancyref
)
713 ("m" . reftex-select-mark
)
714 ("u" . reftex-select-unmark
)
715 ("," . reftex-select-mark-comma
)
716 ("-" . reftex-select-mark-to
)
717 ("+" . reftex-select-mark-and
)
718 ([(tab)] . reftex-select-read-label
)
719 ("\C-i" . reftex-select-read-label
)
720 ("\C-c\C-n" . reftex-select-next-heading
)
721 ("\C-c\C-p" . reftex-select-previous-heading
))
723 (define-key reftex-select-label-map
(car x
) (cdr x
)))
725 ;; Specific bindings in reftex-select-bib-map
726 (loop for key across
"grRaAeE" do
727 (define-key reftex-select-bib-map
(vector (list key
))
729 "Press `?' during selection to find out about this key."
730 '(interactive) (list 'throw
'(quote myexit
) key
))))
733 '(("\C-i" . reftex-select-read-cite
)
734 ([(tab)] . reftex-select-read-cite
)
735 ("m" . reftex-select-mark
)
736 ("u" . reftex-select-unmark
))
737 do
(define-key reftex-select-bib-map
(car x
) (cdr x
)))
740 ;; arch-tag: 842078ff-0586-4e0b-957e-536e08218464
741 ;;; reftex-sel.el ends here