reftex-dcr.el 19 KB

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