refer.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. ;;; refer.el --- look up references in bibliography files
  2. ;; Copyright (C) 1992, 1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Ashwin Ram <ashwin@cc.gatech.edu>
  4. ;; Maintainer: Gernot Heiser <gernot@acm.org>
  5. ;; Adapted-By: ESR
  6. ;; Keywords: bib
  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. ;; Functions to look up references in bibliography files given lists of
  20. ;; keywords, similar to refer(1). I don't use tags since tags on .bib files
  21. ;; only picks up the cite key, where as refer-find-entry looks for occurrences
  22. ;; of keywords anywhere in the bibliography entry.
  23. ;;
  24. ;; To use:
  25. ;; (autoload 'refer-find-entry "refer" nil t)
  26. ;; or (require 'refer)
  27. ;;
  28. ;; To look for an article by Knuth about semaphores:
  29. ;; Invoke refer-find-entry, then in response to the Keywords: prompt,
  30. ;; say: Knuth semaphores (a blank-separated list of keywords to be used
  31. ;; as search strings).
  32. ;;
  33. ;; To continue the previous search, i.e., to search for the next occurrence
  34. ;; of the keywords, use refer-find-next-entry, or invoke refer-find-entry
  35. ;; with a prefix argument.
  36. ;;
  37. ;; Once you've found the entry you want to reference, invoke
  38. ;; refer-yank-key to insert it at point in the current buffer
  39. ;; (typically as the argument of a \cite{} command).
  40. ;;
  41. ;; I use (define-key tex-mode-map "\C-c\C-y" 'refer-yank-key)
  42. ;; to bind this often-used function to a key in (la)tex-mode.
  43. ;;
  44. ;; If the list of bibliography files changes, reinitialize the variable
  45. ;; refer-bib-files.
  46. ;;
  47. ;; To customize:
  48. ;; See variables refer-bib-files, refer-cache-bib-files and
  49. ;; refer-bib-files-regexp. By default, these are set up so that refer
  50. ;; looks for the keywords you specify in all the .bib files in the current
  51. ;; directory.
  52. ;;
  53. ;; The only assumption I make about bib files is that they contain a bunch
  54. ;; of entries, one to a paragraph. refer-find-entry searches paragraph by
  55. ;; paragraph, looking for a paragraph containing all the keywords
  56. ;; specified. So you should be able to use pretty much any bib file with
  57. ;; this code. If your bib file does not use paragraphs to separate
  58. ;; entries, try setting the paragraph-start/separate variables, or changing
  59. ;; the (forward-paragraph 1) call in refer-find-entry-in-file.
  60. ;;; Code:
  61. (provide 'refer)
  62. (defgroup refer nil
  63. "Look up references in bibliography files."
  64. :prefix "refer-"
  65. :group 'wp)
  66. (defcustom refer-bib-directory nil
  67. "Directory, or list of directories, to search for \\.bib files.
  68. Can be set to 'bibinputs or 'texinputs, in which case the environment
  69. variable BIBINPUTS or TEXINPUTS, respectively, is used to obtain a
  70. list of directories. Useful only if `refer-bib-files' is set to 'dir or
  71. a list of file names (without directory). A value of nil indicates the
  72. current working directory.
  73. If `refer-bib-directory' is 'bibinputs or 'texinputs, it is setq'd to
  74. the appropriate list of directories when it is first used.
  75. Note that an empty directory is interpreted by BibTeX as indicating
  76. the default search path. Since Refer does not know that default path,
  77. it cannot search it. Include that path explicitly in your BIBINPUTS
  78. environment if you really want it searched (which is not likely to
  79. happen anyway)."
  80. :type '(choice (repeat directory) (const bibinputs) (const texinputs))
  81. :group 'refer)
  82. (defcustom refer-bib-files 'dir
  83. "List of \\.bib files to search for references,
  84. or one of the following special values:
  85. nil = prompt for \\.bib file (if visiting a \\.bib file, use it as default)
  86. auto = read \\.bib file names from appropriate command in buffer (see
  87. `refer-bib-files-regexp') unless the buffer's mode is `bibtex-mode',
  88. in which case only the buffer is searched
  89. dir = use all \\.bib files in directories referenced by `refer-bib-directory'.
  90. If a specified file doesn't exist and has no extension, a \\.bib extension
  91. is automatically tried.
  92. If `refer-bib-files' is nil, auto or dir, it is setq'd to the appropriate
  93. list of files when it is first used if `refer-cache-bib-files' is t. If
  94. `refer-cache-bib-files' is nil, the list of \\.bib files to use is re-read
  95. each time it is needed."
  96. :type '(choice (repeat file) (const nil) (const auto) (const dir))
  97. :group 'refer)
  98. (defcustom refer-cache-bib-files t
  99. "Variable determining whether the value of `refer-bib-files' should be cached.
  100. If t, initialize the value of refer-bib-files the first time it is used. If
  101. nil, re-read the list of \\.bib files depending on the value of `refer-bib-files'
  102. each time it is needed."
  103. :type 'boolean
  104. :group 'refer)
  105. (defcustom refer-bib-files-regexp "\\\\bibliography"
  106. "Regexp matching a bibliography file declaration.
  107. The current buffer is expected to contain a line such as
  108. \\bibliography{file1,file2,file3}
  109. which is read to set up `refer-bib-files'. The regexp must specify the command
  110. \(such as \\bibliography) that is used to specify the list of bib files. The
  111. command is expected to specify a file name, or a list of comma-separated file
  112. names, within curly braces.
  113. If a specified file doesn't exist and has no extension, a \\.bib extension
  114. is automatically tried."
  115. :type 'regexp
  116. :group 'refer)
  117. (make-variable-buffer-local 'refer-bib-files)
  118. (make-variable-buffer-local 'refer-cache-bib-files)
  119. (make-variable-buffer-local 'refer-bib-directory)
  120. ;;; Internal variables
  121. (defvar refer-saved-state nil)
  122. (defvar refer-previous-keywords nil)
  123. (defvar refer-saved-pos nil)
  124. (defvar refer-same-file nil)
  125. (defun refer-find-entry (keywords &optional continue)
  126. "Find entry in refer-bib-files containing KEYWORDS.
  127. If KEYWORDS is nil, prompt user for blank-separated list of keywords.
  128. If CONTINUE is non-nil, or if called interactively with a prefix arg,
  129. look for next entry by continuing search from previous point."
  130. (interactive (list nil current-prefix-arg))
  131. (or keywords (setq keywords (if continue
  132. refer-previous-keywords
  133. (read-string "Keywords: "))))
  134. (setq refer-previous-keywords keywords)
  135. (refer-find-entry-internal keywords continue))
  136. (defun refer-find-next-entry ()
  137. "Find next occurrence of entry in `refer-bib-files'. See `refer-find-entry'."
  138. (interactive)
  139. (refer-find-entry-internal refer-previous-keywords t))
  140. (defun refer-yank-key ()
  141. "Inserts at point in current buffer the \"key\" field of the entry
  142. found on the last `refer-find-entry' or `refer-find-next-entry'."
  143. (interactive)
  144. (let ((old-point (point)))
  145. (insert
  146. (save-window-excursion
  147. (save-excursion
  148. (find-file (car refer-saved-state))
  149. (if (looking-at
  150. "[ \t\n]*@\\s-*[a-zA-Z][a-zA-Z0-9]*\\s-*{\\s-*\\([^ \t\n,]+\\)\\s-*,")
  151. (buffer-substring (match-beginning 1) (match-end 1))
  152. (error "Cannot find key for entry in file %s"
  153. (car refer-saved-state))))))
  154. (if (not (= (point) old-point))
  155. (set-mark old-point))))
  156. (defun refer-find-entry-internal (keywords continue)
  157. (let ((keywords-list (refer-convert-string-to-list-of-strings keywords))
  158. (old-buffer (current-buffer))
  159. (old-window (selected-window))
  160. (new-window (selected-window))
  161. (files (if continue
  162. refer-saved-state
  163. (setq refer-saved-pos nil)
  164. (refer-get-bib-files)))
  165. (n 0)
  166. (found nil)
  167. (file nil))
  168. ;; find window in which to display bibliography file.
  169. ;; if a bibliography file is already displayed in a window, use
  170. ;; that one, otherwise use any window other than the current one
  171. (setq new-window
  172. (get-window-with-predicate
  173. (lambda (w)
  174. (while (and (not (null (setq file (nth n files))))
  175. (setq n (1+ n))
  176. (not (string-equal file
  177. (buffer-file-name
  178. (window-buffer w))))))
  179. file)))
  180. (unless new-window
  181. ;; didn't find bib file in any window:
  182. (when (one-window-p 'nomini)
  183. (setq old-window (split-window)))
  184. (setq new-window (next-window old-window 'nomini)))
  185. (select-window (if refer-same-file
  186. old-window
  187. new-window)) ; the window in which to show the bib file
  188. (catch 'found
  189. (while files
  190. (let ((file (cond ((file-exists-p (car files)) (car files))
  191. ((file-exists-p (concat (car files) ".bib"))
  192. (concat (car files) ".bib")))))
  193. (setq refer-saved-state files)
  194. (if file
  195. (if (refer-find-entry-in-file keywords-list file refer-saved-pos)
  196. (progn
  197. (setq refer-saved-pos (point))
  198. (recenter 0)
  199. (throw 'found (find-file file)))
  200. (setq refer-saved-pos nil
  201. files (cdr files)))
  202. (progn (ding)
  203. (message "Scanning %s... No such file" (car files))
  204. (sit-for 1)
  205. (setq files (cdr files))))))
  206. (ding)
  207. (message "Keywords \"%s\" not found in any \.bib file" keywords))
  208. (select-window old-window)))
  209. (defun refer-find-entry-in-file (keywords-list file &optional old-pos)
  210. (message "Scanning %s..." file)
  211. (expand-file-name file)
  212. (set-buffer (find-file-noselect file))
  213. (find-file file)
  214. (if (not old-pos)
  215. (goto-char (point-min))
  216. (goto-char old-pos)
  217. (forward-paragraph 1))
  218. (let ((begin (point))
  219. (end 0)
  220. (found nil))
  221. (while (and (not found)
  222. (not (eobp)))
  223. (forward-paragraph 1)
  224. (setq end (point))
  225. (setq found
  226. (refer-every (function (lambda (keyword)
  227. (goto-char begin)
  228. (re-search-forward keyword end t)))
  229. keywords-list))
  230. (if (not found)
  231. (progn
  232. (setq begin end)
  233. (goto-char begin))))
  234. (if found
  235. (progn (goto-char begin)
  236. (re-search-forward "\\W" nil t)
  237. (message "Scanning %s... found" file))
  238. (progn (message "Scanning %s... not found" file)
  239. nil))))
  240. (defun refer-every (pred l)
  241. (cond ((null l) nil)
  242. ((funcall pred (car l))
  243. (or (null (cdr l))
  244. (refer-every pred (cdr l))))))
  245. (defun refer-convert-string-to-list-of-strings (s)
  246. (let ((current (current-buffer))
  247. (temp-buffer (get-buffer-create "*refer-temp*")))
  248. (set-buffer temp-buffer)
  249. (erase-buffer)
  250. (insert (regexp-quote s))
  251. (goto-char (point-min))
  252. (insert "(\"")
  253. (while (re-search-forward "[ \t]+" nil t)
  254. (replace-match "\" \"" t t))
  255. (goto-char (point-max))
  256. (insert "\")")
  257. (goto-char (point-min))
  258. (prog1 (read temp-buffer)
  259. (set-buffer current))))
  260. (defun refer-expand-files (file-list dir-list)
  261. (let (file files dir dirs)
  262. (while (setq file (car file-list))
  263. (setq dirs (copy-alist dir-list))
  264. (while (setq dir (car dirs))
  265. (if (file-exists-p (expand-file-name file dir))
  266. (setq files (append files (list (expand-file-name file dir)))
  267. dirs nil)
  268. (if (file-exists-p (expand-file-name (concat file ".bib") dir))
  269. (setq files (append files (list (expand-file-name (concat file ".bib")
  270. dir)))
  271. dirs nil)
  272. (setq dirs (cdr dirs)))))
  273. (setq file-list (cdr file-list)))
  274. files))
  275. (defun refer-get-bib-files ()
  276. (let* ((dir-list
  277. (cond
  278. ((null refer-bib-directory)
  279. '("."))
  280. ((or (eq refer-bib-directory 'texinputs)
  281. (eq refer-bib-directory 'bibinputs))
  282. (let ((envvar (getenv (if (eq refer-bib-directory 'texinputs)
  283. "TEXINPUTS"
  284. "BIBINPUTS")))
  285. (dirs nil))
  286. (if (null envvar)
  287. (setq envvar "."))
  288. (while (string-match ":" envvar)
  289. (let ((dir (substring envvar 0 (match-beginning 0))))
  290. (if (and (not (string-equal "" dir))
  291. (file-directory-p dir))
  292. (setq dirs (append (list (expand-file-name dir nil))
  293. dirs))))
  294. (setq envvar (substring envvar (match-end 0))))
  295. (if (and (not (string-equal "" envvar))
  296. (file-directory-p envvar))
  297. (setq dirs (append (list envvar) dirs)))
  298. (setq dirs (nreverse dirs))))
  299. ((listp refer-bib-directory)
  300. refer-bib-directory)
  301. (t
  302. (list refer-bib-directory))))
  303. (files
  304. (cond
  305. ((null refer-bib-files)
  306. (list (expand-file-name
  307. (if (eq major-mode 'bibtex-mode)
  308. (read-file-name
  309. (format ".bib file (default %s): "
  310. (file-name-nondirectory
  311. (buffer-file-name)))
  312. (file-name-directory (buffer-file-name))
  313. (file-name-nondirectory (buffer-file-name))
  314. t)
  315. (read-file-name ".bib file: " nil nil t)))))
  316. ((eq refer-bib-files 'auto)
  317. (let ((files
  318. (save-excursion
  319. (if (setq refer-same-file (eq major-mode 'bibtex-mode))
  320. (list buffer-file-name)
  321. (if (progn
  322. (goto-char (point-min))
  323. (re-search-forward (concat refer-bib-files-regexp
  324. "\\s-*\{") nil t))
  325. (let ((files (list (buffer-substring
  326. (point)
  327. (progn
  328. (re-search-forward "[,\}]"
  329. nil t)
  330. (backward-char 1)
  331. (point))))))
  332. (while (not (looking-at "\}"))
  333. (setq files (append files
  334. (list (buffer-substring
  335. (progn (forward-char 1)
  336. (point))
  337. (progn (re-search-forward
  338. "[,\}]" nil t)
  339. (backward-char 1)
  340. (point)))))))
  341. files)
  342. (error (concat "No \\\\bibliography command in this "
  343. "buffer, can't read refer-bib-files")))))))
  344. (refer-expand-files files dir-list)))
  345. ((eq refer-bib-files 'dir)
  346. (let ((dirs (nreverse dir-list))
  347. dir files)
  348. (while (setq dir (car dirs))
  349. (setq files
  350. (append (directory-files dir t "\\.bib$")
  351. files))
  352. (setq dirs (cdr dirs)))
  353. files))
  354. ((and (listp refer-bib-files)
  355. (or (eq refer-bib-directory 'texinputs)
  356. (eq refer-bib-directory 'bibinputs)))
  357. (refer-expand-files refer-bib-files dir-list))
  358. ((listp refer-bib-files) refer-bib-files)
  359. (t (error "Invalid value for refer-bib-files: %s"
  360. refer-bib-files)))))
  361. (if (or (eq refer-bib-directory 'texinputs)
  362. (eq refer-bib-directory 'bibinputs))
  363. (setq refer-bib-directory dir-list))
  364. (if refer-cache-bib-files
  365. (setq refer-bib-files files))
  366. files))
  367. ;;; refer.el ends here