(C_SWITCH_SYSTEM): Add -I /usr/local/include and
[bpt/emacs.git] / lisp / textmodes / reftex-vcr.el
CommitLineData
1a9461d0 1;;; reftex-vcr.el - Viewing cross references and citations with RefTeX
3ba2590f
RS
2;; Copyright (c) 1997, 1998, 1999 Free Software Foundation, Inc.
3
4;; Author: Carsten Dominik <dominik@strw.LeidenUniv.nl>
5;; Version: 4.9
6;; Keywords: tex
7
8;; This file is part of GNU Emacs.
9
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)
13;; any later version.
14
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.
19
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.
1a9461d0 24
7c4d13cc 25(eval-when-compile (require 'cl))
1a9461d0
CD
26(provide 'reftex-vcr)
27(require 'reftex)
28;;;
29
30(defun reftex-view-crossref (&optional arg auto-how)
31 "View cross reference of macro at point. Point must be on the KEY
32argument. When at at `\ref' macro, show corresponding `\label'
33definition, also in external documents (`xr'). When on a label, show
34a locations where KEY is referenced. Subsequent calls find additional
35locations. When on a `\cite', show the associated `\bibitem' macro or
36the BibTeX database entry. When on a `\bibitem', show a `\cite' macro
37which uses this KEY. When on an `\index', show other locations marked
38by the same index entry.
39To define additional cross referencing items, use the option
40`reftex-view-crossref-extra'. See also `reftex-view-crossref-from-bibtex'.
41With one or two C-u prefixes, enforce rescanning of the document.
42With argument 2, select the window showing the cross reference.
43AUTO-HOW is only for the automatic crossref display and is handed through
44to the functions `reftex-view-cr-cite' and `reftex-view-cr-ref'."
45
46 (interactive "P")
47 ;; See where we are.
48 (let* ((macro (car (reftex-what-macro-safe 1)))
49 (key (reftex-this-word "^{}%\n\r,"))
50 dw)
51
52 (if (or (null macro) (reftex-in-comment))
53 (error "Not on a crossref macro argument"))
54
55 (setq reftex-call-back-to-this-buffer (current-buffer))
56
57 (cond
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))
71 4 nil nil)))
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))
77 3 nil nil)))
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
82 (regexp-quote key))
83 3 nil nil)))
84 (t
85 (reftex-access-scan-info arg)
86 (catch 'exit
87 (let ((list reftex-view-crossref-extra)
88 entry mre action group)
89 (while (setq entry (pop list))
90 (setq mre (car entry)
91 action (nth 1 entry)
92 group (nth 2 entry))
93 (when (string-match mre macro)
94 (setq dw (reftex-view-regexp-match
95 (format action key) group nil nil))
96 (throw 'exit t))))
97 (error "Not on a crossref macro argument"))))
98 (if (and (eq arg 2) (windowp dw)) (select-window dw))))
99
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.
105
106 ;; Ensure access to scanning info
107 (reftex-access-scan-info (or arg current-prefix-arg))
108
109 (if (eq how 'tmp-window)
110 ;; Remember the window configuration
111 (put 'reftex-auto-view-crossref 'last-window-conf
112 (current-window-configuration)))
113
114 (let (files size item (pos (point)) (win (selected-window)) pop-win)
115 ;; Find the citation mode and the file list
116 (cond
117 ((assq 'bib (symbol-value reftex-docstruct-symbol))
118 (setq item nil
119 files (reftex-get-bibfile-list)))
120 ((assq 'thebib (symbol-value reftex-docstruct-symbol))
121 (setq item t
24d66fcc
CD
122 files (reftex-uniquify
123 (mapcar 'cdr
124 (reftex-all-assq
125 'thebib (symbol-value reftex-docstruct-symbol))))))
1a9461d0
CD
126 (reftex-default-bibliography
127 (setq item nil
128 files (reftex-default-bibliography)))
129 (how) ;; don't throw for special display
130 (t (error "Cannot display crossref")))
131
132 (if (eq how 'echo)
133 ;; Display in Echo area
134 (reftex-echo-cite key files item)
135 ;; Display in a window
136 (if (not (eq how 'tmp-window))
137 ;; Normal display
138 (reftex-pop-to-bibtex-entry key files nil t item)
139 ;; A temporary window
140 (condition-case nil
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)
144 (error "")))
145 ;; Resize the window
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)))
150 (recenter 0))
151 ;; Arrange restoration
152 (add-hook 'pre-command-hook 'reftex-restore-window-conf))
153
154 ;; Normal display in other window
155 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
156 (setq pop-win (selected-window))
157 (select-window win)
158 (goto-char pos)
159 (when (equal arg 2)
160 (select-window pop-win)))))
161
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.
167
168 ;; Ensure access to scanning info
169 (reftex-access-scan-info (or arg current-prefix-arg))
170
171 (if (eq how 'tmp-window)
172 ;; Remember the window configuration
173 (put 'reftex-auto-view-crossref 'last-window-conf
174 (current-window-configuration)))
175
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)))
180
181 (if (and (not entry) (stringp label) xr-re (string-match xr-re label))
182 ;; Label is defined in external document
183 (save-excursion
184 (save-match-data
185 (set-buffer
186 (or (reftex-get-file-buffer-force
187 (cdr (assoc (match-string 1 label) (nth 1
188 xr-data))))
189 (error "Problem with external label %s" label))))
190 (setq label (substring label (match-end 1)))
191 (reftex-access-scan-info)
192 (setq entry
193 (assoc label (symbol-value reftex-docstruct-symbol)))))
194 (if (eq how 'echo)
195 ;; Display in echo area
196 (reftex-echo-ref label entry (symbol-value reftex-docstruct-symbol))
197 (let ((window-conf (current-window-configuration)))
198 (condition-case nil
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)
204
205 (when (eq how 'tmp-window)
206 ;; Resize window and arrange restauration
207 (shrink-window (1- (- (window-height) 9)))
208 (recenter '(4))
209 (add-hook 'pre-command-hook 'reftex-restore-window-conf))
210 (setq pop-win (selected-window))
211 (select-window win)
212 (goto-char pos)
213 (when (equal arg 2)
214 (select-window pop-win)))))
215
216(defun reftex-mouse-view-crossref (ev)
217 "View cross reference of \\ref or \\cite macro where you click.
218If the macro at point is a \\ref, show the corresponding label definition.
219If it is a \\cite, show the BibTeX database entry.
220If there is no such macro at point, search forward to find one.
221With argument, actually select the window showing the cross reference."
222 (interactive "e")
223 (mouse-set-point ev)
224 (reftex-view-crossref current-prefix-arg))
225
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.
230 (and reftex-mode
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
238 (save-excursion
239 (search-backward "\\" nil t)
240 (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\)"))
241
242 (condition-case nil
243 (let ((current-prefix-arg nil))
244 (cond
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))
249 (t nil)))
250 (error nil))))
251
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))
256
257(defun reftex-echo-ref (label entry docstruct)
258 ;; Display crossref info in echo area.
259 (cond
260 ((null docstruct)
261 (message (substitute-command-keys (format reftex-no-info-message "ref"))))
262 ((null entry)
263 (message "ref: unknown label: %s" label))
264 (t
265 (when (stringp (nth 2 entry))
266 (message "ref(%s): %s" (nth 1 entry) (nth 2 entry)))
267 (let ((buf (get-buffer " *Echo Area*")))
268 (when buf
269 (save-excursion
270 (set-buffer buf)
271 (run-hooks 'reftex-display-copied-context-hook)))))))
272
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))
278
279 (if (and reftex-cache-cite-echo cache-entry)
280 ;; We can just use the cache
281 (setq string (cdr cache-entry))
282
283 ;; Need to look in the database
284 (unless reftex-revisit-to-echo
285 (setq files (reftex-visited-files files)))
286
287 (setq entry
288 (condition-case nil
289 (save-excursion
290 (reftex-pop-to-bibtex-entry key files nil nil item t))
291 (error
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"))))
296 nil)))
297 (when entry
298 (if item
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*"))
306 (save-excursion
307 (set-buffer buf)
308 (run-hooks 'reftex-display-copied-context-hook)))))
309
310(defvar reftex-use-itimer-in-xemacs nil
311 "*Non-nil means use the idle timers in XEmacs for crossref display.
312Currently, idle timer restart is broken and we use the post-command-hook.")
313
314(defun reftex-toggle-auto-view-crossref ()
315 "Toggle the automatic display of crossref information in the echo area.
316When active, leaving point idle in the argument of a \\ref or \\cite macro
317will display info in the echo area."
318 (interactive)
319 (if reftex-auto-view-crossref-timer
320 (progn
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)
335 t)
336 (run-with-idle-timer
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")))
341
342(defun reftex-start-itimer-once ()
343 (and reftex-mode
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))))
349
350(defun reftex-view-crossref-from-bibtex (&optional arg)
351 "View location in a LaTeX document which cites the BibTeX entry at point.
352Since BibTeX files can be used by many LaTeX documents, this function
353prompts upon first use for a buffer in RefTeX mode. To reset this
354link to a document, call the function with with a prefix arg.
355Calling this function several times find successive citation locations."
356 (interactive "P")
357 (when arg
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
362 (unless ref-buffer
363 (setq ref-buffer
364 (save-excursion
365 (completing-read
366 "Reference buffer: "
367 (delq nil
368 (mapcar
369 (lambda (b)
370 (set-buffer b)
371 (if reftex-mode (list (buffer-name b)) nil))
372 (buffer-list)))
373 nil t)))
374 (put 'reftex-bibtex-view-cite-locations :ref-buffer ref-buffer))
375 ;; Search for citations
376 (bibtex-beginning-of-entry)
377 (if (looking-at
378 "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*\\([^, \t\r\n}]+\\)")
379 (progn
380 (goto-char (match-beginning 1))
381 (reftex-view-regexp-match
382 (format reftex-find-citation-regexp-format
383 (regexp-quote (match-string 1)))
384 3 arg ref-buffer))
385 (error "Cannot find citation key in BibTeX entry"))))
386
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.
394
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))
402 match pop-window)
403 (switch-to-buffer-other-window (or ref-buffer (current-buffer)))
404 ;; Search
405 (condition-case nil
406 (if cont
407 (setq match (reftex-global-search-continue))
408 (reftex-access-scan-info)
409 (setq match (reftex-global-search re (reftex-all-document-files))))
410 (error nil))
411 ;; Evaluate the match.
412 (if match
413 (progn
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)
423 (if match
424 (progn
425 (message "Match Nr. %s" cnt)
426 pop-window)
427 (if cont
428 (error "No further matches (total number of matches: %d)" cnt)
429 (error "No matches")))))
430
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))
438
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))
448 file)
449 ;; Take up starting position
450 (unless buf (error "No such buffer %s" buf))
451 (switch-to-buffer buf)
452 (widen)
453 (goto-char pos)
454 ;; Search and switch file if necessary
455 (if (catch 'exit
456 (while t
457 (when (re-search-forward regexp nil t)
458 (move-marker reftex-global-search-marker (point))
459 (throw 'exit t))
460 ;; No match - goto next file
461 (pop file-list)
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)
468 (widen)
469 (goto-char 1)))
470 t
471 (move-marker reftex-global-search-marker nil)
472 (error "All files processed"))))
473
474;;; reftex-vcr.el ends here