* textmodes/refbib.el, textmodes/refer.el, textmodes/reftex-cite.el,
[bpt/emacs.git] / lisp / textmodes / reftex-ref.el
CommitLineData
3afbc435 1;;; reftex-ref.el --- code to create labels and references with RefTeX
5181ff9f
WL
2;; Copyright (c) 1997, 1998, 1999, 2000, 2003, 2004, 2005
3;; Free Software Foundation, Inc.
3ba2590f 4
6fbeb429 5;; Author: Carsten Dominik <dominik@science.uva.nl>
f3c18bd0 6;; Version: 4.26
3ba2590f
RS
7
8;; This file is part of GNU Emacs.
9
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)
13;; any later version.
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
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.
1a9461d0 24
3afbc435
PJ
25;;; Commentary:
26
27;;; Code:
28
7c4d13cc 29(eval-when-compile (require 'cl))
1a9461d0
CD
30(provide 'reftex-ref)
31(require 'reftex)
32;;;
33
34(defun reftex-label-location (&optional bound)
35 "Return the environment or macro which determines the label type at point.
36If optional BOUND is an integer, limit backward searches to that point."
37
38 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound))
39 (loc2 (reftex-what-environment reftex-label-env-list bound))
3666daf6 40 (loc3 (reftex-what-special-env 1 bound))
1a9461d0
CD
41 (p1 (or (cdr loc1) 0))
42 (p2 (or (cdr loc2) 0))
3666daf6
CD
43 (p3 (or (cdr loc3) 0))
44 (pmax (max p1 p2 p3)))
1a9461d0
CD
45
46 (setq reftex-location-start pmax)
47 (cond
48 ((= p1 pmax)
49 ;; A macro. Default context after macro name.
50 (setq reftex-default-context-position (+ p1 (length (car loc1))))
51 (or (car loc1) "section"))
52 ((= p2 pmax)
53 ;; An environment. Default context after \begin{name}.
54 (setq reftex-default-context-position (+ p2 8 (length (car loc2))))
55 (or (car loc2) "section"))
56 ((= p3 pmax)
57 ;; A special. Default context right there.
58 (setq reftex-default-context-position p3)
59 (setq loc3 (car loc3))
60 (cond ((null loc3) "section")
3666daf6
CD
61 ((symbolp loc3) (symbol-name loc3))
62 ((stringp loc3) loc3)
63 (t "section")))
1a9461d0
CD
64 (t ;; This should not happen, I think?
65 "section"))))
66
67(defun reftex-label-info-update (cell)
68 ;; Update information about just one label in a different file.
69 ;; CELL contains the old info list
70 (let* ((label (nth 0 cell))
71 (typekey (nth 1 cell))
72 ;; (text (nth 2 cell))
73 (file (nth 3 cell))
74 (comment (nth 4 cell))
75 (note (nth 5 cell))
76 (buf (reftex-get-file-buffer-force
77 file (not (eq t reftex-keep-temporary-buffers)))))
78 (if (not buf)
79 (list label typekey "" file comment "LOST LABEL. RESCAN TO FIX.")
80 (save-excursion
81 (set-buffer buf)
82 (save-restriction
83 (widen)
84 (goto-char 1)
85
86 (if (or (re-search-forward
3666daf6
CD
87 (format reftex-find-label-regexp-format
88 (regexp-quote label)) nil t)
89 (re-search-forward
90 (format reftex-find-label-regexp-format2
91 (regexp-quote label)) nil t))
1a9461d0
CD
92
93 (progn
94 (backward-char 1)
95 (append (reftex-label-info label file) (list note)))
96 (list label typekey "" file "LOST LABEL. RESCAN TO FIX.")))))))
97
98(defun reftex-label-info (label &optional file bound derive env-or-mac)
99 ;; Return info list on LABEL at point.
f3c18bd0
CD
100 (let* ((prefix (if (string-match "^[a-zA-Z0-9]+:" label)
101 (match-string 0 label)))
102 (typekey (cdr (assoc prefix reftex-prefix-to-typekey-alist)))
1a9461d0 103 (file (or file (buffer-file-name)))
f3c18bd0 104 (trust reftex-trust-label-prefix)
3666daf6 105 (in-comment (reftex-in-comment)))
f3c18bd0
CD
106 (if (and typekey
107 (cond ((eq trust t) t)
108 ((null trust) nil)
109 ((stringp trust) (string-match trust typekey))
110 ((listp trust) (member typekey trust))
111 (t nil)))
112 (list label typekey
113 (reftex-nicify-text (reftex-context-substring))
114 file in-comment)
115 (let* ((env-or-mac (or env-or-mac (reftex-label-location bound)))
116 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist)))
117 (parse (nth 2 (assoc env-or-mac reftex-env-or-mac-alist)))
118 (text (reftex-short-context env-or-mac parse reftex-location-start
119 derive)))
120 (list label typekey text file in-comment)))))
1a9461d0
CD
121
122;;; Creating labels ---------------------------------------------------------
123
124(defun reftex-label (&optional environment no-insert)
125 "Insert a unique label. Return the label.
126If ENVIRONMENT is given, don't bother to find out yourself.
127If NO-INSERT is non-nil, do not insert label into buffer.
128With prefix arg, force to rescan document first.
129When you are prompted to enter or confirm a label, and you reply with
130just the prefix or an empty string, no label at all will be inserted.
131A new label is also recorded into the label list.
132This function is controlled by the settings of reftex-insert-label-flags."
133
134 (interactive)
135
136 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4).
137 (reftex-access-scan-info current-prefix-arg)
138
139 ;; Find out what kind of environment this is and abort if necessary.
140 (if (or (not environment)
141 (not (assoc environment reftex-env-or-mac-alist)))
142 (setq environment (reftex-label-location)))
143 (unless environment
144 (error "Can't figure out what kind of label should be inserted"))
145
146 ;; Ok, go ahead.
147 (catch 'exit
148 (let* ((entry (assoc environment reftex-env-or-mac-alist))
3666daf6
CD
149 (typekey (nth 1 entry))
150 (format (nth 3 entry))
151 (macro-cell (reftex-what-macro 1))
152 (entry1 (assoc (car macro-cell) reftex-env-or-mac-alist))
153 label naked prefix valid default force-prompt rescan-is-useful)
1a9461d0 154 (when (and (or (nth 5 entry) (nth 5 entry1))
3666daf6
CD
155 (memq (preceding-char) '(?\[ ?\{)))
156 ;; This is an argument of a label macro. Insert naked label.
157 (setq naked t format "%s"))
1a9461d0
CD
158
159 (setq prefix (or (cdr (assoc typekey reftex-typekey-to-prefix-alist))
3666daf6 160 (concat typekey "-")))
1a9461d0
CD
161 ;; Replace any escapes in the prefix
162 (setq prefix (reftex-replace-prefix-escapes prefix))
163
164 ;; Make a default label.
165 (cond
166
167 ((reftex-typekey-check typekey (nth 0 reftex-insert-label-flags))
3666daf6
CD
168 ;; Derive a label from context.
169 (setq reftex-active-toc (reftex-last-assoc-before-elt
170 'toc (car (reftex-where-am-I))
171 (symbol-value reftex-docstruct-symbol)))
172 (setq default (reftex-no-props
173 (nth 2 (reftex-label-info " " nil nil t))))
174 ;; Catch the cases where the is actually no context available.
175 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default)
5181ff9f 176 (string-match "INVALID VALUE OF PARSE" default)
3666daf6
CD
177 (string-match "SECTION HEADING NOT FOUND" default)
178 (string-match "HOOK ERROR" default)
179 (string-match "^[ \t]*$" default))
180 (setq default prefix
181 force-prompt t) ; need to prompt
182 (setq default
183 (concat prefix
184 (funcall reftex-string-to-label-function default)))
185
186 ;; Make it unique.
187 (setq default (reftex-uniquify-label default nil "-"))))
1a9461d0
CD
188
189 ((reftex-typekey-check typekey (nth 1 reftex-insert-label-flags))
3666daf6
CD
190 ;; Minimal default: the user will be prompted.
191 (setq default prefix))
1a9461d0
CD
192
193 (t
3666daf6
CD
194 ;; Make an automatic label.
195 (setq default (reftex-uniquify-label prefix t))))
1a9461d0
CD
196
197 ;; Should we ask the user?
198 (if (or (reftex-typekey-check typekey
3666daf6
CD
199 (nth 1 reftex-insert-label-flags)) ; prompt
200 force-prompt)
201
202 (while (not valid)
203 ;; iterate until we get a legal label
204
205 (setq label (read-string
206 (if naked "Naked Label: " "Label: ")
207 default))
208
209 ;; Lets make sure that this is a legal label
210 (cond
211
212 ((string-match (concat "\\`\\(" (regexp-quote prefix)
213 "\\)?[ \t]*\\'")
214 label)
215 ;; No label at all, please
216 (message "No label inserted.")
217 (throw 'exit nil))
218
219 ;; Test if label contains strange characters
220 ((string-match reftex-label-illegal-re label)
5181ff9f 221 (message "Label \"%s\" contains invalid characters" label)
3666daf6
CD
222 (ding)
223 (sit-for 2))
224
225 ;; Look it up in the label list
226 ((setq entry (assoc label
227 (symbol-value reftex-docstruct-symbol)))
228 (ding)
229 (if (y-or-n-p
230 (format "Label '%s' exists. Use anyway? " label))
231 (setq valid t)))
232
233 ;; Label is ok
234 (t
235 (setq valid t))))
236 (setq label default))
1a9461d0
CD
237
238 ;; Insert the label into the label list
3666daf6
CD
239 (let* ((here-I-am-info
240 (save-excursion
241 (if (and (or naked no-insert)
242 (integerp (cdr macro-cell)))
243 (goto-char (cdr macro-cell)))
244 (reftex-where-am-I)))
245 (here-I-am (car here-I-am-info))
246 (note (if (cdr here-I-am-info)
247 ""
248 "POSITION UNCERTAIN. RESCAN TO FIX."))
249 (file (buffer-file-name))
250 (text nil)
251 (tail (memq here-I-am (symbol-value reftex-docstruct-symbol))))
252
253 (or (cdr here-I-am-info) (setq rescan-is-useful t))
254
255 (when tail
256 (push (list label typekey text file nil note) (cdr tail))
257 (put reftex-docstruct-symbol 'modified t)))
1a9461d0
CD
258
259 ;; Insert the label into the buffer
260 (unless no-insert
3666daf6
CD
261 (insert
262 (if reftex-format-label-function
263 (funcall reftex-format-label-function label format)
264 (format format label)))
265 (if (and reftex-plug-into-AUCTeX
266 (fboundp 'LaTeX-add-labels))
267 ;; Tell AUCTeX about this
268 (LaTeX-add-labels label)))
1a9461d0
CD
269
270 ;; Delete the corresponding selection buffers to force update on next use.
271 (when reftex-auto-update-selection-buffers
3666daf6
CD
272 (reftex-erase-buffer (reftex-make-selection-buffer-name typekey))
273 (reftex-erase-buffer (reftex-make-selection-buffer-name " ")))
1a9461d0
CD
274
275 (when (and rescan-is-useful reftex-allow-automatic-rescan)
3666daf6 276 (reftex-parse-one))
1a9461d0
CD
277
278 ;; return value of the function is the label
279 label)))
280
281(defun reftex-string-to-label (string)
282 "Convert a string (a sentence) to a label.
283Uses `reftex-derive-label-parameters' and `reftex-label-illegal-re'. It
284also applies `reftex-translate-to-ascii-function' to the string."
285 (when (and reftex-translate-to-ascii-function
3666daf6 286 (fboundp reftex-translate-to-ascii-function))
1a9461d0
CD
287 (setq string (funcall reftex-translate-to-ascii-function string)))
288 (apply 'reftex-convert-string string
3666daf6
CD
289 "[-~ \t\n\r,;]+" reftex-label-illegal-re nil nil
290 reftex-derive-label-parameters))
1a9461d0
CD
291
292(defun reftex-latin1-to-ascii (string)
293 ;; Translate the upper 128 chars in the Latin-1 charset to ASCII equivalents
294 (let ((tab "@@@@@@@@@@@@@@@@@@'@@@@@@@@@@@@@ icLxY|S\"ca<--R-o|23'uq..1o>423?AAAAAAACEEEEIIIIDNOOOOOXOUUUUYP3aaaaaaaceeeeiiiidnooooo:ouuuuypy")
3666daf6
CD
295 (emacsp (not (featurep 'xemacs))))
296 (mapconcat
1a9461d0
CD
297 (lambda (c)
298 (cond ((and (> c 127) (< c 256)) ; 8 bit Latin-1
3666daf6
CD
299 (char-to-string (aref tab (- c 128))))
300 ((and emacsp ; Not for XEmacs
301 (> c 2175) (< c 2304)) ; Mule Latin-1
302 (char-to-string (aref tab (- c 2176))))
303 (t (char-to-string c))))
1a9461d0
CD
304 string "")))
305
306(defun reftex-replace-prefix-escapes (prefix)
307 ;; Replace %escapes in a label prefix
308 (save-match-data
309 (let (letter (num 0) replace)
310 (while (string-match "\\%\\([a-zA-Z]\\)" prefix num)
311 (setq letter (match-string 1 prefix))
312 (setq replace
f3c18bd0
CD
313 (save-match-data
314 (cond
315 ((equal letter "f")
316 (file-name-sans-extension
317 (file-name-nondirectory (buffer-file-name))))
318 ((equal letter "F")
319 (let ((masterdir (file-name-directory (reftex-TeX-master-file)))
320 (file (file-name-sans-extension (buffer-file-name))))
321 (if (string-match (concat "\\`" (regexp-quote masterdir))
322 file)
323 (substring file (length masterdir))
324 file)))
325 ((equal letter "m")
326 (file-name-sans-extension
327 (file-name-nondirectory (reftex-TeX-master-file))))
328 ((equal letter "M")
329 (file-name-nondirectory
330 (substring (file-name-directory (reftex-TeX-master-file))
331 0 -1)))
332 ((equal letter "u")
333 (or (user-login-name) ""))
334 ((equal letter "S")
335 (let* (macro level-exp level)
336 (save-excursion
337 (save-match-data
338 (when (re-search-backward reftex-section-regexp nil t)
339 (setq macro (reftex-match-string 2)
340 level-exp (cdr (assoc macro reftex-section-levels-all))
341 level (if (symbolp level-exp)
342 (abs (save-match-data
343 (funcall level-exp)))
344 (abs level-exp))))
345 (cdr (or (assoc macro reftex-section-prefixes)
346 (assoc level reftex-section-prefixes)
347 (assq t reftex-section-prefixes)
348 (list t "sec:")))))))
349 (t ""))))
1a9461d0
CD
350 (setq num (1- (+ (match-beginning 1) (length replace)))
351 prefix (replace-match replace nil nil prefix)))
352 prefix)))
353
354(defun reftex-uniquify-label (label &optional force separator)
355 ;; Make label unique by appending a number.
356 ;; Optional FORCE means, force appending a number, even if label is unique.
357 ;; Optional SEPARATOR is a string to stick between label and number.
358
359 ;; Ensure access to scanning info
360 (reftex-access-scan-info)
361
362 (cond
363 ((and (not force)
364 (not (assoc label (symbol-value reftex-docstruct-symbol))))
365 label)
366 (t
367 (let* ((label-numbers (assq 'label-numbers
368 (symbol-value reftex-docstruct-symbol)))
369 (label-numbers-alist (cdr label-numbers))
370 (cell (or (assoc label label-numbers-alist)
371 (car (setcdr label-numbers
372 (cons (cons label 0)
373 label-numbers-alist)))))
374 (num (1+ (cdr cell)))
375 (sep (or separator "")))
376 (while (assoc (concat label sep (int-to-string num))
377 (symbol-value reftex-docstruct-symbol))
378 (incf num))
379 (setcdr cell num)
380 (concat label sep (int-to-string num))))))
381
382;;; Referencing labels ------------------------------------------------------
383
384;; Help string for the reference label menu
385(defconst reftex-select-label-prompt
386 "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more")
387
388(defconst reftex-select-label-help
389 " n / p Go to next/previous label (Cursor motion works as well)
390 C-c C-n/p Go to next/previous section heading.
391 b / l Jump back to previous selection / Reuse last referenced label.
70d797cd 392 z Jump to a specific section, e.g. '3 z' jumps to section 3.
1a9461d0
CD
393 g / s Update menu / Switch label type.
394 r / C-u r Reparse document / Reparse entire document.
395 x Switch to label menu of external document (with LaTeX package `xr').
396 F t c Toggle: [F]ile borders, [t]able of contents, [c]ontext
397 # % Toggle: [#] label counters, [%] labels in comments
398 SPC / f Show full context in other window / Toggle follow mode.
399 . Show insertion point in other window.
400 v / V Toggle \\ref <-> \\vref / Rotate \\ref <=> \\fref <=> \\Fref
401 TAB Enter a label with completion.
402 m , - + Mark entry. `,-+' also assign a separator.
403 a / A Put all marked entries into one/many \\ref commands.
404 q / RET Quit without referencing / Accept current label (also on mouse-2).")
405
406(defun reftex-reference (&optional type no-insert cut)
407 "Make a LaTeX reference. Look only for labels of a certain TYPE.
408With prefix arg, force to rescan buffer for labels. This should only be
409necessary if you have recently entered labels yourself without using
410reftex-label. Rescanning of the buffer can also be requested from the
411label selection menu.
412The function returns the selected label or nil.
413If NO-INSERT is non-nil, do not insert \\ref command, just return label.
414When called with 2 C-u prefix args, disable magic word recognition."
415
416 (interactive)
417
418 ;; check for active recursive edits
419 (reftex-check-recursive-edit)
420
3666daf6 421 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
1a9461d0
CD
422 (reftex-access-scan-info current-prefix-arg)
423
424 (unless type
425 ;; guess type from context
426 (if (and reftex-guess-label-type
427 (setq type (reftex-guess-label-type)))
3666daf6
CD
428 (setq cut (cdr type)
429 type (car type))
1a9461d0
CD
430 (setq type (reftex-query-label-type))))
431
3666daf6
CD
432 (let* ((refstyle
433 (cond ((reftex-typekey-check type reftex-vref-is-default) "\\vref")
434 ((reftex-typekey-check type reftex-fref-is-default) "\\fref")
435 (t "\\ref")))
436 (reftex-format-ref-function reftex-format-ref-function)
437 (form "\\ref{%s}")
438 label labels sep sep1)
1a9461d0
CD
439
440 ;; Have the user select a label
441 (set-marker reftex-select-return-marker (point))
442 (setq labels (save-excursion
3666daf6 443 (reftex-offer-label-menu type)))
1a9461d0
CD
444 (reftex-ensure-compiled-variables)
445 (set-marker reftex-select-return-marker nil)
3b919c9f 446 ;; If the first entry is the symbol 'concat, concat all labels.
1a9461d0
CD
447 ;; We keep the cdr of the first label for typekey etc information.
448 (if (eq (car labels) 'concat)
3666daf6
CD
449 (setq labels (list (list (mapconcat 'car (cdr labels) ",")
450 (cdr (nth 1 labels))))))
1a9461d0 451 (setq type (nth 1 (car labels))
3666daf6
CD
452 form (or (cdr (assoc type reftex-typekey-to-format-alist))
453 form))
454
1a9461d0
CD
455 (cond
456 (no-insert
457 ;; Just return the first label
458 (car (car labels)))
459 ((null labels)
460 (message "Quit")
461 nil)
462 (t
463 (while labels
3666daf6
CD
464 (setq label (car (car labels))
465 sep (nth 2 (car labels))
466 sep1 (cdr (assoc sep reftex-multiref-punctuation))
467 labels (cdr labels))
468 (when cut
469 (backward-delete-char cut)
470 (setq cut nil))
471
472 ;; remove ~ if we do already have a space
473 (when (and (= ?~ (string-to-char form))
f3c18bd0 474 (member (preceding-char) '(?\ ?\t ?\n ?~)))
3666daf6
CD
475 (setq form (substring form 1)))
476 ;; do we have a special format?
477 (setq reftex-format-ref-function
478 (cond
479 ((string= refstyle "\\vref") 'reftex-format-vref)
480 ((string= refstyle "\\fref") 'reftex-format-fref)
481 ((string= refstyle "\\Fref") 'reftex-format-Fref)
482 (t reftex-format-ref-function)))
483 ;; ok, insert the reference
484 (if sep1 (insert sep1))
485 (insert
486 (if reftex-format-ref-function
487 (funcall reftex-format-ref-function label form)
488 (format form label label)))
489 ;; take out the initial ~ for good
490 (and (= ?~ (string-to-char form))
491 (setq form (substring form 1))))
1a9461d0
CD
492 (message "")
493 label))))
494
495(defun reftex-guess-label-type ()
496 ;; Examine context to guess what a \ref might want to reference.
497 (let ((words reftex-words-to-typekey-alist)
3666daf6
CD
498 (case-fold-search t)
499 (bound (max (point-min) (- (point) 35)))
500 matched cell)
1a9461d0
CD
501 (save-excursion
502 (while (and (setq cell (pop words))
3666daf6
CD
503 (not (setq matched
504 (re-search-backward (car cell) bound t))))))
1a9461d0 505 (if matched
3666daf6 506 (cons (cdr cell) (- (match-end 0) (match-end 1)))
1a9461d0
CD
507 nil)))
508
509(defvar reftex-select-label-map)
510(defun reftex-offer-label-menu (typekey)
511 ;; Offer a menu with the appropriate labels.
512 (let* ((buf (current-buffer))
3666daf6
CD
513 (xr-data (assq 'xr (symbol-value reftex-docstruct-symbol)))
514 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data)))
515 (xr-index 0)
1a9461d0 516 (here-I-am (car (reftex-where-am-I)))
3666daf6 517 (here-I-am1 here-I-am)
1a9461d0
CD
518 (toc (reftex-typekey-check typekey reftex-label-menu-flags 0))
519 (files (reftex-typekey-check typekey reftex-label-menu-flags 7))
520 (context (not (reftex-typekey-check
521 typekey reftex-label-menu-flags 3)))
522 (counter (reftex-typekey-check
523 typekey reftex-label-menu-flags 2))
524 (follow (reftex-typekey-check
525 typekey reftex-label-menu-flags 4))
526 (commented (nth 5 reftex-label-menu-flags))
3666daf6
CD
527 (prefix "")
528 selection-buffers
1a9461d0
CD
529 offset rtn key data last-data entries)
530
531 (unwind-protect
532 (catch 'exit
533 (while t
534 (save-window-excursion
3666daf6
CD
535 (delete-other-windows)
536 (setq reftex-call-back-to-this-buffer buf
537 reftex-latex-syntax-table (syntax-table))
538 (let ((default-major-mode 'reftex-select-label-mode))
539 (if reftex-use-multiple-selection-buffers
540 (switch-to-buffer-other-window
541 (save-excursion
542 (set-buffer buf)
543 (reftex-make-selection-buffer-name typekey)))
544 (switch-to-buffer-other-window "*RefTeX Select*")
545 (reftex-erase-buffer)))
546 (unless (eq major-mode 'reftex-select-label-mode)
547 (reftex-select-label-mode))
548 (add-to-list 'selection-buffers (current-buffer))
1a9461d0 549 (setq truncate-lines t)
3666daf6
CD
550 (setq mode-line-format
551 (list "---- " 'mode-line-buffer-identification
552 " " 'global-mode-string " (" mode-name ")"
553 " S<" 'refstyle ">"
554 " -%-"))
555 (cond
556 ((= 0 (buffer-size))
557 (let ((buffer-read-only nil))
558 (message "Creating Selection Buffer...")
559 (setq offset (reftex-insert-docstruct
560 buf
561 toc
562 typekey
563 nil ; index
564 files
565 context
566 counter
567 commented
568 (or here-I-am offset)
569 prefix
570 nil ; no a toc buffer
571 ))))
572 (here-I-am
573 (setq offset (reftex-get-offset buf here-I-am typekey)))
574 (t (setq offset t)))
575 (setq buffer-read-only t)
576 (setq offset (or offset t))
1a9461d0
CD
577
578 (setq here-I-am nil) ; turn off determination of offset
579 (setq rtn
580 (reftex-select-item
581 reftex-select-label-prompt
582 reftex-select-label-help
3666daf6 583 reftex-select-label-map
1a9461d0
CD
584 offset
585 'reftex-show-label-location follow))
586 (setq key (car rtn)
587 data (nth 1 rtn)
588 last-data (nth 2 rtn)
3666daf6 589 offset t)
1a9461d0
CD
590 (unless key (throw 'exit nil))
591 (cond
3666daf6
CD
592 ((eq key ?g)
593 ;; update buffer
594 (reftex-erase-buffer))
1a9461d0
CD
595 ((or (eq key ?r)
596 (eq key ?R))
597 ;; rescan buffer
3666daf6
CD
598 (and current-prefix-arg (setq key ?R))
599 (reftex-erase-buffer)
1a9461d0
CD
600 (reftex-reparse-document buf last-data key))
601 ((eq key ?c)
602 ;; toggle context mode
3666daf6 603 (reftex-erase-buffer)
1a9461d0
CD
604 (setq context (not context)))
605 ((eq key ?s)
606 ;; switch type
3666daf6 607 (setq here-I-am here-I-am1)
1a9461d0
CD
608 (setq typekey (reftex-query-label-type)))
609 ((eq key ?t)
7c4d13cc 610 ;; toggle table of contents display, or change depth
3666daf6
CD
611 (reftex-erase-buffer)
612 (if current-prefix-arg
613 (setq reftex-toc-max-level (prefix-numeric-value
614 current-prefix-arg))
615 (setq toc (not toc))))
1a9461d0
CD
616 ((eq key ?F)
617 ;; toggle display of included file borders
3666daf6 618 (reftex-erase-buffer)
1a9461d0
CD
619 (setq files (not files)))
620 ((eq key ?#)
621 ;; toggle counter display
3666daf6 622 (reftex-erase-buffer)
1a9461d0
CD
623 (setq counter (not counter)))
624 ((eq key ?%)
625 ;; toggle display of commented labels
3666daf6 626 (reftex-erase-buffer)
1a9461d0
CD
627 (setq commented (not commented)))
628 ((eq key ?l)
629 ;; reuse the last referenced label again
630 (setq entries reftex-last-used-reference)
631 (throw 'exit t))
3666daf6
CD
632 ((eq key ?x)
633 ;; select an external document
634 (setq xr-index (reftex-select-external-document
635 xr-alist xr-index))
636 (setq buf (or (reftex-get-file-buffer-force
637 (cdr (nth xr-index xr-alist)))
638 (error "Cannot switch document"))
639 prefix (or (car (nth xr-index xr-alist)) ""))
1a9461d0 640 (set-buffer buf)
3666daf6
CD
641 (reftex-access-scan-info))
642 ((stringp key)
643 (setq entries
644 (list
645 (list
646 (or (assoc key (symbol-value reftex-docstruct-symbol))
647 (list key typekey)))))
1a9461d0 648 (throw 'exit t))
3666daf6
CD
649 ((memq key '(?a ?A return))
650 (cond
651 (reftex-select-marked
652 (setq entries (nreverse reftex-select-marked)))
653 (data
654 (setq entries (list (list data))))
655 (t (setq entries nil)))
656 (when entries
657 (if (equal key ?a) (push 'concat entries))
658 (setq reftex-last-used-reference entries))
659 (set-buffer buf)
660 (throw 'exit t))
661 (t (error "This should not happen (reftex-offer-label-menu)"))))))
1a9461d0 662 (save-excursion
3666daf6
CD
663 (while reftex-buffers-with-changed-invisibility
664 (set-buffer (car (car reftex-buffers-with-changed-invisibility)))
665 (setq buffer-invisibility-spec
666 (cdr (pop reftex-buffers-with-changed-invisibility)))))
1a9461d0 667 (mapcar (lambda (buf) (and (buffer-live-p buf) (bury-buffer buf)))
3666daf6 668 selection-buffers)
1a9461d0
CD
669 (reftex-kill-temporary-buffers))
670 ;; Add the prefixes, put together the relevant information in the form
7ac7387b 671 ;; (LABEL TYPEKEY SEPARATOR) and return a list of those.
1a9461d0 672 (mapcar (lambda (x)
3666daf6
CD
673 (if (listp x)
674 (list (concat prefix (car (car x)))
675 (nth 1 (car x))
676 (nth 2 x))
677 x))
678 entries)))
1a9461d0
CD
679
680(defun reftex-reparse-document (&optional buffer data key)
681 ;; Rescan the document.
682 (save-window-excursion
683 (save-excursion
684 (if buffer
685 (if (not (bufferp buffer))
686 (error "No such buffer %s" (buffer-name buffer))
687 (set-buffer buffer)))
688 (let ((arg (if (eq key ?R) '(16) '(4)))
689 (file (nth 3 data)))
690 (reftex-access-scan-info arg file)))))
691
692(defun reftex-query-label-type ()
693 ;; Ask for label type
3666daf6
CD
694 (let ((key (reftex-select-with-char
695 reftex-type-query-prompt reftex-type-query-help 3)))
1a9461d0
CD
696 (unless (member (char-to-string key) reftex-typekey-list)
697 (error "No such label type: %s" (char-to-string key)))
698 (char-to-string key)))
699
3666daf6
CD
700(defun reftex-show-label-location (data forward no-revisit
701 &optional stay error)
1a9461d0
CD
702 ;; View the definition site of a label in another window.
703 ;; DATA is an entry from the docstruct list.
704 ;; FORWARD indicates if the label is likely forward from current point.
705 ;; NO-REVISIT means do not load a file to show this label.
706 ;; STAY means leave the new window selected.
707 ;; ERROR means throw an error exception when the label cannot be found.
708 ;; If ERROR is nil, the return value of this function indicates success.
709 (let* ((this-window (selected-window))
3666daf6 710 (errorf (if error 'error 'message))
1a9461d0
CD
711 label file buffer re found)
712
713 (catch 'exit
714 (setq label (nth 0 data)
3666daf6 715 file (nth 3 data))
1a9461d0
CD
716
717 (unless file
3666daf6
CD
718 (funcall errorf "Unknown label - reparse might help")
719 (throw 'exit nil))
1a9461d0
CD
720
721 ;; Goto the file in another window
3666daf6
CD
722 (setq buffer
723 (if no-revisit
724 (reftex-get-buffer-visiting file)
725 (reftex-get-file-buffer-force
726 file (not reftex-keep-temporary-buffers))))
1a9461d0
CD
727 (if buffer
728 ;; good - the file is available
729 (switch-to-buffer-other-window buffer)
730 ;; we have got a problem here. The file does not exist.
731 ;; Let' get out of here..
3666daf6
CD
732 (funcall errorf "Label %s not found" label)
733 (throw 'exit nil))
1a9461d0
CD
734
735 ;; search for that label
736 (setq re (format reftex-find-label-regexp-format (regexp-quote label)))
737 (setq found
3666daf6
CD
738 (if forward
739 (re-search-forward re nil t)
740 (re-search-backward re nil t)))
1a9461d0
CD
741 (unless found
742 (goto-char (point-min))
3666daf6
CD
743 (unless (setq found (re-search-forward re nil t))
744 ;; Ooops. Must be in a macro with distributed args.
745 (setq found
746 (re-search-forward
747 (format reftex-find-label-regexp-format2
748 (regexp-quote label)) nil t))))
1a9461d0 749 (if (match-end 3)
3666daf6
CD
750 (progn
751 (reftex-highlight 0 (match-beginning 3) (match-end 3))
752 (reftex-show-entry (match-beginning 3) (match-end 3))
753 (recenter '(4))
754 (unless stay (select-window this-window)))
755 (select-window this-window)
756 (funcall errorf "Label %s not found" label))
1a9461d0
CD
757 found)))
758
759(defvar font-lock-mode)
760(defun reftex-show-entry (beg-hlt end-hlt)
761 ;; Show entry if point is hidden
762 (let* ((n (/ (reftex-window-height) 2))
763 (beg (save-excursion
764 (re-search-backward "[\n\r]" nil 1 n) (point)))
765 (end (save-excursion
766 (re-search-forward "[\n\r]" nil 1 n) (point))))
767 (cond
768 ((and (boundp 'buffer-invisibility-spec) buffer-invisibility-spec
3666daf6 769 (get-char-property (1+ beg-hlt) 'invisible))
1a9461d0
CD
770 ;; Invisible with text properties. That is easy to change.
771 (push (cons (current-buffer) buffer-invisibility-spec)
3666daf6 772 reftex-buffers-with-changed-invisibility)
1a9461d0
CD
773 (setq buffer-invisibility-spec nil))
774 ((string-match "\r" (buffer-substring beg end))
775 ;; Invisible with selective display. We need to copy it.
776 (let ((string (buffer-substring-no-properties beg end)))
3666daf6
CD
777 (switch-to-buffer "*RefTeX Context Copy*")
778 (setq buffer-read-only nil)
779 (erase-buffer)
780 (insert string)
781 (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
782 (goto-char (- beg-hlt beg))
783 (reftex-highlight 0 (1+ (- beg-hlt beg)) (1+ (- end-hlt beg)))
784 (if (reftex-refontify)
785 (when (or (not (eq major-mode 'latex-mode))
786 (not font-lock-mode))
787 (latex-mode)
788 (run-hook-with-args
789 'reftex-pre-refontification-functions
790 reftex-call-back-to-this-buffer 'reftex-hidden)
791 (turn-on-font-lock))
792 (when (or (not (eq major-mode 'fundamental-mode))
793 font-lock-mode)
794 (fundamental-mode)))
795 (run-hooks 'reftex-display-copied-context-hook)
796 (setq buffer-read-only t))))))
1a9461d0
CD
797
798(defun reftex-varioref-vref ()
70d797cd 799 "Insert a reference using the `\\vref' macro from the varioref package."
1a9461d0
CD
800 (interactive)
801 (let ((reftex-format-ref-function 'reftex-format-vref))
802 (reftex-reference)))
803(defun reftex-fancyref-fref ()
70d797cd 804 "Insert a reference using the `\\fref' macro from the fancyref package."
1a9461d0
CD
805 (interactive)
806 (let ((reftex-format-ref-function 'reftex-format-fref)
3666daf6
CD
807 ;;(reftex-guess-label-type nil) ;FIXME do we want this????
808 )
1a9461d0
CD
809 (reftex-reference)))
810(defun reftex-fancyref-Fref ()
70d797cd 811 "Insert a reference using the `\\Fref' macro from the fancyref package."
1a9461d0
CD
812 (interactive)
813 (let ((reftex-format-ref-function 'reftex-format-Fref)
3666daf6
CD
814 ;;(reftex-guess-label-type nil) ;FIXME do we want this????
815 )
1a9461d0
CD
816 (reftex-reference)))
817
818(defun reftex-format-vref (label fmt)
819 (while (string-match "\\\\ref{" fmt)
820 (setq fmt (replace-match "\\vref{" t t fmt)))
821 (format fmt label label))
822(defun reftex-format-Fref (label def-fmt)
823 (format "\\Fref{%s}" label))
824(defun reftex-format-fref (label def-fmt)
825 (format "\\fref{%s}" label))
826
3b919c9f
CD
827(defun reftex-goto-label (&optional other-window)
828 "Prompt for a label (with completion) and jump to the location of this label.
829Optional prefix argument OTHER-WINDOW goes to the label in another window."
830 (interactive "P")
831 (reftex-access-scan-info)
832 (let* ((wcfg (current-window-configuration))
3666daf6
CD
833 (docstruct (symbol-value reftex-docstruct-symbol))
834 (label (completing-read "Label: " docstruct
835 (lambda (x) (stringp (car x))) t))
836 (selection (assoc label docstruct))
837 (where (progn
838 (reftex-show-label-location selection t nil 'stay)
839 (point-marker))))
3b919c9f
CD
840 (unless other-window
841 (set-window-configuration wcfg)
842 (switch-to-buffer (marker-buffer where))
3666daf6 843 (goto-char where))
3b919c9f
CD
844 (reftex-unhighlight 0)))
845
846
847
ab5796a9 848;;; arch-tag: 52f14032-fb76-4d31-954f-750c72415675
1a9461d0 849;;; reftex-ref.el ends here