1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de>
6 ;; Bengt Martensson <bengt@mathematik.uni-Bremen.de>
7 ;; Mark Shapiro <shapiro@corto.inria.fr>
8 ;; Mike Newton <newton@gumby.cs.caltech.edu>
9 ;; Aaron Larson <alarson@src.honeywell.com>
10 ;; Maintainer: none right now
11 ;; Keywords: BibTeX, LaTeX, TeX
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; Major mode for editing and validating BibTeX files.
35 ;; See documentation for function bibtex-mode (or type "\M-x describe-mode"
36 ;; when you are in BibTeX mode).
39 ;; Distribute texinfo file.
53 (defgroup bibtex-autokey nil
54 "Generates automatically a key from the author/editor and the title field"
56 :prefix 'bibtex-autokey)
58 (defcustom bibtex-mode-hook nil
59 "List of functions to call on entry to BibTeX mode."
61 :type '(repeat function))
63 (defcustom bibtex-field-delimiters 'braces
64 "*Controls type of field delimiters used.
65 Set this to `braces' or `double-quotes' according to your personal
66 preferences. This variable is buffer-local."
68 :type '(choice (const braces)
69 (const double-quotes)))
70 (make-variable-buffer-local 'bibtex-field-delimiters)
72 (defcustom bibtex-entry-delimiters 'braces
73 "*Controls type of entry delimiters used.
74 Set this to `braces' or `parentheses' according to your personal
75 preferences. This variable is buffer-local."
77 :type '(choice (const braces)
79 (make-variable-buffer-local 'bibtex-entry-delimiters)
81 (defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
82 "*All entries listed here will have an OPTcrossref field."
84 :type '(repeat string))
86 (defcustom bibtex-include-OPTkey t
87 "*If non-nil, all entries will have an OPTkey field.
88 If this is a string, it will be used as the initial field text.
89 If this is a function, it will be called to generate the initial field text."
91 :type '(choice (const :tag "None" nil)
92 (const :tag "Default" t)
93 (string :tag "Initial text")
94 (function :tag "Initialize Function")))
96 (defcustom bibtex-user-optional-fields
97 '(("annote" "Personal annotation (ignored)"))
98 "*List of optional fields the user wants to have always present.
99 Entries should be of the same form as the OPTIONAL and
100 CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (see documentation
101 of this variable for details)."
106 (defcustom bibtex-entry-format '(opts-or-alts numerical-fields)
107 "*Controls 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 numerical-fields Delete delimiters around numeral fields.
112 page-dashes Change double dashes in page field to single dash
113 (for scribe compatibility).
114 inherit-booktitle If entry contains a crossref field and booktitle
115 field is empty, it is set to the contents of the
116 title field of the crossreferenced entry.
117 Caution: this will work only if buffer is
119 realign Realign entries, so that field texts and perhaps equal
120 signs (depending on the value of
121 bibtex-align-at-equal-sign) begin in the same column.
122 last-comma Add or delete comma on end of last field in entry,
123 according to value of `bibtex-comma-after-last-field'.
124 delimiters Change delimiters according to variables
125 `bibtex-field-delimiters' and `bibtex-entry-delimiters'.
126 unify-case Change case of entry and field names.
128 The value t means do all of the above formatting actions.
129 The value nil means do no formatting at all."
131 :type '(choice (const :tag "None" nil)
135 (defcustom bibtex-clean-entry-hook nil
136 "*List of functions to call when entry has been cleaned.
137 Functions are called with point inside the cleaned entry, and the buffer
138 narrowed to just the entry."
140 :type '(repeat function))
142 (defcustom bibtex-sort-ignore-string-entries t
143 "*If non-nil, BibTeX @String entries are not sort-significant.
144 That means they are ignored when determining ordering of the buffer
145 (e.g. sorting, locating alphabetical position for new entries, etc.).
146 This variable is buffer-local."
149 (make-variable-buffer-local 'bibtex-sort-ignore-string-entries)
151 (defcustom bibtex-maintain-sorted-entries nil
152 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order.
153 Setting this variable to nil will strip off some comfort (e.g. TAB
154 completion for reference keys in minibuffer, automatic detection of
155 duplicates) from BibTeX mode. See also `bibtex-sort-ignore-string-entries'.
156 This variable is buffer-local."
159 (make-variable-buffer-local 'bibtex-maintain-sorted-entries)
161 (defcustom bibtex-field-kill-ring-max 20
162 "*Max length of `bibtex-field-kill-ring' before discarding oldest elements."
166 (defcustom bibtex-entry-kill-ring-max 20
167 "*Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
171 (defcustom bibtex-parse-keys-timeout 60
172 "*Specifies interval for parsing buffers.
173 All BibTeX buffers in emacs are parsed if emacs has been idle
174 `bibtex-parse-keys-timeout' seconds. Only buffers which were modified
175 after last parsing and which are maintained in sorted order are parsed."
179 (defvar bibtex-entry-field-alist
181 ("Article" . (((("author" "Author1 [and Author2 ...] [and others]")
182 ("title" "Title of the article (BibTeX converts it to lowercase)")
183 ("journal" "Name of the journal (use string, remove braces)")
184 ("year" "Year of publication"))
185 (("volume" "Volume of the journal")
186 ("number" "Number of the journal (only allowed if entry contains volume)")
187 ("pages" "Pages in the journal")
188 ("month" "Month of the publication as a string (remove braces)")
189 ("note" "Remarks to be put at the end of the \\bibitem")))
190 ((("author" "Author1 [and Author2 ...] [and others]")
191 ("title" "Title of the article (BibTeX converts it to lowercase)"))
192 (("pages" "Pages in the journal")
193 ("journal" "Name of the journal (use string, remove braces)")
194 ("year" "Year of publication")
195 ("volume" "Volume of the journal")
196 ("number" "Number of the journal")
197 ("month" "Month of the publication as a string (remove braces)")
198 ("note" "Remarks to be put at the end of the \\bibitem")))))
199 ("Book" . (((("author" "Author1 [and Author2 ...] [and others]" "" t)
200 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
201 ("title" "Title of the book")
202 ("publisher" "Publishing company")
203 ("year" "Year of publication"))
204 (("volume" "Volume of the book in the series")
205 ("number" "Number of the book in a small series (overwritten by volume)")
206 ("series" "Series in which the book appeared")
207 ("address" "Address of the publisher")
208 ("edition" "Edition of the book as a capitalized English word")
209 ("month" "Month of the publication as a string (remove braces)")
210 ("note" "Remarks to be put at the end of the \\bibitem")))
211 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
212 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
213 ("title" "Title of the book"))
214 (("publisher" "Publishing company")
215 ("year" "Year of publication")
216 ("volume" "Volume of the book in the series")
217 ("number" "Number of the book in a small series (overwritten by volume)")
218 ("series" "Series in which the book appeared")
219 ("address" "Address of the publisher")
220 ("edition" "Edition of the book as a capitalized English word")
221 ("month" "Month of the publication as a string (remove braces)")
222 ("note" "Remarks to be put at the end of the \\bibitem")))))
223 ("Booklet" . (((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
224 (("author" "Author1 [and Author2 ...] [and others]")
225 ("howpublished" "The way in which the booklet was published")
226 ("address" "Address of the publisher")
227 ("month" "Month of the publication as a string (remove braces)")
228 ("year" "Year of publication")
229 ("note" "Remarks to be put at the end of the \\bibitem")))))
230 ("InBook" . (((("author" "Author1 [and Author2 ...] [and others]" "" t)
231 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
232 ("title" "Title of the book")
233 ("chapter" "Chapter in the book")
234 ("publisher" "Publishing company")
235 ("year" "Year of publication"))
236 (("volume" "Volume of the book in the series")
237 ("number" "Number of the book in a small series (overwritten by volume)")
238 ("series" "Series in which the book appeared")
239 ("type" "Word to use instead of \"chapter\"")
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 ("pages" "Pages in the book")
244 ("note" "Remarks to be put at the end of the \\bibitem")))
245 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
246 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
247 ("title" "Title of the book")
248 ("chapter" "Chapter in the book"))
249 (("pages" "Pages in the book")
250 ("publisher" "Publishing company")
251 ("year" "Year of publication")
252 ("volume" "Volume of the book in the series")
253 ("number" "Number of the book in a small series (overwritten by volume)")
254 ("series" "Series in which the book appeared")
255 ("type" "Word to use instead of \"chapter\"")
256 ("address" "Address of the publisher")
257 ("edition" "Edition of the book as a capitalized English word")
258 ("month" "Month of the publication as a string (remove braces)")
259 ("note" "Remarks to be put at the end of the \\bibitem")))))
260 ("InCollection" . (((("author" "Author1 [and Author2 ...] [and others]")
261 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
262 ("booktitle" "Name of the book")
263 ("publisher" "Publishing company")
264 ("year" "Year of publication"))
265 (("editor" "Editor1 [and Editor2 ...] [and others]")
266 ("volume" "Volume of the book in the series")
267 ("number" "Number of the book in a small series (overwritten by volume)")
268 ("series" "Series in which the book appeared")
269 ("type" "Word to use instead of \"chapter\"")
270 ("chapter" "Chapter in the book")
271 ("pages" "Pages in the book")
272 ("address" "Address of the publisher")
273 ("edition" "Edition of the book as a capitalized English word")
274 ("month" "Month of the publication as a string (remove braces)")
275 ("note" "Remarks to be put at the end of the \\bibitem")))
276 ((("author" "Author1 [and Author2 ...] [and others]")
277 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
278 ("booktitle" "Name of the book"))
279 (("pages" "Pages in the book")
280 ("publisher" "Publishing company")
281 ("year" "Year of publication")
282 ("editor" "Editor1 [and Editor2 ...] [and others]")
283 ("volume" "Volume of the book in the series")
284 ("number" "Number of the book in a small series (overwritten by volume)")
285 ("series" "Series in which the book appeared")
286 ("type" "Word to use instead of \"chapter\"")
287 ("chapter" "Chapter in the book")
288 ("address" "Address of the publisher")
289 ("edition" "Edition of the book as a capitalized English word")
290 ("month" "Month of the publication as a string (remove braces)")
291 ("note" "Remarks to be put at the end of the \\bibitem")))))
292 ("InProceedings" . (((("author" "Author1 [and Author2 ...] [and others]")
293 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
294 ("booktitle" "Name of the conference proceedings")
295 ("year" "Year of publication"))
296 (("editor" "Editor1 [and Editor2 ...] [and others]")
297 ("volume" "Volume of the conference proceedings in the series")
298 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
299 ("series" "Series in which the conference proceedings appeared")
300 ("pages" "Pages in the conference proceedings")
301 ("address" "Location of the Proceedings")
302 ("month" "Month of the publication as a string (remove braces)")
303 ("organization" "Sponsoring organization of the conference")
304 ("publisher" "Publishing company, its location")
305 ("note" "Remarks to be put at the end of the \\bibitem")))
306 ((("author" "Author1 [and Author2 ...] [and others]")
307 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
308 ("booktitle" "Name of the conference proceedings"))
309 (("pages" "Pages in the conference proceedings")
310 ("year" "Year of publication")
311 ("editor" "Editor1 [and Editor2 ...] [and others]")
312 ("volume" "Volume of the conference proceedings in the series")
313 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
314 ("series" "Series in which the conference proceedings appeared")
315 ("address" "Location of the Proceedings")
316 ("month" "Month of the publication as a string (remove braces)")
317 ("organization" "Sponsoring organization of the conference")
318 ("publisher" "Publishing company, its location")
319 ("note" "Remarks to be put at the end of the \\bibitem")))))
320 ("Manual" . (((("title" "Title of the manual"))
321 (("author" "Author1 [and Author2 ...] [and others]")
322 ("organization" "Publishing organization of the manual")
323 ("address" "Address of the organization")
324 ("edition" "Edition of the manual as a capitalized English word")
325 ("month" "Month of the publication as a string (remove braces)")
326 ("year" "Year of publication")
327 ("note" "Remarks to be put at the end of the \\bibitem")))))
328 ("MastersThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
329 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
330 ("school" "School where the master\'s thesis was written")
331 ("year" "Year of publication"))
332 (("type" "Type of the master\'s thesis (if other than \"Master\'s thesis\")")
333 ("address" "Address of the school (if not part of field \"school\") or country")
334 ("month" "Month of the publication as a string (remove braces)")
335 ("note" "Remarks to be put at the end of the \\bibitem")))))
337 (("author" "Author1 [and Author2 ...] [and others]")
338 ("title" "Title of the reference (BibTeX converts it to lowercase)")
339 ("howpublished" "The way in which the reference was published")
340 ("month" "Month of the publication as a string (remove braces)")
341 ("year" "Year of publication")
342 ("note" "Remarks to be put at the end of the \\bibitem")))))
343 ("PhdThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
344 ("title" "Title of the PhD. thesis")
345 ("school" "School where the PhD. thesis was written")
346 ("year" "Year of publication"))
347 (("type" "Type of the PhD. thesis")
348 ("address" "Address of the school (if not part of field \"school\") or country")
349 ("month" "Month of the publication as a string (remove braces)")
350 ("note" "Remarks to be put at the end of the \\bibitem")))))
351 ("Proceedings" . (((("title" "Title of the conference proceedings")
352 ("year" "Year of publication"))
353 (("editor" "Editor1 [and Editor2 ...] [and others]")
354 ("volume" "Volume of the conference proceedings in the series")
355 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
356 ("series" "Series in which the conference proceedings appeared")
357 ("address" "Location of the Proceedings")
358 ("month" "Month of the publication as a string (remove braces)")
359 ("organization" "Sponsoring organization of the conference")
360 ("publisher" "Publishing company, its location")
361 ("note" "Remarks to be put at the end of the \\bibitem")))))
362 ("TechReport" . (((("author" "Author1 [and Author2 ...] [and others]")
363 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
364 ("institution" "Sponsoring institution of the report")
365 ("year" "Year of publication"))
366 (("type" "Type of the report (if other than \"technical report\")")
367 ("number" "Number of the technical report")
368 ("address" "Address of the institution (if not part of field \"institution\") or country")
369 ("month" "Month of the publication as a string (remove braces)")
370 ("note" "Remarks to be put at the end of the \\bibitem")))))
371 ("Unpublished" . (((("author" "Author1 [and Author2 ...] [and others]")
372 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)")
373 ("note" "Remarks to be put at the end of the \\bibitem"))
374 (("month" "Month of the publication as a string (remove braces)")
375 ("year" "Year of publication")))))
378 "Defines reference types and their associated fields.
380 (ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL))
382 If the third element is nil, the first pair is always used.
383 If not, the second pair is used in the case of presence of a crossref
384 field and the third in the case of absence.
385 REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.
386 Each element of these lists is a list of the form
387 (FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).
388 COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.
389 FIELD-NAME is the name of the field, COMMENT-STRING the comment to
390 appear in the echo area, INIT is either the initial content of the
391 field or a function, which is called to determine the initial content
392 of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
393 field is an alternative. ALTERNATIVE-FLAG may be t only in the
394 REQUIRED or CROSSREF-REQUIRED lists.")
396 (defcustom bibtex-add-entry-hook nil
397 "List of functions to call when entry has been inserted."
399 :type '(repeat function))
401 (defcustom bibtex-predefined-month-strings
403 ("jan") ("feb") ("mar") ("apr") ("may") ("jun")
404 ("jul") ("aug") ("sep") ("oct") ("nov") ("dec")
406 "Alist of month string definitions.
407 Should contain all strings used for months in the BibTeX style files.
408 Each element is a list with just one element: the string."
413 (defcustom bibtex-predefined-strings
415 bibtex-predefined-month-strings
417 ("acmcs") ("acta") ("cacm") ("ibmjrd") ("ibmsj") ("ieeese")
418 ("ieeetc") ("ieeetcad") ("ipl") ("jacm") ("jcss") ("scp")
419 ("sicomp") ("tcs") ("tocs") ("tods") ("tog") ("toms") ("toois")
422 "Alist of string definitions.
423 Should contain the strings defined in the BibTeX style files. Each
424 element is a list with just one element: the string."
429 (defcustom bibtex-string-files nil
430 "*List of BibTeX files containing string definitions.
431 Those files must be specified using pathnames relative to the
432 directories specified in `bibtex-string-file-path'. This variable is only
433 evaluated when BibTeX mode is entered (i. e. when loading the BibTeX
436 :type '(repeat file))
438 (defvar bibtex-string-file-path (getenv "BIBINPUTS")
439 "*Colon separated list of pathes to search for `bibtex-string-files'.")
441 (defcustom bibtex-help-message t
442 "*If not nil print help messages in the echo area on entering a new field."
446 (defcustom bibtex-autokey-prefix-string ""
447 "*String to use as a prefix for all generated keys.
448 See the documentation of function `bibtex-generate-autokey' for more detail."
449 :group 'bibtex-autokey
452 (defcustom bibtex-autokey-names 1
453 "*Number of names to use for the automatically generated reference key.
454 If this is set to anything but a number, all names are used.
455 Possibly more names are used according to `bibtex-autokey-names-stretch'.
456 See the documentation of function `bibtex-generate-autokey' for more detail."
457 :group 'bibtex-autokey
460 (defcustom bibtex-autokey-names-stretch 0
461 "*Number of names that can additionally be used.
462 These names are used only, if all names are used then.
463 See the documentation of function `bibtex-generate-autokey' for details."
464 :group 'bibtex-autokey
467 (defcustom bibtex-autokey-additional-names ""
468 "*String to prepend to the generated key if not all names could be used.
469 See the documentation of function `bibtex-generate-autokey' for details."
470 :group 'bibtex-autokey
473 (defvar bibtex-autokey-transcriptions
475 ;; language specific characters
478 ("\\\"a\\|\\\\\\\"a\\|\\\\ae" "ae")
479 ("\\\"A\\|\\\\\\\"A\\|\\\\AE" "Ae")
484 ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" "oe")
485 ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" "Oe")
486 ("\\\"s\\|\\\\\\\"s" "ss")
487 ("\\\"u\\|\\\\\\\"u" "ue")
488 ("\\\"U\\|\\\\\\\"U" "Ue")
490 ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" "")
493 "Alist of (old-regexp new-string) pairs.
494 Used by the default values of `bibtex-autokey-name-change-strings' and
495 `bibtex-autokey-titleword-change-strings'. Defaults to translating some
496 language specific characters to their ASCII transcriptions, and
497 removing any character accents.")
499 (defcustom bibtex-autokey-name-change-strings
500 bibtex-autokey-transcriptions
501 "Alist of (OLD-REGEXP NEW-STRING) pairs.
502 Any part of name matching a OLD-REGEXP is replaced by NEW-STRING.
503 Case is significant in OLD-REGEXP. All regexps are tried in the
504 order in which they appear in the list, so be sure to avoid inifinite
506 See the documentation of function `bibtex-generate-autokey' for details."
507 :group 'bibtex-autokey
509 (list (regexp :tag "Old")
510 (string :tag "New"))))
512 (defcustom bibtex-autokey-name-length 'infty
513 "*Number of characters from name to incorporate into key.
514 If this is set to anything but a number, all characters are used.
515 See the documentation of function `bibtex-generate-autokey' for details."
516 :group 'bibtex-autokey
517 :type '(choice (const :tag "All" infty)
520 (defcustom bibtex-autokey-name-separator ""
521 "*String that comes between any two names in the key.
522 See the documentation of function `bibtex-generate-autokey' for details."
523 :group 'bibtex-autokey
526 (defcustom bibtex-autokey-year-length 2
527 "*Number of rightmost digits from the year field yo incorporate into key.
528 See the documentation of function `bibtex-generate-autokey' for details."
529 :group 'bibtex-autokey
532 (defcustom bibtex-autokey-year-use-crossref-entry t
533 "*If non-nil use year field from crossreferenced entry if necessary.
534 If this variable is non-nil and the current entry has no year, but a
535 valid crossref entry, the year field from the crossreferenced entry is
537 See the documentation of function `bibtex-generate-autokey' for details."
538 :group 'bibtex-autokey
541 (defcustom bibtex-autokey-titlewords 5
542 "*Number of title words to use for the automatically generated reference key.
543 If this is set to anything but a number, all title words are used.
544 Possibly more words from the title are used according to
545 `bibtex-autokey-titlewords-stretch'.
546 See the documentation of function `bibtex-generate-autokey' for details."
547 :group 'bibtex-autokey
548 :type '(choice (const :tag "All" infty)
551 (defcustom bibtex-autokey-title-terminators
552 '("\\." "!" "\\?" ":" ";" "--")
553 "*Regexp list defining the termination of the main part of the title.
554 Case of the regexps is ignored.
555 See the documentation of function `bibtex-generate-autokey' for details."
556 :group 'bibtex-autokey
557 :type '(repeat regexp))
559 (defcustom bibtex-autokey-titlewords-stretch 2
560 "*Number of words that can additionally be used from the title.
561 These words are used only, if a sentence from the title can be ended then.
562 See the documentation of function `bibtex-generate-autokey' for details."
563 :group 'bibtex-autokey
566 (defcustom bibtex-autokey-titleword-first-ignore
567 '("a" "an" "on" "the" "eine?" "der" "die" "das")
568 "*Determines words that may begin a title but are not to be used in the key.
569 Each item of the list is a regexp. If the first word of the title matchs a
570 regexp from that list, it is not included in the title, even if it is
571 capitalized. Case of regexps in this list doesn't matter.
572 See the documentation of function `bibtex-generate-autokey' for details."
573 :group 'bibtex-autokey
574 :type '(repeat regexp))
576 (defcustom bibtex-autokey-titleword-abbrevs nil
577 "*Determines exceptions to the usual abbreviation mechanism.
578 An alist of (OLD-REGEXP NEW-STRING) pairs. Case is ignored
579 in matching against OLD-REGEXP, and the first matching pair is used.
580 See the documentation of function `bibtex-generate-autokey' for details.")
582 (defcustom bibtex-autokey-titleword-change-strings
583 bibtex-autokey-transcriptions
584 "Alist of (OLD-REGEXP NEW-STRING) pairs.
585 Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
586 Case is significant in OLD-REGEXP. All regexps are tried in the
587 order in which they appear in the list, so be sure to avoid inifinite
589 See the documentation of function `bibtex-generate-autokey' for details."
590 :group 'bibtex-autokey
592 (list (regexp :tag "Old")
593 (string :tag "New"))))
595 (defcustom bibtex-autokey-titleword-length 5
596 "*Number of characters from title words to incorporate into key.
597 If this is set to anything but a number, all characters are used.
598 See the documentation of function `bibtex-generate-autokey' for details."
599 :group 'bibtex-autokey
600 :type '(choice (const :tag "All" infty)
603 (defcustom bibtex-autokey-titleword-separator "_"
604 "*String to be put between the title words.
605 See the documentation of function `bibtex-generate-autokey' for details."
606 :group 'bibtex-autokey
609 (defcustom bibtex-autokey-name-year-separator ""
610 "*String to be put between name part and year part of key.
611 See the documentation of function `bibtex-generate-autokey' for details."
612 :group 'bibtex-autokey
615 (defcustom bibtex-autokey-year-title-separator ":_"
616 "*String to be put between name part and year part of key.
617 See the documentation of function `bibtex-generate-autokey' for details."
618 :group 'bibtex-autokey
621 (defcustom bibtex-autokey-preserve-case nil
622 "*If non-nil, names and titlewords used aren't converted to lower case.
623 See the documentation of function `bibtex-generate-autokey' for details."
624 :group 'bibtex-autokey
627 (defcustom bibtex-autokey-edit-before-use t
628 "*If non-nil, user is allowed to edit the generated key before it is used."
629 :group 'bibtex-autokey
632 (defcustom bibtex-autokey-before-presentation-hook nil
633 "Function to call before the generated key is presented.
634 If non-nil this should be a single function, which is called before
635 the generated key is presented (in entry or, if
636 `bibtex-autokey-edit-before-use' is t, in minibuffer). This function
637 must take one argument (the automatically generated key), and must
638 return with a string (the key to use)."
639 :group 'bibtex-autokey
642 (defcustom bibtex-entry-offset 0
643 "*Offset for BibTeX entries.
644 Added to the value of all other variables which determine colums."
648 (defcustom bibtex-field-indentation 2
649 "*Starting column for the name part in BibTeX fields."
653 (defcustom bibtex-text-indentation
655 bibtex-field-indentation
656 (length "organization = "))
657 "*Starting column for the text part in BibTeX fields.
658 Should be equal to the space needed for the longest name part."
662 (defcustom bibtex-contline-indentation
663 (+ bibtex-text-indentation 1)
664 "*Starting column for continuation lines of BibTeX fields."
668 (defcustom bibtex-align-at-equal-sign nil
669 "*If non-nil, align fields at equal sign instead of field text.
670 If non-nil, the column for the equal sign is
671 the value of `bibtex-text-indentation', minus 2."
675 (defcustom bibtex-comma-after-last-field nil
676 "*If non-nil, a comma is put at end of last field in the entry template."
680 ;; bibtex-font-lock-keywords is a user option as well, but since the
681 ;; patterns used to define this variable are defined in a later
682 ;; section of this file, it is defined later.
685 ;; Syntax Table, Keybindings and BibTeX Entry List
686 (defvar bibtex-mode-syntax-table
687 (let ((st (make-syntax-table)))
688 (modify-syntax-entry ?\" "\"" st)
689 (modify-syntax-entry ?$ "$$ " st)
690 (modify-syntax-entry ?% "< " st)
691 (modify-syntax-entry ?' "w " st)
692 (modify-syntax-entry ?@ "w " st)
693 (modify-syntax-entry ?\\ "\\" st)
694 (modify-syntax-entry ?\f "> " st)
695 (modify-syntax-entry ?\n "> " st)
696 (modify-syntax-entry ?~ " " st)
699 (defvar bibtex-mode-map
700 (let ((km (make-sparse-keymap)))
701 (define-key km "\t" 'bibtex-find-text)
702 (define-key km "\n" 'bibtex-next-field)
703 (define-key km "\M-\t" 'bibtex-complete-string)
704 (define-key km [(control tab)] 'bibtex-complete-key)
705 (define-key km "\C-c\"" 'bibtex-remove-delimiters)
706 (define-key km "\C-c{" 'bibtex-remove-delimiters)
707 (define-key km "\C-c}" 'bibtex-remove-delimiters)
708 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
709 (define-key km "\C-c\C-q" 'bibtex-fill-entry)
710 (define-key km "\C-c?" 'bibtex-print-help-message)
711 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
712 (define-key km "\C-c\C-n" 'bibtex-pop-next)
713 (define-key km "\C-c\C-k" 'bibtex-kill-field)
714 (define-key km "\C-c\M-k" 'bibtex-copy-field-as-kill)
715 (define-key km "\C-c\C-w" 'bibtex-kill-entry)
716 (define-key km "\C-c\M-w" 'bibtex-copy-entry-as-kill)
717 (define-key km "\C-c\C-y" 'bibtex-yank)
718 (define-key km "\C-c\M-y" 'bibtex-yank-pop)
719 (define-key km "\C-c\C-d" 'bibtex-empty-field)
720 (define-key km "\C-c\C-f" 'bibtex-make-field)
721 (define-key km "\C-c$" 'bibtex-ispell-abstract)
722 (define-key km "\M-\C-a" 'bibtex-beginning-of-entry)
723 (define-key km "\M-\C-e" 'bibtex-end-of-entry)
724 (define-key km "\C-\M-l" 'bibtex-reposition-window)
725 (define-key km "\C-\M-h" 'bibtex-mark-entry)
726 (define-key km "\C-c\C-b" 'bibtex-entry)
727 (define-key km "\C-c\C-t" 'bibtex-hide-entry-bodies)
728 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
729 (define-key km "\C-c\C-rw" 'widen)
730 (define-key km "\C-c\C-o" 'bibtex-remove-OPT-or-ALT)
731 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
732 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
733 (define-key km "\C-c\C-eI" 'bibtex-InBook)
734 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
735 (define-key km "\C-c\C-e\C-b" 'bibtex-InBook)
736 (define-key km "\C-c\C-eb" 'bibtex-Book)
737 (define-key km "\C-c\C-eB" 'bibtex-Booklet)
738 (define-key km "\C-c\C-e\C-c" 'bibtex-InCollection)
739 (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
740 (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
741 (define-key km "\C-c\C-eM" 'bibtex-Misc)
742 (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
743 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
744 (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
745 (define-key km "\C-c\C-e\M-p" 'bibtex-Preamble)
746 (define-key km "\C-c\C-e\C-s" 'bibtex-String)
747 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
748 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
752 bibtex-edit-menu bibtex-mode-map "BibTeX-Edit Menu in BibTeX mode"
754 ("Moving inside an Entry"
755 ["End of Field" bibtex-find-text t]
756 ["Next Field" bibtex-next-field t]
757 ["Beginning of Entry" bibtex-beginning-of-entry t]
758 ["End of Entry" bibtex-end-of-entry t])
759 ("Operating on Current Entry"
760 ["Fill Entry" bibtex-fill-entry t]
761 ["Clean Entry" bibtex-clean-entry t]
763 ["Kill Entry" bibtex-kill-entry t]
764 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t]
765 ["Paste Most Recently Killed Entry" bibtex-yank t]
766 ["Paste Previously Killed Entry" bibtex-yank-pop t]
768 ["Ispell Entry" bibtex-ispell-entry t]
769 ["Ispell Entry Abstract" bibtex-ispell-abstract t]
770 ["Narrow to Entry" bibtex-narrow-to-entry t])
771 ("Operating on Current Field"
772 ["Remove Delimiters" bibtex-remove-delimiters t]
773 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t]
774 ["Clear Field" bibtex-empty-field t]
776 ["Kill Field" bibtex-kill-field t]
777 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t]
778 ["Paste Most Recently Killed Field" bibtex-yank t]
779 ["Paste Previously Killed Field" bibtex-yank-pop t]
781 ["Make New Field" bibtex-make-field t]
783 ["Snatch from Similar Following Field" bibtex-pop-next t]
784 ["Snatch from Similar Preceding Field" bibtex-pop-previous t]
786 ["String Complete" bibtex-complete-string t]
787 ["Key Complete" bibtex-complete-key t]
789 ["Help about Current Field" bibtex-print-help-message t])
790 ("Operating on Buffer or Region"
791 ["Validate Entries" bibtex-validate t]
792 ["Sort Entries" bibtex-sort-buffer t]
793 ["Reformat Entries" bibtex-reformat t]
794 ["Hide Entry Bodies" bibtex-hide-entry-bodies t]
795 ["Count Entries" bibtex-count-entries t])
797 ["Convert Alien Buffer" bibtex-convert-alien t]
798 ["Submit Bug Report" bibtex-submit-bug-report t])))
801 bibtex-entry-menu bibtex-mode-map "Entry-Types Menu in BibTeX mode"
803 ["Article in Journal" bibtex-Article t]
804 ["Article in Conference Proceedings" bibtex-InProceedings t]
805 ["Article in a Collection" bibtex-InCollection t]
806 ["Chapter or Pages in a Book" bibtex-InBook t]
807 ["Conference Proceedings" bibtex-Proceedings t]
808 ["Book" bibtex-Book t]
809 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t]
810 ["PhD. Thesis" bibtex-PhdThesis t]
811 ["Master's Thesis" bibtex-MastersThesis t]
812 ["Technical Report" bibtex-TechReport t]
813 ["Technical Manual" bibtex-Manual t]
814 ["Unpublished" bibtex-Unpublished t]
815 ["Miscellaneous" bibtex-Misc t]
816 ["String" bibtex-String t]
817 ["Preamble" bibtex-Preamble t]))
823 bibtex-maintainer-address "Stefan Schoef <schoef@offis.uni-oldenburg.de>")
824 ;; current maintainer
827 ;; Internal Variables
829 (defvar bibtex-pop-previous-search-point nil)
830 ;; Next point where bibtex-pop-previous starts looking for a similar
833 (defvar bibtex-pop-next-search-point nil)
834 ;; Next point where bibtex-pop-next starts looking for a similar entry.
836 (defvar bibtex-field-kill-ring nil)
837 ;; Ring of least recently killed fields. At most
838 ;; bibtex-field-kill-ring-max items are kept here.
840 (defvar bibtex-field-kill-ring-yank-pointer nil)
841 ;; The tail of bibtex-field-kill-ring whose car is the last item yanked.
843 (defvar bibtex-entry-kill-ring nil)
844 ;; Ring of least recently killed entries. At most
845 ;; bibtex-entry-kill-ring-max items are kept here.
847 (defvar bibtex-entry-kill-ring-yank-pointer nil)
848 ;; The tail of bibtex-entry-kill-ring whose car is the last item yanked.
850 (defvar bibtex-last-kill-command nil)
851 ;; Holds the type of the last kill command (either 'field or 'entry)
853 (defvar bibtex-strings nil)
854 ;; Candidates for bibtex-complete-string. Initialized from
855 ;; bibtex-predefined-strings and bibtex-string-files. This variable is
857 (make-variable-buffer-local 'bibtex-strings)
859 (defvar bibtex-keys nil)
860 ;; Candidates for TAB completion when entering a reference key using
861 ;; the minibuffer. Also used for bibtex-complete-key. Initialized in
862 ;; bibtex-mode and updated for each new entry. This variable is
864 (make-variable-buffer-local 'bibtex-keys)
866 (defvar bibtex-buffer-last-parsed-tick nil)
867 ;; Remembers the value returned by buffer-modified-tick when buffer
868 ;; was parsed for keys the last time.
869 (make-variable-buffer-local 'bibtex-buffer-last-parsed-tick)
871 (defvar bibtex-parse-idle-timer nil)
872 ;; Stores if timer is already installed
874 (defvar bibtex-progress-lastperc nil)
875 ;; Holds the last reported percentage for the progress message
877 (defvar bibtex-progress-lastmes nil)
878 ;; Holds the last reported progress message
880 (defvar bibtex-progress-interval nil)
881 ;; Holds the chosen interval
883 (defvar bibtex-key-history nil)
884 ;; Used to store the history list for reading keys
886 (defvar bibtex-entry-type-history nil)
887 ;; Used to store the history list for reading entry types
889 (defvar bibtex-field-history nil)
890 ;; Used to store the history list for reading field names
892 (defvar bibtex-reformat-previous-options nil)
893 ;; Used to store the last reformat options given
895 (defvar bibtex-reformat-previous-labels nil)
896 ;; Used to store the last reformat label option given
899 ;; Functions to Parse the BibTeX Entries
901 (defun bibtex-cfield (name text)
902 ;; Create a regexp for a BibTeX field of name NAME and text TEXT.
903 (concat ",[ \t\n]*\\("
905 "\\)[ \t\n]*=[ \t\n]*\\("
908 (defconst bibtex-name-in-cfield 1)
909 ;; The regexp subexpression number of the name part in bibtex-cfield.
911 (defconst bibtex-text-in-cfield 2)
912 ;; The regexp subexpression number of the text part in bibtex-cfield.
914 (defconst bibtex-reference-type "@[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*")
915 ;; Regexp defining the type part of a BibTeX reference entry (almost
916 ;; the same as bibtex-field-name)
918 (defconst bibtex-reference-key "[][a-z0-9.:;?!`'/*@+=|()<>&_^$-]+")
919 ;; Regexp defining the label part of a BibTeX reference entry
921 (defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*")
922 ;; Regexp defining the name part of a BibTeX field (almost the same as
923 ;; bibtex-reference-type)
925 (defconst bibtex-field-const "[][a-z0-9.:;?!`'/*@+=|<>&_^$-]+")
926 ;; Regexp defining a bibtex field constant
928 (defconst bibtex-field-string-part-not-braced
930 ;; Match field string part without braces
932 (defconst bibtex-field-string-part-no-inner-braces
935 bibtex-field-string-part-not-braced "*"
937 ;; Match field string part with no inner braces
939 (defconst bibtex-field-string-part-1-inner-brace
943 bibtex-field-string-part-not-braced
945 "\\(" bibtex-field-string-part-no-inner-braces "\\)"
948 ;; Match field string part with at most 1 inner brace
950 (defconst bibtex-field-string-part-2-inner-braces
954 bibtex-field-string-part-not-braced
956 "\\(" bibtex-field-string-part-1-inner-brace "\\)"
959 ;; Match field string part with at most 2 inner braces
961 (defconst bibtex-field-string-part-3-inner-braces
965 bibtex-field-string-part-not-braced
967 "\\(" bibtex-field-string-part-2-inner-braces "\\)"
970 ;; Match field string part with at most 3 inner braces
972 (defconst bibtex-field-string-braced
973 bibtex-field-string-part-3-inner-braces)
974 ;; Match braced field string with inner nesting level of braces at most 3
976 (defconst bibtex-field-string-quoted
980 "[^\"\\]" ;; every character except quote or backslash
982 ;; "\\(" "\"[a-z-]" "\\)" ;; a quote followed by a letter or dash
984 ;; last two lines commented out until lines like
985 ;; author = "Stefan Sch"of"
986 ;; are supported by BibTeX
987 "\\(" "\\\\\\(.\\|\n\\)" "\\)" ;; a backslash followed by any character
990 ;; Match quoted field string
992 (defconst bibtex-field-string
994 "\\(" bibtex-field-string-braced "\\)"
996 "\\(" bibtex-field-string-quoted "\\)"))
997 ;; Match a braced or quoted string
999 (defconst bibtex-field-string-or-const
1000 (concat bibtex-field-const "\\|" bibtex-field-string))
1001 ;; Match either bibtex-field-string or bibtex-field-const.
1003 (defconst bibtex-field-text
1005 "\\(" bibtex-field-string-or-const "\\)"
1006 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*"))
1007 ;; Regexp defining the text part of a BibTeX field: either a string,
1008 ;; or an empty string, or a constant followed by one or more # /
1011 (defconst bibtex-field
1012 (bibtex-cfield bibtex-field-name bibtex-field-text))
1013 ;; Regexp defining the format of a BibTeX field.
1015 (defconst bibtex-name-in-field bibtex-name-in-cfield)
1016 ;; The regexp subexpression number of the name part in BibTeX-field.
1018 (defconst bibtex-text-in-field bibtex-text-in-cfield)
1019 ;; The regexp subexpression number of the text part in BibTeX-field.
1021 (defconst bibtex-reference-head
1022 (concat "^[ \t]*\\("
1023 bibtex-reference-type
1024 "\\)[ \t]*[({][ \t]*\\("
1025 bibtex-reference-key
1027 ;; Regexp defining format of the header line of a BibTeX reference
1030 (defconst bibtex-reference-maybe-empty-head
1031 (concat bibtex-reference-head "?"))
1032 ;; Regexp defining format of the header line of a maybe empty
1033 ;; BibTeX reference entry (without reference key).
1035 (defconst bibtex-type-in-head 1)
1036 ;; The regexp subexpression number of the type part in
1037 ;; bibtex-reference-head.
1039 (defconst bibtex-key-in-head 2)
1040 ;; The regexp subexpression number of the key part in
1041 ;; bibtex-reference-head.
1043 (defconst bibtex-reference-infix (concat "[ \t\n]*" bibtex-field))
1044 ;; Regexp defining the (repeatable) infix of a bibtex reference
1046 (defconst bibtex-reference-postfix "[ \t\n]*,?[ \t\n]*[})]")
1047 ;; Regexp defining the postfix of a bibtex reference
1049 (defconst bibtex-key-in-reference bibtex-key-in-head)
1050 ;; The regexp subexpression number of the key part in
1051 ;; bibtex-reference.
1053 (defconst bibtex-string
1054 (concat "^[ \t]*@string[ \t\n]*[({][ \t\n]*\\("
1055 bibtex-reference-key
1056 "\\)[ \t\n]*=[ \t\n]*\\("
1059 ;; Regexp defining the format of a BibTeX string entry.
1061 (defconst bibtex-key-in-string 1)
1062 ;; The regexp subexpression of the name part in bibtex-string.
1064 (defconst bibtex-text-in-string 2)
1065 ;; The regexp subexpression of the text part in bibtex-string.
1067 (defvar bibtex-font-lock-keywords
1069 (list bibtex-reference-maybe-empty-head
1070 (list bibtex-type-in-head 'font-lock-function-name-face)
1071 (list bibtex-key-in-head 'font-lock-reference-face nil t))
1072 ;; reference type and reference label
1074 (concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
1075 1 'font-lock-comment-face)
1076 ;; optional field names (treated as comments)
1077 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1078 1 'font-lock-variable-name-face)
1080 "*Default expressions to highlight in BibTeX mode."))
1081 ;; now all needed patterns are defined
1086 (defun bibtex-delete-whitespace ()
1087 ;; Delete all whitespace starting at point
1088 (if (looking-at "[ \t\n]+")
1089 (delete-region (point) (match-end 0))))
1091 (defun bibtex-current-line ()
1092 ;; this computes line number of point regardless whether the buffer
1094 (+ (count-lines 1 (point))
1095 (if (equal (current-column) 0) 1 0)))
1097 (defun bibtex-member-of-regexp (string list)
1098 ;; Return non-nil if STRING is exactly matched by an element of
1099 ;; LIST (case ignored). The value is actually the tail of LIST whose
1100 ;; car matches STRING.
1101 (let ((case-fold-search t))
1103 (and list (not (string-match (concat "^" (car list) "$") string)))
1104 (setq list (cdr list)))
1107 (defun bibtex-assoc-of-regexp (string alist)
1108 ;; Return non-nil if STRING is exactly matched by the car of an
1109 ;; element of LIST (case ignored). The value is actually the element
1110 ;; of LIST whose car matches STRING.
1111 (let ((case-fold-search t))
1114 (not (string-match (concat "^" (car (car alist)) "$") string)))
1115 (setq alist (cdr alist)))
1118 (defun bibtex-skip-to-valid-entry (&optional backward)
1119 ;; If not at beginning of valid BibTeX entry, move to beginning of
1120 ;; the next valid one. With argument backward non-nil, move backward
1121 ;; to beginning of previous valid one. A valid entry is a
1122 ;; syntactical correct one with type contained in
1123 ;; bibtex-entry-field-alist or, if bibtex-sort-ignore-string-entries
1124 ;; is nil, a syntactical correct string entry.
1125 (let ((case-fold-search t)
1131 (concat "\\(" (car type) "\\)"))
1132 bibtex-entry-field-alist
1136 (while (and (not found)
1140 (let ((pnt (point)))
1142 ((looking-at valid-bibtex-entry)
1144 (bibtex-search-reference nil nil t)
1145 (equal (match-beginning 0) pnt))
1147 ((and (not bibtex-sort-ignore-string-entries)
1148 (looking-at bibtex-string))
1154 (goto-char (1- pnt))
1155 (if (re-search-backward "^[ \t]*\\(@\\)" nil 'move)
1156 (goto-char (match-beginning 1))))
1157 (goto-char (1+ pnt))
1158 (if (re-search-forward "^[ \t]*@" nil 'move)
1159 (forward-char -1))))))))
1161 (defun bibtex-map-entries (fun)
1162 ;; Call FUN for each BibTeX entry starting with the current. Do this
1163 ;; to the end of the file. FUN is called with one argument, the key
1164 ;; of the entry, and with point inside the entry. If
1165 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be
1166 ;; called for @String entries.
1167 (let ((case-fold-search t))
1168 (bibtex-beginning-of-entry)
1169 (while (re-search-forward bibtex-reference-maybe-empty-head nil t)
1172 (downcase (buffer-substring-no-properties
1173 (1+ (match-beginning bibtex-type-in-head))
1174 (match-end bibtex-type-in-head))))
1176 (if (match-beginning bibtex-key-in-head)
1177 (buffer-substring-no-properties
1178 (match-beginning bibtex-key-in-head)
1179 (match-end bibtex-key-in-head))
1183 (not bibtex-sort-ignore-string-entries)
1184 (string-equal "string" (downcase reference-type)))
1185 (assoc-ignore-case reference-type bibtex-entry-field-alist))
1186 (funcall fun reference-key))
1188 (bibtex-end-of-entry)))))
1190 (defun bibtex-progress-message (&optional flag interval)
1191 ;; echos a message about progress of current buffer
1192 ;; if flag is a string, the message is initialized (in this case a
1193 ;; value for INTERVAL may be given as well (if not this is set to 5))
1194 ;; if flag is done, the message is deinitialized
1195 ;; if flag is absent, a message is echoed if point was incremented
1196 ;; at least INTERVAL percent since last message was echoed
1197 (let* ((size (- (point-max) (point-min)))
1198 (perc (if (= size 0)
1200 (/ (* 100 (- (point) (point-min))) size))))
1201 (if (or (and (not flag)
1203 (+ bibtex-progress-interval bibtex-progress-lastperc)))
1208 (setq bibtex-progress-lastmes flag)
1210 (setq bibtex-progress-interval interval)
1211 (setq bibtex-progress-interval 5))))
1212 (setq bibtex-progress-lastperc perc)
1213 (message (concat bibtex-progress-lastmes " (%d%%)") perc))
1214 (if (equal flag 'done)
1216 (message (concat bibtex-progress-lastmes " (done)"))
1217 (setq bibtex-progress-lastmes nil))))))
1220 (defun bibtex-field-left-delimiter ()
1221 ;; returns a string dependent on bibtex-field-delimiters
1222 (if (equal bibtex-field-delimiters 'braces)
1226 (defun bibtex-field-right-delimiter ()
1227 ;; returns a string dependent on bibtex-field-delimiters
1228 (if (equal bibtex-field-delimiters 'braces)
1232 (defun bibtex-entry-left-delimiter ()
1233 ;; returns a string dependent on bibtex-field-delimiters
1234 (if (equal bibtex-entry-delimiters 'braces)
1238 (defun bibtex-entry-right-delimiter ()
1239 ;; returns a string dependent on bibtex-field-delimiters
1240 (if (equal bibtex-entry-delimiters 'braces)
1244 (defun bibtex-search-reference
1245 (empty-head &optional bound noerror backward)
1246 ;; A helper function necessary since the failure stack size limit for
1247 ;; regexps was reduced in emacs 19.32.
1248 ;; It searches for a BibTeX reference (maybe with an empty head if
1249 ;; EMPTY-HEAD is t).
1250 ;; BOUND and NOERROR are exactly as in re-search-forward. If
1251 ;; BACKWARD is non-nil, search is done in reverse direction. After
1252 ;; call to this function MATCH-BEGINNING and MATCH-END functions are
1253 ;; defined, but only for the head part of the reference (especially
1254 ;; (match-end 0) just gives the end of the head part).
1256 (prefix (if empty-head
1257 bibtex-reference-maybe-empty-head
1258 bibtex-reference-head))
1259 (infix bibtex-reference-infix)
1260 (postfix bibtex-reference-postfix))
1263 (while (and (not found)
1264 (re-search-backward prefix bound noerror))
1265 (setq found (bibtex-search-reference empty-head pnt t)))
1267 (goto-char (match-beginning 0))
1268 (if (equal noerror nil)
1270 (error "Search of BibTeX reference failed."))
1271 (if (equal noerror t)
1275 (let ((limit (if bound bound (point-max)))
1278 (while (and (not found)
1279 (re-search-forward prefix bound noerror))
1280 (setq md (match-data))
1281 ;; save match-data of prefix regexp
1284 (goto-char (match-end bibtex-type-in-head))
1285 (looking-at "[ \t]*("))
1286 ;; entry opened with parenthesis
1291 (<= (match-end 0) limit))
1292 (goto-char (match-end 0)))
1293 ;; This matches the infix* part. The AND construction assures
1294 ;; that BOUND is respected.
1295 (if (and (looking-at postfix)
1297 (buffer-substring-no-properties
1298 (1- (match-end 0)) (match-end 0))
1300 (<= (match-end 0) limit))
1302 (re-search-forward postfix)
1306 (store-match-data md)
1307 ;; to set match-beginning/end again
1309 (if (equal noerror nil)
1311 (error "Search of BibTeX reference failed."))
1312 (if (equal noerror t)
1317 (defun bibtex-flash-head ()
1318 ;; Flash at BibTeX reference head before point, if exists.
1319 (let ((case-fold-search t)
1321 (cond ((re-search-backward bibtex-reference-head nil t)
1322 (goto-char (match-beginning bibtex-type-in-head))
1323 (setq flash (match-end bibtex-key-in-reference)))
1326 (skip-chars-backward " \t")
1327 (setq flash (point))
1329 (skip-chars-forward " \t")))
1330 (if (pos-visible-in-window-p (point))
1333 (buffer-substring (point) flash)))))
1335 (defun bibtex-make-optional-field (e-t)
1336 "Makes an optional field named E-T in current BibTeX entry."
1338 (bibtex-make-field (cons (concat "OPT" (car e-t)) (cdr e-t)))
1339 (bibtex-make-field (concat "OPT" e-t))))
1341 (defun bibtex-move-outside-of-entry ()
1342 ;; Make sure we are outside of a BibTeX entry.
1343 (bibtex-end-of-entry)
1344 (skip-chars-forward " \t\n"))
1346 (defun bibtex-beginning-of-first-entry ()
1347 ;; Go to the beginning of the first BibTeX entry in buffer. Return
1349 (goto-char (point-min))
1350 (if (re-search-forward "^[ \t]*@" nil 'move)
1351 (beginning-of-line))
1354 (defun bibtex-beginning-of-last-entry ()
1355 ;; Go to the beginning of the last BibTeX entry in buffer.
1356 (goto-char (point-max))
1357 (if (re-search-backward "^[ \t]*@" nil 'move)
1358 (beginning-of-line))
1361 (defun bibtex-inside-field ()
1362 ;; Try to avoid point being at end of a BibTeX field.
1364 (skip-chars-backward " \t")
1365 (cond ((= (preceding-char) ?,)
1368 (= (preceding-char) ?})
1369 (= (preceding-char) ?\"))
1370 (forward-char -1))))
1372 (defun bibtex-enclosing-field (&optional noerr)
1373 ;; Search for BibTeX field enclosing point. Point moves to end of
1374 ;; field. Use match-beginning and match-end to parse the field. If
1375 ;; NOERR is non-nil, no error is signalled. In this case, t is
1376 ;; returned on success, nil otherwise.
1377 (let ((case-fold-search t)
1379 (boe (bibtex-beginning-of-entry))
1381 (goto-char old-point)
1382 (if (not (re-search-backward
1383 (bibtex-cfield bibtex-field-name "")
1385 ;; Search possible beginning of field
1387 (goto-char old-point)
1390 (error "Can't find enclosing BibTeX field.")))
1391 (if (or (not (re-search-forward bibtex-field nil t))
1392 (< (match-end 0) old-point)
1393 (> (match-beginning 0) old-point))
1395 (goto-char old-point)
1398 (error "Can't find enclosing BibTeX field.")))))
1401 (defun bibtex-enclosing-reference-maybe-empty-head ()
1402 ;; Search for BibTeX reference enclosing point. Point moves to
1403 ;; end of reference. Beginning (but not end) of reference is given
1404 ;; by (match-beginning 0).
1405 (let ((case-fold-search t)
1406 (old-point (point)))
1409 bibtex-reference-maybe-empty-head nil t))
1411 (error "Can't find enclosing BibTeX reference.")
1412 (goto-char old-point)))
1413 (goto-char (match-beginning bibtex-type-in-head))
1415 (bibtex-search-reference t nil t))
1417 (error "Can't find enclosing BibTeX reference.")
1418 (goto-char old-point)))))
1420 (defun bibtex-insert-current-kill (n)
1421 (if (not bibtex-last-kill-command)
1422 (error "BibTeX kill ring is empty.")
1423 (let* ((kr (if (equal bibtex-last-kill-command 'field)
1424 'bibtex-field-kill-ring
1425 'bibtex-entry-kill-ring))
1426 (kryp (if (equal bibtex-last-kill-command 'field)
1427 'bibtex-field-kill-ring-yank-pointer
1428 'bibtex-entry-kill-ring-yank-pointer))
1431 (mod (- n (length (eval kryp))) (length (eval kr)))
1433 (current (car (set kryp ARGth-kill-element))))
1435 ((equal bibtex-last-kill-command 'field)
1436 (let (bibtex-help-message)
1437 (bibtex-find-text nil t)
1438 (if (looking-at "[}\"]")
1441 (message "Mark set")
1442 (bibtex-make-field (list (elt current 1) nil (elt current 2)) t))
1443 ((equal bibtex-last-kill-command 'entry)
1445 (bibtex-beginning-of-entry))
1447 (message "Mark set")
1448 (insert (elt current 1)))
1451 "Unknown tag field: %s. Please submit a bug report."
1452 bibtex-last-kill-command))))))
1454 (defun bibtex-format-entry ()
1455 ;; Helper function for bibtex-clean-entry. Formats current entry
1456 ;; according to variable bibtex-entry-format.
1457 (let ((case-fold-search t)
1459 (start (bibtex-beginning-of-entry))
1462 non-empty-alternative)
1463 (let ((end (copy-marker (bibtex-end-of-entry))))
1464 (if (equal start (marker-position end))
1465 (error "Not on a known BibTeX entry.")
1467 (while (re-search-forward bibtex-field end t)
1468 ;; determine if reference has crossref entry and if at least
1469 ;; one alternative is non-empty
1470 (let ((begin-name (match-beginning bibtex-name-in-field))
1471 (end-name (match-end bibtex-name-in-field))
1472 (begin-text (match-beginning bibtex-text-in-field))
1473 (end-text (match-end bibtex-text-in-field)))
1474 (goto-char begin-name)
1475 (if (looking-at "ALT")
1477 (setq alternatives-there t)
1478 (goto-char begin-text)
1479 (if (not (looking-at "\\(\"\"\\)\\|\\({}\\)"))
1480 (setq non-empty-alternative t))))
1482 "\\(OPT\\)?crossref"
1483 (buffer-substring-no-properties begin-name end-name))
1487 (buffer-substring-no-properties
1488 (1+ begin-text) (1- end-text)))
1489 (if (equal crossref-there "")
1490 (setq crossref-there nil))))))
1491 (if (and alternatives-there
1492 (not non-empty-alternative))
1495 (error "All alternatives are empty.")))
1497 (re-search-forward bibtex-reference-type end)
1498 (let* ((begin-type (1+ (match-beginning 0)))
1499 (end-type (match-end 0))
1502 (buffer-substring-no-properties begin-type end-type)))
1504 (assoc-ignore-case reference-type bibtex-entry-field-alist))
1505 (req (elt (elt entry-list 1) 0))
1506 (creq (elt (elt entry-list 2) 0))
1507 (format (if (equal bibtex-entry-format t)
1508 '(realign opts-or-alts numerical-fields
1509 last-comma page-dashes delimiters
1510 unify-case inherit-booktitle)
1511 bibtex-entry-format))
1513 (if (memq 'unify-case format)
1515 (delete-region begin-type end-type)
1516 (insert (car entry-list))))
1517 (if (memq 'delimiters format)
1519 (goto-char end-type)
1520 (skip-chars-forward " \t\n")
1522 (insert (bibtex-entry-left-delimiter))))
1524 (while (re-search-forward bibtex-field end t)
1526 (copy-marker (match-beginning 0)))
1528 (copy-marker (match-end 0)))
1530 (copy-marker (match-beginning bibtex-name-in-field)))
1532 (copy-marker (match-end bibtex-name-in-field)))
1534 (copy-marker (match-beginning bibtex-text-in-field)))
1536 (copy-marker (match-end bibtex-text-in-field)))
1538 (buffer-substring-no-properties
1541 (buffer-substring-no-properties
1542 begin-name (+ begin-name 3)))
1548 (memq 'opts-or-alts format)
1549 (progn (goto-char begin-name)
1550 (looking-at "OPT\\|ALT")))
1551 (goto-char begin-text)
1552 (if (looking-at "\\(\"\"\\)\\|\\({}\\)")
1553 ;; empty: delete whole field if really optional
1554 ;; (missing crossref handled) or complain
1557 (goto-char begin-name)
1559 (not crossref-there)
1560 (assoc-ignore-case field-name req))
1561 ;; field is not really optional
1563 (goto-char begin-name)
1564 (bibtex-remove-OPT-or-ALT)
1566 "Mandatory field ``%s'' is empty." field-name))
1567 ;; field is optional
1568 (delete-region begin-field end-field))
1569 ;; otherwise: not empty, delete "OPT"
1570 (goto-char begin-name)
1571 (bibtex-remove-OPT-or-ALT)))
1573 (memq 'numerical-fields format)
1575 (goto-char begin-text)
1576 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1577 (goto-char end-text)
1579 (goto-char begin-text)
1582 (if (memq 'delimiters format)
1584 (goto-char begin-text)
1586 (<= (point) end-text)
1588 bibtex-field-string-or-const end-text t))
1589 (let ((end (point)))
1590 (goto-char (match-beginning 0))
1593 (equal bibtex-field-delimiters 'double-quotes)
1594 (looking-at bibtex-field-string-braced))
1596 (equal bibtex-field-delimiters 'braces)
1597 (looking-at bibtex-field-string-quoted)))
1599 (goto-char (match-end 0))
1600 (delete-backward-char 1)
1601 (insert (bibtex-field-right-delimiter))
1602 (goto-char (match-beginning 0))
1604 (insert (bibtex-field-left-delimiter))))
1607 (memq 'page-dashes format)
1608 (string-match "^\\(OPT\\)?pages$" (downcase field-name))
1610 (goto-char begin-text)
1612 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1613 (replace-match "\\1-\\2"))
1615 (memq 'inherit-booktitle format)
1616 (equal (downcase field-name) "booktitle")
1618 (goto-char begin-text)
1619 (looking-at "\\(\"\"\\)\\|\\({}\\)"))
1621 (not (bibtex-find-entry-location crossref-there t)))
1622 ;; booktitle field empty and crossref entry found
1623 ;; --> insert title field of crossreferenced entry if there
1624 (let ((end-of-crefd-entry (bibtex-end-of-entry)))
1625 (bibtex-beginning-of-entry)
1626 (if (re-search-forward
1627 (bibtex-cfield "title" bibtex-field-text)
1628 end-of-crefd-entry t)
1630 (goto-char begin-text)
1633 (buffer-substring-no-properties
1634 (1+ (match-beginning bibtex-text-in-field))
1635 (1- (match-end bibtex-text-in-field))))))))
1637 (goto-char begin-text)
1638 (looking-at "\\(\"\"\\)\\|\\({}\\)"))
1639 ;; if empty field, complain
1647 (not crossref-there)
1651 "Mandatory field ``%s'' is empty." field-name))))
1652 (if (memq 'unify-case format)
1654 (car (cdr (assoc-ignore-case
1656 bibtex-entry-field-alist))))
1661 bibtex-user-optional-fields))
1664 (assoc-ignore-case field-name field-list))))
1665 (goto-char begin-name)
1668 (delete-region begin-name end-name)
1669 (insert new-field-name))
1670 (downcase-region begin-name end-name))))
1671 (setq field-done t)))
1672 (if (not field-done)
1673 (goto-char begin-field)
1674 (setq field-done nil)
1675 (goto-char end-field))))
1676 (if (looking-at (bibtex-field-right-delimiter))
1678 (if (memq 'last-comma format)
1680 bibtex-comma-after-last-field
1681 (not (looking-at ",")))
1684 (not bibtex-comma-after-last-field)
1687 (if (looking-at ",")
1689 (if (memq 'delimiters format)
1691 (skip-chars-forward " \t\n")
1693 (insert (bibtex-entry-right-delimiter))))
1694 (if (memq 'realign format)
1695 (bibtex-fill-entry)))))))
1697 (defun bibtex-autokey-change (string change-list)
1698 ;; Returns a string where some regexps are changed according to
1699 ;; change-list. Every item of change-list is an (old-regexp
1700 ;; new-string) pair.
1701 (let (case-fold-search
1702 (return-string string)
1704 (len (length change-list))
1706 (while (< index len)
1707 (setq change-item (elt change-list index))
1708 (while (string-match (car change-item) return-string)
1711 (concat (substring return-string 0 (match-beginning 0))
1713 (substring return-string (match-end 0)))))
1714 (setq index (1+ index)))
1717 (defun bibtex-autokey-abbrev (string len)
1718 ;; Returns an abbreviation of string with at least len
1719 ;; characters. String is aborted only after a consonant or at the
1720 ;; word end. If len is not a number, string is returned unchanged.
1723 (<= (length string) len))
1728 (let* ((case-fold-search t)
1730 (string-match "[^aeiou]" string (1- len))))
1732 (substring string 0 (1+ abort-char))
1735 (defun bibtex-autokey-get-namefield (min max)
1736 ;; returns the contents of the name field of the current entry
1737 ;; does some modifications based on
1738 ;; `bibtex-autokey-name-change-strings'
1740 (let ((case-fold-search t))
1741 (if (re-search-forward
1742 (bibtex-cfield "\\(author\\)\\|\\(editor\\)" bibtex-field-text)
1744 (bibtex-autokey-change
1745 (buffer-substring-no-properties
1746 (1+ (match-beginning (+ bibtex-text-in-cfield 2)))
1747 (1- (match-end (+ bibtex-text-in-cfield 2))))
1748 bibtex-autokey-name-change-strings)
1751 (defun bibtex-autokey-get-names (namefield)
1752 ;; gathers all names in namefield into a list
1753 (let ((case-fold-search t)
1755 (while (not (equal namefield ""))
1757 (if (string-match "[ \t\n]and[ \t\n]" namefield)
1758 (setq name (substring namefield 0 (match-beginning 0))
1759 namefield (substring namefield (match-end 0)))
1760 (setq name namefield
1762 (setq names (append names (list name)))))
1765 (defun bibtex-autokey-demangle-name (fullname)
1766 ;; gets the `last' part from a well-formed name
1767 (let* (case-fold-search
1769 (if (string-match "," fullname)
1770 ;; name is of the form "von Last, First" or
1771 ;; "von Last, Jr, First"
1772 ;; --> take only the part before the comma
1774 (substring fullname 0 (match-beginning 0))))
1775 (if (string-match "^[a-z]" von-last)
1776 ;; von-last has a "von" part --> take the "last" part
1777 (if (string-match "[ \t][A-Z]" von-last)
1778 (substring von-last (1+ (match-beginning 0)))
1780 "Name %s is incorrectly formed" fullname))
1781 ;; von-last has no "von" part --> take all
1783 ;; name is of the form "First von Last"
1784 (if (string-match "[ \t]" fullname)
1785 ;; more than one token
1786 (if (string-match "[ \t][a-z]" fullname)
1787 ;; there is a "von" part
1788 ;; --> take everything after that
1790 "[ \t][A-Z]" fullname (match-end 0))
1791 (substring fullname (1+ (match-beginning 0)))
1793 "Name %s is incorrectly formed" fullname))
1794 ;; there is no "von" part --> take only the last token
1795 (if (string-match " [^ ]*$" fullname)
1796 (substring fullname (1+ (match-beginning 0)))
1797 (error "Name %s is incorrectly formed" fullname)))
1798 ;; only one token --> take it
1801 (if (string-match "[ \t]+" lastname)
1802 ;; lastname consists of two or more tokens
1803 ;; --> take only the first one
1804 (substring lastname 0 (match-beginning 0))
1806 (if bibtex-autokey-preserve-case
1808 (downcase usename))))
1810 (defun bibtex-autokey-get-namelist (namefield)
1811 ;; gets namefield, performs abbreviations on the last parts, and
1812 ;; return results as a list
1816 fullname (substring fullname (string-match "[^ \t]" fullname)))
1817 (bibtex-autokey-abbrev
1818 (bibtex-autokey-demangle-name fullname)
1819 bibtex-autokey-name-length))
1820 (bibtex-autokey-get-names namefield)))
1822 (defun bibtex-autokey-get-yearfield (min max)
1823 ;; get year field from current or maybe crossreferenced entry
1824 (let ((case-fold-search t))
1826 (if (re-search-forward
1827 (bibtex-cfield "year" "[0-9]+") max t)
1828 (buffer-substring-no-properties
1829 (match-beginning bibtex-text-in-cfield)
1830 (match-end bibtex-text-in-cfield))
1831 (if bibtex-autokey-year-use-crossref-entry
1832 (let ((crossref-field
1835 (if (re-search-forward
1837 "\\(OPT\\)?crossref" bibtex-field-text)
1839 (buffer-substring-no-properties
1841 (match-beginning (+ bibtex-text-in-cfield 1)))
1843 (match-end (+ bibtex-text-in-cfield 1))))))))
1844 (if (not (bibtex-find-entry-location crossref-field t))
1845 (let ((end-of-crefd-entry (bibtex-end-of-entry)))
1846 (bibtex-beginning-of-entry)
1847 (if (re-search-forward
1848 (bibtex-cfield "year" "[0-9]+")
1849 end-of-crefd-entry t)
1850 (buffer-substring-no-properties
1851 (match-beginning bibtex-text-in-cfield)
1852 (match-end bibtex-text-in-cfield))
1857 (defun bibtex-autokey-get-titlestring (min max)
1858 ;; get title field contents up to a terminator
1859 (let ((case-fold-search t))
1863 (if (re-search-forward
1864 (bibtex-cfield "title" bibtex-field-text) max t)
1865 (bibtex-autokey-change
1866 (buffer-substring-no-properties
1867 (match-beginning bibtex-text-in-cfield)
1868 (match-end bibtex-text-in-cfield))
1869 bibtex-autokey-titleword-change-strings)
1873 (length bibtex-autokey-title-terminators)))
1874 (while (< index numberofitems)
1876 (elt bibtex-autokey-title-terminators index) titlefield)
1878 titlefield (substring titlefield 0 (match-beginning 0))))
1879 (setq index (1+ index)))
1882 (defun bibtex-autokey-get-titles (titlestring)
1883 ;; gathers capitalized words from titlestring into a list. Ignores
1884 ;; specific words at the beginning and use only a specific amount of
1886 (let (case-fold-search
1892 (not (equal titlestring ""))
1894 (not (numberp bibtex-autokey-titlewords))
1896 (+ bibtex-autokey-titlewords
1897 bibtex-autokey-titlewords-stretch))))
1898 (if (string-match "\\b[A-Z][A-Za-z0-9]*" titlestring)
1899 (let* ((end-match (match-end 0))
1901 (if bibtex-autokey-preserve-case
1903 titlestring (match-beginning 0) end-match)
1906 titlestring (match-beginning 0) end-match)))))
1908 (not (numberp bibtex-autokey-titlewords))
1909 (< counter bibtex-autokey-titlewords))
1912 (bibtex-member-of-regexp
1914 bibtex-autokey-titleword-first-ignore))
1917 titlewords (append titlewords (list titleword))))
1920 (append titlewords-extra (list titleword))))
1922 titlestring (substring titlestring end-match)))
1923 (setq titlestring ""))
1925 counter (1+ counter)))
1926 (if (string-match "\\b[A-Z][^ ]*\\b" titlestring)
1928 (append titlewords titlewords-extra))))
1930 (defun bibtex-autokey-get-titlelist (titlestring)
1931 ;; returns all capitalized words in titlestring as a list
1932 ;; does some abbreviation on the found words
1936 (bibtex-assoc-of-regexp
1937 titleword bibtex-autokey-titleword-abbrevs)))
1940 (bibtex-autokey-abbrev
1942 bibtex-autokey-titleword-length))))
1943 (bibtex-autokey-get-titles titlestring)))
1945 (defun bibtex-generate-autokey ()
1946 ;; Generates automatically a key from the author/editor and the
1947 ;; title field. This will only work for entries where each field
1948 ;; begins on a separate line.
1949 ;; The generation algorithm works as follows:
1950 ;; 1. Use the value of `bibtex-autokey-prefix-string' as a prefix.
1951 ;; 2. If there is a non-empty author (preferred) or editor field,
1952 ;; use it as the name part of the key.
1953 ;; 3. Change any substring found in
1954 ;; `bibtex-autokey-name-change-strings' to the corresponding new
1955 ;; one (see documentation of this variable for further detail).
1956 ;; 4. For every of at least first `bibtex-autokey-names' names in
1957 ;; the name field, determine the last name. If there are maximal
1958 ;; `bibtex-autokey-names' + `bibtex-autokey-names-stretch'
1959 ;; names, all names are used.
1960 ;; 5. From every last name, take at least
1961 ;; `bibtex-autokey-name-length' characters (abort only after a
1962 ;; consonant or at a word end).
1963 ;; 6. Unless `bibtex-autokey-preserve-case' is non-nil, convert all
1964 ;; last names to lowercase letters.
1965 ;; 7. Build the name part of the key by concatenating all
1966 ;; abbreviated last names with the string
1967 ;; `bibtex-autokey-name-separator' between any two. If there are
1968 ;; more names than are used in the name part, prepend the string
1969 ;; contained in `bibtex-autokey-additional-names'.
1970 ;; 8. Build the year part of the key by truncating the contents of
1971 ;; the year field to the rightmost `bibtex-autokey-year-length'
1972 ;; digits (useful values are 2 and 4). If the year field is
1973 ;; absent, but the entry has a valid crossref field and the
1974 ;; variable `bibtex-autokey-year-use-crossref-entry' is non-nil,
1975 ;; use the year field of the crossreferenced entry instead.
1976 ;; 9. For the title part of the key change the contents of the
1977 ;; title field of the reference according to
1978 ;; `bibtex-autokey-titleword-change-strings' to the
1979 ;; corresponding new one (see documentation of this variable for
1981 ;; 10. Abbreviate the result to the string up to (but not including)
1982 ;; the first occurrence of a regexp matched by the items of
1983 ;; `bibtex-autokey-title-terminators' and delete the first
1984 ;; word if it appears in
1985 ;; `bibtex-autokey-titleword-first-ignore'. Build the title part
1986 ;; of the key by using at least the first
1987 ;; `bibtex-autokey-titlewords' capitalized words from this
1988 ;; abbreviated title. If the abbreviated title ends after
1989 ;; maximal `bibtex-autokey-titlewords' +
1990 ;; `bibtex-autokey-titlewords-stretch' capitalized words, all
1991 ;; capitalized words from the abbreviated title are used.
1992 ;; 11. Unless `bibtex-autokey-preserve-case' is non-nil, convert all
1993 ;; used titlewords to lowercase letters.
1994 ;; 12. For every used title word that appears in
1995 ;; `bibtex-autokey-titleword-abbrevs' use the corresponding
1996 ;; abbreviation (see documentation of this variable for further
1998 ;; 13. From every title word not generated by an abbreviation, take
1999 ;; at least `bibtex-autokey-titleword-length' characters (abort
2000 ;; only after a consonant or at a word end).
2001 ;; 14. Build the title part of the key by concatenating all
2002 ;; abbreviated title words with the string
2003 ;; `bibtex-autokey-titleword-separator' between any two.
2004 ;; 15. At least, to get the key, concatenate
2005 ;; `bibtex-autokey-prefix-string', the name part, the year part
2006 ;; and the title part with `bibtex-autokey-name-year-separator'
2007 ;; between the name part and the year part if both are non-empty
2008 ;; and `bibtex-autokey-year-title-separator' between the year
2009 ;; part and the title part if both are non-empty. If the year
2010 ;; part is empty, but not the other two parts,
2011 ;; `bibtex-autokey-year-title-separator' is used as well.
2012 ;; 16. If the value of `bibtex-autokey-before-presentation-hook' is
2013 ;; non-nil, it must be a function taking one argument. This
2014 ;; function is then called with the generated key as the
2015 ;; argument. The return value of this function (a string) is
2017 ;; 17. If the value of `bibtex-autokey-edit-before-use' is non-nil,
2018 ;; the key is then presented in the minibuffer to the user,
2019 ;; where it can be edited. The key given by the user is then
2022 (let* ((pnt (point))
2023 (min (bibtex-beginning-of-entry))
2024 (max (bibtex-end-of-entry))
2025 (namefield (bibtex-autokey-get-namefield min max))
2028 (let ((nl (bibtex-autokey-get-namelist namefield)))
2029 (if (or (not (numberp bibtex-autokey-names))
2031 (+ bibtex-autokey-names
2032 bibtex-autokey-names-stretch)))
2034 (setq name-etal bibtex-autokey-additional-names)
2036 (while (< (length nnl) bibtex-autokey-names)
2037 (setq nnl (append nnl (list (car nl)))
2042 (mapconcat (lambda (name) name)
2044 bibtex-autokey-name-separator)
2046 (yearfield (bibtex-autokey-get-yearfield min max))
2048 (if (equal yearfield "")
2052 (- (length yearfield) bibtex-autokey-year-length))))
2053 (titlestring (bibtex-autokey-get-titlestring min max))
2054 (titlelist (bibtex-autokey-get-titlelist titlestring))
2057 (lambda (name) name)
2059 bibtex-autokey-titleword-separator))
2062 bibtex-autokey-prefix-string
2067 (equal yearpart "")))
2068 bibtex-autokey-name-year-separator)
2074 (equal yearpart ""))
2075 (equal titlepart "")))
2076 bibtex-autokey-year-title-separator)
2078 (if bibtex-autokey-before-presentation-hook
2081 (funcall bibtex-autokey-before-presentation-hook autokey)))
2085 (defun bibtex-parse-keys (add verbose &optional abortable)
2086 ;; Sets bibtex-keys to the keys used in the whole (possibly
2087 ;; restricted) buffer (either as entry keys or as crossref entries).
2088 ;; If ADD is non-nil adds the new keys to bibtex-keys instead of
2089 ;; simply resetting it. If VERBOSE is non-nil gives messages about
2090 ;; progress. If ABORTABLE is non-nil abort on user input.
2091 (if bibtex-maintain-sorted-entries
2092 (let ((case-fold-search t)
2095 "crossref" (concat "[{\"]" bibtex-reference-key "[}\"]")))
2099 (goto-char (point-min))
2101 (bibtex-progress-message
2102 (concat (buffer-name) ": parsing reference keys")))
2104 (bibtex-skip-to-valid-entry)
2111 (bibtex-progress-message))
2115 ((looking-at bibtex-reference-head)
2118 (buffer-substring-no-properties
2119 (match-beginning bibtex-key-in-head)
2120 (match-end bibtex-key-in-head)))
2122 (m (bibtex-end-of-entry)))
2124 (if (re-search-forward crossref-field m t)
2127 (buffer-substring-no-properties
2128 (1+ (match-beginning bibtex-text-in-cfield))
2129 (1- (match-end bibtex-text-in-cfield)))))
2131 ((looking-at bibtex-string)
2134 (buffer-substring-no-properties
2135 (match-beginning bibtex-key-in-string)
2136 (match-end bibtex-key-in-string)))))
2138 (bibtex-skip-to-valid-entry)
2139 (if (not (assoc label labels))
2141 (cons (list label) labels)))
2143 (not (assoc label2 labels)))
2145 (cons (list label2) labels))))))
2146 ;; user has aborted by typing a key --> return nil
2148 ;; successful operation --> return t
2150 bibtex-buffer-last-parsed-tick (buffer-modified-tick)
2153 (bibtex-progress-message 'done))
2156 (defun bibtex-parse-buffers-stealthily ()
2157 ;; Called by run-with-idle-timer. Whenever emacs has been idle for
2158 ;; bibtex-parse-keys-timeout seconds, all BibTeX buffers (starting
2159 ;; with the current) are parsed.
2160 (let ((buffers (buffer-list)))
2162 (while (and buffers (not (input-pending-p)))
2163 (set-buffer (car buffers))
2167 (eq major-mode 'bibtex-mode)
2168 bibtex-maintain-sorted-entries
2170 (eq (buffer-modified-tick)
2171 bibtex-buffer-last-parsed-tick)))
2172 (if (bibtex-parse-keys nil t t)
2173 ;; successful operation --> remove buffer from list
2174 (setq buffers (cdr buffers)))
2175 ;; buffer is no BibTeX buffer or needs no parsing
2176 (setq buffers (cdr buffers))))))))
2178 (defun bibtex-complete (string-list &optional complete-strings)
2179 ;; Complete word fragment before point to longest prefix of one
2180 ;; string defined in STRING-LIST. If point is not after the part of
2181 ;; a word, all strings are listed. If COMPLETE-STRINGS is non-nil,
2182 ;; add the strings defined in this buffer before cursor to
2183 ;; STRING-LIST and remove surrounding delimiters if complete string
2184 ;; could be expanded.
2185 (let* ((case-fold-search t)
2187 (beg (save-excursion
2188 (re-search-backward "[ \t{\"]")
2191 (part-of-word (buffer-substring-no-properties beg end))
2192 (completions (copy-sequence string-list))
2193 (completion (save-excursion
2194 (if complete-strings
2195 (while (re-search-backward
2196 bibtex-string nil t)
2200 (buffer-substring-no-properties
2201 (match-beginning bibtex-key-in-string)
2202 (match-end bibtex-key-in-string)))
2210 (try-completion part-of-word completions))))
2211 (cond ((eq completion t)
2212 (if complete-strings
2213 ;; remove double-quotes or braces if field is no concatenation
2215 (bibtex-inside-field)
2216 (bibtex-enclosing-field)
2217 (let ((end (match-end bibtex-text-in-field)))
2218 (goto-char (match-beginning bibtex-text-in-field))
2220 (looking-at bibtex-field-string)
2221 (equal (match-end 0) end))
2222 (bibtex-remove-delimiters))))))
2224 (error "Can't find completion for \"%s\"." part-of-word))
2225 ((not (string= part-of-word completion))
2226 (delete-region beg end)
2228 (if (and (assoc completion completions)
2230 ;; remove double-quotes or braces if field is no concatenation
2232 (bibtex-inside-field)
2233 (bibtex-enclosing-field)
2234 (let ((end (match-end bibtex-text-in-field)))
2235 (goto-char (match-beginning bibtex-text-in-field))
2237 (looking-at bibtex-field-string)
2238 (equal (match-end 0) end))
2239 (bibtex-remove-delimiters))))))
2241 (message "Making completion list...")
2242 (let ((list (all-completions part-of-word completions)))
2243 (with-output-to-temp-buffer "*Completions*"
2244 (display-completion-list list)))
2245 (message "Making completion list...done")))))
2247 (defun bibtex-do-auto-fill ()
2250 (+ bibtex-entry-offset bibtex-contline-indentation) ? )))
2253 (defun bibtex-pop (arg direction)
2254 ;; generic function to be used by bibtex-pop-previous and bibtex-pop-next
2255 (let (bibtex-help-message)
2256 (bibtex-find-text nil))
2258 ;; parse current field
2259 (bibtex-inside-field)
2260 (bibtex-enclosing-field)
2261 (let ((case-fold-search t)
2262 (start-old-text (match-beginning bibtex-text-in-field))
2263 (stop-old-text (match-end bibtex-text-in-field))
2264 (start-name (match-beginning bibtex-name-in-field))
2265 (stop-name (match-end bibtex-name-in-field))
2267 (goto-char start-name)
2268 ;; construct regexp for field with same name as this one,
2269 ;; ignoring possible OPT's or ALT's
2270 (let ((matching-entry
2272 (buffer-substring-no-properties
2273 (if (looking-at "OPT\\|ALT")
2274 (+ (point) (length "OPT"))
2277 bibtex-field-text)))
2278 ;; if executed several times in a row, start each search where
2279 ;; the last one was finished
2280 (cond ((eq last-command 'bibtex-pop)
2284 (bibtex-enclosing-reference-maybe-empty-head)
2286 bibtex-pop-previous-search-point (match-beginning 0)
2287 bibtex-pop-next-search-point (point))))
2288 (if (eq direction 'previous)
2289 (goto-char bibtex-pop-previous-search-point)
2290 (goto-char bibtex-pop-next-search-point))
2291 ;; Now search for arg'th previous/next similar field
2293 ((if (eq direction 'previous)
2294 (re-search-backward matching-entry nil t arg)
2295 (re-search-forward matching-entry nil t arg))
2296 ;; Found a matching field. Remember boundaries.
2297 (setq bibtex-pop-previous-search-point (match-beginning 0))
2298 (setq bibtex-pop-next-search-point (match-end 0))
2300 (buffer-substring-no-properties
2301 (match-beginning bibtex-text-in-field)
2302 (match-end bibtex-text-in-field)))
2303 ;; change delimiters, if any changes needed
2309 (if (equal bibtex-field-delimiters 'braces)
2318 (while (string-match bibtex-field-string new-text start)
2319 (let ((beg (match-beginning 0))
2320 (end (1- (match-end 0))))
2322 (eq (aref new-text beg) old-open)
2323 (eq (aref new-text end) old-close))
2325 (aset new-text beg new-open)
2326 (aset new-text end new-close))))
2327 (setq start (match-end 0))))
2329 ;; Go back to where we started, delete old text, and pop new.
2330 (goto-char stop-old-text)
2331 (delete-region start-old-text stop-old-text)
2335 (error (concat "No "
2336 (if (eq direction 'previous)
2339 " matching BibTeX field.")))))))
2340 (let (bibtex-help-message)
2341 (bibtex-find-text nil))
2342 (setq this-command 'bibtex-pop))
2345 ;; Interactive Functions:
2348 (defun bibtex-mode ()
2349 "Major mode for editing BibTeX files.
2351 To submit a problem report, enter \\[bibtex-submit-bug-report] from a
2352 BibTeX mode buffer. This automatically sets up a mail buffer with
2353 version information already added. You just need to add a description
2354 of the problem, including a reproducable test case and send the
2358 General information on working with BibTeX mode:
2360 You should use commands as \\[bibtex-Book] to get a template for a
2361 specific entry. You should then fill in all desired fields using
2362 \\[bibtex-next-field] to jump from field to field. After having filled
2363 in all desired fields in the entry, you should clean the new entry
2364 with command \\[bibtex-clean-entry].
2366 Some features of BibTeX mode are available only by setting variable
2367 bibtex-maintain-sorted-entries to t. However, then BibTeX mode will
2368 work with buffer containing only valid (syntactical correct) entries
2369 and with entries being sorted. This is usually the case, if you have
2370 created a buffer completely with BibTeX mode and finished every new
2371 entry with \\[bibtex-clean-entry].
2373 For third party BibTeX buffers, please call the function
2374 `bibtex-convert-alien' to fully take advantage of all features of
2378 Special information:
2380 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
2382 The optional fields start with the string OPT, and are thus ignored by BibTeX.
2383 Alternatives from which only one is required start with the string ALT.
2384 The OPT or ALT string may be removed from a field with \\[bibtex-remove-OPT-or-ALT].
2385 \\[bibtex-make-field] inserts a new field after the current one.
2386 \\[bibtex-kill-field] kills the current field entirely.
2387 \\[bibtex-yank] will yank the last recently killed field after the
2389 \\[bibtex-remove-delimiters] removes the double-quotes or braces around the text of the current field.
2390 \\[bibtex-empty-field] replaces the text of the current field with the default \"\" or {}.
2392 The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2393 from all non-empty optional or alternative fields, checks that no required
2394 fields are empty, and does some formatting dependent on the value of
2395 bibtex-entry-format.
2396 Note: some functions in BibTeX mode depend on entries being in a special
2397 format (all fields beginning on separate lines), so it is usually a bad
2398 idea to remove `realign' from bibtex-entry-format.
2400 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
2401 Use \\[bibtex-next-field] to move to end of the next field.
2403 The following may be of interest as well:
2411 bibtex-complete-string
2413 bibtex-print-help-message
2414 bibtex-generate-autokey
2415 bibtex-beginning-of-entry
2417 bibtex-reposition-window
2419 bibtex-ispell-abstract
2421 bibtex-narrow-to-entry
2422 bibtex-hide-entry-bodies
2428 bibtex-convert-alien
2431 bibtex-field-delimiters
2432 bibtex-include-OPTcrossref
2433 bibtex-include-OPTkey
2434 bibtex-user-optional-fields
2436 bibtex-sort-ignore-string-entries
2437 bibtex-maintain-sorted-entries
2438 bibtex-entry-field-alist
2439 bibtex-predefined-strings
2442 ---------------------------------------------------------
2443 Entry to BibTeX mode calls the value of `bibtex-mode-hook' if that value is
2449 (kill-all-local-variables)
2450 (use-local-map bibtex-mode-map)
2451 (setq major-mode 'bibtex-mode)
2452 (setq mode-name "BibTeX")
2453 (set-syntax-table bibtex-mode-syntax-table)
2454 (setq bibtex-strings bibtex-predefined-strings)
2457 ;; collect pathnames
2458 (let* ((path (if bibtex-string-file-path
2459 bibtex-string-file-path
2463 (lambda (dirname) ;; strips off trailing slashes
2464 (let ((len (length dirname)))
2465 (if (equal (elt dirname (1- len)) "/")
2466 (substring dirname 0 (1- (1- len)))
2469 (while (string-match ":" path)
2472 (list (substring path 0 (1- (match-end 0)))))
2473 path (substring path (match-end 0))))
2474 (append actdirs (list path)))))
2475 (filename (if (string-match "\.bib$" filename)
2477 (concat filename ".bib")))
2480 (size (length dirs)))
2484 (not (file-readable-p
2486 (concat (elt dirs item) "/" filename)))))
2487 (setq item (1+ item)))
2490 (let ((case-fold-search t)
2491 (curbuf (current-buffer))
2492 (bufname (make-temp-name ""))
2493 (compl bibtex-strings))
2494 (create-file-buffer bufname)
2495 (set-buffer bufname)
2496 (insert-file-contents fullfilename)
2497 (goto-char (point-min))
2498 (while (re-search-forward bibtex-string nil t)
2502 (list (buffer-substring-no-properties
2503 (match-beginning bibtex-key-in-string)
2504 (match-end bibtex-key-in-string)))))))
2505 (kill-buffer bufname)
2507 (setq bibtex-strings compl))
2509 "File %s not in paths defined by bibtex-string-file-path variable."
2511 bibtex-string-files)
2512 (if bibtex-maintain-sorted-entries
2513 (run-with-idle-timer
2516 (bibtex-parse-keys nil t t))))
2517 ;; to get buffer parsed once if everything else (including things
2518 ;; installed in bibtex-mode-hook) has done its work
2519 (if (not bibtex-parse-idle-timer)
2520 (setq bibtex-parse-idle-timer
2521 (run-with-idle-timer
2522 bibtex-parse-keys-timeout t
2523 'bibtex-parse-buffers-stealthily)))
2524 ;; Install stealthy parse function if not already installed
2525 (set (make-local-variable 'paragraph-start) "[ \f\n\t]*$")
2526 (set (make-local-variable 'comment-start) "@Comment ")
2527 (set (make-local-variable 'comment-start-skip) "@Comment ")
2528 (set (make-local-variable 'comment-column) 0)
2529 (set (make-local-variable 'normal-auto-fill-function)
2530 'bibtex-do-auto-fill)
2531 (set (make-local-variable 'font-lock-defaults)
2532 '(bibtex-font-lock-keywords
2534 ;; Mathematical expressions should be fontified as strings
2536 ;; Quotes are field delimiters and quote-delimited
2537 ;; entries should be fontified in the same way as
2538 ;; brace-delimited ones
2540 (setq font-lock-mark-block-function
2542 (set-mark (bibtex-end-of-entry))
2543 (bibtex-beginning-of-entry)))
2544 (setq imenu-generic-expression
2545 (list (list nil bibtex-reference-head bibtex-key-in-head)))
2546 (run-hooks 'bibtex-mode-hook))
2548 (defun bibtex-submit-bug-report ()
2549 "Submit via mail a bug report on bibtex.el."
2551 (if (y-or-n-p "Do you want to submit a bug report on BibTeX mode? ")
2554 (let ((reporter-prompt-for-summary-p t))
2555 (reporter-submit-bug-report
2556 bibtex-maintainer-address
2557 (concat "bibtex.el " "(emacs 19.35)")
2559 'system-configuration
2560 'system-configuration-options
2562 'bibtex-parse-keys-timeout
2563 ;; possible general errors
2564 'bibtex-sort-ignore-string-entries
2565 'bibtex-maintain-sorted-entries
2566 'bibtex-entry-delimiters
2567 'bibtex-field-delimiters
2568 'bibtex-comma-after-last-field
2569 'bibtex-entry-offset
2570 'bibtex-field-indentation
2571 'bibtex-text-indentation
2572 'bibtex-contline-indentation
2573 'bibtex-align-at-equal-sign
2574 ;; possible sorting and parsing bugs
2575 'bibtex-entry-format
2576 'bibtex-add-entry-hook
2577 'bibtex-clean-entry-hook
2578 ;; possible cleaning error
2579 'bibtex-user-optional-fields
2580 ;; possible format error
2581 'bibtex-predefined-month-strings
2582 'bibtex-predefined-strings
2583 'bibtex-string-files
2584 'bibtex-string-file-path
2585 ;; possible format error
2586 'bibtex-font-lock-keywords
2587 ;; possible bugs regarding fontlocking
2588 'bibtex-autokey-prefix-string
2589 'bibtex-autokey-names
2590 'bibtex-autokey-names-stretch
2591 'bibtex-autokey-additional-names
2592 'bibtex-autokey-transcriptions
2593 'bibtex-autokey-name-change-strings
2594 'bibtex-autokey-name-length
2595 'bibtex-autokey-name-separator
2596 'bibtex-autokey-year-length
2597 'bibtex-autokey-year-use-crossref-entry
2598 'bibtex-autokey-titlewords
2599 'bibtex-autokey-title-terminators
2600 'bibtex-autokey-titlewords-stretch
2601 'bibtex-autokey-titleword-first-ignore
2602 'bibtex-autokey-titleword-abbrevs
2603 'bibtex-autokey-titleword-change-strings
2604 'bibtex-autokey-titleword-length
2605 'bibtex-autokey-titleword-separator
2606 'bibtex-autokey-name-year-separator
2607 'bibtex-autokey-year-title-separator
2608 'bibtex-autokey-preserve-case
2609 'bibtex-autokey-edit-before-use
2610 'bibtex-autokey-before-presentation-hook
2611 ;; possible bugs regarding automatic labels
2612 'bibtex-entry-field-alist
2613 ;; possible format error
2614 'bibtex-help-message
2615 'bibtex-include-OPTcrossref
2616 'bibtex-include-OPTkey
2617 'bibtex-field-kill-ring-max
2618 'bibtex-entry-kill-ring-max
2619 ;; user variables which shouldn't cause any errors
2624 I want to report a bug on Emacs BibTeX mode.
2625 I've read the `Bugs' section in the `Emacs' info page, so I know how
2626 to make a clear and unambiguous report. I have started a fresh Emacs
2627 via `"invocation-name " --no-init-file --no-site-file', thereafter (in
2628 case I'm reporting on a version of `bibtex.el' which is not part of
2629 the standard emacs distribution) I loaded the questionable version
2630 of `bibtex.el' with `M-x load-file', and then, to produce the buggy
2631 behaviour, I did the following:")))
2634 (defun bibtex-entry (entry-type)
2635 "Inserts a new BibTeX entry.
2636 After insertion it calls the functions in `bibtex-add-entry-hook'."
2637 (interactive (let* ((completion-ignore-case t)
2638 (e-t (completing-read
2640 bibtex-entry-field-alist
2641 nil t nil 'bibtex-entry-type-history)))
2643 (if (not bibtex-keys)
2644 (bibtex-parse-keys nil t))
2648 (if bibtex-maintain-sorted-entries
2650 (format "%s key: " entry-type)
2651 bibtex-keys nil nil nil 'bibtex-key-history)))
2652 (e (assoc-ignore-case entry-type bibtex-entry-field-alist))
2656 (error "Bibtex entry type %s not defined." entry-type))
2658 (member entry-type bibtex-include-OPTcrossref)
2660 (setq required (elt c-ref 0)
2661 optional (elt c-ref 1))
2662 (setq required (elt r-n-o 0)
2663 optional (elt r-n-o 1)))
2664 (if bibtex-maintain-sorted-entries
2665 (bibtex-find-entry-location key)
2666 (bibtex-move-outside-of-entry))
2667 (indent-to-column bibtex-entry-offset)
2668 (insert "@" entry-type (bibtex-entry-left-delimiter))
2672 (mapcar 'bibtex-make-field required)
2673 (if (member entry-type bibtex-include-OPTcrossref)
2674 (bibtex-make-optional-field '("crossref")))
2675 (if bibtex-include-OPTkey
2677 (stringp bibtex-include-OPTkey)
2678 (fboundp bibtex-include-OPTkey))
2679 (bibtex-make-optional-field
2680 (list "key" nil bibtex-include-OPTkey))
2681 (bibtex-make-optional-field '("key"))))
2682 (mapcar 'bibtex-make-optional-field optional)
2683 (mapcar 'bibtex-make-optional-field bibtex-user-optional-fields)
2684 (if bibtex-comma-after-last-field
2687 (indent-to-column bibtex-entry-offset)
2688 (insert (bibtex-entry-right-delimiter) "\n\n"))
2689 (bibtex-next-field t)
2690 (run-hooks 'bibtex-add-entry-hook)))
2692 (defun bibtex-print-help-message ()
2693 "Prints helpful information about current field in current BibTeX entry."
2695 (let* ((case-fold-search t)
2699 (condition-case errname
2700 (bibtex-enclosing-field)
2703 (error "Not on BibTeX field.")))
2704 (let ((mb (match-beginning bibtex-name-in-field))
2705 (me (match-end bibtex-name-in-field)))
2707 (buffer-substring-no-properties
2708 (if (looking-at "OPT\\|ALT")
2715 bibtex-reference-maybe-empty-head nil t)
2716 (buffer-substring-no-properties
2717 (1+ (match-beginning bibtex-type-in-head))
2718 (match-end bibtex-type-in-head))))
2720 (assoc-ignore-case reference-type
2721 bibtex-entry-field-alist))
2722 (c-r-list (elt entry-list 2))
2725 (member reference-type bibtex-include-OPTcrossref)
2728 (elt entry-list 1)))
2729 (list-of-entries (append
2730 (elt req-opt-list 0)
2731 (elt req-opt-list 1)
2732 bibtex-user-optional-fields
2735 bibtex-include-OPTcrossref)
2737 "Label of the crossreferenced entry")))
2738 (if bibtex-include-OPTkey
2740 "Key used for label creation if author and editor fields are missing"))))))
2742 (let ((comment (assoc-ignore-case field-name list-of-entries)))
2744 (message (elt comment 1))
2745 (message "NO COMMENT AVAILABLE")))))
2747 (defun bibtex-make-field (e-t &optional called-by-yank)
2748 "Makes a field named E-T in current BibTeX entry.
2749 This function is for interactive and non-interactive purposes. To call
2750 it interactively, just give it no arguments and enter the field name
2751 using the minibuffer."
2756 (let* ((reference-type
2758 (bibtex-enclosing-reference-maybe-empty-head)
2759 (buffer-substring-no-properties
2760 (1+ (match-beginning bibtex-type-in-head))
2761 (match-end bibtex-type-in-head))))
2763 (car (cdr (assoc-ignore-case
2764 reference-type bibtex-entry-field-alist))))
2767 (elt fl 0) (elt fl 1) bibtex-user-optional-fields
2768 (if bibtex-include-OPTcrossref '(("crossref" nil)))
2769 (if bibtex-include-OPTkey '(("key" nil)))))
2770 (completion-ignore-case t))
2772 "BibTeX field name: " field-list
2773 nil nil nil bibtex-field-history))))
2774 (if (not (consp e-t))
2775 (setq e-t (list e-t)))
2776 (if (equal (length e-t) 1)
2777 (setq e-t (append e-t (list ""))))
2778 (if (equal (length e-t) 2)
2779 (setq e-t (append e-t (list ""))))
2780 (let ((name (if (elt e-t 3)
2781 (concat "ALT" (car e-t))
2783 (if (or (interactive-p) called-by-yank)
2784 (let (bibtex-help-message)
2785 (bibtex-find-text nil t)
2786 (if (looking-at "[}\"]")
2790 (+ bibtex-entry-offset bibtex-field-indentation))
2792 (if bibtex-align-at-equal-sign
2794 (+ bibtex-entry-offset (- bibtex-text-indentation 2))))
2796 (if (not bibtex-align-at-equal-sign)
2798 (+ bibtex-entry-offset bibtex-text-indentation)))
2799 (insert (if called-by-yank
2801 (bibtex-field-left-delimiter))
2802 (let ((init (elt e-t 2)))
2809 (error "%s is neither a string nor a function." init))))
2812 (bibtex-field-right-delimiter)))
2814 (forward-char -1))))
2816 (defun bibtex-beginning-of-entry ()
2817 "Move to beginning of BibTeX entry.
2818 If inside an entry, move to the beginning of it, otherwise move to the
2819 beginning of the previous entry.
2820 If called from a program, this function returns the new location of point."
2822 (skip-chars-forward " \t")
2823 (if (looking-at "@")
2825 (re-search-backward "^[ \t]*@" nil 'move))
2827 (defun bibtex-end-of-entry ()
2828 "Move to end of BibTeX entry.
2829 If inside an entry, move to the end of it, otherwise move to the end
2830 of the previous entry.
2831 If called from a program, this function returns the new location of point."
2833 (let ((case-fold-search t)
2835 (concat "[ \t]*@[ \t]*\\("
2838 (concat "\\(" (car type) "\\)"))
2839 bibtex-entry-field-alist
2843 (pnt (bibtex-beginning-of-entry))
2846 ((looking-at "[ \t]*@[ \t]*string[ \t\n]*[({]")
2848 (re-search-forward bibtex-string nil t)
2849 (equal (match-beginning 0) pnt)))
2851 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")
2852 (goto-char (match-end 0))
2855 (if (not (re-search-forward ")[ \t]*\n\n" nil 'move))
2858 (if (not (re-search-forward "}[ \t]*\n\n" nil 'move))
2864 (goto-char (match-beginning 0))
2866 ((looking-at valid-entry-head)
2867 (bibtex-search-reference t nil t)
2868 (if (not (equal (match-beginning 0) pnt))
2872 (message "Not on a known BibTeX entry."))
2877 (error "Syntactical incorrect entry starts here."))))
2880 (defun bibtex-reposition-window (arg)
2881 "Make the current BibTeX entry visible."
2885 (/ (+ (bibtex-beginning-of-entry) (bibtex-end-of-entry)) 2))
2888 (defun bibtex-mark-entry ()
2889 "Put mark at beginning, point at end of current BibTeX entry."
2891 (set-mark (bibtex-beginning-of-entry))
2892 (bibtex-end-of-entry))
2894 (defun bibtex-count-entries (&optional count-string-entries)
2895 "Count number of entries in current buffer or region.
2896 With prefix argument it counts all entries, otherwise it counts all
2898 If mark is active it counts entries in region, if not in whole buffer."
2904 (bibtex-beginning-of-first-entry)))
2910 (bibtex-sort-ignore-string-entries
2911 (not count-string-entries)))
2913 (narrow-to-region start-point end-point)
2914 (goto-char start-point)
2917 (setq number (1+ number)))))
2918 (message (concat (if mark-active "Region" "Buffer")
2919 " contains %d entries.") number)
2922 (defun bibtex-ispell-entry ()
2923 "Spell whole BibTeX entry."
2925 (ispell-region (bibtex-beginning-of-entry) (bibtex-end-of-entry)))
2927 (defun bibtex-ispell-abstract ()
2928 "Spell abstract of BibTeX entry."
2930 (let ((case-fold-search t)
2931 (pnt (bibtex-end-of-entry)))
2932 (bibtex-beginning-of-entry)
2935 (bibtex-cfield "abstract" bibtex-field-text) pnt t))
2936 (error "No abstract in entry.")))
2937 (ispell-region (match-beginning bibtex-text-in-cfield)
2938 (match-end bibtex-text-in-cfield)))
2940 (defun bibtex-narrow-to-entry ()
2941 "Narrow buffer to current BibTeX entry."
2945 (bibtex-beginning-of-entry) (bibtex-end-of-entry))))
2947 (defun bibtex-hide-entry-bodies (&optional arg)
2948 "Hide all lines between first and last BibTeX entries not beginning with @.
2949 With argument, show all text."
2952 (bibtex-beginning-of-first-entry)
2953 (let ((buffer-read-only nil))
2955 (subst-char-in-region (point) (point-max) ?\r ?\n t)
2957 (subst-char-in-region
2960 (re-search-forward "[\n\r]@" nil t)
2965 (setq selective-display (not arg)))))
2967 (defun bibtex-sort-buffer ()
2968 "Sort BibTeX buffer alphabetically by key.
2969 Text outside of BibTeX entries is not affected. If
2970 `bibtex-sort-ignore-string-entries' is non-nil, @String entries will be
2975 (bibtex-beginning-of-first-entry)
2977 (goto-char (point-max))
2978 (bibtex-end-of-entry)))
2979 (bibtex-skip-to-valid-entry)
2983 'bibtex-skip-to-valid-entry
2985 'bibtex-end-of-entry
2986 ;; STARTKEY function
2988 (let ((case-fold-search t))
2989 (re-search-forward bibtex-reference-head)
2990 (buffer-substring-no-properties
2991 (match-beginning bibtex-key-in-head)
2992 (match-end bibtex-key-in-head)))))))
2994 (defun bibtex-find-entry-location (entry-name &optional ignore-dups)
2995 "Looking for place to put the BibTeX entry named ENTRY-NAME.
2996 Performs a binary search (therefore, buffer is assumed to be in sorted
2997 order, without duplicates (see \\[bibtex-validate]), if it is
2998 not, bibtex-find-entry-location will fail). If entry-name is already
2999 used as a reference key, an error is signaled. However, if optional
3000 variable IGNORE-DUPS is non-nil, no error messages about duplicate
3001 entries are signaled, but the error handling is assumed to be made in
3002 the calling function.
3003 The value is nil if an duplicate entry error occurred,
3004 and t in all other cases."
3005 (let* ((case-fold-search t)
3008 (bibtex-beginning-of-first-entry)
3009 (bibtex-skip-to-valid-entry)
3010 (bibtex-end-of-entry)))
3013 (bibtex-beginning-of-last-entry)
3014 (bibtex-skip-to-valid-entry t)
3018 (done (>= left right))
3022 (setq actual-point (/ (+ left right) 2))
3023 (goto-char actual-point)
3024 (bibtex-skip-to-valid-entry t)
3027 (re-search-forward bibtex-reference-head)
3028 (buffer-substring-no-properties
3029 (match-beginning bibtex-key-in-head)
3030 (match-end bibtex-key-in-head))))
3032 ((string-lessp entry-name actual-key)
3033 (setq new (bibtex-beginning-of-entry))
3034 (if (equal right new)
3037 ((string-lessp actual-key entry-name)
3038 (setq new (bibtex-end-of-entry))
3039 (if (equal left new)
3042 ((string-equal actual-key entry-name)
3045 (if (not ignore-dups)
3047 (bibtex-beginning-of-entry)
3048 (error "Entry with key `%s' already exists." entry-name))))))
3051 (bibtex-beginning-of-entry)
3055 (if (looking-at bibtex-reference-head)
3056 (buffer-substring-no-properties
3057 (match-beginning bibtex-key-in-reference)
3058 (match-end bibtex-key-in-reference))))
3061 (string-lessp actual-key entry-name))
3062 ;; buffer contains no valid entries or
3063 ;; greater than last entry --> append
3065 (bibtex-end-of-entry)
3067 (newline (forward-line 2)))
3068 (beginning-of-line))
3072 (defun bibtex-validate (&optional test-thoroughly)
3073 "Validate if buffer or region is syntactically correct.
3074 Only known reference types are checked, so you can put comments
3076 With optional argument TEST-THOROUGHLY non-nil it checks for absence of
3077 required fields and questionable month fields as well.
3078 If mark is active, it validates current region, if not whole buffer.
3079 Returns t if test was successful, nil otherwise."
3083 (case-fold-search t)
3086 "@[ \t]*\\(\\(string\\)\\|"
3089 (concat "\\(" (car type) "\\)"))
3090 bibtex-entry-field-alist
3097 (bibtex-beginning-of-first-entry)))
3103 (narrow-to-region start-point end-point)
3104 ;; looking if entries fit syntactical structure
3105 (goto-char start-point)
3106 (bibtex-progress-message "Checking syntactical structure")
3107 (while (re-search-forward "^[ \t]*@" nil t)
3108 (bibtex-progress-message)
3112 (looking-at valid-bibtex-entry)))
3113 (if (not must-match)
3115 (let (bibtex-sort-ignore-string-entries)
3116 (bibtex-skip-to-valid-entry))
3117 (if (equal (point) p)
3123 (bibtex-current-line)
3124 "Syntax error (check esp. commas, braces, and quotes)")
3127 (bibtex-progress-message 'done)
3129 (setq syntax-error t)
3130 ;; looking for correct sort order and duplicates (only if
3131 ;; there were no syntax errors)
3132 (if bibtex-maintain-sorted-entries
3134 (goto-char start-point)
3135 (bibtex-progress-message "Checking correct sort order")
3138 (bibtex-progress-message)
3139 (cond ((or (not previous)
3140 (string< previous current))
3141 (setq previous current))
3142 ((string-equal previous current)
3145 (cons (list (bibtex-current-line)
3146 "Duplicate key with previous")
3149 (setq previous current
3151 (cons (list (bibtex-current-line)
3152 "Entries out of order")
3154 (bibtex-progress-message 'done)))
3157 (goto-char start-point)
3158 (bibtex-progress-message
3159 "Checking required fields and month fields")
3160 (let ((bibtex-sort-ignore-string-entries t)
3166 (concat "\\(" (car mon) "\\)"))
3167 bibtex-predefined-month-strings
3172 (bibtex-progress-message)
3173 (let* ((beg (bibtex-beginning-of-entry))
3174 (end (bibtex-end-of-entry))
3178 (bibtex-search-reference nil end)
3180 (buffer-substring-no-properties
3181 (1+ (match-beginning bibtex-type-in-head))
3182 (match-end bibtex-type-in-head))
3183 bibtex-entry-field-alist)))
3184 (req (copy-sequence (elt (elt entry-list 1) 0)))
3185 (creq (copy-sequence (elt (elt entry-list 2) 0)))
3188 (while (re-search-forward bibtex-field end t)
3190 (buffer-substring-no-properties
3191 (match-beginning bibtex-name-in-field)
3192 (match-end bibtex-name-in-field))))
3193 (if (and (equal (downcase field-name) "month")
3196 (buffer-substring-no-properties
3197 (match-beginning bibtex-text-in-field)
3198 (match-end bibtex-text-in-field))))
3203 (bibtex-current-line)
3204 "Questionable month field (delimited string)")
3208 (delete (assoc-ignore-case field-name req) req)
3210 (delete (assoc-ignore-case field-name creq) creq))
3211 (if (equal (downcase field-name) "crossref")
3212 (setq crossref-there t))))
3215 (if (or (> (length req) 1)
3216 (and (= (length req) 1)
3217 (not (elt (car req) 3))))
3218 ;; two (or more) fields missed or one field
3219 ;; missed and this isn't flagged alternative
3220 ;; (notice that this fails if there are more
3221 ;; than two alternatives in a BibTeX entry,
3222 ;; which isn't the case momentarily)
3226 (list (save-excursion
3227 (bibtex-beginning-of-entry)
3228 (bibtex-current-line))
3234 (bibtex-progress-message 'done)))))
3237 (let ((bufnam (buffer-name))
3238 (dir default-directory))
3242 (< (car a) (car b)))))
3243 (let ((pop-up-windows t))
3244 (pop-to-buffer nil t))
3246 (get-buffer-create "*BibTeX validation errors*") t)
3247 ;; don't use switch-to-buffer-other-window, since this
3248 ;; doesn't allow the second parameter NORECORD
3249 (setq default-directory dir)
3250 (toggle-read-only -1)
3252 (delete-region (point-min) (point-max))
3253 (goto-char (point-min))
3255 "BibTeX mode command `bibtex-validate'\n"
3257 "Maybe undetected errors due to syntax errors. Correct and validate again."
3262 bufnam ":" (number-to-string (elt (car error-list) 0))
3263 ": " (elt (car error-list) 1) "\n")
3264 (setq error-list (cdr error-list)))
3265 (compilation-parse-errors nil nil)
3266 (setq compilation-old-error-list compilation-error-list)
3267 ;; this is necessary to avoid reparsing of buffer if you
3268 ;; switch to compilation buffer and enter
3269 ;; `compile-goto-error'
3270 (set-buffer-modified-p nil)
3271 (toggle-read-only 1)
3272 (goto-char (point-min))
3277 (message "Region is syntactically correct")
3278 (message "Buffer is syntactically correct"))
3281 (defun bibtex-next-field (arg)
3282 "Finds end of text of next BibTeX field; with arg, to its beginning."
3284 (bibtex-inside-field)
3285 (let ((start (point)))
3288 (bibtex-enclosing-field)
3289 (goto-char (match-end 0))
3295 (bibtex-find-text arg t))
3297 (defun bibtex-find-text (arg &optional as-if-interactive silent)
3298 "Go to end of text of current field; with ARG, go to beginning."
3300 (bibtex-inside-field)
3301 (if (bibtex-enclosing-field (or (interactive-p) as-if-interactive))
3305 (goto-char (match-beginning bibtex-text-in-field))
3306 (if (looking-at "[{\"]")
3308 (goto-char (match-end bibtex-text-in-field))
3310 (= (preceding-char) ?})
3311 (= (preceding-char) ?\"))
3313 (if bibtex-help-message
3314 (bibtex-print-help-message)))
3317 ((looking-at bibtex-string)
3320 (match-beginning bibtex-text-in-string)
3321 (match-end bibtex-text-in-string))))
3322 ((looking-at bibtex-reference-maybe-empty-head)
3325 (match-beginning bibtex-key-in-head)
3329 (error "Not on BibTeX field."))))))
3331 (defun bibtex-remove-OPT-or-ALT ()
3332 "Removes the string starting optional/alternative fields.
3333 Aligns text and goes thereafter to end of text."
3335 (let ((case-fold-search t))
3336 (bibtex-inside-field)
3337 (bibtex-enclosing-field)
3339 (goto-char (match-beginning bibtex-name-in-field))
3340 (if (looking-at "OPT\\|ALT")
3342 (delete-char (length "OPT"))
3343 ;; make field non-OPT
3344 (search-forward "=")
3346 (delete-horizontal-space)
3347 (if bibtex-align-at-equal-sign
3348 (indent-to-column (- bibtex-text-indentation 2))
3350 (search-forward "=")
3351 (delete-horizontal-space)
3352 (if bibtex-align-at-equal-sign
3354 (indent-to-column bibtex-text-indentation)))))
3355 (bibtex-inside-field)))
3357 (defun bibtex-remove-delimiters ()
3358 "Removes \"\" or {} around string."
3360 (let ((case-fold-search t))
3362 (bibtex-inside-field)
3363 (bibtex-enclosing-field)
3364 (let ((start (match-beginning bibtex-text-in-field))
3365 (stop (match-end bibtex-text-in-field)))
3367 (while (re-search-forward bibtex-field-string stop t)
3368 (let ((beg (copy-marker (match-beginning 0)))
3369 (end (copy-marker (match-end 0))))
3371 (if (looking-at "[{\"]")
3375 (if (looking-at "[}\"]")
3376 (delete-char 1))))))))
3378 (defun bibtex-kill-field (&optional copy-only)
3379 "Kills the entire enclosing BibTeX field.
3380 With prefix arg, copy the current field to `bibtex-field-kill-ring,'
3381 but do not actually kill it."
3384 (case-fold-search t))
3385 (bibtex-inside-field)
3386 (bibtex-enclosing-field)
3387 (let ((the-end (match-end 0))
3388 (the-beginning (match-beginning 0)))
3390 (skip-chars-forward " \t\n,")
3392 bibtex-field-kill-ring
3396 (buffer-substring-no-properties
3397 (match-beginning bibtex-name-in-field)
3398 (match-end bibtex-name-in-field))
3399 (buffer-substring-no-properties
3400 (match-beginning bibtex-text-in-field)
3401 (match-end bibtex-text-in-field)))
3402 bibtex-field-kill-ring))
3403 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
3405 (nthcdr (1- bibtex-field-kill-ring-max) bibtex-field-kill-ring)
3407 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring)
3410 (delete-region the-beginning the-end)
3411 (let (bibtex-help-message)
3412 (bibtex-find-text nil t t)))))
3413 (setq bibtex-last-kill-command 'field))
3415 (defun bibtex-copy-field-as-kill ()
3417 (bibtex-kill-field t))
3419 (defun bibtex-kill-entry (&optional copy-only)
3420 "Kill the entire enclosing BibTeX reference entry.
3421 With prefix arg copy the current reference entry to
3422 `bibtex-entry-kill-ring', but do not actually kill it."
3425 (case-fold-search t)
3426 (beg (bibtex-beginning-of-entry))
3429 (bibtex-end-of-entry)
3430 (if (re-search-forward
3431 bibtex-reference-maybe-empty-head nil 'move)
3432 (goto-char (match-beginning 0)))
3435 bibtex-entry-kill-ring
3437 (list 'entry (buffer-substring-no-properties beg end))
3438 bibtex-entry-kill-ring))
3439 (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
3441 (nthcdr (1- bibtex-entry-kill-ring-max) bibtex-entry-kill-ring)
3443 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring)
3446 (delete-region beg end)))
3447 (setq bibtex-last-kill-command 'entry))
3449 (defun bibtex-copy-entry-as-kill ()
3451 (bibtex-kill-entry t))
3453 (defun bibtex-yank (&optional n)
3454 "Reinsert the last BibTeX item.
3455 More precisely, reinsert the field or entry killed or yanked most recently.
3456 With argument N, reinsert the Nth most recently killed BibTeX item.
3457 See also the command \\[bibtex-yank-pop]]."
3459 (bibtex-insert-current-kill (1- n))
3460 (setq this-command 'bibtex-yank))
3462 (defun bibtex-yank-pop (n)
3463 "Replace just-yanked killed BibTeX item with a different.
3464 This command is allowed only immediately after a `bibtex-yank' or a
3466 At such a time, the region contains a reinserted previously killed
3467 BibTeX item. `bibtex-yank-pop' deletes that item and inserts in its
3468 place a different killed BibTeX item.
3470 With no argument, the previous kill is inserted.
3471 With argument N, insert the Nth previous kill.
3472 If N is negative, this is a more recent kill.
3474 The sequence of kills wraps around, so that after the oldest one
3475 comes the newest one."
3477 (if (not (eq last-command 'bibtex-yank))
3478 (error "Previous command was not a BibTeX yank"))
3479 (setq this-command 'bibtex-yank)
3480 (let ((inhibit-read-only t))
3481 (delete-region (point) (mark t))
3482 (bibtex-insert-current-kill n)))
3484 (defun bibtex-empty-field ()
3485 "Delete the text part of the current field, replace with empty text."
3487 (bibtex-inside-field)
3488 (bibtex-enclosing-field)
3489 (goto-char (match-beginning bibtex-text-in-field))
3490 (delete-region (point) (match-end bibtex-text-in-field))
3491 (insert (concat (bibtex-field-left-delimiter)
3492 (bibtex-field-right-delimiter)) )
3493 (bibtex-find-text t))
3495 (defun bibtex-pop-previous (arg)
3496 "Replace text of current field with the similar field in previous entry.
3497 With arg, goes up ARG entries. Repeated, goes up so many times. May be
3498 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
3500 (bibtex-pop arg 'previous))
3502 (defun bibtex-pop-next (arg)
3503 "Replace text of current field with the text of similar field in next entry.
3504 With arg, goes down ARG entries. Repeated, goes down so many times. May be
3505 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3507 (bibtex-pop arg 'next))
3509 (defun bibtex-clean-entry (&optional new-label called-by-reformat)
3510 "Finish editing the current BibTeX entry and clean it up.
3511 Checks that no required fields are empty and formats entry dependent
3512 on the value of bibtex-entry-format.
3513 If label of entry is empty or a prefix argument is given, calculate a
3514 new entry label (note: this only will work if fields in entry begin on
3515 separate lines prior to calling bibtex-clean-entry or if 'realign is
3516 contained in bibtex-entry-format).
3517 Don't call this on `string' or `preamble' entries.
3518 At end of the cleaning process, the functions in
3519 bibtex-clean-entry-hook are called with region narrowed to entry."
3521 (bibtex-format-entry)
3522 (let* ((case-fold-search t)
3523 (eob (bibtex-end-of-entry))
3525 (bibtex-beginning-of-entry)
3526 (if (re-search-forward
3527 bibtex-reference-head eob t)
3528 (buffer-substring-no-properties
3529 (match-beginning bibtex-key-in-head)
3530 (match-end bibtex-key-in-head))))))
3536 (if bibtex-autokey-edit-before-use
3537 (read-from-minibuffer
3538 "Key to use: " (bibtex-generate-autokey) nil nil
3539 'bibtex-key-history)
3540 (bibtex-generate-autokey))))
3541 (bibtex-beginning-of-entry)
3542 (re-search-forward bibtex-reference-maybe-empty-head)
3543 (if (match-beginning bibtex-key-in-head)
3544 (delete-region (match-beginning bibtex-key-in-head)
3545 (match-end bibtex-key-in-head)))
3547 (let* ((start (bibtex-beginning-of-entry))
3549 (bibtex-end-of-entry)
3550 (if (re-search-forward
3551 bibtex-reference-maybe-empty-head nil 'move)
3552 (goto-char (match-beginning 0)))
3554 (entry (buffer-substring start end)))
3555 (delete-region start end)
3559 (not bibtex-maintain-sorted-entries)
3560 (bibtex-find-entry-location autokey t))))
3563 (bibtex-beginning-of-entry)
3564 (re-search-forward bibtex-reference-head)
3567 "New inserted reference yields duplicate key."))))))))
3568 (if (not called-by-reformat)
3572 (bibtex-beginning-of-entry) (bibtex-end-of-entry))
3573 (bibtex-parse-keys t nil)
3574 (run-hooks 'bibtex-clean-entry-hook)))))
3576 (defun bibtex-fill-entry ()
3577 "Fill current entry.
3578 Realigns entry, so that every field starts on a separate line. Field
3579 names appear in column `bibtex-field-indentation', field text starts in
3580 column bibtex-text-indentation and continuation lines start here, too.
3581 If `bibtex-align-at-equal-sign' is non-nil, align equal signs also."
3583 (let ((pnt (copy-marker (point)))
3584 (end (copy-marker (bibtex-end-of-entry))))
3585 (bibtex-beginning-of-entry)
3586 (bibtex-delete-whitespace)
3587 (indent-to-column bibtex-entry-offset)
3588 (while (re-search-forward bibtex-field end t)
3590 (copy-marker (match-beginning 0)))
3592 (copy-marker (match-end 0)))
3594 (copy-marker (match-beginning bibtex-name-in-field)))
3596 (copy-marker (match-end bibtex-name-in-field))))
3597 (goto-char begin-field)
3599 (bibtex-delete-whitespace)
3603 (+ bibtex-entry-offset bibtex-field-indentation))
3604 (re-search-forward "[ \t\n]*=" end)
3607 (if bibtex-align-at-equal-sign
3609 (+ bibtex-entry-offset (- bibtex-text-indentation 2)))
3612 (bibtex-delete-whitespace)
3613 (if bibtex-align-at-equal-sign
3615 (indent-to-column bibtex-text-indentation))
3616 (while (re-search-forward "[ \t\n]+" end-field 'move)
3617 (replace-match " "))
3618 (bibtex-do-auto-fill)))
3619 (if (looking-at ",")
3621 (bibtex-delete-whitespace)
3624 (indent-to-column bibtex-entry-offset)
3627 (defun bibtex-reformat (&optional additional-options called-by-convert-alien)
3628 "Reformat all BibTeX entries in buffer or region.
3629 With prefix argument, read options for reformatting from minibuffer.
3630 With C-u C-u prefix argument, reuse previous answers (if any) again.
3631 If mark is active it reformats entries in region, if not in whole buffer."
3633 (let* ((pnt (point))
3634 (use-previous-options
3635 (and (equal (prefix-numeric-value additional-options) 16)
3636 (or bibtex-reformat-previous-options
3637 bibtex-reformat-previous-labels)))
3638 (bibtex-entry-format
3639 (if additional-options
3640 (if use-previous-options
3641 bibtex-reformat-previous-options
3643 bibtex-reformat-previous-options
3648 called-by-convert-alien
3650 "Realign entries (recommended for files not created by BibTeX mode)? "))
3653 "Remove empty optional and alternative fields? ")
3656 "Remove delimiters around pure numerical fields? ")
3658 (if (y-or-n-p (concat
3659 (if bibtex-comma-after-last-field
3662 " comma at end of entry? "))
3665 "Replace double page dashes by single ones? ")
3668 "Force delimiters? ")
3671 "Unify case of entry types and field names? ")
3675 (if additional-options
3676 (if use-previous-options
3677 bibtex-reformat-previous-labels
3679 bibtex-reformat-previous-labels
3680 (y-or-n-p "Generate automatically new reference labels? ")))))
3681 bibtex-autokey-edit-before-use
3682 (bibtex-sort-ignore-string-entries t)
3687 (bibtex-beginning-of-first-entry)
3688 (bibtex-skip-to-valid-entry)
3696 "[ \t\n]+\\(@[ \t]*\\("
3699 (concat "\\(" (car type) "\\)"))
3700 bibtex-entry-field-alist
3704 (narrow-to-region start-point end-point)
3705 (if (memq 'realign bibtex-entry-format)
3707 (goto-char (point-min))
3708 (while (re-search-forward valid-bibtex-entry nil t)
3709 (replace-match "\n\\1"))))
3710 (goto-char start-point)
3711 (bibtex-progress-message "Formatting" 1)
3714 (bibtex-progress-message)
3715 (bibtex-clean-entry labels labels)
3716 (if (memq 'realign bibtex-entry-format)
3718 (bibtex-end-of-entry)
3719 (bibtex-delete-whitespace)
3721 (bibtex-progress-message 'done))
3724 bibtex-maintain-sorted-entries
3725 (not called-by-convert-alien))
3727 (bibtex-sort-buffer)
3728 (setq bibtex-keys nil)
3729 (bibtex-parse-keys nil t t)))
3732 (defun bibtex-convert-alien (&optional do-additional-reformatting)
3733 "Converts an alien BibTeX buffer to be fully usable by BibTeX mode.
3734 If a file doesn't confirm with some standards used by BibTeX mode,
3735 some of the high-level features of BibTeX mode won't be available.
3736 This function tries to convert current buffer to confirm with these standards.
3737 With prefix argument DO-ADDITIONAL-REFORMATTING
3738 non-nil, read options for reformatting entries from minibuffer."
3740 (message "Starting to validate buffer...")
3742 (goto-char (point-min))
3743 (while (re-search-forward "[ \t\n]+@" nil t)
3744 (replace-match "\n@"))
3746 "If errors occur, correct them and call `bibtex-convert-alien' again")
3748 (if (let (mark-active
3749 bibtex-maintain-sorted-entries)
3752 (message "Starting to reformat entries...")
3754 (bibtex-reformat do-additional-reformatting t)
3755 (if bibtex-maintain-sorted-entries
3757 (message "Starting to sort buffer...")
3758 (bibtex-sort-buffer)))
3759 (goto-char (point-max))
3760 (message "Buffer is now parsable. Please save it."))))
3762 (defun bibtex-complete-string ()
3763 "Complete word fragment before point to longest prefix of a defined string.
3764 If point is not after the part of a word, all strings are listed.
3765 Remove surrounding delimiters if complete string could be expanded."
3767 (bibtex-complete bibtex-strings t))
3769 (defun bibtex-complete-key ()
3770 "Complete word fragment before point to longest prefix of a defined key.
3771 If point is not after the part of a word, all keys are listed. This
3772 function is most useful in completing crossref entries."
3774 (if (not bibtex-keys)
3775 (bibtex-parse-keys nil t))
3776 (bibtex-complete bibtex-keys))
3778 (defun bibtex-Article ()
3780 (bibtex-entry "Article"))
3782 (defun bibtex-Book ()
3784 (bibtex-entry "Book"))
3786 (defun bibtex-Booklet ()
3788 (bibtex-entry "Booklet"))
3790 (defun bibtex-InBook ()
3792 (bibtex-entry "InBook"))
3794 (defun bibtex-InCollection ()
3796 (bibtex-entry "InCollection"))
3798 (defun bibtex-InProceedings ()
3800 (bibtex-entry "InProceedings"))
3802 (defun bibtex-Manual ()
3804 (bibtex-entry "Manual"))
3806 (defun bibtex-MastersThesis ()
3808 (bibtex-entry "MastersThesis"))
3810 (defun bibtex-Misc ()
3812 (bibtex-entry "Misc"))
3814 (defun bibtex-PhdThesis ()
3816 (bibtex-entry "PhdThesis"))
3818 (defun bibtex-Proceedings ()
3820 (bibtex-entry "Proceedings"))
3822 (defun bibtex-TechReport ()
3824 (bibtex-entry "TechReport"))
3826 (defun bibtex-Unpublished ()
3828 (bibtex-entry "Unpublished"))
3830 (defun bibtex-String ()
3832 (if (not bibtex-keys)
3833 (bibtex-parse-keys nil t))
3836 bibtex-maintain-sorted-entries
3837 (not bibtex-sort-ignore-string-entries))
3839 "String key: " bibtex-keys nil nil nil 'bibtex-key-history))))
3841 bibtex-maintain-sorted-entries
3842 (not bibtex-sort-ignore-string-entries))
3843 (bibtex-find-entry-location key)
3844 (bibtex-move-outside-of-entry))
3845 (indent-to-column bibtex-entry-offset)
3849 (bibtex-entry-left-delimiter)
3851 bibtex-maintain-sorted-entries
3852 (not bibtex-sort-ignore-string-entries))
3855 (bibtex-field-left-delimiter)
3856 (bibtex-field-right-delimiter)
3857 (bibtex-entry-right-delimiter)
3862 bibtex-maintain-sorted-entries
3863 (not bibtex-sort-ignore-string-entries))
3864 (+ (length "@String{") (length key) (length " = {"))
3865 (length "@String{")))))
3867 (defun bibtex-Preamble ()
3869 (bibtex-move-outside-of-entry)
3870 (indent-to-column bibtex-entry-offset)
3873 (bibtex-entry-left-delimiter)
3874 (bibtex-entry-right-delimiter)
3880 ;; Make BibTeX a Feature
3884 ;;; bibtex.el ends here