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