1 ;;; reftex-vcr.el - Viewing cross references and citations with RefTeX
2 ;; Copyright (c) 1997, 1998, 1999 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
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 2, or (at your option)
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; 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.
25 (eval-when-compile (require 'cl
))
30 (defun reftex-view-crossref (&optional arg auto-how
)
31 "View cross reference of macro at point. Point must be on the KEY
32 argument. When at at `\ref' macro, show corresponding `\label'
33 definition, also in external documents (`xr'). When on a label, show
34 a locations where KEY is referenced. Subsequent calls find additional
35 locations. When on a `\cite', show the associated `\bibitem' macro or
36 the BibTeX database entry. When on a `\bibitem', show a `\cite' macro
37 which uses this KEY. When on an `\index', show other locations marked
38 by the same index entry.
39 To define additional cross referencing items, use the option
40 `reftex-view-crossref-extra'. See also `reftex-view-crossref-from-bibtex'.
41 With one or two C-u prefixes, enforce rescanning of the document.
42 With argument 2, select the window showing the cross reference.
43 AUTO-HOW is only for the automatic crossref display and is handed through
44 to the functions `reftex-view-cr-cite' and `reftex-view-cr-ref'."
48 (let* ((macro (car (reftex-what-macro-safe 1)))
49 (key (reftex-this-word "^{}%\n\r,"))
52 (if (or (null macro
) (reftex-in-comment))
53 (error "Not on a crossref macro argument"))
55 (setq reftex-call-back-to-this-buffer
(current-buffer))
58 ((string-match "\\`\\\\cite\\|cite\\*?\\'" macro
)
59 ;; A citation macro: search for bibitems or BibTeX entries
60 (setq dw
(reftex-view-cr-cite arg key auto-how
)))
61 ((string-match "\\`\\\\ref\\|ref\\(range\\)?\\*?\\'" macro
)
62 ;; A reference macro: search for labels
63 (setq dw
(reftex-view-cr-ref arg key auto-how
)))
64 (auto-how nil
) ;; No further action for automatic display (speed)
65 ((or (equal macro
"\\label")
66 (member macro reftex-macros-with-labels
))
67 ;; A label macro: search for reference macros
68 (reftex-access-scan-info arg
)
69 (setq dw
(reftex-view-regexp-match
70 (format reftex-find-reference-format
(regexp-quote key
))
72 ((equal macro
"\\bibitem")
73 ;; A bibitem macro: search for citations
74 (reftex-access-scan-info arg
)
75 (setq dw
(reftex-view-regexp-match
76 (format reftex-find-citation-regexp-format
(regexp-quote key
))
78 ((member macro reftex-macros-with-index
)
79 (reftex-access-scan-info arg
)
80 (setq dw
(reftex-view-regexp-match
81 (format reftex-find-index-entry-regexp-format
85 (reftex-access-scan-info arg
)
87 (let ((list reftex-view-crossref-extra
)
88 entry mre action group
)
89 (while (setq entry
(pop list
))
93 (when (string-match mre macro
)
94 (setq dw
(reftex-view-regexp-match
95 (format action key
) group nil nil
))
97 (error "Not on a crossref macro argument"))))
98 (if (and (eq arg
2) (windowp dw
)) (select-window dw
))))
100 (defun reftex-view-cr-cite (arg key how
)
101 ;; View crossreference of a ref cite. HOW can have the values
102 ;; nil: Show in another window.
103 ;; echo: Show one-line info in echo area.
104 ;; tmp-window: Show in small window and arrange for window to disappear.
106 ;; Ensure access to scanning info
107 (reftex-access-scan-info (or arg current-prefix-arg
))
109 (if (eq how
'tmp-window
)
110 ;; Remember the window configuration
111 (put 'reftex-auto-view-crossref
'last-window-conf
112 (current-window-configuration)))
114 (let (files size item
(pos (point)) (win (selected-window)) pop-win
)
115 ;; Find the citation mode and the file list
117 ((assq 'bib
(symbol-value reftex-docstruct-symbol
))
119 files
(reftex-get-bibfile-list)))
120 ((assq 'thebib
(symbol-value reftex-docstruct-symbol
))
122 files
(reftex-uniquify
125 'thebib
(symbol-value reftex-docstruct-symbol
))))))
126 (reftex-default-bibliography
128 files
(reftex-default-bibliography)))
129 (how) ;; don't throw for special display
130 (t (error "Cannot display crossref")))
133 ;; Display in Echo area
134 (reftex-echo-cite key files item
)
135 ;; Display in a window
136 (if (not (eq how
'tmp-window
))
138 (reftex-pop-to-bibtex-entry key files nil t item
)
139 ;; A temporary window
141 (reftex-pop-to-bibtex-entry key files nil t item
)
142 (error (goto-char pos
)
143 (message "cite: no such citation key %s" key
)
146 (setq size
(max 1 (count-lines (point)
147 (reftex-end-of-bib-entry item
))))
148 (let ((window-min-height 2))
149 (shrink-window (1- (- (window-height) size
)))
151 ;; Arrange restoration
152 (add-hook 'pre-command-hook
'reftex-restore-window-conf
))
154 ;; Normal display in other window
155 (add-hook 'pre-command-hook
'reftex-highlight-shall-die
)
156 (setq pop-win
(selected-window))
160 (select-window pop-win
)))))
162 (defun reftex-view-cr-ref (arg label how
)
163 ;; View crossreference of a ref macro. HOW can have the values
164 ;; nil: Show in another window.
165 ;; echo: Show one-line info in echo area.
166 ;; tmp-window: Show in small window and arrange for window to disappear.
168 ;; Ensure access to scanning info
169 (reftex-access-scan-info (or arg current-prefix-arg
))
171 (if (eq how
'tmp-window
)
172 ;; Remember the window configuration
173 (put 'reftex-auto-view-crossref
'last-window-conf
174 (current-window-configuration)))
176 (let* ((xr-data (assoc 'xr
(symbol-value reftex-docstruct-symbol
)))
177 (xr-re (nth 2 xr-data
))
178 (entry (assoc label
(symbol-value reftex-docstruct-symbol
)))
179 (win (selected-window)) pop-win
(pos (point)))
181 (if (and (not entry
) (stringp label
) xr-re
(string-match xr-re label
))
182 ;; Label is defined in external document
186 (or (reftex-get-file-buffer-force
187 (cdr (assoc (match-string 1 label
) (nth 1
189 (error "Problem with external label %s" label
))))
190 (setq label
(substring label
(match-end 1)))
191 (reftex-access-scan-info)
193 (assoc label
(symbol-value reftex-docstruct-symbol
)))))
195 ;; Display in echo area
196 (reftex-echo-ref label entry
(symbol-value reftex-docstruct-symbol
))
197 (let ((window-conf (current-window-configuration)))
199 (reftex-show-label-location entry t nil t t
)
200 (error (set-window-configuration window-conf
)
201 (message "ref: Label %s not found" label
)
202 (error "ref: Label %s not found" label
)))) ;; 2nd is line OK
203 (add-hook 'pre-command-hook
'reftex-highlight-shall-die
)
205 (when (eq how
'tmp-window
)
206 ;; Resize window and arrange restauration
207 (shrink-window (1- (- (window-height) 9)))
209 (add-hook 'pre-command-hook
'reftex-restore-window-conf
))
210 (setq pop-win
(selected-window))
214 (select-window pop-win
)))))
216 (defun reftex-mouse-view-crossref (ev)
217 "View cross reference of \\ref or \\cite macro where you click.
218 If the macro at point is a \\ref, show the corresponding label definition.
219 If it is a \\cite, show the BibTeX database entry.
220 If there is no such macro at point, search forward to find one.
221 With argument, actually select the window showing the cross reference."
224 (reftex-view-crossref current-prefix-arg
))
226 (defun reftex-view-crossref-when-idle ()
227 ;; Display info about crossref at point in echo area or a window.
228 ;; This function was desigend to work with an idle timer.
229 ;; We try to get out of here as quickly as possible if the call is useless.
231 ;; Make sure message area is free if we need it.
232 (or (eq reftex-auto-view-crossref
'window
) (not (current-message)))
233 ;; Make sure we are not already displaying this one
234 (not (memq last-command
'(reftex-view-crossref
235 reftex-mouse-view-crossref
)))
236 ;; Quick precheck if this might be a relevant spot
237 ;; FIXME: Can fail with backslash in comment
239 (search-backward "\\" nil t
)
240 (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\)"))
243 (let ((current-prefix-arg nil
))
245 ((eq reftex-auto-view-crossref t
)
246 (reftex-view-crossref -
1 'echo
))
247 ((eq reftex-auto-view-crossref
'window
)
248 (reftex-view-crossref -
1 'tmp-window
))
252 (defun reftex-restore-window-conf ()
253 (set-window-configuration (get 'reftex-auto-view-crossref
'last-window-conf
))
254 (put 'reftex-auto-view-crossref
'last-window-conf nil
)
255 (remove-hook 'pre-command-hook
'reftex-restore-window-conf
))
257 (defun reftex-echo-ref (label entry docstruct
)
258 ;; Display crossref info in echo area.
261 (message (substitute-command-keys (format reftex-no-info-message
"ref"))))
263 (message "ref: unknown label: %s" label
))
265 (when (stringp (nth 2 entry
))
266 (message "ref(%s): %s" (nth 1 entry
) (nth 2 entry
)))
267 (let ((buf (get-buffer " *Echo Area*")))
271 (run-hooks 'reftex-display-copied-context-hook
)))))))
273 (defun reftex-echo-cite (key files item
)
274 ;; Display citation info in echo area.
275 (let* ((cache (assq 'bibview-cache
(symbol-value reftex-docstruct-symbol
)))
276 (cache-entry (assoc key
(cdr cache
)))
277 entry string buf
(all-files files
))
279 (if (and reftex-cache-cite-echo cache-entry
)
280 ;; We can just use the cache
281 (setq string
(cdr cache-entry
))
283 ;; Need to look in the database
284 (unless reftex-revisit-to-echo
285 (setq files
(reftex-visited-files files
)))
290 (reftex-pop-to-bibtex-entry key files nil nil item t
))
292 (if (and files
(= (length all-files
) (length files
)))
293 (message "cite: no such database entry: %s" key
)
294 (message (substitute-command-keys
295 (format reftex-no-info-message
"cite"))))
299 (setq string
(reftex-nicify-text entry
))
300 (setq string
(reftex-make-cite-echo-string
301 (reftex-parse-bibtex-entry entry
)
302 reftex-docstruct-symbol
)))))
303 (unless (or (null string
) (equal string
""))
304 (message "cite: %s" string
))
305 (when (setq buf
(get-buffer " *Echo Area*"))
308 (run-hooks 'reftex-display-copied-context-hook
)))))
310 (defvar reftex-use-itimer-in-xemacs nil
311 "*Non-nil means use the idle timers in XEmacs for crossref display.
312 Currently, idle timer restart is broken and we use the post-command-hook.")
314 (defun reftex-toggle-auto-view-crossref ()
315 "Toggle the automatic display of crossref information in the echo area.
316 When active, leaving point idle in the argument of a \\ref or \\cite macro
317 will display info in the echo area."
319 (if reftex-auto-view-crossref-timer
321 (if (featurep 'xemacs
)
322 (if reftex-use-itimer-in-xemacs
323 (delete-itimer reftex-auto-view-crossref-timer
)
324 (remove-hook 'post-command-hook
'reftex-start-itimer-once
))
325 (cancel-timer reftex-auto-view-crossref-timer
))
326 (setq reftex-auto-view-crossref-timer nil
)
327 (message "Automatic display of crossref information was turned off"))
328 (setq reftex-auto-view-crossref-timer
329 (if (featurep 'xemacs
)
330 (if reftex-use-itimer-in-xemacs
331 (start-itimer "RefTeX Idle Timer"
332 'reftex-view-crossref-when-idle
333 reftex-idle-time reftex-idle-time t
)
334 (add-hook 'post-command-hook
'reftex-start-itimer-once
)
337 reftex-idle-time t
'reftex-view-crossref-when-idle
)))
338 (unless reftex-auto-view-crossref
339 (setq reftex-auto-view-crossref t
))
340 (message "Automatic display of crossref information was turned on")))
342 (defun reftex-start-itimer-once ()
344 (not (itimer-live-p reftex-auto-view-crossref-timer
))
345 (setq reftex-auto-view-crossref-timer
346 (start-itimer "RefTeX Idle Timer"
347 'reftex-view-crossref-when-idle
348 reftex-idle-time nil t
))))
350 (defun reftex-view-crossref-from-bibtex (&optional arg
)
351 "View location in a LaTeX document which cites the BibTeX entry at point.
352 Since BibTeX files can be used by many LaTeX documents, this function
353 prompts upon first use for a buffer in RefTeX mode. To reset this
354 link to a document, call the function with with a prefix arg.
355 Calling this function several times find successive citation locations."
358 ;; Break connection to reference buffer
359 (remprop 'reftex-bibtex-view-cite-locations
:ref-buffer
))
360 (let ((ref-buffer (get 'reftex-bibtex-view-cite-locations
:ref-buffer
)))
361 ;; Establish connection to reference buffer
371 (if reftex-mode
(list (buffer-name b
)) nil
))
374 (put 'reftex-bibtex-view-cite-locations
:ref-buffer ref-buffer
))
375 ;; Search for citations
376 (bibtex-beginning-of-entry)
378 "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*\\([^, \t\r\n}]+\\)")
380 (goto-char (match-beginning 1))
381 (reftex-view-regexp-match
382 (format reftex-find-citation-regexp-format
383 (regexp-quote (match-string 1)))
385 (error "Cannot find citation key in BibTeX entry"))))
387 (defun reftex-view-regexp-match (re &optional highlight-group new ref-buffer
)
388 ;; Search for RE in current document or in the document of REF-BUFFER.
389 ;; Continue the search, if the same re was searched last.
390 ;; Highlight the group HIGHLIGHT-GROUP of the match.
391 ;; When NEW is non-nil, start a new search regardless.
392 ;; Match point is displayed in another window.
393 ;; Upon success, returns the window which displays the match.
395 ;;; Decide if new search or continued search
396 (let* ((oldprop (get 'reftex-view-regexp-match
:props
))
397 (newprop (list (current-buffer) re
))
398 (cont (and (not new
) (equal oldprop newprop
)))
399 (cnt (if cont
(get 'reftex-view-regexp-match
:cnt
) 0))
400 (current-window (selected-window))
401 (window-conf (current-window-configuration))
403 (switch-to-buffer-other-window (or ref-buffer
(current-buffer)))
407 (setq match
(reftex-global-search-continue))
408 (reftex-access-scan-info)
409 (setq match
(reftex-global-search re
(reftex-all-document-files))))
411 ;; Evaluate the match.
414 (put 'reftex-view-regexp-match
:props newprop
)
415 (put 'reftex-view-regexp-match
:cnt
(incf cnt
))
416 (reftex-highlight 0 (match-beginning highlight-group
)
417 (match-end highlight-group
))
418 (add-hook 'pre-command-hook
'reftex-highlight-shall-die
)
419 (setq pop-window
(selected-window)))
420 (remprop 'reftex-view-regexp-match
:props
)
421 (or cont
(set-window-configuration window-conf
)))
422 (select-window current-window
)
425 (message "Match Nr. %s" cnt
)
428 (error "No further matches (total number of matches: %d)" cnt
)
429 (error "No matches")))))
431 (defvar reftex-global-search-marker
(make-marker))
432 (defun reftex-global-search (regexp file-list
)
433 ;; Start a search for REGEXP in all files of FILE-LIST
434 (put 'reftex-global-search
:file-list file-list
)
435 (put 'reftex-global-search
:regexp regexp
)
436 (move-marker reftex-global-search-marker nil
)
437 (reftex-global-search-continue))
439 (defun reftex-global-search-continue ()
440 ;; Continue a global search started with `reftex-global-search'
441 (unless (get 'reftex-global-search
:file-list
)
442 (error "No global search to continue"))
443 (let* ((file-list (get 'reftex-global-search
:file-list
))
444 (regexp (get 'reftex-global-search
:regexp
))
445 (buf (or (marker-buffer reftex-global-search-marker
)
446 (reftex-get-file-buffer-force (car file-list
))))
447 (pos (or (marker-position reftex-global-search-marker
) 1))
449 ;; Take up starting position
450 (unless buf
(error "No such buffer %s" buf
))
451 (switch-to-buffer buf
)
454 ;; Search and switch file if necessary
457 (when (re-search-forward regexp nil t
)
458 (move-marker reftex-global-search-marker
(point))
460 ;; No match - goto next file
462 (or file-list
(throw 'exit nil
))
463 (setq file
(car file-list
)
464 buf
(reftex-get-file-buffer-force file
))
465 (unless buf
(error "Cannot access file %s" file
))
466 (put 'reftex-global-search
:file-list file-list
)
467 (switch-to-buffer buf
)
471 (move-marker reftex-global-search-marker nil
)
472 (error "All files processed"))))
474 ;;; reftex-vcr.el ends here