1 ;;; reftex-cite.el --- creating citations with RefTeX
3 ;; Copyright (C) 1997-2016 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/>.
27 (eval-when-compile (require 'cl
))
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.")
53 (defmacro reftex-with-special-syntax-for-bib
(&rest body
)
54 `(let ((saved-syntax (syntax-table)))
57 (set-syntax-table reftex-syntax-table-for-bib
)
59 (set-syntax-table saved-syntax
))))
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
))
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
)
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
))
92 (if thebib
'thebib nil
))))
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.
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."
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))
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
))
142 (setq oldpos
(point))
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))
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
)
161 (reftex-highlight 0 (match-beginning 0) (match-end 0)))
162 (throw 'exit
(selected-window))))
163 (set-buffer buffer-conf
)
165 (error "No \\bibitem with citation key %s" key
)
166 (error "No BibTeX entry with citation key %s" key
)))))
169 (defun reftex-end-of-bib-entry (item)
175 "\\\\bibitem\\|\\end{thebibliography}")
176 (1- (match-beginning 0)))
177 (progn (forward-list 1) (point)))
178 (error (min (point-max) (+ 300 (point)))))))
180 (defun reftex--query-search-regexps (default)
181 "Query for regexps for searching entries using DEFAULT as default.
182 Return a list of regular expressions."
186 "Regex { && Regex...}: "
188 ;; Ensure default is always in the completion list.
189 (let ((def (when default
(list default
)))
190 (coll (if reftex-mode
191 (if (fboundp 'LaTeX-bibitem-list
)
193 (cdr (assoc 'bibview-cache
194 (symbol-value reftex-docstruct-symbol
))))
196 (if (and def
(member def coll
))
199 nil nil nil
'reftex-cite-regexp-hist
)
202 ;;; Parse bibtex buffers
203 (defun reftex-extract-bib-entries (buffers)
204 "Extract bib entries which match regexps from BUFFERS.
205 BUFFERS is a list of buffers or file names.
206 Return list with entries."
207 (let* (re-list first-re rest-re
208 (buffer-list (if (listp buffers
) buffers
(list buffers
)))
209 found-list entry buffer1 buffer alist
210 key-point start-point end-point default
)
212 ;; Read a regexp, completing on known citation keys.
213 (setq default
(regexp-quote (reftex-get-bibkey-default)))
214 (setq re-list
(reftex--query-search-regexps default
))
216 (if (or (null re-list
) (equal re-list
'("")))
217 (setq re-list
(list default
)))
219 (setq first-re
(car re-list
) ; We'll use the first re to find things,
220 rest-re
(cdr re-list
)) ; the others to narrow down.
221 (if (string-match "\\`[ \t]*\\'" (or first-re
""))
222 (user-error "Empty regular expression"))
223 (if (string-match first-re
"")
224 (user-error "Regular expression matches the empty string."))
227 (save-window-excursion
229 ;; Walk through all bibtex files
231 (setq buffer
(car buffer-list
)
232 buffer-list
(cdr buffer-list
))
233 (if (and (bufferp buffer
)
234 (buffer-live-p buffer
))
235 (setq buffer1 buffer
)
236 (setq buffer1
(reftex-get-file-buffer-force
237 buffer
(not reftex-keep-temporary-buffers
))))
239 (message "No such BibTeX file %s (ignored)" buffer
)
240 (message "Scanning bibliography database %s" buffer1
)
241 (unless (verify-visited-file-modtime buffer1
)
243 (format "File %s changed on disk. Reread from disk? "
244 (file-name-nondirectory
245 (buffer-file-name buffer1
))))
246 (with-current-buffer buffer1
(revert-buffer t t
)))))
249 (reftex-with-special-syntax-for-bib
251 (goto-char (point-min))
252 (while (re-search-forward first-re nil t
)
254 (setq key-point
(point))
255 (unless (re-search-backward "\\(\\`\\|[\n\r]\\)[ \t]*\
256 @\\(\\(?:\\w\\|\\s_\\)+\\)[ \t\n\r]*[{(]" nil t
)
257 (throw 'search-again nil
))
258 (setq start-point
(point))
259 (goto-char (match-end 0))
262 (error (goto-char key-point
)
263 (throw 'search-again nil
)))
264 (setq end-point
(point))
266 ;; Ignore @string, @comment and @c entries or things
268 (when (or (string= (downcase (match-string 2)) "string")
269 (string= (downcase (match-string 2)) "comment")
270 (string= (downcase (match-string 2)) "c")
271 (< (point) key-point
)) ; this means match not in {}
272 (goto-char key-point
)
273 (throw 'search-again nil
))
275 ;; Well, we have got a match
276 ;;(setq entry (concat
277 ;; (buffer-substring start-point (point)) "\n"))
278 (setq entry
(buffer-substring start-point
(point)))
280 ;; Check if other regexp match as well
281 (setq re-list rest-re
)
283 (unless (string-match (car re-list
) entry
)
285 (throw 'search-again nil
))
288 (setq alist
(reftex-parse-bibtex-entry
289 nil start-point end-point
))
290 (push (cons "&entry" entry
) alist
)
292 ;; check for crossref entries
293 (if (assoc "crossref" alist
)
296 alist
(reftex-get-crossref-alist alist
))))
299 (push (cons "&formatted" (reftex-format-bib-entry alist
))
302 ;; make key the first element
303 (push (reftex-get-bib-field "&key" alist
) alist
)
305 ;; add it to the list
306 (push alist found-list
)))))
307 (reftex-kill-temporary-buffers))))
308 (setq found-list
(nreverse found-list
))
312 ((eq 'author reftex-sort-bibtex-matches
)
313 (sort found-list
'reftex-bib-sort-author
))
314 ((eq 'year reftex-sort-bibtex-matches
)
315 (sort found-list
'reftex-bib-sort-year
))
316 ((eq 'reverse-year reftex-sort-bibtex-matches
)
317 (sort found-list
'reftex-bib-sort-year-reverse
))
320 (defun reftex-bib-sort-author (e1 e2
)
321 "Compare bib entries E1 and E2 by author.
322 The name of the first different author/editor is used."
323 (let ((al1 (reftex-get-bib-names "author" e1
))
324 (al2 (reftex-get-bib-names "author" e2
)))
325 (while (and al1 al2
(string= (car al1
) (car al2
)))
328 (if (and (stringp (car al1
))
330 (string< (car al1
) (car al2
))
331 (not (stringp (car al1
))))))
333 (defun reftex-bib-sort-year (e1 e2
)
334 "Compare bib entries E1 and E2 by year in ascending order."
335 (< (string-to-number (or (cdr (assoc "year" e1
)) "0"))
336 (string-to-number (or (cdr (assoc "year" e2
)) "0"))))
338 (defun reftex-bib-sort-year-reverse (e1 e2
)
339 "Compare bib entries E1 and E2 by year in descending order."
340 (> (string-to-number (or (cdr (assoc "year" e1
)) "0"))
341 (string-to-number (or (cdr (assoc "year" e2
)) "0"))))
343 (defun reftex-get-crossref-alist (entry)
344 "Return the alist from a crossref ENTRY."
345 (let ((crkey (cdr (assoc "crossref" entry
)))
350 (if (re-search-forward
351 (concat "@\\w+[{(][ \t\n\r]*" (regexp-quote crkey
)
352 "[ \t\n\r]*,") nil t
)
354 (setq start
(match-beginning 0))
358 (reftex-parse-bibtex-entry nil start
(point)))
361 ;; Parse the bibliography environment
362 (defun reftex-extract-bib-entries-from-thebibliography (files)
363 "Extract bib-entries from the \begin{thebibliography} environment.
364 Parsing is not as good as for the BibTeX database stuff.
365 The environment should be located in FILES."
366 (let* (start end buf entries re re-list file default
)
368 (error "Need file name to find thebibliography environment"))
369 (while (setq file
(pop files
))
370 (setq buf
(reftex-get-file-buffer-force
371 file
(not reftex-keep-temporary-buffers
)))
373 (error "No such file %s" file
))
374 (message "Scanning thebibliography environment in %s" file
)
376 (with-current-buffer buf
380 (goto-char (point-min))
381 (while (re-search-forward
382 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\begin{thebibliography}" nil t
)
383 (beginning-of-line 2)
385 (if (re-search-forward
386 "\\(\\`\\|[\n\r]\\)[ \t]*\\\\end{thebibliography}" nil t
)
388 (beginning-of-line 1)
390 (when (and start end
)
393 (mapcar 'reftex-parse-bibitem
396 (buffer-substring-no-properties
398 "[ \t\n\r]*\\\\bibitem[ \t]*\
399 \\(\\[[^]]*]\\)*[ \t]*"))))))
402 (error "No bibitems found"))
404 ;; Read a regexp, completing on known citation keys.
405 (setq default
(regexp-quote (reftex-get-bibkey-default)))
406 (setq re-list
(reftex--query-search-regexps default
))
408 (if (or (null re-list
) (equal re-list
'("")))
409 (setq re-list
(list default
)))
411 (if (string-match "\\`[ \t]*\\'" (car re-list
))
412 (error "Empty regular expression"))
414 (while (and (setq re
(pop re-list
)) entries
)
418 (if (string-match re
(cdr (assoc "&entry" x
)))
424 (push (cons "&formatted" (reftex-format-bibitem x
)) x
)
425 (push (reftex-get-bib-field "&key" x
) x
)
431 (defun reftex-get-bibkey-default ()
432 "Return the word before the cursor.
433 If the cursor is in a citation macro, return the word before the macro."
434 (let* ((macro (reftex-what-macro 1)))
436 (if (and macro
(string-match "cite" (car macro
)))
437 (goto-char (cdr macro
)))
438 (skip-chars-backward "^a-zA-Z0-9")
439 (reftex-this-word))))
441 ;;; Parse and format individual entries
442 (defun reftex-get-bib-names (field entry
)
443 "Return a list with the author or editor names in ENTRY.
444 If FIELD is empty try \"editor\" field."
445 (let ((names (reftex-get-bib-field field entry
)))
447 (setq names
(reftex-get-bib-field "editor" entry
)))
448 (while (string-match "\\band\\b[ \t]*" names
)
449 (setq names
(replace-match "\n" nil t names
)))
450 (while (string-match "[\\.a-zA-Z\\-]+\\.[ \t]*\\|,.*\\|[{}]+" names
)
451 (setq names
(replace-match "" nil t names
)))
452 (while (string-match "^[ \t]+\\|[ \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 (split-string names
"\n")))
459 (defun reftex-parse-bibtex-entry (entry &optional from to raw
)
461 If ENTRY is nil then parse the entry in current buffer between FROM and TO.
462 If RAW is non-nil, keep double quotes/curly braces delimiting fields."
463 (let (alist key start field
)
468 (set-buffer (get-buffer-create " *RefTeX-scratch*"))
470 (set-syntax-table reftex-syntax-table-for-bib
)
474 (if (and from to
) (narrow-to-region from to
)))
475 (goto-char (point-min))
477 (if (re-search-forward "@\\(\\(?:\\w\\|\\s_\\)+\\)[ \t\n\r]*\
478 [{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t
)
481 (cons "&type" (downcase (reftex-match-string 1)))
482 (cons "&key" (reftex-match-string 2)))))
483 (while (re-search-forward "\\(\\(?:\\w\\|-\\)+\\)[ \t\n\r]*=[ \t\n\r]*"
485 (setq key
(downcase (reftex-match-string 1)))
487 ((= (following-char) ?
{)
498 ((= (following-char) ?
\")
505 (setq start
(point))))
506 (while (and (search-forward "\"" nil t
)
507 (= ?
\\ (char-after (- (point) 2))))))
510 (re-search-forward "[ \t]*[\n\r,}]" nil
1)))
511 ;; extract field value, ignore trailing comma if in RAW mode
512 (let ((stop (if (and raw
(not (= (char-after (1- (point))) ?
,)))
515 (setq field
(buffer-substring-no-properties start stop
)))
516 ;; remove extra whitespace
517 (while (string-match "[\n\t\r]\\|[ \t][ \t]+" field
)
518 (setq field
(replace-match " " nil t field
)))
519 (push (cons key field
) alist
))))
522 (defun reftex-get-bib-field (fieldname entry
&optional format
)
523 "Extract the field FIELDNAME from ENTRY.
524 If FORMAT is non-nil `format' entry accordingly."
525 (let ((cell (assoc fieldname entry
)))
528 (format format
(cdr cell
))
532 (defun reftex-format-bib-entry (entry)
533 "Format a BibTeX ENTRY so that it is nice to look at."
535 ((auth-list (reftex-get-bib-names "author" entry
))
536 (authors (mapconcat 'identity auth-list
", "))
537 (year (reftex-get-bib-field "year" entry
))
538 (title (reftex-get-bib-field "title" entry
))
539 (type (reftex-get-bib-field "&type" entry
))
540 (key (reftex-get-bib-field "&key" entry
))
543 ((equal type
"article")
544 (concat (let ((jt (reftex-get-bib-field "journal" entry
)))
545 ;; biblatex prefers the alternative journaltitle
546 ;; field, so check if that exists in case journal
548 (if (zerop (length jt
))
549 (reftex-get-bib-field "journaltitle" entry
)
552 (reftex-get-bib-field "volume" entry
) ", "
553 (reftex-get-bib-field "pages" entry
)))
555 (concat "book (" (reftex-get-bib-field "publisher" entry
) ")"))
556 ((equal type
"phdthesis")
557 (concat "PhD: " (reftex-get-bib-field "school" entry
)))
558 ((equal type
"mastersthesis")
559 (concat "Master: " (reftex-get-bib-field "school" entry
)))
560 ((equal type
"inbook")
561 (concat "Chap: " (reftex-get-bib-field "chapter" entry
)
562 ", pp. " (reftex-get-bib-field "pages" entry
)))
563 ((or (equal type
"conference")
564 (equal type
"incollection")
565 (equal type
"inproceedings"))
566 (reftex-get-bib-field "booktitle" entry
"in: %s"))
568 (setq authors
(reftex-truncate authors
30 t t
))
569 (when (reftex-use-fonts)
570 (put-text-property 0 (length key
) 'face reftex-label-face
572 (put-text-property 0 (length authors
) 'face reftex-bib-author-face
574 (put-text-property 0 (length year
) 'face reftex-bib-year-face
576 (put-text-property 0 (length title
) 'face reftex-bib-title-face
578 (put-text-property 0 (length extra
) 'face reftex-bib-extra-face
580 (concat key
"\n " authors
" " year
" " extra
"\n " title
"\n\n")))
582 (defun reftex-parse-bibitem (item)
583 "Parse a \bibitem entry in ITEM."
584 (let ((key "") (text ""))
585 (when (string-match "\\`{\\([^}]+\\)}\\([^\000]*\\)" item
)
586 (setq key
(match-string 1 item
)
587 text
(match-string 2 item
)))
588 ;; Clean up the text a little bit
589 (while (string-match "[\n\r\t]\\|[ \t][ \t]+" text
)
590 (setq text
(replace-match " " nil t text
)))
591 (if (string-match "\\`[ \t]+" text
)
592 (setq text
(replace-match "" nil t text
)))
596 (cons "&entry" (concat key
" " text
)))))
598 (defun reftex-format-bibitem (item)
599 "Format a \bibitem entry in ITEM so that it is (relatively) nice to look at."
600 (let ((text (reftex-get-bib-field "&text" item
))
601 (key (reftex-get-bib-field "&key" item
))
604 ;; Wrap the text into several lines.
605 (while (and (> (length text
) 70)
606 (string-match " " (substring text
60)))
607 (push (substring text
0 (+ 60 (match-beginning 0))) lines
)
608 (setq text
(substring text
(+ 61 (match-beginning 0)))))
610 (setq text
(mapconcat 'identity
(nreverse lines
) "\n "))
612 (when (reftex-use-fonts)
613 (put-text-property 0 (length text
) 'face reftex-bib-author-face text
))
614 (concat key
"\n " text
"\n\n")))
618 ;; NB this is a global autoload - see reftex.el.
620 (defun reftex-citation (&optional no-insert format-key
)
621 "Make a citation using BibTeX database files.
622 After prompting for a regular expression, scans the buffers with
623 bibtex entries (taken from the \\bibliography command) and offers the
624 matching entries for selection. The selected entry is formatted according
625 to `reftex-cite-format' and inserted into the buffer.
627 If NO-INSERT is non-nil, nothing is inserted, only the selected key returned.
629 FORMAT-KEY can be used to pre-select a citation format.
631 When called with a `C-u' prefix, prompt for optional arguments in
632 cite macros. When called with a numeric prefix, make that many
633 citations. When called with point inside the braces of a `\\cite'
634 command, it will add another key, ignoring the value of
635 `reftex-cite-format'.
637 The regular expression uses an expanded syntax: && is interpreted as `and'.
638 Thus, `aaaa&&bbb' matches entries which contain both `aaaa' and `bbb'.
639 While entering the regexp, completion on knows citation keys is possible.
640 `=' is a good regular expression to match all entries in all files."
643 ;; check for recursive edit
644 (reftex-check-recursive-edit)
646 ;; This function may also be called outside reftex-mode.
647 ;; Thus look for the scanning info only if in reftex-mode.
650 (reftex-access-scan-info nil
))
652 ;; Call reftex-do-citation, but protected
654 (reftex-do-citation current-prefix-arg no-insert format-key
)
655 (reftex-kill-temporary-buffers)))
657 (defun reftex-do-citation (&optional arg no-insert format-key
)
658 "This really does the work of `reftex-citation'."
659 (let* ((format (reftex-figure-out-cite-format arg no-insert format-key
))
660 (docstruct-symbol reftex-docstruct-symbol
)
661 (selected-entries (reftex-offer-bib-menu))
662 (insert-entries selected-entries
)
663 entry string cite-view
)
665 (unless selected-entries
(error "Quit"))
667 (if (stringp selected-entries
)
669 (setq insert-entries
(list (list selected-entries
670 (cons "&key" selected-entries
)))
671 selected-entries nil
)
672 ;; It makes sense to compute the cite-view strings.
675 (when (eq (car selected-entries
) 'concat
)
676 ;; All keys go into a single command - we need to trick a little
677 ;; FIXME: Unfortunately, this means that commenting does not work right.
678 (pop selected-entries
)
679 (let ((concat-keys (mapconcat 'car selected-entries
680 reftex-cite-key-separator
)))
682 (list (list concat-keys
(cons "&key" concat-keys
))))))
686 ;; We shall insert this into the buffer...
687 (message "Formatting...")
689 (while (setq entry
(pop insert-entries
))
690 ;; Format the citation and insert it
691 (setq string
(if reftex-format-cite-function
692 (funcall reftex-format-cite-function
693 (reftex-get-bib-field "&key" entry
)
695 (reftex-format-citation entry format
)))
696 (when (or (eq reftex-cite-prompt-optional-args t
)
697 (and reftex-cite-prompt-optional-args
699 (let ((start 0) (nth 0) value
)
700 (while (setq start
(string-match "\\[\\]" string start
))
701 (setq value
(save-match-data
702 (read-string (format "Optional argument %d: "
703 (setq nth
(1+ nth
))))))
704 (setq string
(replace-match (concat "[" value
"]") t t string
))
705 (setq start
(1+ start
)))))
706 ;; Should we cleanup empty optional arguments?
707 ;; if the first is empty, it can be removed. If the second is empty,
708 ;; it has to go. If there is only a single arg and empty, it can go
710 (when reftex-cite-cleanup-optional-args
712 ((string-match "\\([a-zA-Z0-9]\\)\\[\\]{" string
)
713 (setq string
(replace-match "\\1{" nil nil string
)))
714 ((string-match "\\[\\]\\(\\[[a-zA-Z0-9., ]+\\]\\)" string
)
715 (setq string
(replace-match "\\1" nil nil string
)))
716 ((string-match "\\[\\]\\[\\]" string
)
717 (setq string
(replace-match "" t t string
)))))
720 ;; Reposition cursor?
721 (when (string-match "\\?" string
)
722 (search-backward "?")
726 (when (and reftex-mode
727 (fboundp 'LaTeX-add-bibitems
)
728 reftex-plug-into-AUCTeX
)
729 (apply 'LaTeX-add-bibitems
(mapcar 'car selected-entries
)))
731 ;; Produce the cite-view strings
732 (when (and reftex-mode reftex-cache-cite-echo cite-view
)
733 (mapc (lambda (entry)
734 (reftex-make-cite-echo-string entry docstruct-symbol
))
739 (set-marker reftex-select-return-marker nil
)
740 (reftex-kill-buffer "*RefTeX Select*")
742 ;; Check if the prefix arg was numeric, and call recursively
746 (skip-chars-backward "}")
748 (reftex-do-citation arg
))
751 ;; Return the citation key
752 (mapcar 'car selected-entries
)))
754 (defun reftex-figure-out-cite-format (arg &optional no-insert format-key
)
755 "Check if there is already a cite command at point and change cite format
756 in order to only add another reference in the same cite command."
757 (let ((macro (car (reftex-what-macro 1)))
758 (cite-format-value (reftex-get-cite-format))
762 ;; Format does not really matter because nothing will be inserted.
765 ((and (stringp macro
)
766 (string-match "\\`\\\\cite\\|cite\\'" macro
))
767 ;; We are already inside a cite macro
768 (if (or (not arg
) (not (listp arg
)))
771 (if (member (preceding-char) '(?\
{ ?
,))
773 reftex-cite-key-separator
)
775 (if (member (following-char) '(?\
} ?
,))
777 reftex-cite-key-separator
)))
780 ;; Figure out the correct format
782 (if (and (symbolp cite-format-value
)
783 (assq cite-format-value reftex-cite-format-builtin
))
784 (nth 2 (assq cite-format-value reftex-cite-format-builtin
))
789 (reftex-select-with-char
790 "" (concat "SELECT A CITATION FORMAT\n\n"
793 (format "[%c] %s %s" (car x
)
794 (if (> (car x
) 31) " " "")
797 (if (assq key format
)
798 (setq format
(cdr (assq key format
)))
799 (error "No citation format associated with key `%c'" key
)))))
803 (defun reftex-citep ()
804 "Call `reftex-citation' with a format selector `?p'."
806 (reftex-citation nil ?p
))
809 (defun reftex-citet ()
810 "Call `reftex-citation' with a format selector `?t'."
812 (reftex-citation nil ?t
))
814 (defvar reftex-select-bib-map
)
815 (defun reftex-offer-bib-menu ()
816 "Offer bib menu and return list of selected items."
817 (let ((bibtype (reftex-bib-or-thebib))
818 found-list rtn key data selected-entries
)
826 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
827 ;; using BibTeX database files.
828 (reftex-extract-bib-entries (reftex-get-bibfile-list)))
829 ((eq bibtype
'thebib
)
830 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
831 ;; using thebibliography environment.
832 (reftex-extract-bib-entries-from-thebibliography
836 'thebib
(symbol-value reftex-docstruct-symbol
))))))
837 (reftex-default-bibliography
838 (message "Using default bibliography")
839 (reftex-extract-bib-entries (reftex-default-bibliography)))
840 (t (error "No valid bibliography in this document, and no default available"))))
843 (error "Sorry, no matches found"))
845 ;; Remember where we came from
846 (setq reftex-call-back-to-this-buffer
(current-buffer))
847 (set-marker reftex-select-return-marker
(point))
850 (save-window-excursion
851 (delete-other-windows)
852 (reftex-kill-buffer "*RefTeX Select*")
853 (switch-to-buffer-other-window "*RefTeX Select*")
854 (unless (eq major-mode
'reftex-select-bib-mode
)
855 (reftex-select-bib-mode))
856 (let ((buffer-read-only nil
))
858 (reftex-insert-bib-matches found-list
))
859 (setq buffer-read-only t
)
860 (if (= 0 (buffer-size))
861 (error "No matches found"))
862 (setq truncate-lines t
)
867 reftex-citation-prompt
869 reftex-select-bib-map
871 'reftex-bibtex-selection-callback nil
))
874 (unless key
(throw 'done t
))
880 ;; Restrict with new regular expression
881 (setq found-list
(reftex-restrict-bib-matches found-list
))
882 (let ((buffer-read-only nil
))
884 (reftex-insert-bib-matches found-list
))
888 (setq selected-entries
889 (if reftex-select-marked
890 (mapcar 'car
(nreverse reftex-select-marked
))
894 ;; Take all (marked), and push the symbol 'concat
895 (setq selected-entries
897 (if reftex-select-marked
898 (mapcar 'car
(nreverse reftex-select-marked
))
902 ;; Take all (marked), and push the symbol 'concat
903 (reftex-extract-bib-file found-list reftex-select-marked
)
904 (setq selected-entries
"BibTeX database file created")
907 ;; Take all (marked), and push the symbol 'concat
908 (reftex-extract-bib-file found-list reftex-select-marked
910 (setq selected-entries
"BibTeX database file created")
915 (setq selected-entries
916 (if reftex-select-marked
918 (mapcar 'car
(nreverse reftex-select-marked
)))
919 (if data
(list data
) nil
)))
922 ;; Got this one with completion
923 (setq selected-entries key
)
929 (defun reftex-restrict-bib-matches (found-list)
930 "Limit FOUND-LIST with more regular expressions."
931 (let ((re-list (split-string (read-string
932 "RegExp [ && RegExp...]: "
933 nil
'reftex-cite-regexp-hist
)
935 (found-list-r found-list
)
937 (while (setq re
(pop re-list
))
943 re
(cdr (assoc "&entry" x
)))
952 (defun reftex-extract-bib-file (all &optional marked complement
)
953 "Limit FOUND-LIST with more regular expressions."
954 (let ((file (read-file-name "File to create: ")))
955 (find-file-other-window file
)
956 (if (> (buffer-size) 0)
958 (format "Overwrite non-empty file %s? " file
))
965 (if (or (and (assoc x marked
) (not complement
))
966 (and (not (assoc x marked
)) complement
))
967 (cdr (assoc "&entry" x
))
969 (cdr (assoc "&entry" x
))))
971 (insert (mapconcat 'identity all
"\n\n"))
973 (goto-char (point-min))))
975 (defun reftex-insert-bib-matches (list)
976 "Insert the bib matches and number them correctly."
978 (if (memq reftex-highlight-selection
'(mouse both
))
979 reftex-mouse-selected-face
984 (setq tmp
(cdr (assoc "&formatted" x
))
986 (put-text-property 0 len
:data x tmp
)
987 (put-text-property 0 (1- len
) 'mouse-face mouse-face tmp
)
990 (run-hooks 'reftex-display-copied-context-hook
))
992 (defun reftex-format-names (namelist n
)
993 (let (last (len (length namelist
)))
994 (if (= n
0) (setq n len
))
997 ((= 1 len
) (car namelist
))
998 ((> len n
) (concat (car namelist
) (nth 2 reftex-cite-punctuation
)))
1001 last
(nth (1- n
) namelist
))
1002 (setcdr (nthcdr (- n
2) namelist
) nil
)
1004 (mapconcat 'identity namelist
(nth 0 reftex-cite-punctuation
))
1005 (nth 1 reftex-cite-punctuation
)
1008 (defun reftex-format-citation (entry format
)
1009 "Format a citation from the info in the BibTeX ENTRY according to FORMAT."
1010 (unless (stringp format
) (setq format
"\\cite{%l}"))
1012 (if (and reftex-comment-citations
1013 (string-match "%l" reftex-cite-comment-format
))
1014 (error "reftex-cite-comment-format contains invalid %%l"))
1016 (while (string-match
1017 "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
1019 (let ((n (string-to-number (match-string 4 format
)))
1020 (l (string-to-char (match-string 5 format
)))
1026 (reftex-get-bib-field "&key" entry
)
1027 (if reftex-comment-citations
1028 reftex-cite-comment-format
1030 ((= l ?a
) (reftex-format-names
1031 (reftex-get-bib-names "author" entry
)
1033 ((= l ?A
) (car (reftex-get-bib-names "author" entry
)))
1034 ((= l ?b
) (reftex-get-bib-field "booktitle" entry
"in: %s"))
1035 ((= l ?B
) (reftex-abbreviate-title
1036 (reftex-get-bib-field "booktitle" entry
"in: %s")))
1037 ((= l ?c
) (reftex-get-bib-field "chapter" entry
))
1038 ((= l ?d
) (reftex-get-bib-field "edition" entry
))
1039 ((= l ?e
) (reftex-format-names
1040 (reftex-get-bib-names "editor" entry
)
1042 ((= l ?E
) (car (reftex-get-bib-names "editor" entry
)))
1043 ((= l ?h
) (reftex-get-bib-field "howpublished" entry
))
1044 ((= l ?i
) (reftex-get-bib-field "institution" entry
))
1045 ((= l ?j
) (reftex-get-bib-field "journal" entry
))
1046 ((= l ?k
) (reftex-get-bib-field "key" entry
))
1047 ((= l ?m
) (reftex-get-bib-field "month" entry
))
1048 ((= l ?n
) (reftex-get-bib-field "number" entry
))
1049 ((= l ?N
) (reftex-get-bib-field "note" entry
))
1050 ((= l ?o
) (reftex-get-bib-field "organization" entry
))
1051 ((= l ?p
) (reftex-get-bib-field "pages" entry
))
1052 ((= l ?P
) (car (split-string
1053 (reftex-get-bib-field "pages" entry
)
1055 ((= l ?s
) (reftex-get-bib-field "school" entry
))
1056 ((= l ?u
) (reftex-get-bib-field "publisher" entry
))
1057 ((= l ?U
) (reftex-get-bib-field "url" entry
))
1058 ((= l ?r
) (reftex-get-bib-field "address" entry
))
1059 ((= l ?t
) (reftex-get-bib-field "title" entry
))
1060 ((= l ?T
) (reftex-abbreviate-title
1061 (reftex-get-bib-field "title" entry
)))
1062 ((= l ?v
) (reftex-get-bib-field "volume" entry
))
1063 ((= l ?y
) (reftex-get-bib-field "year" entry
)))))
1065 (if (string= rpl
"")
1066 (setq b
(match-beginning 2) e
(match-end 2))
1067 (setq b
(match-beginning 3) e
(match-end 3)))
1068 (setq format
(concat (substring format
0 b
) rpl
(substring format e
)))))
1069 (while (string-match "%%" format
)
1070 (setq format
(replace-match "%" t t format
)))
1071 (while (string-match "[ ,.;:]*%<" format
)
1072 (setq format
(replace-match "" t t format
)))
1076 (defun reftex-make-cite-echo-string (entry docstruct-symbol
)
1077 "Format a bibtex ENTRY for the echo area and cache the result."
1078 (let* ((key (reftex-get-bib-field "&key" entry
))
1080 (let* ((reftex-cite-punctuation '(" " " & " " etal.")))
1081 (reftex-format-citation entry reftex-cite-view-format
)))
1082 (cache (assq 'bibview-cache
(symbol-value docstruct-symbol
)))
1083 (cache-entry (assoc key
(cdr cache
))))
1085 ;; This docstruct has no cache - make one.
1086 (set docstruct-symbol
(cons (cons 'bibview-cache nil
)
1087 (symbol-value docstruct-symbol
))))
1088 (when reftex-cache-cite-echo
1089 (setq key
(copy-sequence key
))
1090 (set-text-properties 0 (length key
) nil key
)
1091 (set-text-properties 0 (length string
) nil string
)
1093 (unless (string= (cdr cache-entry
) string
)
1094 (setcdr cache-entry string
)
1095 (put reftex-docstruct-symbol
'modified t
))
1096 (push (cons key string
) (cdr cache
))
1097 (put reftex-docstruct-symbol
'modified t
)))
1100 (defun reftex-bibtex-selection-callback (data ignore no-revisit
)
1101 "Callback function to be called from the BibTeX selection, in
1102 order to display context. This function is relatively slow and not
1103 recommended for follow mode. It works OK for individual lookups."
1104 (let ((win (selected-window))
1105 (key (reftex-get-bib-field "&key" data
))
1106 bibfile-list item bibtype
)
1109 (with-current-buffer reftex-call-back-to-this-buffer
1110 (setq bibtype
(reftex-bib-or-thebib))
1113 ; ((assq 'bib (symbol-value reftex-docstruct-symbol))
1114 (setq bibfile-list
(reftex-get-bibfile-list)))
1115 ((eq bibtype
'thebib
)
1116 ; ((assq 'thebib (symbol-value reftex-docstruct-symbol))
1121 'thebib
(symbol-value reftex-docstruct-symbol
))))
1123 (reftex-default-bibliography
1124 (setq bibfile-list
(reftex-default-bibliography)))
1125 (t (ding) (throw 'exit nil
))))
1128 (setq bibfile-list
(reftex-visited-files bibfile-list
)))
1131 (reftex-pop-to-bibtex-entry
1132 key bibfile-list
(not reftex-keep-temporary-buffers
) t item
)
1135 (select-window win
)))
1137 ;;; Global BibTeX file
1138 (defun reftex-all-used-citation-keys ()
1139 (reftex-access-scan-info)
1140 (let ((files (reftex-all-document-files)) file keys kk k
)
1141 (save-current-buffer
1142 (while (setq file
(pop files
))
1143 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1147 (goto-char (point-min))
1148 (while (re-search-forward "\\(?:^\\|\\=\\)[^%\n\r]*?\\\\\\(bibentry\\|[a-zA-Z]*cite[a-zA-Z]*\\)\\(\\[[^]]*\\]\\)?{\\([^}]+\\)}" nil t
)
1149 (setq kk
(match-string-no-properties 3))
1150 (while (string-match "%.*\n?" kk
)
1151 (setq kk
(replace-match "" t t kk
)))
1152 (setq kk
(split-string kk
"[, \t\r\n]+"))
1153 (while (setq k
(pop kk
))
1155 (setq keys
(cons k keys
)))))))))
1156 (reftex-kill-temporary-buffers)
1159 (defun reftex-get-string-refs (alist)
1160 "Return a list of BibTeX @string references that appear as values in ALIST."
1161 (reftex-remove-if (lambda (x) (string-match "^\\([\"{]\\|[0-9]+$\\)" x
))
1162 ;; get list of values, discard keys
1164 ;; remove &key and &type entries
1165 (reftex-remove-if (lambda (pair)
1166 (string-match "^&" (car pair
)))
1170 (defun reftex-create-bibtex-file (bibfile)
1171 "Create a new BibTeX database BIBFILE with all entries referenced in document.
1172 The command prompts for a filename and writes the collected
1173 entries to that file. Only entries referenced in the current
1174 document with any \\cite-like macros are used. The sequence in
1175 the new file is the same as it was in the old database.
1177 Entries referenced from other entries must appear after all
1178 referencing entries.
1180 You can define strings to be used as header or footer for the
1181 created files in the variables `reftex-create-bibtex-header' or
1182 `reftex-create-bibtex-footer' respectively."
1183 (interactive "FNew BibTeX file: ")
1184 (let ((keys (reftex-all-used-citation-keys))
1185 (files (reftex-get-bibfile-list))
1186 file key entries beg end entry string-keys string-entries
)
1187 (save-current-buffer
1188 (dolist (file files
)
1189 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1190 (reftex-with-special-syntax-for-bib
1194 (goto-char (point-min))
1195 (while (re-search-forward "^[ \t]*@\\(?:\\w\\|\\s_\\)+[ \t\n\r]*\
1196 [{(][ \t\n\r]*\\([^ \t\n\r,]+\\)" nil t
)
1197 (setq key
(match-string 1)
1198 beg
(match-beginning 0)
1200 (goto-char (match-beginning 1))
1203 (error (goto-char (match-end 0))))
1205 (when (member key keys
)
1206 (setq entry
(buffer-substring beg end
)
1207 entries
(cons entry entries
)
1208 keys
(delete key keys
))
1210 ;; check for crossref entries
1211 (let* ((attr-list (reftex-parse-bibtex-entry nil beg end
))
1212 (xref-key (cdr (assoc "crossref" attr-list
))))
1213 (if xref-key
(pushnew xref-key keys
)))
1214 ;; check for string references
1215 (let* ((raw-fields (reftex-parse-bibtex-entry nil beg end t
))
1216 (string-fields (reftex-get-string-refs raw-fields
)))
1217 (dolist (skey string-fields
)
1218 (unless (member skey string-keys
)
1219 (push skey string-keys
)))))))))))
1220 ;; second pass: grab @string references
1222 (save-current-buffer
1223 (dolist (file files
)
1224 (set-buffer (reftex-get-file-buffer-force file
'mark
))
1225 (reftex-with-special-syntax-for-bib
1229 (goto-char (point-min))
1230 (while (re-search-forward
1231 "^[ \t]*@[Ss][Tt][Rr][Ii][Nn][Gg][ \t]*{[ \t]*\\([^ \t\r\n]+\\)"
1233 (setq key
(match-string 1)
1234 beg
(match-beginning 0)
1236 (goto-char (match-beginning 1))
1239 (error (goto-char (match-end 0))))
1241 (when (member key string-keys
)
1242 (setq entry
(buffer-substring beg end
)
1243 string-entries
(cons entry string-entries
)
1244 string-keys
(delete key string-keys
))))))))))
1245 (find-file-other-window bibfile
)
1246 (if (> (buffer-size) 0)
1247 (unless (yes-or-no-p
1248 (format "Overwrite non-empty file %s? " bibfile
))
1251 (if reftex-create-bibtex-header
(insert reftex-create-bibtex-header
"\n\n"))
1252 (insert (mapconcat 'identity
(reverse string-entries
) "\n\n"))
1253 (if string-entries
(insert "\n\n\n"))
1254 (insert (mapconcat 'identity
(reverse entries
) "\n\n"))
1255 (if reftex-create-bibtex-footer
(insert "\n\n" reftex-create-bibtex-footer
))
1256 (goto-char (point-min))
1258 (message "%d entries extracted and copied to new database"
1261 (provide 'reftex-cite
)
1262 ;;; reftex-cite.el ends here
1265 ;; generated-autoload-file: "reftex.el"