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