* frame.c (x_fullscreen_adjust): Declare var as Display_Info.
[emacs.git] / lisp / textmodes / refer.el
blob87a1d39a74679655ad96a80d60eea5e01a14ab7d
1 ;;; refer.el --- look up references in bibliography files
3 ;; Copyright (C) 1992, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Ashwin Ram <ashwin@cc.gatech.edu>
7 ;; Maintainer: Gernot Heiser <gernot@acm.org>
8 ;; Adapted-By: ESR
9 ;; Keywords: bib
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
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.
33 ;; To use:
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 ;; 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).
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.
53 ;; If the list of bibliography files changes, reinitialize the variable
54 ;; refer-bib-files.
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.
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.
70 ;;; Code:
72 (provide 'refer)
74 (defgroup refer nil
75 "Look up references in bibliography files."
76 :prefix "refer-"
77 :group 'wp)
79 (defcustom refer-bib-directory nil
80 "Directory, or list of directories, to search for \\.bib files.
81 Can be set to 'bibinputs or 'texinputs, in which case the environment
82 variable BIBINPUTS or TEXINPUTS, respectively, is used to obtain a
83 list of directories. Useful only if `refer-bib-files' is set to 'dir or
84 a list of file names (without directory). A value of nil indicates the
85 current working directory.
87 If `refer-bib-directory' is 'bibinputs or 'texinputs, it is setq'd to
88 the appropriate list of directories when it is first used.
90 Note that an empty directory is interpreted by BibTeX as indicating
91 the default search path. Since Refer does not know that default path,
92 it cannot search it. Include that path explicitly in your BIBINPUTS
93 environment if you really want it searched (which is not likely to
94 happen anyway)."
95 :type '(choice (repeat directory) (const bibinputs) (const texinputs))
96 :group 'refer)
98 (defcustom refer-bib-files 'dir
99 "*List of \\.bib files to search for references,
100 or one of the following special values:
101 nil = prompt for \\.bib file (if visiting a \\.bib file, use it as default)
102 auto = read \\.bib file names from appropriate command in buffer (see
103 `refer-bib-files-regexp') unless the buffer's mode is `bibtex-mode',
104 in which case only the buffer is searched
105 dir = use all \\.bib files in directories referenced by `refer-bib-directory'.
107 If a specified file doesn't exist and has no extension, a \\.bib extension
108 is automatically tried.
110 If `refer-bib-files' is nil, auto or dir, it is setq'd to the appropriate
111 list 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
113 each time it is needed."
114 :type '(choice (repeat file) (const nil) (const auto) (const dir))
115 :group 'refer)
117 (defcustom refer-cache-bib-files t
118 "*Variable determining whether the value of `refer-bib-files' should be cached.
119 If t, initialize the value of refer-bib-files the first time it is used. If
120 nil, re-read the list of \\.bib files depending on the value of `refer-bib-files'
121 each time it is needed."
122 :type 'boolean
123 :group 'refer)
125 (defcustom refer-bib-files-regexp "\\\\bibliography"
126 "*Regexp matching a bibliography file declaration.
127 The current buffer is expected to contain a line such as
128 \\bibliography{file1,file2,file3}
129 which 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
131 command is expected to specify a file name, or a list of comma-separated file
132 names, within curly braces.
133 If a specified file doesn't exist and has no extension, a \\.bib extension
134 is automatically tried."
135 :type 'regexp
136 :group 'refer)
138 (make-variable-buffer-local 'refer-bib-files)
139 (make-variable-buffer-local 'refer-cache-bib-files)
140 (make-variable-buffer-local 'refer-bib-directory)
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)
148 (defun refer-find-entry (keywords &optional continue)
149 "Find entry in refer-bib-files containing KEYWORDS.
150 If KEYWORDS is nil, prompt user for blank-separated list of keywords.
151 If CONTINUE is non-nil, or if called interactively with a prefix arg,
152 look for next entry by continuing search from previous point."
153 (interactive (list nil current-prefix-arg))
154 (or keywords (setq keywords (if continue
155 refer-previous-keywords
156 (read-string "Keywords: "))))
157 (setq refer-previous-keywords keywords)
158 (refer-find-entry-internal keywords continue))
160 (defun refer-find-next-entry ()
161 "Find next occurrence of entry in `refer-bib-files'. See `refer-find-entry'."
162 (interactive)
163 (refer-find-entry-internal refer-previous-keywords t))
165 (defun refer-yank-key ()
166 "Inserts at point in current buffer the \"key\" field of the entry
167 found on the last `refer-find-entry' or `refer-find-next-entry'."
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))
177 (error "Cannot find key for entry in file %s"
178 (car refer-saved-state))))))
179 (if (not (= (point) old-point))
180 (set-mark old-point))))
182 (defun refer-find-entry-internal (keywords continue)
183 (let ((keywords-list (refer-convert-string-to-list-of-strings keywords))
184 (old-buffer (current-buffer))
185 (old-window (selected-window))
186 (new-window (selected-window))
187 (files (if continue
188 refer-saved-state
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
197 (setq new-window
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)))
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)))
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 (ding)
229 (message "Scanning %s... No such file" (car files))
230 (sit-for 1)
231 (setq files (cdr files))))))
232 (ding)
233 (message "Keywords \"%s\" not found in any \.bib file" keywords))
234 (select-window old-window)))
236 (defun refer-find-entry-in-file (keywords-list file &optional old-pos)
237 (message "Scanning %s..." file)
238 (expand-file-name file)
239 (set-buffer (find-file-noselect file))
240 (find-file file)
241 (if (not old-pos)
242 (goto-char (point-min))
243 (goto-char old-pos)
244 (forward-paragraph 1))
245 (let ((begin (point))
246 (end 0)
247 (found nil))
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))))
268 (defun refer-every (pred l)
269 (cond ((null l) nil)
270 ((funcall pred (car l))
271 (or (null (cdr l))
272 (refer-every pred (cdr l))))))
274 (defun refer-convert-string-to-list-of-strings (s)
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))))
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))
306 (defun refer-get-bib-files ()
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)
333 (list refer-bib-directory))))
334 (files
335 (cond
336 ((null refer-bib-files)
337 (list (expand-file-name
338 (if (eq major-mode 'bibtex-mode)
339 (read-file-name
340 (format ".bib file (default %s): "
341 (file-name-nondirectory
342 (buffer-file-name)))
343 (file-name-directory (buffer-file-name))
344 (file-name-nondirectory (buffer-file-name))
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)
390 (t (error "Invalid value for refer-bib-files: %s"
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))
399 ;; arch-tag: 151f641b-e79b-462b-9a29-a95c3793f300
400 ;;; refer.el ends here