Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / textmodes / reftex-dcr.el
CommitLineData
3afbc435 1;;; reftex-dcr.el --- viewing cross references and citations with RefTeX
3a6584a1 2
acaf905b 3;; Copyright (C) 1997-2012 Free Software Foundation, Inc.
3ba2590f 4
6fbeb429 5;; Author: Carsten Dominik <dominik@science.uva.nl>
ce545621 6;; Maintainer: auctex-devel@gnu.org
5d2a58e0 7;; Version: 4.31
bd78fa1d 8;; Package: reftex
3ba2590f
RS
9
10;; This file is part of GNU Emacs.
11
1fecc8fe 12;; GNU Emacs is free software: you can redistribute it and/or modify
3ba2590f 13;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
3ba2590f
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
1fecc8fe 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1a9461d0 24
3afbc435
PJ
25;;; Commentary:
26
27;;; Code:
28
7c4d13cc 29(eval-when-compile (require 'cl))
df543367 30(provide 'reftex-dcr)
9f286482 31(provide 'reftex-vcr)
1a9461d0
CD
32(require 'reftex)
33;;;
34
5d2a58e0 35(defun reftex-view-crossref (&optional arg auto-how fail-quietly)
1a9461d0 36 "View cross reference of macro at point. Point must be on the KEY
9b053e76 37argument. When at a `\\ref' macro, show corresponding `\\label'
1a9461d0
CD
38definition, also in external documents (`xr'). When on a label, show
39a locations where KEY is referenced. Subsequent calls find additional
5f52a7df
AS
40locations. When on a `\\cite', show the associated `\\bibitem' macro or
41the BibTeX database entry. When on a `\\bibitem', show a `\\cite' macro
42which uses this KEY. When on an `\\index', show other locations marked
1a9461d0
CD
43by the same index entry.
44To define additional cross referencing items, use the option
45`reftex-view-crossref-extra'. See also `reftex-view-crossref-from-bibtex'.
46With one or two C-u prefixes, enforce rescanning of the document.
47With argument 2, select the window showing the cross reference.
48AUTO-HOW is only for the automatic crossref display and is handed through
49to the functions `reftex-view-cr-cite' and `reftex-view-cr-ref'."
50
51 (interactive "P")
52 ;; See where we are.
53 (let* ((macro (car (reftex-what-macro-safe 1)))
4e33ebe0 54 (key (reftex-this-word "^{}%\n\r, \t"))
3666daf6 55 dw)
1a9461d0
CD
56
57 (if (or (null macro) (reftex-in-comment))
5d2a58e0
CD
58 (or fail-quietly
59 (error "Not on a crossref macro argument"))
60
61 (setq reftex-call-back-to-this-buffer (current-buffer))
4c36be58 62
5d2a58e0
CD
63 (cond
64 ((string-match "\\`\\\\cite\\|cite\\*?\\'\\|bibentry" macro)
65 ;; A citation macro: search for bibitems or BibTeX entries
66 (setq dw (reftex-view-cr-cite arg key auto-how)))
67 ((string-match "\\`\\\\ref\\|ref\\(range\\)?\\*?\\'" macro)
68 ;; A reference macro: search for labels
69 (setq dw (reftex-view-cr-ref arg key auto-how)))
70 (auto-how nil) ;; No further action for automatic display (speed)
71 ((or (equal macro "\\label")
72 (member macro reftex-macros-with-labels))
73 ;; A label macro: search for reference macros
74 (reftex-access-scan-info arg)
75 (setq dw (reftex-view-regexp-match
76 (format reftex-find-reference-format (regexp-quote key))
77 4 nil nil)))
78 ((equal macro "\\bibitem")
79 ;; A bibitem macro: search for citations
80 (reftex-access-scan-info arg)
81 (setq dw (reftex-view-regexp-match
82 (format reftex-find-citation-regexp-format (regexp-quote key))
83 4 nil nil)))
84 ((member macro reftex-macros-with-index)
85 (reftex-access-scan-info arg)
86 (setq dw (reftex-view-regexp-match
87 (format reftex-find-index-entry-regexp-format
88 (regexp-quote key))
89 3 nil nil)))
4c36be58 90 (t
5d2a58e0
CD
91 (reftex-access-scan-info arg)
92 (catch 'exit
93 (let ((list reftex-view-crossref-extra)
94 entry mre action group)
95 (while (setq entry (pop list))
96 (setq mre (car entry)
97 action (nth 1 entry)
98 group (nth 2 entry))
99 (when (string-match mre macro)
4c36be58 100 (setq dw (reftex-view-regexp-match
5d2a58e0
CD
101 (format action key) group nil nil))
102 (throw 'exit t))))
103 (error "Not on a crossref macro argument"))))
104 (if (and (eq arg 2) (windowp dw)) (select-window dw)))))
4c36be58 105
1a9461d0 106(defun reftex-view-cr-cite (arg key how)
4c36be58 107 ;; View crossreference of a ref cite. HOW can have the values
1a9461d0
CD
108 ;; nil: Show in another window.
109 ;; echo: Show one-line info in echo area.
110 ;; tmp-window: Show in small window and arrange for window to disappear.
111
112 ;; Ensure access to scanning info
113 (reftex-access-scan-info (or arg current-prefix-arg))
114
115 (if (eq how 'tmp-window)
116 ;; Remember the window configuration
4c36be58 117 (put 'reftex-auto-view-crossref 'last-window-conf
3666daf6 118 (current-window-configuration)))
1a9461d0 119
20cd3579 120 (let (files size item (pos (point)) (win (selected-window)) pop-win
3666daf6 121 (bibtype (reftex-bib-or-thebib)))
1a9461d0
CD
122 ;; Find the citation mode and the file list
123 (cond
20cd3579
CD
124; ((assq 'bib (symbol-value reftex-docstruct-symbol))
125 ((eq bibtype 'bib)
1a9461d0 126 (setq item nil
3666daf6 127 files (reftex-get-bibfile-list)))
20cd3579
CD
128; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
129 ((eq bibtype 'thebib)
1a9461d0 130 (setq item t
3666daf6
CD
131 files (reftex-uniquify
132 (mapcar 'cdr
4c36be58 133 (reftex-all-assq
3666daf6 134 'thebib (symbol-value reftex-docstruct-symbol))))))
1a9461d0
CD
135 (reftex-default-bibliography
136 (setq item nil
3666daf6 137 files (reftex-default-bibliography)))
1a9461d0
CD
138 (how) ;; don't throw for special display
139 (t (error "Cannot display crossref")))
140
141 (if (eq how 'echo)
3666daf6
CD
142 ;; Display in Echo area
143 (reftex-echo-cite key files item)
1a9461d0
CD
144 ;; Display in a window
145 (if (not (eq how 'tmp-window))
3666daf6
CD
146 ;; Normal display
147 (reftex-pop-to-bibtex-entry key files nil t item)
148 ;; A temporary window
149 (condition-case nil
150 (reftex-pop-to-bibtex-entry key files nil t item)
151 (error (goto-char pos)
152 (message "cite: no such citation key %s" key)
153 (error "")))
154 ;; Resize the window
155 (setq size (max 1 (count-lines (point)
156 (reftex-end-of-bib-entry item))))
157 (let ((window-min-height 2))
158 (shrink-window (1- (- (window-height) size)))
159 (recenter 0))
160 ;; Arrange restoration
161 (add-hook 'pre-command-hook 'reftex-restore-window-conf))
162
163 ;; Normal display in other window
1a9461d0
CD
164 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
165 (setq pop-win (selected-window))
166 (select-window win)
167 (goto-char pos)
168 (when (equal arg 2)
3666daf6 169 (select-window pop-win)))))
1a9461d0
CD
170
171(defun reftex-view-cr-ref (arg label how)
4c36be58 172 ;; View crossreference of a ref macro. HOW can have the values
1a9461d0
CD
173 ;; nil: Show in another window.
174 ;; echo: Show one-line info in echo area.
175 ;; tmp-window: Show in small window and arrange for window to disappear.
176
177 ;; Ensure access to scanning info
178 (reftex-access-scan-info (or arg current-prefix-arg))
4c36be58 179
1a9461d0
CD
180 (if (eq how 'tmp-window)
181 ;; Remember the window configuration
4c36be58 182 (put 'reftex-auto-view-crossref 'last-window-conf
3666daf6 183 (current-window-configuration)))
1a9461d0
CD
184
185 (let* ((xr-data (assoc 'xr (symbol-value reftex-docstruct-symbol)))
3666daf6
CD
186 (xr-re (nth 2 xr-data))
187 (entry (assoc label (symbol-value reftex-docstruct-symbol)))
188 (win (selected-window)) pop-win (pos (point)))
1a9461d0
CD
189
190 (if (and (not entry) (stringp label) xr-re (string-match xr-re label))
3666daf6
CD
191 ;; Label is defined in external document
192 (save-excursion
193 (save-match-data
4c36be58 194 (set-buffer
3666daf6
CD
195 (or (reftex-get-file-buffer-force
196 (cdr (assoc (match-string 1 label) (nth 1
197 xr-data))))
198 (error "Problem with external label %s" label))))
199 (setq label (substring label (match-end 1)))
200 (reftex-access-scan-info)
4c36be58 201 (setq entry
3666daf6 202 (assoc label (symbol-value reftex-docstruct-symbol)))))
1a9461d0 203 (if (eq how 'echo)
3666daf6
CD
204 ;; Display in echo area
205 (reftex-echo-ref label entry (symbol-value reftex-docstruct-symbol))
1a9461d0 206 (let ((window-conf (current-window-configuration)))
3666daf6
CD
207 (condition-case nil
208 (reftex-show-label-location entry t nil t t)
209 (error (set-window-configuration window-conf)
210 (message "ref: Label %s not found" label)
211 (error "ref: Label %s not found" label)))) ;; 2nd is line OK
1a9461d0
CD
212 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
213
214 (when (eq how 'tmp-window)
c80e3b4a 215 ;; Resize window and arrange restoration
3666daf6
CD
216 (shrink-window (1- (- (window-height) 9)))
217 (recenter '(4))
218 (add-hook 'pre-command-hook 'reftex-restore-window-conf))
1a9461d0
CD
219 (setq pop-win (selected-window))
220 (select-window win)
221 (goto-char pos)
222 (when (equal arg 2)
3666daf6 223 (select-window pop-win)))))
1a9461d0
CD
224
225(defun reftex-mouse-view-crossref (ev)
226 "View cross reference of \\ref or \\cite macro where you click.
227If the macro at point is a \\ref, show the corresponding label definition.
228If it is a \\cite, show the BibTeX database entry.
229If there is no such macro at point, search forward to find one.
230With argument, actually select the window showing the cross reference."
231 (interactive "e")
232 (mouse-set-point ev)
233 (reftex-view-crossref current-prefix-arg))
234
235(defun reftex-view-crossref-when-idle ()
236 ;; Display info about crossref at point in echo area or a window.
4c36be58 237 ;; This function was designed to work with an idle timer.
1a9461d0
CD
238 ;; We try to get out of here as quickly as possible if the call is useless.
239 (and reftex-mode
240 ;; Make sure message area is free if we need it.
241 (or (eq reftex-auto-view-crossref 'window) (not (current-message)))
242 ;; Make sure we are not already displaying this one
243 (not (memq last-command '(reftex-view-crossref
3666daf6 244 reftex-mouse-view-crossref)))
1a9461d0 245 ;; Quick precheck if this might be a relevant spot
5d2a58e0 246 ;; `reftex-view-crossref' will do a more thorough check.
4c36be58 247 (save-excursion
3666daf6
CD
248 (search-backward "\\" nil t)
249 (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\|bibentry\\)"))
1a9461d0
CD
250
251 (condition-case nil
3666daf6
CD
252 (let ((current-prefix-arg nil))
253 (cond
254 ((eq reftex-auto-view-crossref t)
5d2a58e0 255 (reftex-view-crossref -1 'echo 'quiet))
3666daf6 256 ((eq reftex-auto-view-crossref 'window)
5d2a58e0 257 (reftex-view-crossref -1 'tmp-window 'quiet))
3666daf6
CD
258 (t nil)))
259 (error nil))))
1a9461d0
CD
260
261(defun reftex-restore-window-conf ()
262 (set-window-configuration (get 'reftex-auto-view-crossref 'last-window-conf))
263 (put 'reftex-auto-view-crossref 'last-window-conf nil)
264 (remove-hook 'pre-command-hook 'reftex-restore-window-conf))
4c36be58 265
1a9461d0
CD
266(defun reftex-echo-ref (label entry docstruct)
267 ;; Display crossref info in echo area.
268 (cond
269 ((null docstruct)
4c36be58 270 (message "%s"
5d2a58e0 271 (substitute-command-keys (format reftex-no-info-message "ref"))))
1a9461d0
CD
272 ((null entry)
273 (message "ref: unknown label: %s" label))
274 (t
275 (when (stringp (nth 2 entry))
276 (message "ref(%s): %s" (nth 1 entry) (nth 2 entry)))
277 (let ((buf (get-buffer " *Echo Area*")))
278 (when buf
9a529312 279 (with-current-buffer buf
3666daf6 280 (run-hooks 'reftex-display-copied-context-hook)))))))
1a9461d0
CD
281
282(defun reftex-echo-cite (key files item)
283 ;; Display citation info in echo area.
284 (let* ((cache (assq 'bibview-cache (symbol-value reftex-docstruct-symbol)))
3666daf6
CD
285 (cache-entry (assoc key (cdr cache)))
286 entry string buf (all-files files))
1a9461d0
CD
287
288 (if (and reftex-cache-cite-echo cache-entry)
3666daf6
CD
289 ;; We can just use the cache
290 (setq string (cdr cache-entry))
1a9461d0
CD
291
292 ;; Need to look in the database
293 (unless reftex-revisit-to-echo
3666daf6
CD
294 (setq files (reftex-visited-files files)))
295
4c36be58 296 (setq entry
3666daf6
CD
297 (condition-case nil
298 (save-excursion
299 (reftex-pop-to-bibtex-entry key files nil nil item t))
300 (error
301 (if (and files (= (length all-files) (length files)))
302 (message "cite: no such database entry: %s" key)
4c36be58 303 (message "%s" (substitute-command-keys
5d2a58e0 304 (format reftex-no-info-message "cite"))))
3666daf6 305 nil)))
1a9461d0 306 (when entry
3666daf6
CD
307 (if item
308 (setq string (reftex-nicify-text entry))
309 (setq string (reftex-make-cite-echo-string
310 (reftex-parse-bibtex-entry entry)
311 reftex-docstruct-symbol)))))
1a9461d0
CD
312 (unless (or (null string) (equal string ""))
313 (message "cite: %s" string))
314 (when (setq buf (get-buffer " *Echo Area*"))
9a529312 315 (with-current-buffer buf
3666daf6 316 (run-hooks 'reftex-display-copied-context-hook)))))
1a9461d0
CD
317
318(defvar reftex-use-itimer-in-xemacs nil
319 "*Non-nil means use the idle timers in XEmacs for crossref display.
320Currently, idle timer restart is broken and we use the post-command-hook.")
321
322(defun reftex-toggle-auto-view-crossref ()
323 "Toggle the automatic display of crossref information in the echo area.
324When active, leaving point idle in the argument of a \\ref or \\cite macro
325will display info in the echo area."
326 (interactive)
327 (if reftex-auto-view-crossref-timer
328 (progn
3666daf6
CD
329 (if (featurep 'xemacs)
330 (if reftex-use-itimer-in-xemacs
331 (delete-itimer reftex-auto-view-crossref-timer)
332 (remove-hook 'post-command-hook 'reftex-start-itimer-once))
333 (cancel-timer reftex-auto-view-crossref-timer))
334 (setq reftex-auto-view-crossref-timer nil)
335 (message "Automatic display of crossref information was turned off"))
1a9461d0 336 (setq reftex-auto-view-crossref-timer
3666daf6
CD
337 (if (featurep 'xemacs)
338 (if reftex-use-itimer-in-xemacs
339 (start-itimer "RefTeX Idle Timer"
4c36be58 340 'reftex-view-crossref-when-idle
3666daf6
CD
341 reftex-idle-time reftex-idle-time t)
342 (add-hook 'post-command-hook 'reftex-start-itimer-once)
343 t)
344 (run-with-idle-timer
345 reftex-idle-time t 'reftex-view-crossref-when-idle)))
1a9461d0
CD
346 (unless reftex-auto-view-crossref
347 (setq reftex-auto-view-crossref t))
348 (message "Automatic display of crossref information was turned on")))
349
350(defun reftex-start-itimer-once ()
9efa445f 351 (and (featurep 'xemacs) reftex-mode
3666daf6
CD
352 (not (itimer-live-p reftex-auto-view-crossref-timer))
353 (setq reftex-auto-view-crossref-timer
354 (start-itimer "RefTeX Idle Timer"
4c36be58 355 'reftex-view-crossref-when-idle
3666daf6 356 reftex-idle-time nil t))))
1a9461d0 357
153ef845
DN
358(declare-function bibtex-beginning-of-entry "bibtex" ())
359
1a9461d0
CD
360(defun reftex-view-crossref-from-bibtex (&optional arg)
361 "View location in a LaTeX document which cites the BibTeX entry at point.
362Since BibTeX files can be used by many LaTeX documents, this function
363prompts upon first use for a buffer in RefTeX mode. To reset this
cd1181db 364link to a document, call the function with a prefix arg.
1a9461d0
CD
365Calling this function several times find successive citation locations."
366 (interactive "P")
4c36be58 367 (when arg
1a9461d0 368 ;; Break connection to reference buffer
d8fb2015 369 (put 'reftex-bibtex-view-cite-locations :ref-buffer nil))
1a9461d0
CD
370 (let ((ref-buffer (get 'reftex-bibtex-view-cite-locations :ref-buffer)))
371 ;; Establish connection to reference buffer
372 (unless ref-buffer
373 (setq ref-buffer
9a529312 374 (save-current-buffer
4c36be58 375 (completing-read
3666daf6
CD
376 "Reference buffer: "
377 (delq nil
4c36be58 378 (mapcar
3666daf6
CD
379 (lambda (b)
380 (set-buffer b)
381 (if reftex-mode (list (buffer-name b)) nil))
382 (buffer-list)))
383 nil t)))
1a9461d0
CD
384 (put 'reftex-bibtex-view-cite-locations :ref-buffer ref-buffer))
385 ;; Search for citations
386 (bibtex-beginning-of-entry)
387 (if (looking-at
3666daf6
CD
388 "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*\\([^, \t\r\n}]+\\)")
389 (progn
390 (goto-char (match-beginning 1))
391 (reftex-view-regexp-match
392 (format reftex-find-citation-regexp-format
393 (regexp-quote (match-string 1)))
394 4 arg ref-buffer))
1a9461d0
CD
395 (error "Cannot find citation key in BibTeX entry"))))
396
397(defun reftex-view-regexp-match (re &optional highlight-group new ref-buffer)
398 ;; Search for RE in current document or in the document of REF-BUFFER.
399 ;; Continue the search, if the same re was searched last.
400 ;; Highlight the group HIGHLIGHT-GROUP of the match.
401 ;; When NEW is non-nil, start a new search regardless.
402 ;; Match point is displayed in another window.
403 ;; Upon success, returns the window which displays the match.
404
405 ;;; Decide if new search or continued search
406 (let* ((oldprop (get 'reftex-view-regexp-match :props))
3666daf6
CD
407 (newprop (list (current-buffer) re))
408 (cont (and (not new) (equal oldprop newprop)))
409 (cnt (if cont (get 'reftex-view-regexp-match :cnt) 0))
410 (current-window (selected-window))
411 (window-conf (current-window-configuration))
412 match pop-window)
1a9461d0
CD
413 (switch-to-buffer-other-window (or ref-buffer (current-buffer)))
414 ;; Search
415 (condition-case nil
3666daf6
CD
416 (if cont
417 (setq match (reftex-global-search-continue))
418 (reftex-access-scan-info)
419 (setq match (reftex-global-search re (reftex-all-document-files))))
1a9461d0
CD
420 (error nil))
421 ;; Evaluate the match.
422 (if match
3666daf6
CD
423 (progn
424 (put 'reftex-view-regexp-match :props newprop)
425 (put 'reftex-view-regexp-match :cnt (incf cnt))
426 (reftex-highlight 0 (match-beginning highlight-group)
427 (match-end highlight-group))
428 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
429 (setq pop-window (selected-window)))
d8fb2015 430 (put 'reftex-view-regexp-match :props nil)
1a9461d0
CD
431 (or cont (set-window-configuration window-conf)))
432 (select-window current-window)
433 (if match
3666daf6
CD
434 (progn
435 (message "Match Nr. %s" cnt)
436 pop-window)
1a9461d0 437 (if cont
3666daf6
CD
438 (error "No further matches (total number of matches: %d)" cnt)
439 (error "No matches")))))
1a9461d0
CD
440
441(defvar reftex-global-search-marker (make-marker))
442(defun reftex-global-search (regexp file-list)
443 ;; Start a search for REGEXP in all files of FILE-LIST
444 (put 'reftex-global-search :file-list file-list)
445 (put 'reftex-global-search :regexp regexp)
446 (move-marker reftex-global-search-marker nil)
447 (reftex-global-search-continue))
448
449(defun reftex-global-search-continue ()
450 ;; Continue a global search started with `reftex-global-search'
451 (unless (get 'reftex-global-search :file-list)
452 (error "No global search to continue"))
453 (let* ((file-list (get 'reftex-global-search :file-list))
3666daf6
CD
454 (regexp (get 'reftex-global-search :regexp))
455 (buf (or (marker-buffer reftex-global-search-marker)
456 (reftex-get-file-buffer-force (car file-list))))
457 (pos (or (marker-position reftex-global-search-marker) 1))
458 file)
1a9461d0
CD
459 ;; Take up starting position
460 (unless buf (error "No such buffer %s" buf))
461 (switch-to-buffer buf)
462 (widen)
463 (goto-char pos)
464 ;; Search and switch file if necessary
465 (if (catch 'exit
3666daf6
CD
466 (while t
467 (when (re-search-forward regexp nil t)
468 (move-marker reftex-global-search-marker (point))
469 (throw 'exit t))
470 ;; No match - goto next file
471 (pop file-list)
472 (or file-list (throw 'exit nil))
473 (setq file (car file-list)
474 buf (reftex-get-file-buffer-force file))
475 (unless buf (error "Cannot access file %s" file))
476 (put 'reftex-global-search :file-list file-list)
477 (switch-to-buffer buf)
478 (widen)
479 (goto-char 1)))
480 t
1a9461d0
CD
481 (move-marker reftex-global-search-marker nil)
482 (error "All files processed"))))
483
df543367 484;;; reftex-dcr.el ends here