]> code.delx.au - gnu-emacs/blob - lisp/textmodes/refer.el
(describe-mouse-briefly): Fix message spelling.
[gnu-emacs] / lisp / textmodes / refer.el
1 ;;; refer.el --- look up references in bibliography files.
2
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
4
5 ;; Author: Ashwin Ram <Ram-Ashwin@cs.yale.edu>
6 ;; Adapted-By: ESR
7 ;; Keywords: bib
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26 ;;
27 ;; Functions to look up references in bibliography files given lists of
28 ;; keywords, similar to refer(1). I don't use tags since tags on .bib files
29 ;; only picks up the cite key, where as refer-find-entry looks for occurrences
30 ;; of keywords anywhere in the bibliography entry.
31 ;;
32 ;; To use:
33 ;; (autoload 'refer-find-entry "refer" nil t)
34 ;; or (require 'refer)
35 ;;
36 ;; To look for an article by Knuth about semaphores:
37 ;; Invoke refer-find-entry, then in response to the Keywords: prompt,
38 ;; say: Knuth semaphores (a blank-separated list of keywords to be used
39 ;; as search strings).
40 ;;
41 ;; To continue the previous search, i.e., to search for the next occurrence
42 ;; of the keywords, use refer-find-next-entry, or invoke refer-find-entry
43 ;; with a prefix argument.
44 ;;
45 ;; If the list of bibliography files changes, reinitialize the variable
46 ;; refer-bib-files.
47 ;;
48 ;; To customize:
49 ;; See variables refer-bib-files, refer-cache-bib-files and
50 ;; refer-bib-files-regexp. By default, these are set up so that refer
51 ;; looks for the keywords you specify in all the .bib files in the current
52 ;; directory.
53 ;;
54 ;; The only assumption I make about bib files is that they contain a bunch
55 ;; of entries, one to a paragraph. refer-find-entry searches paragraph by
56 ;; paragraph, looking for a paragraph containing all the keywords
57 ;; specified. So you should be able to use pretty much any bib file with
58 ;; this code. If your bib file does not use paragraphs to separate
59 ;; entries, try setting the paragraph-start/separate variables, or changing
60 ;; the (forward-paragraph 1) call in refer-find-entry-in-file.
61
62 ;;; ChangeLog:
63 ;;
64 ;; 01/08/89 Ashwin Ram <Ram-Ashwin@cs.yale.edu>
65 ;; Initial release.
66 ;;
67
68 ;;; Code:
69
70 (provide 'refer)
71
72 (defvar refer-bib-files 'dir
73 "*List of \\.bib files to search for references,
74 or one of the following special values:
75 nil = prompt for \\.bib file (if visiting a \\.bib file, use it as default)
76 auto = read \\.bib file names from appropriate command in buffer (see refer-bib-files-regexp)
77 dir = use all \\.bib files in current directory.
78
79 If a specified file doesn't exist and has no extension, a \\.bib extension
80 is automatically tried.
81
82 If refer-bib-files is nil, auto or dir, it is setq'd to the appropriate
83 list of files when it is first used if refer-cache-bib-files is t. If
84 refer-cache-bib-files is nil, the list of \\.bib files to use is re-read
85 each time it is needed.")
86
87 (defvar refer-cache-bib-files t
88 "*Variable determining whether the value of refer-bib-files should be cached.
89 If t, initialize the value of refer-bib-files the first time it is used. If
90 nil, re-read the list of \\.bib files depending on the value of refer-bib-files
91 each time it is needed.")
92
93 (defvar refer-bib-files-regexp "\\\\bibliography"
94 "*Regexp matching a bibliography file declaration.
95 The current buffer is expected to contain a line such as
96 \\bibliography{file1,file2,file3}
97 which is read to set up refer-bib-files. The regexp must specify the command
98 \(such as \\bibliography) that is used to specify the list of bib files. The
99 command is expected to specify a file name, or a list of comma-separated file
100 names, within curly braces.
101 If a specified file doesn't exist and has no extension, a \\.bib extension
102 is automatically tried.")
103
104 (make-variable-buffer-local 'refer-bib-files)
105 (make-variable-buffer-local 'refer-cache-bib-files)
106
107 (defun refer-find-entry (keywords &optional continue)
108 "Find entry in refer-bib-files containing KEYWORDS.
109 If KEYWORDS is nil, prompt user for blank-separated list of keywords.
110 If CONTINUE is t, or if called interactively with a prefix arg, look for next
111 entry by continuing search from previous point."
112 (interactive (list nil current-prefix-arg))
113 (or keywords (setq keywords (if continue
114 refer-previous-keywords
115 (read-string "Keywords: "))))
116 (setq refer-previous-keywords keywords)
117 (refer-find-entry-internal keywords continue))
118
119 (defun refer-find-next-entry ()
120 "Find next occurrence of entry in refer-bib-files. See refer-find-entry."
121 (interactive)
122 (refer-find-entry-internal refer-previous-keywords t))
123
124 (defun refer-find-entry-internal (keywords continue)
125 (let ((keywords-list (refer-convert-string-to-list-of-strings keywords))
126 (files (if continue
127 refer-saved-state
128 (refer-get-bib-files))))
129 (catch 'found
130 (while files
131 (let ((file (cond ((file-exists-p (car files)) (car files))
132 ((file-exists-p (concat (car files) ".bib")) (concat (car files) ".bib")))))
133 (setq refer-saved-state files)
134 (if file
135 (if (refer-find-entry-in-file keywords-list file continue)
136 (throw 'found (find-file file))
137 (setq files (cdr files)))
138 (progn (message "Scanning %s... No such file" (car files) (ding))
139 (sit-for 1)
140 (setq files (cdr files))))))
141 (message "Keywords \"%s\" not found in any \.bib file" keywords (ding)))))
142
143 (defun refer-find-entry-in-file (keywords-list file &optional continue)
144 (message "Scanning %s..." file) ; (expand-file-name file)
145 (set-buffer (find-file-noselect file))
146 (if continue
147 (forward-paragraph 1)
148 (goto-char (point-min)))
149 (let ((begin (point))
150 (end 0)
151 (found nil))
152 (while (and (not found)
153 (not (eobp)))
154 (forward-paragraph 1)
155 (setq end (point))
156 (setq found
157 (refer-every (function (lambda (keyword)
158 (goto-char begin)
159 (re-search-forward keyword end t)))
160 keywords-list))
161 (if (not found)
162 (progn
163 (setq begin end)
164 (goto-char begin))))
165 (if found
166 (progn (goto-char begin)
167 (re-search-forward "\\W" nil t)
168 (message "Scanning %s... found" file))
169 (progn (message "Scanning %s... not found" file)
170 nil))))
171
172 (defun refer-every (pred l)
173 (cond ((null l) nil)
174 ((funcall pred (car l))
175 (or (null (cdr l))
176 (refer-every pred (cdr l))))))
177
178 (defun refer-convert-string-to-list-of-strings (s)
179 (let ((current (current-buffer))
180 (temp-buffer (get-buffer-create "*refer-temp*")))
181 (set-buffer temp-buffer)
182 (erase-buffer)
183 (insert (regexp-quote s))
184 (goto-char (point-min))
185 (insert "(\"")
186 (while (re-search-forward "[ \t]+" nil t)
187 (replace-match "\" \"" t t))
188 (goto-char (point-max))
189 (insert "\")")
190 (goto-char (point-min))
191 (prog1 (read temp-buffer)
192 (set-buffer current))))
193
194 (defun refer-get-bib-files ()
195 (let ((files
196 (cond ((null refer-bib-files)
197 (list (expand-file-name
198 (if (eq major-mode 'bibtex-mode)
199 (read-file-name (format ".bib file: (default %s) " (file-name-nondirectory (buffer-file-name)))
200 (file-name-directory (buffer-file-name))
201 (file-name-nondirectory (buffer-file-name))
202 t)
203 (read-file-name ".bib file: " nil nil t)))))
204 ((listp refer-bib-files) refer-bib-files)
205 ((eq refer-bib-files 'auto)
206 (save-excursion
207 (if (progn (goto-char (point-min))
208 (re-search-forward (concat refer-bib-files-regexp "\{") nil t))
209 (let ((files (list (buffer-substring (point)
210 (progn (re-search-forward "[,\}]" nil t)
211 (backward-char 1)
212 (point))))))
213 (while (not (looking-at "\}"))
214 (setq files (append files
215 (list (buffer-substring (progn (forward-char 1)
216 (point))
217 (progn (re-search-forward "[,\}]" nil t)
218 (backward-char 1)
219 (point)))))))
220 files)
221 (error "No \\\\bibliography command in this buffer, can't read refer-bib-files"))))
222 ((eq refer-bib-files 'dir)
223 (directory-files "." t "\\.bib$"))
224 (t (error "Illegal value for refer-bib-files: %s" refer-bib-files)))))
225 (if refer-cache-bib-files
226 (setq refer-bib-files files))
227 files))
228
229 ;;; refer.el ends here