(Reverting): Add anchor.
[emacs.git] / lisp / textmodes / bibtex.el
blob15348205c512c228e400968a9b76ed3144f870f9
1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992,94,95,96,97,98,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)
20 ;; any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; 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.
32 ;;; Commentary:
34 ;; Major mode for editing and validating BibTeX files.
36 ;; Usage:
37 ;; See documentation for function bibtex-mode (or type "\M-x describe-mode"
38 ;; when you are in BibTeX mode).
40 ;; Todo:
41 ;; Distribute texinfo file.
43 ;;; Code:
46 ;; User Options:
48 (defgroup bibtex nil
49 "BibTeX mode"
50 :group 'tex
51 :prefix "bibtex-")
53 (defgroup bibtex-autokey nil
54 "Generate automatically a key from the author/editor and the title field"
55 :group 'bibtex
56 :prefix "bibtex-autokey-")
58 (defcustom bibtex-mode-hook nil
59 "List of functions to call on entry to BibTeX mode."
60 :group 'bibtex
61 :type 'hook)
63 (defcustom bibtex-field-delimiters 'braces
64 "*Type of field delimiters. Allowed values are `braces' or `double-quotes'."
65 :group 'bibtex
66 :type '(choice (const braces)
67 (const double-quotes)))
69 (defcustom bibtex-entry-delimiters 'braces
70 "*Type of entry delimiters. Allowed values are `braces' or `parentheses'."
71 :group 'bibtex
72 :type '(choice (const braces)
73 (const parentheses)))
75 (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
76 "*List of entries that get an OPTcrossref field."
77 :group 'bibtex
78 :type '(repeat string))
80 (defcustom bibtex-include-OPTkey t
81 "*If non-nil, all entries will have an OPTkey field.
82 If this is a string, it will be used as the initial field text.
83 If this is a function, it will be called to generate the initial field text."
84 :group 'bibtex
85 :type '(choice (const :tag "None" nil)
86 (string :tag "Initial text")
87 (function :tag "Initialize Function" :value fun)
88 (other :tag "Default" t)))
90 (defcustom bibtex-user-optional-fields
91 '(("annote" "Personal annotation (ignored)"))
92 "*List of optional fields the user wants to have always present.
93 Entries should be of the same form as the OPTIONAL and
94 CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (see documentation
95 of this variable for details)."
96 :group 'bibtex
97 :type '(repeat (group (string :tag "Field")
98 (string :tag "Comment")
99 (option (group :inline t
100 :extra-offset -4
101 (choice :tag "Init" :value ""
102 string
103 function))))))
105 (defcustom bibtex-entry-format
106 '(opts-or-alts required-fields numerical-fields)
107 "*Type of formatting performed by `bibtex-clean-entry'.
108 It may be t, nil, or a list of symbols out of the following:
109 opts-or-alts Delete empty optional and alternative fields and
110 remove OPT and ALT prefixes from used fields.
111 required-fields Signal an error if a required field is missing.
112 numerical-fields Delete delimiters around numeral fields.
113 page-dashes Change double dashes in page field to single dash
114 (for scribe compatibility).
115 inherit-booktitle If entry contains a crossref field and booktitle
116 field is empty, it is set to the contents of the
117 title field of the crossreferenced entry.
118 Caution: this will work only if buffer is
119 correctly sorted.
120 realign Realign entries, so that field texts and perhaps equal
121 signs (depending on the value of
122 `bibtex-align-at-equal-sign') begin in the same column.
123 last-comma Add or delete comma on end of last field in entry,
124 according to value of `bibtex-comma-after-last-field'.
125 delimiters Change delimiters according to variables
126 `bibtex-field-delimiters' and `bibtex-entry-delimiters'.
127 unify-case Change case of entry and field names.
129 The value t means do all of the above formatting actions.
130 The value nil means do no formatting at all."
131 :group 'bibtex
132 :type '(choice (const :tag "None" nil)
133 (const :tag "All" t)
134 (set :menu-tag "Some"
135 (const opts-or-alts)
136 (const required-fields)
137 (const numerical-fields)
138 (const page-dashes)
139 (const inherit-booktitle)
140 (const realign)
141 (const last-comma)
142 (const delimiters)
143 (const unify-case))))
145 (defcustom bibtex-clean-entry-hook nil
146 "*List of functions to call when entry has been cleaned.
147 Functions are called with point inside the cleaned entry, and the buffer
148 narrowed to just the entry."
149 :group 'bibtex
150 :type 'hook)
152 (defcustom bibtex-maintain-sorted-entries nil
153 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order.
154 Allowed non-nil values are:
155 plain All entries are sorted alphabetically.
156 crossref All entries are sorted alphabetically unless an entry has a
157 crossref field. These crossrefed entries are placed in
158 alphabetical order immediately preceding the main entry.
159 entry-class The entries are divided into classes according to their
160 entry name, see `bibtex-sort-entry-class'. Within each class
161 the entries are sorted alphabetically.
162 See also `bibtex-sort-ignore-string-entries'."
163 :group 'bibtex
164 :type '(choice (const nil)
165 (const plain)
166 (const crossref)
167 (const entry-class)))
169 (defvar bibtex-sort-entry-class
170 '(("String")
171 (catch-all)
172 ("Book" "Proceedings"))
173 "*List of classes of BibTeX entry names, used for sorting entries.
174 If value of `bibtex-maintain-sorted-entries' is `entry-class'
175 entries are ordered according to the classes they belong to. Each
176 class contains a list of entry names. An entry `catch-all' applies
177 to all entries not explicitely mentioned.")
179 (defcustom bibtex-sort-ignore-string-entries t
180 "*If non-nil, BibTeX @String entries are not sort-significant.
181 That means they are ignored when determining ordering of the buffer
182 \(e.g., sorting, locating alphabetical position for new entries, etc.)."
183 :group 'bibtex
184 :type 'boolean)
186 (defcustom bibtex-field-kill-ring-max 20
187 "*Max length of `bibtex-field-kill-ring' before discarding oldest elements."
188 :group 'bibtex
189 :type 'integer)
191 (defcustom bibtex-entry-kill-ring-max 20
192 "*Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
193 :group 'bibtex
194 :type 'integer)
196 (defcustom bibtex-parse-keys-timeout 60
197 "*Specify interval for parsing BibTeX buffers.
198 All BibTeX buffers in Emacs are parsed if Emacs has been idle
199 `bibtex-parse-keys-timeout' seconds. Only buffers which were modified
200 after last parsing and which are maintained in sorted order are parsed."
201 :group 'bibtex
202 :type 'integer)
204 (defcustom bibtex-parse-keys-fast t
205 "*If non-nil, use fast but simplified algorithm for parsing BibTeX keys.
206 If parsing fails, try to set this variable to nil."
207 :group 'bibtex
208 :type 'boolean)
210 (defvar bibtex-entry-field-alist
212 ("Article"
213 ((("author" "Author1 [and Author2 ...] [and others]")
214 ("title" "Title of the article (BibTeX converts it to lowercase)")
215 ("journal" "Name of the journal (use string, remove braces)")
216 ("year" "Year of publication"))
217 (("volume" "Volume of the journal")
218 ("number" "Number of the journal (only allowed if entry contains volume)")
219 ("pages" "Pages in the journal")
220 ("month" "Month of the publication as a string (remove braces)")
221 ("note" "Remarks to be put at the end of the \\bibitem")))
222 ((("author" "Author1 [and Author2 ...] [and others]")
223 ("title" "Title of the article (BibTeX converts it to lowercase)"))
224 (("pages" "Pages in the journal")
225 ("journal" "Name of the journal (use string, remove braces)")
226 ("year" "Year of publication")
227 ("volume" "Volume of the journal")
228 ("number" "Number of the journal")
229 ("month" "Month of the publication as a string (remove braces)")
230 ("note" "Remarks to be put at the end of the \\bibitem"))))
231 ("Book"
232 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
233 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
234 ("title" "Title of the book")
235 ("publisher" "Publishing company")
236 ("year" "Year of publication"))
237 (("volume" "Volume of the book in the series")
238 ("number" "Number of the book in a small series (overwritten by volume)")
239 ("series" "Series in which the book appeared")
240 ("address" "Address of the publisher")
241 ("edition" "Edition of the book as a capitalized English word")
242 ("month" "Month of the publication as a string (remove braces)")
243 ("note" "Remarks to be put at the end of the \\bibitem")))
244 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
245 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
246 ("title" "Title of the book"))
247 (("publisher" "Publishing company")
248 ("year" "Year of publication")
249 ("volume" "Volume of the book in the series")
250 ("number" "Number of the book in a small series (overwritten by volume)")
251 ("series" "Series in which the book appeared")
252 ("address" "Address of the publisher")
253 ("edition" "Edition of the book as a capitalized English word")
254 ("month" "Month of the publication as a string (remove braces)")
255 ("note" "Remarks to be put at the end of the \\bibitem"))))
256 ("Booklet"
257 ((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
258 (("author" "Author1 [and Author2 ...] [and others]")
259 ("howpublished" "The way in which the booklet was published")
260 ("address" "Address of the publisher")
261 ("month" "Month of the publication as a string (remove braces)")
262 ("year" "Year of publication")
263 ("note" "Remarks to be put at the end of the \\bibitem"))))
264 ("InBook"
265 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
266 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
267 ("title" "Title of the book")
268 ("chapter" "Chapter in the book")
269 ("publisher" "Publishing company")
270 ("year" "Year of publication"))
271 (("volume" "Volume of the book in the series")
272 ("number" "Number of the book in a small series (overwritten by volume)")
273 ("series" "Series in which the book appeared")
274 ("type" "Word to use instead of \"chapter\"")
275 ("address" "Address of the publisher")
276 ("edition" "Edition of the book as a capitalized English word")
277 ("month" "Month of the publication as a string (remove braces)")
278 ("pages" "Pages in the book")
279 ("note" "Remarks to be put at the end of the \\bibitem")))
280 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
281 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
282 ("title" "Title of the book")
283 ("chapter" "Chapter in the book"))
284 (("pages" "Pages in the book")
285 ("publisher" "Publishing company")
286 ("year" "Year of publication")
287 ("volume" "Volume of the book in the series")
288 ("number" "Number of the book in a small series (overwritten by volume)")
289 ("series" "Series in which the book appeared")
290 ("type" "Word to use instead of \"chapter\"")
291 ("address" "Address of the publisher")
292 ("edition" "Edition of the book as a capitalized English word")
293 ("month" "Month of the publication as a string (remove braces)")
294 ("note" "Remarks to be put at the end of the \\bibitem"))))
295 ("InCollection"
296 ((("author" "Author1 [and Author2 ...] [and others]")
297 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
298 ("booktitle" "Name of the book")
299 ("publisher" "Publishing company")
300 ("year" "Year of publication"))
301 (("editor" "Editor1 [and Editor2 ...] [and others]")
302 ("volume" "Volume of the book in the series")
303 ("number" "Number of the book in a small series (overwritten by volume)")
304 ("series" "Series in which the book appeared")
305 ("type" "Word to use instead of \"chapter\"")
306 ("chapter" "Chapter in the book")
307 ("pages" "Pages in the book")
308 ("address" "Address of the publisher")
309 ("edition" "Edition of the book as a capitalized English word")
310 ("month" "Month of the publication as a string (remove braces)")
311 ("note" "Remarks to be put at the end of the \\bibitem")))
312 ((("author" "Author1 [and Author2 ...] [and others]")
313 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
314 ("booktitle" "Name of the book"))
315 (("pages" "Pages in the book")
316 ("publisher" "Publishing company")
317 ("year" "Year of publication")
318 ("editor" "Editor1 [and Editor2 ...] [and others]")
319 ("volume" "Volume of the book in the series")
320 ("number" "Number of the book in a small series (overwritten by volume)")
321 ("series" "Series in which the book appeared")
322 ("type" "Word to use instead of \"chapter\"")
323 ("chapter" "Chapter in the book")
324 ("address" "Address of the publisher")
325 ("edition" "Edition of the book as a capitalized English word")
326 ("month" "Month of the publication as a string (remove braces)")
327 ("note" "Remarks to be put at the end of the \\bibitem"))))
328 ("InProceedings"
329 ((("author" "Author1 [and Author2 ...] [and others]")
330 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
331 ("booktitle" "Name of the conference proceedings")
332 ("year" "Year of publication"))
333 (("editor" "Editor1 [and Editor2 ...] [and others]")
334 ("volume" "Volume of the conference proceedings in the series")
335 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
336 ("series" "Series in which the conference proceedings appeared")
337 ("pages" "Pages in the conference proceedings")
338 ("address" "Location of the Proceedings")
339 ("month" "Month of the publication as a string (remove braces)")
340 ("organization" "Sponsoring organization of the conference")
341 ("publisher" "Publishing company, its location")
342 ("note" "Remarks to be put at the end of the \\bibitem")))
343 ((("author" "Author1 [and Author2 ...] [and others]")
344 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)"))
345 (("booktitle" "Name of the conference proceedings")
346 ("pages" "Pages in the conference proceedings")
347 ("year" "Year of publication")
348 ("editor" "Editor1 [and Editor2 ...] [and others]")
349 ("volume" "Volume of the conference proceedings in the series")
350 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
351 ("series" "Series in which the conference proceedings appeared")
352 ("address" "Location of the Proceedings")
353 ("month" "Month of the publication as a string (remove braces)")
354 ("organization" "Sponsoring organization of the conference")
355 ("publisher" "Publishing company, its location")
356 ("note" "Remarks to be put at the end of the \\bibitem"))))
357 ("Manual"
358 ((("title" "Title of the manual"))
359 (("author" "Author1 [and Author2 ...] [and others]")
360 ("organization" "Publishing organization of the manual")
361 ("address" "Address of the organization")
362 ("edition" "Edition of the manual as a capitalized English word")
363 ("month" "Month of the publication as a string (remove braces)")
364 ("year" "Year of publication")
365 ("note" "Remarks to be put at the end of the \\bibitem"))))
366 ("MastersThesis"
367 ((("author" "Author1 [and Author2 ...] [and others]")
368 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
369 ("school" "School where the master\'s thesis was written")
370 ("year" "Year of publication"))
371 (("type" "Type of the master\'s thesis (if other than \"Master\'s thesis\")")
372 ("address" "Address of the school (if not part of field \"school\") or country")
373 ("month" "Month of the publication as a string (remove braces)")
374 ("note" "Remarks to be put at the end of the \\bibitem"))))
375 ("Misc"
377 (("author" "Author1 [and Author2 ...] [and others]")
378 ("title" "Title of the work (BibTeX converts it to lowercase)")
379 ("howpublished" "The way in which the work was published")
380 ("month" "Month of the publication as a string (remove braces)")
381 ("year" "Year of publication")
382 ("note" "Remarks to be put at the end of the \\bibitem"))))
383 ("PhdThesis"
384 ((("author" "Author1 [and Author2 ...] [and others]")
385 ("title" "Title of the PhD. thesis")
386 ("school" "School where the PhD. thesis was written")
387 ("year" "Year of publication"))
388 (("type" "Type of the PhD. thesis")
389 ("address" "Address of the school (if not part of field \"school\") or country")
390 ("month" "Month of the publication as a string (remove braces)")
391 ("note" "Remarks to be put at the end of the \\bibitem"))))
392 ("Proceedings"
393 ((("title" "Title of the conference proceedings")
394 ("year" "Year of publication"))
395 (("booktitle" "Title of the proceedings for cross references")
396 ("editor" "Editor1 [and Editor2 ...] [and others]")
397 ("volume" "Volume of the conference proceedings in the series")
398 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
399 ("series" "Series in which the conference proceedings appeared")
400 ("address" "Location of the Proceedings")
401 ("month" "Month of the publication as a string (remove braces)")
402 ("organization" "Sponsoring organization of the conference")
403 ("publisher" "Publishing company, its location")
404 ("note" "Remarks to be put at the end of the \\bibitem"))))
405 ("TechReport"
406 ((("author" "Author1 [and Author2 ...] [and others]")
407 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
408 ("institution" "Sponsoring institution of the report")
409 ("year" "Year of publication"))
410 (("type" "Type of the report (if other than \"technical report\")")
411 ("number" "Number of the technical report")
412 ("address" "Address of the institution (if not part of field \"institution\") or country")
413 ("month" "Month of the publication as a string (remove braces)")
414 ("note" "Remarks to be put at the end of the \\bibitem"))))
415 ("Unpublished"
416 ((("author" "Author1 [and Author2 ...] [and others]")
417 ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
418 ("note" "Remarks to be put at the end of the \\bibitem"))
419 (("month" "Month of the publication as a string (remove braces)")
420 ("year" "Year of publication"))))
423 "Defines entry types and their associated fields.
424 List of
425 \(ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL))
426 triples.
427 If the third element is nil, the first pair is always used.
428 If not, the second pair is used in the case of presence of a crossref
429 field and the third in the case of absence.
430 REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.
431 Each element of these lists is a list of the form
432 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).
433 COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.
434 FIELD-NAME is the name of the field, COMMENT-STRING the comment to
435 appear in the echo area, INIT is either the initial content of the
436 field or a function, which is called to determine the initial content
437 of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
438 field is an alternative. ALTERNATIVE-FLAG may be t only in the
439 REQUIRED or CROSSREF-REQUIRED lists.")
441 (defvar bibtex-comment-start "@Comment"
442 "String starting a BibTeX comment.")
444 (defcustom bibtex-add-entry-hook nil
445 "List of functions to call when entry has been inserted."
446 :group 'bibtex
447 :type 'hook)
449 (defcustom bibtex-predefined-month-strings
450 '(("jan" . "January")
451 ("feb" . "February")
452 ("mar" . "March")
453 ("apr" . "April")
454 ("may" . "May")
455 ("jun" . "June")
456 ("jul" . "July")
457 ("aug" . "August")
458 ("sep" . "September")
459 ("oct" . "October")
460 ("nov" . "November")
461 ("dec" . "December"))
462 "Alist of month string definitions used in the BibTeX style files.
463 Each element is a pair of strings (ABBREVIATION . EXPANSION)."
464 :group 'bibtex
465 :type '(repeat (cons (string :tag "Month abbreviation")
466 (string :tag "Month expansion"))))
468 (defcustom bibtex-predefined-strings
469 (append
470 bibtex-predefined-month-strings
471 '(("acmcs" . "ACM Computing Surveys")
472 ("acta" . "Acta Informatica")
473 ("cacm" . "Communications of the ACM")
474 ("ibmjrd" . "IBM Journal of Research and Development")
475 ("ibmsj" . "IBM Systems Journal")
476 ("ieeese" . "IEEE Transactions on Software Engineering")
477 ("ieeetc" . "IEEE Transactions on Computers")
478 ("ieeetcad" . "IEEE Transactions on Computer-Aided Design of Integrated Circuits")
479 ("ipl" . "Information Processing Letters")
480 ("jacm" . "Journal of the ACM")
481 ("jcss" . "Journal of Computer and System Sciences")
482 ("scp" . "Science of Computer Programming")
483 ("sicomp" . "SIAM Journal on Computing")
484 ("tcs" . "Theoretical Computer Science")
485 ("tocs" . "ACM Transactions on Computer Systems")
486 ("tods" . "ACM Transactions on Database Systems")
487 ("tog" . "ACM Transactions on Graphics")
488 ("toms" . "ACM Transactions on Mathematical Software")
489 ("toois" . "ACM Transactions on Office Information Systems")
490 ("toplas" . "ACM Transactions on Programming Languages and Systems")))
491 "Alist of string definitions used in the BibTeX style files.
492 Each element is a pair of strings (ABBREVIATION . EXPANSION)."
493 :group 'bibtex
494 :type '(repeat (cons (string :tag "String")
495 (string :tag "String expansion"))))
497 (defcustom bibtex-string-files nil
498 "*List of BibTeX files containing string definitions.
499 Those files must be specified using pathnames relative to the
500 directories specified in `bibtex-string-file-path'."
501 :group 'bibtex
502 :type '(repeat file))
504 (defvar bibtex-string-file-path (getenv "BIBINPUTS")
505 "*Colon separated list of paths to search for `bibtex-string-files'.")
507 (defcustom bibtex-help-message t
508 "*If non-nil print help messages in the echo area on entering a new field."
509 :group 'bibtex
510 :type 'boolean)
512 (defcustom bibtex-autokey-prefix-string ""
513 "*String to use as a prefix for all generated keys.
514 See `bibtex-generate-autokey' for details."
515 :group 'bibtex-autokey
516 :type 'string)
518 (defcustom bibtex-autokey-names 1
519 "*Number of names to use for the automatically generated reference key.
520 Possibly more names are used according to `bibtex-autokey-names-stretch'.
521 If this variable is nil, all names are used.
522 See `bibtex-generate-autokey' for details."
523 :group 'bibtex-autokey
524 :type '(choice (const :tag "All" infty)
525 integer))
527 (defcustom bibtex-autokey-names-stretch 0
528 "*Number of names that can additionally be used.
529 These names are used only, if all names are used then.
530 See `bibtex-generate-autokey' for details."
531 :group 'bibtex-autokey
532 :type 'integer)
534 (defcustom bibtex-autokey-additional-names ""
535 "*String to prepend to the generated key if not all names could be used.
536 See `bibtex-generate-autokey' for details."
537 :group 'bibtex-autokey
538 :type 'string)
540 (defvar bibtex-autokey-transcriptions
541 '(;; language specific characters
542 ("\\\\aa" . "a") ; \aa -> a
543 ("\\\\AA" . "A") ; \AA -> A
544 ("\\\"a\\|\\\\\\\"a\\|\\\\ae" . "ae") ; "a,\"a,\ae -> ae
545 ("\\\"A\\|\\\\\\\"A\\|\\\\AE" . "Ae") ; "A,\"A,\AE -> Ae
546 ("\\\\i" . "i") ; \i -> i
547 ("\\\\j" . "j") ; \j -> j
548 ("\\\\l" . "l") ; \l -> l
549 ("\\\\L" . "L") ; \L -> L
550 ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" . "oe") ; "o,\"o,\o,\oe -> oe
551 ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" . "Oe") ; "O,\"O,\O,\OE -> Oe
552 ("\\\"s\\|\\\\\\\"s\\|\\\\3" . "ss") ; "s,\"s,\3 -> ss
553 ("\\\"u\\|\\\\\\\"u" . "ue") ; "u,\"u -> ue
554 ("\\\"U\\|\\\\\\\"U" . "Ue") ; "U,\"U -> Ue
555 ;; accents
556 ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" . "")
557 ;; braces, quotes, concatenation.
558 ("[`'\"{}#]" . "")
559 ;; spaces
560 ("[ \t\n]+" . " "))
561 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
562 Used by the default values of `bibtex-autokey-name-change-strings' and
563 `bibtex-autokey-titleword-change-strings'. Defaults to translating some
564 language specific characters to their ASCII transcriptions, and
565 removing any character accents.")
567 (defcustom bibtex-autokey-name-change-strings
568 bibtex-autokey-transcriptions
569 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
570 Any part of name matching a OLD-REGEXP is replaced by NEW-STRING.
571 Case is significant in OLD-REGEXP. All regexps are tried in the
572 order in which they appear in the list.
573 See `bibtex-generate-autokey' for details."
574 :group 'bibtex-autokey
575 :type '(repeat (cons (regexp :tag "Old")
576 (string :tag "New"))))
578 (defcustom bibtex-autokey-name-case-convert 'downcase
579 "*Function called for each name to perform case conversion.
580 See `bibtex-generate-autokey' for details."
581 :group 'bibtex-autokey
582 :type '(choice (const :tag "Preserve case" identity)
583 (const :tag "Downcase" downcase)
584 (const :tag "Capitalize" capitalize)
585 (const :tag "Upcase" upcase)
586 (function :tag "Conversion function")))
588 (defcustom bibtex-autokey-name-length 'infty
589 "*Number of characters from name to incorporate into key.
590 If this is set to anything but a number, all characters are used.
591 See `bibtex-generate-autokey' for details."
592 :group 'bibtex-autokey
593 :type '(choice (const :tag "All" infty)
594 integer))
596 (defcustom bibtex-autokey-name-separator ""
597 "*String that comes between any two names in the key.
598 See `bibtex-generate-autokey' for details."
599 :group 'bibtex-autokey
600 :type 'string)
602 (defcustom bibtex-autokey-year-length 2
603 "*Number of rightmost digits from the year field to incorporate into key.
604 See `bibtex-generate-autokey' for details."
605 :group 'bibtex-autokey
606 :type 'integer)
608 (defcustom bibtex-autokey-use-crossref t
609 "*If non-nil use fields from crossreferenced entry if necessary.
610 If this variable is non-nil and some field has no entry, but a
611 valid crossref entry, the field from the crossreferenced entry is used.
612 See `bibtex-generate-autokey' for details."
613 :group 'bibtex-autokey
614 :type 'boolean)
616 (defcustom bibtex-autokey-titlewords 5
617 "*Number of title words to use for the automatically generated reference key.
618 If this is set to anything but a number, all title words are used.
619 Possibly more words from the title are used according to
620 `bibtex-autokey-titlewords-stretch'.
621 See `bibtex-generate-autokey' for details."
622 :group 'bibtex-autokey
623 :type '(choice (const :tag "All" infty)
624 integer))
626 (defcustom bibtex-autokey-title-terminators
627 '("\\." "!" "\\?" ":" ";" "--")
628 "*Regexp list defining the termination of the main part of the title.
629 Case of the regexps is ignored.
630 See `bibtex-generate-autokey' for details."
631 :group 'bibtex-autokey
632 :type '(repeat regexp))
634 (defcustom bibtex-autokey-titlewords-stretch 2
635 "*Number of words that can additionally be used from the title.
636 These words are used only, if a sentence from the title can be ended then.
637 See `bibtex-generate-autokey' for details."
638 :group 'bibtex-autokey
639 :type 'integer)
641 (defcustom bibtex-autokey-titleword-ignore
642 '("A" "An" "On" "The" "Eine?" "Der" "Die" "Das"
643 "[^A-Z].*" ".*[^a-zA-Z0-9].*")
644 "*Determines words from the title that are not to be used in the key.
645 Each item of the list is a regexp. If a word of the title matchs a
646 regexp from that list, it is not included in the title part of the key.
647 See `bibtex-generate-autokey' for details."
648 :group 'bibtex-autokey
649 :type '(repeat regexp))
651 (defcustom bibtex-autokey-titleword-case-convert 'downcase
652 "*Function called for each titleword to perform case conversion.
653 See `bibtex-generate-autokey' for details."
654 :group 'bibtex-autokey
655 :type '(choice (const :tag "Preserve case" identity)
656 (const :tag "Downcase" downcase)
657 (const :tag "Capitalize" capitalize)
658 (const :tag "Upcase" upcase)
659 (function :tag "Conversion function")))
661 (defcustom bibtex-autokey-titleword-abbrevs nil
662 "*Determines exceptions to the usual abbreviation mechanism.
663 An alist of (OLD-REGEXP . NEW-STRING) pairs. Case is ignored
664 in matching against OLD-REGEXP, and the first matching pair is used.
665 See `bibtex-generate-autokey' for details."
666 :group 'bibtex-autokey
667 :type '(repeat (cons (regexp :tag "Old")
668 (string :tag "New"))))
670 (defcustom bibtex-autokey-titleword-change-strings
671 bibtex-autokey-transcriptions
672 "Alist of (OLD-REGEXP . NEW-STRING) pairs.
673 Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
674 Case is significant in OLD-REGEXP. All regexps are tried in the
675 order in which they appear in the list.
676 See `bibtex-generate-autokey' for details."
677 :group 'bibtex-autokey
678 :type '(repeat (cons (regexp :tag "Old")
679 (string :tag "New"))))
681 (defcustom bibtex-autokey-titleword-length 5
682 "*Number of characters from title words to incorporate into key.
683 If this is set to anything but a number, all characters are used.
684 See `bibtex-generate-autokey' for details."
685 :group 'bibtex-autokey
686 :type '(choice (const :tag "All" infty)
687 integer))
689 (defcustom bibtex-autokey-titleword-separator "_"
690 "*String to be put between the title words.
691 See `bibtex-generate-autokey' for details."
692 :group 'bibtex-autokey
693 :type 'string)
695 (defcustom bibtex-autokey-name-year-separator ""
696 "*String to be put between name part and year part of key.
697 See `bibtex-generate-autokey' for details."
698 :group 'bibtex-autokey
699 :type 'string)
701 (defcustom bibtex-autokey-year-title-separator ":_"
702 "*String to be put between name part and year part of key.
703 See `bibtex-generate-autokey' for details."
704 :group 'bibtex-autokey
705 :type 'string)
707 (defcustom bibtex-autokey-edit-before-use t
708 "*If non-nil, user is allowed to edit the generated key before it is used."
709 :group 'bibtex-autokey
710 :type 'boolean)
712 (defcustom bibtex-autokey-before-presentation-function nil
713 "*Function to call before the generated key is presented.
714 If non-nil this should be a function which is called before the generated key
715 is presented. The function must take one argument (the automatically
716 generated key), and must return a string (the key to use)."
717 :group 'bibtex-autokey
718 :type '(choice (const nil) function))
720 (defcustom bibtex-entry-offset 0
721 "*Offset for BibTeX entries.
722 Added to the value of all other variables which determine colums."
723 :group 'bibtex
724 :type 'integer)
726 (defcustom bibtex-field-indentation 2
727 "*Starting column for the name part in BibTeX fields."
728 :group 'bibtex
729 :type 'integer)
731 (defcustom bibtex-text-indentation
732 (+ bibtex-field-indentation
733 (length "organization = "))
734 "*Starting column for the text part in BibTeX fields.
735 Should be equal to the space needed for the longest name part."
736 :group 'bibtex
737 :type 'integer)
739 (defcustom bibtex-contline-indentation
740 (+ bibtex-text-indentation 1)
741 "*Starting column for continuation lines of BibTeX fields."
742 :group 'bibtex
743 :type 'integer)
745 (defcustom bibtex-align-at-equal-sign nil
746 "*If non-nil, align fields at equal sign instead of field text.
747 If non-nil, the column for the equal sign is the value of
748 `bibtex-text-indentation', minus 2."
749 :group 'bibtex
750 :type 'boolean)
752 (defcustom bibtex-comma-after-last-field nil
753 "*If non-nil, a comma is put at end of last field in the entry template."
754 :group 'bibtex
755 :type 'boolean)
757 (defcustom bibtex-autoadd-commas t
758 "If non-nil automatically add missing commas at end of BibTeX fields."
759 :type 'boolean)
761 (defcustom bibtex-autofill-types '("Proceedings")
762 "Automatically fill fields if possible for those BibTeX entry types."
763 :type '(repeat string))
765 (defcustom bibtex-complete-key-cleanup nil
766 "*Function called by `bibtex-complete' after insertion of a key fragment."
767 :group 'bibtex-autokey
768 :type '(choice (const :tag "None" nil)
769 (function :tag "Cleanup function")))
771 ;; bibtex-font-lock-keywords is a user option as well, but since the
772 ;; patterns used to define this variable are defined in a later
773 ;; section of this file, it is defined later.
776 ;; Syntax Table, Keybindings and BibTeX Entry List
777 (defvar bibtex-mode-syntax-table
778 (let ((st (make-syntax-table)))
779 (modify-syntax-entry ?\" "\"" st)
780 (modify-syntax-entry ?$ "$$ " st)
781 (modify-syntax-entry ?% "< " st)
782 (modify-syntax-entry ?' "w " st)
783 (modify-syntax-entry ?@ "w " st)
784 (modify-syntax-entry ?\\ "\\" st)
785 (modify-syntax-entry ?\f "> " st)
786 (modify-syntax-entry ?\n "> " st)
787 ;; Keys cannot have = in them (wrong font-lock of @string{foo=bar}).
788 (modify-syntax-entry ?= "." st)
789 (modify-syntax-entry ?~ " " st)
791 "Syntax table used in BibTeX mode buffers.")
793 (defvar bibtex-mode-map
794 (let ((km (make-sparse-keymap)))
795 ;; The Key `C-c&' is reserved for reftex.el
796 (define-key km "\t" 'bibtex-find-text)
797 (define-key km "\n" 'bibtex-next-field)
798 (define-key km "\M-\t" 'bibtex-complete)
799 (define-key km "\C-c\"" 'bibtex-remove-delimiters)
800 (define-key km "\C-c{" 'bibtex-remove-delimiters)
801 (define-key km "\C-c}" 'bibtex-remove-delimiters)
802 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
803 (define-key km "\C-c\C-q" 'bibtex-fill-entry)
804 (define-key km "\C-c?" 'bibtex-print-help-message)
805 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
806 (define-key km "\C-c\C-n" 'bibtex-pop-next)
807 (define-key km "\C-c\C-k" 'bibtex-kill-field)
808 (define-key km "\C-c\M-k" 'bibtex-copy-field-as-kill)
809 (define-key km "\C-c\C-w" 'bibtex-kill-entry)
810 (define-key km "\C-c\M-w" 'bibtex-copy-entry-as-kill)
811 (define-key km "\C-c\C-y" 'bibtex-yank)
812 (define-key km "\C-c\M-y" 'bibtex-yank-pop)
813 (define-key km "\C-c\C-d" 'bibtex-empty-field)
814 (define-key km "\C-c\C-f" 'bibtex-make-field)
815 (define-key km "\C-c\C-u" 'bibtex-entry-update)
816 (define-key km "\C-c$" 'bibtex-ispell-abstract)
817 (define-key km "\M-\C-a" 'bibtex-beginning-of-entry)
818 (define-key km "\M-\C-e" 'bibtex-end-of-entry)
819 (define-key km "\C-\M-l" 'bibtex-reposition-window)
820 (define-key km "\C-\M-h" 'bibtex-mark-entry)
821 (define-key km "\C-c\C-b" 'bibtex-entry)
822 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
823 (define-key km "\C-c\C-rw" 'widen)
824 (define-key km "\C-c\C-o" 'bibtex-remove-OPT-or-ALT)
825 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
826 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
827 (define-key km "\C-c\C-eI" 'bibtex-InBook)
828 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
829 (define-key km "\C-c\C-e\C-b" 'bibtex-InBook)
830 (define-key km "\C-c\C-eb" 'bibtex-Book)
831 (define-key km "\C-c\C-eB" 'bibtex-Booklet)
832 (define-key km "\C-c\C-e\C-c" 'bibtex-InCollection)
833 (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
834 (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
835 (define-key km "\C-c\C-eM" 'bibtex-Misc)
836 (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
837 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
838 (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
839 (define-key km "\C-c\C-e\M-p" 'bibtex-Preamble)
840 (define-key km "\C-c\C-e\C-s" 'bibtex-String)
841 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
842 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
844 "Keymap used in BibTeX mode.")
846 (easy-menu-define
847 bibtex-edit-menu bibtex-mode-map "BibTeX-Edit Menu in BibTeX mode"
848 '("BibTeX-Edit"
849 ("Moving inside an Entry"
850 ["End of Field" bibtex-find-text t]
851 ["Next Field" bibtex-next-field t]
852 ["Beginning of Entry" bibtex-beginning-of-entry t]
853 ["End of Entry" bibtex-end-of-entry t])
854 ("Moving in BibTeX Buffer"
855 ["Find Entry" bibtex-find-entry t]
856 ["Find Crossref Entry" bibtex-find-crossref t])
857 ("Operating on Current Entry"
858 ["Fill Entry" bibtex-fill-entry t]
859 ["Clean Entry" bibtex-clean-entry t]
860 "--"
861 ["Kill Entry" bibtex-kill-entry t]
862 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t]
863 ["Paste Most Recently Killed Entry" bibtex-yank t]
864 ["Paste Previously Killed Entry" bibtex-yank-pop t]
865 "--"
866 ["Ispell Entry" bibtex-ispell-entry t]
867 ["Ispell Entry Abstract" bibtex-ispell-abstract t]
868 ["Narrow to Entry" bibtex-narrow-to-entry t]
869 "--"
870 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
871 (fboundp 'reftex-view-crossref-from-bibtex)])
872 ("Operating on Current Field"
873 ["Fill Field" fill-paragraph t]
874 ["Remove Delimiters" bibtex-remove-delimiters t]
875 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t]
876 ["Clear Field" bibtex-empty-field t]
877 "--"
878 ["Kill Field" bibtex-kill-field t]
879 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t]
880 ["Paste Most Recently Killed Field" bibtex-yank t]
881 ["Paste Previously Killed Field" bibtex-yank-pop t]
882 "--"
883 ["Make New Field" bibtex-make-field t]
884 "--"
885 ["Snatch from Similar Following Field" bibtex-pop-next t]
886 ["Snatch from Similar Preceding Field" bibtex-pop-previous t]
887 "--"
888 ["String or Key Complete" bibtex-complete t]
889 "--"
890 ["Help about Current Field" bibtex-print-help-message t])
891 ("Operating on Buffer or Region"
892 ["Validate Entries" bibtex-validate t]
893 ["Sort Entries" bibtex-sort-buffer t]
894 ["Reformat Entries" bibtex-reformat t]
895 ["Count Entries" bibtex-count-entries t])
896 ("Miscellaneous"
897 ["Convert Alien Buffer" bibtex-convert-alien t])))
899 (easy-menu-define
900 bibtex-entry-menu bibtex-mode-map "Entry-Types Menu in BibTeX mode"
901 (list "Entry-Types"
902 ["Article in Journal" bibtex-Article t]
903 ["Article in Conference Proceedings" bibtex-InProceedings t]
904 ["Article in a Collection" bibtex-InCollection t]
905 ["Chapter or Pages in a Book" bibtex-InBook t]
906 ["Conference Proceedings" bibtex-Proceedings t]
907 ["Book" bibtex-Book t]
908 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t]
909 ["PhD. Thesis" bibtex-PhdThesis t]
910 ["Master's Thesis" bibtex-MastersThesis t]
911 ["Technical Report" bibtex-TechReport t]
912 ["Technical Manual" bibtex-Manual t]
913 ["Unpublished" bibtex-Unpublished t]
914 ["Miscellaneous" bibtex-Misc t]
915 ["String" bibtex-String t]
916 ["Preamble" bibtex-Preamble t]))
919 ;; Internal Variables
921 (defvar bibtex-pop-previous-search-point nil
922 "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
924 (defvar bibtex-pop-next-search-point nil
925 "Next point where `bibtex-pop-next' starts looking for a similar entry.")
927 (defvar bibtex-field-kill-ring nil
928 "Ring of least recently killed fields.
929 At most `bibtex-field-kill-ring-max' items are kept here.")
931 (defvar bibtex-field-kill-ring-yank-pointer nil
932 "The tail of `bibtex-field-kill-ring' whose car is the last item yanked.")
934 (defvar bibtex-entry-kill-ring nil
935 "Ring of least recently killed entries.
936 At most `bibtex-entry-kill-ring-max' items are kept here.")
938 (defvar bibtex-entry-kill-ring-yank-pointer nil
939 "The tail of `bibtex-entry-kill-ring' whose car is the last item yanked.")
941 (defvar bibtex-last-kill-command nil
942 "Type of the last kill command (either 'field or 'entry).")
944 (defvar bibtex-strings
945 (lazy-completion-table bibtex-strings
946 bibtex-parse-strings (bibtex-string-files-init))
947 "Completion table for BibTeX string keys.
948 Initialized from `bibtex-predefined-strings' and `bibtex-string-files'.")
949 (make-variable-buffer-local 'bibtex-strings)
951 (defvar bibtex-reference-keys
952 (lazy-completion-table bibtex-reference-keys bibtex-parse-keys nil nil t)
953 "Completion table for BibTeX reference keys.")
954 (make-variable-buffer-local 'bibtex-reference-keys)
956 (defvar bibtex-buffer-last-parsed-tick nil
957 "Last value returned by `buffer-modified-tick' when buffer
958 was parsed for keys the last time.")
960 (defvar bibtex-parse-idle-timer nil
961 "Stores if timer is already installed.")
963 (defvar bibtex-progress-lastperc nil
964 "Last reported percentage for the progress message.")
966 (defvar bibtex-progress-lastmes nil
967 "Last reported progress message.")
969 (defvar bibtex-progress-interval nil
970 "Interval for progress messages.")
972 (defvar bibtex-key-history nil
973 "History list for reading keys.")
975 (defvar bibtex-entry-type-history nil
976 "History list for reading entry types.")
978 (defvar bibtex-field-history nil
979 "History list for reading field names.")
981 (defvar bibtex-reformat-previous-options nil
982 "Last reformat options given.")
984 (defvar bibtex-reformat-previous-reference-keys nil
985 "Last reformat reference keys option given.")
987 (defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*"
988 "Regexp matching the name part of a BibTeX field.")
990 (defconst bibtex-entry-type (concat "@" bibtex-field-name)
991 "Regexp matching the type part of a BibTeX entry.")
993 (defconst bibtex-reference-key "[][a-zA-Z0-9.:;?!`'/*@+|()<>&_^$-]+"
994 "Regexp matching the reference key part of a BibTeX entry.")
996 (defconst bibtex-field-const "[][a-zA-Z0-9.:;?!`'/*@+=|<>&_^$-]+"
997 "Regexp matching a BibTeX field constant.")
999 (defconst bibtex-entry-head
1000 (concat "^[ \t]*\\("
1001 bibtex-entry-type
1002 "\\)[ \t]*[({][ \t\n]*\\("
1003 bibtex-reference-key
1004 "\\)")
1005 "Regexp matching the header line of a BibTeX entry.")
1007 (defconst bibtex-entry-maybe-empty-head
1008 (concat bibtex-entry-head "?")
1009 "Regexp matching the header line of a BibTeX entry (possibly without key).")
1011 (defconst bibtex-type-in-head 1
1012 "Regexp subexpression number of the type part in `bibtex-entry-head'.")
1014 (defconst bibtex-key-in-head 2
1015 "Regexp subexpression number of the key part in `bibtex-entry-head'.")
1017 (defconst bibtex-entry-postfix "[ \t\n]*,?[ \t\n]*[})]"
1018 "Regexp matching the postfix of a BibTeX entry.")
1020 (defvar bibtex-known-entry-type-re
1021 (regexp-opt (mapcar 'car bibtex-entry-field-alist))
1022 "Regexp matching the name of a BibTeX entry type.")
1024 (defvar bibtex-valid-entry-re
1025 (concat "@[ \t]*\\(" bibtex-known-entry-type-re "\\)")
1026 "Regexp matching the name of a valid BibTeX entry.")
1028 (defvar bibtex-valid-entry-whitespace-re
1029 (concat "[ \t\n]*\\(" bibtex-valid-entry-re "\\)")
1030 "Regexp matching the name of a valid BibTeX entry preceded by whitespace.")
1032 (defvar bibtex-any-valid-entry-re
1033 (concat "@[ \t]*"
1034 (regexp-opt (append '("String")
1035 (mapcar 'car bibtex-entry-field-alist))
1037 "Regexp matching the name of any valid BibTeX entry (including string).")
1040 (defconst bibtex-empty-field-re "\"\"\\|{}"
1041 "Regexp matching an empty field.")
1043 (defconst bibtex-quoted-string-re
1044 (concat "\""
1045 "\\("
1046 "[^\"\\]" ; anything but quote or backslash
1047 "\\|"
1048 "\\("
1049 "\\\\\\(.\\|\n\\)" ; any backslash quoted character
1050 "\\)"
1051 "\\)*"
1052 "\"")
1053 "Regexp matching a field string enclosed by quotes.")
1055 (defconst bibtex-font-lock-syntactic-keywords
1056 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start 0 1) "\\)"
1057 (substring bibtex-comment-start 1) "\\>")
1058 1 '(11))))
1060 (defvar bibtex-font-lock-keywords
1061 (list
1062 ;; entry type and reference key
1063 (list bibtex-entry-maybe-empty-head
1064 (list bibtex-type-in-head 'font-lock-function-name-face)
1065 (list bibtex-key-in-head 'font-lock-constant-face nil t))
1066 ;; optional field names (treated as comments)
1067 (list
1068 (concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
1069 1 'font-lock-comment-face)
1070 ;; field names
1071 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1072 1 'font-lock-variable-name-face))
1073 "*Default expressions to highlight in BibTeX mode.")
1075 (defvar bibtex-field-name-for-parsing nil
1076 "Temporary variable storing the name string to be parsed by the callback
1077 function `bibtex-parse-field-name'.")
1079 (defvar bibtex-sort-entry-class-alist
1080 (let ((i -1) alist)
1081 (dolist (class bibtex-sort-entry-class alist)
1082 (setq i (1+ i))
1083 (dolist (entry class)
1084 ;; all entry names should be downcase (for ease of comparison)
1085 (push (cons (if (stringp entry) (downcase entry) entry) i) alist))))
1086 "Alist for the classes of the entry types if the value of
1087 `bibtex-maintain-sorted-entries' is `entry-class'.")
1090 ;; Special support taking care of variants
1091 (defvar zmacs-regions)
1092 (if (boundp 'mark-active)
1093 (defun bibtex-mark-active ()
1094 ;; In Emacs mark-active indicates if mark is active.
1095 mark-active)
1096 (defun bibtex-mark-active ()
1097 ;; In XEmacs (mark) returns nil when not active.
1098 (if zmacs-regions (mark) (mark t))))
1100 (if (fboundp 'run-with-idle-timer)
1101 ;; timer.el is distributed with Emacs
1102 (fset 'bibtex-run-with-idle-timer 'run-with-idle-timer)
1103 ;; timer.el is not distributed with XEmacs
1104 ;; Notice that this does not (yet) pass the arguments, but they
1105 ;; are not used (yet) in bibtex.el. Fix if needed.
1106 (defun bibtex-run-with-idle-timer (secs repeat function &rest args)
1107 (start-itimer "bibtex" function secs (if repeat secs nil) t)))
1110 ;; Support for hideshow minor mode
1111 (defun bibtex-hs-forward-sexp (arg)
1112 "Replacement for `forward-sexp' to be used by `hs-minor-mode'."
1113 (if (< arg 0)
1114 (backward-sexp 1)
1115 (if (looking-at "@\\S(*\\s(")
1116 (progn
1117 (goto-char (match-end 0))
1118 (forward-char -1)
1119 (forward-sexp 1))
1120 (forward-sexp 1))))
1122 (add-to-list
1123 'hs-special-modes-alist
1124 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil))
1127 (defun bibtex-parse-association (parse-lhs parse-rhs)
1128 "Parse a string of the format <left-hand-side = right-hand-side>.
1129 The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
1130 substrings. These functions are expected to return nil if parsing is not
1131 successfull. If both functions return non-nil, a pair containing the returned
1132 values of the functions PARSE-LHS and PARSE-RHS is returned."
1133 (save-match-data
1134 (save-excursion
1135 (let ((left (funcall parse-lhs))
1136 right)
1137 (if (and left
1138 (looking-at "[ \t\n]*=[ \t\n]*")
1139 (goto-char (match-end 0))
1140 (setq right (funcall parse-rhs)))
1141 (cons left right))))))
1143 (defun bibtex-parse-field-name ()
1144 "Parse the field name stored in `bibtex-field-name-for-parsing'.
1145 If the field name is found, return a triple consisting of the position of the
1146 very first character of the match, the actual starting position of the name
1147 part and end position of the match. Move point to end of field name.
1148 If `bibtex-autoadd-commas' is non-nil add missing comma at end of preceeding
1149 BibTeX field as necessary."
1150 (cond ((looking-at ",[ \t\n]*")
1151 (let ((start (point)))
1152 (goto-char (match-end 0))
1153 (when (looking-at bibtex-field-name-for-parsing)
1154 (goto-char (match-end 0))
1155 (list start (match-beginning 0) (match-end 0)))))
1156 ;; Maybe add a missing comma.
1157 ((and bibtex-autoadd-commas
1158 (looking-at (concat "[ \t\n]*\\(?:" bibtex-field-name-for-parsing
1159 "\\)[ \t\n]*=")))
1160 (skip-chars-backward " \t\n")
1161 (insert ",")
1162 (forward-char -1)
1163 ;; Now try again.
1164 (bibtex-parse-field-name))))
1166 (defconst bibtex-braced-string-syntax-table
1167 (let ((st (make-syntax-table)))
1168 (modify-syntax-entry ?\{ "(}" st)
1169 (modify-syntax-entry ?\} "){" st)
1170 (modify-syntax-entry ?\[ "." st)
1171 (modify-syntax-entry ?\] "." st)
1172 (modify-syntax-entry ?\( "." st)
1173 (modify-syntax-entry ?\) "." st)
1174 (modify-syntax-entry ?\\ "." st)
1175 (modify-syntax-entry ?\" "." st)
1177 "Syntax-table to parse matched braces.")
1179 (defconst bibtex-quoted-string-syntax-table
1180 (let ((st (make-syntax-table)))
1181 (modify-syntax-entry ?\\ "\\" st)
1182 (modify-syntax-entry ?\" "\"" st)
1184 "Syntax-table to parse matched quotes.")
1186 (defun bibtex-parse-field-string ()
1187 "Parse a field string enclosed by braces or quotes.
1188 If a syntactically correct string is found, a pair containing the start and
1189 end position of the field string is returned, nil otherwise."
1190 (let ((end-point
1191 (or (and (eq (following-char) ?\")
1192 (save-excursion
1193 (with-syntax-table bibtex-quoted-string-syntax-table
1194 (forward-sexp 1))
1195 (point)))
1196 (and (eq (following-char) ?\{)
1197 (save-excursion
1198 (with-syntax-table bibtex-braced-string-syntax-table
1199 (forward-sexp 1))
1200 (point))))))
1201 (if end-point
1202 (cons (point) end-point))))
1204 (defun bibtex-parse-field-text ()
1205 "Parse the text part of a BibTeX field.
1206 The text part is either a string, or an empty string, or a constant followed
1207 by one or more <# (string|constant)> pairs. If a syntactically correct text
1208 is found, a pair containing the start and end position of the text is
1209 returned, nil otherwise. Move point to end of field text."
1210 (let ((starting-point (point))
1211 end-point failure boundaries)
1212 (while (not (or end-point failure))
1213 (cond ((looking-at bibtex-field-const)
1214 (goto-char (match-end 0)))
1215 ((setq boundaries (bibtex-parse-field-string))
1216 (goto-char (cdr boundaries)))
1217 ((setq failure t)))
1218 (if (not (looking-at "[ \t\n]*#[ \t\n]*"))
1219 (setq end-point (point))
1220 (goto-char (match-end 0))))
1221 (if (and (not failure)
1222 end-point)
1223 (cons starting-point end-point))))
1225 (defun bibtex-parse-field (name)
1226 "Parse a BibTeX field of regexp NAME.
1227 If a syntactically correct field is found, a pair containing the boundaries of
1228 the name and text parts of the field is returned."
1229 (let ((bibtex-field-name-for-parsing name))
1230 (bibtex-parse-association 'bibtex-parse-field-name
1231 'bibtex-parse-field-text)))
1233 (defun bibtex-search-forward-field (name &optional bound)
1234 "Search forward to find a field of name NAME.
1235 If a syntactically correct field is found, a pair containing the boundaries of
1236 the name and text parts of the field is returned. The search is limited by
1237 optional arg BOUND. If BOUND is t the search is limited by the end of the current
1238 entry. Do not move point."
1239 (save-match-data
1240 (save-excursion
1241 (unless (integer-or-marker-p bound)
1242 (setq bound (if bound
1243 (save-excursion (bibtex-end-of-entry))
1244 (point-max))))
1245 (let ((case-fold-search t)
1246 (bibtex-field-name-for-parsing name)
1247 boundaries temp-boundaries)
1248 (while (and (not boundaries)
1249 (< (point) bound)
1250 (search-forward "," bound t))
1251 (goto-char (match-beginning 0))
1252 (if (and (setq temp-boundaries
1253 (bibtex-parse-association 'bibtex-parse-field-name
1254 'bibtex-parse-field-text))
1255 (<= (cddr temp-boundaries) bound))
1256 (setq boundaries temp-boundaries)
1257 (forward-char 1)))
1258 boundaries))))
1260 (defun bibtex-search-backward-field (name &optional bound)
1261 "Search backward to find a field of name NAME.
1262 If a syntactically correct field is found, a pair containing the boundaries of
1263 the name and text parts of the field is returned. The search is limited by
1264 optional arg BOUND. If BOUND is t the search is limited by the beginning of the
1265 current entry. Do not move point."
1266 (save-match-data
1267 (save-excursion
1268 (unless (integer-or-marker-p bound)
1269 (setq bound (if bound
1270 (save-excursion (bibtex-beginning-of-entry))
1271 (point-min))))
1272 (let ((case-fold-search t)
1273 (bibtex-field-name-for-parsing name)
1274 boundaries temp-boundaries)
1275 (while (and (not boundaries)
1276 (>= (point) bound)
1277 (search-backward "," bound t))
1278 (if (setq temp-boundaries
1279 (bibtex-parse-association 'bibtex-parse-field-name
1280 'bibtex-parse-field-text))
1281 (setq boundaries temp-boundaries)))
1282 boundaries))))
1284 (defsubst bibtex-start-of-field (bounds)
1285 (nth 0 (car bounds)))
1286 (defsubst bibtex-start-of-name-in-field (bounds)
1287 (nth 1 (car bounds)))
1288 (defsubst bibtex-end-of-name-in-field (bounds)
1289 (nth 2 (car bounds)))
1290 (defsubst bibtex-end-of-field (bounds)
1291 (cddr bounds))
1292 (defsubst bibtex-start-of-text-in-field (bounds)
1293 (cadr bounds))
1294 (defsubst bibtex-end-of-text-in-field (bounds)
1295 (cddr bounds))
1297 (defun bibtex-name-in-field (bounds)
1298 "Get content of name in BibTeX field defined via BOUNDS."
1299 (buffer-substring-no-properties (nth 1 (car bounds))
1300 (nth 2 (car bounds))))
1302 (defun bibtex-text-in-field-bounds (bounds &optional remove-delim)
1303 "Get content of text in BibTeX field defined via BOUNDS.
1304 If optional arg REMOVE-DELIM is non-nil remove enclosing field delimiters
1305 if present."
1306 (let ((content (buffer-substring-no-properties (cadr bounds)
1307 (cddr bounds))))
1308 (if (and remove-delim
1309 (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content))
1310 (substring content (match-beginning 1) (match-end 1))
1311 content)))
1313 (defun bibtex-text-in-field (field &optional follow-crossref)
1314 "Get content of field FIELD of current BibTeX entry. Return nil if not found.
1315 If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1316 (save-excursion
1317 (save-restriction
1318 ;; We want to jump back and forth while searching FIELD
1319 (bibtex-narrow-to-entry)
1320 (goto-char (point-min))
1321 (let ((bounds (bibtex-search-forward-field field))
1322 crossref-field)
1323 (cond (bounds (bibtex-text-in-field-bounds bounds t))
1324 ((and follow-crossref
1325 (progn (goto-char (point-min))
1326 (setq bounds (bibtex-search-forward-field
1327 "\\(OPT\\)?crossref"))))
1328 (setq crossref-field (bibtex-text-in-field-bounds bounds t))
1329 (widen)
1330 (if (bibtex-find-crossref crossref-field)
1331 ;; Do not pass FOLLOW-CROSSREF because we want
1332 ;; to follow crossrefs only one level of recursion.
1333 (bibtex-text-in-field field))))))))
1335 (defun bibtex-parse-string-prefix ()
1336 "Parse the prefix part of a BibTeX string entry, including reference key.
1337 If the string prefix is found, return a triple consisting of the position of
1338 the very first character of the match, the actual starting position of the
1339 reference key and the end position of the match."
1340 (let ((case-fold-search t))
1341 (if (looking-at "^[ \t]*@string[ \t\n]*[({][ \t\n]*")
1342 (let ((start (point)))
1343 (goto-char (match-end 0))
1344 (when (looking-at bibtex-reference-key)
1345 (goto-char (match-end 0))
1346 (list start
1347 (match-beginning 0)
1348 (match-end 0)))))))
1350 (defun bibtex-parse-string-postfix ()
1351 "Parse the postfix part of a BibTeX string entry, including the text.
1352 If the string postfix is found, return a triple consisting of the position of
1353 the actual starting and ending position of the text and the very last
1354 character of the string entry. Move point past BibTeX string entry."
1355 (let* ((case-fold-search t)
1356 (bounds (bibtex-parse-field-text)))
1357 (when bounds
1358 (goto-char (cdr bounds))
1359 (when (looking-at "[ \t\n]*[})]")
1360 (goto-char (match-end 0))
1361 (list (car bounds)
1362 (cdr bounds)
1363 (match-end 0))))))
1365 (defun bibtex-parse-string ()
1366 "Parse a BibTeX string entry.
1367 If a syntactically correct entry is found, a pair containing the boundaries of
1368 the reference key and text parts of the entry is returned.
1369 Move point past BibTeX string entry."
1370 (bibtex-parse-association 'bibtex-parse-string-prefix
1371 'bibtex-parse-string-postfix))
1373 (defun bibtex-search-forward-string ()
1374 "Search forward to find a BibTeX string entry.
1375 If a syntactically correct entry is found, a pair containing the boundaries of
1376 the reference key and text parts of the string is returned. Do not move point."
1377 (save-excursion
1378 (save-match-data
1379 (let ((case-fold-search t)
1380 boundaries)
1381 (while (and (not boundaries)
1382 (search-forward-regexp
1383 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t))
1384 (goto-char (match-beginning 0))
1385 (unless (setq boundaries (bibtex-parse-string))
1386 (forward-char 1)))
1387 boundaries))))
1389 (defun bibtex-search-backward-string ()
1390 "Search backward to find a BibTeX string entry.
1391 If a syntactically correct entry is found, a pair containing the boundaries of
1392 the reference key and text parts of the field is returned. Do not move point."
1393 (save-excursion
1394 (save-match-data
1395 (let ((case-fold-search t)
1396 boundaries)
1397 (while (and (not boundaries)
1398 (search-backward-regexp
1399 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t))
1400 (goto-char (match-beginning 0))
1401 (setq boundaries (bibtex-parse-string)))
1402 boundaries))))
1404 (defun bibtex-reference-key-in-string (bounds)
1405 (buffer-substring-no-properties (nth 1 (car bounds))
1406 (nth 2 (car bounds))))
1408 (defun bibtex-text-in-string (bounds &optional remove-delim)
1409 "Get content of text in BibTeX string field defined via BOUNDS.
1410 If optional arg REMOVE-DELIM is non-nil remove enclosing field
1411 delimiters if present."
1412 (let ((content (buffer-substring-no-properties (nth 0 (cdr bounds))
1413 (nth 1 (cdr bounds)))))
1414 (if (and remove-delim
1415 (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content))
1416 (substring content (match-beginning 1) (match-end 1))
1417 content)))
1419 (defsubst bibtex-start-of-text-in-string (bounds)
1420 (nth 0 (cdr bounds)))
1421 (defsubst bibtex-end-of-text-in-string (bounds)
1422 (nth 1 (cdr bounds)))
1423 (defsubst bibtex-end-of-string (bounds)
1424 (nth 2 (cdr bounds)))
1426 (defsubst bibtex-type-in-head ()
1427 "Extract BibTeX type in head."
1428 ;; ignore @
1429 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head))
1430 (match-end bibtex-type-in-head)))
1432 (defun bibtex-key-in-head (&optional empty)
1433 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
1434 (if (match-beginning bibtex-key-in-head)
1435 (buffer-substring-no-properties (match-beginning bibtex-key-in-head)
1436 (match-end bibtex-key-in-head))
1437 empty))
1439 ;; Helper Functions
1441 (defun bibtex-delete-whitespace ()
1442 "Delete all whitespace starting at point."
1443 (if (looking-at "[ \t\n]+")
1444 (delete-region (point) (match-end 0))))
1446 (defun bibtex-current-line ()
1447 "Compute line number of point regardless whether the buffer is narrowed."
1448 (+ (count-lines 1 (point))
1449 (if (equal (current-column) 0) 1 0)))
1451 (defun bibtex-member-of-regexp (string list)
1452 "Return non-nil if STRING is exactly matched by an element of LIST.
1453 The value is actually the tail of LIST whose car matches STRING."
1454 (let (case-fold-search)
1455 (while (and list
1456 (not (string-match (concat "\\`\\(?:" (car list) "\\)\\'") string)))
1457 (setq list (cdr list)))
1458 list))
1460 (defun bibtex-skip-to-valid-entry (&optional backward)
1461 "Unless at beginning of a valid BibTeX entry, move point to beginning of the
1462 next valid one. With optional argument BACKWARD non-nil, move backward to
1463 beginning of previous valid one. A valid entry is a syntactical correct one
1464 with type contained in `bibtex-entry-field-alist' or, if
1465 `bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
1466 entry. Return buffer position of beginning and ending of entry if a valid
1467 entry is found, nil otherwise."
1468 (interactive "P")
1469 (let ((case-fold-search t)
1470 found)
1471 (while (not (or found (if backward (bobp) (eobp))))
1472 (let ((pnt (point))
1473 bounds)
1474 (cond ((or (and (looking-at bibtex-valid-entry-re)
1475 (setq found (bibtex-search-entry nil nil t))
1476 (equal (match-beginning 0) pnt))
1477 (and (not bibtex-sort-ignore-string-entries)
1478 (setq bounds (bibtex-parse-string))
1479 (setq found (cons (bibtex-start-of-field bounds)
1480 (bibtex-end-of-string bounds)))))
1481 (goto-char pnt))
1482 (backward
1483 (if (re-search-backward "^[ \t]*\\(@\\)" nil 'move)
1484 (goto-char (match-beginning 1))))
1485 (t (if (re-search-forward "\n[ \t]*@" nil 'move)
1486 (forward-char -1))))))
1487 found))
1489 (defun bibtex-map-entries (fun)
1490 "Call FUN for each BibTeX entry starting with the current.
1491 Do this to the end of the file. FUN is called with three arguments, the key of
1492 the entry and the buffer positions (marker) of beginning and end of entry.
1493 Point is inside the entry. If `bibtex-sort-ignore-string-entries' is non-nil,
1494 FUN will not be called for @String entries."
1495 (let ((case-fold-search t))
1496 (bibtex-beginning-of-entry)
1497 (while (re-search-forward bibtex-entry-head nil t)
1498 (let ((entry-type (bibtex-type-in-head))
1499 (key (bibtex-key-in-head ""))
1500 (beg (copy-marker (match-beginning 0)))
1501 (end (copy-marker (save-excursion (bibtex-end-of-entry)))))
1502 (save-excursion
1503 (if (or (and (not bibtex-sort-ignore-string-entries)
1504 (string-equal "string" (downcase entry-type)))
1505 (assoc-string entry-type bibtex-entry-field-alist t))
1506 (funcall fun key beg end)))
1507 (goto-char end)))))
1509 (defun bibtex-progress-message (&optional flag interval)
1510 "Echo a message about progress of current buffer.
1511 If FLAG is a string, the message is initialized (in this case a
1512 value for INTERVAL may be given as well (if not this is set to 5)).
1513 If FLAG is done, the message is deinitialized.
1514 If FLAG is nil, a message is echoed if point was incremented at least
1515 `bibtex-progress-interval' percent since last message was echoed."
1516 (cond ((stringp flag)
1517 (setq bibtex-progress-lastmes flag)
1518 (setq bibtex-progress-interval (or interval 5)
1519 bibtex-progress-lastperc 0))
1520 ((equal flag 'done)
1521 (message "%s (done)" bibtex-progress-lastmes)
1522 (setq bibtex-progress-lastmes nil))
1524 (let* ((size (- (point-max) (point-min)))
1525 (perc (if (= size 0)
1527 (/ (* 100 (- (point) (point-min))) size))))
1528 (when (>= perc (+ bibtex-progress-lastperc
1529 bibtex-progress-interval))
1530 (setq bibtex-progress-lastperc perc)
1531 (message "%s (%d%%)" bibtex-progress-lastmes perc))))))
1533 (defun bibtex-field-left-delimiter ()
1534 "Return a string dependent on `bibtex-field-delimiters'."
1535 (if (equal bibtex-field-delimiters 'braces)
1537 "\""))
1539 (defun bibtex-field-right-delimiter ()
1540 "Return a string dependent on `bibtex-field-delimiters'."
1541 (if (equal bibtex-field-delimiters 'braces)
1543 "\""))
1545 (defun bibtex-entry-left-delimiter ()
1546 "Return a string dependent on `bibtex-field-delimiters'."
1547 (if (equal bibtex-entry-delimiters 'braces)
1549 "("))
1551 (defun bibtex-entry-right-delimiter ()
1552 "Return a string dependent on `bibtex-field-delimiters'."
1553 (if (equal bibtex-entry-delimiters 'braces)
1555 ")"))
1557 (defun bibtex-search-entry (empty-head &optional bound noerror backward)
1558 "Search for a BibTeX entry (maybe without reference key if EMPTY-HEAD is t).
1559 BOUND and NOERROR are exactly as in `re-search-forward'. If BACKWARD
1560 is non-nil, search is done in reverse direction. Point is moved past the
1561 closing delimiter (at the beginning of entry if BACKWARD is non-nil).
1562 Return a cons pair with buffer positions of beginning and end of entry.
1563 After call to this function MATCH-BEGINNING and MATCH-END functions
1564 are defined, but only for the head part of the entry
1565 \(especially (match-end 0) just gives the end of the head part)."
1566 (let ((pnt (point))
1567 (entry-head-re (if empty-head
1568 bibtex-entry-maybe-empty-head
1569 bibtex-entry-head)))
1570 (if backward
1571 (let (found)
1572 (while (and (not found)
1573 (re-search-backward entry-head-re bound noerror))
1574 (setq found (bibtex-search-entry empty-head pnt t)))
1575 (if found
1576 (progn (goto-char (match-beginning 0))
1577 found)
1578 (cond ((equal noerror nil)
1579 ;; yell
1580 (error "Backward search of BibTeX entry failed"))
1581 ((equal noerror t)
1582 ;; don't move
1583 (goto-char pnt)))
1584 nil))
1585 (let ((limit (or bound (point-max)))
1586 found)
1587 (while (and (not found)
1588 (re-search-forward entry-head-re bound noerror))
1589 (save-match-data
1590 (let ((entry-closer
1591 (if (save-excursion
1592 (goto-char (match-end bibtex-type-in-head))
1593 (looking-at "[ \t]*("))
1594 ;; entry opened with parenthesis
1596 ?\}))
1597 (infix-start (point))
1598 finished bounds)
1599 (while (not finished)
1600 (skip-chars-forward " \t\n" limit)
1601 (if (and (setq bounds (bibtex-parse-field bibtex-field-name))
1602 (<= (bibtex-end-of-field bounds) limit))
1603 (setq infix-start (bibtex-end-of-field bounds))
1604 (setq finished t))
1605 (goto-char infix-start))
1606 ;; This matches the infix* part. The AND construction assures
1607 ;; that BOUND is respected.
1608 (when (and (looking-at bibtex-entry-postfix)
1609 (eq (char-before (match-end 0)) entry-closer)
1610 (<= (match-end 0) limit))
1611 (goto-char (match-end 0))
1612 (setq found t)))))
1613 (if found
1614 (cons (match-beginning 0) (point))
1615 (cond ((not noerror)
1616 ;; yell
1617 (error "Search of BibTeX entry failed"))
1618 ((equal noerror t)
1619 ;; don't move
1620 (goto-char pnt)))
1621 nil)))))
1623 (defun bibtex-flash-head ()
1624 "Flash at BibTeX entry head before point, if exists."
1625 (let ((case-fold-search t)
1626 flash)
1627 (cond ((re-search-backward bibtex-entry-head nil t)
1628 (goto-char (match-beginning bibtex-type-in-head))
1629 (setq flash (match-end bibtex-key-in-head)))
1631 (end-of-line)
1632 (skip-chars-backward " \t")
1633 (setq flash (point))
1634 (beginning-of-line)
1635 (skip-chars-forward " \t")))
1636 (if (pos-visible-in-window-p (point))
1637 (sit-for 1)
1638 (message "From: %s"
1639 (buffer-substring (point) flash)))))
1641 (defun bibtex-make-optional-field (field)
1642 "Make an optional field named FIELD in current BibTeX entry."
1643 (if (consp field)
1644 (bibtex-make-field (cons (concat "OPT" (car field)) (cdr field)))
1645 (bibtex-make-field (concat "OPT" field))))
1647 (defun bibtex-move-outside-of-entry ()
1648 "Make sure point is outside of a BibTeX entry."
1649 (let ((orig-point (point)))
1650 (bibtex-end-of-entry)
1651 (when (< (point) orig-point)
1652 ;; We moved backward, so we weren't inside an entry to begin with.
1653 ;; Leave point at the beginning of a line, and preferably
1654 ;; at the beginning of a paragraph.
1655 (goto-char orig-point)
1656 (beginning-of-line 1)
1657 (unless (= ?\n (char-before (1- (point))))
1658 (re-search-forward "^[ \t]*[@\n]" nil 'move)
1659 (backward-char 1)))
1660 (skip-chars-forward " \t\n")))
1662 (defun bibtex-beginning-of-first-entry ()
1663 "Go to the beginning of the first BibTeX entry in buffer. Return point."
1664 (goto-char (point-min))
1665 (if (re-search-forward "^[ \t]*@" nil 'move)
1666 (beginning-of-line))
1667 (point))
1669 (defun bibtex-beginning-of-last-entry ()
1670 "Go to the beginning of the last BibTeX entry in buffer."
1671 (goto-char (point-max))
1672 (if (re-search-backward "^[ \t]*@" nil 'move)
1673 (beginning-of-line))
1674 (point))
1676 (defun bibtex-inside-field ()
1677 "Try to avoid point being at end of a BibTeX field."
1678 (end-of-line)
1679 (skip-chars-backward " \t")
1680 (if (= (preceding-char) ?,)
1681 (forward-char -2))
1682 (if (or (= (preceding-char) ?})
1683 (= (preceding-char) ?\"))
1684 (forward-char -1)))
1686 (defun bibtex-enclosing-field (&optional noerr)
1687 "Search for BibTeX field enclosing point. Point moves to end of field.
1688 Use `match-beginning' and `match-end' to parse the field. If NOERR is non-nil,
1689 no error is signalled. In this case, bounds are returned on success,
1690 nil otherwise."
1691 (let ((bounds (bibtex-search-backward-field bibtex-field-name t)))
1692 (if (and bounds
1693 (<= (bibtex-start-of-field bounds) (point))
1694 (>= (bibtex-end-of-field bounds) (point)))
1695 bounds
1696 (unless noerr
1697 (error "Can't find enclosing BibTeX field")))))
1699 (defun bibtex-enclosing-entry-maybe-empty-head ()
1700 "Search for BibTeX entry enclosing point. Move point to end of entry.
1701 Beginning (but not end) of entry is given by (`match-beginning' 0)."
1702 (let ((case-fold-search t)
1703 (old-point (point)))
1704 (unless (re-search-backward bibtex-entry-maybe-empty-head nil t)
1705 (goto-char old-point)
1706 (error "Can't find beginning of enclosing BibTeX entry"))
1707 (goto-char (match-beginning bibtex-type-in-head))
1708 (unless (bibtex-search-entry t nil t)
1709 (goto-char old-point)
1710 (error "Can't find end of enclosing BibTeX entry"))))
1712 (defun bibtex-insert-current-kill (n)
1713 (if (not bibtex-last-kill-command)
1714 (error "BibTeX kill ring is empty")
1715 (let* ((kr (if (equal bibtex-last-kill-command 'field)
1716 'bibtex-field-kill-ring
1717 'bibtex-entry-kill-ring))
1718 (kryp (if (equal bibtex-last-kill-command 'field)
1719 'bibtex-field-kill-ring-yank-pointer
1720 'bibtex-entry-kill-ring-yank-pointer))
1721 (ARGth-kill-element (nthcdr (mod (- n (length (eval kryp)))
1722 (length (eval kr)))
1723 (eval kr)))
1724 (current (car (set kryp ARGth-kill-element))))
1725 (cond
1726 ((equal bibtex-last-kill-command 'field)
1727 (let (bibtex-help-message)
1728 (bibtex-find-text nil t)
1729 (if (looking-at "[}\"]")
1730 (forward-char)))
1731 (set-mark (point))
1732 (message "Mark set")
1733 (bibtex-make-field (list (elt current 1) nil (elt current 2)) t))
1734 ((equal bibtex-last-kill-command 'entry)
1735 (if (not (eobp))
1736 (bibtex-beginning-of-entry))
1737 (set-mark (point))
1738 (message "Mark set")
1739 (insert (elt current 1)))
1741 (error "Unknown tag field: %s. Please submit a bug report"
1742 bibtex-last-kill-command))))))
1744 (defun bibtex-assoc-regexp (regexp alist)
1745 "Return non-nil if REGEXP matches the car of an element of ALIST.
1746 The value is actually the element of ALIST matched by REGEXP.
1747 Case is ignored if `case-fold-search' is non-nil in the current buffer."
1748 (while (and alist
1749 (not (string-match regexp (caar alist))))
1750 (setq alist (cdr alist)))
1751 (car alist))
1753 (defun bibtex-format-entry ()
1754 "Helper function for `bibtex-clean-entry'.
1755 Formats current entry according to variable `bibtex-entry-format'."
1756 (save-excursion
1757 (save-restriction
1758 (bibtex-narrow-to-entry)
1759 (let ((case-fold-search t)
1760 (format (if (equal bibtex-entry-format t)
1761 '(realign opts-or-alts required-fields
1762 numerical-fields
1763 last-comma page-dashes delimiters
1764 unify-case inherit-booktitle)
1765 bibtex-entry-format))
1766 crossref-key bounds alternatives-there non-empty-alternative
1767 entry-list req-field-list field-done field-list)
1769 ;; identify entry type
1770 (goto-char (point-min))
1771 (re-search-forward bibtex-entry-type)
1772 (let ((beg-type (1+ (match-beginning 0)))
1773 (end-type (match-end 0)))
1774 (setq entry-list (assoc-string (buffer-substring-no-properties
1775 beg-type end-type)
1776 bibtex-entry-field-alist
1779 ;; unify case of entry name
1780 (when (memq 'unify-case format)
1781 (delete-region beg-type end-type)
1782 (insert (car entry-list)))
1784 ;; update left entry delimiter
1785 (when (memq 'delimiters format)
1786 (goto-char end-type)
1787 (skip-chars-forward " \t\n")
1788 (delete-char 1)
1789 (insert (bibtex-entry-left-delimiter))))
1791 ;; determine if entry has crossref field and if at least
1792 ;; one alternative is non-empty
1793 (goto-char (point-min))
1794 (let* ((fields-alist (bibtex-parse-entry))
1795 (case-fold-search t)
1796 (field (bibtex-assoc-regexp "\\`\\(OPT\\)?crossref\\'"
1797 fields-alist)))
1798 (setq crossref-key (and field
1799 (not (string-match bibtex-empty-field-re
1800 (cdr field)))
1801 (cdr field))
1802 req-field-list (if crossref-key
1803 (nth 0 (nth 2 entry-list)) ; crossref part
1804 (nth 0 (nth 1 entry-list)))) ; required part
1806 (dolist (rfield req-field-list)
1807 (when (nth 3 rfield) ; we should have an alternative
1808 (setq alternatives-there t
1809 field (bibtex-assoc-regexp
1810 (concat "\\`\\(ALT\\)?" (car rfield) "\\'")
1811 fields-alist))
1812 (if (and field
1813 (not (string-match bibtex-empty-field-re
1814 (cdr field))))
1815 (cond ((not non-empty-alternative)
1816 (setq non-empty-alternative t))
1817 ((memq 'required-fields format)
1818 (error "More than one non-empty alternative.")))))))
1820 (if (and alternatives-there
1821 (not non-empty-alternative)
1822 (memq 'required-fields format))
1823 (error "All alternatives are empty"))
1825 ;; process all fields
1826 (goto-char (point-min))
1827 (while (setq bounds (bibtex-search-forward-field bibtex-field-name))
1828 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))
1829 (end-field (copy-marker (bibtex-end-of-field bounds)))
1830 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds)))
1831 (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))
1832 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds)))
1833 (end-text (copy-marker (bibtex-end-of-text-in-field bounds)))
1834 (opt-alt (string-match "OPT\\|ALT"
1835 (buffer-substring-no-properties beg-name (+ beg-name 3))))
1836 (field-name (buffer-substring-no-properties
1837 (if opt-alt (+ beg-name 3) beg-name) end-name))
1838 (empty-field (string-match bibtex-empty-field-re
1839 (buffer-substring-no-properties beg-field end-field)))
1840 deleted)
1842 ;; We have more elegant high-level functions for several
1843 ;; tasks done by bibtex-format-entry. However, they contain
1844 ;; quite some redundancy compared with what we need to do
1845 ;; anyway. So for speed-up we avoid using them.
1847 (if (memq 'opts-or-alts format)
1848 (cond ((and empty-field
1849 (or opt-alt
1850 (let ((field (assoc-string
1851 field-name req-field-list t)))
1852 (or (not field) ; OPT field
1853 (nth 3 field))))) ; ALT field
1854 ;; Either it is an empty ALT field. Then we have checked
1855 ;; already that we have one non-empty alternative. Or it
1856 ;; is an empty OPT field that we do not miss anyway.
1857 ;; So we can safely delete this field.
1858 (delete-region beg-field end-field)
1859 (setq deleted t))
1860 ;; otherwise: not empty, delete "OPT" or "ALT"
1861 (opt-alt
1862 (goto-char beg-name)
1863 (delete-char 3))))
1865 (unless deleted
1866 (push field-name field-list)
1868 ;; remove delimiters from purely numerical fields
1869 (when (and (memq 'numerical-fields format)
1870 (progn (goto-char beg-text)
1871 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1872 (goto-char end-text)
1873 (delete-char -1)
1874 (goto-char beg-text)
1875 (delete-char 1))
1877 ;; update delimiters
1878 (when (memq 'delimiters format)
1879 (goto-char beg-text)
1880 (when (looking-at "[{\"]")
1881 (delete-char 1)
1882 (insert (bibtex-field-left-delimiter)))
1883 (goto-char (1- (marker-position end-text)))
1884 (when (looking-at "[}\"]")
1885 (delete-char 1)
1886 (insert (bibtex-field-right-delimiter))))
1888 ;; update page dashes
1889 (if (and (memq 'page-dashes format)
1890 (string-match "\\`\\(OPT\\)?pages\\'" field-name)
1891 (progn (goto-char beg-text)
1892 (looking-at
1893 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1894 (replace-match "\\1-\\2"))
1896 ;; use book title of crossref'd entry
1897 (if (and (memq 'inherit-booktitle format)
1898 empty-field
1899 (equal (downcase field-name) "booktitle")
1900 crossref-key)
1901 (let ((title (save-restriction
1902 (widen)
1903 (if (bibtex-find-entry crossref-key)
1904 (bibtex-text-in-field "title")))))
1905 (when title
1906 (setq empty-field nil)
1907 (goto-char (1+ beg-text))
1908 (insert title))))
1910 ;; Use booktitle to set a missing title.
1911 (if (and empty-field
1912 (equal (downcase field-name) "title"))
1913 (let ((booktitle (bibtex-text-in-field "booktitle")))
1914 (when booktitle
1915 (setq empty-field nil)
1916 (goto-char (1+ beg-text))
1917 (insert booktitle))))
1919 ;; if empty field, complain
1920 (if (and empty-field
1921 (memq 'required-fields format)
1922 (assoc-string field-name req-field-list t))
1923 (error "Mandatory field `%s' is empty" field-name))
1925 ;; unify case of field name
1926 (if (memq 'unify-case format)
1927 (let ((fname (car (assoc-string
1928 field-name
1929 (append (nth 0 (nth 1 entry-list))
1930 (nth 1 (nth 1 entry-list))
1931 bibtex-user-optional-fields)
1932 t))))
1933 (if fname
1934 (progn
1935 (delete-region beg-name end-name)
1936 (goto-char beg-name)
1937 (insert fname))
1938 ;; there are no rules we could follow
1939 (downcase-region beg-name end-name))))
1941 ;; update point
1942 (goto-char end-field))))
1944 ;; check whether all required fields are present
1945 (if (memq 'required-fields format)
1946 (let ((found 0) altlist)
1947 (dolist (fname req-field-list)
1948 (if (nth 3 fname)
1949 (push (car fname) altlist))
1950 (unless (or (member (car fname) field-list)
1951 (nth 3 fname))
1952 (error "Mandatory field `%s' is missing" (car fname))))
1953 (when altlist
1954 (dolist (fname altlist)
1955 (if (member fname field-list)
1956 (setq found (1+ found))))
1957 (cond ((= found 0)
1958 (error "Alternative mandatory field `%s' is missing"
1959 altlist))
1960 ((> found 1)
1961 (error "Alternative fields `%s' are defined %s times"
1962 altlist found))))))
1964 ;; update point
1965 (if (looking-at (bibtex-field-right-delimiter))
1966 (forward-char))
1968 ;; update comma after last field
1969 (if (memq 'last-comma format)
1970 (cond ((and bibtex-comma-after-last-field
1971 (not (looking-at ",")))
1972 (insert ","))
1973 ((and (not bibtex-comma-after-last-field)
1974 (looking-at ","))
1975 (delete-char 1))))
1977 ;; update right entry delimiter
1978 (if (looking-at ",")
1979 (forward-char))
1980 (when (memq 'delimiters format)
1981 (skip-chars-forward " \t\n")
1982 (delete-char 1)
1983 (insert (bibtex-entry-right-delimiter)))
1985 ;; fill entry
1986 (if (memq 'realign format)
1987 (bibtex-fill-entry))))))
1990 (defun bibtex-autokey-abbrev (string len)
1991 "Return an abbreviation of STRING with at least LEN characters.
1992 If LEN is positive the abbreviation is terminated only after a consonant
1993 or at the word end. If LEN is negative the abbreviation is strictly
1994 enforced using abs (LEN) characters. If LEN is not a number, STRING
1995 is returned unchanged."
1996 (cond ((or (not (numberp len))
1997 (<= (length string) (abs len)))
1998 string)
1999 ((equal len 0)
2001 ((< len 0)
2002 (substring string 0 (abs len)))
2003 (t (let* ((case-fold-search t)
2004 (abort-char (string-match "[^aeiou]" string (1- len))))
2005 (if abort-char
2006 (substring string 0 (1+ abort-char))
2007 string)))))
2009 (defun bibtex-autokey-get-field (field &optional change-list)
2010 "Get content of BibTeX field FIELD. Return empty string if not found.
2011 Optional arg CHANGE-LIST is a list of substitution patterns that is
2012 applied to the content of FIELD. It is an alist with pairs
2013 \(OLD-REGEXP . NEW-STRING\)."
2014 (let ((content (bibtex-text-in-field field bibtex-autokey-use-crossref))
2015 case-fold-search)
2016 (unless content (setq content ""))
2017 (dolist (pattern change-list content)
2018 (setq content (replace-regexp-in-string (car pattern)
2019 (cdr pattern)
2020 content)))))
2022 (defun bibtex-autokey-get-names ()
2023 "Get contents of the name field of the current entry.
2024 Do some modifications based on `bibtex-autokey-name-change-strings'
2025 and return results as a list."
2026 (let ((case-fold-search t))
2027 (mapcar 'bibtex-autokey-demangle-name
2028 (split-string (bibtex-autokey-get-field
2029 "author\\|editor"
2030 bibtex-autokey-name-change-strings)
2031 "[ \t\n]+and[ \t\n]+"))))
2033 (defun bibtex-autokey-demangle-name (fullname)
2034 "Get the last part from a well-formed name and perform abbreviations."
2035 (let* (case-fold-search
2036 (name (cond ((string-match "\\([A-Z][^, ]*\\)[^,]*," fullname)
2037 ;; Name is of the form "von Last, First" or
2038 ;; "von Last, Jr, First"
2039 ;; --> Take the first capital part before the comma
2040 (match-string 1 fullname))
2041 ((string-match "\\([^, ]*\\)," fullname)
2042 ;; Strange name: we have a comma, but nothing capital
2043 ;; So we accept even lowercase names
2044 (match-string 1 fullname))
2045 ((string-match "\\(\\<[a-z][^ ]* +\\)+\\([A-Z][^ ]*\\)"
2046 fullname)
2047 ;; name is of the form "First von Last", "von Last",
2048 ;; "First von von Last", or "d'Last"
2049 ;; --> take the first capital part after the "von" parts
2050 (match-string 2 fullname))
2051 ((string-match "\\([^ ]+\\) *\\'" fullname)
2052 ;; name is of the form "First Middle Last" or "Last"
2053 ;; --> take the last token
2054 (match-string 1 fullname))
2055 (t (error "Name `%s' is incorrectly formed" fullname)))))
2056 (bibtex-autokey-abbrev
2057 (funcall bibtex-autokey-name-case-convert name)
2058 bibtex-autokey-name-length)))
2060 (defun bibtex-autokey-get-title ()
2061 "Get title field contents up to a terminator."
2062 (let ((titlestring
2063 (bibtex-autokey-get-field "title"
2064 bibtex-autokey-titleword-change-strings)))
2065 ;; ignore everything past a terminator
2066 (let ((case-fold-search t))
2067 (dolist (terminator bibtex-autokey-title-terminators)
2068 (if (string-match terminator titlestring)
2069 (setq titlestring (substring titlestring 0 (match-beginning 0))))))
2070 ;; gather words from titlestring into a list. Ignore
2071 ;; specific words and use only a specific amount of words.
2072 (let ((counter 0)
2073 case-fold-search titlewords titlewords-extra titleword end-match)
2074 (while (and (or (not (numberp bibtex-autokey-titlewords))
2075 (< counter (+ bibtex-autokey-titlewords
2076 bibtex-autokey-titlewords-stretch)))
2077 (string-match "\\b\\w+" titlestring))
2078 (setq end-match (match-end 0)
2079 titleword (substring titlestring
2080 (match-beginning 0) end-match))
2081 (unless (bibtex-member-of-regexp titleword
2082 bibtex-autokey-titleword-ignore)
2083 (setq titleword
2084 (funcall bibtex-autokey-titleword-case-convert titleword))
2085 (if (or (not (numberp bibtex-autokey-titlewords))
2086 (< counter bibtex-autokey-titlewords))
2087 (setq titlewords (append titlewords (list titleword)))
2088 (setq titlewords-extra
2089 (append titlewords-extra (list titleword))))
2090 (setq counter (1+ counter)))
2091 (setq titlestring (substring titlestring end-match)))
2092 (unless (string-match "\\b\\w+" titlestring)
2093 (setq titlewords (append titlewords titlewords-extra)))
2094 (mapcar 'bibtex-autokey-demangle-title titlewords))))
2096 (defun bibtex-autokey-demangle-title (titleword)
2097 "Do some abbreviations on TITLEWORD.
2098 The rules are defined in `bibtex-autokey-titleword-abbrevs'
2099 and `bibtex-autokey-titleword-length'."
2100 (let ((case-folde-search t)
2101 (alist bibtex-autokey-titleword-abbrevs))
2102 (while (and alist
2103 (not (string-match (concat "\\`\\(?:" (caar alist) "\\)\\'")
2104 titleword)))
2105 (setq alist (cdr alist)))
2106 (if alist
2107 (cdar alist)
2108 (bibtex-autokey-abbrev titleword
2109 bibtex-autokey-titleword-length))))
2111 (defun bibtex-generate-autokey ()
2112 "Generate automatically a key from the author/editor and the title field.
2113 This will only work for entries where each field begins on a separate line.
2114 The generation algorithm works as follows:
2115 1. Use the value of `bibtex-autokey-prefix-string' as a prefix.
2116 2. If there is a non-empty author (preferred) or editor field,
2117 use it as the name part of the key.
2118 3. Change any substring found in
2119 `bibtex-autokey-name-change-strings' to the corresponding new
2120 one (see documentation of this variable for further detail).
2121 4. For every of at least first `bibtex-autokey-names' names in
2122 the name field, determine the last name. If there are maximal
2123 `bibtex-autokey-names' + `bibtex-autokey-names-stretch'
2124 names, all names are used.
2125 5. From every last name, take at least `bibtex-autokey-name-length'
2126 characters (abort only after a consonant or at a word end).
2127 6. Convert all last names according to the conversion function
2128 `bibtex-autokey-name-case-convert'.
2129 7. Build the name part of the key by concatenating all
2130 abbreviated last names with the string
2131 `bibtex-autokey-name-separator' between any two. If there are
2132 more names than are used in the name part, prepend the string
2133 contained in `bibtex-autokey-additional-names'.
2134 8. Build the year part of the key by truncating the contents of
2135 the year field to the rightmost `bibtex-autokey-year-length'
2136 digits (useful values are 2 and 4). If the year field (or any
2137 other field required to generate the key) is absent, but the entry
2138 has a valid crossref field and the variable
2139 `bibtex-autokey-use-crossref' is non-nil, use the field of the
2140 crossreferenced entry instead.
2141 9. For the title part of the key change the contents of the
2142 title field of the entry according to
2143 `bibtex-autokey-titleword-change-strings' to the
2144 corresponding new one (see documentation of this variable for
2145 further detail).
2146 10. Abbreviate the result to the string up to (but not including)
2147 the first occurrence of a regexp matched by the items of
2148 `bibtex-autokey-title-terminators' and delete those words which
2149 appear in `bibtex-autokey-titleword-ignore'.
2150 Build the title part of the key by using at least the first
2151 `bibtex-autokey-titlewords' words from this
2152 abbreviated title. If the abbreviated title ends after
2153 maximal `bibtex-autokey-titlewords' +
2154 `bibtex-autokey-titlewords-stretch' words, all
2155 words from the abbreviated title are used.
2156 11. Convert all used titlewords according to the conversion function
2157 `bibtex-autokey-titleword-case-convert'.
2158 12. For every used title word that appears in
2159 `bibtex-autokey-titleword-abbrevs' use the corresponding
2160 abbreviation (see documentation of this variable for further
2161 detail).
2162 13. From every title word not generated by an abbreviation, take
2163 at least `bibtex-autokey-titleword-length' characters (abort
2164 only after a consonant or at a word end).
2165 14. Build the title part of the key by concatenating all
2166 abbreviated title words with the string
2167 `bibtex-autokey-titleword-separator' between any two.
2168 15. At least, to get the key, concatenate
2169 `bibtex-autokey-prefix-string', the name part, the year part
2170 and the title part with `bibtex-autokey-name-year-separator'
2171 between the name part and the year part if both are non-empty
2172 and `bibtex-autokey-year-title-separator' between the year
2173 part and the title part if both are non-empty. If the year
2174 part is empty, but not the other two parts,
2175 `bibtex-autokey-year-title-separator' is used as well.
2176 16. If the value of `bibtex-autokey-before-presentation-function'
2177 is non-nil, it must be a function taking one argument. This
2178 function is then called with the generated key as the
2179 argument. The return value of this function (a string) is
2180 used as the key.
2181 17. If the value of `bibtex-autokey-edit-before-use' is non-nil,
2182 the key is then presented in the minibuffer to the user,
2183 where it can be edited. The key given by the user is then
2184 used."
2185 (let* ((name-etal "")
2186 (namelist
2187 (let ((nl (bibtex-autokey-get-names))
2188 nnl)
2189 (if (or (not (numberp bibtex-autokey-names))
2190 (<= (length nl)
2191 (+ bibtex-autokey-names
2192 bibtex-autokey-names-stretch)))
2194 (setq name-etal bibtex-autokey-additional-names)
2195 (while (< (length nnl) bibtex-autokey-names)
2196 (setq nnl (append nnl (list (car nl)))
2197 nl (cdr nl)))
2198 nnl)))
2199 (namepart (concat (mapconcat 'identity
2200 namelist
2201 bibtex-autokey-name-separator)
2202 name-etal))
2203 (yearfield (bibtex-autokey-get-field "year"))
2204 (yearpart (if (equal yearfield "")
2206 (substring yearfield
2207 (- (length yearfield)
2208 bibtex-autokey-year-length))))
2209 (titlepart (mapconcat 'identity
2210 (bibtex-autokey-get-title)
2211 bibtex-autokey-titleword-separator))
2212 (autokey (concat bibtex-autokey-prefix-string
2213 namepart
2214 (unless (or (equal namepart "")
2215 (equal yearpart ""))
2216 bibtex-autokey-name-year-separator)
2217 yearpart
2218 (unless (or (and (equal namepart "")
2219 (equal yearpart ""))
2220 (equal titlepart ""))
2221 bibtex-autokey-year-title-separator)
2222 titlepart)))
2223 (if bibtex-autokey-before-presentation-function
2224 (funcall bibtex-autokey-before-presentation-function autokey)
2225 autokey)))
2228 (defun bibtex-parse-keys (&optional add abortable verbose)
2229 "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2230 The buffer might possibly be restricted.
2231 Find both entry keys and crossref entries.
2232 If ADD is non-nil add the new keys to `bibtex-reference-keys' instead of
2233 simply resetting it. If ADD is an alist of keys, also add ADD to
2234 `bibtex-reference-keys'. If ABORTABLE is non-nil abort on user
2235 input. If VERBOSE is non-nil gives messages about progress.
2236 Return alist of keys if parsing was completed, `aborted' otherwise."
2237 (let ((reference-keys (if (and add
2238 (listp bibtex-reference-keys))
2239 bibtex-reference-keys)))
2240 (if (listp add)
2241 (dolist (key add)
2242 (unless (assoc (car key) reference-keys)
2243 (push key reference-keys))))
2244 (save-excursion
2245 (save-match-data
2246 (if verbose
2247 (bibtex-progress-message
2248 (concat (buffer-name) ": parsing reference keys")))
2249 (catch 'userkey
2250 (goto-char (point-min))
2251 (if bibtex-parse-keys-fast
2252 (let ((case-fold-search t)
2253 (re (concat bibtex-entry-head "\\|"
2254 ",[ \t\n]*crossref[ \t\n]*=[ \t\n]*"
2255 "\\(\"[^\"]*\"\\|{[^}]*}\\)[ \t\n]*[,})]")))
2256 (while (re-search-forward re nil t)
2257 (if (and abortable (input-pending-p))
2258 ;; user has aborted by typing a key --> return `aborted'
2259 (throw 'userkey 'aborted))
2260 (let ((key (cond ((match-end 3)
2261 ;; This is a crossref.
2262 (buffer-substring-no-properties
2263 (1+ (match-beginning 3)) (1- (match-end 3))))
2264 ((assoc-string (bibtex-type-in-head)
2265 bibtex-entry-field-alist t)
2266 ;; This is an entry.
2267 (match-string-no-properties bibtex-key-in-head)))))
2268 (if (and (stringp key)
2269 (not (assoc key reference-keys)))
2270 (push (list key) reference-keys)))))
2272 (let (;; ignore @String entries because they are handled
2273 ;; separately by bibtex-parse-strings
2274 (bibtex-sort-ignore-string-entries t)
2275 crossref-key bounds)
2276 (bibtex-map-entries
2277 (lambda (key beg end)
2278 (if (and abortable
2279 (input-pending-p))
2280 ;; user has aborted by typing a key --> return `aborted'
2281 (throw 'userkey 'aborted))
2282 (if verbose (bibtex-progress-message))
2283 (unless (assoc key reference-keys)
2284 (push (list key) reference-keys))
2285 (if (and (setq bounds (bibtex-search-forward-field "crossref" end))
2286 (setq crossref-key (bibtex-text-in-field-bounds bounds t))
2287 (not (assoc crossref-key reference-keys)))
2288 (push (list crossref-key) reference-keys))))))
2290 (if verbose
2291 (bibtex-progress-message 'done))
2292 ;; successful operation --> return `bibtex-reference-keys'
2293 (setq bibtex-reference-keys reference-keys))))))
2295 (defun bibtex-parse-strings (&optional add abortable)
2296 "Set `bibtex-strings' to the string definitions in the whole buffer.
2297 The buffer might possibly be restricted.
2298 If ADD is non-nil add the new strings to `bibtex-strings' instead of
2299 simply resetting it. If ADD is an alist of strings, also add ADD to
2300 `bibtex-strings'. If ABORTABLE is non-nil abort on user input.
2301 Return alist of strings if parsing was completed, `aborted' otherwise."
2302 (save-excursion
2303 (save-match-data
2304 (goto-char (point-min))
2305 (let ((strings (if (and add
2306 (listp bibtex-strings))
2307 bibtex-strings))
2308 bounds key)
2309 (if (listp add)
2310 (dolist (string add)
2311 (unless (assoc (car string) strings)
2312 (push string strings))))
2313 (catch 'userkey
2314 (while (setq bounds (bibtex-search-forward-string))
2315 (if (and abortable
2316 (input-pending-p))
2317 ;; user has aborted by typing a key --> return `aborted'
2318 (throw 'userkey 'aborted))
2319 (setq key (bibtex-reference-key-in-string bounds))
2320 (if (not (assoc key strings))
2321 (push (cons key (bibtex-text-in-string bounds t))
2322 strings))
2323 (goto-char (bibtex-end-of-text-in-string bounds)))
2324 ;; successful operation --> return `bibtex-strings'
2325 (setq bibtex-strings strings))))))
2327 (defun bibtex-string-files-init ()
2328 "Return initialization for `bibtex-strings'.
2329 Use `bibtex-predefined-strings' and bib files `bibtex-string-files'."
2330 (save-match-data
2331 ;; collect pathnames
2332 (let ((dirlist (split-string (or bibtex-string-file-path ".")
2333 ":+"))
2334 (case-fold-search)
2335 compl)
2336 (dolist (filename bibtex-string-files)
2337 (unless (string-match "\\.bib\\'" filename)
2338 (setq filename (concat filename ".bib")))
2339 ;; test filenames
2340 (let (fullfilename bounds found)
2341 (dolist (dir dirlist)
2342 (when (file-readable-p
2343 (setq fullfilename (expand-file-name filename dir)))
2344 ;; file was found
2345 (with-temp-buffer
2346 (insert-file-contents fullfilename)
2347 (goto-char (point-min))
2348 (while (setq bounds (bibtex-search-forward-string))
2349 (push (cons (bibtex-reference-key-in-string bounds)
2350 (bibtex-text-in-string bounds t))
2351 compl)
2352 (goto-char (bibtex-end-of-string bounds))))
2353 (setq found t)))
2354 (unless found
2355 (error "File %s not in paths defined via bibtex-string-file-path"
2356 filename))))
2357 (append bibtex-predefined-strings (nreverse compl)))))
2359 (defun bibtex-parse-buffers-stealthily ()
2360 "Called by `bibtex-run-with-idle-timer'. Whenever emacs has been idle
2361 for `bibtex-parse-keys-timeout' seconds, all BibTeX buffers (starting
2362 with the current) are parsed."
2363 (save-excursion
2364 (let ((buffers (buffer-list))
2365 (strings-init (bibtex-string-files-init)))
2366 (while (and buffers (not (input-pending-p)))
2367 (set-buffer (car buffers))
2368 (if (and (eq major-mode 'bibtex-mode)
2369 (not (eq (buffer-modified-tick)
2370 bibtex-buffer-last-parsed-tick)))
2371 (save-restriction
2372 (widen)
2373 ;; Output no progress messages in bibtex-parse-keys
2374 ;; because when in y-or-n-p that can hide the question.
2375 (if (and (listp (bibtex-parse-keys nil t))
2376 ;; update bibtex-strings
2377 (listp (bibtex-parse-strings strings-init t)))
2379 ;; remember that parsing was successful
2380 (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick)))))
2381 (setq buffers (cdr buffers))))))
2383 (defun bibtex-complete-internal (completions)
2384 "Complete word fragment before point to longest prefix of one
2385 string defined in list COMPLETIONS. If point is not after the part
2386 of a word, all strings are listed. Return completion."
2387 (let* ((case-fold-search t)
2388 (beg (save-excursion
2389 (re-search-backward "[ \t{\"]")
2390 (forward-char)
2391 (point)))
2392 (end (point))
2393 (part-of-word (buffer-substring-no-properties beg end))
2394 (completion (try-completion part-of-word completions)))
2395 (cond ((not completion)
2396 (error "Can't find completion for `%s'" part-of-word))
2397 ((eq completion t)
2398 part-of-word)
2399 ((not (string= part-of-word completion))
2400 (delete-region beg end)
2401 (insert completion)
2402 completion)
2404 (message "Making completion list...")
2405 (with-output-to-temp-buffer "*Completions*"
2406 (display-completion-list (all-completions part-of-word
2407 completions)))
2408 (message "Making completion list...done")
2409 ;; return value is handled by choose-completion-string-functions
2410 nil))))
2412 (defun bibtex-complete-string-cleanup (str)
2413 "Cleanup after inserting string STR.
2414 Remove enclosing field delimiters for string STR. Display message with
2415 expansion of STR."
2416 (let ((pair (assoc str bibtex-strings)))
2417 (when pair
2418 (if (cdr pair)
2419 (message "Abbreviation for `%s'" (cdr pair)))
2420 (save-excursion
2421 (bibtex-inside-field)
2422 (let ((bounds (bibtex-enclosing-field)))
2423 (goto-char (bibtex-start-of-text-in-field bounds))
2424 (let ((boundaries (bibtex-parse-field-string)))
2425 (if (and boundaries
2426 (equal (cdr boundaries)
2427 (bibtex-end-of-text-in-field bounds)))
2428 (bibtex-remove-delimiters))))))))
2430 (defun bibtex-choose-completion-string (choice buffer mini-p base-size)
2431 ;; Code borrowed from choose-completion-string:
2432 ;; We must duplicate the code from choose-completion-string
2433 ;; because it runs the hook choose-completion-string-functions
2434 ;; before it inserts the completion. But we want to do something
2435 ;; after the completion has been inserted.
2437 ;; Insert the completion into the buffer where it was requested.
2438 (set-buffer buffer)
2439 (if base-size
2440 (delete-region (+ base-size (point-min))
2441 (point))
2442 ;; Delete the longest partial match for CHOICE
2443 ;; that can be found before point.
2444 (choose-completion-delete-max-match choice))
2445 (insert choice)
2446 (remove-text-properties (- (point) (length choice)) (point)
2447 '(mouse-face nil))
2448 ;; Update point in the window that BUFFER is showing in.
2449 (let ((window (get-buffer-window buffer t)))
2450 (set-window-point window (point))))
2452 (defun bibtex-pop (arg direction)
2453 "Generic function used by `bibtex-pop-previous' and `bibtex-pop-next'."
2454 (let (bibtex-help-message)
2455 (bibtex-find-text nil))
2456 (save-excursion
2457 ;; parse current field
2458 (bibtex-inside-field)
2459 (let* ((case-fold-search t)
2460 (bounds (bibtex-enclosing-field))
2461 (start-old-text (bibtex-start-of-text-in-field bounds))
2462 (stop-old-text (bibtex-end-of-text-in-field bounds))
2463 (start-name (bibtex-start-of-name-in-field bounds))
2464 (stop-name (bibtex-end-of-name-in-field bounds))
2465 ;; construct regexp for field with same name as this one,
2466 ;; ignoring possible OPT's or ALT's
2467 (field-name (progn
2468 (goto-char start-name)
2469 (buffer-substring-no-properties
2470 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)")
2471 (match-end 0)
2472 (point))
2473 stop-name))))
2474 ;; if executed several times in a row, start each search where
2475 ;; the last one was finished
2476 (unless (eq last-command 'bibtex-pop)
2477 (bibtex-enclosing-entry-maybe-empty-head)
2478 (setq bibtex-pop-previous-search-point (match-beginning 0)
2479 bibtex-pop-next-search-point (point)))
2480 (if (eq direction 'previous)
2481 (goto-char bibtex-pop-previous-search-point)
2482 (goto-char bibtex-pop-next-search-point))
2483 ;; Now search for arg'th previous/next similar field
2484 (let (bounds failure new-text)
2485 (while (and (not failure)
2486 (> arg 0))
2487 (cond ((eq direction 'previous)
2488 (if (setq bounds (bibtex-search-backward-field field-name))
2489 (goto-char (bibtex-start-of-field bounds))
2490 (setq failure t)))
2491 ((eq direction 'next)
2492 (if (setq bounds (bibtex-search-forward-field field-name))
2493 (goto-char (bibtex-end-of-field bounds))
2494 (setq failure t))))
2495 (setq arg (- arg 1)))
2496 (if failure
2497 (error "No %s matching BibTeX field"
2498 (if (eq direction 'previous) "previous" "next"))
2499 ;; Found a matching field. Remember boundaries.
2500 (setq bibtex-pop-previous-search-point (bibtex-start-of-field bounds)
2501 bibtex-pop-next-search-point (bibtex-end-of-field bounds)
2502 new-text (bibtex-text-in-field-bounds bounds))
2503 (bibtex-flash-head)
2504 ;; Go back to where we started, delete old text, and pop new.
2505 (goto-char stop-old-text)
2506 (delete-region start-old-text stop-old-text)
2507 (insert new-text)))))
2508 (let (bibtex-help-message)
2509 (bibtex-find-text nil))
2510 (setq this-command 'bibtex-pop))
2512 (defsubst bibtex-read-key (prompt &optional key)
2513 "Read BibTeX key from minibuffer using PROMPT and default KEY."
2514 (completing-read prompt bibtex-reference-keys
2515 nil nil key 'bibtex-key-history))
2517 ;; Interactive Functions:
2519 ;;;###autoload
2520 (defun bibtex-mode ()
2521 "Major mode for editing BibTeX files.
2523 General information on working with BibTeX mode:
2525 You should use commands such as \\[bibtex-Book] to get a template for a
2526 specific entry. You should then fill in all desired fields using
2527 \\[bibtex-next-field] to jump from field to field. After having filled
2528 in all desired fields in the entry, you should clean the new entry
2529 with the command \\[bibtex-clean-entry].
2531 Some features of BibTeX mode are available only by setting the variable
2532 `bibtex-maintain-sorted-entries' to non-nil. However, then BibTeX mode will
2533 work only with buffers containing valid (syntactical correct) entries
2534 and with entries being sorted. This is usually the case, if you have
2535 created a buffer completely with BibTeX mode and finished every new
2536 entry with \\[bibtex-clean-entry].
2538 For third party BibTeX files, call the function `bibtex-convert-alien'
2539 to fully take advantage of all features of BibTeX mode.
2542 Special information:
2544 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
2546 The optional fields start with the string OPT, and are thus ignored by BibTeX.
2547 Alternatives from which only one is required start with the string ALT.
2548 The OPT or ALT string may be removed from a field with \\[bibtex-remove-OPT-or-ALT].
2549 \\[bibtex-make-field] inserts a new field after the current one.
2550 \\[bibtex-kill-field] kills the current field entirely.
2551 \\[bibtex-yank] yanks the last recently killed field after the current field.
2552 \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
2553 \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
2555 The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2556 from all non-empty optional or alternative fields, checks that no required
2557 fields are empty, and does some formatting dependent on the value of
2558 `bibtex-entry-format'.
2559 Note: some functions in BibTeX mode depend on entries being in a special
2560 format (all fields beginning on separate lines), so it is usually a bad
2561 idea to remove `realign' from `bibtex-entry-format'.
2563 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
2564 Use \\[bibtex-next-field] to move to end of the next field.
2566 The following may be of interest as well:
2568 Functions:
2569 `bibtex-entry'
2570 `bibtex-kill-entry'
2571 `bibtex-yank-pop'
2572 `bibtex-pop-previous'
2573 `bibtex-pop-next'
2574 `bibtex-complete'
2575 `bibtex-print-help-message'
2576 `bibtex-generate-autokey'
2577 `bibtex-beginning-of-entry'
2578 `bibtex-end-of-entry'
2579 `bibtex-reposition-window'
2580 `bibtex-mark-entry'
2581 `bibtex-ispell-abstract'
2582 `bibtex-ispell-entry'
2583 `bibtex-narrow-to-entry'
2584 `bibtex-sort-buffer'
2585 `bibtex-validate'
2586 `bibtex-count'
2587 `bibtex-fill-entry'
2588 `bibtex-reformat'
2589 `bibtex-convert-alien'
2591 Variables:
2592 `bibtex-field-delimiters'
2593 `bibtex-include-OPTcrossref'
2594 `bibtex-include-OPTkey'
2595 `bibtex-user-optional-fields'
2596 `bibtex-entry-format'
2597 `bibtex-sort-ignore-string-entries'
2598 `bibtex-maintain-sorted-entries'
2599 `bibtex-entry-field-alist'
2600 `bibtex-predefined-strings'
2601 `bibtex-string-files'
2603 ---------------------------------------------------------
2604 Entry to BibTeX mode calls the value of `bibtex-mode-hook' if that value is
2605 non-nil.
2607 \\{bibtex-mode-map}"
2608 (interactive)
2609 (kill-all-local-variables)
2610 (use-local-map bibtex-mode-map)
2611 (setq major-mode 'bibtex-mode)
2612 (setq mode-name "BibTeX")
2613 (set-syntax-table bibtex-mode-syntax-table)
2614 (make-local-variable 'bibtex-buffer-last-parsed-tick)
2615 ;; Install stealthy parse function if not already installed
2616 (unless bibtex-parse-idle-timer
2617 (setq bibtex-parse-idle-timer (bibtex-run-with-idle-timer
2618 bibtex-parse-keys-timeout t
2619 'bibtex-parse-buffers-stealthily)))
2620 (set (make-local-variable 'paragraph-start) "[ \f\n\t]*$")
2621 (set (make-local-variable 'comment-start) bibtex-comment-start)
2622 (set (make-local-variable 'comment-start-skip)
2623 (concat (regexp-quote bibtex-comment-start) "\\>[ \t]*"))
2624 (set (make-local-variable 'comment-column) 0)
2625 (set (make-local-variable 'defun-prompt-regexp) "^[ \t]*@[a-zA-Z0-9]+[ \t]*")
2626 (set (make-local-variable 'outline-regexp) "[ \t]*@")
2627 (set (make-local-variable 'fill-paragraph-function) 'bibtex-fill-field)
2628 (set (make-local-variable 'fill-prefix) (make-string (+ bibtex-entry-offset
2629 bibtex-contline-indentation)
2630 ? ))
2631 (set (make-local-variable 'font-lock-defaults)
2632 '(bibtex-font-lock-keywords
2633 nil t ((?$ . "\"")
2634 ;; Mathematical expressions should be fontified as strings
2635 (?\" . ".")
2636 ;; Quotes are field delimiters and quote-delimited
2637 ;; entries should be fontified in the same way as
2638 ;; brace-delimited ones
2641 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords)
2642 (font-lock-mark-block-function
2643 . (lambda ()
2644 (set-mark (bibtex-end-of-entry))
2645 (bibtex-beginning-of-entry)))))
2646 (setq imenu-generic-expression
2647 (list (list nil bibtex-entry-head bibtex-key-in-head)))
2648 (make-local-variable 'choose-completion-string-functions)
2649 (setq imenu-case-fold-search t)
2650 ;; XEmacs needs easy-menu-add, Emacs does not care
2651 (easy-menu-add bibtex-edit-menu)
2652 (easy-menu-add bibtex-entry-menu)
2653 (run-hooks 'bibtex-mode-hook))
2655 (defun bibtex-field-list (entry-type)
2656 "Return list of allowed fields for entry ENTRY-TYPE.
2657 More specifically, the return value is a cons pair (REQUIRED . OPTIONAL),
2658 where REQUIRED and OPTIONAL are lists of the required and optional field
2659 names for ENTRY-TYPE according to `bibtex-entry-field-alist'."
2660 (let ((e (assoc-string entry-type bibtex-entry-field-alist t))
2661 required optional)
2662 (unless e
2663 (error "Bibtex entry type %s not defined" entry-type))
2664 (if (and (member-ignore-case entry-type bibtex-include-OPTcrossref)
2665 (nth 2 e))
2666 (setq required (nth 0 (nth 2 e))
2667 optional (nth 1 (nth 2 e)))
2668 (setq required (nth 0 (nth 1 e))
2669 optional (nth 1 (nth 1 e))))
2670 (if bibtex-include-OPTkey
2671 (push (list "key"
2672 "Used for reference key creation if author and editor fields are missing"
2673 (if (or (stringp bibtex-include-OPTkey)
2674 (fboundp bibtex-include-OPTkey))
2675 bibtex-include-OPTkey))
2676 optional))
2677 (if (member-ignore-case entry-type bibtex-include-OPTcrossref)
2678 (push '("crossref" "Reference key of the cross-referenced entry")
2679 optional))
2680 (setq optional (append optional bibtex-user-optional-fields))
2681 (cons required optional)))
2683 (defun bibtex-entry (entry-type)
2684 "Insert a new BibTeX entry.
2685 After insertion it calls the functions in `bibtex-add-entry-hook'."
2686 (interactive (let* ((completion-ignore-case t)
2687 (e-t (completing-read
2688 "Entry Type: "
2689 bibtex-entry-field-alist
2690 nil t nil 'bibtex-entry-type-history)))
2691 (list e-t)))
2692 (let ((key (if bibtex-maintain-sorted-entries
2693 (bibtex-read-key (format "%s key: " entry-type))))
2694 (field-list (bibtex-field-list entry-type)))
2695 (unless (bibtex-prepare-new-entry (list key nil entry-type))
2696 (error "Entry with key `%s' already exists" key))
2697 (indent-to-column bibtex-entry-offset)
2698 (insert "@" entry-type (bibtex-entry-left-delimiter))
2699 (if key (insert key))
2700 (save-excursion
2701 (mapcar 'bibtex-make-field (car field-list))
2702 (mapcar 'bibtex-make-optional-field (cdr field-list))
2703 (if bibtex-comma-after-last-field
2704 (insert ","))
2705 (insert "\n")
2706 (indent-to-column bibtex-entry-offset)
2707 (insert (bibtex-entry-right-delimiter) "\n\n"))
2708 (bibtex-next-field t)
2709 (if (member-ignore-case entry-type bibtex-autofill-types)
2710 (bibtex-autofill-entry))
2711 (run-hooks 'bibtex-add-entry-hook)))
2713 (defun bibtex-entry-update ()
2714 "Update an existing BibTeX entry.
2715 In the BibTeX entry at point, make new fields for those items that may occur
2716 according to `bibtex-entry-field-alist', but are not yet present."
2717 (interactive)
2718 (save-excursion
2719 (bibtex-beginning-of-entry)
2720 ;; For inserting new fields, we use the fact that
2721 ;; bibtex-parse-entry moves point to the end of the last field.
2722 (let* ((fields-alist (bibtex-parse-entry))
2723 (field-list (bibtex-field-list
2724 (substring (cdr (assoc "=type=" fields-alist))
2725 1))) ; don't want @
2726 (case-fold-search t))
2727 (dolist (field (car field-list))
2728 (unless (bibtex-assoc-regexp (concat "\\`\\(ALT\\)?" (car field) "\\'")
2729 fields-alist)
2730 (bibtex-make-field field)))
2731 (dolist (field (cdr field-list))
2732 (unless (bibtex-assoc-regexp (concat "\\`\\(OPT\\)?" (car field) "\\'")
2733 fields-alist)
2734 (bibtex-make-optional-field field))))))
2736 (defun bibtex-parse-entry ()
2737 "Parse entry at point, return an alist.
2738 The alist elements have the form (FIELD . TEXT), where FIELD can also be
2739 the special strings \"=type=\" and \"=key=\". For the FIELD \"=key=\"
2740 TEXT may be nil. Move point to the end of the last field."
2741 (let (alist bounds)
2742 (when (looking-at bibtex-entry-maybe-empty-head)
2743 (push (cons "=type=" (match-string bibtex-type-in-head)) alist)
2744 (push (cons "=key=" (match-string bibtex-key-in-head)) alist)
2745 (goto-char (match-end 0))
2746 (while (setq bounds (bibtex-parse-field bibtex-field-name))
2747 (push (cons (bibtex-name-in-field bounds)
2748 (bibtex-text-in-field-bounds bounds))
2749 alist)
2750 (goto-char (bibtex-end-of-field bounds))))
2751 alist))
2753 (defun bibtex-autofill-entry ()
2754 "Try to fill fields based on surrounding entries."
2755 (interactive)
2756 (undo-boundary) ;So you can easily undo it, if it didn't work right.
2757 (bibtex-beginning-of-entry)
2758 (when (looking-at bibtex-entry-head)
2759 (let ((type (match-string bibtex-type-in-head))
2760 (key (match-string bibtex-key-in-head))
2761 (key-end (match-end bibtex-key-in-head))
2762 (case-fold-search t)
2763 tmp other-key other bounds)
2764 ;; The fields we want to change start right after the key.
2765 (goto-char key-end)
2766 ;; First see whether to use the previous or the next entry
2767 ;; for "inspiration".
2768 (save-excursion
2769 (goto-char (1- (match-beginning 0)))
2770 (bibtex-beginning-of-entry)
2771 (when (and
2772 (looking-at bibtex-entry-head)
2773 (equal type (match-string bibtex-type-in-head))
2774 ;; In case we found ourselves :-(
2775 (not (equal key (setq tmp (match-string bibtex-key-in-head)))))
2776 (setq other-key tmp)
2777 (setq other (point))))
2778 (save-excursion
2779 (bibtex-end-of-entry)
2780 (bibtex-skip-to-valid-entry)
2781 (when (and
2782 (looking-at bibtex-entry-head)
2783 (equal type (match-string bibtex-type-in-head))
2784 ;; In case we found ourselves :-(
2785 (not (equal key (setq tmp (match-string bibtex-key-in-head))))
2786 (or (not other-key)
2787 ;; Check which is the best match.
2788 (< (length (try-completion "" (list key other-key)))
2789 (length (try-completion "" (list key tmp))))))
2790 (setq other-key tmp)
2791 (setq other (point))))
2792 ;; Then fill the new entry's fields with the chosen other entry.
2793 (when other
2794 (setq other (save-excursion (goto-char other) (bibtex-parse-entry)))
2795 (setq key-end (point)) ;In case parse-entry changed the buffer.
2796 (while (setq bounds (bibtex-parse-field bibtex-field-name))
2797 (goto-char (bibtex-start-of-name-in-field bounds))
2798 (let* ((name (buffer-substring
2799 (if (looking-at "ALT\\|OPT") (match-end 0) (point))
2800 (bibtex-end-of-name-in-field bounds)))
2801 (text (assoc-string name other t)))
2802 (goto-char (bibtex-start-of-text-in-field bounds))
2803 (if (not (and (looking-at bibtex-empty-field-re) text))
2804 (goto-char (bibtex-end-of-field bounds))
2805 (delete-region (point) (bibtex-end-of-text-in-field bounds))
2806 (insert (cdr text)))))
2807 ;; Finally try to update the text based on the difference between
2808 ;; the two keys.
2809 (let* ((prefix (try-completion "" (list key other-key)))
2810 ;; If the keys are foo91 and foo92, don't replace 1 for 2
2811 ;; but 91 for 92 instead.
2812 (_ (if (string-match "[0-9]+\\'" prefix)
2813 (setq prefix (substring prefix 0 (match-beginning 0)))))
2814 (suffix (substring key (length prefix)))
2815 (other-suffix (substring other-key (length prefix))))
2816 (while (re-search-backward (regexp-quote other-suffix) key-end 'move)
2817 (replace-match suffix)))))))
2819 (defun bibtex-print-help-message ()
2820 "Print helpful information about current field in current BibTeX entry."
2821 (interactive)
2822 (save-excursion
2823 (let* ((case-fold-search t)
2824 (bounds (bibtex-enclosing-field))
2825 (mb (bibtex-start-of-name-in-field bounds))
2826 (field-name (buffer-substring-no-properties
2827 (if (progn (goto-char mb)
2828 (looking-at "OPT\\|ALT"))
2829 (match-end 0) mb)
2830 (bibtex-end-of-name-in-field bounds)))
2831 (field-list (bibtex-field-list (progn (re-search-backward
2832 bibtex-entry-maybe-empty-head nil t)
2833 (bibtex-type-in-head))))
2834 (comment (assoc-string field-name
2835 (append (car field-list)
2836 (cdr field-list))
2837 t)))
2838 (if comment
2839 (message (nth 1 comment))
2840 (message "No comment available")))))
2842 (defun bibtex-make-field (field &optional called-by-yank)
2843 "Make a field named FIELD in current BibTeX entry.
2844 FIELD is either a string or a list of the form
2845 \(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG) as in
2846 `bibtex-entry-field-alist'."
2847 (interactive
2848 (list (let ((completion-ignore-case t)
2849 (field-list (bibtex-field-list
2850 (save-excursion
2851 (bibtex-enclosing-entry-maybe-empty-head)
2852 (bibtex-type-in-head)))))
2853 (completing-read "BibTeX field name: "
2854 (append (car field-list) (cdr field-list))
2855 nil nil nil bibtex-field-history))))
2856 (unless (consp field)
2857 (setq field (list field)))
2858 (if (or (interactive-p) called-by-yank)
2859 (let (bibtex-help-message)
2860 (bibtex-find-text nil t t)
2861 (if (looking-at "[}\"]")
2862 (forward-char))))
2863 (insert ",\n")
2864 (indent-to-column (+ bibtex-entry-offset bibtex-field-indentation))
2865 (if (nth 3 field) (insert "ALT"))
2866 (insert (car field) " ")
2867 (if bibtex-align-at-equal-sign
2868 (indent-to-column (+ bibtex-entry-offset
2869 (- bibtex-text-indentation 2))))
2870 (insert "= ")
2871 (if (not bibtex-align-at-equal-sign)
2872 (indent-to-column (+ bibtex-entry-offset
2873 bibtex-text-indentation)))
2874 (if (not called-by-yank) (insert (bibtex-field-left-delimiter)))
2875 (let ((init (nth 2 field)))
2876 (cond ((stringp init)
2877 (insert init))
2878 ((fboundp init)
2879 (insert (funcall init)))))
2880 (if (not called-by-yank) (insert (bibtex-field-right-delimiter)))
2881 (when (interactive-p)
2882 (forward-char -1)
2883 (bibtex-print-help-message)))
2885 (defun bibtex-beginning-of-entry ()
2886 "Move to beginning of BibTeX entry (beginning of line).
2887 If inside an entry, move to the beginning of it, otherwise move to the
2888 beginning of the previous entry. If point is ahead of all BibTeX entries
2889 move point to the beginning of buffer. Return the new location of point."
2890 (interactive)
2891 (skip-chars-forward " \t")
2892 (if (looking-at "@")
2893 (forward-char))
2894 (re-search-backward "^[ \t]*@" nil 'move)
2895 (point))
2897 (defun bibtex-end-of-entry ()
2898 "Move to end of BibTeX entry (past the closing brace).
2899 If inside an entry, move to the end of it, otherwise move to the end
2900 of the previous entry. Do not move if ahead of first entry.
2901 Return the new location of point."
2902 (interactive)
2903 (let ((case-fold-search t)
2904 (org (point))
2905 (pnt (bibtex-beginning-of-entry))
2906 err bounds)
2907 (cond ((looking-at bibtex-valid-entry-whitespace-re)
2908 (bibtex-search-entry t nil t)
2909 (unless (equal (match-beginning 0) pnt)
2910 (setq err t)))
2911 ((setq bounds (bibtex-parse-string))
2912 (goto-char (bibtex-end-of-string bounds)))
2913 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")
2914 (goto-char (match-end 0))
2915 (cond ((looking-at "(")
2916 (unless (re-search-forward ")[ \t]*\n\n" nil 'move)
2917 (setq err t)))
2918 ((looking-at "{")
2919 (unless (re-search-forward "}[ \t]*\n\n" nil 'move)
2920 (setq err t)))
2922 (setq err t)))
2923 (unless err
2924 (goto-char (match-beginning 0))
2925 (forward-char)))
2927 (if (interactive-p)
2928 (message "Not on a known BibTeX entry."))
2929 (goto-char org)))
2930 (when err
2931 (goto-char pnt)
2932 (error "Syntactically incorrect BibTeX entry starts here")))
2933 (point))
2935 (defun bibtex-reposition-window (&optional arg)
2936 "Make the current BibTeX entry visible.
2937 Optional argument ARG is exactly as in `recenter'."
2938 (interactive "P")
2939 (save-excursion
2940 (goto-char
2941 (/ (+ (bibtex-beginning-of-entry) (bibtex-end-of-entry)) 2))
2942 (recenter arg)))
2944 (defun bibtex-mark-entry ()
2945 "Put mark at beginning, point at end of current BibTeX entry."
2946 (interactive)
2947 (set-mark (bibtex-beginning-of-entry))
2948 (bibtex-end-of-entry))
2950 (defun bibtex-count-entries (&optional count-string-entries)
2951 "Count number of entries in current buffer or region.
2952 With prefix argument COUNT-STRING-ENTRIES it counts all entries,
2953 otherwise it counts all except Strings.
2954 If mark is active it counts entries in region, if not in whole buffer."
2955 (interactive "P")
2956 (let ((number 0)
2957 (bibtex-sort-ignore-string-entries
2958 (not count-string-entries)))
2959 (save-excursion
2960 (save-restriction
2961 (narrow-to-region (if (bibtex-mark-active)
2962 (region-beginning)
2963 (bibtex-beginning-of-first-entry))
2964 (if (bibtex-mark-active)
2965 (region-end)
2966 (point-max)))
2967 (goto-char (point-min))
2968 (bibtex-map-entries (lambda (key beg end)
2969 (setq number (1+ number))))))
2970 (message "%s contains %d entries."
2971 (if (bibtex-mark-active) "Region" "Buffer")
2972 number)))
2974 (defun bibtex-ispell-entry ()
2975 "Spell whole BibTeX entry."
2976 (interactive)
2977 (ispell-region (save-excursion (bibtex-beginning-of-entry))
2978 (save-excursion (bibtex-end-of-entry))))
2980 (defun bibtex-ispell-abstract ()
2981 "Spell abstract of BibTeX entry."
2982 (interactive)
2983 (let ((bounds (save-excursion
2984 (bibtex-beginning-of-entry)
2985 (bibtex-search-forward-field "abstract" t))))
2986 (if bounds
2987 (ispell-region (bibtex-start-of-text-in-field bounds)
2988 (bibtex-end-of-text-in-field bounds))
2989 (error "No abstract in entry"))))
2991 (defun bibtex-narrow-to-entry ()
2992 "Narrow buffer to current BibTeX entry."
2993 (interactive)
2994 (save-excursion
2995 (widen)
2996 (narrow-to-region (bibtex-beginning-of-entry)
2997 (bibtex-end-of-entry))))
2999 (defun bibtex-entry-index ()
3000 "Return the index of the BibTeX entry at point. Move point.
3001 The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting
3002 the entries of the BibTeX buffer. Return nil if no entry found."
3003 (let ((case-fold-search t))
3004 (if (re-search-forward bibtex-entry-maybe-empty-head nil t)
3005 (let ((key (bibtex-key-in-head))
3006 ;; all entry names should be downcase (for ease of comparison)
3007 (entry-name (downcase (bibtex-type-in-head))))
3008 ;; Don't search CROSSREF-KEY if we don't need it.
3009 (if (equal bibtex-maintain-sorted-entries 'crossref)
3010 (save-excursion
3011 (bibtex-beginning-of-entry)
3012 (let ((bounds (bibtex-search-forward-field
3013 "\\(OPT\\)?crossref" t)))
3014 (list key
3015 (if bounds (bibtex-text-in-field-bounds bounds t))
3016 entry-name)))
3017 (list key nil entry-name))))))
3019 (defun bibtex-lessp (index1 index2)
3020 "Predicate for sorting BibTeX entries with indices INDEX1 and INDEX2.
3021 Each index is a list (KEY CROSSREF-KEY ENTRY-NAME).
3022 The predicate depends on the variable `bibtex-maintain-sorted-entries'.
3023 If its value is nil use plain sorting."
3024 (cond ((not index1) (not index2)) ; indices can be nil
3025 ((not index2) nil)
3026 ((equal bibtex-maintain-sorted-entries 'crossref)
3027 (if (nth 1 index1)
3028 (if (nth 1 index2)
3029 (or (string-lessp (nth 1 index1) (nth 1 index2))
3030 (and (string-equal (nth 1 index1) (nth 1 index2))
3031 (string-lessp (nth 0 index1) (nth 0 index2))))
3032 (not (string-lessp (nth 0 index2) (nth 1 index1))))
3033 (if (nth 1 index2)
3034 (string-lessp (nth 0 index1) (nth 1 index2))
3035 (string-lessp (nth 0 index1) (nth 0 index2)))))
3036 ((equal bibtex-maintain-sorted-entries 'entry-class)
3037 (let ((n1 (cdr (or (assoc (nth 2 index1) bibtex-sort-entry-class-alist)
3038 (assoc 'catch-all bibtex-sort-entry-class-alist)
3039 '(nil . 1000)))) ; if there is nothing else
3040 (n2 (cdr (or (assoc (nth 2 index2) bibtex-sort-entry-class-alist)
3041 (assoc 'catch-all bibtex-sort-entry-class-alist)
3042 '(nil . 1000))))) ; if there is nothing else
3043 (or (< n1 n2)
3044 (and (= n1 n2)
3045 (string-lessp (car index1) (car index2))))))
3046 (t ; (equal bibtex-maintain-sorted-entries 'plain)
3047 (string-lessp (car index1) (car index2)))))
3049 (defun bibtex-sort-buffer ()
3050 "Sort BibTeX buffer alphabetically by key.
3051 The predicate for sorting is defined via `bibtex-maintain-sorted-entries'.
3052 If its value is nil use plain sorting. Text outside of BibTeX entries is not
3053 affected. If `bibtex-sort-ignore-string-entries' is non-nil, @String entries
3054 will be ignored."
3055 (interactive)
3056 (save-restriction
3057 (narrow-to-region (bibtex-beginning-of-first-entry)
3058 (save-excursion (goto-char (point-max))
3059 (bibtex-end-of-entry)))
3060 (bibtex-skip-to-valid-entry)
3061 (sort-subr nil
3062 'bibtex-skip-to-valid-entry ; NEXTREC function
3063 'bibtex-end-of-entry ; ENDREC function
3064 'bibtex-entry-index ; STARTKEY function
3065 nil ; ENDKEY function
3066 'bibtex-lessp))) ; PREDICATE
3068 (defun bibtex-find-crossref (crossref-key)
3069 "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3070 Return position of entry if CROSSREF-KEY is found and nil otherwise.
3071 If position of current entry is after CROSSREF-KEY an error is signaled.
3072 If called interactively, CROSSREF-KEY defaults to crossref key of current
3073 entry."
3074 (interactive
3075 (let ((crossref-key
3076 (save-excursion
3077 (bibtex-beginning-of-entry)
3078 (let ((bounds (bibtex-search-forward-field "crossref" t)))
3079 (if bounds
3080 (bibtex-text-in-field-bounds bounds t))))))
3081 (list (bibtex-read-key "Find crossref key: " crossref-key))))
3082 (let ((pos (save-excursion (bibtex-find-entry crossref-key))))
3083 (if (and pos (> (point) pos))
3084 (error "This entry must not follow the crossrefed entry!"))
3085 (goto-char pos)))
3087 (defun bibtex-find-entry (key)
3088 "Move point to the beginning of BibTeX entry named KEY.
3089 Return position of entry if KEY is found or nil if not found."
3090 (interactive (list (bibtex-read-key "Find key: ")))
3091 (let* (case-fold-search
3092 (pnt (save-excursion
3093 (goto-char (point-min))
3094 (if (re-search-forward (concat "^[ \t]*\\("
3095 bibtex-entry-type
3096 "\\)[ \t]*[({][ \t\n]*\\("
3097 (regexp-quote key)
3098 "\\)[ \t\n]*[,=]")
3099 nil t)
3100 (match-beginning 0)))))
3101 (cond (pnt
3102 (goto-char pnt))
3103 ((interactive-p)
3104 (message "Key `%s' not found" key)))))
3106 (defun bibtex-prepare-new-entry (index)
3107 "Prepare a new BibTeX entry with index INDEX.
3108 INDEX is a list (KEY CROSSREF-KEY ENTRY-NAME).
3109 Move point where the entry KEY should be placed.
3110 If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
3111 search to look for place for KEY. This will fail if buffer is not in
3112 sorted order, see \\[bibtex-validate].)
3113 Return t if preparation was successful or nil if entry KEY already exists."
3114 (let ((key (nth 0 index))
3115 key-exist)
3116 (cond ((or (null key)
3117 (and (stringp key)
3118 (string-equal key ""))
3119 (and (not (setq key-exist (bibtex-find-entry key)))
3120 (not bibtex-maintain-sorted-entries)))
3121 (bibtex-move-outside-of-entry))
3122 ;; if key-exist is non-nil due to the previous cond clause
3123 ;; then point will be at beginning of entry named key.
3124 (key-exist)
3125 (t ; bibtex-maintain-sorted-entries is non-nil
3126 (let* ((case-fold-search t)
3127 (left (save-excursion (bibtex-beginning-of-first-entry)
3128 (bibtex-skip-to-valid-entry)
3129 (point)))
3130 (right (save-excursion (bibtex-beginning-of-last-entry)
3131 (bibtex-end-of-entry)))
3132 (found (if (>= left right) left))
3133 actual-index new)
3134 (save-excursion
3135 ;; Binary search
3136 (while (not found)
3137 (goto-char (/ (+ left right) 2))
3138 (bibtex-skip-to-valid-entry t)
3139 (setq actual-index (bibtex-entry-index))
3140 (cond ((bibtex-lessp index actual-index)
3141 (setq new (bibtex-beginning-of-entry))
3142 (if (equal right new)
3143 (setq found right)
3144 (setq right new)))
3146 (bibtex-end-of-entry)
3147 (bibtex-skip-to-valid-entry)
3148 (setq new (point))
3149 (if (equal left new)
3150 (setq found right)
3151 (setq left new))))))
3152 (goto-char found)
3153 (bibtex-beginning-of-entry)
3154 (setq actual-index (save-excursion (bibtex-entry-index)))
3155 (when (or (not actual-index)
3156 (bibtex-lessp actual-index index))
3157 ;; buffer contains no valid entries or
3158 ;; greater than last entry --> append
3159 (bibtex-end-of-entry)
3160 (if (not (bobp))
3161 (newline (forward-line 2)))
3162 (beginning-of-line)))))
3163 (unless key-exist t)))
3165 (defun bibtex-validate (&optional test-thoroughly)
3166 "Validate if buffer or region is syntactically correct.
3167 Only known entry types are checked, so you can put comments
3168 outside of entries.
3169 With optional argument TEST-THOROUGHLY non-nil it checks for absence of
3170 required fields and questionable month fields as well.
3171 If mark is active, validate current region, if not the whole buffer.
3172 Returns t if test was successful, nil otherwise."
3173 (interactive "P")
3174 (let* ((case-fold-search t)
3175 error-list syntax-error)
3176 (save-excursion
3177 (save-restriction
3178 (narrow-to-region (if (bibtex-mark-active)
3179 (region-beginning)
3180 (bibtex-beginning-of-first-entry))
3181 (if (bibtex-mark-active)
3182 (region-end)
3183 (point-max)))
3185 ;; looking if entries fit syntactical structure
3186 (goto-char (point-min))
3187 (bibtex-progress-message "Checking syntactical structure")
3188 (let (bibtex-sort-ignore-string-entries)
3189 (while (re-search-forward "^[ \t]*@" nil t)
3190 (bibtex-progress-message)
3191 (forward-char -1)
3192 (let ((pnt (point)))
3193 (if (not (looking-at bibtex-any-valid-entry-re))
3194 (forward-char)
3195 (bibtex-skip-to-valid-entry)
3196 (if (equal (point) pnt)
3197 (forward-char)
3198 (goto-char pnt)
3199 (push (list (bibtex-current-line)
3200 "Syntax error (check esp. commas, braces, and quotes)")
3201 error-list)
3202 (forward-char))))))
3203 (bibtex-progress-message 'done)
3205 (if error-list
3206 (setq syntax-error t)
3207 ;; looking for correct sort order and duplicates (only if
3208 ;; there were no syntax errors)
3209 (if bibtex-maintain-sorted-entries
3210 (let (previous current)
3211 (goto-char (point-min))
3212 (bibtex-progress-message "Checking correct sort order")
3213 (bibtex-map-entries
3214 (lambda (key beg end)
3215 (bibtex-progress-message)
3216 (goto-char beg)
3217 (setq current (bibtex-entry-index))
3218 (cond ((or (not previous)
3219 (bibtex-lessp previous current))
3220 (setq previous current))
3221 ((string-equal (car previous) (car current))
3222 (push (list (bibtex-current-line)
3223 "Duplicate key with previous")
3224 error-list))
3226 (setq previous current)
3227 (push (list (bibtex-current-line)
3228 "Entries out of order")
3229 error-list)))))
3230 (bibtex-progress-message 'done)))
3232 (when test-thoroughly
3233 (goto-char (point-min))
3234 (bibtex-progress-message
3235 "Checking required fields and month fields")
3236 (let ((bibtex-sort-ignore-string-entries t)
3237 (questionable-month
3238 (regexp-opt (mapcar 'car bibtex-predefined-month-strings))))
3239 (bibtex-map-entries
3240 (lambda (key beg end)
3241 (bibtex-progress-message)
3242 (let* ((entry-list (progn
3243 (goto-char beg)
3244 (bibtex-search-entry nil end)
3245 (assoc-string (bibtex-type-in-head)
3246 bibtex-entry-field-alist t)))
3247 (req (copy-sequence (elt (elt entry-list 1) 0)))
3248 (creq (copy-sequence (elt (elt entry-list 2) 0)))
3249 crossref-there bounds)
3250 (goto-char beg)
3251 (while (setq bounds (bibtex-search-forward-field
3252 bibtex-field-name end))
3253 (goto-char (bibtex-start-of-text-in-field bounds))
3254 (let ((field-name (downcase (bibtex-name-in-field bounds)))
3255 case-fold-search)
3256 (if (and (equal field-name "month")
3257 (not (string-match questionable-month
3258 (bibtex-text-in-field-bounds bounds))))
3259 (push (list (bibtex-current-line)
3260 "Questionable month field")
3261 error-list))
3262 (setq req (delete (assoc-string field-name req t) req)
3263 creq (delete (assoc-string field-name creq t) creq))
3264 (if (equal field-name "crossref")
3265 (setq crossref-there t))))
3266 (if crossref-there
3267 (setq req creq))
3268 (if (or (> (length req) 1)
3269 (and (= (length req) 1)
3270 (not (elt (car req) 3))))
3271 ;; two (or more) fields missed or one field
3272 ;; missed and this isn't flagged alternative
3273 ;; (notice that this fails if there are more
3274 ;; than two alternatives in a BibTeX entry,
3275 ;; which isn't the case momentarily)
3276 (push (list (save-excursion
3277 (bibtex-beginning-of-entry)
3278 (bibtex-current-line))
3279 (concat "Required field `" (caar req) "' missing"))
3280 error-list))))))
3281 (bibtex-progress-message 'done)))))
3282 (if error-list
3283 (let ((bufnam (buffer-name))
3284 (dir default-directory))
3285 (setq error-list
3286 (sort error-list
3287 (lambda (a b)
3288 (< (car a) (car b)))))
3289 (let ((pop-up-windows t))
3290 (pop-to-buffer nil t))
3291 (switch-to-buffer
3292 (get-buffer-create "*BibTeX validation errors*") t)
3293 ;; don't use switch-to-buffer-other-window, since this
3294 ;; doesn't allow the second parameter NORECORD
3295 (setq default-directory dir)
3296 (toggle-read-only -1)
3297 (compilation-mode)
3298 (delete-region (point-min) (point-max))
3299 (goto-char (point-min))
3300 (insert "BibTeX mode command `bibtex-validate'\n"
3301 (if syntax-error
3302 "Maybe undetected errors due to syntax errors. Correct and validate again."
3304 "\n")
3305 (dolist (err error-list)
3306 (insert bufnam ":" (number-to-string (elt err 0))
3307 ": " (elt err 1) "\n"))
3308 (compilation-parse-errors nil nil)
3309 (setq compilation-old-error-list compilation-error-list)
3310 ;; this is necessary to avoid reparsing of buffer if you
3311 ;; switch to compilation buffer and enter `compile-goto-error'
3312 (set-buffer-modified-p nil)
3313 (toggle-read-only 1)
3314 (goto-char (point-min))
3315 (other-window -1)
3316 ;; return nil
3317 nil)
3318 (if (bibtex-mark-active)
3319 (message "Region is syntactically correct")
3320 (message "Buffer is syntactically correct"))
3321 t)))
3323 (defun bibtex-next-field (arg)
3324 "Find end of text of next BibTeX field; with ARG, to its beginning."
3325 (interactive "P")
3326 (bibtex-inside-field)
3327 (let ((start (point)))
3328 (condition-case ()
3329 (let ((bounds (bibtex-enclosing-field)))
3330 (goto-char (bibtex-end-of-field bounds))
3331 (forward-char 2))
3332 (error
3333 (goto-char start)
3334 (end-of-line)
3335 (forward-char))))
3336 (bibtex-find-text arg t))
3338 (defun bibtex-find-text (arg &optional as-if-interactive no-error)
3339 "Go to end of text of current field; with ARG, go to beginning."
3340 (interactive "P")
3341 (bibtex-inside-field)
3342 (let ((bounds (bibtex-enclosing-field (or (interactive-p)
3343 as-if-interactive))))
3344 (if bounds
3345 (progn (if arg
3346 (progn (goto-char (bibtex-start-of-text-in-field bounds))
3347 (if (looking-at "[{\"]")
3348 (forward-char)))
3349 (goto-char (bibtex-end-of-text-in-field bounds))
3350 (if (or (= (preceding-char) ?})
3351 (= (preceding-char) ?\"))
3352 (forward-char -1)))
3353 (if bibtex-help-message
3354 (bibtex-print-help-message)))
3355 (beginning-of-line)
3356 (cond ((setq bounds (bibtex-parse-string))
3357 (goto-char (if arg
3358 (bibtex-start-of-text-in-string bounds)
3359 (bibtex-end-of-text-in-string bounds))))
3360 ((looking-at bibtex-entry-maybe-empty-head)
3361 (goto-char (if arg
3362 (match-beginning bibtex-key-in-head)
3363 (match-end 0))))
3365 (unless no-error
3366 (error "Not on BibTeX field")))))))
3368 (defun bibtex-remove-OPT-or-ALT ()
3369 "Remove the string starting optional/alternative fields.
3370 Align text and go thereafter to end of text."
3371 (interactive)
3372 (bibtex-inside-field)
3373 (let ((case-fold-search t)
3374 (bounds (bibtex-enclosing-field)))
3375 (save-excursion
3376 (goto-char (bibtex-start-of-name-in-field bounds))
3377 (when (looking-at "OPT\\|ALT")
3378 (delete-region (match-beginning 0) (match-end 0))
3379 ;; make field non-OPT
3380 (search-forward "=")
3381 (forward-char -1)
3382 (delete-horizontal-space)
3383 (if bibtex-align-at-equal-sign
3384 (indent-to-column (- bibtex-text-indentation 2))
3385 (insert " "))
3386 (search-forward "=")
3387 (delete-horizontal-space)
3388 (if bibtex-align-at-equal-sign
3389 (insert " ")
3390 (indent-to-column bibtex-text-indentation))))
3391 (bibtex-inside-field)))
3393 (defun bibtex-remove-delimiters ()
3394 "Remove \"\" or {} around string."
3395 (interactive)
3396 (save-excursion
3397 (bibtex-inside-field)
3398 (let ((bounds (bibtex-enclosing-field)))
3399 (goto-char (bibtex-start-of-text-in-field bounds))
3400 (delete-char 1)
3401 (goto-char (1- (bibtex-end-of-text-in-field bounds)))
3402 (delete-backward-char 1))))
3404 (defun bibtex-kill-field (&optional copy-only)
3405 "Kill the entire enclosing BibTeX field.
3406 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
3407 but do not actually kill it."
3408 (interactive "P")
3409 (save-excursion
3410 (bibtex-inside-field)
3411 (let* ((case-fold-search t)
3412 (bounds (bibtex-enclosing-field))
3413 (end (bibtex-end-of-field bounds))
3414 (beg (bibtex-start-of-field bounds)))
3415 (goto-char end)
3416 (skip-chars-forward " \t\n,")
3417 (push (list 'field (bibtex-name-in-field bounds)
3418 (bibtex-text-in-field-bounds bounds))
3419 bibtex-field-kill-ring)
3420 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
3421 (setcdr (nthcdr (1- bibtex-field-kill-ring-max)
3422 bibtex-field-kill-ring)
3423 nil))
3424 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring)
3425 (unless copy-only
3426 (delete-region beg end))))
3427 (setq bibtex-last-kill-command 'field))
3429 (defun bibtex-copy-field-as-kill ()
3430 (interactive)
3431 (bibtex-kill-field t))
3433 (defun bibtex-kill-entry (&optional copy-only)
3434 "Kill the entire enclosing BibTeX entry.
3435 With prefix arg COPY-ONLY the current entry to
3436 `bibtex-entry-kill-ring', but do not actually kill it."
3437 (interactive "P")
3438 (save-excursion
3439 (let* ((case-fold-search t)
3440 (beg (bibtex-beginning-of-entry))
3441 (end (progn (bibtex-end-of-entry)
3442 (if (re-search-forward
3443 bibtex-entry-maybe-empty-head nil 'move)
3444 (goto-char (match-beginning 0)))
3445 (point))))
3446 (push (list 'entry (buffer-substring-no-properties beg end))
3447 bibtex-entry-kill-ring)
3448 (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
3449 (setcdr (nthcdr (1- bibtex-entry-kill-ring-max)
3450 bibtex-entry-kill-ring)
3451 nil))
3452 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring)
3453 (unless copy-only
3454 (delete-region beg end))))
3455 (setq bibtex-last-kill-command 'entry))
3457 (defun bibtex-copy-entry-as-kill ()
3458 (interactive)
3459 (bibtex-kill-entry t))
3461 (defun bibtex-yank (&optional n)
3462 "Reinsert the last BibTeX item.
3463 More precisely, reinsert the field or entry killed or yanked most recently.
3464 With argument N, reinsert the Nth most recently killed BibTeX item.
3465 See also the command \\[bibtex-yank-pop]]."
3466 (interactive "*p")
3467 (bibtex-insert-current-kill (1- n))
3468 (setq this-command 'bibtex-yank))
3470 (defun bibtex-yank-pop (n)
3471 "Replace just-yanked killed BibTeX item with a different.
3472 This command is allowed only immediately after a `bibtex-yank' or a
3473 `bibtex-yank-pop'.
3474 At such a time, the region contains a reinserted previously killed
3475 BibTeX item. `bibtex-yank-pop' deletes that item and inserts in its
3476 place a different killed BibTeX item.
3478 With no argument, the previous kill is inserted.
3479 With argument N, insert the Nth previous kill.
3480 If N is negative, this is a more recent kill.
3482 The sequence of kills wraps around, so that after the oldest one
3483 comes the newest one."
3484 (interactive "*p")
3485 (if (not (eq last-command 'bibtex-yank))
3486 (error "Previous command was not a BibTeX yank"))
3487 (setq this-command 'bibtex-yank)
3488 (let ((inhibit-read-only t))
3489 (delete-region (point) (mark t))
3490 (bibtex-insert-current-kill n)))
3492 (defun bibtex-empty-field ()
3493 "Delete the text part of the current field, replace with empty text."
3494 (interactive)
3495 (bibtex-inside-field)
3496 (let ((bounds (bibtex-enclosing-field)))
3497 (goto-char (bibtex-start-of-text-in-field bounds))
3498 (delete-region (point) (bibtex-end-of-text-in-field bounds))
3499 (insert (concat (bibtex-field-left-delimiter)
3500 (bibtex-field-right-delimiter)) )
3501 (bibtex-find-text t)))
3503 (defun bibtex-pop-previous (arg)
3504 "Replace text of current field with the similar field in previous entry.
3505 With arg, goes up ARG entries. Repeated, goes up so many times. May be
3506 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
3507 (interactive "p")
3508 (bibtex-pop arg 'previous))
3510 (defun bibtex-pop-next (arg)
3511 "Replace text of current field with the text of similar field in next entry.
3512 With arg, goes down ARG entries. Repeated, goes down so many times. May be
3513 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3514 (interactive "p")
3515 (bibtex-pop arg 'next))
3517 (defun bibtex-clean-entry (&optional new-key called-by-reformat)
3518 "Finish editing the current BibTeX entry and clean it up.
3519 Check that no required fields are empty and formats entry dependent
3520 on the value of `bibtex-entry-format'.
3521 If the reference key of the entry is empty or a prefix argument is given,
3522 calculate a new reference key. (Note: this will only work if fields in entry
3523 begin on separate lines prior to calling `bibtex-clean-entry' or if
3524 'realign is contained in `bibtex-entry-format'.)
3525 Don't call `bibtex-clean-entry' on @Preamble entries.
3526 At end of the cleaning process, the functions in
3527 `bibtex-clean-entry-hook' are called with region narrowed to entry."
3528 ;; Opt. arg called-by-reformat is t if bibtex-clean-entry
3529 ;; is called by bibtex-reformat
3530 (interactive "P")
3531 (let ((case-fold-search t)
3532 entry-type key)
3533 (bibtex-beginning-of-entry)
3534 (save-excursion
3535 (when (re-search-forward bibtex-entry-maybe-empty-head nil t)
3536 (setq entry-type (downcase (bibtex-type-in-head)))
3537 (setq key (bibtex-key-in-head))))
3538 ;; formatting
3539 (cond ((equal entry-type "preamble")
3540 ;; (bibtex-format-preamble)
3541 (error "No clean up of @Preamble entries"))
3542 ((equal entry-type "string"))
3543 ;; (bibtex-format-string)
3544 (t (bibtex-format-entry)))
3545 ;; set key
3546 (when (or new-key (not key))
3547 (setq key (bibtex-generate-autokey))
3548 (if bibtex-autokey-edit-before-use
3549 (setq key (bibtex-read-key "Key to use: " key)))
3550 (re-search-forward bibtex-entry-maybe-empty-head)
3551 (if (match-beginning bibtex-key-in-head)
3552 (delete-region (match-beginning bibtex-key-in-head)
3553 (match-end bibtex-key-in-head)))
3554 (insert key))
3555 ;; sorting
3556 (unless called-by-reformat
3557 (let* ((start (bibtex-beginning-of-entry))
3558 (end (progn (bibtex-end-of-entry)
3559 (if (re-search-forward
3560 bibtex-entry-maybe-empty-head nil 'move)
3561 (goto-char (match-beginning 0)))
3562 (point)))
3563 (entry (buffer-substring start end))
3564 (index (progn (goto-char start)
3565 (bibtex-entry-index)))
3566 no-error)
3567 (if (and bibtex-maintain-sorted-entries
3568 (not (and bibtex-sort-ignore-string-entries
3569 (equal entry-type "string"))))
3570 (progn
3571 (delete-region start end)
3572 (setq no-error (bibtex-prepare-new-entry index))
3573 (insert entry)
3574 (forward-char -1)
3575 (bibtex-beginning-of-entry) ; moves backward
3576 (re-search-forward bibtex-entry-head))
3577 (setq no-error (bibtex-find-entry (car index))))
3578 (unless no-error
3579 (error "New inserted entry yields duplicate key"))))
3580 ;; final clean up
3581 (unless called-by-reformat
3582 (save-excursion
3583 (save-restriction
3584 (bibtex-narrow-to-entry)
3585 ;; Only update the list of keys if it has been built already.
3586 (cond ((equal entry-type "string")
3587 (if (listp bibtex-strings) (bibtex-parse-strings t)))
3588 ((listp bibtex-reference-keys) (bibtex-parse-keys t)))
3589 (run-hooks 'bibtex-clean-entry-hook))))))
3591 (defun bibtex-fill-field-bounds (bounds justify &optional move)
3592 "Fill BibTeX field delimited by BOUNDS.
3593 If JUSTIFY is non-nil justify as well.
3594 If optional arg MOVE is non-nil move point to end of field."
3595 (let ((end-field (copy-marker (bibtex-end-of-field bounds))))
3596 (goto-char (bibtex-start-of-field bounds))
3597 (if justify
3598 (progn
3599 (forward-char)
3600 (bibtex-delete-whitespace)
3601 (open-line 1)
3602 (forward-char)
3603 (indent-to-column (+ bibtex-entry-offset
3604 bibtex-field-indentation))
3605 (re-search-forward "[ \t\n]*=" end-field)
3606 (replace-match "=")
3607 (forward-char -1)
3608 (if bibtex-align-at-equal-sign
3609 (indent-to-column
3610 (+ bibtex-entry-offset (- bibtex-text-indentation 2)))
3611 (insert " "))
3612 (forward-char)
3613 (bibtex-delete-whitespace)
3614 (if bibtex-align-at-equal-sign
3615 (insert " ")
3616 (indent-to-column bibtex-text-indentation)))
3617 (re-search-forward "[ \t\n]*=[ \t\n]*" end-field))
3618 (while (re-search-forward "[ \t\n]+" end-field 'move)
3619 (replace-match " "))
3620 (do-auto-fill)
3621 (if move (goto-char end-field))))
3623 (defun bibtex-fill-field (&optional justify)
3624 "Like \\[fill-paragraph], but fill current BibTeX field.
3625 Optional prefix arg JUSTIFY non-nil means justify as well.
3626 In BibTeX mode this function is bound to `fill-paragraph-function'."
3627 (interactive "*P")
3628 (let ((pnt (copy-marker (point)))
3629 (bounds (bibtex-enclosing-field)))
3630 (when bounds
3631 (bibtex-fill-field-bounds bounds justify)
3632 (goto-char pnt))))
3634 (defun bibtex-fill-entry ()
3635 "Fill current BibTeX entry.
3636 Realign entry, so that every field starts on a separate line. Field
3637 names appear in column `bibtex-field-indentation', field text starts in
3638 column `bibtex-text-indentation' and continuation lines start here, too.
3639 If `bibtex-align-at-equal-sign' is non-nil, align equal signs, too."
3640 (interactive "*")
3641 (let ((pnt (copy-marker (point)))
3642 (end (copy-marker (bibtex-end-of-entry)))
3643 bounds)
3644 (bibtex-beginning-of-entry)
3645 (bibtex-delete-whitespace)
3646 (indent-to-column bibtex-entry-offset)
3647 (while (setq bounds (bibtex-search-forward-field bibtex-field-name end))
3648 (bibtex-fill-field-bounds bounds t t))
3649 (if (looking-at ",")
3650 (forward-char))
3651 (bibtex-delete-whitespace)
3652 (open-line 1)
3653 (forward-char)
3654 (indent-to-column bibtex-entry-offset)
3655 (goto-char pnt)))
3657 (defun bibtex-realign ()
3658 "Realign BibTeX entries such that they are separated by one blank line."
3659 (goto-char (point-min))
3660 (let ((case-fold-search t))
3661 (when (looking-at bibtex-valid-entry-whitespace-re)
3662 (replace-match "\\1"))
3663 (while (re-search-forward bibtex-valid-entry-whitespace-re nil t)
3664 (replace-match "\n\n\\1"))))
3666 (defun bibtex-reformat (&optional read-options)
3667 "Reformat all BibTeX entries in buffer or region.
3668 With prefix argument, read options for reformatting from minibuffer.
3669 With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again.
3670 If mark is active reformat entries in region, if not in whole buffer."
3671 (interactive "*P")
3672 (let* ((pnt (point))
3673 (use-previous-options
3674 (and (equal (prefix-numeric-value read-options) 16)
3675 (or bibtex-reformat-previous-options
3676 bibtex-reformat-previous-reference-keys)))
3677 (bibtex-entry-format
3678 (if read-options
3679 (if use-previous-options
3680 bibtex-reformat-previous-options
3681 (setq bibtex-reformat-previous-options
3682 (mapcar (lambda (option)
3683 (if (y-or-n-p (car option)) (cdr option)))
3684 `(("Realign entries (recommended)? " . 'realign)
3685 ("Remove empty optional and alternative fields? " . 'opts-or-alts)
3686 ("Remove delimiters around pure numerical fields? " . 'numerical-fields)
3687 (,(concat (if bibtex-comma-after-last-field "Insert" "Remove")
3688 " comma at end of entry? ") . 'last-comma)
3689 ("Replace double page dashes by single ones? " . 'page-dashes)
3690 ("Force delimiters? " . 'delimiters)
3691 ("Unify case of entry types and field names? " . 'unify-case)))))
3692 '(realign)))
3693 (reformat-reference-keys
3694 (if read-options
3695 (if use-previous-options
3696 bibtex-reformat-previous-reference-keys
3697 (setq bibtex-reformat-previous-reference-keys
3698 (y-or-n-p "Generate new reference keys automatically? ")))))
3699 (start-point (if (bibtex-mark-active)
3700 (region-beginning)
3701 (point-min)))
3702 (end-point (if (bibtex-mark-active)
3703 (region-end)
3704 (point-max)))
3705 (bibtex-sort-ignore-string-entries t)
3706 bibtex-autokey-edit-before-use)
3708 (save-restriction
3709 (narrow-to-region start-point end-point)
3710 (if (memq 'realign bibtex-entry-format)
3711 (bibtex-realign))
3712 (goto-char start-point)
3713 (bibtex-progress-message "Formatting" 1)
3714 (bibtex-map-entries (lambda (key beg end)
3715 (bibtex-progress-message)
3716 (bibtex-clean-entry reformat-reference-keys t)))
3717 (when (memq 'realign bibtex-entry-format)
3718 (bibtex-delete-whitespace)
3719 (open-line (if (eobp) 1 2)))
3720 (bibtex-progress-message 'done))
3721 (when (and reformat-reference-keys
3722 bibtex-maintain-sorted-entries)
3723 (bibtex-progress-message "Sorting" 1)
3724 (bibtex-sort-buffer)
3725 (kill-local-variable 'bibtex-reference-keys)
3726 (bibtex-progress-message 'done))
3727 (goto-char pnt)))
3729 (defun bibtex-convert-alien (&optional read-options)
3730 "Convert an alien BibTeX buffer to be fully usable by BibTeX mode.
3731 If a file does not conform with all standards used by BibTeX mode,
3732 some of the high-level features of BibTeX mode will not be available.
3733 This function tries to convert current buffer to conform with these standards.
3734 With prefix argument READ-OPTIONS non-nil, read options for reformatting
3735 entries from minibuffer."
3736 (interactive "*P")
3737 (message "Starting to validate buffer...")
3738 (sit-for 1 nil t)
3739 (bibtex-realign)
3740 (message
3741 "If errors occur, correct them and call `bibtex-convert-alien' again")
3742 (sit-for 5 nil t)
3743 (deactivate-mark) ; So bibtex-validate works on the whole buffer.
3744 (when (let (bibtex-maintain-sorted-entries)
3745 (bibtex-validate))
3746 (message "Starting to reformat entries...")
3747 (sit-for 2 nil t)
3748 (bibtex-reformat read-options)
3749 (goto-char (point-max))
3750 (message "Buffer is now parsable. Please save it.")))
3752 (defun bibtex-complete ()
3753 "Complete word fragment before point according to context.
3754 If point is inside key or crossref field perform key completion based on
3755 `bibtex-reference-keys'. Inside any other field perform string
3756 completion based on `bibtex-strings'. An error is signaled if point
3757 is outside key or BibTeX field."
3758 (interactive)
3759 (let* ((pnt (point))
3760 (case-fold-search t)
3761 bounds compl)
3762 (save-excursion
3763 (if (and (setq bounds (bibtex-enclosing-field t))
3764 (>= pnt (bibtex-start-of-text-in-field bounds))
3765 (<= pnt (bibtex-end-of-text-in-field bounds)))
3766 (progn
3767 (goto-char (bibtex-start-of-name-in-field bounds))
3768 (setq compl (if (string= "crossref"
3769 (downcase
3770 (buffer-substring-no-properties
3771 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)")
3772 (match-end 0)
3773 (point))
3774 (bibtex-end-of-name-in-field bounds))))
3775 'key
3776 'str)))
3777 (bibtex-beginning-of-entry)
3778 (if (and (re-search-forward bibtex-entry-maybe-empty-head nil t)
3779 ;; point is inside a key
3780 (or (and (match-beginning bibtex-key-in-head)
3781 (>= pnt (match-beginning bibtex-key-in-head))
3782 (<= pnt (match-end bibtex-key-in-head)))
3783 ;; or point is on empty key
3784 (and (not (match-beginning bibtex-key-in-head))
3785 (= pnt (match-end 0)))))
3786 (setq compl 'key))))
3788 (cond ((equal compl 'key)
3789 ;; key completion
3790 (setq choose-completion-string-functions
3791 (lambda (choice buffer mini-p base-size)
3792 (bibtex-choose-completion-string choice buffer mini-p base-size)
3793 (if bibtex-complete-key-cleanup
3794 (funcall bibtex-complete-key-cleanup choice))
3795 ;; return t (required by choose-completion-string-functions)
3797 (let ((choice (bibtex-complete-internal bibtex-reference-keys)))
3798 (if bibtex-complete-key-cleanup
3799 (funcall bibtex-complete-key-cleanup choice))))
3801 ((equal compl 'str)
3802 ;; string completion
3803 (setq choose-completion-string-functions
3804 (lambda (choice buffer mini-p base-size)
3805 (bibtex-choose-completion-string choice buffer mini-p base-size)
3806 (bibtex-complete-string-cleanup choice)
3807 ;; return t (required by choose-completion-string-functions)
3809 (bibtex-complete-string-cleanup (bibtex-complete-internal bibtex-strings)))
3811 (t (error "Point outside key or BibTeX field")))))
3813 (defun bibtex-Article ()
3814 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
3815 (interactive "*")
3816 (bibtex-entry "Article"))
3818 (defun bibtex-Book ()
3819 "Insert a new BibTeX @Book entry; see also `bibtex-entry'."
3820 (interactive "*")
3821 (bibtex-entry "Book"))
3823 (defun bibtex-Booklet ()
3824 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'."
3825 (interactive "*")
3826 (bibtex-entry "Booklet"))
3828 (defun bibtex-InBook ()
3829 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'."
3830 (interactive "*")
3831 (bibtex-entry "InBook"))
3833 (defun bibtex-InCollection ()
3834 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'."
3835 (interactive "*")
3836 (bibtex-entry "InCollection"))
3838 (defun bibtex-InProceedings ()
3839 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'."
3840 (interactive "*")
3841 (bibtex-entry "InProceedings"))
3843 (defun bibtex-Manual ()
3844 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'."
3845 (interactive "*")
3846 (bibtex-entry "Manual"))
3848 (defun bibtex-MastersThesis ()
3849 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'."
3850 (interactive "*")
3851 (bibtex-entry "MastersThesis"))
3853 (defun bibtex-Misc ()
3854 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'."
3855 (interactive "*")
3856 (bibtex-entry "Misc"))
3858 (defun bibtex-PhdThesis ()
3859 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'."
3860 (interactive "*")
3861 (bibtex-entry "PhdThesis"))
3863 (defun bibtex-Proceedings ()
3864 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'."
3865 (interactive "*")
3866 (bibtex-entry "Proceedings"))
3868 (defun bibtex-TechReport ()
3869 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'."
3870 (interactive "*")
3871 (bibtex-entry "TechReport"))
3873 (defun bibtex-Unpublished ()
3874 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'."
3875 (interactive "*")
3876 (bibtex-entry "Unpublished"))
3878 (defun bibtex-String (&optional key)
3879 "Insert a new BibTeX @String entry with key KEY."
3880 (interactive (list (completing-read "String key: " bibtex-strings
3881 nil nil nil 'bibtex-key-history)))
3882 (let ((bibtex-maintain-sorted-entries
3883 (if (not bibtex-sort-ignore-string-entries)
3884 bibtex-maintain-sorted-entries))
3885 endpos)
3886 (unless (bibtex-prepare-new-entry (list key nil "String"))
3887 (error "Entry with key `%s' already exists" key))
3888 (if (zerop (length key)) (setq key nil))
3889 (indent-to-column bibtex-entry-offset)
3890 (insert "@String"
3891 (bibtex-entry-left-delimiter))
3892 (if key
3893 (insert key)
3894 (setq endpos (point)))
3895 (insert " = "
3896 (bibtex-field-left-delimiter))
3897 (if key
3898 (setq endpos (point)))
3899 (insert (bibtex-field-right-delimiter)
3900 (bibtex-entry-right-delimiter)
3901 "\n")
3902 (goto-char endpos)))
3904 (defun bibtex-Preamble ()
3905 "Insert a new BibTeX @Preamble entry."
3906 (interactive "*")
3907 (bibtex-move-outside-of-entry)
3908 (indent-to-column bibtex-entry-offset)
3909 (insert "@Preamble"
3910 (bibtex-entry-left-delimiter))
3911 (let ((endpos (point)))
3912 (insert (bibtex-entry-right-delimiter)
3913 "\n")
3914 (goto-char endpos)))
3917 ;; Make BibTeX a Feature
3919 (provide 'bibtex)
3921 ;; arch-tag: ee2be3af-caad-427f-b42a-d20fad630d04
3922 ;;; bibtex.el ends here