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 entries that get an OPTcrossref field."
80 :type
'(repeat string
))
82 (defcustom bibtex-include-OPTkey t
83 "*If non-nil, all entries will have an OPTkey field.
84 If this is a string, it will be used as the initial field text.
85 If this is a function, it will be called 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' (see documentation
97 of this variable for details)."
99 :type
'(repeat (group (string :tag
"Field")
100 (string :tag
"Comment")
101 (option (group :inline t
103 (choice :tag
"Init" :value
""
107 (defcustom bibtex-entry-format
108 '(opts-or-alts required-fields numerical-fields
)
109 "*Type of formatting performed by `bibtex-clean-entry'.
110 It may be t, nil, or a list of symbols out of the following:
111 opts-or-alts Delete empty optional and alternative fields and
112 remove OPT and ALT prefixes from used fields.
113 required-fields Signal an error if a required field is missing.
114 numerical-fields Delete delimiters around numeral fields.
115 page-dashes Change double dashes in page field to single dash
116 (for scribe compatibility).
117 inherit-booktitle If entry contains a crossref field and booktitle
118 field is empty, it is set to the contents of the
119 title field of the crossreferenced entry.
120 Caution: this will work only if buffer is
122 realign Realign entries, so that field texts and perhaps equal
123 signs (depending on the value of
124 `bibtex-align-at-equal-sign') begin in the same column.
125 last-comma Add or delete comma on end of last field in entry,
126 according to value of `bibtex-comma-after-last-field'.
127 delimiters Change delimiters according to variables
128 `bibtex-field-delimiters' and `bibtex-entry-delimiters'.
129 unify-case Change case of entry and field names.
131 The value t means do all of the above formatting actions.
132 The value nil means do no formatting at all."
134 :type
'(choice (const :tag
"None" nil
)
136 (set :menu-tag
"Some"
138 (const required-fields
)
139 (const numerical-fields
)
141 (const inherit-booktitle
)
145 (const unify-case
))))
147 (defcustom bibtex-clean-entry-hook nil
148 "*List of functions to call when entry has been cleaned.
149 Functions are called with point inside the cleaned entry, and the buffer
150 narrowed to just the entry."
154 (defcustom bibtex-maintain-sorted-entries nil
155 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order.
156 Allowed non-nil values are:
157 plain All entries are sorted alphabetically.
158 crossref All entries are sorted alphabetically unless an entry has a
159 crossref field. These crossrefed entries are placed in
160 alphabetical order immediately preceding the main entry.
161 entry-class The entries are divided into classes according to their
162 entry name, see `bibtex-sort-entry-class'. Within each class
163 the entries are sorted alphabetically.
164 See also `bibtex-sort-ignore-string-entries'."
166 :type
'(choice (const nil
)
169 (const entry-class
)))
171 (defvar bibtex-sort-entry-class
174 ("Book" "Proceedings"))
175 "*List of classes of BibTeX entry names, used for sorting entries.
176 If value of `bibtex-maintain-sorted-entries' is `entry-class'
177 entries are ordered according to the classes they belong to. Each
178 class contains a list of entry names. An entry `catch-all' applies
179 to all entries not explicitely mentioned.")
181 (defcustom bibtex-sort-ignore-string-entries t
182 "*If non-nil, BibTeX @String entries are not sort-significant.
183 That means they are ignored when determining ordering of the buffer
184 \(e.g., sorting, locating alphabetical position for new entries, etc.)."
188 (defcustom bibtex-field-kill-ring-max
20
189 "*Max length of `bibtex-field-kill-ring' before discarding oldest elements."
193 (defcustom bibtex-entry-kill-ring-max
20
194 "*Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
198 (defcustom bibtex-parse-keys-timeout
60
199 "*Specify interval for parsing BibTeX buffers.
200 All BibTeX buffers in Emacs are parsed if Emacs has been idle
201 `bibtex-parse-keys-timeout' seconds. Only buffers which were modified
202 after last parsing and which are maintained in sorted order are parsed."
206 (defcustom bibtex-parse-keys-fast t
207 "*If non-nil, use fast but simplified algorithm for parsing BibTeX keys.
208 If parsing fails, try to set this variable to nil."
212 (defvar bibtex-entry-field-alist
215 ((("author" "Author1 [and Author2 ...] [and others]")
216 ("title" "Title of the article (BibTeX converts it to lowercase)")
217 ("journal" "Name of the journal (use string, remove braces)")
218 ("year" "Year of publication"))
219 (("volume" "Volume of the journal")
220 ("number" "Number of the journal (only allowed if entry contains volume)")
221 ("pages" "Pages in the journal")
222 ("month" "Month of the publication as a string (remove braces)")
223 ("note" "Remarks to be put at the end of the \\bibitem")))
224 ((("author" "Author1 [and Author2 ...] [and others]")
225 ("title" "Title of the article (BibTeX converts it to lowercase)"))
226 (("pages" "Pages in the journal")
227 ("journal" "Name of the journal (use string, remove braces)")
228 ("year" "Year of publication")
229 ("volume" "Volume of the journal")
230 ("number" "Number of the journal")
231 ("month" "Month of the publication as a string (remove braces)")
232 ("note" "Remarks to be put at the end of the \\bibitem"))))
234 ((("author" "Author1 [and Author2 ...] [and others]" "" t
)
235 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t
)
236 ("title" "Title of the book")
237 ("publisher" "Publishing company")
238 ("year" "Year of publication"))
239 (("volume" "Volume of the book in the series")
240 ("number" "Number of the book in a small series (overwritten by volume)")
241 ("series" "Series in which the book appeared")
242 ("address" "Address of the publisher")
243 ("edition" "Edition of the book as a capitalized English word")
244 ("month" "Month of the publication as a string (remove braces)")
245 ("note" "Remarks to be put at the end of the \\bibitem")))
246 ((("author" "Author1 [and Author2 ...] [and others]" "" t
)
247 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t
)
248 ("title" "Title of the book"))
249 (("publisher" "Publishing company")
250 ("year" "Year of publication")
251 ("volume" "Volume of the book in the series")
252 ("number" "Number of the book in a small series (overwritten by volume)")
253 ("series" "Series in which the book appeared")
254 ("address" "Address of the publisher")
255 ("edition" "Edition of the book as a capitalized English word")
256 ("month" "Month of the publication as a string (remove braces)")
257 ("note" "Remarks to be put at the end of the \\bibitem"))))
259 ((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
260 (("author" "Author1 [and Author2 ...] [and others]")
261 ("howpublished" "The way in which the booklet was published")
262 ("address" "Address of the publisher")
263 ("month" "Month of the publication as a string (remove braces)")
264 ("year" "Year of publication")
265 ("note" "Remarks to be put at the end of the \\bibitem"))))
267 ((("author" "Author1 [and Author2 ...] [and others]" "" t
)
268 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t
)
269 ("title" "Title of the book")
270 ("chapter" "Chapter in the book")
271 ("publisher" "Publishing company")
272 ("year" "Year of publication"))
273 (("volume" "Volume of the book in the series")
274 ("number" "Number of the book in a small series (overwritten by volume)")
275 ("series" "Series in which the book appeared")
276 ("type" "Word to use instead of \"chapter\"")
277 ("address" "Address of the publisher")
278 ("edition" "Edition of the book as a capitalized English word")
279 ("month" "Month of the publication as a string (remove braces)")
280 ("pages" "Pages in the book")
281 ("note" "Remarks to be put at the end of the \\bibitem")))
282 ((("author" "Author1 [and Author2 ...] [and others]" "" t
)
283 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t
)
284 ("title" "Title of the book")
285 ("chapter" "Chapter in the book"))
286 (("pages" "Pages in the book")
287 ("publisher" "Publishing company")
288 ("year" "Year of publication")
289 ("volume" "Volume of the book in the series")
290 ("number" "Number of the book in a small series (overwritten by volume)")
291 ("series" "Series in which the book appeared")
292 ("type" "Word to use instead of \"chapter\"")
293 ("address" "Address of the publisher")
294 ("edition" "Edition of the book as a capitalized English word")
295 ("month" "Month of the publication as a string (remove braces)")
296 ("note" "Remarks to be put at the end of the \\bibitem"))))
298 ((("author" "Author1 [and Author2 ...] [and others]")
299 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
300 ("booktitle" "Name of the book")
301 ("publisher" "Publishing company")
302 ("year" "Year of publication"))
303 (("editor" "Editor1 [and Editor2 ...] [and others]")
304 ("volume" "Volume of the book in the series")
305 ("number" "Number of the book in a small series (overwritten by volume)")
306 ("series" "Series in which the book appeared")
307 ("type" "Word to use instead of \"chapter\"")
308 ("chapter" "Chapter in the book")
309 ("pages" "Pages in the book")
310 ("address" "Address of the publisher")
311 ("edition" "Edition of the book as a capitalized English word")
312 ("month" "Month of the publication as a string (remove braces)")
313 ("note" "Remarks to be put at the end of the \\bibitem")))
314 ((("author" "Author1 [and Author2 ...] [and others]")
315 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
316 ("booktitle" "Name of the book"))
317 (("pages" "Pages in the book")
318 ("publisher" "Publishing company")
319 ("year" "Year of publication")
320 ("editor" "Editor1 [and Editor2 ...] [and others]")
321 ("volume" "Volume of the book in the series")
322 ("number" "Number of the book in a small series (overwritten by volume)")
323 ("series" "Series in which the book appeared")
324 ("type" "Word to use instead of \"chapter\"")
325 ("chapter" "Chapter in the book")
326 ("address" "Address of the publisher")
327 ("edition" "Edition of the book as a capitalized English word")
328 ("month" "Month of the publication as a string (remove braces)")
329 ("note" "Remarks to be put at the end of the \\bibitem"))))
331 ((("author" "Author1 [and Author2 ...] [and others]")
332 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
333 ("booktitle" "Name of the conference proceedings")
334 ("year" "Year of publication"))
335 (("editor" "Editor1 [and Editor2 ...] [and others]")
336 ("volume" "Volume of the conference proceedings in the series")
337 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
338 ("series" "Series in which the conference proceedings appeared")
339 ("pages" "Pages in the conference proceedings")
340 ("address" "Location of the Proceedings")
341 ("month" "Month of the publication as a string (remove braces)")
342 ("organization" "Sponsoring organization of the conference")
343 ("publisher" "Publishing company, its location")
344 ("note" "Remarks to be put at the end of the \\bibitem")))
345 ((("author" "Author1 [and Author2 ...] [and others]")
346 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)"))
347 (("booktitle" "Name of the conference proceedings")
348 ("pages" "Pages in the conference proceedings")
349 ("year" "Year of publication")
350 ("editor" "Editor1 [and Editor2 ...] [and others]")
351 ("volume" "Volume of the conference proceedings in the series")
352 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
353 ("series" "Series in which the conference proceedings appeared")
354 ("address" "Location of the Proceedings")
355 ("month" "Month of the publication as a string (remove braces)")
356 ("organization" "Sponsoring organization of the conference")
357 ("publisher" "Publishing company, its location")
358 ("note" "Remarks to be put at the end of the \\bibitem"))))
360 ((("title" "Title of the manual"))
361 (("author" "Author1 [and Author2 ...] [and others]")
362 ("organization" "Publishing organization of the manual")
363 ("address" "Address of the organization")
364 ("edition" "Edition of the manual as a capitalized English word")
365 ("month" "Month of the publication as a string (remove braces)")
366 ("year" "Year of publication")
367 ("note" "Remarks to be put at the end of the \\bibitem"))))
369 ((("author" "Author1 [and Author2 ...] [and others]")
370 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
371 ("school" "School where the master\'s thesis was written")
372 ("year" "Year of publication"))
373 (("type" "Type of the master\'s thesis (if other than \"Master\'s thesis\")")
374 ("address" "Address of the school (if not part of field \"school\") or country")
375 ("month" "Month of the publication as a string (remove braces)")
376 ("note" "Remarks to be put at the end of the \\bibitem"))))
379 (("author" "Author1 [and Author2 ...] [and others]")
380 ("title" "Title of the work (BibTeX converts it to lowercase)")
381 ("howpublished" "The way in which the work was published")
382 ("month" "Month of the publication as a string (remove braces)")
383 ("year" "Year of publication")
384 ("note" "Remarks to be put at the end of the \\bibitem"))))
386 ((("author" "Author1 [and Author2 ...] [and others]")
387 ("title" "Title of the PhD. thesis")
388 ("school" "School where the PhD. thesis was written")
389 ("year" "Year of publication"))
390 (("type" "Type of the PhD. thesis")
391 ("address" "Address of the school (if not part of field \"school\") or country")
392 ("month" "Month of the publication as a string (remove braces)")
393 ("note" "Remarks to be put at the end of the \\bibitem"))))
395 ((("title" "Title of the conference proceedings")
396 ("year" "Year of publication"))
397 (("booktitle" "Title of the proceedings for cross references")
398 ("editor" "Editor1 [and Editor2 ...] [and others]")
399 ("volume" "Volume of the conference proceedings in the series")
400 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
401 ("series" "Series in which the conference proceedings appeared")
402 ("address" "Location of the Proceedings")
403 ("month" "Month of the publication as a string (remove braces)")
404 ("organization" "Sponsoring organization of the conference")
405 ("publisher" "Publishing company, its location")
406 ("note" "Remarks to be put at the end of the \\bibitem"))))
408 ((("author" "Author1 [and Author2 ...] [and others]")
409 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
410 ("institution" "Sponsoring institution of the report")
411 ("year" "Year of publication"))
412 (("type" "Type of the report (if other than \"technical report\")")
413 ("number" "Number of the technical report")
414 ("address" "Address of the institution (if not part of field \"institution\") or country")
415 ("month" "Month of the publication as a string (remove braces)")
416 ("note" "Remarks to be put at the end of the \\bibitem"))))
418 ((("author" "Author1 [and Author2 ...] [and others]")
419 ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
420 ("note" "Remarks to be put at the end of the \\bibitem"))
421 (("month" "Month of the publication as a string (remove braces)")
422 ("year" "Year of publication"))))
425 "Defines entry types and their associated fields.
427 \(ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL))
429 If the third element is nil, the first pair is always used.
430 If not, the second pair is used in the case of presence of a crossref
431 field and the third in the case of absence.
432 REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.
433 Each element of these lists is a list of the form
434 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).
435 COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.
436 FIELD-NAME is the name of the field, COMMENT-STRING the comment to
437 appear in the echo area, INIT is either the initial content of the
438 field or a function, which is called to determine the initial content
439 of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
440 field is an alternative. ALTERNATIVE-FLAG may be t only in the
441 REQUIRED or CROSSREF-REQUIRED lists.")
443 (defvar bibtex-comment-start
"@Comment"
444 "String starting a BibTeX comment.")
446 (defcustom bibtex-add-entry-hook nil
447 "List of functions to call when 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 to the
502 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 checked for duplicate 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 to use as a prefix for all generated 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.
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 prepend 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 name matching a 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 '("\\." "!" "\\?" ":" ";" "--")
642 "*Regexp list defining the termination of the main part of the title.
643 Case of the regexps is ignored.
644 See `bibtex-generate-autokey' for details."
645 :group
'bibtex-autokey
646 :type
'(repeat regexp
))
648 (defcustom bibtex-autokey-titlewords-stretch
2
649 "*Number of words that can additionally be used from the title.
650 These words are used only, if a sentence from the title can be ended then.
651 See `bibtex-generate-autokey' for details."
652 :group
'bibtex-autokey
655 (defcustom bibtex-autokey-titleword-ignore
656 '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das"
657 "[^A-Z].*" ".*[^A-Z0-9].*")
658 "*Determines words from the title that are not to be used in the key.
659 Each item of the list is a regexp. If a word of the title matchs a
660 regexp from that list, it is not included in the title part of the key.
661 See `bibtex-generate-autokey' for details."
662 :group
'bibtex-autokey
663 :type
'(repeat regexp
))
665 (defcustom bibtex-autokey-titleword-case-convert
'downcase
666 "*Function called for each titleword to perform case conversion.
667 See `bibtex-generate-autokey' for details."
668 :group
'bibtex-autokey
669 :type
'(choice (const :tag
"Preserve case" identity
)
670 (const :tag
"Downcase" downcase
)
671 (const :tag
"Capitalize" capitalize
)
672 (const :tag
"Upcase" upcase
)
673 (function :tag
"Conversion function")))
675 (defcustom bibtex-autokey-titleword-abbrevs nil
676 "*Determines exceptions to the usual abbreviation mechanism.
677 An alist of (OLD-REGEXP . NEW-STRING) pairs. Case is ignored
678 in matching against OLD-REGEXP, and the first matching pair is used.
679 See `bibtex-generate-autokey' for details."
680 :group
'bibtex-autokey
681 :type
'(repeat (cons (regexp :tag
"Old")
682 (string :tag
"New"))))
684 (defcustom bibtex-autokey-titleword-change-strings
685 bibtex-autokey-transcriptions
686 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
687 Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
688 Case is significant in OLD-REGEXP. All regexps are tried in the
689 order in which they appear in the list.
690 See `bibtex-generate-autokey' for details."
691 :group
'bibtex-autokey
692 :type
'(repeat (cons (regexp :tag
"Old")
693 (string :tag
"New"))))
695 (defcustom bibtex-autokey-titleword-length
5
696 "*Number of characters from title words to incorporate into key.
697 If this is set to anything but a number, all characters are used.
698 See `bibtex-generate-autokey' for details."
699 :group
'bibtex-autokey
700 :type
'(choice (const :tag
"All" infty
)
703 (defcustom bibtex-autokey-titleword-separator
"_"
704 "*String to be put between the title words.
705 See `bibtex-generate-autokey' for details."
706 :group
'bibtex-autokey
709 (defcustom bibtex-autokey-name-year-separator
""
710 "*String to be put between name part and year part of key.
711 See `bibtex-generate-autokey' for details."
712 :group
'bibtex-autokey
715 (defcustom bibtex-autokey-year-title-separator
":_"
716 "*String to be put between name part and year part of key.
717 See `bibtex-generate-autokey' for details."
718 :group
'bibtex-autokey
721 (defcustom bibtex-autokey-edit-before-use t
722 "*If non-nil, user is allowed to edit the generated key before it is used."
723 :group
'bibtex-autokey
726 (defcustom bibtex-autokey-before-presentation-function nil
727 "*Function to call before the generated key is presented.
728 If non-nil this should be a function which is called before the generated key
729 is presented. The function must take one argument (the automatically
730 generated key), and must return a string (the key to use)."
731 :group
'bibtex-autokey
732 :type
'(choice (const nil
) function
))
734 (defcustom bibtex-entry-offset
0
735 "*Offset for BibTeX entries.
736 Added to the value of all other variables which determine colums."
740 (defcustom bibtex-field-indentation
2
741 "*Starting column for the name part in BibTeX fields."
745 (defcustom bibtex-text-indentation
746 (+ bibtex-field-indentation
747 (length "organization = "))
748 "*Starting column for the text part in BibTeX fields.
749 Should be equal to the space needed for the longest name part."
753 (defcustom bibtex-contline-indentation
754 (+ bibtex-text-indentation
1)
755 "*Starting column for continuation lines of BibTeX fields."
759 (defcustom bibtex-align-at-equal-sign nil
760 "*If non-nil, align fields at equal sign instead of field text.
761 If non-nil, the column for the equal sign is the value of
762 `bibtex-text-indentation', minus 2."
766 (defcustom bibtex-comma-after-last-field nil
767 "*If non-nil, a comma is put at end of last field in the entry template."
771 (defcustom bibtex-autoadd-commas t
772 "If non-nil automatically add missing commas at end of BibTeX fields."
776 (defcustom bibtex-autofill-types
'("Proceedings")
777 "Automatically fill fields if possible for those BibTeX entry types."
779 :type
'(repeat string
))
781 (defcustom bibtex-summary-function
'bibtex-summary
782 "Function to call for generating a one-line summary of a BibTeX entry.
783 It takes one argument, the key of the entry.
784 Used by `bibtex-complete-key-cleanup' and `bibtex-copy-summary-as-kill'."
786 :type
'(choice (const :tag
"Default" bibtex-summary
)
787 (function :tag
"Personalized function")))
789 (defcustom bibtex-generate-url-list
790 '((("url" .
".*:.*"))
791 ;; Example of a complex setup.
792 (("journal" .
"\\<\\(PR[ABCDEL]?\\|RMP\\)\\>")
793 "http://publish.aps.org/abstract/"
794 ("journal" ".*" downcase
)
798 ("pages" "\\`\\([0-9]+\\)" 1)))
799 "List of schemes for generating the URL of a BibTeX entry.
800 These schemes are used by `bibtex-url'.
802 Each scheme is of the form ((FIELD . REGEXP) STEP...).
804 FIELD is a field name as returned by `bibtex-parse-entry'.
805 REGEXP is matched against the text of FIELD. If the match succeeds, then
806 this scheme will be used. If no STEPS are specified the matched text is used
807 as the URL, otherwise the URL is built by concatenating the STEPS.
809 A STEP can be a string or a list (FIELD REGEXP REPLACE) in which case
810 the text of FIELD is matched against REGEXP, and is replaced with REPLACE.
811 REPLACE can be a string, or a number (which selects the corresponding submatch)
812 or a function called with the field's text as argument and with the
813 `match-data' properly set.
815 Case is always ignored. Always remove the field delimiters."
819 (cons :tag
"Matcher" :extra-offset
4
820 (string :tag
"BibTeX field")
821 (regexp :tag
"Regexp"))
822 (repeat :tag
"Steps to generate URL" :inline t
824 (string :tag
"Literal text")
825 (list (string :tag
"BibTeX field")
826 (regexp :tag
"Regexp")
827 (choice (string :tag
"Replacement")
828 (integer :tag
"Sub-match")
829 (function :tag
"Filter"))))))))
831 ;; bibtex-font-lock-keywords is a user option as well, but since the
832 ;; patterns used to define this variable are defined in a later
833 ;; section of this file, it is defined later.
836 ;; Syntax Table, Keybindings and BibTeX Entry List
837 (defvar bibtex-mode-syntax-table
838 (let ((st (make-syntax-table)))
839 (modify-syntax-entry ?
\" "\"" st
)
840 (modify-syntax-entry ?$
"$$ " st
)
841 (modify-syntax-entry ?%
"< " st
)
842 (modify-syntax-entry ?
' "w " st
)
843 (modify-syntax-entry ?
@ "w " st
)
844 (modify-syntax-entry ?
\\ "\\" st
)
845 (modify-syntax-entry ?
\f "> " st
)
846 (modify-syntax-entry ?
\n "> " st
)
847 ;; Keys cannot have = in them (wrong font-lock of @string{foo=bar}).
848 (modify-syntax-entry ?
= "." st
)
849 (modify-syntax-entry ?~
" " st
)
851 "Syntax table used in BibTeX mode buffers.")
853 (defvar bibtex-mode-map
854 (let ((km (make-sparse-keymap)))
855 ;; The Key `C-c&' is reserved for reftex.el
856 (define-key km
"\t" 'bibtex-find-text
)
857 (define-key km
"\n" 'bibtex-next-field
)
858 (define-key km
"\M-\t" 'bibtex-complete
)
859 (define-key km
"\C-c\"" 'bibtex-remove-delimiters
)
860 (define-key km
"\C-c{" 'bibtex-remove-delimiters
)
861 (define-key km
"\C-c}" 'bibtex-remove-delimiters
)
862 (define-key km
"\C-c\C-c" 'bibtex-clean-entry
)
863 (define-key km
"\C-c\C-q" 'bibtex-fill-entry
)
864 (define-key km
"\C-c\C-s" 'bibtex-find-entry
)
865 (define-key km
"\C-c\C-t" 'bibtex-copy-summary-as-kill
)
866 (define-key km
"\C-c?" 'bibtex-print-help-message
)
867 (define-key km
"\C-c\C-p" 'bibtex-pop-previous
)
868 (define-key km
"\C-c\C-n" 'bibtex-pop-next
)
869 (define-key km
"\C-c\C-k" 'bibtex-kill-field
)
870 (define-key km
"\C-c\M-k" 'bibtex-copy-field-as-kill
)
871 (define-key km
"\C-c\C-w" 'bibtex-kill-entry
)
872 (define-key km
"\C-c\M-w" 'bibtex-copy-entry-as-kill
)
873 (define-key km
"\C-c\C-y" 'bibtex-yank
)
874 (define-key km
"\C-c\M-y" 'bibtex-yank-pop
)
875 (define-key km
"\C-c\C-d" 'bibtex-empty-field
)
876 (define-key km
"\C-c\C-f" 'bibtex-make-field
)
877 (define-key km
"\C-c\C-u" 'bibtex-entry-update
)
878 (define-key km
"\C-c$" 'bibtex-ispell-abstract
)
879 (define-key km
"\M-\C-a" 'bibtex-beginning-of-entry
)
880 (define-key km
"\M-\C-e" 'bibtex-end-of-entry
)
881 (define-key km
"\C-\M-l" 'bibtex-reposition-window
)
882 (define-key km
"\C-\M-h" 'bibtex-mark-entry
)
883 (define-key km
"\C-c\C-b" 'bibtex-entry
)
884 (define-key km
"\C-c\C-rn" 'bibtex-narrow-to-entry
)
885 (define-key km
"\C-c\C-rw" 'widen
)
886 (define-key km
"\C-c\C-l" 'bibtex-url
)
887 (define-key km
"\C-c\C-o" 'bibtex-remove-OPT-or-ALT
)
888 (define-key km
"\C-c\C-e\C-i" 'bibtex-InProceedings
)
889 (define-key km
"\C-c\C-ei" 'bibtex-InCollection
)
890 (define-key km
"\C-c\C-eI" 'bibtex-InBook
)
891 (define-key km
"\C-c\C-e\C-a" 'bibtex-Article
)
892 (define-key km
"\C-c\C-e\C-b" 'bibtex-InBook
)
893 (define-key km
"\C-c\C-eb" 'bibtex-Book
)
894 (define-key km
"\C-c\C-eB" 'bibtex-Booklet
)
895 (define-key km
"\C-c\C-e\C-c" 'bibtex-InCollection
)
896 (define-key km
"\C-c\C-e\C-m" 'bibtex-Manual
)
897 (define-key km
"\C-c\C-em" 'bibtex-MastersThesis
)
898 (define-key km
"\C-c\C-eM" 'bibtex-Misc
)
899 (define-key km
"\C-c\C-e\C-p" 'bibtex-InProceedings
)
900 (define-key km
"\C-c\C-ep" 'bibtex-Proceedings
)
901 (define-key km
"\C-c\C-eP" 'bibtex-PhdThesis
)
902 (define-key km
"\C-c\C-e\M-p" 'bibtex-Preamble
)
903 (define-key km
"\C-c\C-e\C-s" 'bibtex-String
)
904 (define-key km
"\C-c\C-e\C-t" 'bibtex-TechReport
)
905 (define-key km
"\C-c\C-e\C-u" 'bibtex-Unpublished
)
907 "Keymap used in BibTeX mode.")
910 bibtex-edit-menu bibtex-mode-map
"BibTeX-Edit Menu in BibTeX mode"
912 ("Moving inside an Entry"
913 ["End of Field" bibtex-find-text t
]
914 ["Next Field" bibtex-next-field t
]
915 ["Beginning of Entry" bibtex-beginning-of-entry t
]
916 ["End of Entry" bibtex-end-of-entry t
])
917 ("Moving in BibTeX Buffer"
918 ["Find Entry" bibtex-find-entry t
]
919 ["Find Crossref Entry" bibtex-find-crossref t
])
920 ("Moving between BibTeX Buffers"
921 ["Find Entry Globally" bibtex-find-entry-globally t
])
923 ("Operating on Current Field"
924 ["Fill Field" fill-paragraph t
]
925 ["Remove Delimiters" bibtex-remove-delimiters t
]
926 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t
]
927 ["Clear Field" bibtex-empty-field t
]
929 ["Kill Field" bibtex-kill-field t
]
930 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t
]
931 ["Paste Most Recently Killed Field" bibtex-yank t
]
932 ["Paste Previously Killed Field" bibtex-yank-pop t
]
934 ["Make New Field" bibtex-make-field t
]
936 ["Snatch from Similar Following Field" bibtex-pop-next t
]
937 ["Snatch from Similar Preceding Field" bibtex-pop-previous t
]
939 ["String or Key Complete" bibtex-complete t
]
941 ["Help about Current Field" bibtex-print-help-message t
])
942 ("Operating on Current Entry"
943 ["Fill Entry" bibtex-fill-entry t
]
944 ["Clean Entry" bibtex-clean-entry t
]
945 ["Update Entry" bibtex-entry-update t
]
947 ["Kill Entry" bibtex-kill-entry t
]
948 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t
]
949 ["Paste Most Recently Killed Entry" bibtex-yank t
]
950 ["Paste Previously Killed Entry" bibtex-yank-pop t
]
952 ["Copy Summary to Kill Ring" bibtex-copy-summary-as-kill t
]
954 ["Ispell Entry" bibtex-ispell-entry t
]
955 ["Ispell Entry Abstract" bibtex-ispell-abstract t
]
956 ["Narrow to Entry" bibtex-narrow-to-entry t
]
958 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
959 (fboundp 'reftex-view-crossref-from-bibtex
)])
960 ("Operating on Buffer or Region"
961 ["Validate Entries" bibtex-validate t
]
962 ["Sort Entries" bibtex-sort-buffer t
]
963 ["Reformat Entries" bibtex-reformat t
]
964 ["Count Entries" bibtex-count-entries t
]
966 ["Convert Alien Buffer" bibtex-convert-alien t
])
967 ("Operating on Multiple Buffers"
968 ["Validate Entries" bibtex-validate-globally t
])))
971 bibtex-entry-menu bibtex-mode-map
"Entry-Types Menu in BibTeX mode"
973 ["Article in Journal" bibtex-Article t
]
974 ["Article in Conference Proceedings" bibtex-InProceedings t
]
975 ["Article in a Collection" bibtex-InCollection t
]
976 ["Chapter or Pages in a Book" bibtex-InBook t
]
977 ["Conference Proceedings" bibtex-Proceedings t
]
978 ["Book" bibtex-Book t
]
979 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t
]
980 ["PhD. Thesis" bibtex-PhdThesis t
]
981 ["Master's Thesis" bibtex-MastersThesis t
]
982 ["Technical Report" bibtex-TechReport t
]
983 ["Technical Manual" bibtex-Manual t
]
984 ["Unpublished" bibtex-Unpublished t
]
985 ["Miscellaneous" bibtex-Misc t
]
986 ["String" bibtex-String t
]
987 ["Preamble" bibtex-Preamble t
]))
990 ;; Internal Variables
992 (defvar bibtex-pop-previous-search-point nil
993 "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
995 (defvar bibtex-pop-next-search-point nil
996 "Next point where `bibtex-pop-next' starts looking for a similar entry.")
998 (defvar bibtex-field-kill-ring nil
999 "Ring of least recently killed fields.
1000 At most `bibtex-field-kill-ring-max' items are kept here.")
1002 (defvar bibtex-field-kill-ring-yank-pointer nil
1003 "The tail of `bibtex-field-kill-ring' whose car is the last item yanked.")
1005 (defvar bibtex-entry-kill-ring nil
1006 "Ring of least recently killed entries.
1007 At most `bibtex-entry-kill-ring-max' items are kept here.")
1009 (defvar bibtex-entry-kill-ring-yank-pointer nil
1010 "The tail of `bibtex-entry-kill-ring' whose car is the last item yanked.")
1012 (defvar bibtex-last-kill-command nil
1013 "Type of the last kill command (either 'field or 'entry).")
1015 (defvar bibtex-strings
1016 (lazy-completion-table bibtex-strings
1017 bibtex-parse-strings
(bibtex-string-files-init))
1018 "Completion table for BibTeX string keys.
1019 Initialized from `bibtex-predefined-strings' and `bibtex-string-files'.")
1020 (make-variable-buffer-local 'bibtex-strings
)
1022 (defvar bibtex-reference-keys
1023 (lazy-completion-table bibtex-reference-keys bibtex-parse-keys nil t
)
1024 "Completion table for BibTeX reference keys.
1025 The CDRs of the elements are t for header keys and nil for crossref keys.")
1026 (make-variable-buffer-local 'bibtex-reference-keys
)
1028 (defvar bibtex-buffer-last-parsed-tick nil
1029 "Value of `buffer-modified-tick' last time buffer was parsed for keys.")
1031 (defvar bibtex-parse-idle-timer nil
1032 "Stores if timer is already installed.")
1034 (defvar bibtex-progress-lastperc nil
1035 "Last reported percentage for the progress message.")
1037 (defvar bibtex-progress-lastmes nil
1038 "Last reported progress message.")
1040 (defvar bibtex-progress-interval nil
1041 "Interval for progress messages.")
1043 (defvar bibtex-key-history nil
1044 "History list for reading keys.")
1046 (defvar bibtex-entry-type-history nil
1047 "History list for reading entry types.")
1049 (defvar bibtex-field-history nil
1050 "History list for reading field names.")
1052 (defvar bibtex-reformat-previous-options nil
1053 "Last reformat options given.")
1055 (defvar bibtex-reformat-previous-reference-keys nil
1056 "Last reformat reference keys option given.")
1058 (defconst bibtex-field-name
"[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*"
1059 "Regexp matching the name part of a BibTeX field.")
1061 (defconst bibtex-entry-type
(concat "@" bibtex-field-name
)
1062 "Regexp matching the type part of a BibTeX entry.")
1064 (defconst bibtex-reference-key
"[][a-zA-Z0-9.:;?!`'/*@+|()<>&_^$-]+"
1065 "Regexp matching the reference key part of a BibTeX entry.")
1067 (defconst bibtex-field-const
"[][a-zA-Z0-9.:;?!`'/*@+=|<>&_^$-]+"
1068 "Regexp matching a BibTeX field constant.")
1070 (defconst bibtex-entry-head
1071 (concat "^[ \t]*\\("
1073 "\\)[ \t]*[({][ \t\n]*\\("
1074 bibtex-reference-key
1076 "Regexp matching the header line of a BibTeX entry.")
1078 (defconst bibtex-entry-maybe-empty-head
1079 (concat bibtex-entry-head
"?")
1080 "Regexp matching the header line of a BibTeX entry (possibly without key).")
1082 (defconst bibtex-type-in-head
1
1083 "Regexp subexpression number of the type part in `bibtex-entry-head'.")
1085 (defconst bibtex-key-in-head
2
1086 "Regexp subexpression number of the key part in `bibtex-entry-head'.")
1088 (defconst bibtex-entry-postfix
"[ \t\n]*,?[ \t\n]*[})]"
1089 "Regexp matching the postfix of a BibTeX entry.")
1091 (defvar bibtex-known-entry-type-re
1092 (regexp-opt (mapcar 'car bibtex-entry-field-alist
))
1093 "Regexp matching the name of a BibTeX entry type.")
1095 (defvar bibtex-valid-entry-re
1096 (concat "@[ \t]*\\(" bibtex-known-entry-type-re
"\\)")
1097 "Regexp matching the name of a valid BibTeX entry.")
1099 (defvar bibtex-valid-entry-whitespace-re
1100 (concat "[ \t\n]*\\(" bibtex-valid-entry-re
"\\)")
1101 "Regexp matching the name of a valid BibTeX entry preceded by whitespace.")
1103 (defvar bibtex-any-valid-entry-re
1105 (regexp-opt (append '("String")
1106 (mapcar 'car bibtex-entry-field-alist
))
1108 "Regexp matching the name of any valid BibTeX entry (including string).")
1111 (defconst bibtex-empty-field-re
"\"\"\\|{}"
1112 "Regexp matching an empty field.")
1114 (defconst bibtex-font-lock-syntactic-keywords
1115 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start
0 1) "\\)"
1116 (substring bibtex-comment-start
1) "\\>")
1119 (defvar bibtex-font-lock-keywords
1120 ;; entry type and reference key
1121 `((,bibtex-entry-maybe-empty-head
1122 (,bibtex-type-in-head font-lock-function-name-face
)
1123 (,bibtex-key-in-head font-lock-constant-face nil t
))
1124 ;; optional field names (treated as comments)
1125 (,(concat "^[ \t]*\\(OPT" bibtex-field-name
"\\)[ \t]*=")
1126 1 font-lock-comment-face
)
1128 (,(concat "^[ \t]*\\(" bibtex-field-name
"\\)[ \t]*=")
1129 1 font-lock-variable-name-face
)
1131 bibtex-font-lock-url bibtex-font-lock-crossref
)
1132 "*Default expressions to highlight in BibTeX mode.")
1134 (defvar bibtex-font-lock-url-regexp
1135 ;; Assume that field names begin at the beginning of a line.
1136 (concat "^[ \t]*" (regexp-opt (mapcar 'caar bibtex-generate-url-list
) t
)
1138 "Regexp for `bibtex-font-lock-url'.")
1140 (defvar bibtex-field-name-for-parsing nil
1141 "Regexp of field name to be parsed by function `bibtex-parse-field-name'.
1142 Passed by dynamic scoping.")
1144 (defvar bibtex-sort-entry-class-alist
1146 (dolist (class bibtex-sort-entry-class alist
)
1148 (dolist (entry class
)
1149 ;; all entry names should be downcase (for ease of comparison)
1150 (push (cons (if (stringp entry
) (downcase entry
) entry
) i
) alist
))))
1151 "Alist mapping entry types to their sorting index.
1152 Auto-generated from `bibtex-sort-entry-class'.
1153 Used when `bibtex-maintain-sorted-entries' is `entry-class'.")
1156 ;; Support for hideshow minor mode
1157 (defun bibtex-hs-forward-sexp (arg)
1158 "Replacement for `forward-sexp' to be used by `hs-minor-mode'.
1160 (if (looking-at "@\\S(*\\s(")
1161 (goto-char (1- (match-end 0))))
1165 'hs-special-modes-alist
1166 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil
))
1169 (defun bibtex-parse-association (parse-lhs parse-rhs
)
1170 "Parse a string of the format <left-hand-side = right-hand-side>.
1171 The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
1172 substrings. These functions are expected to return nil if parsing is not
1173 successfull. If both functions return non-nil, a pair containing the returned
1174 values of the functions PARSE-LHS and PARSE-RHS is returned."
1177 (let ((left (funcall parse-lhs
))
1180 (looking-at "[ \t\n]*=[ \t\n]*")
1181 (goto-char (match-end 0))
1182 (setq right
(funcall parse-rhs
)))
1183 (cons left right
))))))
1185 (defun bibtex-parse-field-name ()
1186 "Parse the field name stored in `bibtex-field-name-for-parsing'.
1187 If the field name is found, return a triple consisting of the position of the
1188 very first character of the match, the actual starting position of the name
1189 part and end position of the match. Move point to end of field name.
1190 If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceeding
1191 BibTeX field as necessary."
1192 (cond ((looking-at ",[ \t\n]*")
1193 (let ((start (point)))
1194 (goto-char (match-end 0))
1195 (when (looking-at bibtex-field-name-for-parsing
)
1196 (goto-char (match-end 0))
1197 (list start
(match-beginning 0) (match-end 0)))))
1198 ;; Maybe add a missing comma.
1199 ((and bibtex-autoadd-commas
1200 (looking-at (concat "[ \t\n]*\\(?:" bibtex-field-name-for-parsing
1202 (skip-chars-backward " \t\n")
1206 (bibtex-parse-field-name))))
1208 (defconst bibtex-braced-string-syntax-table
1209 (let ((st (make-syntax-table)))
1210 (modify-syntax-entry ?\
{ "(}" st
)
1211 (modify-syntax-entry ?\
} "){" st
)
1212 (modify-syntax-entry ?\
[ "." st
)
1213 (modify-syntax-entry ?\
] "." st
)
1214 (modify-syntax-entry ?\
( "." st
)
1215 (modify-syntax-entry ?\
) "." st
)
1216 (modify-syntax-entry ?
\\ "." st
)
1217 (modify-syntax-entry ?
\" "." st
)
1219 "Syntax-table to parse matched braces.")
1221 (defconst bibtex-quoted-string-syntax-table
1222 (let ((st (make-syntax-table)))
1223 (modify-syntax-entry ?
\\ "\\" st
)
1224 (modify-syntax-entry ?
\" "\"" st
)
1226 "Syntax-table to parse matched quotes.")
1228 (defun bibtex-parse-field-string ()
1229 "Parse a field string enclosed by braces or quotes.
1230 If a syntactically correct string is found, a pair containing the start and
1231 end position of the field string is returned, nil otherwise."
1233 (or (and (eq (following-char) ?
\")
1235 (with-syntax-table bibtex-quoted-string-syntax-table
1238 (and (eq (following-char) ?\
{)
1240 (with-syntax-table bibtex-braced-string-syntax-table
1244 (cons (point) end-point
))))
1246 (defun bibtex-parse-field-text ()
1247 "Parse the text part of a BibTeX field.
1248 The text part is either a string, or an empty string, or a constant followed
1249 by one or more <# (string|constant)> pairs. If a syntactically correct text
1250 is found, a pair containing the start and end position of the text is
1251 returned, nil otherwise. Move point to end of field text."
1252 (let ((starting-point (point))
1253 end-point failure boundaries
)
1254 (while (not (or end-point failure
))
1255 (cond ((looking-at bibtex-field-const
)
1256 (goto-char (match-end 0)))
1257 ((setq boundaries
(bibtex-parse-field-string))
1258 (goto-char (cdr boundaries
)))
1260 (if (looking-at "[ \t\n]*#[ \t\n]*")
1261 (goto-char (match-end 0))
1262 (setq end-point
(point))))
1263 (if (and (not failure
)
1265 (cons starting-point end-point
))))
1267 (defun bibtex-parse-field (name)
1268 "Parse a BibTeX field of regexp NAME.
1269 If a syntactically correct field is found, a pair containing the boundaries of
1270 the name and text parts of the field is returned."
1271 (let ((bibtex-field-name-for-parsing name
))
1272 (bibtex-parse-association 'bibtex-parse-field-name
1273 'bibtex-parse-field-text
)))
1275 (defun bibtex-search-forward-field (name &optional bound
)
1276 "Search forward to find a field of name NAME.
1277 If a syntactically correct field is found, a pair containing the boundaries of
1278 the name and text parts of the field is returned. The search is limited by
1279 optional arg BOUND. If BOUND is t the search is limited by the end of the
1280 current entry. Do not move point."
1283 (unless (integer-or-marker-p bound
)
1284 (setq bound
(if bound
1285 (save-excursion (bibtex-end-of-entry))
1287 (let ((case-fold-search t
)
1288 (bibtex-field-name-for-parsing name
)
1289 boundaries temp-boundaries
)
1290 (while (and (not boundaries
)
1292 (search-forward "," bound t
))
1293 (goto-char (match-beginning 0))
1294 (if (and (setq temp-boundaries
1295 (bibtex-parse-association 'bibtex-parse-field-name
1296 'bibtex-parse-field-text
))
1297 (<= (cddr temp-boundaries
) bound
))
1298 (setq boundaries temp-boundaries
)
1302 (defun bibtex-search-backward-field (name &optional bound
)
1303 "Search backward to find a field of name NAME.
1304 If a syntactically correct field is found, a pair containing the boundaries of
1305 the name and text parts of the field is returned. The search is limited by
1306 optional arg BOUND. If BOUND is t the search is limited by the beginning of the
1307 current entry. Do not move point."
1310 (unless (integer-or-marker-p bound
)
1311 (setq bound
(if bound
1312 (save-excursion (bibtex-beginning-of-entry))
1314 (let ((case-fold-search t
)
1315 (bibtex-field-name-for-parsing name
)
1316 boundaries temp-boundaries
)
1317 (while (and (not boundaries
)
1319 (search-backward "," bound t
))
1320 (if (setq temp-boundaries
1321 (bibtex-parse-association 'bibtex-parse-field-name
1322 'bibtex-parse-field-text
))
1323 (setq boundaries temp-boundaries
)))
1326 (defsubst bibtex-start-of-field
(bounds)
1327 (nth 0 (car bounds
)))
1328 (defsubst bibtex-start-of-name-in-field
(bounds)
1329 (nth 1 (car bounds
)))
1330 (defsubst bibtex-end-of-name-in-field
(bounds)
1331 (nth 2 (car bounds
)))
1332 (defsubst bibtex-end-of-field
(bounds)
1334 (defsubst bibtex-start-of-text-in-field
(bounds)
1336 (defsubst bibtex-end-of-text-in-field
(bounds)
1339 (defun bibtex-name-in-field (bounds &optional remove-opt-alt
)
1340 "Get content of name in BibTeX field defined via BOUNDS.
1341 If optional arg REMOVE-OPT-ALT is non-nil remove \"OPT\" and \"ALT\"."
1342 (let ((name (buffer-substring-no-properties (nth 1 (car bounds
))
1343 (nth 2 (car bounds
)))))
1344 (if (and remove-opt-alt
1345 (string-match "\\`\\(OPT\\|ALT\\)" name
))
1349 (defun bibtex-text-in-field-bounds (bounds &optional remove-delim
)
1350 "Get content of text in BibTeX field defined via BOUNDS.
1351 If optional arg REMOVE-DELIM is non-nil remove enclosing field delimiters
1353 (let ((content (buffer-substring-no-properties (cadr bounds
)
1355 (if (and remove-delim
1356 (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content
))
1357 (substring content
(match-beginning 1) (match-end 1))
1360 (defun bibtex-text-in-field (field &optional follow-crossref
)
1361 "Get content of field FIELD of current BibTeX entry. Return nil if not found.
1362 If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1365 ;; We want to jump back and forth while searching FIELD
1366 (bibtex-narrow-to-entry)
1367 (goto-char (point-min))
1368 (let ((bounds (bibtex-search-forward-field field
))
1370 (cond (bounds (bibtex-text-in-field-bounds bounds t
))
1371 ((and follow-crossref
1372 (progn (goto-char (point-min))
1373 (setq bounds
(bibtex-search-forward-field
1374 "\\(OPT\\)?crossref"))))
1375 (setq crossref-field
(bibtex-text-in-field-bounds bounds t
))
1377 (if (bibtex-find-crossref crossref-field
)
1378 ;; Do not pass FOLLOW-CROSSREF because we want
1379 ;; to follow crossrefs only one level of recursion.
1380 (bibtex-text-in-field field
))))))))
1382 (defun bibtex-parse-string-prefix ()
1383 "Parse the prefix part of a BibTeX string entry, including reference key.
1384 If the string prefix is found, return a triple consisting of the position of
1385 the very first character of the match, the actual starting position of the
1386 reference key and the end position of the match."
1387 (let ((case-fold-search t
))
1388 (if (looking-at "^[ \t]*@string[ \t\n]*[({][ \t\n]*")
1389 (let ((start (point)))
1390 (goto-char (match-end 0))
1391 (when (looking-at bibtex-reference-key
)
1392 (goto-char (match-end 0))
1397 (defun bibtex-parse-string-postfix ()
1398 "Parse the postfix part of a BibTeX string entry, including the text.
1399 If the string postfix is found, return a triple consisting of the position of
1400 the actual starting and ending position of the text and the very last
1401 character of the string entry. Move point past BibTeX string entry."
1402 (let* ((case-fold-search t
)
1403 (bounds (bibtex-parse-field-text)))
1405 (goto-char (cdr bounds
))
1406 (when (looking-at "[ \t\n]*[})]")
1407 (goto-char (match-end 0))
1412 (defun bibtex-parse-string ()
1413 "Parse a BibTeX string entry.
1414 If a syntactically correct entry is found, a pair containing the boundaries of
1415 the reference key and text parts of the entry is returned.
1416 Move point past BibTeX string entry."
1417 (bibtex-parse-association 'bibtex-parse-string-prefix
1418 'bibtex-parse-string-postfix
))
1420 (defun bibtex-search-forward-string ()
1421 "Search forward to find a BibTeX string entry.
1422 If a syntactically correct entry is found, a pair containing the boundaries of
1423 the reference key and text parts of the string is returned. Do not move point."
1426 (let ((case-fold-search t
)
1428 (while (and (not boundaries
)
1429 (search-forward-regexp
1430 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t
))
1431 (goto-char (match-beginning 0))
1432 (unless (setq boundaries
(bibtex-parse-string))
1436 (defun bibtex-search-backward-string ()
1437 "Search backward to find a BibTeX string entry.
1438 If a syntactically correct entry is found, a pair containing the boundaries of
1439 the reference key and text parts of the field is returned. Do not move point."
1442 (let ((case-fold-search t
)
1444 (while (and (not boundaries
)
1445 (search-backward-regexp
1446 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t
))
1447 (goto-char (match-beginning 0))
1448 (setq boundaries
(bibtex-parse-string)))
1451 (defun bibtex-reference-key-in-string (bounds)
1452 (buffer-substring-no-properties (nth 1 (car bounds
))
1453 (nth 2 (car bounds
))))
1455 (defun bibtex-text-in-string (bounds &optional remove-delim
)
1456 "Get content of text in BibTeX string field defined via BOUNDS.
1457 If optional arg REMOVE-DELIM is non-nil remove enclosing field
1458 delimiters if present."
1459 (let ((content (buffer-substring-no-properties (nth 0 (cdr bounds
))
1460 (nth 1 (cdr bounds
)))))
1461 (if (and remove-delim
1462 (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content
))
1463 (substring content
(match-beginning 1) (match-end 1))
1466 (defsubst bibtex-start-of-text-in-string
(bounds)
1467 (nth 0 (cdr bounds
)))
1468 (defsubst bibtex-end-of-text-in-string
(bounds)
1469 (nth 1 (cdr bounds
)))
1470 (defsubst bibtex-end-of-string
(bounds)
1471 (nth 2 (cdr bounds
)))
1473 (defsubst bibtex-type-in-head
()
1474 "Extract BibTeX type in head."
1476 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head
))
1477 (match-end bibtex-type-in-head
)))
1479 (defsubst bibtex-key-in-head
(&optional empty
)
1480 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
1481 (or (match-string-no-properties bibtex-key-in-head
)
1486 (defsubst bibtex-string
= (str1 str2
)
1487 "Return t if STR1 and STR2 are equal, ignoring case."
1488 (eq t
(compare-strings str1
0 nil str2
0 nil t
)))
1490 (defun bibtex-delete-whitespace ()
1491 "Delete all whitespace starting at point."
1492 (if (looking-at "[ \t\n]+")
1493 (delete-region (point) (match-end 0))))
1495 (defun bibtex-current-line ()
1496 "Compute line number of point regardless whether the buffer is narrowed."
1497 (+ (count-lines 1 (point))
1500 (defun bibtex-skip-to-valid-entry (&optional backward
)
1501 "Move point to beginning of the next valid BibTeX entry.
1502 Do not move if we are already at beginning of a valid BibTeX entry.
1503 With optional argument BACKWARD non-nil, move backward to
1504 beginning of previous valid one. A valid entry is a syntactical correct one
1505 with type contained in `bibtex-entry-field-alist' or, if
1506 `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
1507 entry. Return buffer position of beginning and ending of entry if a valid
1508 entry is found, nil otherwise."
1510 (let ((case-fold-search t
)
1512 (while (not (or found
(if backward
(bobp) (eobp))))
1515 (cond ((or (and (looking-at bibtex-valid-entry-re
)
1516 (setq found
(bibtex-search-entry nil nil t
))
1517 (equal (match-beginning 0) pnt
))
1518 (and (not bibtex-sort-ignore-string-entries
)
1519 (setq bounds
(bibtex-parse-string))
1520 (setq found
(cons (bibtex-start-of-field bounds
)
1521 (bibtex-end-of-string bounds
)))))
1524 (if (re-search-backward "^[ \t]*\\(@\\)" nil
'move
)
1525 (goto-char (match-beginning 1))))
1526 (t (if (re-search-forward "\n[ \t]*@" nil
'move
)
1527 (forward-char -
1))))))
1530 (defun bibtex-map-entries (fun)
1531 "Call FUN for each BibTeX entry in buffer (possibly narrowed).
1532 FUN is called with three arguments, the key of the entry and the buffer
1533 positions (marker) of beginning and end of entry. Point is inside the entry.
1534 If `bibtex-sort-ignore-string-entries' is non-nil, FUN will not be called for
1536 (let ((case-fold-search t
))
1538 (goto-char (point-min))
1539 (while (re-search-forward bibtex-entry-head nil t
)
1540 (let ((entry-type (bibtex-type-in-head))
1541 (key (bibtex-key-in-head ""))
1542 (beg (copy-marker (match-beginning 0)))
1543 (end (copy-marker (save-excursion (bibtex-end-of-entry)))))
1545 (if (or (and (not bibtex-sort-ignore-string-entries
)
1546 (bibtex-string= entry-type
"string"))
1547 (assoc-string entry-type bibtex-entry-field-alist t
))
1548 (funcall fun key beg end
)))
1549 (goto-char end
))))))
1551 (defun bibtex-progress-message (&optional flag interval
)
1552 "Echo a message about progress of current buffer.
1553 If FLAG is a string, the message is initialized (in this case a
1554 value for INTERVAL may be given as well (if not this is set to 5)).
1555 If FLAG is done, the message is deinitialized.
1556 If FLAG is nil, a message is echoed if point was incremented at least
1557 `bibtex-progress-interval' percent since last message was echoed."
1558 (cond ((stringp flag
)
1559 (setq bibtex-progress-lastmes flag
)
1560 (setq bibtex-progress-interval
(or interval
5)
1561 bibtex-progress-lastperc
0))
1563 (message "%s (done)" bibtex-progress-lastmes
)
1564 (setq bibtex-progress-lastmes nil
))
1566 (let* ((size (- (point-max) (point-min)))
1567 (perc (if (= size
0)
1569 (/ (* 100 (- (point) (point-min))) size
))))
1570 (when (>= perc
(+ bibtex-progress-lastperc
1571 bibtex-progress-interval
))
1572 (setq bibtex-progress-lastperc perc
)
1573 (message "%s (%d%%)" bibtex-progress-lastmes perc
))))))
1575 (defun bibtex-field-left-delimiter ()
1576 "Return a string dependent on `bibtex-field-delimiters'."
1577 (if (equal bibtex-field-delimiters
'braces
)
1581 (defun bibtex-field-right-delimiter ()
1582 "Return a string dependent on `bibtex-field-delimiters'."
1583 (if (equal bibtex-field-delimiters
'braces
)
1587 (defun bibtex-entry-left-delimiter ()
1588 "Return a string dependent on `bibtex-entry-delimiters'."
1589 (if (equal bibtex-entry-delimiters
'braces
)
1593 (defun bibtex-entry-right-delimiter ()
1594 "Return a string dependent on `bibtex-entry-delimiters'."
1595 (if (equal bibtex-entry-delimiters
'braces
)
1599 (defun bibtex-search-entry (empty-head &optional bound noerror backward
)
1600 "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t).
1601 BOUND and NOERROR are exactly as in `re-search-forward'. If BACKWARD
1602 is non-nil, search is done in reverse direction. Point is moved past the
1603 closing delimiter (at the beginning of entry if BACKWARD is non-nil).
1604 Return a cons pair with buffer positions of beginning and end of entry.
1605 After call to this function MATCH-BEGINNING and MATCH-END functions
1606 are defined, but only for the head part of the entry
1607 \(especially (match-end 0) just gives the end of the head part)."
1609 (entry-head-re (if empty-head
1610 bibtex-entry-maybe-empty-head
1611 bibtex-entry-head
)))
1614 (while (and (not found
)
1615 (re-search-backward entry-head-re bound noerror
))
1616 (setq found
(bibtex-search-entry empty-head pnt t
)))
1618 (progn (goto-char (match-beginning 0))
1620 (cond ((not noerror
)
1622 (error "Backward search of BibTeX entry failed"))
1627 (let ((limit (or bound
(point-max)))
1629 (while (and (not found
)
1630 (re-search-forward entry-head-re bound noerror
))
1634 (goto-char (match-end bibtex-type-in-head
))
1635 (looking-at "[ \t]*("))
1636 ;; entry opened with parenthesis
1639 (infix-start (point))
1641 (while (not finished
)
1642 (skip-chars-forward " \t\n" limit
)
1643 (if (and (setq bounds
(bibtex-parse-field bibtex-field-name
))
1644 (<= (bibtex-end-of-field bounds
) limit
))
1645 (setq infix-start
(bibtex-end-of-field bounds
))
1647 (goto-char infix-start
))
1648 ;; This matches the infix* part. The AND construction assures
1649 ;; that BOUND is respected.
1650 (when (and (looking-at bibtex-entry-postfix
)
1651 (eq (char-before (match-end 0)) entry-closer
)
1652 (<= (match-end 0) limit
))
1653 (goto-char (match-end 0))
1656 (cons (match-beginning 0) (point))
1657 (cond ((not noerror
)
1659 (error "Search of BibTeX entry failed"))
1665 (defun bibtex-flash-head ()
1666 "Flash at BibTeX entry head before point, if exists."
1667 (let ((case-fold-search t
)
1669 (cond ((re-search-backward bibtex-entry-head nil t
)
1670 (goto-char (match-beginning bibtex-type-in-head
))
1671 (setq flash
(match-end bibtex-key-in-head
)))
1674 (skip-chars-backward " \t")
1675 (setq flash
(point))
1677 (skip-chars-forward " \t")))
1678 (if (pos-visible-in-window-p (point))
1681 (buffer-substring (point) flash
)))))
1683 (defun bibtex-make-optional-field (field)
1684 "Make an optional field named FIELD in current BibTeX entry."
1686 (bibtex-make-field (cons (concat "OPT" (car field
)) (cdr field
)))
1687 (bibtex-make-field (concat "OPT" field
))))
1689 (defun bibtex-move-outside-of-entry ()
1690 "Make sure point is outside of a BibTeX entry."
1691 (let ((orig-point (point)))
1692 (bibtex-end-of-entry)
1693 (when (< (point) orig-point
)
1694 ;; We moved backward, so we weren't inside an entry to begin with.
1695 ;; Leave point at the beginning of a line, and preferably
1696 ;; at the beginning of a paragraph.
1697 (goto-char orig-point
)
1698 (beginning-of-line 1)
1699 (unless (= ?
\n (char-before (1- (point))))
1700 (re-search-forward "^[ \t]*[@\n]" nil
'move
)
1702 (skip-chars-forward " \t\n")))
1704 (defun bibtex-beginning-of-first-entry ()
1705 "Go to the beginning of the first BibTeX entry in buffer. Return point."
1706 (goto-char (point-min))
1707 (if (re-search-forward "^[ \t]*@" nil
'move
)
1708 (beginning-of-line))
1711 (defun bibtex-beginning-of-last-entry ()
1712 "Go to the beginning of the last BibTeX entry in buffer."
1713 (goto-char (point-max))
1714 (if (re-search-backward "^[ \t]*@" nil
'move
)
1715 (beginning-of-line))
1718 (defun bibtex-inside-field ()
1719 "Try to avoid point being at end of a BibTeX field."
1721 (skip-chars-backward " \t")
1722 (if (= (preceding-char) ?
,)
1724 (if (or (= (preceding-char) ?
})
1725 (= (preceding-char) ?
\"))
1728 (defun bibtex-enclosing-field (&optional noerr
)
1729 "Search for BibTeX field enclosing point.
1730 Use `match-beginning' and `match-end' to parse the field. If NOERR is non-nil,
1731 no error is signalled. In this case, bounds are returned on success,
1732 nil otherwise. Does not move point."
1733 (let ((bounds (bibtex-search-backward-field bibtex-field-name t
)))
1735 (<= (bibtex-start-of-field bounds
) (point))
1736 (>= (bibtex-end-of-field bounds
) (point)))
1739 (error "Can't find enclosing BibTeX field")))))
1741 (defun bibtex-enclosing-entry-maybe-empty-head ()
1742 "Search for BibTeX entry enclosing point. Move point to end of entry.
1743 Beginning (but not end) of entry is given by (`match-beginning' 0)."
1744 (let ((case-fold-search t
)
1745 (old-point (point)))
1746 (unless (re-search-backward bibtex-entry-maybe-empty-head nil t
)
1747 (goto-char old-point
)
1748 (error "Can't find beginning of enclosing BibTeX entry"))
1749 (goto-char (match-beginning bibtex-type-in-head
))
1750 (unless (bibtex-search-entry t nil t
)
1751 (goto-char old-point
)
1752 (error "Can't find end of enclosing BibTeX entry"))))
1754 (defun bibtex-insert-current-kill (n)
1755 (if (not bibtex-last-kill-command
)
1756 (error "BibTeX kill ring is empty")
1757 (let* ((kr (if (equal bibtex-last-kill-command
'field
)
1758 'bibtex-field-kill-ring
1759 'bibtex-entry-kill-ring
))
1760 (kryp (if (equal bibtex-last-kill-command
'field
)
1761 'bibtex-field-kill-ring-yank-pointer
1762 'bibtex-entry-kill-ring-yank-pointer
))
1763 (ARGth-kill-element (nthcdr (mod (- n
(length (eval kryp
)))
1766 (current (car (set kryp ARGth-kill-element
))))
1768 ((equal bibtex-last-kill-command
'field
)
1769 (let (bibtex-help-message)
1770 (bibtex-find-text nil t
)
1771 (if (looking-at "[}\"]")
1774 (message "Mark set")
1775 (bibtex-make-field (list (elt current
1) nil
(elt current
2)) t
))
1776 ((equal bibtex-last-kill-command
'entry
)
1777 (unless (eobp) (bibtex-beginning-of-entry))
1779 (message "Mark set")
1780 (insert (elt current
1)))
1782 (error "Unknown tag field: %s. Please submit a bug report"
1783 bibtex-last-kill-command
))))))
1785 (defun bibtex-format-entry ()
1786 "Helper function for `bibtex-clean-entry'.
1787 Formats current entry according to variable `bibtex-entry-format'."
1790 (bibtex-narrow-to-entry)
1791 (let ((case-fold-search t
)
1792 (format (if (equal bibtex-entry-format t
)
1793 '(realign opts-or-alts required-fields
1795 last-comma page-dashes delimiters
1796 unify-case inherit-booktitle
)
1797 bibtex-entry-format
))
1798 crossref-key bounds alternatives-there non-empty-alternative
1799 entry-list req-field-list field-list
)
1801 ;; identify entry type
1802 (goto-char (point-min))
1803 (re-search-forward bibtex-entry-type
)
1804 (let ((beg-type (1+ (match-beginning 0)))
1805 (end-type (match-end 0)))
1806 (setq entry-list
(assoc-string (buffer-substring-no-properties
1808 bibtex-entry-field-alist
1811 ;; unify case of entry name
1812 (when (memq 'unify-case format
)
1813 (delete-region beg-type end-type
)
1814 (insert (car entry-list
)))
1816 ;; update left entry delimiter
1817 (when (memq 'delimiters format
)
1818 (goto-char end-type
)
1819 (skip-chars-forward " \t\n")
1821 (insert (bibtex-entry-left-delimiter))))
1823 ;; determine if entry has crossref field and if at least
1824 ;; one alternative is non-empty
1825 (goto-char (point-min))
1826 (let* ((fields-alist (bibtex-parse-entry))
1827 (field (assoc-string "crossref" fields-alist t
)))
1828 (setq crossref-key
(and field
1829 (not (string-match bibtex-empty-field-re
1832 req-field-list
(if crossref-key
1833 (nth 0 (nth 2 entry-list
)) ; crossref part
1834 (nth 0 (nth 1 entry-list
)))) ; required part
1836 (dolist (rfield req-field-list
)
1837 (when (nth 3 rfield
) ; we should have an alternative
1838 (setq alternatives-there t
1839 field
(assoc-string (car rfield
) fields-alist t
))
1841 (not (string-match bibtex-empty-field-re
1843 (cond ((not non-empty-alternative
)
1844 (setq non-empty-alternative t
))
1845 ((memq 'required-fields format
)
1846 (error "More than one non-empty alternative.")))))))
1848 (if (and alternatives-there
1849 (not non-empty-alternative
)
1850 (memq 'required-fields format
))
1851 (error "All alternatives are empty"))
1853 ;; process all fields
1854 (goto-char (point-min))
1855 (while (setq bounds
(bibtex-search-forward-field bibtex-field-name
))
1856 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds
)))
1857 (end-field (copy-marker (bibtex-end-of-field bounds
)))
1858 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds
)))
1859 (end-name (copy-marker (bibtex-end-of-name-in-field bounds
)))
1860 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds
)))
1861 (end-text (copy-marker (bibtex-end-of-text-in-field bounds
)))
1862 (opt-alt (string-match "OPT\\|ALT"
1863 (buffer-substring-no-properties beg-name
(+ beg-name
3))))
1864 (field-name (buffer-substring-no-properties
1865 (if opt-alt
(+ beg-name
3) beg-name
) end-name
))
1866 (empty-field (string-match bibtex-empty-field-re
1867 (buffer-substring-no-properties beg-field end-field
)))
1870 ;; We have more elegant high-level functions for several
1871 ;; tasks done by bibtex-format-entry. However, they contain
1872 ;; quite some redundancy compared with what we need to do
1873 ;; anyway. So for speed-up we avoid using them.
1875 (if (memq 'opts-or-alts format
)
1876 (cond ((and empty-field
1878 (let ((field (assoc-string
1879 field-name req-field-list t
)))
1880 (or (not field
) ; OPT field
1881 (nth 3 field
))))) ; ALT field
1882 ;; Either it is an empty ALT field. Then we have checked
1883 ;; already that we have one non-empty alternative. Or it
1884 ;; is an empty OPT field that we do not miss anyway.
1885 ;; So we can safely delete this field.
1886 (delete-region beg-field end-field
)
1888 ;; otherwise: not empty, delete "OPT" or "ALT"
1890 (goto-char beg-name
)
1894 (push field-name field-list
)
1896 ;; remove delimiters from purely numerical fields
1897 (when (and (memq 'numerical-fields format
)
1898 (progn (goto-char beg-text
)
1899 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1900 (goto-char end-text
)
1902 (goto-char beg-text
)
1905 ;; update delimiters
1906 (when (memq 'delimiters format
)
1907 (goto-char beg-text
)
1908 (when (looking-at "[{\"]")
1910 (insert (bibtex-field-left-delimiter)))
1911 (goto-char (1- (marker-position end-text
)))
1912 (when (looking-at "[}\"]")
1914 (insert (bibtex-field-right-delimiter))))
1916 ;; update page dashes
1917 (if (and (memq 'page-dashes format
)
1918 (bibtex-string= field-name
"pages")
1919 (progn (goto-char beg-text
)
1921 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1922 (replace-match "\\1-\\2"))
1924 ;; use book title of crossref'd entry
1925 (if (and (memq 'inherit-booktitle format
)
1927 (bibtex-string= field-name
"booktitle")
1929 (let ((title (save-restriction
1931 (if (bibtex-find-entry crossref-key
)
1932 (bibtex-text-in-field "title")))))
1934 (setq empty-field nil
)
1935 (goto-char (1+ beg-text
))
1938 ;; Use booktitle to set a missing title.
1939 (if (and empty-field
1940 (bibtex-string= field-name
"title"))
1941 (let ((booktitle (bibtex-text-in-field "booktitle")))
1943 (setq empty-field nil
)
1944 (goto-char (1+ beg-text
))
1945 (insert booktitle
))))
1947 ;; if empty field, complain
1948 (if (and empty-field
1949 (memq 'required-fields format
)
1950 (assoc-string field-name req-field-list t
))
1951 (error "Mandatory field `%s' is empty" field-name
))
1953 ;; unify case of field name
1954 (if (memq 'unify-case format
)
1955 (let ((fname (car (assoc-string
1957 (append (nth 0 (nth 1 entry-list
))
1958 (nth 1 (nth 1 entry-list
))
1959 bibtex-user-optional-fields
)
1963 (delete-region beg-name end-name
)
1964 (goto-char beg-name
)
1966 ;; there are no rules we could follow
1967 (downcase-region beg-name end-name
))))
1970 (goto-char end-field
))))
1972 ;; check whether all required fields are present
1973 (if (memq 'required-fields format
)
1974 (let ((found 0) altlist
)
1975 (dolist (fname req-field-list
)
1977 (push (car fname
) altlist
))
1978 (unless (or (member (car fname
) field-list
)
1980 (error "Mandatory field `%s' is missing" (car fname
))))
1982 (dolist (fname altlist
)
1983 (if (member fname field-list
)
1984 (setq found
(1+ found
))))
1986 (error "Alternative mandatory field `%s' is missing"
1989 (error "Alternative fields `%s' are defined %s times"
1993 (if (looking-at (bibtex-field-right-delimiter))
1996 ;; update comma after last field
1997 (if (memq 'last-comma format
)
1998 (cond ((and bibtex-comma-after-last-field
1999 (not (looking-at ",")))
2001 ((and (not bibtex-comma-after-last-field
)
2005 ;; update right entry delimiter
2006 (if (looking-at ",")
2008 (when (memq 'delimiters format
)
2009 (skip-chars-forward " \t\n")
2011 (insert (bibtex-entry-right-delimiter)))
2014 (if (memq 'realign format
)
2015 (bibtex-fill-entry))))))
2018 (defun bibtex-autokey-abbrev (string len
)
2019 "Return an abbreviation of STRING with at least LEN characters.
2020 If LEN is positive the abbreviation is terminated only after a consonant
2021 or at the word end. If LEN is negative the abbreviation is strictly
2022 enforced using abs (LEN) characters. If LEN is not a number, STRING
2023 is returned unchanged."
2024 (cond ((or (not (numberp len
))
2025 (<= (length string
) (abs len
)))
2030 (substring string
0 (abs len
)))
2031 (t (let* ((case-fold-search t
)
2032 (abort-char (string-match "[^aeiou]" string
(1- len
))))
2034 (substring string
0 (1+ abort-char
))
2037 (defun bibtex-autokey-get-field (field &optional change-list
)
2038 "Get content of BibTeX field FIELD. Return empty string if not found.
2039 Optional arg CHANGE-LIST is a list of substitution patterns that is
2040 applied to the content of FIELD. It is an alist with pairs
2041 \(OLD-REGEXP . NEW-STRING\)."
2042 (let ((content (bibtex-text-in-field field bibtex-autokey-use-crossref
))
2044 (unless content
(setq content
""))
2045 (dolist (pattern change-list content
)
2046 (setq content
(replace-regexp-in-string (car pattern
)
2050 (defun bibtex-autokey-get-names ()
2051 "Get contents of the name field of the current entry.
2052 Do some modifications based on `bibtex-autokey-name-change-strings'.
2053 Return the names as a concatenated string obeying `bibtex-autokey-names'
2054 and `bibtex-autokey-names-stretch'."
2055 (let ((names (bibtex-autokey-get-field "author\\|editor"
2056 bibtex-autokey-name-change-strings
)))
2057 ;; Some entries do not have a name field.
2058 (unless (string= "" names
)
2059 (let* ((case-fold-search t
)
2060 (name-list (mapcar 'bibtex-autokey-demangle-name
2061 (split-string names
"[ \t\n]+and[ \t\n]+")))
2063 (unless (or (not (numberp bibtex-autokey-names
))
2064 (<= (length name-list
)
2065 (+ bibtex-autokey-names
2066 bibtex-autokey-names-stretch
)))
2067 ;; Take bibtex-autokey-names elements from beginning of name-list
2068 (setq name-list
(nreverse (nthcdr (- (length name-list
)
2069 bibtex-autokey-names
)
2070 (nreverse name-list
)))
2071 additional-names bibtex-autokey-additional-names
))
2072 (concat (mapconcat 'identity name-list
2073 bibtex-autokey-name-separator
)
2074 additional-names
)))))
2076 (defun bibtex-autokey-demangle-name (fullname)
2077 "Get the last part from a well-formed FULLNAME and perform abbreviations."
2078 (let* (case-fold-search
2079 (name (cond ((string-match "\\([A-Z][^, ]*\\)[^,]*," fullname
)
2080 ;; Name is of the form "von Last, First" or
2081 ;; "von Last, Jr, First"
2082 ;; --> Take the first capital part before the comma
2083 (match-string 1 fullname
))
2084 ((string-match "\\([^, ]*\\)," fullname
)
2085 ;; Strange name: we have a comma, but nothing capital
2086 ;; So we accept even lowercase names
2087 (match-string 1 fullname
))
2088 ((string-match "\\(\\<[a-z][^ ]* +\\)+\\([A-Z][^ ]*\\)"
2090 ;; name is of the form "First von Last", "von Last",
2091 ;; "First von von Last", or "d'Last"
2092 ;; --> take the first capital part after the "von" parts
2093 (match-string 2 fullname
))
2094 ((string-match "\\([^ ]+\\) *\\'" fullname
)
2095 ;; name is of the form "First Middle Last" or "Last"
2096 ;; --> take the last token
2097 (match-string 1 fullname
))
2098 (t (error "Name `%s' is incorrectly formed" fullname
)))))
2099 (bibtex-autokey-abbrev
2100 (funcall bibtex-autokey-name-case-convert name
)
2101 bibtex-autokey-name-length
)))
2103 (defun bibtex-autokey-get-year ()
2104 "Return year field contents as a string obeying `bibtex-autokey-year-length'."
2105 (let ((yearfield (bibtex-autokey-get-field "year")))
2106 (substring yearfield
(max 0 (- (length yearfield
)
2107 bibtex-autokey-year-length
)))))
2109 (defun bibtex-autokey-get-title ()
2110 "Get title field contents up to a terminator.
2111 Return the result as a string"
2112 (let ((case-fold-search t
)
2114 (bibtex-autokey-get-field "title"
2115 bibtex-autokey-titleword-change-strings
)))
2116 ;; ignore everything past a terminator
2117 (dolist (terminator bibtex-autokey-title-terminators
)
2118 (if (string-match terminator titlestring
)
2119 (setq titlestring
(substring titlestring
0 (match-beginning 0)))))
2120 ;; gather words from titlestring into a list. Ignore
2121 ;; specific words and use only a specific amount of words.
2123 titlewords titlewords-extra word
)
2124 (while (and (or (not (numberp bibtex-autokey-titlewords
))
2125 (< counter
(+ bibtex-autokey-titlewords
2126 bibtex-autokey-titlewords-stretch
)))
2127 (string-match "\\b\\w+" titlestring
))
2128 (setq word
(match-string 0 titlestring
)
2129 titlestring
(substring titlestring
(match-end 0)))
2130 ;; Ignore words matched by one of the elements of
2131 ;; bibtex-autokey-titleword-ignore
2132 (unless (let ((lst bibtex-autokey-titleword-ignore
))
2134 (not (string-match (concat "\\`\\(?:" (car lst
)
2136 (setq lst
(cdr lst
)))
2138 (setq word
(funcall bibtex-autokey-titleword-case-convert word
)
2139 counter
(1+ counter
))
2140 (if (or (not (numberp bibtex-autokey-titlewords
))
2141 (< counter bibtex-autokey-titlewords
))
2142 (push word titlewords
)
2143 (push word titlewords-extra
))))
2144 ;; Obey bibtex-autokey-titlewords-stretch:
2145 ;; If by now we have processed all words in titlestring, we include
2146 ;; titlewords-extra in titlewords. Otherwise, we ignore titlewords-extra.
2147 (unless (string-match "\\b\\w+" titlestring
)
2148 (setq titlewords
(append titlewords-extra titlewords
)))
2149 (mapconcat 'bibtex-autokey-demangle-title
(nreverse titlewords
)
2150 bibtex-autokey-titleword-separator
))))
2152 (defun bibtex-autokey-demangle-title (titleword)
2153 "Do some abbreviations on TITLEWORD.
2154 The rules are defined in `bibtex-autokey-titleword-abbrevs'
2155 and `bibtex-autokey-titleword-length'."
2156 (let ((case-fold-search t
)
2157 (alist bibtex-autokey-titleword-abbrevs
))
2159 (not (string-match (concat "\\`\\(?:" (caar alist
) "\\)\\'")
2161 (setq alist
(cdr alist
)))
2164 (bibtex-autokey-abbrev titleword
2165 bibtex-autokey-titleword-length
))))
2167 (defun bibtex-generate-autokey ()
2168 "Generate automatically a key from the author/editor and the title field.
2169 This will only work for entries where each field begins on a separate line.
2170 The generation algorithm works as follows:
2171 1. Use the value of `bibtex-autokey-prefix-string' as a prefix.
2172 2. If there is a non-empty author (preferred) or editor field,
2173 use it as the name part of the key.
2174 3. Change any substring found in
2175 `bibtex-autokey-name-change-strings' to the corresponding new
2176 one (see documentation of this variable for further detail).
2177 4. For every of at least first `bibtex-autokey-names' names in
2178 the name field, determine the last name. If there are maximal
2179 `bibtex-autokey-names' + `bibtex-autokey-names-stretch'
2180 names, all names are used.
2181 5. From every last name, take at least `bibtex-autokey-name-length'
2182 characters (abort only after a consonant or at a word end).
2183 6. Convert all last names according to the conversion function
2184 `bibtex-autokey-name-case-convert'.
2185 7. Build the name part of the key by concatenating all
2186 abbreviated last names with the string
2187 `bibtex-autokey-name-separator' between any two. If there are
2188 more names than are used in the name part, prepend the string
2189 contained in `bibtex-autokey-additional-names'.
2190 8. Build the year part of the key by truncating the contents of
2191 the year field to the rightmost `bibtex-autokey-year-length'
2192 digits (useful values are 2 and 4). If the year field (or any
2193 other field required to generate the key) is absent, but the entry
2194 has a valid crossref field and the variable
2195 `bibtex-autokey-use-crossref' is non-nil, use the field of the
2196 crossreferenced entry instead.
2197 9. For the title part of the key change the contents of the
2198 title field of the entry according to
2199 `bibtex-autokey-titleword-change-strings' to the
2200 corresponding new one (see documentation of this variable for
2202 10. Abbreviate the result to the string up to (but not including)
2203 the first occurrence of a regexp matched by the items of
2204 `bibtex-autokey-title-terminators' and delete those words which
2205 appear in `bibtex-autokey-titleword-ignore'.
2206 Build the title part of the key by using at least the first
2207 `bibtex-autokey-titlewords' words from this
2208 abbreviated title. If the abbreviated title ends after
2209 maximal `bibtex-autokey-titlewords' +
2210 `bibtex-autokey-titlewords-stretch' words, all
2211 words from the abbreviated title are used.
2212 11. Convert all used titlewords according to the conversion function
2213 `bibtex-autokey-titleword-case-convert'.
2214 12. For every used title word that appears in
2215 `bibtex-autokey-titleword-abbrevs' use the corresponding
2216 abbreviation (see documentation of this variable for further
2218 13. From every title word not generated by an abbreviation, take
2219 at least `bibtex-autokey-titleword-length' characters (abort
2220 only after a consonant or at a word end).
2221 14. Build the title part of the key by concatenating all
2222 abbreviated title words with the string
2223 `bibtex-autokey-titleword-separator' between any two.
2224 15. At least, to get the key, concatenate
2225 `bibtex-autokey-prefix-string', the name part, the year part
2226 and the title part with `bibtex-autokey-name-year-separator'
2227 between the name part and the year part if both are non-empty
2228 and `bibtex-autokey-year-title-separator' between the year
2229 part and the title part if both are non-empty. If the year
2230 part is empty, but not the other two parts,
2231 `bibtex-autokey-year-title-separator' is used as well.
2232 16. If the value of `bibtex-autokey-before-presentation-function'
2233 is non-nil, it must be a function taking one argument. This
2234 function is then called with the generated key as the
2235 argument. The return value of this function (a string) is
2237 17. If the value of `bibtex-autokey-edit-before-use' is non-nil,
2238 the key is then presented in the minibuffer to the user,
2239 where it can be edited. The key given by the user is then
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-read-key (prompt &optional key
)
2261 "Read BibTeX key from minibuffer using PROMPT and default KEY."
2262 (completing-read prompt bibtex-reference-keys
2263 nil nil key
'bibtex-key-history
))
2265 (defun bibtex-parse-keys (&optional abortable verbose
)
2266 "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2267 Find both entry keys and crossref entries. If ABORTABLE is non-nil abort on
2268 user input. If VERBOSE is non-nil gives messages about progress. Return alist
2269 of keys if parsing was completed, `aborted' otherwise."
2270 (let (ref-keys crossref-keys
)
2274 (bibtex-progress-message
2275 (concat (buffer-name) ": parsing reference keys")))
2277 (goto-char (point-min))
2278 (if bibtex-parse-keys-fast
2279 (let ((case-fold-search t
)
2280 (re (concat bibtex-entry-head
"\\|"
2281 ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*"
2282 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]")))
2283 (while (re-search-forward re nil t
)
2284 (if (and abortable
(input-pending-p))
2285 ;; user has aborted by typing a key --> return `aborted'
2286 (throw 'userkey
'aborted
))
2287 (cond ((match-end 3)
2288 ;; This is a crossref.
2289 (let ((key (buffer-substring-no-properties
2290 (1+ (match-beginning 3)) (1- (match-end 3)))))
2291 (unless (assoc key crossref-keys
)
2292 (push (list key
) crossref-keys
))))
2293 ;; only keys of known entries
2294 ((assoc-string (bibtex-type-in-head)
2295 bibtex-entry-field-alist t
)
2296 ;; This is an entry.
2297 (let ((key (bibtex-key-in-head)))
2298 (unless (assoc key ref-keys
)
2299 (push (cons key t
) ref-keys
)))))))
2301 (let (;; ignore @String entries because they are handled
2302 ;; separately by bibtex-parse-strings
2303 (bibtex-sort-ignore-string-entries t
)
2306 (lambda (key beg end
)
2309 ;; user has aborted by typing a key --> return `aborted'
2310 (throw 'userkey
'aborted
))
2311 (if verbose
(bibtex-progress-message))
2312 (unless (assoc key ref-keys
)
2313 (push (cons key t
) ref-keys
))
2314 (if (and (setq bounds
(bibtex-search-forward-field "crossref" end
))
2315 (setq key
(bibtex-text-in-field-bounds bounds t
))
2316 (not (assoc key crossref-keys
)))
2317 (push (list key
) crossref-keys
))))))
2319 (dolist (key crossref-keys
)
2320 (unless (assoc (car key
) ref-keys
) (push key ref-keys
)))
2322 (bibtex-progress-message 'done
))
2323 ;; successful operation --> return `bibtex-reference-keys'
2324 (setq bibtex-reference-keys ref-keys
))))))
2326 (defun bibtex-parse-strings (&optional add abortable
)
2327 "Set `bibtex-strings' to the string definitions in the whole buffer.
2328 The buffer might possibly be restricted.
2329 If ADD is non-nil add the new strings to `bibtex-strings' instead of
2330 simply resetting it. If ADD is an alist of strings, also add ADD to
2331 `bibtex-strings'. If ABORTABLE is non-nil abort on user input.
2332 Return alist of strings if parsing was completed, `aborted' otherwise."
2335 (goto-char (point-min))
2336 (let ((strings (if (and add
2337 (listp bibtex-strings
))
2341 (dolist (string add
)
2342 (unless (assoc-string (car string
) strings t
)
2343 (push string strings
))))
2345 (while (setq bounds
(bibtex-search-forward-string))
2348 ;; user has aborted by typing a key --> return `aborted'
2349 (throw 'userkey
'aborted
))
2350 (setq key
(bibtex-reference-key-in-string bounds
))
2351 (unless (assoc-string key strings t
)
2352 (push (cons key
(bibtex-text-in-string bounds t
))
2354 (goto-char (bibtex-end-of-text-in-string bounds
)))
2355 ;; successful operation --> return `bibtex-strings'
2356 (setq bibtex-strings strings
))))))
2358 (defun bibtex-string-files-init ()
2359 "Return initialization for `bibtex-strings'.
2360 Use `bibtex-predefined-strings' and BibTeX files `bibtex-string-files'."
2362 (let ((dirlist (split-string (or bibtex-string-file-path default-directory
)
2365 string-files fullfilename compl bounds found
)
2366 ;; collect absolute file names of valid string files
2367 (dolist (filename bibtex-string-files
)
2368 (unless (string-match "\\.bib\\'" filename
)
2369 (setq filename
(concat filename
".bib")))
2371 (if (file-name-absolute-p filename
)
2372 (if (file-readable-p filename
)
2373 (push filename string-files
)
2374 (error "BibTeX strings file %s not found" filename
))
2375 (dolist (dir dirlist
)
2376 (when (file-readable-p
2377 (setq fullfilename
(expand-file-name filename dir
)))
2378 (push fullfilename string-files
)
2381 (error "File %s not in paths defined via bibtex-string-file-path"
2383 ;; parse string files
2384 (dolist (filename string-files
)
2386 (insert-file-contents filename
)
2387 (goto-char (point-min))
2388 (while (setq bounds
(bibtex-search-forward-string))
2389 (push (cons (bibtex-reference-key-in-string bounds
)
2390 (bibtex-text-in-string bounds t
))
2392 (goto-char (bibtex-end-of-string bounds
)))))
2393 (append bibtex-predefined-strings
(nreverse compl
)))))
2395 (defun bibtex-parse-buffers-stealthily ()
2396 "Parse buffer in the background during idle time.
2397 Called by `run-with-idle-timer'. Whenever Emacs has been idle
2398 for `bibtex-parse-keys-timeout' seconds, all BibTeX buffers (starting
2399 with the current) are parsed."
2401 (let ((buffers (buffer-list))
2402 (strings-init (bibtex-string-files-init)))
2403 (while (and buffers
(not (input-pending-p)))
2404 (set-buffer (car buffers
))
2405 (if (and (eq major-mode
'bibtex-mode
)
2406 (not (eq (buffer-modified-tick)
2407 bibtex-buffer-last-parsed-tick
)))
2410 ;; Output no progress messages in bibtex-parse-keys
2411 ;; because when in y-or-n-p that can hide the question.
2412 (if (and (listp (bibtex-parse-keys t
))
2413 ;; update bibtex-strings
2414 (listp (bibtex-parse-strings strings-init t
)))
2416 ;; remember that parsing was successful
2417 (setq bibtex-buffer-last-parsed-tick
(buffer-modified-tick)))))
2418 (setq buffers
(cdr buffers
))))))
2420 (defun bibtex-files-expand (&optional current
)
2421 "Return an expanded list of BibTeX buffers based on `bibtex-files'.
2422 Initialize in these buffers `bibtex-reference-keys' if not yet set.
2423 List includes current buffer if CURRENT is non-nil."
2424 (let ((file-path (split-string (or bibtex-file-path default-directory
) ":+"))
2425 file-list dir-list buffer-list
)
2426 (dolist (file bibtex-files
)
2427 (cond ((eq file
'bibtex-file-path
)
2428 (setq dir-list
(append dir-list file-path
)))
2429 ((file-accessible-directory-p file
)
2430 (push file dir-list
))
2431 ((progn (unless (string-match "\\.bib\\'" file
)
2432 (setq file
(concat file
".bib")))
2433 (file-name-absolute-p file
))
2434 (push file file-list
))
2436 (let (fullfilename found
)
2437 (dolist (dir file-path
)
2438 (when (file-readable-p
2439 (setq fullfilename
(expand-file-name file dir
)))
2440 (push fullfilename file-list
)
2443 (error "File %s not in paths defined via bibtex-file-path"
2445 (dolist (file file-list
)
2446 (unless (file-readable-p file
)
2447 (error "BibTeX file %s not found" file
)))
2449 (dolist (dir dir-list
)
2451 (append file-list
(directory-files dir t
"\\.bib\\'" t
))))
2452 (delete-dups file-list
)
2453 (dolist (file file-list
)
2454 (when (file-readable-p file
)
2455 (push (find-file-noselect file
) buffer-list
)
2456 (with-current-buffer (car buffer-list
)
2457 (unless (listp bibtex-reference-keys
)
2458 (bibtex-parse-keys)))))
2459 (cond ((and current
(not (memq (current-buffer) buffer-list
)))
2460 (push (current-buffer) buffer-list
))
2461 ((and (not current
) (memq (current-buffer) buffer-list
))
2462 (setq buffer-list
(delq (current-buffer) buffer-list
))))
2465 (defun bibtex-complete-internal (completions)
2466 "Complete word fragment before point to longest prefix of COMPLETIONS.
2467 COMPLETIONS should be a list of strings. If point is not after the part
2468 of a word, all strings are listed. Return completion."
2469 (let* ((case-fold-search t
)
2470 (beg (save-excursion
2471 (re-search-backward "[ \t{\"]")
2475 (part-of-word (buffer-substring-no-properties beg end
))
2476 (completion (try-completion part-of-word completions
)))
2477 (cond ((not completion
)
2478 (error "Can't find completion for `%s'" part-of-word
))
2481 ((not (string= part-of-word completion
))
2482 (delete-region beg end
)
2486 (message "Making completion list...")
2487 (with-output-to-temp-buffer "*Completions*"
2488 (display-completion-list (all-completions part-of-word
2490 (message "Making completion list...done")
2491 ;; return value is handled by choose-completion-string-functions
2494 (defun bibtex-complete-string-cleanup (str strings-alist
)
2495 "Cleanup after inserting string STR.
2496 Remove enclosing field delimiters for string STR. Display message with
2497 expansion of STR using expansion list STRINGS-ALIST."
2498 (let ((pair (if (stringp str
)
2499 (assoc-string str strings-alist t
))))
2502 (message "Abbreviation for `%s'" (cdr pair
)))
2504 (bibtex-inside-field)
2505 (let ((bounds (bibtex-enclosing-field)))
2506 (goto-char (bibtex-start-of-text-in-field bounds
))
2507 (let ((boundaries (bibtex-parse-field-string)))
2509 (equal (cdr boundaries
)
2510 (bibtex-end-of-text-in-field bounds
)))
2511 (bibtex-remove-delimiters))))))))
2513 (defun bibtex-complete-key-cleanup (key)
2514 "Display summary message on entry KEY after completion of a crossref key.
2515 Use `bibtex-summary-function' to generate summary."
2517 ;; Don't do anything if we completed the key of an entry.
2518 (let ((pnt (bibtex-beginning-of-entry)))
2519 (if (and (stringp key
)
2520 (bibtex-find-entry key
)
2522 (message "Ref: %s" (funcall bibtex-summary-function key
))))))
2524 (defun bibtex-copy-summary-as-kill (key)
2525 "Push summery of BibTeX entry KEY to kill ring.
2526 Use `bibtex-summary-function' to generate summary."
2528 (list (bibtex-read-key
2529 "Key: " (save-excursion
2530 (bibtex-beginning-of-entry)
2531 (when (re-search-forward bibtex-entry-head nil t
)
2532 (bibtex-key-in-head))))))
2533 (kill-new (message "%s" (funcall bibtex-summary-function key
))))
2535 (defun bibtex-summary (key)
2536 "Return summary of BibTeX entry KEY.
2537 Used as default value of `bibtex-summary-function'."
2538 ;; It would be neat to customize this function. How?
2540 (if (bibtex-find-entry key
)
2541 (let* ((bibtex-autokey-name-case-convert 'identity
)
2542 (bibtex-autokey-name-length 'infty
)
2543 (bibtex-autokey-names 1)
2544 (bibtex-autokey-names-stretch 0)
2545 (bibtex-autokey-name-separator " ")
2546 (bibtex-autokey-additional-names " etal")
2547 (names (bibtex-autokey-get-names))
2548 (bibtex-autokey-year-length 4)
2549 (year (bibtex-autokey-get-year))
2550 (bibtex-autokey-titlewords 5)
2551 (bibtex-autokey-titlewords-stretch 2)
2552 (bibtex-autokey-titleword-case-convert 'identity
)
2553 (bibtex-autokey-titleword-length 5)
2554 (bibtex-autokey-titleword-separator " ")
2555 (title (bibtex-autokey-get-title))
2556 (journal (bibtex-autokey-get-field
2557 "journal" bibtex-autokey-transcriptions
))
2558 (volume (bibtex-autokey-get-field "volume"))
2559 (pages (bibtex-autokey-get-field "pages" '(("-.*\\'" .
"")))))
2560 (mapconcat (lambda (arg)
2561 (if (not (string= "" (cdr arg
)))
2562 (concat (car arg
) (cdr arg
))))
2563 `((" " .
,names
) (" " .
,year
) (": " .
,title
)
2564 (", " .
,journal
) (" " .
,volume
) (":" .
,pages
))
2566 (error "Key `%s' not found." key
))))
2568 (defun bibtex-pop (arg direction
)
2569 "Fill current field from the ARG'th same field's text in DIRECTION.
2570 Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
2571 (let (bibtex-help-message)
2572 (bibtex-find-text nil
))
2574 ;; parse current field
2575 (bibtex-inside-field)
2576 (let* ((case-fold-search t
)
2577 (bounds (bibtex-enclosing-field))
2578 (start-old-text (bibtex-start-of-text-in-field bounds
))
2579 (stop-old-text (bibtex-end-of-text-in-field bounds
))
2580 (field-name (bibtex-name-in-field bounds t
)))
2581 ;; if executed several times in a row, start each search where
2582 ;; the last one was finished
2583 (unless (eq last-command
'bibtex-pop
)
2584 (bibtex-enclosing-entry-maybe-empty-head)
2585 (setq bibtex-pop-previous-search-point
(match-beginning 0)
2586 bibtex-pop-next-search-point
(point)))
2587 (if (eq direction
'previous
)
2588 (goto-char bibtex-pop-previous-search-point
)
2589 (goto-char bibtex-pop-next-search-point
))
2590 ;; Now search for arg'th previous/next similar field
2591 (let (bounds failure new-text
)
2592 (while (and (not failure
)
2594 (cond ((eq direction
'previous
)
2595 (if (setq bounds
(bibtex-search-backward-field field-name
))
2596 (goto-char (bibtex-start-of-field bounds
))
2598 ((eq direction
'next
)
2599 (if (setq bounds
(bibtex-search-forward-field field-name
))
2600 (goto-char (bibtex-end-of-field bounds
))
2602 (setq arg
(- arg
1)))
2604 (error "No %s matching BibTeX field"
2605 (if (eq direction
'previous
) "previous" "next"))
2606 ;; Found a matching field. Remember boundaries.
2607 (setq bibtex-pop-previous-search-point
(bibtex-start-of-field bounds
)
2608 bibtex-pop-next-search-point
(bibtex-end-of-field bounds
)
2609 new-text
(bibtex-text-in-field-bounds bounds
))
2611 ;; Go back to where we started, delete old text, and pop new.
2612 (goto-char stop-old-text
)
2613 (delete-region start-old-text stop-old-text
)
2614 (insert new-text
)))))
2615 (let (bibtex-help-message)
2616 (bibtex-find-text nil
))
2617 (setq this-command
'bibtex-pop
))
2619 (defun bibtex-beginning-of-field ()
2620 "Move point backward to beginning of field.
2621 This function uses a simple, fast algorithm assuming that the field
2622 begins at the beginning of a line. We use this function for font-locking."
2623 (let ((field-reg (concat "^[ \t]*" bibtex-field-name
"[ \t]*=")))
2625 (unless (looking-at field-reg
)
2626 (re-search-backward field-reg nil t
))))
2628 (defun bibtex-font-lock-url (bound)
2629 "Font-lock for URLs."
2630 (let ((case-fold-search t
)
2632 field bounds start end found
)
2633 (bibtex-beginning-of-field)
2634 (while (and (not found
)
2635 (prog1 (re-search-forward bibtex-font-lock-url-regexp bound t
)
2636 (setq field
(match-string-no-properties 1)))
2637 (setq bounds
(bibtex-parse-field-text))
2639 (setq start
(car bounds
) end
(cdr bounds
))
2640 ;; Always ignore field delimiters
2641 (if (memq (char-before end
) '(?\
} ?
\"))
2642 (setq end
(1- end
)))
2643 (if (memq (char-after start
) '(?\
{ ?
\"))
2644 (setq start
(1+ start
)))
2646 (let ((lst bibtex-generate-url-list
) url
)
2648 (while (and (not found
)
2649 (setq url
(caar lst
)))
2650 (setq found
(and (bibtex-string= field
(car url
))
2651 (re-search-forward (cdr url
) end t
)
2652 (>= (match-beginning 0) pnt
))
2655 (if found
(bibtex-button (match-beginning 0) (match-end 0)
2656 'bibtex-url
(match-beginning 0)))
2659 (defun bibtex-font-lock-crossref (bound)
2660 "Font-lock for crossref fields."
2661 (let ((case-fold-search t
)
2663 (crossref-reg (concat "^[ \t]*crossref[ \t]*=[ \t\n]*"
2664 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]"))
2666 (bibtex-beginning-of-field)
2667 (while (and (not found
)
2668 (re-search-forward crossref-reg bound t
))
2669 (setq start
(1+ (match-beginning 1))
2670 end
(1- (match-end 1))
2671 found
(>= start pnt
)))
2672 (if found
(bibtex-button start end
'bibtex-find-crossref
2673 (buffer-substring-no-properties start end
)
2677 (defun bibtex-button-action (button)
2678 "Call BUTTON's BibTeX function."
2679 (apply (button-get button
'bibtex-function
)
2680 (button-get button
'bibtex-args
)))
2682 (define-button-type 'bibtex-url
2683 'action
'bibtex-button-action
2684 'bibtex-function
'bibtex-url
2685 'help-echo
(purecopy "mouse-2, RET: follow URL"))
2687 (define-button-type 'bibtex-find-crossref
2688 'action
'bibtex-button-action
2689 'bibtex-function
'bibtex-find-crossref
2690 'help-echo
(purecopy "mouse-2, RET: follow crossref"))
2692 (defun bibtex-button (beg end type
&rest args
)
2693 (make-text-button beg end
'type type
'bibtex-args args
))
2696 ;; Interactive Functions:
2699 (defun bibtex-mode ()
2700 "Major mode for editing BibTeX files.
2702 General information on working with BibTeX mode:
2704 You should use commands such as \\[bibtex-Book] to get a template for a
2705 specific entry. You should then fill in all desired fields using
2706 \\[bibtex-next-field] to jump from field to field. After having filled
2707 in all desired fields in the entry, you should clean the new entry
2708 with the command \\[bibtex-clean-entry].
2710 Some features of BibTeX mode are available only by setting the variable
2711 `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode will
2712 work only with buffers containing valid (syntactical correct) entries
2713 and with entries being sorted. This is usually the case, if you have
2714 created a buffer completely with BibTeX mode and finished every new
2715 entry with \\[bibtex-clean-entry].
2717 For third party BibTeX files, call the function `bibtex-convert-alien'
2718 to fully take advantage of all features of BibTeX mode.
2721 Special information:
2723 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
2725 The optional fields start with the string OPT, and are thus ignored by BibTeX.
2726 Alternatives from which only one is required start with the string ALT.
2727 The OPT or ALT string may be removed from a field with \\[bibtex-remove-OPT-or-ALT].
2728 \\[bibtex-make-field] inserts a new field after the current one.
2729 \\[bibtex-kill-field] kills the current field entirely.
2730 \\[bibtex-yank] yanks the last recently killed field after the current field.
2731 \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
2732 \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
2734 The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2735 from all non-empty optional or alternative fields, checks that no required
2736 fields are empty, and does some formatting dependent on the value of
2737 `bibtex-entry-format'.
2738 Note: some functions in BibTeX mode depend on entries being in a special
2739 format (all fields beginning on separate lines), so it is usually a bad
2740 idea to remove `realign' from `bibtex-entry-format'.
2742 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
2743 Use \\[bibtex-next-field] to move to end of the next field.
2745 The following may be of interest as well:
2751 `bibtex-pop-previous'
2754 `bibtex-print-help-message'
2755 `bibtex-generate-autokey'
2756 `bibtex-beginning-of-entry'
2757 `bibtex-end-of-entry'
2758 `bibtex-reposition-window'
2760 `bibtex-ispell-abstract'
2761 `bibtex-ispell-entry'
2762 `bibtex-narrow-to-entry'
2763 `bibtex-sort-buffer'
2768 `bibtex-convert-alien'
2771 `bibtex-field-delimiters'
2772 `bibtex-include-OPTcrossref'
2773 `bibtex-include-OPTkey'
2774 `bibtex-user-optional-fields'
2775 `bibtex-entry-format'
2776 `bibtex-sort-ignore-string-entries'
2777 `bibtex-maintain-sorted-entries'
2778 `bibtex-entry-field-alist'
2779 `bibtex-predefined-strings'
2780 `bibtex-string-files'
2782 ---------------------------------------------------------
2783 Entry to BibTeX mode calls the value of `bibtex-mode-hook' if that value is
2786 \\{bibtex-mode-map}"
2788 (kill-all-local-variables)
2789 (use-local-map bibtex-mode-map
)
2790 (setq major-mode
'bibtex-mode
)
2791 (setq mode-name
"BibTeX")
2792 (set-syntax-table bibtex-mode-syntax-table
)
2793 (make-local-variable 'bibtex-buffer-last-parsed-tick
)
2794 ;; Install stealthy parse function if not already installed
2795 (unless bibtex-parse-idle-timer
2796 (setq bibtex-parse-idle-timer
(run-with-idle-timer
2797 bibtex-parse-keys-timeout t
2798 'bibtex-parse-buffers-stealthily
)))
2799 (set (make-local-variable 'paragraph-start
) "[ \f\n\t]*$")
2800 (set (make-local-variable 'comment-start
) bibtex-comment-start
)
2801 (set (make-local-variable 'comment-start-skip
)
2802 (concat (regexp-quote bibtex-comment-start
) "\\>[ \t]*"))
2803 (set (make-local-variable 'comment-column
) 0)
2804 (set (make-local-variable 'defun-prompt-regexp
) "^[ \t]*@[a-zA-Z0-9]+[ \t]*")
2805 (set (make-local-variable 'outline-regexp
) "[ \t]*@")
2806 (set (make-local-variable 'fill-paragraph-function
) 'bibtex-fill-field
)
2807 (set (make-local-variable 'fill-prefix
) (make-string (+ bibtex-entry-offset
2808 bibtex-contline-indentation
)
2810 (set (make-local-variable 'font-lock-defaults
)
2811 '(bibtex-font-lock-keywords
2813 ;; Mathematical expressions should be fontified as strings
2815 ;; Quotes are field delimiters and quote-delimited
2816 ;; entries should be fontified in the same way as
2817 ;; brace-delimited ones
2820 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords
)
2821 (font-lock-extra-managed-props .
(category))
2822 (font-lock-mark-block-function
2824 (set-mark (bibtex-end-of-entry))
2825 (bibtex-beginning-of-entry)))))
2826 (setq imenu-generic-expression
2827 (list (list nil bibtex-entry-head bibtex-key-in-head
)))
2828 (make-local-variable 'choose-completion-string-functions
)
2829 (setq imenu-case-fold-search t
)
2830 ;; XEmacs needs easy-menu-add, Emacs does not care
2831 (easy-menu-add bibtex-edit-menu
)
2832 (easy-menu-add bibtex-entry-menu
)
2833 (run-hooks 'bibtex-mode-hook
))
2835 (defun bibtex-field-list (entry-type)
2836 "Return list of allowed fields for entry ENTRY-TYPE.
2837 More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),
2838 where REQUIRED and OPTIONAL are lists of the required and optional field
2839 names for ENTRY-TYPE according to `bibtex-entry-field-alist'."
2840 (let ((e (assoc-string entry-type bibtex-entry-field-alist t
))
2843 (error "Bibtex entry type %s not defined" entry-type
))
2844 (if (and (member-ignore-case entry-type bibtex-include-OPTcrossref
)
2846 (setq required
(nth 0 (nth 2 e
))
2847 optional
(nth 1 (nth 2 e
)))
2848 (setq required
(nth 0 (nth 1 e
))
2849 optional
(nth 1 (nth 1 e
))))
2850 (if bibtex-include-OPTkey
2852 "Used for reference key creation if author and editor fields are missing"
2853 (if (or (stringp bibtex-include-OPTkey
)
2854 (fboundp bibtex-include-OPTkey
))
2855 bibtex-include-OPTkey
))
2857 (if (member-ignore-case entry-type bibtex-include-OPTcrossref
)
2858 (push '("crossref" "Reference key of the cross-referenced entry")
2860 (setq optional
(append optional bibtex-user-optional-fields
))
2861 (cons required optional
)))
2863 (defun bibtex-entry (entry-type)
2864 "Insert a new BibTeX entry of type ENTRY-TYPE.
2865 After insertion it calls the functions in `bibtex-add-entry-hook'."
2866 (interactive (let* ((completion-ignore-case t
)
2867 (e-t (completing-read
2869 bibtex-entry-field-alist
2870 nil t nil
'bibtex-entry-type-history
)))
2872 (let ((key (if bibtex-maintain-sorted-entries
2873 (bibtex-read-key (format "%s key: " entry-type
))))
2874 (field-list (bibtex-field-list entry-type
)))
2875 (unless (bibtex-prepare-new-entry (list key nil entry-type
))
2876 (error "Entry with key `%s' already exists" key
))
2877 (indent-to-column bibtex-entry-offset
)
2878 (insert "@" entry-type
(bibtex-entry-left-delimiter))
2879 (if key
(insert key
))
2881 (mapc 'bibtex-make-field
(car field-list
))
2882 (mapc 'bibtex-make-optional-field
(cdr field-list
))
2883 (if bibtex-comma-after-last-field
2886 (indent-to-column bibtex-entry-offset
)
2887 (insert (bibtex-entry-right-delimiter) "\n\n"))
2888 (bibtex-next-field t
)
2889 (if (member-ignore-case entry-type bibtex-autofill-types
)
2890 (bibtex-autofill-entry))
2891 (run-hooks 'bibtex-add-entry-hook
)))
2893 (defun bibtex-entry-update ()
2894 "Update an existing BibTeX entry.
2895 In the BibTeX entry at point, make new fields for those items that may occur
2896 according to `bibtex-entry-field-alist', but are not yet present."
2899 (bibtex-beginning-of-entry)
2900 ;; For inserting new fields, we use the fact that
2901 ;; bibtex-parse-entry moves point to the end of the last field.
2902 (let* ((fields-alist (bibtex-parse-entry))
2903 (field-list (bibtex-field-list
2904 (cdr (assoc "=type=" fields-alist
)))))
2905 (dolist (field (car field-list
))
2906 (unless (assoc-string (car field
) fields-alist t
)
2907 (bibtex-make-field field
)))
2908 (dolist (field (cdr field-list
))
2909 (unless (assoc-string (car field
) fields-alist t
)
2910 (bibtex-make-optional-field field
))))))
2912 (defun bibtex-parse-entry ()
2913 "Parse entry at point, return an alist.
2914 The alist elements have the form (FIELD . TEXT), where FIELD can also be
2915 the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\"
2916 TEXT may be nil. Remove \"OPT\" and \"ALT\" from FIELD.
2917 Move point to the end of the last field."
2919 (when (looking-at bibtex-entry-maybe-empty-head
)
2920 (push (cons "=type=" (bibtex-type-in-head)) alist
)
2921 (push (cons "=key=" (bibtex-key-in-head)) alist
)
2922 (goto-char (match-end 0))
2923 (while (setq bounds
(bibtex-parse-field bibtex-field-name
))
2924 (push (cons (bibtex-name-in-field bounds t
)
2925 (bibtex-text-in-field-bounds bounds
))
2927 (goto-char (bibtex-end-of-field bounds
))))
2930 (defun bibtex-autofill-entry ()
2931 "Try to fill fields based on surrounding entries."
2933 (undo-boundary) ;So you can easily undo it, if it didn't work right.
2934 (bibtex-beginning-of-entry)
2935 (when (looking-at bibtex-entry-head
)
2936 (let ((type (bibtex-type-in-head))
2937 (key (bibtex-key-in-head))
2938 (key-end (match-end bibtex-key-in-head
))
2939 (case-fold-search t
)
2940 tmp other-key other bounds
)
2941 ;; The fields we want to change start right after the key.
2943 ;; First see whether to use the previous or the next entry
2944 ;; for "inspiration".
2946 (goto-char (1- (match-beginning 0)))
2947 (bibtex-beginning-of-entry)
2949 (looking-at bibtex-entry-head
)
2950 (bibtex-string= type
(bibtex-type-in-head))
2951 ;; In case we found ourselves :-(
2952 (not (equal key
(setq tmp
(bibtex-key-in-head)))))
2953 (setq other-key tmp
)
2954 (setq other
(point))))
2956 (bibtex-end-of-entry)
2957 (bibtex-skip-to-valid-entry)
2959 (looking-at bibtex-entry-head
)
2960 (bibtex-string= type
(bibtex-type-in-head))
2961 ;; In case we found ourselves :-(
2962 (not (equal key
(setq tmp
(bibtex-key-in-head))))
2964 ;; Check which is the best match.
2965 (< (length (try-completion "" (list key other-key
)))
2966 (length (try-completion "" (list key tmp
))))))
2967 (setq other-key tmp
)
2968 (setq other
(point))))
2969 ;; Then fill the new entry's fields with the chosen other entry.
2971 (setq other
(save-excursion (goto-char other
) (bibtex-parse-entry)))
2972 (setq key-end
(point)) ;In case parse-entry changed the buffer.
2973 (while (setq bounds
(bibtex-parse-field bibtex-field-name
))
2974 (let ((text (assoc-string (bibtex-name-in-field bounds t
)
2976 (goto-char (bibtex-start-of-text-in-field bounds
))
2977 (if (not (and (looking-at bibtex-empty-field-re
) text
))
2978 (goto-char (bibtex-end-of-field bounds
))
2979 (delete-region (point) (bibtex-end-of-text-in-field bounds
))
2980 (insert (cdr text
)))))
2981 ;; Finally try to update the text based on the difference between
2983 (let* ((prefix (try-completion "" (list key other-key
)))
2984 ;; If the keys are foo91 and foo92, don't replace 1 for 2
2985 ;; but 91 for 92 instead.
2986 (_ (if (string-match "[0-9]+\\'" prefix
)
2987 (setq prefix
(substring prefix
0 (match-beginning 0)))))
2988 (suffix (substring key
(length prefix
)))
2989 (other-suffix (substring other-key
(length prefix
))))
2990 (while (re-search-backward (regexp-quote other-suffix
) key-end
'move
)
2991 (replace-match suffix
)))))))
2993 (defun bibtex-print-help-message ()
2994 "Print helpful information about current field in current BibTeX entry."
2997 (let* ((case-fold-search t
)
2998 (field-name (bibtex-name-in-field (bibtex-enclosing-field) t
))
2999 (field-list (bibtex-field-list (progn (re-search-backward
3000 bibtex-entry-maybe-empty-head nil t
)
3001 (bibtex-type-in-head))))
3002 (comment (assoc-string field-name
3003 (append (car field-list
)
3007 (message (nth 1 comment
))
3008 (message "No comment available")))))
3010 (defun bibtex-make-field (field &optional called-by-yank interactive
)
3011 "Make a field named FIELD in current BibTeX entry.
3012 FIELD is either a string or a list of the form
3013 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
3014 `bibtex-entry-field-alist'.
3015 If CALLED-BY-YANK is non-nil, don't insert delimiters.
3016 In that case, or when called interactively, also don't do (WHAT?)."
3018 (list (let ((completion-ignore-case t
)
3019 (field-list (bibtex-field-list
3021 (bibtex-enclosing-entry-maybe-empty-head)
3022 (bibtex-type-in-head)))))
3023 (completing-read "BibTeX field name: "
3024 (append (car field-list
) (cdr field-list
))
3025 nil nil nil bibtex-field-history
))
3027 (unless (consp field
)
3028 (setq field
(list field
)))
3029 (if (or interactive called-by-yank
)
3030 (let (bibtex-help-message)
3031 (bibtex-find-text nil t t
)
3032 (if (looking-at "[}\"]")
3035 (indent-to-column (+ bibtex-entry-offset bibtex-field-indentation
))
3036 (if (nth 3 field
) (insert "ALT"))
3037 (insert (car field
) " ")
3038 (if bibtex-align-at-equal-sign
3039 (indent-to-column (+ bibtex-entry-offset
3040 (- bibtex-text-indentation
2))))
3042 (unless bibtex-align-at-equal-sign
3043 (indent-to-column (+ bibtex-entry-offset
3044 bibtex-text-indentation
)))
3045 (unless called-by-yank
(insert (bibtex-field-left-delimiter)))
3046 (let ((init (nth 2 field
)))
3047 (cond ((stringp init
)
3050 (insert (funcall init
)))))
3051 (unless called-by-yank
(insert (bibtex-field-right-delimiter)))
3054 (bibtex-print-help-message)))
3056 (defun bibtex-beginning-of-entry ()
3057 "Move to beginning of BibTeX entry (beginning of line).
3058 If inside an entry, move to the beginning of it, otherwise move to the
3059 beginning of the previous entry. If point is ahead of all BibTeX entries
3060 move point to the beginning of buffer. Return the new location of point."
3062 (skip-chars-forward " \t")
3063 (if (looking-at "@")
3065 (re-search-backward "^[ \t]*@" nil
'move
)
3068 (defun bibtex-end-of-entry ()
3069 "Move to end of BibTeX entry (past the closing brace).
3070 If inside an entry, move to the end of it, otherwise move to the end
3071 of the previous entry. Do not move if ahead of first entry.
3072 Return the new location of point."
3074 (let ((case-fold-search t
)
3076 (pnt (bibtex-beginning-of-entry))
3078 (cond ((looking-at bibtex-valid-entry-whitespace-re
)
3079 (bibtex-search-entry t nil t
)
3080 (unless (equal (match-beginning 0) pnt
)
3082 ((setq bounds
(bibtex-parse-string))
3083 (goto-char (bibtex-end-of-string bounds
)))
3084 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")
3085 (goto-char (match-end 0))
3086 (cond ((looking-at "(")
3087 (unless (re-search-forward ")[ \t]*\n\n" nil
'move
)
3090 (unless (re-search-forward "}[ \t]*\n\n" nil
'move
)
3095 (goto-char (match-beginning 0))
3099 (message "Not on a known BibTeX entry."))
3103 (error "Syntactically incorrect BibTeX entry starts here")))
3106 (defun bibtex-reposition-window (&optional arg
)
3107 "Make the current BibTeX entry visible.
3108 Optional argument ARG is exactly as in `recenter'."
3112 (/ (+ (bibtex-beginning-of-entry) (bibtex-end-of-entry)) 2))
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 it counts all entries,
3124 otherwise it counts all except Strings.
3125 If mark is active it counts 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 "Spell whole BibTeX entry."
3144 (ispell-region (save-excursion (bibtex-beginning-of-entry))
3145 (save-excursion (bibtex-end-of-entry))))
3147 (defun bibtex-ispell-abstract ()
3148 "Spell abstract of BibTeX entry."
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 the index of the BibTeX entry at point. Move point.
3168 The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting
3169 the entries of the BibTeX buffer. Return nil if no entry found."
3170 (let ((case-fold-search t
))
3171 (if (re-search-forward bibtex-entry-maybe-empty-head nil t
)
3172 (let ((key (bibtex-key-in-head))
3173 ;; all entry names should be downcase (for ease of comparison)
3174 (entry-name (downcase (bibtex-type-in-head))))
3175 ;; Don't search CROSSREF-KEY if we don't need it.
3176 (if (equal bibtex-maintain-sorted-entries
'crossref
)
3178 (bibtex-beginning-of-entry)
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 ((equal 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 ((equal 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 ; (equal 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-entry-globally (key)
3236 "Move point to the beginning of BibTeX entry named KEY in `bibtex-files'."
3238 (list (let (key-alist)
3239 (dolist (buffer (bibtex-files-expand t
))
3240 (with-current-buffer buffer
3241 (setq key-alist
(append bibtex-reference-keys key-alist
))))
3242 (completing-read "Find key: " key-alist
3243 nil nil nil
'bibtex-key-history
))))
3244 (let ((buffer-list (bibtex-files-expand t
))
3246 (while (and (not found
)
3247 (setq buffer
(pop buffer-list
)))
3248 (with-current-buffer buffer
3249 (if (cdr (assoc-string key bibtex-reference-keys
))
3253 (let ((same-window-buffer-names
3254 (cons (buffer-name buffer
) same-window-buffer-names
)))
3255 (pop-to-buffer buffer
))
3256 (bibtex-find-entry key
))
3257 (message "Key `%s' not found" key
))))
3259 (defun bibtex-find-crossref (crossref-key &optional pnt split
)
3260 "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3261 Return position of entry if CROSSREF-KEY is found and nil otherwise.
3262 If position of current entry is after CROSSREF-KEY an error is signaled.
3263 Optional arg PNT is the position of the referencing entry.
3264 If optional arg SPLIT is non-nil, split window so that both the referencing
3265 and the crossrefed entry are displayed.
3266 If called interactively, CROSSREF-KEY defaults to crossref key of current
3267 entry and SPLIT is t."
3271 (bibtex-beginning-of-entry)
3272 (let ((bounds (bibtex-search-forward-field "crossref" t
)))
3274 (bibtex-text-in-field-bounds bounds t
))))))
3275 (list (bibtex-read-key "Find crossref key: " crossref-key
) (point) t
)))
3276 (let ((pos (save-excursion (bibtex-find-entry crossref-key
))))
3277 (unless pnt
(setq pnt
(point)))
3279 (message "Crossref key `%s' not found" crossref-key
))
3282 (select-window (split-window))
3285 (set-window-start (selected-window) (point))
3287 (error "The referencing entry must preceed the crossrefed entry!")))
3289 (error "The referencing entry must preceed the crossrefed entry!"))
3290 (t (goto-char pos
)))
3293 (defun bibtex-find-entry (key &optional start
)
3294 "Move point to the beginning of BibTeX entry named KEY.
3295 Return position of entry if KEY is found or nil if not found.
3296 Optional arg START is buffer position where the search starts.
3297 If it is nil, start search at beginning of buffer.
3298 With prefix arg, the value of START is position of point."
3299 (interactive (list (bibtex-read-key "Find key: ")
3300 (if current-prefix-arg
(point))))
3301 (let* (case-fold-search
3302 (pnt (save-excursion
3303 (goto-char (or start
(point-min)))
3304 (if (re-search-forward (concat "^[ \t]*\\("
3306 "\\)[ \t]*[({][ \t\n]*\\("
3310 (match-beginning 0)))))
3314 (message "Key `%s' not found" key
)))))
3316 (defun bibtex-prepare-new-entry (index)
3317 "Prepare a new BibTeX entry with index INDEX.
3318 INDEX is a list (KEY CROSSREF-KEY ENTRY-NAME).
3319 Move point where the entry KEY should be placed.
3320 If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
3321 search to look for place for KEY. This will fail if buffer is not in
3322 sorted order, see \\[bibtex-validate].)
3323 Return t if preparation was successful or nil if entry KEY already exists."
3324 (let ((key (nth 0 index
))
3326 (cond ((or (null key
)
3328 (string-equal key
""))
3329 (and (not (setq key-exist
(bibtex-find-entry key
)))
3330 (not bibtex-maintain-sorted-entries
)))
3331 (bibtex-move-outside-of-entry))
3332 ;; if key-exist is non-nil due to the previous cond clause
3333 ;; then point will be at beginning of entry named key.
3335 (t ; bibtex-maintain-sorted-entries is non-nil
3336 (let* ((case-fold-search t
)
3337 (left (save-excursion (bibtex-beginning-of-first-entry)
3338 (bibtex-skip-to-valid-entry)
3340 (right (save-excursion (bibtex-beginning-of-last-entry)
3341 (bibtex-end-of-entry)))
3342 (found (if (>= left right
) left
))
3347 (goto-char (/ (+ left right
) 2))
3348 (bibtex-skip-to-valid-entry t
)
3349 (setq actual-index
(bibtex-entry-index))
3350 (cond ((bibtex-lessp index actual-index
)
3351 (setq new
(bibtex-beginning-of-entry))
3352 (if (equal right new
)
3356 (bibtex-end-of-entry)
3357 (bibtex-skip-to-valid-entry)
3359 (if (equal left new
)
3361 (setq left new
))))))
3363 (bibtex-beginning-of-entry)
3364 (setq actual-index
(save-excursion (bibtex-entry-index)))
3365 (when (or (not actual-index
)
3366 (bibtex-lessp actual-index index
))
3367 ;; buffer contains no valid entries or
3368 ;; greater than last entry --> append
3369 (bibtex-end-of-entry)
3370 (unless (bobp) (newline (forward-line 2)))
3371 (beginning-of-line)))))
3372 (unless key-exist t
)))
3374 (defun bibtex-validate (&optional test-thoroughly
)
3375 "Validate if buffer or region is syntactically correct.
3376 Check also for duplicate keys and correct sort order provided
3377 `bibtex-maintain-sorted-entries' is non-nil.
3378 With optional argument TEST-THOROUGHLY non-nil check also for
3379 the absence of required fields and for questionable month fields.
3380 If mark is active, validate current region, if not the whole buffer.
3381 Only check known entry types, so you can put comments outside of entries.
3382 Return t if test was successful, nil otherwise."
3384 (let* ((case-fold-search t
)
3385 error-list syntax-error
)
3388 (narrow-to-region (if mark-active
(region-beginning)
3389 (bibtex-beginning-of-first-entry))
3390 (if mark-active
(region-end) (point-max)))
3392 ;; looking if entries fit syntactical structure
3393 (goto-char (point-min))
3394 (bibtex-progress-message "Checking syntactical structure")
3395 (let (bibtex-sort-ignore-string-entries)
3396 (while (re-search-forward "^[ \t]*@" nil t
)
3397 (bibtex-progress-message)
3399 (let ((pnt (point)))
3400 (if (not (looking-at bibtex-any-valid-entry-re
))
3402 (bibtex-skip-to-valid-entry)
3403 (if (equal (point) pnt
)
3406 (push (cons (bibtex-current-line)
3407 "Syntax error (check esp. commas, braces, and quotes)")
3410 (bibtex-progress-message 'done
)
3413 ;; proceed only if there were no syntax errors.
3414 (setq syntax-error t
)
3416 ;; looking for duplicate keys and correct sort order
3417 (let (previous current key-list
)
3418 (bibtex-progress-message "Checking for duplicate keys")
3420 (lambda (key beg end
)
3421 (bibtex-progress-message)
3423 (setq current
(bibtex-entry-index))
3424 (cond ((not previous
))
3425 ((member key key-list
)
3426 (push (cons (bibtex-current-line)
3427 (format "Duplicate key `%s'" key
))
3429 ((and bibtex-maintain-sorted-entries
3430 (not (bibtex-lessp previous current
)))
3431 (push (cons (bibtex-current-line)
3432 "Entries out of order")
3435 (setq previous current
)))
3436 (bibtex-progress-message 'done
))
3438 ;; Check for duplicate keys in `bibtex-files'.
3440 (dolist (buffer (bibtex-files-expand))
3441 (dolist (key (with-current-buffer buffer
3442 ;; We don't want to be fooled by outdated
3443 ;; bibtex-reference-keys.
3444 (bibtex-parse-keys) bibtex-reference-keys
))
3445 (when (and (cdr key
)
3446 (cdr (assoc-string (car key
) bibtex-reference-keys
)))
3447 (bibtex-find-entry (car key
))
3448 (push (cons (bibtex-current-line)
3449 (format "Duplicate key `%s' in %s" (car key
)
3450 (abbreviate-file-name (buffer-file-name buffer
))))
3453 (when test-thoroughly
3454 (bibtex-progress-message
3455 "Checking required fields and month fields")
3456 (let ((bibtex-sort-ignore-string-entries t
))
3458 (lambda (key beg end
)
3459 (bibtex-progress-message)
3460 (let* ((entry-list (progn
3462 (bibtex-search-entry nil end
)
3463 (assoc-string (bibtex-type-in-head)
3464 bibtex-entry-field-alist t
)))
3465 (req (copy-sequence (elt (elt entry-list
1) 0)))
3466 (creq (copy-sequence (elt (elt entry-list
2) 0)))
3467 crossref-there bounds alt-there field
)
3469 (while (setq bounds
(bibtex-search-forward-field
3470 bibtex-field-name end
))
3471 (goto-char (bibtex-start-of-text-in-field bounds
))
3472 (let ((field-name (bibtex-name-in-field bounds
)))
3473 (if (and (bibtex-string= field-name
"month")
3474 ;; Check only abbreviated month fields.
3475 (let ((month (bibtex-text-in-field-bounds bounds
)))
3476 (not (or (string-match "\\`[\"{].+[\"}]\\'" month
)
3479 bibtex-predefined-month-strings t
)))))
3480 (push (cons (bibtex-current-line)
3481 "Questionable month field")
3483 (setq field
(assoc-string field-name req t
))
3485 (if alt-there
(push (cons (bibtex-current-line)
3486 "More than one non-empty alternative")
3488 (setq alt-there t
)))
3489 (setq req
(delete field req
)
3490 creq
(delete (assoc-string field-name creq t
) creq
))
3491 (if (bibtex-string= field-name
"crossref")
3492 (setq crossref-there t
))))
3498 (push (car field
) alt
)
3499 (push (cons (save-excursion (goto-char beg
)
3500 (bibtex-current-line))
3501 (format "Required field `%s' missing"
3504 ;; The following fails if there are more than two
3505 ;; alternatives in a BibTeX entry, which isn't
3506 ;; the case momentarily.
3508 (push (cons (save-excursion (goto-char beg
)
3509 (bibtex-current-line))
3510 (format "Alternative fields `%s'/`%s' missing"
3511 (car alt
) (cadr alt
)))
3513 (bibtex-progress-message 'done
)))))
3516 (let ((file (file-name-nondirectory (buffer-file-name)))
3517 (dir default-directory
)
3518 (err-buf "*BibTeX validation errors*"))
3519 (setq error-list
(sort error-list
'car-less-than-car
))
3520 (with-current-buffer (get-buffer-create err-buf
)
3521 (setq default-directory dir
)
3522 (unless (eq major-mode
'compilation-mode
) (compilation-mode))
3523 (toggle-read-only -
1)
3524 (delete-region (point-min) (point-max))
3525 (insert "BibTeX mode command `bibtex-validate'\n"
3527 "Maybe undetected errors due to syntax errors. Correct and validate again.\n"
3529 (dolist (err error-list
)
3530 (insert (format "%s:%d: %s\n" file
(car err
) (cdr err
))))
3531 (set-buffer-modified-p nil
)
3532 (toggle-read-only 1)
3533 (goto-line 3)) ; first error message
3534 (display-buffer err-buf
)
3537 (message "%s is syntactically correct"
3538 (if mark-active
"Region" "Buffer"))
3541 (defun bibtex-validate-globally (&optional strings
)
3542 "Check for duplicate keys in `bibtex-files'.
3543 With prefix arg STRINGS, check for duplicate strings, too.
3544 Return t if test was successful, nil otherwise."
3546 (let ((buffer-list (bibtex-files-expand t
))
3547 buffer-key-list current-buf current-keys error-list
)
3548 ;; Check for duplicate keys within BibTeX buffer
3549 (dolist (buffer buffer-list
)
3552 (let (entry-type key key-list
)
3553 (goto-char (point-min))
3554 (while (re-search-forward bibtex-entry-head nil t
)
3555 (setq entry-type
(bibtex-type-in-head)
3556 key
(bibtex-key-in-head))
3557 (if (or (and strings
(bibtex-string= entry-type
"string"))
3558 (assoc-string entry-type bibtex-entry-field-alist t
))
3559 (if (member key key-list
)
3560 (push (format "%s:%d: Duplicate key `%s'\n"
3562 (bibtex-current-line) key
)
3564 (push key key-list
))))
3565 (push (cons buffer key-list
) buffer-key-list
))))
3567 ;; Check for duplicate keys among BibTeX buffers
3568 (while (setq current-buf
(pop buffer-list
))
3569 (setq current-keys
(cdr (assq current-buf buffer-key-list
)))
3570 (with-current-buffer current-buf
3571 (dolist (buffer buffer-list
)
3572 (dolist (key (cdr (assq buffer buffer-key-list
)))
3573 (when (assoc-string key current-keys
)
3574 (bibtex-find-entry key
)
3575 (push (format "%s:%d: Duplicat key `%s' in %s\n"
3576 (buffer-file-name) (bibtex-current-line) key
3577 (abbreviate-file-name (buffer-file-name buffer
)))
3580 ;; Process error list
3582 (let ((err-buf "*BibTeX validation errors*"))
3583 (with-current-buffer (get-buffer-create err-buf
)
3584 (unless (eq major-mode
'compilation-mode
) (compilation-mode))
3585 (toggle-read-only -
1)
3586 (delete-region (point-min) (point-max))
3587 (insert "BibTeX mode command `bibtex-validate-globally'\n\n")
3588 (dolist (err (sort error-list
'string-lessp
)) (insert err
))
3589 (set-buffer-modified-p nil
)
3590 (toggle-read-only 1)
3591 (goto-line 3)) ; first error message
3592 (display-buffer err-buf
)
3595 (message "No duplicate keys.")
3598 (defun bibtex-next-field (arg)
3599 "Find end of text of next BibTeX field; with ARG, to its beginning."
3601 (bibtex-inside-field)
3602 (let ((start (point)))
3604 (let ((bounds (bibtex-enclosing-field)))
3605 (goto-char (bibtex-end-of-field bounds
))
3611 (bibtex-find-text arg t
))
3613 (defun bibtex-find-text (arg &optional as-if-interactive no-error
)
3614 "Go to end of text of current field; with ARG, go to beginning."
3615 (interactive "P\np")
3616 (bibtex-inside-field)
3617 (let ((bounds (bibtex-enclosing-field as-if-interactive
)))
3620 (progn (goto-char (bibtex-start-of-text-in-field bounds
))
3621 (if (looking-at "[{\"]")
3623 (goto-char (bibtex-end-of-text-in-field bounds
))
3624 (if (or (= (preceding-char) ?
})
3625 (= (preceding-char) ?
\"))
3627 (if bibtex-help-message
3628 (bibtex-print-help-message)))
3630 (cond ((setq bounds
(bibtex-parse-string))
3632 (bibtex-start-of-text-in-string bounds
)
3633 (bibtex-end-of-text-in-string bounds
))))
3634 ((looking-at bibtex-entry-maybe-empty-head
)
3636 (match-beginning bibtex-key-in-head
)
3640 (error "Not on BibTeX field")))))))
3642 (defun bibtex-remove-OPT-or-ALT ()
3643 "Remove the string starting optional/alternative fields.
3644 Align text and go thereafter to end of text."
3646 (bibtex-inside-field)
3647 (let ((case-fold-search t
)
3648 (bounds (bibtex-enclosing-field)))
3650 (goto-char (bibtex-start-of-name-in-field bounds
))
3651 (when (looking-at "OPT\\|ALT")
3652 (delete-region (match-beginning 0) (match-end 0))
3653 ;; make field non-OPT
3654 (search-forward "=")
3656 (delete-horizontal-space)
3657 (if bibtex-align-at-equal-sign
3658 (indent-to-column (- bibtex-text-indentation
2))
3660 (search-forward "=")
3661 (delete-horizontal-space)
3662 (if bibtex-align-at-equal-sign
3664 (indent-to-column bibtex-text-indentation
))))
3665 (bibtex-inside-field)))
3667 (defun bibtex-remove-delimiters ()
3668 "Remove \"\" or {} around string."
3671 (bibtex-inside-field)
3672 (let* ((bounds (bibtex-enclosing-field))
3673 (end (bibtex-end-of-text-in-field bounds
))
3674 (start (bibtex-start-of-text-in-field bounds
)))
3675 (if (memq (char-before end
) '(?\
} ?
\"))
3676 (delete-region (1- end
) end
))
3677 (if (memq (char-after start
) '(?\
{ ?
\"))
3678 (delete-region start
(1+ start
))))))
3680 (defun bibtex-kill-field (&optional copy-only
)
3681 "Kill the entire enclosing BibTeX field.
3682 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
3683 but do not actually kill it."
3686 (bibtex-inside-field)
3687 (let* ((case-fold-search t
)
3688 (bounds (bibtex-enclosing-field))
3689 (end (bibtex-end-of-field bounds
))
3690 (beg (bibtex-start-of-field bounds
)))
3692 (skip-chars-forward " \t\n,")
3693 (push (list 'field
(bibtex-name-in-field bounds
)
3694 (bibtex-text-in-field-bounds bounds
))
3695 bibtex-field-kill-ring
)
3696 (if (> (length bibtex-field-kill-ring
) bibtex-field-kill-ring-max
)
3697 (setcdr (nthcdr (1- bibtex-field-kill-ring-max
)
3698 bibtex-field-kill-ring
)
3700 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring
)
3702 (delete-region beg end
))))
3703 (setq bibtex-last-kill-command
'field
))
3705 (defun bibtex-copy-field-as-kill ()
3706 "Copy the field at point to the kill ring."
3708 (bibtex-kill-field t
))
3710 (defun bibtex-kill-entry (&optional copy-only
)
3711 "Kill the entire enclosing BibTeX entry.
3712 With prefix arg COPY-ONLY the current entry to
3713 `bibtex-entry-kill-ring', but do not actually kill it."
3716 (let* ((case-fold-search t
)
3717 (beg (bibtex-beginning-of-entry))
3718 (end (progn (bibtex-end-of-entry)
3719 (if (re-search-forward
3720 bibtex-entry-maybe-empty-head nil
'move
)
3721 (goto-char (match-beginning 0)))
3723 (push (list 'entry
(buffer-substring-no-properties beg end
))
3724 bibtex-entry-kill-ring
)
3725 (if (> (length bibtex-entry-kill-ring
) bibtex-entry-kill-ring-max
)
3726 (setcdr (nthcdr (1- bibtex-entry-kill-ring-max
)
3727 bibtex-entry-kill-ring
)
3729 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring
)
3731 (delete-region beg end
))))
3732 (setq bibtex-last-kill-command
'entry
))
3734 (defun bibtex-copy-entry-as-kill ()
3735 "Copy the entire enclosing BibTeX entry to `bibtex-entry-kill-ring'."
3737 (bibtex-kill-entry t
))
3739 (defun bibtex-yank (&optional n
)
3740 "Reinsert the last BibTeX item.
3741 More precisely, reinsert the field or entry killed or yanked most recently.
3742 With argument N, reinsert the Nth most recently killed BibTeX item.
3743 See also the command \\[bibtex-yank-pop]]."
3745 (bibtex-insert-current-kill (1- n
))
3746 (setq this-command
'bibtex-yank
))
3748 (defun bibtex-yank-pop (n)
3749 "Replace just-yanked killed BibTeX item with a different.
3750 This command is allowed only immediately after a `bibtex-yank' or a
3752 At such a time, the region contains a reinserted previously killed
3753 BibTeX item. `bibtex-yank-pop' deletes that item and inserts in its
3754 place a different killed BibTeX item.
3756 With no argument, the previous kill is inserted.
3757 With argument N, insert the Nth previous kill.
3758 If N is negative, this is a more recent kill.
3760 The sequence of kills wraps around, so that after the oldest one
3761 comes the newest one."
3763 (unless (eq last-command
'bibtex-yank
)
3764 (error "Previous command was not a BibTeX yank"))
3765 (setq this-command
'bibtex-yank
)
3766 (let ((inhibit-read-only t
))
3767 (delete-region (point) (mark t
))
3768 (bibtex-insert-current-kill n
)))
3770 (defun bibtex-empty-field ()
3771 "Delete the text part of the current field, replace with empty text."
3773 (bibtex-inside-field)
3774 (let ((bounds (bibtex-enclosing-field)))
3775 (goto-char (bibtex-start-of-text-in-field bounds
))
3776 (delete-region (point) (bibtex-end-of-text-in-field bounds
))
3777 (insert (concat (bibtex-field-left-delimiter)
3778 (bibtex-field-right-delimiter)) )
3779 (bibtex-find-text t
)))
3781 (defun bibtex-pop-previous (arg)
3782 "Replace text of current field with the similar field in previous entry.
3783 With arg, goes up ARG entries. Repeated, goes up so many times. May be
3784 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
3786 (bibtex-pop arg
'previous
))
3788 (defun bibtex-pop-next (arg)
3789 "Replace text of current field with the text of similar field in next entry.
3790 With arg, goes down ARG entries. Repeated, goes down so many times. May be
3791 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3793 (bibtex-pop arg
'next
))
3795 (defun bibtex-clean-entry (&optional new-key called-by-reformat
)
3796 "Finish editing the current BibTeX entry and clean it up.
3797 Check that no required fields are empty and formats entry dependent
3798 on the value of `bibtex-entry-format'.
3799 If the reference key of the entry is empty or a prefix argument is given,
3800 calculate a new reference key. (Note: this will only work if fields in entry
3801 begin on separate lines prior to calling `bibtex-clean-entry' or if
3802 'realign is contained in `bibtex-entry-format'.)
3803 Don't call `bibtex-clean-entry' on @Preamble entries.
3804 At end of the cleaning process, the functions in
3805 `bibtex-clean-entry-hook' are called with region narrowed to entry."
3806 ;; Opt. arg called-by-reformat is t if bibtex-clean-entry
3807 ;; is called by bibtex-reformat
3809 (let ((case-fold-search t
)
3811 (bibtex-beginning-of-entry)
3813 (when (re-search-forward bibtex-entry-maybe-empty-head nil t
)
3814 (setq entry-type
(bibtex-type-in-head))
3815 (setq key
(bibtex-key-in-head))))
3817 (cond ((bibtex-string= entry-type
"preamble")
3818 ;; (bibtex-format-preamble)
3819 (error "No clean up of @Preamble entries"))
3820 ((bibtex-string= entry-type
"string"))
3821 ;; (bibtex-format-string)
3822 (t (bibtex-format-entry)))
3824 (when (or new-key
(not key
))
3825 (setq key
(bibtex-generate-autokey))
3826 ;; Sometimes bibtex-generate-autokey returns an empty string
3827 (if (or bibtex-autokey-edit-before-use
(string= "" key
))
3828 (setq key
(bibtex-read-key "Key to use: " key
)))
3829 (re-search-forward bibtex-entry-maybe-empty-head
)
3830 (if (match-beginning bibtex-key-in-head
)
3831 (delete-region (match-beginning bibtex-key-in-head
)
3832 (match-end bibtex-key-in-head
)))
3835 (unless called-by-reformat
3836 (let* ((start (bibtex-beginning-of-entry))
3837 (end (progn (bibtex-end-of-entry)
3838 (if (re-search-forward
3839 bibtex-entry-maybe-empty-head nil
'move
)
3840 (goto-char (match-beginning 0)))
3842 (entry (buffer-substring start end
))
3843 ;; include the crossref key in index
3844 (index (let ((bibtex-maintain-sorted-entries 'crossref
))
3846 (bibtex-entry-index)))
3849 (if (and bibtex-maintain-sorted-entries
3850 (not (and bibtex-sort-ignore-string-entries
3851 (bibtex-string= entry-type
"string"))))
3853 (delete-region start end
)
3854 (setq error
(not (bibtex-prepare-new-entry index
)))
3857 (bibtex-beginning-of-entry) ; moves backward
3858 (re-search-forward bibtex-entry-head
))
3859 (bibtex-find-entry key
)
3860 (setq error
(or (/= (point) start
)
3861 (bibtex-find-entry key end
))))
3863 (error "New inserted entry yields duplicate key"))
3864 (dolist (buffer (bibtex-files-expand))
3865 (with-current-buffer buffer
3866 (if (cdr (assoc-string key bibtex-reference-keys
))
3867 (error "Duplicate key in %s" (buffer-file-name)))))
3869 ;; Only update the list of keys if it has been built already.
3870 (cond ((bibtex-string= entry-type
"string")
3871 (if (and (listp bibtex-strings
)
3872 (not (assoc key bibtex-strings
)))
3873 (push (list key
) bibtex-strings
)))
3874 ;; We have a normal entry.
3875 ((listp bibtex-reference-keys
)
3876 (cond ((not (assoc key bibtex-reference-keys
))
3877 (push (cons key t
) bibtex-reference-keys
))
3878 ((not (cdr (assoc key bibtex-reference-keys
)))
3879 ;; Turn a crossref key into a header key
3880 (setq bibtex-reference-keys
3882 (delete (list key
) bibtex-reference-keys
)))))
3883 ;; Handle crossref key.
3884 (if (and (nth 1 index
)
3885 (not (assoc (nth 1 index
) bibtex-reference-keys
)))
3886 (push (list (nth 1 index
)) bibtex-reference-keys
)))))
3889 (if bibtex-clean-entry-hook
3892 (bibtex-narrow-to-entry)
3893 (run-hooks 'bibtex-clean-entry-hook
)))))))
3895 (defun bibtex-fill-field-bounds (bounds justify
&optional move
)
3896 "Fill BibTeX field delimited by BOUNDS.
3897 If JUSTIFY is non-nil justify as well.
3898 If optional arg MOVE is non-nil move point to end of field."
3899 (let ((end-field (copy-marker (bibtex-end-of-field bounds
))))
3900 (goto-char (bibtex-start-of-field bounds
))
3904 (bibtex-delete-whitespace)
3907 (indent-to-column (+ bibtex-entry-offset
3908 bibtex-field-indentation
))
3909 (re-search-forward "[ \t\n]*=" end-field
)
3912 (if bibtex-align-at-equal-sign
3914 (+ bibtex-entry-offset
(- bibtex-text-indentation
2)))
3917 (bibtex-delete-whitespace)
3918 (if bibtex-align-at-equal-sign
3920 (indent-to-column bibtex-text-indentation
)))
3921 (re-search-forward "[ \t\n]*=[ \t\n]*" end-field
))
3922 (while (re-search-forward "[ \t\n]+" end-field
'move
)
3923 (replace-match " "))
3925 (if move
(goto-char end-field
))))
3927 (defun bibtex-fill-field (&optional justify
)
3928 "Like \\[fill-paragraph], but fill current BibTeX field.
3929 Optional prefix arg JUSTIFY non-nil means justify as well.
3930 In BibTeX mode this function is bound to `fill-paragraph-function'."
3932 (let ((pnt (copy-marker (point)))
3933 (bounds (bibtex-enclosing-field)))
3935 (bibtex-fill-field-bounds bounds justify
)
3938 (defun bibtex-fill-entry ()
3939 "Fill current BibTeX entry.
3940 Realign entry, so that every field starts on a separate line. Field
3941 names appear in column `bibtex-field-indentation', field text starts in
3942 column `bibtex-text-indentation' and continuation lines start here, too.
3943 If `bibtex-align-at-equal-sign' is non-nil, align equal signs, too."
3945 (let ((pnt (copy-marker (point)))
3946 (end (copy-marker (bibtex-end-of-entry)))
3948 (bibtex-beginning-of-entry)
3949 (bibtex-delete-whitespace)
3950 (indent-to-column bibtex-entry-offset
)
3951 (while (setq bounds
(bibtex-search-forward-field bibtex-field-name end
))
3952 (bibtex-fill-field-bounds bounds t t
))
3953 (if (looking-at ",")
3955 (bibtex-delete-whitespace)
3958 (indent-to-column bibtex-entry-offset
)
3961 (defun bibtex-realign ()
3962 "Realign BibTeX entries such that they are separated by one blank line."
3963 (goto-char (point-min))
3964 (let ((case-fold-search t
))
3965 ;; No blank lines prior to the first valid entry if there no
3966 ;; non-white characters in front of it.
3967 (when (looking-at bibtex-valid-entry-whitespace-re
)
3968 (replace-match "\\1"))
3969 ;; Valid entries are separated by one blank line.
3970 (while (re-search-forward bibtex-valid-entry-whitespace-re nil t
)
3971 (replace-match "\n\n\\1"))
3972 ;; One blank line past the last valid entry if it is followed by
3973 ;; non-white characters, no blank line otherwise.
3975 (when (re-search-forward bibtex-valid-entry-re nil t
)
3976 (bibtex-end-of-entry)
3977 (bibtex-delete-whitespace)
3978 (open-line (if (eobp) 1 2)))))
3980 (defun bibtex-reformat (&optional read-options
)
3981 "Reformat all BibTeX entries in buffer or region.
3982 Without prefix argument, reformatting is based on `bibtex-entry-format'.
3983 With prefix argument, read options for reformatting from minibuffer.
3984 With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again.
3985 If mark is active reformat entries in region, if not in whole buffer."
3987 (let* ((pnt (point))
3988 (use-previous-options
3989 (and (equal (prefix-numeric-value read-options
) 16)
3990 (or bibtex-reformat-previous-options
3991 bibtex-reformat-previous-reference-keys
)))
3992 (bibtex-entry-format
3994 (if use-previous-options
3995 bibtex-reformat-previous-options
3996 (setq bibtex-reformat-previous-options
3997 (mapcar (lambda (option)
3998 (if (y-or-n-p (car option
)) (cdr option
)))
3999 `(("Realign entries (recommended)? " .
'realign
)
4000 ("Remove empty optional and alternative fields? " .
'opts-or-alts
)
4001 ("Remove delimiters around pure numerical fields? " .
'numerical-fields
)
4002 (,(concat (if bibtex-comma-after-last-field
"Insert" "Remove")
4003 " comma at end of entry? ") .
'last-comma
)
4004 ("Replace double page dashes by single ones? " .
'page-dashes
)
4005 ("Inherit booktitle? " .
'inherit-booktitle
)
4006 ("Force delimiters? " .
'delimiters
)
4007 ("Unify case of entry types and field names? " .
'unify-case
))))))
4008 ;; Do not include required-fields because `bibtex-reformat'
4009 ;; cannot handle the error messages of `bibtex-format-entry'.
4010 ;; Use `bibtex-validate' to check for required fields.
4011 ((eq t bibtex-entry-format
)
4012 '(realign opts-or-alts numerical-fields delimiters
4013 last-comma page-dashes unify-case inherit-booktitle
))
4015 (remove 'required-fields
(push 'realign bibtex-entry-format
)))))
4016 (reformat-reference-keys
4018 (if use-previous-options
4019 bibtex-reformat-previous-reference-keys
4020 (setq bibtex-reformat-previous-reference-keys
4021 (y-or-n-p "Generate new reference keys automatically? ")))))
4022 (bibtex-sort-ignore-string-entries t
)
4023 bibtex-autokey-edit-before-use
)
4026 (narrow-to-region (if mark-active
(region-beginning) (point-min))
4027 (if mark-active
(region-end) (point-max)))
4028 (if (memq 'realign bibtex-entry-format
)
4030 (bibtex-progress-message "Formatting" 1)
4031 (bibtex-map-entries (lambda (key beg end
)
4032 (bibtex-progress-message)
4033 (bibtex-clean-entry reformat-reference-keys t
)))
4034 (bibtex-progress-message 'done
))
4035 (when reformat-reference-keys
4036 (kill-local-variable 'bibtex-reference-keys
)
4037 (when bibtex-maintain-sorted-entries
4038 (bibtex-progress-message "Sorting" 1)
4039 (bibtex-sort-buffer)
4040 (bibtex-progress-message 'done
)))
4043 (defun bibtex-convert-alien (&optional read-options
)
4044 "Convert an alien BibTeX buffer to be fully usable by BibTeX mode.
4045 If a file does not conform with all standards used by BibTeX mode,
4046 some of the high-level features of BibTeX mode will not be available.
4047 This function tries to convert current buffer to conform with these standards.
4048 With prefix argument READ-OPTIONS non-nil, read options for reformatting
4049 entries from minibuffer."
4051 (message "Starting to validate buffer...")
4055 "If errors occur, correct them and call `bibtex-convert-alien' again")
4057 (deactivate-mark) ; So bibtex-validate works on the whole buffer.
4058 (when (let (bibtex-maintain-sorted-entries)
4060 (message "Starting to reformat entries...")
4062 (bibtex-reformat read-options
)
4063 (goto-char (point-max))
4064 (message "Buffer is now parsable. Please save it.")))
4066 (defun bibtex-complete ()
4067 "Complete word fragment before point according to context.
4068 If point is inside key or crossref field perform key completion based on
4069 `bibtex-reference-keys'. Inside a month field perform key completion
4070 based on `bibtex-predefined-month-strings'. Inside any other field
4071 perform string completion based on `bibtex-strings'. An error is
4072 signaled if point is outside key or BibTeX field."
4075 (case-fold-search t
)
4078 (if (and (setq bounds
(bibtex-enclosing-field t
))
4079 (>= pnt
(bibtex-start-of-text-in-field bounds
))
4080 (<= pnt
(bibtex-end-of-text-in-field bounds
)))
4081 (setq name
(bibtex-name-in-field bounds t
)
4082 compl
(cond ((bibtex-string= name
"crossref")
4084 ((bibtex-string= name
"month")
4085 bibtex-predefined-month-strings
)
4086 (t (if (listp bibtex-strings
)
4088 ;; so that bibtex-complete-string-cleanup
4090 (bibtex-parse-strings
4091 (bibtex-string-files-init))))))
4092 (bibtex-beginning-of-entry)
4093 (if (and (re-search-forward bibtex-entry-maybe-empty-head nil t
)
4094 ;; point is inside a key
4095 (or (and (match-beginning bibtex-key-in-head
)
4096 (>= pnt
(match-beginning bibtex-key-in-head
))
4097 (<= pnt
(match-end bibtex-key-in-head
)))
4098 ;; or point is on empty key
4099 (and (not (match-beginning bibtex-key-in-head
))
4100 (= pnt
(match-end 0)))))
4101 (setq compl
'key
))))
4103 (cond ((equal compl
'key
)
4105 (setq choose-completion-string-functions
4106 (lambda (choice buffer mini-p base-size
)
4107 (let ((choose-completion-string-functions nil
))
4108 (choose-completion-string choice buffer base-size
))
4109 (bibtex-complete-key-cleanup choice
)
4110 ;; return t (required by choose-completion-string-functions)
4112 (bibtex-complete-key-cleanup (bibtex-complete-internal
4113 bibtex-reference-keys
)))
4116 ;; string completion
4117 (setq choose-completion-string-functions
4118 `(lambda (choice buffer mini-p base-size
)
4119 (let ((choose-completion-string-functions nil
))
4120 (choose-completion-string choice buffer base-size
))
4121 (bibtex-complete-string-cleanup choice
',compl
)
4122 ;; return t (required by choose-completion-string-functions)
4124 (bibtex-complete-string-cleanup (bibtex-complete-internal compl
)
4127 (t (error "Point outside key or BibTeX field")))))
4129 (defun bibtex-Article ()
4130 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
4132 (bibtex-entry "Article"))
4134 (defun bibtex-Book ()
4135 "Insert a new BibTeX @Book entry; see also `bibtex-entry'."
4137 (bibtex-entry "Book"))
4139 (defun bibtex-Booklet ()
4140 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'."
4142 (bibtex-entry "Booklet"))
4144 (defun bibtex-InBook ()
4145 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'."
4147 (bibtex-entry "InBook"))
4149 (defun bibtex-InCollection ()
4150 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'."
4152 (bibtex-entry "InCollection"))
4154 (defun bibtex-InProceedings ()
4155 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'."
4157 (bibtex-entry "InProceedings"))
4159 (defun bibtex-Manual ()
4160 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'."
4162 (bibtex-entry "Manual"))
4164 (defun bibtex-MastersThesis ()
4165 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'."
4167 (bibtex-entry "MastersThesis"))
4169 (defun bibtex-Misc ()
4170 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'."
4172 (bibtex-entry "Misc"))
4174 (defun bibtex-PhdThesis ()
4175 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'."
4177 (bibtex-entry "PhdThesis"))
4179 (defun bibtex-Proceedings ()
4180 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'."
4182 (bibtex-entry "Proceedings"))
4184 (defun bibtex-TechReport ()
4185 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'."
4187 (bibtex-entry "TechReport"))
4189 (defun bibtex-Unpublished ()
4190 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'."
4192 (bibtex-entry "Unpublished"))
4194 (defun bibtex-String (&optional key
)
4195 "Insert a new BibTeX @String entry with key KEY."
4196 (interactive (list (completing-read "String key: " bibtex-strings
4197 nil nil nil
'bibtex-key-history
)))
4198 (let ((bibtex-maintain-sorted-entries
4199 (unless bibtex-sort-ignore-string-entries
4200 bibtex-maintain-sorted-entries
))
4202 (unless (bibtex-prepare-new-entry (list key nil
"String"))
4203 (error "Entry with key `%s' already exists" key
))
4204 (if (zerop (length key
)) (setq key nil
))
4205 (indent-to-column bibtex-entry-offset
)
4207 (bibtex-entry-left-delimiter))
4210 (setq endpos
(point)))
4212 (bibtex-field-left-delimiter))
4214 (setq endpos
(point)))
4215 (insert (bibtex-field-right-delimiter)
4216 (bibtex-entry-right-delimiter)
4218 (goto-char endpos
)))
4220 (defun bibtex-Preamble ()
4221 "Insert a new BibTeX @Preamble entry."
4223 (bibtex-move-outside-of-entry)
4224 (indent-to-column bibtex-entry-offset
)
4226 (bibtex-entry-left-delimiter))
4227 (let ((endpos (point)))
4228 (insert (bibtex-entry-right-delimiter)
4230 (goto-char endpos
)))
4232 (defun bibtex-url (&optional pos
)
4233 "Browse a URL for the BibTeX entry at point.
4234 Optional POS is the location of the BibTeX entry.
4235 The URL is generated using the schemes defined in `bibtex-generate-url-list'
4236 \(see there\). Then the URL is passed to `browse-url'."
4239 (if pos
(goto-char pos
))
4240 (bibtex-beginning-of-entry)
4241 (let ((fields-alist (bibtex-parse-entry))
4242 ;; Always ignore case,
4243 (case-fold-search t
)
4244 (lst bibtex-generate-url-list
)
4245 (delim-regexp "\\`[{\"]\\(.*\\)[}\"]\\'")
4247 (while (setq scheme
(pop lst
))
4248 (when (and (setq field
(cdr (assoc-string (caar scheme
)
4250 ;; Always remove field delimiters
4251 (progn (if (string-match delim-regexp field
)
4252 (setq field
(match-string 1 field
)))
4253 (string-match (cdar scheme
) field
)))
4255 (if (null (cdr scheme
))
4256 (setq url
(match-string 0 field
)))
4257 (dolist (step (cdr scheme
))
4258 (cond ((stringp step
)
4259 (setq url
(concat url step
)))
4260 ((setq field
(cdr (assoc-string (car step
) fields-alist t
)))
4261 ;; Always remove field delimiters
4262 (if (string-match delim-regexp field
)
4263 (setq field
(match-string 1 field
)))
4264 (if (string-match (nth 1 step
) field
)
4266 ((functionp (nth 2 step
))
4267 (funcall (nth 2 step
) field
))
4268 ((numberp (nth 2 step
))
4269 (match-string (nth 2 step
) field
))
4271 (replace-match (nth 2 step
) nil nil field
))))
4272 ;; If the scheme is set up correctly,
4273 ;; we should never reach this point
4274 (error "Match failed: %s" field
))
4275 (setq url
(concat url field
)))
4276 ;; If the scheme is set up correctly,
4277 ;; we should never reach this point
4278 (t (error "Step failed: %s" step
))))
4281 (unless url
(message "No URL known.")))))
4284 ;; Make BibTeX a Feature
4288 ;; arch-tag: ee2be3af-caad-427f-b42a-d20fad630d04
4289 ;;; bibtex.el ends here