1 ;;; reftex-dcr.el --- viewing cross references and citations with RefTeX
3 ;; Copyright (C) 1997-2013 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
))
29 (declare-function bibtex-beginning-of-entry
"bibtex" ())
33 (defun reftex-view-crossref (&optional arg auto-how fail-quietly
)
34 "View cross reference of macro at point. Point must be on the KEY
35 argument. When at a `\\ref' macro, show corresponding `\\label'
36 definition, also in external documents (`xr'). When on a label, show
37 a locations where KEY is referenced. Subsequent calls find additional
38 locations. When on a `\\cite', show the associated `\\bibitem' macro or
39 the BibTeX database entry. When on a `\\bibitem', show a `\\cite' macro
40 which uses this KEY. When on an `\\index', show other locations marked
41 by the same index entry.
42 To define additional cross referencing items, use the option
43 `reftex-view-crossref-extra'. See also `reftex-view-crossref-from-bibtex'.
44 With one or two C-u prefixes, enforce rescanning of the document.
45 With argument 2, select the window showing the cross reference.
46 AUTO-HOW is only for the automatic crossref display and is handed through
47 to the functions `reftex-view-cr-cite' and `reftex-view-cr-ref'."
51 (let* ((macro (car (reftex-what-macro-safe 1)))
52 (key (reftex-this-word "^{}%\n\r, \t"))
55 (if (or (null macro
) (reftex-in-comment))
57 (error "Not on a crossref macro argument"))
59 (setq reftex-call-back-to-this-buffer
(current-buffer))
62 ((string-match "\\`\\\\cite\\|cite\\*?\\'\\|bibentry" macro
)
63 ;; A citation macro: search for bibitems or BibTeX entries
64 (setq dw
(reftex-view-cr-cite arg key auto-how
)))
65 ((string-match "\\`\\\\ref\\|ref\\(range\\)?\\*?\\'" macro
)
66 ;; A reference macro: search for labels
67 (setq dw
(reftex-view-cr-ref arg key auto-how
)))
68 (auto-how nil
) ;; No further action for automatic display (speed)
69 ((or (equal macro
"\\label")
70 (member macro reftex-macros-with-labels
))
71 ;; A label macro: search for reference macros
72 (reftex-access-scan-info arg
)
73 (setq dw
(reftex-view-regexp-match
74 (format reftex-find-reference-format
(regexp-quote key
))
76 ((equal macro
"\\bibitem")
77 ;; A bibitem macro: search for citations
78 (reftex-access-scan-info arg
)
79 (setq dw
(reftex-view-regexp-match
80 (format reftex-find-citation-regexp-format
(regexp-quote key
))
82 ((member macro reftex-macros-with-index
)
83 (reftex-access-scan-info arg
)
84 (setq dw
(reftex-view-regexp-match
85 (format reftex-find-index-entry-regexp-format
89 (reftex-access-scan-info arg
)
91 (let ((list reftex-view-crossref-extra
)
92 entry mre action group
)
93 (while (setq entry
(pop list
))
97 (when (string-match mre macro
)
98 (setq dw
(reftex-view-regexp-match
99 (format action key
) group nil nil
))
101 (error "Not on a crossref macro argument"))))
102 (if (and (eq arg
2) (windowp dw
)) (select-window dw
)))))
104 (defun reftex-view-cr-cite (arg key how
)
105 ;; View crossreference of a ref cite. HOW can have the values
106 ;; nil: Show in another window.
107 ;; echo: Show one-line info in echo area.
108 ;; tmp-window: Show in small window and arrange for window to disappear.
110 ;; Ensure access to scanning info
111 (reftex-access-scan-info (or arg current-prefix-arg
))
113 (if (eq how
'tmp-window
)
114 ;; Remember the window configuration
115 (put 'reftex-auto-view-crossref
'last-window-conf
116 (current-window-configuration)))
118 (let (files size item
(pos (point)) (win (selected-window)) pop-win
119 (bibtype (reftex-bib-or-thebib)))
120 ;; Find the citation mode and the file list
122 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
125 files
(reftex-get-bibfile-list)))
126 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
127 ((eq bibtype
'thebib
)
129 files
(reftex-uniquify
132 'thebib
(symbol-value reftex-docstruct-symbol
))))))
133 (reftex-default-bibliography
135 files
(reftex-default-bibliography)))
136 (how) ;; don't throw for special display
137 (t (error "Cannot display crossref")))
140 ;; Display in Echo area
141 (reftex-echo-cite key files item
)
142 ;; Display in a window
143 (if (not (eq how
'tmp-window
))
145 (reftex-pop-to-bibtex-entry key files nil t item
)
146 ;; A temporary window
148 (reftex-pop-to-bibtex-entry key files nil t item
)
149 (error (goto-char pos
)
150 (message "cite: no such citation key %s" key
)
153 (setq size
(max 1 (count-lines (point)
154 (reftex-end-of-bib-entry item
))))
155 (let ((window-min-height 2))
156 (shrink-window (1- (- (window-height) size
)))
158 ;; Arrange restoration
159 (add-hook 'pre-command-hook
'reftex-restore-window-conf
))
161 ;; Normal display in other window
162 (add-hook 'pre-command-hook
'reftex-highlight-shall-die
)
163 (setq pop-win
(selected-window))
167 (select-window pop-win
)))))
169 (defun reftex-view-cr-ref (arg label how
)
170 ;; View crossreference of a ref macro. HOW can have the values
171 ;; nil: Show in another window.
172 ;; echo: Show one-line info in echo area.
173 ;; tmp-window: Show in small window and arrange for window to disappear.
175 ;; Ensure access to scanning info
176 (reftex-access-scan-info (or arg current-prefix-arg
))
178 (if (eq how
'tmp-window
)
179 ;; Remember the window configuration
180 (put 'reftex-auto-view-crossref
'last-window-conf
181 (current-window-configuration)))
183 (let* ((xr-data (assoc 'xr
(symbol-value reftex-docstruct-symbol
)))
184 (xr-re (nth 2 xr-data
))
185 (entry (assoc label
(symbol-value reftex-docstruct-symbol
)))
186 (win (selected-window)) pop-win
(pos (point)))
188 (if (and (not entry
) (stringp label
) xr-re
(string-match xr-re label
))
189 ;; Label is defined in external document
193 (or (reftex-get-file-buffer-force
194 (cdr (assoc (match-string 1 label
) (nth 1
196 (error "Problem with external label %s" label
))))
197 (setq label
(substring label
(match-end 1)))
198 (reftex-access-scan-info)
200 (assoc label
(symbol-value reftex-docstruct-symbol
)))))
202 ;; Display in echo area
203 (reftex-echo-ref label entry
(symbol-value reftex-docstruct-symbol
))
204 (let ((window-conf (current-window-configuration)))
206 (reftex-show-label-location entry t nil t t
)
207 (error (set-window-configuration window-conf
)
208 (message "ref: Label %s not found" label
)
209 (error "ref: Label %s not found" label
)))) ;; 2nd is line OK
210 (add-hook 'pre-command-hook
'reftex-highlight-shall-die
)
212 (when (eq how
'tmp-window
)
213 ;; Resize window and arrange restoration
214 (shrink-window (1- (- (window-height) 9)))
216 (add-hook 'pre-command-hook
'reftex-restore-window-conf
))
217 (setq pop-win
(selected-window))
221 (select-window pop-win
)))))
223 (defun reftex-mouse-view-crossref (ev)
224 "View cross reference of \\ref or \\cite macro where you click.
225 If the macro at point is a \\ref, show the corresponding label definition.
226 If it is a \\cite, show the BibTeX database entry.
227 If there is no such macro at point, search forward to find one.
228 With argument, actually select the window showing the cross reference."
230 ;; Make sure the referencing macro stays visible in the original window.
232 (reftex-view-crossref current-prefix-arg
))
234 (defun reftex-view-crossref-when-idle ()
235 ;; Display info about crossref at point in echo area or a window.
236 ;; This function was designed to work with an idle timer.
237 ;; We try to get out of here as quickly as possible if the call is useless.
239 ;; Make sure message area is free if we need it.
240 (or (eq reftex-auto-view-crossref
'window
) (not (current-message)))
241 ;; Make sure we are not already displaying this one
242 (not (memq last-command
'(reftex-view-crossref
243 reftex-mouse-view-crossref
)))
244 ;; Quick precheck if this might be a relevant spot
245 ;; `reftex-view-crossref' will do a more thorough check.
247 (search-backward "\\" nil t
)
248 (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\|bibentry\\)"))
251 (let ((current-prefix-arg nil
))
253 ((eq reftex-auto-view-crossref t
)
254 (reftex-view-crossref -
1 'echo
'quiet
))
255 ((eq reftex-auto-view-crossref
'window
)
256 (reftex-view-crossref -
1 'tmp-window
'quiet
))
260 (defun reftex-restore-window-conf ()
261 (set-window-configuration (get 'reftex-auto-view-crossref
'last-window-conf
))
262 (put 'reftex-auto-view-crossref
'last-window-conf nil
)
263 (remove-hook 'pre-command-hook
'reftex-restore-window-conf
))
265 (defun reftex-echo-ref (label entry docstruct
)
266 ;; Display crossref info in echo area.
270 (substitute-command-keys (format reftex-no-info-message
"ref"))))
272 (message "ref: unknown label: %s" label
))
274 (when (stringp (nth 2 entry
))
275 (message "ref(%s): %s" (nth 1 entry
) (nth 2 entry
)))
276 (let ((buf (get-buffer " *Echo Area*")))
278 (with-current-buffer buf
279 (run-hooks 'reftex-display-copied-context-hook
)))))))
281 (defun reftex-echo-cite (key files item
)
282 ;; Display citation info in echo area.
283 (let* ((cache (assq 'bibview-cache
(symbol-value reftex-docstruct-symbol
)))
284 (cache-entry (assoc key
(cdr cache
)))
285 entry string buf
(all-files files
))
287 (if (and reftex-cache-cite-echo cache-entry
)
288 ;; We can just use the cache
289 (setq string
(cdr cache-entry
))
291 ;; Need to look in the database
292 (unless reftex-revisit-to-echo
293 (setq files
(reftex-visited-files files
)))
298 (reftex-pop-to-bibtex-entry key files nil nil item t
))
300 (if (and files
(= (length all-files
) (length files
)))
301 (message "cite: no such database entry: %s" key
)
302 (message "%s" (substitute-command-keys
303 (format reftex-no-info-message
"cite"))))
307 (setq string
(reftex-nicify-text entry
))
308 (setq string
(reftex-make-cite-echo-string
309 (reftex-parse-bibtex-entry entry
)
310 reftex-docstruct-symbol
)))))
311 (unless (or (null string
) (equal string
""))
312 (message "cite: %s" string
))
313 (when (setq buf
(get-buffer " *Echo Area*"))
314 (with-current-buffer buf
315 (run-hooks 'reftex-display-copied-context-hook
)))))
317 (defvar reftex-use-itimer-in-xemacs nil
318 "Non-nil means use the idle timers in XEmacs for crossref display.
319 Currently, idle timer restart is broken and we use the post-command-hook.")
321 (defun reftex-toggle-auto-view-crossref ()
322 "Toggle the automatic display of crossref information in the echo area.
323 When active, leaving point idle in the argument of a \\ref or \\cite macro
324 will display info in the echo area."
326 (if reftex-auto-view-crossref-timer
328 (if (featurep 'xemacs
)
329 (if reftex-use-itimer-in-xemacs
330 (delete-itimer reftex-auto-view-crossref-timer
)
331 (remove-hook 'post-command-hook
'reftex-start-itimer-once
))
332 (cancel-timer reftex-auto-view-crossref-timer
))
333 (setq reftex-auto-view-crossref-timer nil
)
334 (message "Automatic display of crossref information was turned off"))
335 (setq reftex-auto-view-crossref-timer
336 (if (featurep 'xemacs
)
337 (if reftex-use-itimer-in-xemacs
338 (start-itimer "RefTeX Idle Timer"
339 'reftex-view-crossref-when-idle
340 reftex-idle-time reftex-idle-time t
)
341 (add-hook 'post-command-hook
'reftex-start-itimer-once
)
344 reftex-idle-time t
'reftex-view-crossref-when-idle
)))
345 (unless reftex-auto-view-crossref
346 (setq reftex-auto-view-crossref t
))
347 (message "Automatic display of crossref information was turned on")))
349 (defun reftex-start-itimer-once ()
350 (and (featurep 'xemacs
)
352 (not (itimer-live-p reftex-auto-view-crossref-timer
))
353 (setq reftex-auto-view-crossref-timer
354 (start-itimer "RefTeX Idle Timer"
355 'reftex-view-crossref-when-idle
356 reftex-idle-time nil t
))))
358 (defun reftex-view-crossref-from-bibtex (&optional arg
)
359 "View location in a LaTeX document which cites the BibTeX entry at point.
360 Since BibTeX files can be used by many LaTeX documents, this function
361 prompts upon first use for a buffer in RefTeX mode. To reset this
362 link to a document, call the function with a prefix arg.
363 Calling this function several times find successive citation locations."
366 ;; Break connection to reference buffer
367 (put 'reftex-bibtex-view-cite-locations
:ref-buffer nil
))
368 (let ((ref-buffer (get 'reftex-bibtex-view-cite-locations
:ref-buffer
)))
369 ;; Establish connection to reference buffer
379 (if reftex-mode
(list (buffer-name b
)) nil
))
382 (put 'reftex-bibtex-view-cite-locations
:ref-buffer ref-buffer
))
383 ;; Search for citations
384 (bibtex-beginning-of-entry)
386 "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*\\([^, \t\r\n}]+\\)")
388 (goto-char (match-beginning 1))
389 (reftex-view-regexp-match
390 (format reftex-find-citation-regexp-format
391 (regexp-quote (match-string 1)))
393 (error "Cannot find citation key in BibTeX entry"))))
395 (defun reftex-view-regexp-match (re &optional highlight-group new ref-buffer
)
396 ;; Search for RE in current document or in the document of REF-BUFFER.
397 ;; Continue the search, if the same re was searched last.
398 ;; Highlight the group HIGHLIGHT-GROUP of the match.
399 ;; When NEW is non-nil, start a new search regardless.
400 ;; Match point is displayed in another window.
401 ;; Upon success, returns the window which displays the match.
403 ;;; Decide if new search or continued search
404 (let* ((oldprop (get 'reftex-view-regexp-match
:props
))
405 (newprop (list (current-buffer) re
))
406 (cont (and (not new
) (equal oldprop newprop
)))
407 (cnt (if cont
(get 'reftex-view-regexp-match
:cnt
) 0))
408 (current-window (selected-window))
409 (window-conf (current-window-configuration))
411 (switch-to-buffer-other-window (or ref-buffer
(current-buffer)))
415 (setq match
(reftex-global-search-continue))
416 (reftex-access-scan-info)
417 (setq match
(reftex-global-search re
(reftex-all-document-files))))
419 ;; Evaluate the match.
422 (put 'reftex-view-regexp-match
:props newprop
)
423 (put 'reftex-view-regexp-match
:cnt
(incf cnt
))
424 (reftex-highlight 0 (match-beginning highlight-group
)
425 (match-end highlight-group
))
426 (add-hook 'pre-command-hook
'reftex-highlight-shall-die
)
427 (setq pop-window
(selected-window)))
428 (put 'reftex-view-regexp-match
:props nil
)
429 (or cont
(set-window-configuration window-conf
)))
430 (select-window current-window
)
433 (message "Match Nr. %s" cnt
)
436 (error "No further matches (total number of matches: %d)" cnt
)
437 (error "No matches")))))
439 (defvar reftex-global-search-marker
(make-marker))
440 (defun reftex-global-search (regexp file-list
)
441 ;; Start a search for REGEXP in all files of FILE-LIST
442 (put 'reftex-global-search
:file-list file-list
)
443 (put 'reftex-global-search
:regexp regexp
)
444 (move-marker reftex-global-search-marker nil
)
445 (reftex-global-search-continue))
447 (defun reftex-global-search-continue ()
448 ;; Continue a global search started with `reftex-global-search'
449 (unless (get 'reftex-global-search
:file-list
)
450 (error "No global search to continue"))
451 (let* ((file-list (get 'reftex-global-search
:file-list
))
452 (regexp (get 'reftex-global-search
:regexp
))
453 (buf (or (marker-buffer reftex-global-search-marker
)
454 (reftex-get-file-buffer-force (car file-list
))))
455 (pos (or (marker-position reftex-global-search-marker
) 1))
457 ;; Take up starting position
458 (unless buf
(error "No such buffer %s" buf
))
459 (switch-to-buffer buf
)
462 ;; Search and switch file if necessary
465 (when (re-search-forward regexp nil t
)
466 (move-marker reftex-global-search-marker
(point))
468 ;; No match - goto next file
470 (or file-list
(throw 'exit nil
))
471 (setq file
(car file-list
)
472 buf
(reftex-get-file-buffer-force file
))
473 (unless buf
(error "Cannot access file %s" file
))
474 (put 'reftex-global-search
:file-list file-list
)
475 (switch-to-buffer buf
)
479 (move-marker reftex-global-search-marker nil
)
480 (error "All files processed"))))
482 (provide 'reftex-dcr
)
484 ;;; reftex-dcr.el ends here