1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Stefan Schoef <schoef@informatik.uni-oldenburg.de>
6 ;; Bengt Martensson <ubrinf!mond!bengt>
7 ;; Mark Shapiro <shapiro@corto.inria.fr>
8 ;; Mike Newton <newton@gumby.cs.caltech.edu>
9 ;; Aaron Larson <alarson@src.honeywell.com>
10 ;; Maintainer: Stefan Schoef <schoef@informatik.uni-oldenburg.de>
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
27 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;; Major mode for editing and validating BibTeX files.
33 ;; See documentation for function bibtex-mode (or type "\M-x describe-mode"
34 ;; when you are in bibtex-mode).
37 ;; Distribute texinfo file.
40 ;; 1. using regular expressions to match the entire BibTeX entry dies
41 ;; on long entries (e.g. those containing abstracts) since
42 ;; the length of regular expression matches is fairly limited.
43 ;; 2. Calling bibtex-find-text in a string entry results in the
44 ;; error message "Can't find enclosing Bibtex field" instead of
45 ;; moving to the empty string. [reported by gernot@cs.unsw.oz.au]
46 ;; 3. Quotes inside quote-parenthesized fields (like
47 ;; `author = "Stefan Sch{\"o}f"') break bibtex-validate-buffer.
48 ;; Further, you must use braces here, if you want to set
49 ;; bibtex-maintain-sorted-entries to a non-nil value.
51 ;; (current keeper: schoef@informatik.uni-oldenburg.de
52 ;; previous: alarson@src.honeywell.com)
57 (defvar bibtex-field-left-delimiter "{"
58 "*Set this to { or \" according to your personal preferences.
59 This variable is buffer local.")
60 (make-variable-buffer-local 'bibtex-field-left-delimiter)
62 (defvar bibtex-field-right-delimiter "}"
63 "*Set this to } or \" according to your personal preferences.
64 This variable is buffer local.")
65 (make-variable-buffer-local 'bibtex-field-right-delimiter)
67 (defvar bibtex-include-OPTcrossref '("InProceedings" "InCollection")
68 "*All entries listed here will have an OPTcrossref field.")
70 (defvar bibtex-include-OPTkey t
71 "*If non-nil, all entries will have an OPTkey field.")
73 (defvar bibtex-include-OPTannote t
74 "*If non-nil, all entries will have an OPTannote field.")
76 (defvar bibtex-mode-user-optional-fields nil
77 "*List of optional fields the user wants to have always present.
78 Entries should be lists of strings with two elements (first element =
79 name of the field, second element = comment to appear in the echo area).")
81 (defvar bibtex-clean-entry-zap-empty-opts t
82 "*If non-nil, bibtex-clean-entry will delete all empty optional fields.")
84 (defvar bibtex-sort-ignore-string-entries t
85 "*If non-nil, BibTeX @STRING entries are not sort-significant.
86 That means they are ignored when determining ordering of the buffer
87 (e.g. sorting, locating alphabetical position for new entries, etc.).
88 This variable is buffer local.")
89 (make-variable-buffer-local 'bibtex-sort-ignore-string-entries)
91 (defvar bibtex-maintain-sorted-entries nil
92 "*If non-nil, bibtex-mode maintains all BibTeX entries in sorted order.
93 Setting this variable to nil will strip off some comfort (e.g. TAB
94 completion for reference keys in minibuffer, automatic detection of
95 duplicates) from bibtex-mode. See also bibtex-sort-ignore-string-entries.
96 This variable is buffer local.")
97 (make-variable-buffer-local 'bibtex-maintain-sorted-entries)
99 (defvar bibtex-entry-field-alist
101 ("Article" . (((("author" "Author1 [and Author2 ...] [and others]")
102 ("title" "Title of the article (BibTeX converts it to lowercase)")
103 ("journal" "Name of the journal (use string, remove braces)")
104 ("year" "Year of publication"))
105 (("volume" "Volume of the journal")
106 ("number" "Number of the journal")
107 ("month" "Month of the publication as a string (remove braces)")
108 ("pages" "Pages in the journal")
109 ("note" "Remarks to be put at the end of the \\bibitem")))
110 ((("author" "Author1 [and Author2 ...] [and others]")
111 ("title" "Title of the article (BibTeX converts it to lowercase)"))
112 (("journal" "Name of the journal (use string, remove braces)")
113 ("year" "Year of publication")
114 ("volume" "Volume of the journal")
115 ("number" "Number of the journal")
116 ("month" "Month of the publication as a string (remove braces)")
117 ("pages" "Pages in the journal")
118 ("note" "Remarks to be put at the end of the \\bibitem")))))
119 ("Book" . (((("author" "Author1 [and Author2 ...] [and others]")
120 ("title" "Title of the book")
121 ("publisher" "Publishing company")
122 ("year" "Year of publication"))
123 (("editor" "Editor1 [and Editor2 ...] [and others]")
124 ("volume" "Volume of the book in the series")
125 ("number" "Number of the book in a small series (overwritten by volume)")
126 ("series" "Series in which the book appeared")
127 ("address" "Address of the publisher")
128 ("edition" "Edition of the book as a capitalized English word")
129 ("month" "Month of the publication as a string (remove braces)")
130 ("note" "Remarks to be put at the end of the \\bibitem")))))
131 ("Booklet" . (((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
132 (("author" "Author1 [and Author2 ...] [and others]")
133 ("howpublished" "The way in which the booklet was published")
134 ("address" "Address of the publisher")
135 ("year" "Year of publication")
136 ("month" "Month of the publication as a string (remove braces)")
137 ("note" "Remarks to be put at the end of the \\bibitem")))))
138 ("InBook" . (((("author" "Author1 [and Author2 ...] [and others]")
139 ("title" "Title of the book")
140 ("chapter" "Chapter in the book")
141 ("publisher" "Publishing company")
142 ("year" "Year of publication"))
143 (("editor" "Editor1 [and Editor2 ...] [and others]")
144 ("volume" "Volume of the book in the series")
145 ("number" "Number of the book in a small series (overwritten by volume)")
146 ("series" "Series in which the book appeared")
147 ("address" "Address of the publisher")
148 ("edition" "Edition of the book as a capitalized English word")
149 ("month" "Month of the publication as a string (remove braces)")
150 ("pages" "Pages in the book")
151 ("type" "Word to use instead of \"chapter\"")
152 ("note" "Remarks to be put at the end of the \\bibitem")))
153 ((("author" "Author1 [and Author2 ...] [and others]")
154 ("title" "Title of the book")
155 ("chapter" "Chapter in the book"))
156 (("publisher" "Publishing company")
157 ("year" "Year of publication")
158 ("editor" "Editor1 [and Editor2 ...] [and others]")
159 ("volume" "Volume of the book in the series")
160 ("number" "Number of the book in a small series (overwritten by volume)")
161 ("series" "Series in which the book appeared")
162 ("address" "Address of the publisher")
163 ("edition" "Edition of the book as a capitalized English word")
164 ("month" "Month of the publication as a string (remove braces)")
165 ("pages" "Pages in the book")
166 ("type" "Word to use instead of \"chapter\"")
167 ("note" "Remarks to be put at the end of the \\bibitem")))))
168 ("InCollection" . (((("author" "Author1 [and Author2 ...] [and others]")
169 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
170 ("booktitle" "Name of the book")
171 ("publisher" "Publishing company")
172 ("year" "Year of publication"))
173 (("editor" "Editor1 [and Editor2 ...] [and others]")
174 ("volume" "Volume of the book in the series")
175 ("number" "Number of the book in a small series (overwritten by volume)")
176 ("series" "Series in which the book appeared")
177 ("chapter" "Chapter in the book")
178 ("type" "Word to use instead of \"chapter\"")
179 ("address" "Address of the publisher")
180 ("edition" "Edition of the book as a capitalized English word")
181 ("month" "Month of the publication as a string (remove braces)")
182 ("pages" "Pages in the book")
183 ("note" "Remarks to be put at the end of the \\bibitem")))
184 ((("author" "Author1 [and Author2 ...] [and others]")
185 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
186 ("booktitle" "Name of the book"))
187 (("publisher" "Publishing company")
188 ("year" "Year of publication")
189 ("editor" "Editor1 [and Editor2 ...] [and others]")
190 ("volume" "Volume of the book in the series")
191 ("number" "Number of the book in a small series (overwritten by volume)")
192 ("series" "Series in which the book appeared")
193 ("chapter" "Chapter in the book")
194 ("type" "Word to use instead of \"chapter\"")
195 ("address" "Address of the publisher")
196 ("edition" "Edition of the book as a capitalized English word")
197 ("month" "Month of the publication as a string (remove braces)")
198 ("pages" "Pages in the book")
199 ("note" "Remarks to be put at the end of the \\bibitem")))))
200 ("InProceedings" . (((("author" "Author1 [and Author2 ...] [and others]")
201 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
202 ("booktitle" "Name of the conference proceedings")
203 ("year" "Year of publication"))
204 (("editor" "Editor1 [and Editor2 ...] [and others]")
205 ("volume" "Volume of the conference proceedings in the series")
206 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
207 ("series" "Series in which the conference proceedings appeared")
208 ("organization" "Sponsoring organization of the conference")
209 ("publisher" "Publishing company, its location")
210 ("address" "Location of the Proceedings")
211 ("month" "Month of the publication as a string (remove braces)")
212 ("pages" "Pages in the conference proceedings")
213 ("note" "Remarks to be put at the end of the \\bibitem")))
214 ((("author" "Author1 [and Author2 ...] [and others]")
215 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
216 ("booktitle" "Name of the conference proceedings"))
217 (("editor" "Editor1 [and Editor2 ...] [and others]")
218 ("volume" "Volume of the conference proceedings in the series")
219 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
220 ("series" "Series in which the conference proceedings appeared")
221 ("year" "Year of publication")
222 ("organization" "Sponsoring organization of the conference")
223 ("publisher" "Publishing company, its location")
224 ("address" "Location of the Proceedings")
225 ("month" "Month of the publication as a string (remove braces)")
226 ("pages" "Pages in the conference proceedings")
227 ("note" "Remarks to be put at the end of the \\bibitem")))))
228 ("Manual" . (((("title" "Title of the manual"))
229 (("author" "Author1 [and Author2 ...] [and others]")
230 ("organization" "Publishing organization of the manual")
231 ("address" "Address of the organization")
232 ("edition" "Edition of the manual as a capitalized English word")
233 ("year" "Year of publication")
234 ("month" "Month of the publication as a string (remove braces)")
235 ("note" "Remarks to be put at the end of the \\bibitem")))))
237 ("MastersThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
238 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
239 ("school" "School where the master\'s thesis was written")
240 ("year" "Year of publication"))
241 (("address" "Address of the school (if not part of field \"school\") or country")
242 ("type" "Type of the master\'s thesis")
243 ("month" "Month of the publication as a string (remove braces)")
244 ("note" "Remarks to be put at the end of the \\bibitem")))))
246 (("author" "Author1 [and Author2 ...] [and others]")
247 ("title" "Title of the reference (BibTeX converts it to lowercase)")
248 ("howpublished" "The way in which the reference was published")
249 ("year" "Year of publication")
250 ("month" "Month of the publication as a string (remove braces)")
251 ("note" "Remarks to be put at the end of the \\bibitem")))))
252 ("PhdThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
253 ("title" "Title of the PhD. thesis")
254 ("school" "School where the PhD. thesis was written")
255 ("year" "Year of publication"))
256 (("address" "Address of the school (if not part of field \"school\") or country")
257 ("type" "Type of the PhD. thesis")
258 ("month" "Month of the publication as a string (remove braces)")
259 ("note" "Remarks to be put at the end of the \\bibitem")))))
260 ("Proceedings" . (((("title" "Title of the conference proceedings")
261 ("year" "Year of publication"))
262 (("editor" "Editor1 [and Editor2 ...] [and others]")
263 ("volume" "Volume of the conference proceedings in the series")
264 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
265 ("series" "Series in which the conference proceedings appeared")
266 ("publisher" "Publishing company, its location")
267 ("organization" "Sponsoring organization of the conference")
268 ("address" "Location of the Proceedings")
269 ("month" "Month of the publication as a string (remove braces)")
270 ("note" "Remarks to be put at the end of the \\bibitem")))))
271 ("TechReport" . (((("author" "Author1 [and Author2 ...] [and others]")
272 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
273 ("institution" "Sponsoring institution of the report")
274 ("year" "Year of publication"))
275 (("type" "Type of the report (if other than \"technical report\")")
276 ("number" "Number of the technical report")
277 ("address" "Address of the institution (if not part of field \"institution\") or country")
278 ("month" "Month of the publication as a string (remove braces)")
279 ("note" "Remarks to be put at the end of the \\bibitem")))))
280 ("Unpublished" . (((("author" "Author1 [and Author2 ...] [and others]")
281 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)")
282 ("note" "Remarks to be put at the end of the \\bibitem"))
283 (("year" "Year of publication")
284 ("month" "Month of the publication as a string (remove braces)")))))
287 "Defines reference types and their associated fields.
289 (entry-name (required optional) (crossref-required crossref-optional))
291 If the third element is nil, the first pair is always to be used.
292 If not, the second pair is to be used in the case of presence of a
293 crossref field and the third in the case of absence.
294 Required , optional, crossref-required and crossref-optional are lists.
295 Each element of these lists is a list of strings with two elements
296 (first element = name of the field,
297 second element = comment to appear in the echo area).")
299 (defvar bibtex-predefined-strings
301 ("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug")
302 ("sep") ("oct") ("nov") ("dec")
303 ("acmcs") ("acta") ("cacm") ("ibmjrd") ("ibmsj") ("ieeese")
304 ("ieeetc") ("ieeetcad") ("ipl") ("jacm") ("jcss") ("scp")
305 ("sicomp") ("tcs") ("tocs") ("tods") ("tog") ("toms") ("toois")
308 "Alist of string definitions.
309 Should contain the strings defined in the BibTeX style files. Each
310 element is a list with just one element: the string.")
312 (defvar bibtex-string-files nil
313 "*List of BibTeX files containing string definitions.
314 Those files must be specified using pathnames relative to the
315 directories specified in $BIBINPUTS. This variable is only evaluated
316 when bibtex-mode is entered (i. e. when loading the BibTeX file).")
318 (defvar bibtex-help-message t
319 "*If not nil print help messages in the echo area on entering a new field.")
321 (defvar bibtex-autokey-names 1
322 "*Number of names to use for the automatically generated reference key.
323 If this is set to anything but a number, all names are used.
324 See the documentation of function bibtex-generate-autokey for further detail.")
326 (defvar bibtex-autokey-name-change-strings
327 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
329 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
331 "Alist of (old-regexp new-string) pairs.
332 Any part of name matching a old-regexp is replaced by new-string.
333 Case of the old-regexp is significant. All regexps are tried in the
334 order in which they appear in the list, so be sure to avoid recursion here.
335 See the documentation of function bibtex-generate-autokey for further detail.")
337 (defvar bibtex-autokey-name-length 'infty
338 "*Number of characters from name to incorporate into key.
339 If this is set to anything but a number, all characters are used.
340 See the documentation of function bibtex-generate-autokey for further detail.")
342 (defvar bibtex-autokey-name-separator ""
343 "*String that comes between any two names in the key.
344 See the documentation of function bibtex-generate-autokey for further detail.")
346 (defvar bibtex-autokey-year-length 2
347 "*Number of rightmost digits from the year field yo incorporate into key.
348 See the documentation of function bibtex-generate-autokey for further detail.")
350 (defvar bibtex-autokey-titlewords 5
351 "*Number of title words to use for the automatically generated reference key.
352 If this is set to anything but a number, all title words are used.
353 See the documentation of function bibtex-generate-autokey for further detail.")
355 (defvar bibtex-autokey-title-terminators
356 '("\\." "!" "\\?" ":" ";" "---")
357 "*Regexp list defining the termination of the main part of the title.
358 Case of the regexps is ignored.
359 See the documentation of function bibtex-generate-autokey for further detail.")
361 (defvar bibtex-autokey-titlewords-stretch 2
362 "*Number of words that can additionally be used from the title.
363 These words are used only, if a sentence from the title can be ended then.
364 See the documentation of function bibtex-generate-autokey for further detail.")
366 (defvar bibtex-autokey-titleword-first-ignore
367 '("a" "an" "on" "the" "eine?" "der" "die" "das")
368 "*Determines words that may begin a title but are not to be used in the key.
369 Each item of the list is a regexp. If the first word of the title matchs a
370 regexp from that list, it is not included in the title, even if it is
371 capitalized. Regexps in the list must be entered using lowercase letters.")
373 (defvar bibtex-autokey-titleword-abbrevs nil
374 "*Determines exceptions to the usual abbreviation mechanism.
375 A list of (old-regexp new-string) pairs.
376 Use all lowercase letters for old-regexp.
377 See the documentation of function bibtex-generate-autokey for further detail.")
379 (defvar bibtex-autokey-titleword-change-strings
380 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
382 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
384 "Alist of (old-regexp new-string) pairs.
385 Any part of title word matching a old-regexp is replaced by new-string.
386 Case of the old-regexp is significant.
387 See the documentation of function bibtex-generate-autokey for further detail.")
389 (defvar bibtex-autokey-titleword-length 5
390 "*Number of characters from title words to incorporate into key.
391 If this is set to anything but a number, all characters are used.
392 See the documentation of function bibtex-generate-autokey for further detail.")
394 (defvar bibtex-autokey-titleword-separator "_"
395 "*String to be put between the title words.
396 See the documentation of function bibtex-generate-autokey for further detail.")
398 (defvar bibtex-autokey-name-year-separator ""
399 "*String to be put between name part and year part of key.
400 See the documentation of function bibtex-generate-autokey for further detail.")
402 (defvar bibtex-autokey-year-title-separator ":_"
403 "*String to be put between name part and year part of key.
404 See the documentation of function bibtex-generate-autokey for further detail.")
406 (defvar bibtex-autokey-edit-before-use t
407 "*If non-nil, user is allowed to edit the generated key before it is used.")
409 (defvar bibtex-font-lock-keywords
413 '("^\\([ \t]*OPT[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
414 1 font-lock-comment-face)
415 ;; optional field names
416 '("^\\([ \t]*[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
417 1 font-lock-function-name-face)
419 '("^@[A-Za-z]*[({]\\([^\n,]*\\),"
420 1 font-lock-string-face)
423 "*Fonts to use in BibTeX mode")
426 ;; Syntax Table, Keybindings and BibTeX Entry List
427 (defvar bibtex-mode-syntax-table
428 (let ((st (make-syntax-table)))
429 ;; [alarson:19920214.1004CST] make double quote a string quote
430 (modify-syntax-entry ?\" "\"" st)
431 (modify-syntax-entry ?$ "$$ " st)
432 (modify-syntax-entry ?% "< " st)
433 (modify-syntax-entry ?' "w " st)
434 (modify-syntax-entry ?@ "w " st)
435 (modify-syntax-entry ?\\ "\\" st)
436 (modify-syntax-entry ?\f "> " st)
437 (modify-syntax-entry ?\n "> " st)
438 (modify-syntax-entry ?~ " " st)
441 (defvar bibtex-mode-map
442 (let ((km (make-sparse-keymap)))
444 (define-key km "\t" 'bibtex-find-text)
445 (define-key km "\n" 'bibtex-next-field)
446 (define-key km "\M-\t" 'bibtex-complete-string)
447 (define-key km "\C-c\"" 'bibtex-remove-double-quotes-or-braces)
448 (define-key km "\C-c{" 'bibtex-remove-double-quotes-or-braces)
449 (define-key km "\C-c}" 'bibtex-remove-double-quotes-or-braces)
450 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
451 (define-key km "\C-c?" 'bibtex-print-help-message)
452 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
453 (define-key km "\C-c\C-n" 'bibtex-pop-next)
454 (define-key km "\C-c\C-k" 'bibtex-kill-optional-field)
455 (define-key km "\C-c\C-d" 'bibtex-empty-field)
456 (define-key km "\C-c$" 'bibtex-ispell-entry)
457 (define-key km "\M-\C-a" 'bibtex-beginning-of-entry)
458 (define-key km "\M-\C-e" 'bibtex-end-of-entry)
459 (define-key km "\C-c\C-b" 'bibtex-entry)
460 (define-key km "\C-c\C-q" 'bibtex-hide-entry-bodies)
461 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
462 (define-key km "\C-c\C-rw" 'widen)
463 (define-key km "\C-c\C-o" 'bibtex-remove-OPT)
465 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
466 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
467 (define-key km "\C-c\C-eI" 'bibtex-InBook)
468 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
469 (define-key km "\C-c\C-e\C-b" 'bibtex-InBook)
470 (define-key km "\C-c\C-eb" 'bibtex-Book)
471 (define-key km "\C-c\C-eB" 'bibtex-Booklet)
472 (define-key km "\C-c\C-e\C-c" 'bibtex-InCollection)
473 (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
474 (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
475 (define-key km "\C-c\C-eM" 'bibtex-Misc)
476 (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
477 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
478 (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
479 (define-key km "\C-c\C-e\M-p" 'bibtex-preamble)
480 (define-key km "\C-c\C-e\C-s" 'bibtex-string)
481 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
482 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
485 (define-key bibtex-mode-map [menu-bar bibtex-edit]
486 (cons "BibTeX-Edit" (make-sparse-keymap "BibTeX-Edit")))
487 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-print-help-message]
488 '("Help about Current Field" . bibtex-print-help-message))
489 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-complete-string]
490 '("String Complete" . bibtex-complete-string))
491 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-next-field]
492 '("Next Field" . bibtex-next-field))
493 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-find-text]
494 '("End of Field" . bibtex-find-text))
495 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-pop-previous]
496 '("Snatch from Similar Preceding Field" . bibtex-pop-previous))
497 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-pop-next]
498 '("Snatch from Similar Following Field" . bibtex-pop-next))
499 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-remove-OPT]
500 '("Remove OPT" . bibtex-remove-OPT))
501 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-remove-double-quotes-or-braces]
502 '("Remove Quotes or Braces" . bibtex-remove-double-quotes-or-braces))
503 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-clean-entry]
504 '("Clean Up Entry" . bibtex-clean-entry))
505 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-sort-entries]
506 '("Sort Entries" . bibtex-sort-entries))
507 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-validate-buffer]
508 '("Validate Entries" . bibtex-validate-buffer))
510 (define-key bibtex-mode-map [menu-bar entry-types]
511 (cons "Entry-Types" (make-sparse-keymap "Entry-Types")))
512 (define-key bibtex-mode-map [menu-bar entry-types bibtex-preamble]
513 '("Preamble" . bibtex-preamble))
514 (define-key bibtex-mode-map [menu-bar entry-types bibtex-string]
515 '("String" . bibtex-string))
516 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Misc]
517 '("Miscellaneous" . bibtex-Misc))
518 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Unpublished]
519 '("Unpublished" . bibtex-Unpublished))
520 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Manual]
521 '("Technical Manual" . bibtex-Manual))
522 (define-key bibtex-mode-map [menu-bar entry-types bibtex-TechReport]
523 '("Technical Report" . bibtex-TechReport))
524 (define-key bibtex-mode-map [menu-bar entry-types bibtex-MastersThesis]
525 '("Master's Thesis" . bibtex-MastersThesis))
526 (define-key bibtex-mode-map [menu-bar entry-types bibtex-PhdThesis]
527 '("PhD. Thesis" . bibtex-PhdThesis))
528 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Booklet]
529 '("Booklet (Bound, but no Publisher/Institution)" . bibtex-Booklet))
530 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Book]
531 '("Book" . bibtex-Book))
532 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Proceedings]
533 '("Conference Proceedings" . bibtex-Proceedings))
534 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InBook]
535 '("Chapter or Pages in a Book" . bibtex-InBook))
536 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InCollection]
537 '("Article in a Collection" . bibtex-InCollection))
538 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InProceedings]
539 '("Article in Conference Proceedings" . bibtex-InProceedings))
540 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Article]
541 '("Article in Journal" . bibtex-Article))
545 ;; Internal Variables
547 (defvar bibtex-pop-previous-search-point nil)
548 ;; Next point where bibtex-pop-previous starts looking for a similar
551 (defvar bibtex-pop-next-search-point nil)
552 ;; Next point where bibtex-pop-next starts looking for a similar entry.
554 (defvar bibtex-completion-candidates nil)
555 ;; Candidates for bibtex-complete-string. Initialized from
556 ;; bibtex-predefined-strings and bibtex-string-files. This variable is
558 (make-variable-buffer-local 'bibtex-completion-candidates)
560 (defvar bibtex-keys nil)
561 ;; Candidates for TAB completion when entering a reference key using
562 ;; the minibuffer. Initialized in bibtex-mode and updated for each
563 ;; new entry. This variable is buffer-local.
564 (make-variable-buffer-local 'bibtex-keys)
566 (defvar bibtex-buffer-last-parsed-for-keys-tick nil)
567 ;; Remembers the value returned by buffer-modified-tick when buffer
568 ;; was parsed for keys the last time.
569 (make-variable-buffer-local 'bibtex-keys)
573 ;; Functions to Parse the BibTeX Entries
575 (defun bibtex-cfield (name text)
576 ;; Create a regexp for a BibTeX field of name NAME and text TEXT.
577 (concat ",[ \t\n]*\\("
579 "\\)[ \t\n]*=[ \t\n]*\\("
582 (defconst bibtex-name-in-cfield 1)
583 ;; The regexp subexpression number of the name part in bibtex-cfield.
585 (defconst bibtex-text-in-cfield 2)
586 ;; The regexp subexpression number of the text part in bibtex-cfield.
588 (defconst bibtex-field-name "[A-Za-z_-][A-Za-z0-9_-]*")
589 ;; Regexp defining the name part of a BibTeX field.
591 (defconst bibtex-field-const "[0-9A-Za-z][A-Za-z0-9:_+-]*"
592 "Format of a bibtex field constant.")
594 (defconst bibtex-field-string
597 "{\\(\\({\\(\\({[^}]*}\\)\\|\\([^{}]\\)\\)*}\\)\\|\\([^{}]\\)\\)*}"
598 ;; maximal twice nested {}
600 "\"[^\"]*[^\\\\]\"\\|\"\""
602 ;; Match either a string or an empty string.
604 (defconst bibtex-field-string-or-const
605 (concat bibtex-field-const "\\|" bibtex-field-string))
606 ;; Match either bibtex-field-string or bibtex-field-const.
608 (defconst bibtex-field-text
610 "\\(" bibtex-field-string-or-const "\\)"
611 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*"))
612 ;; Regexp defining the text part of a BibTeX field: either a string,
613 ;; or an empty string, or a constant followed by one or more # /
616 (defconst bibtex-field
617 (bibtex-cfield bibtex-field-name bibtex-field-text))
618 ;; Regexp defining the format of a BibTeX field.
620 (defconst bibtex-name-in-field bibtex-name-in-cfield)
621 ;; The regexp subexpression number of the name part in BibTeX-field.
623 (defconst bibtex-text-in-field bibtex-text-in-cfield)
624 ;; The regexp subexpression number of the text part in BibTeX-field.
626 (defconst bibtex-reference-type "@[A-Za-z]+")
627 ;; Regexp defining the type part of a BibTeX reference entry.
629 (defconst bibtex-reference-key "[A-Za-z][A-Za-z0-9.:;?!`'/*@_+-]*")
630 ;; Regexp defining the label part of a BibTeX reference entry.
632 (defconst bibtex-reference-head
633 (concat "^\\( \\|\t\\)*\\("
634 bibtex-reference-type
638 ;; Regexp defining format of the header line of a BibTeX reference
641 (defconst bibtex-reference-maybe-empty-head
642 (concat bibtex-reference-head "?"))
643 ;; Regexp defining format of the header line of a maybe empty
644 ;; BibTeX reference entry (without reference key).
646 (defconst bibtex-type-in-head 2)
647 ;; The regexp subexpression number of the type part in
648 ;; bibtex-reference-head.
650 (defconst bibtex-key-in-head 3)
651 ;; The regexp subexpression number of the key part in
652 ;; bibtex-reference-head.
654 (defconst bibtex-reference
655 (concat bibtex-reference-head
656 "\\([ \t\n]*" bibtex-field "\\)*"
657 "[ \t\n]*,?[ \t\n]*[})]"))
658 ;; Regexp defining the format of a BibTeX reference entry.
660 (defconst bibtex-type-in-reference bibtex-type-in-head)
661 ;; The regexp subexpression number of the type part in
664 (defconst bibtex-key-in-reference bibtex-key-in-head)
665 ;; The regexp subexpression number of the key part in
668 (defconst bibtex-string
669 (concat "^[ \t]*@[sS][tT][rR][iI][nN][gG][ \t\n]*[({][ \t\n]*\\("
671 "\\)[ \t\n]*=[ \t\n]*\\("
674 ;; Regexp defining the format of a BibTeX string entry.
676 (defconst bibtex-key-in-string 1)
677 ;; The regexp subexpression of the name part in bibtex-string.
679 (defconst bibtex-text-in-string 2)
680 ;; The regexp subexpression of the text part in bibtex-string.
682 (defconst bibtex-name-alignment 2)
683 ;; Alignment for the name part in BibTeX fields. Chosen on aesthetic
686 (defconst bibtex-text-alignment (length " organization = "))
687 ;; Alignment for the text part in BibTeX fields. Equal to the space
688 ;; needed for the longest name part.
694 (defun assoc-ignore-case (string alist)
695 ;; Return non-nil if STRING is `equal' to the car of an element of
696 ;; LIST. Comparison is done with case ignored. The value is actually
697 ;; the element of LIST whose car is `equal' to STRING.
698 (or (assoc string alist)
702 (downcase (car (car alist))))))
703 (setq alist (cdr alist)))
706 (defun member-of-regexp (string list)
707 ;; Return non-nil if STRING is exactly matched by an element of
708 ;; LIST. This function is influenced by the actual value of
709 ;; `case-fold-search'. The value is actually the tail of LIST whose
710 ;; car matches STRING.
716 (concat "^" (car list) "$")
718 (setq list (cdr list)))
721 (defun assoc-of-regexp (string alist)
722 ;; Return non-nil if STRING is exactly matched by the car of an
723 ;; element of LIST. This function is influenced by the actual value
724 ;; of `case-fold-search'. The value is actually the element of LIST
725 ;; whose car matches STRING.
731 (concat "^" (car (car alist)) "$")
733 (setq alist (cdr alist)))
736 (defun skip-whitespace-and-comments ()
737 (let ((md (match-data)))
739 (while (cond ((looking-at "\\s>+\\|\\s +")
741 ;; NOTE: also checked end-comment. In latex and
742 ;; lisp modes, newline is an end comment, but it
743 ;; should also be a whitespace char.
744 (goto-char (match-end 0)))
745 ;; If looking at beginning of comment, skip to end.
747 (re-search-forward "\\s>"))))
748 (store-match-data md))))
750 (defun map-bibtex-entries (fun)
751 ;; Call FUN for each BibTeX entry starting with the current. Do this
752 ;; to the end of the file. FUN is called with one argument, the key
753 ;; of the entry, and with point inside the entry. If
754 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be called
755 ;; for @string entries.
756 (bibtex-beginning-of-entry)
757 (while (re-search-forward bibtex-reference-head nil t)
758 (if (and bibtex-sort-ignore-string-entries
759 (string-equal "@string"
760 (downcase (buffer-substring-no-properties
761 (match-beginning bibtex-type-in-head)
762 (match-end bibtex-type-in-head)))))
764 (funcall fun (buffer-substring-no-properties
765 (match-beginning bibtex-key-in-head)
766 (match-end bibtex-key-in-head))))))
768 (defun bibtex-flash-head ()
769 ;; Flash at BibTeX reference head before point, if exists.
771 (cond ((re-search-backward bibtex-reference-head (point-min) t)
772 (goto-char (match-beginning bibtex-type-in-head))
773 (setq flash (match-end bibtex-key-in-reference)))
776 (skip-chars-backward " \t")
779 (skip-chars-forward " \t")))
780 (if (pos-visible-in-window-p (point))
783 (buffer-substring (point) flash)))))
785 (defun bibtex-move-outside-of-entry ()
786 ;; Make sure we are outside of a BibTeX entry.
788 (= (point) (point-max))
789 (= (point) (point-min))
790 (looking-at "[ \n]*@")
795 (forward-paragraph)))
796 (re-search-forward "[ \t\n]*" (point-max) t))
798 (defun beginning-of-first-bibtex-entry ()
799 ;; Go to the beginning of the first BibTeX entry in buffer.
800 (goto-char (point-min))
802 ((re-search-forward "^@" nil 'move)
807 (message "Warning: No BibTeX entries found!"))))
809 (defun bibtex-inside-field ()
810 ;; Try to avoid point being at end of a BibTeX field.
812 (skip-chars-backward " \t")
813 (cond ((= (preceding-char) ?,)
816 (= (preceding-char) ?})
817 (= (preceding-char) ?\"))
820 (defun bibtex-enclosing-field ()
821 ;; Search for BibTeX field enclosing point. Point moves to end of
822 ;; field; also, use match-beginning and match-end to parse the field.
823 ;; sct@dcs.edinburgh.ac.uk
824 (let ((old-point (point)))
825 (condition-case errname
826 (bibtex-enclosing-regexp bibtex-field)
828 (goto-char old-point)
829 (error "Can't find enclosing BibTeX field.")))))
831 (defun bibtex-enclosing-reference ()
832 ;; Search for BibTeX reference enclosing point. Point moves to
833 ;; beginning of reference. Beginning/end of reference is given by
834 ;; (match-beginning/match-end 0).
835 (let ((old-point (point)))
837 (re-search-backward bibtex-reference-head (point-min) t))
839 (error "Can't find enclosing BibTeX reference.")
840 (goto-char old-point)))
841 (goto-char (match-beginning bibtex-type-in-head))
844 (re-search-forward bibtex-reference (point-max) t))
846 (error "Can't find enclosing BibTeX reference.")
847 (goto-char old-point))
850 (defun bibtex-enclosing-reference-maybe-empty-head ()
851 ;; Search for BibTeX reference enclosing point. Point moves to
852 ;; beginning of reference. Beginning/end of reference is given by
853 ;; (match-beginning/match-end 0).
854 (let ((old-point (point)))
857 bibtex-reference-maybe-empty-head (point-min) t))
859 (error "Can't find enclosing BibTeX reference.")
860 (goto-char old-point)))
861 (goto-char (match-beginning bibtex-type-in-head))
866 bibtex-reference-maybe-empty-head
867 "\\([ \t\n]*" bibtex-field "\\)*"
868 "[ \t\n]*,?[ \t\n]*[})]")
871 (error "Can't find enclosing BibTeX reference.")
872 (goto-char old-point))
875 (defun bibtex-enclosing-regexp (regexp)
876 ;; Search for REGEXP enclosing point. Point moves to end of
877 ;; REGEXP. See also match-beginning and match-end. If an enclosing
878 ;; REGEXP is not found, signals search-failed; point is left in an
879 ;; undefined location.
880 ;; Doesn't something like this exist already?
881 ; compute reasonable limits for the loop
882 (let* ((initial (point))
883 (right (if (re-search-forward regexp (point-max) t)
889 (if (re-search-backward regexp (point-min) t)
892 ; within the prescribed limits, loop until a match is found
894 (re-search-forward regexp right nil 1)
895 (if (> (match-beginning 0) initial)
896 (signal 'search-failed (list regexp)))
897 (while (<= (match-end 0) initial)
898 (re-search-forward regexp right nil 1)
899 (if (> (match-beginning 0) initial)
900 (signal 'search-failed (list regexp))))
903 (defun bibtex-autokey-change (string change-list)
904 ;; Returns a string where some regexps are changed according to
905 ;; change-list. Every item of change-list is an (old-regexp
907 (let ((return-string string)
910 (len (length change-list))
913 (setq change-item (elt change-list index))
914 (while (string-match (car change-item) return-string)
917 (concat (substring return-string 0 (match-beginning 0))
919 (substring return-string (match-end 0)))))
920 (setq index (1+ index)))
923 (defun bibtex-autokey-abbrev (string len)
924 ;; Returns an abbreviation of string with at least len
925 ;; characters. String is aborted only after a consonant or at the
926 ;; word end. If len is not a number, string is returned unchanged.
927 (let* ((string-length (length string))
928 (len (if (numberp len)
929 (min len string-length)
931 (return-string (if (numberp len)
932 (substring string 0 len)))
934 (vowels '(?a ?e ?i ?o ?u ?A ?E ?I ?O ?U)))
938 (< index string-length)
939 (member (elt return-string
940 (1- (length return-string)))
942 (setq return-string (concat return-string
944 string index (1+ index)))
949 (defun bibtex-generate-autokey ()
950 "Generates automatically a key from the author/editor and the title field.
951 The generation algorithm works as follows:
952 1. If there is a non-empty author (preferred) or editor field,
953 use it for the name part of the key.
954 2. Change any substring found in `bibtex-autokey-name-change-strings'
955 to the corresponding new one (see documentation of this variable
957 3. For every of the first `bibtex-autokey-names' names in the
958 \"name\" field, determine the last name.
959 4. From every last name, take at least `bibtex-autokey-name-length'
960 characters (abort only after a consonant or at a word end).
961 5. Build the name part of the key by concatenating all abbreviated last
962 names with the string `bibtex-autokey-name-separator' between
964 6. Build the year part of the key by truncating the contents of the
965 \"year\" field to the rightmost `bibtex-autokey-year-length'
966 digits (useful values are 2 and 4).
967 7. For the title part of the key change the contents of the \"title\"
968 field of the reference according to
969 `bibtex-autokey-titleword-change-strings' to the corresponding
970 new one (see documentation of this variable for further detail).
971 8. Abbreviate the result to the string up to (but not including) the
972 first occurence of a regexp matched by the items of
973 `bibtex-autokey-title-terminators' and delete the first
974 word if it appears in `bibtex-autokey-titleword-first-ignore'.
975 Build the title part of the key by using at least the first
976 `bibtex-autokey-titlewords' capitalized words from this
977 abbreviated title. If the abbreviated title ends after maximal
978 `bibtex-autokey-titlewords' + `bibtex-autokey-titlewords-stretch'
979 capitalized words, all capitalized words from the abbreviated title
981 9. For every used title word that appears in
982 `bibtex-autokey-titleword-abbrevs' use the corresponding abbreviation
983 (see documentation of this variable for further detail).
984 10. From every title word not generated by an abbreviation, take at
985 least `bibtex-autokey-titleword-length' characters (abort only after
986 a consonant or at a word end).
987 11. Build the title part of the key by concatenating all abbreviated
988 title words with the string `bibtex-autokey-titleword-separator'
990 12. At least, to get the key, concatenate the name part, the year part
991 and the title part with `bibtex-autokey-name-year-separator'
992 between the name and the year if both are non-empty and
993 `bibtex-autokey-year-title-separator' between the year and
994 the title if both are non-empty."
999 (bibtex-beginning-of-entry)
1003 (bibtex-end-of-entry)
1009 (re-search-forward "^[ \t]*author[ \t]*=" max t)
1010 (re-search-forward "^[ \t]*editor[ \t]*=" max t))
1011 (let* (bibtex-help-message
1013 (bibtex-find-text t)
1016 (bibtex-find-text nil)
1018 (bibtex-autokey-change
1019 (buffer-substring-no-properties start end)
1020 bibtex-autokey-name-change-strings))
1026 (bibtex-autokey-abbrev
1027 (if (string-match "," fullname)
1028 (substring fullname 0 (match-beginning 0))
1030 (if (string-match " [^ ]*$" fullname)
1032 fullname (1+ (match-beginning 0)))
1034 bibtex-autokey-name-length)))
1035 ;; Gather all names into a list
1039 (not (equal namefield ""))
1041 (not (numberp bibtex-autokey-names))
1042 (< counter bibtex-autokey-names)))
1043 (if (string-match " and " namefield)
1051 namefield 0 (match-beginning 0)))))
1053 (substring namefield (match-end 0))))
1055 (append names (list (downcase namefield)))
1057 (setq counter (1+ counter)))
1059 (namepart (mapconcat (function (lambda (name) name))
1061 bibtex-autokey-name-separator))
1065 (if (re-search-forward
1066 "^[ \t]*year[ \t]*=[ \t]*\\([0-9]*\\)" max t)
1067 (buffer-substring-no-properties
1068 (match-beginning 1) (match-end 1))
1071 (if (equal yearfield "")
1073 (substring yearfield
1074 (- (length yearfield)
1075 bibtex-autokey-year-length))))
1077 (let ((case-fold-search t)
1081 (if (re-search-forward
1082 "^[ \t]*title[ \t]*=" max t)
1083 (let* (bibtex-help-message
1085 (bibtex-find-text t)
1088 (bibtex-find-text nil)
1090 (bibtex-autokey-change
1091 (buffer-substring-no-properties start end)
1092 bibtex-autokey-titleword-change-strings))
1097 (length bibtex-autokey-title-terminators)))
1098 (while (< index numberofitems)
1100 (elt bibtex-autokey-title-terminators index)
1103 (substring titlefield 0 (match-beginning 0))))
1104 (setq index (1+ index)))
1112 titleword bibtex-autokey-titleword-abbrevs)))
1115 (bibtex-autokey-abbrev
1117 bibtex-autokey-titleword-length)))))
1118 ;; Gather all titlewords into a list
1125 (not (equal titlestring ""))
1127 (not (numberp bibtex-autokey-titlewords))
1129 bibtex-autokey-titlewords
1130 bibtex-autokey-titlewords-stretch))))
1131 (if (string-match "\\b[A-Z][A-Za-z0-9]*" titlestring)
1132 (let* ((end-match (match-end 0))
1134 (downcase (substring titlestring
1138 (not (numberp bibtex-autokey-titlewords))
1139 (< counter bibtex-autokey-titlewords))
1144 bibtex-autokey-titleword-first-ignore))
1147 (append titlewords (list titleword))))
1150 (append titlewords-extra (list titleword))))
1152 (substring titlestring end-match)))
1153 (setq titlestring ""))
1155 counter (1+ counter)))
1156 (if (string-match "\\b[A-Z][^ ]*\\b" titlestring)
1158 (append titlewords titlewords-extra)))))
1159 (titlepart (mapconcat (function (lambda (name) name))
1161 bibtex-autokey-titleword-separator))
1168 (equal yearpart "")))
1169 bibtex-autokey-name-year-separator)
1175 (equal yearpart ""))
1176 (equal titlepart "")))
1177 bibtex-autokey-year-title-separator)
1182 (defun bibtex-parse-keys (add &optional abortable)
1183 ;; Sets bibtex-keys to the keys used in the whole (possibly
1184 ;; restricted) buffer (either as entry keys or as crossref entries).
1185 ;; If ADD is non-nil adds the new keys to bibtex-keys instead of
1186 ;; simply resetting it. If ABORTABLE is non-nil abort on user input.
1187 (if bibtex-maintain-sorted-entries
1188 (let ((labels (if add
1191 (case-fold-search t))
1193 (goto-char (point-min))
1195 (message "Parsing reference keys..."))
1203 bibtex-reference-head
1205 "^[ \t\n]*crossref[ \t\n]*=[ \t\n]*[{\"]\\([A-Za-z][]A-Za-z0-9.:;?!`'()/*@_+-]*\\)[}\"],?$"
1212 (if (match-beginning (1+ bibtex-key-in-head))
1215 (buffer-substring-no-properties
1216 (match-beginning (1+ bibtex-key-in-head))
1217 (match-end (1+ bibtex-key-in-head))))
1220 (buffer-substring-no-properties
1221 (match-beginning (+ 3 bibtex-key-in-head))
1222 (match-end (+ 3 bibtex-key-in-head)))))
1223 (if (not (assoc label labels))
1225 (cons (list label) labels))))))
1228 bibtex-buffer-last-parsed-for-keys-tick
1229 (buffer-modified-tick))
1231 (message "Parsing reference keys... done"))
1232 (setq bibtex-keys labels)))))))
1234 (defun bibtex-auto-fill-function ()
1235 (let ((fill-prefix (make-string (+ bibtex-text-alignment 1) ? )))
1240 ;; Interactive Functions:
1243 (defun bibtex-mode ()
1244 "Major mode for editing BibTeX files.
1248 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
1250 The optional fields start with the string OPT, and thus ignored by BibTeX.
1251 The OPT string may be removed from a field with \\[bibtex-remove-OPT].
1252 \\[bibtex-kill-optional-field] kills the current optional field entirely.
1253 \\[bibtex-remove-double-quotes-or-braces] removes the double-quotes or
1254 braces around the text of the current field. \\[bibtex-empty-field]
1255 replaces the text of the current field with the default \"\" or {}.
1257 The command \\[bibtex-clean-entry] cleans the current entry, i.e. (i) removes
1258 double-quotes or braces from entirely numerical fields, (ii) removes
1259 OPT from all non-empty optional fields, (iii) removes all empty
1260 optional fields, and (iv) checks that no non-optional fields are empty.
1262 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
1263 Use \\[bibtex-next-field] to move to end of the next field.
1265 The following may be of interest as well:
1269 bibtex-print-help-message
1270 bibtex-beginning-of-entry
1272 bibtex-ispell-abstract
1273 bibtex-narrow-to-entry
1274 bibtex-hide-entry-bodies
1276 bibtex-validate-buffer
1279 bibtex-complete-string
1282 bibtex-field-left-delimiter
1283 bibtex-field-right-delimiter
1284 bibtex-include-OPTcrossref
1285 bibtex-include-OPTkey
1286 bibtex-include-OPTannote
1287 bibtex-mode-user-optional-fields
1288 bibtex-clean-entry-zap-empty-opts
1289 bibtex-sort-ignore-string-entries
1290 bibtex-maintain-sorted-entries
1291 bibtex-entry-field-alist
1292 bibtex-predefined-strings
1295 ---------------------------------------------------------
1296 Entry to this mode calls the value of bibtex-mode-hook if that value is
1299 (kill-all-local-variables)
1300 (use-local-map bibtex-mode-map)
1301 (setq major-mode 'bibtex-mode)
1302 (setq mode-name "BibTeX")
1303 (set-syntax-table bibtex-mode-syntax-table)
1304 (setq bibtex-completion-candidates bibtex-predefined-strings)
1308 ;; collect pathnames
1309 (let* ((bib (getenv "BIBINPUTS"))
1316 (lambda (dirname) ;; strips off trailing slashes
1317 (let ((len (length dirname)))
1318 (if (equal (elt dirname (1- len)) "/")
1319 (substring dirname 0 (1- (1- len)))
1322 (while (string-match ":" path)
1327 (1- (match-end 0)))))
1328 path (substring path (match-end 0))))
1329 (append actdirs (list path)))))
1330 (filename (if (string-match "\.bib$" filename)
1332 (concat filename ".bib")))
1335 (size (length dirs)))
1339 (not (file-readable-p
1341 (concat (elt dirs item) "/" filename)))))
1342 (setq item (1+ item)))
1345 (let ((curbuf (current-buffer))
1346 (bufname (make-temp-name ""))
1347 (compl bibtex-completion-candidates))
1348 (create-file-buffer bufname)
1349 (set-buffer bufname)
1350 (insert-file-contents fullfilename)
1351 (goto-char (point-min))
1352 (while (re-search-forward bibtex-string nil t)
1358 (list (buffer-substring-no-properties
1359 (match-beginning bibtex-key-in-string)
1360 (match-end bibtex-key-in-string)))))))
1361 (kill-buffer bufname)
1363 (setq bibtex-completion-candidates compl))
1364 (error "File %s not in $BIBINPUTS paths" filename)))))
1365 bibtex-string-files)
1371 bibtex-maintain-sorted-entries
1372 (eq major-mode 'bibtex-mode)
1374 (eq (buffer-modified-tick)
1375 bibtex-buffer-last-parsed-for-keys-tick)))
1376 (bibtex-parse-keys nil t)))))
1377 (bibtex-parse-keys nil)
1378 (make-local-variable 'paragraph-start)
1379 (setq paragraph-start "[ \f\n\t]*$")
1380 (make-local-variable 'comment-start)
1381 (setq comment-start "%")
1383 (setq auto-fill-function 'bibtex-auto-fill-function)
1384 (set (make-local-variable 'font-lock-defaults)
1385 '(bibtex-font-lock-keywords nil t ((?$ . "\""))))
1386 (run-hooks 'bibtex-mode-hook))
1388 (defun bibtex-entry (entry-type &optional required optional)
1389 "Inserts a new BibTeX entry.
1390 Calls the value of bibtex-add-entry-hook if that value is non-nil."
1391 (interactive (let* ((completion-ignore-case t)
1392 (e-t (completing-read
1394 bibtex-entry-field-alist
1397 (if (and (null required) (null optional))
1398 (let* ((e (assoc-ignore-case entry-type bibtex-entry-field-alist))
1402 (error "Bibtex entry type %s not defined!" entry-type))
1404 (member entry-type bibtex-include-OPTcrossref)
1406 (setq required (elt c-ref 0)
1407 optional (elt c-ref 1))
1408 (setq required (elt r-n-o 0)
1409 optional (elt r-n-o 1)))))
1411 (if bibtex-maintain-sorted-entries
1413 (format "%s key: " entry-type)
1415 (if bibtex-maintain-sorted-entries
1416 (bibtex-find-entry-location key)
1417 (bibtex-move-outside-of-entry))
1418 (insert "@" entry-type "{")
1422 (mapcar 'bibtex-make-field required)
1423 (if (member entry-type bibtex-include-OPTcrossref)
1424 (bibtex-make-optional-field '("crossref")))
1425 (if bibtex-include-OPTkey
1426 (bibtex-make-optional-field '("key")))
1427 (mapcar 'bibtex-make-optional-field optional)
1428 (mapcar 'bibtex-make-optional-field
1429 bibtex-mode-user-optional-fields)
1430 (if bibtex-include-OPTannote
1431 (bibtex-make-optional-field '("annote")))
1433 (bibtex-next-field t)
1434 (run-hooks 'bibtex-add-entry-hook)))
1436 (defun bibtex-print-help-message ()
1437 "Prints helpful information about current field in current BibTeX entry."
1439 (let* ((pnt (point))
1443 (condition-case errname
1444 (bibtex-enclosing-regexp bibtex-field)
1447 (error "Not on BibTeX field")))
1448 (let ((mb (match-beginning bibtex-name-in-field))
1449 (me (match-end bibtex-name-in-field)))
1451 (buffer-substring-no-properties
1452 (if (looking-at "OPT")
1459 bibtex-reference-maybe-empty-head nil t)
1460 (buffer-substring-no-properties
1461 (1+ (match-beginning bibtex-type-in-head))
1462 (match-end bibtex-type-in-head))))
1464 (assoc-ignore-case reference-type
1465 bibtex-entry-field-alist))
1466 (c-r-list (elt entry-list 2))
1469 (member reference-type bibtex-include-OPTcrossref)
1472 (elt entry-list 1)))
1473 (list-of-entries (append
1474 (elt req-opt-list 0)
1475 (elt req-opt-list 1)
1476 bibtex-mode-user-optional-fields
1479 bibtex-include-OPTcrossref)
1481 "Label of the crossreferenced entry")))
1482 (if bibtex-include-OPTannote
1484 "Personal annotation (ignored)")))
1485 (if bibtex-include-OPTkey
1487 "Key used for label creation if author and editor fields are missing"))))))
1489 (if (assoc field-name list-of-entries)
1490 (message (elt (assoc field-name list-of-entries) 1))
1491 (message "NO COMMENT AVAILABLE"))))
1493 (defun bibtex-make-field (e-t)
1494 "Makes a field named E-T in current BibTeX entry."
1495 (interactive "sBibTeX field name: ")
1496 (let ((name (if (consp e-t)
1501 (bibtex-find-text nil)
1502 (if (looking-at "[}\"]")
1505 (indent-to-column bibtex-name-alignment)
1507 (indent-to-column bibtex-text-alignment)
1508 (insert bibtex-field-left-delimiter bibtex-field-right-delimiter)
1510 (forward-char -1))))
1512 (defun bibtex-make-optional-field (e-t)
1513 "Makes an optional field named E-T in current BibTeX entry."
1515 (setq e-t (cons (concat "OPT" (car e-t)) (cdr e-t)))
1516 (setq e-t (concat "OPT" e-t)))
1517 (bibtex-make-field e-t))
1519 (defun bibtex-beginning-of-entry ()
1520 "Move to beginning of BibTeX entry.
1521 If inside an entry, move to the beginning of it, otherwise move to the
1522 beginning of the previous entry."
1524 (if (looking-at "^@")
1526 (re-search-backward "^@" nil 'move))
1528 (defun bibtex-end-of-entry ()
1529 "Move to end of BibTeX entry.
1530 If inside an entry, move to the end of it, otherwise move to the end
1531 of the previous entry."
1533 (bibtex-beginning-of-entry)
1534 (let ((parse-sexp-ignore-comments t))
1535 (forward-sexp 2) ;; skip entry type and body
1538 (defun bibtex-ispell-entry ()
1539 "Spell whole BibTeX entry."
1541 (ispell-region (progn (bibtex-beginning-of-entry) (point))
1542 (progn (bibtex-end-of-entry) (point))))
1544 (defun bibtex-ispell-abstract ()
1545 "Spell abstract of BibTeX entry."
1547 (let ((pnt (bibtex-end-of-entry)))
1548 (bibtex-beginning-of-entry)
1550 (re-search-forward "^[ \t]*[OPT]*abstract[ \t]*=" pnt))
1551 (error "No abstract in entry.")))
1552 (ispell-region (point)
1553 (save-excursion (forward-sexp) (point))))
1555 (defun bibtex-narrow-to-entry ()
1556 "Narrow buffer to current BibTeX entry."
1559 (narrow-to-region (progn (bibtex-beginning-of-entry) (point))
1560 (progn (bibtex-end-of-entry) (point)))))
1563 (defun bibtex-hide-entry-bodies (&optional arg)
1564 "Hide all lines between first and last BibTeX entries not beginning with @.
1565 With argument, show all text."
1568 (beginning-of-first-bibtex-entry)
1569 ;; subst-char-in-region modifies the buffer, despite what the
1570 ;; documentation says...
1571 (let ((modifiedp (buffer-modified-p))
1572 (buffer-read-only nil))
1574 (subst-char-in-region (point) (point-max) ?\r ?\n t)
1575 (while (save-excursion (re-search-forward "\n[^@]" (point-max) t))
1576 ;; (save-excursion (replace-regexp "\n\\([^@]\\)" "\r\\1"))
1578 (while (re-search-forward "\n\\([^@]\\)" nil t)
1579 (replace-match "\r\\1" nil nil)))))
1580 (setq selective-display (not arg))
1581 (set-buffer-modified-p modifiedp))))
1583 (defun bibtex-sort-entries ()
1584 "Sort BibTeX entries alphabetically by key.
1585 Text outside of BibTeX entries is not affected. If
1586 bibtex-sort-ignore-string-entries is non-nil, @string entries will be
1590 (beginning-of-first-bibtex-entry)
1594 (goto-char (point-max))
1595 (bibtex-end-of-entry)
1597 (if bibtex-sort-ignore-string-entries
1598 (if (re-search-forward bibtex-reference nil 'move)
1599 (goto-char (match-beginning 0))))
1605 (if bibtex-sort-ignore-string-entries
1606 (if (re-search-forward bibtex-reference nil 'move)
1607 (goto-char (match-beginning 0)))
1608 (if (re-search-forward bibtex-reference-head nil 'move)
1609 (goto-char (match-beginning 0))))))
1611 'bibtex-end-of-entry
1612 ;; STARTKEY function
1615 (if bibtex-sort-ignore-string-entries
1617 (re-search-forward bibtex-reference)
1618 (buffer-substring-no-properties
1619 (match-beginning bibtex-key-in-reference)
1620 (match-end bibtex-key-in-reference)))
1621 (re-search-forward bibtex-reference-head)
1622 (buffer-substring-no-properties
1623 (match-beginning bibtex-key-in-head)
1624 (match-end bibtex-key-in-head)))))
1628 (defun bibtex-find-entry-location (entry-name &optional ignore-dups)
1629 "Looking for place to put the BibTeX entry named ENTRY-NAME.
1630 Performs a binary search (therefore, buffer is assumed to be in sorted
1631 order, without duplicates (see \\[bibtex-validate-buffer]), if it is
1632 not, bibtex-find-entry-location will fail). If entry-name is already
1633 used as a reference key, an error is signalled. However, if optional
1634 variable IGNORE-DUPS is non-nil, no error messages about duplicate
1635 entries are signalled, but the error handling is assumed to be made in
1636 the calling function. Nil is returned, if an duplicate entry error
1637 occured, and t in all other cases."
1640 (beginning-of-first-bibtex-entry)
1641 (if bibtex-sort-ignore-string-entries
1642 (re-search-forward bibtex-reference nil `move)
1643 (bibtex-end-of-entry))
1647 (goto-char (point-max))
1648 (if bibtex-sort-ignore-string-entries
1649 (re-search-backward bibtex-reference nil `move)
1650 (bibtex-beginning-of-entry))
1654 (done (>= left right))
1658 (setq actual-point (/ (+ left right) 2))
1659 (goto-char actual-point)
1660 (bibtex-beginning-of-entry)
1662 (if bibtex-sort-ignore-string-entries
1664 (re-search-forward bibtex-reference)
1665 (buffer-substring-no-properties
1666 (match-beginning bibtex-key-in-reference)
1667 (match-end bibtex-key-in-reference)))
1668 (re-search-forward bibtex-reference-head)
1669 (buffer-substring-no-properties
1670 (match-beginning bibtex-key-in-head)
1671 (match-end bibtex-key-in-head))))
1673 ((string-lessp entry-name actual-key)
1674 (setq new (match-beginning 0))
1675 (if (equal right new)
1678 ((string-lessp actual-key entry-name)
1679 (setq new (match-end 0))
1680 (if (equal left new)
1683 ((string-equal actual-key entry-name)
1686 (if (not ignore-dups)
1687 (error "Entry with key `%s' already exists!" entry-name)))))
1691 (if (re-search-forward bibtex-reference nil t)
1694 (buffer-substring-no-properties
1695 (match-beginning bibtex-key-in-reference)
1696 (match-end bibtex-key-in-reference)))
1697 (if (string-lessp actual-key entry-name)
1698 ;; even greater than last entry --> we must append
1700 (goto-char (match-end 0))
1701 (newline (forward-line 2))
1702 (beginning-of-line))
1703 (goto-char right))))
1706 (defun bibtex-validate-buffer ()
1707 "Validate if the current BibTeX buffer is syntactically correct.
1708 Any garbage (e.g. comments) before the first \"@\" is not tested (so
1709 you can put comments here)."
1713 ;; looking if entries fit syntactical structure
1714 (goto-char (point-min))
1715 (while (< (re-search-forward "@\\|\\'") max)
1719 (looking-at "@string")
1720 (looking-at "@preamble"))
1723 (re-search-forward bibtex-reference nil t)
1724 (equal p (match-beginning 0))))
1727 (error "Bad entry begins here"))))))
1728 ;; looking if entries are balanced (a single non-escaped quote
1729 ;; inside braces is not detected by the former check, but
1730 ;; bibtex-sort-entries stumbles about it
1731 (goto-char (point-min))
1735 (bibtex-beginning-of-entry)
1737 ;; looking for correct sort order and duplicates
1738 (if bibtex-maintain-sorted-entries
1739 (let ((entry-name (make-string 10 255))
1742 (beginning-of-first-bibtex-entry)
1746 (cond ((or (null previous)
1747 (string< previous current))
1748 (setq previous current
1750 ((string-equal previous current)
1751 (error "Duplicate here with previous!"))
1753 (error "Entries out of order here!"))))))))
1755 (message "BibTeX buffer is syntactically correct")))
1757 (defun bibtex-next-field (arg)
1758 "Finds end of text of next BibTeX field; with arg, to its beginning."
1760 (bibtex-inside-field)
1761 (let ((start (point)))
1764 (bibtex-enclosing-field)
1765 (goto-char (match-end 0))
1771 (bibtex-find-text arg))
1773 (defun bibtex-find-text (arg)
1774 "Go to end of text of current field; with arg, go to beginning."
1776 (bibtex-inside-field)
1777 (bibtex-enclosing-field)
1780 (goto-char (match-beginning bibtex-text-in-field))
1781 (if (looking-at "[{\"]")
1783 (goto-char (match-end bibtex-text-in-field))
1785 (= (preceding-char) ?})
1786 (= (preceding-char) ?\"))
1788 (if bibtex-help-message
1789 (bibtex-print-help-message)))
1791 (defun bibtex-remove-OPT ()
1792 "Removes the 'OPT' starting optional arguments and goes to end of text."
1794 (bibtex-inside-field)
1795 (bibtex-enclosing-field)
1797 (goto-char (match-beginning bibtex-name-in-field))
1798 (if (looking-at "OPT")
1799 ;; sct@dcs.edinburgh.ac.uk
1801 (delete-char (length "OPT"))
1802 (search-forward "=")
1803 (delete-horizontal-space)
1804 (indent-to-column bibtex-text-alignment))))
1805 (bibtex-inside-field))
1807 (defun bibtex-remove-double-quotes-or-braces ()
1808 "Removes \"\" or {} around string."
1811 (bibtex-inside-field)
1812 (bibtex-enclosing-field)
1813 (let ((start (match-beginning bibtex-text-in-field))
1814 (stop (match-end bibtex-text-in-field)))
1817 (if (looking-at "[}\"]")
1820 (if (looking-at "[{\"]")
1823 (defun bibtex-kill-optional-field ()
1824 "Kill the entire enclosing optional BibTeX field."
1826 (bibtex-inside-field)
1827 (bibtex-enclosing-field)
1828 (goto-char (match-beginning bibtex-name-in-field))
1829 (let ((the-end (match-end 0))
1830 (the-beginning (match-beginning 0)))
1831 (if (looking-at "OPT")
1834 (skip-chars-forward " \t\n,")
1835 (kill-region the-beginning the-end))
1836 (error "Mandatory fields can't be killed"))))
1838 (defun bibtex-empty-field ()
1839 "Delete the text part of the current field, replace with empty text."
1841 (bibtex-inside-field)
1842 (bibtex-enclosing-field)
1843 (goto-char (match-beginning bibtex-text-in-field))
1844 (kill-region (point) (match-end bibtex-text-in-field))
1845 (insert (concat bibtex-field-left-delimiter
1846 bibtex-field-right-delimiter))
1847 (bibtex-find-text t))
1849 (defun bibtex-pop-previous (arg)
1850 "Replace text of current field with the text of similar field in previous entry.
1851 With arg, go up ARG entries. Repeated, goes up so many times. May be
1852 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
1854 (bibtex-inside-field)
1856 ; parse current field
1857 (bibtex-enclosing-field)
1858 (let ((start-old-text (match-beginning bibtex-text-in-field))
1859 (stop-old-text (match-end bibtex-text-in-field))
1860 (start-name (match-beginning bibtex-name-in-field))
1861 (stop-name (match-end bibtex-name-in-field))
1863 (goto-char start-name)
1864 ; construct regexp for previous field with same name as this one
1865 (let ((matching-entry
1867 (buffer-substring-no-properties (if (looking-at "OPT")
1868 (+ (point) (length "OPT"))
1871 bibtex-field-text)))
1872 ; if executed several times in a row, start each search where the
1874 (cond ((or (eq last-command 'bibtex-pop-previous)
1875 (eq last-command 'bibtex-pop-next))
1879 (bibtex-enclosing-reference-maybe-empty-head)
1880 (setq bibtex-pop-previous-search-point (point))
1881 (setq bibtex-pop-next-search-point (match-end 0))))
1882 (goto-char bibtex-pop-previous-search-point)
1883 ; Now search for arg'th previous similar field
1885 ((re-search-backward matching-entry (point-min) t arg)
1887 (buffer-substring-no-properties
1888 (match-beginning bibtex-text-in-cfield)
1889 (match-end bibtex-text-in-cfield)))
1890 ;; change delimiters, if any changes needed
1893 (equal bibtex-field-left-delimiter "{")
1894 (eq (aref new-text 0) ?\")
1895 (eq (aref new-text (1- (length new-text))) ?\"))
1896 (aset new-text 0 ?\{)
1897 (aset new-text (1- (length new-text)) ?\}))
1899 (equal bibtex-field-left-delimiter "\"")
1900 (eq (aref new-text 0) ?\{)
1901 (eq (aref new-text (1- (length new-text))) ?\}))
1902 (aset new-text 0 ?\")
1903 (aset new-text (1- (length new-text)) ?\"))
1905 (not (eq (aref new-text 0)
1906 (aref bibtex-field-left-delimiter 0)))
1907 (not (eq (aref new-text (1- (length new-text)))
1908 (aref bibtex-field-right-delimiter 0))))
1909 (setq new-text (concat bibtex-field-left-delimiter
1911 bibtex-field-right-delimiter))))
1912 ; Found a matching field. Remember boundaries.
1913 (setq bibtex-pop-next-search-point (match-end 0))
1914 (setq bibtex-pop-previous-search-point (match-beginning 0))
1916 ; Go back to where we started, delete old text, and pop new.
1917 (goto-char stop-old-text)
1918 (delete-region start-old-text stop-old-text)
1921 (error "No previous matching BibTeX field."))))))
1922 (setq this-command 'bibtex-pop-previous))
1924 (defun bibtex-pop-next (arg)
1925 "Replace text of current field with the text of similar field in next entry.
1926 With arg, go up ARG entries. Repeated, goes up so many times. May be
1927 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
1929 (bibtex-inside-field)
1931 ; parse current field
1932 (bibtex-enclosing-field)
1933 (let ((start-old-text (match-beginning bibtex-text-in-field))
1934 (stop-old-text (match-end bibtex-text-in-field))
1935 (start-name (match-beginning bibtex-name-in-field))
1936 (stop-name (match-end bibtex-name-in-field))
1938 (goto-char start-name)
1939 ; construct regexp for next field with same name as this one,
1940 ; ignoring possible OPT's
1941 (let ((matching-entry
1943 (buffer-substring-no-properties (if (looking-at "OPT")
1944 (+ (point) (length "OPT"))
1947 bibtex-field-text)))
1949 ; if executed several times in a row, start each search where the
1951 (cond ((or (eq last-command 'bibtex-pop-next)
1952 (eq last-command 'bibtex-pop-previous))
1956 (bibtex-enclosing-reference-maybe-empty-head)
1957 (setq bibtex-pop-previous-search-point (point))
1958 (setq bibtex-pop-next-search-point (match-end 0))))
1959 (goto-char bibtex-pop-next-search-point)
1961 ; Now search for arg'th next similar field
1963 ((re-search-forward matching-entry (point-max) t arg)
1965 (buffer-substring-no-properties
1966 (match-beginning bibtex-text-in-cfield)
1967 (match-end bibtex-text-in-cfield)))
1968 ;; change delimiters, if any changes needed
1971 (equal bibtex-field-left-delimiter "{")
1972 (eq (aref new-text 0) ?\")
1973 (eq (aref new-text (1- (length new-text))) ?\"))
1974 (aset new-text 0 ?\{)
1975 (aset new-text (1- (length new-text)) ?\}))
1977 (equal bibtex-field-left-delimiter "\"")
1978 (eq (aref new-text 0) ?\{)
1979 (eq (aref new-text (1- (length new-text))) ?\}))
1980 (aset new-text 0 ?\")
1981 (aset new-text (1- (length new-text)) ?\"))
1983 (not (eq (aref new-text 0)
1984 (aref bibtex-field-left-delimiter 0)))
1985 (not (eq (aref new-text (1- (length new-text)))
1986 (aref bibtex-field-right-delimiter 0))))
1987 (setq new-text (concat bibtex-field-left-delimiter
1989 bibtex-field-right-delimiter))))
1990 ; Found a matching field. Remember boundaries.
1991 (setq bibtex-pop-next-search-point (match-end 0))
1992 (setq bibtex-pop-previous-search-point (match-beginning 0))
1994 ; Go back to where we started, delete old text, and pop new.
1995 (goto-char stop-old-text)
1996 (delete-region start-old-text stop-old-text)
1999 (error "No next matching BibTeX field."))))))
2000 (setq this-command 'bibtex-pop-next))
2002 (defun bibtex-clean-entry (&optional arg)
2003 "Finish editing the current BibTeX entry and clean it up.
2004 For all optional fields of current BibTeX entry: if empty, kill the
2005 whole field; otherwise, remove the \"OPT\" string in the name; if text
2006 numerical, remove double-quotes. For all mandatory fields: if empty,
2007 signal error. If label of entry is empty or a prefix argument was
2008 given, calculate a new entry label."
2010 (bibtex-beginning-of-entry)
2011 (let ((start (point))
2014 (narrow-to-region start (save-excursion (bibtex-end-of-entry) (point)))
2016 (re-search-forward bibtex-field (point-max) t 1)
2017 (not crossref-there))
2018 ;; determine if reference has crossref entry
2019 (let ((begin-name (match-beginning bibtex-name-in-field))
2020 (begin-text (match-beginning bibtex-text-in-field)))
2021 (goto-char begin-name)
2022 (if (looking-at "\\(OPTcrossref\\)\\|\\(crossref\\)")
2024 (goto-char begin-text)
2025 (if (not (looking-at
2027 bibtex-field-left-delimiter
2028 bibtex-field-right-delimiter)))
2029 (setq crossref-there t))))))
2030 (bibtex-enclosing-reference-maybe-empty-head)
2031 (re-search-forward bibtex-reference-type)
2032 (let ((begin-type (1+ (match-beginning 0)))
2033 (end-type (match-end 0)))
2035 (while (re-search-forward bibtex-field (point-max) t 1)
2036 (let ((begin-field (match-beginning 0))
2037 (end-field (match-end 0))
2038 (begin-name (match-beginning bibtex-name-in-field))
2039 (end-name (match-end bibtex-name-in-field))
2040 (begin-text (match-beginning bibtex-text-in-field))
2041 (end-text (match-end bibtex-text-in-field))
2043 (goto-char begin-name)
2046 bibtex-clean-entry-zap-empty-opts)
2047 (goto-char begin-text)
2050 bibtex-field-left-delimiter
2051 bibtex-field-right-delimiter))
2052 ;; empty: delete whole field if really optional
2053 ;; (missing crossref handled) or complain
2055 (not crossref-there)
2058 (buffer-substring-no-properties
2059 (+ (length "OPT") begin-name) end-name))
2062 (buffer-substring-no-properties
2063 begin-type end-type)
2064 bibtex-entry-field-alist))))))
2065 ;; field is not really optional
2067 (goto-char begin-name)
2068 (delete-char (length "OPT"))
2069 ;; make field non-OPT
2070 (search-forward "=")
2071 (delete-horizontal-space)
2072 (indent-to-column bibtex-text-alignment)
2074 ;; and loop to go through next test
2075 (error "Mandatory field ``%s'' is empty"
2076 (buffer-substring-no-properties
2079 ;; field is optional
2080 (delete-region begin-field end-field))
2081 ;; otherwise: not empty, delete "OPT"
2082 (goto-char begin-name)
2083 (delete-char (length "OPT"))
2085 ;; fixup alignment. [alarson:19920309.2047CST]
2086 (search-forward "=")
2087 (delete-horizontal-space)
2088 (indent-to-column bibtex-text-alignment))
2089 (goto-char begin-field) ; and loop to go through next test
2092 (goto-char begin-text)
2093 (cond ((looking-at (concat
2094 bibtex-field-left-delimiter
2096 bibtex-field-right-delimiter))
2098 (goto-char end-text)
2099 (delete-char -1) ; delete enclosing double-quotes
2100 (goto-char begin-text)
2102 (goto-char end-field) ; go to end for next search
2103 (forward-char -2) ; to compensate for the 2 quotes deleted
2105 ((looking-at (concat
2106 bibtex-field-left-delimiter
2107 bibtex-field-right-delimiter))
2108 ;; if empty quotes, complain
2110 (if (not (or (equal (buffer-substring-no-properties
2114 (equal (buffer-substring-no-properties
2118 (error "Mandatory field ``%s'' is empty"
2119 (buffer-substring-no-properties
2120 begin-name end-name))))
2122 (goto-char end-field)))))))))
2124 (bibtex-end-of-entry))
2126 (bibtex-end-of-entry)
2129 (bibtex-beginning-of-entry)
2130 (if (re-search-forward
2131 bibtex-reference-head eob t)
2132 (buffer-substring-no-properties
2133 (match-beginning bibtex-key-in-head)
2134 (match-end bibtex-key-in-head))))))
2140 (if bibtex-autokey-edit-before-use
2141 (read-from-minibuffer "Key to use: "
2142 (bibtex-generate-autokey))
2143 (bibtex-generate-autokey))))
2144 (bibtex-beginning-of-entry)
2145 (re-search-forward bibtex-reference-maybe-empty-head)
2146 (if (match-beginning bibtex-key-in-head)
2147 (delete-region (match-beginning bibtex-key-in-head)
2148 (match-end bibtex-key-in-head)))
2151 (bibtex-beginning-of-entry)
2154 (bibtex-end-of-entry)
2155 (re-search-forward "^@" nil 'move)
2159 (kill-region start end)
2162 (not bibtex-maintain-sorted-entries)
2163 (bibtex-find-entry-location autokey t))))
2165 (setq kill-ring (cdr kill-ring))
2167 (bibtex-beginning-of-entry)
2168 (re-search-forward bibtex-reference-head)
2171 "New inserted reference may be a duplicate."))))))))
2173 (let ((start (progn (bibtex-beginning-of-entry) (point)))
2174 (end (progn (bibtex-end-of-entry) (point))))
2176 (narrow-to-region start end)
2177 (bibtex-parse-keys t)))))
2179 (defun bibtex-complete-string ()
2180 "Complete word fragment before point to longest prefix of a defined string.
2181 If point is not after the part of a word, all strings are listed."
2183 (let* ((end (point))
2184 (beg (save-excursion
2185 (re-search-backward "[ \t{\"]")
2188 (part-of-word (buffer-substring-no-properties beg end))
2189 (string-list (copy-sequence bibtex-completion-candidates))
2190 (case-fold-search t)
2191 (completion (save-excursion
2193 (while (re-search-backward
2194 bibtex-string (point-min) t)
2198 (buffer-substring-no-properties
2199 (match-beginning bibtex-key-in-string)
2200 (match-end bibtex-key-in-string)))
2208 (try-completion part-of-word string-list)))))
2209 (cond ((eq completion t)
2210 (bibtex-remove-double-quotes-or-braces))
2212 (error "Can't find completion for \"%s\"" part-of-word))
2213 ((not (string= part-of-word completion))
2214 (delete-region beg end)
2216 (if (assoc completion string-list)
2217 (bibtex-remove-double-quotes-or-braces)))
2219 (message "Making completion list...")
2220 (let ((list (all-completions part-of-word string-list)))
2221 (with-output-to-temp-buffer "*Completions*"
2222 (display-completion-list list)))
2223 (message "Making completion list...done")))))
2225 (defun bibtex-Article ()
2227 (bibtex-entry "Article"))
2229 (defun bibtex-Book ()
2231 (bibtex-entry "Book"))
2233 (defun bibtex-Booklet ()
2235 (bibtex-entry "Booklet"))
2237 (defun bibtex-InBook ()
2239 (bibtex-entry "InBook"))
2241 (defun bibtex-InCollection ()
2243 (bibtex-entry "InCollection"))
2245 (defun bibtex-InProceedings ()
2247 (bibtex-entry "InProceedings"))
2249 (defun bibtex-Manual ()
2251 (bibtex-entry "Manual"))
2253 (defun bibtex-MastersThesis ()
2255 (bibtex-entry "MastersThesis"))
2257 (defun bibtex-Misc ()
2259 (bibtex-entry "Misc"))
2261 (defun bibtex-PhdThesis ()
2263 (bibtex-entry "PhdThesis"))
2265 (defun bibtex-Proceedings ()
2267 (bibtex-entry "Proceedings"))
2269 (defun bibtex-TechReport ()
2271 (bibtex-entry "TechReport"))
2273 (defun bibtex-Unpublished ()
2275 (bibtex-entry "Unpublished"))
2277 (defun bibtex-string ()
2279 (bibtex-move-outside-of-entry)
2283 bibtex-field-left-delimiter
2284 bibtex-field-right-delimiter
2289 (defun bibtex-preamble ()
2291 (bibtex-move-outside-of-entry)
2292 (insert "@Preamble{}\n")
2298 ;; Make BibTeX a Feature
2303 ;;; bibtex.el ends here