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