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