Use forward-line rather than goto-line.
[bpt/emacs.git] / lisp / textmodes / reftex-sel.el
CommitLineData
3afbc435 1;;; reftex-sel.el --- the selection modes for RefTeX
d5a2f2b0
GM
2
3;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
ae940284 4;; 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3ba2590f 5
6fbeb429 6;; Author: Carsten Dominik <dominik@science.uva.nl>
ce545621 7;; Maintainer: auctex-devel@gnu.org
5d2a58e0 8;; Version: 4.31
3ba2590f
RS
9
10;; This file is part of GNU Emacs.
11
1fecc8fe 12;; GNU Emacs is free software: you can redistribute it and/or modify
3ba2590f 13;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
3ba2590f
RS
16
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.
21
22;; You should have received a copy of the GNU General Public License
1fecc8fe 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1a9461d0 24
3afbc435
PJ
25;;; Commentary:
26
27;;; Code:
28
7c4d13cc 29(eval-when-compile (require 'cl))
1a9461d0
CD
30(provide 'reftex-sel)
31(require 'reftex)
32;;;
33
34(defvar reftex-select-label-map nil
35 "Keymap used for *RefTeX Select* buffer, when selecting a label.
36This keymap can be used to configure the label selection process which is
37started with the command \\[reftex-reference].")
38
39(defun reftex-select-label-mode ()
40 "Major mode for selecting a label in a LaTeX document.
41This buffer was created with RefTeX.
e6ce8c42 42It only has a meaningful keymap when you are in the middle of a
1a9461d0
CD
43selection process.
44To select a label, move the cursor to it and press RET.
45Press `?' for a summary of important key bindings.
46
47During a selection process, these are the local bindings.
48
49\\{reftex-select-label-map}"
50
51 (interactive)
52 (kill-all-local-variables)
7ac7387b
CD
53 (when (featurep 'xemacs)
54 ;; XEmacs needs the call to make-local-hook
55 (make-local-hook 'pre-command-hook)
56 (make-local-hook 'post-command-hook))
1a9461d0 57 (setq major-mode 'reftex-select-label-mode
3666daf6 58 mode-name "LSelect")
1a9461d0
CD
59 (set (make-local-variable 'reftex-select-marked) nil)
60 (when (syntax-table-p reftex-latex-syntax-table)
61 (set-syntax-table reftex-latex-syntax-table))
62 ;; We do not set a local map - reftex-select-item does this.
7b07114a 63 (run-hooks 'reftex-select-label-mode-hook))
1a9461d0
CD
64
65(defvar reftex-select-bib-map nil
66 "Keymap used for *RefTeX Select* buffer, when selecting a BibTeX entry.
67This keymap can be used to configure the BibTeX selection process which is
68started with the command \\[reftex-citation].")
69
70(defun reftex-select-bib-mode ()
71 "Major mode for selecting a citation key in a LaTeX document.
72This buffer was created with RefTeX.
e6ce8c42 73It only has a meaningful keymap when you are in the middle of a
1a9461d0
CD
74selection process.
75In order to select a citation, move the cursor to it and press RET.
76Press `?' for a summary of important key bindings.
77
78During a selection process, these are the local bindings.
79
80\\{reftex-select-label-map}"
81 (interactive)
82 (kill-all-local-variables)
7ac7387b
CD
83 (when (featurep 'xemacs)
84 ;; XEmacs needs the call to make-local-hook
85 (make-local-hook 'pre-command-hook)
86 (make-local-hook 'post-command-hook))
1a9461d0 87 (setq major-mode 'reftex-select-bib-mode
3666daf6 88 mode-name "BSelect")
1a9461d0
CD
89 (set (make-local-variable 'reftex-select-marked) nil)
90 ;; We do not set a local map - reftex-select-item does this.
7b07114a 91 (run-hooks 'reftex-select-bib-mode-hook))
1a9461d0 92
3b919c9f
CD
93;;; (defun reftex-get-offset (buf here-am-I &optional typekey toc index file)
94;;; ;; Find the correct offset data, like insert-docstruct would, but faster.
95;;; ;; Buffer BUF knows the correct docstruct to use.
96;;; ;; Basically this finds the first docstruct entry after HERE-I-AM which
97;;; ;; is of allowed type. The optional arguments specify what is allowed.
98;;; (catch 'exit
99;;; (save-excursion
100;;; (set-buffer buf)
101;;; (reftex-access-scan-info)
102;;; (let* ((rest (memq here-am-I (symbol-value reftex-docstruct-symbol)))
3666daf6
CD
103;;; entry)
104;;; (while (setq entry (pop rest))
105;;; (if (or (and typekey
106;;; (stringp (car entry))
107;;; (or (equal typekey " ")
108;;; (equal typekey (nth 1 entry))))
109;;; (and toc (eq (car entry) 'toc))
110;;; (and index (eq (car entry) 'index))
111;;; (and file
112;;; (memq (car entry) '(bof eof file-error))))
113;;; (throw 'exit entry)))
114;;; nil))))
3b919c9f 115
1a9461d0
CD
116(defun reftex-get-offset (buf here-am-I &optional typekey toc index file)
117 ;; Find the correct offset data, like insert-docstruct would, but faster.
118 ;; Buffer BUF knows the correct docstruct to use.
3b919c9f 119 ;; Basically this finds the first docstruct entry before HERE-I-AM which
1a9461d0
CD
120 ;; is of allowed type. The optional arguments specify what is allowed.
121 (catch 'exit
122 (save-excursion
123 (set-buffer buf)
124 (reftex-access-scan-info)
3b919c9f 125 (let* ((rest (symbol-value reftex-docstruct-symbol))
3666daf6
CD
126 lastentry entry)
127 (while (setq entry (pop rest))
128 (if (or (and typekey
129 (stringp (car entry))
130 (or (equal typekey " ")
131 (equal typekey (nth 1 entry))))
132 (and toc (eq (car entry) 'toc))
133 (and index (eq (car entry) 'index))
134 (and file
135 (memq (car entry) '(bof eof file-error))))
136 (setq lastentry entry))
137 (if (eq entry here-am-I)
138 (throw 'exit (or lastentry entry))))
139 nil))))
1a9461d0
CD
140
141(defun reftex-insert-docstruct
142 (buf toc labels index-entries files context counter show-commented
3666daf6 143 here-I-am xr-prefix toc-buffer)
1a9461d0
CD
144 ;; Insert an excerpt of the docstruct list.
145 ;; Return the data property of the entry corresponding to HERE-I-AM.
146 ;; BUF is the buffer which has the correct docstruct-symbol.
147 ;; LABELS non-nil means to include labels into the list.
148 ;; When a string, indicates the label type to include
11b4a0d2 149 ;; FILES non-nil means to display file boundaries.
1a9461d0
CD
150 ;; CONTEXT non-nil means to include label context.
151 ;; COUNTER means to count the labels.
152 ;; SHOW-COMMENTED means to include also labels which are commented out.
153 ;; HERE-I-AM is a member of the docstruct list. The function will return
154 ;; a used member near to this one, as a possible starting point.
155 ;; XR-PREFIX is the prefix to put in front of labels.
156 ;; TOC-BUFFER means this is to fill the toc buffer.
157 (let* ((font (reftex-use-fonts))
158 (cnt 0)
159 (index -1)
160 (toc-indent " ")
161 (label-indent
162 (concat "> "
163 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
164 (context-indent
165 (concat ". "
166 (if toc (make-string (* 7 reftex-level-indent) ?\ ) "")))
3666daf6
CD
167 (mouse-face
168 (if (memq reftex-highlight-selection '(mouse both))
169 reftex-mouse-selected-face
170 nil))
171 (label-face (reftex-verified-face reftex-label-face
172 'font-lock-constant-face
173 'font-lock-reference-face))
174 (index-face (reftex-verified-face reftex-index-face
175 'font-lock-constant-face
176 'font-lock-reference-face))
1a9461d0 177 all cell text label typekey note comment master-dir-re
3666daf6 178 prev-inserted offset from to index-tag docstruct-symbol)
1a9461d0
CD
179
180 ;; Pop to buffer buf to get the correct buffer-local variables
181 (save-excursion
182 (set-buffer buf)
183
184 ;; Ensure access to scanning info
185 (reftex-access-scan-info)
186
187 (setq docstruct-symbol reftex-docstruct-symbol
3666daf6 188 all (symbol-value reftex-docstruct-symbol)
1a9461d0
CD
189 reftex-active-toc nil
190 master-dir-re
191 (concat "\\`" (regexp-quote
192 (file-name-directory (reftex-TeX-master-file))))))
193
194 (set (make-local-variable 'reftex-docstruct-symbol) docstruct-symbol)
195 (set (make-local-variable 'reftex-prefix)
3666daf6 196 (cdr (assoc labels reftex-typekey-to-prefix-alist)))
1a9461d0
CD
197 (if (equal reftex-prefix " ") (setq reftex-prefix nil))
198
199 ;; Walk the docstruct and insert the appropriate stuff
200 (while (setq cell (pop all))
201
202 (incf index)
203 (setq from (point))
204
1a9461d0
CD
205 (cond
206
207 ((memq (car cell) '(bib thebib label-numbers appendix
3666daf6 208 master-dir bibview-cache is-multi xr xr-doc)))
1a9461d0
CD
209 ;; These are currently ignored
210
211 ((memq (car cell) '(bof eof file-error))
212 ;; Beginning or end of a file
213 (when files
3666daf6
CD
214 (setq prev-inserted cell)
215; (if (eq offset 'attention) (setq offset cell))
1a9461d0
CD
216 (insert
217 " File " (if (string-match master-dir-re (nth 1 cell))
218 (substring (nth 1 cell) (match-end 0))
219 (nth 1 cell))
220 (cond ((eq (car cell) 'bof) " starts here\n")
221 ((eq (car cell) 'eof) " ends here\n")
222 ((eq (car cell) 'file-error) " was not found\n")))
3666daf6 223 (setq to (point))
1a9461d0
CD
224 (when font
225 (put-text-property from to
226 'face reftex-file-boundary-face))
3666daf6
CD
227 (when toc-buffer
228 (if mouse-face
229 (put-text-property from (1- to)
230 'mouse-face mouse-face))
231 (put-text-property from to :data cell))))
1a9461d0
CD
232
233 ((eq (car cell) 'toc)
234 ;; a table of contents entry
7c4d13cc 235 (when (and toc
3666daf6
CD
236 (<= (nth 5 cell) reftex-toc-max-level))
237 (setq prev-inserted cell)
238; (if (eq offset 'attention) (setq offset cell))
1a9461d0
CD
239 (setq reftex-active-toc cell)
240 (insert (concat toc-indent (nth 2 cell) "\n"))
3666daf6
CD
241 (setq to (point))
242 (when font
243 (put-text-property from to
244 'face reftex-section-heading-face))
245 (when toc-buffer
246 (if mouse-face
247 (put-text-property from (1- to)
248 'mouse-face mouse-face))
249 (put-text-property from to :data cell))
250 (goto-char to)))
1a9461d0
CD
251
252 ((stringp (car cell))
253 ;; a label
254 (when (null (nth 2 cell))
255 ;; No context yet. Quick update.
3666daf6
CD
256 (setcdr cell (cdr (reftex-label-info-update cell)))
257 (put docstruct-symbol 'modified t))
1a9461d0
CD
258
259 (setq label (car cell)
260 typekey (nth 1 cell)
261 text (nth 2 cell)
262 comment (nth 4 cell)
263 note (nth 5 cell))
264
265 (when (and labels
e6ce8c42 266 (or (eq labels t)
3666daf6
CD
267 (string= typekey labels)
268 (string= labels " "))
1a9461d0
CD
269 (or show-commented (null comment)))
270
271 ;; Yes we want this one
272 (incf cnt)
3666daf6
CD
273 (setq prev-inserted cell)
274; (if (eq offset 'attention) (setq offset cell))
1a9461d0 275
3666daf6 276 (setq label (concat xr-prefix label))
1a9461d0
CD
277 (when comment (setq label (concat "% " label)))
278 (insert label-indent label)
279 (when font
3666daf6 280 (setq to (point))
1a9461d0
CD
281 (put-text-property
282 (- (point) (length label)) to
283 'face (if comment
284 'font-lock-comment-face
285 label-face))
3666daf6 286 (goto-char to))
1a9461d0
CD
287
288 (insert (if counter (format " (%d) " cnt) "")
289 (if comment " LABEL IS COMMENTED OUT " "")
290 (if (stringp note) (concat " " note) "")
291 "\n")
292 (setq to (point))
293
294 (when context
295 (insert context-indent text "\n")
296 (setq to (point)))
297 (put-text-property from to :data cell)
3666daf6
CD
298 (when mouse-face
299 (put-text-property from (1- to)
e6ce8c42 300 'mouse-face mouse-face))
1a9461d0
CD
301 (goto-char to)))
302
303 ((eq (car cell) 'index)
3666daf6
CD
304 ;; index entry
305 (when (and index-entries
306 (or (eq t index-entries)
307 (string= index-entries (nth 1 cell))))
308 (setq prev-inserted cell)
309; (if (eq offset 'attention) (setq offset cell))
310 (setq index-tag (format "<%s>" (nth 1 cell)))
311 (and font
312 (put-text-property 0 (length index-tag)
313 'face reftex-index-tag-face index-tag))
314 (insert label-indent index-tag " " (nth 7 cell))
315
316 (when font
317 (setq to (point))
e6ce8c42 318 (put-text-property
3666daf6
CD
319 (- (point) (length (nth 7 cell))) to
320 'face index-face)
321 (goto-char to))
322 (insert "\n")
323 (setq to (point))
324
325 (when context
326 (insert context-indent (nth 2 cell) "\n")
327 (setq to (point)))
328 (put-text-property from to :data cell)
329 (when mouse-face
330 (put-text-property from (1- to)
e6ce8c42 331 'mouse-face mouse-face))
3b919c9f
CD
332 (goto-char to))))
333
e6ce8c42 334 (if (eq cell here-I-am)
3666daf6 335 (setq offset 'attention))
3b919c9f 336 (if (and prev-inserted (eq offset 'attention))
3666daf6 337 (setq offset prev-inserted))
3b919c9f 338 )
1a9461d0
CD
339
340 (when (reftex-refontify)
341 ;; we need to fontify the buffer
342 (reftex-fontify-select-label-buffer buf))
343 (run-hooks 'reftex-display-copied-context-hook)
344 offset))
345
346(defun reftex-find-start-point (fallback &rest locations)
347 ;; Set point to the first available LOCATION. When a LOCATION is a list,
348 ;; search for such a :data text property. When it is an integer,
349 ;; use is as line number. FALLBACK is a buffer position used if everything
350 ;; else fails.
351 (catch 'exit
352 (goto-char (point-min))
353 (let (loc pos)
354 (while locations
3666daf6
CD
355 (setq loc (pop locations))
356 (cond
357 ((null loc))
358 ((listp loc)
359 (setq pos (text-property-any (point-min) (point-max) :data loc))
360 (when pos
e6ce8c42 361 (goto-char pos)
3666daf6
CD
362 (throw 'exit t)))
363 ((integerp loc)
364 (when (<= loc (count-lines (point-min) (point-max)))
e6ce8c42
GM
365 (goto-char (point-min))
366 (forward-line (1- loc))
3666daf6 367 (throw 'exit t)))))
1a9461d0
CD
368 (goto-char fallback))))
369
370(defvar reftex-last-data nil)
371(defvar reftex-last-line nil)
372(defvar reftex-select-marked nil)
373
374(defun reftex-select-item (prompt help-string keymap
3666daf6
CD
375 &optional offset
376 call-back cb-flag)
1a9461d0
CD
377;; Select an item, using PROMPT. The function returns a key indicating
378;; an exit status, along with a data structure indicating which item was
379;; selected.
380;; HELP-STRING contains help. KEYMAP is a keymap with the available
381;; selection commands.
382;; OFFSET can be a label list item which will be selected at start.
383;; When it is t, point will start out at the beginning of the buffer.
384;; Any other value will cause restart where last selection left off.
385;; When CALL-BACK is given, it is a function which is called with the index
386;; of the element.
387;; CB-FLAG is the initial value of that flag.
388
389 (let* (ev data last-data (selection-buffer (current-buffer)))
390
391 (setq reftex-select-marked nil)
392
393 (setq ev
394 (catch 'myexit
395 (save-window-excursion
396 (setq truncate-lines t)
397
3666daf6 398 ;; Find a good starting point
e6ce8c42 399 (reftex-find-start-point
3666daf6 400 (point-min) offset reftex-last-data reftex-last-line)
1a9461d0 401 (beginning-of-line 1)
3666daf6 402 (set (make-local-variable 'reftex-last-follow-point) (point))
1a9461d0
CD
403
404 (unwind-protect
3666daf6
CD
405 (progn
406 (use-local-map keymap)
407 (add-hook 'pre-command-hook 'reftex-select-pre-command-hook nil t)
408 (add-hook 'post-command-hook 'reftex-select-post-command-hook nil t)
409 (princ prompt)
410 (set-marker reftex-recursive-edit-marker (point))
411 ;; XEmacs does not run post-command-hook here
412 (and (featurep 'xemacs) (run-hooks 'post-command-hook))
413 (recursive-edit))
414
415 (set-marker reftex-recursive-edit-marker nil)
416 (save-excursion
417 (set-buffer selection-buffer)
418 (use-local-map nil)
419 (remove-hook 'pre-command-hook 'reftex-select-pre-command-hook t)
47aad890 420 (remove-hook 'post-command-hook
3666daf6
CD
421 'reftex-select-post-command-hook t))
422 ;; Kill the mark overlays
47aad890
GM
423 (mapc (lambda (c) (reftex-delete-overlay (nth 1 c)))
424 reftex-select-marked)))))
1a9461d0
CD
425
426 (set (make-local-variable 'reftex-last-line)
3666daf6 427 (+ (count-lines (point-min) (point)) (if (bolp) 1 0)))
1a9461d0
CD
428 (set (make-local-variable 'reftex-last-data) last-data)
429 (reftex-kill-buffer "*RefTeX Help*")
430 (setq reftex-callback-fwd (not reftex-callback-fwd)) ;; ;-)))
431 (message "")
432 (list ev data last-data)))
433
434;; The following variables are all bound dynamically in `reftex-select-item'.
435;; The defvars are here only to silence the byte compiler.
436
437(defvar found-list)
438(defvar cb-flag)
439(defvar data)
440(defvar prompt)
441(defvar last-data)
442(defvar call-back)
443(defvar help-string)
444(defvar refstyle)
445
446;; The selection commands
447
448(defun reftex-select-pre-command-hook ()
449 (reftex-unhighlight 1)
450 (reftex-unhighlight 0))
451
452(defun reftex-select-post-command-hook ()
453 (let (b e)
454 (setq data (get-text-property (point) :data))
455 (setq last-data (or data last-data))
e6ce8c42 456
1a9461d0 457 (when (and data cb-flag
3666daf6 458 (not (equal reftex-last-follow-point (point))))
1a9461d0 459 (setq reftex-last-follow-point (point))
e6ce8c42 460 (funcall call-back data reftex-callback-fwd
3666daf6 461 (not reftex-revisit-to-follow)))
1a9461d0 462 (if data
3666daf6
CD
463 (setq b (or (previous-single-property-change
464 (1+ (point)) :data)
465 (point-min))
466 e (or (next-single-property-change
467 (point) :data)
468 (point-max)))
1a9461d0
CD
469 (setq b (point) e (point)))
470 (and (memq reftex-highlight-selection '(cursor both))
3666daf6 471 (reftex-highlight 1 b e))
1a9461d0 472 (if (or (not (pos-visible-in-window-p b))
3666daf6
CD
473 (not (pos-visible-in-window-p e)))
474 (recenter '(4)))
1a9461d0
CD
475 (unless (current-message)
476 (princ prompt))))
477
478(defun reftex-select-next (&optional arg)
479 "Move to next selectable item."
480 (interactive "p")
481 (setq reftex-callback-fwd t)
482 (or (eobp) (forward-char 1))
483 (re-search-forward "^[^. \t\n\r]" nil t arg)
484 (beginning-of-line 1))
485(defun reftex-select-previous (&optional arg)
486 "Move to previous selectable item."
487 (interactive "p")
488 (setq reftex-callback-fwd nil)
489 (re-search-backward "^[^. \t\n\r]" nil t arg))
70d797cd
CD
490(defun reftex-select-jump (arg)
491 "Jump to a specific section. E.g. '3 z' jumps to section 3.
492Useful for large TOC's."
493 (interactive "P")
494 (goto-char (point-min))
495 (re-search-forward
496 (concat "^ *" (number-to-string (if (numberp arg) arg 1)) " ")
497 nil t)
498 (beginning-of-line))
1a9461d0
CD
499(defun reftex-select-next-heading (&optional arg)
500 "Move to next table of contentes line."
501 (interactive "p")
502 (end-of-line)
503 (re-search-forward "^ " nil t arg)
504 (beginning-of-line))
505(defun reftex-select-previous-heading (&optional arg)
506 "Move to previous table of contentes line."
507 (interactive "p")
508 (re-search-backward "^ " nil t arg))
509(defun reftex-select-quit ()
510 "Abort selection process."
511 (interactive)
512 (throw 'myexit nil))
513(defun reftex-select-keyboard-quit ()
514 "Abort selection process."
515 (interactive)
516 (throw 'exit t))
517(defun reftex-select-jump-to-previous ()
518 "Jump back to where previous selection process left off."
519 (interactive)
520 (let (pos)
521 (cond
522 ((and (local-variable-p 'reftex-last-data (current-buffer))
3666daf6
CD
523 reftex-last-data
524 (setq pos (text-property-any (point-min) (point-max)
525 :data reftex-last-data)))
1a9461d0
CD
526 (goto-char pos))
527 ((and (local-variable-p 'reftex-last-line (current-buffer))
3666daf6 528 (integerp reftex-last-line))
e6ce8c42
GM
529 (goto-char (point-min))
530 (forward-line (1- reftex-last-line)))
1a9461d0
CD
531 (t (ding)))))
532(defun reftex-select-toggle-follow ()
533 "Toggle follow mode: Other window follows with full context."
534 (interactive)
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."
539 (interactive)
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."
546 (interactive)
547 (setq refstyle
3666daf6
CD
548 (cond ((string= refstyle "\\ref") "\\fref")
549 ((string= refstyle "\\fref") "\\Fref")
550 (t "\\ref")))
1a9461d0
CD
551 (force-mode-line-update))
552(defun reftex-select-show-insertion-point ()
553 "Show the point from where selection was started in another window."
554 (interactive)
555 (let ((this-window (selected-window)))
556 (unwind-protect
3666daf6
CD
557 (progn
558 (switch-to-buffer-other-window
559 (marker-buffer reftex-select-return-marker))
560 (goto-char (marker-position reftex-select-return-marker))
561 (recenter '(4)))
1a9461d0
CD
562 (select-window this-window))))
563(defun reftex-select-callback ()
564 "Show full context in another window."
565 (interactive)
566 (if data (funcall call-back data reftex-callback-fwd nil) (ding)))
567(defun reftex-select-accept ()
568 "Accept the currently selected item."
569 (interactive)
570 (throw 'myexit 'return))
571(defun reftex-select-mouse-accept (ev)
572 "Accept the item at the mouse click."
573 (interactive "e")
574 (mouse-set-point ev)
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."
580 (interactive)
e6ce8c42 581 (let ((label (completing-read
3666daf6
CD
582 "Label: " (symbol-value reftex-docstruct-symbol)
583 nil nil reftex-prefix)))
1a9461d0
CD
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."
588 (interactive)
589 (let* ((key (completing-read "Citation key: " found-list))
3666daf6 590 (entry (assoc key found-list)))
1a9461d0
CD
591 (cond
592 ((or (null key) (equal key "")))
593 (entry
594 (setq data entry)
595 (setq last-data data)
596 (throw 'myexit 'return))
597 (t (throw 'myexit key)))))
598
599(defun reftex-select-mark (&optional separator)
600 "Mark the entry."
601 (interactive)
602 (let* ((data (get-text-property (point) :data))
3666daf6 603 boe eoe ovl)
1a9461d0
CD
604 (or data (error "No entry to mark at point"))
605 (if (assq data reftex-select-marked)
3666daf6 606 (error "Entry is already marked"))
1a9461d0 607 (setq boe (or (previous-single-property-change (1+ (point)) :data)
3666daf6
CD
608 (point-min))
609 eoe (or (next-single-property-change (point) :data) (point-max)))
3a1e8128 610 (setq ovl (reftex-make-overlay boe eoe))
1a9461d0 611 (push (list data ovl separator) reftex-select-marked)
3a1e8128
CD
612 (reftex-overlay-put ovl 'face reftex-select-mark-face)
613 (reftex-overlay-put ovl 'before-string
614 (if separator
615 (format "*%c%d* " separator
616 (length reftex-select-marked))
617 (format "*%d* " (length reftex-select-marked))))
1a9461d0
CD
618 (message "Entry has mark no. %d" (length reftex-select-marked))))
619
620(defun reftex-select-mark-comma ()
621 "Mark the entry and store the `comma' separator."
622 (interactive)
623 (reftex-select-mark ?,))
624(defun reftex-select-mark-to ()
625 "Mark the entry and store the `to' separator."
626 (interactive)
627 (reftex-select-mark ?-))
628(defun reftex-select-mark-and ()
629 "Mark the entry and store `and' to separator."
630 (interactive)
631 (reftex-select-mark ?+))
632
633(defun reftex-select-unmark ()
634 "Unmark the entry."
635 (interactive)
636 (let* ((data (get-text-property (point) :data))
3666daf6
CD
637 (cell (assq data reftex-select-marked))
638 (ovl (nth 1 cell))
639 (cnt 0)
640 sep)
1a9461d0
CD
641 (unless cell
642 (error "No marked entry at point"))
3a1e8128 643 (and ovl (reftex-delete-overlay ovl))
1a9461d0 644 (setq reftex-select-marked (delq cell reftex-select-marked))
3a1e8128 645 (setq cnt (1+ (length reftex-select-marked)))
91529f3d
GM
646 (mapc (lambda (c)
647 (setq sep (nth 2 c))
648 (reftex-overlay-put (nth 1 c) 'before-string
649 (if sep
650 (format "*%c%d* " sep (decf cnt))
651 (format "*%d* " (decf cnt)))))
3a1e8128 652 reftex-select-marked)
1a9461d0
CD
653 (message "Entry no longer marked")))
654
655(defun reftex-select-help ()
656 "Display a summary of the special key bindings."
657 (interactive)
658 (with-output-to-temp-buffer "*RefTeX Help*"
659 (princ help-string))
660 (reftex-enlarge-to-fit "*RefTeX Help*" t))
661
662;; Common bindings in reftex-select-label-map and reftex-select-bib-map
663(let ((map (make-sparse-keymap)))
664 (substitute-key-definition
3666daf6 665 'next-line 'reftex-select-next map global-map)
1a9461d0 666 (substitute-key-definition
3666daf6 667 'previous-line 'reftex-select-previous map global-map)
1a9461d0
CD
668 (substitute-key-definition
669 'keyboard-quit 'reftex-select-keyboard-quit map global-map)
670 (substitute-key-definition
3666daf6 671 'newline 'reftex-select-accept map global-map)
1a9461d0
CD
672
673 (loop for x in
3666daf6
CD
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)
e6ce8c42 681 ([(return)] . reftex-select-accept)
3666daf6
CD
682 ("q" . reftex-select-quit)
683 ("." . reftex-select-show-insertion-point)
684 ("?" . reftex-select-help))
685 do (define-key map (car x) (cdr x)))
1a9461d0
CD
686
687 ;; The mouse-2 binding
688 (if (featurep 'xemacs)
689 (define-key map [(button2)] 'reftex-select-mouse-accept)
1607e1bb
CD
690 (define-key map [(mouse-2)] 'reftex-select-mouse-accept)
691 (define-key map [follow-link] 'mouse-face))
e6ce8c42 692
1a9461d0
CD
693
694 ;; Digit arguments
695 (loop for key across "0123456789" do
3666daf6 696 (define-key map (vector (list key)) 'digit-argument))
1a9461d0
CD
697 (define-key map "-" 'negative-argument)
698
699 ;; Make two maps
700 (setq reftex-select-label-map map)
701 (setq reftex-select-bib-map (copy-keymap map)))
702
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))
e6ce8c42 706 (list 'lambda '()
3666daf6
CD
707 "Press `?' during selection to find out about this key."
708 '(interactive) (list 'throw '(quote myexit) key))))
1a9461d0
CD
709
710(loop for x in
711 '(("b" . reftex-select-jump-to-previous)
3666daf6
CD
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))
1a9461d0
CD
724 do
725 (define-key reftex-select-label-map (car x) (cdr x)))
726
727;; Specific bindings in reftex-select-bib-map
f3c18bd0 728(loop for key across "grRaAeE" do
1a9461d0 729 (define-key reftex-select-bib-map (vector (list key))
e6ce8c42 730 (list 'lambda '()
3666daf6
CD
731 "Press `?' during selection to find out about this key."
732 '(interactive) (list 'throw '(quote myexit) key))))
1a9461d0
CD
733
734(loop for x in
735 '(("\C-i" . reftex-select-read-cite)
3666daf6
CD
736 ([(tab)] . reftex-select-read-cite)
737 ("m" . reftex-select-mark)
738 ("u" . reftex-select-unmark))
1a9461d0 739 do (define-key reftex-select-bib-map (car x) (cdr x)))
e6ce8c42 740
1a9461d0 741
cbee283d 742;; arch-tag: 842078ff-0586-4e0b-957e-536e08218464
1a9461d0 743;;; reftex-sel.el ends here