1 ;;; refer.el --- look up references in bibliography files.
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Ashwin Ram <Ram-Ashwin@cs.yale.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Functions to look up references in bibliography files given lists of
29 ;; keywords, similar to refer(1). I don't use tags since tags on .bib files
30 ;; only picks up the cite key, where as refer-find-entry looks for occurrences
31 ;; of keywords anywhere in the bibliography entry.
34 ;; (autoload 'refer-find-entry "refer" nil t)
35 ;; or (require 'refer)
37 ;; To look for an article by Knuth about semaphores:
38 ;; Invoke refer-find-entry, then in response to the Keywords: prompt,
39 ;; say: Knuth semaphores (a blank-separated list of keywords to be used
40 ;; as search strings).
42 ;; To continue the previous search, i.e., to search for the next occurrence
43 ;; of the keywords, use refer-find-next-entry, or invoke refer-find-entry
44 ;; with a prefix argument.
46 ;; If the list of bibliography files changes, reinitialize the variable
50 ;; See variables refer-bib-files, refer-cache-bib-files and
51 ;; refer-bib-files-regexp. By default, these are set up so that refer
52 ;; looks for the keywords you specify in all the .bib files in the current
55 ;; The only assumption I make about bib files is that they contain a bunch
56 ;; of entries, one to a paragraph. refer-find-entry searches paragraph by
57 ;; paragraph, looking for a paragraph containing all the keywords
58 ;; specified. So you should be able to use pretty much any bib file with
59 ;; this code. If your bib file does not use paragraphs to separate
60 ;; entries, try setting the paragraph-start/separate variables, or changing
61 ;; the (forward-paragraph 1) call in refer-find-entry-in-file.
65 ;; 01/08/89 Ashwin Ram <Ram-Ashwin@cs.yale.edu>
73 (defvar refer-bib-files
'dir
74 "*List of \\.bib files to search for references,
75 or one of the following special values:
76 nil = prompt for \\.bib file (if visiting a \\.bib file, use it as default)
77 auto = read \\.bib file names from appropriate command in buffer (see refer-bib-files-regexp)
78 dir = use all \\.bib files in current directory.
80 If a specified file doesn't exist and has no extension, a \\.bib extension
81 is automatically tried.
83 If refer-bib-files is nil, auto or dir, it is setq'd to the appropriate
84 list of files when it is first used if refer-cache-bib-files is t. If
85 refer-cache-bib-files is nil, the list of \\.bib files to use is re-read
86 each time it is needed.")
88 (defvar refer-cache-bib-files t
89 "*Variable determining whether the value of refer-bib-files should be cached.
90 If t, initialize the value of refer-bib-files the first time it is used. If
91 nil, re-read the list of \\.bib files depending on the value of refer-bib-files
92 each time it is needed.")
94 (defvar refer-bib-files-regexp
"\\\\bibliography"
95 "*Regexp matching a bibliography file declaration.
96 The current buffer is expected to contain a line such as
97 \\bibliography{file1,file2,file3}
98 which is read to set up refer-bib-files. The regexp must specify the command
99 \(such as \\bibliography) that is used to specify the list of bib files. The
100 command is expected to specify a file name, or a list of comma-separated file
101 names, within curly braces.
102 If a specified file doesn't exist and has no extension, a \\.bib extension
103 is automatically tried.")
105 (make-variable-buffer-local 'refer-bib-files
)
106 (make-variable-buffer-local 'refer-cache-bib-files
)
108 (defun refer-find-entry (keywords &optional continue
)
109 "Find entry in refer-bib-files containing KEYWORDS.
110 If KEYWORDS is nil, prompt user for blank-separated list of keywords.
111 If CONTINUE is t, or if called interactively with a prefix arg, look for next
112 entry by continuing search from previous point."
113 (interactive (list nil current-prefix-arg
))
114 (or keywords
(setq keywords
(if continue
115 refer-previous-keywords
116 (read-string "Keywords: "))))
117 (setq refer-previous-keywords keywords
)
118 (refer-find-entry-internal keywords continue
))
120 (defun refer-find-next-entry ()
121 "Find next occurrence of entry in refer-bib-files. See refer-find-entry."
123 (refer-find-entry-internal refer-previous-keywords t
))
125 (defun refer-find-entry-internal (keywords continue
)
126 (let ((keywords-list (refer-convert-string-to-list-of-strings keywords
))
129 (refer-get-bib-files))))
132 (let ((file (cond ((file-exists-p (car files
)) (car files
))
133 ((file-exists-p (concat (car files
) ".bib")) (concat (car files
) ".bib")))))
134 (setq refer-saved-state files
)
136 (if (refer-find-entry-in-file keywords-list file continue
)
137 (throw 'found
(find-file file
))
138 (setq files
(cdr files
)))
139 (progn (message "Scanning %s... No such file" (car files
) (ding))
141 (setq files
(cdr files
))))))
142 (message "Keywords \"%s\" not found in any \.bib file" keywords
(ding)))))
144 (defun refer-find-entry-in-file (keywords-list file
&optional continue
)
145 (message "Scanning %s..." file
) ; (expand-file-name file)
146 (set-buffer (find-file-noselect file
))
148 (forward-paragraph 1)
149 (goto-char (point-min)))
150 (let ((begin (point))
153 (while (and (not found
)
155 (forward-paragraph 1)
158 (refer-every (function (lambda (keyword)
160 (re-search-forward keyword end t
)))
167 (progn (goto-char begin
)
168 (re-search-forward "\\W" nil t
)
169 (message "Scanning %s... found" file
))
170 (progn (message "Scanning %s... not found" file
)
173 (defun refer-every (pred l
)
175 ((funcall pred
(car l
))
177 (refer-every pred
(cdr l
))))))
179 (defun refer-convert-string-to-list-of-strings (s)
180 (let ((current (current-buffer))
181 (temp-buffer (get-buffer-create "*refer-temp*")))
182 (set-buffer temp-buffer
)
184 (insert (regexp-quote s
))
185 (goto-char (point-min))
187 (while (re-search-forward "[ \t]+" nil t
)
188 (replace-match "\" \"" t t
))
189 (goto-char (point-max))
191 (goto-char (point-min))
192 (prog1 (read temp-buffer
)
193 (set-buffer current
))))
195 (defun refer-get-bib-files ()
197 (cond ((null refer-bib-files
)
198 (list (expand-file-name
199 (if (eq major-mode
'bibtex-mode
)
200 (read-file-name (format ".bib file: (default %s) " (file-name-nondirectory (buffer-file-name)))
201 (file-name-directory (buffer-file-name))
202 (file-name-nondirectory (buffer-file-name))
204 (read-file-name ".bib file: " nil nil t
)))))
205 ((listp refer-bib-files
) refer-bib-files
)
206 ((eq refer-bib-files
'auto
)
208 (if (progn (goto-char (point-min))
209 (re-search-forward (concat refer-bib-files-regexp
"\{") nil t
))
210 (let ((files (list (buffer-substring (point)
211 (progn (re-search-forward "[,\}]" nil t
)
214 (while (not (looking-at "\}"))
215 (setq files
(append files
216 (list (buffer-substring (progn (forward-char 1)
218 (progn (re-search-forward "[,\}]" nil t
)
222 (error "No \\\\bibliography command in this buffer, can't read refer-bib-files"))))
223 ((eq refer-bib-files
'dir
)
224 (directory-files "." t
"\\.bib$"))
225 (t (error "Illegal value for refer-bib-files: %s" refer-bib-files
)))))
226 (if refer-cache-bib-files
227 (setq refer-bib-files files
))
230 ;;; refer.el ends here