1 ;;; reftex-ref.el --- code to create labels and references with RefTeX
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <dominik@science.uva.nl>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl
))
33 (require 'reftex-parse
)
36 (defun reftex-label-location (&optional bound
)
37 "Return the environment or macro which determines the label type at point.
38 If optional BOUND is an integer, limit backward searches to that point."
40 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound
))
41 (loc2 (reftex-what-environment reftex-label-env-list bound
))
42 (loc3 (reftex-what-special-env 1 bound
))
43 (p1 (or (cdr loc1
) 0))
44 (p2 (or (cdr loc2
) 0))
45 (p3 (or (cdr loc3
) 0))
46 (pmax (max p1 p2 p3
)))
48 (setq reftex-location-start pmax
)
51 ;; A macro. Default context after macro name.
52 (setq reftex-default-context-position
(+ p1
(length (car loc1
))))
53 (or (car loc1
) "section"))
55 ;; An environment. Default context after \begin{name}.
56 (setq reftex-default-context-position
(+ p2
8 (length (car loc2
))))
57 (or (car loc2
) "section"))
59 ;; A special. Default context right there.
60 (setq reftex-default-context-position p3
)
61 (setq loc3
(car loc3
))
62 (cond ((null loc3
) "section")
63 ((symbolp loc3
) (symbol-name loc3
))
66 (t ;; This should not happen, I think?
69 (defun reftex-label-info-update (cell)
70 ;; Update information about just one label in a different file.
71 ;; CELL contains the old info list
72 (let* ((label (nth 0 cell
))
73 (typekey (nth 1 cell
))
74 ;; (text (nth 2 cell))
76 (comment (nth 4 cell
))
78 (buf (reftex-get-file-buffer-force
79 file
(not (eq t reftex-keep-temporary-buffers
)))))
81 (list label typekey
"" file comment
"LOST LABEL. RESCAN TO FIX.")
88 (if (or (re-search-forward
89 (format reftex-find-label-regexp-format
90 (regexp-quote label
)) nil t
)
92 (format reftex-find-label-regexp-format2
93 (regexp-quote label
)) nil t
))
97 (append (reftex-label-info label file
) (list note
)))
98 (list label typekey
"" file
"LOST LABEL. RESCAN TO FIX.")))))))
100 (defun reftex-label-info (label &optional file bound derive env-or-mac
)
101 ;; Return info list on LABEL at point.
102 (let* ((prefix (if (string-match "^[a-zA-Z0-9]+:" label
)
103 (match-string 0 label
)))
104 (typekey (cdr (assoc prefix reftex-prefix-to-typekey-alist
)))
105 (file (or file
(buffer-file-name)))
106 (trust reftex-trust-label-prefix
)
107 (in-comment (reftex-in-comment)))
109 (cond ((eq trust t
) t
)
111 ((stringp trust
) (string-match trust typekey
))
112 ((listp trust
) (member typekey trust
))
115 (reftex-nicify-text (reftex-context-substring))
117 (let* ((env-or-mac (or env-or-mac
(reftex-label-location bound
)))
118 (typekey (nth 1 (assoc env-or-mac reftex-env-or-mac-alist
)))
119 (parse (nth 2 (assoc env-or-mac reftex-env-or-mac-alist
)))
120 (text (reftex-short-context env-or-mac parse reftex-location-start
122 (list label typekey text file in-comment
)))))
124 ;;; Creating labels ---------------------------------------------------------
126 (defun reftex-label (&optional environment no-insert
)
127 "Insert a unique label. Return the label.
128 If ENVIRONMENT is given, don't bother to find out yourself.
129 If NO-INSERT is non-nil, do not insert label into buffer.
130 With prefix arg, force to rescan document first.
131 When you are prompted to enter or confirm a label, and you reply with
132 just the prefix or an empty string, no label at all will be inserted.
133 A new label is also recorded into the label list.
134 This function is controlled by the settings of reftex-insert-label-flags."
138 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4).
139 (reftex-access-scan-info current-prefix-arg
)
141 ;; Find out what kind of environment this is and abort if necessary.
142 (if (or (not environment
)
143 (not (assoc environment reftex-env-or-mac-alist
)))
144 (setq environment
(reftex-label-location)))
146 (error "Can't figure out what kind of label should be inserted"))
150 (let* ((entry (assoc environment reftex-env-or-mac-alist
))
151 (typekey (nth 1 entry
))
152 (format (nth 3 entry
))
153 (macro-cell (reftex-what-macro 1))
154 (entry1 (assoc (car macro-cell
) reftex-env-or-mac-alist
))
155 label naked prefix valid default force-prompt rescan-is-useful
)
156 (when (and (or (nth 5 entry
) (nth 5 entry1
))
157 (memq (preceding-char) '(?\
[ ?\
{)))
158 ;; This is an argument of a label macro. Insert naked label.
159 (setq naked t format
"%s"))
161 (setq prefix
(or (cdr (assoc typekey reftex-typekey-to-prefix-alist
))
162 (concat typekey
"-")))
163 ;; Replace any escapes in the prefix
164 (setq prefix
(reftex-replace-prefix-escapes prefix
))
166 ;; Make a default label.
169 ((reftex-typekey-check typekey
(nth 0 reftex-insert-label-flags
))
170 ;; Derive a label from context.
171 (setq reftex-active-toc
(reftex-last-assoc-before-elt
172 'toc
(car (reftex-where-am-I))
173 (symbol-value reftex-docstruct-symbol
)))
174 (setq default
(reftex-no-props
175 (nth 2 (reftex-label-info " " nil nil t
))))
176 ;; Catch the cases where the is actually no context available.
177 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default
)
178 (string-match "INVALID VALUE OF PARSE" default
)
179 (string-match "SECTION HEADING NOT FOUND" default
)
180 (string-match "HOOK ERROR" default
)
181 (string-match "^[ \t]*$" default
))
183 force-prompt t
) ; need to prompt
186 (funcall reftex-string-to-label-function default
)))
189 (setq default
(reftex-uniquify-label default nil
"-"))))
191 ((reftex-typekey-check typekey
(nth 1 reftex-insert-label-flags
))
192 ;; Minimal default: the user will be prompted.
193 (setq default prefix
))
196 ;; Make an automatic label.
197 (setq default
(reftex-uniquify-label prefix t
))))
199 ;; Should we ask the user?
200 (if (or (reftex-typekey-check typekey
201 (nth 1 reftex-insert-label-flags
)) ; prompt
205 ;; iterate until we get a valid label
207 (setq label
(read-string
208 (if naked
"Naked Label: " "Label: ")
211 ;; Lets make sure that this is a valid label
214 ((string-match (concat "\\`\\(" (regexp-quote prefix
)
217 ;; No label at all, please
218 (message "No label inserted.")
221 ;; Test if label contains strange characters
222 ((string-match reftex-label-illegal-re label
)
223 (message "Label \"%s\" contains invalid characters" label
)
227 ;; Look it up in the label list
228 ((setq entry
(assoc label
229 (symbol-value reftex-docstruct-symbol
)))
232 (format "Label '%s' exists. Use anyway? " label
))
238 (setq label default
))
240 ;; Insert the label into the label list
241 (let* ((here-I-am-info
243 (if (and (or naked no-insert
)
244 (integerp (cdr macro-cell
)))
245 (goto-char (cdr macro-cell
)))
246 (reftex-where-am-I)))
247 (here-I-am (car here-I-am-info
))
248 (note (if (cdr here-I-am-info
)
250 "POSITION UNCERTAIN. RESCAN TO FIX."))
251 (file (buffer-file-name))
253 (tail (memq here-I-am
(symbol-value reftex-docstruct-symbol
))))
255 (or (cdr here-I-am-info
) (setq rescan-is-useful t
))
258 (push (list label typekey text file nil note
) (cdr tail
))
259 (put reftex-docstruct-symbol
'modified t
)))
261 ;; Insert the label into the buffer
264 (if reftex-format-label-function
265 (funcall reftex-format-label-function label format
)
266 (format format label
)))
267 (if (and reftex-plug-into-AUCTeX
268 (fboundp 'LaTeX-add-labels
))
269 ;; Tell AUCTeX about this
270 (LaTeX-add-labels label
)))
272 ;; Delete the corresponding selection buffers to force update on next use.
273 (when reftex-auto-update-selection-buffers
274 (reftex-erase-buffer (reftex-make-selection-buffer-name typekey
))
275 (reftex-erase-buffer (reftex-make-selection-buffer-name " ")))
277 (when (and rescan-is-useful reftex-allow-automatic-rescan
)
280 ;; return value of the function is the label
283 (defun reftex-string-to-label (string)
284 "Convert a string (a sentence) to a label.
285 Uses `reftex-derive-label-parameters' and `reftex-label-illegal-re'. It
286 also applies `reftex-translate-to-ascii-function' to the string."
287 (when (and reftex-translate-to-ascii-function
288 (fboundp reftex-translate-to-ascii-function
))
289 (setq string
(funcall reftex-translate-to-ascii-function string
)))
290 (apply 'reftex-convert-string string
291 "[-~ \t\n\r,;]+" reftex-label-illegal-re nil nil
292 reftex-derive-label-parameters
))
294 (defun reftex-latin1-to-ascii (string)
295 ;; Translate the upper 128 chars in the Latin-1 charset to ASCII equivalents
296 (let ((tab "@@@@@@@@@@@@@@@@@@'@@@@@@@@@@@@@ icLxY|S\"ca<--R-o|23'uq..1o>423?AAAAAAACEEEEIIIIDNOOOOOXOUUUUYP3aaaaaaaceeeeiiiidnooooo:ouuuuypy")
297 (emacsp (not (featurep 'xemacs
))))
300 (cond ((and (> c
127) (< c
256)) ; 8 bit Latin-1
301 (char-to-string (aref tab
(- c
128))))
302 ((and emacsp
; Not for XEmacs
303 (> c
2175) (< c
2304)) ; Mule Latin-1
304 (char-to-string (aref tab
(- c
2176))))
305 (t (char-to-string c
))))
308 (defun reftex-replace-prefix-escapes (prefix)
309 ;; Replace %escapes in a label prefix
311 (let (letter (num 0) replace
)
312 (while (string-match "\\%\\([a-zA-Z]\\)" prefix num
)
313 (setq letter
(match-string 1 prefix
))
318 (file-name-sans-extension
319 (file-name-nondirectory (buffer-file-name))))
321 (let ((masterdir (file-name-directory (reftex-TeX-master-file)))
322 (file (file-name-sans-extension (buffer-file-name))))
323 (if (string-match (concat "\\`" (regexp-quote masterdir
))
325 (substring file
(length masterdir
))
328 (file-name-sans-extension
329 (file-name-nondirectory (reftex-TeX-master-file))))
331 (file-name-nondirectory
332 (substring (file-name-directory (reftex-TeX-master-file))
335 (or (user-login-name) ""))
337 (let* (macro level-exp level
)
340 (when (re-search-backward reftex-section-regexp nil t
)
341 (setq macro
(reftex-match-string 2)
342 level-exp
(cdr (assoc macro reftex-section-levels-all
))
343 level
(if (symbolp level-exp
)
344 (abs (save-match-data
345 (funcall level-exp
)))
347 (cdr (or (assoc macro reftex-section-prefixes
)
348 (assoc level reftex-section-prefixes
)
349 (assq t reftex-section-prefixes
)
350 (list t
"sec:")))))))
352 (setq num
(1- (+ (match-beginning 1) (length replace
)))
353 prefix
(replace-match replace nil nil prefix
)))
356 (defun reftex-uniquify-label (label &optional force separator
)
357 ;; Make label unique by appending a number.
358 ;; Optional FORCE means, force appending a number, even if label is unique.
359 ;; Optional SEPARATOR is a string to stick between label and number.
361 ;; Ensure access to scanning info
362 (reftex-access-scan-info)
366 (not (assoc label
(symbol-value reftex-docstruct-symbol
))))
369 (let* ((label-numbers (assq 'label-numbers
370 (symbol-value reftex-docstruct-symbol
)))
371 (label-numbers-alist (cdr label-numbers
))
372 (cell (or (assoc label label-numbers-alist
)
373 (car (setcdr label-numbers
375 label-numbers-alist
)))))
376 (num (1+ (cdr cell
)))
377 (sep (or separator
"")))
378 (while (assoc (concat label sep
(int-to-string num
))
379 (symbol-value reftex-docstruct-symbol
))
382 (concat label sep
(int-to-string num
))))))
384 ;;; Referencing labels ------------------------------------------------------
386 ;; Help string for the reference label menu
387 (defconst reftex-select-label-prompt
388 "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more")
390 (defconst reftex-select-label-help
391 " n / p Go to next/previous label (Cursor motion works as well)
392 C-c C-n/p Go to next/previous section heading.
393 b / l Jump back to previous selection / Reuse last referenced label.
394 z Jump to a specific section, e.g. '3 z' jumps to section 3.
395 g / s Update menu / Switch label type.
396 r / C-u r Reparse document / Reparse entire document.
397 x Switch to label menu of external document (with LaTeX package `xr').
398 F t c Toggle: [F]ile borders, [t]able of contents, [c]ontext
399 # % Toggle: [#] label counters, [%] labels in comments
400 SPC / f Show full context in other window / Toggle follow mode.
401 . Show insertion point in other window.
402 v / V Toggle \\ref <-> \\vref / Rotate \\ref <=> \\fref <=> \\Fref
403 TAB Enter a label with completion.
404 m , - + Mark entry. `,-+' also assign a separator.
405 a / A Put all marked entries into one/many \\ref commands.
406 q / RET Quit without referencing / Accept current label (also on mouse-2).")
408 (defun reftex-reference (&optional type no-insert cut
)
409 "Make a LaTeX reference. Look only for labels of a certain TYPE.
410 With prefix arg, force to rescan buffer for labels. This should only be
411 necessary if you have recently entered labels yourself without using
412 reftex-label. Rescanning of the buffer can also be requested from the
413 label selection menu.
414 The function returns the selected label or nil.
415 If NO-INSERT is non-nil, do not insert \\ref command, just return label.
416 When called with 2 C-u prefix args, disable magic word recognition."
420 ;; check for active recursive edits
421 (reftex-check-recursive-edit)
423 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4)
424 (reftex-access-scan-info current-prefix-arg
)
427 ;; guess type from context
428 (if (and reftex-guess-label-type
429 (setq type
(reftex-guess-label-type)))
432 (setq type
(reftex-query-label-type))))
435 (cond ((reftex-typekey-check type reftex-vref-is-default
) "\\vref")
436 ((reftex-typekey-check type reftex-fref-is-default
) "\\fref")
438 (reftex-format-ref-function reftex-format-ref-function
)
440 label labels sep sep1
)
442 ;; Have the user select a label
443 (set-marker reftex-select-return-marker
(point))
444 (setq labels
(save-excursion
445 (reftex-offer-label-menu type
)))
446 (reftex-ensure-compiled-variables)
447 (set-marker reftex-select-return-marker nil
)
448 ;; If the first entry is the symbol 'concat, concat all labels.
449 ;; We keep the cdr of the first label for typekey etc information.
450 (if (eq (car labels
) 'concat
)
451 (setq labels
(list (list (mapconcat 'car
(cdr labels
) ",")
452 (cdr (nth 1 labels
))))))
453 (setq type
(nth 1 (car labels
))
454 form
(or (cdr (assoc type reftex-typekey-to-format-alist
))
459 ;; Just return the first label
466 (setq label
(car (car labels
))
467 sep
(nth 2 (car labels
))
468 sep1
(cdr (assoc sep reftex-multiref-punctuation
))
471 (backward-delete-char cut
)
474 ;; remove ~ if we do already have a space
475 (when (and (= ?~
(string-to-char form
))
476 (member (preceding-char) '(?\ ?
\t ?
\n ?~
)))
477 (setq form
(substring form
1)))
478 ;; do we have a special format?
479 (setq reftex-format-ref-function
481 ((string= refstyle
"\\vref") 'reftex-format-vref
)
482 ((string= refstyle
"\\fref") 'reftex-format-fref
)
483 ((string= refstyle
"\\Fref") 'reftex-format-Fref
)
484 (t reftex-format-ref-function
)))
485 ;; ok, insert the reference
486 (if sep1
(insert sep1
))
488 (if reftex-format-ref-function
489 (funcall reftex-format-ref-function label form
)
490 (format form label label
)))
491 ;; take out the initial ~ for good
492 (and (= ?~
(string-to-char form
))
493 (setq form
(substring form
1))))
497 (defun reftex-guess-label-type ()
498 ;; Examine context to guess what a \ref might want to reference.
499 (let ((words reftex-words-to-typekey-alist
)
501 (bound (max (point-min) (- (point) 35)))
504 (while (and (setq cell
(pop words
))
506 (re-search-backward (car cell
) bound t
))))))
508 (cons (cdr cell
) (- (match-end 0) (match-end 1)))
511 (defvar reftex-select-label-map
)
512 (defun reftex-offer-label-menu (typekey)
513 ;; Offer a menu with the appropriate labels.
514 (let* ((buf (current-buffer))
515 (xr-data (assq 'xr
(symbol-value reftex-docstruct-symbol
)))
516 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data
)))
518 (here-I-am (car (reftex-where-am-I)))
519 (here-I-am1 here-I-am
)
520 (toc (reftex-typekey-check typekey reftex-label-menu-flags
0))
521 (files (reftex-typekey-check typekey reftex-label-menu-flags
7))
522 (context (not (reftex-typekey-check
523 typekey reftex-label-menu-flags
3)))
524 (counter (reftex-typekey-check
525 typekey reftex-label-menu-flags
2))
526 (follow (reftex-typekey-check
527 typekey reftex-label-menu-flags
4))
528 (commented (nth 5 reftex-label-menu-flags
))
531 offset rtn key data last-data entries
)
536 (save-window-excursion
537 (delete-other-windows)
538 (setq reftex-call-back-to-this-buffer buf
539 reftex-latex-syntax-table
(syntax-table))
540 (let ((default-major-mode 'reftex-select-label-mode
))
541 (if reftex-use-multiple-selection-buffers
542 (switch-to-buffer-other-window
545 (reftex-make-selection-buffer-name typekey
)))
546 (switch-to-buffer-other-window "*RefTeX Select*")
547 (reftex-erase-buffer)))
548 (unless (eq major-mode
'reftex-select-label-mode
)
549 (reftex-select-label-mode))
550 (add-to-list 'selection-buffers
(current-buffer))
551 (setq truncate-lines t
)
552 (setq mode-line-format
553 (list "---- " 'mode-line-buffer-identification
554 " " 'global-mode-string
" (" mode-name
")"
559 (let ((buffer-read-only nil
))
560 (message "Creating Selection Buffer...")
561 (setq offset
(reftex-insert-docstruct
570 (or here-I-am offset
)
572 nil
; no a toc buffer
575 (setq offset
(reftex-get-offset buf here-I-am typekey
)))
577 (setq buffer-read-only t
)
578 (setq offset
(or offset t
))
580 (setq here-I-am nil
) ; turn off determination of offset
583 reftex-select-label-prompt
584 reftex-select-label-help
585 reftex-select-label-map
587 'reftex-show-label-location follow
))
590 last-data
(nth 2 rtn
)
592 (unless key
(throw 'exit nil
))
596 (reftex-erase-buffer))
600 (and current-prefix-arg
(setq key ?R
))
601 (reftex-erase-buffer)
602 (reftex-reparse-document buf last-data key
))
604 ;; toggle context mode
605 (reftex-erase-buffer)
606 (setq context
(not context
)))
609 (setq here-I-am here-I-am1
)
610 (setq typekey
(reftex-query-label-type)))
612 ;; toggle table of contents display, or change depth
613 (reftex-erase-buffer)
614 (if current-prefix-arg
615 (setq reftex-toc-max-level
(prefix-numeric-value
617 (setq toc
(not toc
))))
619 ;; toggle display of included file borders
620 (reftex-erase-buffer)
621 (setq files
(not files
)))
623 ;; toggle counter display
624 (reftex-erase-buffer)
625 (setq counter
(not counter
)))
627 ;; toggle display of commented labels
628 (reftex-erase-buffer)
629 (setq commented
(not commented
)))
631 ;; reuse the last referenced label again
632 (setq entries reftex-last-used-reference
)
635 ;; select an external document
636 (setq xr-index
(reftex-select-external-document
638 (setq buf
(or (reftex-get-file-buffer-force
639 (cdr (nth xr-index xr-alist
)))
640 (error "Cannot switch document"))
641 prefix
(or (car (nth xr-index xr-alist
)) ""))
643 (reftex-access-scan-info))
648 (or (assoc key
(symbol-value reftex-docstruct-symbol
))
649 (list key typekey
)))))
651 ((memq key
'(?a ?A return
))
653 (reftex-select-marked
654 (setq entries
(nreverse reftex-select-marked
)))
656 (setq entries
(list (list data
))))
657 (t (setq entries nil
)))
659 (if (equal key ?a
) (push 'concat entries
))
660 (setq reftex-last-used-reference entries
))
663 (t (error "This should not happen (reftex-offer-label-menu)"))))))
665 (while reftex-buffers-with-changed-invisibility
666 (set-buffer (car (car reftex-buffers-with-changed-invisibility
)))
667 (setq buffer-invisibility-spec
668 (cdr (pop reftex-buffers-with-changed-invisibility
)))))
669 (mapcar (lambda (buf) (and (buffer-live-p buf
) (bury-buffer buf
)))
671 (reftex-kill-temporary-buffers))
672 ;; Add the prefixes, put together the relevant information in the form
673 ;; (LABEL TYPEKEY SEPARATOR) and return a list of those.
676 (list (concat prefix
(car (car x
)))
682 (defun reftex-reparse-document (&optional buffer data key
)
683 ;; Rescan the document.
684 (save-window-excursion
687 (if (not (bufferp buffer
))
688 (error "No such buffer %s" (buffer-name buffer
))
689 (set-buffer buffer
)))
690 (let ((arg (if (eq key ?R
) '(16) '(4)))
692 (reftex-access-scan-info arg file
)))))
694 (defun reftex-query-label-type ()
695 ;; Ask for label type
696 (let ((key (reftex-select-with-char
697 reftex-type-query-prompt reftex-type-query-help
3)))
698 (unless (member (char-to-string key
) reftex-typekey-list
)
699 (error "No such label type: %s" (char-to-string key
)))
700 (char-to-string key
)))
702 (defun reftex-show-label-location (data forward no-revisit
703 &optional stay error
)
704 ;; View the definition site of a label in another window.
705 ;; DATA is an entry from the docstruct list.
706 ;; FORWARD indicates if the label is likely forward from current point.
707 ;; NO-REVISIT means do not load a file to show this label.
708 ;; STAY means leave the new window selected.
709 ;; ERROR means throw an error exception when the label cannot be found.
710 ;; If ERROR is nil, the return value of this function indicates success.
711 (let* ((this-window (selected-window))
712 (errorf (if error
'error
'message
))
713 label file buffer re found
)
716 (setq label
(nth 0 data
)
720 (funcall errorf
"Unknown label - reparse might help")
723 ;; Goto the file in another window
726 (reftex-get-buffer-visiting file
)
727 (reftex-get-file-buffer-force
728 file
(not reftex-keep-temporary-buffers
))))
730 ;; good - the file is available
731 (switch-to-buffer-other-window buffer
)
732 ;; we have got a problem here. The file does not exist.
733 ;; Let' get out of here..
734 (funcall errorf
"Label %s not found" label
)
737 ;; search for that label
738 (setq re
(format reftex-find-label-regexp-format
(regexp-quote label
)))
741 (re-search-forward re nil t
)
742 (re-search-backward re nil t
)))
744 (goto-char (point-min))
745 (unless (setq found
(re-search-forward re nil t
))
746 ;; Ooops. Must be in a macro with distributed args.
749 (format reftex-find-label-regexp-format2
750 (regexp-quote label
)) nil t
))))
753 (reftex-highlight 0 (match-beginning 3) (match-end 3))
754 (reftex-show-entry (match-beginning 3) (match-end 3))
756 (unless stay
(select-window this-window
)))
757 (select-window this-window
)
758 (funcall errorf
"Label %s not found" label
))
761 (defvar font-lock-mode
)
762 (defun reftex-show-entry (beg-hlt end-hlt
)
763 ;; Show entry if point is hidden
764 (let* ((n (/ (reftex-window-height) 2))
766 (re-search-backward "[\n\r]" nil
1 n
) (point)))
768 (re-search-forward "[\n\r]" nil
1 n
) (point))))
770 ((and (boundp 'buffer-invisibility-spec
) buffer-invisibility-spec
771 (get-char-property (1+ beg-hlt
) 'invisible
))
772 ;; Invisible with text properties. That is easy to change.
773 (push (cons (current-buffer) buffer-invisibility-spec
)
774 reftex-buffers-with-changed-invisibility
)
775 (setq buffer-invisibility-spec nil
))
776 ((string-match "\r" (buffer-substring beg end
))
777 ;; Invisible with selective display. We need to copy it.
778 (let ((string (buffer-substring-no-properties beg end
)))
779 (switch-to-buffer "*RefTeX Context Copy*")
780 (setq buffer-read-only nil
)
783 (subst-char-in-region (point-min) (point-max) ?
\r ?
\n t
)
784 (goto-char (- beg-hlt beg
))
785 (reftex-highlight 0 (1+ (- beg-hlt beg
)) (1+ (- end-hlt beg
)))
786 (if (reftex-refontify)
787 (when (or (not (eq major-mode
'latex-mode
))
788 (not font-lock-mode
))
791 'reftex-pre-refontification-functions
792 reftex-call-back-to-this-buffer
'reftex-hidden
)
794 (when (or (not (eq major-mode
'fundamental-mode
))
797 (run-hooks 'reftex-display-copied-context-hook
)
798 (setq buffer-read-only t
))))))
800 (defun reftex-varioref-vref ()
801 "Insert a reference using the `\\vref' macro from the varioref package."
803 (let ((reftex-format-ref-function 'reftex-format-vref
))
805 (defun reftex-fancyref-fref ()
806 "Insert a reference using the `\\fref' macro from the fancyref package."
808 (let ((reftex-format-ref-function 'reftex-format-fref
)
809 ;;(reftex-guess-label-type nil) ;FIXME do we want this????
812 (defun reftex-fancyref-Fref ()
813 "Insert a reference using the `\\Fref' macro from the fancyref package."
815 (let ((reftex-format-ref-function 'reftex-format-Fref
)
816 ;;(reftex-guess-label-type nil) ;FIXME do we want this????
820 (defun reftex-format-vref (label fmt
)
821 (while (string-match "\\\\ref{" fmt
)
822 (setq fmt
(replace-match "\\vref{" t t fmt
)))
823 (format fmt label label
))
824 (defun reftex-format-Fref (label def-fmt
)
825 (format "\\Fref{%s}" label
))
826 (defun reftex-format-fref (label def-fmt
)
827 (format "\\fref{%s}" label
))
829 (defun reftex-goto-label (&optional other-window
)
830 "Prompt for a label (with completion) and jump to the location of this label.
831 Optional prefix argument OTHER-WINDOW goes to the label in another window."
833 (reftex-access-scan-info)
834 (let* ((wcfg (current-window-configuration))
835 (docstruct (symbol-value reftex-docstruct-symbol
))
836 (label (completing-read "Label: " docstruct
837 (lambda (x) (stringp (car x
))) t
))
838 (selection (assoc label docstruct
))
840 (reftex-show-label-location selection t nil
'stay
)
843 (set-window-configuration wcfg
)
844 (switch-to-buffer (marker-buffer where
))
846 (reftex-unhighlight 0)))
850 ;;; arch-tag: 52f14032-fb76-4d31-954f-750c72415675
851 ;;; reftex-ref.el ends here