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