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, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
31 (eval-when-compile (require 'cl
))
36 (defvar reftex-select-label-map nil
37 "Keymap used for *RefTeX Select* buffer, when selecting a label.
38 This keymap can be used to configure the label selection process which is
39 started with the command \\[reftex-reference].")
41 (defun reftex-select-label-mode ()
42 "Major mode for selecting a label in a LaTeX document.
43 This buffer was created with RefTeX.
44 It only has a meaningful keymap when you are in the middle of a
46 To select a label, move the cursor to it and press RET.
47 Press `?' for a summary of important key bindings.
49 During a selection process, these are the local bindings.
51 \\{reftex-select-label-map}"
54 (kill-all-local-variables)
55 (when (featurep 'xemacs
)
56 ;; XEmacs needs the call to make-local-hook
57 (make-local-hook 'pre-command-hook
)
58 (make-local-hook 'post-command-hook
))
59 (setq major-mode
'reftex-select-label-mode
61 (set (make-local-variable 'reftex-select-marked
) nil
)
62 (when (syntax-table-p reftex-latex-syntax-table
)
63 (set-syntax-table reftex-latex-syntax-table
))
64 ;; We do not set a local map - reftex-select-item does this.
65 (run-hooks 'reftex-select-label-mode-hook
))
67 (defvar reftex-select-bib-map nil
68 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
69 This keymap can be used to configure the BibTeX selection process which is
70 started with the command \\[reftex-citation].")
72 (defun reftex-select-bib-mode ()
73 "Major mode for selecting a citation key in a LaTeX document.
74 This buffer was created with RefTeX.
75 It only has a meaningful keymap when you are in the middle of a
77 In order to select a citation, move the cursor to it and press RET.
78 Press `?' for a summary of important key bindings.
80 During a selection process, these are the local bindings.
82 \\{reftex-select-label-map}"
84 (kill-all-local-variables)
85 (when (featurep 'xemacs
)
86 ;; XEmacs needs the call to make-local-hook
87 (make-local-hook 'pre-command-hook
)
88 (make-local-hook 'post-command-hook
))
89 (setq major-mode
'reftex-select-bib-mode
91 (set (make-local-variable 'reftex-select-marked
) nil
)
92 ;; We do not set a local map - reftex-select-item does this.
93 (run-hooks 'reftex-select-bib-mode-hook
))
95 ;;; (defun reftex-get-offset (buf here-am-I &optional typekey toc index file)
96 ;;; ;; Find the correct offset data, like insert-docstruct would, but faster.
97 ;;; ;; Buffer BUF knows the correct docstruct to use.
98 ;;; ;; Basically this finds the first docstruct entry after HERE-I-AM which
99 ;;; ;; is of allowed type. The optional arguments specify what is allowed.
103 ;;; (reftex-access-scan-info)
104 ;;; (let* ((rest (memq here-am-I (symbol-value reftex-docstruct-symbol)))
106 ;;; (while (setq entry (pop rest))
107 ;;; (if (or (and typekey
108 ;;; (stringp (car entry))
109 ;;; (or (equal typekey " ")
110 ;;; (equal typekey (nth 1 entry))))
111 ;;; (and toc (eq (car entry) 'toc))
112 ;;; (and index (eq (car entry) 'index))
114 ;;; (memq (car entry) '(bof eof file-error))))
115 ;;; (throw 'exit entry)))
118 (defun reftex-get-offset (buf here-am-I
&optional typekey toc index file
)
119 ;; Find the correct offset data, like insert-docstruct would, but faster.
120 ;; Buffer BUF knows the correct docstruct to use.
121 ;; Basically this finds the first docstruct entry before HERE-I-AM which
122 ;; is of allowed type. The optional arguments specify what is allowed.
126 (reftex-access-scan-info)
127 (let* ((rest (symbol-value reftex-docstruct-symbol
))
129 (while (setq entry
(pop rest
))
131 (stringp (car entry
))
132 (or (equal typekey
" ")
133 (equal typekey
(nth 1 entry
))))
134 (and toc
(eq (car entry
) 'toc
))
135 (and index
(eq (car entry
) 'index
))
137 (memq (car entry
) '(bof eof file-error
))))
138 (setq lastentry entry
))
139 (if (eq entry here-am-I
)
140 (throw 'exit
(or lastentry entry
))))
143 (defun reftex-insert-docstruct
144 (buf toc labels index-entries files context counter show-commented
145 here-I-am xr-prefix toc-buffer
)
146 ;; Insert an excerpt of the docstruct list.
147 ;; Return the data property of the entry corresponding to HERE-I-AM.
148 ;; BUF is the buffer which has the correct docstruct-symbol.
149 ;; LABELS non-nil means to include labels into the list.
150 ;; When a string, indicates the label type to include
151 ;; FILES non-nil means to display file boundaries.
152 ;; CONTEXT non-nil means to include label context.
153 ;; COUNTER means to count the labels.
154 ;; SHOW-COMMENTED means to include also labels which are commented out.
155 ;; HERE-I-AM is a member of the docstruct list. The function will return
156 ;; a used member near to this one, as a possible starting point.
157 ;; XR-PREFIX is the prefix to put in front of labels.
158 ;; TOC-BUFFER means this is to fill the toc buffer.
159 (let* ((font (reftex-use-fonts))
165 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
168 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
170 (if (memq reftex-highlight-selection
'(mouse both
))
171 reftex-mouse-selected-face
173 (label-face (reftex-verified-face reftex-label-face
174 'font-lock-constant-face
175 'font-lock-reference-face
))
176 (index-face (reftex-verified-face reftex-index-face
177 'font-lock-constant-face
178 'font-lock-reference-face
))
179 all cell text label typekey note comment master-dir-re
180 prev-inserted offset from to index-tag docstruct-symbol
)
182 ;; Pop to buffer buf to get the correct buffer-local variables
186 ;; Ensure access to scanning info
187 (reftex-access-scan-info)
189 (setq docstruct-symbol reftex-docstruct-symbol
190 all
(symbol-value reftex-docstruct-symbol
)
191 reftex-active-toc nil
193 (concat "\\`" (regexp-quote
194 (file-name-directory (reftex-TeX-master-file))))))
196 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
197 (set (make-local-variable 'reftex-prefix
)
198 (cdr (assoc labels reftex-typekey-to-prefix-alist
)))
199 (if (equal reftex-prefix
" ") (setq reftex-prefix nil
))
201 ;; Walk the docstruct and insert the appropriate stuff
202 (while (setq cell
(pop all
))
209 ((memq (car cell
) '(bib thebib label-numbers appendix
210 master-dir bibview-cache is-multi xr xr-doc
)))
211 ;; These are currently ignored
213 ((memq (car cell
) '(bof eof file-error
))
214 ;; Beginning or end of a file
216 (setq prev-inserted cell
)
217 ; (if (eq offset 'attention) (setq offset cell))
219 " File " (if (string-match master-dir-re
(nth 1 cell
))
220 (substring (nth 1 cell
) (match-end 0))
222 (cond ((eq (car cell
) 'bof
) " starts here\n")
223 ((eq (car cell
) 'eof
) " ends here\n")
224 ((eq (car cell
) 'file-error
) " was not found\n")))
227 (put-text-property from to
228 'face reftex-file-boundary-face
))
231 (put-text-property from
(1- to
)
232 'mouse-face mouse-face
))
233 (put-text-property from to
:data cell
))))
235 ((eq (car cell
) 'toc
)
236 ;; a table of contents entry
238 (<= (nth 5 cell
) reftex-toc-max-level
))
239 (setq prev-inserted cell
)
240 ; (if (eq offset 'attention) (setq offset cell))
241 (setq reftex-active-toc cell
)
242 (insert (concat toc-indent
(nth 2 cell
) "\n"))
245 (put-text-property from to
246 'face reftex-section-heading-face
))
249 (put-text-property from
(1- to
)
250 'mouse-face mouse-face
))
251 (put-text-property from to
:data cell
))
254 ((stringp (car cell
))
256 (when (null (nth 2 cell
))
257 ;; No context yet. Quick update.
258 (setcdr cell
(cdr (reftex-label-info-update cell
)))
259 (put docstruct-symbol
'modified t
))
261 (setq label
(car cell
)
269 (string= typekey labels
)
270 (string= labels
" "))
271 (or show-commented
(null comment
)))
273 ;; Yes we want this one
275 (setq prev-inserted cell
)
276 ; (if (eq offset 'attention) (setq offset cell))
278 (setq label
(concat xr-prefix label
))
279 (when comment
(setq label
(concat "% " label
)))
280 (insert label-indent label
)
284 (- (point) (length label
)) to
286 'font-lock-comment-face
290 (insert (if counter
(format " (%d) " cnt
) "")
291 (if comment
" LABEL IS COMMENTED OUT " "")
292 (if (stringp note
) (concat " " note
) "")
297 (insert context-indent text
"\n")
299 (put-text-property from to
:data cell
)
301 (put-text-property from
(1- to
)
302 'mouse-face mouse-face
))
305 ((eq (car cell
) 'index
)
307 (when (and index-entries
308 (or (eq t index-entries
)
309 (string= index-entries
(nth 1 cell
))))
310 (setq prev-inserted cell
)
311 ; (if (eq offset 'attention) (setq offset cell))
312 (setq index-tag
(format "<%s>" (nth 1 cell
)))
314 (put-text-property 0 (length index-tag
)
315 'face reftex-index-tag-face index-tag
))
316 (insert label-indent index-tag
" " (nth 7 cell
))
321 (- (point) (length (nth 7 cell
))) to
328 (insert context-indent
(nth 2 cell
) "\n")
330 (put-text-property from to
:data cell
)
332 (put-text-property from
(1- to
)
333 'mouse-face mouse-face
))
336 (if (eq cell here-I-am
)
337 (setq offset
'attention
))
338 (if (and prev-inserted
(eq offset
'attention
))
339 (setq offset prev-inserted
))
342 (when (reftex-refontify)
343 ;; we need to fontify the buffer
344 (reftex-fontify-select-label-buffer buf
))
345 (run-hooks 'reftex-display-copied-context-hook
)
348 (defun reftex-find-start-point (fallback &rest locations
)
349 ;; Set point to the first available LOCATION. When a LOCATION is a list,
350 ;; search for such a :data text property. When it is an integer,
351 ;; use is as line number. FALLBACK is a buffer position used if everything
354 (goto-char (point-min))
357 (setq loc
(pop locations
))
361 (setq pos
(text-property-any (point-min) (point-max) :data loc
))
366 (when (<= loc
(count-lines (point-min) (point-max)))
369 (goto-char fallback
))))
371 (defvar reftex-last-data nil
)
372 (defvar reftex-last-line nil
)
373 (defvar reftex-select-marked nil
)
375 (defun reftex-select-item (prompt help-string keymap
378 ;; Select an item, using PROMPT. The function returns a key indicating
379 ;; an exit status, along with a data structure indicating which item was
381 ;; HELP-STRING contains help. KEYMAP is a keymap with the available
382 ;; selection commands.
383 ;; OFFSET can be a label list item which will be selected at start.
384 ;; When it is t, point will start out at the beginning of the buffer.
385 ;; Any other value will cause restart where last selection left off.
386 ;; When CALL-BACK is given, it is a function which is called with the index
388 ;; CB-FLAG is the initial value of that flag.
390 (let* (ev data last-data
(selection-buffer (current-buffer)))
392 (setq reftex-select-marked nil
)
396 (save-window-excursion
397 (setq truncate-lines t
)
399 ;; Find a good starting point
400 (reftex-find-start-point
401 (point-min) offset reftex-last-data reftex-last-line
)
402 (beginning-of-line 1)
403 (set (make-local-variable 'reftex-last-follow-point
) (point))
407 (use-local-map keymap
)
408 (add-hook 'pre-command-hook
'reftex-select-pre-command-hook nil t
)
409 (add-hook 'post-command-hook
'reftex-select-post-command-hook nil t
)
411 (set-marker reftex-recursive-edit-marker
(point))
412 ;; XEmacs does not run post-command-hook here
413 (and (featurep 'xemacs
) (run-hooks 'post-command-hook
))
416 (set-marker reftex-recursive-edit-marker nil
)
418 (set-buffer selection-buffer
)
420 (remove-hook 'pre-command-hook
'reftex-select-pre-command-hook t
)
421 (remove-hook 'post-command-hook
422 'reftex-select-post-command-hook t
))
423 ;; Kill the mark overlays
424 (mapcar (lambda (c) (reftex-delete-overlay (nth 1 c
)))
425 reftex-select-marked
)))))
427 (set (make-local-variable 'reftex-last-line
)
428 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
429 (set (make-local-variable 'reftex-last-data
) last-data
)
430 (reftex-kill-buffer "*RefTeX Help*")
431 (setq reftex-callback-fwd
(not reftex-callback-fwd
)) ;; ;-)))
433 (list ev data last-data
)))
435 ;; The following variables are all bound dynamically in `reftex-select-item'.
436 ;; The defvars are here only to silence the byte compiler.
447 ;; The selection commands
449 (defun reftex-select-pre-command-hook ()
450 (reftex-unhighlight 1)
451 (reftex-unhighlight 0))
453 (defun reftex-select-post-command-hook ()
455 (setq data
(get-text-property (point) :data
))
456 (setq last-data
(or data last-data
))
458 (when (and data cb-flag
459 (not (equal reftex-last-follow-point
(point))))
460 (setq reftex-last-follow-point
(point))
461 (funcall call-back data reftex-callback-fwd
462 (not reftex-revisit-to-follow
)))
464 (setq b
(or (previous-single-property-change
467 e
(or (next-single-property-change
470 (setq b
(point) e
(point)))
471 (and (memq reftex-highlight-selection
'(cursor both
))
472 (reftex-highlight 1 b e
))
473 (if (or (not (pos-visible-in-window-p b
))
474 (not (pos-visible-in-window-p e
)))
476 (unless (current-message)
479 (defun reftex-select-next (&optional arg
)
480 "Move to next selectable item."
482 (setq reftex-callback-fwd t
)
483 (or (eobp) (forward-char 1))
484 (re-search-forward "^[^. \t\n\r]" nil t arg
)
485 (beginning-of-line 1))
486 (defun reftex-select-previous (&optional arg
)
487 "Move to previous selectable item."
489 (setq reftex-callback-fwd nil
)
490 (re-search-backward "^[^. \t\n\r]" nil t arg
))
491 (defun reftex-select-jump (arg)
492 "Jump to a specific section. E.g. '3 z' jumps to section 3.
493 Useful for large TOC's."
495 (goto-char (point-min))
497 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
500 (defun reftex-select-next-heading (&optional arg
)
501 "Move to next table of contentes line."
504 (re-search-forward "^ " nil t arg
)
506 (defun reftex-select-previous-heading (&optional arg
)
507 "Move to previous table of contentes line."
509 (re-search-backward "^ " nil t arg
))
510 (defun reftex-select-quit ()
511 "Abort selection process."
514 (defun reftex-select-keyboard-quit ()
515 "Abort selection process."
518 (defun reftex-select-jump-to-previous ()
519 "Jump back to where previous selection process left off."
523 ((and (local-variable-p 'reftex-last-data
(current-buffer))
525 (setq pos
(text-property-any (point-min) (point-max)
526 :data reftex-last-data
)))
528 ((and (local-variable-p 'reftex-last-line
(current-buffer))
529 (integerp reftex-last-line
))
530 (goto-line reftex-last-line
))
532 (defun reftex-select-toggle-follow ()
533 "Toggle follow mode: Other window follows with full context."
535 (setq reftex-last-follow-point -
1)
536 (setq cb-flag
(not cb-flag
)))
537 (defun reftex-select-toggle-varioref ()
538 "Toggle the macro used for referencing the label between \\ref and \\vref."
540 (if (string= refstyle
"\\ref")
541 (setq refstyle
"\\vref")
542 (setq refstyle
"\\ref"))
543 (force-mode-line-update))
544 (defun reftex-select-toggle-fancyref ()
545 "Toggle the macro used for referencing the label between \\ref and \\vref."
548 (cond ((string= refstyle
"\\ref") "\\fref")
549 ((string= refstyle
"\\fref") "\\Fref")
551 (force-mode-line-update))
552 (defun reftex-select-show-insertion-point ()
553 "Show the point from where selection was started in another window."
555 (let ((this-window (selected-window)))
558 (switch-to-buffer-other-window
559 (marker-buffer reftex-select-return-marker
))
560 (goto-char (marker-position reftex-select-return-marker
))
562 (select-window this-window
))))
563 (defun reftex-select-callback ()
564 "Show full context in another window."
566 (if data
(funcall call-back data reftex-callback-fwd nil
) (ding)))
567 (defun reftex-select-accept ()
568 "Accept the currently selected item."
570 (throw 'myexit
'return
))
571 (defun reftex-select-mouse-accept (ev)
572 "Accept the item at the mouse click."
575 (setq data
(get-text-property (point) :data
))
576 (setq last-data
(or data last-data
))
577 (throw 'myexit
'return
))
578 (defun reftex-select-read-label ()
579 "Use minibuffer to read a label to reference, with completion."
581 (let ((label (completing-read
582 "Label: " (symbol-value reftex-docstruct-symbol
)
583 nil nil reftex-prefix
)))
584 (unless (or (equal label
"") (equal label reftex-prefix
))
585 (throw 'myexit label
))))
586 (defun reftex-select-read-cite ()
587 "Use minibuffer to read a citation key with completion."
589 (let* ((key (completing-read "Citation key: " found-list
))
590 (entry (assoc key found-list
)))
592 ((or (null key
) (equal key
"")))
595 (setq last-data data
)
596 (throw 'myexit
'return
))
597 (t (throw 'myexit key
)))))
599 (defun reftex-select-mark (&optional separator
)
602 (let* ((data (get-text-property (point) :data
))
604 (or data
(error "No entry to mark at point"))
605 (if (assq data reftex-select-marked
)
606 (error "Entry is already marked"))
607 (setq boe
(or (previous-single-property-change (1+ (point)) :data
)
609 eoe
(or (next-single-property-change (point) :data
) (point-max)))
610 (setq ovl
(reftex-make-overlay boe eoe
))
611 (push (list data ovl separator
) reftex-select-marked
)
612 (reftex-overlay-put ovl
'face reftex-select-mark-face
)
613 (reftex-overlay-put ovl
'before-string
615 (format "*%c%d* " separator
616 (length reftex-select-marked
))
617 (format "*%d* " (length reftex-select-marked
))))
618 (message "Entry has mark no. %d" (length reftex-select-marked
))))
620 (defun reftex-select-mark-comma ()
621 "Mark the entry and store the `comma' separator."
623 (reftex-select-mark ?
,))
624 (defun reftex-select-mark-to ()
625 "Mark the entry and store the `to' separator."
627 (reftex-select-mark ?-
))
628 (defun reftex-select-mark-and ()
629 "Mark the entry and store `and' to separator."
631 (reftex-select-mark ?
+))
633 (defun reftex-select-unmark ()
636 (let* ((data (get-text-property (point) :data
))
637 (cell (assq data reftex-select-marked
))
642 (error "No marked entry at point"))
643 (and ovl
(reftex-delete-overlay ovl
))
644 (setq reftex-select-marked
(delq cell reftex-select-marked
))
645 (setq cnt
(1+ (length reftex-select-marked
)))
648 (reftex-overlay-put (nth 1 c
) 'before-string
650 (format "*%c%d* " sep
(decf cnt
))
651 (format "*%d* " (decf cnt
)))))
652 reftex-select-marked
)
653 (message "Entry no longer marked")))
655 (defun reftex-select-help ()
656 "Display a summary of the special key bindings."
658 (with-output-to-temp-buffer "*RefTeX Help*"
660 (reftex-enlarge-to-fit "*RefTeX Help*" t
))
662 ;; Common bindings in reftex-select-label-map and reftex-select-bib-map
663 (let ((map (make-sparse-keymap)))
664 (substitute-key-definition
665 'next-line
'reftex-select-next map global-map
)
666 (substitute-key-definition
667 'previous-line
'reftex-select-previous map global-map
)
668 (substitute-key-definition
669 'keyboard-quit
'reftex-select-keyboard-quit map global-map
)
670 (substitute-key-definition
671 'newline
'reftex-select-accept map global-map
)
674 '((" " . reftex-select-callback
)
675 ("n" . reftex-select-next
)
676 ([(down)] . reftex-select-next
)
677 ("p" . reftex-select-previous
)
678 ([(up)] . reftex-select-previous
)
679 ("f" . reftex-select-toggle-follow
)
680 ("\C-m" . reftex-select-accept
)
681 ([(return)] . reftex-select-accept
)
682 ("q" . reftex-select-quit
)
683 ("." . reftex-select-show-insertion-point
)
684 ("?" . reftex-select-help
))
685 do
(define-key map
(car x
) (cdr x
)))
687 ;; The mouse-2 binding
688 (if (featurep 'xemacs
)
689 (define-key map
[(button2)] 'reftex-select-mouse-accept
)
690 (define-key map
[(mouse-2)] 'reftex-select-mouse-accept
)
691 (define-key map
[follow-link
] 'mouse-face
))
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
"grRaAeE" 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