| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
(provide 'refer) |
|---|
| 75 |
|
|---|
| 76 |
(defgroup refer nil |
|---|
| 77 |
"Look up references in bibliography files." |
|---|
| 78 |
:prefix "refer-" |
|---|
| 79 |
:group 'wp) |
|---|
| 80 |
|
|---|
| 81 |
(defcustom refer-bib-directory nil |
|---|
| 82 |
"Directory, or list of directories, to search for \\.bib files. |
|---|
| 83 |
Can be set to 'bibinputs or 'texinputs, in which case the environment |
|---|
| 84 |
variable BIBINPUTS or TEXINPUTS, respectively, is used to obtain a |
|---|
| 85 |
list of directories. Useful only if `refer-bib-files' is set to 'dir or |
|---|
| 86 |
a list of file names (without directory). A value of nil indicates the |
|---|
| 87 |
current working directory. |
|---|
| 88 |
|
|---|
| 89 |
If `refer-bib-directory' is 'bibinputs or 'texinputs, it is setq'd to |
|---|
| 90 |
the appropriate list of directories when it is first used. |
|---|
| 91 |
|
|---|
| 92 |
Note that an empty directory is interpreted by BibTeX as indicating |
|---|
| 93 |
the default search path. Since Refer does not know that default path, |
|---|
| 94 |
it cannot search it. Include that path explicitly in your BIBINPUTS |
|---|
| 95 |
environment if you really want it searched (which is not likely to |
|---|
| 96 |
happen anyway)." |
|---|
| 97 |
:type '(choice (repeat directory) (const bibinputs) (const texinputs)) |
|---|
| 98 |
:group 'refer) |
|---|
| 99 |
|
|---|
| 100 |
(defcustom refer-bib-files 'dir |
|---|
| 101 |
"*List of \\.bib files to search for references, |
|---|
| 102 |
or one of the following special values: |
|---|
| 103 |
nil = prompt for \\.bib file (if visiting a \\.bib file, use it as default) |
|---|
| 104 |
auto = read \\.bib file names from appropriate command in buffer (see |
|---|
| 105 |
`refer-bib-files-regexp') unless the buffer's mode is `bibtex-mode', |
|---|
| 106 |
in which case only the buffer is searched |
|---|
| 107 |
dir = use all \\.bib files in directories referenced by `refer-bib-directory'. |
|---|
| 108 |
|
|---|
| 109 |
If a specified file doesn't exist and has no extension, a \\.bib extension |
|---|
| 110 |
is automatically tried. |
|---|
| 111 |
|
|---|
| 112 |
If `refer-bib-files' is nil, auto or dir, it is setq'd to the appropriate |
|---|
| 113 |
list of files when it is first used if `refer-cache-bib-files' is t. If |
|---|
| 114 |
`refer-cache-bib-files' is nil, the list of \\.bib files to use is re-read |
|---|
| 115 |
each time it is needed." |
|---|
| 116 |
:type '(choice (repeat file) (const nil) (const auto) (const dir)) |
|---|
| 117 |
:group 'refer) |
|---|
| 118 |
|
|---|
| 119 |
(defcustom refer-cache-bib-files t |
|---|
| 120 |
"*Variable determining whether the value of `refer-bib-files' should be cached. |
|---|
| 121 |
If t, initialize the value of refer-bib-files the first time it is used. If |
|---|
| 122 |
nil, re-read the list of \\.bib files depending on the value of `refer-bib-files' |
|---|
| 123 |
each time it is needed." |
|---|
| 124 |
:type 'boolean |
|---|
| 125 |
:group 'refer) |
|---|
| 126 |
|
|---|
| 127 |
(defcustom refer-bib-files-regexp "\\\\bibliography" |
|---|
| 128 |
"*Regexp matching a bibliography file declaration. |
|---|
| 129 |
The current buffer is expected to contain a line such as |
|---|
| 130 |
\\bibliography{file1,file2,file3} |
|---|
| 131 |
which is read to set up `refer-bib-files'. The regexp must specify the command |
|---|
| 132 |
\(such as \\bibliography) that is used to specify the list of bib files. The |
|---|
| 133 |
command is expected to specify a file name, or a list of comma-separated file |
|---|
| 134 |
names, within curly braces. |
|---|
| 135 |
If a specified file doesn't exist and has no extension, a \\.bib extension |
|---|
| 136 |
is automatically tried." |
|---|
| 137 |
:type 'regexp |
|---|
| 138 |
:group 'refer) |
|---|
| 139 |
|
|---|
| 140 |
(make-variable-buffer-local 'refer-bib-files) |
|---|
| 141 |
(make-variable-buffer-local 'refer-cache-bib-files) |
|---|
| 142 |
(make-variable-buffer-local 'refer-bib-directory) |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
(defvar refer-saved-state nil) |
|---|
| 146 |
(defvar refer-previous-keywords nil) |
|---|
| 147 |
(defvar refer-saved-pos nil) |
|---|
| 148 |
(defvar refer-same-file nil) |
|---|
| 149 |
|
|---|
| 150 |
(defun refer-find-entry (keywords &optional continue) |
|---|
| 151 |
"Find entry in refer-bib-files containing KEYWORDS. |
|---|
| 152 |
If KEYWORDS is nil, prompt user for blank-separated list of keywords. |
|---|
| 153 |
If CONTINUE is non-nil, or if called interactively with a prefix arg, |
|---|
| 154 |
look for next entry by continuing search from previous point." |
|---|
| 155 |
(interactive (list nil current-prefix-arg)) |
|---|
| 156 |
(or keywords (setq keywords (if continue |
|---|
| 157 |
refer-previous-keywords |
|---|
| 158 |
(read-string "Keywords: ")))) |
|---|
| 159 |
(setq refer-previous-keywords keywords) |
|---|
| 160 |
(refer-find-entry-internal keywords continue)) |
|---|
| 161 |
|
|---|
| 162 |
(defun refer-find-next-entry () |
|---|
| 163 |
"Find next occurrence of entry in `refer-bib-files'. See `refer-find-entry'." |
|---|
| 164 |
(interactive) |
|---|
| 165 |
(refer-find-entry-internal refer-previous-keywords t)) |
|---|
| 166 |
|
|---|
| 167 |
(defun refer-yank-key () |
|---|
| 168 |
"Inserts at point in current buffer the \"key\" field of the entry |
|---|
| 169 |
found on the last `refer-find-entry' or `refer-find-next-entry'." |
|---|
| 170 |
(interactive) |
|---|
| 171 |
(let ((old-point (point))) |
|---|
| 172 |
(insert |
|---|
| 173 |
(save-window-excursion |
|---|
| 174 |
(save-excursion |
|---|
| 175 |
(find-file (car refer-saved-state)) |
|---|
| 176 |
(if (looking-at |
|---|
| 177 |
"[ \t\n]*@\\s-*[a-zA-Z][a-zA-Z0-9]*\\s-*{\\s-*\\([^ \t\n,]+\\)\\s-*,") |
|---|
| 178 |
(buffer-substring (match-beginning 1) (match-end 1)) |
|---|
| 179 |
(error "Cannot find key for entry in file %s" |
|---|
| 180 |
(car refer-saved-state)))))) |
|---|
| 181 |
(if (not (= (point) old-point)) |
|---|
| 182 |
(set-mark old-point)))) |
|---|
| 183 |
|
|---|
| 184 |
(defun refer-find-entry-internal (keywords continue) |
|---|
| 185 |
(let ((keywords-list (refer-convert-string-to-list-of-strings keywords)) |
|---|
| 186 |
(old-buffer (current-buffer)) |
|---|
| 187 |
(old-window (selected-window)) |
|---|
| 188 |
(new-window (selected-window)) |
|---|
| 189 |
(files (if continue |
|---|
| 190 |
refer-saved-state |
|---|
| 191 |
(setq refer-saved-pos nil) |
|---|
| 192 |
(refer-get-bib-files))) |
|---|
| 193 |
(n 0) |
|---|
| 194 |
(found nil) |
|---|
| 195 |
(file nil)) |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
(setq new-window |
|---|
| 200 |
(get-window-with-predicate |
|---|
| 201 |
(lambda (w) |
|---|
| 202 |
(while (and (not (null (setq file (nth n files)))) |
|---|
| 203 |
(setq n (1+ n)) |
|---|
| 204 |
(not (string-equal file |
|---|
| 205 |
(buffer-file-name |
|---|
| 206 |
(window-buffer w)))))) |
|---|
| 207 |
file))) |
|---|
| 208 |
(unless new-window |
|---|
| 209 |
|
|---|
| 210 |
(when (one-window-p 'nomini) |
|---|
| 211 |
(setq old-window (split-window))) |
|---|
| 212 |
(setq new-window (next-window old-window 'nomini))) |
|---|
| 213 |
(select-window (if refer-same-file |
|---|
| 214 |
old-window |
|---|
| 215 |
new-window)) |
|---|
| 216 |
(catch 'found |
|---|
| 217 |
(while files |
|---|
| 218 |
(let ((file (cond ((file-exists-p (car files)) (car files)) |
|---|
| 219 |
((file-exists-p (concat (car files) ".bib")) |
|---|
| 220 |
(concat (car files) ".bib"))))) |
|---|
| 221 |
(setq refer-saved-state files) |
|---|
| 222 |
(if file |
|---|
| 223 |
(if (refer-find-entry-in-file keywords-list file refer-saved-pos) |
|---|
| 224 |
(progn |
|---|
| 225 |
(setq refer-saved-pos (point)) |
|---|
| 226 |
(recenter 0) |
|---|
| 227 |
(throw 'found (find-file file))) |
|---|
| 228 |
(setq refer-saved-pos nil |
|---|
| 229 |
files (cdr files))) |
|---|
| 230 |
(progn (ding) |
|---|
| 231 |
(message "Scanning %s... No such file" (car files)) |
|---|
| 232 |
(sit-for 1) |
|---|
| 233 |
(setq files (cdr files)))))) |
|---|
| 234 |
(ding) |
|---|
| 235 |
(message "Keywords \"%s\" not found in any \.bib file" keywords)) |
|---|
| 236 |
(select-window old-window))) |
|---|
| 237 |
|
|---|
| 238 |
(defun refer-find-entry-in-file (keywords-list file &optional old-pos) |
|---|
| 239 |
(message "Scanning %s..." file) |
|---|
| 240 |
(expand-file-name file) |
|---|
| 241 |
(set-buffer (find-file-noselect file)) |
|---|
| 242 |
(find-file file) |
|---|
| 243 |
(if (not old-pos) |
|---|
| 244 |
(goto-char (point-min)) |
|---|
| 245 |
(goto-char old-pos) |
|---|
| 246 |
(forward-paragraph 1)) |
|---|
| 247 |
(let ((begin (point)) |
|---|
| 248 |
(end 0) |
|---|
| 249 |
(found nil)) |
|---|
| 250 |
(while (and (not found) |
|---|
| 251 |
(not (eobp))) |
|---|
| 252 |
(forward-paragraph 1) |
|---|
| 253 |
(setq end (point)) |
|---|
| 254 |
(setq found |
|---|
| 255 |
(refer-every (function (lambda (keyword) |
|---|
| 256 |
(goto-char begin) |
|---|
| 257 |
(re-search-forward keyword end t))) |
|---|
| 258 |
keywords-list)) |
|---|
| 259 |
(if (not found) |
|---|
| 260 |
(progn |
|---|
| 261 |
(setq begin end) |
|---|
| 262 |
(goto-char begin)))) |
|---|
| 263 |
(if found |
|---|
| 264 |
(progn (goto-char begin) |
|---|
| 265 |
(re-search-forward "\\W" nil t) |
|---|
| 266 |
(message "Scanning %s... found" file)) |
|---|
| 267 |
(progn (message "Scanning %s... not found" file) |
|---|
| 268 |
nil)))) |
|---|
| 269 |
|
|---|
| 270 |
(defun refer-every (pred l) |
|---|
| 271 |
(cond ((null l) nil) |
|---|
| 272 |
((funcall pred (car l)) |
|---|
| 273 |
(or (null (cdr l)) |
|---|
| 274 |
(refer-every pred (cdr l)))))) |
|---|
| 275 |
|
|---|
| 276 |
(defun refer-convert-string-to-list-of-strings (s) |
|---|
| 277 |
(let ((current (current-buffer)) |
|---|
| 278 |
(temp-buffer (get-buffer-create "*refer-temp*"))) |
|---|
| 279 |
(set-buffer temp-buffer) |
|---|
| 280 |
(erase-buffer) |
|---|
| 281 |
(insert (regexp-quote s)) |
|---|
| 282 |
(goto-char (point-min)) |
|---|
| 283 |
(insert "(\"") |
|---|
| 284 |
(while (re-search-forward "[ \t]+" nil t) |
|---|
| 285 |
(replace-match "\" \"" t t)) |
|---|
| 286 |
(goto-char (point-max)) |
|---|
| 287 |
(insert "\")") |
|---|
| 288 |
(goto-char (point-min)) |
|---|
| 289 |
(prog1 (read temp-buffer) |
|---|
| 290 |
(set-buffer current)))) |
|---|
| 291 |
|
|---|
| 292 |
(defun refer-expand-files (file-list dir-list) |
|---|
| 293 |
(let (file files dir dirs) |
|---|
| 294 |
(while (setq file (car file-list)) |
|---|
| 295 |
(setq dirs (copy-alist dir-list)) |
|---|
| 296 |
(while (setq dir (car dirs)) |
|---|
| 297 |
(if (file-exists-p (expand-file-name file dir)) |
|---|
| 298 |
(setq files (append files (list (expand-file-name file dir))) |
|---|
| 299 |
dirs nil) |
|---|
| 300 |
(if (file-exists-p (expand-file-name (concat file ".bib") dir)) |
|---|
| 301 |
(setq files (append files (list (expand-file-name (concat file ".bib") |
|---|
| 302 |
dir))) |
|---|
| 303 |
dirs nil) |
|---|
| 304 |
(setq dirs (cdr dirs))))) |
|---|
| 305 |
(setq file-list (cdr file-list))) |
|---|
| 306 |
files)) |
|---|
| 307 |
|
|---|
| 308 |
(defun refer-get-bib-files () |
|---|
| 309 |
(let* ((dir-list |
|---|
| 310 |
(cond |
|---|
| 311 |
((null refer-bib-directory) |
|---|
| 312 |
'(".")) |
|---|
| 313 |
((or (eq refer-bib-directory 'texinputs) |
|---|
| 314 |
(eq refer-bib-directory 'bibinputs)) |
|---|
| 315 |
(let ((envvar (getenv (if (eq refer-bib-directory 'texinputs) |
|---|
| 316 |
"TEXINPUTS" |
|---|
| 317 |
"BIBINPUTS"))) |
|---|
| 318 |
(dirs nil)) |
|---|
| 319 |
(if (null envvar) |
|---|
| 320 |
(setq envvar ".")) |
|---|
| 321 |
(while (string-match ":" envvar) |
|---|
| 322 |
(let ((dir (substring envvar 0 (match-beginning 0)))) |
|---|
| 323 |
(if (and (not (string-equal "" dir)) |
|---|
| 324 |
(file-directory-p dir)) |
|---|
| 325 |
(setq dirs (append (list (expand-file-name dir nil)) |
|---|
| 326 |
dirs)))) |
|---|
| 327 |
(setq envvar (substring envvar (match-end 0)))) |
|---|
| 328 |
(if (and (not (string-equal "" envvar)) |
|---|
| 329 |
(file-directory-p envvar)) |
|---|
| 330 |
(setq dirs (append (list envvar) dirs))) |
|---|
| 331 |
(setq dirs (nreverse dirs)))) |
|---|
| 332 |
((listp refer-bib-directory) |
|---|
| 333 |
refer-bib-directory) |
|---|
| 334 |
(t |
|---|
| 335 |
(list refer-bib-directory)))) |
|---|
| 336 |
(files |
|---|
| 337 |
(cond |
|---|
| 338 |
((null refer-bib-files) |
|---|
| 339 |
(list (expand-file-name |
|---|
| 340 |
(if (eq major-mode 'bibtex-mode) |
|---|
| 341 |
(read-file-name |
|---|
| 342 |
(format ".bib file (default %s): " |
|---|
| 343 |
(file-name-nondirectory |
|---|
| 344 |
(buffer-file-name))) |
|---|
| 345 |
(file-name-directory (buffer-file-name)) |
|---|
| 346 |
(file-name-nondirectory (buffer-file-name)) |
|---|
| 347 |
t) |
|---|
| 348 |
(read-file-name ".bib file: " nil nil t))))) |
|---|
| 349 |
((eq refer-bib-files 'auto) |
|---|
| 350 |
(let ((files |
|---|
| 351 |
(save-excursion |
|---|
| 352 |
(if (setq refer-same-file (eq major-mode 'bibtex-mode)) |
|---|
| 353 |
(list buffer-file-name) |
|---|
| 354 |
(if (progn |
|---|
| 355 |
(goto-char (point-min)) |
|---|
| 356 |
(re-search-forward (concat refer-bib-files-regexp |
|---|
| 357 |
"\\s-*\{") nil t)) |
|---|
| 358 |
(let ((files (list (buffer-substring |
|---|
| 359 |
(point) |
|---|
| 360 |
(progn |
|---|
| 361 |
(re-search-forward "[,\}]" |
|---|
| 362 |
nil t) |
|---|
| 363 |
(backward-char 1) |
|---|
| 364 |
(point)))))) |
|---|
| 365 |
(while (not (looking-at "\}")) |
|---|
| 366 |
(setq files (append files |
|---|
| 367 |
(list (buffer-substring |
|---|
| 368 |
(progn (forward-char 1) |
|---|
| 369 |
(point)) |
|---|
| 370 |
(progn (re-search-forward |
|---|
| 371 |
"[,\}]" nil t) |
|---|
| 372 |
(backward-char 1) |
|---|
| 373 |
(point))))))) |
|---|
| 374 |
files) |
|---|
| 375 |
(error (concat "No \\\\bibliography command in this " |
|---|
| 376 |
"buffer, can't read refer-bib-files"))))))) |
|---|
| 377 |
(refer-expand-files files dir-list))) |
|---|
| 378 |
((eq refer-bib-files 'dir) |
|---|
| 379 |
(let ((dirs (nreverse dir-list)) |
|---|
| 380 |
dir files) |
|---|
| 381 |
(while (setq dir (car dirs)) |
|---|
| 382 |
(setq files |
|---|
| 383 |
(append (directory-files dir t "\\.bib$") |
|---|
| 384 |
files)) |
|---|
| 385 |
(setq dirs (cdr dirs))) |
|---|
| 386 |
files)) |
|---|
| 387 |
((and (listp refer-bib-files) |
|---|
| 388 |
(or (eq refer-bib-directory 'texinputs) |
|---|
| 389 |
(eq refer-bib-directory 'bibinputs))) |
|---|
| 390 |
(refer-expand-files refer-bib-files dir-list)) |
|---|
| 391 |
((listp refer-bib-files) refer-bib-files) |
|---|
| 392 |
(t (error "Invalid value for refer-bib-files: %s" |
|---|
| 393 |
refer-bib-files))))) |
|---|
| 394 |
(if (or (eq refer-bib-directory 'texinputs) |
|---|
| 395 |
(eq refer-bib-directory 'bibinputs)) |
|---|
| 396 |
(setq refer-bib-directory dir-list)) |
|---|
| 397 |
(if refer-cache-bib-files |
|---|
| 398 |
(setq refer-bib-files files)) |
|---|
| 399 |
files)) |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|