Update FSF's address.
[bpt/emacs.git] / lisp / textmodes / bibtex.el
1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
2
3 ;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
4
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
12
13 ;; This file is part of GNU Emacs.
14
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)
18 ;; any later version.
19
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.
24
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.
29
30 ;;; Commentary:
31
32 ;; Major mode for editing and validating BibTeX files.
33
34 ;; Usage:
35 ;; See documentation for function bibtex-mode (or type "\M-x describe-mode"
36 ;; when you are in bibtex-mode).
37
38 ;; Todo:
39 ;; Distribute texinfo file.
40
41 ;; Known Bugs:
42 ;; 1. using regular expressions to match the entire BibTeX entry dies
43 ;; on long entries (e.g. those containing abstracts) since
44 ;; the length of regular expression matches is fairly limited.
45 ;; 2. Calling bibtex-find-text in a string entry results in the
46 ;; error message "Can't find enclosing Bibtex field" instead of
47 ;; moving to the empty string. [reported by gernot@cs.unsw.oz.au]
48 ;; 3. Quotes inside quote-parenthesized fields (like
49 ;; `author = "Stefan Sch{\"o}f"') break bibtex-validate-buffer.
50 ;; Further, you must use braces here, if you want to set
51 ;; bibtex-maintain-sorted-entries to a non-nil value.
52
53 ;; (current keeper: schoef@informatik.uni-oldenburg.de
54 ;; previous: alarson@src.honeywell.com)
55
56 ;;; Code:
57
58 ;; User Options:
59
60 (defvar bibtex-field-left-delimiter "{"
61 "*Set this to { or \" according to your personal preferences.
62 This variable is buffer local.")
63 (make-variable-buffer-local 'bibtex-field-left-delimiter)
64
65 (defvar bibtex-field-right-delimiter "}"
66 "*Set this to } or \" according to your personal preferences.
67 This variable is buffer local.")
68 (make-variable-buffer-local 'bibtex-field-right-delimiter)
69
70 (defvar bibtex-include-OPTcrossref '("InProceedings" "InCollection")
71 "*All entries listed here will have an OPTcrossref field.")
72
73 (defvar bibtex-include-OPTkey t
74 "*If non-nil, all entries will have an OPTkey field.")
75
76 (defvar bibtex-include-OPTannote t
77 "*If non-nil, all entries will have an OPTannote field.")
78
79 (defvar bibtex-mode-user-optional-fields nil
80 "*List of optional fields the user wants to have always present.
81 Entries should be lists of strings with two elements (first element =
82 name of the field, second element = comment to appear in the echo area).")
83
84 (defvar bibtex-clean-entry-zap-empty-opts t
85 "*If non-nil, bibtex-clean-entry will delete all empty optional fields.")
86
87 (defvar bibtex-sort-ignore-string-entries t
88 "*If non-nil, BibTeX @STRING entries are not sort-significant.
89 That means they are ignored when determining ordering of the buffer
90 (e.g. sorting, locating alphabetical position for new entries, etc.).
91 This variable is buffer local.")
92 (make-variable-buffer-local 'bibtex-sort-ignore-string-entries)
93
94 (defvar bibtex-maintain-sorted-entries nil
95 "*If non-nil, bibtex-mode maintains all BibTeX entries in sorted order.
96 Setting this variable to nil will strip off some comfort (e.g. TAB
97 completion for reference keys in minibuffer, automatic detection of
98 duplicates) from bibtex-mode. See also bibtex-sort-ignore-string-entries.
99 This variable is buffer local.")
100 (make-variable-buffer-local 'bibtex-maintain-sorted-entries)
101
102 (defvar bibtex-entry-field-alist
103 '(
104 ("Article" . (((("author" "Author1 [and Author2 ...] [and others]")
105 ("title" "Title of the article (BibTeX converts it to lowercase)")
106 ("journal" "Name of the journal (use string, remove braces)")
107 ("year" "Year of publication"))
108 (("volume" "Volume of the journal")
109 ("number" "Number of the journal")
110 ("month" "Month of the publication as a string (remove braces)")
111 ("pages" "Pages in the journal")
112 ("note" "Remarks to be put at the end of the \\bibitem")))
113 ((("author" "Author1 [and Author2 ...] [and others]")
114 ("title" "Title of the article (BibTeX converts it to lowercase)"))
115 (("journal" "Name of the journal (use string, remove braces)")
116 ("year" "Year of publication")
117 ("volume" "Volume of the journal")
118 ("number" "Number of the journal")
119 ("month" "Month of the publication as a string (remove braces)")
120 ("pages" "Pages in the journal")
121 ("note" "Remarks to be put at the end of the \\bibitem")))))
122 ("Book" . (((("author" "Author1 [and Author2 ...] [and others]")
123 ("title" "Title of the book")
124 ("publisher" "Publishing company")
125 ("year" "Year of publication"))
126 (("editor" "Editor1 [and Editor2 ...] [and others]")
127 ("volume" "Volume of the book in the series")
128 ("number" "Number of the book in a small series (overwritten by volume)")
129 ("series" "Series in which the book appeared")
130 ("address" "Address of the publisher")
131 ("edition" "Edition of the book as a capitalized English word")
132 ("month" "Month of the publication as a string (remove braces)")
133 ("note" "Remarks to be put at the end of the \\bibitem")))))
134 ("Booklet" . (((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
135 (("author" "Author1 [and Author2 ...] [and others]")
136 ("howpublished" "The way in which the booklet was published")
137 ("address" "Address of the publisher")
138 ("year" "Year of publication")
139 ("month" "Month of the publication as a string (remove braces)")
140 ("note" "Remarks to be put at the end of the \\bibitem")))))
141 ("InBook" . (((("author" "Author1 [and Author2 ...] [and others]")
142 ("title" "Title of the book")
143 ("chapter" "Chapter in the book")
144 ("publisher" "Publishing company")
145 ("year" "Year of publication"))
146 (("editor" "Editor1 [and Editor2 ...] [and others]")
147 ("volume" "Volume of the book in the series")
148 ("number" "Number of the book in a small series (overwritten by volume)")
149 ("series" "Series in which the book appeared")
150 ("address" "Address of the publisher")
151 ("edition" "Edition of the book as a capitalized English word")
152 ("month" "Month of the publication as a string (remove braces)")
153 ("pages" "Pages in the book")
154 ("type" "Word to use instead of \"chapter\"")
155 ("note" "Remarks to be put at the end of the \\bibitem")))
156 ((("author" "Author1 [and Author2 ...] [and others]")
157 ("title" "Title of the book")
158 ("chapter" "Chapter in the book"))
159 (("publisher" "Publishing company")
160 ("year" "Year of publication")
161 ("editor" "Editor1 [and Editor2 ...] [and others]")
162 ("volume" "Volume of the book in the series")
163 ("number" "Number of the book in a small series (overwritten by volume)")
164 ("series" "Series in which the book appeared")
165 ("address" "Address of the publisher")
166 ("edition" "Edition of the book as a capitalized English word")
167 ("month" "Month of the publication as a string (remove braces)")
168 ("pages" "Pages in the book")
169 ("type" "Word to use instead of \"chapter\"")
170 ("note" "Remarks to be put at the end of the \\bibitem")))))
171 ("InCollection" . (((("author" "Author1 [and Author2 ...] [and others]")
172 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
173 ("booktitle" "Name of the book")
174 ("publisher" "Publishing company")
175 ("year" "Year of publication"))
176 (("editor" "Editor1 [and Editor2 ...] [and others]")
177 ("volume" "Volume of the book in the series")
178 ("number" "Number of the book in a small series (overwritten by volume)")
179 ("series" "Series in which the book appeared")
180 ("chapter" "Chapter in the book")
181 ("type" "Word to use instead of \"chapter\"")
182 ("address" "Address of the publisher")
183 ("edition" "Edition of the book as a capitalized English word")
184 ("month" "Month of the publication as a string (remove braces)")
185 ("pages" "Pages in the book")
186 ("note" "Remarks to be put at the end of the \\bibitem")))
187 ((("author" "Author1 [and Author2 ...] [and others]")
188 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
189 ("booktitle" "Name of the book"))
190 (("publisher" "Publishing company")
191 ("year" "Year of publication")
192 ("editor" "Editor1 [and Editor2 ...] [and others]")
193 ("volume" "Volume of the book in the series")
194 ("number" "Number of the book in a small series (overwritten by volume)")
195 ("series" "Series in which the book appeared")
196 ("chapter" "Chapter in the book")
197 ("type" "Word to use instead of \"chapter\"")
198 ("address" "Address of the publisher")
199 ("edition" "Edition of the book as a capitalized English word")
200 ("month" "Month of the publication as a string (remove braces)")
201 ("pages" "Pages in the book")
202 ("note" "Remarks to be put at the end of the \\bibitem")))))
203 ("InProceedings" . (((("author" "Author1 [and Author2 ...] [and others]")
204 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
205 ("booktitle" "Name of the conference proceedings")
206 ("year" "Year of publication"))
207 (("editor" "Editor1 [and Editor2 ...] [and others]")
208 ("volume" "Volume of the conference proceedings in the series")
209 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
210 ("series" "Series in which the conference proceedings appeared")
211 ("organization" "Sponsoring organization of the conference")
212 ("publisher" "Publishing company, its location")
213 ("address" "Location of the Proceedings")
214 ("month" "Month of the publication as a string (remove braces)")
215 ("pages" "Pages in the conference proceedings")
216 ("note" "Remarks to be put at the end of the \\bibitem")))
217 ((("author" "Author1 [and Author2 ...] [and others]")
218 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
219 ("booktitle" "Name of the conference proceedings"))
220 (("editor" "Editor1 [and Editor2 ...] [and others]")
221 ("volume" "Volume of the conference proceedings in the series")
222 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
223 ("series" "Series in which the conference proceedings appeared")
224 ("year" "Year of publication")
225 ("organization" "Sponsoring organization of the conference")
226 ("publisher" "Publishing company, its location")
227 ("address" "Location of the Proceedings")
228 ("month" "Month of the publication as a string (remove braces)")
229 ("pages" "Pages in the conference proceedings")
230 ("note" "Remarks to be put at the end of the \\bibitem")))))
231 ("Manual" . (((("title" "Title of the manual"))
232 (("author" "Author1 [and Author2 ...] [and others]")
233 ("organization" "Publishing organization of the manual")
234 ("address" "Address of the organization")
235 ("edition" "Edition of the manual as a capitalized English word")
236 ("year" "Year of publication")
237 ("month" "Month of the publication as a string (remove braces)")
238 ("note" "Remarks to be put at the end of the \\bibitem")))))
239
240 ("MastersThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
241 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
242 ("school" "School where the master\'s thesis was written")
243 ("year" "Year of publication"))
244 (("address" "Address of the school (if not part of field \"school\") or country")
245 ("type" "Type of the master\'s thesis")
246 ("month" "Month of the publication as a string (remove braces)")
247 ("note" "Remarks to be put at the end of the \\bibitem")))))
248 ("Misc" . ((()
249 (("author" "Author1 [and Author2 ...] [and others]")
250 ("title" "Title of the reference (BibTeX converts it to lowercase)")
251 ("howpublished" "The way in which the reference was published")
252 ("year" "Year of publication")
253 ("month" "Month of the publication as a string (remove braces)")
254 ("note" "Remarks to be put at the end of the \\bibitem")))))
255 ("PhdThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
256 ("title" "Title of the PhD. thesis")
257 ("school" "School where the PhD. thesis was written")
258 ("year" "Year of publication"))
259 (("address" "Address of the school (if not part of field \"school\") or country")
260 ("type" "Type of the PhD. thesis")
261 ("month" "Month of the publication as a string (remove braces)")
262 ("note" "Remarks to be put at the end of the \\bibitem")))))
263 ("Proceedings" . (((("title" "Title of the conference proceedings")
264 ("year" "Year of publication"))
265 (("editor" "Editor1 [and Editor2 ...] [and others]")
266 ("volume" "Volume of the conference proceedings in the series")
267 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
268 ("series" "Series in which the conference proceedings appeared")
269 ("publisher" "Publishing company, its location")
270 ("organization" "Sponsoring organization of the conference")
271 ("address" "Location of the Proceedings")
272 ("month" "Month of the publication as a string (remove braces)")
273 ("note" "Remarks to be put at the end of the \\bibitem")))))
274 ("TechReport" . (((("author" "Author1 [and Author2 ...] [and others]")
275 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
276 ("institution" "Sponsoring institution of the report")
277 ("year" "Year of publication"))
278 (("type" "Type of the report (if other than \"technical report\")")
279 ("number" "Number of the technical report")
280 ("address" "Address of the institution (if not part of field \"institution\") or country")
281 ("month" "Month of the publication as a string (remove braces)")
282 ("note" "Remarks to be put at the end of the \\bibitem")))))
283 ("Unpublished" . (((("author" "Author1 [and Author2 ...] [and others]")
284 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)")
285 ("note" "Remarks to be put at the end of the \\bibitem"))
286 (("year" "Year of publication")
287 ("month" "Month of the publication as a string (remove braces)")))))
288 )
289
290 "Defines reference types and their associated fields.
291 List of
292 (entry-name (required optional) (crossref-required crossref-optional))
293 triples.
294 If the third element is nil, the first pair is always to be used.
295 If not, the second pair is to be used in the case of presence of a
296 crossref field and the third in the case of absence.
297 Required , optional, crossref-required and crossref-optional are lists.
298 Each element of these lists is a list of strings with two elements
299 (first element = name of the field,
300 second element = comment to appear in the echo area).")
301
302 (defvar bibtex-predefined-strings
303 '(
304 ("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug")
305 ("sep") ("oct") ("nov") ("dec")
306 ("acmcs") ("acta") ("cacm") ("ibmjrd") ("ibmsj") ("ieeese")
307 ("ieeetc") ("ieeetcad") ("ipl") ("jacm") ("jcss") ("scp")
308 ("sicomp") ("tcs") ("tocs") ("tods") ("tog") ("toms") ("toois")
309 ("toplas")
310 )
311 "Alist of string definitions.
312 Should contain the strings defined in the BibTeX style files. Each
313 element is a list with just one element: the string.")
314
315 (defvar bibtex-string-files nil
316 "*List of BibTeX files containing string definitions.
317 Those files must be specified using pathnames relative to the
318 directories specified in $BIBINPUTS. This variable is only evaluated
319 when bibtex-mode is entered (i. e. when loading the BibTeX file).")
320
321 (defvar bibtex-help-message t
322 "*If not nil print help messages in the echo area on entering a new field.")
323
324 (defvar bibtex-autokey-names 1
325 "*Number of names to use for the automatically generated reference key.
326 If this is set to anything but a number, all names are used.
327 See the documentation of function bibtex-generate-autokey for further detail.")
328
329 (defvar bibtex-autokey-name-change-strings
330 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
331 ("\\\\\\\"s" "ss")
332 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
333 ("{" "") ("}" ""))
334 "Alist of (old-regexp new-string) pairs.
335 Any part of name matching a old-regexp is replaced by new-string.
336 Case of the old-regexp is significant. All regexps are tried in the
337 order in which they appear in the list, so be sure to avoid recursion here.
338 See the documentation of function bibtex-generate-autokey for further detail.")
339
340 (defvar bibtex-autokey-name-length 'infty
341 "*Number of characters from name to incorporate into key.
342 If this is set to anything but a number, all characters are used.
343 See the documentation of function bibtex-generate-autokey for further detail.")
344
345 (defvar bibtex-autokey-name-separator ""
346 "*String that comes between any two names in the key.
347 See the documentation of function bibtex-generate-autokey for further detail.")
348
349 (defvar bibtex-autokey-year-length 2
350 "*Number of rightmost digits from the year field yo incorporate into key.
351 See the documentation of function bibtex-generate-autokey for further detail.")
352
353 (defvar bibtex-autokey-titlewords 5
354 "*Number of title words to use for the automatically generated reference key.
355 If this is set to anything but a number, all title words are used.
356 See the documentation of function bibtex-generate-autokey for further detail.")
357
358 (defvar bibtex-autokey-title-terminators
359 '("\\." "!" "\\?" ":" ";" "---")
360 "*Regexp list defining the termination of the main part of the title.
361 Case of the regexps is ignored.
362 See the documentation of function bibtex-generate-autokey for further detail.")
363
364 (defvar bibtex-autokey-titlewords-stretch 2
365 "*Number of words that can additionally be used from the title.
366 These words are used only, if a sentence from the title can be ended then.
367 See the documentation of function bibtex-generate-autokey for further detail.")
368
369 (defvar bibtex-autokey-titleword-first-ignore
370 '("a" "an" "on" "the" "eine?" "der" "die" "das")
371 "*Determines words that may begin a title but are not to be used in the key.
372 Each item of the list is a regexp. If the first word of the title matchs a
373 regexp from that list, it is not included in the title, even if it is
374 capitalized. Regexps in the list must be entered using lowercase letters.")
375
376 (defvar bibtex-autokey-titleword-abbrevs nil
377 "*Determines exceptions to the usual abbreviation mechanism.
378 A list of (old-regexp new-string) pairs.
379 Use all lowercase letters for old-regexp.
380 See the documentation of function bibtex-generate-autokey for further detail.")
381
382 (defvar bibtex-autokey-titleword-change-strings
383 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
384 ("\\\\\\\"s" "ss")
385 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
386 ("{" "") ("}" ""))
387 "Alist of (old-regexp new-string) pairs.
388 Any part of title word matching a old-regexp is replaced by new-string.
389 Case of the old-regexp is significant.
390 See the documentation of function bibtex-generate-autokey for further detail.")
391
392 (defvar bibtex-autokey-titleword-length 5
393 "*Number of characters from title words to incorporate into key.
394 If this is set to anything but a number, all characters are used.
395 See the documentation of function bibtex-generate-autokey for further detail.")
396
397 (defvar bibtex-autokey-titleword-separator "_"
398 "*String to be put between the title words.
399 See the documentation of function bibtex-generate-autokey for further detail.")
400
401 (defvar bibtex-autokey-name-year-separator ""
402 "*String to be put between name part and year part of key.
403 See the documentation of function bibtex-generate-autokey for further detail.")
404
405 (defvar bibtex-autokey-year-title-separator ":_"
406 "*String to be put between name part and year part of key.
407 See the documentation of function bibtex-generate-autokey for further detail.")
408
409 (defvar bibtex-autokey-edit-before-use t
410 "*If non-nil, user is allowed to edit the generated key before it is used.")
411
412 (defvar bibtex-font-lock-keywords
413 (list
414 "^@[A-Za-z]*[({]"
415 ;; reference type
416 '("^\\([ \t]*OPT[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
417 1 font-lock-comment-face)
418 ;; optional field names
419 '("^\\([ \t]*[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
420 1 font-lock-function-name-face)
421 ;; field names
422 '("^@[A-Za-z]*[({]\\([^\n,]*\\),"
423 1 font-lock-string-face)
424 ;; reference labels
425 )
426 "*Fonts to use in BibTeX mode")
427
428 \f
429 ;; Syntax Table, Keybindings and BibTeX Entry List
430 (defvar bibtex-mode-syntax-table
431 (let ((st (make-syntax-table)))
432 ;; [alarson:19920214.1004CST] make double quote a string quote
433 (modify-syntax-entry ?\" "\"" st)
434 (modify-syntax-entry ?$ "$$ " st)
435 (modify-syntax-entry ?% "< " st)
436 (modify-syntax-entry ?' "w " st)
437 (modify-syntax-entry ?@ "w " st)
438 (modify-syntax-entry ?\\ "\\" st)
439 (modify-syntax-entry ?\f "> " st)
440 (modify-syntax-entry ?\n "> " st)
441 (modify-syntax-entry ?~ " " st)
442 st))
443
444 (defvar bibtex-mode-map
445 (let ((km (make-sparse-keymap)))
446
447 (define-key km "\t" 'bibtex-find-text)
448 (define-key km "\n" 'bibtex-next-field)
449 (define-key km "\M-\t" 'bibtex-complete-string)
450 (define-key km "\C-c\"" 'bibtex-remove-double-quotes-or-braces)
451 (define-key km "\C-c{" 'bibtex-remove-double-quotes-or-braces)
452 (define-key km "\C-c}" 'bibtex-remove-double-quotes-or-braces)
453 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
454 (define-key km "\C-c?" 'bibtex-print-help-message)
455 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
456 (define-key km "\C-c\C-n" 'bibtex-pop-next)
457 (define-key km "\C-c\C-k" 'bibtex-kill-optional-field)
458 (define-key km "\C-c\C-d" 'bibtex-empty-field)
459 (define-key km "\C-c$" 'bibtex-ispell-entry)
460 (define-key km "\M-\C-a" 'bibtex-beginning-of-entry)
461 (define-key km "\M-\C-e" 'bibtex-end-of-entry)
462 (define-key km "\C-c\C-b" 'bibtex-entry)
463 (define-key km "\C-c\C-q" 'bibtex-hide-entry-bodies)
464 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
465 (define-key km "\C-c\C-rw" 'widen)
466 (define-key km "\C-c\C-o" 'bibtex-remove-OPT)
467
468 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
469 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
470 (define-key km "\C-c\C-eI" 'bibtex-InBook)
471 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
472 (define-key km "\C-c\C-e\C-b" 'bibtex-InBook)
473 (define-key km "\C-c\C-eb" 'bibtex-Book)
474 (define-key km "\C-c\C-eB" 'bibtex-Booklet)
475 (define-key km "\C-c\C-e\C-c" 'bibtex-InCollection)
476 (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
477 (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
478 (define-key km "\C-c\C-eM" 'bibtex-Misc)
479 (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
480 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
481 (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
482 (define-key km "\C-c\C-e\M-p" 'bibtex-preamble)
483 (define-key km "\C-c\C-e\C-s" 'bibtex-string)
484 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
485 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
486 km))
487
488 (define-key bibtex-mode-map [menu-bar bibtex-edit]
489 (cons "BibTeX-Edit" (make-sparse-keymap "BibTeX-Edit")))
490 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-print-help-message]
491 '("Help about Current Field" . bibtex-print-help-message))
492 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-complete-string]
493 '("String Complete" . bibtex-complete-string))
494 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-next-field]
495 '("Next Field" . bibtex-next-field))
496 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-find-text]
497 '("End of Field" . bibtex-find-text))
498 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-pop-previous]
499 '("Snatch from Similar Preceding Field" . bibtex-pop-previous))
500 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-pop-next]
501 '("Snatch from Similar Following Field" . bibtex-pop-next))
502 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-remove-OPT]
503 '("Remove OPT" . bibtex-remove-OPT))
504 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-remove-double-quotes-or-braces]
505 '("Remove Quotes or Braces" . bibtex-remove-double-quotes-or-braces))
506 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-clean-entry]
507 '("Clean Up Entry" . bibtex-clean-entry))
508 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-sort-entries]
509 '("Sort Entries" . bibtex-sort-entries))
510 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-validate-buffer]
511 '("Validate Entries" . bibtex-validate-buffer))
512
513 (define-key bibtex-mode-map [menu-bar entry-types]
514 (cons "Entry-Types" (make-sparse-keymap "Entry-Types")))
515 (define-key bibtex-mode-map [menu-bar entry-types bibtex-preamble]
516 '("Preamble" . bibtex-preamble))
517 (define-key bibtex-mode-map [menu-bar entry-types bibtex-string]
518 '("String" . bibtex-string))
519 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Misc]
520 '("Miscellaneous" . bibtex-Misc))
521 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Unpublished]
522 '("Unpublished" . bibtex-Unpublished))
523 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Manual]
524 '("Technical Manual" . bibtex-Manual))
525 (define-key bibtex-mode-map [menu-bar entry-types bibtex-TechReport]
526 '("Technical Report" . bibtex-TechReport))
527 (define-key bibtex-mode-map [menu-bar entry-types bibtex-MastersThesis]
528 '("Master's Thesis" . bibtex-MastersThesis))
529 (define-key bibtex-mode-map [menu-bar entry-types bibtex-PhdThesis]
530 '("PhD. Thesis" . bibtex-PhdThesis))
531 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Booklet]
532 '("Booklet (Bound, but no Publisher/Institution)" . bibtex-Booklet))
533 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Book]
534 '("Book" . bibtex-Book))
535 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Proceedings]
536 '("Conference Proceedings" . bibtex-Proceedings))
537 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InBook]
538 '("Chapter or Pages in a Book" . bibtex-InBook))
539 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InCollection]
540 '("Article in a Collection" . bibtex-InCollection))
541 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InProceedings]
542 '("Article in Conference Proceedings" . bibtex-InProceedings))
543 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Article]
544 '("Article in Journal" . bibtex-Article))
545
546
547 \f
548 ;; Internal Variables
549
550 (defvar bibtex-pop-previous-search-point nil)
551 ;; Next point where bibtex-pop-previous starts looking for a similar
552 ;; entry.
553
554 (defvar bibtex-pop-next-search-point nil)
555 ;; Next point where bibtex-pop-next starts looking for a similar entry.
556
557 (defvar bibtex-completion-candidates nil)
558 ;; Candidates for bibtex-complete-string. Initialized from
559 ;; bibtex-predefined-strings and bibtex-string-files. This variable is
560 ;; buffer-local.
561 (make-variable-buffer-local 'bibtex-completion-candidates)
562
563 (defvar bibtex-keys nil)
564 ;; Candidates for TAB completion when entering a reference key using
565 ;; the minibuffer. Initialized in bibtex-mode and updated for each
566 ;; new entry. This variable is buffer-local.
567 (make-variable-buffer-local 'bibtex-keys)
568
569 (defvar bibtex-buffer-last-parsed-for-keys-tick nil)
570 ;; Remembers the value returned by buffer-modified-tick when buffer
571 ;; was parsed for keys the last time.
572 (make-variable-buffer-local 'bibtex-keys)
573
574
575 \f
576 ;; Functions to Parse the BibTeX Entries
577
578 (defun bibtex-cfield (name text)
579 ;; Create a regexp for a BibTeX field of name NAME and text TEXT.
580 (concat ",[ \t\n]*\\("
581 name
582 "\\)[ \t\n]*=[ \t\n]*\\("
583 text
584 "\\)"))
585 (defconst bibtex-name-in-cfield 1)
586 ;; The regexp subexpression number of the name part in bibtex-cfield.
587
588 (defconst bibtex-text-in-cfield 2)
589 ;; The regexp subexpression number of the text part in bibtex-cfield.
590
591 (defconst bibtex-field-name "[A-Za-z_-][A-Za-z0-9_-]*")
592 ;; Regexp defining the name part of a BibTeX field.
593
594 (defconst bibtex-field-const "[0-9A-Za-z][A-Za-z0-9:_+-]*"
595 "Format of a bibtex field constant.")
596
597 (defconst bibtex-field-string
598 (concat
599 "\\("
600 "{\\(\\({\\(\\({[^}]*}\\)\\|\\([^{}]\\)\\)*}\\)\\|\\([^{}]\\)\\)*}"
601 ;; maximal twice nested {}
602 "\\)\\|\\("
603 "\"[^\"]*[^\\\\]\"\\|\"\""
604 "\\)"))
605 ;; Match either a string or an empty string.
606
607 (defconst bibtex-field-string-or-const
608 (concat bibtex-field-const "\\|" bibtex-field-string))
609 ;; Match either bibtex-field-string or bibtex-field-const.
610
611 (defconst bibtex-field-text
612 (concat
613 "\\(" bibtex-field-string-or-const "\\)"
614 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*"))
615 ;; Regexp defining the text part of a BibTeX field: either a string,
616 ;; or an empty string, or a constant followed by one or more # /
617 ;; constant pairs.
618
619 (defconst bibtex-field
620 (bibtex-cfield bibtex-field-name bibtex-field-text))
621 ;; Regexp defining the format of a BibTeX field.
622
623 (defconst bibtex-name-in-field bibtex-name-in-cfield)
624 ;; The regexp subexpression number of the name part in BibTeX-field.
625
626 (defconst bibtex-text-in-field bibtex-text-in-cfield)
627 ;; The regexp subexpression number of the text part in BibTeX-field.
628
629 (defconst bibtex-reference-type "@[A-Za-z]+")
630 ;; Regexp defining the type part of a BibTeX reference entry.
631
632 (defconst bibtex-reference-key "[A-Za-z][A-Za-z0-9.:;?!`'/*@_+-]*")
633 ;; Regexp defining the label part of a BibTeX reference entry.
634
635 (defconst bibtex-reference-head
636 (concat "^\\( \\|\t\\)*\\("
637 bibtex-reference-type
638 "\\)[ \t]*[({]\\("
639 bibtex-reference-key
640 "\\)"))
641 ;; Regexp defining format of the header line of a BibTeX reference
642 ;; entry.
643
644 (defconst bibtex-reference-maybe-empty-head
645 (concat bibtex-reference-head "?"))
646 ;; Regexp defining format of the header line of a maybe empty
647 ;; BibTeX reference entry (without reference key).
648
649 (defconst bibtex-type-in-head 2)
650 ;; The regexp subexpression number of the type part in
651 ;; bibtex-reference-head.
652
653 (defconst bibtex-key-in-head 3)
654 ;; The regexp subexpression number of the key part in
655 ;; bibtex-reference-head.
656
657 (defconst bibtex-reference
658 (concat bibtex-reference-head
659 "\\([ \t\n]*" bibtex-field "\\)*"
660 "[ \t\n]*,?[ \t\n]*[})]"))
661 ;; Regexp defining the format of a BibTeX reference entry.
662
663 (defconst bibtex-type-in-reference bibtex-type-in-head)
664 ;; The regexp subexpression number of the type part in
665 ;; bibtex-reference.
666
667 (defconst bibtex-key-in-reference bibtex-key-in-head)
668 ;; The regexp subexpression number of the key part in
669 ;; bibtex-reference.
670
671 (defconst bibtex-string
672 (concat "^[ \t]*@[sS][tT][rR][iI][nN][gG][ \t\n]*[({][ \t\n]*\\("
673 bibtex-reference-key
674 "\\)[ \t\n]*=[ \t\n]*\\("
675 bibtex-field-text
676 "\\)[ \t\n]*[})]"))
677 ;; Regexp defining the format of a BibTeX string entry.
678
679 (defconst bibtex-key-in-string 1)
680 ;; The regexp subexpression of the name part in bibtex-string.
681
682 (defconst bibtex-text-in-string 2)
683 ;; The regexp subexpression of the text part in bibtex-string.
684
685 (defconst bibtex-name-alignment 2)
686 ;; Alignment for the name part in BibTeX fields. Chosen on aesthetic
687 ;; grounds only.
688
689 (defconst bibtex-text-alignment (length " organization = "))
690 ;; Alignment for the text part in BibTeX fields. Equal to the space
691 ;; needed for the longest name part.
692
693
694 \f
695 ;; Helper Functions
696
697 (defun assoc-ignore-case (string alist)
698 ;; Return non-nil if STRING is `equal' to the car of an element of
699 ;; LIST. Comparison is done with case ignored. The value is actually
700 ;; the element of LIST whose car is `equal' to STRING.
701 (or (assoc string alist)
702 (while (and alist
703 (not (string-equal
704 (downcase string)
705 (downcase (car (car alist))))))
706 (setq alist (cdr alist)))
707 (car alist)))
708
709 (defun member-of-regexp (string list)
710 ;; Return non-nil if STRING is exactly matched by an element of
711 ;; LIST. This function is influenced by the actual value of
712 ;; `case-fold-search'. The value is actually the tail of LIST whose
713 ;; car matches STRING.
714 (while
715 (and
716 list
717 (not
718 (string-match
719 (concat "^" (car list) "$")
720 string)))
721 (setq list (cdr list)))
722 list)
723
724 (defun assoc-of-regexp (string alist)
725 ;; Return non-nil if STRING is exactly matched by the car of an
726 ;; element of LIST. This function is influenced by the actual value
727 ;; of `case-fold-search'. The value is actually the element of LIST
728 ;; whose car matches STRING.
729 (while
730 (and
731 alist
732 (not
733 (string-match
734 (concat "^" (car (car alist)) "$")
735 string)))
736 (setq alist (cdr alist)))
737 (car alist))
738
739 (defun skip-whitespace-and-comments ()
740 (let ((md (match-data)))
741 (unwind-protect
742 (while (cond ((looking-at "\\s>+\\|\\s +")
743 ;; was whitespace
744 ;; NOTE: also checked end-comment. In latex and
745 ;; lisp modes, newline is an end comment, but it
746 ;; should also be a whitespace char.
747 (goto-char (match-end 0)))
748 ;; If looking at beginning of comment, skip to end.
749 ((looking-at "\\s<")
750 (re-search-forward "\\s>"))))
751 (store-match-data md))))
752
753 (defun map-bibtex-entries (fun)
754 ;; Call FUN for each BibTeX entry starting with the current. Do this
755 ;; to the end of the file. FUN is called with one argument, the key
756 ;; of the entry, and with point inside the entry. If
757 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be called
758 ;; for @string entries.
759 (bibtex-beginning-of-entry)
760 (while (re-search-forward bibtex-reference-head nil t)
761 (if (and bibtex-sort-ignore-string-entries
762 (string-equal "@string"
763 (downcase (buffer-substring-no-properties
764 (match-beginning bibtex-type-in-head)
765 (match-end bibtex-type-in-head)))))
766 nil
767 (funcall fun (buffer-substring-no-properties
768 (match-beginning bibtex-key-in-head)
769 (match-end bibtex-key-in-head))))))
770
771 (defun bibtex-flash-head ()
772 ;; Flash at BibTeX reference head before point, if exists.
773 (let ((flash))
774 (cond ((re-search-backward bibtex-reference-head (point-min) t)
775 (goto-char (match-beginning bibtex-type-in-head))
776 (setq flash (match-end bibtex-key-in-reference)))
777 (t
778 (end-of-line)
779 (skip-chars-backward " \t")
780 (setq flash (point))
781 (beginning-of-line)
782 (skip-chars-forward " \t")))
783 (if (pos-visible-in-window-p (point))
784 (sit-for 1)
785 (message "From: %s"
786 (buffer-substring (point) flash)))))
787
788 (defun bibtex-move-outside-of-entry ()
789 ;; Make sure we are outside of a BibTeX entry.
790 (cond ((or
791 (= (point) (point-max))
792 (= (point) (point-min))
793 (looking-at "[ \n]*@")
794 )
795 t)
796 (t
797 (backward-paragraph)
798 (forward-paragraph)))
799 (re-search-forward "[ \t\n]*" (point-max) t))
800
801 (defun beginning-of-first-bibtex-entry ()
802 ;; Go to the beginning of the first BibTeX entry in buffer.
803 (goto-char (point-min))
804 (cond
805 ((re-search-forward "^@" nil 'move)
806 (beginning-of-line))
807 ((and (bobp) (eobp))
808 nil)
809 (t
810 (message "Warning: No BibTeX entries found!"))))
811
812 (defun bibtex-inside-field ()
813 ;; Try to avoid point being at end of a BibTeX field.
814 (end-of-line)
815 (skip-chars-backward " \t")
816 (cond ((= (preceding-char) ?,)
817 (forward-char -2)))
818 (cond ((or
819 (= (preceding-char) ?})
820 (= (preceding-char) ?\"))
821 (forward-char -1))))
822
823 (defun bibtex-enclosing-field ()
824 ;; Search for BibTeX field enclosing point. Point moves to end of
825 ;; field; also, use match-beginning and match-end to parse the field.
826 ;; sct@dcs.edinburgh.ac.uk
827 (let ((old-point (point)))
828 (condition-case errname
829 (bibtex-enclosing-regexp bibtex-field)
830 (search-failed
831 (goto-char old-point)
832 (error "Can't find enclosing BibTeX field.")))))
833
834 (defun bibtex-enclosing-reference ()
835 ;; Search for BibTeX reference enclosing point. Point moves to
836 ;; beginning of reference. Beginning/end of reference is given by
837 ;; (match-beginning/match-end 0).
838 (let ((old-point (point)))
839 (if (not
840 (re-search-backward bibtex-reference-head (point-min) t))
841 (progn
842 (error "Can't find enclosing BibTeX reference.")
843 (goto-char old-point)))
844 (goto-char (match-beginning bibtex-type-in-head))
845 (let ((pnt (point)))
846 (if (not
847 (re-search-forward bibtex-reference (point-max) t))
848 (progn
849 (error "Can't find enclosing BibTeX reference.")
850 (goto-char old-point))
851 (goto-char pnt)))))
852
853 (defun bibtex-enclosing-reference-maybe-empty-head ()
854 ;; Search for BibTeX reference enclosing point. Point moves to
855 ;; beginning of reference. Beginning/end of reference is given by
856 ;; (match-beginning/match-end 0).
857 (let ((old-point (point)))
858 (if (not
859 (re-search-backward
860 bibtex-reference-maybe-empty-head (point-min) t))
861 (progn
862 (error "Can't find enclosing BibTeX reference.")
863 (goto-char old-point)))
864 (goto-char (match-beginning bibtex-type-in-head))
865 (let ((pnt (point)))
866 (if (not
867 (re-search-forward
868 (concat
869 bibtex-reference-maybe-empty-head
870 "\\([ \t\n]*" bibtex-field "\\)*"
871 "[ \t\n]*,?[ \t\n]*[})]")
872 (point-max) t))
873 (progn
874 (error "Can't find enclosing BibTeX reference.")
875 (goto-char old-point))
876 (goto-char pnt)))))
877
878 (defun bibtex-enclosing-regexp (regexp)
879 ;; Search for REGEXP enclosing point. Point moves to end of
880 ;; REGEXP. See also match-beginning and match-end. If an enclosing
881 ;; REGEXP is not found, signals search-failed; point is left in an
882 ;; undefined location.
883 ;; Doesn't something like this exist already?
884 ; compute reasonable limits for the loop
885 (let* ((initial (point))
886 (right (if (re-search-forward regexp (point-max) t)
887 (match-end 0)
888 (point-max)))
889 (left
890 (progn
891 (goto-char initial)
892 (if (re-search-backward regexp (point-min) t)
893 (match-beginning 0)
894 (point-min)))))
895 ; within the prescribed limits, loop until a match is found
896 (goto-char left)
897 (re-search-forward regexp right nil 1)
898 (if (> (match-beginning 0) initial)
899 (signal 'search-failed (list regexp)))
900 (while (<= (match-end 0) initial)
901 (re-search-forward regexp right nil 1)
902 (if (> (match-beginning 0) initial)
903 (signal 'search-failed (list regexp))))
904 ))
905
906 (defun bibtex-autokey-change (string change-list)
907 ;; Returns a string where some regexps are changed according to
908 ;; change-list. Every item of change-list is an (old-regexp
909 ;; new-string) pair.
910 (let ((return-string string)
911 case-fold-search
912 (index 0)
913 (len (length change-list))
914 change-item)
915 (while (< index len)
916 (setq change-item (elt change-list index))
917 (while (string-match (car change-item) return-string)
918 (setq
919 return-string
920 (concat (substring return-string 0 (match-beginning 0))
921 (elt change-item 1)
922 (substring return-string (match-end 0)))))
923 (setq index (1+ index)))
924 return-string))
925
926 (defun bibtex-autokey-abbrev (string len)
927 ;; Returns an abbreviation of string with at least len
928 ;; characters. String is aborted only after a consonant or at the
929 ;; word end. If len is not a number, string is returned unchanged.
930 (let* ((string-length (length string))
931 (len (if (numberp len)
932 (min len string-length)
933 len))
934 (return-string (if (numberp len)
935 (substring string 0 len)))
936 (index len)
937 (vowels '(?a ?e ?i ?o ?u ?A ?E ?I ?O ?U)))
938 (if (numberp len)
939 (progn
940 (while (and
941 (< index string-length)
942 (member (elt return-string
943 (1- (length return-string)))
944 vowels))
945 (setq return-string (concat return-string
946 (substring
947 string index (1+ index)))
948 index (1+ index)))
949 return-string)
950 string)))
951
952 (defun bibtex-generate-autokey ()
953 "Generates automatically a key from the author/editor and the title field.
954 The generation algorithm works as follows:
955 1. If there is a non-empty author (preferred) or editor field,
956 use it for the name part of the key.
957 2. Change any substring found in `bibtex-autokey-name-change-strings'
958 to the corresponding new one (see documentation of this variable
959 for further detail).
960 3. For every of the first `bibtex-autokey-names' names in the
961 \"name\" field, determine the last name.
962 4. From every last name, take at least `bibtex-autokey-name-length'
963 characters (abort only after a consonant or at a word end).
964 5. Build the name part of the key by concatenating all abbreviated last
965 names with the string `bibtex-autokey-name-separator' between
966 any two.
967 6. Build the year part of the key by truncating the contents of the
968 \"year\" field to the rightmost `bibtex-autokey-year-length'
969 digits (useful values are 2 and 4).
970 7. For the title part of the key change the contents of the \"title\"
971 field of the reference according to
972 `bibtex-autokey-titleword-change-strings' to the corresponding
973 new one (see documentation of this variable for further detail).
974 8. Abbreviate the result to the string up to (but not including) the
975 first occurrence of a regexp matched by the items of
976 `bibtex-autokey-title-terminators' and delete the first
977 word if it appears in `bibtex-autokey-titleword-first-ignore'.
978 Build the title part of the key by using at least the first
979 `bibtex-autokey-titlewords' capitalized words from this
980 abbreviated title. If the abbreviated title ends after maximal
981 `bibtex-autokey-titlewords' + `bibtex-autokey-titlewords-stretch'
982 capitalized words, all capitalized words from the abbreviated title
983 are used.
984 9. For every used title word that appears in
985 `bibtex-autokey-titleword-abbrevs' use the corresponding abbreviation
986 (see documentation of this variable for further detail).
987 10. From every title word not generated by an abbreviation, take at
988 least `bibtex-autokey-titleword-length' characters (abort only after
989 a consonant or at a word end).
990 11. Build the title part of the key by concatenating all abbreviated
991 title words with the string `bibtex-autokey-titleword-separator'
992 between any two.
993 12. At least, to get the key, concatenate the name part, the year part
994 and the title part with `bibtex-autokey-name-year-separator'
995 between the name and the year if both are non-empty and
996 `bibtex-autokey-year-title-separator' between the year and
997 the title if both are non-empty."
998
999 (let* ((pnt (point))
1000 (min
1001 (progn
1002 (bibtex-beginning-of-entry)
1003 (point)))
1004 (max
1005 (progn
1006 (bibtex-end-of-entry)
1007 (point)))
1008 (namefield
1009 (progn
1010 (goto-char min)
1011 (if (or
1012 (re-search-forward "^[ \t]*author[ \t]*=" max t)
1013 (re-search-forward "^[ \t]*editor[ \t]*=" max t))
1014 (let* (bibtex-help-message
1015 (start (progn
1016 (bibtex-find-text t)
1017 (point)))
1018 (end (progn
1019 (bibtex-find-text nil)
1020 (point))))
1021 (bibtex-autokey-change
1022 (buffer-substring-no-properties start end)
1023 bibtex-autokey-name-change-strings))
1024 "")))
1025 (namelist
1026 (mapcar
1027 (function
1028 (lambda (fullname)
1029 (bibtex-autokey-abbrev
1030 (if (string-match "," fullname)
1031 (substring fullname 0 (match-beginning 0))
1032 (progn
1033 (if (string-match " [^ ]*$" fullname)
1034 (substring
1035 fullname (1+ (match-beginning 0)))
1036 fullname)))
1037 bibtex-autokey-name-length)))
1038 ;; Gather all names into a list
1039 (let (names
1040 (counter 0))
1041 (while (and
1042 (not (equal namefield ""))
1043 (or
1044 (not (numberp bibtex-autokey-names))
1045 (< counter bibtex-autokey-names)))
1046 (if (string-match " and " namefield)
1047 (progn
1048 (setq
1049 names
1050 (append names
1051 (list
1052 (downcase
1053 (substring
1054 namefield 0 (match-beginning 0)))))
1055 namefield
1056 (substring namefield (match-end 0))))
1057 (setq names
1058 (append names (list (downcase namefield)))
1059 namefield ""))
1060 (setq counter (1+ counter)))
1061 names)))
1062 (namepart (mapconcat (function (lambda (name) name))
1063 namelist
1064 bibtex-autokey-name-separator))
1065 (yearfield
1066 (progn
1067 (goto-char min)
1068 (if (re-search-forward
1069 "^[ \t]*year[ \t]*=[ \t]*\\([0-9]*\\)" max t)
1070 (buffer-substring-no-properties
1071 (match-beginning 1) (match-end 1))
1072 "")))
1073 (yearpart
1074 (if (equal yearfield "")
1075 ""
1076 (substring yearfield
1077 (- (length yearfield)
1078 bibtex-autokey-year-length))))
1079 (titlestring
1080 (let ((case-fold-search t)
1081 (titlefield
1082 (progn
1083 (goto-char min)
1084 (if (re-search-forward
1085 "^[ \t]*title[ \t]*=" max t)
1086 (let* (bibtex-help-message
1087 (start (progn
1088 (bibtex-find-text t)
1089 (point)))
1090 (end (progn
1091 (bibtex-find-text nil)
1092 (point))))
1093 (bibtex-autokey-change
1094 (buffer-substring-no-properties start end)
1095 bibtex-autokey-titleword-change-strings))
1096 "")))
1097 case-fold-search
1098 (index 0)
1099 (numberofitems
1100 (length bibtex-autokey-title-terminators)))
1101 (while (< index numberofitems)
1102 (if (string-match
1103 (elt bibtex-autokey-title-terminators index)
1104 titlefield)
1105 (setq titlefield
1106 (substring titlefield 0 (match-beginning 0))))
1107 (setq index (1+ index)))
1108 titlefield))
1109 (titlelist
1110 (mapcar
1111 (function
1112 (lambda (titleword)
1113 (let ((abbrev
1114 (assoc-of-regexp
1115 titleword bibtex-autokey-titleword-abbrevs)))
1116 (if abbrev
1117 (elt abbrev 1)
1118 (bibtex-autokey-abbrev
1119 titleword
1120 bibtex-autokey-titleword-length)))))
1121 ;; Gather all titlewords into a list
1122 (let (titlewords
1123 titlewords-extra
1124 case-fold-search
1125 (counter 0)
1126 (first t))
1127 (while (and
1128 (not (equal titlestring ""))
1129 (or
1130 (not (numberp bibtex-autokey-titlewords))
1131 (< counter (+
1132 bibtex-autokey-titlewords
1133 bibtex-autokey-titlewords-stretch))))
1134 (if (string-match "\\b[A-Z][A-Za-z0-9]*" titlestring)
1135 (let* ((end-match (match-end 0))
1136 (titleword
1137 (downcase (substring titlestring
1138 (match-beginning 0)
1139 end-match))))
1140 (if (or
1141 (not (numberp bibtex-autokey-titlewords))
1142 (< counter bibtex-autokey-titlewords))
1143 (if (and
1144 first
1145 (member-of-regexp
1146 titleword
1147 bibtex-autokey-titleword-first-ignore))
1148 (setq counter -1)
1149 (setq titlewords
1150 (append titlewords (list titleword))))
1151 (setq
1152 titlewords-extra
1153 (append titlewords-extra (list titleword))))
1154 (setq titlestring
1155 (substring titlestring end-match)))
1156 (setq titlestring ""))
1157 (setq first nil
1158 counter (1+ counter)))
1159 (if (string-match "\\b[A-Z][^ ]*\\b" titlestring)
1160 titlewords
1161 (append titlewords titlewords-extra)))))
1162 (titlepart (mapconcat (function (lambda (name) name))
1163 titlelist
1164 bibtex-autokey-titleword-separator))
1165 (autokey
1166 (concat
1167 namepart
1168 (if (not
1169 (or
1170 (equal namepart "")
1171 (equal yearpart "")))
1172 bibtex-autokey-name-year-separator)
1173 yearpart
1174 (if (not
1175 (or
1176 (and
1177 (equal namepart "")
1178 (equal yearpart ""))
1179 (equal titlepart "")))
1180 bibtex-autokey-year-title-separator)
1181 titlepart)))
1182 (goto-char pnt)
1183 autokey))
1184
1185 (defun bibtex-parse-keys (add &optional abortable)
1186 ;; Sets bibtex-keys to the keys used in the whole (possibly
1187 ;; restricted) buffer (either as entry keys or as crossref entries).
1188 ;; If ADD is non-nil adds the new keys to bibtex-keys instead of
1189 ;; simply resetting it. If ABORTABLE is non-nil abort on user input.
1190 (if bibtex-maintain-sorted-entries
1191 (let ((labels (if add
1192 bibtex-keys))
1193 label
1194 (case-fold-search t))
1195 (save-excursion
1196 (goto-char (point-min))
1197 (if (not add)
1198 (message "Parsing reference keys..."))
1199
1200 (if (not
1201 (catch 'userkey
1202 (while
1203 (re-search-forward
1204 (concat
1205 "\\("
1206 bibtex-reference-head
1207 "\\)\\|\\("
1208 "^[ \t\n]*crossref[ \t\n]*=[ \t\n]*[{\"]\\([A-Za-z][]A-Za-z0-9.:;?!`'()/*@_+-]*\\)[}\"],?$"
1209 "\\)")
1210 nil t)
1211 (if (and
1212 abortable
1213 (input-pending-p))
1214 (throw 'userkey t))
1215 (if (match-beginning (1+ bibtex-key-in-head))
1216 (setq
1217 label
1218 (buffer-substring-no-properties
1219 (match-beginning (1+ bibtex-key-in-head))
1220 (match-end (1+ bibtex-key-in-head))))
1221 (setq
1222 label
1223 (buffer-substring-no-properties
1224 (match-beginning (+ 3 bibtex-key-in-head))
1225 (match-end (+ 3 bibtex-key-in-head)))))
1226 (if (not (assoc label labels))
1227 (setq labels
1228 (cons (list label) labels))))))
1229 (progn
1230 (setq
1231 bibtex-buffer-last-parsed-for-keys-tick
1232 (buffer-modified-tick))
1233 (if (not add)
1234 (message "Parsing reference keys... done"))
1235 (setq bibtex-keys labels)))))))
1236
1237 (defun bibtex-auto-fill-function ()
1238 (let ((fill-prefix (make-string (+ bibtex-text-alignment 1) ? )))
1239 (do-auto-fill)))
1240
1241
1242 \f
1243 ;; Interactive Functions:
1244
1245 ;;;###autoload
1246 (defun bibtex-mode ()
1247 "Major mode for editing BibTeX files.
1248
1249 \\{bibtex-mode-map}
1250
1251 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
1252
1253 The optional fields start with the string OPT, and thus ignored by BibTeX.
1254 The OPT string may be removed from a field with \\[bibtex-remove-OPT].
1255 \\[bibtex-kill-optional-field] kills the current optional field entirely.
1256 \\[bibtex-remove-double-quotes-or-braces] removes the double-quotes or
1257 braces around the text of the current field. \\[bibtex-empty-field]
1258 replaces the text of the current field with the default \"\" or {}.
1259
1260 The command \\[bibtex-clean-entry] cleans the current entry, i.e. (i) removes
1261 double-quotes or braces from entirely numerical fields, (ii) removes
1262 OPT from all non-empty optional fields, (iii) removes all empty
1263 optional fields, and (iv) checks that no non-optional fields are empty.
1264
1265 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
1266 Use \\[bibtex-next-field] to move to end of the next field.
1267
1268 The following may be of interest as well:
1269
1270 Functions:
1271 bibtex-entry
1272 bibtex-print-help-message
1273 bibtex-beginning-of-entry
1274 bibtex-end-of-entry
1275 bibtex-ispell-abstract
1276 bibtex-narrow-to-entry
1277 bibtex-hide-entry-bodies
1278 bibtex-sort-entries
1279 bibtex-validate-buffer
1280 bibtex-pop-previous
1281 bibtex-pop-next
1282 bibtex-complete-string
1283
1284 Variables:
1285 bibtex-field-left-delimiter
1286 bibtex-field-right-delimiter
1287 bibtex-include-OPTcrossref
1288 bibtex-include-OPTkey
1289 bibtex-include-OPTannote
1290 bibtex-mode-user-optional-fields
1291 bibtex-clean-entry-zap-empty-opts
1292 bibtex-sort-ignore-string-entries
1293 bibtex-maintain-sorted-entries
1294 bibtex-entry-field-alist
1295 bibtex-predefined-strings
1296 bibtex-string-files
1297
1298 ---------------------------------------------------------
1299 Entry to this mode calls the value of bibtex-mode-hook if that value is
1300 non-nil."
1301 (interactive)
1302 (kill-all-local-variables)
1303 (use-local-map bibtex-mode-map)
1304 (setq major-mode 'bibtex-mode)
1305 (setq mode-name "BibTeX")
1306 (set-syntax-table bibtex-mode-syntax-table)
1307 (setq bibtex-completion-candidates bibtex-predefined-strings)
1308 (mapcar
1309 (function
1310 (lambda (filename)
1311 ;; collect pathnames
1312 (let* ((bib (getenv "BIBINPUTS"))
1313 (path (if bib
1314 bib
1315 "."))
1316 (dirs
1317 (mapcar
1318 (function
1319 (lambda (dirname) ;; strips off trailing slashes
1320 (let ((len (length dirname)))
1321 (if (equal (elt dirname (1- len)) "/")
1322 (substring dirname 0 (1- (1- len)))
1323 dirname))))
1324 (let (actdirs)
1325 (while (string-match ":" path)
1326 (setq actdirs
1327 (append actdirs
1328 (list (substring
1329 path 0
1330 (1- (match-end 0)))))
1331 path (substring path (match-end 0))))
1332 (append actdirs (list path)))))
1333 (filename (if (string-match "\.bib$" filename)
1334 filename
1335 (concat filename ".bib")))
1336 fullfilename
1337 (item 0)
1338 (size (length dirs)))
1339 ;; test filenames
1340 (while (and
1341 (< item size)
1342 (not (file-readable-p
1343 (setq fullfilename
1344 (concat (elt dirs item) "/" filename)))))
1345 (setq item (1+ item)))
1346 (if (< item size)
1347 ;; file was found
1348 (let ((curbuf (current-buffer))
1349 (bufname (make-temp-name ""))
1350 (compl bibtex-completion-candidates))
1351 (create-file-buffer bufname)
1352 (set-buffer bufname)
1353 (insert-file-contents fullfilename)
1354 (goto-char (point-min))
1355 (while (re-search-forward bibtex-string nil t)
1356 (setq
1357 compl
1358 (append
1359 compl
1360 (list
1361 (list (buffer-substring-no-properties
1362 (match-beginning bibtex-key-in-string)
1363 (match-end bibtex-key-in-string)))))))
1364 (kill-buffer bufname)
1365 (set-buffer curbuf)
1366 (setq bibtex-completion-candidates compl))
1367 (error "File %s not in $BIBINPUTS paths" filename)))))
1368 bibtex-string-files)
1369 (add-hook
1370 'auto-save-hook
1371 (function
1372 (lambda ()
1373 (if (and
1374 bibtex-maintain-sorted-entries
1375 (eq major-mode 'bibtex-mode)
1376 (not
1377 (eq (buffer-modified-tick)
1378 bibtex-buffer-last-parsed-for-keys-tick)))
1379 (bibtex-parse-keys nil t)))))
1380 (bibtex-parse-keys nil)
1381 (make-local-variable 'paragraph-start)
1382 (setq paragraph-start "[ \f\n\t]*$")
1383 (make-local-variable 'comment-start)
1384 (setq comment-start "%")
1385 (auto-fill-mode 1)
1386 (setq auto-fill-function 'bibtex-auto-fill-function)
1387 (set (make-local-variable 'font-lock-defaults)
1388 '(bibtex-font-lock-keywords nil t ((?$ . "\""))))
1389 (run-hooks 'bibtex-mode-hook))
1390
1391 (defun bibtex-entry (entry-type &optional required optional)
1392 "Inserts a new BibTeX entry.
1393 Calls the value of bibtex-add-entry-hook if that value is non-nil."
1394 (interactive (let* ((completion-ignore-case t)
1395 (e-t (completing-read
1396 "Entry Type: "
1397 bibtex-entry-field-alist
1398 nil t)))
1399 (list e-t)))
1400 (if (and (null required) (null optional))
1401 (let* ((e (assoc-ignore-case entry-type bibtex-entry-field-alist))
1402 (r-n-o (elt e 1))
1403 (c-ref (elt e 2)))
1404 (if (null e)
1405 (error "Bibtex entry type %s not defined!" entry-type))
1406 (if (and
1407 (member entry-type bibtex-include-OPTcrossref)
1408 c-ref)
1409 (setq required (elt c-ref 0)
1410 optional (elt c-ref 1))
1411 (setq required (elt r-n-o 0)
1412 optional (elt r-n-o 1)))))
1413 (let ((key
1414 (if bibtex-maintain-sorted-entries
1415 (completing-read
1416 (format "%s key: " entry-type)
1417 bibtex-keys))))
1418 (if bibtex-maintain-sorted-entries
1419 (bibtex-find-entry-location key)
1420 (bibtex-move-outside-of-entry))
1421 (insert "@" entry-type "{")
1422 (if key
1423 (insert key))
1424 (save-excursion
1425 (mapcar 'bibtex-make-field required)
1426 (if (member entry-type bibtex-include-OPTcrossref)
1427 (bibtex-make-optional-field '("crossref")))
1428 (if bibtex-include-OPTkey
1429 (bibtex-make-optional-field '("key")))
1430 (mapcar 'bibtex-make-optional-field optional)
1431 (mapcar 'bibtex-make-optional-field
1432 bibtex-mode-user-optional-fields)
1433 (if bibtex-include-OPTannote
1434 (bibtex-make-optional-field '("annote")))
1435 (insert "\n}\n\n"))
1436 (bibtex-next-field t)
1437 (run-hooks 'bibtex-add-entry-hook)))
1438
1439 (defun bibtex-print-help-message ()
1440 "Prints helpful information about current field in current BibTeX entry."
1441 (interactive)
1442 (let* ((pnt (point))
1443 (field-name
1444 (progn
1445 (beginning-of-line)
1446 (condition-case errname
1447 (bibtex-enclosing-regexp bibtex-field)
1448 (search-failed
1449 (goto-char pnt)
1450 (error "Not on BibTeX field")))
1451 (let ((mb (match-beginning bibtex-name-in-field))
1452 (me (match-end bibtex-name-in-field)))
1453 (goto-char mb)
1454 (buffer-substring-no-properties
1455 (if (looking-at "OPT")
1456 (+ 3 mb)
1457 mb)
1458 me))))
1459 (reference-type
1460 (progn
1461 (re-search-backward
1462 bibtex-reference-maybe-empty-head nil t)
1463 (buffer-substring-no-properties
1464 (1+ (match-beginning bibtex-type-in-head))
1465 (match-end bibtex-type-in-head))))
1466 (entry-list
1467 (assoc-ignore-case reference-type
1468 bibtex-entry-field-alist))
1469 (c-r-list (elt entry-list 2))
1470 (req-opt-list
1471 (if (and
1472 (member reference-type bibtex-include-OPTcrossref)
1473 c-r-list)
1474 c-r-list
1475 (elt entry-list 1)))
1476 (list-of-entries (append
1477 (elt req-opt-list 0)
1478 (elt req-opt-list 1)
1479 bibtex-mode-user-optional-fields
1480 (if (member
1481 reference-type
1482 bibtex-include-OPTcrossref)
1483 '(("crossref"
1484 "Label of the crossreferenced entry")))
1485 (if bibtex-include-OPTannote
1486 '(("annote"
1487 "Personal annotation (ignored)")))
1488 (if bibtex-include-OPTkey
1489 '(("key"
1490 "Key used for label creation if author and editor fields are missing"))))))
1491 (goto-char pnt)
1492 (if (assoc field-name list-of-entries)
1493 (message (elt (assoc field-name list-of-entries) 1))
1494 (message "NO COMMENT AVAILABLE"))))
1495
1496 (defun bibtex-make-field (e-t)
1497 "Makes a field named E-T in current BibTeX entry."
1498 (interactive "sBibTeX field name: ")
1499 (let ((name (if (consp e-t)
1500 (elt e-t 0)
1501 e-t)))
1502 (if (interactive-p)
1503 (progn
1504 (bibtex-find-text nil)
1505 (if (looking-at "[}\"]")
1506 (forward-char 1))))
1507 (insert ",\n")
1508 (indent-to-column bibtex-name-alignment)
1509 (insert name " = ")
1510 (indent-to-column bibtex-text-alignment)
1511 (insert bibtex-field-left-delimiter bibtex-field-right-delimiter)
1512 (if (interactive-p)
1513 (forward-char -1))))
1514
1515 (defun bibtex-make-optional-field (e-t)
1516 "Makes an optional field named E-T in current BibTeX entry."
1517 (if (consp e-t)
1518 (setq e-t (cons (concat "OPT" (car e-t)) (cdr e-t)))
1519 (setq e-t (concat "OPT" e-t)))
1520 (bibtex-make-field e-t))
1521
1522 (defun bibtex-beginning-of-entry ()
1523 "Move to beginning of BibTeX entry.
1524 If inside an entry, move to the beginning of it, otherwise move to the
1525 beginning of the previous entry."
1526 (interactive)
1527 (if (looking-at "^@")
1528 (forward-char))
1529 (re-search-backward "^@" nil 'move))
1530
1531 (defun bibtex-end-of-entry ()
1532 "Move to end of BibTeX entry.
1533 If inside an entry, move to the end of it, otherwise move to the end
1534 of the previous entry."
1535 (interactive)
1536 (bibtex-beginning-of-entry)
1537 (let ((parse-sexp-ignore-comments t))
1538 (forward-sexp 2) ;; skip entry type and body
1539 ))
1540
1541 (defun bibtex-ispell-entry ()
1542 "Spell whole BibTeX entry."
1543 (interactive)
1544 (ispell-region (progn (bibtex-beginning-of-entry) (point))
1545 (progn (bibtex-end-of-entry) (point))))
1546
1547 (defun bibtex-ispell-abstract ()
1548 "Spell abstract of BibTeX entry."
1549 (interactive)
1550 (let ((pnt (bibtex-end-of-entry)))
1551 (bibtex-beginning-of-entry)
1552 (if (null
1553 (re-search-forward "^[ \t]*[OPT]*abstract[ \t]*=" pnt))
1554 (error "No abstract in entry.")))
1555 (ispell-region (point)
1556 (save-excursion (forward-sexp) (point))))
1557
1558 (defun bibtex-narrow-to-entry ()
1559 "Narrow buffer to current BibTeX entry."
1560 (interactive)
1561 (save-excursion
1562 (narrow-to-region (progn (bibtex-beginning-of-entry) (point))
1563 (progn (bibtex-end-of-entry) (point)))))
1564
1565
1566 (defun bibtex-hide-entry-bodies (&optional arg)
1567 "Hide all lines between first and last BibTeX entries not beginning with @.
1568 With argument, show all text."
1569 (interactive "P")
1570 (save-excursion
1571 (beginning-of-first-bibtex-entry)
1572 ;; subst-char-in-region modifies the buffer, despite what the
1573 ;; documentation says...
1574 (let ((modifiedp (buffer-modified-p))
1575 (buffer-read-only nil))
1576 (if arg
1577 (subst-char-in-region (point) (point-max) ?\r ?\n t)
1578 (while (save-excursion (re-search-forward "\n[^@]" (point-max) t))
1579 ;; (save-excursion (replace-regexp "\n\\([^@]\\)" "\r\\1"))
1580 (save-excursion
1581 (while (re-search-forward "\n\\([^@]\\)" nil t)
1582 (replace-match "\r\\1" nil nil)))))
1583 (setq selective-display (not arg))
1584 (set-buffer-modified-p modifiedp))))
1585
1586 (defun bibtex-sort-entries ()
1587 "Sort BibTeX entries alphabetically by key.
1588 Text outside of BibTeX entries is not affected. If
1589 bibtex-sort-ignore-string-entries is non-nil, @string entries will be
1590 ignored."
1591 (interactive)
1592 (save-restriction
1593 (beginning-of-first-bibtex-entry)
1594 (narrow-to-region
1595 (point)
1596 (save-excursion
1597 (goto-char (point-max))
1598 (bibtex-end-of-entry)
1599 (point)))
1600 (if bibtex-sort-ignore-string-entries
1601 (if (re-search-forward bibtex-reference nil 'move)
1602 (goto-char (match-beginning 0))))
1603 (sort-subr
1604 nil
1605 ;; NEXTREC function
1606 (function
1607 (lambda ()
1608 (if bibtex-sort-ignore-string-entries
1609 (if (re-search-forward bibtex-reference nil 'move)
1610 (goto-char (match-beginning 0)))
1611 (if (re-search-forward bibtex-reference-head nil 'move)
1612 (goto-char (match-beginning 0))))))
1613 ;; ENDREC function
1614 'bibtex-end-of-entry
1615 ;; STARTKEY function
1616 (function
1617 (lambda ()
1618 (if bibtex-sort-ignore-string-entries
1619 (progn
1620 (re-search-forward bibtex-reference)
1621 (buffer-substring-no-properties
1622 (match-beginning bibtex-key-in-reference)
1623 (match-end bibtex-key-in-reference)))
1624 (re-search-forward bibtex-reference-head)
1625 (buffer-substring-no-properties
1626 (match-beginning bibtex-key-in-head)
1627 (match-end bibtex-key-in-head)))))
1628 ;; ENDKEY function
1629 nil)))
1630
1631 (defun bibtex-find-entry-location (entry-name &optional ignore-dups)
1632 "Looking for place to put the BibTeX entry named ENTRY-NAME.
1633 Performs a binary search (therefore, buffer is assumed to be in sorted
1634 order, without duplicates (see \\[bibtex-validate-buffer]), if it is
1635 not, bibtex-find-entry-location will fail). If entry-name is already
1636 used as a reference key, an error is signaled. However, if optional
1637 variable IGNORE-DUPS is non-nil, no error messages about duplicate
1638 entries are signaled, but the error handling is assumed to be made in
1639 the calling function. Nil is returned, if an duplicate entry error
1640 occurred, and t in all other cases."
1641 (let* ((left
1642 (progn
1643 (beginning-of-first-bibtex-entry)
1644 (if bibtex-sort-ignore-string-entries
1645 (re-search-forward bibtex-reference nil `move)
1646 (bibtex-end-of-entry))
1647 (point)))
1648 (right
1649 (progn
1650 (goto-char (point-max))
1651 (if bibtex-sort-ignore-string-entries
1652 (re-search-backward bibtex-reference nil `move)
1653 (bibtex-beginning-of-entry))
1654 (point)))
1655 actual-point
1656 actual-key
1657 (done (>= left right))
1658 new
1659 dup)
1660 (while (not done)
1661 (setq actual-point (/ (+ left right) 2))
1662 (goto-char actual-point)
1663 (bibtex-beginning-of-entry)
1664 (setq actual-key
1665 (if bibtex-sort-ignore-string-entries
1666 (progn
1667 (re-search-forward bibtex-reference)
1668 (buffer-substring-no-properties
1669 (match-beginning bibtex-key-in-reference)
1670 (match-end bibtex-key-in-reference)))
1671 (re-search-forward bibtex-reference-head)
1672 (buffer-substring-no-properties
1673 (match-beginning bibtex-key-in-head)
1674 (match-end bibtex-key-in-head))))
1675 (cond
1676 ((string-lessp entry-name actual-key)
1677 (setq new (match-beginning 0))
1678 (if (equal right new)
1679 (setq done t)
1680 (setq right new)))
1681 ((string-lessp actual-key entry-name)
1682 (setq new (match-end 0))
1683 (if (equal left new)
1684 (setq done t)
1685 (setq left new)))
1686 ((string-equal actual-key entry-name)
1687 (setq dup t
1688 done t)
1689 (if (not ignore-dups)
1690 (error "Entry with key `%s' already exists!" entry-name)))))
1691 (if dup
1692 nil
1693 (goto-char right)
1694 (if (re-search-forward bibtex-reference nil t)
1695 (progn
1696 (setq actual-key
1697 (buffer-substring-no-properties
1698 (match-beginning bibtex-key-in-reference)
1699 (match-end bibtex-key-in-reference)))
1700 (if (string-lessp actual-key entry-name)
1701 ;; even greater than last entry --> we must append
1702 (progn
1703 (goto-char (match-end 0))
1704 (newline (forward-line 2))
1705 (beginning-of-line))
1706 (goto-char right))))
1707 t)))
1708
1709 (defun bibtex-validate-buffer ()
1710 "Validate if the current BibTeX buffer is syntactically correct.
1711 Any garbage (e.g. comments) before the first \"@\" is not tested (so
1712 you can put comments here)."
1713 (interactive)
1714 (let ((pnt (point))
1715 (max (point-max)))
1716 ;; looking if entries fit syntactical structure
1717 (goto-char (point-min))
1718 (while (< (re-search-forward "@\\|\\'") max)
1719 (forward-char -1)
1720 (let ((p (point)))
1721 (if (or
1722 (looking-at "@string")
1723 (looking-at "@preamble"))
1724 (forward-char)
1725 (if (not (and
1726 (re-search-forward bibtex-reference nil t)
1727 (equal p (match-beginning 0))))
1728 (progn
1729 (goto-char p)
1730 (error "Bad entry begins here"))))))
1731 ;; looking if entries are balanced (a single non-escaped quote
1732 ;; inside braces is not detected by the former check, but
1733 ;; bibtex-sort-entries stumbles about it
1734 (goto-char (point-min))
1735 (map-bibtex-entries
1736 (function
1737 (lambda (current)
1738 (bibtex-beginning-of-entry)
1739 (forward-sexp 2))))
1740 ;; looking for correct sort order and duplicates
1741 (if bibtex-maintain-sorted-entries
1742 (let ((entry-name (make-string 10 255))
1743 (previous nil)
1744 point)
1745 (beginning-of-first-bibtex-entry)
1746 (map-bibtex-entries
1747 (function
1748 (lambda (current)
1749 (cond ((or (null previous)
1750 (string< previous current))
1751 (setq previous current
1752 point (point)))
1753 ((string-equal previous current)
1754 (error "Duplicate here with previous!"))
1755 (t
1756 (error "Entries out of order here!"))))))))
1757 (goto-char pnt)
1758 (message "BibTeX buffer is syntactically correct")))
1759
1760 (defun bibtex-next-field (arg)
1761 "Finds end of text of next BibTeX field; with arg, to its beginning."
1762 (interactive "P")
1763 (bibtex-inside-field)
1764 (let ((start (point)))
1765 (condition-case ()
1766 (progn
1767 (bibtex-enclosing-field)
1768 (goto-char (match-end 0))
1769 (forward-char 2))
1770 (error
1771 (goto-char start)
1772 (end-of-line)
1773 (forward-char 1))))
1774 (bibtex-find-text arg))
1775
1776 (defun bibtex-find-text (arg)
1777 "Go to end of text of current field; with arg, go to beginning."
1778 (interactive "P")
1779 (bibtex-inside-field)
1780 (bibtex-enclosing-field)
1781 (if arg
1782 (progn
1783 (goto-char (match-beginning bibtex-text-in-field))
1784 (if (looking-at "[{\"]")
1785 (forward-char 1)))
1786 (goto-char (match-end bibtex-text-in-field))
1787 (if (or
1788 (= (preceding-char) ?})
1789 (= (preceding-char) ?\"))
1790 (forward-char -1)))
1791 (if bibtex-help-message
1792 (bibtex-print-help-message)))
1793
1794 (defun bibtex-remove-OPT ()
1795 "Removes the 'OPT' starting optional arguments and goes to end of text."
1796 (interactive)
1797 (bibtex-inside-field)
1798 (bibtex-enclosing-field)
1799 (save-excursion
1800 (goto-char (match-beginning bibtex-name-in-field))
1801 (if (looking-at "OPT")
1802 ;; sct@dcs.edinburgh.ac.uk
1803 (progn
1804 (delete-char (length "OPT"))
1805 (search-forward "=")
1806 (delete-horizontal-space)
1807 (indent-to-column bibtex-text-alignment))))
1808 (bibtex-inside-field))
1809
1810 (defun bibtex-remove-double-quotes-or-braces ()
1811 "Removes \"\" or {} around string."
1812 (interactive)
1813 (save-excursion
1814 (bibtex-inside-field)
1815 (bibtex-enclosing-field)
1816 (let ((start (match-beginning bibtex-text-in-field))
1817 (stop (match-end bibtex-text-in-field)))
1818 (goto-char stop)
1819 (forward-char -1)
1820 (if (looking-at "[}\"]")
1821 (delete-char 1))
1822 (goto-char start)
1823 (if (looking-at "[{\"]")
1824 (delete-char 1)))))
1825
1826 (defun bibtex-kill-optional-field ()
1827 "Kill the entire enclosing optional BibTeX field."
1828 (interactive)
1829 (bibtex-inside-field)
1830 (bibtex-enclosing-field)
1831 (goto-char (match-beginning bibtex-name-in-field))
1832 (let ((the-end (match-end 0))
1833 (the-beginning (match-beginning 0)))
1834 (if (looking-at "OPT")
1835 (progn
1836 (goto-char the-end)
1837 (skip-chars-forward " \t\n,")
1838 (kill-region the-beginning the-end))
1839 (error "Mandatory fields can't be killed"))))
1840
1841 (defun bibtex-empty-field ()
1842 "Delete the text part of the current field, replace with empty text."
1843 (interactive)
1844 (bibtex-inside-field)
1845 (bibtex-enclosing-field)
1846 (goto-char (match-beginning bibtex-text-in-field))
1847 (kill-region (point) (match-end bibtex-text-in-field))
1848 (insert (concat bibtex-field-left-delimiter
1849 bibtex-field-right-delimiter))
1850 (bibtex-find-text t))
1851
1852 (defun bibtex-pop-previous (arg)
1853 "Replace text of current field with the text of similar field in previous entry.
1854 With arg, go up ARG entries. Repeated, goes up so many times. May be
1855 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
1856 (interactive "p")
1857 (bibtex-inside-field)
1858 (save-excursion
1859 ; parse current field
1860 (bibtex-enclosing-field)
1861 (let ((start-old-text (match-beginning bibtex-text-in-field))
1862 (stop-old-text (match-end bibtex-text-in-field))
1863 (start-name (match-beginning bibtex-name-in-field))
1864 (stop-name (match-end bibtex-name-in-field))
1865 (new-text))
1866 (goto-char start-name)
1867 ; construct regexp for previous field with same name as this one
1868 (let ((matching-entry
1869 (bibtex-cfield
1870 (buffer-substring-no-properties (if (looking-at "OPT")
1871 (+ (point) (length "OPT"))
1872 (point))
1873 stop-name)
1874 bibtex-field-text)))
1875 ; if executed several times in a row, start each search where the
1876 ; last one finished
1877 (cond ((or (eq last-command 'bibtex-pop-previous)
1878 (eq last-command 'bibtex-pop-next))
1879 t
1880 )
1881 (t
1882 (bibtex-enclosing-reference-maybe-empty-head)
1883 (setq bibtex-pop-previous-search-point (point))
1884 (setq bibtex-pop-next-search-point (match-end 0))))
1885 (goto-char bibtex-pop-previous-search-point)
1886 ; Now search for arg'th previous similar field
1887 (cond
1888 ((re-search-backward matching-entry (point-min) t arg)
1889 (setq new-text
1890 (buffer-substring-no-properties
1891 (match-beginning bibtex-text-in-cfield)
1892 (match-end bibtex-text-in-cfield)))
1893 ;; change delimiters, if any changes needed
1894 (cond
1895 ((and
1896 (equal bibtex-field-left-delimiter "{")
1897 (eq (aref new-text 0) ?\")
1898 (eq (aref new-text (1- (length new-text))) ?\"))
1899 (aset new-text 0 ?\{)
1900 (aset new-text (1- (length new-text)) ?\}))
1901 ((and
1902 (equal bibtex-field-left-delimiter "\"")
1903 (eq (aref new-text 0) ?\{)
1904 (eq (aref new-text (1- (length new-text))) ?\}))
1905 (aset new-text 0 ?\")
1906 (aset new-text (1- (length new-text)) ?\"))
1907 ((or
1908 (not (eq (aref new-text 0)
1909 (aref bibtex-field-left-delimiter 0)))
1910 (not (eq (aref new-text (1- (length new-text)))
1911 (aref bibtex-field-right-delimiter 0))))
1912 (setq new-text (concat bibtex-field-left-delimiter
1913 new-text
1914 bibtex-field-right-delimiter))))
1915 ; Found a matching field. Remember boundaries.
1916 (setq bibtex-pop-next-search-point (match-end 0))
1917 (setq bibtex-pop-previous-search-point (match-beginning 0))
1918 (bibtex-flash-head)
1919 ; Go back to where we started, delete old text, and pop new.
1920 (goto-char stop-old-text)
1921 (delete-region start-old-text stop-old-text)
1922 (insert new-text))
1923 (t ; search failed
1924 (error "No previous matching BibTeX field."))))))
1925 (setq this-command 'bibtex-pop-previous))
1926
1927 (defun bibtex-pop-next (arg)
1928 "Replace text of current field with the text of similar field in next entry.
1929 With arg, go up ARG entries. Repeated, goes up so many times. May be
1930 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
1931 (interactive "p")
1932 (bibtex-inside-field)
1933 (save-excursion
1934 ; parse current field
1935 (bibtex-enclosing-field)
1936 (let ((start-old-text (match-beginning bibtex-text-in-field))
1937 (stop-old-text (match-end bibtex-text-in-field))
1938 (start-name (match-beginning bibtex-name-in-field))
1939 (stop-name (match-end bibtex-name-in-field))
1940 (new-text))
1941 (goto-char start-name)
1942 ; construct regexp for next field with same name as this one,
1943 ; ignoring possible OPT's
1944 (let ((matching-entry
1945 (bibtex-cfield
1946 (buffer-substring-no-properties (if (looking-at "OPT")
1947 (+ (point) (length "OPT"))
1948 (point))
1949 stop-name)
1950 bibtex-field-text)))
1951
1952 ; if executed several times in a row, start each search where the
1953 ; last one finished
1954 (cond ((or (eq last-command 'bibtex-pop-next)
1955 (eq last-command 'bibtex-pop-previous))
1956 t
1957 )
1958 (t
1959 (bibtex-enclosing-reference-maybe-empty-head)
1960 (setq bibtex-pop-previous-search-point (point))
1961 (setq bibtex-pop-next-search-point (match-end 0))))
1962 (goto-char bibtex-pop-next-search-point)
1963
1964 ; Now search for arg'th next similar field
1965 (cond
1966 ((re-search-forward matching-entry (point-max) t arg)
1967 (setq new-text
1968 (buffer-substring-no-properties
1969 (match-beginning bibtex-text-in-cfield)
1970 (match-end bibtex-text-in-cfield)))
1971 ;; change delimiters, if any changes needed
1972 (cond
1973 ((and
1974 (equal bibtex-field-left-delimiter "{")
1975 (eq (aref new-text 0) ?\")
1976 (eq (aref new-text (1- (length new-text))) ?\"))
1977 (aset new-text 0 ?\{)
1978 (aset new-text (1- (length new-text)) ?\}))
1979 ((and
1980 (equal bibtex-field-left-delimiter "\"")
1981 (eq (aref new-text 0) ?\{)
1982 (eq (aref new-text (1- (length new-text))) ?\}))
1983 (aset new-text 0 ?\")
1984 (aset new-text (1- (length new-text)) ?\"))
1985 ((or
1986 (not (eq (aref new-text 0)
1987 (aref bibtex-field-left-delimiter 0)))
1988 (not (eq (aref new-text (1- (length new-text)))
1989 (aref bibtex-field-right-delimiter 0))))
1990 (setq new-text (concat bibtex-field-left-delimiter
1991 new-text
1992 bibtex-field-right-delimiter))))
1993 ; Found a matching field. Remember boundaries.
1994 (setq bibtex-pop-next-search-point (match-end 0))
1995 (setq bibtex-pop-previous-search-point (match-beginning 0))
1996 (bibtex-flash-head)
1997 ; Go back to where we started, delete old text, and pop new.
1998 (goto-char stop-old-text)
1999 (delete-region start-old-text stop-old-text)
2000 (insert new-text))
2001 (t ; search failed
2002 (error "No next matching BibTeX field."))))))
2003 (setq this-command 'bibtex-pop-next))
2004
2005 (defun bibtex-clean-entry (&optional arg)
2006 "Finish editing the current BibTeX entry and clean it up.
2007 For all optional fields of current BibTeX entry: if empty, kill the
2008 whole field; otherwise, remove the \"OPT\" string in the name; if text
2009 numerical, remove double-quotes. For all mandatory fields: if empty,
2010 signal error. If label of entry is empty or a prefix argument was
2011 given, calculate a new entry label."
2012 (interactive "P")
2013 (bibtex-beginning-of-entry)
2014 (let ((start (point))
2015 crossref-there)
2016 (save-restriction
2017 (narrow-to-region start (save-excursion (bibtex-end-of-entry) (point)))
2018 (while (and
2019 (re-search-forward bibtex-field (point-max) t 1)
2020 (not crossref-there))
2021 ;; determine if reference has crossref entry
2022 (let ((begin-name (match-beginning bibtex-name-in-field))
2023 (begin-text (match-beginning bibtex-text-in-field)))
2024 (goto-char begin-name)
2025 (if (looking-at "\\(OPTcrossref\\)\\|\\(crossref\\)")
2026 (progn
2027 (goto-char begin-text)
2028 (if (not (looking-at
2029 (concat
2030 bibtex-field-left-delimiter
2031 bibtex-field-right-delimiter)))
2032 (setq crossref-there t))))))
2033 (bibtex-enclosing-reference-maybe-empty-head)
2034 (re-search-forward bibtex-reference-type)
2035 (let ((begin-type (1+ (match-beginning 0)))
2036 (end-type (match-end 0)))
2037 (goto-char start)
2038 (while (re-search-forward bibtex-field (point-max) t 1)
2039 (let ((begin-field (match-beginning 0))
2040 (end-field (match-end 0))
2041 (begin-name (match-beginning bibtex-name-in-field))
2042 (end-name (match-end bibtex-name-in-field))
2043 (begin-text (match-beginning bibtex-text-in-field))
2044 (end-text (match-end bibtex-text-in-field))
2045 )
2046 (goto-char begin-name)
2047 (cond ((and
2048 (looking-at "OPT")
2049 bibtex-clean-entry-zap-empty-opts)
2050 (goto-char begin-text)
2051 (if (looking-at
2052 (concat
2053 bibtex-field-left-delimiter
2054 bibtex-field-right-delimiter))
2055 ;; empty: delete whole field if really optional
2056 ;; (missing crossref handled) or complain
2057 (if (and
2058 (not crossref-there)
2059 (assoc
2060 (downcase
2061 (buffer-substring-no-properties
2062 (+ (length "OPT") begin-name) end-name))
2063 (car (car (cdr
2064 (assoc-ignore-case
2065 (buffer-substring-no-properties
2066 begin-type end-type)
2067 bibtex-entry-field-alist))))))
2068 ;; field is not really optional
2069 (progn
2070 (goto-char begin-name)
2071 (delete-char (length "OPT"))
2072 ;; make field non-OPT
2073 (search-forward "=")
2074 (delete-horizontal-space)
2075 (indent-to-column bibtex-text-alignment)
2076 (forward-char)
2077 ;; and loop to go through next test
2078 (error "Mandatory field ``%s'' is empty"
2079 (buffer-substring-no-properties
2080 begin-name
2081 end-name)))
2082 ;; field is optional
2083 (delete-region begin-field end-field))
2084 ;; otherwise: not empty, delete "OPT"
2085 (goto-char begin-name)
2086 (delete-char (length "OPT"))
2087 (progn
2088 ;; fixup alignment. [alarson:19920309.2047CST]
2089 (search-forward "=")
2090 (delete-horizontal-space)
2091 (indent-to-column bibtex-text-alignment))
2092 (goto-char begin-field) ; and loop to go through next test
2093 ))
2094 (t
2095 (goto-char begin-text)
2096 (cond ((looking-at (concat
2097 bibtex-field-left-delimiter
2098 "[0-9]+"
2099 bibtex-field-right-delimiter))
2100 ;; if numerical,
2101 (goto-char end-text)
2102 (delete-char -1) ; delete enclosing double-quotes
2103 (goto-char begin-text)
2104 (delete-char 1)
2105 (goto-char end-field) ; go to end for next search
2106 (forward-char -2) ; to compensate for the 2 quotes deleted
2107 )
2108 ((looking-at (concat
2109 bibtex-field-left-delimiter
2110 bibtex-field-right-delimiter))
2111 ;; if empty quotes, complain
2112 (forward-char 1)
2113 (if (not (or (equal (buffer-substring-no-properties
2114 begin-name
2115 (+ begin-name 3))
2116 "OPT")
2117 (equal (buffer-substring-no-properties
2118 begin-name
2119 (+ begin-name 3))
2120 "opt")))
2121 (error "Mandatory field ``%s'' is empty"
2122 (buffer-substring-no-properties
2123 begin-name end-name))))
2124 (t
2125 (goto-char end-field)))))))))
2126 (goto-char start)
2127 (bibtex-end-of-entry))
2128 (let* ((eob (progn
2129 (bibtex-end-of-entry)
2130 (point)))
2131 (key (progn
2132 (bibtex-beginning-of-entry)
2133 (if (re-search-forward
2134 bibtex-reference-head eob t)
2135 (buffer-substring-no-properties
2136 (match-beginning bibtex-key-in-head)
2137 (match-end bibtex-key-in-head))))))
2138 (if (or
2139 arg
2140 (not key))
2141 (progn
2142 (let ((autokey
2143 (if bibtex-autokey-edit-before-use
2144 (read-from-minibuffer "Key to use: "
2145 (bibtex-generate-autokey))
2146 (bibtex-generate-autokey))))
2147 (bibtex-beginning-of-entry)
2148 (re-search-forward bibtex-reference-maybe-empty-head)
2149 (if (match-beginning bibtex-key-in-head)
2150 (delete-region (match-beginning bibtex-key-in-head)
2151 (match-end bibtex-key-in-head)))
2152 (insert autokey)
2153 (let ((start (progn
2154 (bibtex-beginning-of-entry)
2155 (point)))
2156 (end (progn
2157 (bibtex-end-of-entry)
2158 (re-search-forward "^@" nil 'move)
2159 (beginning-of-line)
2160 (point)))
2161 last-command)
2162 (kill-region start end)
2163 (let ((success
2164 (or
2165 (not bibtex-maintain-sorted-entries)
2166 (bibtex-find-entry-location autokey t))))
2167 (yank)
2168 (setq kill-ring (cdr kill-ring))
2169 (forward-char -1)
2170 (bibtex-beginning-of-entry)
2171 (re-search-forward bibtex-reference-head)
2172 (if (not success)
2173 (error
2174 "New inserted reference may be a duplicate."))))))))
2175 (save-excursion
2176 (let ((start (progn (bibtex-beginning-of-entry) (point)))
2177 (end (progn (bibtex-end-of-entry) (point))))
2178 (save-restriction
2179 (narrow-to-region start end)
2180 (bibtex-parse-keys t)))))
2181
2182 (defun bibtex-complete-string ()
2183 "Complete word fragment before point to longest prefix of a defined string.
2184 If point is not after the part of a word, all strings are listed."
2185 (interactive "*")
2186 (let* ((end (point))
2187 (beg (save-excursion
2188 (re-search-backward "[ \t{\"]")
2189 (forward-char 1)
2190 (point)))
2191 (part-of-word (buffer-substring-no-properties beg end))
2192 (string-list (copy-sequence bibtex-completion-candidates))
2193 (case-fold-search t)
2194 (completion (save-excursion
2195 (progn
2196 (while (re-search-backward
2197 bibtex-string (point-min) t)
2198 (setq string-list
2199 (cons
2200 (list
2201 (buffer-substring-no-properties
2202 (match-beginning bibtex-key-in-string)
2203 (match-end bibtex-key-in-string)))
2204 string-list)))
2205 (setq string-list
2206 (sort string-list
2207 (lambda(x y)
2208 (string-lessp
2209 (car x)
2210 (car y)))))
2211 (try-completion part-of-word string-list)))))
2212 (cond ((eq completion t)
2213 (bibtex-remove-double-quotes-or-braces))
2214 ((null completion)
2215 (error "Can't find completion for \"%s\"" part-of-word))
2216 ((not (string= part-of-word completion))
2217 (delete-region beg end)
2218 (insert completion)
2219 (if (assoc completion string-list)
2220 (bibtex-remove-double-quotes-or-braces)))
2221 (t
2222 (message "Making completion list...")
2223 (let ((list (all-completions part-of-word string-list)))
2224 (with-output-to-temp-buffer "*Completions*"
2225 (display-completion-list list)))
2226 (message "Making completion list...done")))))
2227
2228 (defun bibtex-Article ()
2229 (interactive)
2230 (bibtex-entry "Article"))
2231
2232 (defun bibtex-Book ()
2233 (interactive)
2234 (bibtex-entry "Book"))
2235
2236 (defun bibtex-Booklet ()
2237 (interactive)
2238 (bibtex-entry "Booklet"))
2239
2240 (defun bibtex-InBook ()
2241 (interactive)
2242 (bibtex-entry "InBook"))
2243
2244 (defun bibtex-InCollection ()
2245 (interactive)
2246 (bibtex-entry "InCollection"))
2247
2248 (defun bibtex-InProceedings ()
2249 (interactive)
2250 (bibtex-entry "InProceedings"))
2251
2252 (defun bibtex-Manual ()
2253 (interactive)
2254 (bibtex-entry "Manual"))
2255
2256 (defun bibtex-MastersThesis ()
2257 (interactive)
2258 (bibtex-entry "MastersThesis"))
2259
2260 (defun bibtex-Misc ()
2261 (interactive)
2262 (bibtex-entry "Misc"))
2263
2264 (defun bibtex-PhdThesis ()
2265 (interactive)
2266 (bibtex-entry "PhdThesis"))
2267
2268 (defun bibtex-Proceedings ()
2269 (interactive)
2270 (bibtex-entry "Proceedings"))
2271
2272 (defun bibtex-TechReport ()
2273 (interactive)
2274 (bibtex-entry "TechReport"))
2275
2276 (defun bibtex-Unpublished ()
2277 (interactive)
2278 (bibtex-entry "Unpublished"))
2279
2280 (defun bibtex-string ()
2281 (interactive)
2282 (bibtex-move-outside-of-entry)
2283 (insert
2284 (concat
2285 "@string{ = "
2286 bibtex-field-left-delimiter
2287 bibtex-field-right-delimiter
2288 "}\n"))
2289 (forward-line -1)
2290 (forward-char 8))
2291
2292 (defun bibtex-preamble ()
2293 (interactive)
2294 (bibtex-move-outside-of-entry)
2295 (insert "@Preamble{}\n")
2296 (forward-line -1)
2297 (forward-char 10))
2298
2299
2300 \f
2301 ;; Make BibTeX a Feature
2302
2303 (provide 'bibtex)
2304
2305
2306 ;;; bibtex.el ends here