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