1 ;;; reftex-ref.el --- code to create labels and references with RefTeX
3 ;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
30 (require 'reftex-parse
)
33 (defun reftex-label-location (&optional bound
)
34 "Return the environment or macro which determines the label type at point.
35 If optional BOUND is an integer, limit backward searches to that point."
37 (let* ((loc1 (reftex-what-macro reftex-label-mac-list bound
))
38 (loc2 (reftex-what-environment reftex-label-env-list bound
))
39 (loc3 (reftex-what-special-env 1 bound
))
40 (p1 (or (cdr loc1
) 0))
41 (p2 (or (cdr loc2
) 0))
42 (p3 (or (cdr loc3
) 0))
43 (pmax (max p1 p2 p3
)))
45 (setq reftex-location-start pmax
)
48 ;; A macro. Default context after macro name.
49 (setq reftex-default-context-position
(+ p1
(length (car loc1
))))
50 (or (car loc1
) "section"))
52 ;; An environment. Default context after \begin{name}.
53 (setq reftex-default-context-position
(+ p2
8 (length (car loc2
))))
54 (or (car loc2
) "section"))
56 ;; A special. Default context right there.
57 (setq reftex-default-context-position p3
)
58 (setq loc3
(car loc3
))
59 (cond ((null loc3
) "section")
60 ((symbolp loc3
) (symbol-name loc3
))
63 (t ;; This should not happen, I think?
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))
74 (comment (nth 4 cell
))
76 (buf (reftex-get-file-buffer-force
77 file
(not (eq t reftex-keep-temporary-buffers
)))))
79 (list label typekey
"" file comment
"LOST LABEL. RESCAN TO FIX.")
80 (with-current-buffer buf
85 (if (or (re-search-forward
86 (format reftex-find-label-regexp-format
87 (regexp-quote label
)) nil t
)
89 (format reftex-find-label-regexp-format2
90 (regexp-quote label
)) nil t
))
94 (append (reftex-label-info label file
) (list note
)))
95 (list label typekey
"" file
"LOST LABEL. RESCAN TO FIX.")))))))
98 (defun reftex-label-info (label &optional file bound derive env-or-mac
)
99 ;; Return info list on LABEL at point.
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
)))
103 (file (or file
(buffer-file-name)))
104 (trust reftex-trust-label-prefix
)
105 (in-comment (reftex-in-comment)))
107 (cond ((eq trust t
) t
)
109 ((stringp trust
) (string-match trust typekey
))
110 ((listp trust
) (member typekey trust
))
113 (reftex-nicify-text (reftex-context-substring))
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
120 (list label typekey text file in-comment
)))))
122 ;;; Creating labels ---------------------------------------------------------
125 (defun reftex-label (&optional environment no-insert
)
126 "Insert a unique label. Return the label.
127 If ENVIRONMENT is given, don't bother to find out yourself.
128 If NO-INSERT is non-nil, do not insert label into buffer.
129 With prefix arg, force to rescan document first.
130 When you are prompted to enter or confirm a label, and you reply with
131 just the prefix or an empty string, no label at all will be inserted.
132 A new label is also recorded into the label list.
133 This function is controlled by the settings of reftex-insert-label-flags."
137 ;; Ensure access to scanning info and rescan buffer if prefix are is '(4).
138 (reftex-access-scan-info current-prefix-arg
)
140 ;; Find out what kind of environment this is and abort if necessary.
141 (if (or (not environment
)
142 (not (assoc environment reftex-env-or-mac-alist
)))
143 (setq environment
(reftex-label-location)))
145 (error "Can't figure out what kind of label should be inserted"))
149 (let* ((entry (assoc environment reftex-env-or-mac-alist
))
150 (typekey (nth 1 entry
))
151 (format (nth 3 entry
))
152 (macro-cell (reftex-what-macro 1))
153 (entry1 (assoc (car macro-cell
) reftex-env-or-mac-alist
))
154 label naked prefix valid default force-prompt rescan-is-useful
)
155 (when (and (or (nth 5 entry
) (nth 5 entry1
))
156 (memq (preceding-char) '(?\
[ ?\
{)))
157 ;; This is an argument of a label macro. Insert naked label.
158 (setq naked t format
"%s"))
160 (setq prefix
(or (cdr (assoc typekey reftex-typekey-to-prefix-alist
))
161 (concat typekey
"-")))
162 ;; Replace any escapes in the prefix
163 (setq prefix
(reftex-replace-prefix-escapes prefix
))
165 ;; Make a default label.
168 ((reftex-typekey-check typekey
(nth 0 reftex-insert-label-flags
))
169 ;; Derive a label from context.
170 (setq reftex-active-toc
(reftex-last-assoc-before-elt
171 'toc
(car (reftex-where-am-I))
172 (symbol-value reftex-docstruct-symbol
)))
173 (setq default
(reftex-no-props
174 (nth 2 (reftex-label-info " " nil nil t
))))
175 ;; Catch the cases where the is actually no context available.
176 (if (or (string-match "NO MATCH FOR CONTEXT REGEXP" default
)
177 (string-match "INVALID VALUE OF PARSE" default
)
178 (string-match "SECTION HEADING NOT FOUND" default
)
179 (string-match "HOOK ERROR" default
)
180 (string-match "^[ \t]*$" default
))
182 force-prompt t
) ; need to prompt
185 (funcall reftex-string-to-label-function default
)))
188 (setq default
(reftex-uniquify-label default nil
"-"))))
190 ((reftex-typekey-check typekey
(nth 1 reftex-insert-label-flags
))
191 ;; Minimal default: the user will be prompted.
192 (setq default prefix
))
195 ;; Make an automatic label.
196 (setq default
(reftex-uniquify-label prefix t
))))
198 ;; Should we ask the user?
199 (if (or (reftex-typekey-check typekey
200 (nth 1 reftex-insert-label-flags
)) ; prompt
204 ;; iterate until we get a valid label
206 (setq label
(read-string
207 (if naked
"Naked Label: " "Label: ")
210 ;; Let's make sure that this is a valid label
213 ((string-match (concat "\\`\\(" (regexp-quote prefix
)
216 ;; No label at all, please
217 (message "No label inserted.")
220 ;; Test if label contains strange characters
221 ((string-match reftex-label-illegal-re label
)
222 (message "Label \"%s\" contains invalid characters" label
)
226 ;; Look it up in the label list
227 ((setq entry
(assoc label
228 (symbol-value reftex-docstruct-symbol
)))
231 (format "Label '%s' exists. Use anyway? " label
))
237 (setq label default
))
239 ;; Insert the label into the label list
240 (let* ((here-I-am-info
242 (if (and (or naked no-insert
)
243 (integerp (cdr macro-cell
)))
244 (goto-char (cdr macro-cell
)))
245 (reftex-where-am-I)))
246 (here-I-am (car here-I-am-info
))
247 (note (if (cdr here-I-am-info
)
249 "POSITION UNCERTAIN. RESCAN TO FIX."))
250 (file (buffer-file-name))
252 (tail (memq here-I-am
(symbol-value reftex-docstruct-symbol
))))
254 (or (cdr here-I-am-info
) (setq rescan-is-useful t
))
257 (push (list label typekey text file nil note
) (cdr tail
))
258 (put reftex-docstruct-symbol
'modified t
)))
260 ;; Insert the label into the buffer
263 (if reftex-format-label-function
264 (funcall reftex-format-label-function label format
)
265 (format format label
)))
266 (if (and reftex-plug-into-AUCTeX
267 (fboundp 'LaTeX-add-labels
))
268 ;; Tell AUCTeX about this
269 (LaTeX-add-labels label
)))
271 ;; Delete the corresponding selection buffers to force update on next use.
272 (when reftex-auto-update-selection-buffers
273 (reftex-erase-buffer (reftex-make-selection-buffer-name typekey
))
274 (reftex-erase-buffer (reftex-make-selection-buffer-name " ")))
276 (when (and rescan-is-useful reftex-allow-automatic-rescan
)
279 ;; return value of the function is the label
282 (defun reftex-string-to-label (string)
283 "Convert a string (a sentence) to a label.
284 Uses `reftex-derive-label-parameters' and `reftex-label-illegal-re'. It
285 also applies `reftex-translate-to-ascii-function' to the string."
286 (when (and reftex-translate-to-ascii-function
287 (fboundp reftex-translate-to-ascii-function
))
288 (setq string
(funcall reftex-translate-to-ascii-function string
)))
289 (apply 'reftex-convert-string string
290 "[-~ \t\n\r,;]+" reftex-label-illegal-re nil nil
291 reftex-derive-label-parameters
))
293 (defun reftex-latin1-to-ascii (string)
294 ;; Translate the upper 128 chars in the Latin-1 charset to ASCII equivalents
295 (let ((tab "@@@@@@@@@@@@@@@@@@'@@@@@@@@@@@@@ icLxY|S\"ca<--R-o|23'uq..1o>423?AAAAAAACEEEEIIIIDNOOOOOXOUUUUYP3aaaaaaaceeeeiiiidnooooo:ouuuuypy")
296 (emacsp (not (featurep 'xemacs
))))
299 (cond ((and (> c
127) (< c
256)) ; 8 bit Latin-1
300 (char-to-string (aref tab
(- c
128))))
301 ((and emacsp
; Not for XEmacs
302 (> c
2175) (< c
2304)) ; Mule Latin-1
303 (char-to-string (aref tab
(- c
2176))))
304 (t (char-to-string c
))))
307 (defun reftex-replace-prefix-escapes (prefix)
308 ;; Replace %escapes in a label prefix
310 (let (letter (num 0) replace
)
311 (while (string-match "\\%\\([a-zA-Z]\\)" prefix num
)
312 (setq letter
(match-string 1 prefix
))
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
))
323 (substring file
(length masterdir
))
326 (file-name-base (reftex-TeX-master-file)))
328 (file-name-nondirectory
329 (substring (file-name-directory (reftex-TeX-master-file))
332 (or (user-login-name) ""))
334 (let* (macro level-exp level
)
337 (when (re-search-backward reftex-section-regexp nil t
)
338 (setq macro
(reftex-match-string 2)
339 level-exp
(cdr (assoc macro reftex-section-levels-all
))
340 level
(if (symbolp level-exp
)
341 (abs (save-match-data
342 (funcall level-exp
)))
344 (cdr (or (assoc macro reftex-section-prefixes
)
345 (assoc level reftex-section-prefixes
)
346 (assq t reftex-section-prefixes
)
347 (list t
"sec:")))))))
349 (setq num
(1- (+ (match-beginning 1) (length replace
)))
350 prefix
(replace-match replace nil nil prefix
)))
353 (defun reftex-uniquify-label (label &optional force separator
)
354 ;; Make label unique by appending a number.
355 ;; Optional FORCE means, force appending a number, even if label is unique.
356 ;; Optional SEPARATOR is a string to stick between label and number.
358 ;; Ensure access to scanning info
359 (reftex-access-scan-info)
363 (not (assoc label
(symbol-value reftex-docstruct-symbol
))))
366 (let* ((label-numbers (assq 'label-numbers
367 (symbol-value reftex-docstruct-symbol
)))
368 (label-numbers-alist (cdr label-numbers
))
369 (cell (or (assoc label label-numbers-alist
)
370 (car (setcdr label-numbers
372 label-numbers-alist
)))))
373 (num (1+ (cdr cell
)))
374 (sep (or separator
"")))
375 (while (assoc (concat label sep
(int-to-string num
))
376 (symbol-value reftex-docstruct-symbol
))
379 (concat label sep
(int-to-string num
))))))
381 ;;; Referencing labels ------------------------------------------------------
383 ;; Help string for the reference label menu
384 (defconst reftex-select-label-prompt
385 "Select: [n]ext [p]revious [r]escan [ ]context e[x]tern [q]uit RET [?]HELP+more")
387 (defconst reftex-select-label-help
388 " n / p Go to next/previous label (Cursor motion works as well)
389 C-c C-n/p Go to next/previous section heading.
390 b / l Jump back to previous selection / Reuse last referenced label.
391 z Jump to a specific section, e.g. '3 z' jumps to section 3.
392 g / s Update menu / Switch label type.
393 r / C-u r Reparse document / Reparse entire document.
394 x Switch to label menu of external document (with LaTeX package `xr').
395 F t c Toggle: [F]ile borders, [t]able of contents, [c]ontext
396 # % Toggle: [#] label counters, [%] labels in comments
397 SPC / f Show full context in other window / Toggle follow mode.
398 . Show insertion point in other window.
399 v / V Toggle \\ref <-> \\vref / Rotate \\ref <=> \\fref <=> \\Fref
400 TAB Enter a label with completion.
401 m , - + Mark entry. `,-+' also assign a separator.
402 a / A Put all marked entries into one/many \\ref commands.
403 q / RET Quit without referencing / Accept current label (also on mouse-2).")
406 (defun reftex-reference (&optional type no-insert cut
)
407 "Make a LaTeX reference. Look only for labels of a certain TYPE.
408 With prefix arg, force to rescan buffer for labels. This should only be
409 necessary if you have recently entered labels yourself without using
410 reftex-label. Rescanning of the buffer can also be requested from the
411 label selection menu.
412 The function returns the selected label or nil.
413 If NO-INSERT is non-nil, do not insert \\ref command, just return label.
414 When called with 2 C-u prefix args, disable magic word recognition."
418 ;; Check for active recursive edits
419 (reftex-check-recursive-edit)
421 ;; Ensure access to scanning info and rescan buffer if prefix is '(4)
422 (reftex-access-scan-info current-prefix-arg
)
424 (let ((reftex-refstyle (when (and (boundp 'reftex-refstyle
) reftex-refstyle
)
426 (reftex-format-ref-function reftex-format-ref-function
)
428 label labels sep sep1 style-alist
)
430 (unless reftex-refstyle
431 (if reftex-ref-macro-prompt
433 ;; Build a temporary list which handles more easily.
434 (dolist (elt reftex-ref-style-alist
)
435 (when (member (car elt
) (reftex-ref-style-list))
437 (add-to-list 'style-alist
(cons (cadr x
) (car x
)) t
))
439 ;; Prompt the user for the macro.
440 (let ((key (reftex-select-with-char
441 "" (concat "SELECT A REFERENCE FORMAT\n\n"
444 (format "[%c] %s %s" (car x
)
445 (if (> (car x
) 31) " " "")
447 style-alist
"\n")))))
448 (setq reftex-refstyle
(cdr (assoc key style-alist
)))
449 (unless reftex-refstyle
450 (error "No reference macro associated with key `%c'" key
))))
451 ;; Get the first macro from `reftex-ref-style-alist' which
452 ;; matches the first entry in the list of active styles.
453 (setq reftex-refstyle
454 (or (caar (nth 2 (assoc (car (reftex-ref-style-list))
455 reftex-ref-style-alist
)))
456 ;; Use the first entry in r-r-s-a as a last resort.
457 (caar (nth 2 (car reftex-ref-style-alist
)))))))
460 ;; Guess type from context
461 (if (and reftex-guess-label-type
462 (setq type
(reftex-guess-label-type)))
465 (setq type
(reftex-query-label-type))))
467 ;; Have the user select a label
468 (set-marker reftex-select-return-marker
(point))
469 (setq labels
(save-excursion
470 (reftex-offer-label-menu type
)))
471 (reftex-ensure-compiled-variables)
472 (set-marker reftex-select-return-marker nil
)
473 ;; If the first entry is the symbol 'concat, concat all labels.
474 ;; We keep the cdr of the first label for typekey etc information.
475 (if (eq (car labels
) 'concat
)
476 (setq labels
(list (list (mapconcat 'car
(cdr labels
) ",")
477 (cdr (nth 1 labels
))))))
478 (setq type
(nth 1 (car labels
))
479 form
(or (cdr (assoc type reftex-typekey-to-format-alist
))
484 ;; Just return the first label
491 (setq label
(car (car labels
))
492 sep
(nth 2 (car labels
))
493 sep1
(cdr (assoc sep reftex-multiref-punctuation
))
496 (backward-delete-char cut
)
499 ;; remove ~ if we do already have a space
500 (when (and (= ?~
(string-to-char form
))
501 (member (preceding-char) '(?\ ?
\t ?
\n ?~
)))
502 (setq form
(substring form
1)))
503 ;; do we have a special format?
504 (unless (string= reftex-refstyle
"\\ref")
505 (setq reftex-format-ref-function
'reftex-format-special
))
506 ;; ok, insert the reference
507 (if sep1
(insert sep1
))
509 (if reftex-format-ref-function
510 (funcall reftex-format-ref-function label form reftex-refstyle
)
511 (format form label label
)))
512 ;; take out the initial ~ for good
513 (and (= ?~
(string-to-char form
))
514 (setq form
(substring form
1))))
518 (defun reftex-guess-label-type ()
519 ;; Examine context to guess what a \ref might want to reference.
520 (let ((words reftex-words-to-typekey-alist
)
522 (bound (max (point-min) (- (point) 35)))
525 (while (and (setq cell
(pop words
))
527 (re-search-backward (car cell
) bound t
))))))
529 (cons (cdr cell
) (- (match-end 0) (match-end 1)))
532 (defvar reftex-select-label-map
)
533 (defun reftex-offer-label-menu (typekey)
534 ;; Offer a menu with the appropriate labels.
535 (let* ((buf (current-buffer))
536 (xr-data (assq 'xr
(symbol-value reftex-docstruct-symbol
)))
537 (xr-alist (cons (cons "" (buffer-file-name)) (nth 1 xr-data
)))
539 (here-I-am (car (reftex-where-am-I)))
540 (here-I-am1 here-I-am
)
541 (toc (reftex-typekey-check typekey reftex-label-menu-flags
0))
542 (files (reftex-typekey-check typekey reftex-label-menu-flags
7))
543 (context (not (reftex-typekey-check
544 typekey reftex-label-menu-flags
3)))
545 (counter (reftex-typekey-check
546 typekey reftex-label-menu-flags
2))
547 (follow (reftex-typekey-check
548 typekey reftex-label-menu-flags
4))
549 (commented (nth 5 reftex-label-menu-flags
))
552 offset rtn key data last-data entries
)
557 (save-window-excursion
558 (delete-other-windows)
559 (setq reftex-call-back-to-this-buffer buf
560 reftex-latex-syntax-table
(syntax-table))
561 (if reftex-use-multiple-selection-buffers
562 (switch-to-buffer-other-window
563 (with-current-buffer buf
564 (reftex-make-selection-buffer-name typekey
)))
565 (switch-to-buffer-other-window "*RefTeX Select*")
566 (reftex-erase-buffer))
567 (unless (eq major-mode
'reftex-select-label-mode
)
568 (reftex-select-label-mode))
569 (add-to-list 'selection-buffers
(current-buffer))
570 (setq truncate-lines t
)
571 (setq mode-line-format
572 (list "---- " 'mode-line-buffer-identification
573 " " 'global-mode-string
" (" mode-name
")"
574 " S<" 'reftex-refstyle
">"
578 (let ((buffer-read-only nil
))
579 (message "Creating Selection Buffer...")
580 (setq offset
(reftex-insert-docstruct
589 (or here-I-am offset
)
591 nil
; no a toc buffer
594 (setq offset
(reftex-get-offset buf here-I-am typekey
)))
596 (setq buffer-read-only t
)
597 (setq offset
(or offset t
))
599 (setq here-I-am nil
) ; turn off determination of offset
602 reftex-select-label-prompt
603 reftex-select-label-help
604 reftex-select-label-map
606 'reftex-show-label-location follow
))
609 last-data
(nth 2 rtn
)
611 (unless key
(throw 'exit nil
))
615 (reftex-erase-buffer))
619 (and current-prefix-arg
(setq key ?R
))
620 (reftex-erase-buffer)
621 (reftex-reparse-document buf last-data key
))
623 ;; toggle context mode
624 (reftex-erase-buffer)
625 (setq context
(not context
)))
628 (setq here-I-am here-I-am1
)
629 (setq typekey
(reftex-query-label-type)))
631 ;; toggle table of contents display, or change depth
632 (reftex-erase-buffer)
633 (if current-prefix-arg
634 (setq reftex-toc-max-level
(prefix-numeric-value
636 (setq toc
(not toc
))))
638 ;; toggle display of included file borders
639 (reftex-erase-buffer)
640 (setq files
(not files
)))
642 ;; toggle counter display
643 (reftex-erase-buffer)
644 (setq counter
(not counter
)))
646 ;; toggle display of commented labels
647 (reftex-erase-buffer)
648 (setq commented
(not commented
)))
650 ;; reuse the last referenced label again
651 (setq entries reftex-last-used-reference
)
654 ;; select an external document
655 (setq xr-index
(reftex-select-external-document
657 (setq buf
(or (reftex-get-file-buffer-force
658 (cdr (nth xr-index xr-alist
)))
659 (error "Cannot switch document"))
660 prefix
(or (car (nth xr-index xr-alist
)) ""))
662 (reftex-access-scan-info))
667 (or (assoc key
(symbol-value reftex-docstruct-symbol
))
668 (list key typekey
)))))
670 ((memq key
'(?a ?A return
))
672 (reftex-select-marked
673 (setq entries
(nreverse reftex-select-marked
)))
675 (setq entries
(list (list data
))))
676 (t (setq entries nil
)))
678 (if (equal key ?a
) (push 'concat entries
))
679 (setq reftex-last-used-reference entries
))
682 (t (error "This should not happen (reftex-offer-label-menu)"))))))
684 (while reftex-buffers-with-changed-invisibility
685 (set-buffer (car (car reftex-buffers-with-changed-invisibility
)))
686 (setq buffer-invisibility-spec
687 (cdr (pop reftex-buffers-with-changed-invisibility
)))))
688 (mapc (lambda (buf) (and (buffer-live-p buf
) (bury-buffer buf
)))
690 (reftex-kill-temporary-buffers))
691 ;; Add the prefixes, put together the relevant information in the form
692 ;; (LABEL TYPEKEY SEPARATOR) and return a list of those.
695 (list (concat prefix
(car (car x
)))
701 (defun reftex-reparse-document (&optional buffer data key
)
702 ;; Rescan the document.
703 (save-window-excursion
706 (if (not (bufferp buffer
))
707 (error "No such buffer %s" (buffer-name buffer
))
708 (set-buffer buffer
)))
709 (let ((arg (if (eq key ?R
) '(16) '(4)))
711 (reftex-access-scan-info arg file
)))))
714 (defun reftex-query-label-type ()
715 ;; Ask for label type
716 (let ((key (reftex-select-with-char
717 reftex-type-query-prompt reftex-type-query-help
3)))
718 (unless (member (char-to-string key
) reftex-typekey-list
)
719 (error "No such label type: %s" (char-to-string key
)))
720 (char-to-string key
)))
723 (defun reftex-show-label-location (data forward no-revisit
724 &optional stay error
)
725 ;; View the definition site of a label in another window.
726 ;; DATA is an entry from the docstruct list.
727 ;; FORWARD indicates if the label is likely forward from current point.
728 ;; NO-REVISIT means do not load a file to show this label.
729 ;; STAY means leave the new window selected.
730 ;; ERROR means throw an error exception when the label cannot be found.
731 ;; If ERROR is nil, the return value of this function indicates success.
732 (let* ((this-window (selected-window))
733 (errorf (if error
'error
'message
))
734 label file buffer re found
)
737 (setq label
(nth 0 data
)
741 (funcall errorf
"Unknown label - reparse might help")
744 ;; Goto the file in another window
747 (reftex-get-buffer-visiting file
)
748 (reftex-get-file-buffer-force
749 file
(not reftex-keep-temporary-buffers
))))
751 ;; good - the file is available
752 (switch-to-buffer-other-window buffer
)
753 ;; we have got a problem here. The file does not exist.
754 ;; Let' get out of here..
755 (funcall errorf
"Label %s not found" label
)
758 ;; search for that label
759 (setq re
(format reftex-find-label-regexp-format
(regexp-quote label
)))
762 (re-search-forward re nil t
)
763 (re-search-backward re nil t
)))
765 (goto-char (point-min))
766 (unless (setq found
(re-search-forward re nil t
))
767 ;; Ooops. Must be in a macro with distributed args.
770 (format reftex-find-label-regexp-format2
771 (regexp-quote label
)) nil t
))))
774 (reftex-highlight 0 (match-beginning 3) (match-end 3))
775 (reftex-show-entry (match-beginning 3) (match-end 3))
777 (unless stay
(select-window this-window
)))
778 (select-window this-window
)
779 (funcall errorf
"Label %s not found" label
))
782 (defvar font-lock-mode
)
783 (defun reftex-show-entry (beg-hlt end-hlt
)
784 ;; Show entry if point is hidden
785 (let* ((n (/ (reftex-window-height) 2))
787 (re-search-backward "[\n\r]" nil
1 n
) (point)))
789 (re-search-forward "[\n\r]" nil
1 n
) (point))))
791 ((and (boundp 'buffer-invisibility-spec
) buffer-invisibility-spec
792 (get-char-property (1+ beg-hlt
) 'invisible
))
793 ;; Invisible with text properties. That is easy to change.
794 (push (cons (current-buffer) buffer-invisibility-spec
)
795 reftex-buffers-with-changed-invisibility
)
796 (setq buffer-invisibility-spec nil
))
797 ((string-match "\r" (buffer-substring beg end
))
798 ;; Invisible with selective display. We need to copy it.
799 (let ((string (buffer-substring-no-properties beg end
)))
800 (switch-to-buffer "*RefTeX Context Copy*")
801 (setq buffer-read-only nil
)
804 (subst-char-in-region (point-min) (point-max) ?
\r ?
\n t
)
805 (goto-char (- beg-hlt beg
))
806 (reftex-highlight 0 (1+ (- beg-hlt beg
)) (1+ (- end-hlt beg
)))
807 (if (reftex-refontify)
808 (when (or (not (eq major-mode
'latex-mode
))
809 (not font-lock-mode
))
812 'reftex-pre-refontification-functions
813 reftex-call-back-to-this-buffer
'reftex-hidden
)
815 (when (or (not (eq major-mode
'fundamental-mode
))
818 (run-hooks 'reftex-display-copied-context-hook
)
819 (setq buffer-read-only t
))))))
821 ;; Generate functions for direct insertion of specific referencing
822 ;; macros. The functions are named `reftex-<package>-<macro>',
823 ;; e.g. `reftex-varioref-vref'.
824 (dolist (elt reftex-ref-style-alist
)
825 (when (stringp (nth 1 elt
))
826 (dolist (item (nth 2 elt
))
827 (let ((macro (car item
))
828 (package (nth 1 elt
)))
829 (eval `(defun ,(intern (format "reftex-%s-%s" package
830 (substring macro
1 (length macro
)))) ()
831 ,(format "Insert a reference using the `%s' macro from the %s \
832 package.\n\nThis is a generated function."
835 (let ((reftex-refstyle ,macro
))
836 (reftex-reference))))))))
838 (defun reftex-format-special (label fmt refstyle
)
839 "Apply selected reference style to format FMT and add LABEL.
840 Replace any occurrences of \"\\ref\" with REFSTYLE."
841 ;; Replace instances of \ref in `fmt' with the special reference
842 ;; style selected by the user.
844 ((while (string-match "\\(\\\\ref\\)[ \t]*{" fmt
)
845 (setq fmt
(replace-match refstyle t t fmt
1))))
846 ((string-match "\\(\\\\[[:alpha:]]+\\)[ \t]*{" fmt
)
847 (setq fmt
(replace-match refstyle t t fmt
1))))
851 (defun reftex-goto-label (&optional other-window
)
852 "Prompt for a label (with completion) and jump to the location of this label.
853 Optional prefix argument OTHER-WINDOW goes to the label in another window."
855 (reftex-access-scan-info)
856 (let* ((wcfg (current-window-configuration))
857 (docstruct (symbol-value reftex-docstruct-symbol
))
858 ;; If point is inside a \ref{} or \pageref{}, use that as
860 (default (when (looking-back "\\\\\\(?:page\\)?ref{[-a-zA-Z0-9_*.:]*"
861 (line-beginning-position))
862 (reftex-this-word "-a-zA-Z0-9_*.:")))
863 (label (completing-read (if default
864 (format "Label (default %s): " default
)
867 (lambda (x) (stringp (car x
))) t nil nil
869 (selection (assoc label docstruct
))
871 (reftex-show-label-location selection t nil
'stay
)
874 (set-window-configuration wcfg
)
875 (switch-to-buffer (marker-buffer where
))
877 (reftex-unhighlight 0)))
879 (provide 'reftex-ref
)
881 ;;; reftex-ref.el ends here
884 ;; generated-autoload-file: "reftex.el"