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