Commit | Line | Data |
---|---|---|
3afbc435 | 1 | ;;; reftex-cite.el --- creating citations with RefTeX |
f2e3589a | 2 | |
ba318903 | 3 | ;; Copyright (C) 1997-2014 Free Software Foundation, Inc. |
3ba2590f | 4 | |
6fbeb429 | 5 | ;; Author: Carsten Dominik <dominik@science.uva.nl> |
ce545621 | 6 | ;; Maintainer: auctex-devel@gnu.org |
3ba2590f RS |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
1fecc8fe | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3ba2590f | 11 | ;; it under the terms of the GNU General Public License as published by |
1fecc8fe GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
3ba2590f RS |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
1fecc8fe | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1a9461d0 | 22 | |
3afbc435 PJ |
23 | ;;; Commentary: |
24 | ||
25 | ;;; Code: | |
26 | ||
7c4d13cc | 27 | (eval-when-compile (require 'cl)) |
1a9461d0 | 28 | |
79d7167f | 29 | (require 'reftex) |
1a9461d0 | 30 | |
79d7167f TH |
31 | ;;; Variables and constants |
32 | (defvar reftex-cite-regexp-hist nil | |
33 | "The history list of regular expressions used for citations") | |
1a9461d0 | 34 | |
1a9461d0 | 35 | (defconst reftex-citation-prompt |
79d7167f TH |
36 | "Select: [n]ext [p]revious [r]estrict [ ]full_entry [q]uit RET [?]Help+more" |
37 | "Prompt and help string for citation selection") | |
1a9461d0 CD |
38 | |
39 | (defconst reftex-citation-help | |
40 | " n / p Go to next/previous entry (Cursor motion works as well). | |
41 | g / r Start over with new regexp / Refine with additional regexp. | |
42 | SPC Show full database entry in other window. | |
43 | f Toggle follow mode: Other window will follow with full db entry. | |
44 | . Show insertion point. | |
45 | q Quit without inserting \\cite macro into buffer. | |
46 | TAB Enter citation key with completion. | |
47 | RET Accept current entry (also on mouse-2) and create \\cite macro. | |
48 | m / u Mark/Unmark the entry. | |
f3c18bd0 | 49 | e / E Create BibTeX file with all (marked/unmarked) entries |
1a9461d0 CD |
50 | a / A Put all (marked) entries into one/many \\cite commands.") |
51 | ||
79d7167f | 52 | ;;; Find bibtex files |
6fbeb429 CD |
53 | (defmacro reftex-with-special-syntax-for-bib (&rest body) |
54 | `(let ((saved-syntax (syntax-table))) | |
55 | (unwind-protect | |
3666daf6 CD |
56 | (progn |
57 | (set-syntax-table reftex-syntax-table-for-bib) | |
58 | ,@body) | |
6fbeb429 CD |
59 | (set-syntax-table saved-syntax)))) |
60 | ||
1a9461d0 | 61 | (defun reftex-default-bibliography () |
79d7167f TH |
62 | "Return the expanded value of variable `reftex-default-bibliography'. |
63 | The expanded value is cached." | |
1a9461d0 | 64 | (unless (eq (get 'reftex-default-bibliography :reftex-raw) |
3666daf6 | 65 | reftex-default-bibliography) |
1a9461d0 | 66 | (put 'reftex-default-bibliography :reftex-expanded |
53964682 | 67 | (reftex-locate-bibliography-files |
3666daf6 | 68 | default-directory reftex-default-bibliography)) |
1a9461d0 | 69 | (put 'reftex-default-bibliography :reftex-raw |
3666daf6 | 70 | reftex-default-bibliography)) |
1a9461d0 CD |
71 | (get 'reftex-default-bibliography :reftex-expanded)) |
72 | ||
20cd3579 | 73 | (defun reftex-bib-or-thebib () |
79d7167f TH |
74 | "Test if BibTeX or \begin{thebibliography} should be used for the citation. |
75 | Find the bof of the current file" | |
20cd3579 | 76 | (let* ((docstruct (symbol-value reftex-docstruct-symbol)) |
3666daf6 CD |
77 | (rest (or (member (list 'bof (buffer-file-name)) docstruct) |
78 | docstruct)) | |
79 | (bib (assq 'bib rest)) | |
80 | (thebib (assq 'thebib rest)) | |
81 | (bibmem (memq bib rest)) | |
82 | (thebibmem (memq thebib rest))) | |
20cd3579 CD |
83 | (when (not (or thebib bib)) |
84 | (setq bib (assq 'bib docstruct) | |
3666daf6 CD |
85 | thebib (assq 'thebib docstruct) |
86 | bibmem (memq bib docstruct) | |
87 | thebibmem (memq thebib docstruct))) | |
20cd3579 | 88 | (if (> (length bibmem) (length thebibmem)) |
3666daf6 | 89 | (if bib 'bib nil) |
20cd3579 CD |
90 | (if thebib 'thebib nil)))) |
91 | ||
1a9461d0 | 92 | (defun reftex-get-bibfile-list () |
79d7167f TH |
93 | "Return list of bibfiles for current document. |
94 | When using the chapterbib or bibunits package you should either | |
95 | use the same database files everywhere, or separate parts using | |
96 | different databases into different files (included into the mater file). | |
97 | Then this function will return the applicable database files." | |
1a9461d0 CD |
98 | |
99 | ;; Ensure access to scanning info | |
100 | (reftex-access-scan-info) | |
101 | (or | |
102 | ;; Try inside this file (and its includes) | |
103 | (cdr (reftex-last-assoc-before-elt | |
104 | 'bib (list 'eof (buffer-file-name)) | |
105 | (member (list 'bof (buffer-file-name)) | |
106 | (symbol-value reftex-docstruct-symbol)))) | |
107 | ;; Try after the beginning of this file | |
108 | (cdr (assq 'bib (member (list 'bof (buffer-file-name)) | |
109 | (symbol-value reftex-docstruct-symbol)))) | |
110 | ;; Anywhere in the entire document | |
111 | (cdr (assq 'bib (symbol-value reftex-docstruct-symbol))) | |
112 | (error "\\bibliography statement missing or .bib files not found"))) | |
113 | ||
79d7167f | 114 | ;;; Find a certain reference in any of the BibTeX files. |
1a9461d0 | 115 | (defun reftex-pop-to-bibtex-entry (key file-list &optional mark-to-kill |
3666daf6 | 116 | highlight item return) |
79d7167f TH |
117 | "Find BibTeX KEY in any file in FILE-LIST in another window. |
118 | If MARK-TO-KILL is non-nil, mark new buffer to kill. | |
119 | If HIGHLIGHT is non-nil, highlight the match. | |
120 | If ITEM in non-nil, search for bibitem instead of database entry. | |
121 | If RETURN is non-nil, just return the entry and restore point." | |
1a9461d0 | 122 | (let* ((re |
53964682 | 123 | (if item |
4f595e15 RA |
124 | (concat "\\\\bibitem[ \t]*\\(\\[[^]]*\\]\\)?[ \t]*{" |
125 | (regexp-quote key) "}") | |
126 | (concat "@\\(?:\\w\\|\\s_\\)+[ \t\n\r]*[{(][ \t\n\r]*" | |
127 | (regexp-quote key) "[, \t\r\n}]"))) | |
3666daf6 | 128 | (buffer-conf (current-buffer)) |
759f7eed | 129 | file buf pos oldpos) |
1a9461d0 CD |
130 | |
131 | (catch 'exit | |
132 | (while file-list | |
133 | (setq file (car file-list) | |
134 | file-list (cdr file-list)) | |
135 | (unless (setq buf (reftex-get-file-buffer-force file mark-to-kill)) | |
136 | (error "No such file %s" file)) | |
137 | (set-buffer buf) | |
759f7eed | 138 | (setq oldpos (point)) |
1a9461d0 CD |
139 | (widen) |
140 | (goto-char (point-min)) | |
759f7eed CD |
141 | (if (not (re-search-forward re nil t)) |
142 | (goto-char oldpos) ;; restore previous position of point | |
1a9461d0 | 143 | (goto-char (match-beginning 0)) |
3666daf6 CD |
144 | (setq pos (point)) |
145 | (when return | |
146 | ;; Just return the relevant entry | |
147 | (if item (goto-char (match-end 0))) | |
53964682 | 148 | (setq return (buffer-substring |
3666daf6 | 149 | (point) (reftex-end-of-bib-entry item))) |
759f7eed | 150 | (goto-char oldpos) ;; restore point. |
3666daf6 CD |
151 | (set-buffer buffer-conf) |
152 | (throw 'exit return)) | |
153 | (switch-to-buffer-other-window buf) | |
154 | (goto-char pos) | |
1a9461d0 CD |
155 | (recenter 0) |
156 | (if highlight | |
157 | (reftex-highlight 0 (match-beginning 0) (match-end 0))) | |
158 | (throw 'exit (selected-window)))) | |
159 | (set-buffer buffer-conf) | |
160 | (if item | |
3666daf6 CD |
161 | (error "No \\bibitem with citation key %s" key) |
162 | (error "No BibTeX entry with citation key %s" key))))) | |
1a9461d0 CD |
163 | |
164 | (defun reftex-end-of-bib-entry (item) | |
53964682 | 165 | (save-excursion |
1a9461d0 | 166 | (condition-case nil |
53964682 | 167 | (if item |
3666daf6 CD |
168 | (progn (end-of-line) |
169 | (re-search-forward | |
170 | "\\\\bibitem\\|\\end{thebibliography}") | |
171 | (1- (match-beginning 0))) | |
172 | (progn (forward-list 1) (point))) | |
1a9461d0 CD |
173 | (error (min (point-max) (+ 300 (point))))))) |
174 | ||
79d7167f | 175 | ;;; Parse bibtex buffers |
3666daf6 | 176 | (defun reftex-extract-bib-entries (buffers) |
79d7167f TH |
177 | "Extract bib entries which match regexps from BUFFERS. |
178 | BUFFERS is a list of buffers or file names. | |
179 | Return list with entries." | |
3666daf6 CD |
180 | (let* (re-list first-re rest-re |
181 | (buffer-list (if (listp buffers) buffers (list buffers))) | |
182 | found-list entry buffer1 buffer alist | |
183 | key-point start-point end-point default) | |
184 | ||
185 | ;; Read a regexp, completing on known citation keys. | |
186 | (setq default (regexp-quote (reftex-get-bibkey-default))) | |
53964682 PE |
187 | (setq re-list |
188 | (split-string | |
189 | (completing-read | |
3666daf6 CD |
190 | (concat |
191 | "Regex { && Regex...}: " | |
192 | "[" default "]: ") | |
193 | (if reftex-mode | |
194 | (if (fboundp 'LaTeX-bibitem-list) | |
195 | (LaTeX-bibitem-list) | |
53964682 | 196 | (cdr (assoc 'bibview-cache |
3666daf6 CD |
197 | (symbol-value reftex-docstruct-symbol)))) |
198 | nil) | |
199 | nil nil nil 'reftex-cite-regexp-hist) | |
200 | "[ \t]*&&[ \t]*")) | |
201 | ||
202 | (if (or (null re-list ) (equal re-list '(""))) | |
203 | (setq re-list (list default))) | |
204 | ||
205 | (setq first-re (car re-list) ; We'll use the first re to find things, | |
206 | rest-re (cdr re-list)) ; the others to narrow down. | |
207 | (if (string-match "\\`[ \t]*\\'" (or first-re "")) | |
208 | (error "Empty regular expression")) | |
1a9461d0 CD |
209 | |
210 | (save-excursion | |
211 | (save-window-excursion | |
212 | ||
213 | ;; Walk through all bibtex files | |
214 | (while buffer-list | |
215 | (setq buffer (car buffer-list) | |
216 | buffer-list (cdr buffer-list)) | |
217 | (if (and (bufferp buffer) | |
218 | (buffer-live-p buffer)) | |
219 | (setq buffer1 buffer) | |
220 | (setq buffer1 (reftex-get-file-buffer-force | |
221 | buffer (not reftex-keep-temporary-buffers)))) | |
222 | (if (not buffer1) | |
223 | (message "No such BibTeX file %s (ignored)" buffer) | |
4f595e15 RA |
224 | (message "Scanning bibliography database %s" buffer1) |
225 | (unless (verify-visited-file-modtime buffer1) | |
226 | (when (y-or-n-p | |
227 | (format "File %s changed on disk. Reread from disk? " | |
228 | (file-name-nondirectory | |
229 | (buffer-file-name buffer1)))) | |
230 | (with-current-buffer buffer1 (revert-buffer t t))))) | |
1a9461d0 CD |
231 | |
232 | (set-buffer buffer1) | |
3666daf6 CD |
233 | (reftex-with-special-syntax-for-bib |
234 | (save-excursion | |
235 | (goto-char (point-min)) | |
236 | (while (re-search-forward first-re nil t) | |
237 | (catch 'search-again | |
238 | (setq key-point (point)) | |
4f595e15 RA |
239 | (unless (re-search-backward "\\(\\`\\|[\n\r]\\)[ \t]*\ |
240 | @\\(\\(?:\\w\\|\\s_\\)+\\)[ \t\n\r]*[{(]" nil t) | |
3666daf6 CD |
241 | (throw 'search-again nil)) |
242 | (setq start-point (point)) | |
243 | (goto-char (match-end 0)) | |
244 | (condition-case nil | |
245 | (up-list 1) | |
246 | (error (goto-char key-point) | |
1a9461d0 | 247 | (throw 'search-again nil))) |
3666daf6 | 248 | (setq end-point (point)) |
53964682 | 249 | |
3666daf6 CD |
250 | ;; Ignore @string, @comment and @c entries or things |
251 | ;; outside entries | |
252 | (when (or (string= (downcase (match-string 2)) "string") | |
253 | (string= (downcase (match-string 2)) "comment") | |
254 | (string= (downcase (match-string 2)) "c") | |
255 | (< (point) key-point)) ; this means match not in {} | |
256 | (goto-char key-point) | |
257 | (throw 'search-again nil)) | |
53964682 | 258 | |
3666daf6 CD |
259 | ;; Well, we have got a match |
260 | ;;(setq entry (concat | |
261 | ;; (buffer-substring start-point (point)) "\n")) | |
262 | (setq entry (buffer-substring start-point (point))) | |
53964682 | 263 | |
3666daf6 CD |
264 | ;; Check if other regexp match as well |
265 | (setq re-list rest-re) | |
266 | (while re-list | |
267 | (unless (string-match (car re-list) entry) | |
268 | ;; nope - move on | |
269 | (throw 'search-again nil)) | |
270 | (pop re-list)) | |
53964682 | 271 | |
3666daf6 CD |
272 | (setq alist (reftex-parse-bibtex-entry |
273 | nil start-point end-point)) | |
274 | (push (cons "&entry" entry) alist) | |
53964682 | 275 | |
3666daf6 CD |
276 | ;; check for crossref entries |
277 | (if (assoc "crossref" alist) | |
278 | (setq alist | |
279 | (append | |
280 | alist (reftex-get-crossref-alist alist)))) | |
53964682 | 281 | |
3666daf6 CD |
282 | ;; format the entry |
283 | (push (cons "&formatted" (reftex-format-bib-entry alist)) | |
284 | alist) | |
53964682 | 285 | |
3666daf6 CD |
286 | ;; make key the first element |
287 | (push (reftex-get-bib-field "&key" alist) alist) | |
53964682 | 288 | |
3666daf6 CD |
289 | ;; add it to the list |
290 | (push alist found-list))))) | |
291 | (reftex-kill-temporary-buffers)))) | |
1a9461d0 CD |
292 | (setq found-list (nreverse found-list)) |
293 | ||
294 | ;; Sorting | |
295 | (cond | |
296 | ((eq 'author reftex-sort-bibtex-matches) | |
297 | (sort found-list 'reftex-bib-sort-author)) | |
298 | ((eq 'year reftex-sort-bibtex-matches) | |
299 | (sort found-list 'reftex-bib-sort-year)) | |
300 | ((eq 'reverse-year reftex-sort-bibtex-matches) | |
301 | (sort found-list 'reftex-bib-sort-year-reverse)) | |
302 | (t found-list)))) | |
303 | ||
304 | (defun reftex-bib-sort-author (e1 e2) | |
79d7167f TH |
305 | "Compare bib entries E1 and E2 by author. |
306 | The name of the first different author/editor is used." | |
1a9461d0 CD |
307 | (let ((al1 (reftex-get-bib-names "author" e1)) |
308 | (al2 (reftex-get-bib-names "author" e2))) | |
309 | (while (and al1 al2 (string= (car al1) (car al2))) | |
310 | (pop al1) | |
311 | (pop al2)) | |
312 | (if (and (stringp (car al1)) | |
313 | (stringp (car al2))) | |
314 | (string< (car al1) (car al2)) | |
315 | (not (stringp (car al1)))))) | |
316 | ||
317 | (defun reftex-bib-sort-year (e1 e2) | |
79d7167f | 318 | "Compare bib entries E1 and E2 by year in ascending order." |
027a4b6b JB |
319 | (< (string-to-number (or (cdr (assoc "year" e1)) "0")) |
320 | (string-to-number (or (cdr (assoc "year" e2)) "0")))) | |
1a9461d0 CD |
321 | |
322 | (defun reftex-bib-sort-year-reverse (e1 e2) | |
79d7167f | 323 | "Compare bib entries E1 and E2 by year in descending order." |
027a4b6b JB |
324 | (> (string-to-number (or (cdr (assoc "year" e1)) "0")) |
325 | (string-to-number (or (cdr (assoc "year" e2)) "0")))) | |
1a9461d0 CD |
326 | |
327 | (defun reftex-get-crossref-alist (entry) | |
79d7167f | 328 | "Return the alist from a crossref ENTRY." |
1a9461d0 CD |
329 | (let ((crkey (cdr (assoc "crossref" entry))) |
330 | start) | |
331 | (save-excursion | |
332 | (save-restriction | |
333 | (widen) | |
334 | (if (re-search-forward | |
335 | (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey) | |
336 | "[ \t\n\r]*,") nil t) | |
337 | (progn | |
338 | (setq start (match-beginning 0)) | |
339 | (condition-case nil | |
340 | (up-list 1) | |
341 | (error nil)) | |
342 | (reftex-parse-bibtex-entry nil start (point))) | |
343 | nil))))) | |
344 | ||
aa87aafc | 345 | ;; Parse the bibliography environment |
3666daf6 | 346 | (defun reftex-extract-bib-entries-from-thebibliography (files) |
79d7167f TH |
347 | "Extract bib-entries from the \begin{thebibliography} environment. |
348 | Parsing is not as good as for the BibTeX database stuff. | |
349 | The environment should be located in FILES." | |
3666daf6 | 350 | (let* (start end buf entries re re-list file default) |
09308e63 | 351 | (unless files |
1a9461d0 | 352 | (error "Need file name to find thebibliography environment")) |
09308e63 | 353 | (while (setq file (pop files)) |
53964682 | 354 | (setq buf (reftex-get-file-buffer-force |
3666daf6 | 355 | file (not reftex-keep-temporary-buffers))) |
09308e63 | 356 | (unless buf |
3666daf6 | 357 | (error "No such file %s" file)) |
09308e63 | 358 | (message "Scanning thebibliography environment in %s" file) |
1a9461d0 | 359 | |
9a529312 | 360 | (with-current-buffer buf |
99f65cfa RA |
361 | (save-excursion |
362 | (save-restriction | |
363 | (widen) | |
364 | (goto-char (point-min)) | |
365 | (while (re-search-forward | |
366 | "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t) | |
367 | (beginning-of-line 2) | |
368 | (setq start (point)) | |
369 | (if (re-search-forward | |
370 | "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t) | |
371 | (progn | |
372 | (beginning-of-line 1) | |
373 | (setq end (point)))) | |
374 | (when (and start end) | |
375 | (setq entries | |
376 | (append entries | |
377 | (mapcar 'reftex-parse-bibitem | |
378 | (delete "" | |
379 | (split-string | |
380 | (buffer-substring-no-properties | |
381 | start end) | |
5d8f9169 | 382 | "[ \t\n\r]*\\\\bibitem[ \t]*\ |
99f65cfa RA |
383 | \\(\\[[^]]*]\\)*\[ \t]*")))))) |
384 | (goto-char end)))))) | |
1a9461d0 CD |
385 | (unless entries |
386 | (error "No bibitems found")) | |
387 | ||
3666daf6 CD |
388 | ;; Read a regexp, completing on known citation keys. |
389 | (setq default (regexp-quote (reftex-get-bibkey-default))) | |
53964682 PE |
390 | (setq re-list |
391 | (split-string | |
392 | (completing-read | |
3666daf6 CD |
393 | (concat |
394 | "Regex { && Regex...}: " | |
395 | "[" default "]: ") | |
396 | (if reftex-mode | |
397 | (if (fboundp 'LaTeX-bibitem-list) | |
398 | (LaTeX-bibitem-list) | |
53964682 | 399 | (cdr (assoc 'bibview-cache |
3666daf6 CD |
400 | (symbol-value reftex-docstruct-symbol)))) |
401 | nil) | |
402 | nil nil nil 'reftex-cite-regexp-hist) | |
403 | "[ \t]*&&[ \t]*")) | |
404 | ||
405 | (if (or (null re-list ) (equal re-list '(""))) | |
406 | (setq re-list (list default))) | |
407 | ||
408 | (if (string-match "\\`[ \t]*\\'" (car re-list)) | |
409 | (error "Empty regular expression")) | |
410 | ||
1a9461d0 | 411 | (while (and (setq re (pop re-list)) entries) |
53964682 | 412 | (setq entries |
3666daf6 CD |
413 | (delq nil (mapcar |
414 | (lambda (x) | |
415 | (if (string-match re (cdr (assoc "&entry" x))) | |
416 | x nil)) | |
417 | entries)))) | |
53964682 PE |
418 | (setq entries |
419 | (mapcar | |
3666daf6 CD |
420 | (lambda (x) |
421 | (push (cons "&formatted" (reftex-format-bibitem x)) x) | |
422 | (push (reftex-get-bib-field "&key" x) x) | |
423 | x) | |
424 | entries)) | |
1a9461d0 CD |
425 | |
426 | entries)) | |
427 | ||
7ac7387b | 428 | (defun reftex-get-bibkey-default () |
79d7167f TH |
429 | "Return the word before the cursor. |
430 | If the cursor is in a citation macro, return the word before the macro." | |
7ac7387b CD |
431 | (let* ((macro (reftex-what-macro 1))) |
432 | (save-excursion | |
433 | (if (and macro (string-match "cite" (car macro))) | |
3666daf6 | 434 | (goto-char (cdr macro))) |
7ac7387b CD |
435 | (skip-chars-backward "^a-zA-Z0-9") |
436 | (reftex-this-word)))) | |
437 | ||
79d7167f | 438 | ;;; Parse and format individual entries |
1a9461d0 | 439 | (defun reftex-get-bib-names (field entry) |
79d7167f TH |
440 | "Return a list with the author or editor names in ENTRY. |
441 | If FIELD is empty try \"editor\" field." | |
1a9461d0 CD |
442 | (let ((names (reftex-get-bib-field field entry))) |
443 | (if (equal "" names) | |
444 | (setq names (reftex-get-bib-field "editor" entry))) | |
445 | (while (string-match "\\band\\b[ \t]*" names) | |
446 | (setq names (replace-match "\n" nil t names))) | |
447 | (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names) | |
448 | (setq names (replace-match "" nil t names))) | |
449 | (while (string-match "^[ \t]+\\|[ \t]+$" names) | |
450 | (setq names (replace-match "" nil t names))) | |
451 | (while (string-match "[ \t][ \t]+" names) | |
452 | (setq names (replace-match " " nil t names))) | |
453 | (split-string names "\n"))) | |
454 | ||
4f595e15 | 455 | (defun reftex-parse-bibtex-entry (entry &optional from to raw) |
79d7167f TH |
456 | "Parse BibTeX ENTRY. |
457 | If ENTRY is nil then parse the entry in current buffer between FROM and TO. | |
458 | If RAW is non-nil, keep double quotes/curly braces delimiting fields." | |
1a9461d0 CD |
459 | (let (alist key start field) |
460 | (save-excursion | |
461 | (save-restriction | |
462 | (if entry | |
463 | (progn | |
464 | (set-buffer (get-buffer-create " *RefTeX-scratch*")) | |
465 | (fundamental-mode) | |
3666daf6 | 466 | (set-syntax-table reftex-syntax-table-for-bib) |
1a9461d0 CD |
467 | (erase-buffer) |
468 | (insert entry)) | |
469 | (widen) | |
4f595e15 | 470 | (if (and from to) (narrow-to-region from to))) |
1a9461d0 CD |
471 | (goto-char (point-min)) |
472 | ||
4f595e15 RA |
473 | (if (re-search-forward "@\\(\\(?:\\w\\|\\s_\\)+\\)[ \t\n\r]*\ |
474 | \[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t) | |
1a9461d0 CD |
475 | (setq alist |
476 | (list | |
477 | (cons "&type" (downcase (reftex-match-string 1))) | |
478 | (cons "&key" (reftex-match-string 2))))) | |
4f595e15 RA |
479 | (while (re-search-forward "\\(\\(?:\\w\\|-\\)+\\)[ \t\n\r]*=[ \t\n\r]*" |
480 | nil t) | |
1a9461d0 CD |
481 | (setq key (downcase (reftex-match-string 1))) |
482 | (cond | |
483 | ((= (following-char) ?{) | |
4f595e15 RA |
484 | (cond |
485 | (raw | |
486 | (setq start (point)) | |
487 | (forward-char 1)) | |
488 | (t | |
489 | (forward-char 1) | |
490 | (setq start (point)) | |
491 | (condition-case nil | |
492 | (up-list 1) | |
493 | (error nil))))) | |
1a9461d0 | 494 | ((= (following-char) ?\") |
4f595e15 RA |
495 | (cond |
496 | (raw | |
497 | (setq start (point)) | |
498 | (forward-char 1)) | |
499 | (t | |
500 | (forward-char 1) | |
501 | (setq start (point)))) | |
1a9461d0 CD |
502 | (while (and (search-forward "\"" nil t) |
503 | (= ?\\ (char-after (- (point) 2)))))) | |
504 | (t | |
505 | (setq start (point)) | |
506 | (re-search-forward "[ \t]*[\n\r,}]" nil 1))) | |
4f595e15 RA |
507 | ;; extract field value, ignore trailing comma if in RAW mode |
508 | (let ((stop (if (and raw (not (= (char-after (1- (point))) ?,))) | |
509 | (point) | |
510 | (1- (point))) )) | |
511 | (setq field (buffer-substring-no-properties start stop))) | |
1a9461d0 CD |
512 | ;; remove extra whitespace |
513 | (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field) | |
514 | (setq field (replace-match " " nil t field))) | |
1a9461d0 CD |
515 | (push (cons key field) alist)))) |
516 | alist)) | |
517 | ||
518 | (defun reftex-get-bib-field (fieldname entry &optional format) | |
79d7167f TH |
519 | "Extract the field FIELDNAME from ENTRY. |
520 | If FORMAT is non-nil `format' entry accordingly." | |
1a9461d0 CD |
521 | (let ((cell (assoc fieldname entry))) |
522 | (if cell | |
3666daf6 CD |
523 | (if format |
524 | (format format (cdr cell)) | |
525 | (cdr cell)) | |
1a9461d0 CD |
526 | ""))) |
527 | ||
528 | (defun reftex-format-bib-entry (entry) | |
79d7167f | 529 | "Format a BibTeX ENTRY so that it is nice to look at." |
1a9461d0 CD |
530 | (let* |
531 | ((auth-list (reftex-get-bib-names "author" entry)) | |
532 | (authors (mapconcat 'identity auth-list ", ")) | |
533 | (year (reftex-get-bib-field "year" entry)) | |
534 | (title (reftex-get-bib-field "title" entry)) | |
535 | (type (reftex-get-bib-field "&type" entry)) | |
536 | (key (reftex-get-bib-field "&key" entry)) | |
537 | (extra | |
538 | (cond | |
539 | ((equal type "article") | |
540 | (concat (reftex-get-bib-field "journal" entry) " " | |
541 | (reftex-get-bib-field "volume" entry) ", " | |
542 | (reftex-get-bib-field "pages" entry))) | |
543 | ((equal type "book") | |
544 | (concat "book (" (reftex-get-bib-field "publisher" entry) ")")) | |
545 | ((equal type "phdthesis") | |
546 | (concat "PhD: " (reftex-get-bib-field "school" entry))) | |
547 | ((equal type "mastersthesis") | |
548 | (concat "Master: " (reftex-get-bib-field "school" entry))) | |
549 | ((equal type "inbook") | |
550 | (concat "Chap: " (reftex-get-bib-field "chapter" entry) | |
551 | ", pp. " (reftex-get-bib-field "pages" entry))) | |
552 | ((or (equal type "conference") | |
553 | (equal type "incollection") | |
554 | (equal type "inproceedings")) | |
555 | (reftex-get-bib-field "booktitle" entry "in: %s")) | |
556 | (t "")))) | |
557 | (setq authors (reftex-truncate authors 30 t t)) | |
558 | (when (reftex-use-fonts) | |
6c27f0f8 | 559 | (put-text-property 0 (length key) 'face reftex-label-face |
1a9461d0 CD |
560 | key) |
561 | (put-text-property 0 (length authors) 'face reftex-bib-author-face | |
562 | authors) | |
563 | (put-text-property 0 (length year) 'face reftex-bib-year-face | |
564 | year) | |
565 | (put-text-property 0 (length title) 'face reftex-bib-title-face | |
566 | title) | |
567 | (put-text-property 0 (length extra) 'face reftex-bib-extra-face | |
568 | extra)) | |
569 | (concat key "\n " authors " " year " " extra "\n " title "\n\n"))) | |
570 | ||
571 | (defun reftex-parse-bibitem (item) | |
79d7167f | 572 | "Parse a \bibitem entry in ITEM." |
1a9461d0 | 573 | (let ((key "") (text "")) |
70d797cd | 574 | (when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item) |
1a9461d0 | 575 | (setq key (match-string 1 item) |
3666daf6 | 576 | text (match-string 2 item))) |
1a9461d0 CD |
577 | ;; Clean up the text a little bit |
578 | (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text) | |
579 | (setq text (replace-match " " nil t text))) | |
580 | (if (string-match "\\`[ \t]+" text) | |
3666daf6 | 581 | (setq text (replace-match "" nil t text))) |
1a9461d0 CD |
582 | (list |
583 | (cons "&key" key) | |
584 | (cons "&text" text) | |
585 | (cons "&entry" (concat key " " text))))) | |
586 | ||
587 | (defun reftex-format-bibitem (item) | |
79d7167f | 588 | "Format a \bibitem entry in ITEM so that it is (relatively) nice to look at." |
1a9461d0 | 589 | (let ((text (reftex-get-bib-field "&text" item)) |
3666daf6 CD |
590 | (key (reftex-get-bib-field "&key" item)) |
591 | (lines nil)) | |
1a9461d0 CD |
592 | |
593 | ;; Wrap the text into several lines. | |
594 | (while (and (> (length text) 70) | |
3666daf6 CD |
595 | (string-match " " (substring text 60))) |
596 | (push (substring text 0 (+ 60 (match-beginning 0))) lines) | |
597 | (setq text (substring text (+ 61 (match-beginning 0))))) | |
1a9461d0 CD |
598 | (push text lines) |
599 | (setq text (mapconcat 'identity (nreverse lines) "\n ")) | |
600 | ||
601 | (when (reftex-use-fonts) | |
602 | (put-text-property 0 (length text) 'face reftex-bib-author-face text)) | |
603 | (concat key "\n " text "\n\n"))) | |
604 | ||
79d7167f | 605 | ;;; Make a citation |
1a9461d0 CD |
606 | |
607 | ;;;###autoload | |
7c4d13cc | 608 | (defun reftex-citation (&optional no-insert format-key) |
1a9461d0 CD |
609 | "Make a citation using BibTeX database files. |
610 | After prompting for a regular expression, scans the buffers with | |
611 | bibtex entries (taken from the \\bibliography command) and offers the | |
9e3e72cb | 612 | matching entries for selection. The selected entry is formatted according |
1a9461d0 CD |
613 | to `reftex-cite-format' and inserted into the buffer. |
614 | ||
615 | If NO-INSERT is non-nil, nothing is inserted, only the selected key returned. | |
616 | ||
42187e99 | 617 | FORMAT-KEY can be used to pre-select a citation format. |
7c4d13cc | 618 | |
f3c18bd0 CD |
619 | When called with a `C-u' prefix, prompt for optional arguments in |
620 | cite macros. When called with a numeric prefix, make that many | |
621 | citations. When called with point inside the braces of a `\\cite' | |
622 | command, it will add another key, ignoring the value of | |
623 | `reftex-cite-format'. | |
1a9461d0 CD |
624 | |
625 | The regular expression uses an expanded syntax: && is interpreted as `and'. | |
626 | Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'. | |
627 | While entering the regexp, completion on knows citation keys is possible. | |
628 | `=' is a good regular expression to match all entries in all files." | |
1a9461d0 CD |
629 | (interactive) |
630 | ||
631 | ;; check for recursive edit | |
632 | (reftex-check-recursive-edit) | |
633 | ||
634 | ;; This function may also be called outside reftex-mode. | |
635 | ;; Thus look for the scanning info only if in reftex-mode. | |
636 | ||
637 | (when reftex-mode | |
f3c18bd0 | 638 | (reftex-access-scan-info nil)) |
1a9461d0 CD |
639 | |
640 | ;; Call reftex-do-citation, but protected | |
641 | (unwind-protect | |
7c4d13cc | 642 | (reftex-do-citation current-prefix-arg no-insert format-key) |
1a9461d0 CD |
643 | (reftex-kill-temporary-buffers))) |
644 | ||
7c4d13cc | 645 | (defun reftex-do-citation (&optional arg no-insert format-key) |
79d7167f | 646 | "This really does the work of `reftex-citation'." |
7c4d13cc | 647 | (let* ((format (reftex-figure-out-cite-format arg no-insert format-key)) |
3666daf6 CD |
648 | (docstruct-symbol reftex-docstruct-symbol) |
649 | (selected-entries (reftex-offer-bib-menu)) | |
650 | (insert-entries selected-entries) | |
651 | entry string cite-view) | |
1a9461d0 CD |
652 | |
653 | (unless selected-entries (error "Quit")) | |
654 | ||
655 | (if (stringp selected-entries) | |
3666daf6 | 656 | ;; Nonexistent entry |
4f595e15 RA |
657 | (setq insert-entries (list (list selected-entries |
658 | (cons "&key" selected-entries))) | |
659 | selected-entries nil) | |
1a9461d0 CD |
660 | ;; It makes sense to compute the cite-view strings. |
661 | (setq cite-view t)) | |
662 | ||
663 | (when (eq (car selected-entries) 'concat) | |
664 | ;; All keys go into a single command - we need to trick a little | |
53964682 | 665 | ;; FIXME: Unfortunately, this means that commenting does not work right. |
1a9461d0 | 666 | (pop selected-entries) |
4f595e15 RA |
667 | (let ((concat-keys (mapconcat 'car selected-entries |
668 | reftex-cite-key-separator))) | |
53964682 | 669 | (setq insert-entries |
3666daf6 | 670 | (list (list concat-keys (cons "&key" concat-keys)))))) |
53964682 | 671 | |
1a9461d0 CD |
672 | (unless no-insert |
673 | ||
674 | ;; We shall insert this into the buffer... | |
675 | (message "Formatting...") | |
42187e99 | 676 | |
1a9461d0 | 677 | (while (setq entry (pop insert-entries)) |
3666daf6 CD |
678 | ;; Format the citation and insert it |
679 | (setq string (if reftex-format-cite-function | |
680 | (funcall reftex-format-cite-function | |
681 | (reftex-get-bib-field "&key" entry) | |
682 | format) | |
683 | (reftex-format-citation entry format))) | |
f3c18bd0 CD |
684 | (when (or (eq reftex-cite-prompt-optional-args t) |
685 | (and reftex-cite-prompt-optional-args | |
686 | (equal arg '(4)))) | |
687 | (let ((start 0) (nth 0) value) | |
688 | (while (setq start (string-match "\\[\\]" string start)) | |
4f595e15 RA |
689 | (setq value (save-match-data |
690 | (read-string (format "Optional argument %d: " | |
691 | (setq nth (1+ nth)))))) | |
f3c18bd0 CD |
692 | (setq string (replace-match (concat "[" value "]") t t string)) |
693 | (setq start (1+ start))))) | |
694 | ;; Should we cleanup empty optional arguments? | |
695 | ;; if the first is empty, it can be removed. If the second is empty, | |
3ee26b0c | 696 | ;; it has to go. If there is only a single arg and empty, it can go |
3a1e8128 | 697 | ;; as well. |
f3c18bd0 | 698 | (when reftex-cite-cleanup-optional-args |
d63eb0e7 | 699 | (cond |
3ee26b0c CD |
700 | ((string-match "\\([a-zA-Z0-9]\\)\\[\\]{" string) |
701 | (setq string (replace-match "\\1{" nil nil string))) | |
f3c18bd0 CD |
702 | ((string-match "\\[\\]\\(\\[[a-zA-Z0-9., ]+\\]\\)" string) |
703 | (setq string (replace-match "\\1" nil nil string))) | |
704 | ((string-match "\\[\\]\\[\\]" string) | |
705 | (setq string (replace-match "" t t string))))) | |
3666daf6 | 706 | (insert string)) |
1a9461d0 CD |
707 | |
708 | ;; Reposition cursor? | |
709 | (when (string-match "\\?" string) | |
3666daf6 CD |
710 | (search-backward "?") |
711 | (delete-char 1)) | |
1a9461d0 CD |
712 | |
713 | ;; Tell AUCTeX | |
551e625f | 714 | (when (and reftex-mode |
3666daf6 CD |
715 | (fboundp 'LaTeX-add-bibitems) |
716 | reftex-plug-into-AUCTeX) | |
717 | (apply 'LaTeX-add-bibitems (mapcar 'car selected-entries))) | |
551e625f | 718 | |
1a9461d0 CD |
719 | ;; Produce the cite-view strings |
720 | (when (and reftex-mode reftex-cache-cite-echo cite-view) | |
551e625f GM |
721 | (mapc (lambda (entry) |
722 | (reftex-make-cite-echo-string entry docstruct-symbol)) | |
723 | selected-entries)) | |
1a9461d0 CD |
724 | |
725 | (message "")) | |
726 | ||
727 | (set-marker reftex-select-return-marker nil) | |
728 | (reftex-kill-buffer "*RefTeX Select*") | |
551e625f | 729 | |
1a9461d0 CD |
730 | ;; Check if the prefix arg was numeric, and call recursively |
731 | (when (integerp arg) | |
732 | (if (> arg 1) | |
551e625f | 733 | (progn |
3666daf6 CD |
734 | (skip-chars-backward "}") |
735 | (decf arg) | |
736 | (reftex-do-citation arg)) | |
737 | (forward-char 1))) | |
d63eb0e7 | 738 | |
1a9461d0 | 739 | ;; Return the citation key |
4f595e15 | 740 | (mapcar 'car selected-entries))) |
1a9461d0 | 741 | |
7c4d13cc | 742 | (defun reftex-figure-out-cite-format (arg &optional no-insert format-key) |
79d7167f TH |
743 | "Check if there is already a cite command at point and change cite format |
744 | in order to only add another reference in the same cite command." | |
1a9461d0 | 745 | (let ((macro (car (reftex-what-macro 1))) |
3666daf6 CD |
746 | (cite-format-value (reftex-get-cite-format)) |
747 | key format) | |
1a9461d0 CD |
748 | (cond |
749 | (no-insert | |
750 | ;; Format does not really matter because nothing will be inserted. | |
751 | (setq format "%l")) | |
d63eb0e7 | 752 | |
1a9461d0 | 753 | ((and (stringp macro) |
3666daf6 | 754 | (string-match "\\`\\\\cite\\|cite\\'" macro)) |
1a9461d0 CD |
755 | ;; We are already inside a cite macro |
756 | (if (or (not arg) (not (listp arg))) | |
3666daf6 CD |
757 | (setq format |
758 | (concat | |
4f595e15 RA |
759 | (if (member (preceding-char) '(?\{ ?,)) |
760 | "" | |
761 | reftex-cite-key-separator) | |
3666daf6 | 762 | "%l" |
4f595e15 RA |
763 | (if (member (following-char) '(?\} ?,)) |
764 | "" | |
765 | reftex-cite-key-separator))) | |
3666daf6 | 766 | (setq format "%l"))) |
1a9461d0 CD |
767 | (t |
768 | ;; Figure out the correct format | |
769 | (setq format | |
770 | (if (and (symbolp cite-format-value) | |
3666daf6 CD |
771 | (assq cite-format-value reftex-cite-format-builtin)) |
772 | (nth 2 (assq cite-format-value reftex-cite-format-builtin)) | |
773 | cite-format-value)) | |
1a9461d0 | 774 | (when (listp format) |
3666daf6 CD |
775 | (setq key |
776 | (or format-key | |
d63eb0e7 | 777 | (reftex-select-with-char |
3666daf6 CD |
778 | "" (concat "SELECT A CITATION FORMAT\n\n" |
779 | (mapconcat | |
780 | (lambda (x) | |
781 | (format "[%c] %s %s" (car x) | |
782 | (if (> (car x) 31) " " "") | |
783 | (cdr x))) | |
784 | format "\n"))))) | |
785 | (if (assq key format) | |
786 | (setq format (cdr (assq key format))) | |
787 | (error "No citation format associated with key `%c'" key))))) | |
1a9461d0 CD |
788 | format)) |
789 | ||
7c4d13cc CD |
790 | (defun reftex-citep () |
791 | "Call `reftex-citation' with a format selector `?p'." | |
792 | (interactive) | |
793 | (reftex-citation nil ?p)) | |
794 | ||
795 | (defun reftex-citet () | |
796 | "Call `reftex-citation' with a format selector `?t'." | |
797 | (interactive) | |
798 | (reftex-citation nil ?t)) | |
799 | ||
1a9461d0 CD |
800 | (defvar reftex-select-bib-map) |
801 | (defun reftex-offer-bib-menu () | |
79d7167f | 802 | "Offer bib menu and return list of selected items." |
20cd3579 | 803 | (let ((bibtype (reftex-bib-or-thebib)) |
3666daf6 | 804 | found-list rtn key data selected-entries) |
d63eb0e7 GM |
805 | (while |
806 | (not | |
3666daf6 CD |
807 | (catch 'done |
808 | ;; Scan bibtex files | |
809 | (setq found-list | |
810 | (cond | |
811 | ((eq bibtype 'bib) | |
812 | ; ((assq 'bib (symbol-value reftex-docstruct-symbol)) | |
813 | ;; using BibTeX database files. | |
814 | (reftex-extract-bib-entries (reftex-get-bibfile-list))) | |
815 | ((eq bibtype 'thebib) | |
816 | ; ((assq 'thebib (symbol-value reftex-docstruct-symbol)) | |
817 | ;; using thebibliography environment. | |
818 | (reftex-extract-bib-entries-from-thebibliography | |
819 | (reftex-uniquify | |
820 | (mapcar 'cdr | |
d63eb0e7 | 821 | (reftex-all-assq |
3666daf6 CD |
822 | 'thebib (symbol-value reftex-docstruct-symbol)))))) |
823 | (reftex-default-bibliography | |
824 | (message "Using default bibliography") | |
825 | (reftex-extract-bib-entries (reftex-default-bibliography))) | |
826 | (t (error "No valid bibliography in this document, and no default available")))) | |
d63eb0e7 | 827 | |
3666daf6 CD |
828 | (unless found-list |
829 | (error "Sorry, no matches found")) | |
d63eb0e7 | 830 | |
3666daf6 CD |
831 | ;; Remember where we came from |
832 | (setq reftex-call-back-to-this-buffer (current-buffer)) | |
833 | (set-marker reftex-select-return-marker (point)) | |
d63eb0e7 | 834 | |
3666daf6 CD |
835 | ;; Offer selection |
836 | (save-window-excursion | |
837 | (delete-other-windows) | |
d63eb0e7 GM |
838 | (reftex-kill-buffer "*RefTeX Select*") |
839 | (switch-to-buffer-other-window "*RefTeX Select*") | |
840 | (unless (eq major-mode 'reftex-select-bib-mode) | |
841 | (reftex-select-bib-mode)) | |
842 | (let ((buffer-read-only nil)) | |
843 | (erase-buffer) | |
844 | (reftex-insert-bib-matches found-list)) | |
3666daf6 CD |
845 | (setq buffer-read-only t) |
846 | (if (= 0 (buffer-size)) | |
847 | (error "No matches found")) | |
848 | (setq truncate-lines t) | |
849 | (goto-char 1) | |
850 | (while t | |
851 | (setq rtn | |
852 | (reftex-select-item | |
853 | reftex-citation-prompt | |
854 | reftex-citation-help | |
855 | reftex-select-bib-map | |
856 | nil | |
857 | 'reftex-bibtex-selection-callback nil)) | |
858 | (setq key (car rtn) | |
859 | data (nth 1 rtn)) | |
860 | (unless key (throw 'done t)) | |
861 | (cond | |
862 | ((eq key ?g) | |
863 | ;; Start over | |
864 | (throw 'done nil)) | |
865 | ((eq key ?r) | |
866 | ;; Restrict with new regular expression | |
867 | (setq found-list (reftex-restrict-bib-matches found-list)) | |
868 | (let ((buffer-read-only nil)) | |
869 | (erase-buffer) | |
870 | (reftex-insert-bib-matches found-list)) | |
871 | (goto-char 1)) | |
872 | ((eq key ?A) | |
873 | ;; Take all (marked) | |
d63eb0e7 | 874 | (setq selected-entries |
3666daf6 CD |
875 | (if reftex-select-marked |
876 | (mapcar 'car (nreverse reftex-select-marked)) | |
877 | found-list)) | |
878 | (throw 'done t)) | |
879 | ((eq key ?a) | |
880 | ;; Take all (marked), and push the symbol 'concat | |
d63eb0e7 GM |
881 | (setq selected-entries |
882 | (cons 'concat | |
3666daf6 CD |
883 | (if reftex-select-marked |
884 | (mapcar 'car (nreverse reftex-select-marked)) | |
885 | found-list))) | |
886 | (throw 'done t)) | |
f3c18bd0 CD |
887 | ((eq key ?e) |
888 | ;; Take all (marked), and push the symbol 'concat | |
889 | (reftex-extract-bib-file found-list reftex-select-marked) | |
890 | (setq selected-entries "BibTeX database file created") | |
891 | (throw 'done t)) | |
892 | ((eq key ?E) | |
893 | ;; Take all (marked), and push the symbol 'concat | |
894 | (reftex-extract-bib-file found-list reftex-select-marked | |
895 | 'complement) | |
896 | (setq selected-entries "BibTeX database file created") | |
897 | (throw 'done t)) | |
3666daf6 CD |
898 | ((or (eq key ?\C-m) |
899 | (eq key 'return)) | |
900 | ;; Take selected | |
d63eb0e7 | 901 | (setq selected-entries |
3666daf6 | 902 | (if reftex-select-marked |
d63eb0e7 | 903 | (cons 'concat |
3666daf6 CD |
904 | (mapcar 'car (nreverse reftex-select-marked))) |
905 | (if data (list data) nil))) | |
906 | (throw 'done t)) | |
907 | ((stringp key) | |
908 | ;; Got this one with completion | |
909 | (setq selected-entries key) | |
910 | (throw 'done t)) | |
911 | (t | |
912 | (ding)))))))) | |
1a9461d0 CD |
913 | selected-entries)) |
914 | ||
915 | (defun reftex-restrict-bib-matches (found-list) | |
79d7167f | 916 | "Limit FOUND-LIST with more regular expressions." |
1a9461d0 | 917 | (let ((re-list (split-string (read-string |
3666daf6 CD |
918 | "RegExp [ && RegExp...]: " |
919 | nil 'reftex-cite-regexp-hist) | |
920 | "[ \t]*&&[ \t]*")) | |
921 | (found-list-r found-list) | |
922 | re) | |
1a9461d0 CD |
923 | (while (setq re (pop re-list)) |
924 | (setq found-list-r | |
3666daf6 CD |
925 | (delq nil |
926 | (mapcar | |
927 | (lambda (x) | |
928 | (if (string-match | |
929 | re (cdr (assoc "&entry" x))) | |
930 | x | |
931 | nil)) | |
932 | found-list-r)))) | |
1a9461d0 | 933 | (if found-list-r |
3666daf6 | 934 | found-list-r |
1a9461d0 CD |
935 | (ding) |
936 | found-list))) | |
937 | ||
f3c18bd0 | 938 | (defun reftex-extract-bib-file (all &optional marked complement) |
79d7167f | 939 | "Limit FOUND-LIST with more regular expressions." |
f3c18bd0 CD |
940 | (let ((file (read-file-name "File to create: "))) |
941 | (find-file-other-window file) | |
942 | (if (> (buffer-size) 0) | |
d63eb0e7 | 943 | (unless (yes-or-no-p |
f3c18bd0 CD |
944 | (format "Overwrite non-empty file %s? " file)) |
945 | (error "Abort"))) | |
946 | (erase-buffer) | |
947 | (setq all (delq nil | |
948 | (mapcar | |
949 | (lambda (x) | |
950 | (if marked | |
951 | (if (or (and (assoc x marked) (not complement)) | |
952 | (and (not (assoc x marked)) complement)) | |
953 | (cdr (assoc "&entry" x)) | |
954 | nil) | |
955 | (cdr (assoc "&entry" x)))) | |
956 | all))) | |
957 | (insert (mapconcat 'identity all "\n\n")) | |
958 | (save-buffer) | |
959 | (goto-char (point-min)))) | |
960 | ||
1a9461d0 | 961 | (defun reftex-insert-bib-matches (list) |
79d7167f | 962 | "Insert the bib matches and number them correctly." |
1a9461d0 | 963 | (let ((mouse-face |
3666daf6 CD |
964 | (if (memq reftex-highlight-selection '(mouse both)) |
965 | reftex-mouse-selected-face | |
966 | nil)) | |
967 | tmp len) | |
551e625f | 968 | (mapc |
1a9461d0 CD |
969 | (lambda (x) |
970 | (setq tmp (cdr (assoc "&formatted" x)) | |
3666daf6 | 971 | len (length tmp)) |
1a9461d0 CD |
972 | (put-text-property 0 len :data x tmp) |
973 | (put-text-property 0 (1- len) 'mouse-face mouse-face tmp) | |
974 | (insert tmp)) | |
975 | list)) | |
976 | (run-hooks 'reftex-display-copied-context-hook)) | |
977 | ||
978 | (defun reftex-format-names (namelist n) | |
979 | (let (last (len (length namelist))) | |
6fbeb429 | 980 | (if (= n 0) (setq n len)) |
1a9461d0 CD |
981 | (cond |
982 | ((< len 1) "") | |
983 | ((= 1 len) (car namelist)) | |
984 | ((> len n) (concat (car namelist) (nth 2 reftex-cite-punctuation))) | |
985 | (t | |
986 | (setq n (min len n) | |
987 | last (nth (1- n) namelist)) | |
988 | (setcdr (nthcdr (- n 2) namelist) nil) | |
989 | (concat | |
990 | (mapconcat 'identity namelist (nth 0 reftex-cite-punctuation)) | |
991 | (nth 1 reftex-cite-punctuation) | |
992 | last))))) | |
993 | ||
994 | (defun reftex-format-citation (entry format) | |
79d7167f | 995 | "Format a citation from the info in the BibTeX ENTRY according to FORMAT." |
1a9461d0 CD |
996 | (unless (stringp format) (setq format "\\cite{%l}")) |
997 | ||
998 | (if (and reftex-comment-citations | |
999 | (string-match "%l" reftex-cite-comment-format)) | |
5181ff9f | 1000 | (error "reftex-cite-comment-format contains invalid %%l")) |
1a9461d0 CD |
1001 | |
1002 | (while (string-match | |
1003 | "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)" | |
1004 | format) | |
027a4b6b | 1005 | (let ((n (string-to-number (match-string 4 format))) |
1a9461d0 CD |
1006 | (l (string-to-char (match-string 5 format))) |
1007 | rpl b e) | |
1008 | (save-match-data | |
1009 | (setq rpl | |
1010 | (cond | |
1011 | ((= l ?l) (concat | |
1012 | (reftex-get-bib-field "&key" entry) | |
1013 | (if reftex-comment-citations | |
1014 | reftex-cite-comment-format | |
1015 | ""))) | |
1016 | ((= l ?a) (reftex-format-names | |
1017 | (reftex-get-bib-names "author" entry) | |
1018 | (or n 2))) | |
1019 | ((= l ?A) (car (reftex-get-bib-names "author" entry))) | |
1020 | ((= l ?b) (reftex-get-bib-field "booktitle" entry "in: %s")) | |
1021 | ((= l ?B) (reftex-abbreviate-title | |
3666daf6 | 1022 | (reftex-get-bib-field "booktitle" entry "in: %s"))) |
1a9461d0 CD |
1023 | ((= l ?c) (reftex-get-bib-field "chapter" entry)) |
1024 | ((= l ?d) (reftex-get-bib-field "edition" entry)) | |
1025 | ((= l ?e) (reftex-format-names | |
1026 | (reftex-get-bib-names "editor" entry) | |
1027 | (or n 2))) | |
1028 | ((= l ?E) (car (reftex-get-bib-names "editor" entry))) | |
1029 | ((= l ?h) (reftex-get-bib-field "howpublished" entry)) | |
1030 | ((= l ?i) (reftex-get-bib-field "institution" entry)) | |
1031 | ((= l ?j) (reftex-get-bib-field "journal" entry)) | |
1032 | ((= l ?k) (reftex-get-bib-field "key" entry)) | |
1033 | ((= l ?m) (reftex-get-bib-field "month" entry)) | |
1034 | ((= l ?n) (reftex-get-bib-field "number" entry)) | |
e6aff855 | 1035 | ((= l ?N) (reftex-get-bib-field "note" entry)) |
1a9461d0 CD |
1036 | ((= l ?o) (reftex-get-bib-field "organization" entry)) |
1037 | ((= l ?p) (reftex-get-bib-field "pages" entry)) | |
1038 | ((= l ?P) (car (split-string | |
1039 | (reftex-get-bib-field "pages" entry) | |
1040 | "[- .]+"))) | |
1041 | ((= l ?s) (reftex-get-bib-field "school" entry)) | |
1042 | ((= l ?u) (reftex-get-bib-field "publisher" entry)) | |
e6aff855 | 1043 | ((= l ?U) (reftex-get-bib-field "url" entry)) |
1a9461d0 CD |
1044 | ((= l ?r) (reftex-get-bib-field "address" entry)) |
1045 | ((= l ?t) (reftex-get-bib-field "title" entry)) | |
1046 | ((= l ?T) (reftex-abbreviate-title | |
3666daf6 | 1047 | (reftex-get-bib-field "title" entry))) |
1a9461d0 CD |
1048 | ((= l ?v) (reftex-get-bib-field "volume" entry)) |
1049 | ((= l ?y) (reftex-get-bib-field "year" entry))))) | |
1050 | ||
1051 | (if (string= rpl "") | |
1052 | (setq b (match-beginning 2) e (match-end 2)) | |
1053 | (setq b (match-beginning 3) e (match-end 3))) | |
1054 | (setq format (concat (substring format 0 b) rpl (substring format e))))) | |
1055 | (while (string-match "%%" format) | |
1056 | (setq format (replace-match "%" t t format))) | |
1057 | (while (string-match "[ ,.;:]*%<" format) | |
1058 | (setq format (replace-match "" t t format))) | |
1059 | format) | |
1060 | ||
1061 | (defun reftex-make-cite-echo-string (entry docstruct-symbol) | |
79d7167f | 1062 | "Format a bibtex ENTRY for the echo area and cache the result." |
1a9461d0 | 1063 | (let* ((key (reftex-get-bib-field "&key" entry)) |
d63eb0e7 | 1064 | (string |
3666daf6 CD |
1065 | (let* ((reftex-cite-punctuation '(" " " & " " etal."))) |
1066 | (reftex-format-citation entry reftex-cite-view-format))) | |
1067 | (cache (assq 'bibview-cache (symbol-value docstruct-symbol))) | |
1068 | (cache-entry (assoc key (cdr cache)))) | |
1a9461d0 CD |
1069 | (unless cache |
1070 | ;; This docstruct has no cache - make one. | |
1071 | (set docstruct-symbol (cons (cons 'bibview-cache nil) | |
3666daf6 | 1072 | (symbol-value docstruct-symbol)))) |
1a9461d0 CD |
1073 | (when reftex-cache-cite-echo |
1074 | (setq key (copy-sequence key)) | |
1075 | (set-text-properties 0 (length key) nil key) | |
1076 | (set-text-properties 0 (length string) nil string) | |
1077 | (if cache-entry | |
3666daf6 CD |
1078 | (unless (string= (cdr cache-entry) string) |
1079 | (setcdr cache-entry string) | |
1080 | (put reftex-docstruct-symbol 'modified t)) | |
1081 | (push (cons key string) (cdr cache)) | |
1082 | (put reftex-docstruct-symbol 'modified t))) | |
1a9461d0 CD |
1083 | string)) |
1084 | ||
1085 | (defun reftex-bibtex-selection-callback (data ignore no-revisit) | |
79d7167f TH |
1086 | "Callback function to be called from the BibTeX selection, in |
1087 | order to display context. This function is relatively slow and not | |
1088 | recommended for follow mode. It works OK for individual lookups." | |
1a9461d0 CD |
1089 | (let ((win (selected-window)) |
1090 | (key (reftex-get-bib-field "&key" data)) | |
20cd3579 | 1091 | bibfile-list item bibtype) |
1a9461d0 CD |
1092 | |
1093 | (catch 'exit | |
9a529312 | 1094 | (with-current-buffer reftex-call-back-to-this-buffer |
3666daf6 CD |
1095 | (setq bibtype (reftex-bib-or-thebib)) |
1096 | (cond | |
1097 | ((eq bibtype 'bib) | |
1098 | ; ((assq 'bib (symbol-value reftex-docstruct-symbol)) | |
1099 | (setq bibfile-list (reftex-get-bibfile-list))) | |
1100 | ((eq bibtype 'thebib) | |
1101 | ; ((assq 'thebib (symbol-value reftex-docstruct-symbol)) | |
1102 | (setq bibfile-list | |
1103 | (reftex-uniquify | |
1104 | (mapcar 'cdr | |
d63eb0e7 | 1105 | (reftex-all-assq |
3666daf6 CD |
1106 | 'thebib (symbol-value reftex-docstruct-symbol)))) |
1107 | item t)) | |
1108 | (reftex-default-bibliography | |
1109 | (setq bibfile-list (reftex-default-bibliography))) | |
1110 | (t (ding) (throw 'exit nil)))) | |
1a9461d0 CD |
1111 | |
1112 | (when no-revisit | |
3666daf6 | 1113 | (setq bibfile-list (reftex-visited-files bibfile-list))) |
1a9461d0 CD |
1114 | |
1115 | (condition-case nil | |
d63eb0e7 | 1116 | (reftex-pop-to-bibtex-entry |
3666daf6 CD |
1117 | key bibfile-list (not reftex-keep-temporary-buffers) t item) |
1118 | (error (ding)))) | |
d63eb0e7 | 1119 | |
1a9461d0 CD |
1120 | (select-window win))) |
1121 | ||
f3c18bd0 CD |
1122 | ;;; Global BibTeX file |
1123 | (defun reftex-all-used-citation-keys () | |
1124 | (reftex-access-scan-info) | |
7b07114a | 1125 | (let ((files (reftex-all-document-files)) file keys kk k) |
9a529312 | 1126 | (save-current-buffer |
f3c18bd0 CD |
1127 | (while (setq file (pop files)) |
1128 | (set-buffer (reftex-get-file-buffer-force file 'mark)) | |
1129 | (save-excursion | |
1130 | (save-restriction | |
1131 | (widen) | |
1132 | (goto-char (point-min)) | |
e7d807ca | 1133 | (while (re-search-forward "\\(?:^\\|\\=\\)[^%\n\r]*?\\\\\\(bibentry\\|[a-zA-Z]*cite[a-zA-Z]*\\)\\(\\[[^]]*\\]\\)?{\\([^}]+\\)}" nil t) |
f3c18bd0 CD |
1134 | (setq kk (match-string-no-properties 3)) |
1135 | (while (string-match "%.*\n?" kk) | |
1136 | (setq kk (replace-match "" t t kk))) | |
1137 | (setq kk (split-string kk "[, \t\r\n]+")) | |
1138 | (while (setq k (pop kk)) | |
1139 | (or (member k keys) | |
1140 | (setq keys (cons k keys))))))))) | |
1141 | (reftex-kill-temporary-buffers) | |
1142 | keys)) | |
1143 | ||
4f595e15 RA |
1144 | (defun reftex-get-string-refs (alist) |
1145 | "Return a list of BibTeX @string references that appear as values in ALIST." | |
1146 | (reftex-remove-if (lambda (x) (string-match "^\\([\"{]\\|[0-9]+$\\)" x)) | |
1147 | ;; get list of values, discard keys | |
1148 | (mapcar 'cdr | |
1149 | ;; remove &key and &type entries | |
1150 | (reftex-remove-if (lambda (pair) | |
1151 | (string-match "^&" (car pair))) | |
1152 | alist)))) | |
1153 | ||
f3c18bd0 | 1154 | (defun reftex-create-bibtex-file (bibfile) |
79d7167f | 1155 | "Create a new BibTeX database BIBFILE with all entries referenced in document. |
4f595e15 RA |
1156 | The command prompts for a filename and writes the collected |
1157 | entries to that file. Only entries referenced in the current | |
1158 | document with any \\cite-like macros are used. The sequence in | |
1159 | the new file is the same as it was in the old database. | |
1160 | ||
1161 | Entries referenced from other entries must appear after all | |
1162 | referencing entries. | |
1163 | ||
1164 | You can define strings to be used as header or footer for the | |
1165 | created files in the variables `reftex-create-bibtex-header' or | |
1166 | `reftex-create-bibtex-footer' respectively." | |
f3c18bd0 CD |
1167 | (interactive "FNew BibTeX file: ") |
1168 | (let ((keys (reftex-all-used-citation-keys)) | |
1169 | (files (reftex-get-bibfile-list)) | |
4f595e15 | 1170 | file key entries beg end entry string-keys string-entries) |
9a529312 | 1171 | (save-current-buffer |
4f595e15 | 1172 | (dolist (file files) |
f3c18bd0 CD |
1173 | (set-buffer (reftex-get-file-buffer-force file 'mark)) |
1174 | (reftex-with-special-syntax-for-bib | |
1175 | (save-excursion | |
1176 | (save-restriction | |
1177 | (widen) | |
1178 | (goto-char (point-min)) | |
5d8f9169 RA |
1179 | (while (re-search-forward "^[ \t]*@\\(?:\\w\\|\\s_\\)+[ \t\n\r]*\ |
1180 | \[{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t) | |
f3c18bd0 CD |
1181 | (setq key (match-string 1) |
1182 | beg (match-beginning 0) | |
1183 | end (progn | |
1184 | (goto-char (match-beginning 1)) | |
1185 | (condition-case nil | |
1186 | (up-list 1) | |
1187 | (error (goto-char (match-end 0)))) | |
1188 | (point))) | |
1189 | (when (member key keys) | |
1190 | (setq entry (buffer-substring beg end) | |
1191 | entries (cons entry entries) | |
4f595e15 RA |
1192 | keys (delete key keys)) |
1193 | ||
1194 | ;; check for crossref entries | |
1195 | (let* ((attr-list (reftex-parse-bibtex-entry nil beg end)) | |
1196 | (xref-key (cdr (assoc "crossref" attr-list)))) | |
1197 | (if xref-key (pushnew xref-key keys))) | |
1198 | ;; check for string references | |
1199 | (let* ((raw-fields (reftex-parse-bibtex-entry nil beg end t)) | |
1200 | (string-fields (reftex-get-string-refs raw-fields))) | |
1201 | (dolist (skey string-fields) | |
1202 | (unless (member skey string-keys) | |
1203 | (push skey string-keys))))))))))) | |
1204 | ;; second pass: grab @string references | |
1205 | (if string-keys | |
1206 | (save-current-buffer | |
1207 | (dolist (file files) | |
1208 | (set-buffer (reftex-get-file-buffer-force file 'mark)) | |
1209 | (reftex-with-special-syntax-for-bib | |
1210 | (save-excursion | |
1211 | (save-restriction | |
1212 | (widen) | |
1213 | (goto-char (point-min)) | |
1214 | (while (re-search-forward | |
1215 | "^[ \t]*@[Ss][Tt][Rr][Ii][Nn][Gg][ \t]*{[ \t]*\\([^ \t\r\n]+\\)" | |
1216 | nil t) | |
1217 | (setq key (match-string 1) | |
1218 | beg (match-beginning 0) | |
1219 | end (progn | |
1220 | (goto-char (match-beginning 1)) | |
1221 | (condition-case nil | |
1222 | (up-list 1) | |
1223 | (error (goto-char (match-end 0)))) | |
1224 | (point))) | |
1225 | (when (member key string-keys) | |
1226 | (setq entry (buffer-substring beg end) | |
1227 | string-entries (cons entry string-entries) | |
1228 | string-keys (delete key string-keys)))))))))) | |
f3c18bd0 CD |
1229 | (find-file-other-window bibfile) |
1230 | (if (> (buffer-size) 0) | |
d63eb0e7 | 1231 | (unless (yes-or-no-p |
f3c18bd0 CD |
1232 | (format "Overwrite non-empty file %s? " bibfile)) |
1233 | (error "Abort"))) | |
1234 | (erase-buffer) | |
4f595e15 RA |
1235 | (if reftex-create-bibtex-header (insert reftex-create-bibtex-header "\n\n")) |
1236 | (insert (mapconcat 'identity (reverse string-entries) "\n\n")) | |
1237 | (if string-entries (insert "\n\n\n")) | |
f3c18bd0 | 1238 | (insert (mapconcat 'identity (reverse entries) "\n\n")) |
4f595e15 | 1239 | (if reftex-create-bibtex-footer (insert "\n\n" reftex-create-bibtex-footer)) |
f3c18bd0 CD |
1240 | (goto-char (point-min)) |
1241 | (save-buffer) | |
1242 | (message "%d entries extracted and copied to new database" | |
1243 | (length entries)))) | |
1244 | ||
79d7167f | 1245 | (provide 'reftex-cite) |
1a9461d0 | 1246 | ;;; reftex-cite.el ends here |