1 ;;; refer.el --- look up references in bibliography files
3 ;; Copyright (C) 1992, 1996, 2001, 2004 Free Software Foundation, Inc.
5 ;; Author: Ashwin Ram <ashwin@cc.gatech.edu>
6 ;; Maintainer: Gernot Heiser <gernot@acm.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Functions to look up references in bibliography files given lists of
30 ;; keywords, similar to refer(1). I don't use tags since tags on .bib files
31 ;; only picks up the cite key, where as refer-find-entry looks for occurrences
32 ;; of keywords anywhere in the bibliography entry.
35 ;; (autoload 'refer-find-entry "refer" nil t)
36 ;; or (require 'refer)
38 ;; To look for an article by Knuth about semaphores:
39 ;; Invoke refer-find-entry, then in response to the Keywords: prompt,
40 ;; say: Knuth semaphores (a blank-separated list of keywords to be used
41 ;; as search strings).
43 ;; To continue the previous search, i.e., to search for the next occurrence
44 ;; of the keywords, use refer-find-next-entry, or invoke refer-find-entry
45 ;; with a prefix argument.
47 ;; Once you've found the entry you want to reference, invoke
48 ;; refer-yank-key to insert it at point in the current buffer
49 ;; (typically as the argument of a \cite{} command).
51 ;; I use (define-key tex-mode-map "\C-c\C-y" 'refer-yank-key)
52 ;; to bind this often-used function to a key in (la)tex-mode.
54 ;; If the list of bibliography files changes, reinitialize the variable
58 ;; See variables refer-bib-files, refer-cache-bib-files and
59 ;; refer-bib-files-regexp. By default, these are set up so that refer
60 ;; looks for the keywords you specify in all the .bib files in the current
63 ;; The only assumption I make about bib files is that they contain a bunch
64 ;; of entries, one to a paragraph. refer-find-entry searches paragraph by
65 ;; paragraph, looking for a paragraph containing all the keywords
66 ;; specified. So you should be able to use pretty much any bib file with
67 ;; this code. If your bib file does not use paragraphs to separate
68 ;; entries, try setting the paragraph-start/separate variables, or changing
69 ;; the (forward-paragraph 1) call in refer-find-entry-in-file.
76 "Look up references in bibliography files."
80 (defcustom refer-bib-directory nil
81 "Directory, or list of directories, to search for \\.bib files. Can
82 be set to 'bibinputs or 'texinputs, in which case the environment
83 variable BIBINPUTS or TEXINPUTS, respectively, is used to obtain a
84 list of directories. Useful only if refer-bib-files is set to 'dir or
85 a list of file names (without directory). A value of nil indicates the
86 current working directory.
88 If refer-bib-directory is 'bibinputs or 'texinputs, it is setq'd to
89 the appropriate list of directories when it is first used.
91 Note that an empty directory is interpreted by BibTeX as indicating
92 the default search path. Since Refer does not know that default path,
93 it cannot search it. Include that path explicitly in your BIBINPUTS
94 environment if you really want it searched (which is not likely to
96 :type
'(choice (repeat directory
) (const bibinputs
) (const texinputs
))
99 (defcustom refer-bib-files
'dir
100 "*List of \\.bib files to search for references,
101 or one of the following special values:
102 nil = prompt for \\.bib file (if visiting a \\.bib file, use it as default)
103 auto = read \\.bib file names from appropriate command in buffer (see
104 refer-bib-files-regexp) unless the buffer's mode is bibtex-mode,
105 in which case only the buffer is searched
106 dir = use all \\.bib files in directories referenced by refer-bib-directory.
108 If a specified file doesn't exist and has no extension, a \\.bib extension
109 is automatically tried.
111 If refer-bib-files is nil, auto or dir, it is setq'd to the appropriate
112 list of files when it is first used if refer-cache-bib-files is t. If
113 refer-cache-bib-files is nil, the list of \\.bib files to use is re-read
114 each time it is needed."
115 :type
'(choice (repeat file
) (const nil
) (const auto
) (const dir
))
118 (defcustom refer-cache-bib-files t
119 "*Variable determining whether the value of refer-bib-files should be cached.
120 If t, initialize the value of refer-bib-files the first time it is used. If
121 nil, re-read the list of \\.bib files depending on the value of refer-bib-files
122 each time it is needed."
126 (defcustom refer-bib-files-regexp
"\\\\bibliography"
127 "*Regexp matching a bibliography file declaration.
128 The current buffer is expected to contain a line such as
129 \\bibliography{file1,file2,file3}
130 which is read to set up refer-bib-files. The regexp must specify the command
131 (such as \\bibliography) that is used to specify the list of bib files. The
132 command is expected to specify a file name, or a list of comma-separated file
133 names, within curly braces.
134 If a specified file doesn't exist and has no extension, a \\.bib extension
135 is automatically tried."
139 (make-variable-buffer-local 'refer-bib-files
)
140 (make-variable-buffer-local 'refer-cache-bib-files
)
141 (make-variable-buffer-local 'refer-bib-directory
)
143 ;;; Internal variables
144 (defvar refer-saved-state nil
)
145 (defvar refer-previous-keywords nil
)
146 (defvar refer-saved-pos nil
)
147 (defvar refer-same-file nil
)
149 (defun refer-find-entry (keywords &optional continue
)
150 "Find entry in refer-bib-files containing KEYWORDS.
151 If KEYWORDS is nil, prompt user for blank-separated list of keywords.
152 If CONTINUE is t, or if called interactively with a prefix arg, look for next
153 entry by continuing search from previous point."
154 (interactive (list nil current-prefix-arg
))
155 (or keywords
(setq keywords
(if continue
156 refer-previous-keywords
157 (read-string "Keywords: "))))
158 (setq refer-previous-keywords keywords
)
159 (refer-find-entry-internal keywords continue
))
161 (defun refer-find-next-entry ()
162 "Find next occurrence of entry in refer-bib-files. See refer-find-entry."
164 (refer-find-entry-internal refer-previous-keywords t
))
166 (defun refer-yank-key ()
167 "Inserts at point in current buffer the \"key\" field of the entry
168 found on the last refer-find-entry or refer-find-next-entry."
170 (let ((old-point (point)))
172 (save-window-excursion
174 (find-file (car refer-saved-state
))
176 "[ \t\n]*@\\s-*[a-zA-Z][a-zA-Z0-9]*\\s-*{\\s-*\\([^ \t\n,]+\\)\\s-*,")
177 (buffer-substring (match-beginning 1) (match-end 1))
178 (error "Cannot find key for entry in file %s"
179 (car refer-saved-state
))))))
180 (if (not (= (point) old-point
))
181 (set-mark old-point
))))
183 (defun refer-find-entry-internal (keywords continue
)
184 (let ((keywords-list (refer-convert-string-to-list-of-strings keywords
))
185 (old-buffer (current-buffer))
186 (old-window (selected-window))
187 (new-window (selected-window))
190 (setq refer-saved-pos nil
)
191 (refer-get-bib-files)))
195 ;; find window in which to display bibliography file.
196 ;; if a bibliography file is already displayed in a window, use
197 ;; that one, otherwise use any window other than the current one
199 (get-window-with-predicate
201 (while (and (not (null (setq file
(nth n files
))))
203 (not (string-equal file
205 (window-buffer w
))))))
208 ;; didn't find bib file in any window:
209 (when (one-window-p 'nomini
)
210 (setq old-window
(split-window)))
211 (setq new-window
(next-window old-window
'nomini
)))
212 (select-window (if refer-same-file
214 new-window
)) ; the window in which to show the bib file
217 (let ((file (cond ((file-exists-p (car files
)) (car files
))
218 ((file-exists-p (concat (car files
) ".bib"))
219 (concat (car files
) ".bib")))))
220 (setq refer-saved-state files
)
222 (if (refer-find-entry-in-file keywords-list file refer-saved-pos
)
224 (setq refer-saved-pos
(point))
226 (throw 'found
(find-file file
)))
227 (setq refer-saved-pos nil
230 (message "Scanning %s... No such file" (car files
))
232 (setq files
(cdr files
))))))
234 (message "Keywords \"%s\" not found in any \.bib file" keywords
))
235 (select-window old-window
)))
237 (defun refer-find-entry-in-file (keywords-list file
&optional old-pos
)
238 (message "Scanning %s..." file
)
239 (expand-file-name file
)
240 (set-buffer (find-file-noselect file
))
243 (goto-char (point-min))
245 (forward-paragraph 1))
246 (let ((begin (point))
249 (while (and (not found
)
251 (forward-paragraph 1)
254 (refer-every (function (lambda (keyword)
256 (re-search-forward keyword end t
)))
263 (progn (goto-char begin
)
264 (re-search-forward "\\W" nil t
)
265 (message "Scanning %s... found" file
))
266 (progn (message "Scanning %s... not found" file
)
269 (defun refer-every (pred l
)
271 ((funcall pred
(car l
))
273 (refer-every pred
(cdr l
))))))
275 (defun refer-convert-string-to-list-of-strings (s)
276 (let ((current (current-buffer))
277 (temp-buffer (get-buffer-create "*refer-temp*")))
278 (set-buffer temp-buffer
)
280 (insert (regexp-quote s
))
281 (goto-char (point-min))
283 (while (re-search-forward "[ \t]+" nil t
)
284 (replace-match "\" \"" t t
))
285 (goto-char (point-max))
287 (goto-char (point-min))
288 (prog1 (read temp-buffer
)
289 (set-buffer current
))))
291 (defun refer-expand-files (file-list dir-list
)
292 (let (file files dir dirs
)
293 (while (setq file
(car file-list
))
294 (setq dirs
(copy-alist dir-list
))
295 (while (setq dir
(car dirs
))
296 (if (file-exists-p (expand-file-name file dir
))
297 (setq files
(append files
(list (expand-file-name file dir
)))
299 (if (file-exists-p (expand-file-name (concat file
".bib") dir
))
300 (setq files
(append files
(list (expand-file-name (concat file
".bib")
303 (setq dirs
(cdr dirs
)))))
304 (setq file-list
(cdr file-list
)))
307 (defun refer-get-bib-files ()
310 ((null refer-bib-directory
)
312 ((or (eq refer-bib-directory
'texinputs
)
313 (eq refer-bib-directory
'bibinputs
))
314 (let ((envvar (getenv (if (eq refer-bib-directory
'texinputs
)
320 (while (string-match ":" envvar
)
321 (let ((dir (substring envvar
0 (match-beginning 0))))
322 (if (and (not (string-equal "" dir
))
323 (file-directory-p dir
))
324 (setq dirs
(append (list (expand-file-name dir nil
))
326 (setq envvar
(substring envvar
(match-end 0))))
327 (if (and (not (string-equal "" envvar
))
328 (file-directory-p envvar
))
329 (setq dirs
(append (list envvar
) dirs
)))
330 (setq dirs
(nreverse dirs
))))
331 ((listp refer-bib-directory
)
334 (list refer-bib-directory
))))
337 ((null refer-bib-files
)
338 (list (expand-file-name
339 (if (eq major-mode
'bibtex-mode
)
341 (format ".bib file: (default %s) "
342 (file-name-nondirectory
344 (file-name-directory (buffer-file-name))
345 (file-name-nondirectory (buffer-file-name))
347 (read-file-name ".bib file: " nil nil t
)))))
348 ((eq refer-bib-files
'auto
)
351 (if (setq refer-same-file
(eq major-mode
'bibtex-mode
))
352 (list buffer-file-name
)
354 (goto-char (point-min))
355 (re-search-forward (concat refer-bib-files-regexp
357 (let ((files (list (buffer-substring
360 (re-search-forward "[,\}]"
364 (while (not (looking-at "\}"))
365 (setq files
(append files
366 (list (buffer-substring
367 (progn (forward-char 1)
369 (progn (re-search-forward
374 (error (concat "No \\\\bibliography command in this "
375 "buffer, can't read refer-bib-files")))))))
376 (refer-expand-files files dir-list
)))
377 ((eq refer-bib-files
'dir
)
378 (let ((dirs (nreverse dir-list
))
380 (while (setq dir
(car dirs
))
382 (append (directory-files dir t
"\\.bib$")
384 (setq dirs
(cdr dirs
)))
386 ((and (listp refer-bib-files
)
387 (or (eq refer-bib-directory
'texinputs
)
388 (eq refer-bib-directory
'bibinputs
)))
389 (refer-expand-files refer-bib-files dir-list
))
390 ((listp refer-bib-files
) refer-bib-files
)
391 (t (error "Illegal value for refer-bib-files: %s"
393 (if (or (eq refer-bib-directory
'texinputs
)
394 (eq refer-bib-directory
'bibinputs
))
395 (setq refer-bib-directory dir-list
))
396 (if refer-cache-bib-files
397 (setq refer-bib-files files
))
400 ;;; arch-tag: 151f641b-e79b-462b-9a29-a95c3793f300
401 ;;; refer.el ends here