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