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