7638
|
1 ;;; refer.el --- look up references in bibliography files.
|
|
2
|
16473
|
3 ;; Copyright (C) 1992, 1996 Free Software Foundation, Inc.
|
7638
|
4
|
16473
|
5 ;; Author: Ashwin Ram <ashwin@cc.gatech.edu>
|
30326
|
6 ;; Maintainer: Gernot Heiser <gernot@acm.org>
|
7638
|
7 ;; Adapted-By: ESR
|
|
8 ;; Keywords: bib
|
|
9
|
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
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)
|
|
15 ;; any later version.
|
|
16
|
|
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.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
14169
|
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.
|
7638
|
26
|
|
27 ;;; Commentary:
|
14169
|
28
|
7638
|
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.
|
|
33 ;;
|
|
34 ;; To use:
|
|
35 ;; (autoload 'refer-find-entry "refer" nil t)
|
|
36 ;; or (require 'refer)
|
|
37 ;;
|
|
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).
|
|
42 ;;
|
|
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.
|
|
46 ;;
|
16473
|
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).
|
|
50 ;;
|
|
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.
|
|
53 ;;
|
7638
|
54 ;; If the list of bibliography files changes, reinitialize the variable
|
|
55 ;; refer-bib-files.
|
|
56 ;;
|
|
57 ;; To customize:
|
|
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
|
|
61 ;; directory.
|
|
62 ;;
|
|
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.
|
|
70
|
|
71 ;;; Code:
|
|
72
|
|
73 (provide 'refer)
|
|
74
|
20959
|
75 (defgroup refer nil
|
|
76 "Look up references in bibliography files."
|
|
77 :prefix "refer-"
|
|
78 :group 'wp)
|
|
79
|
|
80 (defcustom refer-bib-directory nil
|
16473
|
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.
|
|
87
|
|
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.
|
|
90
|
|
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
|
20959
|
95 happen anyway)."
|
|
96 :type '(choice (repeat directory) (const bibinputs) (const texinputs))
|
|
97 :group 'refer)
|
16473
|
98
|
20959
|
99 (defcustom refer-bib-files 'dir
|
|
100 "*List of \\.bib files to search for references,
|
7638
|
101 or one of the following special values:
|
|
102 nil = prompt for \\.bib file (if visiting a \\.bib file, use it as default)
|
16473
|
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.
|
7638
|
107
|
|
108 If a specified file doesn't exist and has no extension, a \\.bib extension
|
|
109 is automatically tried.
|
|
110
|
|
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
|
20959
|
114 each time it is needed."
|
|
115 :type '(choice (repeat file) (const nil) (const auto) (const dir))
|
|
116 :group 'refer)
|
7638
|
117
|
20959
|
118 (defcustom refer-cache-bib-files t
|
|
119 "*Variable determining whether the value of refer-bib-files should be cached.
|
7638
|
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
|
20959
|
122 each time it is needed."
|
|
123 :type 'boolean
|
|
124 :group 'refer)
|
7638
|
125
|
20959
|
126 (defcustom refer-bib-files-regexp "\\\\bibliography"
|
|
127 "*Regexp matching a bibliography file declaration.
|
7638
|
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
|
16473
|
131 (such as \\bibliography) that is used to specify the list of bib files. The
|
7638
|
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
|
20959
|
135 is automatically tried."
|
|
136 :type 'regexp
|
|
137 :group 'refer)
|
7638
|
138
|
|
139 (make-variable-buffer-local 'refer-bib-files)
|
|
140 (make-variable-buffer-local 'refer-cache-bib-files)
|
16473
|
141 (make-variable-buffer-local 'refer-bib-directory)
|
|
142
|
|
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)
|
7638
|
148
|
|
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
|
16473
|
157 (read-string "Keywords: "))))
|
7638
|
158 (setq refer-previous-keywords keywords)
|
|
159 (refer-find-entry-internal keywords continue))
|
|
160
|
|
161 (defun refer-find-next-entry ()
|
|
162 "Find next occurrence of entry in refer-bib-files. See refer-find-entry."
|
|
163 (interactive)
|
|
164 (refer-find-entry-internal refer-previous-keywords t))
|
|
165
|
16473
|
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."
|
|
169 (interactive)
|
|
170 (let ((old-point (point)))
|
|
171 (insert
|
|
172 (save-window-excursion
|
|
173 (save-excursion
|
|
174 (find-file (car refer-saved-state))
|
|
175 (if (looking-at
|
|
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))))
|
|
182
|
7638
|
183 (defun refer-find-entry-internal (keywords continue)
|
12952
|
184 (let ((keywords-list (refer-convert-string-to-list-of-strings keywords))
|
16473
|
185 (old-buffer (current-buffer))
|
|
186 (old-window (selected-window))
|
|
187 (new-window (selected-window))
|
7638
|
188 (files (if continue
|
|
189 refer-saved-state
|
16473
|
190 (setq refer-saved-pos nil)
|
|
191 (refer-get-bib-files)))
|
|
192 (n 0)
|
|
193 (found nil)
|
|
194 (file nil))
|
|
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
|
29997
|
198 (setq new-window
|
|
199 (some-window (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)))
|
|
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)))
|
16473
|
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)))
|
|
228 (progn (message "Scanning %s... No such file" (car files) (ding))
|
|
229 (sit-for 1)
|
|
230 (setq files (cdr files))))))
|
|
231 (message "Keywords \"%s\" not found in any \.bib file" keywords (ding)))
|
|
232 (select-window old-window)))
|
7638
|
233
|
16473
|
234 (defun refer-find-entry-in-file (keywords-list file &optional old-pos)
|
|
235 (message "Scanning %s..." file)
|
|
236 (expand-file-name file)
|
7638
|
237 (set-buffer (find-file-noselect file))
|
16473
|
238 (find-file file)
|
|
239 (if (not old-pos)
|
|
240 (goto-char (point-min))
|
|
241 (goto-char old-pos)
|
|
242 (forward-paragraph 1))
|
7638
|
243 (let ((begin (point))
|
|
244 (end 0)
|
|
245 (found nil))
|
16473
|
246 (while (and (not found)
|
|
247 (not (eobp)))
|
|
248 (forward-paragraph 1)
|
|
249 (setq end (point))
|
|
250 (setq found
|
|
251 (refer-every (function (lambda (keyword)
|
|
252 (goto-char begin)
|
|
253 (re-search-forward keyword end t)))
|
|
254 keywords-list))
|
|
255 (if (not found)
|
|
256 (progn
|
|
257 (setq begin end)
|
|
258 (goto-char begin))))
|
|
259 (if found
|
|
260 (progn (goto-char begin)
|
|
261 (re-search-forward "\\W" nil t)
|
|
262 (message "Scanning %s... found" file))
|
|
263 (progn (message "Scanning %s... not found" file)
|
|
264 nil))))
|
7638
|
265
|
12952
|
266 (defun refer-every (pred l)
|
16473
|
267 (cond ((null l) nil)
|
|
268 ((funcall pred (car l))
|
|
269 (or (null (cdr l))
|
|
270 (refer-every pred (cdr l))))))
|
7638
|
271
|
12952
|
272 (defun refer-convert-string-to-list-of-strings (s)
|
7638
|
273 (let ((current (current-buffer))
|
|
274 (temp-buffer (get-buffer-create "*refer-temp*")))
|
|
275 (set-buffer temp-buffer)
|
|
276 (erase-buffer)
|
|
277 (insert (regexp-quote s))
|
|
278 (goto-char (point-min))
|
|
279 (insert "(\"")
|
|
280 (while (re-search-forward "[ \t]+" nil t)
|
|
281 (replace-match "\" \"" t t))
|
|
282 (goto-char (point-max))
|
|
283 (insert "\")")
|
|
284 (goto-char (point-min))
|
|
285 (prog1 (read temp-buffer)
|
|
286 (set-buffer current))))
|
|
287
|
16473
|
288 (defun refer-expand-files (file-list dir-list)
|
|
289 (let (file files dir dirs)
|
|
290 (while (setq file (car file-list))
|
|
291 (setq dirs (copy-alist dir-list))
|
|
292 (while (setq dir (car dirs))
|
|
293 (if (file-exists-p (expand-file-name file dir))
|
|
294 (setq files (append files (list (expand-file-name file dir)))
|
|
295 dirs nil)
|
|
296 (if (file-exists-p (expand-file-name (concat file ".bib") dir))
|
|
297 (setq files (append files (list (expand-file-name (concat file ".bib")
|
|
298 dir)))
|
|
299 dirs nil)
|
|
300 (setq dirs (cdr dirs)))))
|
|
301 (setq file-list (cdr file-list)))
|
|
302 files))
|
|
303
|
7638
|
304 (defun refer-get-bib-files ()
|
16473
|
305 (let* ((dir-list
|
|
306 (cond
|
|
307 ((null refer-bib-directory)
|
|
308 '("."))
|
|
309 ((or (eq refer-bib-directory 'texinputs)
|
|
310 (eq refer-bib-directory 'bibinputs))
|
|
311 (let ((envvar (getenv (if (eq refer-bib-directory 'texinputs)
|
|
312 "TEXINPUTS"
|
|
313 "BIBINPUTS")))
|
|
314 (dirs nil))
|
|
315 (if (null envvar)
|
|
316 (setq envvar "."))
|
|
317 (while (string-match ":" envvar)
|
|
318 (let ((dir (substring envvar 0 (match-beginning 0))))
|
|
319 (if (and (not (string-equal "" dir))
|
|
320 (file-directory-p dir))
|
|
321 (setq dirs (append (list (expand-file-name dir nil))
|
|
322 dirs))))
|
|
323 (setq envvar (substring envvar (match-end 0))))
|
|
324 (if (and (not (string-equal "" envvar))
|
|
325 (file-directory-p envvar))
|
|
326 (setq dirs (append (list envvar) dirs)))
|
|
327 (setq dirs (nreverse dirs))))
|
|
328 ((listp refer-bib-directory)
|
|
329 refer-bib-directory)
|
|
330 (t
|
|
331 (list refer-bib-directory))))
|
|
332 (files
|
|
333 (cond
|
|
334 ((null refer-bib-files)
|
|
335 (list (expand-file-name
|
|
336 (if (eq major-mode 'bibtex-mode)
|
|
337 (read-file-name
|
|
338 (format ".bib file: (default %s) "
|
|
339 (file-name-nondirectory
|
|
340 (buffer-file-name)))
|
|
341 (file-name-directory (buffer-file-name))
|
|
342 (file-name-nondirectory (buffer-file-name))
|
|
343 t)
|
|
344 (read-file-name ".bib file: " nil nil t)))))
|
|
345 ((eq refer-bib-files 'auto)
|
|
346 (let ((files
|
|
347 (save-excursion
|
|
348 (if (setq refer-same-file (eq major-mode 'bibtex-mode))
|
|
349 (list buffer-file-name)
|
|
350 (if (progn
|
|
351 (goto-char (point-min))
|
|
352 (re-search-forward (concat refer-bib-files-regexp
|
|
353 "\\s-*\{") nil t))
|
|
354 (let ((files (list (buffer-substring
|
|
355 (point)
|
|
356 (progn
|
|
357 (re-search-forward "[,\}]"
|
|
358 nil t)
|
|
359 (backward-char 1)
|
|
360 (point))))))
|
|
361 (while (not (looking-at "\}"))
|
|
362 (setq files (append files
|
|
363 (list (buffer-substring
|
|
364 (progn (forward-char 1)
|
|
365 (point))
|
|
366 (progn (re-search-forward
|
|
367 "[,\}]" nil t)
|
|
368 (backward-char 1)
|
|
369 (point)))))))
|
|
370 files)
|
|
371 (error (concat "No \\\\bibliography command in this "
|
|
372 "buffer, can't read refer-bib-files")))))))
|
|
373 (refer-expand-files files dir-list)))
|
|
374 ((eq refer-bib-files 'dir)
|
|
375 (let ((dirs (nreverse dir-list))
|
|
376 dir files)
|
|
377 (while (setq dir (car dirs))
|
|
378 (setq files
|
|
379 (append (directory-files dir t "\\.bib$")
|
|
380 files))
|
|
381 (setq dirs (cdr dirs)))
|
|
382 files))
|
|
383 ((and (listp refer-bib-files)
|
|
384 (or (eq refer-bib-directory 'texinputs)
|
|
385 (eq refer-bib-directory 'bibinputs)))
|
|
386 (refer-expand-files refer-bib-files dir-list))
|
|
387 ((listp refer-bib-files) refer-bib-files)
|
|
388 (t (error "Illegal value for refer-bib-files: %s"
|
|
389 refer-bib-files)))))
|
|
390 (if (or (eq refer-bib-directory 'texinputs)
|
|
391 (eq refer-bib-directory 'bibinputs))
|
|
392 (setq refer-bib-directory dir-list))
|
|
393 (if refer-cache-bib-files
|
|
394 (setq refer-bib-files files))
|
|
395 files))
|
7638
|
396
|
|
397 ;;; refer.el ends here
|
16473
|
398
|