reftex-dcr.el 19 KB

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