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