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>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 (eval-when-compile (require 'cl
))
30 (defvar reftex-select-label-map nil
31 "Keymap used for *RefTeX Select* buffer, when selecting a label.
32 This keymap can be used to configure the label selection process which is
33 started with the command \\[reftex-reference].")
35 (defun reftex-select-label-mode ()
36 "Major mode for selecting a label in a LaTeX document.
37 This buffer was created with RefTeX.
38 It only has a meaningful keymap when you are in the middle of a
40 To select a label, move the cursor to it and press RET.
41 Press `?' for a summary of important key bindings.
43 During a selection process, these are the local bindings.
45 \\{reftex-select-label-map}"
48 (kill-all-local-variables)
49 (make-local-hook 'pre-command-hook
)
50 (make-local-hook 'post-command-hook
)
51 (setq major-mode
'reftex-select-label-mode
53 (set (make-local-variable 'reftex-select-marked
) nil
)
54 (when (syntax-table-p reftex-latex-syntax-table
)
55 (set-syntax-table reftex-latex-syntax-table
))
56 ;; We do not set a local map - reftex-select-item does this.
57 (run-hooks 'reftex-select-label-mode-hook
))
59 (defvar reftex-select-bib-map nil
60 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
61 This keymap can be used to configure the BibTeX selection process which is
62 started with the command \\[reftex-citation].")
64 (defun reftex-select-bib-mode ()
65 "Major mode for selecting a citation key in a LaTeX document.
66 This buffer was created with RefTeX.
67 It only has a meaningful keymap when you are in the middle of a
69 In order to select a citation, move the cursor to it and press RET.
70 Press `?' for a summary of important key bindings.
72 During a selection process, these are the local bindings.
74 \\{reftex-select-label-map}"
76 (kill-all-local-variables)
77 (make-local-hook 'pre-command-hook
)
78 (make-local-hook 'post-command-hook
)
79 (setq major-mode
'reftex-select-bib-mode
81 (set (make-local-variable 'reftex-select-marked
) nil
)
82 ;; We do not set a local map - reftex-select-item does this.
83 (run-hooks 'reftex-select-bib-mode-hook
))
85 (defun reftex-get-offset (buf here-am-I
&optional typekey toc index file
)
86 ;; Find the correct offset data, like insert-docstruct would, but faster.
87 ;; Buffer BUF knows the correct docstruct to use.
88 ;; Basically this finds the first docstruct entry after HERE-I-AM which
89 ;; is of allowed type. The optional arguments specify what is allowed.
93 (reftex-access-scan-info)
94 (let* ((rest (memq here-am-I
(symbol-value reftex-docstruct-symbol
)))
96 (while (setq entry
(pop rest
))
99 (or (equal typekey
" ")
100 (equal typekey
(nth 1 entry
))))
101 (and toc
(eq (car entry
) 'toc
))
102 (and index
(eq (car entry
) 'index
))
104 (memq (car entry
) '(bof eof file-error
))))
105 (throw 'exit entry
)))
108 (defun reftex-insert-docstruct
109 (buf toc labels index-entries files context counter show-commented
110 here-I-am xr-prefix toc-buffer
)
111 ;; Insert an excerpt of the docstruct list.
112 ;; Return the data property of the entry corresponding to HERE-I-AM.
113 ;; BUF is the buffer which has the correct docstruct-symbol.
114 ;; LABELS non-nil means to include labels into the list.
115 ;; When a string, indicates the label type to include
116 ;; FILES non-nil menas to display file boundaries.
117 ;; CONTEXT non-nil means to include label context.
118 ;; COUNTER means to count the labels.
119 ;; SHOW-COMMENTED means to include also labels which are commented out.
120 ;; HERE-I-AM is a member of the docstruct list. The function will return
121 ;; a used member near to this one, as a possible starting point.
122 ;; XR-PREFIX is the prefix to put in front of labels.
123 ;; TOC-BUFFER means this is to fill the toc buffer.
124 (let* ((font (reftex-use-fonts))
130 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
133 (if toc
(make-string (* 7 reftex-level-indent
) ?\
) "")))
135 (if (memq reftex-highlight-selection
'(mouse both
))
136 reftex-mouse-selected-face
138 (label-face (reftex-verified-face reftex-label-face
139 'font-lock-constant-face
140 'font-lock-reference-face
))
141 (index-face (reftex-verified-face reftex-index-face
142 'font-lock-constant-face
143 'font-lock-reference-face
))
144 all cell text label typekey note comment master-dir-re
145 offset from to index-tag docstruct-symbol
)
147 ;; Pop to buffer buf to get the correct buffer-local variables
151 ;; Ensure access to scanning info
152 (reftex-access-scan-info)
154 (setq docstruct-symbol reftex-docstruct-symbol
155 all
(symbol-value reftex-docstruct-symbol
)
156 reftex-active-toc nil
158 (concat "\\`" (regexp-quote
159 (file-name-directory (reftex-TeX-master-file))))))
161 (set (make-local-variable 'reftex-docstruct-symbol
) docstruct-symbol
)
162 (set (make-local-variable 'reftex-prefix
)
163 (cdr (assoc labels reftex-typekey-to-prefix-alist
)))
164 (if (equal reftex-prefix
" ") (setq reftex-prefix nil
))
166 ;; Walk the docstruct and insert the appropriate stuff
167 (while (setq cell
(pop all
))
172 (if (eq cell here-I-am
) (setq offset
'attention
))
176 ((memq (car cell
) '(bib thebib label-numbers appendix
177 master-dir bibview-cache is-multi xr xr-doc
)))
178 ;; These are currently ignored
180 ((memq (car cell
) '(bof eof file-error
))
181 ;; Beginning or end of a file
183 (if (eq offset
'attention
) (setq offset cell
))
185 " File " (if (string-match master-dir-re
(nth 1 cell
))
186 (substring (nth 1 cell
) (match-end 0))
188 (cond ((eq (car cell
) 'bof
) " starts here\n")
189 ((eq (car cell
) 'eof
) " ends here\n")
190 ((eq (car cell
) 'file-error
) " was not found\n")))
193 (put-text-property from to
194 'face reftex-file-boundary-face
))
197 (put-text-property from
(1- to
)
198 'mouse-face mouse-face
))
199 (put-text-property from to
:data cell
))))
201 ((eq (car cell
) 'toc
)
202 ;; a table of contents entry
204 (<= (nth 5 cell
) reftex-toc-max-level
))
205 (if (eq offset
'attention
) (setq offset cell
))
206 (setq reftex-active-toc cell
)
207 (insert (concat toc-indent
(nth 2 cell
) "\n"))
210 (put-text-property from to
211 'face reftex-section-heading-face
))
214 (put-text-property from
(1- to
)
215 'mouse-face mouse-face
))
216 (put-text-property from to
:data cell
))
219 ((stringp (car cell
))
221 (when (null (nth 2 cell
))
222 ;; No context yet. Quick update.
223 (setcdr cell
(cdr (reftex-label-info-update cell
)))
224 (put docstruct-symbol
'modified t
))
226 (setq label
(car cell
)
234 (string= typekey labels
)
235 (string= labels
" "))
236 (or show-commented
(null comment
)))
238 ;; Yes we want this one
240 (if (eq offset
'attention
) (setq offset cell
))
242 (setq label
(concat xr-prefix label
))
243 (when comment
(setq label
(concat "% " label
)))
244 (insert label-indent label
)
248 (- (point) (length label
)) to
250 'font-lock-comment-face
254 (insert (if counter
(format " (%d) " cnt
) "")
255 (if comment
" LABEL IS COMMENTED OUT " "")
256 (if (stringp note
) (concat " " note
) "")
261 (insert context-indent text
"\n")
263 (put-text-property from to
:data cell
)
265 (put-text-property from
(1- to
)
266 'mouse-face mouse-face
))
269 ((eq (car cell
) 'index
)
271 (when (and index-entries
272 (or (eq t index-entries
)
273 (string= index-entries
(nth 1 cell
))))
274 (if (eq offset
'attention
) (setq offset cell
))
275 (setq index-tag
(format "<%s>" (nth 1 cell
)))
277 (put-text-property 0 (length index-tag
)
278 'face reftex-index-tag-face index-tag
))
279 (insert label-indent index-tag
" " (nth 7 cell
))
284 (- (point) (length (nth 7 cell
))) to
291 (insert context-indent
(nth 2 cell
) "\n")
293 (put-text-property from to
:data cell
)
295 (put-text-property from
(1- to
)
296 'mouse-face mouse-face
))
299 (when (reftex-refontify)
300 ;; we need to fontify the buffer
301 (reftex-fontify-select-label-buffer buf
))
302 (run-hooks 'reftex-display-copied-context-hook
)
305 (defun reftex-find-start-point (fallback &rest locations
)
306 ;; Set point to the first available LOCATION. When a LOCATION is a list,
307 ;; search for such a :data text property. When it is an integer,
308 ;; use is as line number. FALLBACK is a buffer position used if everything
311 (goto-char (point-min))
314 (setq loc
(pop locations
))
318 (setq pos
(text-property-any (point-min) (point-max) :data loc
))
323 (when (<= loc
(count-lines (point-min) (point-max)))
326 (goto-char fallback
))))
328 (defvar reftex-last-data nil
)
329 (defvar reftex-last-line nil
)
330 (defvar reftex-select-marked nil
)
332 (defun reftex-select-item (prompt help-string keymap
335 ;; Select an item, using PROMPT. The function returns a key indicating
336 ;; an exit status, along with a data structure indicating which item was
338 ;; HELP-STRING contains help. KEYMAP is a keymap with the available
339 ;; selection commands.
340 ;; OFFSET can be a label list item which will be selected at start.
341 ;; When it is t, point will start out at the beginning of the buffer.
342 ;; Any other value will cause restart where last selection left off.
343 ;; When CALL-BACK is given, it is a function which is called with the index
345 ;; CB-FLAG is the initial value of that flag.
347 (let* (ev data last-data
(selection-buffer (current-buffer)))
349 (setq reftex-select-marked nil
)
353 (save-window-excursion
354 (setq truncate-lines t
)
356 ;; Find a good starting point
357 (reftex-find-start-point
358 (point-min) offset reftex-last-data reftex-last-line
)
359 (beginning-of-line 1)
360 (set (make-local-variable 'reftex-last-follow-point
) (point))
364 (use-local-map keymap
)
365 (add-hook 'pre-command-hook
'reftex-select-pre-command-hook nil t
)
366 (add-hook 'post-command-hook
'reftex-select-post-command-hook nil t
)
368 (set-marker reftex-recursive-edit-marker
(point))
369 ;; XEmacs does not run post-command-hook here
370 (and (featurep 'xemacs
) (run-hooks 'post-command-hook
))
373 (set-marker reftex-recursive-edit-marker nil
)
375 (set-buffer selection-buffer
)
377 (remove-hook 'pre-command-hook
'reftex-select-pre-command-hook t
)
378 (remove-hook 'post-command-hook
379 'reftex-select-post-command-hook t
))
380 ;; Kill the mark overlays
381 (mapcar (lambda (c) (delete-overlay (nth 1 c
)))
382 reftex-select-marked
)))))
384 (set (make-local-variable 'reftex-last-line
)
385 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
386 (set (make-local-variable 'reftex-last-data
) last-data
)
387 (reftex-kill-buffer "*RefTeX Help*")
388 (setq reftex-callback-fwd
(not reftex-callback-fwd
)) ;; ;-)))
390 (list ev data last-data
)))
392 ;; The following variables are all bound dynamically in `reftex-select-item'.
393 ;; The defvars are here only to silence the byte compiler.
404 ;; The selection commands
406 (defun reftex-select-pre-command-hook ()
407 (reftex-unhighlight 1)
408 (reftex-unhighlight 0))
410 (defun reftex-select-post-command-hook ()
412 (setq data
(get-text-property (point) :data
))
413 (setq last-data
(or data last-data
))
415 (when (and data cb-flag
416 (not (equal reftex-last-follow-point
(point))))
417 (setq reftex-last-follow-point
(point))
418 (funcall call-back data reftex-callback-fwd
419 (not reftex-revisit-to-follow
)))
421 (setq b
(or (previous-single-property-change
424 e
(or (next-single-property-change
427 (setq b
(point) e
(point)))
428 (and (memq reftex-highlight-selection
'(cursor both
))
429 (reftex-highlight 1 b e
))
430 (if (or (not (pos-visible-in-window-p b
))
431 (not (pos-visible-in-window-p e
)))
433 (unless (current-message)
436 (defun reftex-select-next (&optional arg
)
437 "Move to next selectable item."
439 (setq reftex-callback-fwd t
)
440 (or (eobp) (forward-char 1))
441 (re-search-forward "^[^. \t\n\r]" nil t arg
)
442 (beginning-of-line 1))
443 (defun reftex-select-previous (&optional arg
)
444 "Move to previous selectable item."
446 (setq reftex-callback-fwd nil
)
447 (re-search-backward "^[^. \t\n\r]" nil t arg
))
448 (defun reftex-select-next-heading (&optional arg
)
449 "Move to next table of contentes line."
452 (re-search-forward "^ " nil t arg
)
454 (defun reftex-select-previous-heading (&optional arg
)
455 "Move to previous table of contentes line."
457 (re-search-backward "^ " nil t arg
))
458 (defun reftex-select-quit ()
459 "Abort selection process."
462 (defun reftex-select-keyboard-quit ()
463 "Abort selection process."
466 (defun reftex-select-jump-to-previous ()
467 "Jump back to where previous selection process left off."
471 ((and (local-variable-p 'reftex-last-data
(current-buffer))
473 (setq pos
(text-property-any (point-min) (point-max)
474 :data reftex-last-data
)))
476 ((and (local-variable-p 'reftex-last-line
(current-buffer))
477 (integerp reftex-last-line
))
478 (goto-line reftex-last-line
))
480 (defun reftex-select-toggle-follow ()
481 "Toggle follow mode: Other window follows with full context."
483 (setq reftex-last-follow-point -
1)
484 (setq cb-flag
(not cb-flag
)))
485 (defun reftex-select-toggle-varioref ()
486 "Toggle the macro used for referencing the label between \\ref and \\vref."
488 (if (string= refstyle
"\\ref")
489 (setq refstyle
"\\vref")
490 (setq refstyle
"\\ref"))
491 (force-mode-line-update))
492 (defun reftex-select-toggle-fancyref ()
493 "Toggle the macro used for referencing the label between \\ref and \\vref."
496 (cond ((string= refstyle
"\\ref") "\\fref")
497 ((string= refstyle
"\\fref") "\\Fref")
499 (force-mode-line-update))
500 (defun reftex-select-show-insertion-point ()
501 "Show the point from where selection was started in another window."
503 (let ((this-window (selected-window)))
506 (switch-to-buffer-other-window
507 (marker-buffer reftex-select-return-marker
))
508 (goto-char (marker-position reftex-select-return-marker
))
510 (select-window this-window
))))
511 (defun reftex-select-callback ()
512 "Show full context in another window."
514 (if data
(funcall call-back data reftex-callback-fwd nil
) (ding)))
515 (defun reftex-select-accept ()
516 "Accept the currently selected item."
518 (throw 'myexit
'return
))
519 (defun reftex-select-mouse-accept (ev)
520 "Accept the item at the mouse click."
523 (setq data
(get-text-property (point) :data
))
524 (setq last-data
(or data last-data
))
525 (throw 'myexit
'return
))
526 (defun reftex-select-read-label ()
527 "Use minibuffer to read a label to reference, with completion."
529 (let ((label (completing-read
530 "Label: " (symbol-value reftex-docstruct-symbol
)
531 nil nil reftex-prefix
)))
532 (unless (or (equal label
"") (equal label reftex-prefix
))
533 (throw 'myexit label
))))
534 (defun reftex-select-read-cite ()
535 "Use minibuffer to read a citation key with completion."
537 (let* ((key (completing-read "Citation key: " found-list
))
538 (entry (assoc key found-list
)))
540 ((or (null key
) (equal key
"")))
543 (setq last-data data
)
544 (throw 'myexit
'return
))
545 (t (throw 'myexit key
)))))
547 (defun reftex-select-mark (&optional separator
)
550 (let* ((data (get-text-property (point) :data
))
552 (or data
(error "No entry to mark at point"))
553 (if (assq data reftex-select-marked
)
554 (error "Entry is already marked"))
555 (setq boe
(or (previous-single-property-change (1+ (point)) :data
)
557 eoe
(or (next-single-property-change (point) :data
) (point-max)))
558 (setq ovl
(make-overlay boe eoe
))
559 (push (list data ovl separator
) reftex-select-marked
)
560 (overlay-put ovl
'face reftex-select-mark-face
)
561 (if (featurep 'xemacs
)
562 ;; before-string property is broken in Emacs
563 (overlay-put ovl
'before-string
565 (format "*%c%d* " separator
566 (length reftex-select-marked
))
567 (format "*%d* " (length reftex-select-marked
)))))
568 (message "Entry has mark no. %d" (length reftex-select-marked
))))
570 (defun reftex-select-mark-comma ()
571 "Mark the entry and store the `comma' separator."
573 (reftex-select-mark ?
,))
574 (defun reftex-select-mark-to ()
575 "Mark the entry and store the `to' separator."
577 (reftex-select-mark ?-
))
578 (defun reftex-select-mark-and ()
579 "Mark the entry and store `and' to separator."
581 (reftex-select-mark ?
+))
583 (defun reftex-select-unmark ()
586 (let* ((data (get-text-property (point) :data
))
587 (cell (assq data reftex-select-marked
))
592 (error "No marked entry at point"))
593 (and ovl
(delete-overlay ovl
))
594 (setq reftex-select-marked
(delq cell reftex-select-marked
))
595 (if (featurep 'xemacs
)
596 ;; before-string property is broken in Emacs
598 (setq cnt
(1+ (length reftex-select-marked
)))
601 (overlay-put (nth 1 c
) 'before-string
603 (format "*%c%d* " sep
(decf cnt
))
604 (format "*%d* " (decf cnt
)))))
605 reftex-select-marked
)))
606 (message "Entry no longer marked")))
608 (defun reftex-select-help ()
609 "Display a summary of the special key bindings."
611 (with-output-to-temp-buffer "*RefTeX Help*"
613 (reftex-enlarge-to-fit "*RefTeX Help*" t
))
615 ;; Common bindings in reftex-select-label-map and reftex-select-bib-map
616 (let ((map (make-sparse-keymap)))
617 (substitute-key-definition
618 'next-line
'reftex-select-next map global-map
)
619 (substitute-key-definition
620 'previous-line
'reftex-select-previous map global-map
)
621 (substitute-key-definition
622 'keyboard-quit
'reftex-select-keyboard-quit map global-map
)
623 (substitute-key-definition
624 'newline
'reftex-select-accept map global-map
)
627 '((" " . reftex-select-callback
)
628 ("n" . reftex-select-next
)
629 ([(down)] . reftex-select-next
)
630 ("p" . reftex-select-previous
)
631 ([(up)] . reftex-select-previous
)
632 ("f" . reftex-select-toggle-follow
)
633 ("\C-m" . reftex-select-accept
)
634 ([(return)] . reftex-select-accept
)
635 ("q" . reftex-select-quit
)
636 ("." . reftex-select-show-insertion-point
)
637 ("?" . reftex-select-help
))
638 do
(define-key map
(car x
) (cdr x
)))
640 ;; The mouse-2 binding
641 (if (featurep 'xemacs
)
642 (define-key map
[(button2)] 'reftex-select-mouse-accept
)
643 (define-key map
[(mouse-2)] 'reftex-select-mouse-accept
))
646 (loop for key across
"0123456789" do
647 (define-key map
(vector (list key
)) 'digit-argument
))
648 (define-key map
"-" 'negative-argument
)
651 (setq reftex-select-label-map map
)
652 (setq reftex-select-bib-map
(copy-keymap map
)))
654 ;; Specific bindings in reftex-select-label-map
655 (loop for key across
"aAcgFlrRstx#%" do
656 (define-key reftex-select-label-map
(vector (list key
))
658 "Press `?' during selection to find out about this key."
659 '(interactive) (list 'throw
'(quote myexit
) key
))))
662 '(("b" . reftex-select-jump-to-previous
)
663 ("v" . reftex-select-toggle-varioref
)
664 ("V" . reftex-select-toggle-fancyref
)
665 ("m" . reftex-select-mark
)
666 ("u" . reftex-select-unmark
)
667 ("," . reftex-select-mark-comma
)
668 ("-" . reftex-select-mark-to
)
669 ("+" . reftex-select-mark-and
)
670 ([(tab)] . reftex-select-read-label
)
671 ("\C-i" . reftex-select-read-label
)
672 ("\C-c\C-n" . reftex-select-next-heading
)
673 ("\C-c\C-p" . reftex-select-previous-heading
))
675 (define-key reftex-select-label-map
(car x
) (cdr x
)))
677 ;; Specific bindings in reftex-select-bib-map
678 (loop for key across
"grRaA" do
679 (define-key reftex-select-bib-map
(vector (list key
))
681 "Press `?' during selection to find out about this key."
682 '(interactive) (list 'throw
'(quote myexit
) key
))))
685 '(("\C-i" . reftex-select-read-cite
)
686 ([(tab)] . reftex-select-read-cite
)
687 ("m" . reftex-select-mark
)
688 ("u" . reftex-select-unmark
))
689 do
(define-key reftex-select-bib-map
(car x
) (cdr x
)))
692 ;;; reftex-sel.el ends here