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