1 ;;; reftex-sel.el --- the selection modes for RefTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <dominik@science.uva.nl>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl
))
33 (defvar reftex-select-label-map nil
34 "Keymap used for *RefTeX Select* buffer, when selecting a label.
35 This keymap can be used to configure the label selection process which is
36 started with the command \\[reftex-reference].")
38 (defun reftex-select-label-mode ()
39 "Major mode for selecting a label in a LaTeX document.
40 This buffer was created with RefTeX.
41 It only has a meaningful keymap when you are in the middle of a
43 To select a label, move the cursor to it and press RET.
44 Press `?' for a summary of important key bindings.
46 During a selection process, these are the local bindings.
48 \\{reftex-select-label-map}"
51 (kill-all-local-variables)
52 (when (featurep 'xemacs
)
53 ;; XEmacs needs the call to make-local-hook
54 (make-local-hook 'pre-command-hook
)
55 (make-local-hook 'post-command-hook
))
56 (setq major-mode
'reftex-select-label-mode
58 (set (make-local-variable 'reftex-select-marked
) nil
)
59 (when (syntax-table-p reftex-latex-syntax-table
)
60 (set-syntax-table reftex-latex-syntax-table
))
61 ;; We do not set a local map - reftex-select-item does this.
62 (run-hooks 'reftex-select-label-mode-hook
))
64 (defvar reftex-select-bib-map nil
65 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
66 This keymap can be used to configure the BibTeX selection process which is
67 started with the command \\[reftex-citation].")
69 (defun reftex-select-bib-mode ()
70 "Major mode for selecting a citation key in a LaTeX document.
71 This buffer was created with RefTeX.
72 It only has a meaningful keymap when you are in the middle of a
74 In order to select a citation, move the cursor to it and press RET.
75 Press `?' for a summary of important key bindings.
77 During a selection process, these are the local bindings.
79 \\{reftex-select-label-map}"
81 (kill-all-local-variables)
82 (when (featurep 'xemacs
)
83 ;; XEmacs needs the call to make-local-hook
84 (make-local-hook 'pre-command-hook
)
85 (make-local-hook 'post-command-hook
))
86 (setq major-mode
'reftex-select-bib-mode
88 (set (make-local-variable 'reftex-select-marked
) nil
)
89 ;; We do not set a local map - reftex-select-item does this.
90 (run-hooks 'reftex-select-bib-mode-hook
))
92 ;;; (defun reftex-get-offset (buf here-am-I &optional typekey toc index file)
93 ;;; ;; Find the correct offset data, like insert-docstruct would, but faster.
94 ;;; ;; Buffer BUF knows the correct docstruct to use.
95 ;;; ;; Basically this finds the first docstruct entry after HERE-I-AM which
96 ;;; ;; is of allowed type. The optional arguments specify what is allowed.
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.
123 (reftex-access-scan-info)
124 (let* ((rest (symbol-value reftex-docstruct-symbol
))
126 (while (setq entry
(pop rest
))
128 (stringp (car entry
))
129 (or (equal typekey
" ")
130 (equal typekey
(nth 1 entry
))))
131 (and toc
(eq (car entry
) 'toc
))
132 (and index
(eq (car entry
) 'index
))
134 (memq (car entry
) '(bof eof file-error
))))
135 (setq lastentry entry
))
136 (if (eq entry here-am-I
)
137 (throw 'exit
(or lastentry entry
))))
140 (defun reftex-insert-docstruct
141 (buf toc labels index-entries files context counter show-commented
142 here-I-am xr-prefix toc-buffer
)
143 ;; Insert an excerpt of the docstruct list.
144 ;; Return the data property of the entry corresponding to HERE-I-AM.
145 ;; BUF is the buffer which has the correct docstruct-symbol.
146 ;; LABELS non-nil means to include labels into the list.
147 ;; When a string, indicates the label type to include
148 ;; FILES non-nil means to display file boundaries.
149 ;; CONTEXT non-nil means to include label context.
150 ;; COUNTER means to count the labels.
151 ;; SHOW-COMMENTED means to include also labels which are commented out.
152 ;; HERE-I-AM is a member of the docstruct list. The function will return
153 ;; a used member near to this one, as a possible starting point.
154 ;; XR-PREFIX is the prefix to put in front of labels.
155 ;; TOC-BUFFER means this is to fill the toc buffer.
156 (let* ((font (reftex-use-fonts))
162 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
165 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
167 (if (memq reftex-highlight-selection
'(mouse both
))
168 reftex-mouse-selected-face
170 (label-face (reftex-verified-face reftex-label-face
171 'font-lock-constant-face
172 'font-lock-reference-face
))
173 (index-face (reftex-verified-face reftex-index-face
174 'font-lock-constant-face
175 'font-lock-reference-face
))
176 all cell text label typekey note comment master-dir-re
177 prev-inserted offset from to index-tag docstruct-symbol
)
179 ;; Pop to buffer buf to get the correct buffer-local variables
183 ;; Ensure access to scanning info
184 (reftex-access-scan-info)
186 (setq docstruct-symbol reftex-docstruct-symbol
187 all
(symbol-value reftex-docstruct-symbol
)
188 reftex-active-toc nil
190 (concat "\\`" (regexp-quote
191 (file-name-directory (reftex-TeX-master-file))))))
193 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
194 (set (make-local-variable 'reftex-prefix
)
195 (cdr (assoc labels reftex-typekey-to-prefix-alist
)))
196 (if (equal reftex-prefix
" ") (setq reftex-prefix nil
))
198 ;; Walk the docstruct and insert the appropriate stuff
199 (while (setq cell
(pop all
))
206 ((memq (car cell
) '(bib thebib label-numbers appendix
207 master-dir bibview-cache is-multi xr xr-doc
)))
208 ;; These are currently ignored
210 ((memq (car cell
) '(bof eof file-error
))
211 ;; Beginning or end of a file
213 (setq prev-inserted cell
)
214 ; (if (eq offset 'attention) (setq offset cell))
216 " File " (if (string-match master-dir-re
(nth 1 cell
))
217 (substring (nth 1 cell
) (match-end 0))
219 (cond ((eq (car cell
) 'bof
) " starts here\n")
220 ((eq (car cell
) 'eof
) " ends here\n")
221 ((eq (car cell
) 'file-error
) " was not found\n")))
224 (put-text-property from to
225 'face reftex-file-boundary-face
))
228 (put-text-property from
(1- to
)
229 'mouse-face mouse-face
))
230 (put-text-property from to
:data cell
))))
232 ((eq (car cell
) 'toc
)
233 ;; a table of contents entry
235 (<= (nth 5 cell
) reftex-toc-max-level
))
236 (setq prev-inserted cell
)
237 ; (if (eq offset 'attention) (setq offset cell))
238 (setq reftex-active-toc cell
)
239 (insert (concat toc-indent
(nth 2 cell
) "\n"))
242 (put-text-property from to
243 'face reftex-section-heading-face
))
246 (put-text-property from
(1- to
)
247 'mouse-face mouse-face
))
248 (put-text-property from to
:data cell
))
251 ((stringp (car cell
))
253 (when (null (nth 2 cell
))
254 ;; No context yet. Quick update.
255 (setcdr cell
(cdr (reftex-label-info-update cell
)))
256 (put docstruct-symbol
'modified t
))
258 (setq label
(car cell
)
266 (string= typekey labels
)
267 (string= labels
" "))
268 (or show-commented
(null comment
)))
270 ;; Yes we want this one
272 (setq prev-inserted cell
)
273 ; (if (eq offset 'attention) (setq offset cell))
275 (setq label
(concat xr-prefix label
))
276 (when comment
(setq label
(concat "% " label
)))
277 (insert label-indent label
)
281 (- (point) (length label
)) to
283 'font-lock-comment-face
287 (insert (if counter
(format " (%d) " cnt
) "")
288 (if comment
" LABEL IS COMMENTED OUT " "")
289 (if (stringp note
) (concat " " note
) "")
294 (insert context-indent text
"\n")
296 (put-text-property from to
:data cell
)
298 (put-text-property from
(1- to
)
299 'mouse-face mouse-face
))
302 ((eq (car cell
) 'index
)
304 (when (and index-entries
305 (or (eq t index-entries
)
306 (string= index-entries
(nth 1 cell
))))
307 (setq prev-inserted cell
)
308 ; (if (eq offset 'attention) (setq offset cell))
309 (setq index-tag
(format "<%s>" (nth 1 cell
)))
311 (put-text-property 0 (length index-tag
)
312 'face reftex-index-tag-face index-tag
))
313 (insert label-indent index-tag
" " (nth 7 cell
))
318 (- (point) (length (nth 7 cell
))) to
325 (insert context-indent
(nth 2 cell
) "\n")
327 (put-text-property from to
:data cell
)
329 (put-text-property from
(1- to
)
330 'mouse-face mouse-face
))
333 (if (eq cell here-I-am
)
334 (setq offset
'attention
))
335 (if (and prev-inserted
(eq offset
'attention
))
336 (setq offset prev-inserted
))
339 (when (reftex-refontify)
340 ;; we need to fontify the buffer
341 (reftex-fontify-select-label-buffer buf
))
342 (run-hooks 'reftex-display-copied-context-hook
)
345 (defun reftex-find-start-point (fallback &rest locations
)
346 ;; Set point to the first available LOCATION. When a LOCATION is a list,
347 ;; search for such a :data text property. When it is an integer,
348 ;; use is as line number. FALLBACK is a buffer position used if everything
351 (goto-char (point-min))
354 (setq loc
(pop locations
))
358 (setq pos
(text-property-any (point-min) (point-max) :data loc
))
363 (when (<= loc
(count-lines (point-min) (point-max)))
366 (goto-char fallback
))))
368 (defvar reftex-last-data nil
)
369 (defvar reftex-last-line nil
)
370 (defvar reftex-select-marked nil
)
372 (defun reftex-select-item (prompt help-string keymap
375 ;; Select an item, using PROMPT. The function returns a key indicating
376 ;; an exit status, along with a data structure indicating which item was
378 ;; HELP-STRING contains help. KEYMAP is a keymap with the available
379 ;; selection commands.
380 ;; OFFSET can be a label list item which will be selected at start.
381 ;; When it is t, point will start out at the beginning of the buffer.
382 ;; Any other value will cause restart where last selection left off.
383 ;; When CALL-BACK is given, it is a function which is called with the index
385 ;; CB-FLAG is the initial value of that flag.
387 (let* (ev data last-data
(selection-buffer (current-buffer)))
389 (setq reftex-select-marked nil
)
393 (save-window-excursion
394 (setq truncate-lines t
)
396 ;; Find a good starting point
397 (reftex-find-start-point
398 (point-min) offset reftex-last-data reftex-last-line
)
399 (beginning-of-line 1)
400 (set (make-local-variable 'reftex-last-follow-point
) (point))
404 (use-local-map keymap
)
405 (add-hook 'pre-command-hook
'reftex-select-pre-command-hook nil t
)
406 (add-hook 'post-command-hook
'reftex-select-post-command-hook nil t
)
408 (set-marker reftex-recursive-edit-marker
(point))
409 ;; XEmacs does not run post-command-hook here
410 (and (featurep 'xemacs
) (run-hooks 'post-command-hook
))
413 (set-marker reftex-recursive-edit-marker nil
)
415 (set-buffer selection-buffer
)
417 (remove-hook 'pre-command-hook
'reftex-select-pre-command-hook t
)
418 (remove-hook 'post-command-hook
419 'reftex-select-post-command-hook t
))
420 ;; Kill the mark overlays
421 (mapcar (lambda (c) (delete-overlay (nth 1 c
)))
422 reftex-select-marked
)))))
424 (set (make-local-variable 'reftex-last-line
)
425 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
426 (set (make-local-variable 'reftex-last-data
) last-data
)
427 (reftex-kill-buffer "*RefTeX Help*")
428 (setq reftex-callback-fwd
(not reftex-callback-fwd
)) ;; ;-)))
430 (list ev data last-data
)))
432 ;; The following variables are all bound dynamically in `reftex-select-item'.
433 ;; The defvars are here only to silence the byte compiler.
444 ;; The selection commands
446 (defun reftex-select-pre-command-hook ()
447 (reftex-unhighlight 1)
448 (reftex-unhighlight 0))
450 (defun reftex-select-post-command-hook ()
452 (setq data
(get-text-property (point) :data
))
453 (setq last-data
(or data last-data
))
455 (when (and data cb-flag
456 (not (equal reftex-last-follow-point
(point))))
457 (setq reftex-last-follow-point
(point))
458 (funcall call-back data reftex-callback-fwd
459 (not reftex-revisit-to-follow
)))
461 (setq b
(or (previous-single-property-change
464 e
(or (next-single-property-change
467 (setq b
(point) e
(point)))
468 (and (memq reftex-highlight-selection
'(cursor both
))
469 (reftex-highlight 1 b e
))
470 (if (or (not (pos-visible-in-window-p b
))
471 (not (pos-visible-in-window-p e
)))
473 (unless (current-message)
476 (defun reftex-select-next (&optional arg
)
477 "Move to next selectable item."
479 (setq reftex-callback-fwd t
)
480 (or (eobp) (forward-char 1))
481 (re-search-forward "^[^. \t\n\r]" nil t arg
)
482 (beginning-of-line 1))
483 (defun reftex-select-previous (&optional arg
)
484 "Move to previous selectable item."
486 (setq reftex-callback-fwd nil
)
487 (re-search-backward "^[^. \t\n\r]" nil t arg
))
488 (defun reftex-select-jump (arg)
489 "Jump to a specific section. E.g. '3 z' jumps to section 3.
490 Useful for large TOC's."
492 (goto-char (point-min))
494 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
497 (defun reftex-select-next-heading (&optional arg
)
498 "Move to next table of contentes line."
501 (re-search-forward "^ " nil t arg
)
503 (defun reftex-select-previous-heading (&optional arg
)
504 "Move to previous table of contentes line."
506 (re-search-backward "^ " nil t arg
))
507 (defun reftex-select-quit ()
508 "Abort selection process."
511 (defun reftex-select-keyboard-quit ()
512 "Abort selection process."
515 (defun reftex-select-jump-to-previous ()
516 "Jump back to where previous selection process left off."
520 ((and (local-variable-p 'reftex-last-data
(current-buffer))
522 (setq pos
(text-property-any (point-min) (point-max)
523 :data reftex-last-data
)))
525 ((and (local-variable-p 'reftex-last-line
(current-buffer))
526 (integerp reftex-last-line
))
527 (goto-line reftex-last-line
))
529 (defun reftex-select-toggle-follow ()
530 "Toggle follow mode: Other window follows with full context."
532 (setq reftex-last-follow-point -
1)
533 (setq cb-flag
(not cb-flag
)))
534 (defun reftex-select-toggle-varioref ()
535 "Toggle the macro used for referencing the label between \\ref and \\vref."
537 (if (string= refstyle
"\\ref")
538 (setq refstyle
"\\vref")
539 (setq refstyle
"\\ref"))
540 (force-mode-line-update))
541 (defun reftex-select-toggle-fancyref ()
542 "Toggle the macro used for referencing the label between \\ref and \\vref."
545 (cond ((string= refstyle
"\\ref") "\\fref")
546 ((string= refstyle
"\\fref") "\\Fref")
548 (force-mode-line-update))
549 (defun reftex-select-show-insertion-point ()
550 "Show the point from where selection was started in another window."
552 (let ((this-window (selected-window)))
555 (switch-to-buffer-other-window
556 (marker-buffer reftex-select-return-marker
))
557 (goto-char (marker-position reftex-select-return-marker
))
559 (select-window this-window
))))
560 (defun reftex-select-callback ()
561 "Show full context in another window."
563 (if data
(funcall call-back data reftex-callback-fwd nil
) (ding)))
564 (defun reftex-select-accept ()
565 "Accept the currently selected item."
567 (throw 'myexit
'return
))
568 (defun reftex-select-mouse-accept (ev)
569 "Accept the item at the mouse click."
572 (setq data
(get-text-property (point) :data
))
573 (setq last-data
(or data last-data
))
574 (throw 'myexit
'return
))
575 (defun reftex-select-read-label ()
576 "Use minibuffer to read a label to reference, with completion."
578 (let ((label (completing-read
579 "Label: " (symbol-value reftex-docstruct-symbol
)
580 nil nil reftex-prefix
)))
581 (unless (or (equal label
"") (equal label reftex-prefix
))
582 (throw 'myexit label
))))
583 (defun reftex-select-read-cite ()
584 "Use minibuffer to read a citation key with completion."
586 (let* ((key (completing-read "Citation key: " found-list
))
587 (entry (assoc key found-list
)))
589 ((or (null key
) (equal key
"")))
592 (setq last-data data
)
593 (throw 'myexit
'return
))
594 (t (throw 'myexit key
)))))
596 (defun reftex-select-mark (&optional separator
)
599 (let* ((data (get-text-property (point) :data
))
601 (or data
(error "No entry to mark at point"))
602 (if (assq data reftex-select-marked
)
603 (error "Entry is already marked"))
604 (setq boe
(or (previous-single-property-change (1+ (point)) :data
)
606 eoe
(or (next-single-property-change (point) :data
) (point-max)))
607 (setq ovl
(make-overlay boe eoe
))
608 (push (list data ovl separator
) reftex-select-marked
)
609 (overlay-put ovl
'face reftex-select-mark-face
)
610 (if (featurep 'xemacs
)
611 ;; before-string property is broken in Emacs
612 (overlay-put ovl
'before-string
614 (format "*%c%d* " separator
615 (length reftex-select-marked
))
616 (format "*%d* " (length reftex-select-marked
)))))
617 (message "Entry has mark no. %d" (length reftex-select-marked
))))
619 (defun reftex-select-mark-comma ()
620 "Mark the entry and store the `comma' separator."
622 (reftex-select-mark ?
,))
623 (defun reftex-select-mark-to ()
624 "Mark the entry and store the `to' separator."
626 (reftex-select-mark ?-
))
627 (defun reftex-select-mark-and ()
628 "Mark the entry and store `and' to separator."
630 (reftex-select-mark ?
+))
632 (defun reftex-select-unmark ()
635 (let* ((data (get-text-property (point) :data
))
636 (cell (assq data reftex-select-marked
))
641 (error "No marked entry at point"))
642 (and ovl
(delete-overlay ovl
))
643 (setq reftex-select-marked
(delq cell reftex-select-marked
))
644 (if (featurep 'xemacs
)
645 ;; before-string property is broken in Emacs
647 (setq cnt
(1+ (length reftex-select-marked
)))
650 (overlay-put (nth 1 c
) 'before-string
652 (format "*%c%d* " sep
(decf cnt
))
653 (format "*%d* " (decf cnt
)))))
654 reftex-select-marked
)))
655 (message "Entry no longer marked")))
657 (defun reftex-select-help ()
658 "Display a summary of the special key bindings."
660 (with-output-to-temp-buffer "*RefTeX Help*"
662 (reftex-enlarge-to-fit "*RefTeX Help*" t
))
664 ;; Common bindings in reftex-select-label-map and reftex-select-bib-map
665 (let ((map (make-sparse-keymap)))
666 (substitute-key-definition
667 'next-line
'reftex-select-next map global-map
)
668 (substitute-key-definition
669 'previous-line
'reftex-select-previous map global-map
)
670 (substitute-key-definition
671 'keyboard-quit
'reftex-select-keyboard-quit map global-map
)
672 (substitute-key-definition
673 'newline
'reftex-select-accept map global-map
)
676 '((" " . reftex-select-callback
)
677 ("n" . reftex-select-next
)
678 ([(down)] . reftex-select-next
)
679 ("p" . reftex-select-previous
)
680 ([(up)] . reftex-select-previous
)
681 ("f" . reftex-select-toggle-follow
)
682 ("\C-m" . reftex-select-accept
)
683 ([(return)] . reftex-select-accept
)
684 ("q" . reftex-select-quit
)
685 ("." . reftex-select-show-insertion-point
)
686 ("?" . reftex-select-help
))
687 do
(define-key map
(car x
) (cdr x
)))
689 ;; The mouse-2 binding
690 (if (featurep 'xemacs
)
691 (define-key map
[(button2)] 'reftex-select-mouse-accept
)
692 (define-key map
[(mouse-2)] 'reftex-select-mouse-accept
))
695 (loop for key across
"0123456789" do
696 (define-key map
(vector (list key
)) 'digit-argument
))
697 (define-key map
"-" 'negative-argument
)
700 (setq reftex-select-label-map map
)
701 (setq reftex-select-bib-map
(copy-keymap map
)))
703 ;; Specific bindings in reftex-select-label-map
704 (loop for key across
"aAcgFlrRstx#%" do
705 (define-key reftex-select-label-map
(vector (list key
))
707 "Press `?' during selection to find out about this key."
708 '(interactive) (list 'throw
'(quote myexit
) key
))))
711 '(("b" . reftex-select-jump-to-previous
)
712 ("z" . reftex-select-jump
)
713 ("v" . reftex-select-toggle-varioref
)
714 ("V" . reftex-select-toggle-fancyref
)
715 ("m" . reftex-select-mark
)
716 ("u" . reftex-select-unmark
)
717 ("," . reftex-select-mark-comma
)
718 ("-" . reftex-select-mark-to
)
719 ("+" . reftex-select-mark-and
)
720 ([(tab)] . reftex-select-read-label
)
721 ("\C-i" . reftex-select-read-label
)
722 ("\C-c\C-n" . reftex-select-next-heading
)
723 ("\C-c\C-p" . reftex-select-previous-heading
))
725 (define-key reftex-select-label-map
(car x
) (cdr x
)))
727 ;; Specific bindings in reftex-select-bib-map
728 (loop for key across
"grRaA" do
729 (define-key reftex-select-bib-map
(vector (list key
))
731 "Press `?' during selection to find out about this key."
732 '(interactive) (list 'throw
'(quote myexit
) key
))))
735 '(("\C-i" . reftex-select-read-cite
)
736 ([(tab)] . reftex-select-read-cite
)
737 ("m" . reftex-select-mark
)
738 ("u" . reftex-select-unmark
))
739 do
(define-key reftex-select-bib-map
(car x
) (cdr x
)))
742 ;;; arch-tag: 842078ff-0586-4e0b-957e-536e08218464
743 ;;; reftex-sel.el ends here