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