1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2004
4 ;; 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., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, 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 explicitely 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 (defvar bibtex-autokey-transcriptions
555 '(;; language specific characters
556 ("\\\\aa" .
"a") ; \aa -> a
557 ("\\\\AA" .
"A") ; \AA -> A
558 ("\\\"a\\|\\\\\\\"a\\|\\\\ae" .
"ae") ; "a,\"a,\ae -> ae
559 ("\\\"A\\|\\\\\\\"A\\|\\\\AE" .
"Ae") ; "A,\"A,\AE -> Ae
560 ("\\\\i" .
"i") ; \i -> i
561 ("\\\\j" .
"j") ; \j -> j
562 ("\\\\l" .
"l") ; \l -> l
563 ("\\\\L" .
"L") ; \L -> L
564 ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" .
"oe") ; "o,\"o,\o,\oe -> oe
565 ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" .
"Oe") ; "O,\"O,\O,\OE -> Oe
566 ("\\\"s\\|\\\\\\\"s\\|\\\\3" .
"ss") ; "s,\"s,\3 -> ss
567 ("\\\"u\\|\\\\\\\"u" .
"ue") ; "u,\"u -> ue
568 ("\\\"U\\|\\\\\\\"U" .
"Ue") ; "U,\"U -> Ue
570 ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" .
"")
571 ;; braces, quotes, concatenation.
574 ("\\\\?[ \t\n]+\\|~" .
" "))
575 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
576 Used by the default values of `bibtex-autokey-name-change-strings' and
577 `bibtex-autokey-titleword-change-strings'. Defaults to translating some
578 language specific characters to their ASCII transcriptions, and
579 removing any character accents.")
581 (defcustom bibtex-autokey-name-change-strings
582 bibtex-autokey-transcriptions
583 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
584 Any part of a name matching OLD-REGEXP is replaced by NEW-STRING.
585 Case is significant in OLD-REGEXP. All regexps are tried in the
586 order in which they appear in the list.
587 See `bibtex-generate-autokey' for details."
588 :group
'bibtex-autokey
589 :type
'(repeat (cons (regexp :tag
"Old")
590 (string :tag
"New"))))
592 (defcustom bibtex-autokey-name-case-convert
'downcase
593 "Function called for each name to perform case conversion.
594 See `bibtex-generate-autokey' for details."
595 :group
'bibtex-autokey
596 :type
'(choice (const :tag
"Preserve case" identity
)
597 (const :tag
"Downcase" downcase
)
598 (const :tag
"Capitalize" capitalize
)
599 (const :tag
"Upcase" upcase
)
600 (function :tag
"Conversion function")))
602 (defcustom bibtex-autokey-name-length
'infty
603 "Number of characters from name to incorporate into key.
604 If this is set to anything but a number, all characters are used.
605 See `bibtex-generate-autokey' for details."
606 :group
'bibtex-autokey
607 :type
'(choice (const :tag
"All" infty
)
610 (defcustom bibtex-autokey-name-separator
""
611 "String that comes between any two names in the key.
612 See `bibtex-generate-autokey' for details."
613 :group
'bibtex-autokey
616 (defcustom bibtex-autokey-year-length
2
617 "Number of rightmost digits from the year field to incorporate into key.
618 See `bibtex-generate-autokey' for details."
619 :group
'bibtex-autokey
622 (defcustom bibtex-autokey-use-crossref t
623 "If non-nil use fields from crossreferenced entry if necessary.
624 If this variable is non-nil and some field has no entry, but a
625 valid crossref entry, the field from the crossreferenced entry is used.
626 See `bibtex-generate-autokey' for details."
627 :group
'bibtex-autokey
630 (defcustom bibtex-autokey-titlewords
5
631 "Number of title words to use for the automatically generated reference key.
632 If this is set to anything but a number, all title words are used.
633 Possibly more words from the title are used according to
634 `bibtex-autokey-titlewords-stretch'.
635 See `bibtex-generate-autokey' for details."
636 :group
'bibtex-autokey
637 :type
'(choice (const :tag
"All" infty
)
640 (defcustom bibtex-autokey-title-terminators
"[.!?:;]\\|--"
641 "Regexp defining the termination of the main part of the title.
642 Case of the regexps is ignored. See `bibtex-generate-autokey' for details."
643 :group
'bibtex-autokey
646 (defcustom bibtex-autokey-titlewords-stretch
2
647 "Number of words that can additionally be used from the title.
648 These words are used only, if a sentence from the title can be ended then.
649 See `bibtex-generate-autokey' for details."
650 :group
'bibtex-autokey
653 (defcustom bibtex-autokey-titleword-ignore
654 '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das"
655 "[^A-Z].*" ".*[^A-Z0-9].*")
656 "Determines words from the title that are not to be used in the key.
657 Each item of the list is a regexp. If a word of the title matchs a
658 regexp from that list, it is not included in the title part of the key.
659 See `bibtex-generate-autokey' for details."
660 :group
'bibtex-autokey
661 :type
'(repeat regexp
))
663 (defcustom bibtex-autokey-titleword-case-convert
'downcase
664 "Function called for each titleword to perform case conversion.
665 See `bibtex-generate-autokey' for details."
666 :group
'bibtex-autokey
667 :type
'(choice (const :tag
"Preserve case" identity
)
668 (const :tag
"Downcase" downcase
)
669 (const :tag
"Capitalize" capitalize
)
670 (const :tag
"Upcase" upcase
)
671 (function :tag
"Conversion function")))
673 (defcustom bibtex-autokey-titleword-abbrevs nil
674 "Determines exceptions to the usual abbreviation mechanism.
675 An alist of (OLD-REGEXP . NEW-STRING) pairs. Case is ignored
676 in matching against OLD-REGEXP, and the first matching pair is used.
677 See `bibtex-generate-autokey' for details."
678 :group
'bibtex-autokey
679 :type
'(repeat (cons (regexp :tag
"Old")
680 (string :tag
"New"))))
682 (defcustom bibtex-autokey-titleword-change-strings
683 bibtex-autokey-transcriptions
684 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
685 Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
686 Case is significant in OLD-REGEXP. All regexps are tried in the
687 order in which they appear in the list.
688 See `bibtex-generate-autokey' for details."
689 :group
'bibtex-autokey
690 :type
'(repeat (cons (regexp :tag
"Old")
691 (string :tag
"New"))))
693 (defcustom bibtex-autokey-titleword-length
5
694 "Number of characters from title words to incorporate into key.
695 If this is set to anything but a number, all characters are used.
696 See `bibtex-generate-autokey' for details."
697 :group
'bibtex-autokey
698 :type
'(choice (const :tag
"All" infty
)
701 (defcustom bibtex-autokey-titleword-separator
"_"
702 "String to be put between the title words.
703 See `bibtex-generate-autokey' for details."
704 :group
'bibtex-autokey
707 (defcustom bibtex-autokey-name-year-separator
""
708 "String to be put between name part and year part of key.
709 See `bibtex-generate-autokey' for details."
710 :group
'bibtex-autokey
713 (defcustom bibtex-autokey-year-title-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-edit-before-use t
720 "If non-nil, user is allowed to edit the generated key before it is used."
721 :group
'bibtex-autokey
724 (defcustom bibtex-autokey-before-presentation-function nil
725 "If non-nil, function to call before generated key is presented.
726 The function must take one argument (the automatically generated key),
727 and must return a string (the key to use)."
728 :group
'bibtex-autokey
729 :type
'(choice (const nil
) function
))
731 (defcustom bibtex-entry-offset
0
732 "Offset for BibTeX entries.
733 Added to the value of all other variables which determine colums."
737 (defcustom bibtex-field-indentation
2
738 "Starting column for the name part in BibTeX fields."
742 (defcustom bibtex-text-indentation
743 (+ bibtex-field-indentation
744 (length "organization = "))
745 "Starting column for the text part in BibTeX fields.
746 Should be equal to the space needed for the longest name part."
750 (defcustom bibtex-contline-indentation
751 (+ bibtex-text-indentation
1)
752 "Starting column for continuation lines of BibTeX fields."
756 (defcustom bibtex-align-at-equal-sign nil
757 "If non-nil, align fields at equal sign instead of field text.
758 If non-nil, the column for the equal sign is the value of
759 `bibtex-text-indentation', minus 2."
763 (defcustom bibtex-comma-after-last-field nil
764 "If non-nil, a comma is put at end of last field in the entry template."
768 (defcustom bibtex-autoadd-commas t
769 "If non-nil automatically add missing commas at end of BibTeX fields."
773 (defcustom bibtex-autofill-types
'("Proceedings")
774 "Automatically fill fields if possible for those BibTeX entry types."
776 :type
'(repeat string
))
778 (defcustom bibtex-summary-function
'bibtex-summary
779 "Function to call for generating a one-line summary of a BibTeX entry.
780 It takes one argument, the key of the entry.
781 Used by `bibtex-complete-crossref-cleanup' and `bibtex-copy-summary-as-kill'."
783 :type
'(choice (const :tag
"Default" bibtex-summary
)
784 (function :tag
"Personalized function")))
786 (defcustom bibtex-generate-url-list
787 '((("url" .
".*:.*"))
788 ;; Example of a complex setup.
789 (("journal" .
"\\<\\(PR[ABCDEL]?\\|RMP\\)\\>")
790 "http://link.aps.org/abstract/"
791 ("journal" ".*" downcase
)
795 ("pages" "\\`\\([0-9]+\\)" 1)))
796 "List of schemes for generating the URL of a BibTeX entry.
797 These schemes are used by `bibtex-url'.
799 Each scheme is of the form ((FIELD . REGEXP) STEP...).
801 FIELD is a field name as returned by `bibtex-parse-entry'.
802 REGEXP is matched against the text of FIELD. If the match succeeds, then
803 this scheme is used. If no STEPs are specified the matched text is used
804 as the URL, otherwise the URL is built by concatenating the STEPs.
806 A STEP can be a string or a list (FIELD REGEXP REPLACE) in which case
807 the text of FIELD is matched against REGEXP, and is replaced with REPLACE.
808 REPLACE can be a string, or a number (which selects the corresponding submatch)
809 or a function called with the field's text as argument and with the
810 `match-data' properly set.
812 Case is always ignored. Always remove the field delimiters."
816 (cons :tag
"Matcher" :extra-offset
4
817 (string :tag
"BibTeX field")
818 (regexp :tag
"Regexp"))
819 (repeat :tag
"Steps to generate URL" :inline t
821 (string :tag
"Literal text")
822 (list (string :tag
"BibTeX field")
823 (regexp :tag
"Regexp")
824 (choice (string :tag
"Replacement")
825 (integer :tag
"Sub-match")
826 (function :tag
"Filter"))))))))
828 ;; bibtex-font-lock-keywords is a user option as well, but since the
829 ;; patterns used to define this variable are defined in a later
830 ;; section of this file, it is defined later.
833 ;; Syntax Table, Keybindings and BibTeX Entry List
834 (defvar bibtex-mode-syntax-table
835 (let ((st (make-syntax-table)))
836 (modify-syntax-entry ?
\" "\"" st
)
837 (modify-syntax-entry ?$
"$$ " st
)
838 (modify-syntax-entry ?%
"< " st
)
839 (modify-syntax-entry ?
' "w " st
)
840 (modify-syntax-entry ?
@ "w " st
)
841 (modify-syntax-entry ?
\\ "\\" st
)
842 (modify-syntax-entry ?
\f "> " st
)
843 (modify-syntax-entry ?
\n "> " st
)
844 ;; Keys cannot have = in them (wrong font-lock of @string{foo=bar}).
845 (modify-syntax-entry ?
= "." st
)
846 (modify-syntax-entry ?~
" " st
)
848 "Syntax table used in BibTeX mode buffers.")
850 (defvar bibtex-mode-map
851 (let ((km (make-sparse-keymap)))
852 ;; The Key `C-c&' is reserved for reftex.el
853 (define-key km
"\t" 'bibtex-find-text
)
854 (define-key km
"\n" 'bibtex-next-field
)
855 (define-key km
"\M-\t" 'bibtex-complete
)
856 (define-key km
"\C-c\"" 'bibtex-remove-delimiters
)
857 (define-key km
"\C-c{" 'bibtex-remove-delimiters
)
858 (define-key km
"\C-c}" 'bibtex-remove-delimiters
)
859 (define-key km
"\C-c\C-c" 'bibtex-clean-entry
)
860 (define-key km
"\C-c\C-q" 'bibtex-fill-entry
)
861 (define-key km
"\C-c\C-s" 'bibtex-find-entry
)
862 (define-key km
"\C-c\C-x" 'bibtex-find-crossref
)
863 (define-key km
"\C-c\C-t" 'bibtex-copy-summary-as-kill
)
864 (define-key km
"\C-c?" 'bibtex-print-help-message
)
865 (define-key km
"\C-c\C-p" 'bibtex-pop-previous
)
866 (define-key km
"\C-c\C-n" 'bibtex-pop-next
)
867 (define-key km
"\C-c\C-k" 'bibtex-kill-field
)
868 (define-key km
"\C-c\M-k" 'bibtex-copy-field-as-kill
)
869 (define-key km
"\C-c\C-w" 'bibtex-kill-entry
)
870 (define-key km
"\C-c\M-w" 'bibtex-copy-entry-as-kill
)
871 (define-key km
"\C-c\C-y" 'bibtex-yank
)
872 (define-key km
"\C-c\M-y" 'bibtex-yank-pop
)
873 (define-key km
"\C-c\C-d" 'bibtex-empty-field
)
874 (define-key km
"\C-c\C-f" 'bibtex-make-field
)
875 (define-key km
"\C-c\C-u" 'bibtex-entry-update
)
876 (define-key km
"\C-c$" 'bibtex-ispell-abstract
)
877 (define-key km
"\M-\C-a" 'bibtex-beginning-of-entry
)
878 (define-key km
"\M-\C-e" 'bibtex-end-of-entry
)
879 (define-key km
"\C-\M-l" 'bibtex-reposition-window
)
880 (define-key km
"\C-\M-h" 'bibtex-mark-entry
)
881 (define-key km
"\C-c\C-b" 'bibtex-entry
)
882 (define-key km
"\C-c\C-rn" 'bibtex-narrow-to-entry
)
883 (define-key km
"\C-c\C-rw" 'widen
)
884 (define-key km
"\C-c\C-l" 'bibtex-url
)
885 (define-key km
"\C-c\C-o" 'bibtex-remove-OPT-or-ALT
)
886 (define-key km
"\C-c\C-e\C-i" 'bibtex-InProceedings
)
887 (define-key km
"\C-c\C-ei" 'bibtex-InCollection
)
888 (define-key km
"\C-c\C-eI" 'bibtex-InBook
)
889 (define-key km
"\C-c\C-e\C-a" 'bibtex-Article
)
890 (define-key km
"\C-c\C-e\C-b" 'bibtex-InBook
)
891 (define-key km
"\C-c\C-eb" 'bibtex-Book
)
892 (define-key km
"\C-c\C-eB" 'bibtex-Booklet
)
893 (define-key km
"\C-c\C-e\C-c" 'bibtex-InCollection
)
894 (define-key km
"\C-c\C-e\C-m" 'bibtex-Manual
)
895 (define-key km
"\C-c\C-em" 'bibtex-MastersThesis
)
896 (define-key km
"\C-c\C-eM" 'bibtex-Misc
)
897 (define-key km
"\C-c\C-e\C-p" 'bibtex-InProceedings
)
898 (define-key km
"\C-c\C-ep" 'bibtex-Proceedings
)
899 (define-key km
"\C-c\C-eP" 'bibtex-PhdThesis
)
900 (define-key km
"\C-c\C-e\M-p" 'bibtex-Preamble
)
901 (define-key km
"\C-c\C-e\C-s" 'bibtex-String
)
902 (define-key km
"\C-c\C-e\C-t" 'bibtex-TechReport
)
903 (define-key km
"\C-c\C-e\C-u" 'bibtex-Unpublished
)
905 "Keymap used in BibTeX mode.")
908 bibtex-edit-menu bibtex-mode-map
"BibTeX-Edit Menu in BibTeX mode"
910 ("Moving inside an Entry"
911 ["End of Field" bibtex-find-text t
]
912 ["Next Field" bibtex-next-field t
]
913 ["Beginning of Entry" bibtex-beginning-of-entry t
]
914 ["End of Entry" bibtex-end-of-entry t
]
916 ["Make Entry Visible" bibtex-reposition-window t
])
917 ("Moving in BibTeX Buffers"
918 ["Find Entry" bibtex-find-entry t
]
919 ["Find Crossref Entry" bibtex-find-crossref t
])
921 ("Operating on Current Field"
922 ["Fill Field" fill-paragraph t
]
923 ["Remove Delimiters" bibtex-remove-delimiters t
]
924 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t
]
925 ["Clear Field" bibtex-empty-field t
]
927 ["Kill Field" bibtex-kill-field t
]
928 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t
]
929 ["Paste Most Recently Killed Field" bibtex-yank t
]
930 ["Paste Previously Killed Field" bibtex-yank-pop t
]
932 ["Make New Field" bibtex-make-field t
]
934 ["Snatch from Similar Following Field" bibtex-pop-next t
]
935 ["Snatch from Similar Preceding Field" bibtex-pop-previous t
]
937 ["String or Key Complete" bibtex-complete t
]
939 ["Help about Current Field" bibtex-print-help-message t
])
940 ("Operating on Current Entry"
941 ["Fill Entry" bibtex-fill-entry t
]
942 ["Clean Entry" bibtex-clean-entry t
]
943 ["Update Entry" bibtex-entry-update t
]
945 ["Kill Entry" bibtex-kill-entry t
]
946 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t
]
947 ["Paste Most Recently Killed Entry" bibtex-yank t
]
948 ["Paste Previously Killed Entry" bibtex-yank-pop t
]
950 ["Copy Summary to Kill Ring" bibtex-copy-summary-as-kill t
]
951 ["Browse URL" bibtex-url t
]
953 ["Ispell Entry" bibtex-ispell-entry t
]
954 ["Ispell Entry Abstract" bibtex-ispell-abstract t
]
956 ["Narrow to Entry" bibtex-narrow-to-entry t
]
957 ["Mark Entry" bibtex-mark-entry t
]
959 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
960 (fboundp 'reftex-view-crossref-from-bibtex
)])
961 ("Operating on Buffer or Region"
962 ["Validate Entries" bibtex-validate t
]
963 ["Sort Entries" bibtex-sort-buffer t
]
964 ["Reformat Entries" bibtex-reformat t
]
965 ["Count Entries" bibtex-count-entries t
]
967 ["Convert Alien Buffer" bibtex-convert-alien t
])
968 ("Operating on Multiple Buffers"
969 ["Validate Entries" bibtex-validate-globally t
])))
972 bibtex-entry-menu bibtex-mode-map
"Entry-Types Menu in BibTeX mode"
974 ["Article in Journal" bibtex-Article t
]
975 ["Article in Conference Proceedings" bibtex-InProceedings t
]
976 ["Article in a Collection" bibtex-InCollection t
]
977 ["Chapter or Pages in a Book" bibtex-InBook t
]
978 ["Conference Proceedings" bibtex-Proceedings t
]
979 ["Book" bibtex-Book t
]
980 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t
]
981 ["PhD. Thesis" bibtex-PhdThesis t
]
982 ["Master's Thesis" bibtex-MastersThesis t
]
983 ["Technical Report" bibtex-TechReport t
]
984 ["Technical Manual" bibtex-Manual t
]
985 ["Unpublished" bibtex-Unpublished t
]
986 ["Miscellaneous" bibtex-Misc t
]
988 ["String" bibtex-String t
]
989 ["Preamble" bibtex-Preamble t
]))
992 ;; Internal Variables
994 (defvar bibtex-pop-previous-search-point nil
995 "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
997 (defvar bibtex-pop-next-search-point nil
998 "Next point where `bibtex-pop-next' starts looking for a similar entry.")
1000 (defvar bibtex-field-kill-ring nil
1001 "Ring of least recently killed fields.
1002 At most `bibtex-field-kill-ring-max' items are kept here.")
1004 (defvar bibtex-field-kill-ring-yank-pointer nil
1005 "The tail of `bibtex-field-kill-ring' whose car is the last item yanked.")
1007 (defvar bibtex-entry-kill-ring nil
1008 "Ring of least recently killed entries.
1009 At most `bibtex-entry-kill-ring-max' items are kept here.")
1011 (defvar bibtex-entry-kill-ring-yank-pointer nil
1012 "The tail of `bibtex-entry-kill-ring' whose car is the last item yanked.")
1014 (defvar bibtex-last-kill-command nil
1015 "Type of the last kill command (either 'field or 'entry).")
1017 (defvar bibtex-strings
1018 (lazy-completion-table bibtex-strings
1019 bibtex-parse-strings
(bibtex-string-files-init))
1020 "Completion table for BibTeX string keys.
1021 Initialized from `bibtex-predefined-strings' and `bibtex-string-files'.")
1022 (make-variable-buffer-local 'bibtex-strings
)
1024 (defvar bibtex-reference-keys
1025 (lazy-completion-table bibtex-reference-keys bibtex-parse-keys nil t
)
1026 "Completion table for BibTeX reference keys.
1027 The CDRs of the elements are t for header keys and nil for crossref keys.")
1028 (make-variable-buffer-local 'bibtex-reference-keys
)
1030 (defvar bibtex-buffer-last-parsed-tick nil
1031 "Value of `buffer-modified-tick' last time buffer was parsed for keys.")
1033 (defvar bibtex-parse-idle-timer nil
1034 "Stores if timer is already installed.")
1036 (defvar bibtex-progress-lastperc nil
1037 "Last reported percentage for the progress message.")
1039 (defvar bibtex-progress-lastmes nil
1040 "Last reported progress message.")
1042 (defvar bibtex-progress-interval nil
1043 "Interval for progress messages.")
1045 (defvar bibtex-key-history nil
1046 "History list for reading keys.")
1048 (defvar bibtex-entry-type-history nil
1049 "History list for reading entry types.")
1051 (defvar bibtex-field-history nil
1052 "History list for reading field names.")
1054 (defvar bibtex-reformat-previous-options nil
1055 "Last reformat options given.")
1057 (defvar bibtex-reformat-previous-reference-keys nil
1058 "Last reformat reference keys option given.")
1060 (defconst bibtex-field-name
"[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*"
1061 "Regexp matching the name part of a BibTeX field.")
1063 (defconst bibtex-entry-type
(concat "@" bibtex-field-name
)
1064 "Regexp matching the type part of a BibTeX entry.")
1066 (defconst bibtex-reference-key
"[][a-zA-Z0-9.:;?!`'/*@+|()<>&_^$-]+"
1067 "Regexp matching the reference key part of a BibTeX entry.")
1069 (defconst bibtex-field-const
"[][a-zA-Z0-9.:;?!`'/*@+=|<>&_^$-]+"
1070 "Regexp matching a BibTeX field constant.")
1072 (defconst bibtex-entry-head
1073 (concat "^[ \t]*\\("
1075 "\\)[ \t]*[({][ \t\n]*\\("
1076 bibtex-reference-key
1078 "Regexp matching the header line of a BibTeX entry.")
1080 (defconst bibtex-entry-maybe-empty-head
1081 (concat bibtex-entry-head
"?")
1082 "Regexp matching the header line of a BibTeX entry (possibly without key).")
1084 (defconst bibtex-type-in-head
1
1085 "Regexp subexpression number of the type part in `bibtex-entry-head'.")
1087 (defconst bibtex-key-in-head
2
1088 "Regexp subexpression number of the key part in `bibtex-entry-head'.")
1090 (defconst bibtex-string-maybe-empty-head
1091 (concat "^[ \t]*\\(@String\\)[ \t]*[({]\\("
1092 bibtex-reference-key
1094 "Regexp matching the header line of a BibTeX String entry.")
1096 (defconst bibtex-entry-postfix
"[ \t\n]*,?[ \t\n]*[})]"
1097 "Regexp matching the postfix of a BibTeX entry.")
1099 (defvar bibtex-known-entry-type-re
1100 (regexp-opt (mapcar 'car bibtex-entry-field-alist
))
1101 "Regexp matching the name of a BibTeX entry.")
1103 (defvar bibtex-valid-entry-re
1104 (concat "@[ \t]*\\(" bibtex-known-entry-type-re
"\\)")
1105 "Regexp matching the name of a valid BibTeX entry.")
1107 (defvar bibtex-valid-entry-whitespace-re
1108 (concat "[ \t\n]*\\(" bibtex-valid-entry-re
"\\)")
1109 "Regexp matching the name of a valid BibTeX entry preceded by whitespace.")
1111 (defvar bibtex-any-valid-entry-re
1113 (regexp-opt (append '("String")
1114 (mapcar 'car bibtex-entry-field-alist
))
1116 "Regexp matching the name of any valid BibTeX entry (including string).")
1119 (defconst bibtex-empty-field-re
"\"\"\\|{}"
1120 "Regexp matching an empty field.")
1122 (defconst bibtex-font-lock-syntactic-keywords
1123 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start
0 1) "\\)"
1124 (substring bibtex-comment-start
1) "\\>")
1127 (defvar bibtex-font-lock-keywords
1128 ;; entry type and reference key
1129 `((,bibtex-entry-maybe-empty-head
1130 (,bibtex-type-in-head font-lock-function-name-face
)
1131 (,bibtex-key-in-head font-lock-constant-face nil t
))
1132 ;; optional field names (treated as comments)
1133 (,(concat "^[ \t]*\\(OPT" bibtex-field-name
"\\)[ \t]*=")
1134 1 font-lock-comment-face
)
1136 (,(concat "^[ \t]*\\(" bibtex-field-name
"\\)[ \t]*=")
1137 1 font-lock-variable-name-face
)
1139 bibtex-font-lock-url bibtex-font-lock-crossref
)
1140 "*Default expressions to highlight in BibTeX mode.")
1142 (defvar bibtex-font-lock-url-regexp
1143 ;; Assume that field names begin at the beginning of a line.
1145 (regexp-opt (delete-dups (mapcar 'caar bibtex-generate-url-list
)) t
)
1147 "Regexp for `bibtex-font-lock-url'.")
1149 (defvar bibtex-field-name-for-parsing nil
1150 "Regexp of field name to be parsed by function `bibtex-parse-field-name'.
1151 Passed by dynamic scoping.")
1153 (defvar bibtex-sort-entry-class-alist
1155 (dolist (class bibtex-sort-entry-class alist
)
1157 (dolist (entry class
)
1158 ;; all entry names should be downcase (for ease of comparison)
1159 (push (cons (if (stringp entry
) (downcase entry
) entry
) i
) alist
))))
1160 "Alist mapping entry types to their sorting index.
1161 Auto-generated from `bibtex-sort-entry-class'.
1162 Used when `bibtex-maintain-sorted-entries' is `entry-class'.")
1165 ;; Support for hideshow minor mode
1166 (defun bibtex-hs-forward-sexp (arg)
1167 "Replacement for `forward-sexp' to be used by `hs-minor-mode'.
1169 (if (looking-at "@\\S(*\\s(")
1170 (goto-char (1- (match-end 0))))
1174 'hs-special-modes-alist
1175 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil
))
1178 (defun bibtex-parse-association (parse-lhs parse-rhs
)
1179 "Parse a string of the format <left-hand-side = right-hand-side>.
1180 The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
1181 substrings. These functions are expected to return nil if parsing is not
1182 successfull. If both functions return non-nil, a pair containing the returned
1183 values of the functions PARSE-LHS and PARSE-RHS is returned."
1186 (let ((left (funcall parse-lhs
))
1189 (looking-at "[ \t\n]*=[ \t\n]*")
1190 (goto-char (match-end 0))
1191 (setq right
(funcall parse-rhs
)))
1192 (cons left right
))))))
1194 (defun bibtex-parse-field-name ()
1195 "Parse the field name stored in `bibtex-field-name-for-parsing'.
1196 If the field name is found, return a triple consisting of the position of the
1197 very first character of the match, the actual starting position of the name
1198 part and end position of the match. Move point to end of field name.
1199 If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceeding
1200 BibTeX field as necessary."
1201 (cond ((looking-at ",[ \t\n]*")
1202 (let ((start (point)))
1203 (goto-char (match-end 0))
1204 (when (looking-at bibtex-field-name-for-parsing
)
1205 (goto-char (match-end 0))
1206 (list start
(match-beginning 0) (match-end 0)))))
1207 ;; Maybe add a missing comma.
1208 ((and bibtex-autoadd-commas
1209 (looking-at (concat "[ \t\n]*\\(?:" bibtex-field-name-for-parsing
1211 (skip-chars-backward " \t\n")
1215 (bibtex-parse-field-name))))
1217 (defconst bibtex-braced-string-syntax-table
1218 (let ((st (make-syntax-table)))
1219 (modify-syntax-entry ?\
{ "(}" st
)
1220 (modify-syntax-entry ?\
} "){" st
)
1221 (modify-syntax-entry ?\
[ "." st
)
1222 (modify-syntax-entry ?\
] "." st
)
1223 (modify-syntax-entry ?\
( "." st
)
1224 (modify-syntax-entry ?\
) "." st
)
1225 (modify-syntax-entry ?
\\ "." st
)
1226 (modify-syntax-entry ?
\" "." st
)
1228 "Syntax-table to parse matched braces.")
1230 (defconst bibtex-quoted-string-syntax-table
1231 (let ((st (make-syntax-table)))
1232 (modify-syntax-entry ?
\\ "\\" st
)
1233 (modify-syntax-entry ?
\" "\"" st
)
1235 "Syntax-table to parse matched quotes.")
1237 (defun bibtex-parse-field-string ()
1238 "Parse a BibTeX field string enclosed by braces or quotes.
1239 If a syntactically correct string is found, a pair containing the start and
1240 end position of the field string is returned, nil otherwise."
1242 (or (and (eq (following-char) ?
\")
1244 (with-syntax-table bibtex-quoted-string-syntax-table
1247 (and (eq (following-char) ?\
{)
1249 (with-syntax-table bibtex-braced-string-syntax-table
1253 (cons (point) end-point
))))
1255 (defun bibtex-parse-field-text ()
1256 "Parse the text part of a BibTeX field.
1257 The text part is either a string, or an empty string, or a constant followed
1258 by one or more <# (string|constant)> pairs. If a syntactically correct text
1259 is found, a pair containing the start and end position of the text is
1260 returned, nil otherwise. Move point to end of field text."
1261 (let ((starting-point (point))
1262 end-point failure boundaries
)
1263 (while (not (or end-point failure
))
1264 (cond ((looking-at bibtex-field-const
)
1265 (goto-char (match-end 0)))
1266 ((setq boundaries
(bibtex-parse-field-string))
1267 (goto-char (cdr boundaries
)))
1269 (if (looking-at "[ \t\n]*#[ \t\n]*")
1270 (goto-char (match-end 0))
1271 (setq end-point
(point))))
1272 (if (and (not failure
)
1274 (cons starting-point end-point
))))
1276 (defun bibtex-parse-field (name)
1277 "Parse a BibTeX field of regexp NAME.
1278 If a syntactically correct field is found, a pair containing the boundaries of
1279 the name and text parts of the field is returned."
1280 (let ((bibtex-field-name-for-parsing name
))
1281 (bibtex-parse-association 'bibtex-parse-field-name
1282 'bibtex-parse-field-text
)))
1284 (defun bibtex-search-forward-field (name &optional bound
)
1285 "Search forward to find a BibTeX field of name NAME.
1286 If a syntactically correct field is found, a pair containing the boundaries of
1287 the name and text parts of the field is returned. The search is limited by
1288 optional arg BOUND. If BOUND is t the search is limited by the end of the
1289 current entry. Do not move point."
1292 (unless (integer-or-marker-p bound
)
1293 (setq bound
(if bound
1294 (save-excursion (bibtex-end-of-entry))
1296 (let ((case-fold-search t
)
1297 (bibtex-field-name-for-parsing name
)
1298 boundaries temp-boundaries
)
1299 (while (and (not boundaries
)
1301 (search-forward "," bound t
))
1302 (goto-char (match-beginning 0))
1303 (if (and (setq temp-boundaries
1304 (bibtex-parse-association 'bibtex-parse-field-name
1305 'bibtex-parse-field-text
))
1306 (<= (cddr temp-boundaries
) bound
))
1307 (setq boundaries temp-boundaries
)
1311 (defun bibtex-search-backward-field (name &optional bound
)
1312 "Search backward to find a BibTeX field of name NAME.
1313 If a syntactically correct field is found, a pair containing the boundaries of
1314 the name and text parts of the field is returned. The search is limited by
1315 optional arg BOUND. If BOUND is t the search is limited by the beginning of the
1316 current entry. Do not move point."
1319 (unless (integer-or-marker-p bound
)
1320 (setq bound
(if bound
1321 (save-excursion (bibtex-beginning-of-entry))
1323 (let ((case-fold-search t
)
1324 (bibtex-field-name-for-parsing name
)
1325 boundaries temp-boundaries
)
1326 (while (and (not boundaries
)
1328 (search-backward "," bound t
))
1329 (if (setq temp-boundaries
1330 (bibtex-parse-association 'bibtex-parse-field-name
1331 'bibtex-parse-field-text
))
1332 (setq boundaries temp-boundaries
)))
1335 (defsubst bibtex-start-of-field
(bounds)
1336 (nth 0 (car bounds
)))
1337 (defsubst bibtex-start-of-name-in-field
(bounds)
1338 (nth 1 (car bounds
)))
1339 (defsubst bibtex-end-of-name-in-field
(bounds)
1340 (nth 2 (car bounds
)))
1341 (defsubst bibtex-end-of-field
(bounds)
1343 (defsubst bibtex-start-of-text-in-field
(bounds)
1345 (defsubst bibtex-end-of-text-in-field
(bounds)
1348 (defun bibtex-name-in-field (bounds &optional remove-opt-alt
)
1349 "Get content of name in BibTeX field defined via BOUNDS.
1350 If optional arg REMOVE-OPT-ALT is non-nil remove \"OPT\" and \"ALT\"."
1351 (let ((name (buffer-substring-no-properties (nth 1 (car bounds
))
1352 (nth 2 (car bounds
)))))
1353 (if (and remove-opt-alt
1354 (string-match "\\`\\(OPT\\|ALT\\)" name
))
1358 (defun bibtex-text-in-field-bounds (bounds &optional remove-delim
)
1359 "Get content of text in BibTeX field defined via BOUNDS.
1360 If optional arg REMOVE-DELIM is non-nil remove enclosing field delimiters
1362 (let ((content (buffer-substring-no-properties (cadr bounds
)
1365 (bibtex-remove-delimiters-string content
)
1368 (defun bibtex-text-in-field (field &optional follow-crossref
)
1369 "Get content of field FIELD of current BibTeX entry.
1370 Return nil if not found.
1371 If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1374 ;; We want to jump back and forth while searching FIELD
1375 (bibtex-narrow-to-entry)
1376 (goto-char (point-min))
1377 (let ((bounds (bibtex-search-forward-field field
))
1379 (cond (bounds (bibtex-text-in-field-bounds bounds t
))
1380 ((and follow-crossref
1381 (progn (goto-char (point-min))
1382 (setq bounds
(bibtex-search-forward-field
1383 "\\(OPT\\)?crossref"))))
1384 (setq crossref-field
(bibtex-text-in-field-bounds bounds t
))
1386 (if (bibtex-find-crossref crossref-field
)
1387 ;; Do not pass FOLLOW-CROSSREF because we want
1388 ;; to follow crossrefs only one level of recursion.
1389 (bibtex-text-in-field field
))))))))
1391 (defun bibtex-parse-string-prefix ()
1392 "Parse the prefix part of a BibTeX string entry, including reference key.
1393 If the string prefix is found, return a triple consisting of the position of
1394 the very first character of the match, the actual starting position of the
1395 reference key and the end position of the match."
1396 (let ((case-fold-search t
))
1397 (if (looking-at "^[ \t]*@string[ \t\n]*[({][ \t\n]*")
1398 (let ((start (point)))
1399 (goto-char (match-end 0))
1400 (when (looking-at bibtex-reference-key
)
1401 (goto-char (match-end 0))
1406 (defun bibtex-parse-string-postfix ()
1407 "Parse the postfix part of a BibTeX string entry, including the text.
1408 If the string postfix is found, return a triple consisting of the position of
1409 the actual starting and ending position of the text and the very last
1410 character of the string entry. Move point past BibTeX string entry."
1411 (let* ((case-fold-search t
)
1412 (bounds (bibtex-parse-field-text)))
1414 (goto-char (cdr bounds
))
1415 (when (looking-at "[ \t\n]*[})]")
1416 (goto-char (match-end 0))
1421 (defun bibtex-parse-string ()
1422 "Parse a BibTeX string entry.
1423 If a syntactically correct entry is found, a pair containing the boundaries of
1424 the reference key and text parts of the entry is returned.
1425 Move point past BibTeX string entry."
1426 (bibtex-parse-association 'bibtex-parse-string-prefix
1427 'bibtex-parse-string-postfix
))
1429 (defun bibtex-search-forward-string ()
1430 "Search forward to find a BibTeX string entry.
1431 If a syntactically correct entry is found, a pair containing the boundaries of
1432 the reference key and text parts of the string is returned. Do not move point."
1435 (let ((case-fold-search t
)
1437 (while (and (not boundaries
)
1438 (search-forward-regexp
1439 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t
))
1440 (goto-char (match-beginning 0))
1441 (unless (setq boundaries
(bibtex-parse-string))
1445 (defun bibtex-search-backward-string ()
1446 "Search backward to find a BibTeX string entry.
1447 If a syntactically correct entry is found, a pair containing the boundaries of
1448 the reference key and text parts of the field is returned. Do not move point."
1451 (let ((case-fold-search t
)
1453 (while (and (not boundaries
)
1454 (search-backward-regexp
1455 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t
))
1456 (goto-char (match-beginning 0))
1457 (setq boundaries
(bibtex-parse-string)))
1460 (defun bibtex-reference-key-in-string (bounds)
1461 (buffer-substring-no-properties (nth 1 (car bounds
))
1462 (nth 2 (car bounds
))))
1464 (defun bibtex-text-in-string (bounds &optional remove-delim
)
1465 "Get content of text in BibTeX string field defined via BOUNDS.
1466 If optional arg REMOVE-DELIM is non-nil remove enclosing field
1467 delimiters if present."
1468 (let ((content (buffer-substring-no-properties (nth 0 (cdr bounds
))
1469 (nth 1 (cdr bounds
)))))
1471 (bibtex-remove-delimiters-string content
)
1474 (defsubst bibtex-start-of-text-in-string
(bounds)
1475 (nth 0 (cdr bounds
)))
1476 (defsubst bibtex-end-of-text-in-string
(bounds)
1477 (nth 1 (cdr bounds
)))
1478 (defsubst bibtex-end-of-string
(bounds)
1479 (nth 2 (cdr bounds
)))
1481 (defsubst bibtex-type-in-head
()
1482 "Extract BibTeX type in head."
1484 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head
))
1485 (match-end bibtex-type-in-head
)))
1487 (defsubst bibtex-key-in-head
(&optional empty
)
1488 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
1489 (or (match-string-no-properties bibtex-key-in-head
)
1494 (defun bibtex-remove-delimiters-string (str)
1495 "Remove delimiters of string STR."
1496 (if (and (memq (aref str
0) '(?\
{ ?
\"))
1497 (memq (aref str
(1- (length str
))) '(?\
} ?
\")))
1498 (substring str
1 -
1)
1501 (defsubst bibtex-string
= (str1 str2
)
1502 "Return t if STR1 and STR2 are equal, ignoring case."
1503 (eq t
(compare-strings str1
0 nil str2
0 nil t
)))
1505 (defun bibtex-delete-whitespace ()
1506 "Delete all whitespace starting at point."
1507 (if (looking-at "[ \t\n]+")
1508 (delete-region (point) (match-end 0))))
1510 (defun bibtex-current-line ()
1511 "Compute line number of point regardless whether the buffer is narrowed."
1512 (+ (count-lines 1 (point))
1515 (defun bibtex-skip-to-valid-entry (&optional backward
)
1516 "Move point to beginning of the next valid BibTeX entry.
1517 Do not move if we are already at beginning of a valid BibTeX entry.
1518 With optional argument BACKWARD non-nil, move backward to
1519 beginning of previous valid one. A valid entry is a syntactical correct one
1520 with type contained in `bibtex-entry-field-alist' or, if
1521 `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
1522 entry. Return buffer position of beginning and ending of entry if a valid
1523 entry is found, nil otherwise."
1525 (let ((case-fold-search t
)
1527 (while (not (or found
(if backward
(bobp) (eobp))))
1530 (cond ((or (and (looking-at bibtex-valid-entry-re
)
1531 (setq found
(bibtex-search-entry nil nil t
))
1532 (equal (match-beginning 0) pnt
))
1533 (and (not bibtex-sort-ignore-string-entries
)
1534 (setq bounds
(bibtex-parse-string))
1535 (setq found
(cons (bibtex-start-of-field bounds
)
1536 (bibtex-end-of-string bounds
)))))
1539 (if (re-search-backward "^[ \t]*\\(@\\)" nil
'move
)
1540 (goto-char (match-beginning 1))))
1541 (t (if (re-search-forward "\n[ \t]*@" nil
'move
)
1542 (forward-char -
1))))))
1545 (defun bibtex-map-entries (fun)
1546 "Call FUN for each BibTeX entry in buffer (possibly narrowed).
1547 FUN is called with three arguments, the key of the entry and the buffer
1548 positions (marker) of beginning and end of entry. Point is inside the entry.
1549 If `bibtex-sort-ignore-string-entries' is non-nil, FUN is not called for
1551 (let ((case-fold-search t
))
1553 (goto-char (point-min))
1554 (while (re-search-forward bibtex-entry-head nil t
)
1555 (let ((entry-type (bibtex-type-in-head))
1556 (key (bibtex-key-in-head ""))
1557 (beg (copy-marker (match-beginning 0)))
1558 (end (copy-marker (save-excursion (bibtex-end-of-entry)))))
1560 (if (or (and (not bibtex-sort-ignore-string-entries
)
1561 (bibtex-string= entry-type
"string"))
1562 (assoc-string entry-type bibtex-entry-field-alist t
))
1563 (funcall fun key beg end
)))
1564 (goto-char end
))))))
1566 (defun bibtex-progress-message (&optional flag interval
)
1567 "Echo a message about progress of current buffer.
1568 If FLAG is a string, the message is initialized (in this case a
1569 value for INTERVAL may be given as well (if not this is set to 5)).
1570 If FLAG is `done', the message is deinitialized.
1571 If FLAG is nil, a message is echoed if point was incremented at least
1572 `bibtex-progress-interval' percent since last message was echoed."
1573 (cond ((stringp flag
)
1574 (setq bibtex-progress-lastmes flag
1575 bibtex-progress-interval
(or interval
5)
1576 bibtex-progress-lastperc
0))
1578 (message "%s (done)" bibtex-progress-lastmes
)
1579 (setq bibtex-progress-lastmes nil
))
1581 (let* ((size (- (point-max) (point-min)))
1582 (perc (if (= size
0)
1584 (/ (* 100 (- (point) (point-min))) size
))))
1585 (when (>= perc
(+ bibtex-progress-lastperc
1586 bibtex-progress-interval
))
1587 (setq bibtex-progress-lastperc perc
)
1588 (message "%s (%d%%)" bibtex-progress-lastmes perc
))))))
1590 (defun bibtex-field-left-delimiter ()
1591 "Return a string dependent on `bibtex-field-delimiters'."
1592 (if (eq bibtex-field-delimiters
'braces
)
1596 (defun bibtex-field-right-delimiter ()
1597 "Return a string dependent on `bibtex-field-delimiters'."
1598 (if (eq bibtex-field-delimiters
'braces
)
1602 (defun bibtex-entry-left-delimiter ()
1603 "Return a string dependent on `bibtex-entry-delimiters'."
1604 (if (eq bibtex-entry-delimiters
'braces
)
1608 (defun bibtex-entry-right-delimiter ()
1609 "Return a string dependent on `bibtex-entry-delimiters'."
1610 (if (eq bibtex-entry-delimiters
'braces
)
1614 (defun bibtex-search-entry (empty-head &optional bound noerror backward
)
1615 "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t).
1616 BOUND and NOERROR are exactly as in `re-search-forward'. If BACKWARD
1617 is non-nil, search in reverse direction. Move point past the closing
1618 delimiter (at the beginning of entry if BACKWARD is non-nil).
1619 Return a cons pair with buffer positions of beginning and end of entry.
1620 After call to this function MATCH-BEGINNING and MATCH-END functions
1621 are defined, but only for the head part of the entry
1622 \(especially (match-end 0) just gives the end of the head part)."
1624 (entry-head-re (if empty-head
1625 bibtex-entry-maybe-empty-head
1626 bibtex-entry-head
)))
1629 (while (and (not found
)
1630 (re-search-backward entry-head-re bound noerror
))
1631 (setq found
(bibtex-search-entry empty-head pnt t
)))
1633 (progn (goto-char (match-beginning 0))
1635 (cond ((not noerror
)
1637 (error "Backward search of BibTeX entry failed"))
1643 (unless bound
(setq bound
(point-max)))
1644 (while (and (not found
)
1645 (re-search-forward entry-head-re bound noerror
))
1649 (goto-char (match-end bibtex-type-in-head
))
1650 (looking-at "[ \t]*("))
1651 ;; entry opened with parenthesis
1654 (infix-start (point))
1656 (while (not finished
)
1657 (skip-chars-forward " \t\n" bound
)
1658 (if (and (setq bounds
(bibtex-parse-field bibtex-field-name
))
1659 (<= (bibtex-end-of-field bounds
) bound
))
1660 (setq infix-start
(bibtex-end-of-field bounds
))
1662 (goto-char infix-start
))
1663 ;; This matches the infix* part. The AND construction assures
1664 ;; that BOUND is respected.
1665 (when (and (looking-at bibtex-entry-postfix
)
1666 (eq (char-before (match-end 0)) entry-closer
)
1667 (<= (match-end 0) bound
))
1668 (goto-char (match-end 0))
1671 (cons (match-beginning 0) (point))
1672 (cond ((not noerror
)
1674 (error "Search of BibTeX entry failed"))
1680 (defun bibtex-flash-head ()
1681 "Flash at BibTeX entry head before point, if exists."
1682 (let ((case-fold-search t
)
1684 (cond ((re-search-backward bibtex-entry-head nil t
)
1685 (goto-char (match-beginning bibtex-type-in-head
))
1686 (setq flash
(match-end bibtex-key-in-head
)))
1689 (skip-chars-backward " \t")
1690 (setq flash
(point))
1692 (skip-chars-forward " \t")))
1693 (if (pos-visible-in-window-p (point))
1696 (buffer-substring (point) flash
)))))
1698 (defun bibtex-make-optional-field (field)
1699 "Make an optional field named FIELD in current BibTeX entry."
1701 (bibtex-make-field (cons (concat "OPT" (car field
)) (cdr field
)))
1702 (bibtex-make-field (concat "OPT" field
))))
1704 (defun bibtex-move-outside-of-entry ()
1705 "Make sure point is outside of a BibTeX entry."
1706 (let ((orig-point (point)))
1707 (bibtex-end-of-entry)
1708 (when (< (point) orig-point
)
1709 ;; We moved backward, so we weren't inside an entry to begin with.
1710 ;; Leave point at the beginning of a line, and preferably
1711 ;; at the beginning of a paragraph.
1712 (goto-char orig-point
)
1713 (beginning-of-line 1)
1714 (unless (= ?
\n (char-before (1- (point))))
1715 (re-search-forward "^[ \t]*[@\n]" nil
'move
)
1717 (skip-chars-forward " \t\n")))
1719 (defun bibtex-beginning-of-first-entry ()
1720 "Go to the beginning of the first BibTeX entry in buffer. Return point."
1721 (goto-char (point-min))
1722 (if (re-search-forward "^[ \t]*@" nil
'move
)
1723 (beginning-of-line))
1726 (defun bibtex-beginning-of-last-entry ()
1727 "Go to the beginning of the last BibTeX entry in buffer."
1728 (goto-char (point-max))
1729 (if (re-search-backward "^[ \t]*@" nil
'move
)
1730 (beginning-of-line))
1733 (defun bibtex-inside-field ()
1734 "Try to avoid point being at end of a BibTeX field."
1736 (skip-chars-backward " \t")
1737 (if (= (preceding-char) ?
,)
1739 (if (or (= (preceding-char) ?
})
1740 (= (preceding-char) ?
\"))
1743 (defun bibtex-enclosing-field (&optional noerr
)
1744 "Search for BibTeX field enclosing point.
1745 Unless NOERR is non-nil, signal an error if no enclosing field is found.
1746 On success return bounds, nil otherwise. Do not move point."
1747 (let ((bounds (bibtex-search-backward-field bibtex-field-name t
)))
1749 (<= (bibtex-start-of-field bounds
) (point))
1750 (>= (bibtex-end-of-field bounds
) (point)))
1753 (error "Can't find enclosing BibTeX field")))))
1755 (defun bibtex-enclosing-entry-maybe-empty-head ()
1756 "Search for BibTeX entry enclosing point. Move point to end of entry.
1757 Beginning (but not end) of entry is given by (`match-beginning' 0)."
1758 (let ((case-fold-search t
)
1759 (old-point (point)))
1760 (unless (re-search-backward bibtex-entry-maybe-empty-head nil t
)
1761 (goto-char old-point
)
1762 (error "Can't find beginning of enclosing BibTeX entry"))
1763 (goto-char (match-beginning bibtex-type-in-head
))
1764 (unless (bibtex-search-entry t nil t
)
1765 (goto-char old-point
)
1766 (error "Can't find end of enclosing BibTeX entry"))))
1768 (defun bibtex-insert-kill (n)
1769 "Reinsert the Nth stretch of killed BibTeX text."
1770 (if (not bibtex-last-kill-command
)
1771 (error "BibTeX kill ring is empty")
1772 (let* ((kr (if (eq bibtex-last-kill-command
'field
)
1773 'bibtex-field-kill-ring
1774 'bibtex-entry-kill-ring
))
1775 (kryp (if (eq bibtex-last-kill-command
'field
)
1776 'bibtex-field-kill-ring-yank-pointer
1777 'bibtex-entry-kill-ring-yank-pointer
))
1778 (current (car (set kryp
(nthcdr (mod (- n
(length (eval kryp
)))
1781 (if (eq bibtex-last-kill-command
'field
)
1782 (let (bibtex-help-message)
1784 (if (looking-at "[}\"]")
1787 (message "Mark set")
1788 (bibtex-make-field current t
))
1789 (unless (eobp) (bibtex-beginning-of-entry))
1791 (message "Mark set")
1792 (insert current
)))))
1794 (defun bibtex-format-entry ()
1795 "Helper function for `bibtex-clean-entry'.
1796 Formats current entry according to variable `bibtex-entry-format'."
1799 (bibtex-narrow-to-entry)
1800 (let ((case-fold-search t
)
1801 (format (if (eq bibtex-entry-format t
)
1802 '(realign opts-or-alts required-fields
1804 last-comma page-dashes delimiters
1805 unify-case inherit-booktitle
)
1806 bibtex-entry-format
))
1807 crossref-key bounds alternatives-there non-empty-alternative
1808 entry-list req-field-list field-list
)
1810 ;; identify entry type
1811 (goto-char (point-min))
1812 (re-search-forward bibtex-entry-type
)
1813 (let ((beg-type (1+ (match-beginning 0)))
1814 (end-type (match-end 0)))
1815 (setq entry-list
(assoc-string (buffer-substring-no-properties
1817 bibtex-entry-field-alist
1820 ;; unify case of entry name
1821 (when (memq 'unify-case format
)
1822 (delete-region beg-type end-type
)
1823 (insert (car entry-list
)))
1825 ;; update left entry delimiter
1826 (when (memq 'delimiters format
)
1827 (goto-char end-type
)
1828 (skip-chars-forward " \t\n")
1830 (insert (bibtex-entry-left-delimiter))))
1832 ;; determine if entry has crossref field and if at least
1833 ;; one alternative is non-empty
1834 (goto-char (point-min))
1835 (let* ((fields-alist (bibtex-parse-entry))
1836 (field (assoc-string "crossref" fields-alist t
)))
1837 (setq crossref-key
(and field
1838 (not (string-match bibtex-empty-field-re
1840 (bibtex-remove-delimiters-string (cdr field
)))
1841 req-field-list
(if crossref-key
1842 (nth 0 (nth 2 entry-list
)) ; crossref part
1843 (nth 0 (nth 1 entry-list
)))) ; required part
1845 (dolist (rfield req-field-list
)
1846 (when (nth 3 rfield
) ; we should have an alternative
1847 (setq alternatives-there t
1848 field
(assoc-string (car rfield
) fields-alist t
))
1850 (not (string-match bibtex-empty-field-re
1852 (cond ((not non-empty-alternative
)
1853 (setq non-empty-alternative t
))
1854 ((memq 'required-fields format
)
1855 (error "More than one non-empty alternative")))))))
1857 (if (and alternatives-there
1858 (not non-empty-alternative
)
1859 (memq 'required-fields format
))
1860 (error "All alternatives are empty"))
1862 ;; process all fields
1863 (goto-char (point-min))
1864 (while (setq bounds
(bibtex-search-forward-field bibtex-field-name
))
1865 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds
)))
1866 (end-field (copy-marker (bibtex-end-of-field bounds
) t
))
1867 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds
)))
1868 (end-name (copy-marker (bibtex-end-of-name-in-field bounds
)))
1869 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds
)))
1870 (end-text (copy-marker (bibtex-end-of-text-in-field bounds
) t
))
1871 (opt-alt (string-match "OPT\\|ALT"
1872 (buffer-substring-no-properties
1873 beg-name
(+ beg-name
3))))
1874 (field-name (buffer-substring-no-properties
1875 (if opt-alt
(+ beg-name
3) beg-name
) end-name
))
1876 (empty-field (string-match bibtex-empty-field-re
1877 (buffer-substring-no-properties
1878 beg-field end-field
)))
1881 ;; We have more elegant high-level functions for several
1882 ;; tasks done by bibtex-format-entry. However, they contain
1883 ;; quite some redundancy compared with what we need to do
1884 ;; anyway. So for speed-up we avoid using them.
1886 (if (memq 'opts-or-alts format
)
1887 (cond ((and empty-field
1889 (let ((field (assoc-string
1890 field-name req-field-list t
)))
1891 (or (not field
) ; OPT field
1892 (nth 3 field
))))) ; ALT field
1893 ;; Either it is an empty ALT field. Then we have checked
1894 ;; already that we have one non-empty alternative. Or it
1895 ;; is an empty OPT field that we do not miss anyway.
1896 ;; So we can safely delete this field.
1897 (delete-region beg-field end-field
)
1899 ;; otherwise: not empty, delete "OPT" or "ALT"
1901 (goto-char beg-name
)
1905 (push field-name field-list
)
1907 ;; remove delimiters from purely numerical fields
1908 (when (and (memq 'numerical-fields format
)
1909 (progn (goto-char beg-text
)
1910 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1911 (goto-char end-text
)
1913 (goto-char beg-text
)
1916 ;; update delimiters
1917 (when (memq 'delimiters format
)
1918 (goto-char beg-text
)
1919 (when (looking-at "[{\"]")
1921 (insert (bibtex-field-left-delimiter)))
1922 (goto-char (1- (marker-position end-text
)))
1923 (when (looking-at "[}\"]")
1925 (insert (bibtex-field-right-delimiter))))
1927 ;; update page dashes
1928 (if (and (memq 'page-dashes format
)
1929 (bibtex-string= field-name
"pages")
1930 (progn (goto-char beg-text
)
1932 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1933 (replace-match "\\1-\\2"))
1935 ;; use book title of crossref'd entry
1936 (if (and (memq 'inherit-booktitle format
)
1938 (bibtex-string= field-name
"booktitle")
1940 (let ((title (save-excursion
1943 (if (bibtex-find-entry crossref-key t
)
1944 (bibtex-text-in-field "title"))))))
1946 (setq empty-field nil
)
1947 (goto-char (1+ beg-text
))
1950 ;; Use booktitle to set a missing title.
1951 (if (and empty-field
1952 (bibtex-string= field-name
"title"))
1953 (let ((booktitle (bibtex-text-in-field "booktitle")))
1955 (setq empty-field nil
)
1956 (goto-char (1+ beg-text
))
1957 (insert booktitle
))))
1959 ;; if empty field, complain
1960 (if (and empty-field
1961 (memq 'required-fields format
)
1962 (assoc-string field-name req-field-list t
))
1963 (error "Mandatory field `%s' is empty" field-name
))
1965 ;; unify case of field name
1966 (if (memq 'unify-case format
)
1967 (let ((fname (car (assoc-string
1969 (append (nth 0 (nth 1 entry-list
))
1970 (nth 1 (nth 1 entry-list
))
1971 bibtex-user-optional-fields
)
1975 (delete-region beg-name end-name
)
1976 (goto-char beg-name
)
1978 ;; there are no rules we could follow
1979 (downcase-region beg-name end-name
))))
1982 (goto-char end-field
))))
1984 ;; check whether all required fields are present
1985 (if (memq 'required-fields format
)
1986 (let ((found 0) altlist
)
1987 (dolist (fname req-field-list
)
1989 (push (car fname
) altlist
))
1990 (unless (or (member (car fname
) field-list
)
1992 (error "Mandatory field `%s' is missing" (car fname
))))
1994 (dolist (fname altlist
)
1995 (if (member fname field-list
)
1996 (setq found
(1+ found
))))
1998 (error "Alternative mandatory field `%s' is missing"
2001 (error "Alternative fields `%s' are defined %s times"
2005 (if (looking-at (bibtex-field-right-delimiter))
2008 ;; update comma after last field
2009 (if (memq 'last-comma format
)
2010 (cond ((and bibtex-comma-after-last-field
2011 (not (looking-at ",")))
2013 ((and (not bibtex-comma-after-last-field
)
2017 ;; update right entry delimiter
2018 (if (looking-at ",")
2020 (when (memq 'delimiters format
)
2021 (skip-chars-forward " \t\n")
2023 (insert (bibtex-entry-right-delimiter)))
2026 (if (memq 'realign format
)
2027 (bibtex-fill-entry))))))
2030 (defun bibtex-autokey-abbrev (string len
)
2031 "Return an abbreviation of STRING with at least LEN characters.
2032 If LEN is positive the abbreviation is terminated only after a consonant
2033 or at the word end. If LEN is negative the abbreviation is strictly
2034 enforced using abs (LEN) characters. If LEN is not a number, STRING
2035 is returned unchanged."
2036 (cond ((or (not (numberp len
))
2037 (<= (length string
) (abs len
)))
2042 (substring string
0 (abs len
)))
2043 (t (let* ((case-fold-search t
)
2044 (abort-char (string-match "[^aeiou]" string
(1- len
))))
2046 (substring string
0 (1+ abort-char
))
2049 (defun bibtex-autokey-get-field (field &optional change-list
)
2050 "Get content of BibTeX field FIELD. Return empty string if not found.
2051 Optional arg CHANGE-LIST is a list of substitution patterns that is
2052 applied to the content of FIELD. It is an alist with pairs
2053 \(OLD-REGEXP . NEW-STRING\)."
2054 (let ((content (bibtex-text-in-field field bibtex-autokey-use-crossref
))
2056 (unless content
(setq content
""))
2057 (dolist (pattern change-list content
)
2058 (setq content
(replace-regexp-in-string (car pattern
)
2062 (defun bibtex-autokey-get-names ()
2063 "Get contents of the name field of the current entry.
2064 Do some modifications based on `bibtex-autokey-name-change-strings'.
2065 Return the names as a concatenated string obeying `bibtex-autokey-names'
2066 and `bibtex-autokey-names-stretch'."
2067 (let ((names (bibtex-autokey-get-field "author\\|editor"
2068 bibtex-autokey-name-change-strings
)))
2069 ;; Some entries do not have a name field.
2070 (if (string= "" names
)
2072 (let* ((case-fold-search t
)
2073 (name-list (mapcar 'bibtex-autokey-demangle-name
2074 (split-string names
"[ \t\n]+and[ \t\n]+")))
2076 (unless (or (not (numberp bibtex-autokey-names
))
2077 (<= (length name-list
)
2078 (+ bibtex-autokey-names
2079 bibtex-autokey-names-stretch
)))
2080 ;; Take bibtex-autokey-names elements from beginning of name-list
2081 (setq name-list
(nreverse (nthcdr (- (length name-list
)
2082 bibtex-autokey-names
)
2083 (nreverse name-list
)))
2084 additional-names bibtex-autokey-additional-names
))
2085 (concat (mapconcat 'identity name-list
2086 bibtex-autokey-name-separator
)
2087 additional-names
)))))
2089 (defun bibtex-autokey-demangle-name (fullname)
2090 "Get the last part from a well-formed FULLNAME and perform abbreviations."
2091 (let* (case-fold-search
2092 (name (cond ((string-match "\\([A-Z][^, ]*\\)[^,]*," fullname
)
2093 ;; Name is of the form "von Last, First" or
2094 ;; "von Last, Jr, First"
2095 ;; --> Take the first capital part before the comma
2096 (match-string 1 fullname
))
2097 ((string-match "\\([^, ]*\\)," fullname
)
2098 ;; Strange name: we have a comma, but nothing capital
2099 ;; So we accept even lowercase names
2100 (match-string 1 fullname
))
2101 ((string-match "\\(\\<[a-z][^ ]* +\\)+\\([A-Z][^ ]*\\)"
2103 ;; name is of the form "First von Last", "von Last",
2104 ;; "First von von Last", or "d'Last"
2105 ;; --> take the first capital part after the "von" parts
2106 (match-string 2 fullname
))
2107 ((string-match "\\([^ ]+\\) *\\'" fullname
)
2108 ;; name is of the form "First Middle Last" or "Last"
2109 ;; --> take the last token
2110 (match-string 1 fullname
))
2111 (t (error "Name `%s' is incorrectly formed" fullname
)))))
2112 (funcall bibtex-autokey-name-case-convert
2113 (bibtex-autokey-abbrev name bibtex-autokey-name-length
))))
2115 (defun bibtex-autokey-get-year ()
2116 "Return year field contents as a string obeying `bibtex-autokey-year-length'."
2117 (let ((yearfield (bibtex-autokey-get-field "year")))
2118 (substring yearfield
(max 0 (- (length yearfield
)
2119 bibtex-autokey-year-length
)))))
2121 (defun bibtex-autokey-get-title ()
2122 "Get title field contents up to a terminator.
2123 Return the result as a string"
2124 (let ((case-fold-search t
)
2126 (bibtex-autokey-get-field "title"
2127 bibtex-autokey-titleword-change-strings
)))
2128 ;; ignore everything past a terminator
2129 (if (string-match bibtex-autokey-title-terminators titlestring
)
2130 (setq titlestring
(substring titlestring
0 (match-beginning 0))))
2131 ;; gather words from titlestring into a list. Ignore
2132 ;; specific words and use only a specific amount of words.
2134 titlewords titlewords-extra word
)
2135 (while (and (or (not (numberp bibtex-autokey-titlewords
))
2136 (< counter
(+ bibtex-autokey-titlewords
2137 bibtex-autokey-titlewords-stretch
)))
2138 (string-match "\\b\\w+" titlestring
))
2139 (setq word
(match-string 0 titlestring
)
2140 titlestring
(substring titlestring
(match-end 0)))
2141 ;; Ignore words matched by one of the elements of
2142 ;; bibtex-autokey-titleword-ignore
2143 (unless (let ((lst bibtex-autokey-titleword-ignore
))
2145 (not (string-match (concat "\\`\\(?:" (car lst
)
2147 (setq lst
(cdr lst
)))
2149 (setq counter
(1+ counter
))
2150 (if (or (not (numberp bibtex-autokey-titlewords
))
2151 (<= counter bibtex-autokey-titlewords
))
2152 (push word titlewords
)
2153 (push word titlewords-extra
))))
2154 ;; Obey bibtex-autokey-titlewords-stretch:
2155 ;; If by now we have processed all words in titlestring, we include
2156 ;; titlewords-extra in titlewords. Otherwise, we ignore titlewords-extra.
2157 (unless (string-match "\\b\\w+" titlestring
)
2158 (setq titlewords
(append titlewords-extra titlewords
)))
2159 (mapconcat 'bibtex-autokey-demangle-title
(nreverse titlewords
)
2160 bibtex-autokey-titleword-separator
))))
2162 (defun bibtex-autokey-demangle-title (titleword)
2163 "Do some abbreviations on TITLEWORD.
2164 The rules are defined in `bibtex-autokey-titleword-abbrevs'
2165 and `bibtex-autokey-titleword-length'."
2166 (let ((case-fold-search t
)
2167 (alist bibtex-autokey-titleword-abbrevs
))
2169 (not (string-match (concat "\\`\\(?:" (caar alist
) "\\)\\'")
2171 (setq alist
(cdr alist
)))
2174 (funcall bibtex-autokey-titleword-case-convert
2175 (bibtex-autokey-abbrev titleword bibtex-autokey-titleword-length
)))))
2177 (defun bibtex-generate-autokey ()
2178 "Generate automatically a key for a BibTeX entry.
2179 Use the author/editor, the year and the title field.
2180 The algorithm works as follows.
2183 1. Use the author or editor field to generate the name part of the key.
2184 2. Change the content of the name field according to
2185 `bibtex-autokey-name-change-strings' (see there for further detail).
2186 3. Use the first `bibtex-autokey-names' names in the name field. If there
2187 are up to `bibtex-autokey-names' + `bibtex-autokey-names-stretch' names,
2189 4. Use only the last names to form the name part. From these last names,
2190 take at least `bibtex-autokey-name-length' characters (truncate only
2191 after a consonant or at a word end).
2192 5. Convert all last names using the function
2193 `bibtex-autokey-name-case-convert'.
2194 6. Build the name part of the key by concatenating all abbreviated last
2195 names with the string `bibtex-autokey-name-separator' between any two.
2196 If there are more names in the name field than names used in the name
2197 part, append the string `bibtex-autokey-additional-names'.
2200 1. Build the year part of the key by truncating the content of the year
2201 field to the rightmost `bibtex-autokey-year-length' digits (useful
2202 values are 2 and 4).
2203 2. If the year field (or any other field required to generate the key)
2204 is absent, but the entry has a valid crossref field and
2205 `bibtex-autokey-use-crossref' is non-nil, use the field of the
2206 crossreferenced entry instead.
2209 1. Change the content of the title field according to
2210 `bibtex-autokey-titleword-change-strings' (see there for further detail).
2211 2. Truncate the title before the first match of
2212 `bibtex-autokey-title-terminators' and delete those words which appear
2213 in `bibtex-autokey-titleword-ignore'. Build the title part using the
2214 first `bibtex-autokey-titlewords' words from this truncated title.
2215 If the truncated title ends after up to `bibtex-autokey-titlewords' +
2216 `bibtex-autokey-titlewords-stretch' words, use all words from the
2218 3. For every title word that appears in `bibtex-autokey-titleword-abbrevs'
2219 use the corresponding abbreviation (see documentation of this variable
2220 for further detail).
2221 4. From every title word not generated by an abbreviation, take at least
2222 `bibtex-autokey-titleword-length' characters (truncate only after
2223 a consonant or at a word end).
2224 5. Convert all title words using the function
2225 `bibtex-autokey-titleword-case-convert'.
2226 6. Build the title part by concatenating all abbreviated title words with
2227 the string `bibtex-autokey-titleword-separator' between any two.
2229 Concatenate the key:
2230 1. Concatenate `bibtex-autokey-prefix-string', the name part, the year
2231 part and the title part. If the name part and the year part are both
2232 non-empty insert `bibtex-autokey-name-year-separator' between the two.
2233 If the title part and the year (or name) part are non-empty, insert
2234 `bibtex-autokey-year-title-separator' between the two.
2235 2. If `bibtex-autokey-before-presentation-function' is non-nil, it must be
2236 a function taking one argument. Call this function with the generated
2237 key as the argument. Use the return value of this function (a string)
2239 3. If `bibtex-autokey-edit-before-use' is non-nil, present the key in the
2240 minibuffer to the user for editing. Insert the key given by the user."
2241 (let* ((names (bibtex-autokey-get-names))
2242 (year (bibtex-autokey-get-year))
2243 (title (bibtex-autokey-get-title))
2244 (autokey (concat bibtex-autokey-prefix-string
2246 (unless (or (equal names
"")
2248 bibtex-autokey-name-year-separator
)
2250 (unless (or (and (equal names
"")
2253 bibtex-autokey-year-title-separator
)
2255 (if bibtex-autokey-before-presentation-function
2256 (funcall bibtex-autokey-before-presentation-function autokey
)
2260 (defun bibtex-global-key-alist ()
2261 "Return global key alist based on `bibtex-files'."
2264 (mapcar (lambda (buf)
2265 (with-current-buffer buf bibtex-reference-keys
))
2266 (bibtex-files-expand t
)))
2267 bibtex-reference-keys
))
2269 (defun bibtex-read-key (prompt &optional key global
)
2270 "Read BibTeX key from minibuffer using PROMPT and default KEY.
2271 If optional arg GLOBAL is non-nil, completion is based on the keys in
2272 `bibtex-reference-keys' of `bibtex-files',"
2273 (let (completion-ignore-case)
2274 (completing-read prompt
(if global
(bibtex-global-key-alist)
2275 bibtex-reference-keys
)
2276 nil nil key
'bibtex-key-history
)))
2278 (defun bibtex-read-string-key (&optional key
)
2279 "Read BibTeX string key from minibuffer using default KEY."
2280 (let ((completion-ignore-case t
))
2281 (completing-read "String key: " bibtex-strings
2282 nil nil key
'bibtex-key-history
)))
2284 (defun bibtex-parse-keys (&optional abortable verbose
)
2285 "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2286 Find both entry keys and crossref entries. If ABORTABLE is non-nil abort
2287 on user input. If VERBOSE is non-nil give messages about progress.
2288 Return alist of keys if parsing was completed, `aborted' otherwise."
2289 (let (ref-keys crossref-keys
)
2293 (bibtex-progress-message
2294 (concat (buffer-name) ": parsing reference keys")))
2296 (goto-char (point-min))
2297 (if bibtex-parse-keys-fast
2298 (let ((case-fold-search t
)
2299 (re (concat bibtex-entry-head
"\\|"
2300 ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*"
2301 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]")))
2302 (while (re-search-forward re nil t
)
2303 (if (and abortable
(input-pending-p))
2304 ;; user has aborted by typing a key --> return `aborted'
2305 (throw 'userkey
'aborted
))
2306 (cond ((match-end 3)
2307 ;; This is a crossref.
2308 (let ((key (buffer-substring-no-properties
2309 (1+ (match-beginning 3)) (1- (match-end 3)))))
2310 (unless (assoc key crossref-keys
)
2311 (push (list key
) crossref-keys
))))
2312 ;; only keys of known entries
2313 ((assoc-string (bibtex-type-in-head)
2314 bibtex-entry-field-alist t
)
2315 ;; This is an entry.
2316 (let ((key (bibtex-key-in-head)))
2317 (unless (assoc key ref-keys
)
2318 (push (cons key t
) ref-keys
)))))))
2320 (let (;; ignore @String entries because they are handled
2321 ;; separately by bibtex-parse-strings
2322 (bibtex-sort-ignore-string-entries t
)
2325 (lambda (key beg end
)
2328 ;; user has aborted by typing a key --> return `aborted'
2329 (throw 'userkey
'aborted
))
2330 (if verbose
(bibtex-progress-message))
2331 (unless (assoc key ref-keys
)
2332 (push (cons key t
) ref-keys
))
2333 (if (and (setq bounds
(bibtex-search-forward-field "crossref" end
))
2334 (setq key
(bibtex-text-in-field-bounds bounds t
))
2335 (not (assoc key crossref-keys
)))
2336 (push (list key
) crossref-keys
))))))
2338 (dolist (key crossref-keys
)
2339 (unless (assoc (car key
) ref-keys
) (push key ref-keys
)))
2341 (bibtex-progress-message 'done
))
2342 ;; successful operation --> return `bibtex-reference-keys'
2343 (setq bibtex-reference-keys ref-keys
))))))
2345 (defun bibtex-parse-strings (&optional add abortable
)
2346 "Set `bibtex-strings' to the string definitions in the whole buffer.
2347 If ADD is non-nil add the new strings to `bibtex-strings' instead of
2348 simply resetting it. If ADD is an alist of strings, also add ADD to
2349 `bibtex-strings'. If ABORTABLE is non-nil abort on user input.
2350 Return alist of strings if parsing was completed, `aborted' otherwise."
2353 (goto-char (point-min))
2354 (let ((strings (if (and add
2355 (listp bibtex-strings
))
2359 (dolist (string add
)
2360 (unless (assoc-string (car string
) strings t
)
2361 (push string strings
))))
2363 (while (setq bounds
(bibtex-search-forward-string))
2366 ;; user has aborted by typing a key --> return `aborted'
2367 (throw 'userkey
'aborted
))
2368 (setq key
(bibtex-reference-key-in-string bounds
))
2369 (unless (assoc-string key strings t
)
2370 (push (cons key
(bibtex-text-in-string bounds t
))
2372 (goto-char (bibtex-end-of-text-in-string bounds
)))
2373 ;; successful operation --> return `bibtex-strings'
2374 (setq bibtex-strings strings
))))))
2376 (defun bibtex-string-files-init ()
2377 "Return initialization for `bibtex-strings'.
2378 Use `bibtex-predefined-strings' and BibTeX files `bibtex-string-files'."
2380 (let ((dirlist (split-string (or bibtex-string-file-path default-directory
)
2383 string-files fullfilename compl bounds found
)
2384 ;; collect absolute file names of valid string files
2385 (dolist (filename bibtex-string-files
)
2386 (unless (string-match "\\.bib\\'" filename
)
2387 (setq filename
(concat filename
".bib")))
2389 (if (file-name-absolute-p filename
)
2390 (if (file-readable-p filename
)
2391 (push filename string-files
)
2392 (error "BibTeX strings file %s not found" filename
))
2393 (dolist (dir dirlist
)
2394 (when (file-readable-p
2395 (setq fullfilename
(expand-file-name filename dir
)))
2396 (push fullfilename string-files
)
2399 (error "File %s not in paths defined via bibtex-string-file-path"
2401 ;; parse string files
2402 (dolist (filename string-files
)
2404 (insert-file-contents filename
)
2405 (goto-char (point-min))
2406 (while (setq bounds
(bibtex-search-forward-string))
2407 (push (cons (bibtex-reference-key-in-string bounds
)
2408 (bibtex-text-in-string bounds t
))
2410 (goto-char (bibtex-end-of-string bounds
)))))
2411 (append bibtex-predefined-strings
(nreverse compl
)))))
2413 (defun bibtex-parse-buffers-stealthily ()
2414 "Parse buffer in the background during idle time.
2415 Called by `run-with-idle-timer'. Whenever Emacs has been idle
2416 for `bibtex-parse-keys-timeout' seconds, parse all BibTeX buffers
2417 which have been modified after last parsing.
2418 Parsing initializes `bibtex-reference-keys' and `bibtex-strings'."
2420 (let ((buffers (buffer-list))
2421 (strings-init (bibtex-string-files-init)))
2422 (while (and buffers
(not (input-pending-p)))
2423 (set-buffer (car buffers
))
2424 (if (and (eq major-mode
'bibtex-mode
)
2425 (not (eq (buffer-modified-tick)
2426 bibtex-buffer-last-parsed-tick
)))
2429 ;; Output no progress messages in bibtex-parse-keys
2430 ;; because when in y-or-n-p that can hide the question.
2431 (if (and (listp (bibtex-parse-keys t
))
2432 ;; update bibtex-strings
2433 (listp (bibtex-parse-strings strings-init t
)))
2435 ;; remember that parsing was successful
2436 (setq bibtex-buffer-last-parsed-tick
(buffer-modified-tick)))))
2437 (setq buffers
(cdr buffers
))))))
2439 (defun bibtex-files-expand (&optional current force
)
2440 "Return an expanded list of BibTeX buffers based on `bibtex-files'.
2441 Initialize in these buffers `bibtex-reference-keys' if not yet set.
2442 List of BibTeX buffers includes current buffer if CURRENT is non-nil.
2443 If FORCE is non-nil, (re)initialize `bibtex-reference-keys' even if
2445 (let ((file-path (split-string (or bibtex-file-path default-directory
) ":+"))
2446 file-list dir-list buffer-list
)
2447 (dolist (file bibtex-files
)
2448 (cond ((eq file
'bibtex-file-path
)
2449 (setq dir-list
(append dir-list file-path
)))
2450 ((file-accessible-directory-p file
)
2451 (push file dir-list
))
2452 ((progn (unless (string-match "\\.bib\\'" file
)
2453 (setq file
(concat file
".bib")))
2454 (file-name-absolute-p file
))
2455 (push file file-list
))
2457 (let (fullfilename found
)
2458 (dolist (dir file-path
)
2459 (when (file-readable-p
2460 (setq fullfilename
(expand-file-name file dir
)))
2461 (push fullfilename file-list
)
2464 (error "File %s not in paths defined via bibtex-file-path"
2466 (dolist (file file-list
)
2467 (unless (file-readable-p file
)
2468 (error "BibTeX file %s not found" file
)))
2470 (dolist (dir dir-list
)
2472 (append file-list
(directory-files dir t
"\\.bib\\'" t
))))
2473 (delete-dups file-list
)
2474 (dolist (file file-list
)
2475 (when (file-readable-p file
)
2476 (push (find-file-noselect file
) buffer-list
)
2477 (with-current-buffer (car buffer-list
)
2478 (if (or force
(not (listp bibtex-reference-keys
)))
2479 (bibtex-parse-keys)))))
2480 (cond ((and current
(not (memq (current-buffer) buffer-list
)))
2481 (push (current-buffer) buffer-list
)
2482 (if force
(bibtex-parse-keys)))
2483 ((and (not current
) (memq (current-buffer) buffer-list
))
2484 (setq buffer-list
(delq (current-buffer) buffer-list
))))
2487 (defun bibtex-complete-internal (completions)
2488 "Complete word fragment before point to longest prefix of COMPLETIONS.
2489 COMPLETIONS is an alist of strings. If point is not after the part
2490 of a word, all strings are listed. Return completion."
2491 (let* ((case-fold-search t
)
2492 (beg (save-excursion
2493 (re-search-backward "[ \t{\"]")
2497 (part-of-word (buffer-substring-no-properties beg end
))
2498 (completion (try-completion part-of-word completions
)))
2499 (cond ((not completion
)
2500 (error "Can't find completion for `%s'" part-of-word
))
2503 ((not (string= part-of-word completion
))
2504 (delete-region beg end
)
2508 (message "Making completion list...")
2509 (with-output-to-temp-buffer "*Completions*"
2510 (display-completion-list (all-completions part-of-word
2512 (message "Making completion list...done")
2513 ;; return value is handled by choose-completion-string-functions
2516 (defun bibtex-complete-string-cleanup (str compl
)
2517 "Cleanup after inserting string STR.
2518 Remove enclosing field delimiters for STR. Display message with
2519 expansion of STR using expansion list COMPL."
2521 (bibtex-inside-field)
2522 (let ((bounds (bibtex-enclosing-field))
2523 (abbr (cdr (if (stringp str
)
2524 (assoc-string str compl t
)))))
2525 (if abbr
(message "Abbreviation for `%s'" abbr
))
2526 (goto-char (bibtex-start-of-text-in-field bounds
))
2527 (let ((boundaries (bibtex-parse-field-string)))
2529 (equal (cdr boundaries
)
2530 (bibtex-end-of-text-in-field bounds
)))
2531 (bibtex-remove-delimiters))))))
2533 (defun bibtex-complete-crossref-cleanup (key)
2534 "Display summary message on entry KEY after completion of a crossref key.
2535 Use `bibtex-summary-function' to generate summary."
2537 (if (and (stringp key
)
2538 (bibtex-find-entry key t
))
2539 (message "Ref: %s" (funcall bibtex-summary-function key
)))))
2541 (defun bibtex-copy-summary-as-kill ()
2542 "Push summery of current BibTeX entry to kill ring.
2543 Use `bibtex-summary-function' to generate summary."
2545 (let ((key (save-excursion
2546 (bibtex-beginning-of-entry)
2547 (if (looking-at bibtex-entry-maybe-empty-head
)
2548 (bibtex-key-in-head)
2549 (error "No key found")))))
2550 (kill-new (message "%s" (funcall bibtex-summary-function key
)))))
2552 (defun bibtex-summary (key)
2553 "Return summary of BibTeX entry KEY.
2554 Used as default value of `bibtex-summary-function'."
2555 ;; It would be neat to customize this function. How?
2557 (if (bibtex-find-entry key t
)
2558 (let* ((bibtex-autokey-name-case-convert 'identity
)
2559 (bibtex-autokey-name-length 'infty
)
2560 (bibtex-autokey-names 1)
2561 (bibtex-autokey-names-stretch 0)
2562 (bibtex-autokey-name-separator " ")
2563 (bibtex-autokey-additional-names " etal")
2564 (names (bibtex-autokey-get-names))
2565 (bibtex-autokey-year-length 4)
2566 (year (bibtex-autokey-get-year))
2567 (bibtex-autokey-titlewords 5)
2568 (bibtex-autokey-titlewords-stretch 2)
2569 (bibtex-autokey-titleword-case-convert 'identity
)
2570 (bibtex-autokey-titleword-length 5)
2571 (bibtex-autokey-titleword-separator " ")
2572 (title (bibtex-autokey-get-title))
2573 (journal (bibtex-autokey-get-field
2574 "journal" bibtex-autokey-transcriptions
))
2575 (volume (bibtex-autokey-get-field "volume"))
2576 (pages (bibtex-autokey-get-field "pages" '(("-.*\\'" .
"")))))
2577 (mapconcat (lambda (arg)
2578 (if (not (string= "" (cdr arg
)))
2579 (concat (car arg
) (cdr arg
))))
2580 `((" " .
,names
) (" " .
,year
) (": " .
,title
)
2581 (", " .
,journal
) (" " .
,volume
) (":" .
,pages
))
2583 (error "Key `%s' not found" key
))))
2585 (defun bibtex-pop (arg direction
)
2586 "Fill current field from the ARGth same field's text in DIRECTION.
2587 Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
2588 (let (bibtex-help-message)
2591 ;; parse current field
2592 (bibtex-inside-field)
2593 (let* ((case-fold-search t
)
2594 (bounds (bibtex-enclosing-field))
2595 (start-old-text (bibtex-start-of-text-in-field bounds
))
2596 (stop-old-text (bibtex-end-of-text-in-field bounds
))
2597 (field-name (bibtex-name-in-field bounds t
)))
2598 ;; if executed several times in a row, start each search where
2599 ;; the last one was finished
2600 (unless (eq last-command
'bibtex-pop
)
2601 (bibtex-enclosing-entry-maybe-empty-head)
2602 (setq bibtex-pop-previous-search-point
(match-beginning 0)
2603 bibtex-pop-next-search-point
(point)))
2604 (if (eq direction
'previous
)
2605 (goto-char bibtex-pop-previous-search-point
)
2606 (goto-char bibtex-pop-next-search-point
))
2607 ;; Now search for arg'th previous/next similar field
2608 (let (bounds failure new-text
)
2609 (while (and (not failure
)
2611 (cond ((eq direction
'previous
)
2612 (if (setq bounds
(bibtex-search-backward-field field-name
))
2613 (goto-char (bibtex-start-of-field bounds
))
2615 ((eq direction
'next
)
2616 (if (setq bounds
(bibtex-search-forward-field field-name
))
2617 (goto-char (bibtex-end-of-field bounds
))
2619 (setq arg
(- arg
1)))
2621 (error "No %s matching BibTeX field"
2622 (if (eq direction
'previous
) "previous" "next"))
2623 ;; Found a matching field. Remember boundaries.
2624 (setq bibtex-pop-previous-search-point
(bibtex-start-of-field bounds
)
2625 bibtex-pop-next-search-point
(bibtex-end-of-field bounds
)
2626 new-text
(bibtex-text-in-field-bounds bounds
))
2628 ;; Go back to where we started, delete old text, and pop new.
2629 (goto-char stop-old-text
)
2630 (delete-region start-old-text stop-old-text
)
2631 (insert new-text
)))))
2632 (let (bibtex-help-message)
2634 (setq this-command
'bibtex-pop
))
2636 (defun bibtex-beginning-of-field ()
2637 "Move point backward to beginning of field.
2638 This function uses a simple, fast algorithm assuming that the field
2639 begins at the beginning of a line. We use this function for font-locking."
2640 (let ((field-reg (concat "^[ \t]*" bibtex-field-name
"[ \t]*=")))
2642 (unless (looking-at field-reg
)
2643 (re-search-backward field-reg nil t
))))
2645 (defun bibtex-font-lock-url (bound)
2646 "Font-lock for URLs. BOUND limits the search."
2647 (let ((case-fold-search t
)
2649 field bounds start end found
)
2650 (bibtex-beginning-of-field)
2651 (while (and (not found
)
2652 (prog1 (re-search-forward bibtex-font-lock-url-regexp bound t
)
2653 (setq field
(match-string-no-properties 1)))
2654 (setq bounds
(bibtex-parse-field-text))
2656 (setq start
(car bounds
) end
(cdr bounds
))
2657 ;; Always ignore field delimiters
2658 (if (memq (char-before end
) '(?\
} ?
\"))
2659 (setq end
(1- end
)))
2660 (if (memq (char-after start
) '(?\
{ ?
\"))
2661 (setq start
(1+ start
)))
2663 (let ((lst bibtex-generate-url-list
) url
)
2665 (while (and (not found
)
2666 (setq url
(caar lst
)))
2667 (setq found
(and (bibtex-string= field
(car url
))
2668 (re-search-forward (cdr url
) end t
)
2669 (>= (match-beginning 0) pnt
))
2672 (if found
(bibtex-button (match-beginning 0) (match-end 0)
2673 'bibtex-url
(match-beginning 0)))
2676 (defun bibtex-font-lock-crossref (bound)
2677 "Font-lock for crossref fields. BOUND limits the search."
2678 (let ((case-fold-search t
)
2680 (crossref-reg (concat "^[ \t]*crossref[ \t]*=[ \t\n]*"
2681 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]"))
2683 (bibtex-beginning-of-field)
2684 (while (and (not found
)
2685 (re-search-forward crossref-reg bound t
))
2686 (setq start
(1+ (match-beginning 1))
2687 end
(1- (match-end 1))
2688 found
(>= start pnt
)))
2689 (if found
(bibtex-button start end
'bibtex-find-crossref
2690 (buffer-substring-no-properties start end
)
2694 (defun bibtex-button-action (button)
2695 "Call BUTTON's BibTeX function."
2696 (apply (button-get button
'bibtex-function
)
2697 (button-get button
'bibtex-args
)))
2699 (define-button-type 'bibtex-url
2700 'action
'bibtex-button-action
2701 'bibtex-function
'bibtex-url
2702 'help-echo
(purecopy "mouse-2, RET: follow URL"))
2704 (define-button-type 'bibtex-find-crossref
2705 'action
'bibtex-button-action
2706 'bibtex-function
'bibtex-find-crossref
2707 'help-echo
(purecopy "mouse-2, RET: follow crossref"))
2709 (defun bibtex-button (beg end type
&rest args
)
2710 "Make a BibTeX button from BEG to END of type TYPE in the current buffer."
2711 (make-text-button beg end
'type type
'bibtex-args args
))
2714 ;; Interactive Functions:
2717 (defun bibtex-mode ()
2718 "Major mode for editing BibTeX files.
2720 General information on working with BibTeX mode:
2722 Use commands such as \\[bibtex-Book] to get a template for a specific entry.
2723 Then fill in all desired fields using \\[bibtex-next-field] to jump from field
2724 to field. After having filled in all desired fields in the entry, clean the
2725 new entry with the command \\[bibtex-clean-entry].
2727 Some features of BibTeX mode are available only by setting the variable
2728 `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode
2729 works only with buffers containing valid (syntactical correct) and sorted
2730 entries. This is usually the case, if you have created a buffer completely
2731 with BibTeX mode and finished every new entry with \\[bibtex-clean-entry].
2733 For third party BibTeX files, call the function `bibtex-convert-alien'
2734 to fully take advantage of all features of BibTeX mode.
2737 Special information:
2739 A command such as \\[bibtex-Book] outlines the fields for a BibTeX book entry.
2741 The names of optional fields start with the string OPT, and are thus ignored
2742 by BibTeX. The names of alternative fields from which only one is required
2743 start with the string ALT. The OPT or ALT string may be removed from
2744 the name of a field with \\[bibtex-remove-OPT-or-ALT].
2745 \\[bibtex-make-field] inserts a new field after the current one.
2746 \\[bibtex-kill-field] kills the current field entirely.
2747 \\[bibtex-yank] yanks the last recently killed field after the current field.
2748 \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
2749 \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
2750 \\[bibtex-find-text] moves point to the end of the current field.
2751 \\[bibtex-complete] completes word fragment before point according to context.
2753 The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2754 from the names of all non-empty optional or alternative fields, checks that
2755 no required fields are empty, and does some formatting dependent on the value
2756 of `bibtex-entry-format'. Furthermore, it can automatically generate a key
2757 for the BibTeX entry, see `bibtex-generate-autokey'.
2758 Note: some functions in BibTeX mode depend on entries being in a special
2759 format (all fields beginning on separate lines), so it is usually a bad
2760 idea to remove `realign' from `bibtex-entry-format'.
2762 BibTeX mode supports Imenu and hideshow minor mode (`hs-minor-mode').
2764 ----------------------------------------------------------
2765 Entry to BibTeX mode calls the value of `bibtex-mode-hook'
2766 if that value is non-nil.
2768 \\{bibtex-mode-map}"
2770 (kill-all-local-variables)
2771 (use-local-map bibtex-mode-map
)
2772 (setq major-mode
'bibtex-mode
)
2773 (setq mode-name
"BibTeX")
2774 (set-syntax-table bibtex-mode-syntax-table
)
2775 (make-local-variable 'bibtex-buffer-last-parsed-tick
)
2776 ;; Install stealthy parse function if not already installed
2777 (unless bibtex-parse-idle-timer
2778 (setq bibtex-parse-idle-timer
(run-with-idle-timer
2779 bibtex-parse-keys-timeout t
2780 'bibtex-parse-buffers-stealthily
)))
2781 (set (make-local-variable 'paragraph-start
) "[ \f\n\t]*$")
2782 (set (make-local-variable 'comment-start
) bibtex-comment-start
)
2783 (set (make-local-variable 'comment-start-skip
)
2784 (concat (regexp-quote bibtex-comment-start
) "\\>[ \t]*"))
2785 (set (make-local-variable 'comment-column
) 0)
2786 (set (make-local-variable 'defun-prompt-regexp
) "^[ \t]*@[a-zA-Z0-9]+[ \t]*")
2787 (set (make-local-variable 'outline-regexp
) "[ \t]*@")
2788 (set (make-local-variable 'fill-paragraph-function
) 'bibtex-fill-field
)
2789 (set (make-local-variable 'fill-prefix
) (make-string (+ bibtex-entry-offset
2790 bibtex-contline-indentation
)
2792 (set (make-local-variable 'font-lock-defaults
)
2793 '(bibtex-font-lock-keywords
2795 ;; Mathematical expressions should be fontified as strings
2797 ;; Quotes are field delimiters and quote-delimited
2798 ;; entries should be fontified in the same way as
2799 ;; brace-delimited ones
2802 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords
)
2803 (font-lock-extra-managed-props .
(category))
2804 (font-lock-mark-block-function
2806 (set-mark (bibtex-end-of-entry))
2807 (bibtex-beginning-of-entry)))))
2808 (setq imenu-generic-expression
2809 (list (list nil bibtex-entry-head bibtex-key-in-head
))
2810 imenu-case-fold-search t
)
2811 (make-local-variable 'choose-completion-string-functions
)
2812 ;; XEmacs needs easy-menu-add, Emacs does not care
2813 (easy-menu-add bibtex-edit-menu
)
2814 (easy-menu-add bibtex-entry-menu
)
2815 (run-hooks 'bibtex-mode-hook
))
2817 (defun bibtex-field-list (entry-type)
2818 "Return list of allowed fields for entry ENTRY-TYPE.
2819 More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),
2820 where REQUIRED and OPTIONAL are lists of the required and optional field
2821 names for ENTRY-TYPE according to `bibtex-entry-field-alist',
2822 `bibtex-include-OPTkey', `bibtex-include-OPTcrossref',
2823 and `bibtex-user-optional-fields'."
2824 (let ((e (assoc-string entry-type bibtex-entry-field-alist t
))
2827 (error "Bibtex entry type %s not defined" entry-type
))
2828 (if (and (member-ignore-case entry-type bibtex-include-OPTcrossref
)
2830 (setq required
(nth 0 (nth 2 e
))
2831 optional
(nth 1 (nth 2 e
)))
2832 (setq required
(nth 0 (nth 1 e
))
2833 optional
(nth 1 (nth 1 e
))))
2834 (if bibtex-include-OPTkey
2836 "Used for reference key creation if author and editor fields are missing"
2837 (if (or (stringp bibtex-include-OPTkey
)
2838 (fboundp bibtex-include-OPTkey
))
2839 bibtex-include-OPTkey
))
2841 (if (member-ignore-case entry-type bibtex-include-OPTcrossref
)
2842 (push '("crossref" "Reference key of the cross-referenced entry")
2844 (setq optional
(append optional bibtex-user-optional-fields
))
2845 (cons required optional
)))
2847 (defun bibtex-entry (entry-type)
2848 "Insert a new BibTeX entry of type ENTRY-TYPE.
2849 After insertion call the value of `bibtex-add-entry-hook' if that value
2852 (let ((completion-ignore-case t
))
2853 (list (completing-read "Entry Type: " bibtex-entry-field-alist
2854 nil t nil
'bibtex-entry-type-history
))))
2855 (let ((key (if bibtex-maintain-sorted-entries
2856 (bibtex-read-key (format "%s key: " entry-type
))))
2857 (field-list (bibtex-field-list entry-type
)))
2858 (unless (bibtex-prepare-new-entry (list key nil entry-type
))
2859 (error "Entry with key `%s' already exists" key
))
2860 (indent-to-column bibtex-entry-offset
)
2861 (insert "@" entry-type
(bibtex-entry-left-delimiter))
2862 (if key
(insert key
))
2864 (mapc 'bibtex-make-field
(car field-list
))
2865 (mapc 'bibtex-make-optional-field
(cdr field-list
))
2866 (if bibtex-comma-after-last-field
2869 (indent-to-column bibtex-entry-offset
)
2870 (insert (bibtex-entry-right-delimiter) "\n\n"))
2871 (bibtex-next-field t
)
2872 (if (member-ignore-case entry-type bibtex-autofill-types
)
2873 (bibtex-autofill-entry))
2874 (run-hooks 'bibtex-add-entry-hook
)))
2876 (defun bibtex-entry-update ()
2877 "Update an existing BibTeX entry.
2878 In the BibTeX entry at point, make new fields for those items that may occur
2879 according to `bibtex-field-list', but are not yet present."
2882 (bibtex-beginning-of-entry)
2883 ;; For inserting new fields, we use the fact that
2884 ;; bibtex-parse-entry moves point to the end of the last field.
2885 (let* ((fields-alist (bibtex-parse-entry))
2886 (field-list (bibtex-field-list
2887 (cdr (assoc "=type=" fields-alist
)))))
2888 (dolist (field (car field-list
))
2889 (unless (assoc-string (car field
) fields-alist t
)
2890 (bibtex-make-field field
)))
2891 (dolist (field (cdr field-list
))
2892 (unless (assoc-string (car field
) fields-alist t
)
2893 (bibtex-make-optional-field field
))))))
2895 (defun bibtex-parse-entry ()
2896 "Parse entry at point, return an alist.
2897 The alist elements have the form (FIELD . TEXT), where FIELD can also be
2898 the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\"
2899 TEXT may be nil. Remove \"OPT\" and \"ALT\" from FIELD.
2900 Move point to the end of the last field."
2902 (when (looking-at bibtex-entry-maybe-empty-head
)
2903 (push (cons "=type=" (bibtex-type-in-head)) alist
)
2904 (push (cons "=key=" (bibtex-key-in-head)) alist
)
2905 (goto-char (match-end 0))
2906 (while (setq bounds
(bibtex-parse-field bibtex-field-name
))
2907 (push (cons (bibtex-name-in-field bounds t
)
2908 (bibtex-text-in-field-bounds bounds
))
2910 (goto-char (bibtex-end-of-field bounds
))))
2913 (defun bibtex-autofill-entry ()
2914 "Try to fill fields of current BibTeX entry based on neighboring entries.
2915 The current entry must have a key. Determine the neighboring entry
2916 \(previouse or next\) whose key is more similar to the key of the current
2917 entry. For all empty fields of the current entry insert the corresponding
2918 field contents of the neighboring entry. Finally try to update the text
2919 based on the difference between the keys of the neighboring and the current
2920 entry (for example, the year parts of the keys)."
2922 (undo-boundary) ;So you can easily undo it, if it didn't work right.
2923 (bibtex-beginning-of-entry)
2924 (when (looking-at bibtex-entry-head
)
2925 (let ((type (bibtex-type-in-head))
2926 (key (bibtex-key-in-head))
2927 (key-end (match-end bibtex-key-in-head
))
2928 (case-fold-search t
)
2929 tmp other-key other bounds
)
2930 ;; The fields we want to change start right after the key.
2932 ;; First see whether to use the previous or the next entry
2933 ;; for "inspiration".
2935 (goto-char (1- (match-beginning 0)))
2936 (bibtex-beginning-of-entry)
2937 (if (and (looking-at bibtex-entry-head
)
2938 (bibtex-string= type
(bibtex-type-in-head))
2939 ;; In case we found ourselves :-(
2940 (not (equal key
(setq tmp
(bibtex-key-in-head)))))
2944 (bibtex-end-of-entry)
2945 (bibtex-skip-to-valid-entry)
2946 (if (and (looking-at bibtex-entry-head
)
2947 (bibtex-string= type
(bibtex-type-in-head))
2948 ;; In case we found ourselves :-(
2949 (not (equal key
(setq tmp
(bibtex-key-in-head))))
2951 ;; Check which is the best match.
2952 (< (length (try-completion "" (list key other-key
)))
2953 (length (try-completion "" (list key tmp
))))))
2956 ;; Then fill the new entry's fields with the chosen other entry.
2958 (setq other
(save-excursion (goto-char other
) (bibtex-parse-entry)))
2959 (setq key-end
(point)) ;In case parse-entry changed the buffer.
2960 (while (setq bounds
(bibtex-parse-field bibtex-field-name
))
2961 (let ((text (assoc-string (bibtex-name-in-field bounds t
)
2963 (goto-char (bibtex-start-of-text-in-field bounds
))
2964 (if (not (and (looking-at bibtex-empty-field-re
) text
))
2965 (goto-char (bibtex-end-of-field bounds
))
2966 (delete-region (point) (bibtex-end-of-text-in-field bounds
))
2967 (insert (cdr text
)))))
2968 ;; Finally try to update the text based on the difference between
2970 (let* ((prefix (try-completion "" (list key other-key
)))
2971 ;; If the keys are foo91 and foo92, don't replace 1 for 2
2972 ;; but 91 for 92 instead.
2973 (_ (if (string-match "[0-9]+\\'" prefix
)
2974 (setq prefix
(substring prefix
0 (match-beginning 0)))))
2975 (suffix (substring key
(length prefix
)))
2976 (other-suffix (substring other-key
(length prefix
))))
2977 (while (re-search-backward (regexp-quote other-suffix
) key-end
'move
)
2978 (replace-match suffix
)))))))
2980 (defun bibtex-print-help-message ()
2981 "Print helpful information about current field in current BibTeX entry."
2984 (let* ((case-fold-search t
)
2985 (field-name (bibtex-name-in-field (bibtex-enclosing-field) t
))
2986 (field-list (bibtex-field-list (progn (re-search-backward
2987 bibtex-entry-maybe-empty-head nil t
)
2988 (bibtex-type-in-head))))
2989 (comment (assoc-string field-name
2990 (append (car field-list
)
2994 (message (nth 1 comment
))
2995 (message "No comment available")))))
2997 (defun bibtex-make-field (field &optional move interactive
)
2998 "Make a field named FIELD in current BibTeX entry.
2999 FIELD is either a string or a list of the form
3000 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
3001 `bibtex-entry-field-alist'.
3002 If MOVE is non-nil, move point past the present field before making
3003 the new field. If INTERACTIVE is non-nil, move point to the end of
3004 the new field. Otherwise move point past the new field.
3005 MOVE and INTERACTIVE are t when called interactively."
3007 (list (let ((completion-ignore-case t
)
3008 (field-list (bibtex-field-list
3010 (bibtex-enclosing-entry-maybe-empty-head)
3011 (bibtex-type-in-head)))))
3012 (completing-read "BibTeX field name: "
3013 (append (car field-list
) (cdr field-list
))
3014 nil nil nil bibtex-field-history
))
3016 (unless (consp field
)
3017 (setq field
(list field
)))
3019 (let (bibtex-help-message)
3021 (if (looking-at "[}\"]")
3024 (indent-to-column (+ bibtex-entry-offset bibtex-field-indentation
))
3025 (if (nth 3 field
) (insert "ALT"))
3026 (insert (car field
) " ")
3027 (if bibtex-align-at-equal-sign
3028 (indent-to-column (+ bibtex-entry-offset
3029 (- bibtex-text-indentation
2))))
3031 (unless bibtex-align-at-equal-sign
3032 (indent-to-column (+ bibtex-entry-offset
3033 bibtex-text-indentation
)))
3034 (let ((init (nth 2 field
)))
3035 (insert (cond ((stringp init
) init
)
3036 ((fboundp init
) (funcall init
))
3037 (t (concat (bibtex-field-left-delimiter)
3038 (bibtex-field-right-delimiter))))))
3041 (bibtex-print-help-message)))
3043 (defun bibtex-beginning-of-entry ()
3044 "Move to beginning of BibTeX entry (beginning of line).
3045 If inside an entry, move to the beginning of it, otherwise move to the
3046 beginning of the previous entry. If point is ahead of all BibTeX entries
3047 move point to the beginning of buffer. Return the new location of point."
3049 (skip-chars-forward " \t")
3050 (if (looking-at "@")
3052 (re-search-backward "^[ \t]*@" nil
'move
)
3055 (defun bibtex-end-of-entry ()
3056 "Move to end of BibTeX entry (past the closing brace).
3057 If inside an entry, move to the end of it, otherwise move to the end
3058 of the previous entry. Do not move if ahead of first entry.
3059 Return the new location of point."
3061 (let ((case-fold-search t
)
3063 (pnt (bibtex-beginning-of-entry))
3065 (cond ((looking-at bibtex-valid-entry-whitespace-re
)
3066 (bibtex-search-entry t nil t
)
3067 (unless (equal (match-beginning 0) pnt
)
3069 ((setq bounds
(bibtex-parse-string))
3070 (goto-char (bibtex-end-of-string bounds
)))
3071 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")
3072 (goto-char (match-end 0))
3073 (if (looking-at "[({]")
3078 (message "Not on a known BibTeX entry."))
3082 (error "Syntactically incorrect BibTeX entry starts here")))
3085 (defun bibtex-goto-line (arg)
3086 "Goto line ARG, counting from beginning of (narrowed) buffer."
3087 ;; code adapted from `goto-line'
3088 (goto-char (point-min))
3089 (if (eq selective-display t
)
3090 (re-search-forward "[\n\C-m]" nil
'end
(1- arg
))
3091 (forward-line (1- arg
))))
3093 (defun bibtex-reposition-window ()
3094 "Make the current BibTeX entry visible.
3095 If entry is smaller than `window-body-height', entry is centered in window.
3096 Otherwise display the beginning of entry."
3099 (beg (line-number-at-pos (bibtex-beginning-of-entry)))
3100 (end (line-number-at-pos (bibtex-end-of-entry))))
3101 (if (> (window-body-height) (- end beg
))
3102 ;; entry fits in current window
3104 (bibtex-goto-line (/ (+ 1 beg end
) 2))
3107 ;; entry too large for current window
3108 (bibtex-goto-line beg
)
3110 (if (> (1+ (- (line-number-at-pos pnt
) beg
))
3111 (window-body-height))
3112 (bibtex-goto-line beg
)
3115 (defun bibtex-mark-entry ()
3116 "Put mark at beginning, point at end of current BibTeX entry."
3118 (set-mark (bibtex-beginning-of-entry))
3119 (bibtex-end-of-entry))
3121 (defun bibtex-count-entries (&optional count-string-entries
)
3122 "Count number of entries in current buffer or region.
3123 With prefix argument COUNT-STRING-ENTRIES count all entries,
3124 otherwise count all entries except @String entries.
3125 If mark is active count entries in region, if not in whole buffer."
3128 (bibtex-sort-ignore-string-entries
3129 (not count-string-entries
)))
3132 (narrow-to-region (if mark-active
(region-beginning)
3133 (bibtex-beginning-of-first-entry))
3134 (if mark-active
(region-end) (point-max)))
3135 (bibtex-map-entries (lambda (key beg end
)
3136 (setq number
(1+ number
))))))
3137 (message "%s contains %d entries."
3138 (if mark-active
"Region" "Buffer")
3141 (defun bibtex-ispell-entry ()
3142 "Check BibTeX entry for spelling errors."
3144 (ispell-region (save-excursion (bibtex-beginning-of-entry))
3145 (save-excursion (bibtex-end-of-entry))))
3147 (defun bibtex-ispell-abstract ()
3148 "Check abstract of BibTeX entry for spelling errors."
3150 (let ((bounds (save-excursion
3151 (bibtex-beginning-of-entry)
3152 (bibtex-search-forward-field "abstract" t
))))
3154 (ispell-region (bibtex-start-of-text-in-field bounds
)
3155 (bibtex-end-of-text-in-field bounds
))
3156 (error "No abstract in entry"))))
3158 (defun bibtex-narrow-to-entry ()
3159 "Narrow buffer to current BibTeX entry."
3163 (narrow-to-region (bibtex-beginning-of-entry)
3164 (bibtex-end-of-entry))))
3166 (defun bibtex-entry-index ()
3167 "Return index of BibTeX entry head at or past position of point.
3168 The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting
3169 the entries of the BibTeX buffer. CROSSREF-KEY is nil unless the value
3170 of `bibtex-maintain-sorted-entries' is `crossref'. Move point to the end
3171 of the head of the entry found. Return nil if no entry found."
3172 (let ((case-fold-search t
))
3173 (if (re-search-forward bibtex-entry-maybe-empty-head nil t
)
3174 (let ((key (bibtex-key-in-head))
3175 ;; all entry names should be downcase (for ease of comparison)
3176 (entry-name (downcase (bibtex-type-in-head))))
3177 ;; Don't search CROSSREF-KEY if we don't need it.
3178 (if (eq bibtex-maintain-sorted-entries
'crossref
)
3179 (let ((bounds (bibtex-search-forward-field
3180 "\\(OPT\\)?crossref" t
)))
3182 (if bounds
(bibtex-text-in-field-bounds bounds t
))
3184 (list key nil entry-name
))))))
3186 (defun bibtex-lessp (index1 index2
)
3187 "Predicate for sorting BibTeX entries with indices INDEX1 and INDEX2.
3188 Each index is a list (KEY CROSSREF-KEY ENTRY-NAME).
3189 The predicate depends on the variable `bibtex-maintain-sorted-entries'.
3190 If its value is nil use plain sorting."
3191 (cond ((not index1
) (not index2
)) ; indices can be nil
3193 ((eq bibtex-maintain-sorted-entries
'crossref
)
3196 (or (string-lessp (nth 1 index1
) (nth 1 index2
))
3197 (and (string-equal (nth 1 index1
) (nth 1 index2
))
3198 (string-lessp (nth 0 index1
) (nth 0 index2
))))
3199 (not (string-lessp (nth 0 index2
) (nth 1 index1
))))
3201 (string-lessp (nth 0 index1
) (nth 1 index2
))
3202 (string-lessp (nth 0 index1
) (nth 0 index2
)))))
3203 ((eq bibtex-maintain-sorted-entries
'entry-class
)
3204 (let ((n1 (cdr (or (assoc (nth 2 index1
) bibtex-sort-entry-class-alist
)
3205 (assoc 'catch-all bibtex-sort-entry-class-alist
)
3206 '(nil .
1000)))) ; if there is nothing else
3207 (n2 (cdr (or (assoc (nth 2 index2
) bibtex-sort-entry-class-alist
)
3208 (assoc 'catch-all bibtex-sort-entry-class-alist
)
3209 '(nil .
1000))))) ; if there is nothing else
3212 (string-lessp (car index1
) (car index2
))))))
3213 (t ; (eq bibtex-maintain-sorted-entries 'plain)
3214 (string-lessp (car index1
) (car index2
)))))
3216 (defun bibtex-sort-buffer ()
3217 "Sort BibTeX buffer alphabetically by key.
3218 The predicate for sorting is defined via `bibtex-maintain-sorted-entries'.
3219 If its value is nil use plain sorting. Text outside of BibTeX entries is not
3220 affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
3224 (narrow-to-region (bibtex-beginning-of-first-entry)
3225 (save-excursion (goto-char (point-max))
3226 (bibtex-end-of-entry)))
3227 (bibtex-skip-to-valid-entry)
3229 'bibtex-skip-to-valid-entry
; NEXTREC function
3230 'bibtex-end-of-entry
; ENDREC function
3231 'bibtex-entry-index
; STARTKEY function
3232 nil
; ENDKEY function
3233 'bibtex-lessp
))) ; PREDICATE
3235 (defun bibtex-find-crossref (crossref-key &optional pnt split
)
3236 "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3237 If `bibtex-files' is non-nil, search all these files.
3238 Otherwise the search is limited to the current buffer.
3239 Return position of entry if CROSSREF-KEY is found or nil otherwise.
3240 If CROSSREF-KEY is in the same buffer like current entry but before it
3241 an error is signaled. Optional arg PNT is the position of the referencing
3242 entry. It defaults to position of point. If optional arg SPLIT is non-nil,
3243 split window so that both the referencing and the crossrefed entry are
3245 If called interactively, CROSSREF-KEY defaults to crossref key of current
3246 entry and SPLIT is t."
3250 (bibtex-beginning-of-entry)
3251 (let ((bounds (bibtex-search-forward-field "crossref" t
)))
3253 (bibtex-text-in-field-bounds bounds t
))))))
3254 (list (bibtex-read-key "Find crossref key: " crossref-key t
)
3256 (let (buffer pos eqb
)
3258 (setq pos
(bibtex-find-entry crossref-key t
)
3259 buffer
(current-buffer)))
3260 (setq eqb
(eq buffer
(current-buffer)))
3262 (if split
(message "Crossref key `%s' not found" crossref-key
)))
3263 (split ; called (quasi) interactively
3264 (unless pnt
(setq pnt
(point)))
3266 (if eqb
(select-window (split-window))
3267 (pop-to-buffer buffer
))
3269 (bibtex-reposition-window)
3271 (if (and eqb
(> pnt pos
))
3272 (error "The referencing entry must preceed the crossrefed entry!")))
3273 ;; `bibtex-find-crossref' is called noninteractively during
3274 ;; clean-up of an entry. Then it is not possible to check
3275 ;; whether the current entry and the crossrefed entry have
3276 ;; the correct sorting order.
3277 (eqb (goto-char pos
))
3278 (t (set-buffer buffer
) (goto-char pos
)))
3281 (defun bibtex-find-entry (key &optional global start display
)
3282 "Move point to the beginning of BibTeX entry named KEY.
3283 Return position of entry if KEY is found or nil if not found.
3284 With prefix arg GLOBAL non-nil, search KEY in `bibtex-files'.
3285 Otherwise the search is limited to the current buffer.
3286 Optional arg START is buffer position where the search starts.
3287 If it is nil, start search at beginning of buffer.
3288 If DISPLAY is non-nil, display the buffer containing KEY.
3289 Otherwise, use `set-buffer'. DISPLAY is t when called interactively."
3290 (interactive (list (bibtex-read-key "Find key: " nil current-prefix-arg
)
3291 current-prefix-arg nil t
))
3292 (if (and global bibtex-files
)
3293 (let ((buffer-list (bibtex-files-expand t
))
3295 (while (and (not found
)
3296 (setq buffer
(pop buffer-list
)))
3297 (with-current-buffer buffer
3298 (if (cdr (assoc-string key bibtex-reference-keys
))
3299 ;; `bibtex-find-entry' moves point if key found
3300 (setq found
(bibtex-find-entry key
)))))
3301 (cond ((and found display
)
3302 (let ((same-window-buffer-names
3303 (cons (buffer-name buffer
) same-window-buffer-names
)))
3304 (pop-to-buffer buffer
)
3305 (bibtex-reposition-window)))
3306 (found (set-buffer buffer
))
3307 (display (message "Key `%s' not found" key
)))
3310 (let* (case-fold-search
3311 (pnt (save-excursion
3312 (goto-char (or start
(point-min)))
3313 (if (re-search-forward (concat "^[ \t]*\\("
3315 "\\)[ \t]*[({][ \t\n]*\\("
3319 (match-beginning 0)))))
3322 (if display
(bibtex-reposition-window)))
3323 (display (message "Key `%s' not found" key
)))
3326 (defun bibtex-prepare-new-entry (index)
3327 "Prepare a new BibTeX entry with index INDEX.
3328 INDEX is a list (KEY CROSSREF-KEY ENTRY-NAME).
3329 Move point where the entry KEY should be placed.
3330 If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
3331 search to look for place for KEY. This requires that buffer is sorted,
3332 see \\[bibtex-validate].)
3333 Return t if preparation was successful or nil if entry KEY already exists."
3334 (let ((key (nth 0 index
))
3336 (cond ((or (null key
)
3338 (string-equal key
""))
3339 (and (not (setq key-exist
(bibtex-find-entry key
)))
3340 (not bibtex-maintain-sorted-entries
)))
3341 (bibtex-move-outside-of-entry))
3342 ;; if key-exist is non-nil due to the previous cond clause
3343 ;; then point will be at beginning of entry named key.
3345 (t ; bibtex-maintain-sorted-entries is non-nil
3346 (let* ((case-fold-search t
)
3347 (left (save-excursion (bibtex-beginning-of-first-entry)
3348 (bibtex-skip-to-valid-entry)
3350 (right (save-excursion (bibtex-beginning-of-last-entry)
3351 (bibtex-end-of-entry)))
3352 (found (if (>= left right
) left
))
3357 (goto-char (/ (+ left right
) 2))
3358 (bibtex-skip-to-valid-entry t
)
3359 (setq actual-index
(bibtex-entry-index))
3360 (cond ((bibtex-lessp index actual-index
)
3361 (setq new
(bibtex-beginning-of-entry))
3362 (if (equal right new
)
3366 (bibtex-end-of-entry)
3367 (bibtex-skip-to-valid-entry)
3369 (if (equal left new
)
3371 (setq left new
))))))
3373 (bibtex-beginning-of-entry)
3374 (setq actual-index
(save-excursion (bibtex-entry-index)))
3375 (when (or (not actual-index
)
3376 (bibtex-lessp actual-index index
))
3377 ;; buffer contains no valid entries or
3378 ;; greater than last entry --> append
3379 (bibtex-end-of-entry)
3380 (unless (bobp) (newline (forward-line 2)))
3381 (beginning-of-line)))))
3382 (unless key-exist t
)))
3384 (defun bibtex-validate (&optional test-thoroughly
)
3385 "Validate if buffer or region is syntactically correct.
3386 Check also for duplicate keys and correct sort order provided
3387 `bibtex-maintain-sorted-entries' is non-nil.
3388 With optional argument TEST-THOROUGHLY non-nil check also for
3389 the absence of required fields and for questionable month fields.
3390 If mark is active, validate current region, if not the whole buffer.
3391 Only check known entry types, so you can put comments outside of entries.
3392 Return t if test was successful, nil otherwise."
3394 (let* ((case-fold-search t
)
3395 error-list syntax-error
)
3398 (narrow-to-region (if mark-active
(region-beginning)
3399 (bibtex-beginning-of-first-entry))
3400 (if mark-active
(region-end) (point-max)))
3402 ;; looking if entries fit syntactical structure
3403 (goto-char (point-min))
3404 (bibtex-progress-message "Checking syntactical structure")
3405 (let (bibtex-sort-ignore-string-entries)
3406 (while (re-search-forward "^[ \t]*@" nil t
)
3407 (bibtex-progress-message)
3409 (let ((pnt (point)))
3410 (if (not (looking-at bibtex-any-valid-entry-re
))
3412 (bibtex-skip-to-valid-entry)
3413 (if (equal (point) pnt
)
3416 (push (cons (bibtex-current-line)
3417 "Syntax error (check esp. commas, braces, and quotes)")
3420 (bibtex-progress-message 'done
)
3423 ;; proceed only if there were no syntax errors.
3424 (setq syntax-error t
)
3426 ;; looking for duplicate keys and correct sort order
3427 (let (previous current key-list
)
3428 (bibtex-progress-message "Checking for duplicate keys")
3430 (lambda (key beg end
)
3431 (bibtex-progress-message)
3433 (setq current
(bibtex-entry-index))
3434 (cond ((not previous
))
3435 ((member key key-list
)
3436 (push (cons (bibtex-current-line)
3437 (format "Duplicate key `%s'" key
))
3439 ((and bibtex-maintain-sorted-entries
3440 (not (bibtex-lessp previous current
)))
3441 (push (cons (bibtex-current-line)
3442 "Entries out of order")
3445 (setq previous current
)))
3446 (bibtex-progress-message 'done
))
3448 ;; Check for duplicate keys in `bibtex-files'.
3450 ;; We don't want to be fooled by outdated `bibtex-reference-keys'.
3451 (dolist (buffer (bibtex-files-expand nil t
))
3452 (dolist (key (with-current-buffer buffer bibtex-reference-keys
))
3453 (when (and (cdr key
)
3454 (cdr (assoc-string (car key
) bibtex-reference-keys
)))
3455 (bibtex-find-entry (car key
))
3456 (push (cons (bibtex-current-line)
3457 (format "Duplicate key `%s' in %s" (car key
)
3458 (abbreviate-file-name (buffer-file-name buffer
))))
3461 (when test-thoroughly
3462 (bibtex-progress-message
3463 "Checking required fields and month fields")
3464 (let ((bibtex-sort-ignore-string-entries t
))
3466 (lambda (key beg end
)
3467 (bibtex-progress-message)
3468 (let* ((entry-list (progn
3470 (bibtex-search-entry nil end
)
3471 (assoc-string (bibtex-type-in-head)
3472 bibtex-entry-field-alist t
)))
3473 (req (copy-sequence (elt (elt entry-list
1) 0)))
3474 (creq (copy-sequence (elt (elt entry-list
2) 0)))
3475 crossref-there bounds alt-there field
)
3477 (while (setq bounds
(bibtex-search-forward-field
3478 bibtex-field-name end
))
3479 (goto-char (bibtex-start-of-text-in-field bounds
))
3480 (let ((field-name (bibtex-name-in-field bounds
)))
3481 (if (and (bibtex-string= field-name
"month")
3482 ;; Check only abbreviated month fields.
3483 (let ((month (bibtex-text-in-field-bounds bounds
)))
3484 (not (or (string-match "\\`[\"{].+[\"}]\\'" month
)
3487 bibtex-predefined-month-strings t
)))))
3488 (push (cons (bibtex-current-line)
3489 "Questionable month field")
3491 (setq field
(assoc-string field-name req t
))
3493 (if alt-there
(push (cons (bibtex-current-line)
3494 "More than one non-empty alternative")
3496 (setq alt-there t
)))
3497 (setq req
(delete field req
)
3498 creq
(delete (assoc-string field-name creq t
) creq
))
3499 (if (bibtex-string= field-name
"crossref")
3500 (setq crossref-there t
))))
3506 (push (car field
) alt
)
3507 (push (cons (save-excursion (goto-char beg
)
3508 (bibtex-current-line))
3509 (format "Required field `%s' missing"
3512 ;; The following fails if there are more than two
3513 ;; alternatives in a BibTeX entry, which isn't
3514 ;; the case momentarily.
3516 (push (cons (save-excursion (goto-char beg
)
3517 (bibtex-current-line))
3518 (format "Alternative fields `%s'/`%s' missing"
3519 (car alt
) (cadr alt
)))
3521 (bibtex-progress-message 'done
)))))
3524 (let ((file (file-name-nondirectory (buffer-file-name)))
3525 (dir default-directory
)
3526 (err-buf "*BibTeX validation errors*"))
3527 (setq error-list
(sort error-list
'car-less-than-car
))
3528 (with-current-buffer (get-buffer-create err-buf
)
3529 (setq default-directory dir
)
3530 (unless (eq major-mode
'compilation-mode
) (compilation-mode))
3531 (toggle-read-only -
1)
3532 (delete-region (point-min) (point-max))
3533 (insert "BibTeX mode command `bibtex-validate'\n"
3535 "Maybe undetected errors due to syntax errors. Correct and validate again.\n"
3537 (dolist (err error-list
)
3538 (insert (format "%s:%d: %s\n" file
(car err
) (cdr err
))))
3539 (set-buffer-modified-p nil
)
3540 (toggle-read-only 1)
3541 (goto-line 3)) ; first error message
3542 (display-buffer err-buf
)
3545 (message "%s is syntactically correct"
3546 (if mark-active
"Region" "Buffer"))
3549 (defun bibtex-validate-globally (&optional strings
)
3550 "Check for duplicate keys in `bibtex-files'.
3551 With optional prefix arg STRINGS, check for duplicate strings, too.
3552 Return t if test was successful, nil otherwise."
3554 (let ((buffer-list (bibtex-files-expand t
))
3555 buffer-key-list current-buf current-keys error-list
)
3556 ;; Check for duplicate keys within BibTeX buffer
3557 (dolist (buffer buffer-list
)
3560 (let (entry-type key key-list
)
3561 (goto-char (point-min))
3562 (while (re-search-forward bibtex-entry-head nil t
)
3563 (setq entry-type
(bibtex-type-in-head)
3564 key
(bibtex-key-in-head))
3565 (if (or (and strings
(bibtex-string= entry-type
"string"))
3566 (assoc-string entry-type bibtex-entry-field-alist t
))
3567 (if (member key key-list
)
3568 (push (format "%s:%d: Duplicate key `%s'\n"
3570 (bibtex-current-line) key
)
3572 (push key key-list
))))
3573 (push (cons buffer key-list
) buffer-key-list
))))
3575 ;; Check for duplicate keys among BibTeX buffers
3576 (while (setq current-buf
(pop buffer-list
))
3577 (setq current-keys
(cdr (assq current-buf buffer-key-list
)))
3578 (with-current-buffer current-buf
3579 (dolist (buffer buffer-list
)
3580 (dolist (key (cdr (assq buffer buffer-key-list
)))
3581 (when (assoc-string key current-keys
)
3582 (bibtex-find-entry key
)
3583 (push (format "%s:%d: Duplicat key `%s' in %s\n"
3584 (buffer-file-name) (bibtex-current-line) key
3585 (abbreviate-file-name (buffer-file-name buffer
)))
3588 ;; Process error list
3590 (let ((err-buf "*BibTeX validation errors*"))
3591 (with-current-buffer (get-buffer-create err-buf
)
3592 (unless (eq major-mode
'compilation-mode
) (compilation-mode))
3593 (toggle-read-only -
1)
3594 (delete-region (point-min) (point-max))
3595 (insert "BibTeX mode command `bibtex-validate-globally'\n\n")
3596 (dolist (err (sort error-list
'string-lessp
)) (insert err
))
3597 (set-buffer-modified-p nil
)
3598 (toggle-read-only 1)
3599 (goto-line 3)) ; first error message
3600 (display-buffer err-buf
)
3603 (message "No duplicate keys.")
3606 (defun bibtex-next-field (begin)
3607 "Move point to end of text of next BibTeX field.
3608 With prefix BEGIN non-nil, move point to its beginning."
3610 (bibtex-inside-field)
3611 (let ((start (point)))
3613 (let ((bounds (bibtex-enclosing-field)))
3614 (goto-char (bibtex-end-of-field bounds
))
3620 (bibtex-find-text begin
))
3622 (defun bibtex-find-text (&optional begin noerror
)
3623 "Move point to end of text of current BibTeX field.
3624 With optional prefix BEGIN non-nil, move point to its beginning.
3625 Unless NOERROR is non-nil, an error is signaled if point is not
3628 (let* ((pnt (point))
3629 (_ (bibtex-inside-field))
3630 (bounds (bibtex-enclosing-field t
)))
3634 (progn (goto-char (bibtex-start-of-text-in-field bounds
))
3635 (if (looking-at "[{\"]")
3637 (goto-char (bibtex-end-of-text-in-field bounds
))
3638 (if (or (= (preceding-char) ?
})
3639 (= (preceding-char) ?
\"))
3641 (if bibtex-help-message
3642 (bibtex-print-help-message)))
3643 ((setq bounds
(bibtex-parse-string))
3644 (goto-char (if begin
3645 (1+ (bibtex-start-of-text-in-string bounds
))
3646 (1- (bibtex-end-of-text-in-string bounds
)))))
3647 ((looking-at bibtex-entry-maybe-empty-head
)
3648 (goto-char (if begin
3649 (match-beginning bibtex-key-in-head
)
3653 (unless noerror
(error "Not on BibTeX field"))))))
3655 (defun bibtex-remove-OPT-or-ALT ()
3656 "Remove the string starting optional/alternative fields.
3657 Align text and go thereafter to end of text."
3659 (bibtex-inside-field)
3660 (let ((case-fold-search t
)
3661 (bounds (bibtex-enclosing-field)))
3663 (goto-char (bibtex-start-of-name-in-field bounds
))
3664 (when (looking-at "OPT\\|ALT")
3665 (delete-region (match-beginning 0) (match-end 0))
3666 ;; make field non-OPT
3667 (search-forward "=")
3669 (delete-horizontal-space)
3670 (if bibtex-align-at-equal-sign
3671 (indent-to-column (- bibtex-text-indentation
2))
3673 (search-forward "=")
3674 (delete-horizontal-space)
3675 (if bibtex-align-at-equal-sign
3677 (indent-to-column bibtex-text-indentation
))))
3678 (bibtex-inside-field)))
3680 (defun bibtex-remove-delimiters ()
3681 "Remove \"\" or {} around string."
3684 (bibtex-inside-field)
3685 (let* ((bounds (bibtex-enclosing-field))
3686 (end (bibtex-end-of-text-in-field bounds
))
3687 (start (bibtex-start-of-text-in-field bounds
)))
3688 (if (memq (char-before end
) '(?\
} ?
\"))
3689 (delete-region (1- end
) end
))
3690 (if (memq (char-after start
) '(?\
{ ?
\"))
3691 (delete-region start
(1+ start
))))))
3693 (defun bibtex-kill-field (&optional copy-only
)
3694 "Kill the entire enclosing BibTeX field.
3695 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
3696 but do not actually kill it."
3699 (bibtex-inside-field)
3700 (let* ((case-fold-search t
)
3701 (bounds (bibtex-enclosing-field))
3702 (end (bibtex-end-of-field bounds
))
3703 (beg (bibtex-start-of-field bounds
)))
3705 (skip-chars-forward " \t\n,")
3706 (push (list (bibtex-name-in-field bounds
) nil
3707 (bibtex-text-in-field-bounds bounds
))
3708 bibtex-field-kill-ring
)
3709 (if (> (length bibtex-field-kill-ring
) bibtex-field-kill-ring-max
)
3710 (setcdr (nthcdr (1- bibtex-field-kill-ring-max
)
3711 bibtex-field-kill-ring
)
3713 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring
)
3715 (delete-region beg end
))))
3716 (setq bibtex-last-kill-command
'field
))
3718 (defun bibtex-copy-field-as-kill ()
3719 "Copy the BibTeX field at point to the kill ring."
3721 (bibtex-kill-field t
))
3723 (defun bibtex-kill-entry (&optional copy-only
)
3724 "Kill the entire enclosing BibTeX entry.
3725 With prefix arg COPY-ONLY, copy the current entry to `bibtex-entry-kill-ring',
3726 but do not actually kill it."
3729 (let* ((case-fold-search t
)
3730 (beg (bibtex-beginning-of-entry))
3731 (end (progn (bibtex-end-of-entry)
3732 (if (re-search-forward
3733 bibtex-entry-maybe-empty-head nil
'move
)
3734 (goto-char (match-beginning 0)))
3736 (push (buffer-substring-no-properties beg end
)
3737 bibtex-entry-kill-ring
)
3738 (if (> (length bibtex-entry-kill-ring
) bibtex-entry-kill-ring-max
)
3739 (setcdr (nthcdr (1- bibtex-entry-kill-ring-max
)
3740 bibtex-entry-kill-ring
)
3742 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring
)
3744 (delete-region beg end
))))
3745 (setq bibtex-last-kill-command
'entry
))
3747 (defun bibtex-copy-entry-as-kill ()
3748 "Copy the entire enclosing BibTeX entry to `bibtex-entry-kill-ring'."
3750 (bibtex-kill-entry t
))
3752 (defun bibtex-yank (&optional n
)
3753 "Reinsert the last BibTeX item.
3754 More precisely, reinsert the field or entry killed or yanked most recently.
3755 With argument N, reinsert the Nth most recently killed BibTeX item.
3756 See also the command \\[bibtex-yank-pop]]."
3758 (bibtex-insert-kill (1- n
))
3759 (setq this-command
'bibtex-yank
))
3761 (defun bibtex-yank-pop (n)
3762 "Replace just-yanked killed BibTeX item with a different item.
3763 This command is allowed only immediately after a `bibtex-yank' or a
3764 `bibtex-yank-pop'. At such a time, the region contains a reinserted
3765 previously killed BibTeX item. `bibtex-yank-pop' deletes that item
3766 and inserts in its place a different killed BibTeX item.
3768 With no argument, the previous kill is inserted.
3769 With argument N, insert the Nth previous kill.
3770 If N is negative, this is a more recent kill.
3772 The sequence of kills wraps around, so that after the oldest one
3773 comes the newest one."
3775 (unless (eq last-command
'bibtex-yank
)
3776 (error "Previous command was not a BibTeX yank"))
3777 (setq this-command
'bibtex-yank
)
3778 (let ((inhibit-read-only t
))
3779 (delete-region (point) (mark t
))
3780 (bibtex-insert-kill n
)))
3782 (defun bibtex-empty-field ()
3783 "Delete the text part of the current field, replace with empty text."
3785 (bibtex-inside-field)
3786 (let ((bounds (bibtex-enclosing-field)))
3787 (goto-char (bibtex-start-of-text-in-field bounds
))
3788 (delete-region (point) (bibtex-end-of-text-in-field bounds
))
3789 (insert (concat (bibtex-field-left-delimiter)
3790 (bibtex-field-right-delimiter)) )
3791 (bibtex-find-text t
)))
3793 (defun bibtex-pop-previous (arg)
3794 "Replace text of current field with the similar field in previous entry.
3795 With arg, goes up ARG entries. Repeated, goes up so many times. May be
3796 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
3798 (bibtex-pop arg
'previous
))
3800 (defun bibtex-pop-next (arg)
3801 "Replace text of current field with the text of similar field in next entry.
3802 With arg, goes down ARG entries. Repeated, goes down so many times. May be
3803 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3805 (bibtex-pop arg
'next
))
3807 (defun bibtex-clean-entry (&optional new-key called-by-reformat
)
3808 "Finish editing the current BibTeX entry and clean it up.
3809 Check that no required fields are empty and formats entry dependent
3810 on the value of `bibtex-entry-format'.
3811 If the reference key of the entry is empty or a prefix argument is given,
3812 calculate a new reference key. (Note: this works only if fields in entry
3813 begin on separate lines prior to calling `bibtex-clean-entry' or if
3814 'realign is contained in `bibtex-entry-format'.)
3815 Don't call `bibtex-clean-entry' on @Preamble entries.
3816 At end of the cleaning process, the functions in
3817 `bibtex-clean-entry-hook' are called with region narrowed to entry."
3818 ;; Opt. arg called-by-reformat is t if bibtex-clean-entry
3819 ;; is called by bibtex-reformat
3821 (let ((case-fold-search t
)
3822 (start (bibtex-beginning-of-entry))
3823 (_ (looking-at bibtex-entry-maybe-empty-head
))
3824 (entry-type (bibtex-type-in-head))
3825 (key (bibtex-key-in-head)))
3827 (cond ((bibtex-string= entry-type
"preamble")
3828 ;; (bibtex-format-preamble)
3829 (error "No clean up of @Preamble entries"))
3830 ((bibtex-string= entry-type
"string")
3831 (setq entry-type
'string
))
3832 ;; (bibtex-format-string)
3833 (t (bibtex-format-entry)))
3835 (when (or new-key
(not key
))
3836 (setq key
(bibtex-generate-autokey))
3837 ;; Sometimes bibtex-generate-autokey returns an empty string
3838 (if (or bibtex-autokey-edit-before-use
(string= "" key
))
3839 (setq key
(if (eq entry-type
'string
)
3840 (bibtex-read-string-key key
)
3841 (bibtex-read-key "Key to use: " key
))))
3843 (re-search-forward (if (eq entry-type
'string
)
3844 bibtex-string-maybe-empty-head
3845 bibtex-entry-maybe-empty-head
))
3846 (if (match-beginning bibtex-key-in-head
)
3847 (delete-region (match-beginning bibtex-key-in-head
)
3848 (match-end bibtex-key-in-head
)))
3851 (unless called-by-reformat
3852 (let* ((end (save-excursion
3853 (bibtex-end-of-entry)
3854 (if (re-search-forward
3855 bibtex-entry-maybe-empty-head nil
'move
)
3856 (goto-char (match-beginning 0)))
3858 (entry (buffer-substring start end
))
3859 ;; include the crossref key in index
3860 (index (let ((bibtex-maintain-sorted-entries 'crossref
))
3861 (bibtex-entry-index))) ; moves point to end of head
3864 (if (and bibtex-maintain-sorted-entries
3865 (not (and bibtex-sort-ignore-string-entries
3866 (eq entry-type
'string
))))
3868 (delete-region start end
)
3869 (setq error
(not (bibtex-prepare-new-entry index
))
3870 start
(point)) ; update start
3871 (save-excursion (insert entry
)))
3872 (bibtex-find-entry key
)
3873 (setq error
(or (/= (point) start
)
3874 (bibtex-find-entry key nil end
))))
3876 (error "New inserted entry yields duplicate key"))
3877 (dolist (buffer (bibtex-files-expand))
3878 (with-current-buffer buffer
3879 (if (cdr (assoc-string key bibtex-reference-keys
))
3880 (error "Duplicate key in %s" (buffer-file-name)))))
3882 ;; Only update the list of keys if it has been built already.
3883 (cond ((eq entry-type
'string
)
3884 (if (and (listp bibtex-strings
)
3885 (not (assoc key bibtex-strings
)))
3886 (push (cons key
(bibtex-text-in-string
3887 (save-excursion (bibtex-parse-string)) t
))
3889 ;; We have a normal entry.
3890 ((listp bibtex-reference-keys
)
3891 (cond ((not (assoc key bibtex-reference-keys
))
3892 (push (cons key t
) bibtex-reference-keys
))
3893 ((not (cdr (assoc key bibtex-reference-keys
)))
3894 ;; Turn a crossref key into a header key
3895 (setq bibtex-reference-keys
3897 (delete (list key
) bibtex-reference-keys
)))))
3898 ;; Handle crossref key.
3899 (if (and (nth 1 index
)
3900 (not (assoc (nth 1 index
) bibtex-reference-keys
)))
3901 (push (list (nth 1 index
)) bibtex-reference-keys
)))))
3904 (if bibtex-clean-entry-hook
3907 (bibtex-narrow-to-entry)
3908 (run-hooks 'bibtex-clean-entry-hook
)))))))
3910 (defun bibtex-fill-field-bounds (bounds justify
&optional move
)
3911 "Fill BibTeX field delimited by BOUNDS.
3912 If JUSTIFY is non-nil justify as well.
3913 If optional arg MOVE is non-nil move point to end of field."
3914 (let ((end-field (copy-marker (bibtex-end-of-field bounds
))))
3915 (goto-char (bibtex-start-of-field bounds
))
3919 (bibtex-delete-whitespace)
3922 (indent-to-column (+ bibtex-entry-offset
3923 bibtex-field-indentation
))
3924 (re-search-forward "[ \t\n]*=" end-field
)
3927 (if bibtex-align-at-equal-sign
3929 (+ bibtex-entry-offset
(- bibtex-text-indentation
2)))
3932 (bibtex-delete-whitespace)
3933 (if bibtex-align-at-equal-sign
3935 (indent-to-column bibtex-text-indentation
)))
3936 (re-search-forward "[ \t\n]*=[ \t\n]*" end-field
))
3937 (while (re-search-forward "[ \t\n]+" end-field
'move
)
3938 (replace-match " "))
3940 (if move
(goto-char end-field
))))
3942 (defun bibtex-fill-field (&optional justify
)
3943 "Like \\[fill-paragraph], but fill current BibTeX field.
3944 Optional prefix arg JUSTIFY non-nil means justify as well.
3945 In BibTeX mode this function is bound to `fill-paragraph-function'."
3947 (let ((pnt (copy-marker (point)))
3948 (bounds (bibtex-enclosing-field)))
3950 (bibtex-fill-field-bounds bounds justify
)
3953 (defun bibtex-fill-entry ()
3954 "Fill current BibTeX entry.
3955 Realign entry, so that every field starts on a separate line. Field
3956 names appear in column `bibtex-field-indentation', field text starts in
3957 column `bibtex-text-indentation' and continuation lines start here, too.
3958 If `bibtex-align-at-equal-sign' is non-nil, align equal signs, too."
3960 (let ((pnt (copy-marker (point)))
3961 (end (copy-marker (bibtex-end-of-entry)))
3963 (bibtex-beginning-of-entry)
3964 (bibtex-delete-whitespace)
3965 (indent-to-column bibtex-entry-offset
)
3966 (while (setq bounds
(bibtex-search-forward-field bibtex-field-name end
))
3967 (bibtex-fill-field-bounds bounds t t
))
3968 (if (looking-at ",")
3970 (bibtex-delete-whitespace)
3973 (indent-to-column bibtex-entry-offset
)
3976 (defun bibtex-realign ()
3977 "Realign BibTeX entries such that they are separated by one blank line."
3978 (goto-char (point-min))
3979 (let ((case-fold-search t
))
3980 ;; No blank lines prior to the first valid entry if there no
3981 ;; non-white characters in front of it.
3982 (when (looking-at bibtex-valid-entry-whitespace-re
)
3983 (replace-match "\\1"))
3984 ;; Valid entries are separated by one blank line.
3985 (while (re-search-forward bibtex-valid-entry-whitespace-re nil t
)
3986 (replace-match "\n\n\\1"))
3987 ;; One blank line past the last valid entry if it is followed by
3988 ;; non-white characters, no blank line otherwise.
3990 (when (re-search-forward bibtex-valid-entry-re nil t
)
3991 (bibtex-end-of-entry)
3992 (bibtex-delete-whitespace)
3993 (open-line (if (eobp) 1 2)))))
3995 (defun bibtex-reformat (&optional read-options
)
3996 "Reformat all BibTeX entries in buffer or region.
3997 Without prefix argument, reformatting is based on `bibtex-entry-format'.
3998 With prefix argument, read options for reformatting from minibuffer.
3999 With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again.
4000 If mark is active reformat entries in region, if not in whole buffer."
4002 (let* ((pnt (point))
4003 (use-previous-options
4004 (and (equal (prefix-numeric-value read-options
) 16)
4005 (or bibtex-reformat-previous-options
4006 bibtex-reformat-previous-reference-keys
)))
4007 (bibtex-entry-format
4009 (if use-previous-options
4010 bibtex-reformat-previous-options
4011 (setq bibtex-reformat-previous-options
4012 (mapcar (lambda (option)
4013 (if (y-or-n-p (car option
)) (cdr option
)))
4014 `(("Realign entries (recommended)? " .
'realign
)
4015 ("Remove empty optional and alternative fields? " .
'opts-or-alts
)
4016 ("Remove delimiters around pure numerical fields? " .
'numerical-fields
)
4017 (,(concat (if bibtex-comma-after-last-field
"Insert" "Remove")
4018 " comma at end of entry? ") .
'last-comma
)
4019 ("Replace double page dashes by single ones? " .
'page-dashes
)
4020 ("Inherit booktitle? " .
'inherit-booktitle
)
4021 ("Force delimiters? " .
'delimiters
)
4022 ("Unify case of entry types and field names? " .
'unify-case
))))))
4023 ;; Do not include required-fields because `bibtex-reformat'
4024 ;; cannot handle the error messages of `bibtex-format-entry'.
4025 ;; Use `bibtex-validate' to check for required fields.
4026 ((eq t bibtex-entry-format
)
4027 '(realign opts-or-alts numerical-fields delimiters
4028 last-comma page-dashes unify-case inherit-booktitle
))
4030 (remove 'required-fields
(push 'realign bibtex-entry-format
)))))
4031 (reformat-reference-keys
4033 (if use-previous-options
4034 bibtex-reformat-previous-reference-keys
4035 (setq bibtex-reformat-previous-reference-keys
4036 (y-or-n-p "Generate new reference keys automatically? ")))))
4037 (bibtex-sort-ignore-string-entries t
)
4038 bibtex-autokey-edit-before-use
)
4041 (narrow-to-region (if mark-active
(region-beginning) (point-min))
4042 (if mark-active
(region-end) (point-max)))
4043 (if (memq 'realign bibtex-entry-format
)
4045 (bibtex-progress-message "Formatting" 1)
4046 (bibtex-map-entries (lambda (key beg end
)
4047 (bibtex-progress-message)
4048 (bibtex-clean-entry reformat-reference-keys t
)))
4049 (bibtex-progress-message 'done
))
4050 (when reformat-reference-keys
4051 (kill-local-variable 'bibtex-reference-keys
)
4052 (when bibtex-maintain-sorted-entries
4053 (bibtex-progress-message "Sorting" 1)
4054 (bibtex-sort-buffer)
4055 (bibtex-progress-message 'done
)))
4058 (defun bibtex-convert-alien (&optional read-options
)
4059 "Make an alien BibTeX buffer fully usable by BibTeX mode.
4060 If a file does not conform with all standards used by BibTeX mode,
4061 some of the high-level features of BibTeX mode are not available.
4062 This function tries to convert current buffer to conform with these standards.
4063 With prefix argument READ-OPTIONS non-nil, read options for reformatting
4064 entries from minibuffer."
4066 (message "Starting to validate buffer...")
4070 "If errors occur, correct them and call `bibtex-convert-alien' again")
4072 (deactivate-mark) ; So bibtex-validate works on the whole buffer.
4073 (when (let (bibtex-maintain-sorted-entries)
4075 (message "Starting to reformat entries...")
4077 (bibtex-reformat read-options
)
4078 (goto-char (point-max))
4079 (message "Buffer is now parsable. Please save it.")))
4081 (defun bibtex-complete ()
4082 "Complete word fragment before point according to context.
4083 If point is inside key or crossref field perform key completion based on
4084 `bibtex-reference-keys'. Inside a month field perform key completion
4085 based on `bibtex-predefined-month-strings'. Inside any other field
4086 perform string completion based on `bibtex-strings'. An error is
4087 signaled if point is outside key or BibTeX field."
4090 (case-fold-search t
)
4093 (if (and (setq bounds
(bibtex-enclosing-field t
))
4094 (>= pnt
(bibtex-start-of-text-in-field bounds
))
4095 (<= pnt
(bibtex-end-of-text-in-field bounds
)))
4096 (setq name
(bibtex-name-in-field bounds t
)
4097 compl
(cond ((bibtex-string= name
"crossref")
4098 ;; point is in crossref field
4100 ((bibtex-string= name
"month")
4101 ;; point is in month field
4102 bibtex-predefined-month-strings
)
4103 ;; point is in other field
4104 (t (if (listp bibtex-strings
)
4106 ;; so that bibtex-complete-string-cleanup
4108 (bibtex-parse-strings
4109 (bibtex-string-files-init))))))
4110 (bibtex-beginning-of-entry)
4111 (cond ((and (looking-at bibtex-string-maybe-empty-head
)
4112 ;; point is inside a string key
4113 (or (and (match-beginning bibtex-key-in-head
)
4114 (>= pnt
(match-beginning bibtex-key-in-head
))
4115 (<= pnt
(match-end bibtex-key-in-head
)))
4116 ;; or point is on empty string key
4117 (and (not (match-beginning bibtex-key-in-head
))
4118 (= pnt
(match-end 0)))))
4119 (setq compl
'string
))
4120 ((and (looking-at bibtex-entry-maybe-empty-head
)
4121 ;; point is inside a key
4122 (or (and (match-beginning bibtex-key-in-head
)
4123 (>= pnt
(match-beginning bibtex-key-in-head
))
4124 (<= pnt
(match-end bibtex-key-in-head
)))
4125 ;; or point is on empty key
4126 (and (not (match-beginning bibtex-key-in-head
))
4127 (= pnt
(match-end 0)))))
4128 (setq compl
'key
)))))
4130 (cond ((eq compl
'key
)
4131 ;; key completion: no cleanup needed
4132 (let (completion-ignore-case)
4133 (bibtex-complete-internal (bibtex-global-key-alist))))
4135 ((eq compl
'crossref-key
)
4136 ;; crossref key completion
4137 (let (completion-ignore-case)
4138 (setq choose-completion-string-functions
4139 (lambda (choice buffer mini-p base-size
)
4140 (let ((choose-completion-string-functions nil
))
4141 (choose-completion-string choice buffer base-size
))
4142 (bibtex-complete-crossref-cleanup choice
)
4143 ;; return t (needed by choose-completion-string-functions)
4145 (bibtex-complete-crossref-cleanup (bibtex-complete-internal
4146 (bibtex-global-key-alist)))))
4149 ;; string key completion: no cleanup needed
4150 (let ((completion-ignore-case t
))
4151 (bibtex-complete-internal bibtex-strings
)))
4154 ;; string completion
4155 (let ((completion-ignore-case t
))
4156 (setq choose-completion-string-functions
4157 `(lambda (choice buffer mini-p base-size
)
4158 (let ((choose-completion-string-functions nil
))
4159 (choose-completion-string choice buffer base-size
))
4160 (bibtex-complete-string-cleanup choice
',compl
)
4161 ;; return t (needed by choose-completion-string-functions)
4163 (bibtex-complete-string-cleanup (bibtex-complete-internal compl
)
4166 (t (error "Point outside key or BibTeX field")))))
4168 (defun bibtex-Article ()
4169 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
4171 (bibtex-entry "Article"))
4173 (defun bibtex-Book ()
4174 "Insert a new BibTeX @Book entry; see also `bibtex-entry'."
4176 (bibtex-entry "Book"))
4178 (defun bibtex-Booklet ()
4179 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'."
4181 (bibtex-entry "Booklet"))
4183 (defun bibtex-InBook ()
4184 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'."
4186 (bibtex-entry "InBook"))
4188 (defun bibtex-InCollection ()
4189 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'."
4191 (bibtex-entry "InCollection"))
4193 (defun bibtex-InProceedings ()
4194 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'."
4196 (bibtex-entry "InProceedings"))
4198 (defun bibtex-Manual ()
4199 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'."
4201 (bibtex-entry "Manual"))
4203 (defun bibtex-MastersThesis ()
4204 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'."
4206 (bibtex-entry "MastersThesis"))
4208 (defun bibtex-Misc ()
4209 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'."
4211 (bibtex-entry "Misc"))
4213 (defun bibtex-PhdThesis ()
4214 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'."
4216 (bibtex-entry "PhdThesis"))
4218 (defun bibtex-Proceedings ()
4219 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'."
4221 (bibtex-entry "Proceedings"))
4223 (defun bibtex-TechReport ()
4224 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'."
4226 (bibtex-entry "TechReport"))
4228 (defun bibtex-Unpublished ()
4229 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'."
4231 (bibtex-entry "Unpublished"))
4233 (defun bibtex-String (&optional key
)
4234 "Insert a new BibTeX @String entry with key KEY."
4235 (interactive (list (bibtex-read-string-key)))
4236 (let ((bibtex-maintain-sorted-entries
4237 (unless bibtex-sort-ignore-string-entries
4238 bibtex-maintain-sorted-entries
))
4240 (unless (bibtex-prepare-new-entry (list key nil
"String"))
4241 (error "Entry with key `%s' already exists" key
))
4242 (if (zerop (length key
)) (setq key nil
))
4243 (indent-to-column bibtex-entry-offset
)
4245 (bibtex-entry-left-delimiter))
4248 (setq endpos
(point)))
4250 (bibtex-field-left-delimiter))
4252 (setq endpos
(point)))
4253 (insert (bibtex-field-right-delimiter)
4254 (bibtex-entry-right-delimiter)
4256 (goto-char endpos
)))
4258 (defun bibtex-Preamble ()
4259 "Insert a new BibTeX @Preamble entry."
4261 (bibtex-move-outside-of-entry)
4262 (indent-to-column bibtex-entry-offset
)
4264 (bibtex-entry-left-delimiter))
4265 (let ((endpos (point)))
4266 (insert (bibtex-entry-right-delimiter)
4268 (goto-char endpos
)))
4270 (defun bibtex-url (&optional pos
)
4271 "Browse a URL for the BibTeX entry at point.
4272 Optional POS is the location of the BibTeX entry.
4273 The URL is generated using the schemes defined in `bibtex-generate-url-list'
4274 \(see there\). Then the URL is passed to `browse-url'."
4277 (if pos
(goto-char pos
))
4278 (bibtex-beginning-of-entry)
4279 (let ((fields-alist (bibtex-parse-entry))
4280 ;; Always ignore case,
4281 (case-fold-search t
)
4282 (lst bibtex-generate-url-list
)
4284 (while (setq scheme
(pop lst
))
4285 (when (and (setq field
(cdr (assoc-string (caar scheme
)
4287 ;; Always remove field delimiters
4288 (progn (setq field
(bibtex-remove-delimiters-string field
))
4289 (string-match (cdar scheme
) field
)))
4291 (if (null (cdr scheme
))
4292 (setq url
(match-string 0 field
)))
4293 (dolist (step (cdr scheme
))
4294 (cond ((stringp step
)
4295 (setq url
(concat url step
)))
4296 ((setq field
(cdr (assoc-string (car step
) fields-alist t
)))
4297 ;; Always remove field delimiters
4298 (setq field
(bibtex-remove-delimiters-string field
))
4299 (if (string-match (nth 1 step
) field
)
4301 ((functionp (nth 2 step
))
4302 (funcall (nth 2 step
) field
))
4303 ((numberp (nth 2 step
))
4304 (match-string (nth 2 step
) field
))
4306 (replace-match (nth 2 step
) t nil field
))))
4307 ;; If the scheme is set up correctly,
4308 ;; we should never reach this point
4309 (error "Match failed: %s" field
))
4310 (setq url
(concat url field
)))
4311 ;; If the scheme is set up correctly,
4312 ;; we should never reach this point
4313 (t (error "Step failed: %s" step
))))
4316 (unless url
(message "No URL known.")))))
4319 ;; Make BibTeX a Feature
4323 ;; arch-tag: ee2be3af-caad-427f-b42a-d20fad630d04
4324 ;;; bibtex.el ends here