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