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