1 ;;; reftex-sel.el --- the selection modes for RefTeX
2 ;; Copyright (c) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.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 (setq major-mode
'reftex-select-label-mode
54 (set (make-local-variable 'reftex-select-marked
) nil
)
55 (when (syntax-table-p reftex-latex-syntax-table
)
56 (set-syntax-table reftex-latex-syntax-table
))
57 ;; We do not set a local map - reftex-select-item does this.
58 (run-hooks 'reftex-select-label-mode-hook
))
60 (defvar reftex-select-bib-map nil
61 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
62 This keymap can be used to configure the BibTeX selection process which is
63 started with the command \\[reftex-citation].")
65 (defun reftex-select-bib-mode ()
66 "Major mode for selecting a citation key in a LaTeX document.
67 This buffer was created with RefTeX.
68 It only has a meaningful keymap when you are in the middle of a
70 In order to select a citation, move the cursor to it and press RET.
71 Press `?' for a summary of important key bindings.
73 During a selection process, these are the local bindings.
75 \\{reftex-select-label-map}"
77 (kill-all-local-variables)
78 (setq major-mode
'reftex-select-bib-mode
80 (set (make-local-variable 'reftex-select-marked
) nil
)
81 ;; We do not set a local map - reftex-select-item does this.
82 (run-hooks 'reftex-select-bib-mode-hook
))
84 (defun reftex-get-offset (buf here-am-I
&optional typekey toc index file
)
85 ;; Find the correct offset data, like insert-docstruct would, but faster.
86 ;; Buffer BUF knows the correct docstruct to use.
87 ;; Basically this finds the first docstruct entry after HERE-I-AM which
88 ;; is of allowed type. The optional arguments specify what is allowed.
92 (reftex-access-scan-info)
93 (let* ((rest (memq here-am-I
(symbol-value reftex-docstruct-symbol
)))
95 (while (setq entry
(pop rest
))
98 (or (equal typekey
" ")
99 (equal typekey
(nth 1 entry
))))
100 (and toc
(eq (car entry
) 'toc
))
101 (and index
(eq (car entry
) 'index
))
103 (memq (car entry
) '(bof eof file-error
))))
104 (throw 'exit entry
)))
107 (defun reftex-insert-docstruct
108 (buf toc labels index-entries files context counter show-commented
109 here-I-am xr-prefix toc-buffer
)
110 ;; Insert an excerpt of the docstruct list.
111 ;; Return the data property of the entry corresponding to HERE-I-AM.
112 ;; BUF is the buffer which has the correct docstruct-symbol.
113 ;; LABELS non-nil means to include labels into the list.
114 ;; When a string, indicates the label type to include
115 ;; FILES non-nil means to display file boundaries.
116 ;; CONTEXT non-nil means to include label context.
117 ;; COUNTER means to count the labels.
118 ;; SHOW-COMMENTED means to include also labels which are commented out.
119 ;; HERE-I-AM is a member of the docstruct list. The function will return
120 ;; a used member near to this one, as a possible starting point.
121 ;; XR-PREFIX is the prefix to put in front of labels.
122 ;; TOC-BUFFER means this is to fill the toc buffer.
123 (let* ((font (reftex-use-fonts))
129 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
132 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
134 (if (memq reftex-highlight-selection
'(mouse both
))
135 reftex-mouse-selected-face
137 (label-face (reftex-verified-face reftex-label-face
138 'font-lock-constant-face
139 'font-lock-reference-face
))
140 (index-face (reftex-verified-face reftex-index-face
141 'font-lock-constant-face
142 'font-lock-reference-face
))
143 all cell text label typekey note comment master-dir-re
144 offset from to index-tag docstruct-symbol
)
146 ;; Pop to buffer buf to get the correct buffer-local variables
150 ;; Ensure access to scanning info
151 (reftex-access-scan-info)
153 (setq docstruct-symbol reftex-docstruct-symbol
154 all
(symbol-value reftex-docstruct-symbol
)
155 reftex-active-toc nil
157 (concat "\\`" (regexp-quote
158 (file-name-directory (reftex-TeX-master-file))))))
160 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
161 (set (make-local-variable 'reftex-prefix
)
162 (cdr (assoc labels reftex-typekey-to-prefix-alist
)))
163 (if (equal reftex-prefix
" ") (setq reftex-prefix nil
))
165 ;; Walk the docstruct and insert the appropriate stuff
166 (while (setq cell
(pop all
))
171 (if (eq cell here-I-am
) (setq offset
'attention
))
175 ((memq (car cell
) '(bib thebib label-numbers appendix
176 master-dir bibview-cache is-multi xr xr-doc
)))
177 ;; These are currently ignored
179 ((memq (car cell
) '(bof eof file-error
))
180 ;; Beginning or end of a file
182 (if (eq offset
'attention
) (setq offset cell
))
184 " File " (if (string-match master-dir-re
(nth 1 cell
))
185 (substring (nth 1 cell
) (match-end 0))
187 (cond ((eq (car cell
) 'bof
) " starts here\n")
188 ((eq (car cell
) 'eof
) " ends here\n")
189 ((eq (car cell
) 'file-error
) " was not found\n")))
192 (put-text-property from to
193 'face reftex-file-boundary-face
))
196 (put-text-property from
(1- to
)
197 'mouse-face mouse-face
))
198 (put-text-property from to
:data cell
))))
200 ((eq (car cell
) 'toc
)
201 ;; a table of contents entry
203 (<= (nth 5 cell
) reftex-toc-max-level
))
204 (if (eq offset
'attention
) (setq offset cell
))
205 (setq reftex-active-toc cell
)
206 (insert (concat toc-indent
(nth 2 cell
) "\n"))
209 (put-text-property from to
210 'face reftex-section-heading-face
))
213 (put-text-property from
(1- to
)
214 'mouse-face mouse-face
))
215 (put-text-property from to
:data cell
))
218 ((stringp (car cell
))
220 (when (null (nth 2 cell
))
221 ;; No context yet. Quick update.
222 (setcdr cell
(cdr (reftex-label-info-update cell
)))
223 (put docstruct-symbol
'modified t
))
225 (setq label
(car cell
)
233 (string= typekey labels
)
234 (string= labels
" "))
235 (or show-commented
(null comment
)))
237 ;; Yes we want this one
239 (if (eq offset
'attention
) (setq offset cell
))
241 (setq label
(concat xr-prefix label
))
242 (when comment
(setq label
(concat "% " label
)))
243 (insert label-indent label
)
247 (- (point) (length label
)) to
249 'font-lock-comment-face
253 (insert (if counter
(format " (%d) " cnt
) "")
254 (if comment
" LABEL IS COMMENTED OUT " "")
255 (if (stringp note
) (concat " " note
) "")
260 (insert context-indent text
"\n")
262 (put-text-property from to
:data cell
)
264 (put-text-property from
(1- to
)
265 'mouse-face mouse-face
))
268 ((eq (car cell
) 'index
)
270 (when (and index-entries
271 (or (eq t index-entries
)
272 (string= index-entries
(nth 1 cell
))))
273 (if (eq offset
'attention
) (setq offset cell
))
274 (setq index-tag
(format "<%s>" (nth 1 cell
)))
276 (put-text-property 0 (length index-tag
)
277 'face reftex-index-tag-face index-tag
))
278 (insert label-indent index-tag
" " (nth 7 cell
))
283 (- (point) (length (nth 7 cell
))) to
290 (insert context-indent
(nth 2 cell
) "\n")
292 (put-text-property from to
:data cell
)
294 (put-text-property from
(1- to
)
295 'mouse-face mouse-face
))
298 (when (reftex-refontify)
299 ;; we need to fontify the buffer
300 (reftex-fontify-select-label-buffer buf
))
301 (run-hooks 'reftex-display-copied-context-hook
)
304 (defun reftex-find-start-point (fallback &rest locations
)
305 ;; Set point to the first available LOCATION. When a LOCATION is a list,
306 ;; search for such a :data text property. When it is an integer,
307 ;; use is as line number. FALLBACK is a buffer position used if everything
310 (goto-char (point-min))
313 (setq loc
(pop locations
))
317 (setq pos
(text-property-any (point-min) (point-max) :data loc
))
322 (when (<= loc
(count-lines (point-min) (point-max)))
325 (goto-char fallback
))))
327 (defvar reftex-last-data nil
)
328 (defvar reftex-last-line nil
)
329 (defvar reftex-select-marked nil
)
331 (defun reftex-select-item (prompt help-string keymap
334 ;; Select an item, using PROMPT. The function returns a key indicating
335 ;; an exit status, along with a data structure indicating which item was
337 ;; HELP-STRING contains help. KEYMAP is a keymap with the available
338 ;; selection commands.
339 ;; OFFSET can be a label list item which will be selected at start.
340 ;; When it is t, point will start out at the beginning of the buffer.
341 ;; Any other value will cause restart where last selection left off.
342 ;; When CALL-BACK is given, it is a function which is called with the index
344 ;; CB-FLAG is the initial value of that flag.
346 (let* (ev data last-data
(selection-buffer (current-buffer)))
348 (setq reftex-select-marked nil
)
352 (save-window-excursion
353 (setq truncate-lines t
)
355 ;; Find a good starting point
356 (reftex-find-start-point
357 (point-min) offset reftex-last-data reftex-last-line
)
358 (beginning-of-line 1)
359 (set (make-local-variable 'reftex-last-follow-point
) (point))
363 (use-local-map keymap
)
364 (add-hook 'pre-command-hook
'reftex-select-pre-command-hook nil t
)
365 (add-hook 'post-command-hook
'reftex-select-post-command-hook nil t
)
367 (set-marker reftex-recursive-edit-marker
(point))
368 ;; XEmacs does not run post-command-hook here
369 (and (featurep 'xemacs
) (run-hooks 'post-command-hook
))
372 (set-marker reftex-recursive-edit-marker nil
)
374 (set-buffer selection-buffer
)
376 (remove-hook 'pre-command-hook
'reftex-select-pre-command-hook t
)
377 (remove-hook 'post-command-hook
378 'reftex-select-post-command-hook t
))
379 ;; Kill the mark overlays
380 (mapcar (lambda (c) (delete-overlay (nth 1 c
)))
381 reftex-select-marked
)))))
383 (set (make-local-variable 'reftex-last-line
)
384 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
385 (set (make-local-variable 'reftex-last-data
) last-data
)
386 (reftex-kill-buffer "*RefTeX Help*")
387 (setq reftex-callback-fwd
(not reftex-callback-fwd
)) ;; ;-)))
389 (list ev data last-data
)))
391 ;; The following variables are all bound dynamically in `reftex-select-item'.
392 ;; The defvars are here only to silence the byte compiler.
403 ;; The selection commands
405 (defun reftex-select-pre-command-hook ()
406 (reftex-unhighlight 1)
407 (reftex-unhighlight 0))
409 (defun reftex-select-post-command-hook ()
411 (setq data
(get-text-property (point) :data
))
412 (setq last-data
(or data last-data
))
414 (when (and data cb-flag
415 (not (equal reftex-last-follow-point
(point))))
416 (setq reftex-last-follow-point
(point))
417 (funcall call-back data reftex-callback-fwd
418 (not reftex-revisit-to-follow
)))
420 (setq b
(or (previous-single-property-change
423 e
(or (next-single-property-change
426 (setq b
(point) e
(point)))
427 (and (memq reftex-highlight-selection
'(cursor both
))
428 (reftex-highlight 1 b e
))
429 (if (or (not (pos-visible-in-window-p b
))
430 (not (pos-visible-in-window-p e
)))
432 (unless (current-message)
435 (defun reftex-select-next (&optional arg
)
436 "Move to next selectable item."
438 (setq reftex-callback-fwd t
)
439 (or (eobp) (forward-char 1))
440 (re-search-forward "^[^. \t\n\r]" nil t arg
)
441 (beginning-of-line 1))
442 (defun reftex-select-previous (&optional arg
)
443 "Move to previous selectable item."
445 (setq reftex-callback-fwd nil
)
446 (re-search-backward "^[^. \t\n\r]" nil t arg
))
447 (defun reftex-select-jump (arg)
448 "Jump to a specific section. E.g. '3 z' jumps to section 3.
449 Useful for large TOC's."
451 (goto-char (point-min))
453 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
456 (defun reftex-select-next-heading (&optional arg
)
457 "Move to next table of contentes line."
460 (re-search-forward "^ " nil t arg
)
462 (defun reftex-select-previous-heading (&optional arg
)
463 "Move to previous table of contentes line."
465 (re-search-backward "^ " nil t arg
))
466 (defun reftex-select-quit ()
467 "Abort selection process."
470 (defun reftex-select-keyboard-quit ()
471 "Abort selection process."
474 (defun reftex-select-jump-to-previous ()
475 "Jump back to where previous selection process left off."
479 ((and (local-variable-p 'reftex-last-data
(current-buffer))
481 (setq pos
(text-property-any (point-min) (point-max)
482 :data reftex-last-data
)))
484 ((and (local-variable-p 'reftex-last-line
(current-buffer))
485 (integerp reftex-last-line
))
486 (goto-line reftex-last-line
))
488 (defun reftex-select-toggle-follow ()
489 "Toggle follow mode: Other window follows with full context."
491 (setq reftex-last-follow-point -
1)
492 (setq cb-flag
(not cb-flag
)))
493 (defun reftex-select-toggle-varioref ()
494 "Toggle the macro used for referencing the label between \\ref and \\vref."
496 (if (string= refstyle
"\\ref")
497 (setq refstyle
"\\vref")
498 (setq refstyle
"\\ref"))
499 (force-mode-line-update))
500 (defun reftex-select-toggle-fancyref ()
501 "Toggle the macro used for referencing the label between \\ref and \\vref."
504 (cond ((string= refstyle
"\\ref") "\\fref")
505 ((string= refstyle
"\\fref") "\\Fref")
507 (force-mode-line-update))
508 (defun reftex-select-show-insertion-point ()
509 "Show the point from where selection was started in another window."
511 (let ((this-window (selected-window)))
514 (switch-to-buffer-other-window
515 (marker-buffer reftex-select-return-marker
))
516 (goto-char (marker-position reftex-select-return-marker
))
518 (select-window this-window
))))
519 (defun reftex-select-callback ()
520 "Show full context in another window."
522 (if data
(funcall call-back data reftex-callback-fwd nil
) (ding)))
523 (defun reftex-select-accept ()
524 "Accept the currently selected item."
526 (throw 'myexit
'return
))
527 (defun reftex-select-mouse-accept (ev)
528 "Accept the item at the mouse click."
531 (setq data
(get-text-property (point) :data
))
532 (setq last-data
(or data last-data
))
533 (throw 'myexit
'return
))
534 (defun reftex-select-read-label ()
535 "Use minibuffer to read a label to reference, with completion."
537 (let ((label (completing-read
538 "Label: " (symbol-value reftex-docstruct-symbol
)
539 nil nil reftex-prefix
)))
540 (unless (or (equal label
"") (equal label reftex-prefix
))
541 (throw 'myexit label
))))
542 (defun reftex-select-read-cite ()
543 "Use minibuffer to read a citation key with completion."
545 (let* ((key (completing-read "Citation key: " found-list
))
546 (entry (assoc key found-list
)))
548 ((or (null key
) (equal key
"")))
551 (setq last-data data
)
552 (throw 'myexit
'return
))
553 (t (throw 'myexit key
)))))
555 (defun reftex-select-mark (&optional separator
)
558 (let* ((data (get-text-property (point) :data
))
560 (or data
(error "No entry to mark at point"))
561 (if (assq data reftex-select-marked
)
562 (error "Entry is already marked"))
563 (setq boe
(or (previous-single-property-change (1+ (point)) :data
)
565 eoe
(or (next-single-property-change (point) :data
) (point-max)))
566 (setq ovl
(make-overlay boe eoe
))
567 (push (list data ovl separator
) reftex-select-marked
)
568 (overlay-put ovl
'face reftex-select-mark-face
)
569 (if (featurep 'xemacs
)
570 ;; before-string property is broken in Emacs
571 (overlay-put ovl
'before-string
573 (format "*%c%d* " separator
574 (length reftex-select-marked
))
575 (format "*%d* " (length reftex-select-marked
)))))
576 (message "Entry has mark no. %d" (length reftex-select-marked
))))
578 (defun reftex-select-mark-comma ()
579 "Mark the entry and store the `comma' separator."
581 (reftex-select-mark ?
,))
582 (defun reftex-select-mark-to ()
583 "Mark the entry and store the `to' separator."
585 (reftex-select-mark ?-
))
586 (defun reftex-select-mark-and ()
587 "Mark the entry and store `and' to separator."
589 (reftex-select-mark ?
+))
591 (defun reftex-select-unmark ()
594 (let* ((data (get-text-property (point) :data
))
595 (cell (assq data reftex-select-marked
))
600 (error "No marked entry at point"))
601 (and ovl
(delete-overlay ovl
))
602 (setq reftex-select-marked
(delq cell reftex-select-marked
))
603 (if (featurep 'xemacs
)
604 ;; before-string property is broken in Emacs
606 (setq cnt
(1+ (length reftex-select-marked
)))
609 (overlay-put (nth 1 c
) 'before-string
611 (format "*%c%d* " sep
(decf cnt
))
612 (format "*%d* " (decf cnt
)))))
613 reftex-select-marked
)))
614 (message "Entry no longer marked")))
616 (defun reftex-select-help ()
617 "Display a summary of the special key bindings."
619 (with-output-to-temp-buffer "*RefTeX Help*"
621 (reftex-enlarge-to-fit "*RefTeX Help*" t
))
623 ;; Common bindings in reftex-select-label-map and reftex-select-bib-map
624 (let ((map (make-sparse-keymap)))
625 (substitute-key-definition
626 'next-line
'reftex-select-next map global-map
)
627 (substitute-key-definition
628 'previous-line
'reftex-select-previous map global-map
)
629 (substitute-key-definition
630 'keyboard-quit
'reftex-select-keyboard-quit map global-map
)
631 (substitute-key-definition
632 'newline
'reftex-select-accept map global-map
)
635 '((" " . reftex-select-callback
)
636 ("n" . reftex-select-next
)
637 ([(down)] . reftex-select-next
)
638 ("p" . reftex-select-previous
)
639 ([(up)] . reftex-select-previous
)
640 ("f" . reftex-select-toggle-follow
)
641 ("\C-m" . reftex-select-accept
)
642 ([(return)] . reftex-select-accept
)
643 ("q" . reftex-select-quit
)
644 ("." . reftex-select-show-insertion-point
)
645 ("?" . reftex-select-help
))
646 do
(define-key map
(car x
) (cdr x
)))
648 ;; The mouse-2 binding
649 (if (featurep 'xemacs
)
650 (define-key map
[(button2)] 'reftex-select-mouse-accept
)
651 (define-key map
[(mouse-2)] 'reftex-select-mouse-accept
))
654 (loop for key across
"0123456789" do
655 (define-key map
(vector (list key
)) 'digit-argument
))
656 (define-key map
"-" 'negative-argument
)
659 (setq reftex-select-label-map map
)
660 (setq reftex-select-bib-map
(copy-keymap map
)))
662 ;; Specific bindings in reftex-select-label-map
663 (loop for key across
"aAcgFlrRstx#%" do
664 (define-key reftex-select-label-map
(vector (list key
))
666 "Press `?' during selection to find out about this key."
667 '(interactive) (list 'throw
'(quote myexit
) key
))))
670 '(("b" . reftex-select-jump-to-previous
)
671 ("z" . reftex-select-jump
)
672 ("v" . reftex-select-toggle-varioref
)
673 ("V" . reftex-select-toggle-fancyref
)
674 ("m" . reftex-select-mark
)
675 ("u" . reftex-select-unmark
)
676 ("," . reftex-select-mark-comma
)
677 ("-" . reftex-select-mark-to
)
678 ("+" . reftex-select-mark-and
)
679 ([(tab)] . reftex-select-read-label
)
680 ("\C-i" . reftex-select-read-label
)
681 ("\C-c\C-n" . reftex-select-next-heading
)
682 ("\C-c\C-p" . reftex-select-previous-heading
))
684 (define-key reftex-select-label-map
(car x
) (cdr x
)))
686 ;; Specific bindings in reftex-select-bib-map
687 (loop for key across
"grRaA" do
688 (define-key reftex-select-bib-map
(vector (list key
))
690 "Press `?' during selection to find out about this key."
691 '(interactive) (list 'throw
'(quote myexit
) key
))))
694 '(("\C-i" . reftex-select-read-cite
)
695 ([(tab)] . reftex-select-read-cite
)
696 ("m" . reftex-select-mark
)
697 ("u" . reftex-select-unmark
))
698 do
(define-key reftex-select-bib-map
(car x
) (cdr x
)))
701 ;;; reftex-sel.el ends here