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