(bibtex-global-key-alist, bibtex-initialize): Exclude current buffer
[emacs.git] / lisp / textmodes / bibtex.el
blobebe17d7e809c5ded6c613fedd599c3cf60e98385
1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de>
7 ;; Bengt Martensson <bengt@mathematik.uni-Bremen.de>
8 ;; Marc Shapiro <marc.shapiro@acm.org>
9 ;; Mike Newton <newton@gumby.cs.caltech.edu>
10 ;; Aaron Larson <alarson@src.honeywell.com>
11 ;; Dirk Herrmann <D.Herrmann@tu-bs.de>
12 ;; Maintainer: Roland Winkler <roland.winkler@physik.uni-erlangen.de>
13 ;; Keywords: BibTeX, LaTeX, TeX
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;;; Commentary:
32 ;; Major mode for editing and validating BibTeX files.
34 ;; Usage:
35 ;; See documentation for `bibtex-mode' or type "M-x describe-mode"
36 ;; when you are in BibTeX mode.
38 ;; Todo:
39 ;; Distribute texinfo file.
41 ;;; Code:
43 (require 'button)
46 ;; User Options:
48 (defgroup bibtex nil
49 "BibTeX mode."
50 :group 'tex
51 :prefix "bibtex-")
53 (defgroup bibtex-autokey nil
54 "Generate automatically a key from the author/editor and the title field."
55 :group 'bibtex
56 :prefix "bibtex-autokey-")
58 (defcustom bibtex-mode-hook nil
59 "List of functions to call on entry to BibTeX mode."
60 :group 'bibtex
61 :type 'hook)
63 (defcustom bibtex-field-delimiters 'braces
64 "Type of field delimiters. Allowed values are `braces' or `double-quotes'."
65 :group 'bibtex
66 :type '(choice (const braces)
67 (const double-quotes)))
69 (defcustom bibtex-entry-delimiters 'braces
70 "Type of entry delimiters. Allowed values are `braces' or `parentheses'."
71 :group 'bibtex
72 :type '(choice (const braces)
73 (const parentheses)))
75 (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
76 "List of BibTeX entries that get an OPTcrossref field."
77 :group 'bibtex
78 :type '(repeat string))
80 (defcustom bibtex-include-OPTkey t
81 "If non-nil, all newly created entries get an OPTkey field.
82 If this is a string, use it as the initial field text.
83 If this is a function, call it to generate the initial field text."
84 :group 'bibtex
85 :type '(choice (const :tag "None" nil)
86 (string :tag "Initial text")
87 (function :tag "Initialize Function")
88 (const :tag "Default" t)))
89 (put 'bibtex-include-OPTkey 'risky-local-variable t)
91 (defcustom bibtex-user-optional-fields
92 '(("annote" "Personal annotation (ignored)"))
93 "List of optional fields the user wants to have always present.
94 Entries should be of the same form as the OPTIONAL and
95 CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (which see)."
96 :group 'bibtex
97 :type '(repeat (group (string :tag "Field")
98 (string :tag "Comment")
99 (option (choice :tag "Init"
100 (const nil) string function)))))
101 (put 'bibtex-user-optional-fields 'risky-local-variable t)
103 (defcustom bibtex-entry-format
104 '(opts-or-alts required-fields numerical-fields)
105 "Type of formatting performed by `bibtex-clean-entry'.
106 It may be t, nil, or a list of symbols out of the following:
107 opts-or-alts Delete empty optional and alternative fields and
108 remove OPT and ALT prefixes from used fields.
109 required-fields Signal an error if a required field is missing.
110 numerical-fields Delete delimiters around numeral fields.
111 page-dashes Change double dashes in page field to single dash
112 (for scribe compatibility).
113 whitespace Delete whitespace at the beginning and end of fields.
114 inherit-booktitle If entry contains a crossref field and the booktitle
115 field is empty, set the booktitle field to the content
116 of the title field of the crossreferenced entry.
117 realign Realign entries, so that field texts and perhaps equal
118 signs (depending on the value of
119 `bibtex-align-at-equal-sign') begin in the same column.
120 Also fill fields.
121 last-comma Add or delete comma on end of last field in entry,
122 according to value of `bibtex-comma-after-last-field'.
123 delimiters Change delimiters according to variables
124 `bibtex-field-delimiters' and `bibtex-entry-delimiters'.
125 unify-case Change case of entry types and field names.
126 braces Enclose parts of field entries by braces according to
127 `bibtex-field-braces-alist'.
128 strings Replace parts of field entries by string constants
129 according to `bibtex-field-strings-alist'.
131 The value t means do all of the above formatting actions.
132 The value nil means do no formatting at all."
133 :group 'bibtex
134 :type '(choice (const :tag "None" nil)
135 (const :tag "All" t)
136 (set :menu-tag "Some"
137 (const opts-or-alts)
138 (const required-fields)
139 (const numerical-fields)
140 (const page-dashes)
141 (const whitespace)
142 (const inherit-booktitle)
143 (const realign)
144 (const last-comma)
145 (const delimiters)
146 (const unify-case)
147 (const braces)
148 (const strings))))
149 (put 'bibtex-entry-format 'safe-local-variable
150 (lambda (x)
151 (or (eq x t)
152 (let ((OK t))
153 (while (consp x)
154 (unless (memq (pop x)
155 '(opts-or-alts required-fields numerical-fields
156 page-dashes whitespace inherit-booktitle realign
157 last-comma delimiters unify-case braces strings))
158 (setq OK nil)))
159 (unless (null x) (setq OK nil))
160 OK))))
162 (defcustom bibtex-field-braces-alist nil
163 "Alist of field regexps that \\[bibtex-clean-entry] encloses by braces.
164 Each element has the form (FIELDS REGEXP), where FIELDS is a list
165 of BibTeX field names and REGEXP is a regexp.
166 Whitespace in REGEXP will be replaced by \"[ \\t\\n]+\"."
167 :group 'bibtex
168 :type '(repeat (list (repeat (string :tag "field name"))
169 (choice (regexp :tag "regexp")
170 (sexp :tag "sexp")))))
172 (defcustom bibtex-field-strings-alist nil
173 "Alist of regexps that \\[bibtex-clean-entry] replaces by string constants.
174 Each element has the form (FIELDS REGEXP TO-STR), where FIELDS is a list
175 of BibTeX field names. In FIELDS search for REGEXP, which are replaced
176 by the BibTeX string constant TO-STR.
177 Whitespace in REGEXP will be replaced by \"[ \\t\\n]+\"."
178 :group 'bibtex
179 :type '(repeat (list (repeat (string :tag "field name"))
180 (regexp :tag "From regexp")
181 (regexp :tag "To string constant"))))
183 (defcustom bibtex-clean-entry-hook nil
184 "List of functions to call when entry has been cleaned.
185 Functions are called with point inside the cleaned entry, and the buffer
186 narrowed to just the entry."
187 :group 'bibtex
188 :type 'hook)
190 (defcustom bibtex-maintain-sorted-entries nil
191 "If non-nil, BibTeX mode maintains all entries in sorted order.
192 Allowed non-nil values are:
193 plain or t All entries are sorted alphabetically.
194 crossref All entries are sorted alphabetically unless an entry has a
195 crossref field. These crossrefed entries are placed in
196 alphabetical order immediately preceding the main entry.
197 entry-class The entries are divided into classes according to their
198 entry type, see `bibtex-sort-entry-class'. Within each class
199 the entries are sorted alphabetically.
200 See also `bibtex-sort-ignore-string-entries'."
201 :group 'bibtex
202 :type '(choice (const nil)
203 (const plain)
204 (const crossref)
205 (const entry-class)
206 (const t)))
207 (put 'bibtex-maintain-sorted-entries 'safe-local-variable
208 '(lambda (a) (memq a '(nil t plain crossref entry-class))))
210 (defcustom bibtex-sort-entry-class
211 '(("String")
212 (catch-all)
213 ("Book" "Proceedings"))
214 "List of classes of BibTeX entry types, used for sorting entries.
215 If value of `bibtex-maintain-sorted-entries' is `entry-class'
216 entries are ordered according to the classes they belong to. Each
217 class contains a list of entry types. An entry `catch-all' applies
218 to all entries not explicitly mentioned."
219 :group 'BibTeX
220 :type '(repeat (choice :tag "Class"
221 (const :tag "catch-all" (catch-all))
222 (repeat :tag "Entry type" string))))
223 (put 'bibtex-sort-entry-class 'safe-local-variable
224 (lambda (x) (let ((OK t))
225 (while (consp x)
226 (let ((y (pop x)))
227 (while (consp y)
228 (let ((z (pop y)))
229 (unless (or (stringp z) (eq z 'catch-all))
230 (setq OK nil))))
231 (unless (null y) (setq OK nil))))
232 (unless (null x) (setq OK nil))
233 OK)))
235 (defcustom bibtex-sort-ignore-string-entries t
236 "If non-nil, BibTeX @String entries are not sort-significant.
237 That means they are ignored when determining ordering of the buffer
238 \(e.g., sorting, locating alphabetical position for new entries, etc.)."
239 :group 'bibtex
240 :type 'boolean)
242 (defcustom bibtex-field-kill-ring-max 20
243 "Max length of `bibtex-field-kill-ring' before discarding oldest elements."
244 :group 'bibtex
245 :type 'integer)
247 (defcustom bibtex-entry-kill-ring-max 20
248 "Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
249 :group 'bibtex
250 :type 'integer)
252 (defcustom bibtex-parse-keys-timeout 60
253 "Time interval in seconds for parsing BibTeX buffers during idle time.
254 Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
255 :group 'bibtex
256 :type 'integer)
258 (defcustom bibtex-parse-keys-fast t
259 "If non-nil, use fast but simplified algorithm for parsing BibTeX keys.
260 If parsing fails, try to set this variable to nil."
261 :group 'bibtex
262 :type 'boolean)
264 (defcustom bibtex-entry-field-alist
265 '(("Article"
266 ((("author" "Author1 [and Author2 ...] [and others]")
267 ("title" "Title of the article (BibTeX converts it to lowercase)")
268 ("journal" "Name of the journal (use string, remove braces)")
269 ("year" "Year of publication"))
270 (("volume" "Volume of the journal")
271 ("number" "Number of the journal (only allowed if entry contains volume)")
272 ("pages" "Pages in the journal")
273 ("month" "Month of the publication as a string (remove braces)")
274 ("note" "Remarks to be put at the end of the \\bibitem")))
275 ((("author" "Author1 [and Author2 ...] [and others]")
276 ("title" "Title of the article (BibTeX converts it to lowercase)"))
277 (("pages" "Pages in the journal")
278 ("journal" "Name of the journal (use string, remove braces)")
279 ("year" "Year of publication")
280 ("volume" "Volume of the journal")
281 ("number" "Number of the journal")
282 ("month" "Month of the publication as a string (remove braces)")
283 ("note" "Remarks to be put at the end of the \\bibitem"))))
284 ("Book"
285 ((("author" "Author1 [and Author2 ...] [and others]" nil t)
286 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
287 ("title" "Title of the book")
288 ("publisher" "Publishing company")
289 ("year" "Year of publication"))
290 (("volume" "Volume of the book in the series")
291 ("number" "Number of the book in a small series (overwritten by volume)")
292 ("series" "Series in which the book appeared")
293 ("address" "Address of the publisher")
294 ("edition" "Edition of the book as a capitalized English word")
295 ("month" "Month of the publication as a string (remove braces)")
296 ("note" "Remarks to be put at the end of the \\bibitem")))
297 ((("author" "Author1 [and Author2 ...] [and others]" nil t)
298 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
299 ("title" "Title of the book"))
300 (("publisher" "Publishing company")
301 ("year" "Year of publication")
302 ("volume" "Volume of the book in the series")
303 ("number" "Number of the book in a small series (overwritten by volume)")
304 ("series" "Series in which the book appeared")
305 ("address" "Address of the publisher")
306 ("edition" "Edition of the book as a capitalized English word")
307 ("month" "Month of the publication as a string (remove braces)")
308 ("note" "Remarks to be put at the end of the \\bibitem"))))
309 ("Booklet"
310 ((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
311 (("author" "Author1 [and Author2 ...] [and others]")
312 ("howpublished" "The way in which the booklet was published")
313 ("address" "Address of the publisher")
314 ("month" "Month of the publication as a string (remove braces)")
315 ("year" "Year of publication")
316 ("note" "Remarks to be put at the end of the \\bibitem"))))
317 ("InBook"
318 ((("author" "Author1 [and Author2 ...] [and others]" nil t)
319 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
320 ("title" "Title of the book")
321 ("chapter" "Chapter in the book")
322 ("publisher" "Publishing company")
323 ("year" "Year of publication"))
324 (("volume" "Volume of the book in the series")
325 ("number" "Number of the book in a small series (overwritten by volume)")
326 ("series" "Series in which the book appeared")
327 ("type" "Word to use instead of \"chapter\"")
328 ("address" "Address of the publisher")
329 ("edition" "Edition of the book as a capitalized English word")
330 ("month" "Month of the publication as a string (remove braces)")
331 ("pages" "Pages in the book")
332 ("note" "Remarks to be put at the end of the \\bibitem")))
333 ((("author" "Author1 [and Author2 ...] [and others]" nil t)
334 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t)
335 ("title" "Title of the book")
336 ("chapter" "Chapter in the book"))
337 (("pages" "Pages in the book")
338 ("publisher" "Publishing company")
339 ("year" "Year of publication")
340 ("volume" "Volume of the book in the series")
341 ("number" "Number of the book in a small series (overwritten by volume)")
342 ("series" "Series in which the book appeared")
343 ("type" "Word to use instead of \"chapter\"")
344 ("address" "Address of the publisher")
345 ("edition" "Edition of the book as a capitalized English word")
346 ("month" "Month of the publication as a string (remove braces)")
347 ("note" "Remarks to be put at the end of the \\bibitem"))))
348 ("InCollection"
349 ((("author" "Author1 [and Author2 ...] [and others]")
350 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
351 ("booktitle" "Name of the book")
352 ("publisher" "Publishing company")
353 ("year" "Year of publication"))
354 (("editor" "Editor1 [and Editor2 ...] [and others]")
355 ("volume" "Volume of the book in the series")
356 ("number" "Number of the book in a small series (overwritten by volume)")
357 ("series" "Series in which the book appeared")
358 ("type" "Word to use instead of \"chapter\"")
359 ("chapter" "Chapter in the book")
360 ("pages" "Pages in the book")
361 ("address" "Address of the publisher")
362 ("edition" "Edition of the book as a capitalized English word")
363 ("month" "Month of the publication as a string (remove braces)")
364 ("note" "Remarks to be put at the end of the \\bibitem")))
365 ((("author" "Author1 [and Author2 ...] [and others]")
366 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
367 ("booktitle" "Name of the book"))
368 (("pages" "Pages in the book")
369 ("publisher" "Publishing company")
370 ("year" "Year of publication")
371 ("editor" "Editor1 [and Editor2 ...] [and others]")
372 ("volume" "Volume of the book in the series")
373 ("number" "Number of the book in a small series (overwritten by volume)")
374 ("series" "Series in which the book appeared")
375 ("type" "Word to use instead of \"chapter\"")
376 ("chapter" "Chapter in the book")
377 ("address" "Address of the publisher")
378 ("edition" "Edition of the book as a capitalized English word")
379 ("month" "Month of the publication as a string (remove braces)")
380 ("note" "Remarks to be put at the end of the \\bibitem"))))
381 ("InProceedings"
382 ((("author" "Author1 [and Author2 ...] [and others]")
383 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
384 ("booktitle" "Name of the conference proceedings")
385 ("year" "Year of publication"))
386 (("editor" "Editor1 [and Editor2 ...] [and others]")
387 ("volume" "Volume of the conference proceedings in the series")
388 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
389 ("series" "Series in which the conference proceedings appeared")
390 ("pages" "Pages in the conference proceedings")
391 ("address" "Location of the Proceedings")
392 ("month" "Month of the publication as a string (remove braces)")
393 ("organization" "Sponsoring organization of the conference")
394 ("publisher" "Publishing company, its location")
395 ("note" "Remarks to be put at the end of the \\bibitem")))
396 ((("author" "Author1 [and Author2 ...] [and others]")
397 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)"))
398 (("booktitle" "Name of the conference proceedings")
399 ("pages" "Pages in the conference proceedings")
400 ("year" "Year of publication")
401 ("editor" "Editor1 [and Editor2 ...] [and others]")
402 ("volume" "Volume of the conference proceedings in the series")
403 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
404 ("series" "Series in which the conference proceedings appeared")
405 ("address" "Location of the Proceedings")
406 ("month" "Month of the publication as a string (remove braces)")
407 ("organization" "Sponsoring organization of the conference")
408 ("publisher" "Publishing company, its location")
409 ("note" "Remarks to be put at the end of the \\bibitem"))))
410 ("Manual"
411 ((("title" "Title of the manual"))
412 (("author" "Author1 [and Author2 ...] [and others]")
413 ("organization" "Publishing organization of the manual")
414 ("address" "Address of the organization")
415 ("edition" "Edition of the manual as a capitalized English word")
416 ("month" "Month of the publication as a string (remove braces)")
417 ("year" "Year of publication")
418 ("note" "Remarks to be put at the end of the \\bibitem"))))
419 ("MastersThesis"
420 ((("author" "Author1 [and Author2 ...] [and others]")
421 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
422 ("school" "School where the master\'s thesis was written")
423 ("year" "Year of publication"))
424 (("type" "Type of the master\'s thesis (if other than \"Master\'s thesis\")")
425 ("address" "Address of the school (if not part of field \"school\") or country")
426 ("month" "Month of the publication as a string (remove braces)")
427 ("note" "Remarks to be put at the end of the \\bibitem"))))
428 ("Misc"
430 (("author" "Author1 [and Author2 ...] [and others]")
431 ("title" "Title of the work (BibTeX converts it to lowercase)")
432 ("howpublished" "The way in which the work was published")
433 ("month" "Month of the publication as a string (remove braces)")
434 ("year" "Year of publication")
435 ("note" "Remarks to be put at the end of the \\bibitem"))))
436 ("PhdThesis"
437 ((("author" "Author1 [and Author2 ...] [and others]")
438 ("title" "Title of the PhD. thesis")
439 ("school" "School where the PhD. thesis was written")
440 ("year" "Year of publication"))
441 (("type" "Type of the PhD. thesis")
442 ("address" "Address of the school (if not part of field \"school\") or country")
443 ("month" "Month of the publication as a string (remove braces)")
444 ("note" "Remarks to be put at the end of the \\bibitem"))))
445 ("Proceedings"
446 ((("title" "Title of the conference proceedings")
447 ("year" "Year of publication"))
448 (("booktitle" "Title of the proceedings for cross references")
449 ("editor" "Editor1 [and Editor2 ...] [and others]")
450 ("volume" "Volume of the conference proceedings in the series")
451 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
452 ("series" "Series in which the conference proceedings appeared")
453 ("address" "Location of the Proceedings")
454 ("month" "Month of the publication as a string (remove braces)")
455 ("organization" "Sponsoring organization of the conference")
456 ("publisher" "Publishing company, its location")
457 ("note" "Remarks to be put at the end of the \\bibitem"))))
458 ("TechReport"
459 ((("author" "Author1 [and Author2 ...] [and others]")
460 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
461 ("institution" "Sponsoring institution of the report")
462 ("year" "Year of publication"))
463 (("type" "Type of the report (if other than \"technical report\")")
464 ("number" "Number of the technical report")
465 ("address" "Address of the institution (if not part of field \"institution\") or country")
466 ("month" "Month of the publication as a string (remove braces)")
467 ("note" "Remarks to be put at the end of the \\bibitem"))))
468 ("Unpublished"
469 ((("author" "Author1 [and Author2 ...] [and others]")
470 ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
471 ("note" "Remarks to be put at the end of the \\bibitem"))
472 (("month" "Month of the publication as a string (remove braces)")
473 ("year" "Year of publication")))))
475 "List of BibTeX entry types and their associated fields.
476 List elements are triples
477 \(ENTRY-TYPE (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL)).
478 ENTRY-TYPE is the type of a BibTeX entry. The remaining pairs contain
479 the required and optional fields of the BibTeX entry.
480 The second pair is used if a crossref field is present
481 and the first pair is used if a crossref field is absent.
482 If the second pair is nil, the first pair is always used.
483 REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.
484 Each element of these lists is a list of the form
485 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).
486 COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.
487 FIELD-NAME is the name of the field, COMMENT-STRING is the comment that
488 appears in the echo area, INIT is either the initial content of the
489 field or a function, which is called to determine the initial content
490 of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
491 field is an alternative. ALTERNATIVE-FLAG may be t only in the
492 REQUIRED or CROSSREF-REQUIRED lists."
493 :group 'bibtex
494 :type '(repeat (group (string :tag "Entry type")
495 (group (repeat :tag "Required fields"
496 (group (string :tag "Field")
497 (string :tag "Comment")
498 (option (choice :tag "Init" :value nil
499 (const nil) string function))
500 (option (choice :tag "Alternative"
501 (const :tag "No" nil)
502 (const :tag "Yes" t)))))
503 (repeat :tag "Optional fields"
504 (group (string :tag "Field")
505 (string :tag "Comment")
506 (option (choice :tag "Init" :value nil
507 (const nil) string function)))))
508 (option :extra-offset -4
509 (group (repeat :tag "Crossref: required fields"
510 (group (string :tag "Field")
511 (string :tag "Comment")
512 (option (choice :tag "Init" :value nil
513 (const nil) string function))
514 (option (choice :tag "Alternative"
515 (const :tag "No" nil)
516 (const :tag "Yes" t)))))
517 (repeat :tag "Crossref: optional fields"
518 (group (string :tag "Field")
519 (string :tag "Comment")
520 (option (choice :tag "Init" :value nil
521 (const nil) string function)))))))))
522 (put 'bibtex-entry-field-alist 'risky-local-variable t)
524 (defcustom bibtex-comment-start "@Comment"
525 "String starting a BibTeX comment."
526 :group 'bibtex
527 :type 'string)
529 (defcustom bibtex-add-entry-hook nil
530 "List of functions to call when BibTeX entry has been inserted."
531 :group 'bibtex
532 :type 'hook)
534 (defcustom bibtex-predefined-month-strings
535 '(("jan" . "January")
536 ("feb" . "February")
537 ("mar" . "March")
538 ("apr" . "April")
539 ("may" . "May")
540 ("jun" . "June")
541 ("jul" . "July")
542 ("aug" . "August")
543 ("sep" . "September")
544 ("oct" . "October")
545 ("nov" . "November")
546 ("dec" . "December"))
547 "Alist of month string definitions used in the BibTeX style files.
548 Each element is a pair of strings (ABBREVIATION . EXPANSION)."
549 :group 'bibtex
550 :type '(repeat (cons (string :tag "Month abbreviation")
551 (string :tag "Month expansion"))))
553 (defcustom bibtex-predefined-strings
554 (append
555 bibtex-predefined-month-strings
556 '(("acmcs" . "ACM Computing Surveys")
557 ("acta" . "Acta Informatica")
558 ("cacm" . "Communications of the ACM")
559 ("ibmjrd" . "IBM Journal of Research and Development")
560 ("ibmsj" . "IBM Systems Journal")
561 ("ieeese" . "IEEE Transactions on Software Engineering")
562 ("ieeetc" . "IEEE Transactions on Computers")
563 ("ieeetcad" . "IEEE Transactions on Computer-Aided Design of Integrated Circuits")
564 ("ipl" . "Information Processing Letters")
565 ("jacm" . "Journal of the ACM")
566 ("jcss" . "Journal of Computer and System Sciences")
567 ("scp" . "Science of Computer Programming")
568 ("sicomp" . "SIAM Journal on Computing")
569 ("tcs" . "Theoretical Computer Science")
570 ("tocs" . "ACM Transactions on Computer Systems")
571 ("tods" . "ACM Transactions on Database Systems")
572 ("tog" . "ACM Transactions on Graphics")
573 ("toms" . "ACM Transactions on Mathematical Software")
574 ("toois" . "ACM Transactions on Office Information Systems")
575 ("toplas" . "ACM Transactions on Programming Languages and Systems")))
576 "Alist of string definitions used in the BibTeX style files.
577 Each element is a pair of strings (ABBREVIATION . EXPANSION)."
578 :group 'bibtex
579 :type '(repeat (cons (string :tag "String")
580 (string :tag "String expansion"))))
582 (defcustom bibtex-string-files nil
583 "List of BibTeX files containing string definitions.
584 List elements can be absolute file names or file names relative
585 to the directories specified in `bibtex-string-file-path'."
586 :group 'bibtex
587 :type '(repeat file))
589 (defvar bibtex-string-file-path (getenv "BIBINPUTS")
590 "*Colon separated list of paths to search for `bibtex-string-files'.")
592 (defcustom bibtex-files nil
593 "List of BibTeX files that are searched for entry keys.
594 List elements can be absolute file names or file names relative to the
595 directories specified in `bibtex-file-path'. If an element is a directory,
596 check all BibTeX files in this directory. If an element is the symbol
597 `bibtex-file-path', check all BibTeX files in `bibtex-file-path'."
598 :group 'bibtex
599 :type '(repeat (choice (const :tag "bibtex-file-path" bibtex-file-path)
600 directory file)))
602 (defvar bibtex-file-path (getenv "BIBINPUTS")
603 "*Colon separated list of paths to search for `bibtex-files'.")
605 (defcustom bibtex-help-message t
606 "If non-nil print help messages in the echo area on entering a new field."
607 :group 'bibtex
608 :type 'boolean)
610 (defcustom bibtex-autokey-prefix-string ""
611 "String prefix for automatically generated reference keys.
612 See `bibtex-generate-autokey' for details."
613 :group 'bibtex-autokey
614 :type 'string)
616 (defcustom bibtex-autokey-names 1
617 "Number of names to use for the automatically generated reference key.
618 Possibly more names are used according to `bibtex-autokey-names-stretch'.
619 If this variable is nil, all names are used.
620 See `bibtex-generate-autokey' for details."
621 :group 'bibtex-autokey
622 :type '(choice (const :tag "All" infty)
623 integer))
625 (defcustom bibtex-autokey-names-stretch 0
626 "Number of names that can additionally be used for reference keys.
627 These names are used only, if all names are used then.
628 See `bibtex-generate-autokey' for details."
629 :group 'bibtex-autokey
630 :type 'integer)
632 (defcustom bibtex-autokey-additional-names ""
633 "String to append to the generated key if not all names could be used.
634 See `bibtex-generate-autokey' for details."
635 :group 'bibtex-autokey
636 :type 'string)
638 (defcustom bibtex-autokey-expand-strings nil
639 "If non-nil, expand strings when extracting the content of a BibTeX field.
640 See `bibtex-generate-autokey' for details."
641 :group 'bibtex-autokey
642 :type 'boolean)
644 (defvar bibtex-autokey-transcriptions
645 '(;; language specific characters
646 ("\\\\aa" . "a") ; \aa -> a
647 ("\\\\AA" . "A") ; \AA -> A
648 ("\\\"a\\|\\\\\\\"a\\|\\\\ae" . "ae") ; "a,\"a,\ae -> ae
649 ("\\\"A\\|\\\\\\\"A\\|\\\\AE" . "Ae") ; "A,\"A,\AE -> Ae
650 ("\\\\i" . "i") ; \i -> i
651 ("\\\\j" . "j") ; \j -> j
652 ("\\\\l" . "l") ; \l -> l
653 ("\\\\L" . "L") ; \L -> L
654 ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" . "oe") ; "o,\"o,\o,\oe -> oe
655 ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" . "Oe") ; "O,\"O,\O,\OE -> Oe
656 ("\\\"s\\|\\\\\\\"s\\|\\\\3" . "ss") ; "s,\"s,\3 -> ss
657 ("\\\"u\\|\\\\\\\"u" . "ue") ; "u,\"u -> ue
658 ("\\\"U\\|\\\\\\\"U" . "Ue") ; "U,\"U -> Ue
659 ;; accents
660 ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" . "")
661 ;; braces, quotes, concatenation.
662 ("[`'\"{}#]" . "")
663 ;; spaces
664 ("\\\\?[ \t\n]+\\|~" . " "))
665 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
666 Used by the default values of `bibtex-autokey-name-change-strings' and
667 `bibtex-autokey-titleword-change-strings'. Defaults to translating some
668 language specific characters to their ASCII transcriptions, and
669 removing any character accents.")
671 (defcustom bibtex-autokey-name-change-strings
672 bibtex-autokey-transcriptions
673 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
674 Any part of a name matching OLD-REGEXP is replaced by NEW-STRING.
675 Case is significant in OLD-REGEXP. All regexps are tried in the
676 order in which they appear in the list.
677 See `bibtex-generate-autokey' for details."
678 :group 'bibtex-autokey
679 :type '(repeat (cons (regexp :tag "Old")
680 (string :tag "New"))))
682 (defcustom bibtex-autokey-name-case-convert-function 'downcase
683 "Function called for each name to perform case conversion.
684 See `bibtex-generate-autokey' for details."
685 :group 'bibtex-autokey
686 :type '(choice (const :tag "Preserve case" identity)
687 (const :tag "Downcase" downcase)
688 (const :tag "Capitalize" capitalize)
689 (const :tag "Upcase" upcase)
690 (function :tag "Conversion function")))
691 (put 'bibtex-autokey-name-case-convert-function 'safe-local-variable
692 (lambda (x) (memq x '(upcase downcase capitalize identity))))
693 (defvaralias 'bibtex-autokey-name-case-convert
694 'bibtex-autokey-name-case-convert-function)
696 (defcustom bibtex-autokey-name-length 'infty
697 "Number of characters from name to incorporate into key.
698 If this is set to anything but a number, all characters are used.
699 See `bibtex-generate-autokey' for details."
700 :group 'bibtex-autokey
701 :type '(choice (const :tag "All" infty)
702 integer))
704 (defcustom bibtex-autokey-name-separator ""
705 "String that comes between any two names in the key.
706 See `bibtex-generate-autokey' for details."
707 :group 'bibtex-autokey
708 :type 'string)
710 (defcustom bibtex-autokey-year-length 2
711 "Number of rightmost digits from the year field to incorporate into key.
712 See `bibtex-generate-autokey' for details."
713 :group 'bibtex-autokey
714 :type 'integer)
716 (defcustom bibtex-autokey-use-crossref t
717 "If non-nil use fields from crossreferenced entry if necessary.
718 If this variable is non-nil and some field has no entry, but a
719 valid crossref entry, the field from the crossreferenced entry is used.
720 See `bibtex-generate-autokey' for details."
721 :group 'bibtex-autokey
722 :type 'boolean)
724 (defcustom bibtex-autokey-titlewords 5
725 "Number of title words to use for the automatically generated reference key.
726 If this is set to anything but a number, all title words are used.
727 Possibly more words from the title are used according to
728 `bibtex-autokey-titlewords-stretch'.
729 See `bibtex-generate-autokey' for details."
730 :group 'bibtex-autokey
731 :type '(choice (const :tag "All" infty)
732 integer))
734 (defcustom bibtex-autokey-title-terminators "[.!?:;]\\|--"
735 "Regexp defining the termination of the main part of the title.
736 Case of the regexps is ignored. See `bibtex-generate-autokey' for details."
737 :group 'bibtex-autokey
738 :type 'regexp)
740 (defcustom bibtex-autokey-titlewords-stretch 2
741 "Number of words that can additionally be used from the title.
742 These words are used only, if a sentence from the title can be ended then.
743 See `bibtex-generate-autokey' for details."
744 :group 'bibtex-autokey
745 :type 'integer)
747 (defcustom bibtex-autokey-titleword-ignore
748 '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das"
749 "[^[:upper:]].*" ".*[^[:upper:]0-9].*")
750 "Determines words from the title that are not to be used in the key.
751 Each item of the list is a regexp. If a word of the title matches a
752 regexp from that list, it is not included in the title part of the key.
753 See `bibtex-generate-autokey' for details."
754 :group 'bibtex-autokey
755 :type '(repeat regexp))
757 (defcustom bibtex-autokey-titleword-case-convert-function 'downcase
758 "Function called for each titleword to perform case conversion.
759 See `bibtex-generate-autokey' for details."
760 :group 'bibtex-autokey
761 :type '(choice (const :tag "Preserve case" identity)
762 (const :tag "Downcase" downcase)
763 (const :tag "Capitalize" capitalize)
764 (const :tag "Upcase" upcase)
765 (function :tag "Conversion function")))
766 (defvaralias 'bibtex-autokey-titleword-case-convert
767 'bibtex-autokey-titleword-case-convert-function)
769 (defcustom bibtex-autokey-titleword-abbrevs nil
770 "Determines exceptions to the usual abbreviation mechanism.
771 An alist of (OLD-REGEXP . NEW-STRING) pairs. Case is ignored
772 in matching against OLD-REGEXP, and the first matching pair is used.
773 See `bibtex-generate-autokey' for details."
774 :group 'bibtex-autokey
775 :type '(repeat (cons (regexp :tag "Old")
776 (string :tag "New"))))
778 (defcustom bibtex-autokey-titleword-change-strings
779 bibtex-autokey-transcriptions
780 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
781 Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
782 Case is significant in OLD-REGEXP. All regexps are tried in the
783 order in which they appear in the list.
784 See `bibtex-generate-autokey' for details."
785 :group 'bibtex-autokey
786 :type '(repeat (cons (regexp :tag "Old")
787 (string :tag "New"))))
789 (defcustom bibtex-autokey-titleword-length 5
790 "Number of characters from title words to incorporate into key.
791 If this is set to anything but a number, all characters are used.
792 See `bibtex-generate-autokey' for details."
793 :group 'bibtex-autokey
794 :type '(choice (const :tag "All" infty)
795 integer))
797 (defcustom bibtex-autokey-titleword-separator "_"
798 "String to be put between the title words.
799 See `bibtex-generate-autokey' for details."
800 :group 'bibtex-autokey
801 :type 'string)
803 (defcustom bibtex-autokey-name-year-separator ""
804 "String to be put between name part and year part of key.
805 See `bibtex-generate-autokey' for details."
806 :group 'bibtex-autokey
807 :type 'string)
809 (defcustom bibtex-autokey-year-title-separator ":_"
810 "String to be put between year part and title part of key.
811 See `bibtex-generate-autokey' for details."
812 :group 'bibtex-autokey
813 :type 'string)
815 (defcustom bibtex-autokey-edit-before-use t
816 "If non-nil, user is allowed to edit the generated key before it is used."
817 :group 'bibtex-autokey
818 :type 'boolean)
820 (defcustom bibtex-autokey-before-presentation-function nil
821 "If non-nil, function to call before generated key is presented.
822 The function must take one argument (the automatically generated key),
823 and must return a string (the key to use)."
824 :group 'bibtex-autokey
825 :type '(choice (const nil) function))
827 (defcustom bibtex-entry-offset 0
828 "Offset for BibTeX entries.
829 Added to the value of all other variables which determine columns."
830 :group 'bibtex
831 :type 'integer)
833 (defcustom bibtex-field-indentation 2
834 "Starting column for the name part in BibTeX fields."
835 :group 'bibtex
836 :type 'integer)
838 (defcustom bibtex-text-indentation
839 (+ bibtex-field-indentation
840 (length "organization = "))
841 "Starting column for the text part in BibTeX fields.
842 Should be equal to the space needed for the longest name part."
843 :group 'bibtex
844 :type 'integer)
846 (defcustom bibtex-contline-indentation
847 (+ bibtex-text-indentation 1)
848 "Starting column for continuation lines of BibTeX fields."
849 :group 'bibtex
850 :type 'integer)
852 (defcustom bibtex-align-at-equal-sign nil
853 "If non-nil, align fields at equal sign instead of field text.
854 If non-nil, the column for the equal sign is the value of
855 `bibtex-text-indentation', minus 2."
856 :group 'bibtex
857 :type 'boolean)
859 (defcustom bibtex-comma-after-last-field nil
860 "If non-nil, a comma is put at end of last field in the entry template."
861 :group 'bibtex
862 :type 'boolean)
864 (defcustom bibtex-autoadd-commas t
865 "If non-nil automatically add missing commas at end of BibTeX fields."
866 :group 'bibtex
867 :type 'boolean)
869 (defcustom bibtex-autofill-types '("Proceedings")
870 "Automatically fill fields if possible for those BibTeX entry types."
871 :group 'bibtex
872 :type '(repeat string))
874 (defcustom bibtex-summary-function 'bibtex-summary
875 "Function to call for generating a summary of current BibTeX entry.
876 It takes no arguments. Point must be at beginning of entry.
877 Used by `bibtex-complete-crossref-cleanup' and `bibtex-copy-summary-as-kill'."
878 :group 'bibtex
879 :type '(choice (const :tag "Default" bibtex-summary)
880 (function :tag "Personalized function")))
882 (defcustom bibtex-generate-url-list
883 '((("url" . ".*:.*")))
884 "List of schemes for generating the URL of a BibTeX entry.
885 These schemes are used by `bibtex-url'.
887 Each scheme should have one of these forms:
889 ((FIELD . REGEXP))
890 ((FIELD . REGEXP) STEP...)
891 ((FIELD . REGEXP) STRING STEP...)
893 FIELD is a field name as returned by `bibtex-parse-entry'.
894 REGEXP is matched against the text of FIELD. If the match succeeds,
895 then this scheme is used. If no STRING and STEPs are specified
896 the matched text is used as the URL, otherwise the URL is built
897 by evaluating STEPs. If no STRING is specified the STEPs must result
898 in strings which are concatenated. Otherwise the resulting objects
899 are passed through `format' using STRING as format control string.
901 A STEP is a list (FIELD REGEXP REPLACE). The text of FIELD
902 is matched against REGEXP, and is replaced with REPLACE.
903 REPLACE can be a string, or a number (which selects the corresponding
904 submatch), or a function called with the field's text as argument
905 and with the `match-data' properly set.
907 Case is always ignored. Always remove the field delimiters.
908 If `bibtex-expand-strings' is non-nil, BibTeX strings are expanded
909 for generating the URL.
911 The following is a complex example, see http://link.aps.org/linkfaq.html.
913 (((\"journal\" . \"\\\\=<\\(PR[ABCDEL]?\\|RMP\\)\\\\=>\")
914 \"http://link.aps.org/abstract/%s/v%s/p%s\"
915 (\"journal\" \".*\" downcase)
916 (\"volume\" \".*\" 0)
917 (\"pages\" \"\\`[A-Z]?[0-9]+\" 0)))"
918 :group 'bibtex
919 :type '(repeat
920 (cons :tag "Scheme"
921 (cons :tag "Matcher" :extra-offset 4
922 (string :tag "BibTeX field")
923 (regexp :tag "Regexp"))
924 (choice
925 (const :tag "Take match as is" nil)
926 (cons :tag "Formatted"
927 (string :tag "Format control string")
928 (repeat :tag "Steps to generate URL"
929 (list (string :tag "BibTeX field")
930 (regexp :tag "Regexp")
931 (choice (string :tag "Replacement")
932 (integer :tag "Sub-match")
933 (function :tag "Filter")))))
934 (repeat :tag "Concatenated"
935 (list (string :tag "BibTeX field")
936 (regexp :tag "Regexp")
937 (choice (string :tag "Replacement")
938 (integer :tag "Sub-match")
939 (function :tag "Filter"))))))))
940 (put 'bibtex-generate-url-list 'risky-local-variable t)
942 (defcustom bibtex-cite-matcher-alist
943 '(("\\\\cite[ \t\n]*{\\([^}]+\\)}" . 1))
944 "Alist of rules to identify cited keys in a BibTeX entry.
945 Each rule should be of the form (REGEXP . SUBEXP), where SUBEXP
946 specifies which parenthesized expression in REGEXP is a cited key.
947 Case is significant.
948 Used by `bibtex-search-crossref' and for font-locking."
949 :group 'bibtex
950 :type '(repeat (cons (regexp :tag "Regexp")
951 (integer :tag "Number")))
952 :version "23.1")
954 (defcustom bibtex-expand-strings nil
955 "If non-nil, expand strings when extracting the content of a BibTeX field."
956 :group 'bibtex
957 :type 'boolean)
959 ;; `bibtex-font-lock-keywords' is a user option, too. But since the
960 ;; patterns used to define this variable are defined in a later
961 ;; section of this file, it is defined later.
964 ;; Syntax Table and Keybindings
965 (defvar bibtex-mode-syntax-table
966 (let ((st (make-syntax-table)))
967 (modify-syntax-entry ?\" "\"" st)
968 (modify-syntax-entry ?$ "$$ " st)
969 (modify-syntax-entry ?% "< " st)
970 (modify-syntax-entry ?' "w " st)
971 (modify-syntax-entry ?@ "w " st)
972 (modify-syntax-entry ?\\ "\\" st)
973 (modify-syntax-entry ?\f "> " st)
974 (modify-syntax-entry ?\n "> " st)
975 ;; Keys cannot have = in them (wrong font-lock of @string{foo=bar}).
976 (modify-syntax-entry ?= "." st)
977 (modify-syntax-entry ?~ " " st)
979 "Syntax table used in BibTeX mode buffers.")
981 (defvar bibtex-mode-map
982 (let ((km (make-sparse-keymap)))
983 ;; The Key `C-c&' is reserved for reftex.el
984 (define-key km "\t" 'bibtex-find-text)
985 (define-key km "\n" 'bibtex-next-field)
986 (define-key km "\M-\t" 'bibtex-complete)
987 (define-key km "\C-c\"" 'bibtex-remove-delimiters)
988 (define-key km "\C-c{" 'bibtex-remove-delimiters)
989 (define-key km "\C-c}" 'bibtex-remove-delimiters)
990 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
991 (define-key km "\C-c\C-q" 'bibtex-fill-entry)
992 (define-key km "\C-c\C-s" 'bibtex-search-entry)
993 (define-key km "\C-c\C-x" 'bibtex-search-crossref)
994 (define-key km "\C-c\C-t" 'bibtex-copy-summary-as-kill)
995 (define-key km "\C-c?" 'bibtex-print-help-message)
996 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
997 (define-key km "\C-c\C-n" 'bibtex-pop-next)
998 (define-key km "\C-c\C-k" 'bibtex-kill-field)
999 (define-key km "\C-c\M-k" 'bibtex-copy-field-as-kill)
1000 (define-key km "\C-c\C-w" 'bibtex-kill-entry)
1001 (define-key km "\C-c\M-w" 'bibtex-copy-entry-as-kill)
1002 (define-key km "\C-c\C-y" 'bibtex-yank)
1003 (define-key km "\C-c\M-y" 'bibtex-yank-pop)
1004 (define-key km "\C-c\C-d" 'bibtex-empty-field)
1005 (define-key km "\C-c\C-f" 'bibtex-make-field)
1006 (define-key km "\C-c\C-u" 'bibtex-entry-update)
1007 (define-key km "\C-c$" 'bibtex-ispell-abstract)
1008 (define-key km "\M-\C-a" 'bibtex-beginning-of-entry)
1009 (define-key km "\M-\C-e" 'bibtex-end-of-entry)
1010 (define-key km "\C-\M-l" 'bibtex-reposition-window)
1011 (define-key km "\C-\M-h" 'bibtex-mark-entry)
1012 (define-key km "\C-c\C-b" 'bibtex-entry)
1013 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
1014 (define-key km "\C-c\C-rw" 'widen)
1015 (define-key km "\C-c\C-l" 'bibtex-url)
1016 (define-key km "\C-c\C-o" 'bibtex-remove-OPT-or-ALT)
1017 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
1018 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
1019 (define-key km "\C-c\C-eI" 'bibtex-InBook)
1020 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
1021 (define-key km "\C-c\C-e\C-b" 'bibtex-InBook)
1022 (define-key km "\C-c\C-eb" 'bibtex-Book)
1023 (define-key km "\C-c\C-eB" 'bibtex-Booklet)
1024 (define-key km "\C-c\C-e\C-c" 'bibtex-InCollection)
1025 (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
1026 (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
1027 (define-key km "\C-c\C-eM" 'bibtex-Misc)
1028 (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
1029 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
1030 (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
1031 (define-key km "\C-c\C-e\M-p" 'bibtex-Preamble)
1032 (define-key km "\C-c\C-e\C-s" 'bibtex-String)
1033 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
1034 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
1036 "Keymap used in BibTeX mode.")
1038 (easy-menu-define
1039 bibtex-edit-menu bibtex-mode-map "BibTeX-Edit Menu in BibTeX mode"
1040 '("BibTeX-Edit"
1041 ("Moving inside an Entry"
1042 ["End of Field" bibtex-find-text t]
1043 ["Next Field" bibtex-next-field t]
1044 ["Beginning of Entry" bibtex-beginning-of-entry t]
1045 ["End of Entry" bibtex-end-of-entry t]
1046 "--"
1047 ["Make Entry Visible" bibtex-reposition-window t])
1048 ("Moving in BibTeX Buffers"
1049 ["Search Entry" bibtex-search-entry t]
1050 ["Search Crossref Entry" bibtex-search-crossref t])
1051 "--"
1052 ("Operating on Current Field"
1053 ["Fill Field" fill-paragraph t]
1054 ["Remove Delimiters" bibtex-remove-delimiters t]
1055 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t]
1056 ["Clear Field" bibtex-empty-field t]
1057 "--"
1058 ["Kill Field" bibtex-kill-field t]
1059 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t]
1060 ["Paste Most Recently Killed Field" bibtex-yank t]
1061 ["Paste Previously Killed Field" bibtex-yank-pop t]
1062 "--"
1063 ["Make New Field" bibtex-make-field t]
1064 "--"
1065 ["Snatch from Similar Following Field" bibtex-pop-next t]
1066 ["Snatch from Similar Preceding Field" bibtex-pop-previous t]
1067 "--"
1068 ["String or Key Complete" bibtex-complete t]
1069 "--"
1070 ["Help about Current Field" bibtex-print-help-message t])
1071 ("Operating on Current Entry"
1072 ["Fill Entry" bibtex-fill-entry t]
1073 ["Clean Entry" bibtex-clean-entry t]
1074 ["Update Entry" bibtex-entry-update t]
1075 "--"
1076 ["Kill Entry" bibtex-kill-entry t]
1077 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t]
1078 ["Paste Most Recently Killed Entry" bibtex-yank t]
1079 ["Paste Previously Killed Entry" bibtex-yank-pop t]
1080 "--"
1081 ["Copy Summary to Kill Ring" bibtex-copy-summary-as-kill t]
1082 ["Browse URL" bibtex-url t]
1083 "--"
1084 ["Ispell Entry" bibtex-ispell-entry t]
1085 ["Ispell Entry Abstract" bibtex-ispell-abstract t]
1086 "--"
1087 ["Narrow to Entry" bibtex-narrow-to-entry t]
1088 ["Mark Entry" bibtex-mark-entry t]
1089 "--"
1090 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
1091 (fboundp 'reftex-view-crossref-from-bibtex)])
1092 ("Operating on Buffer or Region"
1093 ["Validate Entries" bibtex-validate t]
1094 ["Sort Entries" bibtex-sort-buffer t]
1095 ["Reformat Entries" bibtex-reformat t]
1096 ["Count Entries" bibtex-count-entries t]
1097 "--"
1098 ["Convert Alien Buffer" bibtex-convert-alien t])
1099 ("Operating on Multiple Buffers"
1100 ["(Re)Initialize BibTeX Buffers" bibtex-initialize t]
1101 ["Validate Entries" bibtex-validate-globally t])))
1103 (easy-menu-define
1104 bibtex-entry-menu bibtex-mode-map "Entry-Types Menu in BibTeX mode"
1105 (list "Entry-Types"
1106 ["Article in Journal" bibtex-Article t]
1107 ["Article in Conference Proceedings" bibtex-InProceedings t]
1108 ["Article in a Collection" bibtex-InCollection t]
1109 ["Chapter or Pages in a Book" bibtex-InBook t]
1110 ["Conference Proceedings" bibtex-Proceedings t]
1111 ["Book" bibtex-Book t]
1112 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t]
1113 ["PhD. Thesis" bibtex-PhdThesis t]
1114 ["Master's Thesis" bibtex-MastersThesis t]
1115 ["Technical Report" bibtex-TechReport t]
1116 ["Technical Manual" bibtex-Manual t]
1117 ["Unpublished" bibtex-Unpublished t]
1118 ["Miscellaneous" bibtex-Misc t]
1119 "--"
1120 ["String" bibtex-String t]
1121 ["Preamble" bibtex-Preamble t]))
1124 ;; Internal Variables
1126 (defvar bibtex-field-braces-opt nil
1127 "Optimized value of `bibtex-field-braces-alist'.
1128 Created by `bibtex-field-re-init'.
1129 It is a an alist with elements (FIELD . REGEXP).")
1131 (defvar bibtex-field-strings-opt nil
1132 "Optimized value of `bibtex-field-strings-alist'.
1133 Created by `bibtex-field-re-init'.
1134 It is a an alist with elements (FIELD RULE1 RULE2 ...),
1135 where each RULE is (REGEXP . TO-STR).")
1137 (defvar bibtex-pop-previous-search-point nil
1138 "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
1140 (defvar bibtex-pop-next-search-point nil
1141 "Next point where `bibtex-pop-next' starts looking for a similar entry.")
1143 (defvar bibtex-field-kill-ring nil
1144 "Ring of least recently killed fields.
1145 At most `bibtex-field-kill-ring-max' items are kept here.")
1147 (defvar bibtex-field-kill-ring-yank-pointer nil
1148 "The tail of `bibtex-field-kill-ring' whose car is the last item yanked.")
1150 (defvar bibtex-entry-kill-ring nil
1151 "Ring of least recently killed entries.
1152 At most `bibtex-entry-kill-ring-max' items are kept here.")
1154 (defvar bibtex-entry-kill-ring-yank-pointer nil
1155 "The tail of `bibtex-entry-kill-ring' whose car is the last item yanked.")
1157 (defvar bibtex-last-kill-command nil
1158 "Type of the last kill command (either 'field or 'entry).")
1160 (defvar bibtex-strings
1161 (lazy-completion-table bibtex-strings
1162 (lambda ()
1163 (bibtex-parse-strings (bibtex-string-files-init))))
1164 "Completion table for BibTeX string keys.
1165 Initialized from `bibtex-predefined-strings' and `bibtex-string-files'.")
1166 (make-variable-buffer-local 'bibtex-strings)
1167 (put 'bibtex-strings 'risky-local-variable t)
1169 (defvar bibtex-reference-keys
1170 (lazy-completion-table bibtex-reference-keys
1171 (lambda () (bibtex-parse-keys nil t)))
1172 "Completion table for BibTeX reference keys.
1173 The CDRs of the elements are t for header keys and nil for crossref keys.")
1174 (make-variable-buffer-local 'bibtex-reference-keys)
1175 (put 'bibtex-reference-keys 'risky-local-variable t)
1177 (defvar bibtex-buffer-last-parsed-tick nil
1178 "Value of `buffer-modified-tick' last time buffer was parsed for keys.")
1180 (defvar bibtex-parse-idle-timer nil
1181 "Stores if timer is already installed.")
1183 (defvar bibtex-progress-lastperc nil
1184 "Last reported percentage for the progress message.")
1186 (defvar bibtex-progress-lastmes nil
1187 "Last reported progress message.")
1189 (defvar bibtex-progress-interval nil
1190 "Interval for progress messages.")
1192 (defvar bibtex-key-history nil
1193 "History list for reading keys.")
1195 (defvar bibtex-entry-type-history nil
1196 "History list for reading entry types.")
1198 (defvar bibtex-field-history nil
1199 "History list for reading field names.")
1201 (defvar bibtex-reformat-previous-options nil
1202 "Last reformat options given.")
1204 (defvar bibtex-reformat-previous-reference-keys nil
1205 "Last reformat reference keys option given.")
1207 (defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*"
1208 "Regexp matching the name of a BibTeX field.")
1210 (defconst bibtex-name-part
1211 (concat ",[ \t\n]*\\(" bibtex-field-name "\\)")
1212 "Regexp matching the name part of a BibTeX field.")
1214 (defconst bibtex-reference-key "[][[:alnum:].:;?!`'/*@+|()<>&_^$-]+"
1215 "Regexp matching the reference key part of a BibTeX entry.")
1217 (defconst bibtex-field-const "[][[:alnum:].:;?!`'/*@+=|<>&_^$-]+"
1218 "Regexp matching a BibTeX field constant.")
1220 (defvar bibtex-entry-type
1221 (concat "@[ \t]*\\(?:"
1222 (regexp-opt (mapcar 'car bibtex-entry-field-alist)) "\\)")
1223 "Regexp matching the type of a BibTeX entry.")
1225 (defvar bibtex-entry-head
1226 (concat "^[ \t]*\\("
1227 bibtex-entry-type
1228 "\\)[ \t]*[({][ \t\n]*\\("
1229 bibtex-reference-key
1230 "\\)")
1231 "Regexp matching the header line of a BibTeX entry (including key).")
1233 (defvar bibtex-entry-maybe-empty-head
1234 (concat bibtex-entry-head "?")
1235 "Regexp matching the header line of a BibTeX entry (possibly without key).")
1237 (defconst bibtex-any-entry-maybe-empty-head
1238 (concat "^[ \t]*\\(@[ \t]*" bibtex-field-name "\\)[ \t]*[({][ \t\n]*\\("
1239 bibtex-reference-key "\\)?")
1240 "Regexp matching the header line of any BibTeX entry (possibly without key).")
1242 (defvar bibtex-any-valid-entry-type
1243 (concat "^[ \t]*@[ \t]*\\(?:"
1244 (regexp-opt (append '("String" "Preamble")
1245 (mapcar 'car bibtex-entry-field-alist))) "\\)")
1246 "Regexp matching any valid BibTeX entry (including String and Preamble).")
1248 (defconst bibtex-type-in-head 1
1249 "Regexp subexpression number of the type part in `bibtex-entry-head'.")
1251 (defconst bibtex-key-in-head 2
1252 "Regexp subexpression number of the key part in `bibtex-entry-head'.")
1254 (defconst bibtex-string-type "^[ \t]*\\(@[ \t]*String\\)[ \t]*[({][ \t\n]*"
1255 "Regexp matching the name of a BibTeX String entry.")
1257 (defconst bibtex-string-maybe-empty-head
1258 (concat bibtex-string-type "\\(" bibtex-reference-key "\\)?")
1259 "Regexp matching the header line of a BibTeX String entry.")
1261 (defconst bibtex-preamble-prefix
1262 "[ \t]*\\(@[ \t]*Preamble\\)[ \t]*[({][ \t\n]*"
1263 "Regexp matching the prefix part of a BibTeX Preamble entry.")
1265 (defconst bibtex-font-lock-syntactic-keywords
1266 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start 0 1) "\\)"
1267 (substring bibtex-comment-start 1) "\\>")
1268 1 '(11))))
1270 (defvar bibtex-font-lock-keywords
1271 ;; entry type and reference key
1272 `((,bibtex-any-entry-maybe-empty-head
1273 (,bibtex-type-in-head font-lock-function-name-face)
1274 (,bibtex-key-in-head font-lock-constant-face nil t))
1275 ;; optional field names (treated as comments)
1276 (,(concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
1277 1 font-lock-comment-face)
1278 ;; field names
1279 (,(concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1280 1 font-lock-variable-name-face)
1281 ;; url
1282 (bibtex-font-lock-url) (bibtex-font-lock-crossref)
1283 ;; cite
1284 ,@(mapcar (lambda (matcher)
1285 `((lambda (bound) (bibtex-font-lock-cite ',matcher bound))))
1286 bibtex-cite-matcher-alist))
1287 "*Default expressions to highlight in BibTeX mode.")
1289 (defvar bibtex-font-lock-url-regexp
1290 ;; Assume that field names begin at the beginning of a line.
1291 (concat "^[ \t]*"
1292 (regexp-opt (delete-dups (mapcar 'caar bibtex-generate-url-list)) t)
1293 "[ \t]*=[ \t]*")
1294 "Regexp for `bibtex-font-lock-url' derived from `bibtex-generate-url-list'.")
1296 (defvar bibtex-string-empty-key nil
1297 "If non-nil, `bibtex-parse-string' accepts empty key.")
1299 (defvar bibtex-sort-entry-class-alist nil
1300 "Alist mapping entry types to their sorting index.
1301 Auto-generated from `bibtex-sort-entry-class'.
1302 Used when `bibtex-maintain-sorted-entries' is `entry-class'.")
1305 (defun bibtex-parse-association (parse-lhs parse-rhs)
1306 "Parse a string of the format <left-hand-side = right-hand-side>.
1307 The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
1308 substrings. These functions are expected to return nil if parsing is not
1309 successful. If the returned values of both functions are non-nil,
1310 return a cons pair of these values. Do not move point."
1311 (save-match-data
1312 (save-excursion
1313 (let ((left (funcall parse-lhs))
1314 right)
1315 (if (and left
1316 (looking-at "[ \t\n]*=[ \t\n]*")
1317 (goto-char (match-end 0))
1318 (setq right (funcall parse-rhs)))
1319 (cons left right))))))
1321 (defun bibtex-parse-field-name ()
1322 "Parse the name part of a BibTeX field.
1323 If the field name is found, return a triple consisting of the position of the
1324 very first character of the match, the actual starting position of the name
1325 part and end position of the match. Move point to end of field name.
1326 If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceding
1327 BibTeX field as necessary."
1328 (cond ((looking-at bibtex-name-part)
1329 (goto-char (match-end 0))
1330 (list (match-beginning 0) (match-beginning 1) (match-end 0)))
1331 ;; Maybe add a missing comma.
1332 ((and bibtex-autoadd-commas
1333 (looking-at (concat "[ \t\n]*\\(?:" bibtex-field-name
1334 "\\)[ \t\n]*=")))
1335 (skip-chars-backward " \t\n")
1336 ;; It can be confusing if non-editing commands try to
1337 ;; modify the buffer.
1338 (if buffer-read-only
1339 (error "Comma missing at buffer position %s" (point)))
1340 (insert ",")
1341 (forward-char -1)
1342 ;; Now try again.
1343 (bibtex-parse-field-name))))
1345 (defconst bibtex-braced-string-syntax-table
1346 (let ((st (make-syntax-table)))
1347 (modify-syntax-entry ?\{ "(}" st)
1348 (modify-syntax-entry ?\} "){" st)
1349 (modify-syntax-entry ?\[ "." st)
1350 (modify-syntax-entry ?\] "." st)
1351 (modify-syntax-entry ?\( "." st)
1352 (modify-syntax-entry ?\) "." st)
1353 (modify-syntax-entry ?\\ "." st)
1354 (modify-syntax-entry ?\" "." st)
1356 "Syntax-table to parse matched braces.")
1358 (defconst bibtex-quoted-string-syntax-table
1359 (let ((st (make-syntax-table)))
1360 (modify-syntax-entry ?\\ "\\" st)
1361 (modify-syntax-entry ?\" "\"" st)
1363 "Syntax-table to parse matched quotes.")
1365 (defun bibtex-parse-field-string ()
1366 "Parse a BibTeX field string enclosed by braces or quotes.
1367 If a syntactically correct string is found, a pair containing the start and
1368 end position of the field string is returned, nil otherwise.
1369 Do not move point."
1370 (let ((end-point
1371 (or (and (eq (following-char) ?\")
1372 (save-excursion
1373 (with-syntax-table bibtex-quoted-string-syntax-table
1374 (forward-sexp 1))
1375 (point)))
1376 (and (eq (following-char) ?\{)
1377 (save-excursion
1378 (with-syntax-table bibtex-braced-string-syntax-table
1379 (forward-sexp 1))
1380 (point))))))
1381 (if end-point
1382 (cons (point) end-point))))
1384 (defun bibtex-parse-field-text ()
1385 "Parse the text part of a BibTeX field.
1386 The text part is either a string, or an empty string, or a constant followed
1387 by one or more <# (string|constant)> pairs. If a syntactically correct text
1388 is found, a pair containing the start and end position of the text is
1389 returned, nil otherwise. Move point to end of field text."
1390 (let ((starting-point (point))
1391 end-point failure boundaries)
1392 (while (not (or end-point failure))
1393 (cond ((looking-at bibtex-field-const)
1394 (goto-char (match-end 0)))
1395 ((setq boundaries (bibtex-parse-field-string))
1396 (goto-char (cdr boundaries)))
1397 ((setq failure t)))
1398 (if (looking-at "[ \t\n]*#[ \t\n]*")
1399 (goto-char (match-end 0))
1400 (setq end-point (point))))
1401 (skip-chars-forward " \t\n")
1402 (if (and (not failure)
1403 end-point)
1404 (list starting-point end-point (point)))))
1406 (defun bibtex-parse-field ()
1407 "Parse the BibTeX field beginning at the position of point.
1408 If a syntactically correct field is found, return a cons pair containing
1409 the boundaries of the name and text parts of the field. Do not move point."
1410 (bibtex-parse-association 'bibtex-parse-field-name
1411 'bibtex-parse-field-text))
1413 (defsubst bibtex-start-of-field (bounds)
1414 (nth 0 (car bounds)))
1415 (defsubst bibtex-start-of-name-in-field (bounds)
1416 (nth 1 (car bounds)))
1417 (defsubst bibtex-end-of-name-in-field (bounds)
1418 (nth 2 (car bounds)))
1419 (defsubst bibtex-start-of-text-in-field (bounds)
1420 (nth 1 bounds))
1421 (defsubst bibtex-end-of-text-in-field (bounds)
1422 (nth 2 bounds))
1423 (defsubst bibtex-end-of-field (bounds)
1424 (nth 3 bounds))
1426 (defun bibtex-search-forward-field (name &optional bound)
1427 "Search forward to find a BibTeX field of name NAME.
1428 If a syntactically correct field is found, return a pair containing
1429 the boundaries of the name and text parts of the field. The search
1430 is limited by optional arg BOUND. If BOUND is t the search is limited
1431 by the end of the current entry. Do not move point."
1432 (save-match-data
1433 (save-excursion
1434 (if (eq bound t)
1435 (let ((regexp (concat bibtex-name-part "[ \t\n]*=\\|"
1436 bibtex-any-entry-maybe-empty-head))
1437 (case-fold-search t) bounds)
1438 (catch 'done
1439 (if (looking-at "[ \t]*@") (goto-char (match-end 0)))
1440 (while (and (not bounds)
1441 (re-search-forward regexp nil t))
1442 (if (match-beginning 2)
1443 ;; We found a new entry
1444 (throw 'done nil)
1445 ;; We found a field
1446 (goto-char (match-beginning 0))
1447 (setq bounds (bibtex-parse-field))))
1448 ;; Step through all fields so that we cannot overshoot.
1449 (while bounds
1450 (goto-char (bibtex-start-of-name-in-field bounds))
1451 (if (looking-at name) (throw 'done bounds))
1452 (goto-char (bibtex-end-of-field bounds))
1453 (setq bounds (bibtex-parse-field)))))
1454 ;; Bounded search or bound is nil (i.e. we cannot overshoot).
1455 ;; Indeed, the search is bounded when `bibtex-search-forward-field'
1456 ;; is called many times. So we optimize this part of this function.
1457 (let ((name-part (concat ",[ \t\n]*\\(" name "\\)[ \t\n]*=[ \t\n]*"))
1458 (case-fold-search t) left right)
1459 (while (and (not right)
1460 (re-search-forward name-part bound t))
1461 (setq left (list (match-beginning 0) (match-beginning 1)
1462 (match-end 1))
1463 ;; Don't worry that the field text could be past bound.
1464 right (bibtex-parse-field-text)))
1465 (if right (cons left right)))))))
1467 (defun bibtex-search-backward-field (name &optional bound)
1468 "Search backward to find a BibTeX field of name NAME.
1469 If a syntactically correct field is found, return a pair containing
1470 the boundaries of the name and text parts of the field. The search
1471 is limited by the optional arg BOUND. If BOUND is t the search is
1472 limited by the beginning of the current entry. Do not move point."
1473 (save-match-data
1474 (if (eq bound t)
1475 (setq bound (save-excursion (bibtex-beginning-of-entry))))
1476 (let ((name-part (concat ",[ \t\n]*\\(" name "\\)[ \t\n]*=[ \t\n]*"))
1477 (case-fold-search t) left right)
1478 (save-excursion
1479 ;; the parsing functions are not designed for parsing backwards :-(
1480 (when (search-backward "," bound t)
1481 (or (save-excursion
1482 (when (looking-at name-part)
1483 (setq left (list (match-beginning 0) (match-beginning 1)
1484 (match-end 1)))
1485 (goto-char (match-end 0))
1486 (setq right (bibtex-parse-field-text))))
1487 (while (and (not right)
1488 (re-search-backward name-part bound t))
1489 (setq left (list (match-beginning 0) (match-beginning 1)
1490 (match-end 1)))
1491 (save-excursion
1492 (goto-char (match-end 0))
1493 (setq right (bibtex-parse-field-text)))))
1494 (if right (cons left right)))))))
1496 (defun bibtex-name-in-field (bounds &optional remove-opt-alt)
1497 "Get content of name in BibTeX field defined via BOUNDS.
1498 If optional arg REMOVE-OPT-ALT is non-nil remove \"OPT\" and \"ALT\"."
1499 (let ((name (buffer-substring-no-properties
1500 (bibtex-start-of-name-in-field bounds)
1501 (bibtex-end-of-name-in-field bounds))))
1502 (if (and remove-opt-alt
1503 (string-match "\\`\\(OPT\\|ALT\\)" name))
1504 (substring name 3)
1505 name)))
1507 (defun bibtex-text-in-field-bounds (bounds &optional content)
1508 "Get text in BibTeX field defined via BOUNDS.
1509 If optional arg CONTENT is non-nil extract content of field
1510 by removing field delimiters and concatenating the resulting string.
1511 If `bibtex-expand-strings' is non-nil, also expand BibTeX strings."
1512 (if content
1513 (save-excursion
1514 (goto-char (bibtex-start-of-text-in-field bounds))
1515 (let ((epoint (bibtex-end-of-text-in-field bounds))
1516 content opoint)
1517 (while (< (setq opoint (point)) epoint)
1518 (if (looking-at bibtex-field-const)
1519 (let ((mtch (match-string-no-properties 0)))
1520 (push (or (if bibtex-expand-strings
1521 (cdr (assoc-string mtch (bibtex-strings) t)))
1522 mtch) content)
1523 (goto-char (match-end 0)))
1524 (let ((bounds (bibtex-parse-field-string)))
1525 (push (buffer-substring-no-properties
1526 (1+ (car bounds)) (1- (cdr bounds))) content)
1527 (goto-char (cdr bounds))))
1528 (re-search-forward "\\=[ \t\n]*#[ \t\n]*" nil t))
1529 (apply 'concat (nreverse content))))
1530 (buffer-substring-no-properties (bibtex-start-of-text-in-field bounds)
1531 (bibtex-end-of-text-in-field bounds))))
1533 (defun bibtex-text-in-field (field &optional follow-crossref)
1534 "Get content of field FIELD of current BibTeX entry.
1535 Return nil if not found.
1536 If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1537 (save-excursion
1538 (let* ((end (if follow-crossref (bibtex-end-of-entry) t))
1539 (beg (bibtex-beginning-of-entry)) ; move point
1540 (bounds (bibtex-search-forward-field field end)))
1541 (cond (bounds (bibtex-text-in-field-bounds bounds t))
1542 ((and follow-crossref
1543 (progn (goto-char beg)
1544 (setq bounds (bibtex-search-forward-field
1545 "\\(OPT\\)?crossref" end))))
1546 (let ((crossref-field (bibtex-text-in-field-bounds bounds t)))
1547 (if (bibtex-search-crossref crossref-field)
1548 ;; Do not pass FOLLOW-CROSSREF because we want
1549 ;; to follow crossrefs only one level of recursion.
1550 (bibtex-text-in-field field))))))))
1552 (defun bibtex-parse-string-prefix ()
1553 "Parse the prefix part of a BibTeX string entry, including reference key.
1554 If the string prefix is found, return a triple consisting of the position of
1555 the very first character of the match, the actual starting position of the
1556 reference key and the end position of the match.
1557 If `bibtex-string-empty-key' is non-nil accept empty string key."
1558 (let ((case-fold-search t))
1559 (if (looking-at bibtex-string-type)
1560 (let ((start (point)))
1561 (goto-char (match-end 0))
1562 (cond ((looking-at bibtex-reference-key)
1563 (goto-char (match-end 0))
1564 (list start
1565 (match-beginning 0)
1566 (match-end 0)))
1567 ((and bibtex-string-empty-key
1568 (looking-at "="))
1569 (skip-chars-backward " \t\n")
1570 (list start (point) (point))))))))
1572 (defun bibtex-parse-string-postfix ()
1573 "Parse the postfix part of a BibTeX string entry, including the text.
1574 If the string postfix is found, return a triple consisting of the position of
1575 the actual starting and ending position of the text and the very last
1576 character of the string entry. Move point past BibTeX string entry."
1577 (let* ((case-fold-search t)
1578 (bounds (bibtex-parse-field-text)))
1579 (when bounds
1580 (goto-char (nth 1 bounds))
1581 (when (looking-at "[ \t\n]*[})]")
1582 (goto-char (match-end 0))
1583 (list (car bounds)
1584 (nth 1 bounds)
1585 (match-end 0))))))
1587 (defun bibtex-parse-string (&optional empty-key)
1588 "Parse a BibTeX string entry beginning at the position of point.
1589 If a syntactically correct entry is found, return a cons pair containing
1590 the boundaries of the reference key and text parts of the entry.
1591 If EMPTY-KEY is non-nil, key may be empty. Do not move point."
1592 (let ((bibtex-string-empty-key empty-key))
1593 (bibtex-parse-association 'bibtex-parse-string-prefix
1594 'bibtex-parse-string-postfix)))
1596 (defun bibtex-search-forward-string (&optional empty-key)
1597 "Search forward to find a BibTeX string entry.
1598 If a syntactically correct entry is found, a pair containing the boundaries of
1599 the reference key and text parts of the string is returned.
1600 If EMPTY-KEY is non-nil, key may be empty. Do not move point."
1601 (save-excursion
1602 (save-match-data
1603 (let ((case-fold-search t) bounds)
1604 (while (and (not bounds)
1605 (search-forward-regexp bibtex-string-type nil t))
1606 (save-excursion (goto-char (match-beginning 0))
1607 (setq bounds (bibtex-parse-string empty-key))))
1608 bounds))))
1610 (defun bibtex-reference-key-in-string (bounds)
1611 "Return the key part of a BibTeX string defined via BOUNDS."
1612 (buffer-substring-no-properties (nth 1 (car bounds))
1613 (nth 2 (car bounds))))
1615 (defun bibtex-text-in-string (bounds &optional content)
1616 "Get text in BibTeX string field defined via BOUNDS.
1617 If optional arg CONTENT is non-nil extract content
1618 by removing field delimiters and concatenating the resulting string.
1619 If `bibtex-expand-strings' is non-nil, also expand BibTeX strings."
1620 (bibtex-text-in-field-bounds bounds content))
1622 (defsubst bibtex-start-of-text-in-string (bounds)
1623 (nth 0 (cdr bounds)))
1624 (defsubst bibtex-end-of-text-in-string (bounds)
1625 (nth 1 (cdr bounds)))
1626 (defsubst bibtex-end-of-string (bounds)
1627 (nth 2 (cdr bounds)))
1629 (defsubst bibtex-type-in-head ()
1630 "Extract BibTeX type in head."
1631 ;; ignore @
1632 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head))
1633 (match-end bibtex-type-in-head)))
1635 (defsubst bibtex-key-in-head (&optional empty)
1636 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
1637 (or (match-string-no-properties bibtex-key-in-head)
1638 empty))
1640 (defun bibtex-parse-preamble ()
1641 "Parse BibTeX preamble.
1642 Point must be at beginning of preamble. Do not move point."
1643 (let ((case-fold-search t))
1644 (when (looking-at bibtex-preamble-prefix)
1645 (let ((start (match-beginning 0)) (pref-start (match-beginning 1))
1646 (bounds (save-excursion (goto-char (match-end 0))
1647 (bibtex-parse-string-postfix))))
1648 (if bounds (cons (list start pref-start) bounds))))))
1650 ;; Helper Functions
1652 (defsubst bibtex-string= (str1 str2)
1653 "Return t if STR1 and STR2 are equal, ignoring case."
1654 (eq t (compare-strings str1 0 nil str2 0 nil t)))
1656 (defun bibtex-delete-whitespace ()
1657 "Delete all whitespace starting at point."
1658 (if (looking-at "[ \t\n]+")
1659 (delete-region (point) (match-end 0))))
1661 (defun bibtex-current-line ()
1662 "Compute line number of point regardless whether the buffer is narrowed."
1663 (+ (count-lines 1 (point))
1664 (if (bolp) 1 0)))
1666 (defun bibtex-valid-entry (&optional empty-key)
1667 "Parse a valid BibTeX entry (maybe without key if EMPTY-KEY is t).
1668 A valid entry is a syntactical correct one with type contained in
1669 `bibtex-entry-field-alist'. Ignore @String and @Preamble entries.
1670 Return a cons pair with buffer positions of beginning and end of entry
1671 if a valid entry is found, nil otherwise. Do not move point.
1672 After a call to this function `match-data' corresponds to the header
1673 of the entry, see regexp `bibtex-entry-head'."
1674 (let ((case-fold-search t) end)
1675 (if (looking-at (if empty-key bibtex-entry-maybe-empty-head
1676 bibtex-entry-head))
1677 (save-excursion
1678 (save-match-data
1679 (goto-char (match-end 0))
1680 (let ((entry-closer
1681 (if (save-excursion
1682 (goto-char (match-end bibtex-type-in-head))
1683 (looking-at "[ \t]*("))
1684 ",?[ \t\n]*)" ; entry opened with `('
1685 ",?[ \t\n]*}")) ; entry opened with `{'
1686 bounds)
1687 (skip-chars-forward " \t\n")
1688 ;; loop over all BibTeX fields
1689 (while (setq bounds (bibtex-parse-field))
1690 (goto-char (bibtex-end-of-field bounds)))
1691 ;; This matches the infix* part.
1692 (if (looking-at entry-closer) (setq end (match-end 0)))))
1693 (if end (cons (match-beginning 0) end))))))
1695 (defun bibtex-skip-to-valid-entry (&optional backward)
1696 "Move point to beginning of the next valid BibTeX entry.
1697 Do not move if we are already at beginning of a valid BibTeX entry.
1698 With optional argument BACKWARD non-nil, move backward to
1699 beginning of previous valid one. A valid entry is a syntactical correct one
1700 with type contained in `bibtex-entry-field-alist' or, if
1701 `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
1702 entry. Return buffer position of beginning and end of entry if a valid
1703 entry is found, nil otherwise."
1704 (interactive "P")
1705 (let ((case-fold-search t)
1706 found bounds)
1707 (beginning-of-line)
1708 ;; Loop till we look at a valid entry.
1709 (while (not (or found (if backward (bobp) (eobp))))
1710 (cond ((setq found (or (bibtex-valid-entry)
1711 (and (not bibtex-sort-ignore-string-entries)
1712 (setq bounds (bibtex-parse-string))
1713 (cons (bibtex-start-of-field bounds)
1714 (bibtex-end-of-string bounds))))))
1715 (backward (re-search-backward "^[ \t]*@" nil 'move))
1716 (t (if (re-search-forward "\n\\([ \t]*@\\)" nil 'move)
1717 (goto-char (match-beginning 1))))))
1718 found))
1720 (defun bibtex-map-entries (fun)
1721 "Call FUN for each BibTeX entry in buffer (possibly narrowed).
1722 FUN is called with three arguments, the key of the entry and the buffer
1723 positions of beginning and end of entry. Also, point is at beginning of
1724 entry and `match-data' corresponds to the header of the entry,
1725 see regexp `bibtex-entry-head'. If `bibtex-sort-ignore-string-entries'
1726 is non-nil, FUN is not called for @String entries."
1727 (let ((case-fold-search t)
1728 found)
1729 (save-excursion
1730 (goto-char (point-min))
1731 (while (setq found (bibtex-skip-to-valid-entry))
1732 (looking-at bibtex-any-entry-maybe-empty-head)
1733 (funcall fun (bibtex-key-in-head "") (car found) (cdr found))
1734 (goto-char (cdr found))))))
1736 (defun bibtex-progress-message (&optional flag interval)
1737 "Echo a message about progress of current buffer.
1738 If FLAG is a string, the message is initialized (in this case a
1739 value for INTERVAL may be given as well (if not this is set to 5)).
1740 If FLAG is `done', the message is deinitialized.
1741 If FLAG is nil, a message is echoed if point was incremented at least
1742 `bibtex-progress-interval' percent since last message was echoed."
1743 (cond ((stringp flag)
1744 (setq bibtex-progress-lastmes flag
1745 bibtex-progress-interval (or interval 5)
1746 bibtex-progress-lastperc 0))
1747 ((eq flag 'done)
1748 (message "%s (done)" bibtex-progress-lastmes)
1749 (setq bibtex-progress-lastmes nil))
1751 (let* ((size (- (point-max) (point-min)))
1752 (perc (if (= size 0)
1754 (/ (* 100 (- (point) (point-min))) size))))
1755 (when (>= perc (+ bibtex-progress-lastperc
1756 bibtex-progress-interval))
1757 (setq bibtex-progress-lastperc perc)
1758 (message "%s (%d%%)" bibtex-progress-lastmes perc))))))
1760 (defun bibtex-field-left-delimiter ()
1761 "Return a string dependent on `bibtex-field-delimiters'."
1762 (if (eq bibtex-field-delimiters 'braces)
1764 "\""))
1766 (defun bibtex-field-right-delimiter ()
1767 "Return a string dependent on `bibtex-field-delimiters'."
1768 (if (eq bibtex-field-delimiters 'braces)
1770 "\""))
1772 (defun bibtex-entry-left-delimiter ()
1773 "Return a string dependent on `bibtex-entry-delimiters'."
1774 (if (eq bibtex-entry-delimiters 'braces)
1776 "("))
1778 (defun bibtex-entry-right-delimiter ()
1779 "Return a string dependent on `bibtex-entry-delimiters'."
1780 (if (eq bibtex-entry-delimiters 'braces)
1782 ")"))
1784 (defun bibtex-flash-head (prompt)
1785 "Flash at BibTeX entry head before point, if it exists."
1786 (let ((case-fold-search t)
1787 (pnt (point)))
1788 (save-excursion
1789 (bibtex-beginning-of-entry)
1790 (when (and (looking-at bibtex-any-entry-maybe-empty-head)
1791 (< (point) pnt))
1792 (goto-char (match-beginning bibtex-type-in-head))
1793 (if (and (< 0 blink-matching-delay)
1794 (pos-visible-in-window-p (point)))
1795 (sit-for blink-matching-delay)
1796 (message "%s%s" prompt (buffer-substring-no-properties
1797 (point) (match-end bibtex-key-in-head))))))))
1799 (defun bibtex-make-optional-field (field)
1800 "Make an optional field named FIELD in current BibTeX entry."
1801 (if (consp field)
1802 (bibtex-make-field (cons (concat "OPT" (car field)) (cdr field)))
1803 (bibtex-make-field (concat "OPT" field))))
1805 (defun bibtex-move-outside-of-entry ()
1806 "Make sure point is outside of a BibTeX entry."
1807 (let ((orig-point (point)))
1808 (bibtex-end-of-entry)
1809 (when (< (point) orig-point)
1810 ;; We moved backward, so we weren't inside an entry to begin with.
1811 ;; Leave point at the beginning of a line, and preferably
1812 ;; at the beginning of a paragraph.
1813 (goto-char orig-point)
1814 (beginning-of-line 1)
1815 (unless (= ?\n (char-before (1- (point))))
1816 (re-search-forward "^[ \t]*[@\n]" nil 'move)
1817 (backward-char 1)))
1818 (skip-chars-forward " \t\n")))
1820 (defun bibtex-beginning-of-first-entry ()
1821 "Go to beginning of line of first BibTeX entry in buffer.
1822 If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
1823 are ignored. Return point"
1824 (goto-char (point-min))
1825 (bibtex-skip-to-valid-entry)
1826 (point))
1828 (defun bibtex-enclosing-field (&optional comma noerr)
1829 "Search for BibTeX field enclosing point.
1830 For `bibtex-mode''s internal algorithms, a field begins at the comma
1831 following the preceding field. Usually, this is not what the user expects.
1832 Thus if COMMA is non-nil, the \"current field\" includes the terminating comma.
1833 Unless NOERR is non-nil, signal an error if no enclosing field is found.
1834 On success return bounds, nil otherwise. Do not move point."
1835 (save-excursion
1836 (when comma
1837 (end-of-line)
1838 (skip-chars-backward " \t")
1839 (if (= (preceding-char) ?,) (forward-char -1)))
1841 (let ((bounds (bibtex-search-backward-field bibtex-field-name t)))
1842 (cond ((and bounds
1843 (<= (bibtex-start-of-field bounds) (point))
1844 (>= (bibtex-end-of-field bounds) (point)))
1845 bounds)
1846 ((not noerr)
1847 (error "Can't find enclosing BibTeX field"))))))
1849 (defun bibtex-beginning-first-field (&optional beg)
1850 "Move point to beginning of first field.
1851 Optional arg BEG is beginning of entry."
1852 (if beg (goto-char beg) (bibtex-beginning-of-entry))
1853 (looking-at bibtex-any-entry-maybe-empty-head)
1854 (goto-char (match-end 0)))
1856 (defun bibtex-insert-kill (n &optional comma)
1857 "Reinsert the Nth stretch of killed BibTeX text (field or entry).
1858 Optional arg COMMA is as in `bibtex-enclosing-field'."
1859 (unless bibtex-last-kill-command (error "BibTeX kill ring is empty"))
1860 (let ((fun (lambda (kryp kr) ; adapted from `current-kill'
1861 (car (set kryp (nthcdr (mod (- n (length (eval kryp)))
1862 (length kr)) kr))))))
1863 (if (eq bibtex-last-kill-command 'field)
1864 (progn
1865 ;; insert past the current field
1866 (goto-char (bibtex-end-of-field (bibtex-enclosing-field comma)))
1867 (push-mark)
1868 (bibtex-make-field (funcall fun 'bibtex-field-kill-ring-yank-pointer
1869 bibtex-field-kill-ring) t nil t))
1870 ;; insert past the current entry
1871 (bibtex-skip-to-valid-entry)
1872 (push-mark)
1873 (insert (funcall fun 'bibtex-entry-kill-ring-yank-pointer
1874 bibtex-entry-kill-ring)))))
1876 (defun bibtex-format-entry ()
1877 "Helper function for `bibtex-clean-entry'.
1878 Formats current entry according to variable `bibtex-entry-format'."
1879 ;; initialize `bibtex-field-braces-opt' if necessary
1880 (if (and bibtex-field-braces-alist (not bibtex-field-braces-opt))
1881 (setq bibtex-field-braces-opt
1882 (bibtex-field-re-init bibtex-field-braces-alist 'braces)))
1883 ;; initialize `bibtex-field-strings-opt' if necessary
1884 (if (and bibtex-field-strings-alist (not bibtex-field-strings-opt))
1885 (setq bibtex-field-strings-opt
1886 (bibtex-field-re-init bibtex-field-strings-alist 'strings)))
1888 (let ((case-fold-search t)
1889 (format (if (eq bibtex-entry-format t)
1890 '(realign opts-or-alts required-fields numerical-fields
1891 page-dashes whitespace inherit-booktitle
1892 last-comma delimiters unify-case braces
1893 strings)
1894 bibtex-entry-format))
1895 (left-delim-re (regexp-quote (bibtex-field-left-delimiter)))
1896 bounds crossref-key req-field-list default-field-list field-list
1897 alt-fields error-field-name)
1898 (unwind-protect
1899 ;; formatting (undone if error occurs)
1900 (atomic-change-group
1901 (save-excursion
1902 (save-restriction
1903 (bibtex-narrow-to-entry)
1905 ;; There are more elegant high-level functions for several tasks
1906 ;; done by `bibtex-format-entry'. However, they contain some
1907 ;; redundancy compared with what we need to do anyway.
1908 ;; So for speed-up we avoid using them.
1909 ;; (`bibtex-format-entry' is called often by `bibtex-reformat'.)
1911 ;; identify entry type
1912 (goto-char (point-min))
1913 (or (re-search-forward bibtex-entry-type nil t)
1914 (error "Not inside a BibTeX entry"))
1915 (let* ((beg-type (1+ (match-beginning 0)))
1916 (end-type (match-end 0))
1917 (entry-list (assoc-string (buffer-substring-no-properties
1918 beg-type end-type)
1919 bibtex-entry-field-alist t)))
1921 ;; unify case of entry type
1922 (when (memq 'unify-case format)
1923 (delete-region beg-type end-type)
1924 (insert (car entry-list)))
1926 ;; update left entry delimiter
1927 (when (memq 'delimiters format)
1928 (goto-char end-type)
1929 (skip-chars-forward " \t\n")
1930 (delete-char 1)
1931 (insert (bibtex-entry-left-delimiter)))
1933 ;; Do we have a crossref key?
1934 (goto-char (point-min))
1935 (if (setq bounds (bibtex-search-forward-field "crossref"))
1936 (let ((text (bibtex-text-in-field-bounds bounds t)))
1937 (unless (equal "" text)
1938 (setq crossref-key text))))
1940 ;; list of required fields appropriate for an entry with
1941 ;; or without crossref key.
1942 (setq req-field-list (if (and crossref-key (nth 2 entry-list))
1943 (car (nth 2 entry-list))
1944 (car (nth 1 entry-list)))
1945 ;; default list of fields that may appear in this entry
1946 default-field-list (append (nth 0 (nth 1 entry-list))
1947 (nth 1 (nth 1 entry-list))
1948 bibtex-user-optional-fields)))
1950 ;; process all fields
1951 (bibtex-beginning-first-field (point-min))
1952 (while (setq bounds (bibtex-parse-field))
1953 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))
1954 (end-field (copy-marker (bibtex-end-of-field bounds) t))
1955 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds)))
1956 (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))
1957 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds)))
1958 (end-text (copy-marker (bibtex-end-of-text-in-field bounds) t))
1959 (opt-alt (string-match "OPT\\|ALT"
1960 (buffer-substring-no-properties
1961 beg-name (+ beg-name 3))))
1962 (field-name (buffer-substring-no-properties
1963 (if opt-alt (+ beg-name 3) beg-name) end-name))
1964 (empty-field (equal "" (bibtex-text-in-field-bounds bounds t)))
1965 deleted)
1967 ;; keep track of alternatives
1968 (if (nth 3 (assoc-string field-name req-field-list t))
1969 (push field-name alt-fields))
1971 (if (memq 'opts-or-alts format)
1972 ;; delete empty optional and alternative fields
1973 ;; (but keep empty required fields)
1974 (cond ((and empty-field
1975 (or opt-alt
1976 (let ((field (assoc-string
1977 field-name req-field-list t)))
1978 (or (not field) ; OPT field
1979 (nth 3 field))))) ; ALT field
1980 (delete-region beg-field end-field)
1981 (setq deleted t))
1982 ;; otherwise nonempty field: delete "OPT" or "ALT"
1983 (opt-alt
1984 (goto-char beg-name)
1985 (delete-char 3))))
1987 (unless deleted
1988 (push field-name field-list)
1990 ;; remove delimiters from purely numerical fields
1991 (when (and (memq 'numerical-fields format)
1992 (progn (goto-char beg-text)
1993 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1994 (goto-char end-text)
1995 (delete-char -1)
1996 (goto-char beg-text)
1997 (delete-char 1))
1999 ;; update delimiters
2000 (when (memq 'delimiters format)
2001 (goto-char beg-text)
2002 ;; simplified from `bibtex-parse-field-text', as we
2003 ;; already checked that the field format is correct
2004 (while (< (point) end-text)
2005 (if (looking-at bibtex-field-const)
2006 (goto-char (match-end 0))
2007 (let ((boundaries (bibtex-parse-field-string)))
2008 (if (looking-at left-delim-re)
2009 (goto-char (cdr boundaries))
2010 (delete-char 1)
2011 (insert (bibtex-field-left-delimiter))
2012 (goto-char (1- (cdr boundaries)))
2013 (delete-char 1)
2014 (insert (bibtex-field-right-delimiter)))))
2015 (if (looking-at "[ \t\n]*#[ \t\n]*")
2016 (goto-char (match-end 0)))))
2018 ;; update page dashes
2019 (if (and (memq 'page-dashes format)
2020 (bibtex-string= field-name "pages")
2021 (progn (goto-char beg-text)
2022 (looking-at
2023 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
2024 (replace-match "\\1-\\2"))
2026 ;; Remove whitespace at beginning and end of field.
2027 ;; We do not look at individual parts of the field
2028 ;; as {foo } # bar # { baz} is a fine field.
2029 (when (memq 'whitespace format)
2030 (goto-char beg-text)
2031 (if (looking-at "\\([{\"]\\)[ \t\n]+")
2032 (replace-match "\\1"))
2033 (goto-char end-text)
2034 (if (looking-back "[ \t\n]+\\([}\"]\\)" beg-text t)
2035 (replace-match "\\1")))
2037 ;; enclose field text by braces according to
2038 ;; `bibtex-field-braces-alist'.
2039 (let (case-fold-search temp) ; Case-sensitive search
2040 (when (and (memq 'braces format)
2041 (setq temp (cdr (assoc-string field-name
2042 bibtex-field-braces-opt t))))
2043 (goto-char beg-text)
2044 (while (re-search-forward temp end-text t)
2045 (let ((beg (match-beginning 0))
2046 (bounds (bibtex-find-text-internal nil t)))
2047 (unless (or (nth 4 bounds) ; string constant
2048 ;; match already surrounded by braces
2049 ;; (braces are inside field delimiters)
2050 (and (< (point) (1- (nth 2 bounds)))
2051 (< (1+ (nth 1 bounds)) beg)
2052 (looking-at "}")
2053 (save-excursion (goto-char (1- beg))
2054 (looking-at "{"))))
2055 (insert "}")
2056 (goto-char beg)
2057 (insert "{")))))
2059 ;; replace field text by BibTeX string constants
2060 ;; according to `bibtex-field-strings-alist'.
2061 (when (and (memq 'strings format)
2062 (setq temp (cdr (assoc-string field-name
2063 bibtex-field-strings-opt t))))
2064 (goto-char beg-text)
2065 (dolist (re temp)
2066 (while (re-search-forward (car re) end-text t)
2067 (let ((bounds (save-match-data
2068 (bibtex-find-text-internal nil t))))
2069 (unless (nth 4 bounds)
2070 ;; if match not at right subfield boundary...
2071 (if (< (match-end 0) (1- (nth 2 bounds)))
2072 (insert " # " (bibtex-field-left-delimiter))
2073 (delete-char 1))
2074 (replace-match (cdr re))
2075 (goto-char (match-beginning 0))
2076 ;; if match not at left subfield boundary...
2077 (if (< (1+ (nth 1 bounds)) (match-beginning 0))
2078 (insert (bibtex-field-right-delimiter) " # ")
2079 (delete-backward-char 1))))))))
2081 ;; use book title of crossref'd entry
2082 (if (and (memq 'inherit-booktitle format)
2083 empty-field
2084 (bibtex-string= field-name "booktitle")
2085 crossref-key)
2086 (let ((title (save-excursion
2087 (save-restriction
2088 (widen)
2089 (if (bibtex-search-entry crossref-key t)
2090 (bibtex-text-in-field "title"))))))
2091 (when title
2092 (setq empty-field nil)
2093 (goto-char (1+ beg-text))
2094 (insert title))))
2096 ;; if empty field is a required field, complain
2097 (when (and empty-field
2098 (memq 'required-fields format)
2099 (assoc-string field-name req-field-list t))
2100 (setq error-field-name field-name)
2101 (error "Mandatory field `%s' is empty" field-name))
2103 ;; unify case of field name
2104 (if (memq 'unify-case format)
2105 (let ((fname (car (assoc-string field-name
2106 default-field-list t))))
2107 (if fname
2108 (progn
2109 (delete-region beg-name end-name)
2110 (goto-char beg-name)
2111 (insert fname))
2112 ;; there are no rules we could follow
2113 (downcase-region beg-name end-name))))
2115 ;; update point
2116 (goto-char end-field))))
2118 ;; check whether all required fields are present
2119 (if (memq 'required-fields format)
2120 (let ((found 0) alt-list)
2121 (dolist (fname req-field-list)
2122 (cond ((nth 3 fname) ; t if field has alternative flag
2123 (push (car fname) alt-list)
2124 (if (member-ignore-case (car fname) field-list)
2125 (setq found (1+ found))))
2126 ((not (member-ignore-case (car fname) field-list))
2127 ;; If we use the crossref field, a required field
2128 ;; can have the OPT prefix. So if it was empty,
2129 ;; we have deleted by now. Nonetheless we can
2130 ;; move point on this empty field.
2131 (setq error-field-name (car fname))
2132 (error "Mandatory field `%s' is missing" (car fname)))))
2133 (if alt-list
2134 (cond ((= found 0)
2135 (if alt-fields
2136 (setq error-field-name (car (last alt-fields))))
2137 (error "Alternative mandatory field `%s' is missing"
2138 alt-list))
2139 ((> found 1)
2140 (if alt-fields
2141 (setq error-field-name (car (last alt-fields))))
2142 (error "Alternative fields `%s' are defined %s times"
2143 alt-list found))))))
2145 ;; update comma after last field
2146 (if (memq 'last-comma format)
2147 (cond ((and bibtex-comma-after-last-field
2148 (not (looking-at ",")))
2149 (insert ","))
2150 ((and (not bibtex-comma-after-last-field)
2151 (looking-at ","))
2152 (delete-char 1))))
2154 ;; update right entry delimiter
2155 (if (looking-at ",")
2156 (forward-char))
2157 (when (memq 'delimiters format)
2158 (skip-chars-forward " \t\n")
2159 (delete-char 1)
2160 (insert (bibtex-entry-right-delimiter)))
2162 ;; realign and fill entry
2163 (if (memq 'realign format)
2164 (bibtex-fill-entry)))))
2166 ;; Unwindform: move point to location where error occured if possible
2167 (if error-field-name
2168 (let (bounds)
2169 (when (save-excursion
2170 (bibtex-beginning-of-entry)
2171 (setq bounds
2172 (bibtex-search-forward-field
2173 ;; If we use the crossref field, a required field
2174 ;; can have the OPT prefix
2175 (concat "\\(OPT\\|ALT\\)?" error-field-name) t)))
2176 (goto-char (bibtex-start-of-text-in-field bounds))
2177 (bibtex-find-text)))))))
2179 (defun bibtex-field-re-init (regexp-alist type)
2180 "Calculate optimized value for bibtex-regexp-TYPE-opt.
2181 This value is based on bibtex-regexp-TYPE-alist. TYPE is 'braces or 'strings.
2182 Return optimized value to be used by `bibtex-format-entry'."
2183 (setq regexp-alist
2184 (mapcar (lambda (e)
2185 (list (car e)
2186 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" (nth 1 e))
2187 (nth 2 e))) ; nil for 'braces'.
2188 regexp-alist))
2189 (let (opt-list)
2190 ;; Loop over field names
2191 (dolist (field (delete-dups (apply 'append (mapcar 'car regexp-alist))))
2192 (let (rules)
2193 ;; Collect all matches we have for this field name
2194 (dolist (e regexp-alist)
2195 (if (assoc-string field (car e) t)
2196 (push (cons (nth 1 e) (nth 2 e)) rules)))
2197 (if (eq type 'braces)
2198 ;; concatenate all regexps to a single regexp
2199 (setq rules (concat "\\(?:" (mapconcat 'car rules "\\|") "\\)")))
2200 ;; create list of replacement rules.
2201 (push (cons field rules) opt-list)))
2202 opt-list))
2205 (defun bibtex-autokey-abbrev (string len)
2206 "Return an abbreviation of STRING with at least LEN characters.
2207 If LEN is positive the abbreviation is terminated only after a consonant
2208 or at the word end. If LEN is negative the abbreviation is strictly
2209 enforced using abs (LEN) characters. If LEN is not a number, STRING
2210 is returned unchanged."
2211 (cond ((or (not (numberp len))
2212 (<= (length string) (abs len)))
2213 string)
2214 ((equal len 0)
2216 ((< len 0)
2217 (substring string 0 (abs len)))
2218 (t (let* ((case-fold-search t)
2219 (abort-char (string-match "[^aeiou]" string (1- len))))
2220 (if abort-char
2221 (substring string 0 (1+ abort-char))
2222 string)))))
2224 (defun bibtex-autokey-get-field (field &optional change-list)
2225 "Get content of BibTeX field FIELD. Return empty string if not found.
2226 Optional arg CHANGE-LIST is a list of substitution patterns that is
2227 applied to the content of FIELD. It is an alist with pairs
2228 \(OLD-REGEXP . NEW-STRING\)."
2229 (let* ((bibtex-expand-strings bibtex-autokey-expand-strings)
2230 (content (bibtex-text-in-field field bibtex-autokey-use-crossref))
2231 case-fold-search)
2232 (unless content (setq content ""))
2233 (dolist (pattern change-list content)
2234 (setq content (replace-regexp-in-string (car pattern)
2235 (cdr pattern)
2236 content t)))))
2238 (defun bibtex-autokey-get-names ()
2239 "Get contents of the name field of the current entry.
2240 Do some modifications based on `bibtex-autokey-name-change-strings'.
2241 Return the names as a concatenated string obeying `bibtex-autokey-names'
2242 and `bibtex-autokey-names-stretch'."
2243 (let ((names (bibtex-autokey-get-field "author\\|editor"
2244 bibtex-autokey-name-change-strings)))
2245 ;; Some entries do not have a name field.
2246 (if (string= "" names)
2247 names
2248 (let* ((case-fold-search t)
2249 (name-list (mapcar 'bibtex-autokey-demangle-name
2250 (split-string names "[ \t\n]+and[ \t\n]+")))
2251 additional-names)
2252 (unless (or (not (numberp bibtex-autokey-names))
2253 (<= (length name-list)
2254 (+ bibtex-autokey-names
2255 bibtex-autokey-names-stretch)))
2256 ;; Take `bibtex-autokey-names' elements from beginning of name-list
2257 (setq name-list (nreverse (nthcdr (- (length name-list)
2258 bibtex-autokey-names)
2259 (nreverse name-list)))
2260 additional-names bibtex-autokey-additional-names))
2261 (concat (mapconcat 'identity name-list
2262 bibtex-autokey-name-separator)
2263 additional-names)))))
2265 (defun bibtex-autokey-demangle-name (fullname)
2266 "Get the last part from a well-formed FULLNAME and perform abbreviations."
2267 (let* (case-fold-search
2268 (name (cond ((string-match "\\([[:upper:]][^, ]*\\)[^,]*," fullname)
2269 ;; Name is of the form "von Last, First" or
2270 ;; "von Last, Jr, First"
2271 ;; --> Take the first capital part before the comma
2272 (match-string 1 fullname))
2273 ((string-match "\\([^, ]*\\)," fullname)
2274 ;; Strange name: we have a comma, but nothing capital
2275 ;; So we accept even lowercase names
2276 (match-string 1 fullname))
2277 ((string-match "\\(\\<[[:lower:]][^ ]* +\\)+\\([[:upper:]][^ ]*\\)"
2278 fullname)
2279 ;; name is of the form "First von Last", "von Last",
2280 ;; "First von von Last", or "d'Last"
2281 ;; --> take the first capital part after the "von" parts
2282 (match-string 2 fullname))
2283 ((string-match "\\([^ ]+\\) *\\'" fullname)
2284 ;; name is of the form "First Middle Last" or "Last"
2285 ;; --> take the last token
2286 (match-string 1 fullname))
2287 (t (error "Name `%s' is incorrectly formed" fullname)))))
2288 (funcall bibtex-autokey-name-case-convert-function
2289 (bibtex-autokey-abbrev name bibtex-autokey-name-length))))
2291 (defun bibtex-autokey-get-year ()
2292 "Return year field contents as a string obeying `bibtex-autokey-year-length'."
2293 (let ((yearfield (bibtex-autokey-get-field "year")))
2294 (substring yearfield (max 0 (- (length yearfield)
2295 bibtex-autokey-year-length)))))
2297 (defun bibtex-autokey-get-title ()
2298 "Get title field contents up to a terminator.
2299 Return the result as a string"
2300 (let ((case-fold-search t)
2301 (titlestring
2302 (bibtex-autokey-get-field "title"
2303 bibtex-autokey-titleword-change-strings)))
2304 ;; ignore everything past a terminator
2305 (if (string-match bibtex-autokey-title-terminators titlestring)
2306 (setq titlestring (substring titlestring 0 (match-beginning 0))))
2307 ;; gather words from titlestring into a list. Ignore
2308 ;; specific words and use only a specific amount of words.
2309 (let ((counter 0)
2310 titlewords titlewords-extra word)
2311 (while (and (or (not (numberp bibtex-autokey-titlewords))
2312 (< counter (+ bibtex-autokey-titlewords
2313 bibtex-autokey-titlewords-stretch)))
2314 (string-match "\\b\\w+" titlestring))
2315 (setq word (match-string 0 titlestring)
2316 titlestring (substring titlestring (match-end 0)))
2317 ;; Ignore words matched by one of the elements of
2318 ;; `bibtex-autokey-titleword-ignore'
2319 (unless (let ((lst bibtex-autokey-titleword-ignore))
2320 (while (and lst
2321 (not (string-match (concat "\\`\\(?:" (car lst)
2322 "\\)\\'") word)))
2323 (setq lst (cdr lst)))
2324 lst)
2325 (setq counter (1+ counter))
2326 (if (or (not (numberp bibtex-autokey-titlewords))
2327 (<= counter bibtex-autokey-titlewords))
2328 (push word titlewords)
2329 (push word titlewords-extra))))
2330 ;; Obey `bibtex-autokey-titlewords-stretch':
2331 ;; If by now we have processed all words in titlestring, we include
2332 ;; titlewords-extra in titlewords. Otherwise, we ignore titlewords-extra.
2333 (unless (string-match "\\b\\w+" titlestring)
2334 (setq titlewords (append titlewords-extra titlewords)))
2335 (mapconcat 'bibtex-autokey-demangle-title (nreverse titlewords)
2336 bibtex-autokey-titleword-separator))))
2338 (defun bibtex-autokey-demangle-title (titleword)
2339 "Do some abbreviations on TITLEWORD.
2340 The rules are defined in `bibtex-autokey-titleword-abbrevs'
2341 and `bibtex-autokey-titleword-length'."
2342 (let ((case-fold-search t)
2343 (alist bibtex-autokey-titleword-abbrevs))
2344 (while (and alist
2345 (not (string-match (concat "\\`\\(?:" (caar alist) "\\)\\'")
2346 titleword)))
2347 (setq alist (cdr alist)))
2348 (if alist
2349 (cdar alist)
2350 (funcall bibtex-autokey-titleword-case-convert-function
2351 (bibtex-autokey-abbrev titleword bibtex-autokey-titleword-length)))))
2353 (defun bibtex-generate-autokey ()
2354 "Generate automatically a key for a BibTeX entry.
2355 Use the author/editor, the year and the title field.
2356 The algorithm works as follows.
2358 The name part:
2359 1. Use the author or editor field to generate the name part of the key.
2360 Expand BibTeX strings if `bibtex-autokey-expand-strings' is non-nil.
2361 2. Change the content of the name field according to
2362 `bibtex-autokey-name-change-strings' (see there for further detail).
2363 3. Use the first `bibtex-autokey-names' names in the name field. If there
2364 are up to `bibtex-autokey-names' + `bibtex-autokey-names-stretch' names,
2365 use all names.
2366 4. Use only the last names to form the name part. From these last names,
2367 take at least `bibtex-autokey-name-length' characters (truncate only
2368 after a consonant or at a word end).
2369 5. Convert all last names using the function
2370 `bibtex-autokey-name-case-convert-function'.
2371 6. Build the name part of the key by concatenating all abbreviated last
2372 names with the string `bibtex-autokey-name-separator' between any two.
2373 If there are more names in the name field than names used in the name
2374 part, append the string `bibtex-autokey-additional-names'.
2376 The year part:
2377 1. Build the year part of the key by truncating the content of the year
2378 field to the rightmost `bibtex-autokey-year-length' digits (useful
2379 values are 2 and 4).
2380 2. If the year field (or any other field required to generate the key)
2381 is absent, but the entry has a valid crossref field and
2382 `bibtex-autokey-use-crossref' is non-nil, use the field of the
2383 crossreferenced entry instead.
2385 The title part
2386 1. Change the content of the title field according to
2387 `bibtex-autokey-titleword-change-strings' (see there for further detail).
2388 2. Truncate the title before the first match of
2389 `bibtex-autokey-title-terminators' and delete those words which appear
2390 in `bibtex-autokey-titleword-ignore'. Build the title part using the
2391 first `bibtex-autokey-titlewords' words from this truncated title.
2392 If the truncated title ends after up to `bibtex-autokey-titlewords' +
2393 `bibtex-autokey-titlewords-stretch' words, use all words from the
2394 truncated title.
2395 3. For every title word that appears in `bibtex-autokey-titleword-abbrevs'
2396 use the corresponding abbreviation (see documentation of this variable
2397 for further detail).
2398 4. From every title word not generated by an abbreviation, take at least
2399 `bibtex-autokey-titleword-length' characters (truncate only after
2400 a consonant or at a word end).
2401 5. Convert all title words using the function
2402 `bibtex-autokey-titleword-case-convert-function'.
2403 6. Build the title part by concatenating all abbreviated title words with
2404 the string `bibtex-autokey-titleword-separator' between any two.
2406 Concatenate the key:
2407 1. Concatenate `bibtex-autokey-prefix-string', the name part, the year
2408 part and the title part. If the name part and the year part are both
2409 non-empty insert `bibtex-autokey-name-year-separator' between the two.
2410 If the title part and the year (or name) part are non-empty, insert
2411 `bibtex-autokey-year-title-separator' between the two.
2412 2. If `bibtex-autokey-before-presentation-function' is non-nil, it must be
2413 a function taking one argument. Call this function with the generated
2414 key as the argument. Use the return value of this function (a string)
2415 as the key.
2416 3. If `bibtex-autokey-edit-before-use' is non-nil, present the key in the
2417 minibuffer to the user for editing. Insert the key given by the user."
2418 (let* ((names (bibtex-autokey-get-names))
2419 (year (bibtex-autokey-get-year))
2420 (title (bibtex-autokey-get-title))
2421 (autokey (concat bibtex-autokey-prefix-string
2422 names
2423 (unless (or (equal names "")
2424 (equal year ""))
2425 bibtex-autokey-name-year-separator)
2426 year
2427 (unless (or (and (equal names "")
2428 (equal year ""))
2429 (equal title ""))
2430 bibtex-autokey-year-title-separator)
2431 title)))
2432 (if bibtex-autokey-before-presentation-function
2433 (funcall bibtex-autokey-before-presentation-function autokey)
2434 autokey)))
2437 (defun bibtex-global-key-alist ()
2438 "Return global key alist based on `bibtex-files'."
2439 (if bibtex-files
2440 (apply 'append
2441 (mapcar (lambda (buf)
2442 (with-current-buffer buf bibtex-reference-keys))
2443 ;; include current buffer only if it uses `bibtex-mode'
2444 (bibtex-initialize (eq major-mode 'bibtex-mode))))
2445 (if (eq major-mode 'bibtex-mode)
2446 bibtex-reference-keys)))
2448 (defun bibtex-read-key (prompt &optional key global)
2449 "Read BibTeX key from minibuffer using PROMPT and default KEY.
2450 If optional arg GLOBAL is non-nil, completion is based on the keys in
2451 `bibtex-reference-keys' of `bibtex-files',"
2452 (let (completion-ignore-case)
2453 (completing-read prompt (if global (bibtex-global-key-alist)
2454 bibtex-reference-keys)
2455 nil nil key 'bibtex-key-history)))
2457 (defun bibtex-read-string-key (&optional key)
2458 "Read BibTeX string key from minibuffer using default KEY."
2459 (let ((completion-ignore-case t))
2460 (completing-read "String key: " bibtex-strings
2461 nil nil key 'bibtex-key-history)))
2463 (defun bibtex-parse-keys (&optional abortable verbose)
2464 "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2465 Find both entry keys and crossref entries. If ABORTABLE is non-nil abort
2466 on user input. If VERBOSE is non-nil give messages about progress.
2467 Return alist of keys if parsing was completed, `aborted' otherwise.
2468 If `bibtex-parse-keys-fast' is non-nil, use fast but simplified algorithm
2469 for parsing BibTeX keys. If parsing fails, try to set this variable to nil."
2470 (if (eq major-mode 'bibtex-mode)
2471 (let (ref-keys crossref-keys)
2472 (save-excursion
2473 (save-match-data
2474 (if verbose
2475 (bibtex-progress-message
2476 (concat (buffer-name) ": parsing reference keys")))
2477 (catch 'userkey
2478 (goto-char (point-min))
2479 (if bibtex-parse-keys-fast
2480 (let ((case-fold-search t)
2481 (re (concat bibtex-entry-head "\\|"
2482 ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*"
2483 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]")))
2484 (while (re-search-forward re nil t)
2485 (if (and abortable (input-pending-p))
2486 ;; user has aborted by typing a key: return `aborted'
2487 (throw 'userkey 'aborted))
2488 (cond ((match-end 3)
2489 ;; This is a crossref.
2490 (let ((key (buffer-substring-no-properties
2491 (1+ (match-beginning 3)) (1- (match-end 3)))))
2492 (unless (assoc key crossref-keys)
2493 (push (list key) crossref-keys))))
2494 ;; only keys of known entries
2495 ((assoc-string (bibtex-type-in-head)
2496 bibtex-entry-field-alist t)
2497 ;; This is an entry.
2498 (let ((key (bibtex-key-in-head)))
2499 (unless (assoc key ref-keys)
2500 (push (cons key t) ref-keys)))))))
2502 (let (;; ignore @String entries because they are handled
2503 ;; separately by `bibtex-parse-strings'
2504 (bibtex-sort-ignore-string-entries t)
2505 bounds)
2506 (bibtex-map-entries
2507 (lambda (key beg end)
2508 (if (and abortable
2509 (input-pending-p))
2510 ;; user has aborted by typing a key: return `aborted'
2511 (throw 'userkey 'aborted))
2512 (if verbose (bibtex-progress-message))
2513 (unless (assoc key ref-keys)
2514 (push (cons key t) ref-keys))
2515 (if (and (setq bounds (bibtex-search-forward-field "crossref" end))
2516 (setq key (bibtex-text-in-field-bounds bounds t))
2517 (not (assoc key crossref-keys)))
2518 (push (list key) crossref-keys))))))
2520 (dolist (key crossref-keys)
2521 (unless (assoc (car key) ref-keys) (push key ref-keys)))
2522 (if verbose
2523 (bibtex-progress-message 'done))
2524 ;; successful operation --> return `bibtex-reference-keys'
2525 (setq bibtex-reference-keys ref-keys)))))))
2527 (defun bibtex-parse-strings (&optional add abortable)
2528 "Set `bibtex-strings' to the string definitions in the whole buffer.
2529 If ADD is non-nil add the new strings to `bibtex-strings' instead of
2530 simply resetting it. If ADD is an alist of strings, also add ADD to
2531 `bibtex-strings'. If ABORTABLE is non-nil abort on user input.
2532 Return alist of strings if parsing was completed, `aborted' otherwise."
2533 (save-excursion
2534 (save-match-data
2535 (goto-char (point-min))
2536 (let ((strings (if (and add (not (functionp bibtex-strings)))
2537 bibtex-strings))
2538 bounds key)
2539 (if (listp add)
2540 (dolist (string add)
2541 (unless (assoc-string (car string) strings t)
2542 (push string strings))))
2543 (catch 'userkey
2544 (while (setq bounds (bibtex-search-forward-string))
2545 (if (and abortable
2546 (input-pending-p))
2547 ;; user has aborted by typing a key --> return `aborted'
2548 (throw 'userkey 'aborted))
2549 (setq key (bibtex-reference-key-in-string bounds))
2550 (unless (assoc-string key strings t)
2551 (push (cons key (bibtex-text-in-string bounds t))
2552 strings))
2553 (goto-char (bibtex-end-of-text-in-string bounds)))
2554 ;; successful operation --> return `bibtex-strings'
2555 (setq bibtex-strings strings))))))
2557 (defun bibtex-strings ()
2558 "Return `bibtex-strings'. Initialize this variable if necessary."
2559 (if (functionp bibtex-strings)
2560 (bibtex-parse-strings (bibtex-string-files-init))
2561 bibtex-strings))
2563 (defun bibtex-string-files-init ()
2564 "Return initialization for `bibtex-strings'.
2565 Use `bibtex-predefined-strings' and BibTeX files `bibtex-string-files'."
2566 (save-match-data
2567 (let ((dirlist (split-string (or bibtex-string-file-path default-directory)
2568 ":+"))
2569 (case-fold-search)
2570 string-files fullfilename compl bounds found)
2571 ;; collect absolute file names of valid string files
2572 (dolist (filename bibtex-string-files)
2573 (unless (string-match "\\.bib\\'" filename)
2574 (setq filename (concat filename ".bib")))
2575 ;; test filenames
2576 (if (file-name-absolute-p filename)
2577 (if (file-readable-p filename)
2578 (push filename string-files)
2579 (error "BibTeX strings file %s not found" filename))
2580 (dolist (dir dirlist)
2581 (when (file-readable-p
2582 (setq fullfilename (expand-file-name filename dir)))
2583 (push fullfilename string-files)
2584 (setq found t)))
2585 (unless found
2586 (error "File %s not in paths defined via bibtex-string-file-path"
2587 filename))))
2588 ;; parse string files
2589 (dolist (filename string-files)
2590 (with-temp-buffer
2591 (insert-file-contents filename)
2592 (goto-char (point-min))
2593 (while (setq bounds (bibtex-search-forward-string))
2594 (push (cons (bibtex-reference-key-in-string bounds)
2595 (bibtex-text-in-string bounds t))
2596 compl)
2597 (goto-char (bibtex-end-of-string bounds)))))
2598 (append bibtex-predefined-strings (nreverse compl)))))
2600 (defun bibtex-parse-buffers-stealthily ()
2601 "Parse buffer in the background during idle time.
2602 Called by `run-with-idle-timer'. Whenever Emacs has been idle
2603 for `bibtex-parse-keys-timeout' seconds, parse all BibTeX buffers
2604 which have been modified after last parsing.
2605 Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
2606 (save-excursion
2607 (let ((buffers (buffer-list))
2608 (strings-init (bibtex-string-files-init)))
2609 (while (and buffers (not (input-pending-p)))
2610 (set-buffer (car buffers))
2611 (if (and (eq major-mode 'bibtex-mode)
2612 (not (eq (buffer-modified-tick)
2613 bibtex-buffer-last-parsed-tick)))
2614 (save-restriction
2615 (widen)
2616 ;; Output no progress messages in `bibtex-parse-keys'
2617 ;; because when in `y-or-n-p' that can hide the question.
2618 (if (and (listp (bibtex-parse-keys t))
2619 ;; update `bibtex-strings'
2620 (listp (bibtex-parse-strings strings-init t)))
2622 ;; remember that parsing was successful
2623 (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick)))))
2624 (setq buffers (cdr buffers))))))
2626 ;;;###autoload
2627 (defun bibtex-initialize (&optional current force select)
2628 "(Re)Initialize BibTeX buffers.
2629 Visit the BibTeX files defined by `bibtex-files' and return a list
2630 of corresponding buffers.
2631 Initialize in these buffers `bibtex-reference-keys' if not yet set.
2632 List of BibTeX buffers includes current buffer if CURRENT is non-nil.
2633 If FORCE is non-nil, (re)initialize `bibtex-reference-keys' even if
2634 already set. If SELECT is non-nil interactively select a BibTeX buffer.
2635 When called interactively, FORCE is t, CURRENT is t if current buffer uses
2636 `bibtex-mode', and SELECT is t if current buffer does not use `bibtex-mode',"
2637 (interactive (list (eq major-mode 'bibtex-mode) t
2638 (not (eq major-mode 'bibtex-mode))))
2639 (let ((file-path (split-string (or bibtex-file-path default-directory) ":+"))
2640 file-list dir-list buffer-list)
2641 ;; generate list of BibTeX files
2642 (dolist (file bibtex-files)
2643 (cond ((eq file 'bibtex-file-path)
2644 (setq dir-list (append dir-list file-path)))
2645 ((file-accessible-directory-p file)
2646 (push file dir-list))
2647 ((progn (unless (string-match "\\.bib\\'" file)
2648 (setq file (concat file ".bib")))
2649 (file-name-absolute-p file))
2650 (push file file-list))
2652 (let (expanded-file-name found)
2653 (dolist (dir file-path)
2654 (when (file-readable-p
2655 (setq expanded-file-name (expand-file-name file dir)))
2656 (push expanded-file-name file-list)
2657 (setq found t)))
2658 (unless found
2659 (error "File `%s' not in paths defined via bibtex-file-path"
2660 file))))))
2661 (dolist (file file-list)
2662 (unless (file-readable-p file)
2663 (error "BibTeX file `%s' not found" file)))
2664 ;; expand dir-list
2665 (dolist (dir dir-list)
2666 (setq file-list
2667 (append file-list (directory-files dir t "\\.bib\\'" t))))
2668 (delete-dups file-list)
2669 ;; visit files in FILE-LIST
2670 (dolist (file file-list)
2671 (if (file-readable-p file)
2672 (push (find-file-noselect file) buffer-list)))
2673 ;; Include current buffer iff we want it.
2674 ;; Exclude current buffer if it doesn't use `bibtex-mode'.
2675 ;; Thus calling `bibtex-initialize' gives meaningful results for
2676 ;; any current buffer.
2677 (unless (and current (eq major-mode 'bibtex-mode)) (setq current nil))
2678 (cond ((and current (not (memq (current-buffer) buffer-list)))
2679 (push (current-buffer) buffer-list))
2680 ((and (not current) (memq (current-buffer) buffer-list))
2681 (setq buffer-list (delq (current-buffer) buffer-list))))
2682 ;; parse keys
2683 (dolist (buffer buffer-list)
2684 (with-current-buffer buffer
2685 (if (or force (functionp bibtex-reference-keys))
2686 (bibtex-parse-keys))))
2687 ;; select BibTeX buffer
2688 (if select
2689 (if buffer-list
2690 (switch-to-buffer
2691 (completing-read "Switch to BibTeX buffer: "
2692 (mapcar 'buffer-name buffer-list)
2693 nil t
2694 (if current (buffer-name (current-buffer)))))
2695 (message "No BibTeX buffers defined")))
2696 buffer-list))
2698 (defun bibtex-complete-internal (completions)
2699 "Complete word fragment before point to longest prefix of COMPLETIONS.
2700 COMPLETIONS is an alist of strings. If point is not after the part
2701 of a word, all strings are listed. Return completion."
2702 ;; Return value is used by cleanup functions.
2703 ;; Code inspired by `lisp-complete-symbol'.
2704 (let* ((case-fold-search t)
2705 (beg (save-excursion
2706 (re-search-backward "[ \t{\"]")
2707 (forward-char)
2708 (point)))
2709 (end (point))
2710 (pattern (buffer-substring-no-properties beg end))
2711 (completion (try-completion pattern completions)))
2712 (cond ((not completion)
2713 (error "Can't find completion for `%s'" pattern))
2714 ((eq completion t)
2715 pattern)
2716 ((not (string= pattern completion))
2717 (delete-region beg end)
2718 (insert completion)
2719 ;; Don't leave around a completions buffer that's out of date.
2720 (let ((win (get-buffer-window "*Completions*" 0)))
2721 (if win (with-selected-window win (bury-buffer))))
2722 completion)
2724 (let ((minibuf-is-in-use
2725 (eq (minibuffer-window) (selected-window))))
2726 (unless minibuf-is-in-use (message "Making completion list..."))
2727 (with-output-to-temp-buffer "*Completions*"
2728 (display-completion-list
2729 (sort (all-completions pattern completions) 'string<) pattern))
2730 (unless minibuf-is-in-use
2731 (message "Making completion list...done")))
2732 nil))))
2734 (defun bibtex-complete-string-cleanup (str compl)
2735 "Cleanup after inserting string STR.
2736 Remove enclosing field delimiters for STR. Display message with
2737 expansion of STR using expansion list COMPL."
2738 ;; point is at position inside field where completion was requested
2739 (save-excursion
2740 (let ((abbr (cdr (if (stringp str)
2741 (assoc-string str compl t)))))
2742 (if abbr (message "Abbreviation for `%s'" abbr))
2743 (bibtex-remove-delimiters))))
2745 (defun bibtex-complete-crossref-cleanup (key)
2746 "Display summary message on entry KEY after completion of a crossref key.
2747 Use `bibtex-summary-function' to generate summary."
2748 (save-excursion
2749 (if (and (stringp key)
2750 (bibtex-search-entry key t))
2751 (message "Ref: %s" (funcall bibtex-summary-function)))))
2753 (defun bibtex-copy-summary-as-kill (&optional arg)
2754 "Push summery of current BibTeX entry to kill ring.
2755 Use `bibtex-summary-function' to generate summary.
2756 If prefix ARG is non-nil push BibTeX entry's URL to kill ring
2757 that is generated by calling `bibtex-url'."
2758 (interactive "P")
2759 (if arg (let ((url (bibtex-url nil t)))
2760 (if url (kill-new (message "%s" url))
2761 (message "No URL known")))
2762 (save-excursion
2763 (bibtex-beginning-of-entry)
2764 (if (looking-at bibtex-entry-maybe-empty-head)
2765 (kill-new (message "%s" (funcall bibtex-summary-function)))
2766 (error "No entry found")))))
2768 (defun bibtex-summary ()
2769 "Return summary of current BibTeX entry.
2770 Used as default value of `bibtex-summary-function'."
2771 ;; It would be neat to make this function customizable. How?
2772 (if (looking-at bibtex-entry-maybe-empty-head)
2773 (let* ((bibtex-autokey-name-case-convert-function 'identity)
2774 (bibtex-autokey-name-length 'infty)
2775 (bibtex-autokey-names 1)
2776 (bibtex-autokey-names-stretch 0)
2777 (bibtex-autokey-name-separator " ")
2778 (bibtex-autokey-additional-names " etal")
2779 (names (bibtex-autokey-get-names))
2780 (bibtex-autokey-year-length 4)
2781 (year (bibtex-autokey-get-year))
2782 (bibtex-autokey-titlewords 5)
2783 (bibtex-autokey-titlewords-stretch 2)
2784 (bibtex-autokey-titleword-case-convert-function 'identity)
2785 (bibtex-autokey-titleword-length 5)
2786 (bibtex-autokey-titleword-separator " ")
2787 (title (bibtex-autokey-get-title))
2788 (journal (bibtex-autokey-get-field
2789 "journal" bibtex-autokey-transcriptions))
2790 (volume (bibtex-autokey-get-field "volume"))
2791 (pages (bibtex-autokey-get-field "pages" '(("-.*\\'" . "")))))
2792 (mapconcat (lambda (arg)
2793 (if (not (string= "" (cdr arg)))
2794 (concat (car arg) (cdr arg))))
2795 `((" " . ,names) (" " . ,year) (": " . ,title)
2796 (", " . ,journal) (" " . ,volume) (":" . ,pages))
2797 ""))
2798 (error "Entry not found")))
2800 (defun bibtex-pop (arg direction)
2801 "Fill current field from the ARGth same field's text in DIRECTION.
2802 Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
2803 ;; parse current field
2804 (let* ((bounds (bibtex-enclosing-field t))
2805 (start-old-field (bibtex-start-of-field bounds))
2806 (start-old-text (bibtex-start-of-text-in-field bounds))
2807 (end-old-text (bibtex-end-of-text-in-field bounds))
2808 (field-name (bibtex-name-in-field bounds t))
2809 failure)
2810 (save-excursion
2811 ;; if executed several times in a row, start each search where
2812 ;; the last one was finished
2813 (cond ((eq last-command 'bibtex-pop)
2814 (goto-char (if (eq direction 'previous)
2815 bibtex-pop-previous-search-point
2816 bibtex-pop-next-search-point)))
2817 ((eq direction 'previous)
2818 (bibtex-beginning-of-entry))
2819 (t (bibtex-end-of-entry)))
2820 ;; Search for arg'th previous/next similar field
2821 (while (and (not failure)
2822 (>= (setq arg (1- arg)) 0))
2823 ;; The search of BibTeX fields is not bounded by entry boundaries
2824 (if (eq direction 'previous)
2825 (if (setq bounds (bibtex-search-backward-field field-name))
2826 (goto-char (bibtex-start-of-field bounds))
2827 (setq failure t))
2828 (if (setq bounds (bibtex-search-forward-field field-name))
2829 (goto-char (bibtex-end-of-field bounds))
2830 (setq failure t))))
2831 (if failure
2832 (error "No %s matching BibTeX field"
2833 (if (eq direction 'previous) "previous" "next"))
2834 ;; Found a matching field. Remember boundaries.
2835 (let ((new-text (bibtex-text-in-field-bounds bounds))
2836 (nbeg (copy-marker (bibtex-start-of-field bounds)))
2837 (nend (copy-marker (bibtex-end-of-field bounds))))
2838 (bibtex-flash-head "From: ")
2839 ;; Go back to where we started, delete old text, and pop new.
2840 (goto-char end-old-text)
2841 (delete-region start-old-text end-old-text)
2842 (if (= nbeg start-old-field)
2843 (insert (bibtex-field-left-delimiter)
2844 (bibtex-field-right-delimiter))
2845 (insert new-text))
2846 (setq bibtex-pop-previous-search-point (marker-position nbeg)
2847 bibtex-pop-next-search-point (marker-position nend))))))
2848 (bibtex-find-text nil nil nil t)
2849 (setq this-command 'bibtex-pop))
2851 (defun bibtex-beginning-of-field ()
2852 "Move point backward to beginning of field.
2853 This function uses a simple, fast algorithm assuming that the field
2854 begins at the beginning of a line. We use this function for font-locking."
2855 (let ((field-reg (concat "^[ \t]*" bibtex-field-name "[ \t]*=")))
2856 (beginning-of-line)
2857 (unless (looking-at field-reg)
2858 (re-search-backward field-reg nil t))))
2860 (defun bibtex-font-lock-url (bound &optional no-button)
2861 "Font-lock for URLs. BOUND limits the search.
2862 If NO-BUTTON is non-nil do not generate buttons."
2863 (let ((case-fold-search t)
2864 (pnt (point))
2865 name bounds start end found)
2866 (bibtex-beginning-of-field)
2867 (while (and (not found)
2868 (<= (point) bound)
2869 (prog1 (re-search-forward bibtex-font-lock-url-regexp bound t)
2870 (setq name (match-string-no-properties 1)))
2871 (setq bounds (bibtex-parse-field-text))
2872 (progn
2873 (setq start (car bounds) end (nth 1 bounds))
2874 ;; Always ignore field delimiters
2875 (if (memq (char-before end) '(?\} ?\"))
2876 (setq end (1- end)))
2877 (if (memq (char-after start) '(?\{ ?\"))
2878 (setq start (1+ start)))
2879 (if (< start pnt) (setq start (min pnt end)))
2880 (<= start bound)))
2881 (if (<= pnt start)
2882 (let ((lst bibtex-generate-url-list) url)
2883 (while (and (not found) (setq url (car (pop lst))))
2884 (goto-char start)
2885 (setq found (and (bibtex-string= name (car url))
2886 (re-search-forward (cdr url) end t))))))
2887 (unless found (goto-char end)))
2888 (if (and found (not no-button))
2889 (bibtex-button (match-beginning 0) (match-end 0)
2890 'bibtex-url (match-beginning 0)))
2891 found))
2893 (defun bibtex-font-lock-crossref (bound)
2894 "Font-lock for crossref fields. BOUND limits the search."
2895 (let ((case-fold-search t)
2896 (pnt (point))
2897 (crossref-reg (concat "^[ \t]*crossref[ \t]*=[ \t\n]*"
2898 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]"))
2899 start end found)
2900 (bibtex-beginning-of-field)
2901 (while (and (not found)
2902 (re-search-forward crossref-reg bound t))
2903 (setq start (1+ (match-beginning 1))
2904 end (1- (match-end 1))
2905 found (>= start pnt)))
2906 (if found (bibtex-button start end 'bibtex-search-crossref
2907 (buffer-substring-no-properties start end)
2908 start t))
2909 found))
2911 (defun bibtex-font-lock-cite (matcher bound)
2912 "Font-lock for cited keys.
2913 MATCHER identifies the cited key, see `bibtex-cite-matcher-alist'.
2914 BOUND limits the search."
2915 (let (case-fold-search)
2916 (if (re-search-forward (car matcher) bound t)
2917 (let ((start (match-beginning (cdr matcher)))
2918 (end (match-end (cdr matcher))))
2919 (bibtex-button start end 'bibtex-search-crossref
2920 (buffer-substring-no-properties start end)
2921 start t t)
2922 t))))
2924 (defun bibtex-button-action (button)
2925 "Call BUTTON's BibTeX function."
2926 (apply (button-get button 'bibtex-function)
2927 (button-get button 'bibtex-args)))
2929 (define-button-type 'bibtex-url
2930 'action 'bibtex-button-action
2931 'bibtex-function 'bibtex-url
2932 'help-echo (purecopy "mouse-2, RET: follow URL"))
2934 (define-button-type 'bibtex-search-crossref
2935 'action 'bibtex-button-action
2936 'bibtex-function 'bibtex-search-crossref
2937 'help-echo (purecopy "mouse-2, RET: follow crossref"))
2939 (defun bibtex-button (beg end type &rest args)
2940 "Make a BibTeX button from BEG to END of type TYPE in the current buffer."
2941 (make-text-button beg end 'type type 'bibtex-args args))
2944 ;; Interactive Functions:
2946 ;;;###autoload
2947 (defun bibtex-mode ()
2948 "Major mode for editing BibTeX files.
2950 General information on working with BibTeX mode:
2952 Use commands such as \\<bibtex-mode-map>\\[bibtex-Book] to get a template for a specific entry.
2953 Then fill in all desired fields using \\[bibtex-next-field] to jump from field
2954 to field. After having filled in all desired fields in the entry, clean the
2955 new entry with the command \\[bibtex-clean-entry].
2957 Some features of BibTeX mode are available only by setting the variable
2958 `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode
2959 works only with buffers containing valid (syntactical correct) and sorted
2960 entries. This is usually the case, if you have created a buffer completely
2961 with BibTeX mode and finished every new entry with \\[bibtex-clean-entry].
2963 For third party BibTeX files, call the command \\[bibtex-convert-alien]
2964 to fully take advantage of all features of BibTeX mode.
2967 Special information:
2969 A command such as \\[bibtex-Book] outlines the fields for a BibTeX book entry.
2971 The names of optional fields start with the string OPT, and are thus ignored
2972 by BibTeX. The names of alternative fields from which only one is required
2973 start with the string ALT. The OPT or ALT string may be removed from
2974 the name of a field with \\[bibtex-remove-OPT-or-ALT].
2975 \\[bibtex-make-field] inserts a new field after the current one.
2976 \\[bibtex-kill-field] kills the current field entirely.
2977 \\[bibtex-yank] yanks the last recently killed field after the current field.
2978 \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
2979 \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
2980 \\[bibtex-find-text] moves point to the end of the current field.
2981 \\[bibtex-complete] completes word fragment before point according to context.
2983 The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2984 from the names of all non-empty optional or alternative fields, checks that
2985 no required fields are empty, and does some formatting dependent on the value
2986 of `bibtex-entry-format'. Furthermore, it can automatically generate a key
2987 for the BibTeX entry, see `bibtex-generate-autokey'.
2988 Note: some functions in BibTeX mode depend on entries being in a special
2989 format (all fields beginning on separate lines), so it is usually a bad
2990 idea to remove `realign' from `bibtex-entry-format'.
2992 BibTeX mode supports Imenu and hideshow minor mode (`hs-minor-mode').
2994 ----------------------------------------------------------
2995 Entry to BibTeX mode calls the value of `bibtex-mode-hook'
2996 if that value is non-nil.
2998 \\{bibtex-mode-map}"
2999 (interactive)
3000 (kill-all-local-variables)
3001 (use-local-map bibtex-mode-map)
3002 (setq major-mode 'bibtex-mode)
3003 (setq mode-name "BibTeX")
3004 (set-syntax-table bibtex-mode-syntax-table)
3005 (make-local-variable 'bibtex-buffer-last-parsed-tick)
3006 ;; Install stealthy parse function if not already installed
3007 (unless bibtex-parse-idle-timer
3008 (setq bibtex-parse-idle-timer (run-with-idle-timer
3009 bibtex-parse-keys-timeout t
3010 'bibtex-parse-buffers-stealthily)))
3011 (set (make-local-variable 'paragraph-start) "[ \f\n\t]*$")
3012 (set (make-local-variable 'comment-start) bibtex-comment-start)
3013 (set (make-local-variable 'comment-start-skip)
3014 (concat (regexp-quote bibtex-comment-start) "\\>[ \t]*"))
3015 (set (make-local-variable 'comment-column) 0)
3016 (set (make-local-variable 'defun-prompt-regexp) "^[ \t]*@[[:alnum:]]+[ \t]*")
3017 (set (make-local-variable 'outline-regexp) "[ \t]*@")
3018 (set (make-local-variable 'fill-paragraph-function) 'bibtex-fill-field)
3019 (set (make-local-variable 'fill-prefix) (make-string (+ bibtex-entry-offset
3020 bibtex-contline-indentation)
3021 ?\s))
3022 (set (make-local-variable 'font-lock-defaults)
3023 '(bibtex-font-lock-keywords
3024 nil t ((?$ . "\"")
3025 ;; Mathematical expressions should be fontified as strings
3026 (?\" . ".")
3027 ;; Quotes are field delimiters and quote-delimited
3028 ;; entries should be fontified in the same way as
3029 ;; brace-delimited ones
3032 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords)
3033 (font-lock-extra-managed-props . (category))
3034 (font-lock-mark-block-function
3035 . (lambda ()
3036 (set-mark (bibtex-end-of-entry))
3037 (bibtex-beginning-of-entry)))))
3038 (setq imenu-generic-expression
3039 (list (list nil bibtex-entry-head bibtex-key-in-head))
3040 imenu-case-fold-search t)
3041 (make-local-variable 'choose-completion-string-functions)
3042 ;; XEmacs needs `easy-menu-add', Emacs does not care
3043 (easy-menu-add bibtex-edit-menu)
3044 (easy-menu-add bibtex-entry-menu)
3045 (run-mode-hooks 'bibtex-mode-hook))
3047 (defun bibtex-field-list (entry-type)
3048 "Return list of allowed fields for entry ENTRY-TYPE.
3049 More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),
3050 where REQUIRED and OPTIONAL are lists of the required and optional field
3051 names for ENTRY-TYPE according to `bibtex-entry-field-alist',
3052 `bibtex-include-OPTkey', `bibtex-include-OPTcrossref',
3053 and `bibtex-user-optional-fields'."
3054 (let ((e (assoc-string entry-type bibtex-entry-field-alist t))
3055 required optional)
3056 (unless e
3057 (error "Fields for BibTeX entry type %s not defined" entry-type))
3058 (if (and (member-ignore-case entry-type bibtex-include-OPTcrossref)
3059 (nth 2 e))
3060 (setq required (nth 0 (nth 2 e))
3061 optional (nth 1 (nth 2 e)))
3062 (setq required (nth 0 (nth 1 e))
3063 optional (nth 1 (nth 1 e))))
3064 (if bibtex-include-OPTkey
3065 (push (list "key"
3066 "Used for reference key creation if author and editor fields are missing"
3067 (if (or (stringp bibtex-include-OPTkey)
3068 (functionp bibtex-include-OPTkey))
3069 bibtex-include-OPTkey))
3070 optional))
3071 (if (member-ignore-case entry-type bibtex-include-OPTcrossref)
3072 (push '("crossref" "Reference key of the cross-referenced entry")
3073 optional))
3074 (setq optional (append optional bibtex-user-optional-fields))
3075 (cons required optional)))
3077 (defun bibtex-entry (entry-type)
3078 "Insert a new BibTeX entry of type ENTRY-TYPE.
3079 After insertion call the value of `bibtex-add-entry-hook' if that value
3080 is non-nil."
3081 (interactive
3082 (let ((completion-ignore-case t))
3083 (list (completing-read "Entry Type: " bibtex-entry-field-alist
3084 nil t nil 'bibtex-entry-type-history))))
3085 (let ((key (if bibtex-maintain-sorted-entries
3086 (bibtex-read-key (format "%s key: " entry-type))))
3087 (field-list (bibtex-field-list entry-type)))
3088 (unless (bibtex-prepare-new-entry (list key nil entry-type))
3089 (error "Entry with key `%s' already exists" key))
3090 (indent-to-column bibtex-entry-offset)
3091 (insert "@" entry-type (bibtex-entry-left-delimiter))
3092 (if key (insert key))
3093 (save-excursion
3094 (mapc 'bibtex-make-field (car field-list))
3095 (mapc 'bibtex-make-optional-field (cdr field-list))
3096 (if bibtex-comma-after-last-field
3097 (insert ","))
3098 (insert "\n")
3099 (indent-to-column bibtex-entry-offset)
3100 (insert (bibtex-entry-right-delimiter) "\n\n"))
3101 (bibtex-next-field t)
3102 (if (member-ignore-case entry-type bibtex-autofill-types)
3103 (bibtex-autofill-entry))
3104 (run-hooks 'bibtex-add-entry-hook)))
3106 (defun bibtex-entry-update (&optional entry-type)
3107 "Update an existing BibTeX entry.
3108 In the BibTeX entry at point, make new fields for those items that may occur
3109 according to `bibtex-field-list', but are not yet present.
3110 Also, add field delimiters to numerical fields if they are not present.
3111 If ENTRY-TYPE is non-nil, change first the entry type to ENTRY-TYPE.
3112 When called interactively with a prefix arg, query for a value of ENTRY-TYPE."
3113 (interactive
3114 (list (if current-prefix-arg
3115 (let ((completion-ignore-case t))
3116 (completing-read "New entry type: " bibtex-entry-field-alist
3117 nil t nil 'bibtex-entry-type-history)))))
3118 (save-excursion
3119 (bibtex-beginning-of-entry)
3120 (when (looking-at bibtex-entry-maybe-empty-head)
3121 (goto-char (match-end 0))
3122 (if entry-type
3123 (save-excursion
3124 (replace-match (concat "@" entry-type) nil nil nil 1))
3125 (setq entry-type (bibtex-type-in-head)))
3126 (let* ((field-list (bibtex-field-list entry-type))
3127 (required (copy-tree (car field-list)))
3128 (optional (copy-tree (cdr field-list)))
3129 bounds)
3130 (while (setq bounds (bibtex-parse-field))
3131 (let ((fname (bibtex-name-in-field bounds t))
3132 (end (copy-marker (bibtex-end-of-field bounds) t)))
3133 (setq required (delete (assoc-string fname required t) required)
3134 optional (delete (assoc-string fname optional t) optional))
3135 (when (string-match "\\`[0-9]+\\'"
3136 (bibtex-text-in-field-bounds bounds))
3137 (goto-char (bibtex-end-of-text-in-field bounds))
3138 (insert (bibtex-field-right-delimiter))
3139 (goto-char (bibtex-start-of-text-in-field bounds))
3140 (insert (bibtex-field-left-delimiter)))
3141 (goto-char end)))
3142 (skip-chars-backward " \t\n")
3143 (dolist (field required) (bibtex-make-field field))
3144 (dolist (field optional) (bibtex-make-optional-field field))))))
3146 (defun bibtex-parse-entry (&optional content)
3147 "Parse entry at point, return an alist.
3148 The alist elements have the form (FIELD . TEXT), where FIELD can also be
3149 the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\"
3150 TEXT may be nil. Remove \"OPT\" and \"ALT\" from FIELD.
3151 Move point to the end of the last field.
3152 If optional arg CONTENT is non-nil extract content of text fields."
3153 (let (alist bounds)
3154 (when (looking-at bibtex-entry-maybe-empty-head)
3155 (push (cons "=type=" (bibtex-type-in-head)) alist)
3156 (push (cons "=key=" (bibtex-key-in-head)) alist)
3157 (goto-char (match-end 0))
3158 (while (setq bounds (bibtex-parse-field))
3159 (push (cons (bibtex-name-in-field bounds t)
3160 (bibtex-text-in-field-bounds bounds content))
3161 alist)
3162 (goto-char (bibtex-end-of-field bounds))))
3163 alist))
3165 (defun bibtex-autofill-entry ()
3166 "Try to fill fields of current BibTeX entry based on neighboring entries.
3167 The current entry must have a key. Determine the neighboring entry
3168 \(previouse or next\) whose key is more similar to the key of the current
3169 entry. For all empty fields of the current entry insert the corresponding
3170 field contents of the neighboring entry. Finally try to update the text
3171 based on the difference between the keys of the neighboring and the current
3172 entry (for example, the year parts of the keys)."
3173 (interactive)
3174 (bibtex-beginning-of-entry)
3175 (when (looking-at bibtex-entry-head)
3176 (let ((type (bibtex-type-in-head))
3177 (key (bibtex-key-in-head))
3178 (key-end (match-end bibtex-key-in-head))
3179 (case-fold-search t)
3180 (bibtex-sort-ignore-string-entries t)
3181 tmp other-key other bounds)
3182 ;; The fields we want to change start right after the key.
3183 (goto-char key-end)
3184 ;; First see whether to use the previous or the next entry
3185 ;; for "inspiration".
3186 (save-excursion
3187 (goto-char (1- (match-beginning 0)))
3188 (bibtex-beginning-of-entry)
3189 (if (and (looking-at bibtex-entry-head)
3190 (bibtex-string= type (bibtex-type-in-head))
3191 ;; In case we found ourselves :-(
3192 (not (equal key (setq tmp (bibtex-key-in-head)))))
3193 (setq other-key tmp
3194 other (point))))
3195 (save-excursion
3196 (bibtex-end-of-entry)
3197 (bibtex-skip-to-valid-entry)
3198 (if (and (looking-at bibtex-entry-head)
3199 (bibtex-string= type (bibtex-type-in-head))
3200 ;; In case we found ourselves :-(
3201 (not (equal key (setq tmp (bibtex-key-in-head))))
3202 (or (not other-key)
3203 ;; Check which is the best match.
3204 (< (length (try-completion "" (list key other-key)))
3205 (length (try-completion "" (list key tmp))))))
3206 (setq other-key tmp
3207 other (point))))
3208 ;; Then fill the new entry's fields with the chosen other entry.
3209 (when other
3210 (setq other (save-excursion (goto-char other) (bibtex-parse-entry)))
3211 (setq key-end (point)) ;In case parse-entry changed the buffer.
3212 (while (setq bounds (bibtex-parse-field))
3213 (let ((text (assoc-string (bibtex-name-in-field bounds t)
3214 other t)))
3215 (if (not (and text
3216 (equal "" (bibtex-text-in-field-bounds bounds t))))
3217 (goto-char (bibtex-end-of-field bounds))
3218 (goto-char (bibtex-start-of-text-in-field bounds))
3219 (delete-region (point) (bibtex-end-of-text-in-field bounds))
3220 (insert (cdr text)))))
3221 ;; Finally try to update the text based on the difference between
3222 ;; the two keys.
3223 (let* ((prefix (try-completion "" (list key other-key)))
3224 ;; If the keys are foo91 and foo92, don't replace 1 for 2
3225 ;; but 91 for 92 instead.
3226 (_ (if (string-match "[0-9]+\\'" prefix)
3227 (setq prefix (substring prefix 0 (match-beginning 0)))))
3228 (suffix (substring key (length prefix)))
3229 (other-suffix (substring other-key (length prefix))))
3230 (while (re-search-backward (regexp-quote other-suffix) key-end 'move)
3231 (replace-match suffix)))))))
3233 (defun bibtex-print-help-message (&optional field comma)
3234 "Print helpful information about current FIELD in current BibTeX entry.
3235 Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
3236 interactive calls."
3237 (interactive (list nil t))
3238 (unless field (setq field (car (bibtex-find-text-internal nil nil comma))))
3239 (if (string-match "@" field)
3240 (cond ((bibtex-string= field "@string")
3241 (message "String definition"))
3242 ((bibtex-string= field "@preamble")
3243 (message "Preamble definition"))
3244 (t (message "Entry key")))
3245 (let* ((case-fold-search t)
3246 (type (save-excursion
3247 (bibtex-beginning-of-entry)
3248 (looking-at bibtex-entry-maybe-empty-head)
3249 (bibtex-type-in-head)))
3250 (field-list (bibtex-field-list type))
3251 (comment (assoc-string field (append (car field-list)
3252 (cdr field-list)) t)))
3253 (if comment (message "%s" (nth 1 comment))
3254 (message "No comment available")))))
3256 (defun bibtex-make-field (field &optional move interactive nodelim)
3257 "Make a field named FIELD in current BibTeX entry.
3258 FIELD is either a string or a list of the form
3259 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
3260 `bibtex-entry-field-alist'.
3261 If MOVE is non-nil, move point past the present field before making
3262 the new field. If INTERACTIVE is non-nil, move point to the end of
3263 the new field. Otherwise move point past the new field.
3264 MOVE and INTERACTIVE are t when called interactively.
3265 INIT is surrounded by field delimiters, unless NODELIM is non-nil."
3266 (interactive
3267 (list (let ((completion-ignore-case t)
3268 (field-list (bibtex-field-list
3269 (save-excursion
3270 (bibtex-beginning-of-entry)
3271 (looking-at bibtex-any-entry-maybe-empty-head)
3272 (bibtex-type-in-head)))))
3273 (completing-read "BibTeX field name: "
3274 (append (car field-list) (cdr field-list))
3275 nil nil nil bibtex-field-history))
3276 t t))
3277 (unless (consp field)
3278 (setq field (list field)))
3279 (when move
3280 (bibtex-find-text)
3281 (if (looking-at "[}\"]")
3282 (forward-char)))
3283 (insert ",\n")
3284 (indent-to-column (+ bibtex-entry-offset bibtex-field-indentation))
3285 (if (nth 3 field) (insert "ALT"))
3286 (insert (car field) " ")
3287 (if bibtex-align-at-equal-sign
3288 (indent-to-column (+ bibtex-entry-offset
3289 (- bibtex-text-indentation 2))))
3290 (insert "= ")
3291 (unless bibtex-align-at-equal-sign
3292 (indent-to-column (+ bibtex-entry-offset
3293 bibtex-text-indentation)))
3294 (let ((init (nth 2 field)))
3295 (if (not init) (setq init "")
3296 (if (functionp init) (setq init (funcall init)))
3297 (unless (stringp init) (error "`%s' is not a string" init)))
3298 ;; NODELIM is required by `bibtex-insert-kill'
3299 (if nodelim (insert init)
3300 (insert (bibtex-field-left-delimiter) init
3301 (bibtex-field-right-delimiter))))
3302 (when interactive
3303 ;; (bibtex-find-text nil nil bibtex-help-message)
3304 (if (memq (preceding-char) '(?} ?\")) (forward-char -1))
3305 (if bibtex-help-message (bibtex-print-help-message (car field)))))
3307 (defun bibtex-beginning-of-entry ()
3308 "Move to beginning of BibTeX entry (beginning of line).
3309 If inside an entry, move to the beginning of it, otherwise move to the
3310 beginning of the previous entry. If point is ahead of all BibTeX entries
3311 move point to the beginning of buffer. Return the new location of point."
3312 (interactive)
3313 (skip-chars-forward " \t")
3314 (if (looking-at "@")
3315 (forward-char))
3316 (re-search-backward "^[ \t]*@" nil 'move)
3317 (point))
3319 (defun bibtex-end-of-entry ()
3320 "Move to end of BibTeX entry (past the closing brace).
3321 If inside an entry, move to the end of it, otherwise move to the end
3322 of the previous entry. Do not move if ahead of first entry.
3323 Return the new location of point."
3324 (interactive)
3325 (let ((case-fold-search t)
3326 (pnt (point))
3327 (_ (bibtex-beginning-of-entry))
3328 (bounds (bibtex-valid-entry t)))
3329 (cond (bounds (goto-char (cdr bounds))) ; regular entry
3330 ;; @String or @Preamble
3331 ((setq bounds (or (bibtex-parse-string t) (bibtex-parse-preamble)))
3332 (goto-char (bibtex-end-of-string bounds)))
3333 ((looking-at bibtex-any-valid-entry-type)
3334 ;; Parsing of entry failed
3335 (error "Syntactically incorrect BibTeX entry starts here"))
3336 (t (if (interactive-p) (message "Not on a known BibTeX entry."))
3337 (goto-char pnt)))
3338 (point)))
3340 (defun bibtex-goto-line (arg)
3341 "Goto line ARG, counting from beginning of (narrowed) buffer."
3342 ;; code adapted from `goto-line'
3343 (goto-char (point-min))
3344 (if (eq selective-display t)
3345 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
3346 (forward-line (1- arg))))
3348 (defun bibtex-reposition-window ()
3349 "Make the current BibTeX entry visible.
3350 If entry is smaller than `window-body-height', entry is centered in window.
3351 Otherwise display the beginning of entry."
3352 (interactive)
3353 (let ((pnt (point))
3354 (beg (line-number-at-pos (bibtex-beginning-of-entry)))
3355 (end (line-number-at-pos (bibtex-end-of-entry))))
3356 (if (> (window-body-height) (- end beg))
3357 ;; entry fits in current window
3358 (progn
3359 (bibtex-goto-line (/ (+ 1 beg end) 2))
3360 (recenter)
3361 (goto-char pnt))
3362 ;; entry too large for current window
3363 (bibtex-goto-line beg)
3364 (recenter 0)
3365 (if (> (1+ (- (line-number-at-pos pnt) beg))
3366 (window-body-height))
3367 (bibtex-goto-line beg)
3368 (goto-char pnt)))))
3370 (defun bibtex-mark-entry ()
3371 "Put mark at beginning, point at end of current BibTeX entry."
3372 (interactive)
3373 (push-mark (bibtex-beginning-of-entry))
3374 (bibtex-end-of-entry))
3376 (defun bibtex-count-entries (&optional count-string-entries)
3377 "Count number of entries in current buffer or region.
3378 With prefix argument COUNT-STRING-ENTRIES count all entries,
3379 otherwise count all entries except @String entries.
3380 If mark is active count entries in region, if not in whole buffer."
3381 (interactive "P")
3382 (let ((number 0)
3383 (bibtex-sort-ignore-string-entries (not count-string-entries)))
3384 (save-restriction
3385 (if mark-active (narrow-to-region (region-beginning) (region-end)))
3386 (bibtex-map-entries (lambda (key beg end) (setq number (1+ number)))))
3387 (message "%s contains %d entries."
3388 (if mark-active "Region" "Buffer")
3389 number)))
3391 (defun bibtex-ispell-entry ()
3392 "Check BibTeX entry for spelling errors."
3393 (interactive)
3394 (ispell-region (save-excursion (bibtex-beginning-of-entry))
3395 (save-excursion (bibtex-end-of-entry))))
3397 (defun bibtex-ispell-abstract ()
3398 "Check abstract of BibTeX entry for spelling errors."
3399 (interactive)
3400 (let ((bounds (save-excursion
3401 (bibtex-beginning-of-entry)
3402 (bibtex-search-forward-field "abstract" t))))
3403 (if bounds
3404 (ispell-region (bibtex-start-of-text-in-field bounds)
3405 (bibtex-end-of-text-in-field bounds))
3406 (error "No abstract in entry"))))
3408 (defun bibtex-narrow-to-entry ()
3409 "Narrow buffer to current BibTeX entry."
3410 (interactive)
3411 (save-excursion
3412 (widen)
3413 (narrow-to-region (bibtex-beginning-of-entry)
3414 (bibtex-end-of-entry))))
3416 (defun bibtex-entry-index ()
3417 "Return index of BibTeX entry head at or past position of point.
3418 The index is a list (KEY CROSSREF-KEY ENTRY-TYPE) that is used for sorting
3419 the entries of the BibTeX buffer. CROSSREF-KEY is nil unless the value
3420 of `bibtex-maintain-sorted-entries' is `crossref'. Move point to the end
3421 of the head of the entry found. Return nil if no entry found."
3422 (let ((case-fold-search t))
3423 (if (re-search-forward bibtex-entry-maybe-empty-head nil t)
3424 (let ((key (bibtex-key-in-head))
3425 ;; all entry types should be downcase (for ease of comparison)
3426 (entry-type (downcase (bibtex-type-in-head))))
3427 ;; Don't search CROSSREF-KEY if we don't need it.
3428 (if (eq bibtex-maintain-sorted-entries 'crossref)
3429 (let ((bounds (bibtex-search-forward-field
3430 "\\(OPT\\)?crossref" t)))
3431 (list key
3432 (if bounds (bibtex-text-in-field-bounds bounds t))
3433 entry-type))
3434 (list key nil entry-type))))))
3436 (defun bibtex-init-sort-entry-class-alist ()
3437 "Initialize `bibtex-sort-entry-class-alist' (buffer-local)."
3438 (unless (local-variable-p 'bibtex-sort-entry-class-alist)
3439 (set (make-local-variable 'bibtex-sort-entry-class-alist)
3440 (let ((i -1) alist)
3441 (dolist (class bibtex-sort-entry-class alist)
3442 (setq i (1+ i))
3443 (dolist (entry class)
3444 ;; All entry types should be downcase (for ease of comparison).
3445 (push (cons (if (stringp entry) (downcase entry) entry) i)
3446 alist)))))))
3448 (defun bibtex-lessp (index1 index2)
3449 "Predicate for sorting BibTeX entries with indices INDEX1 and INDEX2.
3450 Each index is a list (KEY CROSSREF-KEY ENTRY-TYPE).
3451 The predicate depends on the variable `bibtex-maintain-sorted-entries'.
3452 If its value is nil use plain sorting."
3453 (cond ((not index1) (not index2)) ; indices can be nil
3454 ((not index2) nil)
3455 ((eq bibtex-maintain-sorted-entries 'crossref)
3456 ;; CROSSREF-KEY may be nil or it can point to an entry
3457 ;; in another BibTeX file. In both cases we ignore CROSSREF-KEY.
3458 (if (and (nth 1 index1)
3459 (cdr (assoc-string (nth 1 index1) bibtex-reference-keys)))
3460 (if (and (nth 1 index2)
3461 (cdr (assoc-string (nth 1 index2) bibtex-reference-keys)))
3462 (or (string-lessp (nth 1 index1) (nth 1 index2))
3463 (and (string-equal (nth 1 index1) (nth 1 index2))
3464 (string-lessp (nth 0 index1) (nth 0 index2))))
3465 (not (string-lessp (nth 0 index2) (nth 1 index1))))
3466 (if (and (nth 1 index2)
3467 (cdr (assoc-string (nth 1 index2) bibtex-reference-keys)))
3468 (string-lessp (nth 0 index1) (nth 1 index2))
3469 (string-lessp (nth 0 index1) (nth 0 index2)))))
3470 ((eq bibtex-maintain-sorted-entries 'entry-class)
3471 (let ((n1 (cdr (or (assoc (nth 2 index1) bibtex-sort-entry-class-alist)
3472 (assoc 'catch-all bibtex-sort-entry-class-alist)
3473 '(nil . 1000)))) ; if there is nothing else
3474 (n2 (cdr (or (assoc (nth 2 index2) bibtex-sort-entry-class-alist)
3475 (assoc 'catch-all bibtex-sort-entry-class-alist)
3476 '(nil . 1000))))) ; if there is nothing else
3477 (or (< n1 n2)
3478 (and (= n1 n2)
3479 (string-lessp (car index1) (car index2))))))
3480 (t ; (eq bibtex-maintain-sorted-entries 'plain)
3481 (string-lessp (car index1) (car index2)))))
3483 (defun bibtex-sort-buffer ()
3484 "Sort BibTeX buffer alphabetically by key.
3485 The predicate for sorting is defined via `bibtex-maintain-sorted-entries'.
3486 If its value is nil use plain sorting. Text outside of BibTeX entries is not
3487 affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
3488 are ignored."
3489 (interactive)
3490 (bibtex-beginning-of-first-entry) ; Needed by `sort-subr'
3491 (bibtex-init-sort-entry-class-alist) ; Needed by `bibtex-lessp'.
3492 (if (and (eq bibtex-maintain-sorted-entries 'crossref)
3493 (functionp bibtex-reference-keys))
3494 (bibtex-parse-keys)) ; Needed by `bibtex-lessp'.
3495 (sort-subr nil
3496 'bibtex-skip-to-valid-entry ; NEXTREC function
3497 'bibtex-end-of-entry ; ENDREC function
3498 'bibtex-entry-index ; STARTKEY function
3499 nil ; ENDKEY function
3500 'bibtex-lessp)) ; PREDICATE
3502 (defun bibtex-search-crossref (crossref-key &optional pnt split noerror)
3503 "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3504 If `bibtex-files' is non-nil, search all these files.
3505 Otherwise the search is limited to the current buffer.
3506 Return position of entry if CROSSREF-KEY is found or nil otherwise.
3507 If CROSSREF-KEY is in the same buffer like current entry but before it
3508 an error is signaled. If NOERRER is non-nil this error is suppressed.
3509 Optional arg PNT is the position of the referencing entry. It defaults
3510 to position of point. If optional arg SPLIT is non-nil, split window
3511 so that both the referencing and the crossrefed entry are displayed.
3513 If called interactively, CROSSREF-KEY defaults to either the crossref key
3514 of current entry or a key matched by `bibtex-cite-matcher-alist',
3515 whatever is nearer to the position of point. SPLIT is t. NOERROR is nil
3516 for a crossref key, t otherwise."
3517 (interactive
3518 (save-excursion
3519 (let* ((pnt (point))
3520 (_ (bibtex-beginning-of-entry))
3521 (end (cdr (bibtex-valid-entry t)))
3522 (_ (unless end (error "Not inside valid entry")))
3523 (beg (match-end 0)) ; set by `bibtex-valid-entry'
3524 (bounds (bibtex-search-forward-field "crossref" end))
3525 case-fold-search best temp crossref-key)
3526 (if bounds
3527 (setq crossref-key (bibtex-text-in-field-bounds bounds t)
3528 best (cons (bibtex-dist pnt (bibtex-end-of-field bounds)
3529 (bibtex-start-of-field bounds))
3530 crossref-key)))
3531 (dolist (matcher bibtex-cite-matcher-alist)
3532 (goto-char beg)
3533 (while (re-search-forward (car matcher) end t)
3534 (setq temp (bibtex-dist pnt (match-end (cdr matcher))
3535 (match-beginning (cdr matcher))))
3536 ;; Accept the key closest to the position of point.
3537 (if (or (not best) (< temp (car best)))
3538 (setq best (cons temp (match-string-no-properties
3539 (cdr matcher)))))))
3540 (goto-char pnt)
3541 (setq temp (bibtex-read-key "Find crossref key: " (cdr best) t))
3542 (list temp (point) t (not (and crossref-key
3543 (string= temp crossref-key)))))))
3545 (let (buffer pos eqb)
3546 (save-excursion
3547 (setq pos (bibtex-search-entry crossref-key t)
3548 buffer (current-buffer)))
3549 (setq eqb (eq buffer (current-buffer)))
3550 (cond ((not pos)
3551 (if split (message "Crossref key `%s' not found" crossref-key)))
3552 (split ; called (quasi) interactively
3553 (unless pnt (setq pnt (point)))
3554 (goto-char pnt)
3555 (if (and eqb (= pos (save-excursion (bibtex-beginning-of-entry))))
3556 (message "Key `%s' is current entry" crossref-key)
3557 (if eqb (select-window (split-window))
3558 (pop-to-buffer buffer))
3559 (goto-char pos)
3560 (bibtex-reposition-window)
3561 (beginning-of-line)
3562 (if (and eqb (> pnt pos) (not noerror))
3563 (error "The referencing entry must precede the crossrefed entry!"))))
3564 ;; `bibtex-search-crossref' is called noninteractively during
3565 ;; clean-up of an entry. Then it is not possible to check
3566 ;; whether the current entry and the crossrefed entry have
3567 ;; the correct sorting order.
3568 (eqb (goto-char pos))
3569 (t (set-buffer buffer) (goto-char pos)))
3570 pos))
3571 ;; backward compatibility
3572 (defalias 'bibtex-find-crossref 'bibtex-search-crossref)
3574 (defun bibtex-dist (pos beg end)
3575 "Return distance between POS and region delimited by BEG and END."
3576 (cond ((and (<= beg pos) (<= pos end)) 0)
3577 ((< pos beg) (- beg pos))
3578 (t (- pos end))))
3580 ;;;###autoload
3581 (defun bibtex-search-entry (key &optional global start display)
3582 "Move point to the beginning of BibTeX entry named KEY.
3583 Return position of entry if KEY is found or nil if not found.
3584 With GLOBAL non-nil, search KEY in `bibtex-files'. Otherwise the search
3585 is limited to the current buffer. Optional arg START is buffer position
3586 where the search starts. If it is nil, start search at beginning of buffer.
3587 If DISPLAY is non-nil, display the buffer containing KEY.
3588 Otherwise, use `set-buffer'.
3589 When called interactively, GLOBAL is t if there is a prefix arg or the current
3590 mode is not `bibtex-mode', START is nil, and DISPLAY is t."
3591 (interactive
3592 (let ((global (or current-prefix-arg (not (eq major-mode 'bibtex-mode)))))
3593 (list (bibtex-read-key "Find key: " nil global) global nil t)))
3594 (if (and global bibtex-files)
3595 (let ((buffer-list (bibtex-initialize t))
3596 buffer found)
3597 (while (and (not found)
3598 (setq buffer (pop buffer-list)))
3599 (with-current-buffer buffer
3600 (if (cdr (assoc-string key bibtex-reference-keys))
3601 ;; `bibtex-search-entry' moves point if key found
3602 (setq found (bibtex-search-entry key)))))
3603 (cond ((and found display)
3604 (let ((same-window-buffer-names
3605 (cons (buffer-name buffer) same-window-buffer-names)))
3606 (pop-to-buffer buffer)
3607 (bibtex-reposition-window)))
3608 (found (set-buffer buffer))
3609 (display (message "Key `%s' not found" key)))
3610 found)
3612 (let* ((case-fold-search t)
3613 (pnt (save-excursion
3614 (goto-char (or start (point-min)))
3615 (if (re-search-forward (concat "^[ \t]*\\("
3616 bibtex-entry-type
3617 "\\)[ \t]*[({][ \t\n]*\\("
3618 (regexp-quote key)
3619 "\\)[ \t\n]*[,=]")
3620 nil t)
3621 (match-beginning 0)))))
3622 (cond (pnt
3623 (goto-char pnt)
3624 (if display (bibtex-reposition-window)))
3625 (display (message "Key `%s' not found" key)))
3626 pnt)))
3627 ;; backward compatibility
3628 (defalias 'bibtex-find-entry 'bibtex-search-entry)
3630 (defun bibtex-prepare-new-entry (index)
3631 "Prepare a new BibTeX entry with index INDEX.
3632 INDEX is a list (KEY CROSSREF-KEY ENTRY-TYPE).
3633 Move point where the entry KEY should be placed.
3634 If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
3635 search to look for place for KEY. This requires that buffer is sorted,
3636 see `bibtex-validate'.
3637 Return t if preparation was successful or nil if entry KEY already exists."
3638 (bibtex-init-sort-entry-class-alist) ; Needed by `bibtex-lessp'.
3639 (if (and (eq bibtex-maintain-sorted-entries 'crossref)
3640 (functionp bibtex-reference-keys))
3641 (bibtex-parse-keys)) ; Needed by `bibtex-lessp'.
3642 (let ((key (nth 0 index))
3643 key-exist)
3644 (cond ((or (null key)
3645 (and (stringp key)
3646 (string-equal key ""))
3647 (and (not (setq key-exist (bibtex-search-entry key)))
3648 (not bibtex-maintain-sorted-entries)))
3649 (bibtex-move-outside-of-entry))
3650 ;; if key-exist is non-nil due to the previous cond clause
3651 ;; then point will be at beginning of entry named key.
3652 (key-exist)
3653 (t ; `bibtex-maintain-sorted-entries' is non-nil
3654 (let* ((case-fold-search t)
3655 (left (save-excursion (bibtex-beginning-of-first-entry)))
3656 (bounds (save-excursion (goto-char (point-max))
3657 (bibtex-skip-to-valid-entry t)))
3658 (right (if bounds (cdr bounds) (point-min)))
3659 (found (if (>= left right) left))
3660 actual-index new)
3661 (save-excursion
3662 ;; Binary search
3663 (while (not found)
3664 (goto-char (/ (+ left right) 2))
3665 (bibtex-skip-to-valid-entry t)
3666 (setq actual-index (bibtex-entry-index))
3667 (cond ((bibtex-lessp index actual-index)
3668 (setq new (bibtex-beginning-of-entry))
3669 (if (equal right new)
3670 (setq found right)
3671 (setq right new)))
3673 (bibtex-end-of-entry)
3674 (bibtex-skip-to-valid-entry)
3675 (setq new (point))
3676 (if (equal left new)
3677 (setq found right)
3678 (setq left new))))))
3679 (goto-char found)
3680 (bibtex-beginning-of-entry)
3681 (setq actual-index (save-excursion (bibtex-entry-index)))
3682 (when (or (not actual-index)
3683 (bibtex-lessp actual-index index))
3684 ;; buffer contains no valid entries or
3685 ;; greater than last entry --> append
3686 (bibtex-end-of-entry)
3687 (unless (bobp) (newline (forward-line 2)))
3688 (beginning-of-line)))))
3689 (unless key-exist t)))
3691 (defun bibtex-validate (&optional test-thoroughly)
3692 "Validate if buffer or region is syntactically correct.
3693 Check also for duplicate keys and correct sort order provided
3694 `bibtex-maintain-sorted-entries' is non-nil.
3695 With optional argument TEST-THOROUGHLY non-nil check also for
3696 the absence of required fields and for questionable month fields.
3697 If mark is active, validate current region, if not the whole buffer.
3698 Only check known entry types, so you can put comments outside of entries.
3699 Return t if test was successful, nil otherwise."
3700 (interactive "P")
3701 (let* ((case-fold-search t)
3702 error-list syntax-error)
3703 (save-excursion
3704 (save-restriction
3705 (if mark-active (narrow-to-region (region-beginning) (region-end)))
3707 ;; Check syntactical structure of entries
3708 (goto-char (point-min))
3709 (bibtex-progress-message "Checking syntactical structure")
3710 (let (bounds end)
3711 (while (setq end (re-search-forward "^[ \t]*@" nil t))
3712 (bibtex-progress-message)
3713 (goto-char (match-beginning 0))
3714 (cond ((setq bounds (bibtex-valid-entry))
3715 (goto-char (cdr bounds)))
3716 ((setq bounds (or (bibtex-parse-string)
3717 (bibtex-parse-preamble)))
3718 (goto-char (bibtex-end-of-string bounds)))
3719 ((looking-at bibtex-any-valid-entry-type)
3720 (push (cons (bibtex-current-line)
3721 "Syntax error (check esp. commas, braces, and quotes)")
3722 error-list)
3723 (goto-char (match-end 0)))
3724 (t (goto-char end)))))
3725 (bibtex-progress-message 'done)
3727 (if error-list
3728 ;; Continue only if there were no syntax errors.
3729 (setq syntax-error t)
3731 ;; Check for duplicate keys and correct sort order
3732 (bibtex-init-sort-entry-class-alist) ; Needed by `bibtex-lessp'.
3733 (bibtex-parse-keys) ; Possibly needed by `bibtex-lessp'.
3734 ; Always needed by subsequent global key check.
3735 (let (previous current key-list)
3736 (bibtex-progress-message "Checking for duplicate keys")
3737 (bibtex-map-entries
3738 (lambda (key beg end)
3739 (bibtex-progress-message)
3740 (setq current (bibtex-entry-index))
3741 (cond ((not previous))
3742 ((member key key-list)
3743 (push (cons (bibtex-current-line)
3744 (format "Duplicate key `%s'" key))
3745 error-list))
3746 ((and bibtex-maintain-sorted-entries
3747 (not (bibtex-lessp previous current)))
3748 (push (cons (bibtex-current-line)
3749 "Entries out of order")
3750 error-list)))
3751 (push key key-list)
3752 (setq previous current)))
3753 (bibtex-progress-message 'done))
3755 ;; Check for duplicate keys in `bibtex-files'.
3756 ;; `bibtex-validate' only compares keys in current buffer with keys
3757 ;; in `bibtex-files'. `bibtex-validate-globally' compares keys for
3758 ;; each file in `bibtex-files' with keys of all other files in
3759 ;; `bibtex-files'.
3760 ;; We don't want to be fooled by outdated `bibtex-reference-keys'.
3761 (dolist (buffer (bibtex-initialize nil t))
3762 (dolist (key (with-current-buffer buffer bibtex-reference-keys))
3763 (when (and (cdr key)
3764 (cdr (assoc-string (car key) bibtex-reference-keys)))
3765 (bibtex-search-entry (car key))
3766 (push (cons (bibtex-current-line)
3767 (format "Duplicate key `%s' in %s" (car key)
3768 (abbreviate-file-name (buffer-file-name buffer))))
3769 error-list))))
3771 (when test-thoroughly
3772 (bibtex-progress-message
3773 "Checking required fields and month fields")
3774 (let ((bibtex-sort-ignore-string-entries t))
3775 (bibtex-map-entries
3776 (lambda (key beg end)
3777 (bibtex-progress-message)
3778 (let* ((entry-list (assoc-string (bibtex-type-in-head)
3779 bibtex-entry-field-alist t))
3780 (req (copy-sequence (elt (elt entry-list 1) 0)))
3781 (creq (copy-sequence (elt (elt entry-list 2) 0)))
3782 crossref-there bounds alt-there field)
3783 (bibtex-beginning-first-field beg)
3784 (while (setq bounds (bibtex-parse-field))
3785 (let ((field-name (bibtex-name-in-field bounds)))
3786 (if (and (bibtex-string= field-name "month")
3787 ;; Check only abbreviated month fields.
3788 (let ((month (bibtex-text-in-field-bounds bounds)))
3789 (not (or (string-match "\\`[\"{].+[\"}]\\'" month)
3790 (assoc-string
3791 month
3792 bibtex-predefined-month-strings t)))))
3793 (push (cons (bibtex-current-line)
3794 "Questionable month field")
3795 error-list))
3796 (setq field (assoc-string field-name req t)
3797 req (delete field req)
3798 creq (delete (assoc-string field-name creq t) creq))
3799 (if (nth 3 field)
3800 (if alt-there
3801 (push (cons (bibtex-current-line)
3802 "More than one non-empty alternative")
3803 error-list)
3804 (setq alt-there t)))
3805 (if (bibtex-string= field-name "crossref")
3806 (setq crossref-there t)))
3807 (goto-char (bibtex-end-of-field bounds)))
3808 (if crossref-there (setq req creq))
3809 (let (alt)
3810 (dolist (field req)
3811 (if (nth 3 field)
3812 (push (car field) alt)
3813 (push (cons (save-excursion (goto-char beg)
3814 (bibtex-current-line))
3815 (format "Required field `%s' missing"
3816 (car field)))
3817 error-list)))
3818 ;; The following fails if there are more than two
3819 ;; alternatives in a BibTeX entry, which isn't
3820 ;; the case momentarily.
3821 (if (cdr alt)
3822 (push (cons (save-excursion (goto-char beg)
3823 (bibtex-current-line))
3824 (format "Alternative fields `%s'/`%s' missing"
3825 (car alt) (cadr alt)))
3826 error-list)))))))
3827 (bibtex-progress-message 'done)))))
3829 (if error-list
3830 (let ((file (file-name-nondirectory (buffer-file-name)))
3831 (dir default-directory)
3832 (err-buf "*BibTeX validation errors*"))
3833 (setq error-list (sort error-list 'car-less-than-car))
3834 (with-current-buffer (get-buffer-create err-buf)
3835 (setq default-directory dir)
3836 (unless (eq major-mode 'compilation-mode) (compilation-mode))
3837 (toggle-read-only -1)
3838 (delete-region (point-min) (point-max))
3839 (insert "BibTeX mode command `bibtex-validate'\n"
3840 (if syntax-error
3841 "Maybe undetected errors due to syntax errors. Correct and validate again.\n"
3842 "\n"))
3843 (dolist (err error-list)
3844 (insert (format "%s:%d: %s\n" file (car err) (cdr err))))
3845 (set-buffer-modified-p nil)
3846 (toggle-read-only 1)
3847 (goto-line 3)) ; first error message
3848 (display-buffer err-buf)
3849 nil) ; return `nil' (i.e., buffer is invalid)
3850 (message "%s is syntactically correct"
3851 (if mark-active "Region" "Buffer"))
3852 t))) ; return `t' (i.e., buffer is valid)
3854 (defun bibtex-validate-globally (&optional strings)
3855 "Check for duplicate keys in `bibtex-files'.
3856 With optional prefix arg STRINGS, check for duplicate strings, too.
3857 Return t if test was successful, nil otherwise."
3858 (interactive "P")
3859 (let ((buffer-list (bibtex-initialize t))
3860 buffer-key-list current-buf current-keys error-list)
3861 ;; Check for duplicate keys within BibTeX buffer
3862 (dolist (buffer buffer-list)
3863 (save-excursion
3864 (set-buffer buffer)
3865 (let (entry-type key key-list)
3866 (goto-char (point-min))
3867 (while (re-search-forward bibtex-entry-head nil t)
3868 (setq entry-type (bibtex-type-in-head)
3869 key (bibtex-key-in-head))
3870 (if (or (and strings (bibtex-string= entry-type "string"))
3871 (assoc-string entry-type bibtex-entry-field-alist t))
3872 (if (member key key-list)
3873 (push (format "%s:%d: Duplicate key `%s'\n"
3874 (buffer-file-name)
3875 (bibtex-current-line) key)
3876 error-list)
3877 (push key key-list))))
3878 (push (cons buffer key-list) buffer-key-list))))
3880 ;; Check for duplicate keys among BibTeX buffers
3881 (while (setq current-buf (pop buffer-list))
3882 (setq current-keys (cdr (assq current-buf buffer-key-list)))
3883 (with-current-buffer current-buf
3884 (dolist (buffer buffer-list)
3885 (dolist (key (cdr (assq buffer buffer-key-list)))
3886 (when (assoc-string key current-keys)
3887 (bibtex-search-entry key)
3888 (push (format "%s:%d: Duplicate key `%s' in %s\n"
3889 (buffer-file-name) (bibtex-current-line) key
3890 (abbreviate-file-name (buffer-file-name buffer)))
3891 error-list))))))
3893 ;; Process error list
3894 (if error-list
3895 (let ((err-buf "*BibTeX validation errors*"))
3896 (with-current-buffer (get-buffer-create err-buf)
3897 (unless (eq major-mode 'compilation-mode) (compilation-mode))
3898 (toggle-read-only -1)
3899 (delete-region (point-min) (point-max))
3900 (insert "BibTeX mode command `bibtex-validate-globally'\n\n")
3901 (dolist (err (sort error-list 'string-lessp)) (insert err))
3902 (set-buffer-modified-p nil)
3903 (toggle-read-only 1)
3904 (goto-line 3)) ; first error message
3905 (display-buffer err-buf)
3906 nil) ; return `nil' (i.e., buffer is invalid)
3907 (message "No duplicate keys.")
3908 t))) ; return `t' (i.e., buffer is valid)
3910 (defun bibtex-next-field (begin &optional comma)
3911 "Move point to end of text of next BibTeX field or entry head.
3912 With prefix BEGIN non-nil, move point to its beginning. Optional arg COMMA
3913 is as in `bibtex-enclosing-field'. It is t for interactive calls."
3914 (interactive (list current-prefix-arg t))
3915 (let ((bounds (bibtex-find-text-internal t nil comma))
3916 end-of-entry)
3917 (if (not bounds)
3918 (setq end-of-entry t)
3919 (goto-char (nth 3 bounds))
3920 (if (assoc-string (car bounds) '("@String" "@Preamble") t)
3921 (setq end-of-entry t)
3922 ;; BibTeX key or field
3923 (if (looking-at ",[ \t\n]*") (goto-char (match-end 0)))
3924 ;; end of entry
3925 (if (looking-at "[)}][ \t\n]*") (setq end-of-entry t))))
3926 (if (and end-of-entry
3927 (re-search-forward bibtex-any-entry-maybe-empty-head nil t))
3928 (goto-char (match-beginning 0)))
3929 (bibtex-find-text begin nil bibtex-help-message)))
3931 (defun bibtex-find-text (&optional begin noerror help comma)
3932 "Move point to end of text of current BibTeX field or entry head.
3933 With optional prefix BEGIN non-nil, move point to its beginning.
3934 Unless NOERROR is non-nil, an error is signaled if point is not
3935 on a BibTeX field. If optional arg HELP is non-nil print help message.
3936 When called interactively, the value of HELP is `bibtex-help-message'.
3937 Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
3938 interactive calls."
3939 (interactive (list current-prefix-arg nil bibtex-help-message t))
3940 (let ((bounds (bibtex-find-text-internal t nil comma)))
3941 (cond (bounds
3942 (if begin
3943 (progn (goto-char (nth 1 bounds))
3944 (if (looking-at "[{\"]")
3945 (forward-char)))
3946 (goto-char (nth 2 bounds))
3947 (if (memq (preceding-char) '(?} ?\"))
3948 (forward-char -1)))
3949 (if help (bibtex-print-help-message (car bounds))))
3950 ((not noerror) (error "Not on BibTeX field")))))
3952 (defun bibtex-find-text-internal (&optional noerror subfield comma)
3953 "Find text part of current BibTeX field or entry head.
3954 Return list (NAME START-TEXT END-TEXT END STRING-CONST) with field name
3955 or entry type, start and end of text, and end of field or entry head.
3956 STRING-CONST is a flag which is non-nil if current subfield delimited by #
3957 is a BibTeX string constant. Return value is nil if field or entry head
3958 are not found.
3959 If optional arg NOERROR is non-nil, an error message is suppressed
3960 if text is not found. If optional arg SUBFIELD is non-nil START-TEXT
3961 and END-TEXT correspond to the current subfield delimited by #.
3962 Optional arg COMMA is as in `bibtex-enclosing-field'."
3963 (save-excursion
3964 (let ((pnt (point))
3965 (bounds (bibtex-enclosing-field comma t))
3966 (case-fold-search t)
3967 name start-text end-text end failure done no-sub string-const)
3968 (bibtex-beginning-of-entry)
3969 (cond (bounds
3970 (setq name (bibtex-name-in-field bounds t)
3971 start-text (bibtex-start-of-text-in-field bounds)
3972 end-text (bibtex-end-of-text-in-field bounds)
3973 end (bibtex-end-of-field bounds)))
3974 ;; @String
3975 ((setq bounds (bibtex-parse-string t))
3976 (if (<= pnt (bibtex-end-of-string bounds))
3977 (setq name "@String" ;; not a field name!
3978 start-text (bibtex-start-of-text-in-string bounds)
3979 end-text (bibtex-end-of-text-in-string bounds)
3980 end (bibtex-end-of-string bounds))
3981 (setq failure t)))
3982 ;; @Preamble
3983 ((setq bounds (bibtex-parse-preamble))
3984 (if (<= pnt (bibtex-end-of-string bounds))
3985 (setq name "@Preamble" ;; not a field name!
3986 start-text (bibtex-start-of-text-in-string bounds)
3987 end-text (bibtex-end-of-text-in-string bounds)
3988 end (bibtex-end-of-string bounds))
3989 (setq failure t)))
3990 ;; BibTeX head
3991 ((looking-at bibtex-entry-maybe-empty-head)
3992 (goto-char (match-end 0))
3993 (if comma (save-match-data
3994 (re-search-forward "\\=[ \t\n]*," nil t)))
3995 (if (<= pnt (point))
3996 (setq name (match-string-no-properties bibtex-type-in-head)
3997 start-text (or (match-beginning bibtex-key-in-head)
3998 (match-end 0))
3999 end-text (or (match-end bibtex-key-in-head)
4000 (match-end 0))
4001 end end-text
4002 no-sub t) ; subfields do not make sense
4003 (setq failure t)))
4004 (t (setq failure t)))
4005 (when (and subfield (not failure))
4006 (setq failure no-sub)
4007 (unless failure
4008 (goto-char start-text)
4009 (while (not done)
4010 (if (or (prog1 (looking-at bibtex-field-const)
4011 (setq end-text (match-end 0)
4012 string-const t))
4013 (prog1 (setq bounds (bibtex-parse-field-string))
4014 (setq end-text (cdr bounds)
4015 string-const nil)))
4016 (progn
4017 (if (and (<= start-text pnt) (<= pnt end-text))
4018 (setq done t)
4019 (goto-char end-text))
4020 (if (looking-at "[ \t\n]*#[ \t\n]*")
4021 (setq start-text (goto-char (match-end 0)))))
4022 (setq done t failure t)))))
4023 (cond ((not failure)
4024 (list name start-text end-text end string-const))
4025 ((and no-sub (not noerror))
4026 (error "Not on text part of BibTeX field"))
4027 ((not noerror) (error "Not on BibTeX field"))))))
4029 (defun bibtex-remove-OPT-or-ALT (&optional comma)
4030 "Remove the string starting optional/alternative fields.
4031 Align text and go thereafter to end of text. Optional arg COMMA
4032 is as in `bibtex-enclosing-field'. It is t for interactive calls."
4033 (interactive (list t))
4034 (let ((case-fold-search t)
4035 (bounds (bibtex-enclosing-field comma)))
4036 (save-excursion
4037 (goto-char (bibtex-start-of-name-in-field bounds))
4038 (when (looking-at "OPT\\|ALT")
4039 (delete-region (match-beginning 0) (match-end 0))
4040 ;; make field non-OPT
4041 (search-forward "=")
4042 (forward-char -1)
4043 (delete-horizontal-space)
4044 (if bibtex-align-at-equal-sign
4045 (indent-to-column (- bibtex-text-indentation 2))
4046 (insert " "))
4047 (search-forward "=")
4048 (delete-horizontal-space)
4049 (if bibtex-align-at-equal-sign
4050 (insert " ")
4051 (indent-to-column bibtex-text-indentation))))))
4053 (defun bibtex-remove-delimiters (&optional comma)
4054 "Remove \"\" or {} around current BibTeX field text.
4055 Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
4056 interactive calls."
4057 (interactive (list t))
4058 (let ((bounds (bibtex-find-text-internal nil t comma)))
4059 (unless (nth 4 bounds)
4060 (delete-region (1- (nth 2 bounds)) (nth 2 bounds))
4061 (delete-region (nth 1 bounds) (1+ (nth 1 bounds))))))
4063 (defun bibtex-kill-field (&optional copy-only comma)
4064 "Kill the entire enclosing BibTeX field.
4065 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
4066 but do not actually kill it. Optional arg COMMA is as in
4067 `bibtex-enclosing-field'. It is t for interactive calls."
4068 (interactive (list current-prefix-arg t))
4069 (save-excursion
4070 (let* ((case-fold-search t)
4071 (bounds (bibtex-enclosing-field comma))
4072 (end (bibtex-end-of-field bounds))
4073 (beg (bibtex-start-of-field bounds)))
4074 (goto-char end)
4075 (skip-chars-forward ",")
4076 (push (list (bibtex-name-in-field bounds) nil
4077 (bibtex-text-in-field-bounds bounds))
4078 bibtex-field-kill-ring)
4079 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
4080 (setcdr (nthcdr (1- bibtex-field-kill-ring-max)
4081 bibtex-field-kill-ring)
4082 nil))
4083 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring)
4084 (unless copy-only
4085 (delete-region beg end))))
4086 (setq bibtex-last-kill-command 'field))
4088 (defun bibtex-copy-field-as-kill (&optional comma)
4089 "Copy the BibTeX field at point to `bibtex-field-kill-ring'.
4090 Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
4091 interactive calls."
4092 (interactive (list t))
4093 (bibtex-kill-field t comma))
4095 (defun bibtex-kill-entry (&optional copy-only)
4096 "Kill the entire enclosing BibTeX entry.
4097 With prefix arg COPY-ONLY, copy the current entry to `bibtex-entry-kill-ring',
4098 but do not actually kill it."
4099 (interactive "P")
4100 (save-excursion
4101 (let* ((case-fold-search t)
4102 (beg (bibtex-beginning-of-entry))
4103 (end (progn (bibtex-end-of-entry)
4104 (if (re-search-forward
4105 bibtex-any-entry-maybe-empty-head nil 'move)
4106 (goto-char (match-beginning 0)))
4107 (point))))
4108 (push (buffer-substring-no-properties beg end)
4109 bibtex-entry-kill-ring)
4110 (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
4111 (setcdr (nthcdr (1- bibtex-entry-kill-ring-max)
4112 bibtex-entry-kill-ring)
4113 nil))
4114 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring)
4115 (unless copy-only
4116 (delete-region beg end))))
4117 (setq bibtex-last-kill-command 'entry))
4119 (defun bibtex-copy-entry-as-kill ()
4120 "Copy the entire enclosing BibTeX entry to `bibtex-entry-kill-ring'."
4121 (interactive)
4122 (bibtex-kill-entry t))
4124 (defun bibtex-yank (&optional n)
4125 "Reinsert the last BibTeX item.
4126 More precisely, reinsert the field or entry killed or yanked most recently.
4127 With argument N, reinsert the Nth most recently killed BibTeX item.
4128 See also the command \\[bibtex-yank-pop]."
4129 (interactive "*p")
4130 (bibtex-insert-kill (1- n) t)
4131 (setq this-command 'bibtex-yank))
4133 (defun bibtex-yank-pop (n)
4134 "Replace just-yanked killed BibTeX item with a different item.
4135 This command is allowed only immediately after a `bibtex-yank' or a
4136 `bibtex-yank-pop'. In this case, the region contains a reinserted
4137 previously killed BibTeX item. `bibtex-yank-pop' deletes that item
4138 and inserts in its place a different killed BibTeX item.
4140 With no argument, the previous kill is inserted.
4141 With argument N, insert the Nth previous kill.
4142 If N is negative, this is a more recent kill.
4144 The sequence of kills wraps around, so that after the oldest one
4145 comes the newest one."
4146 (interactive "*p")
4147 (unless (eq last-command 'bibtex-yank)
4148 (error "Previous command was not a BibTeX yank"))
4149 (setq this-command 'bibtex-yank)
4150 (let ((inhibit-read-only t))
4151 (delete-region (point) (mark t))
4152 (bibtex-insert-kill n t)))
4154 (defun bibtex-empty-field (&optional comma)
4155 "Delete the text part of the current field, replace with empty text.
4156 Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for
4157 interactive calls."
4158 (interactive (list t))
4159 (let ((bounds (bibtex-enclosing-field comma)))
4160 (goto-char (bibtex-start-of-text-in-field bounds))
4161 (delete-region (point) (bibtex-end-of-text-in-field bounds))
4162 (insert (bibtex-field-left-delimiter)
4163 (bibtex-field-right-delimiter))
4164 (bibtex-find-text t nil bibtex-help-message)))
4166 (defun bibtex-pop-previous (arg)
4167 "Replace text of current field with the similar field in previous entry.
4168 With arg, goes up ARG entries. Repeated, goes up so many times. May be
4169 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
4170 (interactive "p")
4171 (bibtex-pop arg 'previous))
4173 (defun bibtex-pop-next (arg)
4174 "Replace text of current field with the text of similar field in next entry.
4175 With arg, goes down ARG entries. Repeated, goes down so many times. May be
4176 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
4177 (interactive "p")
4178 (bibtex-pop arg 'next))
4180 (defun bibtex-clean-entry (&optional new-key called-by-reformat)
4181 "Finish editing the current BibTeX entry and clean it up.
4182 Check that no required fields are empty and format entry dependent
4183 on the value of `bibtex-entry-format'.
4184 If the reference key of the entry is empty or a prefix argument is given,
4185 calculate a new reference key. (Note: this works only if fields in entry
4186 begin on separate lines prior to calling `bibtex-clean-entry' or if
4187 'realign is contained in `bibtex-entry-format'.)
4188 Don't call `bibtex-clean-entry' on @Preamble entries.
4189 At end of the cleaning process, the functions in
4190 `bibtex-clean-entry-hook' are called with region narrowed to entry."
4191 ;; Opt. arg CALLED-BY-REFORMAT is t if `bibtex-clean-entry'
4192 ;; is called by `bibtex-reformat'
4193 (interactive "P")
4194 (let ((case-fold-search t)
4195 (start (bibtex-beginning-of-entry))
4196 (_ (or (looking-at bibtex-any-entry-maybe-empty-head)
4197 (error "Not inside a BibTeX entry")))
4198 (entry-type (bibtex-type-in-head))
4199 (key (bibtex-key-in-head)))
4200 (cond ((bibtex-string= entry-type "preamble")
4201 ;; (bibtex-format-preamble)
4202 (error "No clean up of @Preamble entries"))
4203 ((bibtex-string= entry-type "string")
4204 (setq entry-type 'string))
4205 ;; (bibtex-format-string)
4206 (t (bibtex-format-entry)))
4207 ;; set key
4208 (when (or new-key (not key))
4209 (setq key (bibtex-generate-autokey))
4210 ;; Sometimes `bibtex-generate-autokey' returns an empty string
4211 (if (or bibtex-autokey-edit-before-use (string= "" key))
4212 (setq key (if (eq entry-type 'string)
4213 (bibtex-read-string-key key)
4214 (bibtex-read-key "Key to use: " key))))
4215 (save-excursion
4216 (re-search-forward (if (eq entry-type 'string)
4217 bibtex-string-maybe-empty-head
4218 bibtex-entry-maybe-empty-head))
4219 (if (match-beginning bibtex-key-in-head)
4220 (delete-region (match-beginning bibtex-key-in-head)
4221 (match-end bibtex-key-in-head)))
4222 (insert key)))
4224 (unless called-by-reformat
4225 (let* ((end (save-excursion
4226 (bibtex-end-of-entry)
4227 (if (re-search-forward
4228 bibtex-entry-maybe-empty-head nil 'move)
4229 (goto-char (match-beginning 0)))
4230 (point)))
4231 (entry (buffer-substring start end))
4232 ;; include the crossref key in index
4233 (index (let ((bibtex-maintain-sorted-entries 'crossref))
4234 (bibtex-entry-index))) ; moves point to end of head
4235 error)
4236 ;; sorting
4237 (if (and bibtex-maintain-sorted-entries
4238 (not (and bibtex-sort-ignore-string-entries
4239 (eq entry-type 'string))))
4240 (progn
4241 (delete-region start end)
4242 (setq error (not (bibtex-prepare-new-entry index))
4243 start (point)) ; update start
4244 (save-excursion (insert entry)))
4245 (bibtex-search-entry key)
4246 (setq error (or (/= (point) start)
4247 (bibtex-search-entry key nil end))))
4248 (if error
4249 (error "New inserted entry yields duplicate key"))
4250 (dolist (buffer (bibtex-initialize))
4251 (with-current-buffer buffer
4252 (if (cdr (assoc-string key bibtex-reference-keys))
4253 (error "Duplicate key in %s" (buffer-file-name)))))
4255 ;; Only update `bibtex-strings' and `bibtex-reference-keys'
4256 ;; if they have been built already.
4257 (cond ((eq entry-type 'string)
4258 ;; We have a @String entry.
4259 (unless (or (functionp bibtex-strings)
4260 (assoc key bibtex-strings))
4261 (push (cons key (bibtex-text-in-string
4262 (bibtex-parse-string) t))
4263 bibtex-strings)))
4264 ;; We have a normal entry.
4265 ((not (functionp bibtex-reference-keys))
4266 (let ((found (assoc key bibtex-reference-keys)))
4267 (cond ((not found)
4268 (push (cons key t) bibtex-reference-keys))
4269 ((not (cdr found))
4270 ;; Turn a crossref key into a header key
4271 (setq bibtex-reference-keys
4272 (cons (cons key t)
4273 (delete (list key) bibtex-reference-keys))))))
4274 ;; If entry has a crossref key, it goes into the list
4275 ;; `bibtex-reference-keys', too.
4276 (if (and (nth 1 index)
4277 (not (assoc (nth 1 index) bibtex-reference-keys)))
4278 (push (list (nth 1 index)) bibtex-reference-keys)))))
4280 ;; final clean up
4281 (if bibtex-clean-entry-hook
4282 (save-excursion
4283 (save-restriction
4284 (bibtex-narrow-to-entry)
4285 (run-hooks 'bibtex-clean-entry-hook)))))))
4287 (defun bibtex-fill-field-bounds (bounds justify &optional move)
4288 "Fill BibTeX field delimited by BOUNDS.
4289 If JUSTIFY is non-nil justify as well.
4290 If optional arg MOVE is non-nil move point to end of field."
4291 (let ((end-field (copy-marker (bibtex-end-of-field bounds))))
4292 (if (not justify)
4293 (goto-char (bibtex-start-of-text-in-field bounds))
4294 (goto-char (bibtex-start-of-field bounds))
4295 (forward-char) ; leading comma
4296 (bibtex-delete-whitespace)
4297 (open-line 1)
4298 (forward-char)
4299 (indent-to-column (+ bibtex-entry-offset
4300 bibtex-field-indentation))
4301 (re-search-forward "[ \t\n]*=" end-field)
4302 (replace-match "=")
4303 (forward-char -1)
4304 (if bibtex-align-at-equal-sign
4305 (indent-to-column
4306 (+ bibtex-entry-offset (- bibtex-text-indentation 2)))
4307 (insert " "))
4308 (forward-char)
4309 (bibtex-delete-whitespace)
4310 (if bibtex-align-at-equal-sign
4311 (insert " ")
4312 (indent-to-column bibtex-text-indentation)))
4313 ;; Paragraphs within fields are not preserved. Bother?
4314 (fill-region-as-paragraph (line-beginning-position) end-field
4315 default-justification nil (point))
4316 (if move (goto-char end-field))))
4318 (defun bibtex-fill-field (&optional justify)
4319 "Like \\[fill-paragraph], but fill current BibTeX field.
4320 If optional prefix JUSTIFY is non-nil justify as well.
4321 In BibTeX mode this function is bound to `fill-paragraph-function'."
4322 (interactive "*P")
4323 (let ((pnt (copy-marker (point)))
4324 (bounds (bibtex-enclosing-field t)))
4325 (bibtex-fill-field-bounds bounds justify)
4326 (goto-char pnt)))
4328 (defun bibtex-fill-entry ()
4329 "Fill current BibTeX entry.
4330 Realign entry, so that every field starts on a separate line. Field
4331 names appear in column `bibtex-field-indentation', field text starts in
4332 column `bibtex-text-indentation' and continuation lines start here, too.
4333 If `bibtex-align-at-equal-sign' is non-nil, align equal signs, too."
4334 (interactive "*")
4335 (let ((pnt (copy-marker (point)))
4336 (end (copy-marker (bibtex-end-of-entry)))
4337 (beg (bibtex-beginning-of-entry)) ; move point
4338 bounds)
4339 (bibtex-delete-whitespace)
4340 (indent-to-column bibtex-entry-offset)
4341 (bibtex-beginning-first-field beg)
4342 (while (setq bounds (bibtex-parse-field))
4343 (bibtex-fill-field-bounds bounds t t))
4344 (if (looking-at ",")
4345 (forward-char))
4346 (skip-chars-backward " \t\n")
4347 (bibtex-delete-whitespace)
4348 (open-line 1)
4349 (forward-char)
4350 (indent-to-column bibtex-entry-offset)
4351 (goto-char pnt)))
4353 (defun bibtex-realign ()
4354 "Realign BibTeX entries such that they are separated by one blank line."
4355 (goto-char (point-min))
4356 (let ((case-fold-search t)
4357 (entry-type (concat "[ \t\n]*\\(" bibtex-entry-type "\\)")))
4358 ;; No blank lines prior to the first entry if there no
4359 ;; non-white characters in front of it.
4360 (when (looking-at entry-type)
4361 (replace-match "\\1"))
4362 ;; Entries are separated by one blank line.
4363 (while (re-search-forward entry-type nil t)
4364 (replace-match "\n\n\\1"))
4365 ;; One blank line past the last entry if it is followed by
4366 ;; non-white characters, no blank line otherwise.
4367 (beginning-of-line)
4368 (when (re-search-forward bibtex-entry-type nil t)
4369 (bibtex-end-of-entry)
4370 (bibtex-delete-whitespace)
4371 (open-line (if (eobp) 1 2)))))
4373 (defun bibtex-reformat (&optional read-options)
4374 "Reformat all BibTeX entries in buffer or region.
4375 Without prefix argument, reformatting is based on `bibtex-entry-format'.
4376 With prefix argument, read options for reformatting from minibuffer.
4377 With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again.
4378 If mark is active reformat entries in region, if not in whole buffer."
4379 (interactive "*P")
4380 (let* ((pnt (point))
4381 (use-previous-options
4382 (and (equal (prefix-numeric-value read-options) 16)
4383 (or bibtex-reformat-previous-options
4384 bibtex-reformat-previous-reference-keys)))
4385 (bibtex-entry-format
4386 (cond (read-options
4387 (if use-previous-options
4388 bibtex-reformat-previous-options
4389 (setq bibtex-reformat-previous-options
4390 (mapcar (lambda (option)
4391 (if (y-or-n-p (car option)) (cdr option)))
4392 `(("Realign entries (recommended)? " . 'realign)
4393 ("Remove empty optional and alternative fields? " . 'opts-or-alts)
4394 ("Remove delimiters around pure numerical fields? " . 'numerical-fields)
4395 (,(concat (if bibtex-comma-after-last-field "Insert" "Remove")
4396 " comma at end of entry? ") . 'last-comma)
4397 ("Replace double page dashes by single ones? " . 'page-dashes)
4398 ("Delete whitespace at the beginning and end of fields? " . 'whitespace)
4399 ("Inherit booktitle? " . 'inherit-booktitle)
4400 ("Force delimiters? " . 'delimiters)
4401 ("Unify case of entry types and field names? " . 'unify-case)
4402 ("Enclose parts of field entries by braces? " . 'braces)
4403 ("Replace parts of field entries by string constants? " . 'strings))))))
4404 ;; Do not include required-fields because `bibtex-reformat'
4405 ;; cannot handle the error messages of `bibtex-format-entry'.
4406 ;; Use `bibtex-validate' to check for required fields.
4407 ((eq t bibtex-entry-format)
4408 '(realign opts-or-alts numerical-fields delimiters
4409 last-comma page-dashes unify-case inherit-booktitle
4410 whitespace braces strings))
4412 (remove 'required-fields (push 'realign bibtex-entry-format)))))
4413 (reformat-reference-keys
4414 (if read-options
4415 (if use-previous-options
4416 bibtex-reformat-previous-reference-keys
4417 (setq bibtex-reformat-previous-reference-keys
4418 (y-or-n-p "Generate new reference keys automatically? ")))))
4419 (bibtex-sort-ignore-string-entries t)
4420 bibtex-autokey-edit-before-use)
4422 (save-restriction
4423 (if mark-active (narrow-to-region (region-beginning) (region-end)))
4424 (if (memq 'realign bibtex-entry-format)
4425 (bibtex-realign))
4426 (bibtex-progress-message "Formatting" 1)
4427 (bibtex-map-entries (lambda (key beg end)
4428 (bibtex-progress-message)
4429 (bibtex-clean-entry reformat-reference-keys t)))
4430 (bibtex-progress-message 'done))
4431 (when reformat-reference-keys
4432 (kill-local-variable 'bibtex-reference-keys)
4433 (when bibtex-maintain-sorted-entries
4434 (bibtex-progress-message "Sorting" 1)
4435 (bibtex-sort-buffer)
4436 (bibtex-progress-message 'done)))
4437 (goto-char pnt)))
4439 (defun bibtex-convert-alien (&optional read-options)
4440 "Make an alien BibTeX buffer fully usable by BibTeX mode.
4441 If a file does not conform with all standards used by BibTeX mode,
4442 some of the high-level features of BibTeX mode are not available.
4443 This function tries to convert current buffer to conform with these standards.
4444 With prefix argument READ-OPTIONS non-nil, read options for reformatting
4445 entries from minibuffer."
4446 (interactive "*P")
4447 (message "Starting to validate buffer...")
4448 (sit-for 1)
4449 (bibtex-realign)
4450 (deactivate-mark) ; So `bibtex-validate' works on the whole buffer.
4451 (if (not (let (bibtex-maintain-sorted-entries)
4452 (bibtex-validate)))
4453 (message "Correct errors and call `bibtex-convert-alien' again")
4454 (message "Starting to reformat entries...")
4455 (sit-for 2)
4456 (bibtex-reformat read-options)
4457 (goto-char (point-max))
4458 (message "Buffer is now parsable. Please save it.")))
4460 (defun bibtex-complete ()
4461 "Complete word fragment before point according to context.
4462 If point is inside key or crossref field perform key completion based on
4463 `bibtex-reference-keys'. Inside a month field perform key completion
4464 based on `bibtex-predefined-month-strings'. Inside any other field
4465 \(including a String or Preamble definition) perform string completion
4466 based on `bibtex-strings'.
4467 An error is signaled if point is outside key or BibTeX field."
4468 (interactive)
4469 (let ((pnt (point))
4470 (case-fold-search t)
4471 bounds name compl)
4472 (save-excursion
4473 (if (and (setq bounds (bibtex-enclosing-field nil t))
4474 (>= pnt (bibtex-start-of-text-in-field bounds))
4475 (<= pnt (bibtex-end-of-text-in-field bounds)))
4476 (setq name (bibtex-name-in-field bounds t)
4477 compl (cond ((bibtex-string= name "crossref")
4478 ;; point is in crossref field
4479 'crossref-key)
4480 ((bibtex-string= name "month")
4481 ;; point is in month field
4482 bibtex-predefined-month-strings)
4483 ;; point is in other field
4484 (t (bibtex-strings))))
4485 (bibtex-beginning-of-entry)
4486 (cond ((setq bounds (bibtex-parse-string t))
4487 ;; point is inside a @String key
4488 (cond ((and (>= pnt (nth 1 (car bounds)))
4489 (<= pnt (nth 2 (car bounds))))
4490 (setq compl 'string))
4491 ;; point is inside a @String field
4492 ((and (>= pnt (bibtex-start-of-text-in-string bounds))
4493 (<= pnt (bibtex-end-of-text-in-string bounds)))
4494 (setq compl (bibtex-strings)))))
4495 ;; point is inside a @Preamble field
4496 ((setq bounds (bibtex-parse-preamble))
4497 (if (and (>= pnt (bibtex-start-of-text-in-string bounds))
4498 (<= pnt (bibtex-end-of-text-in-string bounds)))
4499 (setq compl (bibtex-strings))))
4500 ((and (looking-at bibtex-entry-maybe-empty-head)
4501 ;; point is inside a key
4502 (or (and (match-beginning bibtex-key-in-head)
4503 (>= pnt (match-beginning bibtex-key-in-head))
4504 (<= pnt (match-end bibtex-key-in-head)))
4505 ;; or point is on empty key
4506 (and (not (match-beginning bibtex-key-in-head))
4507 (= pnt (match-end 0)))))
4508 (setq compl 'key)))))
4510 (cond ((eq compl 'key)
4511 ;; key completion: no cleanup needed
4512 (setq choose-completion-string-functions nil)
4513 (let (completion-ignore-case)
4514 (bibtex-complete-internal (bibtex-global-key-alist))))
4516 ((eq compl 'crossref-key)
4517 ;; crossref key completion
4519 ;; If we quit the *Completions* buffer without requesting
4520 ;; a completion, `choose-completion-string-functions' is still
4521 ;; non-nil. Therefore, `choose-completion-string-functions' is
4522 ;; always set (either to non-nil or nil) when a new completion
4523 ;; is requested.
4524 (let (completion-ignore-case)
4525 (setq choose-completion-string-functions
4526 (lambda (choice buffer mini-p base-size)
4527 (setq choose-completion-string-functions nil)
4528 (choose-completion-string choice buffer base-size)
4529 (bibtex-complete-crossref-cleanup choice)
4530 t)) ; needed by choose-completion-string-functions
4531 (bibtex-complete-crossref-cleanup
4532 (bibtex-complete-internal (bibtex-global-key-alist)))))
4534 ((eq compl 'string)
4535 ;; string key completion: no cleanup needed
4536 (setq choose-completion-string-functions nil)
4537 (let ((completion-ignore-case t))
4538 (bibtex-complete-internal bibtex-strings)))
4540 (compl
4541 ;; string completion
4542 (let ((completion-ignore-case t))
4543 (setq choose-completion-string-functions
4544 `(lambda (choice buffer mini-p base-size)
4545 (setq choose-completion-string-functions nil)
4546 (choose-completion-string choice buffer base-size)
4547 (bibtex-complete-string-cleanup choice ',compl)
4548 t)) ; needed by `choose-completion-string-functions'
4549 (bibtex-complete-string-cleanup (bibtex-complete-internal compl)
4550 compl)))
4552 (t (setq choose-completion-string-functions nil)
4553 (error "Point outside key or BibTeX field")))))
4555 (defun bibtex-Article ()
4556 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
4557 (interactive "*")
4558 (bibtex-entry "Article"))
4560 (defun bibtex-Book ()
4561 "Insert a new BibTeX @Book entry; see also `bibtex-entry'."
4562 (interactive "*")
4563 (bibtex-entry "Book"))
4565 (defun bibtex-Booklet ()
4566 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'."
4567 (interactive "*")
4568 (bibtex-entry "Booklet"))
4570 (defun bibtex-InBook ()
4571 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'."
4572 (interactive "*")
4573 (bibtex-entry "InBook"))
4575 (defun bibtex-InCollection ()
4576 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'."
4577 (interactive "*")
4578 (bibtex-entry "InCollection"))
4580 (defun bibtex-InProceedings ()
4581 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'."
4582 (interactive "*")
4583 (bibtex-entry "InProceedings"))
4585 (defun bibtex-Manual ()
4586 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'."
4587 (interactive "*")
4588 (bibtex-entry "Manual"))
4590 (defun bibtex-MastersThesis ()
4591 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'."
4592 (interactive "*")
4593 (bibtex-entry "MastersThesis"))
4595 (defun bibtex-Misc ()
4596 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'."
4597 (interactive "*")
4598 (bibtex-entry "Misc"))
4600 (defun bibtex-PhdThesis ()
4601 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'."
4602 (interactive "*")
4603 (bibtex-entry "PhdThesis"))
4605 (defun bibtex-Proceedings ()
4606 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'."
4607 (interactive "*")
4608 (bibtex-entry "Proceedings"))
4610 (defun bibtex-TechReport ()
4611 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'."
4612 (interactive "*")
4613 (bibtex-entry "TechReport"))
4615 (defun bibtex-Unpublished ()
4616 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'."
4617 (interactive "*")
4618 (bibtex-entry "Unpublished"))
4620 (defun bibtex-String (&optional key)
4621 "Insert a new BibTeX @String entry with key KEY."
4622 (interactive (list (bibtex-read-string-key)))
4623 (let ((bibtex-maintain-sorted-entries
4624 (unless bibtex-sort-ignore-string-entries
4625 bibtex-maintain-sorted-entries))
4626 endpos)
4627 (unless (bibtex-prepare-new-entry (list key nil "String"))
4628 (error "Entry with key `%s' already exists" key))
4629 (if (zerop (length key)) (setq key nil))
4630 (indent-to-column bibtex-entry-offset)
4631 (insert "@String"
4632 (bibtex-entry-left-delimiter))
4633 (if key
4634 (insert key)
4635 (setq endpos (point)))
4636 (insert " = "
4637 (bibtex-field-left-delimiter))
4638 (if key
4639 (setq endpos (point)))
4640 (insert (bibtex-field-right-delimiter)
4641 (bibtex-entry-right-delimiter)
4642 "\n")
4643 (goto-char endpos)))
4645 (defun bibtex-Preamble ()
4646 "Insert a new BibTeX @Preamble entry."
4647 (interactive "*")
4648 (bibtex-move-outside-of-entry)
4649 (indent-to-column bibtex-entry-offset)
4650 (insert "@Preamble"
4651 (bibtex-entry-left-delimiter)
4652 (bibtex-field-left-delimiter))
4653 (let ((endpos (point)))
4654 (insert (bibtex-field-right-delimiter)
4655 (bibtex-entry-right-delimiter)
4656 "\n")
4657 (goto-char endpos)))
4659 (defun bibtex-url (&optional pos no-browse)
4660 "Browse a URL for the BibTeX entry at point.
4661 Optional POS is the location of the BibTeX entry.
4662 The URL is generated using the schemes defined in `bibtex-generate-url-list'
4663 \(see there\). If multiple schemes match for this entry, or the same scheme
4664 matches more than once, use the one for which the first step's match is the
4665 closest to POS. The URL is passed to `browse-url' unless NO-BROWSE is t.
4666 Return the URL or nil if none can be generated."
4667 (interactive)
4668 (unless pos (setq pos (point)))
4669 (save-excursion
4670 (goto-char pos)
4671 (bibtex-beginning-of-entry)
4672 (let ((end (save-excursion (bibtex-end-of-entry)))
4673 (fields-alist (save-excursion (bibtex-parse-entry t)))
4674 ;; Always ignore case,
4675 (case-fold-search t)
4676 text url scheme obj fmt fl-match step)
4677 ;; The return value of `bibtex-parse-entry' (i.e., FIELDS-ALIST)
4678 ;; is always used to generate the URL. However, if the BibTeX
4679 ;; entry contains more than one URL, we have multiple matches
4680 ;; for the first step defining the generation of the URL.
4681 ;; Therefore, we try to initiate the generation of the URL
4682 ;; based on the match of `bibtex-font-lock-url' that is the
4683 ;; closest to POS. If that fails (no match found) we try to
4684 ;; initiate the generation of the URL based on the properly
4685 ;; concatenated CONTENT of the field as returned by
4686 ;; `bibtex-text-in-field-bounds'. The latter approach can
4687 ;; differ from the former because `bibtex-font-lock-url' uses
4688 ;; the buffer itself.
4689 (while (bibtex-font-lock-url end t)
4690 (push (list (bibtex-dist pos (match-beginning 0) (match-end 0))
4691 (match-beginning 0)
4692 (buffer-substring-no-properties
4693 (match-beginning 0) (match-end 0)))
4694 fl-match)
4695 ;; `bibtex-font-lock-url' moves point to end of match.
4696 (forward-char))
4697 (when fl-match
4698 (setq fl-match (car (sort fl-match (lambda (x y) (< (car x) (car y))))))
4699 (goto-char (nth 1 fl-match))
4700 (bibtex-beginning-of-field) (re-search-backward ",")
4701 (let* ((bounds (bibtex-parse-field))
4702 (name (bibtex-name-in-field bounds))
4703 (content (bibtex-text-in-field-bounds bounds t))
4704 (lst bibtex-generate-url-list))
4705 ;; This match can fail when CONTENT differs from text in buffer.
4706 (when (string-match (regexp-quote (nth 2 fl-match)) content)
4707 ;; TEXT is the part of CONTENT that starts with the match
4708 ;; of `bibtex-font-lock-url' we are looking for.
4709 (setq text (substring content (match-beginning 0)))
4710 (while (and (not url) (setq scheme (pop lst)))
4711 ;; Verify the match of `bibtex-font-lock-url' by
4712 ;; comparing with TEXT.
4713 (when (and (bibtex-string= (caar scheme) name)
4714 (string-match (cdar scheme) text))
4715 (setq url t scheme (cdr scheme)))))))
4717 ;; If the match of `bibtex-font-lock-url' was not approved
4718 ;; parse FIELDS-ALIST, i.e., the output of `bibtex-parse-entry'.
4719 (unless url
4720 (let ((lst bibtex-generate-url-list))
4721 (while (and (not url) (setq scheme (pop lst)))
4722 (when (and (setq text (cdr (assoc-string (caar scheme)
4723 fields-alist t)))
4724 (string-match (cdar scheme) text))
4725 (setq url t scheme (cdr scheme))))))
4727 (when url
4728 (setq url (if (null scheme) (match-string 0 text)
4729 (if (stringp (car scheme))
4730 (setq fmt (pop scheme)))
4731 (dotimes (i (length scheme))
4732 (setq step (nth i scheme))
4733 ;; The first step shall use TEXT as obtained earlier.
4734 (unless (= i 0)
4735 (setq text (cdr (assoc-string (car step) fields-alist t))))
4736 (if (string-match (nth 1 step) text)
4737 (push (cond ((functionp (nth 2 step))
4738 (funcall (nth 2 step) text))
4739 ((numberp (nth 2 step))
4740 (match-string (nth 2 step) text))
4742 (replace-match (nth 2 step) t nil text)))
4743 obj)
4744 ;; If SCHEME is set up correctly,
4745 ;; we should never reach this point
4746 (error "Match failed: %s" text)))
4747 (if fmt (apply 'format fmt (nreverse obj))
4748 (apply 'concat (nreverse obj)))))
4749 (if (interactive-p) (message "%s" url))
4750 (unless no-browse (browse-url url)))
4751 (if (and (not url) (interactive-p)) (message "No URL known."))
4752 url)))
4755 ;; Make BibTeX a Feature
4757 (provide 'bibtex)
4759 ;; arch-tag: ee2be3af-caad-427f-b42a-d20fad630d04
4760 ;;; bibtex.el ends here