1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de>
7 ;; Bengt Martensson <bengt@mathematik.uni-Bremen.de>
8 ;; Mark Shapiro <shapiro@corto.inria.fr>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
34 ;; Major mode for editing and validating BibTeX files.
37 ;; See documentation for function bibtex-mode or type "\M-x describe-mode"
38 ;; when you are in BibTeX mode.
41 ;; Distribute texinfo file.
55 (defgroup bibtex-autokey nil
56 "Generate automatically a key from the author/editor and the title field."
58 :prefix
"bibtex-autokey-")
60 (defcustom bibtex-mode-hook nil
61 "List of functions to call on entry to BibTeX mode."
65 (defcustom bibtex-field-delimiters
'braces
66 "Type of field delimiters. Allowed values are `braces' or `double-quotes'."
68 :type
'(choice (const braces
)
69 (const double-quotes
)))
71 (defcustom bibtex-entry-delimiters
'braces
72 "Type of entry delimiters. Allowed values are `braces' or `parentheses'."
74 :type
'(choice (const braces
)
77 (defcustom bibtex-include-OPTcrossref
'("InProceedings" "InCollection")
78 "List of BibTeX entries that get an OPTcrossref field."
80 :type
'(repeat string
))
82 (defcustom bibtex-include-OPTkey t
83 "If non-nil, all newly created entries get an OPTkey field.
84 If this is a string, use it as the initial field text.
85 If this is a function, call it to generate the initial field text."
87 :type
'(choice (const :tag
"None" nil
)
88 (string :tag
"Initial text")
89 (function :tag
"Initialize Function" :value fun
)
90 (other :tag
"Default" t
)))
92 (defcustom bibtex-user-optional-fields
93 '(("annote" "Personal annotation (ignored)"))
94 "List of optional fields the user wants to have always present.
95 Entries should be of the same form as the OPTIONAL and
96 CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (which see)."
98 :type
'(repeat (group (string :tag
"Field")
99 (string :tag
"Comment")
100 (option (group :inline t
102 (choice :tag
"Init" :value
""
106 (defcustom bibtex-entry-format
107 '(opts-or-alts required-fields numerical-fields
)
108 "Type of formatting performed by `bibtex-clean-entry'.
109 It may be t, nil, or a list of symbols out of the following:
110 opts-or-alts Delete empty optional and alternative fields and
111 remove OPT and ALT prefixes from used fields.
112 required-fields Signal an error if a required field is missing.
113 numerical-fields Delete delimiters around numeral fields.
114 page-dashes Change double dashes in page field to single dash
115 (for scribe compatibility).
116 inherit-booktitle If entry contains a crossref field and the booktitle
117 field is empty, set the booktitle field to the content
118 of the title field of the crossreferenced entry.
119 realign Realign entries, so that field texts and perhaps equal
120 signs (depending on the value of
121 `bibtex-align-at-equal-sign') begin in the same column.
122 last-comma Add or delete comma on end of last field in entry,
123 according to value of `bibtex-comma-after-last-field'.
124 delimiters Change delimiters according to variables
125 `bibtex-field-delimiters' and `bibtex-entry-delimiters'.
126 unify-case Change case of entry and field names.
128 The value t means do all of the above formatting actions.
129 The value nil means do no formatting at all."
131 :type
'(choice (const :tag
"None" nil
)
133 (set :menu-tag
"Some"
135 (const required-fields
)
136 (const numerical-fields
)
138 (const inherit-booktitle
)
142 (const unify-case
))))
144 (defcustom bibtex-clean-entry-hook nil
145 "List of functions to call when entry has been cleaned.
146 Functions are called with point inside the cleaned entry, and the buffer
147 narrowed to just the entry."
151 (defcustom bibtex-maintain-sorted-entries nil
152 "If non-nil, BibTeX mode maintains all entries in sorted order.
153 Allowed non-nil values are:
154 plain All entries are sorted alphabetically.
155 crossref All entries are sorted alphabetically unless an entry has a
156 crossref field. These crossrefed entries are placed in
157 alphabetical order immediately preceding the main entry.
158 entry-class The entries are divided into classes according to their
159 entry name, see `bibtex-sort-entry-class'. Within each class
160 the entries are sorted alphabetically.
161 See also `bibtex-sort-ignore-string-entries'."
163 :type
'(choice (const nil
)
166 (const entry-class
)))
168 (defcustom bibtex-sort-entry-class
171 ("Book" "Proceedings"))
172 "List of classes of BibTeX entry names, used for sorting entries.
173 If value of `bibtex-maintain-sorted-entries' is `entry-class'
174 entries are ordered according to the classes they belong to. Each
175 class contains a list of entry names. An entry `catch-all' applies
176 to all entries not explicitly mentioned."
178 :type
'(repeat (choice :tag
"Class"
179 (const :tag
"catch-all" (catch-all))
180 (repeat :tag
"Entry name" string
))))
182 (defcustom bibtex-sort-ignore-string-entries t
183 "If non-nil, BibTeX @String entries are not sort-significant.
184 That means they are ignored when determining ordering of the buffer
185 \(e.g., sorting, locating alphabetical position for new entries, etc.)."
189 (defcustom bibtex-field-kill-ring-max
20
190 "Max length of `bibtex-field-kill-ring' before discarding oldest elements."
194 (defcustom bibtex-entry-kill-ring-max
20
195 "Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
199 (defcustom bibtex-parse-keys-timeout
60
200 "Time interval in seconds for parsing BibTeX buffers during idle time.
201 Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
205 (defcustom bibtex-parse-keys-fast t
206 "If non-nil, use fast but simplified algorithm for parsing BibTeX keys.
207 If parsing fails, try to set this variable to nil."
211 (defvar bibtex-entry-field-alist
213 ((("author" "Author1 [and Author2 ...] [and others]")
214 ("title" "Title of the article (BibTeX converts it to lowercase)")
215 ("journal" "Name of the journal (use string, remove braces)")
216 ("year" "Year of publication"))
217 (("volume" "Volume of the journal")
218 ("number" "Number of the journal (only allowed if entry contains volume)")
219 ("pages" "Pages in the journal")
220 ("month" "Month of the publication as a string (remove braces)")
221 ("note" "Remarks to be put at the end of the \\bibitem")))
222 ((("author" "Author1 [and Author2 ...] [and others]")
223 ("title" "Title of the article (BibTeX converts it to lowercase)"))
224 (("pages" "Pages in the journal")
225 ("journal" "Name of the journal (use string, remove braces)")
226 ("year" "Year of publication")
227 ("volume" "Volume of the journal")
228 ("number" "Number of the journal")
229 ("month" "Month of the publication as a string (remove braces)")
230 ("note" "Remarks to be put at the end of the \\bibitem"))))
232 ((("author" "Author1 [and Author2 ...] [and others]" nil t
)
233 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t
)
234 ("title" "Title of the book")
235 ("publisher" "Publishing company")
236 ("year" "Year of publication"))
237 (("volume" "Volume of the book in the series")
238 ("number" "Number of the book in a small series (overwritten by volume)")
239 ("series" "Series in which the book appeared")
240 ("address" "Address of the publisher")
241 ("edition" "Edition of the book as a capitalized English word")
242 ("month" "Month of the publication as a string (remove braces)")
243 ("note" "Remarks to be put at the end of the \\bibitem")))
244 ((("author" "Author1 [and Author2 ...] [and others]" nil t
)
245 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t
)
246 ("title" "Title of the book"))
247 (("publisher" "Publishing company")
248 ("year" "Year of publication")
249 ("volume" "Volume of the book in the series")
250 ("number" "Number of the book in a small series (overwritten by volume)")
251 ("series" "Series in which the book appeared")
252 ("address" "Address of the publisher")
253 ("edition" "Edition of the book as a capitalized English word")
254 ("month" "Month of the publication as a string (remove braces)")
255 ("note" "Remarks to be put at the end of the \\bibitem"))))
257 ((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
258 (("author" "Author1 [and Author2 ...] [and others]")
259 ("howpublished" "The way in which the booklet was published")
260 ("address" "Address of the publisher")
261 ("month" "Month of the publication as a string (remove braces)")
262 ("year" "Year of publication")
263 ("note" "Remarks to be put at the end of the \\bibitem"))))
265 ((("author" "Author1 [and Author2 ...] [and others]" nil t
)
266 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t
)
267 ("title" "Title of the book")
268 ("chapter" "Chapter in the book")
269 ("publisher" "Publishing company")
270 ("year" "Year of publication"))
271 (("volume" "Volume of the book in the series")
272 ("number" "Number of the book in a small series (overwritten by volume)")
273 ("series" "Series in which the book appeared")
274 ("type" "Word to use instead of \"chapter\"")
275 ("address" "Address of the publisher")
276 ("edition" "Edition of the book as a capitalized English word")
277 ("month" "Month of the publication as a string (remove braces)")
278 ("pages" "Pages in the book")
279 ("note" "Remarks to be put at the end of the \\bibitem")))
280 ((("author" "Author1 [and Author2 ...] [and others]" nil t
)
281 ("editor" "Editor1 [and Editor2 ...] [and others]" nil t
)
282 ("title" "Title of the book")
283 ("chapter" "Chapter in the book"))
284 (("pages" "Pages in the book")
285 ("publisher" "Publishing company")
286 ("year" "Year of publication")
287 ("volume" "Volume of the book in the series")
288 ("number" "Number of the book in a small series (overwritten by volume)")
289 ("series" "Series in which the book appeared")
290 ("type" "Word to use instead of \"chapter\"")
291 ("address" "Address of the publisher")
292 ("edition" "Edition of the book as a capitalized English word")
293 ("month" "Month of the publication as a string (remove braces)")
294 ("note" "Remarks to be put at the end of the \\bibitem"))))
296 ((("author" "Author1 [and Author2 ...] [and others]")
297 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
298 ("booktitle" "Name of the book")
299 ("publisher" "Publishing company")
300 ("year" "Year of publication"))
301 (("editor" "Editor1 [and Editor2 ...] [and others]")
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 ("type" "Word to use instead of \"chapter\"")
306 ("chapter" "Chapter in the book")
307 ("pages" "Pages in the book")
308 ("address" "Address of the publisher")
309 ("edition" "Edition of the book as a capitalized English word")
310 ("month" "Month of the publication as a string (remove braces)")
311 ("note" "Remarks to be put at the end of the \\bibitem")))
312 ((("author" "Author1 [and Author2 ...] [and others]")
313 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
314 ("booktitle" "Name of the book"))
315 (("pages" "Pages in the book")
316 ("publisher" "Publishing company")
317 ("year" "Year of publication")
318 ("editor" "Editor1 [and Editor2 ...] [and others]")
319 ("volume" "Volume of the book in the series")
320 ("number" "Number of the book in a small series (overwritten by volume)")
321 ("series" "Series in which the book appeared")
322 ("type" "Word to use instead of \"chapter\"")
323 ("chapter" "Chapter in the book")
324 ("address" "Address of the publisher")
325 ("edition" "Edition of the book as a capitalized English word")
326 ("month" "Month of the publication as a string (remove braces)")
327 ("note" "Remarks to be put at the end of the \\bibitem"))))
329 ((("author" "Author1 [and Author2 ...] [and others]")
330 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
331 ("booktitle" "Name of the conference proceedings")
332 ("year" "Year of publication"))
333 (("editor" "Editor1 [and Editor2 ...] [and others]")
334 ("volume" "Volume of the conference proceedings in the series")
335 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
336 ("series" "Series in which the conference proceedings appeared")
337 ("pages" "Pages in the conference proceedings")
338 ("address" "Location of the Proceedings")
339 ("month" "Month of the publication as a string (remove braces)")
340 ("organization" "Sponsoring organization of the conference")
341 ("publisher" "Publishing company, its location")
342 ("note" "Remarks to be put at the end of the \\bibitem")))
343 ((("author" "Author1 [and Author2 ...] [and others]")
344 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)"))
345 (("booktitle" "Name of the conference proceedings")
346 ("pages" "Pages in the conference proceedings")
347 ("year" "Year of publication")
348 ("editor" "Editor1 [and Editor2 ...] [and others]")
349 ("volume" "Volume of the conference proceedings in the series")
350 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
351 ("series" "Series in which the conference proceedings appeared")
352 ("address" "Location of the Proceedings")
353 ("month" "Month of the publication as a string (remove braces)")
354 ("organization" "Sponsoring organization of the conference")
355 ("publisher" "Publishing company, its location")
356 ("note" "Remarks to be put at the end of the \\bibitem"))))
358 ((("title" "Title of the manual"))
359 (("author" "Author1 [and Author2 ...] [and others]")
360 ("organization" "Publishing organization of the manual")
361 ("address" "Address of the organization")
362 ("edition" "Edition of the manual as a capitalized English word")
363 ("month" "Month of the publication as a string (remove braces)")
364 ("year" "Year of publication")
365 ("note" "Remarks to be put at the end of the \\bibitem"))))
367 ((("author" "Author1 [and Author2 ...] [and others]")
368 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
369 ("school" "School where the master\'s thesis was written")
370 ("year" "Year of publication"))
371 (("type" "Type of the master\'s thesis (if other than \"Master\'s thesis\")")
372 ("address" "Address of the school (if not part of field \"school\") or country")
373 ("month" "Month of the publication as a string (remove braces)")
374 ("note" "Remarks to be put at the end of the \\bibitem"))))
377 (("author" "Author1 [and Author2 ...] [and others]")
378 ("title" "Title of the work (BibTeX converts it to lowercase)")
379 ("howpublished" "The way in which the work was published")
380 ("month" "Month of the publication as a string (remove braces)")
381 ("year" "Year of publication")
382 ("note" "Remarks to be put at the end of the \\bibitem"))))
384 ((("author" "Author1 [and Author2 ...] [and others]")
385 ("title" "Title of the PhD. thesis")
386 ("school" "School where the PhD. thesis was written")
387 ("year" "Year of publication"))
388 (("type" "Type of the PhD. thesis")
389 ("address" "Address of the school (if not part of field \"school\") or country")
390 ("month" "Month of the publication as a string (remove braces)")
391 ("note" "Remarks to be put at the end of the \\bibitem"))))
393 ((("title" "Title of the conference proceedings")
394 ("year" "Year of publication"))
395 (("booktitle" "Title of the proceedings for cross references")
396 ("editor" "Editor1 [and Editor2 ...] [and others]")
397 ("volume" "Volume of the conference proceedings in the series")
398 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
399 ("series" "Series in which the conference proceedings appeared")
400 ("address" "Location of the Proceedings")
401 ("month" "Month of the publication as a string (remove braces)")
402 ("organization" "Sponsoring organization of the conference")
403 ("publisher" "Publishing company, its location")
404 ("note" "Remarks to be put at the end of the \\bibitem"))))
406 ((("author" "Author1 [and Author2 ...] [and others]")
407 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
408 ("institution" "Sponsoring institution of the report")
409 ("year" "Year of publication"))
410 (("type" "Type of the report (if other than \"technical report\")")
411 ("number" "Number of the technical report")
412 ("address" "Address of the institution (if not part of field \"institution\") or country")
413 ("month" "Month of the publication as a string (remove braces)")
414 ("note" "Remarks to be put at the end of the \\bibitem"))))
416 ((("author" "Author1 [and Author2 ...] [and others]")
417 ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
418 ("note" "Remarks to be put at the end of the \\bibitem"))
419 (("month" "Month of the publication as a string (remove braces)")
420 ("year" "Year of publication")))))
422 "List of BibTeX entry types and their associated fields.
423 List elements are triples
424 \(ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL)).
425 ENTRY-NAME is the name of a BibTeX entry. The remaining pairs contain
426 the required and optional fields of the BibTeX entry.
427 The second pair is used if a crossref field is present
428 and the first pair is used if a crossref field is absent.
429 If the second pair is nil, the first pair is always used.
430 REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.
431 Each element of these lists is a list of the form
432 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).
433 COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.
434 FIELD-NAME is the name of the field, COMMENT-STRING is the comment that
435 appears in the echo area, INIT is either the initial content of the
436 field or a function, which is called to determine the initial content
437 of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
438 field is an alternative. ALTERNATIVE-FLAG may be t only in the
439 REQUIRED or CROSSREF-REQUIRED lists.")
441 (defcustom bibtex-comment-start
"@Comment"
442 "String starting a BibTeX comment."
446 (defcustom bibtex-add-entry-hook nil
447 "List of functions to call when BibTeX entry has been inserted."
451 (defcustom bibtex-predefined-month-strings
452 '(("jan" .
"January")
460 ("sep" .
"September")
463 ("dec" .
"December"))
464 "Alist of month string definitions used in the BibTeX style files.
465 Each element is a pair of strings (ABBREVIATION . EXPANSION)."
467 :type
'(repeat (cons (string :tag
"Month abbreviation")
468 (string :tag
"Month expansion"))))
470 (defcustom bibtex-predefined-strings
472 bibtex-predefined-month-strings
473 '(("acmcs" .
"ACM Computing Surveys")
474 ("acta" .
"Acta Informatica")
475 ("cacm" .
"Communications of the ACM")
476 ("ibmjrd" .
"IBM Journal of Research and Development")
477 ("ibmsj" .
"IBM Systems Journal")
478 ("ieeese" .
"IEEE Transactions on Software Engineering")
479 ("ieeetc" .
"IEEE Transactions on Computers")
480 ("ieeetcad" .
"IEEE Transactions on Computer-Aided Design of Integrated Circuits")
481 ("ipl" .
"Information Processing Letters")
482 ("jacm" .
"Journal of the ACM")
483 ("jcss" .
"Journal of Computer and System Sciences")
484 ("scp" .
"Science of Computer Programming")
485 ("sicomp" .
"SIAM Journal on Computing")
486 ("tcs" .
"Theoretical Computer Science")
487 ("tocs" .
"ACM Transactions on Computer Systems")
488 ("tods" .
"ACM Transactions on Database Systems")
489 ("tog" .
"ACM Transactions on Graphics")
490 ("toms" .
"ACM Transactions on Mathematical Software")
491 ("toois" .
"ACM Transactions on Office Information Systems")
492 ("toplas" .
"ACM Transactions on Programming Languages and Systems")))
493 "Alist of string definitions used in the BibTeX style files.
494 Each element is a pair of strings (ABBREVIATION . EXPANSION)."
496 :type
'(repeat (cons (string :tag
"String")
497 (string :tag
"String expansion"))))
499 (defcustom bibtex-string-files nil
500 "List of BibTeX files containing string definitions.
501 List elements can be absolute file names or file names relative
502 to the directories specified in `bibtex-string-file-path'."
504 :type
'(repeat file
))
506 (defvar bibtex-string-file-path
(getenv "BIBINPUTS")
507 "*Colon separated list of paths to search for `bibtex-string-files'.")
509 (defcustom bibtex-files nil
510 "List of BibTeX files that are searched for entry keys.
511 List elements can be absolute file names or file names relative to the
512 directories specified in `bibtex-file-path'. If an element is a directory,
513 check all BibTeX files in this directory. If an element is the symbol
514 `bibtex-file-path', check all BibTeX files in `bibtex-file-path'."
516 :type
'(repeat file
))
518 (defvar bibtex-file-path
(getenv "BIBINPUTS")
519 "*Colon separated list of paths to search for `bibtex-files'.")
521 (defcustom bibtex-help-message t
522 "If non-nil print help messages in the echo area on entering a new field."
526 (defcustom bibtex-autokey-prefix-string
""
527 "String prefix for automatically generated reference keys.
528 See `bibtex-generate-autokey' for details."
529 :group
'bibtex-autokey
532 (defcustom bibtex-autokey-names
1
533 "Number of names to use for the automatically generated reference key.
534 Possibly more names are used according to `bibtex-autokey-names-stretch'.
535 If this variable is nil, all names are used.
536 See `bibtex-generate-autokey' for details."
537 :group
'bibtex-autokey
538 :type
'(choice (const :tag
"All" infty
)
541 (defcustom bibtex-autokey-names-stretch
0
542 "Number of names that can additionally be used for reference keys.
543 These names are used only, if all names are used then.
544 See `bibtex-generate-autokey' for details."
545 :group
'bibtex-autokey
548 (defcustom bibtex-autokey-additional-names
""
549 "String to append to the generated key if not all names could be used.
550 See `bibtex-generate-autokey' for details."
551 :group
'bibtex-autokey
554 (defcustom bibtex-autokey-expand-strings nil
555 "If non-nil, expand strings when extracting the content of a BibTeX field.
556 See `bibtex-generate-autokey' for details."
557 :group
'bibtex-autokey
560 (defvar bibtex-autokey-transcriptions
561 '(;; language specific characters
562 ("\\\\aa" .
"a") ; \aa -> a
563 ("\\\\AA" .
"A") ; \AA -> A
564 ("\\\"a\\|\\\\\\\"a\\|\\\\ae" .
"ae") ; "a,\"a,\ae -> ae
565 ("\\\"A\\|\\\\\\\"A\\|\\\\AE" .
"Ae") ; "A,\"A,\AE -> Ae
566 ("\\\\i" .
"i") ; \i -> i
567 ("\\\\j" .
"j") ; \j -> j
568 ("\\\\l" .
"l") ; \l -> l
569 ("\\\\L" .
"L") ; \L -> L
570 ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" .
"oe") ; "o,\"o,\o,\oe -> oe
571 ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" .
"Oe") ; "O,\"O,\O,\OE -> Oe
572 ("\\\"s\\|\\\\\\\"s\\|\\\\3" .
"ss") ; "s,\"s,\3 -> ss
573 ("\\\"u\\|\\\\\\\"u" .
"ue") ; "u,\"u -> ue
574 ("\\\"U\\|\\\\\\\"U" .
"Ue") ; "U,\"U -> Ue
576 ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" .
"")
577 ;; braces, quotes, concatenation.
580 ("\\\\?[ \t\n]+\\|~" .
" "))
581 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
582 Used by the default values of `bibtex-autokey-name-change-strings' and
583 `bibtex-autokey-titleword-change-strings'. Defaults to translating some
584 language specific characters to their ASCII transcriptions, and
585 removing any character accents.")
587 (defcustom bibtex-autokey-name-change-strings
588 bibtex-autokey-transcriptions
589 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
590 Any part of a name matching OLD-REGEXP is replaced by NEW-STRING.
591 Case is significant in OLD-REGEXP. All regexps are tried in the
592 order in which they appear in the list.
593 See `bibtex-generate-autokey' for details."
594 :group
'bibtex-autokey
595 :type
'(repeat (cons (regexp :tag
"Old")
596 (string :tag
"New"))))
598 (defcustom bibtex-autokey-name-case-convert
'downcase
599 "Function called for each name to perform case conversion.
600 See `bibtex-generate-autokey' for details."
601 :group
'bibtex-autokey
602 :type
'(choice (const :tag
"Preserve case" identity
)
603 (const :tag
"Downcase" downcase
)
604 (const :tag
"Capitalize" capitalize
)
605 (const :tag
"Upcase" upcase
)
606 (function :tag
"Conversion function")))
608 (defcustom bibtex-autokey-name-length
'infty
609 "Number of characters from name to incorporate into key.
610 If this is set to anything but a number, all characters are used.
611 See `bibtex-generate-autokey' for details."
612 :group
'bibtex-autokey
613 :type
'(choice (const :tag
"All" infty
)
616 (defcustom bibtex-autokey-name-separator
""
617 "String that comes between any two names in the key.
618 See `bibtex-generate-autokey' for details."
619 :group
'bibtex-autokey
622 (defcustom bibtex-autokey-year-length
2
623 "Number of rightmost digits from the year field to incorporate into key.
624 See `bibtex-generate-autokey' for details."
625 :group
'bibtex-autokey
628 (defcustom bibtex-autokey-use-crossref t
629 "If non-nil use fields from crossreferenced entry if necessary.
630 If this variable is non-nil and some field has no entry, but a
631 valid crossref entry, the field from the crossreferenced entry is used.
632 See `bibtex-generate-autokey' for details."
633 :group
'bibtex-autokey
636 (defcustom bibtex-autokey-titlewords
5
637 "Number of title words to use for the automatically generated reference key.
638 If this is set to anything but a number, all title words are used.
639 Possibly more words from the title are used according to
640 `bibtex-autokey-titlewords-stretch'.
641 See `bibtex-generate-autokey' for details."
642 :group
'bibtex-autokey
643 :type
'(choice (const :tag
"All" infty
)
646 (defcustom bibtex-autokey-title-terminators
"[.!?:;]\\|--"
647 "Regexp defining the termination of the main part of the title.
648 Case of the regexps is ignored. See `bibtex-generate-autokey' for details."
649 :group
'bibtex-autokey
652 (defcustom bibtex-autokey-titlewords-stretch
2
653 "Number of words that can additionally be used from the title.
654 These words are used only, if a sentence from the title can be ended then.
655 See `bibtex-generate-autokey' for details."
656 :group
'bibtex-autokey
659 (defcustom bibtex-autokey-titleword-ignore
660 '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das"
661 "[^[:upper:]].*" ".*[^[:upper:]0-9].*")
662 "Determines words from the title that are not to be used in the key.
663 Each item of the list is a regexp. If a word of the title matches a
664 regexp from that list, it is not included in the title part of the key.
665 See `bibtex-generate-autokey' for details."
666 :group
'bibtex-autokey
667 :type
'(repeat regexp
))
669 (defcustom bibtex-autokey-titleword-case-convert
'downcase
670 "Function called for each titleword to perform case conversion.
671 See `bibtex-generate-autokey' for details."
672 :group
'bibtex-autokey
673 :type
'(choice (const :tag
"Preserve case" identity
)
674 (const :tag
"Downcase" downcase
)
675 (const :tag
"Capitalize" capitalize
)
676 (const :tag
"Upcase" upcase
)
677 (function :tag
"Conversion function")))
679 (defcustom bibtex-autokey-titleword-abbrevs nil
680 "Determines exceptions to the usual abbreviation mechanism.
681 An alist of (OLD-REGEXP . NEW-STRING) pairs. Case is ignored
682 in matching against OLD-REGEXP, and the first matching pair is used.
683 See `bibtex-generate-autokey' for details."
684 :group
'bibtex-autokey
685 :type
'(repeat (cons (regexp :tag
"Old")
686 (string :tag
"New"))))
688 (defcustom bibtex-autokey-titleword-change-strings
689 bibtex-autokey-transcriptions
690 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
691 Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
692 Case is significant in OLD-REGEXP. All regexps are tried in the
693 order in which they appear in the list.
694 See `bibtex-generate-autokey' for details."
695 :group
'bibtex-autokey
696 :type
'(repeat (cons (regexp :tag
"Old")
697 (string :tag
"New"))))
699 (defcustom bibtex-autokey-titleword-length
5
700 "Number of characters from title words to incorporate into key.
701 If this is set to anything but a number, all characters are used.
702 See `bibtex-generate-autokey' for details."
703 :group
'bibtex-autokey
704 :type
'(choice (const :tag
"All" infty
)
707 (defcustom bibtex-autokey-titleword-separator
"_"
708 "String to be put between the title words.
709 See `bibtex-generate-autokey' for details."
710 :group
'bibtex-autokey
713 (defcustom bibtex-autokey-name-year-separator
""
714 "String to be put between name part and year part of key.
715 See `bibtex-generate-autokey' for details."
716 :group
'bibtex-autokey
719 (defcustom bibtex-autokey-year-title-separator
":_"
720 "String to be put between name part and year part of key.
721 See `bibtex-generate-autokey' for details."
722 :group
'bibtex-autokey
725 (defcustom bibtex-autokey-edit-before-use t
726 "If non-nil, user is allowed to edit the generated key before it is used."
727 :group
'bibtex-autokey
730 (defcustom bibtex-autokey-before-presentation-function nil
731 "If non-nil, function to call before generated key is presented.
732 The function must take one argument (the automatically generated key),
733 and must return a string (the key to use)."
734 :group
'bibtex-autokey
735 :type
'(choice (const nil
) function
))
737 (defcustom bibtex-entry-offset
0
738 "Offset for BibTeX entries.
739 Added to the value of all other variables which determine columns."
743 (defcustom bibtex-field-indentation
2
744 "Starting column for the name part in BibTeX fields."
748 (defcustom bibtex-text-indentation
749 (+ bibtex-field-indentation
750 (length "organization = "))
751 "Starting column for the text part in BibTeX fields.
752 Should be equal to the space needed for the longest name part."
756 (defcustom bibtex-contline-indentation
757 (+ bibtex-text-indentation
1)
758 "Starting column for continuation lines of BibTeX fields."
762 (defcustom bibtex-align-at-equal-sign nil
763 "If non-nil, align fields at equal sign instead of field text.
764 If non-nil, the column for the equal sign is the value of
765 `bibtex-text-indentation', minus 2."
769 (defcustom bibtex-comma-after-last-field nil
770 "If non-nil, a comma is put at end of last field in the entry template."
774 (defcustom bibtex-autoadd-commas t
775 "If non-nil automatically add missing commas at end of BibTeX fields."
779 (defcustom bibtex-autofill-types
'("Proceedings")
780 "Automatically fill fields if possible for those BibTeX entry types."
782 :type
'(repeat string
))
784 (defcustom bibtex-summary-function
'bibtex-summary
785 "Function to call for generating a summary of current BibTeX entry.
786 It takes no arguments. Point must be at beginning of entry.
787 Used by `bibtex-complete-crossref-cleanup' and `bibtex-copy-summary-as-kill'."
789 :type
'(choice (const :tag
"Default" bibtex-summary
)
790 (function :tag
"Personalized function")))
792 (defcustom bibtex-generate-url-list
793 '((("url" .
".*:.*")))
794 "List of schemes for generating the URL of a BibTeX entry.
795 These schemes are used by `bibtex-url'.
797 Each scheme should have one of these forms:
800 ((FIELD . REGEXP) STEP...)
801 ((FIELD . REGEXP) STRING STEP...)
803 FIELD is a field name as returned by `bibtex-parse-entry'.
804 REGEXP is matched against the text of FIELD. If the match succeeds,
805 then this scheme is used. If no STRING and STEPs are specified
806 the matched text is used as the URL, otherwise the URL is built
807 by evaluating STEPs. If no STRING is specified the STEPs must result
808 in strings which are concatenated. Otherwise the resulting objects
809 are passed through `format' using STRING as format control string.
811 A STEP is a list (FIELD REGEXP REPLACE). The text of FIELD
812 is matched against REGEXP, and is replaced with REPLACE.
813 REPLACE can be a string, or a number (which selects the corresponding
814 submatch), or a function called with the field's text as argument
815 and with the `match-data' properly set.
817 Case is always ignored. Always remove the field delimiters.
818 If `bibtex-expand-strings' is non-nil, BibTeX strings are expanded
819 for generating the URL.
821 The following is a complex example, see http://link.aps.org/linkfaq.html.
823 (((\"journal\" . \"\\\\=<\\(PR[ABCDEL]?\\|RMP\\)\\\\=>\")
824 \"http://link.aps.org/abstract/%s/v%s/p%s\"
825 (\"journal\" \".*\" downcase)
826 (\"volume\" \".*\" 0)
827 (\"pages\" \"\\`[A-Z]?[0-9]+\" 0)))"
831 (cons :tag
"Matcher" :extra-offset
4
832 (string :tag
"BibTeX field")
833 (regexp :tag
"Regexp"))
835 (const :tag
"Take match as is" nil
)
836 (cons :tag
"Formatted"
837 (string :tag
"Format control string")
838 (repeat :tag
"Steps to generate URL"
839 (list (string :tag
"BibTeX field")
840 (regexp :tag
"Regexp")
841 (choice (string :tag
"Replacement")
842 (integer :tag
"Sub-match")
843 (function :tag
"Filter")))))
844 (repeat :tag
"Concatenated"
845 (list (string :tag
"BibTeX field")
846 (regexp :tag
"Regexp")
847 (choice (string :tag
"Replacement")
848 (integer :tag
"Sub-match")
849 (function :tag
"Filter"))))))))
851 (defcustom bibtex-expand-strings nil
852 "If non-nil, expand strings when extracting the content of a BibTeX field."
856 ;; `bibtex-font-lock-keywords' is a user option as well, but since the
857 ;; patterns used to define this variable are defined in a later
858 ;; section of this file, it is defined later.
861 ;; Syntax Table and Keybindings
862 (defvar bibtex-mode-syntax-table
863 (let ((st (make-syntax-table)))
864 (modify-syntax-entry ?
\" "\"" st
)
865 (modify-syntax-entry ?$
"$$ " st
)
866 (modify-syntax-entry ?%
"< " st
)
867 (modify-syntax-entry ?
' "w " st
)
868 (modify-syntax-entry ?
@ "w " st
)
869 (modify-syntax-entry ?
\\ "\\" st
)
870 (modify-syntax-entry ?
\f "> " st
)
871 (modify-syntax-entry ?
\n "> " st
)
872 ;; Keys cannot have = in them (wrong font-lock of @string{foo=bar}).
873 (modify-syntax-entry ?
= "." st
)
874 (modify-syntax-entry ?~
" " st
)
876 "Syntax table used in BibTeX mode buffers.")
878 (defvar bibtex-mode-map
879 (let ((km (make-sparse-keymap)))
880 ;; The Key `C-c&' is reserved for reftex.el
881 (define-key km
"\t" 'bibtex-find-text
)
882 (define-key km
"\n" 'bibtex-next-field
)
883 (define-key km
"\M-\t" 'bibtex-complete
)
884 (define-key km
"\C-c\"" 'bibtex-remove-delimiters
)
885 (define-key km
"\C-c{" 'bibtex-remove-delimiters
)
886 (define-key km
"\C-c}" 'bibtex-remove-delimiters
)
887 (define-key km
"\C-c\C-c" 'bibtex-clean-entry
)
888 (define-key km
"\C-c\C-q" 'bibtex-fill-entry
)
889 (define-key km
"\C-c\C-s" 'bibtex-find-entry
)
890 (define-key km
"\C-c\C-x" 'bibtex-find-crossref
)
891 (define-key km
"\C-c\C-t" 'bibtex-copy-summary-as-kill
)
892 (define-key km
"\C-c?" 'bibtex-print-help-message
)
893 (define-key km
"\C-c\C-p" 'bibtex-pop-previous
)
894 (define-key km
"\C-c\C-n" 'bibtex-pop-next
)
895 (define-key km
"\C-c\C-k" 'bibtex-kill-field
)
896 (define-key km
"\C-c\M-k" 'bibtex-copy-field-as-kill
)
897 (define-key km
"\C-c\C-w" 'bibtex-kill-entry
)
898 (define-key km
"\C-c\M-w" 'bibtex-copy-entry-as-kill
)
899 (define-key km
"\C-c\C-y" 'bibtex-yank
)
900 (define-key km
"\C-c\M-y" 'bibtex-yank-pop
)
901 (define-key km
"\C-c\C-d" 'bibtex-empty-field
)
902 (define-key km
"\C-c\C-f" 'bibtex-make-field
)
903 (define-key km
"\C-c\C-u" 'bibtex-entry-update
)
904 (define-key km
"\C-c$" 'bibtex-ispell-abstract
)
905 (define-key km
"\M-\C-a" 'bibtex-beginning-of-entry
)
906 (define-key km
"\M-\C-e" 'bibtex-end-of-entry
)
907 (define-key km
"\C-\M-l" 'bibtex-reposition-window
)
908 (define-key km
"\C-\M-h" 'bibtex-mark-entry
)
909 (define-key km
"\C-c\C-b" 'bibtex-entry
)
910 (define-key km
"\C-c\C-rn" 'bibtex-narrow-to-entry
)
911 (define-key km
"\C-c\C-rw" 'widen
)
912 (define-key km
"\C-c\C-l" 'bibtex-url
)
913 (define-key km
"\C-c\C-o" 'bibtex-remove-OPT-or-ALT
)
914 (define-key km
"\C-c\C-e\C-i" 'bibtex-InProceedings
)
915 (define-key km
"\C-c\C-ei" 'bibtex-InCollection
)
916 (define-key km
"\C-c\C-eI" 'bibtex-InBook
)
917 (define-key km
"\C-c\C-e\C-a" 'bibtex-Article
)
918 (define-key km
"\C-c\C-e\C-b" 'bibtex-InBook
)
919 (define-key km
"\C-c\C-eb" 'bibtex-Book
)
920 (define-key km
"\C-c\C-eB" 'bibtex-Booklet
)
921 (define-key km
"\C-c\C-e\C-c" 'bibtex-InCollection
)
922 (define-key km
"\C-c\C-e\C-m" 'bibtex-Manual
)
923 (define-key km
"\C-c\C-em" 'bibtex-MastersThesis
)
924 (define-key km
"\C-c\C-eM" 'bibtex-Misc
)
925 (define-key km
"\C-c\C-e\C-p" 'bibtex-InProceedings
)
926 (define-key km
"\C-c\C-ep" 'bibtex-Proceedings
)
927 (define-key km
"\C-c\C-eP" 'bibtex-PhdThesis
)
928 (define-key km
"\C-c\C-e\M-p" 'bibtex-Preamble
)
929 (define-key km
"\C-c\C-e\C-s" 'bibtex-String
)
930 (define-key km
"\C-c\C-e\C-t" 'bibtex-TechReport
)
931 (define-key km
"\C-c\C-e\C-u" 'bibtex-Unpublished
)
933 "Keymap used in BibTeX mode.")
936 bibtex-edit-menu bibtex-mode-map
"BibTeX-Edit Menu in BibTeX mode"
938 ("Moving inside an Entry"
939 ["End of Field" bibtex-find-text t
]
940 ["Next Field" bibtex-next-field t
]
941 ["Beginning of Entry" bibtex-beginning-of-entry t
]
942 ["End of Entry" bibtex-end-of-entry t
]
944 ["Make Entry Visible" bibtex-reposition-window t
])
945 ("Moving in BibTeX Buffers"
946 ["Find Entry" bibtex-find-entry t
]
947 ["Find Crossref Entry" bibtex-find-crossref t
])
949 ("Operating on Current Field"
950 ["Fill Field" fill-paragraph t
]
951 ["Remove Delimiters" bibtex-remove-delimiters t
]
952 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t
]
953 ["Clear Field" bibtex-empty-field t
]
955 ["Kill Field" bibtex-kill-field t
]
956 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t
]
957 ["Paste Most Recently Killed Field" bibtex-yank t
]
958 ["Paste Previously Killed Field" bibtex-yank-pop t
]
960 ["Make New Field" bibtex-make-field t
]
962 ["Snatch from Similar Following Field" bibtex-pop-next t
]
963 ["Snatch from Similar Preceding Field" bibtex-pop-previous t
]
965 ["String or Key Complete" bibtex-complete t
]
967 ["Help about Current Field" bibtex-print-help-message t
])
968 ("Operating on Current Entry"
969 ["Fill Entry" bibtex-fill-entry t
]
970 ["Clean Entry" bibtex-clean-entry t
]
971 ["Update Entry" bibtex-entry-update t
]
973 ["Kill Entry" bibtex-kill-entry t
]
974 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t
]
975 ["Paste Most Recently Killed Entry" bibtex-yank t
]
976 ["Paste Previously Killed Entry" bibtex-yank-pop t
]
978 ["Copy Summary to Kill Ring" bibtex-copy-summary-as-kill t
]
979 ["Browse URL" bibtex-url t
]
981 ["Ispell Entry" bibtex-ispell-entry t
]
982 ["Ispell Entry Abstract" bibtex-ispell-abstract t
]
984 ["Narrow to Entry" bibtex-narrow-to-entry t
]
985 ["Mark Entry" bibtex-mark-entry t
]
987 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
988 (fboundp 'reftex-view-crossref-from-bibtex
)])
989 ("Operating on Buffer or Region"
990 ["Validate Entries" bibtex-validate t
]
991 ["Sort Entries" bibtex-sort-buffer t
]
992 ["Reformat Entries" bibtex-reformat t
]
993 ["Count Entries" bibtex-count-entries t
]
995 ["Convert Alien Buffer" bibtex-convert-alien t
])
996 ("Operating on Multiple Buffers"
997 ["Validate Entries" bibtex-validate-globally t
])))
1000 bibtex-entry-menu bibtex-mode-map
"Entry-Types Menu in BibTeX mode"
1002 ["Article in Journal" bibtex-Article t
]
1003 ["Article in Conference Proceedings" bibtex-InProceedings t
]
1004 ["Article in a Collection" bibtex-InCollection t
]
1005 ["Chapter or Pages in a Book" bibtex-InBook t
]
1006 ["Conference Proceedings" bibtex-Proceedings t
]
1007 ["Book" bibtex-Book t
]
1008 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t
]
1009 ["PhD. Thesis" bibtex-PhdThesis t
]
1010 ["Master's Thesis" bibtex-MastersThesis t
]
1011 ["Technical Report" bibtex-TechReport t
]
1012 ["Technical Manual" bibtex-Manual t
]
1013 ["Unpublished" bibtex-Unpublished t
]
1014 ["Miscellaneous" bibtex-Misc t
]
1016 ["String" bibtex-String t
]
1017 ["Preamble" bibtex-Preamble t
]))
1020 ;; Internal Variables
1022 (defvar bibtex-pop-previous-search-point nil
1023 "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
1025 (defvar bibtex-pop-next-search-point nil
1026 "Next point where `bibtex-pop-next' starts looking for a similar entry.")
1028 (defvar bibtex-field-kill-ring nil
1029 "Ring of least recently killed fields.
1030 At most `bibtex-field-kill-ring-max' items are kept here.")
1032 (defvar bibtex-field-kill-ring-yank-pointer nil
1033 "The tail of `bibtex-field-kill-ring' whose car is the last item yanked.")
1035 (defvar bibtex-entry-kill-ring nil
1036 "Ring of least recently killed entries.
1037 At most `bibtex-entry-kill-ring-max' items are kept here.")
1039 (defvar bibtex-entry-kill-ring-yank-pointer nil
1040 "The tail of `bibtex-entry-kill-ring' whose car is the last item yanked.")
1042 (defvar bibtex-last-kill-command nil
1043 "Type of the last kill command (either 'field or 'entry).")
1045 (defvar bibtex-strings
1046 (lazy-completion-table bibtex-strings
1047 bibtex-parse-strings
(bibtex-string-files-init))
1048 "Completion table for BibTeX string keys.
1049 Initialized from `bibtex-predefined-strings' and `bibtex-string-files'.")
1050 (make-variable-buffer-local 'bibtex-strings
)
1052 (defvar bibtex-reference-keys
1053 (lazy-completion-table bibtex-reference-keys bibtex-parse-keys nil t
)
1054 "Completion table for BibTeX reference keys.
1055 The CDRs of the elements are t for header keys and nil for crossref keys.")
1056 (make-variable-buffer-local 'bibtex-reference-keys
)
1058 (defvar bibtex-buffer-last-parsed-tick nil
1059 "Value of `buffer-modified-tick' last time buffer was parsed for keys.")
1061 (defvar bibtex-parse-idle-timer nil
1062 "Stores if timer is already installed.")
1064 (defvar bibtex-progress-lastperc nil
1065 "Last reported percentage for the progress message.")
1067 (defvar bibtex-progress-lastmes nil
1068 "Last reported progress message.")
1070 (defvar bibtex-progress-interval nil
1071 "Interval for progress messages.")
1073 (defvar bibtex-key-history nil
1074 "History list for reading keys.")
1076 (defvar bibtex-entry-type-history nil
1077 "History list for reading entry types.")
1079 (defvar bibtex-field-history nil
1080 "History list for reading field names.")
1082 (defvar bibtex-reformat-previous-options nil
1083 "Last reformat options given.")
1085 (defvar bibtex-reformat-previous-reference-keys nil
1086 "Last reformat reference keys option given.")
1088 (defconst bibtex-field-name
"[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*"
1089 "Regexp matching the name of a BibTeX field.")
1091 (defconst bibtex-name-part
1092 (concat ",[ \t\n]*\\(" bibtex-field-name
"\\)[ \t\n]*=")
1093 "Regexp matching the name part of a BibTeX field.")
1095 (defconst bibtex-reference-key
"[][[:alnum:].:;?!`'/*@+|()<>&_^$-]+"
1096 "Regexp matching the reference key part of a BibTeX entry.")
1098 (defconst bibtex-field-const
"[][[:alnum:].:;?!`'/*@+=|<>&_^$-]+"
1099 "Regexp matching a BibTeX field constant.")
1101 (defvar bibtex-entry-type
1102 (concat "@[ \t]*\\(?:"
1103 (regexp-opt (mapcar 'car bibtex-entry-field-alist
)) "\\)")
1104 "Regexp matching the name of a BibTeX entry.")
1106 (defvar bibtex-entry-type-whitespace
1107 (concat "[ \t]*" bibtex-entry-type
)
1108 "Regexp matching the name of a BibTeX entry preceded by whitespace.")
1110 (defvar bibtex-entry-type-str
1111 (concat "@[ \t]*\\(?:"
1112 (regexp-opt (append '("String")
1113 (mapcar 'car bibtex-entry-field-alist
))) "\\)")
1114 "Regexp matching the name of a BibTeX entry (including @String).")
1116 (defvar bibtex-entry-head
1117 (concat "^[ \t]*\\("
1119 "\\)[ \t]*[({][ \t\n]*\\("
1120 bibtex-reference-key
1122 "Regexp matching the header line of a BibTeX entry (including key).")
1124 (defvar bibtex-entry-maybe-empty-head
1125 (concat bibtex-entry-head
"?")
1126 "Regexp matching the header line of a BibTeX entry (possibly without key).")
1128 (defconst bibtex-any-entry-maybe-empty-head
1129 (concat "^[ \t]*\\(@[ \t]*" bibtex-field-name
"\\)[ \t]*[({][ \t\n]*\\("
1130 bibtex-reference-key
"\\)?")
1131 "Regexp matching the header line of any BibTeX entry (possibly without key).")
1133 (defconst bibtex-type-in-head
1
1134 "Regexp subexpression number of the type part in `bibtex-entry-head'.")
1136 (defconst bibtex-key-in-head
2
1137 "Regexp subexpression number of the key part in `bibtex-entry-head'.")
1139 (defconst bibtex-empty-field-re
"\\`\\(\"\"\\|{}\\)\\'"
1140 "Regexp matching the text part (as a string) of an empty field.")
1142 (defconst bibtex-string-type
"^[ \t]*\\(@[ \t]*String\\)[ \t]*[({][ \t\n]*"
1143 "Regexp matching the name of a BibTeX String entry.")
1145 (defconst bibtex-string-maybe-empty-head
1146 (concat bibtex-string-type
"\\(" bibtex-reference-key
"\\)?")
1147 "Regexp matching the header line of a BibTeX String entry.")
1149 (defconst bibtex-preamble-prefix
"[ \t]*@[ \t]*Preamble[ \t]*"
1150 "Regexp matching the prefix part of a preamble.")
1152 (defconst bibtex-font-lock-syntactic-keywords
1153 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start
0 1) "\\)"
1154 (substring bibtex-comment-start
1) "\\>")
1157 (defvar bibtex-font-lock-keywords
1158 ;; entry type and reference key
1159 `((,bibtex-any-entry-maybe-empty-head
1160 (,bibtex-type-in-head font-lock-function-name-face
)
1161 (,bibtex-key-in-head font-lock-constant-face nil t
))
1162 ;; optional field names (treated as comments)
1163 (,(concat "^[ \t]*\\(OPT" bibtex-field-name
"\\)[ \t]*=")
1164 1 font-lock-comment-face
)
1166 (,(concat "^[ \t]*\\(" bibtex-field-name
"\\)[ \t]*=")
1167 1 font-lock-variable-name-face
)
1169 bibtex-font-lock-url bibtex-font-lock-crossref
)
1170 "*Default expressions to highlight in BibTeX mode.")
1172 (defvar bibtex-font-lock-url-regexp
1173 ;; Assume that field names begin at the beginning of a line.
1175 (regexp-opt (delete-dups (mapcar 'caar bibtex-generate-url-list
)) t
)
1177 "Regexp for `bibtex-font-lock-url'.")
1179 (defvar bibtex-string-empty-key nil
1180 "If non-nil, `bibtex-parse-string' accepts empty key.")
1182 (defvar bibtex-sort-entry-class-alist
1184 (dolist (class bibtex-sort-entry-class alist
)
1186 (dolist (entry class
)
1187 ;; all entry names should be downcase (for ease of comparison)
1188 (push (cons (if (stringp entry
) (downcase entry
) entry
) i
) alist
))))
1189 "Alist mapping entry types to their sorting index.
1190 Auto-generated from `bibtex-sort-entry-class'.
1191 Used when `bibtex-maintain-sorted-entries' is `entry-class'.")
1194 ;; Support for hideshow minor mode
1195 (defun bibtex-hs-forward-sexp (arg)
1196 "Replacement for `forward-sexp' to be used by `hs-minor-mode'.
1198 (if (looking-at "@\\S(*\\s(")
1199 (goto-char (1- (match-end 0))))
1203 'hs-special-modes-alist
1204 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil
))
1207 (defun bibtex-parse-association (parse-lhs parse-rhs
)
1208 "Parse a string of the format <left-hand-side = right-hand-side>.
1209 The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
1210 substrings. These functions are expected to return nil if parsing is not
1211 successful. If the returned values of both functions are non-nil,
1212 return a cons pair of these values. Do not move point."
1215 (let ((left (funcall parse-lhs
))
1218 (looking-at "[ \t\n]*=[ \t\n]*")
1219 (goto-char (match-end 0))
1220 (setq right
(funcall parse-rhs
)))
1221 (cons left right
))))))
1223 (defun bibtex-parse-field-name ()
1224 "Parse the name part of a BibTeX field.
1225 If the field name is found, return a triple consisting of the position of the
1226 very first character of the match, the actual starting position of the name
1227 part and end position of the match. Move point to end of field name.
1228 If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceding
1229 BibTeX field as necessary."
1230 (cond ((looking-at ",[ \t\n]*")
1231 (let ((start (point)))
1232 (goto-char (match-end 0))
1233 (when (looking-at bibtex-field-name
)
1234 (goto-char (match-end 0))
1235 (list start
(match-beginning 0) (match-end 0)))))
1236 ;; Maybe add a missing comma.
1237 ((and bibtex-autoadd-commas
1238 (looking-at (concat "[ \t\n]*\\(?:" bibtex-field-name
1240 (skip-chars-backward " \t\n")
1241 ;; It can be confusing if non-editing commands try to
1242 ;; modify the buffer.
1243 (if buffer-read-only
1244 (error "Comma missing at buffer position %s" (point)))
1248 (bibtex-parse-field-name))))
1250 (defconst bibtex-braced-string-syntax-table
1251 (let ((st (make-syntax-table)))
1252 (modify-syntax-entry ?\
{ "(}" st
)
1253 (modify-syntax-entry ?\
} "){" st
)
1254 (modify-syntax-entry ?\
[ "." st
)
1255 (modify-syntax-entry ?\
] "." st
)
1256 (modify-syntax-entry ?\
( "." st
)
1257 (modify-syntax-entry ?\
) "." st
)
1258 (modify-syntax-entry ?
\\ "." st
)
1259 (modify-syntax-entry ?
\" "." st
)
1261 "Syntax-table to parse matched braces.")
1263 (defconst bibtex-quoted-string-syntax-table
1264 (let ((st (make-syntax-table)))
1265 (modify-syntax-entry ?
\\ "\\" st
)
1266 (modify-syntax-entry ?
\" "\"" st
)
1268 "Syntax-table to parse matched quotes.")
1270 (defun bibtex-parse-field-string ()
1271 "Parse a BibTeX field string enclosed by braces or quotes.
1272 If a syntactically correct string is found, a pair containing the start and
1273 end position of the field string is returned, nil otherwise.
1276 (or (and (eq (following-char) ?
\")
1278 (with-syntax-table bibtex-quoted-string-syntax-table
1281 (and (eq (following-char) ?\
{)
1283 (with-syntax-table bibtex-braced-string-syntax-table
1287 (cons (point) end-point
))))
1289 (defun bibtex-parse-field-text ()
1290 "Parse the text part of a BibTeX field.
1291 The text part is either a string, or an empty string, or a constant followed
1292 by one or more <# (string|constant)> pairs. If a syntactically correct text
1293 is found, a pair containing the start and end position of the text is
1294 returned, nil otherwise. Move point to end of field text."
1295 (let ((starting-point (point))
1296 end-point failure boundaries
)
1297 (while (not (or end-point failure
))
1298 (cond ((looking-at bibtex-field-const
)
1299 (goto-char (match-end 0)))
1300 ((setq boundaries
(bibtex-parse-field-string))
1301 (goto-char (cdr boundaries
)))
1303 (if (looking-at "[ \t\n]*#[ \t\n]*")
1304 (goto-char (match-end 0))
1305 (setq end-point
(point))))
1306 (skip-chars-forward " \t\n")
1307 (if (and (not failure
)
1309 (list starting-point end-point
(point)))))
1311 (defun bibtex-parse-field ()
1312 "Parse the BibTeX field beginning at the position of point.
1313 If a syntactically correct field is found, return a cons pair containing
1314 the boundaries of the name and text parts of the field. Do not move point."
1315 (bibtex-parse-association 'bibtex-parse-field-name
1316 'bibtex-parse-field-text
))
1318 (defsubst bibtex-start-of-field
(bounds)
1319 (nth 0 (car bounds
)))
1320 (defsubst bibtex-start-of-name-in-field
(bounds)
1321 (nth 1 (car bounds
)))
1322 (defsubst bibtex-end-of-name-in-field
(bounds)
1323 (nth 2 (car bounds
)))
1324 (defsubst bibtex-start-of-text-in-field
(bounds)
1326 (defsubst bibtex-end-of-text-in-field
(bounds)
1328 (defsubst bibtex-end-of-field
(bounds)
1331 (defun bibtex-search-forward-field (name &optional bound
)
1332 "Search forward to find a BibTeX field of name NAME.
1333 If a syntactically correct field is found, return a pair containing
1334 the boundaries of the name and text parts of the field. The search
1335 is limited by optional arg BOUND or if nil by the end of the current
1336 entry. Do not move point."
1340 ;; If the search is bounded we need not worry we could overshoot.
1341 ;; This is indeed the case when `bibtex-search-forward-field' is
1342 ;; called many times. So we optimize this part of this function.
1343 (let ((name-part (concat ",[ \t\n]*\\(" name
"\\)[ \t\n]*=[ \t\n]*"))
1344 (case-fold-search t
) left right
)
1345 (while (and (not right
)
1346 (re-search-forward name-part bound t
))
1347 (setq left
(list (match-beginning 0) (match-beginning 1)
1349 ;; Don't worry that the field text could be past bound.
1350 right
(bibtex-parse-field-text)))
1351 (if right
(cons left right
)))
1352 (let ((regexp (concat bibtex-name-part
"\\|"
1353 bibtex-any-entry-maybe-empty-head
))
1354 (case-fold-search t
) bounds
)
1356 (if (looking-at "[ \t]*@") (goto-char (match-end 0)))
1357 (while (and (not bounds
)
1358 (re-search-forward regexp nil t
))
1359 (if (match-beginning 2)
1360 ;; We found a new entry
1363 (goto-char (match-beginning 0))
1364 (setq bounds
(bibtex-parse-field))))
1365 ;; Step through all fields so that we cannot overshoot.
1367 (goto-char (bibtex-start-of-name-in-field bounds
))
1368 (if (looking-at name
) (throw 'done bounds
))
1369 (goto-char (bibtex-end-of-field bounds
))
1370 (setq bounds
(bibtex-parse-field)))))))))
1372 (defun bibtex-search-backward-field (name &optional bound
)
1373 "Search backward to find a BibTeX field of name NAME.
1374 If a syntactically correct field is found, return a pair containing
1375 the boundaries of the name and text parts of the field. The search
1376 is limited by the optional arg BOUND. If BOUND is nil the search is
1377 limited by the beginning of the current entry. Do not move point."
1380 (let ((name-part (concat ",[ \t\n]*\\(?:" name
"\\)[ \t\n]*="))
1381 (case-fold-search t
)
1383 (unless bound
(setq bound
(save-excursion (bibtex-beginning-of-entry))))
1384 (while (and (not bounds
)
1385 (search-backward "," bound t
)
1386 (looking-at name-part
))
1387 (setq bounds
(bibtex-parse-field)))
1390 (defun bibtex-name-in-field (bounds &optional remove-opt-alt
)
1391 "Get content of name in BibTeX field defined via BOUNDS.
1392 If optional arg REMOVE-OPT-ALT is non-nil remove \"OPT\" and \"ALT\"."
1393 (let ((name (buffer-substring-no-properties
1394 (bibtex-start-of-name-in-field bounds
)
1395 (bibtex-end-of-name-in-field bounds
))))
1396 (if (and remove-opt-alt
1397 (string-match "\\`\\(OPT\\|ALT\\)" name
))
1401 (defun bibtex-text-in-field-bounds (bounds &optional content
)
1402 "Get text in BibTeX field defined via BOUNDS.
1403 If optional arg CONTENT is non-nil extract content of field
1404 by removing field delimiters and concatenating the resulting string.
1405 If `bibtex-expand-strings' is non-nil, also expand BibTeX strings."
1408 (let ((epoint (bibtex-end-of-text-in-field bounds
))
1409 content opoint temp
)
1410 (goto-char (bibtex-start-of-text-in-field bounds
))
1411 (while (< (setq opoint
(point)) epoint
)
1412 (cond ((looking-at bibtex-field-const
)
1413 (let ((mtch (match-string-no-properties 0)))
1414 (goto-char (match-end 0))
1415 (setq temp
(if bibtex-expand-strings
1416 (cdr (assoc-string mtch
(bibtex-strings) t
)))
1417 content
(concat content
(or temp mtch
)))))
1419 ((setq temp
(bibtex-parse-field-string))
1420 (setq content
(concat content
(buffer-substring-no-properties
1423 (goto-char (cdr temp
)))
1424 (t (error "Malformed text field")))
1425 (re-search-forward "\\=[ \t\n]*#[ \t\n]*" nil t
))
1427 (buffer-substring-no-properties (bibtex-start-of-text-in-field bounds
)
1428 (bibtex-end-of-text-in-field bounds
))))
1430 (defun bibtex-text-in-field (field &optional follow-crossref
)
1431 "Get content of field FIELD of current BibTeX entry.
1432 Return nil if not found.
1433 If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1436 ;; We want to jump back and forth while searching FIELD
1437 (bibtex-narrow-to-entry)
1438 (goto-char (point-min))
1439 (let ((bounds (bibtex-search-forward-field field
(point-max)))
1441 (cond (bounds (bibtex-text-in-field-bounds bounds t
))
1442 ((and follow-crossref
1443 (progn (goto-char (point-min))
1444 (setq bounds
(bibtex-search-forward-field
1445 "\\(OPT\\)?crossref" (point-max)))))
1446 (setq crossref-field
(bibtex-text-in-field-bounds bounds t
))
1448 (if (bibtex-find-crossref crossref-field
)
1449 ;; Do not pass FOLLOW-CROSSREF because we want
1450 ;; to follow crossrefs only one level of recursion.
1451 (bibtex-text-in-field field
))))))))
1453 (defun bibtex-parse-string-prefix ()
1454 "Parse the prefix part of a BibTeX string entry, including reference key.
1455 If the string prefix is found, return a triple consisting of the position of
1456 the very first character of the match, the actual starting position of the
1457 reference key and the end position of the match.
1458 If `bibtex-string-empty-key' is non-nil accept empty string key."
1459 (let ((case-fold-search t
))
1460 (if (looking-at bibtex-string-type
)
1461 (let ((start (point)))
1462 (goto-char (match-end 0))
1463 (cond ((looking-at bibtex-reference-key
)
1464 (goto-char (match-end 0))
1468 ((and bibtex-string-empty-key
1470 (skip-chars-backward " \t\n")
1471 (list start
(point) (point))))))))
1473 (defun bibtex-parse-string-postfix ()
1474 "Parse the postfix part of a BibTeX string entry, including the text.
1475 If the string postfix is found, return a triple consisting of the position of
1476 the actual starting and ending position of the text and the very last
1477 character of the string entry. Move point past BibTeX string entry."
1478 (let* ((case-fold-search t
)
1479 (bounds (bibtex-parse-field-text)))
1481 (goto-char (nth 1 bounds
))
1482 (when (looking-at "[ \t\n]*[})]")
1483 (goto-char (match-end 0))
1488 (defun bibtex-parse-string ()
1489 "Parse a BibTeX string entry beginning at the position of point.
1490 If a syntactically correct entry is found, return a cons pair containing
1491 the boundaries of the reference key and text parts of the entry.
1493 (bibtex-parse-association 'bibtex-parse-string-prefix
1494 'bibtex-parse-string-postfix
))
1496 (defun bibtex-search-forward-string ()
1497 "Search forward to find a BibTeX string entry.
1498 If a syntactically correct entry is found, a pair containing the boundaries of
1499 the reference key and text parts of the string is returned. Do not move point."
1502 (let ((case-fold-search t
)
1504 (while (and (not boundaries
)
1505 (search-forward-regexp bibtex-string-type nil t
))
1506 (goto-char (match-beginning 0))
1507 (unless (setq boundaries
(bibtex-parse-string))
1511 (defun bibtex-search-backward-string ()
1512 "Search backward to find a BibTeX string entry.
1513 If a syntactically correct entry is found, a pair containing the boundaries of
1514 the reference key and text parts of the field is returned. Do not move point."
1517 (let ((case-fold-search t
)
1519 (while (and (not boundaries
)
1520 (search-backward-regexp bibtex-string-type nil t
))
1521 (goto-char (match-beginning 0))
1522 (setq boundaries
(bibtex-parse-string)))
1525 (defun bibtex-reference-key-in-string (bounds)
1526 "Return the key part of a BibTeX string defined via BOUNDS"
1527 (buffer-substring-no-properties (nth 1 (car bounds
))
1528 (nth 2 (car bounds
))))
1530 (defun bibtex-text-in-string (bounds &optional content
)
1531 "Get text in BibTeX string field defined via BOUNDS.
1532 If optional arg CONTENT is non-nil extract content
1533 by removing field delimiters and concatenating the resulting string.
1534 If `bibtex-expand-strings' is non-nil, also expand BibTeX strings."
1535 (bibtex-text-in-field-bounds bounds content
))
1537 (defsubst bibtex-start-of-text-in-string
(bounds)
1538 (nth 0 (cdr bounds
)))
1539 (defsubst bibtex-end-of-text-in-string
(bounds)
1540 (nth 1 (cdr bounds
)))
1541 (defsubst bibtex-end-of-string
(bounds)
1542 (nth 2 (cdr bounds
)))
1544 (defsubst bibtex-type-in-head
()
1545 "Extract BibTeX type in head."
1547 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head
))
1548 (match-end bibtex-type-in-head
)))
1550 (defsubst bibtex-key-in-head
(&optional empty
)
1551 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
1552 (or (match-string-no-properties bibtex-key-in-head
)
1555 (defun bibtex-preamble-prefix (&optional delim
)
1556 "Parse the prefix part of a BibTeX Preamble.
1557 Point must be at beginning of prefix part. If prefix is found move point
1558 to its end and return position of point. If optional arg DELIM is non-nil,
1559 move past the opening delimiter. If no preamble is found return nil."
1560 (let ((case-fold-search t
))
1561 (re-search-forward (concat "\\=" bibtex-preamble-prefix
1562 (if delim
"[({][ \t\n]*")) nil t
)))
1566 (defsubst bibtex-string
= (str1 str2
)
1567 "Return t if STR1 and STR2 are equal, ignoring case."
1568 (eq t
(compare-strings str1
0 nil str2
0 nil t
)))
1570 (defun bibtex-delete-whitespace ()
1571 "Delete all whitespace starting at point."
1572 (if (looking-at "[ \t\n]+")
1573 (delete-region (point) (match-end 0))))
1575 (defun bibtex-current-line ()
1576 "Compute line number of point regardless whether the buffer is narrowed."
1577 (+ (count-lines 1 (point))
1580 (defun bibtex-skip-to-valid-entry (&optional backward
)
1581 "Move point to beginning of the next valid BibTeX entry.
1582 Do not move if we are already at beginning of a valid BibTeX entry.
1583 With optional argument BACKWARD non-nil, move backward to
1584 beginning of previous valid one. A valid entry is a syntactical correct one
1585 with type contained in `bibtex-entry-field-alist' or, if
1586 `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
1587 entry. Return buffer position of beginning and end of entry if a valid
1588 entry is found, nil otherwise."
1590 (let ((case-fold-search t
)
1593 ;; Loop till we look at a valid entry.
1594 (while (not (or found
(if backward
(bobp) (eobp))))
1597 (cond ((or (and (looking-at bibtex-entry-type-whitespace
)
1598 (setq found
(bibtex-search-entry nil nil t
))
1599 (equal (match-beginning 0) pnt
))
1600 (and (not bibtex-sort-ignore-string-entries
)
1601 (setq bounds
(bibtex-parse-string))
1602 (setq found
(cons (bibtex-start-of-field bounds
)
1603 (bibtex-end-of-string bounds
)))))
1605 (backward (re-search-backward "^[ \t]*@" nil
'move
))
1606 (t (re-search-forward "\\=[ \t]*@" nil t
) ;; don't be stuck
1607 (if (re-search-forward "^[ \t]*@" nil
'move
)
1608 (goto-char (match-beginning 0)))))))
1611 (defun bibtex-map-entries (fun)
1612 "Call FUN for each BibTeX entry in buffer (possibly narrowed).
1613 FUN is called with three arguments, the key of the entry and the buffer
1614 positions (marker) of beginning and end of entry. Point is inside the entry.
1615 If `bibtex-sort-ignore-string-entries' is non-nil, FUN is not called for
1617 (let ((case-fold-search t
)
1620 (goto-char (point-min))
1621 (while (setq found
(bibtex-skip-to-valid-entry))
1622 (looking-at bibtex-any-entry-maybe-empty-head
)
1623 (funcall fun
(bibtex-key-in-head "") (car found
) (cdr found
))
1624 (goto-char (cdr found
))))))
1626 (defun bibtex-progress-message (&optional flag interval
)
1627 "Echo a message about progress of current buffer.
1628 If FLAG is a string, the message is initialized (in this case a
1629 value for INTERVAL may be given as well (if not this is set to 5)).
1630 If FLAG is `done', the message is deinitialized.
1631 If FLAG is nil, a message is echoed if point was incremented at least
1632 `bibtex-progress-interval' percent since last message was echoed."
1633 (cond ((stringp flag
)
1634 (setq bibtex-progress-lastmes flag
1635 bibtex-progress-interval
(or interval
5)
1636 bibtex-progress-lastperc
0))
1638 (message "%s (done)" bibtex-progress-lastmes
)
1639 (setq bibtex-progress-lastmes nil
))
1641 (let* ((size (- (point-max) (point-min)))
1642 (perc (if (= size
0)
1644 (/ (* 100 (- (point) (point-min))) size
))))
1645 (when (>= perc
(+ bibtex-progress-lastperc
1646 bibtex-progress-interval
))
1647 (setq bibtex-progress-lastperc perc
)
1648 (message "%s (%d%%)" bibtex-progress-lastmes perc
))))))
1650 (defun bibtex-field-left-delimiter ()
1651 "Return a string dependent on `bibtex-field-delimiters'."
1652 (if (eq bibtex-field-delimiters
'braces
)
1656 (defun bibtex-field-right-delimiter ()
1657 "Return a string dependent on `bibtex-field-delimiters'."
1658 (if (eq bibtex-field-delimiters
'braces
)
1662 (defun bibtex-entry-left-delimiter ()
1663 "Return a string dependent on `bibtex-entry-delimiters'."
1664 (if (eq bibtex-entry-delimiters
'braces
)
1668 (defun bibtex-entry-right-delimiter ()
1669 "Return a string dependent on `bibtex-entry-delimiters'."
1670 (if (eq bibtex-entry-delimiters
'braces
)
1674 (defun bibtex-search-entry (empty-head &optional bound noerror backward
)
1675 "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t).
1676 BOUND and NOERROR are exactly as in `re-search-forward'. If BACKWARD
1677 is non-nil, search in reverse direction. Move point past the closing
1678 delimiter (at the beginning of entry if BACKWARD is non-nil).
1679 Return a cons pair with buffer positions of beginning and end of entry.
1680 After a call to this function `match-data' corresponds to the head part
1681 of the entry, see regexp `bibtex-entry-head'.
1682 Ignore @String and @Preamble entries."
1684 (entry-head-re (if empty-head
1685 bibtex-entry-maybe-empty-head
1686 bibtex-entry-head
)))
1689 (while (and (not found
)
1690 (re-search-backward entry-head-re bound noerror
))
1691 (setq found
(bibtex-search-entry empty-head pnt t
)))
1693 (goto-char (match-beginning 0))
1695 ((not noerror
) ;; yell
1696 (error "Backward search of BibTeX entry failed"))
1697 (t (if (eq noerror t
) (goto-char pnt
)) ;; don't move
1700 (unless bound
(setq bound
(point-max)))
1701 (while (and (not found
)
1702 (re-search-forward entry-head-re bound noerror
))
1706 (goto-char (match-end bibtex-type-in-head
))
1707 (looking-at "[ \t]*("))
1708 ",?[ \t\n]*)" ;; entry opened with `('
1709 ",?[ \t\n]*}")) ;; entry opened with `{'
1711 (skip-chars-forward " \t\n" bound
)
1712 ;; loop over all BibTeX fields
1713 (while (and (setq bounds
(bibtex-parse-field))
1714 (<= (bibtex-end-of-field bounds
) bound
))
1715 (goto-char (bibtex-end-of-field bounds
)))
1716 ;; This matches the infix* part.
1717 (when (and (looking-at entry-closer
)
1718 (<= (match-end 0) bound
))
1719 (goto-char (match-end 0))
1722 (cons (match-beginning 0) (point)))
1723 ((not noerror
) ;; yell
1724 (error "Search of BibTeX entry failed"))
1725 (t (if (eq noerror t
) (goto-char pnt
)) ;; don't move
1728 (defun bibtex-flash-head ()
1729 "Flash at BibTeX entry head before point, if exists."
1730 (let ((case-fold-search t
)
1734 (bibtex-beginning-of-entry)
1735 (when (and (looking-at bibtex-any-entry-maybe-empty-head
)
1737 (goto-char (match-beginning bibtex-type-in-head
))
1738 (setq flash
(match-end bibtex-key-in-head
))
1739 (if (pos-visible-in-window-p (point))
1742 (buffer-substring (point) flash
)))))))
1744 (defun bibtex-make-optional-field (field)
1745 "Make an optional field named FIELD in current BibTeX entry."
1747 (bibtex-make-field (cons (concat "OPT" (car field
)) (cdr field
)))
1748 (bibtex-make-field (concat "OPT" field
))))
1750 (defun bibtex-move-outside-of-entry ()
1751 "Make sure point is outside of a BibTeX entry."
1752 (let ((orig-point (point)))
1753 (bibtex-end-of-entry)
1754 (when (< (point) orig-point
)
1755 ;; We moved backward, so we weren't inside an entry to begin with.
1756 ;; Leave point at the beginning of a line, and preferably
1757 ;; at the beginning of a paragraph.
1758 (goto-char orig-point
)
1759 (beginning-of-line 1)
1760 (unless (= ?
\n (char-before (1- (point))))
1761 (re-search-forward "^[ \t]*[@\n]" nil
'move
)
1763 (skip-chars-forward " \t\n")))
1765 (defun bibtex-beginning-of-first-entry ()
1766 "Go to beginning of line of first BibTeX entry in buffer.
1767 If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
1768 are ignored. Return point"
1769 (goto-char (point-min))
1770 (bibtex-skip-to-valid-entry)
1773 (defun bibtex-inside-field ()
1774 "Try to avoid point being at end of a BibTeX field."
1776 (skip-chars-backward " \t")
1777 (if (= (preceding-char) ?
,)
1779 (if (or (= (preceding-char) ?
})
1780 (= (preceding-char) ?
\"))
1783 (defun bibtex-enclosing-field (&optional noerr
)
1784 "Search for BibTeX field enclosing point.
1785 Unless NOERR is non-nil, signal an error if no enclosing field is found.
1786 On success return bounds, nil otherwise. Do not move point."
1787 (let ((bounds (bibtex-search-backward-field bibtex-field-name
)))
1789 (<= (bibtex-start-of-field bounds
) (point))
1790 (>= (bibtex-end-of-field bounds
) (point)))
1793 (error "Can't find enclosing BibTeX field")))))
1795 (defun bibtex-enclosing-entry-maybe-empty-head ()
1796 "Search for BibTeX entry enclosing point. Move point to end of entry.
1797 Beginning (but not end) of entry is given by (`match-beginning' 0)."
1798 (let ((case-fold-search t
)
1799 (old-point (point)))
1800 (unless (re-search-backward bibtex-entry-maybe-empty-head nil t
)
1801 (goto-char old-point
)
1802 (error "Can't find beginning of enclosing BibTeX entry"))
1803 (goto-char (match-beginning bibtex-type-in-head
))
1804 (unless (bibtex-search-entry t nil t
)
1805 (goto-char old-point
)
1806 (error "Can't find end of enclosing BibTeX entry"))))
1808 (defun bibtex-insert-kill (n)
1809 "Reinsert the Nth stretch of killed BibTeX text."
1810 (if (not bibtex-last-kill-command
)
1811 (error "BibTeX kill ring is empty")
1812 (let* ((kr (if (eq bibtex-last-kill-command
'field
)
1813 'bibtex-field-kill-ring
1814 'bibtex-entry-kill-ring
))
1815 (kryp (if (eq bibtex-last-kill-command
'field
)
1816 'bibtex-field-kill-ring-yank-pointer
1817 'bibtex-entry-kill-ring-yank-pointer
))
1818 (current (car (set kryp
(nthcdr (mod (- n
(length (eval kryp
)))
1821 (if (eq bibtex-last-kill-command
'field
)
1824 (if (looking-at "[}\"]")
1827 (message "Mark set")
1828 (bibtex-make-field current t
))
1829 (unless (eobp) (bibtex-beginning-of-entry))
1831 (message "Mark set")
1832 (insert current
)))))
1834 (defun bibtex-format-entry ()
1835 "Helper function for `bibtex-clean-entry'.
1836 Formats current entry according to variable `bibtex-entry-format'."
1839 (bibtex-narrow-to-entry)
1840 (let ((case-fold-search t
)
1841 (format (if (eq bibtex-entry-format t
)
1842 '(realign opts-or-alts required-fields
1844 last-comma page-dashes delimiters
1845 unify-case inherit-booktitle
)
1846 bibtex-entry-format
))
1847 crossref-key bounds alternatives-there non-empty-alternative
1848 entry-list req-field-list field-list
)
1850 ;; identify entry type
1851 (goto-char (point-min))
1852 (re-search-forward bibtex-entry-type
)
1853 (let ((beg-type (1+ (match-beginning 0)))
1854 (end-type (match-end 0)))
1855 (setq entry-list
(assoc-string (buffer-substring-no-properties
1857 bibtex-entry-field-alist
1860 ;; unify case of entry name
1861 (when (memq 'unify-case format
)
1862 (delete-region beg-type end-type
)
1863 (insert (car entry-list
)))
1865 ;; update left entry delimiter
1866 (when (memq 'delimiters format
)
1867 (goto-char end-type
)
1868 (skip-chars-forward " \t\n")
1870 (insert (bibtex-entry-left-delimiter))))
1872 ;; determine if entry has crossref field and if at least
1873 ;; one alternative is non-empty
1874 (goto-char (point-min))
1875 (let* ((fields-alist (bibtex-parse-entry t
))
1876 (field (assoc-string "crossref" fields-alist t
)))
1877 (setq crossref-key
(and field
1878 (not (equal "" (cdr field
)))
1880 req-field-list
(if crossref-key
1881 (nth 0 (nth 2 entry-list
)) ; crossref part
1882 (nth 0 (nth 1 entry-list
)))) ; required part
1884 (dolist (rfield req-field-list
)
1885 (when (nth 3 rfield
) ; we should have an alternative
1886 (setq alternatives-there t
1887 field
(assoc-string (car rfield
) fields-alist t
))
1889 (not (equal "" (cdr field
))))
1890 (cond ((not non-empty-alternative
)
1891 (setq non-empty-alternative t
))
1892 ((memq 'required-fields format
)
1893 (error "More than one non-empty alternative")))))))
1895 (if (and alternatives-there
1896 (not non-empty-alternative
)
1897 (memq 'required-fields format
))
1898 (error "All alternatives are empty"))
1900 ;; process all fields
1901 (goto-char (point-min))
1902 (while (setq bounds
(bibtex-search-forward-field
1903 bibtex-field-name
(point-max)))
1904 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds
)))
1905 (end-field (copy-marker (bibtex-end-of-field bounds
) t
))
1906 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds
)))
1907 (end-name (copy-marker (bibtex-end-of-name-in-field bounds
)))
1908 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds
)))
1909 (end-text (copy-marker (bibtex-end-of-text-in-field bounds
) t
))
1910 (opt-alt (string-match "OPT\\|ALT"
1911 (buffer-substring-no-properties
1912 beg-name
(+ beg-name
3))))
1913 (field-name (buffer-substring-no-properties
1914 (if opt-alt
(+ beg-name
3) beg-name
) end-name
))
1915 (empty-field (equal "" (bibtex-text-in-field-bounds bounds t
)))
1918 ;; We have more elegant high-level functions for several
1919 ;; tasks done by bibtex-format-entry. However, they contain
1920 ;; quite some redundancy compared with what we need to do
1921 ;; anyway. So for speed-up we avoid using them.
1923 (if (memq 'opts-or-alts format
)
1924 (cond ((and empty-field
1926 (let ((field (assoc-string
1927 field-name req-field-list t
)))
1928 (or (not field
) ; OPT field
1929 (nth 3 field
))))) ; ALT field
1930 ;; Either it is an empty ALT field. Then we have checked
1931 ;; already that we have one non-empty alternative. Or it
1932 ;; is an empty OPT field that we do not miss anyway.
1933 ;; So we can safely delete this field.
1934 (delete-region beg-field end-field
)
1936 ;; otherwise: not empty, delete "OPT" or "ALT"
1938 (goto-char beg-name
)
1942 (push field-name field-list
)
1944 ;; remove delimiters from purely numerical fields
1945 (when (and (memq 'numerical-fields format
)
1946 (progn (goto-char beg-text
)
1947 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1948 (goto-char end-text
)
1950 (goto-char beg-text
)
1953 ;; update delimiters
1954 (when (memq 'delimiters format
)
1955 (goto-char beg-text
)
1956 (when (looking-at "[{\"]")
1958 (insert (bibtex-field-left-delimiter)))
1959 (goto-char (1- (marker-position end-text
)))
1960 (when (looking-at "[}\"]")
1962 (insert (bibtex-field-right-delimiter))))
1964 ;; update page dashes
1965 (if (and (memq 'page-dashes format
)
1966 (bibtex-string= field-name
"pages")
1967 (progn (goto-char beg-text
)
1969 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1970 (replace-match "\\1-\\2"))
1972 ;; use book title of crossref'd entry
1973 (if (and (memq 'inherit-booktitle format
)
1975 (bibtex-string= field-name
"booktitle")
1977 (let ((title (save-excursion
1980 (if (bibtex-find-entry crossref-key t
)
1981 (bibtex-text-in-field "title"))))))
1983 (setq empty-field nil
)
1984 (goto-char (1+ beg-text
))
1987 ;; Use booktitle to set a missing title.
1988 (if (and empty-field
1989 (bibtex-string= field-name
"title"))
1990 (let ((booktitle (bibtex-text-in-field "booktitle")))
1992 (setq empty-field nil
)
1993 (goto-char (1+ beg-text
))
1994 (insert booktitle
))))
1996 ;; if empty field, complain
1997 (if (and empty-field
1998 (memq 'required-fields format
)
1999 (assoc-string field-name req-field-list t
))
2000 (error "Mandatory field `%s' is empty" field-name
))
2002 ;; unify case of field name
2003 (if (memq 'unify-case format
)
2004 (let ((fname (car (assoc-string
2006 (append (nth 0 (nth 1 entry-list
))
2007 (nth 1 (nth 1 entry-list
))
2008 bibtex-user-optional-fields
)
2012 (delete-region beg-name end-name
)
2013 (goto-char beg-name
)
2015 ;; there are no rules we could follow
2016 (downcase-region beg-name end-name
))))
2019 (goto-char end-field
))))
2021 ;; check whether all required fields are present
2022 (if (memq 'required-fields format
)
2023 (let ((found 0) altlist
)
2024 (dolist (fname req-field-list
)
2026 (push (car fname
) altlist
))
2027 (unless (or (member (car fname
) field-list
)
2029 (error "Mandatory field `%s' is missing" (car fname
))))
2031 (dolist (fname altlist
)
2032 (if (member fname field-list
)
2033 (setq found
(1+ found
))))
2035 (error "Alternative mandatory field `%s' is missing"
2038 (error "Alternative fields `%s' are defined %s times"
2042 (if (looking-at (bibtex-field-right-delimiter))
2045 ;; update comma after last field
2046 (if (memq 'last-comma format
)
2047 (cond ((and bibtex-comma-after-last-field
2048 (not (looking-at ",")))
2050 ((and (not bibtex-comma-after-last-field
)
2054 ;; update right entry delimiter
2055 (if (looking-at ",")
2057 (when (memq 'delimiters format
)
2058 (skip-chars-forward " \t\n")
2060 (insert (bibtex-entry-right-delimiter)))
2063 (if (memq 'realign format
)
2064 (bibtex-fill-entry))))))
2067 (defun bibtex-autokey-abbrev (string len
)
2068 "Return an abbreviation of STRING with at least LEN characters.
2069 If LEN is positive the abbreviation is terminated only after a consonant
2070 or at the word end. If LEN is negative the abbreviation is strictly
2071 enforced using abs (LEN) characters. If LEN is not a number, STRING
2072 is returned unchanged."
2073 (cond ((or (not (numberp len
))
2074 (<= (length string
) (abs len
)))
2079 (substring string
0 (abs len
)))
2080 (t (let* ((case-fold-search t
)
2081 (abort-char (string-match "[^aeiou]" string
(1- len
))))
2083 (substring string
0 (1+ abort-char
))
2086 (defun bibtex-autokey-get-field (field &optional change-list
)
2087 "Get content of BibTeX field FIELD. Return empty string if not found.
2088 Optional arg CHANGE-LIST is a list of substitution patterns that is
2089 applied to the content of FIELD. It is an alist with pairs
2090 \(OLD-REGEXP . NEW-STRING\)."
2091 (let* ((bibtex-expand-strings bibtex-autokey-expand-strings
)
2092 (content (bibtex-text-in-field field bibtex-autokey-use-crossref
))
2094 (unless content
(setq content
""))
2095 (dolist (pattern change-list content
)
2096 (setq content
(replace-regexp-in-string (car pattern
)
2100 (defun bibtex-autokey-get-names ()
2101 "Get contents of the name field of the current entry.
2102 Do some modifications based on `bibtex-autokey-name-change-strings'.
2103 Return the names as a concatenated string obeying `bibtex-autokey-names'
2104 and `bibtex-autokey-names-stretch'."
2105 (let ((names (bibtex-autokey-get-field "author\\|editor"
2106 bibtex-autokey-name-change-strings
)))
2107 ;; Some entries do not have a name field.
2108 (if (string= "" names
)
2110 (let* ((case-fold-search t
)
2111 (name-list (mapcar 'bibtex-autokey-demangle-name
2112 (split-string names
"[ \t\n]+and[ \t\n]+")))
2114 (unless (or (not (numberp bibtex-autokey-names
))
2115 (<= (length name-list
)
2116 (+ bibtex-autokey-names
2117 bibtex-autokey-names-stretch
)))
2118 ;; Take bibtex-autokey-names elements from beginning of name-list
2119 (setq name-list
(nreverse (nthcdr (- (length name-list
)
2120 bibtex-autokey-names
)
2121 (nreverse name-list
)))
2122 additional-names bibtex-autokey-additional-names
))
2123 (concat (mapconcat 'identity name-list
2124 bibtex-autokey-name-separator
)
2125 additional-names
)))))
2127 (defun bibtex-autokey-demangle-name (fullname)
2128 "Get the last part from a well-formed FULLNAME and perform abbreviations."
2129 (let* (case-fold-search
2130 (name (cond ((string-match "\\([[:upper:]][^, ]*\\)[^,]*," fullname
)
2131 ;; Name is of the form "von Last, First" or
2132 ;; "von Last, Jr, First"
2133 ;; --> Take the first capital part before the comma
2134 (match-string 1 fullname
))
2135 ((string-match "\\([^, ]*\\)," fullname
)
2136 ;; Strange name: we have a comma, but nothing capital
2137 ;; So we accept even lowercase names
2138 (match-string 1 fullname
))
2139 ((string-match "\\(\\<[[:lower:]][^ ]* +\\)+\\([[:upper:]][^ ]*\\)"
2141 ;; name is of the form "First von Last", "von Last",
2142 ;; "First von von Last", or "d'Last"
2143 ;; --> take the first capital part after the "von" parts
2144 (match-string 2 fullname
))
2145 ((string-match "\\([^ ]+\\) *\\'" fullname
)
2146 ;; name is of the form "First Middle Last" or "Last"
2147 ;; --> take the last token
2148 (match-string 1 fullname
))
2149 (t (error "Name `%s' is incorrectly formed" fullname
)))))
2150 (funcall bibtex-autokey-name-case-convert
2151 (bibtex-autokey-abbrev name bibtex-autokey-name-length
))))
2153 (defun bibtex-autokey-get-year ()
2154 "Return year field contents as a string obeying `bibtex-autokey-year-length'."
2155 (let ((yearfield (bibtex-autokey-get-field "year")))
2156 (substring yearfield
(max 0 (- (length yearfield
)
2157 bibtex-autokey-year-length
)))))
2159 (defun bibtex-autokey-get-title ()
2160 "Get title field contents up to a terminator.
2161 Return the result as a string"
2162 (let ((case-fold-search t
)
2164 (bibtex-autokey-get-field "title"
2165 bibtex-autokey-titleword-change-strings
)))
2166 ;; ignore everything past a terminator
2167 (if (string-match bibtex-autokey-title-terminators titlestring
)
2168 (setq titlestring
(substring titlestring
0 (match-beginning 0))))
2169 ;; gather words from titlestring into a list. Ignore
2170 ;; specific words and use only a specific amount of words.
2172 titlewords titlewords-extra word
)
2173 (while (and (or (not (numberp bibtex-autokey-titlewords
))
2174 (< counter
(+ bibtex-autokey-titlewords
2175 bibtex-autokey-titlewords-stretch
)))
2176 (string-match "\\b\\w+" titlestring
))
2177 (setq word
(match-string 0 titlestring
)
2178 titlestring
(substring titlestring
(match-end 0)))
2179 ;; Ignore words matched by one of the elements of
2180 ;; bibtex-autokey-titleword-ignore
2181 (unless (let ((lst bibtex-autokey-titleword-ignore
))
2183 (not (string-match (concat "\\`\\(?:" (car lst
)
2185 (setq lst
(cdr lst
)))
2187 (setq counter
(1+ counter
))
2188 (if (or (not (numberp bibtex-autokey-titlewords
))
2189 (<= counter bibtex-autokey-titlewords
))
2190 (push word titlewords
)
2191 (push word titlewords-extra
))))
2192 ;; Obey bibtex-autokey-titlewords-stretch:
2193 ;; If by now we have processed all words in titlestring, we include
2194 ;; titlewords-extra in titlewords. Otherwise, we ignore titlewords-extra.
2195 (unless (string-match "\\b\\w+" titlestring
)
2196 (setq titlewords
(append titlewords-extra titlewords
)))
2197 (mapconcat 'bibtex-autokey-demangle-title
(nreverse titlewords
)
2198 bibtex-autokey-titleword-separator
))))
2200 (defun bibtex-autokey-demangle-title (titleword)
2201 "Do some abbreviations on TITLEWORD.
2202 The rules are defined in `bibtex-autokey-titleword-abbrevs'
2203 and `bibtex-autokey-titleword-length'."
2204 (let ((case-fold-search t
)
2205 (alist bibtex-autokey-titleword-abbrevs
))
2207 (not (string-match (concat "\\`\\(?:" (caar alist
) "\\)\\'")
2209 (setq alist
(cdr alist
)))
2212 (funcall bibtex-autokey-titleword-case-convert
2213 (bibtex-autokey-abbrev titleword bibtex-autokey-titleword-length
)))))
2215 (defun bibtex-generate-autokey ()
2216 "Generate automatically a key for a BibTeX entry.
2217 Use the author/editor, the year and the title field.
2218 The algorithm works as follows.
2221 1. Use the author or editor field to generate the name part of the key.
2222 Expand BibTeX strings if `bibtex-autokey-expand-strings' is non-nil.
2223 2. Change the content of the name field according to
2224 `bibtex-autokey-name-change-strings' (see there for further detail).
2225 3. Use the first `bibtex-autokey-names' names in the name field. If there
2226 are up to `bibtex-autokey-names' + `bibtex-autokey-names-stretch' names,
2228 4. Use only the last names to form the name part. From these last names,
2229 take at least `bibtex-autokey-name-length' characters (truncate only
2230 after a consonant or at a word end).
2231 5. Convert all last names using the function
2232 `bibtex-autokey-name-case-convert'.
2233 6. Build the name part of the key by concatenating all abbreviated last
2234 names with the string `bibtex-autokey-name-separator' between any two.
2235 If there are more names in the name field than names used in the name
2236 part, append the string `bibtex-autokey-additional-names'.
2239 1. Build the year part of the key by truncating the content of the year
2240 field to the rightmost `bibtex-autokey-year-length' digits (useful
2241 values are 2 and 4).
2242 2. If the year field (or any other field required to generate the key)
2243 is absent, but the entry has a valid crossref field and
2244 `bibtex-autokey-use-crossref' is non-nil, use the field of the
2245 crossreferenced entry instead.
2248 1. Change the content of the title field according to
2249 `bibtex-autokey-titleword-change-strings' (see there for further detail).
2250 2. Truncate the title before the first match of
2251 `bibtex-autokey-title-terminators' and delete those words which appear
2252 in `bibtex-autokey-titleword-ignore'. Build the title part using the
2253 first `bibtex-autokey-titlewords' words from this truncated title.
2254 If the truncated title ends after up to `bibtex-autokey-titlewords' +
2255 `bibtex-autokey-titlewords-stretch' words, use all words from the
2257 3. For every title word that appears in `bibtex-autokey-titleword-abbrevs'
2258 use the corresponding abbreviation (see documentation of this variable
2259 for further detail).
2260 4. From every title word not generated by an abbreviation, take at least
2261 `bibtex-autokey-titleword-length' characters (truncate only after
2262 a consonant or at a word end).
2263 5. Convert all title words using the function
2264 `bibtex-autokey-titleword-case-convert'.
2265 6. Build the title part by concatenating all abbreviated title words with
2266 the string `bibtex-autokey-titleword-separator' between any two.
2268 Concatenate the key:
2269 1. Concatenate `bibtex-autokey-prefix-string', the name part, the year
2270 part and the title part. If the name part and the year part are both
2271 non-empty insert `bibtex-autokey-name-year-separator' between the two.
2272 If the title part and the year (or name) part are non-empty, insert
2273 `bibtex-autokey-year-title-separator' between the two.
2274 2. If `bibtex-autokey-before-presentation-function' is non-nil, it must be
2275 a function taking one argument. Call this function with the generated
2276 key as the argument. Use the return value of this function (a string)
2278 3. If `bibtex-autokey-edit-before-use' is non-nil, present the key in the
2279 minibuffer to the user for editing. Insert the key given by the user."
2280 (let* ((names (bibtex-autokey-get-names))
2281 (year (bibtex-autokey-get-year))
2282 (title (bibtex-autokey-get-title))
2283 (autokey (concat bibtex-autokey-prefix-string
2285 (unless (or (equal names
"")
2287 bibtex-autokey-name-year-separator
)
2289 (unless (or (and (equal names
"")
2292 bibtex-autokey-year-title-separator
)
2294 (if bibtex-autokey-before-presentation-function
2295 (funcall bibtex-autokey-before-presentation-function autokey
)
2299 (defun bibtex-global-key-alist ()
2300 "Return global key alist based on `bibtex-files'."
2303 (mapcar (lambda (buf)
2304 (with-current-buffer buf bibtex-reference-keys
))
2305 (bibtex-files-expand t
)))
2306 bibtex-reference-keys
))
2308 (defun bibtex-read-key (prompt &optional key global
)
2309 "Read BibTeX key from minibuffer using PROMPT and default KEY.
2310 If optional arg GLOBAL is non-nil, completion is based on the keys in
2311 `bibtex-reference-keys' of `bibtex-files',"
2312 (let (completion-ignore-case)
2313 (completing-read prompt
(if global
(bibtex-global-key-alist)
2314 bibtex-reference-keys
)
2315 nil nil key
'bibtex-key-history
)))
2317 (defun bibtex-read-string-key (&optional key
)
2318 "Read BibTeX string key from minibuffer using default KEY."
2319 (let ((completion-ignore-case t
))
2320 (completing-read "String key: " bibtex-strings
2321 nil nil key
'bibtex-key-history
)))
2323 (defun bibtex-parse-keys (&optional abortable verbose
)
2324 "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2325 Find both entry keys and crossref entries. If ABORTABLE is non-nil abort
2326 on user input. If VERBOSE is non-nil give messages about progress.
2327 Return alist of keys if parsing was completed, `aborted' otherwise.
2328 If `bibtex-parse-keys-fast' is non-nil, use fast but simplified algorithm
2329 for parsing BibTeX keys. If parsing fails, try to set this variable to nil."
2330 (let (ref-keys crossref-keys
)
2334 (bibtex-progress-message
2335 (concat (buffer-name) ": parsing reference keys")))
2337 (goto-char (point-min))
2338 (if bibtex-parse-keys-fast
2339 (let ((case-fold-search t
)
2340 (re (concat bibtex-entry-head
"\\|"
2341 ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*"
2342 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]")))
2343 (while (re-search-forward re nil t
)
2344 (if (and abortable
(input-pending-p))
2345 ;; user has aborted by typing a key --> return `aborted'
2346 (throw 'userkey
'aborted
))
2347 (cond ((match-end 3)
2348 ;; This is a crossref.
2349 (let ((key (buffer-substring-no-properties
2350 (1+ (match-beginning 3)) (1- (match-end 3)))))
2351 (unless (assoc key crossref-keys
)
2352 (push (list key
) crossref-keys
))))
2353 ;; only keys of known entries
2354 ((assoc-string (bibtex-type-in-head)
2355 bibtex-entry-field-alist t
)
2356 ;; This is an entry.
2357 (let ((key (bibtex-key-in-head)))
2358 (unless (assoc key ref-keys
)
2359 (push (cons key t
) ref-keys
)))))))
2361 (let (;; ignore @String entries because they are handled
2362 ;; separately by bibtex-parse-strings
2363 (bibtex-sort-ignore-string-entries t
)
2366 (lambda (key beg end
)
2369 ;; user has aborted by typing a key --> return `aborted'
2370 (throw 'userkey
'aborted
))
2371 (if verbose
(bibtex-progress-message))
2372 (unless (assoc key ref-keys
)
2373 (push (cons key t
) ref-keys
))
2374 (if (and (setq bounds
(bibtex-search-forward-field "crossref" end
))
2375 (setq key
(bibtex-text-in-field-bounds bounds t
))
2376 (not (assoc key crossref-keys
)))
2377 (push (list key
) crossref-keys
))))))
2379 (dolist (key crossref-keys
)
2380 (unless (assoc (car key
) ref-keys
) (push key ref-keys
)))
2382 (bibtex-progress-message 'done
))
2383 ;; successful operation --> return `bibtex-reference-keys'
2384 (setq bibtex-reference-keys ref-keys
))))))
2386 (defun bibtex-parse-strings (&optional add abortable
)
2387 "Set `bibtex-strings' to the string definitions in the whole buffer.
2388 If ADD is non-nil add the new strings to `bibtex-strings' instead of
2389 simply resetting it. If ADD is an alist of strings, also add ADD to
2390 `bibtex-strings'. If ABORTABLE is non-nil abort on user input.
2391 Return alist of strings if parsing was completed, `aborted' otherwise."
2394 (goto-char (point-min))
2395 (let ((strings (if (and add
2396 (listp bibtex-strings
))
2400 (dolist (string add
)
2401 (unless (assoc-string (car string
) strings t
)
2402 (push string strings
))))
2404 (while (setq bounds
(bibtex-search-forward-string))
2407 ;; user has aborted by typing a key --> return `aborted'
2408 (throw 'userkey
'aborted
))
2409 (setq key
(bibtex-reference-key-in-string bounds
))
2410 (unless (assoc-string key strings t
)
2411 (push (cons key
(bibtex-text-in-string bounds t
))
2413 (goto-char (bibtex-end-of-text-in-string bounds
)))
2414 ;; successful operation --> return `bibtex-strings'
2415 (setq bibtex-strings strings
))))))
2417 (defun bibtex-strings ()
2418 "Return `bibtex-strings'. Initialize this variable if necessary."
2419 (if (listp bibtex-strings
) bibtex-strings
2420 (bibtex-parse-strings (bibtex-string-files-init))))
2422 (defun bibtex-string-files-init ()
2423 "Return initialization for `bibtex-strings'.
2424 Use `bibtex-predefined-strings' and BibTeX files `bibtex-string-files'."
2426 (let ((dirlist (split-string (or bibtex-string-file-path default-directory
)
2429 string-files fullfilename compl bounds found
)
2430 ;; collect absolute file names of valid string files
2431 (dolist (filename bibtex-string-files
)
2432 (unless (string-match "\\.bib\\'" filename
)
2433 (setq filename
(concat filename
".bib")))
2435 (if (file-name-absolute-p filename
)
2436 (if (file-readable-p filename
)
2437 (push filename string-files
)
2438 (error "BibTeX strings file %s not found" filename
))
2439 (dolist (dir dirlist
)
2440 (when (file-readable-p
2441 (setq fullfilename
(expand-file-name filename dir
)))
2442 (push fullfilename string-files
)
2445 (error "File %s not in paths defined via bibtex-string-file-path"
2447 ;; parse string files
2448 (dolist (filename string-files
)
2450 (insert-file-contents filename
)
2451 (goto-char (point-min))
2452 (while (setq bounds
(bibtex-search-forward-string))
2453 (push (cons (bibtex-reference-key-in-string bounds
)
2454 (bibtex-text-in-string bounds t
))
2456 (goto-char (bibtex-end-of-string bounds
)))))
2457 (append bibtex-predefined-strings
(nreverse compl
)))))
2459 (defun bibtex-parse-buffers-stealthily ()
2460 "Parse buffer in the background during idle time.
2461 Called by `run-with-idle-timer'. Whenever Emacs has been idle
2462 for `bibtex-parse-keys-timeout' seconds, parse all BibTeX buffers
2463 which have been modified after last parsing.
2464 Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
2466 (let ((buffers (buffer-list))
2467 (strings-init (bibtex-string-files-init)))
2468 (while (and buffers
(not (input-pending-p)))
2469 (set-buffer (car buffers
))
2470 (if (and (eq major-mode
'bibtex-mode
)
2471 (not (eq (buffer-modified-tick)
2472 bibtex-buffer-last-parsed-tick
)))
2475 ;; Output no progress messages in bibtex-parse-keys
2476 ;; because when in y-or-n-p that can hide the question.
2477 (if (and (listp (bibtex-parse-keys t
))
2478 ;; update bibtex-strings
2479 (listp (bibtex-parse-strings strings-init t
)))
2481 ;; remember that parsing was successful
2482 (setq bibtex-buffer-last-parsed-tick
(buffer-modified-tick)))))
2483 (setq buffers
(cdr buffers
))))))
2485 (defun bibtex-files-expand (&optional current force
)
2486 "Return an expanded list of BibTeX buffers based on `bibtex-files'.
2487 Initialize in these buffers `bibtex-reference-keys' if not yet set.
2488 List of BibTeX buffers includes current buffer if CURRENT is non-nil.
2489 If FORCE is non-nil, (re)initialize `bibtex-reference-keys' even if
2491 (let ((file-path (split-string (or bibtex-file-path default-directory
) ":+"))
2492 file-list dir-list buffer-list
)
2493 (dolist (file bibtex-files
)
2494 (cond ((eq file
'bibtex-file-path
)
2495 (setq dir-list
(append dir-list file-path
)))
2496 ((file-accessible-directory-p file
)
2497 (push file dir-list
))
2498 ((progn (unless (string-match "\\.bib\\'" file
)
2499 (setq file
(concat file
".bib")))
2500 (file-name-absolute-p file
))
2501 (push file file-list
))
2503 (let (fullfilename found
)
2504 (dolist (dir file-path
)
2505 (when (file-readable-p
2506 (setq fullfilename
(expand-file-name file dir
)))
2507 (push fullfilename file-list
)
2510 (error "File %s not in paths defined via bibtex-file-path"
2512 (dolist (file file-list
)
2513 (unless (file-readable-p file
)
2514 (error "BibTeX file %s not found" file
)))
2516 (dolist (dir dir-list
)
2518 (append file-list
(directory-files dir t
"\\.bib\\'" t
))))
2519 (delete-dups file-list
)
2520 (dolist (file file-list
)
2521 (when (file-readable-p file
)
2522 (push (find-file-noselect file
) buffer-list
)
2523 (with-current-buffer (car buffer-list
)
2524 (if (or force
(not (listp bibtex-reference-keys
)))
2525 (bibtex-parse-keys)))))
2526 (cond ((and current
(not (memq (current-buffer) buffer-list
)))
2527 (push (current-buffer) buffer-list
)
2528 (if force
(bibtex-parse-keys)))
2529 ((and (not current
) (memq (current-buffer) buffer-list
))
2530 (setq buffer-list
(delq (current-buffer) buffer-list
))))
2533 (defun bibtex-complete-internal (completions)
2534 "Complete word fragment before point to longest prefix of COMPLETIONS.
2535 COMPLETIONS is an alist of strings. If point is not after the part
2536 of a word, all strings are listed. Return completion."
2537 (let* ((case-fold-search t
)
2538 (beg (save-excursion
2539 (re-search-backward "[ \t{\"]")
2543 (part-of-word (buffer-substring-no-properties beg end
))
2544 (completion (try-completion part-of-word completions
)))
2545 (cond ((not completion
)
2546 (error "Can't find completion for `%s'" part-of-word
))
2549 ((not (string= part-of-word completion
))
2550 (delete-region beg end
)
2554 (message "Making completion list...")
2555 (with-output-to-temp-buffer "*Completions*"
2556 (display-completion-list (all-completions part-of-word completions
)
2558 (message "Making completion list...done")
2559 ;; return value is handled by choose-completion-string-functions
2562 (defun bibtex-complete-string-cleanup (str compl
)
2563 "Cleanup after inserting string STR.
2564 Remove enclosing field delimiters for STR. Display message with
2565 expansion of STR using expansion list COMPL."
2567 (let ((abbr (cdr (if (stringp str
)
2568 (assoc-string str compl t
)))))
2569 (if abbr
(message "Abbreviation for `%s'" abbr
))
2570 (bibtex-remove-delimiters))))
2572 (defun bibtex-complete-crossref-cleanup (key)
2573 "Display summary message on entry KEY after completion of a crossref key.
2574 Use `bibtex-summary-function' to generate summary."
2576 (if (and (stringp key
)
2577 (bibtex-find-entry key t
))
2578 (message "Ref: %s" (funcall bibtex-summary-function
)))))
2580 (defun bibtex-copy-summary-as-kill ()
2581 "Push summery of current BibTeX entry to kill ring.
2582 Use `bibtex-summary-function' to generate summary."
2585 (bibtex-beginning-of-entry)
2586 (if (looking-at bibtex-entry-maybe-empty-head
)
2587 (kill-new (message "%s" (funcall bibtex-summary-function
)))
2588 (error "No entry found"))))
2590 (defun bibtex-summary ()
2591 "Return summary of current BibTeX entry.
2592 Used as default value of `bibtex-summary-function'."
2593 ;; It would be neat to customize this function. How?
2594 (if (looking-at bibtex-entry-maybe-empty-head
)
2595 (let* ((bibtex-autokey-name-case-convert 'identity
)
2596 (bibtex-autokey-name-length 'infty
)
2597 (bibtex-autokey-names 1)
2598 (bibtex-autokey-names-stretch 0)
2599 (bibtex-autokey-name-separator " ")
2600 (bibtex-autokey-additional-names " etal")
2601 (names (bibtex-autokey-get-names))
2602 (bibtex-autokey-year-length 4)
2603 (year (bibtex-autokey-get-year))
2604 (bibtex-autokey-titlewords 5)
2605 (bibtex-autokey-titlewords-stretch 2)
2606 (bibtex-autokey-titleword-case-convert 'identity
)
2607 (bibtex-autokey-titleword-length 5)
2608 (bibtex-autokey-titleword-separator " ")
2609 (title (bibtex-autokey-get-title))
2610 (journal (bibtex-autokey-get-field
2611 "journal" bibtex-autokey-transcriptions
))
2612 (volume (bibtex-autokey-get-field "volume"))
2613 (pages (bibtex-autokey-get-field "pages" '(("-.*\\'" .
"")))))
2614 (mapconcat (lambda (arg)
2615 (if (not (string= "" (cdr arg
)))
2616 (concat (car arg
) (cdr arg
))))
2617 `((" " .
,names
) (" " .
,year
) (": " .
,title
)
2618 (", " .
,journal
) (" " .
,volume
) (":" .
,pages
))
2620 (error "Entry not found")))
2622 (defun bibtex-pop (arg direction
)
2623 "Fill current field from the ARGth same field's text in DIRECTION.
2624 Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
2627 ;; parse current field
2628 (bibtex-inside-field)
2629 (let* ((case-fold-search t
)
2630 (bounds (bibtex-enclosing-field))
2631 (start-old-text (bibtex-start-of-text-in-field bounds
))
2632 (stop-old-text (bibtex-end-of-text-in-field bounds
))
2633 (field-name (bibtex-name-in-field bounds t
)))
2634 ;; if executed several times in a row, start each search where
2635 ;; the last one was finished
2636 (unless (eq last-command
'bibtex-pop
)
2637 (bibtex-enclosing-entry-maybe-empty-head)
2638 (setq bibtex-pop-previous-search-point
(match-beginning 0)
2639 bibtex-pop-next-search-point
(point)))
2640 (if (eq direction
'previous
)
2641 (goto-char bibtex-pop-previous-search-point
)
2642 (goto-char bibtex-pop-next-search-point
))
2643 ;; Now search for arg'th previous/next similar field
2644 (let (bounds failure new-text
)
2645 (while (and (not failure
)
2647 (cond ((eq direction
'previous
)
2648 (if (setq bounds
(bibtex-search-backward-field field-name
))
2649 (goto-char (bibtex-start-of-field bounds
))
2651 ((eq direction
'next
)
2652 (if (setq bounds
(bibtex-search-forward-field field-name
))
2653 (goto-char (bibtex-end-of-field bounds
))
2655 (setq arg
(- arg
1)))
2657 (error "No %s matching BibTeX field"
2658 (if (eq direction
'previous
) "previous" "next"))
2659 ;; Found a matching field. Remember boundaries.
2660 (setq bibtex-pop-previous-search-point
(bibtex-start-of-field bounds
)
2661 bibtex-pop-next-search-point
(bibtex-end-of-field bounds
)
2662 new-text
(bibtex-text-in-field-bounds bounds
))
2664 ;; Go back to where we started, delete old text, and pop new.
2665 (goto-char stop-old-text
)
2666 (delete-region start-old-text stop-old-text
)
2667 (insert new-text
)))))
2669 (setq this-command
'bibtex-pop
))
2671 (defun bibtex-beginning-of-field ()
2672 "Move point backward to beginning of field.
2673 This function uses a simple, fast algorithm assuming that the field
2674 begins at the beginning of a line. We use this function for font-locking."
2675 (let ((field-reg (concat "^[ \t]*" bibtex-field-name
"[ \t]*=")))
2677 (unless (looking-at field-reg
)
2678 (re-search-backward field-reg nil t
))))
2680 (defun bibtex-font-lock-url (bound)
2681 "Font-lock for URLs. BOUND limits the search."
2682 (let ((case-fold-search t
)
2684 field bounds start end found
)
2685 (bibtex-beginning-of-field)
2686 (while (and (not found
)
2688 (prog1 (re-search-forward bibtex-font-lock-url-regexp bound t
)
2689 (setq field
(match-string-no-properties 1)))
2690 (setq bounds
(bibtex-parse-field-text))
2692 (setq start
(car bounds
) end
(nth 1 bounds
))
2693 ;; Always ignore field delimiters
2694 (if (memq (char-before end
) '(?\
} ?
\"))
2695 (setq end
(1- end
)))
2696 (if (memq (char-after start
) '(?\
{ ?
\"))
2697 (setq start
(1+ start
)))
2699 (let ((lst bibtex-generate-url-list
) url
)
2701 (while (and (not found
)
2702 (setq url
(car (pop lst
))))
2703 (setq found
(and (bibtex-string= field
(car url
))
2704 (re-search-forward (cdr url
) end t
)
2705 (>= (match-beginning 0) pnt
)))))
2707 (if found
(bibtex-button (match-beginning 0) (match-end 0)
2708 'bibtex-url
(match-beginning 0)))
2711 (defun bibtex-font-lock-crossref (bound)
2712 "Font-lock for crossref fields. BOUND limits the search."
2713 (let ((case-fold-search t
)
2715 (crossref-reg (concat "^[ \t]*crossref[ \t]*=[ \t\n]*"
2716 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]"))
2718 (bibtex-beginning-of-field)
2719 (while (and (not found
)
2720 (re-search-forward crossref-reg bound t
))
2721 (setq start
(1+ (match-beginning 1))
2722 end
(1- (match-end 1))
2723 found
(>= start pnt
)))
2724 (if found
(bibtex-button start end
'bibtex-find-crossref
2725 (buffer-substring-no-properties start end
)
2729 (defun bibtex-button-action (button)
2730 "Call BUTTON's BibTeX function."
2731 (apply (button-get button
'bibtex-function
)
2732 (button-get button
'bibtex-args
)))
2734 (define-button-type 'bibtex-url
2735 'action
'bibtex-button-action
2736 'bibtex-function
'bibtex-url
2737 'help-echo
(purecopy "mouse-2, RET: follow URL"))
2739 (define-button-type 'bibtex-find-crossref
2740 'action
'bibtex-button-action
2741 'bibtex-function
'bibtex-find-crossref
2742 'help-echo
(purecopy "mouse-2, RET: follow crossref"))
2744 (defun bibtex-button (beg end type
&rest args
)
2745 "Make a BibTeX button from BEG to END of type TYPE in the current buffer."
2746 (make-text-button beg end
'type type
'bibtex-args args
))
2749 ;; Interactive Functions:
2752 (defun bibtex-mode ()
2753 "Major mode for editing BibTeX files.
2755 General information on working with BibTeX mode:
2757 Use commands such as \\[bibtex-Book] to get a template for a specific entry.
2758 Then fill in all desired fields using \\[bibtex-next-field] to jump from field
2759 to field. After having filled in all desired fields in the entry, clean the
2760 new entry with the command \\[bibtex-clean-entry].
2762 Some features of BibTeX mode are available only by setting the variable
2763 `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode
2764 works only with buffers containing valid (syntactical correct) and sorted
2765 entries. This is usually the case, if you have created a buffer completely
2766 with BibTeX mode and finished every new entry with \\[bibtex-clean-entry].
2768 For third party BibTeX files, call the command \\[bibtex-convert-alien]
2769 to fully take advantage of all features of BibTeX mode.
2772 Special information:
2774 A command such as \\[bibtex-Book] outlines the fields for a BibTeX book entry.
2776 The names of optional fields start with the string OPT, and are thus ignored
2777 by BibTeX. The names of alternative fields from which only one is required
2778 start with the string ALT. The OPT or ALT string may be removed from
2779 the name of a field with \\[bibtex-remove-OPT-or-ALT].
2780 \\[bibtex-make-field] inserts a new field after the current one.
2781 \\[bibtex-kill-field] kills the current field entirely.
2782 \\[bibtex-yank] yanks the last recently killed field after the current field.
2783 \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
2784 \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
2785 \\[bibtex-find-text] moves point to the end of the current field.
2786 \\[bibtex-complete] completes word fragment before point according to context.
2788 The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2789 from the names of all non-empty optional or alternative fields, checks that
2790 no required fields are empty, and does some formatting dependent on the value
2791 of `bibtex-entry-format'. Furthermore, it can automatically generate a key
2792 for the BibTeX entry, see `bibtex-generate-autokey'.
2793 Note: some functions in BibTeX mode depend on entries being in a special
2794 format (all fields beginning on separate lines), so it is usually a bad
2795 idea to remove `realign' from `bibtex-entry-format'.
2797 BibTeX mode supports Imenu and hideshow minor mode (`hs-minor-mode').
2799 ----------------------------------------------------------
2800 Entry to BibTeX mode calls the value of `bibtex-mode-hook'
2801 if that value is non-nil.
2803 \\{bibtex-mode-map}"
2805 (kill-all-local-variables)
2806 (use-local-map bibtex-mode-map
)
2807 (setq major-mode
'bibtex-mode
)
2808 (setq mode-name
"BibTeX")
2809 (set-syntax-table bibtex-mode-syntax-table
)
2810 (make-local-variable 'bibtex-buffer-last-parsed-tick
)
2811 ;; Install stealthy parse function if not already installed
2812 (unless bibtex-parse-idle-timer
2813 (setq bibtex-parse-idle-timer
(run-with-idle-timer
2814 bibtex-parse-keys-timeout t
2815 'bibtex-parse-buffers-stealthily
)))
2816 (set (make-local-variable 'paragraph-start
) "[ \f\n\t]*$")
2817 (set (make-local-variable 'comment-start
) bibtex-comment-start
)
2818 (set (make-local-variable 'comment-start-skip
)
2819 (concat (regexp-quote bibtex-comment-start
) "\\>[ \t]*"))
2820 (set (make-local-variable 'comment-column
) 0)
2821 (set (make-local-variable 'defun-prompt-regexp
) "^[ \t]*@[[:alnum:]]+[ \t]*")
2822 (set (make-local-variable 'outline-regexp
) "[ \t]*@")
2823 (set (make-local-variable 'fill-paragraph-function
) 'bibtex-fill-field
)
2824 (set (make-local-variable 'fill-prefix
) (make-string (+ bibtex-entry-offset
2825 bibtex-contline-indentation
)
2827 (set (make-local-variable 'font-lock-defaults
)
2828 '(bibtex-font-lock-keywords
2830 ;; Mathematical expressions should be fontified as strings
2832 ;; Quotes are field delimiters and quote-delimited
2833 ;; entries should be fontified in the same way as
2834 ;; brace-delimited ones
2837 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords
)
2838 (font-lock-extra-managed-props .
(category))
2839 (font-lock-mark-block-function
2841 (set-mark (bibtex-end-of-entry))
2842 (bibtex-beginning-of-entry)))))
2843 (setq imenu-generic-expression
2844 (list (list nil bibtex-entry-head bibtex-key-in-head
))
2845 imenu-case-fold-search t
)
2846 (make-local-variable 'choose-completion-string-functions
)
2847 ;; XEmacs needs easy-menu-add, Emacs does not care
2848 (easy-menu-add bibtex-edit-menu
)
2849 (easy-menu-add bibtex-entry-menu
)
2850 (run-mode-hooks 'bibtex-mode-hook
))
2852 (defun bibtex-field-list (entry-type)
2853 "Return list of allowed fields for entry ENTRY-TYPE.
2854 More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),
2855 where REQUIRED and OPTIONAL are lists of the required and optional field
2856 names for ENTRY-TYPE according to `bibtex-entry-field-alist',
2857 `bibtex-include-OPTkey', `bibtex-include-OPTcrossref',
2858 and `bibtex-user-optional-fields'."
2859 (let ((e (assoc-string entry-type bibtex-entry-field-alist t
))
2862 (error "BibTeX entry type %s not defined" entry-type
))
2863 (if (and (member-ignore-case entry-type bibtex-include-OPTcrossref
)
2865 (setq required
(nth 0 (nth 2 e
))
2866 optional
(nth 1 (nth 2 e
)))
2867 (setq required
(nth 0 (nth 1 e
))
2868 optional
(nth 1 (nth 1 e
))))
2869 (if bibtex-include-OPTkey
2871 "Used for reference key creation if author and editor fields are missing"
2872 (if (or (stringp bibtex-include-OPTkey
)
2873 (fboundp bibtex-include-OPTkey
))
2874 bibtex-include-OPTkey
))
2876 (if (member-ignore-case entry-type bibtex-include-OPTcrossref
)
2877 (push '("crossref" "Reference key of the cross-referenced entry")
2879 (setq optional
(append optional bibtex-user-optional-fields
))
2880 (cons required optional
)))
2882 (defun bibtex-entry (entry-type)
2883 "Insert a new BibTeX entry of type ENTRY-TYPE.
2884 After insertion call the value of `bibtex-add-entry-hook' if that value
2887 (let ((completion-ignore-case t
))
2888 (list (completing-read "Entry Type: " bibtex-entry-field-alist
2889 nil t nil
'bibtex-entry-type-history
))))
2890 (let ((key (if bibtex-maintain-sorted-entries
2891 (bibtex-read-key (format "%s key: " entry-type
))))
2892 (field-list (bibtex-field-list entry-type
)))
2893 (unless (bibtex-prepare-new-entry (list key nil entry-type
))
2894 (error "Entry with key `%s' already exists" key
))
2895 (indent-to-column bibtex-entry-offset
)
2896 (insert "@" entry-type
(bibtex-entry-left-delimiter))
2897 (if key
(insert key
))
2899 (mapc 'bibtex-make-field
(car field-list
))
2900 (mapc 'bibtex-make-optional-field
(cdr field-list
))
2901 (if bibtex-comma-after-last-field
2904 (indent-to-column bibtex-entry-offset
)
2905 (insert (bibtex-entry-right-delimiter) "\n\n"))
2906 (bibtex-next-field t
)
2907 (if (member-ignore-case entry-type bibtex-autofill-types
)
2908 (bibtex-autofill-entry))
2909 (run-hooks 'bibtex-add-entry-hook
)))
2911 (defun bibtex-entry-update ()
2912 "Update an existing BibTeX entry.
2913 In the BibTeX entry at point, make new fields for those items that may occur
2914 according to `bibtex-field-list', but are not yet present."
2917 (bibtex-beginning-of-entry)
2918 ;; For inserting new fields, we use the fact that
2919 ;; bibtex-parse-entry moves point to the end of the last field.
2920 (let* ((fields-alist (bibtex-parse-entry))
2921 (field-list (bibtex-field-list
2922 (cdr (assoc "=type=" fields-alist
)))))
2923 (dolist (field (car field-list
))
2924 (unless (assoc-string (car field
) fields-alist t
)
2925 (bibtex-make-field field
)))
2926 (dolist (field (cdr field-list
))
2927 (unless (assoc-string (car field
) fields-alist t
)
2928 (bibtex-make-optional-field field
))))))
2930 (defun bibtex-parse-entry (&optional content
)
2931 "Parse entry at point, return an alist.
2932 The alist elements have the form (FIELD . TEXT), where FIELD can also be
2933 the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\"
2934 TEXT may be nil. Remove \"OPT\" and \"ALT\" from FIELD.
2935 Move point to the end of the last field.
2936 If optional arg CONTENT is non-nil extract content of text fields."
2938 (when (looking-at bibtex-entry-maybe-empty-head
)
2939 (push (cons "=type=" (bibtex-type-in-head)) alist
)
2940 (push (cons "=key=" (bibtex-key-in-head)) alist
)
2941 (goto-char (match-end 0))
2942 (while (setq bounds
(bibtex-parse-field))
2943 (push (cons (bibtex-name-in-field bounds t
)
2944 (bibtex-text-in-field-bounds bounds content
))
2946 (goto-char (bibtex-end-of-field bounds
))))
2949 (defun bibtex-autofill-entry ()
2950 "Try to fill fields of current BibTeX entry based on neighboring entries.
2951 The current entry must have a key. Determine the neighboring entry
2952 \(previouse or next\) whose key is more similar to the key of the current
2953 entry. For all empty fields of the current entry insert the corresponding
2954 field contents of the neighboring entry. Finally try to update the text
2955 based on the difference between the keys of the neighboring and the current
2956 entry (for example, the year parts of the keys)."
2958 (undo-boundary) ;So you can easily undo it, if it didn't work right.
2959 (bibtex-beginning-of-entry)
2960 (when (looking-at bibtex-entry-head
)
2961 (let ((type (bibtex-type-in-head))
2962 (key (bibtex-key-in-head))
2963 (key-end (match-end bibtex-key-in-head
))
2964 (case-fold-search t
)
2965 tmp other-key other bounds
)
2966 ;; The fields we want to change start right after the key.
2968 ;; First see whether to use the previous or the next entry
2969 ;; for "inspiration".
2971 (goto-char (1- (match-beginning 0)))
2972 (bibtex-beginning-of-entry)
2973 (if (and (looking-at bibtex-entry-head
)
2974 (bibtex-string= type
(bibtex-type-in-head))
2975 ;; In case we found ourselves :-(
2976 (not (equal key
(setq tmp
(bibtex-key-in-head)))))
2980 (bibtex-end-of-entry)
2981 (bibtex-skip-to-valid-entry)
2982 (if (and (looking-at bibtex-entry-head
)
2983 (bibtex-string= type
(bibtex-type-in-head))
2984 ;; In case we found ourselves :-(
2985 (not (equal key
(setq tmp
(bibtex-key-in-head))))
2987 ;; Check which is the best match.
2988 (< (length (try-completion "" (list key other-key
)))
2989 (length (try-completion "" (list key tmp
))))))
2992 ;; Then fill the new entry's fields with the chosen other entry.
2994 (setq other
(save-excursion (goto-char other
) (bibtex-parse-entry)))
2995 (setq key-end
(point)) ;In case parse-entry changed the buffer.
2996 (while (setq bounds
(bibtex-parse-field))
2997 (let ((text (assoc-string (bibtex-name-in-field bounds t
)
3000 (equal "" (bibtex-text-in-field-bounds bounds t
))))
3001 (goto-char (bibtex-end-of-field bounds
))
3002 (goto-char (bibtex-start-of-text-in-field bounds
))
3003 (delete-region (point) (bibtex-end-of-text-in-field bounds
))
3004 (insert (cdr text
)))))
3005 ;; Finally try to update the text based on the difference between
3007 (let* ((prefix (try-completion "" (list key other-key
)))
3008 ;; If the keys are foo91 and foo92, don't replace 1 for 2
3009 ;; but 91 for 92 instead.
3010 (_ (if (string-match "[0-9]+\\'" prefix
)
3011 (setq prefix
(substring prefix
0 (match-beginning 0)))))
3012 (suffix (substring key
(length prefix
)))
3013 (other-suffix (substring other-key
(length prefix
))))
3014 (while (re-search-backward (regexp-quote other-suffix
) key-end
'move
)
3015 (replace-match suffix
)))))))
3017 (defun bibtex-print-help-message ()
3018 "Print helpful information about current field in current BibTeX entry."
3020 (let* ((case-fold-search t
)
3021 (type (save-excursion
3022 (bibtex-beginning-of-entry)
3023 (looking-at bibtex-any-entry-maybe-empty-head
)
3024 (bibtex-type-in-head)))
3026 (cond ((bibtex-string= type
"string")
3027 (message "String definition"))
3028 ((bibtex-string= type
"preamble")
3029 (message "Preamble definition"))
3031 (setq field-list
(bibtex-field-list type
)
3033 (assoc-string (bibtex-name-in-field (bibtex-enclosing-field) t
)
3034 (append (car field-list
) (cdr field-list
))
3037 (message "%s" (nth 1 comment
))
3038 (message "No comment available"))))))
3040 (defun bibtex-make-field (field &optional move interactive
)
3041 "Make a field named FIELD in current BibTeX entry.
3042 FIELD is either a string or a list of the form
3043 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
3044 `bibtex-entry-field-alist'.
3045 If MOVE is non-nil, move point past the present field before making
3046 the new field. If INTERACTIVE is non-nil, move point to the end of
3047 the new field. Otherwise move point past the new field.
3048 MOVE and INTERACTIVE are t when called interactively."
3050 (list (let ((completion-ignore-case t
)
3051 (field-list (bibtex-field-list
3053 (bibtex-enclosing-entry-maybe-empty-head)
3054 (bibtex-type-in-head)))))
3055 (completing-read "BibTeX field name: "
3056 (append (car field-list
) (cdr field-list
))
3057 nil nil nil bibtex-field-history
))
3059 (unless (consp field
)
3060 (setq field
(list field
)))
3063 (if (looking-at "[}\"]")
3066 (indent-to-column (+ bibtex-entry-offset bibtex-field-indentation
))
3067 (if (nth 3 field
) (insert "ALT"))
3068 (insert (car field
) " ")
3069 (if bibtex-align-at-equal-sign
3070 (indent-to-column (+ bibtex-entry-offset
3071 (- bibtex-text-indentation
2))))
3073 (unless bibtex-align-at-equal-sign
3074 (indent-to-column (+ bibtex-entry-offset
3075 bibtex-text-indentation
)))
3076 (let ((init (nth 2 field
)))
3077 (insert (cond ((stringp init
) init
)
3078 ((fboundp init
) (funcall init
))
3079 (t (concat (bibtex-field-left-delimiter)
3080 (bibtex-field-right-delimiter))))))
3083 (bibtex-print-help-message)))
3085 (defun bibtex-beginning-of-entry ()
3086 "Move to beginning of BibTeX entry (beginning of line).
3087 If inside an entry, move to the beginning of it, otherwise move to the
3088 beginning of the previous entry. If point is ahead of all BibTeX entries
3089 move point to the beginning of buffer. Return the new location of point."
3091 (skip-chars-forward " \t")
3092 (if (looking-at "@")
3094 (re-search-backward "^[ \t]*@" nil
'move
)
3097 (defun bibtex-end-of-entry ()
3098 "Move to end of BibTeX entry (past the closing brace).
3099 If inside an entry, move to the end of it, otherwise move to the end
3100 of the previous entry. Do not move if ahead of first entry.
3101 Return the new location of point."
3103 (let ((case-fold-search t
)
3105 (pnt (bibtex-beginning-of-entry))
3107 (cond ((looking-at bibtex-entry-type-whitespace
)
3108 (bibtex-search-entry t nil t
)
3109 (unless (equal (match-beginning 0) pnt
)
3112 ((setq bounds
(bibtex-parse-string))
3113 (goto-char (bibtex-end-of-string bounds
)))
3115 ((bibtex-preamble-prefix t
)
3116 (unless (bibtex-parse-string-postfix) ;; @String postfix OK
3120 (message "Not on a known BibTeX entry."))
3124 (error "Syntactically incorrect BibTeX entry starts here")))
3127 (defun bibtex-goto-line (arg)
3128 "Goto line ARG, counting from beginning of (narrowed) buffer."
3129 ;; code adapted from `goto-line'
3130 (goto-char (point-min))
3131 (if (eq selective-display t
)
3132 (re-search-forward "[\n\C-m]" nil
'end
(1- arg
))
3133 (forward-line (1- arg
))))
3135 (defun bibtex-reposition-window ()
3136 "Make the current BibTeX entry visible.
3137 If entry is smaller than `window-body-height', entry is centered in window.
3138 Otherwise display the beginning of entry."
3141 (beg (line-number-at-pos (bibtex-beginning-of-entry)))
3142 (end (line-number-at-pos (bibtex-end-of-entry))))
3143 (if (> (window-body-height) (- end beg
))
3144 ;; entry fits in current window
3146 (bibtex-goto-line (/ (+ 1 beg end
) 2))
3149 ;; entry too large for current window
3150 (bibtex-goto-line beg
)
3152 (if (> (1+ (- (line-number-at-pos pnt
) beg
))
3153 (window-body-height))
3154 (bibtex-goto-line beg
)
3157 (defun bibtex-mark-entry ()
3158 "Put mark at beginning, point at end of current BibTeX entry."
3160 (set-mark (bibtex-beginning-of-entry))
3161 (bibtex-end-of-entry))
3163 (defun bibtex-count-entries (&optional count-string-entries
)
3164 "Count number of entries in current buffer or region.
3165 With prefix argument COUNT-STRING-ENTRIES count all entries,
3166 otherwise count all entries except @String entries.
3167 If mark is active count entries in region, if not in whole buffer."
3170 (bibtex-sort-ignore-string-entries (not count-string-entries
)))
3172 (if mark-active
(narrow-to-region (region-beginning) (region-end)))
3173 (bibtex-map-entries (lambda (key beg end
) (setq number
(1+ number
)))))
3174 (message "%s contains %d entries."
3175 (if mark-active
"Region" "Buffer")
3178 (defun bibtex-ispell-entry ()
3179 "Check BibTeX entry for spelling errors."
3181 (ispell-region (save-excursion (bibtex-beginning-of-entry))
3182 (save-excursion (bibtex-end-of-entry))))
3184 (defun bibtex-ispell-abstract ()
3185 "Check abstract of BibTeX entry for spelling errors."
3187 (let ((bounds (save-excursion
3188 (bibtex-beginning-of-entry)
3189 (bibtex-search-forward-field "abstract"))))
3191 (ispell-region (bibtex-start-of-text-in-field bounds
)
3192 (bibtex-end-of-text-in-field bounds
))
3193 (error "No abstract in entry"))))
3195 (defun bibtex-narrow-to-entry ()
3196 "Narrow buffer to current BibTeX entry."
3200 (narrow-to-region (bibtex-beginning-of-entry)
3201 (bibtex-end-of-entry))))
3203 (defun bibtex-entry-index ()
3204 "Return index of BibTeX entry head at or past position of point.
3205 The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting
3206 the entries of the BibTeX buffer. CROSSREF-KEY is nil unless the value
3207 of `bibtex-maintain-sorted-entries' is `crossref'. Move point to the end
3208 of the head of the entry found. Return nil if no entry found."
3209 (let ((case-fold-search t
))
3210 (if (re-search-forward bibtex-entry-maybe-empty-head nil t
)
3211 (let ((key (bibtex-key-in-head))
3212 ;; all entry names should be downcase (for ease of comparison)
3213 (entry-name (downcase (bibtex-type-in-head))))
3214 ;; Don't search CROSSREF-KEY if we don't need it.
3215 (if (eq bibtex-maintain-sorted-entries
'crossref
)
3216 (let ((bounds (bibtex-search-forward-field
3217 "\\(OPT\\)?crossref")))
3219 (if bounds
(bibtex-text-in-field-bounds bounds t
))
3221 (list key nil entry-name
))))))
3223 (defun bibtex-lessp (index1 index2
)
3224 "Predicate for sorting BibTeX entries with indices INDEX1 and INDEX2.
3225 Each index is a list (KEY CROSSREF-KEY ENTRY-NAME).
3226 The predicate depends on the variable `bibtex-maintain-sorted-entries'.
3227 If its value is nil use plain sorting."
3228 (cond ((not index1
) (not index2
)) ; indices can be nil
3230 ((eq bibtex-maintain-sorted-entries
'crossref
)
3233 (or (string-lessp (nth 1 index1
) (nth 1 index2
))
3234 (and (string-equal (nth 1 index1
) (nth 1 index2
))
3235 (string-lessp (nth 0 index1
) (nth 0 index2
))))
3236 (not (string-lessp (nth 0 index2
) (nth 1 index1
))))
3238 (string-lessp (nth 0 index1
) (nth 1 index2
))
3239 (string-lessp (nth 0 index1
) (nth 0 index2
)))))
3240 ((eq bibtex-maintain-sorted-entries
'entry-class
)
3241 (let ((n1 (cdr (or (assoc (nth 2 index1
) bibtex-sort-entry-class-alist
)
3242 (assoc 'catch-all bibtex-sort-entry-class-alist
)
3243 '(nil .
1000)))) ; if there is nothing else
3244 (n2 (cdr (or (assoc (nth 2 index2
) bibtex-sort-entry-class-alist
)
3245 (assoc 'catch-all bibtex-sort-entry-class-alist
)
3246 '(nil .
1000))))) ; if there is nothing else
3249 (string-lessp (car index1
) (car index2
))))))
3250 (t ; (eq bibtex-maintain-sorted-entries 'plain)
3251 (string-lessp (car index1
) (car index2
)))))
3253 (defun bibtex-sort-buffer ()
3254 "Sort BibTeX buffer alphabetically by key.
3255 The predicate for sorting is defined via `bibtex-maintain-sorted-entries'.
3256 If its value is nil use plain sorting. Text outside of BibTeX entries is not
3257 affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
3260 (bibtex-beginning-of-first-entry) ;; needed by `sort-subr'
3262 'bibtex-skip-to-valid-entry
; NEXTREC function
3263 'bibtex-end-of-entry
; ENDREC function
3264 'bibtex-entry-index
; STARTKEY function
3265 nil
; ENDKEY function
3266 'bibtex-lessp
)) ; PREDICATE
3268 (defun bibtex-find-crossref (crossref-key &optional pnt split
)
3269 "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3270 If `bibtex-files' is non-nil, search all these files.
3271 Otherwise the search is limited to the current buffer.
3272 Return position of entry if CROSSREF-KEY is found or nil otherwise.
3273 If CROSSREF-KEY is in the same buffer like current entry but before it
3274 an error is signaled. Optional arg PNT is the position of the referencing
3275 entry. It defaults to position of point. If optional arg SPLIT is non-nil,
3276 split window so that both the referencing and the crossrefed entry are
3278 If called interactively, CROSSREF-KEY defaults to crossref key of current
3279 entry and SPLIT is t."
3283 (bibtex-beginning-of-entry)
3284 (let ((bounds (bibtex-search-forward-field "crossref")))
3286 (bibtex-text-in-field-bounds bounds t
))))))
3287 (list (bibtex-read-key "Find crossref key: " crossref-key t
)
3289 (let (buffer pos eqb
)
3291 (setq pos
(bibtex-find-entry crossref-key t
)
3292 buffer
(current-buffer)))
3293 (setq eqb
(eq buffer
(current-buffer)))
3295 (if split
(message "Crossref key `%s' not found" crossref-key
)))
3296 (split ; called (quasi) interactively
3297 (unless pnt
(setq pnt
(point)))
3299 (if eqb
(select-window (split-window))
3300 (pop-to-buffer buffer
))
3302 (bibtex-reposition-window)
3304 (if (and eqb
(> pnt pos
))
3305 (error "The referencing entry must precede the crossrefed entry!")))
3306 ;; `bibtex-find-crossref' is called noninteractively during
3307 ;; clean-up of an entry. Then it is not possible to check
3308 ;; whether the current entry and the crossrefed entry have
3309 ;; the correct sorting order.
3310 (eqb (goto-char pos
))
3311 (t (set-buffer buffer
) (goto-char pos
)))
3314 (defun bibtex-find-entry (key &optional global start display
)
3315 "Move point to the beginning of BibTeX entry named KEY.
3316 Return position of entry if KEY is found or nil if not found.
3317 With prefix arg GLOBAL non-nil, search KEY in `bibtex-files'.
3318 Otherwise the search is limited to the current buffer.
3319 Optional arg START is buffer position where the search starts.
3320 If it is nil, start search at beginning of buffer.
3321 If DISPLAY is non-nil, display the buffer containing KEY.
3322 Otherwise, use `set-buffer'. DISPLAY is t when called interactively."
3323 (interactive (list (bibtex-read-key "Find key: " nil current-prefix-arg
)
3324 current-prefix-arg nil t
))
3325 (if (and global bibtex-files
)
3326 (let ((buffer-list (bibtex-files-expand t
))
3328 (while (and (not found
)
3329 (setq buffer
(pop buffer-list
)))
3330 (with-current-buffer buffer
3331 (if (cdr (assoc-string key bibtex-reference-keys
))
3332 ;; `bibtex-find-entry' moves point if key found
3333 (setq found
(bibtex-find-entry key
)))))
3334 (cond ((and found display
)
3335 (let ((same-window-buffer-names
3336 (cons (buffer-name buffer
) same-window-buffer-names
)))
3337 (pop-to-buffer buffer
)
3338 (bibtex-reposition-window)))
3339 (found (set-buffer buffer
))
3340 (display (message "Key `%s' not found" key
)))
3343 (let* (case-fold-search
3344 (pnt (save-excursion
3345 (goto-char (or start
(point-min)))
3346 (if (re-search-forward (concat "^[ \t]*\\("
3348 "\\)[ \t]*[({][ \t\n]*\\("
3352 (match-beginning 0)))))
3355 (if display
(bibtex-reposition-window)))
3356 (display (message "Key `%s' not found" key
)))
3359 (defun bibtex-prepare-new-entry (index)
3360 "Prepare a new BibTeX entry with index INDEX.
3361 INDEX is a list (KEY CROSSREF-KEY ENTRY-NAME).
3362 Move point where the entry KEY should be placed.
3363 If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
3364 search to look for place for KEY. This requires that buffer is sorted,
3365 see `bibtex-validate'.
3366 Return t if preparation was successful or nil if entry KEY already exists."
3367 (let ((key (nth 0 index
))
3369 (cond ((or (null key
)
3371 (string-equal key
""))
3372 (and (not (setq key-exist
(bibtex-find-entry key
)))
3373 (not bibtex-maintain-sorted-entries
)))
3374 (bibtex-move-outside-of-entry))
3375 ;; if key-exist is non-nil due to the previous cond clause
3376 ;; then point will be at beginning of entry named key.
3378 (t ; bibtex-maintain-sorted-entries is non-nil
3379 (let* ((case-fold-search t
)
3380 (left (save-excursion (bibtex-beginning-of-first-entry)))
3381 (bounds (save-excursion (goto-char (point-max))
3382 (bibtex-skip-to-valid-entry t
)))
3383 (right (if bounds
(cdr bounds
) (point-min)))
3384 (found (if (>= left right
) left
))
3389 (goto-char (/ (+ left right
) 2))
3390 (bibtex-skip-to-valid-entry t
)
3391 (setq actual-index
(bibtex-entry-index))
3392 (cond ((bibtex-lessp index actual-index
)
3393 (setq new
(bibtex-beginning-of-entry))
3394 (if (equal right new
)
3398 (bibtex-end-of-entry)
3399 (bibtex-skip-to-valid-entry)
3401 (if (equal left new
)
3403 (setq left new
))))))
3405 (bibtex-beginning-of-entry)
3406 (setq actual-index
(save-excursion (bibtex-entry-index)))
3407 (when (or (not actual-index
)
3408 (bibtex-lessp actual-index index
))
3409 ;; buffer contains no valid entries or
3410 ;; greater than last entry --> append
3411 (bibtex-end-of-entry)
3412 (unless (bobp) (newline (forward-line 2)))
3413 (beginning-of-line)))))
3414 (unless key-exist t
)))
3416 (defun bibtex-validate (&optional test-thoroughly
)
3417 "Validate if buffer or region is syntactically correct.
3418 Check also for duplicate keys and correct sort order provided
3419 `bibtex-maintain-sorted-entries' is non-nil.
3420 With optional argument TEST-THOROUGHLY non-nil check also for
3421 the absence of required fields and for questionable month fields.
3422 If mark is active, validate current region, if not the whole buffer.
3423 Only check known entry types, so you can put comments outside of entries.
3424 Return t if test was successful, nil otherwise."
3426 (let* ((case-fold-search t
)
3427 error-list syntax-error
)
3431 (narrow-to-region (region-beginning) (region-end)))
3433 ;; looking if entries fit syntactical structure
3434 (goto-char (point-min))
3435 (bibtex-progress-message "Checking syntactical structure")
3436 (let (bibtex-sort-ignore-string-entries)
3437 (while (re-search-forward "^[ \t]*@" nil t
)
3438 (bibtex-progress-message)
3440 (let ((pnt (point)))
3441 (if (not (looking-at bibtex-entry-type-str
))
3443 (bibtex-skip-to-valid-entry)
3444 (if (equal (point) pnt
)
3447 (push (cons (bibtex-current-line)
3448 "Syntax error (check esp. commas, braces, and quotes)")
3451 (bibtex-progress-message 'done
)
3454 ;; proceed only if there were no syntax errors.
3455 (setq syntax-error t
)
3457 ;; looking for duplicate keys and correct sort order
3458 (let (previous current key-list
)
3459 (bibtex-progress-message "Checking for duplicate keys")
3461 (lambda (key beg end
)
3462 (bibtex-progress-message)
3464 (setq current
(bibtex-entry-index))
3465 (cond ((not previous
))
3466 ((member key key-list
)
3467 (push (cons (bibtex-current-line)
3468 (format "Duplicate key `%s'" key
))
3470 ((and bibtex-maintain-sorted-entries
3471 (not (bibtex-lessp previous current
)))
3472 (push (cons (bibtex-current-line)
3473 "Entries out of order")
3476 (setq previous current
)))
3477 (bibtex-progress-message 'done
))
3479 ;; Check for duplicate keys in `bibtex-files'.
3481 ;; We don't want to be fooled by outdated `bibtex-reference-keys'.
3482 (dolist (buffer (bibtex-files-expand nil t
))
3483 (dolist (key (with-current-buffer buffer bibtex-reference-keys
))
3484 (when (and (cdr key
)
3485 (cdr (assoc-string (car key
) bibtex-reference-keys
)))
3486 (bibtex-find-entry (car key
))
3487 (push (cons (bibtex-current-line)
3488 (format "Duplicate key `%s' in %s" (car key
)
3489 (abbreviate-file-name (buffer-file-name buffer
))))
3492 (when test-thoroughly
3493 (bibtex-progress-message
3494 "Checking required fields and month fields")
3495 (let ((bibtex-sort-ignore-string-entries t
))
3497 (lambda (key beg end
)
3498 (bibtex-progress-message)
3499 (let* ((entry-list (progn
3501 (bibtex-search-entry nil end
)
3502 (assoc-string (bibtex-type-in-head)
3503 bibtex-entry-field-alist t
)))
3504 (req (copy-sequence (elt (elt entry-list
1) 0)))
3505 (creq (copy-sequence (elt (elt entry-list
2) 0)))
3506 crossref-there bounds alt-there field
)
3508 (while (setq bounds
(bibtex-search-forward-field
3509 bibtex-field-name end
))
3510 (goto-char (bibtex-start-of-text-in-field bounds
))
3511 (let ((field-name (bibtex-name-in-field bounds
)))
3512 (if (and (bibtex-string= field-name
"month")
3513 ;; Check only abbreviated month fields.
3514 (let ((month (bibtex-text-in-field-bounds bounds
)))
3515 (not (or (string-match "\\`[\"{].+[\"}]\\'" month
)
3518 bibtex-predefined-month-strings t
)))))
3519 (push (cons (bibtex-current-line)
3520 "Questionable month field")
3522 (setq field
(assoc-string field-name req t
))
3524 (if alt-there
(push (cons (bibtex-current-line)
3525 "More than one non-empty alternative")
3527 (setq alt-there t
)))
3528 (setq req
(delete field req
)
3529 creq
(delete (assoc-string field-name creq t
) creq
))
3530 (if (bibtex-string= field-name
"crossref")
3531 (setq crossref-there t
))))
3537 (push (car field
) alt
)
3538 (push (cons (save-excursion (goto-char beg
)
3539 (bibtex-current-line))
3540 (format "Required field `%s' missing"
3543 ;; The following fails if there are more than two
3544 ;; alternatives in a BibTeX entry, which isn't
3545 ;; the case momentarily.
3547 (push (cons (save-excursion (goto-char beg
)
3548 (bibtex-current-line))
3549 (format "Alternative fields `%s'/`%s' missing"
3550 (car alt
) (cadr alt
)))
3552 (bibtex-progress-message 'done
)))))
3555 (let ((file (file-name-nondirectory (buffer-file-name)))
3556 (dir default-directory
)
3557 (err-buf "*BibTeX validation errors*"))
3558 (setq error-list
(sort error-list
'car-less-than-car
))
3559 (with-current-buffer (get-buffer-create err-buf
)
3560 (setq default-directory dir
)
3561 (unless (eq major-mode
'compilation-mode
) (compilation-mode))
3562 (toggle-read-only -
1)
3563 (delete-region (point-min) (point-max))
3564 (insert "BibTeX mode command `bibtex-validate'\n"
3566 "Maybe undetected errors due to syntax errors. Correct and validate again.\n"
3568 (dolist (err error-list
)
3569 (insert (format "%s:%d: %s\n" file
(car err
) (cdr err
))))
3570 (set-buffer-modified-p nil
)
3571 (toggle-read-only 1)
3572 (goto-line 3)) ; first error message
3573 (display-buffer err-buf
)
3576 (message "%s is syntactically correct"
3577 (if mark-active
"Region" "Buffer"))
3580 (defun bibtex-validate-globally (&optional strings
)
3581 "Check for duplicate keys in `bibtex-files'.
3582 With optional prefix arg STRINGS, check for duplicate strings, too.
3583 Return t if test was successful, nil otherwise."
3585 (let ((buffer-list (bibtex-files-expand t
))
3586 buffer-key-list current-buf current-keys error-list
)
3587 ;; Check for duplicate keys within BibTeX buffer
3588 (dolist (buffer buffer-list
)
3591 (let (entry-type key key-list
)
3592 (goto-char (point-min))
3593 (while (re-search-forward bibtex-entry-head nil t
)
3594 (setq entry-type
(bibtex-type-in-head)
3595 key
(bibtex-key-in-head))
3596 (if (or (and strings
(bibtex-string= entry-type
"string"))
3597 (assoc-string entry-type bibtex-entry-field-alist t
))
3598 (if (member key key-list
)
3599 (push (format "%s:%d: Duplicate key `%s'\n"
3601 (bibtex-current-line) key
)
3603 (push key key-list
))))
3604 (push (cons buffer key-list
) buffer-key-list
))))
3606 ;; Check for duplicate keys among BibTeX buffers
3607 (while (setq current-buf
(pop buffer-list
))
3608 (setq current-keys
(cdr (assq current-buf buffer-key-list
)))
3609 (with-current-buffer current-buf
3610 (dolist (buffer buffer-list
)
3611 (dolist (key (cdr (assq buffer buffer-key-list
)))
3612 (when (assoc-string key current-keys
)
3613 (bibtex-find-entry key
)
3614 (push (format "%s:%d: Duplicat key `%s' in %s\n"
3615 (buffer-file-name) (bibtex-current-line) key
3616 (abbreviate-file-name (buffer-file-name buffer
)))
3619 ;; Process error list
3621 (let ((err-buf "*BibTeX validation errors*"))
3622 (with-current-buffer (get-buffer-create err-buf
)
3623 (unless (eq major-mode
'compilation-mode
) (compilation-mode))
3624 (toggle-read-only -
1)
3625 (delete-region (point-min) (point-max))
3626 (insert "BibTeX mode command `bibtex-validate-globally'\n\n")
3627 (dolist (err (sort error-list
'string-lessp
)) (insert err
))
3628 (set-buffer-modified-p nil
)
3629 (toggle-read-only 1)
3630 (goto-line 3)) ; first error message
3631 (display-buffer err-buf
)
3634 (message "No duplicate keys.")
3637 (defun bibtex-next-field (begin)
3638 "Move point to end of text of next BibTeX field.
3639 With prefix BEGIN non-nil, move point to its beginning."
3641 (bibtex-inside-field)
3642 (let ((start (point)))
3644 (let ((bounds (bibtex-enclosing-field)))
3645 (goto-char (bibtex-end-of-field bounds
))
3651 (bibtex-find-text begin nil bibtex-help-message
))
3653 (defun bibtex-find-text (&optional begin noerror help
)
3654 "Move point to end of text of current BibTeX field.
3655 With optional prefix BEGIN non-nil, move point to its beginning.
3656 Unless NOERROR is non-nil, an error is signaled if point is not
3657 on a BibTeX field. If optional arg HELP is non-nil print help message.
3658 When called interactively, the value of HELP is `bibtex-help-message'."
3659 (interactive (list current-prefix-arg nil bibtex-help-message
))
3661 (bounds (bibtex-find-text-internal)))
3665 (progn (goto-char (nth 1 bounds
))
3666 (if (looking-at "[{\"]")
3668 (goto-char (nth 2 bounds
))
3669 (if (memq (preceding-char) '(?
} ?
\"))
3671 (if help
(bibtex-print-help-message)))
3672 ((looking-at bibtex-entry-maybe-empty-head
)
3673 (goto-char (if begin
3674 (match-beginning bibtex-key-in-head
)
3678 (unless noerror
(error "Not on BibTeX field"))))))
3680 (defun bibtex-find-text-internal (&optional noerror subfield
)
3681 "Find text part of current BibTeX field, @String or @Preamble.
3682 Return list (NAME START END) with field name, start and end of text
3683 or nil if not found.
3684 If optional arg NOERROR is non-nil, an error message is suppressed if text
3685 is not found. If optional arg SUBFIELD is non-nil START and END correspond
3686 to the current subfield delimited by #."
3689 (_ (bibtex-inside-field))
3690 (bounds (bibtex-enclosing-field t
))
3691 (case-fold-search t
)
3692 (bibtex-string-empty-key t
)
3694 (bibtex-beginning-of-entry)
3696 (setq name
(bibtex-name-in-field bounds t
)
3697 start
(bibtex-start-of-text-in-field bounds
)
3698 end
(bibtex-end-of-text-in-field bounds
)))
3700 ((setq bounds
(bibtex-parse-string))
3701 (setq name
"@String" ;; not a field name!
3702 start
(bibtex-start-of-text-in-string bounds
)
3703 end
(bibtex-end-of-text-in-string bounds
)))
3705 ((and (bibtex-preamble-prefix t
)
3706 (setq bounds
(bibtex-parse-field-text)))
3707 (setq name
"@Preamble" ;; not a field name!
3709 end
(nth 1 bounds
)))
3710 (t (unless noerror
(error "Not on BibTeX field"))))
3711 (when (and start end subfield
)
3715 (if (or (prog1 (looking-at bibtex-field-const
)
3716 (setq end
(match-end 0)))
3717 (prog1 (setq bounds
(bibtex-parse-field-string))
3718 (setq end
(cdr bounds
))))
3720 (if (and (<= start pnt
) (<= pnt end
))
3723 (if (looking-at "[ \t\n]*#[ \t\n]*")
3724 (setq start
(goto-char (match-end 0)))))
3725 (unless noerror
(error "Not on text part of BibTeX field"))
3726 (setq done t start nil end nil
)))))
3728 (list name start end
)))))
3730 (defun bibtex-remove-OPT-or-ALT ()
3731 "Remove the string starting optional/alternative fields.
3732 Align text and go thereafter to end of text."
3734 (bibtex-inside-field)
3735 (let ((case-fold-search t
)
3736 (bounds (bibtex-enclosing-field)))
3738 (goto-char (bibtex-start-of-name-in-field bounds
))
3739 (when (looking-at "OPT\\|ALT")
3740 (delete-region (match-beginning 0) (match-end 0))
3741 ;; make field non-OPT
3742 (search-forward "=")
3744 (delete-horizontal-space)
3745 (if bibtex-align-at-equal-sign
3746 (indent-to-column (- bibtex-text-indentation
2))
3748 (search-forward "=")
3749 (delete-horizontal-space)
3750 (if bibtex-align-at-equal-sign
3752 (indent-to-column bibtex-text-indentation
))))
3753 (bibtex-inside-field)))
3755 (defun bibtex-remove-delimiters ()
3756 "Remove \"\" or {} around current BibTeX field text."
3758 ;; `bibtex-find-text-internal' issues an error message if bounds is nil.
3759 (let* ((bounds (bibtex-find-text-internal nil t
))
3760 (start (nth 1 bounds
))
3761 (end (nth 2 bounds
)))
3762 (if (memq (char-before end
) '(?\
} ?
\"))
3763 (delete-region (1- end
) end
))
3764 (if (memq (char-after start
) '(?\
{ ?
\"))
3765 (delete-region start
(1+ start
)))))
3767 (defun bibtex-kill-field (&optional copy-only
)
3768 "Kill the entire enclosing BibTeX field.
3769 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
3770 but do not actually kill it."
3773 (bibtex-inside-field)
3774 (let* ((case-fold-search t
)
3775 (bounds (bibtex-enclosing-field))
3776 (end (bibtex-end-of-field bounds
))
3777 (beg (bibtex-start-of-field bounds
)))
3779 (skip-chars-forward ",")
3780 (push (list (bibtex-name-in-field bounds
) nil
3781 (bibtex-text-in-field-bounds bounds
))
3782 bibtex-field-kill-ring
)
3783 (if (> (length bibtex-field-kill-ring
) bibtex-field-kill-ring-max
)
3784 (setcdr (nthcdr (1- bibtex-field-kill-ring-max
)
3785 bibtex-field-kill-ring
)
3787 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring
)
3789 (delete-region beg end
))))
3790 (setq bibtex-last-kill-command
'field
))
3792 (defun bibtex-copy-field-as-kill ()
3793 "Copy the BibTeX field at point to the kill ring."
3795 (bibtex-kill-field t
))
3797 (defun bibtex-kill-entry (&optional copy-only
)
3798 "Kill the entire enclosing BibTeX entry.
3799 With prefix arg COPY-ONLY, copy the current entry to `bibtex-entry-kill-ring',
3800 but do not actually kill it."
3803 (let* ((case-fold-search t
)
3804 (beg (bibtex-beginning-of-entry))
3805 (end (progn (bibtex-end-of-entry)
3806 (if (re-search-forward
3807 bibtex-entry-maybe-empty-head nil
'move
)
3808 (goto-char (match-beginning 0)))
3810 (push (buffer-substring-no-properties beg end
)
3811 bibtex-entry-kill-ring
)
3812 (if (> (length bibtex-entry-kill-ring
) bibtex-entry-kill-ring-max
)
3813 (setcdr (nthcdr (1- bibtex-entry-kill-ring-max
)
3814 bibtex-entry-kill-ring
)
3816 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring
)
3818 (delete-region beg end
))))
3819 (setq bibtex-last-kill-command
'entry
))
3821 (defun bibtex-copy-entry-as-kill ()
3822 "Copy the entire enclosing BibTeX entry to `bibtex-entry-kill-ring'."
3824 (bibtex-kill-entry t
))
3826 (defun bibtex-yank (&optional n
)
3827 "Reinsert the last BibTeX item.
3828 More precisely, reinsert the field or entry killed or yanked most recently.
3829 With argument N, reinsert the Nth most recently killed BibTeX item.
3830 See also the command \\[bibtex-yank-pop]."
3832 (bibtex-insert-kill (1- n
))
3833 (setq this-command
'bibtex-yank
))
3835 (defun bibtex-yank-pop (n)
3836 "Replace just-yanked killed BibTeX item with a different item.
3837 This command is allowed only immediately after a `bibtex-yank' or a
3838 `bibtex-yank-pop'. At such a time, the region contains a reinserted
3839 previously killed BibTeX item. `bibtex-yank-pop' deletes that item
3840 and inserts in its place a different killed BibTeX item.
3842 With no argument, the previous kill is inserted.
3843 With argument N, insert the Nth previous kill.
3844 If N is negative, this is a more recent kill.
3846 The sequence of kills wraps around, so that after the oldest one
3847 comes the newest one."
3849 (unless (eq last-command
'bibtex-yank
)
3850 (error "Previous command was not a BibTeX yank"))
3851 (setq this-command
'bibtex-yank
)
3852 (let ((inhibit-read-only t
))
3853 (delete-region (point) (mark t
))
3854 (bibtex-insert-kill n
)))
3856 (defun bibtex-empty-field ()
3857 "Delete the text part of the current field, replace with empty text."
3859 (bibtex-inside-field)
3860 (let ((bounds (bibtex-enclosing-field)))
3861 (goto-char (bibtex-start-of-text-in-field bounds
))
3862 (delete-region (point) (bibtex-end-of-text-in-field bounds
))
3863 (insert (bibtex-field-left-delimiter)
3864 (bibtex-field-right-delimiter))
3865 (bibtex-find-text t nil bibtex-help-message
)))
3867 (defun bibtex-pop-previous (arg)
3868 "Replace text of current field with the similar field in previous entry.
3869 With arg, goes up ARG entries. Repeated, goes up so many times. May be
3870 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
3872 (bibtex-pop arg
'previous
))
3874 (defun bibtex-pop-next (arg)
3875 "Replace text of current field with the text of similar field in next entry.
3876 With arg, goes down ARG entries. Repeated, goes down so many times. May be
3877 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3879 (bibtex-pop arg
'next
))
3881 (defun bibtex-clean-entry (&optional new-key called-by-reformat
)
3882 "Finish editing the current BibTeX entry and clean it up.
3883 Check that no required fields are empty and formats entry dependent
3884 on the value of `bibtex-entry-format'.
3885 If the reference key of the entry is empty or a prefix argument is given,
3886 calculate a new reference key. (Note: this works only if fields in entry
3887 begin on separate lines prior to calling `bibtex-clean-entry' or if
3888 'realign is contained in `bibtex-entry-format'.)
3889 Don't call `bibtex-clean-entry' on @Preamble entries.
3890 At end of the cleaning process, the functions in
3891 `bibtex-clean-entry-hook' are called with region narrowed to entry."
3892 ;; Opt. arg called-by-reformat is t if bibtex-clean-entry
3893 ;; is called by bibtex-reformat
3895 (let ((case-fold-search t
)
3896 (start (bibtex-beginning-of-entry))
3897 (_ (looking-at bibtex-any-entry-maybe-empty-head
))
3898 (entry-type (bibtex-type-in-head))
3899 (key (bibtex-key-in-head)))
3901 (cond ((bibtex-string= entry-type
"preamble")
3902 ;; (bibtex-format-preamble)
3903 (error "No clean up of @Preamble entries"))
3904 ((bibtex-string= entry-type
"string")
3905 (setq entry-type
'string
))
3906 ;; (bibtex-format-string)
3907 (t (bibtex-format-entry)))
3909 (when (or new-key
(not key
))
3910 (setq key
(bibtex-generate-autokey))
3911 ;; Sometimes bibtex-generate-autokey returns an empty string
3912 (if (or bibtex-autokey-edit-before-use
(string= "" key
))
3913 (setq key
(if (eq entry-type
'string
)
3914 (bibtex-read-string-key key
)
3915 (bibtex-read-key "Key to use: " key
))))
3917 (re-search-forward (if (eq entry-type
'string
)
3918 bibtex-string-maybe-empty-head
3919 bibtex-entry-maybe-empty-head
))
3920 (if (match-beginning bibtex-key-in-head
)
3921 (delete-region (match-beginning bibtex-key-in-head
)
3922 (match-end bibtex-key-in-head
)))
3925 (unless called-by-reformat
3926 (let* ((end (save-excursion
3927 (bibtex-end-of-entry)
3928 (if (re-search-forward
3929 bibtex-entry-maybe-empty-head nil
'move
)
3930 (goto-char (match-beginning 0)))
3932 (entry (buffer-substring start end
))
3933 ;; include the crossref key in index
3934 (index (let ((bibtex-maintain-sorted-entries 'crossref
))
3935 (bibtex-entry-index))) ; moves point to end of head
3938 (if (and bibtex-maintain-sorted-entries
3939 (not (and bibtex-sort-ignore-string-entries
3940 (eq entry-type
'string
))))
3942 (delete-region start end
)
3943 (setq error
(not (bibtex-prepare-new-entry index
))
3944 start
(point)) ; update start
3945 (save-excursion (insert entry
)))
3946 (bibtex-find-entry key
)
3947 (setq error
(or (/= (point) start
)
3948 (bibtex-find-entry key nil end
))))
3950 (error "New inserted entry yields duplicate key"))
3951 (dolist (buffer (bibtex-files-expand))
3952 (with-current-buffer buffer
3953 (if (cdr (assoc-string key bibtex-reference-keys
))
3954 (error "Duplicate key in %s" (buffer-file-name)))))
3956 ;; Only update the list of keys if it has been built already.
3957 (cond ((eq entry-type
'string
)
3958 (if (and (listp bibtex-strings
)
3959 (not (assoc key bibtex-strings
)))
3960 (push (cons key
(bibtex-text-in-string
3961 (save-excursion (bibtex-parse-string)) t
))
3963 ;; We have a normal entry.
3964 ((listp bibtex-reference-keys
)
3965 (cond ((not (assoc key bibtex-reference-keys
))
3966 (push (cons key t
) bibtex-reference-keys
))
3967 ((not (cdr (assoc key bibtex-reference-keys
)))
3968 ;; Turn a crossref key into a header key
3969 (setq bibtex-reference-keys
3971 (delete (list key
) bibtex-reference-keys
)))))
3972 ;; Handle crossref key.
3973 (if (and (nth 1 index
)
3974 (not (assoc (nth 1 index
) bibtex-reference-keys
)))
3975 (push (list (nth 1 index
)) bibtex-reference-keys
)))))
3978 (if bibtex-clean-entry-hook
3981 (bibtex-narrow-to-entry)
3982 (run-hooks 'bibtex-clean-entry-hook
)))))))
3984 (defun bibtex-fill-field-bounds (bounds justify
&optional move
)
3985 "Fill BibTeX field delimited by BOUNDS.
3986 If JUSTIFY is non-nil justify as well.
3987 If optional arg MOVE is non-nil move point to end of field."
3988 (let ((end-field (copy-marker (bibtex-end-of-field bounds
))))
3989 (goto-char (bibtex-start-of-field bounds
))
3993 (bibtex-delete-whitespace)
3996 (indent-to-column (+ bibtex-entry-offset
3997 bibtex-field-indentation
))
3998 (re-search-forward "[ \t\n]*=" end-field
)
4001 (if bibtex-align-at-equal-sign
4003 (+ bibtex-entry-offset
(- bibtex-text-indentation
2)))
4006 (bibtex-delete-whitespace)
4007 (if bibtex-align-at-equal-sign
4009 (indent-to-column bibtex-text-indentation
)))
4010 (re-search-forward "[ \t\n]*=[ \t\n]*" end-field
))
4011 ;; Paragraphs within fields are not preserved. Bother?
4012 (fill-region-as-paragraph (line-beginning-position) end-field
4013 default-justification nil
(point))
4014 (if move
(goto-char end-field
))))
4016 (defun bibtex-fill-field (&optional justify
)
4017 "Like \\[fill-paragraph], but fill current BibTeX field.
4018 Optional prefix arg JUSTIFY non-nil means justify as well.
4019 In BibTeX mode this function is bound to `fill-paragraph-function'."
4021 (let ((pnt (copy-marker (point)))
4022 (bounds (bibtex-enclosing-field)))
4024 (bibtex-fill-field-bounds bounds justify
)
4027 (defun bibtex-fill-entry ()
4028 "Fill current BibTeX entry.
4029 Realign entry, so that every field starts on a separate line. Field
4030 names appear in column `bibtex-field-indentation', field text starts in
4031 column `bibtex-text-indentation' and continuation lines start here, too.
4032 If `bibtex-align-at-equal-sign' is non-nil, align equal signs, too."
4034 (let ((pnt (copy-marker (point)))
4035 (end (copy-marker (bibtex-end-of-entry)))
4037 (bibtex-beginning-of-entry)
4038 (bibtex-delete-whitespace)
4039 (indent-to-column bibtex-entry-offset
)
4040 (while (setq bounds
(bibtex-search-forward-field bibtex-field-name end
))
4041 (bibtex-fill-field-bounds bounds t t
))
4042 (if (looking-at ",")
4044 (bibtex-delete-whitespace)
4047 (indent-to-column bibtex-entry-offset
)
4050 (defun bibtex-realign ()
4051 "Realign BibTeX entries such that they are separated by one blank line."
4052 (goto-char (point-min))
4053 (let ((case-fold-search t
)
4054 (entry-type (concat "[ \t\n]*\\(" bibtex-entry-type
"\\)")))
4055 ;; No blank lines prior to the first entry if there no
4056 ;; non-white characters in front of it.
4057 (when (looking-at entry-type
)
4058 (replace-match "\\1"))
4059 ;; Entries are separated by one blank line.
4060 (while (re-search-forward entry-type nil t
)
4061 (replace-match "\n\n\\1"))
4062 ;; One blank line past the last entry if it is followed by
4063 ;; non-white characters, no blank line otherwise.
4065 (when (re-search-forward bibtex-entry-type nil t
)
4066 (bibtex-end-of-entry)
4067 (bibtex-delete-whitespace)
4068 (open-line (if (eobp) 1 2)))))
4070 (defun bibtex-reformat (&optional read-options
)
4071 "Reformat all BibTeX entries in buffer or region.
4072 Without prefix argument, reformatting is based on `bibtex-entry-format'.
4073 With prefix argument, read options for reformatting from minibuffer.
4074 With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again.
4075 If mark is active reformat entries in region, if not in whole buffer."
4077 (let* ((pnt (point))
4078 (use-previous-options
4079 (and (equal (prefix-numeric-value read-options
) 16)
4080 (or bibtex-reformat-previous-options
4081 bibtex-reformat-previous-reference-keys
)))
4082 (bibtex-entry-format
4084 (if use-previous-options
4085 bibtex-reformat-previous-options
4086 (setq bibtex-reformat-previous-options
4087 (mapcar (lambda (option)
4088 (if (y-or-n-p (car option
)) (cdr option
)))
4089 `(("Realign entries (recommended)? " .
'realign
)
4090 ("Remove empty optional and alternative fields? " .
'opts-or-alts
)
4091 ("Remove delimiters around pure numerical fields? " .
'numerical-fields
)
4092 (,(concat (if bibtex-comma-after-last-field
"Insert" "Remove")
4093 " comma at end of entry? ") .
'last-comma
)
4094 ("Replace double page dashes by single ones? " .
'page-dashes
)
4095 ("Inherit booktitle? " .
'inherit-booktitle
)
4096 ("Force delimiters? " .
'delimiters
)
4097 ("Unify case of entry types and field names? " .
'unify-case
))))))
4098 ;; Do not include required-fields because `bibtex-reformat'
4099 ;; cannot handle the error messages of `bibtex-format-entry'.
4100 ;; Use `bibtex-validate' to check for required fields.
4101 ((eq t bibtex-entry-format
)
4102 '(realign opts-or-alts numerical-fields delimiters
4103 last-comma page-dashes unify-case inherit-booktitle
))
4105 (remove 'required-fields
(push 'realign bibtex-entry-format
)))))
4106 (reformat-reference-keys
4108 (if use-previous-options
4109 bibtex-reformat-previous-reference-keys
4110 (setq bibtex-reformat-previous-reference-keys
4111 (y-or-n-p "Generate new reference keys automatically? ")))))
4112 (bibtex-sort-ignore-string-entries t
)
4113 bibtex-autokey-edit-before-use
)
4116 (narrow-to-region (if mark-active
(region-beginning) (point-min))
4117 (if mark-active
(region-end) (point-max)))
4118 (if (memq 'realign bibtex-entry-format
)
4120 (bibtex-progress-message "Formatting" 1)
4121 (bibtex-map-entries (lambda (key beg end
)
4122 (bibtex-progress-message)
4123 (bibtex-clean-entry reformat-reference-keys t
)))
4124 (bibtex-progress-message 'done
))
4125 (when reformat-reference-keys
4126 (kill-local-variable 'bibtex-reference-keys
)
4127 (when bibtex-maintain-sorted-entries
4128 (bibtex-progress-message "Sorting" 1)
4129 (bibtex-sort-buffer)
4130 (bibtex-progress-message 'done
)))
4133 (defun bibtex-convert-alien (&optional read-options
)
4134 "Make an alien BibTeX buffer fully usable by BibTeX mode.
4135 If a file does not conform with all standards used by BibTeX mode,
4136 some of the high-level features of BibTeX mode are not available.
4137 This function tries to convert current buffer to conform with these standards.
4138 With prefix argument READ-OPTIONS non-nil, read options for reformatting
4139 entries from minibuffer."
4141 (message "Starting to validate buffer...")
4145 "If errors occur, correct them and call `bibtex-convert-alien' again")
4147 (deactivate-mark) ; So bibtex-validate works on the whole buffer.
4148 (when (let (bibtex-maintain-sorted-entries)
4150 (message "Starting to reformat entries...")
4152 (bibtex-reformat read-options
)
4153 (goto-char (point-max))
4154 (message "Buffer is now parsable. Please save it.")))
4156 (defun bibtex-complete ()
4157 "Complete word fragment before point according to context.
4158 If point is inside key or crossref field perform key completion based on
4159 `bibtex-reference-keys'. Inside a month field perform key completion
4160 based on `bibtex-predefined-month-strings'. Inside any other field
4161 \(including a String or Preamble definition) perform string completion
4162 based on `bibtex-strings'.
4163 An error is signaled if point is outside key or BibTeX field."
4166 (case-fold-search t
)
4167 (bibtex-string-empty-key t
)
4170 (if (and (setq bounds
(bibtex-enclosing-field t
))
4171 (>= pnt
(bibtex-start-of-text-in-field bounds
))
4172 (<= pnt
(bibtex-end-of-text-in-field bounds
)))
4173 (setq name
(bibtex-name-in-field bounds t
)
4174 compl
(cond ((bibtex-string= name
"crossref")
4175 ;; point is in crossref field
4177 ((bibtex-string= name
"month")
4178 ;; point is in month field
4179 bibtex-predefined-month-strings
)
4180 ;; point is in other field
4181 (t (bibtex-strings))))
4182 (bibtex-beginning-of-entry)
4183 (cond ((setq bounds
(bibtex-parse-string))
4184 ;; point is inside a @String key
4185 (cond ((and (>= pnt
(nth 1 (car bounds
)))
4186 (<= pnt
(nth 2 (car bounds
))))
4187 (setq compl
'string
))
4188 ;; point is inside a @String field
4189 ((and (>= pnt
(bibtex-start-of-text-in-string bounds
))
4190 (<= pnt
(bibtex-end-of-text-in-string bounds
)))
4191 (setq compl
(bibtex-strings)))))
4192 ;; point is inside a @Preamble field
4193 ((and (bibtex-preamble-prefix t
)
4194 (setq bounds
(bibtex-parse-field-text))
4195 (>= pnt
(car bounds
))
4196 (<= pnt
(nth 1 bounds
)))
4197 (setq compl
(bibtex-strings)))
4198 ((and (looking-at bibtex-entry-maybe-empty-head
)
4199 ;; point is inside a key
4200 (or (and (match-beginning bibtex-key-in-head
)
4201 (>= pnt
(match-beginning bibtex-key-in-head
))
4202 (<= pnt
(match-end bibtex-key-in-head
)))
4203 ;; or point is on empty key
4204 (and (not (match-beginning bibtex-key-in-head
))
4205 (= pnt
(match-end 0)))))
4206 (setq compl
'key
)))))
4208 (cond ((eq compl
'key
)
4209 ;; key completion: no cleanup needed
4210 (let (completion-ignore-case)
4211 (bibtex-complete-internal (bibtex-global-key-alist))))
4213 ((eq compl
'crossref-key
)
4214 ;; crossref key completion
4215 (let (completion-ignore-case)
4216 (setq choose-completion-string-functions
4217 (lambda (choice buffer mini-p base-size
)
4218 (let ((choose-completion-string-functions nil
))
4219 (choose-completion-string choice buffer base-size
))
4220 (bibtex-complete-crossref-cleanup choice
)
4221 ;; return t (needed by choose-completion-string-functions)
4223 (bibtex-complete-crossref-cleanup (bibtex-complete-internal
4224 (bibtex-global-key-alist)))))
4227 ;; string key completion: no cleanup needed
4228 (let ((completion-ignore-case t
))
4229 (bibtex-complete-internal bibtex-strings
)))
4232 ;; string completion
4233 (let ((completion-ignore-case t
))
4234 (setq choose-completion-string-functions
4235 `(lambda (choice buffer mini-p base-size
)
4236 (let ((choose-completion-string-functions nil
))
4237 (choose-completion-string choice buffer base-size
))
4238 (bibtex-complete-string-cleanup choice
',compl
)
4239 ;; return t (needed by choose-completion-string-functions)
4241 (bibtex-complete-string-cleanup (bibtex-complete-internal compl
)
4244 (t (error "Point outside key or BibTeX field")))))
4246 (defun bibtex-Article ()
4247 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
4249 (bibtex-entry "Article"))
4251 (defun bibtex-Book ()
4252 "Insert a new BibTeX @Book entry; see also `bibtex-entry'."
4254 (bibtex-entry "Book"))
4256 (defun bibtex-Booklet ()
4257 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'."
4259 (bibtex-entry "Booklet"))
4261 (defun bibtex-InBook ()
4262 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'."
4264 (bibtex-entry "InBook"))
4266 (defun bibtex-InCollection ()
4267 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'."
4269 (bibtex-entry "InCollection"))
4271 (defun bibtex-InProceedings ()
4272 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'."
4274 (bibtex-entry "InProceedings"))
4276 (defun bibtex-Manual ()
4277 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'."
4279 (bibtex-entry "Manual"))
4281 (defun bibtex-MastersThesis ()
4282 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'."
4284 (bibtex-entry "MastersThesis"))
4286 (defun bibtex-Misc ()
4287 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'."
4289 (bibtex-entry "Misc"))
4291 (defun bibtex-PhdThesis ()
4292 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'."
4294 (bibtex-entry "PhdThesis"))
4296 (defun bibtex-Proceedings ()
4297 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'."
4299 (bibtex-entry "Proceedings"))
4301 (defun bibtex-TechReport ()
4302 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'."
4304 (bibtex-entry "TechReport"))
4306 (defun bibtex-Unpublished ()
4307 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'."
4309 (bibtex-entry "Unpublished"))
4311 (defun bibtex-String (&optional key
)
4312 "Insert a new BibTeX @String entry with key KEY."
4313 (interactive (list (bibtex-read-string-key)))
4314 (let ((bibtex-maintain-sorted-entries
4315 (unless bibtex-sort-ignore-string-entries
4316 bibtex-maintain-sorted-entries
))
4318 (unless (bibtex-prepare-new-entry (list key nil
"String"))
4319 (error "Entry with key `%s' already exists" key
))
4320 (if (zerop (length key
)) (setq key nil
))
4321 (indent-to-column bibtex-entry-offset
)
4323 (bibtex-entry-left-delimiter))
4326 (setq endpos
(point)))
4328 (bibtex-field-left-delimiter))
4330 (setq endpos
(point)))
4331 (insert (bibtex-field-right-delimiter)
4332 (bibtex-entry-right-delimiter)
4334 (goto-char endpos
)))
4336 (defun bibtex-Preamble ()
4337 "Insert a new BibTeX @Preamble entry."
4339 (bibtex-move-outside-of-entry)
4340 (indent-to-column bibtex-entry-offset
)
4342 (bibtex-entry-left-delimiter)
4343 (bibtex-field-left-delimiter))
4344 (let ((endpos (point)))
4345 (insert (bibtex-field-right-delimiter)
4346 (bibtex-entry-right-delimiter)
4348 (goto-char endpos
)))
4350 (defun bibtex-url (&optional pos
)
4351 "Browse a URL for the BibTeX entry at point.
4352 Optional POS is the location of the BibTeX entry.
4353 The URL is generated using the schemes defined in `bibtex-generate-url-list'
4354 \(see there\). Then the URL is passed to `browse-url'."
4357 (if pos
(goto-char pos
))
4358 (bibtex-beginning-of-entry)
4359 ;; Always remove field delimiters
4360 (let ((fields-alist (bibtex-parse-entry t
))
4361 ;; Always ignore case,
4362 (case-fold-search t
)
4363 (lst bibtex-generate-url-list
)
4364 field url scheme obj fmt
)
4365 (while (setq scheme
(pop lst
))
4366 (when (and (setq field
(cdr (assoc-string (caar scheme
)
4368 (string-match (cdar scheme
) field
))
4371 url
(if (null scheme
) (match-string 0 field
)
4372 (if (stringp (car scheme
))
4373 (setq fmt
(pop scheme
)))
4374 (dolist (step scheme
)
4375 (setq field
(cdr (assoc-string (car step
) fields-alist t
)))
4376 (if (string-match (nth 1 step
) field
)
4377 (setq field
(cond ((functionp (nth 2 step
))
4378 (funcall (nth 2 step
) field
))
4379 ((numberp (nth 2 step
))
4380 (match-string (nth 2 step
) field
))
4382 (replace-match (nth 2 step
) t nil field
))))
4383 ;; If the scheme is set up correctly,
4384 ;; we should never reach this point
4385 (error "Match failed: %s" field
))
4387 (if fmt
(apply 'format fmt
(nreverse obj
))
4388 (apply 'concat
(nreverse obj
)))))
4389 (browse-url (message "%s" url
))))
4390 (unless url
(message "No URL known.")))))
4393 ;; Make BibTeX a Feature
4397 ;; arch-tag: ee2be3af-caad-427f-b42a-d20fad630d04
4398 ;;; bibtex.el ends here