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 (make-local-hook 'pre-command-hook
)
53 (make-local-hook 'post-command-hook
)
54 (setq major-mode
'reftex-select-label-mode
56 (set (make-local-variable 'reftex-select-marked
) nil
)
57 (when (syntax-table-p reftex-latex-syntax-table
)
58 (set-syntax-table reftex-latex-syntax-table
))
59 ;; We do not set a local map - reftex-select-item does this.
60 (run-hooks 'reftex-select-label-mode-hook
))
62 (defvar reftex-select-bib-map nil
63 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
64 This keymap can be used to configure the BibTeX selection process which is
65 started with the command \\[reftex-citation].")
67 (defun reftex-select-bib-mode ()
68 "Major mode for selecting a citation key in a LaTeX document.
69 This buffer was created with RefTeX.
70 It only has a meaningful keymap when you are in the middle of a
72 In order to select a citation, move the cursor to it and press RET.
73 Press `?' for a summary of important key bindings.
75 During a selection process, these are the local bindings.
77 \\{reftex-select-label-map}"
79 (kill-all-local-variables)
80 (make-local-hook 'pre-command-hook
)
81 (make-local-hook 'post-command-hook
)
82 (setq major-mode
'reftex-select-bib-mode
84 (set (make-local-variable 'reftex-select-marked
) nil
)
85 ;; We do not set a local map - reftex-select-item does this.
86 (run-hooks 'reftex-select-bib-mode-hook
))
88 (defun reftex-get-offset (buf here-am-I
&optional typekey toc index file
)
89 ;; Find the correct offset data, like insert-docstruct would, but faster.
90 ;; Buffer BUF knows the correct docstruct to use.
91 ;; Basically this finds the first docstruct entry after HERE-I-AM which
92 ;; is of allowed type. The optional arguments specify what is allowed.
96 (reftex-access-scan-info)
97 (let* ((rest (memq here-am-I
(symbol-value reftex-docstruct-symbol
)))
99 (while (setq entry
(pop rest
))
101 (stringp (car entry
))
102 (or (equal typekey
" ")
103 (equal typekey
(nth 1 entry
))))
104 (and toc
(eq (car entry
) 'toc
))
105 (and index
(eq (car entry
) 'index
))
107 (memq (car entry
) '(bof eof file-error
))))
108 (throw 'exit entry
)))
111 (defun reftex-insert-docstruct
112 (buf toc labels index-entries files context counter show-commented
113 here-I-am xr-prefix toc-buffer
)
114 ;; Insert an excerpt of the docstruct list.
115 ;; Return the data property of the entry corresponding to HERE-I-AM.
116 ;; BUF is the buffer which has the correct docstruct-symbol.
117 ;; LABELS non-nil means to include labels into the list.
118 ;; When a string, indicates the label type to include
119 ;; FILES non-nil means to display file boundaries.
120 ;; CONTEXT non-nil means to include label context.
121 ;; COUNTER means to count the labels.
122 ;; SHOW-COMMENTED means to include also labels which are commented out.
123 ;; HERE-I-AM is a member of the docstruct list. The function will return
124 ;; a used member near to this one, as a possible starting point.
125 ;; XR-PREFIX is the prefix to put in front of labels.
126 ;; TOC-BUFFER means this is to fill the toc buffer.
127 (let* ((font (reftex-use-fonts))
133 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
136 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
138 (if (memq reftex-highlight-selection
'(mouse both
))
139 reftex-mouse-selected-face
141 (label-face (reftex-verified-face reftex-label-face
142 'font-lock-constant-face
143 'font-lock-reference-face
))
144 (index-face (reftex-verified-face reftex-index-face
145 'font-lock-constant-face
146 'font-lock-reference-face
))
147 all cell text label typekey note comment master-dir-re
148 offset from to index-tag docstruct-symbol
)
150 ;; Pop to buffer buf to get the correct buffer-local variables
154 ;; Ensure access to scanning info
155 (reftex-access-scan-info)
157 (setq docstruct-symbol reftex-docstruct-symbol
158 all
(symbol-value reftex-docstruct-symbol
)
159 reftex-active-toc nil
161 (concat "\\`" (regexp-quote
162 (file-name-directory (reftex-TeX-master-file))))))
164 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
165 (set (make-local-variable 'reftex-prefix
)
166 (cdr (assoc labels reftex-typekey-to-prefix-alist
)))
167 (if (equal reftex-prefix
" ") (setq reftex-prefix nil
))
169 ;; Walk the docstruct and insert the appropriate stuff
170 (while (setq cell
(pop all
))
175 (if (eq cell here-I-am
) (setq offset
'attention
))
179 ((memq (car cell
) '(bib thebib label-numbers appendix
180 master-dir bibview-cache is-multi xr xr-doc
)))
181 ;; These are currently ignored
183 ((memq (car cell
) '(bof eof file-error
))
184 ;; Beginning or end of a file
186 (if (eq offset
'attention
) (setq offset cell
))
188 " File " (if (string-match master-dir-re
(nth 1 cell
))
189 (substring (nth 1 cell
) (match-end 0))
191 (cond ((eq (car cell
) 'bof
) " starts here\n")
192 ((eq (car cell
) 'eof
) " ends here\n")
193 ((eq (car cell
) 'file-error
) " was not found\n")))
196 (put-text-property from to
197 'face reftex-file-boundary-face
))
200 (put-text-property from
(1- to
)
201 'mouse-face mouse-face
))
202 (put-text-property from to
:data cell
))))
204 ((eq (car cell
) 'toc
)
205 ;; a table of contents entry
207 (<= (nth 5 cell
) reftex-toc-max-level
))
208 (if (eq offset
'attention
) (setq offset cell
))
209 (setq reftex-active-toc cell
)
210 (insert (concat toc-indent
(nth 2 cell
) "\n"))
213 (put-text-property from to
214 'face reftex-section-heading-face
))
217 (put-text-property from
(1- to
)
218 'mouse-face mouse-face
))
219 (put-text-property from to
:data cell
))
222 ((stringp (car cell
))
224 (when (null (nth 2 cell
))
225 ;; No context yet. Quick update.
226 (setcdr cell
(cdr (reftex-label-info-update cell
)))
227 (put docstruct-symbol
'modified t
))
229 (setq label
(car cell
)
237 (string= typekey labels
)
238 (string= labels
" "))
239 (or show-commented
(null comment
)))
241 ;; Yes we want this one
243 (if (eq offset
'attention
) (setq offset cell
))
245 (setq label
(concat xr-prefix label
))
246 (when comment
(setq label
(concat "% " label
)))
247 (insert label-indent label
)
251 (- (point) (length label
)) to
253 'font-lock-comment-face
257 (insert (if counter
(format " (%d) " cnt
) "")
258 (if comment
" LABEL IS COMMENTED OUT " "")
259 (if (stringp note
) (concat " " note
) "")
264 (insert context-indent text
"\n")
266 (put-text-property from to
:data cell
)
268 (put-text-property from
(1- to
)
269 'mouse-face mouse-face
))
272 ((eq (car cell
) 'index
)
274 (when (and index-entries
275 (or (eq t index-entries
)
276 (string= index-entries
(nth 1 cell
))))
277 (if (eq offset
'attention
) (setq offset cell
))
278 (setq index-tag
(format "<%s>" (nth 1 cell
)))
280 (put-text-property 0 (length index-tag
)
281 'face reftex-index-tag-face index-tag
))
282 (insert label-indent index-tag
" " (nth 7 cell
))
287 (- (point) (length (nth 7 cell
))) to
294 (insert context-indent
(nth 2 cell
) "\n")
296 (put-text-property from to
:data cell
)
298 (put-text-property from
(1- to
)
299 'mouse-face mouse-face
))
302 (when (reftex-refontify)
303 ;; we need to fontify the buffer
304 (reftex-fontify-select-label-buffer buf
))
305 (run-hooks 'reftex-display-copied-context-hook
)
308 (defun reftex-find-start-point (fallback &rest locations
)
309 ;; Set point to the first available LOCATION. When a LOCATION is a list,
310 ;; search for such a :data text property. When it is an integer,
311 ;; use is as line number. FALLBACK is a buffer position used if everything
314 (goto-char (point-min))
317 (setq loc
(pop locations
))
321 (setq pos
(text-property-any (point-min) (point-max) :data loc
))
326 (when (<= loc
(count-lines (point-min) (point-max)))
329 (goto-char fallback
))))
331 (defvar reftex-last-data nil
)
332 (defvar reftex-last-line nil
)
333 (defvar reftex-select-marked nil
)
335 (defun reftex-select-item (prompt help-string keymap
338 ;; Select an item, using PROMPT. The function returns a key indicating
339 ;; an exit status, along with a data structure indicating which item was
341 ;; HELP-STRING contains help. KEYMAP is a keymap with the available
342 ;; selection commands.
343 ;; OFFSET can be a label list item which will be selected at start.
344 ;; When it is t, point will start out at the beginning of the buffer.
345 ;; Any other value will cause restart where last selection left off.
346 ;; When CALL-BACK is given, it is a function which is called with the index
348 ;; CB-FLAG is the initial value of that flag.
350 (let* (ev data last-data
(selection-buffer (current-buffer)))
352 (setq reftex-select-marked nil
)
356 (save-window-excursion
357 (setq truncate-lines t
)
359 ;; Find a good starting point
360 (reftex-find-start-point
361 (point-min) offset reftex-last-data reftex-last-line
)
362 (beginning-of-line 1)
363 (set (make-local-variable 'reftex-last-follow-point
) (point))
367 (use-local-map keymap
)
368 (add-hook 'pre-command-hook
'reftex-select-pre-command-hook nil t
)
369 (add-hook 'post-command-hook
'reftex-select-post-command-hook nil t
)
371 (set-marker reftex-recursive-edit-marker
(point))
372 ;; XEmacs does not run post-command-hook here
373 (and (featurep 'xemacs
) (run-hooks 'post-command-hook
))
376 (set-marker reftex-recursive-edit-marker nil
)
378 (set-buffer selection-buffer
)
380 (remove-hook 'pre-command-hook
'reftex-select-pre-command-hook t
)
381 (remove-hook 'post-command-hook
382 'reftex-select-post-command-hook t
))
383 ;; Kill the mark overlays
384 (mapcar (lambda (c) (delete-overlay (nth 1 c
)))
385 reftex-select-marked
)))))
387 (set (make-local-variable 'reftex-last-line
)
388 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
389 (set (make-local-variable 'reftex-last-data
) last-data
)
390 (reftex-kill-buffer "*RefTeX Help*")
391 (setq reftex-callback-fwd
(not reftex-callback-fwd
)) ;; ;-)))
393 (list ev data last-data
)))
395 ;; The following variables are all bound dynamically in `reftex-select-item'.
396 ;; The defvars are here only to silence the byte compiler.
407 ;; The selection commands
409 (defun reftex-select-pre-command-hook ()
410 (reftex-unhighlight 1)
411 (reftex-unhighlight 0))
413 (defun reftex-select-post-command-hook ()
415 (setq data
(get-text-property (point) :data
))
416 (setq last-data
(or data last-data
))
418 (when (and data cb-flag
419 (not (equal reftex-last-follow-point
(point))))
420 (setq reftex-last-follow-point
(point))
421 (funcall call-back data reftex-callback-fwd
422 (not reftex-revisit-to-follow
)))
424 (setq b
(or (previous-single-property-change
427 e
(or (next-single-property-change
430 (setq b
(point) e
(point)))
431 (and (memq reftex-highlight-selection
'(cursor both
))
432 (reftex-highlight 1 b e
))
433 (if (or (not (pos-visible-in-window-p b
))
434 (not (pos-visible-in-window-p e
)))
436 (unless (current-message)
439 (defun reftex-select-next (&optional arg
)
440 "Move to next selectable item."
442 (setq reftex-callback-fwd t
)
443 (or (eobp) (forward-char 1))
444 (re-search-forward "^[^. \t\n\r]" nil t arg
)
445 (beginning-of-line 1))
446 (defun reftex-select-previous (&optional arg
)
447 "Move to previous selectable item."
449 (setq reftex-callback-fwd nil
)
450 (re-search-backward "^[^. \t\n\r]" nil t arg
))
451 (defun reftex-select-jump (arg)
452 "Jump to a specific section. E.g. '3 z' jumps to section 3.
453 Useful for large TOC's."
455 (goto-char (point-min))
457 (concat "^ *" (number-to-string (if (numberp arg
) arg
1)) " ")
460 (defun reftex-select-next-heading (&optional arg
)
461 "Move to next table of contentes line."
464 (re-search-forward "^ " nil t arg
)
466 (defun reftex-select-previous-heading (&optional arg
)
467 "Move to previous table of contentes line."
469 (re-search-backward "^ " nil t arg
))
470 (defun reftex-select-quit ()
471 "Abort selection process."
474 (defun reftex-select-keyboard-quit ()
475 "Abort selection process."
478 (defun reftex-select-jump-to-previous ()
479 "Jump back to where previous selection process left off."
483 ((and (local-variable-p 'reftex-last-data
(current-buffer))
485 (setq pos
(text-property-any (point-min) (point-max)
486 :data reftex-last-data
)))
488 ((and (local-variable-p 'reftex-last-line
(current-buffer))
489 (integerp reftex-last-line
))
490 (goto-line reftex-last-line
))
492 (defun reftex-select-toggle-follow ()
493 "Toggle follow mode: Other window follows with full context."
495 (setq reftex-last-follow-point -
1)
496 (setq cb-flag
(not cb-flag
)))
497 (defun reftex-select-toggle-varioref ()
498 "Toggle the macro used for referencing the label between \\ref and \\vref."
500 (if (string= refstyle
"\\ref")
501 (setq refstyle
"\\vref")
502 (setq refstyle
"\\ref"))
503 (force-mode-line-update))
504 (defun reftex-select-toggle-fancyref ()
505 "Toggle the macro used for referencing the label between \\ref and \\vref."
508 (cond ((string= refstyle
"\\ref") "\\fref")
509 ((string= refstyle
"\\fref") "\\Fref")
511 (force-mode-line-update))
512 (defun reftex-select-show-insertion-point ()
513 "Show the point from where selection was started in another window."
515 (let ((this-window (selected-window)))
518 (switch-to-buffer-other-window
519 (marker-buffer reftex-select-return-marker
))
520 (goto-char (marker-position reftex-select-return-marker
))
522 (select-window this-window
))))
523 (defun reftex-select-callback ()
524 "Show full context in another window."
526 (if data
(funcall call-back data reftex-callback-fwd nil
) (ding)))
527 (defun reftex-select-accept ()
528 "Accept the currently selected item."
530 (throw 'myexit
'return
))
531 (defun reftex-select-mouse-accept (ev)
532 "Accept the item at the mouse click."
535 (setq data
(get-text-property (point) :data
))
536 (setq last-data
(or data last-data
))
537 (throw 'myexit
'return
))
538 (defun reftex-select-read-label ()
539 "Use minibuffer to read a label to reference, with completion."
541 (let ((label (completing-read
542 "Label: " (symbol-value reftex-docstruct-symbol
)
543 nil nil reftex-prefix
)))
544 (unless (or (equal label
"") (equal label reftex-prefix
))
545 (throw 'myexit label
))))
546 (defun reftex-select-read-cite ()
547 "Use minibuffer to read a citation key with completion."
549 (let* ((key (completing-read "Citation key: " found-list
))
550 (entry (assoc key found-list
)))
552 ((or (null key
) (equal key
"")))
555 (setq last-data data
)
556 (throw 'myexit
'return
))
557 (t (throw 'myexit key
)))))
559 (defun reftex-select-mark (&optional separator
)
562 (let* ((data (get-text-property (point) :data
))
564 (or data
(error "No entry to mark at point"))
565 (if (assq data reftex-select-marked
)
566 (error "Entry is already marked"))
567 (setq boe
(or (previous-single-property-change (1+ (point)) :data
)
569 eoe
(or (next-single-property-change (point) :data
) (point-max)))
570 (setq ovl
(make-overlay boe eoe
))
571 (push (list data ovl separator
) reftex-select-marked
)
572 (overlay-put ovl
'face reftex-select-mark-face
)
573 (if (featurep 'xemacs
)
574 ;; before-string property is broken in Emacs
575 (overlay-put ovl
'before-string
577 (format "*%c%d* " separator
578 (length reftex-select-marked
))
579 (format "*%d* " (length reftex-select-marked
)))))
580 (message "Entry has mark no. %d" (length reftex-select-marked
))))
582 (defun reftex-select-mark-comma ()
583 "Mark the entry and store the `comma' separator."
585 (reftex-select-mark ?
,))
586 (defun reftex-select-mark-to ()
587 "Mark the entry and store the `to' separator."
589 (reftex-select-mark ?-
))
590 (defun reftex-select-mark-and ()
591 "Mark the entry and store `and' to separator."
593 (reftex-select-mark ?
+))
595 (defun reftex-select-unmark ()
598 (let* ((data (get-text-property (point) :data
))
599 (cell (assq data reftex-select-marked
))
604 (error "No marked entry at point"))
605 (and ovl
(delete-overlay ovl
))
606 (setq reftex-select-marked
(delq cell reftex-select-marked
))
607 (if (featurep 'xemacs
)
608 ;; before-string property is broken in Emacs
610 (setq cnt
(1+ (length reftex-select-marked
)))
613 (overlay-put (nth 1 c
) 'before-string
615 (format "*%c%d* " sep
(decf cnt
))
616 (format "*%d* " (decf cnt
)))))
617 reftex-select-marked
)))
618 (message "Entry no longer marked")))
620 (defun reftex-select-help ()
621 "Display a summary of the special key bindings."
623 (with-output-to-temp-buffer "*RefTeX Help*"
625 (reftex-enlarge-to-fit "*RefTeX Help*" t
))
627 ;; Common bindings in reftex-select-label-map and reftex-select-bib-map
628 (let ((map (make-sparse-keymap)))
629 (substitute-key-definition
630 'next-line
'reftex-select-next map global-map
)
631 (substitute-key-definition
632 'previous-line
'reftex-select-previous map global-map
)
633 (substitute-key-definition
634 'keyboard-quit
'reftex-select-keyboard-quit map global-map
)
635 (substitute-key-definition
636 'newline
'reftex-select-accept map global-map
)
639 '((" " . reftex-select-callback
)
640 ("n" . reftex-select-next
)
641 ([(down)] . reftex-select-next
)
642 ("p" . reftex-select-previous
)
643 ([(up)] . reftex-select-previous
)
644 ("f" . reftex-select-toggle-follow
)
645 ("\C-m" . reftex-select-accept
)
646 ([(return)] . reftex-select-accept
)
647 ("q" . reftex-select-quit
)
648 ("." . reftex-select-show-insertion-point
)
649 ("?" . reftex-select-help
))
650 do
(define-key map
(car x
) (cdr x
)))
652 ;; The mouse-2 binding
653 (if (featurep 'xemacs
)
654 (define-key map
[(button2)] 'reftex-select-mouse-accept
)
655 (define-key map
[(mouse-2)] 'reftex-select-mouse-accept
))
658 (loop for key across
"0123456789" do
659 (define-key map
(vector (list key
)) 'digit-argument
))
660 (define-key map
"-" 'negative-argument
)
663 (setq reftex-select-label-map map
)
664 (setq reftex-select-bib-map
(copy-keymap map
)))
666 ;; Specific bindings in reftex-select-label-map
667 (loop for key across
"aAcgFlrRstx#%" do
668 (define-key reftex-select-label-map
(vector (list key
))
670 "Press `?' during selection to find out about this key."
671 '(interactive) (list 'throw
'(quote myexit
) key
))))
674 '(("b" . reftex-select-jump-to-previous
)
675 ("z" . reftex-select-jump
)
676 ("v" . reftex-select-toggle-varioref
)
677 ("V" . reftex-select-toggle-fancyref
)
678 ("m" . reftex-select-mark
)
679 ("u" . reftex-select-unmark
)
680 ("," . reftex-select-mark-comma
)
681 ("-" . reftex-select-mark-to
)
682 ("+" . reftex-select-mark-and
)
683 ([(tab)] . reftex-select-read-label
)
684 ("\C-i" . reftex-select-read-label
)
685 ("\C-c\C-n" . reftex-select-next-heading
)
686 ("\C-c\C-p" . reftex-select-previous-heading
))
688 (define-key reftex-select-label-map
(car x
) (cdr x
)))
690 ;; Specific bindings in reftex-select-bib-map
691 (loop for key across
"grRaA" do
692 (define-key reftex-select-bib-map
(vector (list key
))
694 "Press `?' during selection to find out about this key."
695 '(interactive) (list 'throw
'(quote myexit
) key
))))
698 '(("\C-i" . reftex-select-read-cite
)
699 ([(tab)] . reftex-select-read-cite
)
700 ("m" . reftex-select-mark
)
701 ("u" . reftex-select-unmark
))
702 do
(define-key reftex-select-bib-map
(car x
) (cdr x
)))
705 ;;; reftex-sel.el ends here