; Spelling fixes
[emacs.git] / lisp / textmodes / reftex-dcr.el
blob16bc621f88976cf8b8f1b96bdfdb59e52f368f94
1 ;;; reftex-dcr.el --- viewing cross references and citations with RefTeX
3 ;; Copyright (C) 1997-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik@science.uva.nl>
6 ;; Maintainer: auctex-devel@gnu.org
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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl-lib))
29 (declare-function bibtex-beginning-of-entry "bibtex" ())
31 (require 'reftex)
33 ;;;###autoload
34 (defun reftex-view-crossref (&optional arg auto-how fail-quietly)
35 "View cross reference of macro at point. Point must be on the KEY
36 argument. When at a `\\ref' macro, show corresponding `\\label'
37 definition, also in external documents (`xr'). When on a label, show
38 a locations where KEY is referenced. Subsequent calls find additional
39 locations. When on a `\\cite', show the associated `\\bibitem' macro or
40 the BibTeX database entry. When on a `\\bibitem', show a `\\cite' macro
41 which uses this KEY. When on an `\\index', show other locations marked
42 by the same index entry.
43 To define additional cross referencing items, use the option
44 `reftex-view-crossref-extra'. See also `reftex-view-crossref-from-bibtex'.
45 With one or two C-u prefixes, enforce rescanning of the document.
46 With argument 2, select the window showing the cross reference.
47 AUTO-HOW is only for the automatic crossref display and is handed through
48 to the functions `reftex-view-cr-cite' and `reftex-view-cr-ref'."
50 (interactive "P")
51 ;; See where we are.
52 (let* ((macro (car (reftex-what-macro-safe 1)))
53 (key (reftex-this-word "^{}%\n\r, \t"))
54 dw)
56 (if (or (null macro) (reftex-in-comment))
57 (or fail-quietly
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)))
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 (bibtype (reftex-bib-or-thebib)))
121 ;; Find the citation mode and the file list
122 (cond
123 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
124 ((eq bibtype 'bib)
125 (setq item nil
126 files (reftex-get-bibfile-list)))
127 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
128 ((eq bibtype 'thebib)
129 (setq item t
130 files (reftex-uniquify
131 (mapcar 'cdr
132 (reftex-all-assq
133 'thebib (symbol-value reftex-docstruct-symbol))))))
134 (reftex-default-bibliography
135 (setq item nil
136 files (reftex-default-bibliography)))
137 (how) ;; don't throw for special display
138 (t (error "Cannot display crossref")))
140 (if (eq how 'echo)
141 ;; Display in Echo area
142 (reftex-echo-cite key files item)
143 ;; Display in a window
144 (if (not (eq how 'tmp-window))
145 ;; Normal display
146 (reftex-pop-to-bibtex-entry key files nil t item)
147 ;; A temporary window
148 (condition-case nil
149 (reftex-pop-to-bibtex-entry key files nil t item)
150 (error (goto-char pos)
151 (message "cite: no such citation key %s" key)
152 (error "")))
153 ;; Resize the window
154 (setq size (max 1 (count-lines (point)
155 (reftex-end-of-bib-entry item))))
156 (let ((window-min-height 2))
157 (shrink-window (1- (- (window-height) size)))
158 (recenter 0))
159 ;; Arrange restoration
160 (add-hook 'pre-command-hook 'reftex-restore-window-conf))
162 ;; Normal display in other window
163 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
164 (setq pop-win (selected-window))
165 (select-window win)
166 (goto-char pos)
167 (when (equal arg 2)
168 (select-window pop-win)))))
170 (defun reftex-view-cr-ref (arg label how)
171 ;; View crossreference of a ref macro. HOW can have the values
172 ;; nil: Show in another window.
173 ;; echo: Show one-line info in echo area.
174 ;; tmp-window: Show in small window and arrange for window to disappear.
176 ;; Ensure access to scanning info
177 (reftex-access-scan-info (or arg current-prefix-arg))
179 (if (eq how 'tmp-window)
180 ;; Remember the window configuration
181 (put 'reftex-auto-view-crossref 'last-window-conf
182 (current-window-configuration)))
184 (let* ((xr-data (assoc 'xr (symbol-value reftex-docstruct-symbol)))
185 (xr-re (nth 2 xr-data))
186 (entry (assoc label (symbol-value reftex-docstruct-symbol)))
187 (win (selected-window)) pop-win (pos (point)))
189 (if (and (not entry) (stringp label) xr-re (string-match xr-re label))
190 ;; Label is defined in external document
191 (save-excursion
192 (save-match-data
193 (set-buffer
194 (or (reftex-get-file-buffer-force
195 (cdr (assoc (match-string 1 label) (nth 1
196 xr-data))))
197 (error "Problem with external label %s" label))))
198 (setq label (substring label (match-end 1)))
199 (reftex-access-scan-info)
200 (setq entry
201 (assoc label (symbol-value reftex-docstruct-symbol)))))
202 (if (eq how 'echo)
203 ;; Display in echo area
204 (reftex-echo-ref label entry (symbol-value reftex-docstruct-symbol))
205 (let ((window-conf (current-window-configuration)))
206 (condition-case nil
207 (reftex-show-label-location entry t nil t t)
208 (error (set-window-configuration window-conf)
209 (message "ref: Label %s not found" label)
210 (error "ref: Label %s not found" label)))) ;; 2nd is line OK
211 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
213 (when (eq how 'tmp-window)
214 ;; Resize window and arrange restoration
215 (shrink-window (1- (- (window-height) 9)))
216 (recenter '(4))
217 (add-hook 'pre-command-hook 'reftex-restore-window-conf))
218 (setq pop-win (selected-window))
219 (select-window win)
220 (goto-char pos)
221 (when (equal arg 2)
222 (select-window pop-win)))))
224 ;;;###autoload
225 (defun reftex-mouse-view-crossref (ev)
226 "View cross reference of \\ref or \\cite macro where you click.
227 If the macro at point is a \\ref, show the corresponding label definition.
228 If it is a \\cite, show the BibTeX database entry.
229 If there is no such macro at point, search forward to find one.
230 With argument, actually select the window showing the cross reference."
231 (interactive "e")
232 ;; Make sure the referencing macro stays visible in the original window.
233 (mouse-set-point ev)
234 (reftex-view-crossref current-prefix-arg))
236 (defun reftex-view-crossref-when-idle ()
237 ;; Display info about crossref at point in echo area or a window.
238 ;; This function was designed 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
245 reftex-mouse-view-crossref)))
246 ;; Quick precheck if this might be a relevant spot
247 ;; `reftex-view-crossref' will do a more thorough check.
248 (save-excursion
249 (search-backward "\\" nil t)
250 (looking-at "\\\\[a-zA-Z]*\\(cite\\|ref\\|bibentry\\)"))
252 (condition-case nil
253 (let ((current-prefix-arg nil))
254 (cond
255 ((eq reftex-auto-view-crossref t)
256 (reftex-view-crossref -1 'echo 'quiet))
257 ((eq reftex-auto-view-crossref 'window)
258 (reftex-view-crossref -1 'tmp-window 'quiet))
259 (t nil)))
260 (error nil))))
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))
267 (defun reftex-echo-ref (label entry docstruct)
268 ;; Display crossref info in echo area.
269 (cond
270 ((null docstruct)
271 (message "%s"
272 (substitute-command-keys (format reftex-no-info-message "ref"))))
273 ((null entry)
274 (message "ref: unknown label: %s" label))
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
280 (with-current-buffer buf
281 (run-hooks 'reftex-display-copied-context-hook)))))))
283 (defun reftex-echo-cite (key files item)
284 ;; Display citation info in echo area.
285 (let* ((cache (assq 'bibview-cache (symbol-value reftex-docstruct-symbol)))
286 (cache-entry (assoc key (cdr cache)))
287 entry string buf (all-files files))
289 (if (and reftex-cache-cite-echo cache-entry)
290 ;; We can just use the cache
291 (setq string (cdr cache-entry))
293 ;; Need to look in the database
294 (unless reftex-revisit-to-echo
295 (setq files (reftex-visited-files files)))
297 (setq entry
298 (condition-case nil
299 (save-excursion
300 (reftex-pop-to-bibtex-entry key files nil nil item t))
301 (error
302 (if (and files (= (length all-files) (length files)))
303 (message "cite: no such database entry: %s" key)
304 (message "%s" (substitute-command-keys
305 (format reftex-no-info-message "cite"))))
306 nil)))
307 (when entry
308 (if item
309 (setq string (reftex-nicify-text entry))
310 (setq string (reftex-make-cite-echo-string
311 (reftex-parse-bibtex-entry entry)
312 reftex-docstruct-symbol)))))
313 (unless (or (null string) (equal string ""))
314 (message "cite: %s" string))
315 (when (setq buf (get-buffer " *Echo Area*"))
316 (with-current-buffer buf
317 (run-hooks 'reftex-display-copied-context-hook)))))
319 (defvar reftex-use-itimer-in-xemacs nil
320 "Non-nil means use the idle timers in XEmacs for crossref display.
321 Currently, idle timer restart is broken and we use the post-command-hook.")
323 ;;;###autoload
324 (defun reftex-toggle-auto-view-crossref ()
325 "Toggle the automatic display of crossref information in the echo area.
326 When active, leaving point idle in the argument of a \\ref or \\cite macro
327 will display info in the echo area."
328 (interactive)
329 (if reftex-auto-view-crossref-timer
330 (progn
331 (if (featurep 'xemacs)
332 (if reftex-use-itimer-in-xemacs
333 (delete-itimer reftex-auto-view-crossref-timer)
334 (remove-hook 'post-command-hook 'reftex-start-itimer-once))
335 (cancel-timer reftex-auto-view-crossref-timer))
336 (setq reftex-auto-view-crossref-timer nil)
337 (message "Automatic display of crossref information was turned off"))
338 (setq reftex-auto-view-crossref-timer
339 (if (featurep 'xemacs)
340 (if reftex-use-itimer-in-xemacs
341 (start-itimer "RefTeX Idle Timer"
342 'reftex-view-crossref-when-idle
343 reftex-idle-time reftex-idle-time t)
344 (add-hook 'post-command-hook 'reftex-start-itimer-once)
346 (run-with-idle-timer
347 reftex-idle-time t 'reftex-view-crossref-when-idle)))
348 (unless reftex-auto-view-crossref
349 (setq reftex-auto-view-crossref t))
350 (message "Automatic display of crossref information was turned on")))
352 (defun reftex-start-itimer-once ()
353 (and (featurep 'xemacs)
354 reftex-mode
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))))
361 ;;;###autoload
362 (defun reftex-view-crossref-from-bibtex (&optional arg)
363 "View location in a LaTeX document which cites the BibTeX entry at point.
364 Since BibTeX files can be used by many LaTeX documents, this function
365 prompts upon first use for a buffer in RefTeX mode. To reset this
366 link to a document, call the function with a prefix arg.
367 Calling this function several times find successive citation locations."
368 (interactive "P")
369 (when arg
370 ;; Break connection to reference buffer
371 (put 'reftex-bibtex-view-cite-locations :ref-buffer nil))
372 (let ((ref-buffer (get 'reftex-bibtex-view-cite-locations :ref-buffer)))
373 ;; Establish connection to reference buffer
374 (unless ref-buffer
375 (setq ref-buffer
376 (save-current-buffer
377 (completing-read
378 "Reference buffer: "
379 (delq nil
380 (mapcar
381 (lambda (b)
382 (set-buffer b)
383 (if reftex-mode (list (buffer-name b)) nil))
384 (buffer-list)))
385 nil t)))
386 (put 'reftex-bibtex-view-cite-locations :ref-buffer ref-buffer))
387 ;; Search for citations
388 (bibtex-beginning-of-entry)
389 (if (looking-at
390 "@[a-zA-Z]+[ \t\n\r]*[{(][ \t\n\r]*\\([^, \t\r\n}]+\\)")
391 (progn
392 (goto-char (match-beginning 1))
393 (reftex-view-regexp-match
394 (format reftex-find-citation-regexp-format
395 (regexp-quote (match-string 1)))
396 4 arg ref-buffer))
397 (error "Cannot find citation key in BibTeX entry"))))
399 (defun reftex-view-regexp-match (re &optional highlight-group new ref-buffer)
400 ;; Search for RE in current document or in the document of REF-BUFFER.
401 ;; Continue the search, if the same re was searched last.
402 ;; Highlight the group HIGHLIGHT-GROUP of the match.
403 ;; When NEW is non-nil, start a new search regardless.
404 ;; Match point is displayed in another window.
405 ;; Upon success, returns the window which displays the match.
407 ;;; Decide if new search or continued search
408 (let* ((oldprop (get 'reftex-view-regexp-match :props))
409 (newprop (list (current-buffer) re))
410 (cont (and (not new) (equal oldprop newprop)))
411 (cnt (if cont (get 'reftex-view-regexp-match :cnt) 0))
412 (current-window (selected-window))
413 (window-conf (current-window-configuration))
414 match pop-window)
415 (switch-to-buffer-other-window (or ref-buffer (current-buffer)))
416 ;; Search
417 (condition-case nil
418 (if cont
419 (setq match (reftex-global-search-continue))
420 (reftex-access-scan-info)
421 (setq match (reftex-global-search re (reftex-all-document-files))))
422 (error nil))
423 ;; Evaluate the match.
424 (if match
425 (progn
426 (put 'reftex-view-regexp-match :props newprop)
427 (put 'reftex-view-regexp-match :cnt (cl-incf cnt))
428 (reftex-highlight 0 (match-beginning highlight-group)
429 (match-end highlight-group))
430 (add-hook 'pre-command-hook 'reftex-highlight-shall-die)
431 (setq pop-window (selected-window)))
432 (put 'reftex-view-regexp-match :props nil)
433 (or cont (set-window-configuration window-conf)))
434 (select-window current-window)
435 (if match
436 (progn
437 (message "Match Nr. %s" cnt)
438 pop-window)
439 (if cont
440 (error "No further matches (total number of matches: %d)" cnt)
441 (error "No matches")))))
443 (defvar reftex-global-search-marker (make-marker))
444 (defun reftex-global-search (regexp file-list)
445 ;; Start a search for REGEXP in all files of FILE-LIST
446 (put 'reftex-global-search :file-list file-list)
447 (put 'reftex-global-search :regexp regexp)
448 (move-marker reftex-global-search-marker nil)
449 (reftex-global-search-continue))
451 (defun reftex-global-search-continue ()
452 ;; Continue a global search started with `reftex-global-search'
453 (unless (get 'reftex-global-search :file-list)
454 (error "No global search to continue"))
455 (let* ((file-list (get 'reftex-global-search :file-list))
456 (regexp (get 'reftex-global-search :regexp))
457 (buf (or (marker-buffer reftex-global-search-marker)
458 (reftex-get-file-buffer-force (car file-list))))
459 (pos (or (marker-position reftex-global-search-marker) 1))
460 file)
461 ;; Take up starting position
462 (unless buf (error "No such buffer %s" buf))
463 (switch-to-buffer buf)
464 (widen)
465 (goto-char pos)
466 ;; Search and switch file if necessary
467 (if (catch 'exit
468 (while t
469 (when (re-search-forward regexp nil t)
470 (move-marker reftex-global-search-marker (point))
471 (throw 'exit t))
472 ;; No match - goto next file
473 (pop file-list)
474 (or file-list (throw 'exit nil))
475 (setq file (car file-list)
476 buf (reftex-get-file-buffer-force file))
477 (unless buf (error "Cannot access file %s" file))
478 (put 'reftex-global-search :file-list file-list)
479 (switch-to-buffer buf)
480 (widen)
481 (goto-char 1)))
483 (move-marker reftex-global-search-marker nil)
484 (error "All files processed"))))
486 (provide 'reftex-dcr)
488 ;;; reftex-dcr.el ends here
490 ;; Local Variables:
491 ;; generated-autoload-file: "reftex-loaddefs.el"
492 ;; End: