(shadow-noquery): Use `other' widget type.
[bpt/emacs.git] / lisp / textmodes / bibtex.el
CommitLineData
c0274f38
ER
1;;; bibtex.el --- BibTeX mode for GNU Emacs
2
12ef05f4 3;; Copyright (C) 1992, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
9750e079 4
31bc4210 5;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de>
ab2d0cdb 6;; Bengt Martensson <bengt@mathematik.uni-Bremen.de>
f961a17c
ER
7;; Mark Shapiro <shapiro@corto.inria.fr>
8;; Mike Newton <newton@gumby.cs.caltech.edu>
9;; Aaron Larson <alarson@src.honeywell.com>
2231645f 10;; Maintainer: Dirk Herrmann <D.Herrmann@tu-bs.de>
cb4ad359 11;; Keywords: BibTeX, LaTeX, TeX
f961a17c 12
745bc783
JB
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
a1ddedc6 17;; the Free Software Foundation; either version 2, or (at your option)
745bc783
JB
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
b578f267
EN
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.
745bc783 29
5c69dbfc 30;;; Commentary:
b578f267 31
cb4ad359 32;; Major mode for editing and validating BibTeX files.
e5167999 33
5c69dbfc 34;; Usage:
cb4ad359 35;; See documentation for function bibtex-mode (or type "\M-x describe-mode"
d0388eac 36;; when you are in BibTeX mode).
e5167999 37
5c69dbfc
RS
38;; Todo:
39;; Distribute texinfo file.
9ae11a89 40
5c69dbfc 41;;; Code:
b578f267 42
50e4b39e
RS
43(eval-when-compile
44 (require 'compile))
45
5c69dbfc 46;; User Options:
e5167999 47
f754fb7b
RS
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
ab2d0cdb 56 :prefix "bibtex-autokey-")
f754fb7b
RS
57
58(defcustom bibtex-mode-hook nil
59 "List of functions to call on entry to BibTeX mode."
60 :group 'bibtex
ab2d0cdb 61 :type 'hook)
f754fb7b
RS
62
63(defcustom bibtex-field-delimiters 'braces
50e4b39e 64 "*Controls type of field delimiters used.
d0388eac
RS
65Set this to `braces' or `double-quotes' according to your personal
66preferences. This variable is buffer-local."
f754fb7b
RS
67 :group 'bibtex
68 :type '(choice (const braces)
69 (const double-quotes)))
50e4b39e
RS
70(make-variable-buffer-local 'bibtex-field-delimiters)
71
f754fb7b 72(defcustom bibtex-entry-delimiters 'braces
50e4b39e 73 "*Controls type of entry delimiters used.
d0388eac
RS
74Set this to `braces' or `parentheses' according to your personal
75preferences. This variable is buffer-local."
f754fb7b
RS
76 :group 'bibtex
77 :type '(choice (const braces)
78 (const parentheses)))
50e4b39e 79(make-variable-buffer-local 'bibtex-entry-delimiters)
cb4ad359 80
f754fb7b
RS
81(defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
82 "*All entries listed here will have an OPTcrossref field."
83 :group 'bibtex
84 :type '(repeat string))
e5167999 85
f754fb7b 86(defcustom bibtex-include-OPTkey t
50e4b39e
RS
87 "*If non-nil, all entries will have an OPTkey field.
88If this is a string, it will be used as the initial field text.
f754fb7b
RS
89If 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 (const :tag "Default" t)
93 (string :tag "Initial text")
ab2d0cdb 94 (function :tag "Initialize Function" :value fun)))
f754fb7b
RS
95
96(defcustom bibtex-user-optional-fields
50e4b39e 97 '(("annote" "Personal annotation (ignored)"))
cb4ad359 98 "*List of optional fields the user wants to have always present.
50e4b39e 99Entries should be of the same form as the OPTIONAL and
d0388eac 100CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (see documentation
f754fb7b
RS
101of this variable for details)."
102 :group 'bibtex
103 :type '(repeat
ab2d0cdb
RS
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))))))
50e4b39e 111
f754fb7b 112(defcustom bibtex-entry-format '(opts-or-alts numerical-fields)
d0388eac 113 "*Controls type of formatting performed by `bibtex-clean-entry'.
f0cb6034 114It may be t, nil, or a list of symbols out of the following:
d0388eac
RS
115opts-or-alts Delete empty optional and alternative fields and
116 remove OPT and ALT prefixes from used fields.
117numerical-fields Delete delimiters around numeral fields.
118page-dashes Change double dashes in page field to single dash
119 (for scribe compatibility).
120inherit-booktitle If entry contains a crossref field and booktitle
50e4b39e 121 field is empty, it is set to the contents of the
d0388eac
RS
122 title field of the crossreferenced entry.
123 Caution: this will work only if buffer is
124 correctly sorted.
125realign Realign entries, so that field texts and perhaps equal
50e4b39e 126 signs (depending on the value of
f0cb6034 127 `bibtex-align-at-equal-sign') begin in the same column.
d0388eac
RS
128last-comma Add or delete comma on end of last field in entry,
129 according to value of `bibtex-comma-after-last-field'.
130delimiters Change delimiters according to variables
131 `bibtex-field-delimiters' and `bibtex-entry-delimiters'.
132unify-case Change case of entry and field names.
133
134The value t means do all of the above formatting actions.
135The value nil means do no formatting at all."
f754fb7b
RS
136 :group 'bibtex
137 :type '(choice (const :tag "None" nil)
138 (const :tag "All" t)
ab2d0cdb
RS
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))))
50e4b39e 148
f754fb7b 149(defcustom bibtex-clean-entry-hook nil
50e4b39e 150 "*List of functions to call when entry has been cleaned.
d0388eac 151Functions are called with point inside the cleaned entry, and the buffer
f754fb7b
RS
152narrowed to just the entry."
153 :group 'bibtex
ab2d0cdb 154 :type 'hook)
cb4ad359 155
f754fb7b 156(defcustom bibtex-sort-ignore-string-entries t
50e4b39e 157 "*If non-nil, BibTeX @String entries are not sort-significant.
cb4ad359 158That means they are ignored when determining ordering of the buffer
f0cb6034 159(e.g., sorting, locating alphabetical position for new entries, etc.).
d0388eac 160This variable is buffer-local."
f754fb7b
RS
161 :group 'bibtex
162 :type 'boolean)
0640d7bf 163(make-variable-buffer-local 'bibtex-sort-ignore-string-entries)
e5167999 164
f754fb7b 165(defcustom bibtex-maintain-sorted-entries nil
d0388eac 166 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order.
f0cb6034 167Setting this variable to nil will strip off some comfort (e.g., TAB
0640d7bf 168completion for reference keys in minibuffer, automatic detection of
d0388eac
RS
169duplicates) from BibTeX mode. See also `bibtex-sort-ignore-string-entries'.
170This variable is buffer-local."
f754fb7b
RS
171 :group 'bibtex
172 :type 'boolean)
0640d7bf 173(make-variable-buffer-local 'bibtex-maintain-sorted-entries)
9ae11a89 174
f754fb7b 175(defcustom bibtex-field-kill-ring-max 20
d0388eac 176 "*Max length of `bibtex-field-kill-ring' before discarding oldest elements."
f754fb7b
RS
177 :group 'bibtex
178 :type 'integer)
50e4b39e 179
f754fb7b 180(defcustom bibtex-entry-kill-ring-max 20
d0388eac 181 "*Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
f754fb7b
RS
182 :group 'bibtex
183 :type 'integer)
50e4b39e 184
f754fb7b 185(defcustom bibtex-parse-keys-timeout 60
50e4b39e 186 "*Specifies interval for parsing buffers.
f0cb6034 187All BibTeX buffers in Emacs are parsed if Emacs has been idle
d0388eac 188`bibtex-parse-keys-timeout' seconds. Only buffers which were modified
f754fb7b
RS
189after last parsing and which are maintained in sorted order are parsed."
190 :group 'bibtex
191 :type 'integer)
af6fb89d 192
cb4ad359
RS
193(defvar bibtex-entry-field-alist
194 '(
195 ("Article" . (((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 196 ("title" "Title of the article (BibTeX converts it to lowercase)")
cb4ad359
RS
197 ("journal" "Name of the journal (use string, remove braces)")
198 ("year" "Year of publication"))
199 (("volume" "Volume of the journal")
50e4b39e 200 ("number" "Number of the journal (only allowed if entry contains volume)")
cb4ad359 201 ("pages" "Pages in the journal")
50e4b39e 202 ("month" "Month of the publication as a string (remove braces)")
cb4ad359
RS
203 ("note" "Remarks to be put at the end of the \\bibitem")))
204 ((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 205 ("title" "Title of the article (BibTeX converts it to lowercase)"))
50e4b39e 206 (("pages" "Pages in the journal")
f0cb6034 207 ("journal" "Name of the journal (use string, remove braces)")
cb4ad359
RS
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)")
cb4ad359 212 ("note" "Remarks to be put at the end of the \\bibitem")))))
50e4b39e
RS
213 ("Book" . (((("author" "Author1 [and Author2 ...] [and others]" "" t)
214 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
cb4ad359
RS
215 ("title" "Title of the book")
216 ("publisher" "Publishing company")
217 ("year" "Year of publication"))
50e4b39e
RS
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")
cb4ad359
RS
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")))))
0640d7bf 237 ("Booklet" . (((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
cb4ad359
RS
238 (("author" "Author1 [and Author2 ...] [and others]")
239 ("howpublished" "The way in which the booklet was published")
240 ("address" "Address of the publisher")
cb4ad359 241 ("month" "Month of the publication as a string (remove braces)")
50e4b39e 242 ("year" "Year of publication")
cb4ad359 243 ("note" "Remarks to be put at the end of the \\bibitem")))))
50e4b39e
RS
244 ("InBook" . (((("author" "Author1 [and Author2 ...] [and others]" "" t)
245 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
cb4ad359
RS
246 ("title" "Title of the book")
247 ("chapter" "Chapter in the book")
248 ("publisher" "Publishing company")
249 ("year" "Year of publication"))
50e4b39e 250 (("volume" "Volume of the book in the series")
cb4ad359
RS
251 ("number" "Number of the book in a small series (overwritten by volume)")
252 ("series" "Series in which the book appeared")
50e4b39e 253 ("type" "Word to use instead of \"chapter\"")
cb4ad359
RS
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")
cb4ad359 258 ("note" "Remarks to be put at the end of the \\bibitem")))
50e4b39e
RS
259 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
260 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
cb4ad359
RS
261 ("title" "Title of the book")
262 ("chapter" "Chapter in the book"))
50e4b39e
RS
263 (("pages" "Pages in the book")
264 ("publisher" "Publishing company")
cb4ad359 265 ("year" "Year of publication")
cb4ad359
RS
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")
50e4b39e 269 ("type" "Word to use instead of \"chapter\"")
cb4ad359
RS
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)")
cb4ad359
RS
273 ("note" "Remarks to be put at the end of the \\bibitem")))))
274 ("InCollection" . (((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 275 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
cb4ad359
RS
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")
cb4ad359 283 ("type" "Word to use instead of \"chapter\"")
50e4b39e
RS
284 ("chapter" "Chapter in the book")
285 ("pages" "Pages in the book")
cb4ad359
RS
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)")
cb4ad359
RS
289 ("note" "Remarks to be put at the end of the \\bibitem")))
290 ((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 291 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
cb4ad359 292 ("booktitle" "Name of the book"))
50e4b39e
RS
293 (("pages" "Pages in the book")
294 ("publisher" "Publishing company")
cb4ad359
RS
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")
cb4ad359 300 ("type" "Word to use instead of \"chapter\"")
50e4b39e 301 ("chapter" "Chapter in the book")
cb4ad359
RS
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)")
cb4ad359
RS
305 ("note" "Remarks to be put at the end of the \\bibitem")))))
306 ("InProceedings" . (((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 307 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
cb4ad359
RS
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")
50e4b39e 314 ("pages" "Pages in the conference proceedings")
cb4ad359
RS
315 ("address" "Location of the Proceedings")
316 ("month" "Month of the publication as a string (remove braces)")
50e4b39e
RS
317 ("organization" "Sponsoring organization of the conference")
318 ("publisher" "Publishing company, its location")
cb4ad359
RS
319 ("note" "Remarks to be put at the end of the \\bibitem")))
320 ((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 321 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
cb4ad359 322 ("booktitle" "Name of the conference proceedings"))
50e4b39e
RS
323 (("pages" "Pages in the conference proceedings")
324 ("year" "Year of publication")
325 ("editor" "Editor1 [and Editor2 ...] [and others]")
cb4ad359
RS
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")
cb4ad359
RS
329 ("address" "Location of the Proceedings")
330 ("month" "Month of the publication as a string (remove braces)")
50e4b39e
RS
331 ("organization" "Sponsoring organization of the conference")
332 ("publisher" "Publishing company, its location")
cb4ad359
RS
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")
cb4ad359 339 ("month" "Month of the publication as a string (remove braces)")
50e4b39e 340 ("year" "Year of publication")
cb4ad359 341 ("note" "Remarks to be put at the end of the \\bibitem")))))
cb4ad359 342 ("MastersThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 343 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
cb4ad359
RS
344 ("school" "School where the master\'s thesis was written")
345 ("year" "Year of publication"))
50e4b39e
RS
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")
cb4ad359
RS
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]")
0640d7bf 352 ("title" "Title of the reference (BibTeX converts it to lowercase)")
cb4ad359 353 ("howpublished" "The way in which the reference was published")
cb4ad359 354 ("month" "Month of the publication as a string (remove braces)")
50e4b39e 355 ("year" "Year of publication")
cb4ad359
RS
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"))
50e4b39e
RS
361 (("type" "Type of the PhD. thesis")
362 ("address" "Address of the school (if not part of field \"school\") or country")
cb4ad359
RS
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")
cb4ad359
RS
371 ("address" "Location of the Proceedings")
372 ("month" "Month of the publication as a string (remove braces)")
50e4b39e
RS
373 ("organization" "Sponsoring organization of the conference")
374 ("publisher" "Publishing company, its location")
cb4ad359
RS
375 ("note" "Remarks to be put at the end of the \\bibitem")))))
376 ("TechReport" . (((("author" "Author1 [and Author2 ...] [and others]")
0640d7bf 377 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
cb4ad359
RS
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]")
0640d7bf 386 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)")
cb4ad359 387 ("note" "Remarks to be put at the end of the \\bibitem"))
50e4b39e
RS
388 (("month" "Month of the publication as a string (remove braces)")
389 ("year" "Year of publication")))))
cb4ad359
RS
390 )
391
392 "Defines reference types and their associated fields.
393List of
50e4b39e 394(ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL))
cb4ad359 395triples.
50e4b39e
RS
396If the third element is nil, the first pair is always used.
397If not, the second pair is used in the case of presence of a crossref
398field and the third in the case of absence.
f0cb6034 399REQUIRED, OPTIONAL, CROSSREF-REQUIRED and CROSSREF-OPTIONAL are lists.
50e4b39e
RS
400Each element of these lists is a list of the form
401(FIELD-NAME COMMENT-STRING INIT ALTERNATIVE-FLAG).
402COMMENT-STRING, INIT, and ALTERNATIVE-FLAG are optional.
403FIELD-NAME is the name of the field, COMMENT-STRING the comment to
404appear in the echo area, INIT is either the initial content of the
405field or a function, which is called to determine the initial content
406of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
d0388eac 407field is an alternative. ALTERNATIVE-FLAG may be t only in the
50e4b39e
RS
408REQUIRED or CROSSREF-REQUIRED lists.")
409
ab2d0cdb
RS
410(defvar bibtex-comment-start "@Comment ")
411
f754fb7b
RS
412(defcustom bibtex-add-entry-hook nil
413 "List of functions to call when entry has been inserted."
414 :group 'bibtex
ab2d0cdb 415 :type 'hook)
50e4b39e 416
f754fb7b 417(defcustom bibtex-predefined-month-strings
cb4ad359 418 '(
50e4b39e
RS
419 ("jan") ("feb") ("mar") ("apr") ("may") ("jun")
420 ("jul") ("aug") ("sep") ("oct") ("nov") ("dec")
cb4ad359 421 )
50e4b39e
RS
422 "Alist of month string definitions.
423Should contain all strings used for months in the BibTeX style files.
f754fb7b
RS
424Each element is a list with just one element: the string."
425 :group 'bibtex
426 :type '(repeat
427 (list string)))
50e4b39e 428
f754fb7b 429(defcustom bibtex-predefined-strings
50e4b39e
RS
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 ))
cb4ad359 438 "Alist of string definitions.
d0388eac 439Should contain the strings defined in the BibTeX style files. Each
f754fb7b
RS
440element is a list with just one element: the string."
441 :group 'bibtex
442 :type '(repeat
443 (list string)))
cb4ad359 444
f754fb7b 445(defcustom bibtex-string-files nil
cb4ad359
RS
446 "*List of BibTeX files containing string definitions.
447Those files must be specified using pathnames relative to the
d0388eac 448directories specified in `bibtex-string-file-path'. This variable is only
f0cb6034 449evaluated when BibTeX mode is entered (i.e., when loading the BibTeX
f754fb7b
RS
450file)."
451 :group 'bibtex
452 :type '(repeat file))
50e4b39e
RS
453
454(defvar bibtex-string-file-path (getenv "BIBINPUTS")
d0388eac 455 "*Colon separated list of pathes to search for `bibtex-string-files'.")
cb4ad359 456
f754fb7b
RS
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)
cb4ad359 461
f754fb7b 462(defcustom bibtex-autokey-prefix-string ""
50e4b39e 463 "*String to use as a prefix for all generated keys.
d0388eac 464See the documentation of function `bibtex-generate-autokey' for more detail."
f754fb7b
RS
465 :group 'bibtex-autokey
466 :type 'string)
50e4b39e 467
f754fb7b 468(defcustom bibtex-autokey-names 1
cb4ad359 469 "*Number of names to use for the automatically generated reference key.
ab2d0cdb 470If this is variable is nil, all names are used.
d0388eac
RS
471Possibly more names are used according to `bibtex-autokey-names-stretch'.
472See the documentation of function `bibtex-generate-autokey' for more detail."
f754fb7b 473 :group 'bibtex-autokey
ab2d0cdb
RS
474 :type '(choice (const :tag "All" infty)
475 integer))
cb4ad359 476
f754fb7b 477(defcustom bibtex-autokey-names-stretch 0
50e4b39e
RS
478 "*Number of names that can additionally be used.
479These names are used only, if all names are used then.
d0388eac 480See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
481 :group 'bibtex-autokey
482 :type 'integer)
50e4b39e 483
f754fb7b 484(defcustom bibtex-autokey-additional-names ""
50e4b39e 485 "*String to prepend to the generated key if not all names could be used.
d0388eac 486See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
487 :group 'bibtex-autokey
488 :type 'string)
50e4b39e
RS
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
cb4ad359
RS
509 ("{" "") ("}" ""))
510 "Alist of (old-regexp new-string) pairs.
d0388eac
RS
511Used by the default values of `bibtex-autokey-name-change-strings' and
512`bibtex-autokey-titleword-change-strings'. Defaults to translating some
513language specific characters to their ASCII transcriptions, and
50e4b39e
RS
514removing any character accents.")
515
f754fb7b 516(defcustom bibtex-autokey-name-change-strings
50e4b39e 517 bibtex-autokey-transcriptions
d0388eac
RS
518 "Alist of (OLD-REGEXP NEW-STRING) pairs.
519Any part of name matching a OLD-REGEXP is replaced by NEW-STRING.
520Case is significant in OLD-REGEXP. All regexps are tried in the
50e4b39e
RS
521order in which they appear in the list, so be sure to avoid inifinite
522loops here.
d0388eac 523See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
524 :group 'bibtex-autokey
525 :type '(repeat
526 (list (regexp :tag "Old")
527 (string :tag "New"))))
cb4ad359 528
ab2d0cdb
RS
529(defcustom bibtex-autokey-name-case-convert 'downcase
530 "*Function called for each name to perform case conversion.
531See 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
f754fb7b 539(defcustom bibtex-autokey-name-length 'infty
cb4ad359
RS
540 "*Number of characters from name to incorporate into key.
541If this is set to anything but a number, all characters are used.
d0388eac 542See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
543 :group 'bibtex-autokey
544 :type '(choice (const :tag "All" infty)
545 integer))
cb4ad359 546
f754fb7b 547(defcustom bibtex-autokey-name-separator ""
cb4ad359 548 "*String that comes between any two names in the key.
d0388eac 549See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
550 :group 'bibtex-autokey
551 :type 'string)
cb4ad359 552
f754fb7b 553(defcustom bibtex-autokey-year-length 2
24c5a085 554 "*Number of rightmost digits from the year field to incorporate into key.
d0388eac 555See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
556 :group 'bibtex-autokey
557 :type 'integer)
50e4b39e 558
f754fb7b 559(defcustom bibtex-autokey-year-use-crossref-entry t
50e4b39e
RS
560 "*If non-nil use year field from crossreferenced entry if necessary.
561If this variable is non-nil and the current entry has no year, but a
562valid crossref entry, the year field from the crossreferenced entry is
563used.
d0388eac 564See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
565 :group 'bibtex-autokey
566 :type 'boolean)
cb4ad359 567
f754fb7b 568(defcustom bibtex-autokey-titlewords 5
cb4ad359
RS
569 "*Number of title words to use for the automatically generated reference key.
570If this is set to anything but a number, all title words are used.
50e4b39e 571Possibly more words from the title are used according to
d0388eac
RS
572`bibtex-autokey-titlewords-stretch'.
573See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
574 :group 'bibtex-autokey
575 :type '(choice (const :tag "All" infty)
576 integer))
cb4ad359 577
f754fb7b 578(defcustom bibtex-autokey-title-terminators
50e4b39e 579 '("\\." "!" "\\?" ":" ";" "--")
cb4ad359
RS
580 "*Regexp list defining the termination of the main part of the title.
581Case of the regexps is ignored.
d0388eac 582See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
583 :group 'bibtex-autokey
584 :type '(repeat regexp))
cb4ad359 585
f754fb7b 586(defcustom bibtex-autokey-titlewords-stretch 2
cb4ad359
RS
587 "*Number of words that can additionally be used from the title.
588These words are used only, if a sentence from the title can be ended then.
d0388eac 589See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
590 :group 'bibtex-autokey
591 :type 'integer)
cb4ad359 592
ab2d0cdb
RS
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.
597Each item of the list is a regexp. If a word of the title matchs a
598regexp from that list, it is not included in the title part of the key.
d0388eac 599See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
600 :group 'bibtex-autokey
601 :type '(repeat regexp))
cb4ad359 602
ab2d0cdb
RS
603(defcustom bibtex-autokey-titleword-case-convert 'downcase
604 "*Function called for each titleword to perform case conversion.
605See 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
f754fb7b 613(defcustom bibtex-autokey-titleword-abbrevs nil
cb4ad359 614 "*Determines exceptions to the usual abbreviation mechanism.
d0388eac
RS
615An alist of (OLD-REGEXP NEW-STRING) pairs. Case is ignored
616in matching against OLD-REGEXP, and the first matching pair is used.
617See the documentation of function `bibtex-generate-autokey' for details.")
cb4ad359 618
f754fb7b 619(defcustom bibtex-autokey-titleword-change-strings
50e4b39e 620 bibtex-autokey-transcriptions
d0388eac
RS
621 "Alist of (OLD-REGEXP NEW-STRING) pairs.
622Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
623Case is significant in OLD-REGEXP. All regexps are tried in the
50e4b39e
RS
624order in which they appear in the list, so be sure to avoid inifinite
625loops here.
d0388eac 626See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
627 :group 'bibtex-autokey
628 :type '(repeat
629 (list (regexp :tag "Old")
630 (string :tag "New"))))
cb4ad359 631
f754fb7b 632(defcustom bibtex-autokey-titleword-length 5
cb4ad359
RS
633 "*Number of characters from title words to incorporate into key.
634If this is set to anything but a number, all characters are used.
d0388eac 635See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
636 :group 'bibtex-autokey
637 :type '(choice (const :tag "All" infty)
638 integer))
cb4ad359 639
f754fb7b 640(defcustom bibtex-autokey-titleword-separator "_"
cb4ad359 641 "*String to be put between the title words.
d0388eac 642See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
643 :group 'bibtex-autokey
644 :type 'string)
cb4ad359 645
f754fb7b 646(defcustom bibtex-autokey-name-year-separator ""
cb4ad359 647 "*String to be put between name part and year part of key.
d0388eac 648See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
649 :group 'bibtex-autokey
650 :type 'string)
cb4ad359 651
f754fb7b 652(defcustom bibtex-autokey-year-title-separator ":_"
cb4ad359 653 "*String to be put between name part and year part of key.
d0388eac 654See the documentation of function `bibtex-generate-autokey' for details."
f754fb7b
RS
655 :group 'bibtex-autokey
656 :type 'string)
50e4b39e 657
f754fb7b
RS
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)
cb4ad359 662
ab2d0cdb 663(defcustom bibtex-autokey-before-presentation-function nil
50e4b39e
RS
664 "Function to call before the generated key is presented.
665If non-nil this should be a single function, which is called before
666the generated key is presented (in entry or, if
f0cb6034 667`bibtex-autokey-edit-before-use' is t, in minibuffer). This function
50e4b39e 668must take one argument (the automatically generated key), and must
f754fb7b
RS
669return with a string (the key to use)."
670 :group 'bibtex-autokey
671 :type 'function)
50e4b39e 672
f754fb7b 673(defcustom bibtex-entry-offset 0
50e4b39e 674 "*Offset for BibTeX entries.
f754fb7b
RS
675Added to the value of all other variables which determine colums."
676 :group 'bibtex
677 :type 'integer)
50e4b39e 678
f754fb7b
RS
679(defcustom bibtex-field-indentation 2
680 "*Starting column for the name part in BibTeX fields."
681 :group 'bibtex
682 :type 'integer)
50e4b39e 683
f754fb7b 684(defcustom bibtex-text-indentation
50e4b39e
RS
685 (+
686 bibtex-field-indentation
687 (length "organization = "))
688 "*Starting column for the text part in BibTeX fields.
f754fb7b
RS
689Should be equal to the space needed for the longest name part."
690 :group 'bibtex
691 :type 'integer)
50e4b39e 692
f754fb7b 693(defcustom bibtex-contline-indentation
50e4b39e 694 (+ bibtex-text-indentation 1)
f754fb7b
RS
695 "*Starting column for continuation lines of BibTeX fields."
696 :group 'bibtex
697 :type 'integer)
50e4b39e 698
f754fb7b 699(defcustom bibtex-align-at-equal-sign nil
50e4b39e 700 "*If non-nil, align fields at equal sign instead of field text.
d0388eac
RS
701If non-nil, the column for the equal sign is
702the value of `bibtex-text-indentation', minus 2."
f754fb7b
RS
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)
50e4b39e 710
31bc4210
RS
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
50e4b39e 713;; section of this file, it is defined later.
ab2d0cdb
RS
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)))
31bc4210 732
cb4ad359 733\f
5c69dbfc 734;; Syntax Table, Keybindings and BibTeX Entry List
9ae11a89
ER
735(defvar bibtex-mode-syntax-table
736 (let ((st (make-syntax-table)))
50e4b39e 737 (modify-syntax-entry ?\" "\"" st)
9ae11a89
ER
738 (modify-syntax-entry ?$ "$$ " st)
739 (modify-syntax-entry ?% "< " st)
50e4b39e
RS
740 (modify-syntax-entry ?' "w " st)
741 (modify-syntax-entry ?@ "w " st)
9ae11a89
ER
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
9ae11a89
ER
748(defvar bibtex-mode-map
749 (let ((km (make-sparse-keymap)))
9ae11a89
ER
750 (define-key km "\t" 'bibtex-find-text)
751 (define-key km "\n" 'bibtex-next-field)
cb4ad359 752 (define-key km "\M-\t" 'bibtex-complete-string)
ab2d0cdb 753 (define-key km [(meta tab)] 'bibtex-complete-key)
50e4b39e
RS
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)
9ae11a89 757 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
50e4b39e 758 (define-key km "\C-c\C-q" 'bibtex-fill-entry)
cb4ad359 759 (define-key km "\C-c?" 'bibtex-print-help-message)
9ae11a89
ER
760 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
761 (define-key km "\C-c\C-n" 'bibtex-pop-next)
50e4b39e
RS
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)
9ae11a89 768 (define-key km "\C-c\C-d" 'bibtex-empty-field)
50e4b39e
RS
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)
cb4ad359
RS
777 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
778 (define-key km "\C-c\C-rw" 'widen)
50e4b39e 779 (define-key km "\C-c\C-o" 'bibtex-remove-OPT-or-ALT)
cb4ad359 780 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
9ae11a89 781 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
cb4ad359
RS
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)
9ae11a89
ER
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)
cb4ad359 791 (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
9ae11a89 792 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
cb4ad359 793 (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
50e4b39e
RS
794 (define-key km "\C-c\C-e\M-p" 'bibtex-Preamble)
795 (define-key km "\C-c\C-e\C-s" 'bibtex-String)
cb4ad359 796 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
9ae11a89
ER
797 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
798 km))
799
50e4b39e
RS
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]))
9ae11a89 867
31bc4210
RS
868\f
869;; Bug Reporting
870
871(defconst
2231645f 872 bibtex-maintainer-address "Dirk Herrmann <D.Herrmann@tu-bs.de>")
31bc4210 873;; current maintainer
9ae11a89 874
cb4ad359 875\f
5c69dbfc 876;; Internal Variables
9ae11a89 877
cb4ad359
RS
878(defvar bibtex-pop-previous-search-point nil)
879;; Next point where bibtex-pop-previous starts looking for a similar
880;; entry.
9ae11a89 881
cb4ad359
RS
882(defvar bibtex-pop-next-search-point nil)
883;; Next point where bibtex-pop-next starts looking for a similar entry.
745bc783 884
50e4b39e
RS
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)
cb4ad359
RS
903;; Candidates for bibtex-complete-string. Initialized from
904;; bibtex-predefined-strings and bibtex-string-files. This variable is
905;; buffer-local.
50e4b39e 906(make-variable-buffer-local 'bibtex-strings)
745bc783 907
0640d7bf
KH
908(defvar bibtex-keys nil)
909;; Candidates for TAB completion when entering a reference key using
50e4b39e
RS
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.
0640d7bf
KH
913(make-variable-buffer-local 'bibtex-keys)
914
50e4b39e 915(defvar bibtex-buffer-last-parsed-tick nil)
0640d7bf
KH
916;; Remembers the value returned by buffer-modified-tick when buffer
917;; was parsed for keys the last time.
50e4b39e
RS
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
0640d7bf 946
cb4ad359 947\f
5c69dbfc 948;; Functions to Parse the BibTeX Entries
745bc783 949
745bc783 950(defun bibtex-cfield (name text)
cb4ad359 951 ;; Create a regexp for a BibTeX field of name NAME and text TEXT.
745bc783
JB
952 (concat ",[ \t\n]*\\("
953 name
954 "\\)[ \t\n]*=[ \t\n]*\\("
955 text
956 "\\)"))
cb4ad359
RS
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
50e4b39e
RS
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
31bc4210 970(defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*")
50e4b39e
RS
971;; Regexp defining the name part of a BibTeX field (almost the same as
972;; bibtex-reference-type)
cb4ad359 973
50e4b39e
RS
974(defconst bibtex-field-const "[][a-z0-9.:;?!`'/*@+=|<>&_^$-]+")
975;; Regexp defining a bibtex field constant
d30bfc76 976
31bc4210
RS
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 "{"
50e4b39e 984 bibtex-field-string-part-not-braced "*"
31bc4210
RS
985 "}"))
986;; Match field string part with no inner braces
987
988(defconst bibtex-field-string-part-1-inner-brace
745bc783 989 (concat
31bc4210 990 "{"
cb4ad359 991 "\\("
f0cb6034 992 bibtex-field-string-part-not-braced
31bc4210
RS
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 "\\("
50e4b39e 1003 bibtex-field-string-part-not-braced
31bc4210
RS
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 "\\("
50e4b39e 1014 bibtex-field-string-part-not-braced
31bc4210
RS
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 "\\("
50e4b39e 1029 "[^\"\\]" ;; every character except quote or backslash
31bc4210 1030 "\\|"
f0cb6034 1031;; "\\(" "\"[a-z-]" "\\)" ;; a quote followed by a letter or dash
af6fb89d
KH
1032;; "\\|"
1033;; last two lines commented out until lines like
1034;; author = "Stefan Sch"of"
1035;; are supported by BibTeX
50e4b39e 1036 "\\(" "\\\\\\(.\\|\n\\)" "\\)" ;; a backslash followed by any character
31bc4210
RS
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
e5167999 1047
745bc783 1048(defconst bibtex-field-string-or-const
cb4ad359
RS
1049 (concat bibtex-field-const "\\|" bibtex-field-string))
1050;; Match either bibtex-field-string or bibtex-field-const.
745bc783 1051
745bc783
JB
1052(defconst bibtex-field-text
1053 (concat
1054 "\\(" bibtex-field-string-or-const "\\)"
31bc4210 1055 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*"))
cb4ad359
RS
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 # /
0640d7bf 1058;; constant pairs.
745bc783
JB
1059
1060(defconst bibtex-field
cb4ad359
RS
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.
745bc783 1066
cb4ad359
RS
1067(defconst bibtex-text-in-field bibtex-text-in-cfield)
1068;; The regexp subexpression number of the text part in BibTeX-field.
745bc783 1069
745bc783 1070(defconst bibtex-reference-head
af6fb89d 1071 (concat "^[ \t]*\\("
745bc783 1072 bibtex-reference-type
87988794 1073 "\\)[ \t]*[({][ \t]*\\("
0640d7bf 1074 bibtex-reference-key
cb4ad359
RS
1075 "\\)"))
1076;; Regexp defining format of the header line of a BibTeX reference
1077;; entry.
1078
0640d7bf
KH
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
af6fb89d 1084(defconst bibtex-type-in-head 1)
cb4ad359
RS
1085;; The regexp subexpression number of the type part in
1086;; bibtex-reference-head.
1087
af6fb89d 1088(defconst bibtex-key-in-head 2)
cb4ad359
RS
1089;; The regexp subexpression number of the key part in
1090;; bibtex-reference-head.
745bc783 1091
50e4b39e
RS
1092(defconst bibtex-reference-infix (concat "[ \t\n]*" bibtex-field))
1093;; Regexp defining the (repeatable) infix of a bibtex reference
cb4ad359 1094
50e4b39e
RS
1095(defconst bibtex-reference-postfix "[ \t\n]*,?[ \t\n]*[})]")
1096;; Regexp defining the postfix of a bibtex reference
cb4ad359
RS
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
745bc783 1102(defconst bibtex-string
50e4b39e 1103 (concat "^[ \t]*@string[ \t\n]*[({][ \t\n]*\\("
0640d7bf 1104 bibtex-reference-key
745bc783
JB
1105 "\\)[ \t\n]*=[ \t\n]*\\("
1106 bibtex-field-text
cb4ad359
RS
1107 "\\)[ \t\n]*[})]"))
1108;; Regexp defining the format of a BibTeX string entry.
745bc783 1109
0640d7bf 1110(defconst bibtex-key-in-string 1)
cb4ad359 1111;; The regexp subexpression of the name part in bibtex-string.
745bc783 1112
cb4ad359
RS
1113(defconst bibtex-text-in-string 2)
1114;; The regexp subexpression of the text part in bibtex-string.
745bc783 1115
31bc4210
RS
1116(defvar bibtex-font-lock-keywords
1117 (list
ab2d0cdb 1118 ;; reference type and reference label
31bc4210
RS
1119 (list bibtex-reference-maybe-empty-head
1120 (list bibtex-type-in-head 'font-lock-function-name-face)
883212ce 1121 (list bibtex-key-in-head 'font-lock-constant-face nil t))
ab2d0cdb 1122 ;; comments
f0cb6034 1123 (list
ab2d0cdb
RS
1124 (concat "^\\([ \t]*" bibtex-comment-start ".*\\)$")
1125 1 'font-lock-comment-face)
1126 ;; optional field names (treated as comments)
50e4b39e
RS
1127 (list
1128 (concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
1129 1 'font-lock-comment-face)
ab2d0cdb 1130 ;; field names
31bc4210
RS
1131 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1132 1 'font-lock-variable-name-face)
31bc4210
RS
1133 "*Default expressions to highlight in BibTeX mode."))
1134;; now all needed patterns are defined
1135
cb4ad359 1136\f
5c69dbfc 1137;; Helper Functions
e5167999 1138
55fe21fc 1139(defun bibtex-delete-whitespace ()
50e4b39e
RS
1140 ;; Delete all whitespace starting at point
1141 (if (looking-at "[ \t\n]+")
1142 (delete-region (point) (match-end 0))))
1143
55fe21fc 1144(defun bibtex-current-line ()
50e4b39e
RS
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
43a8874d 1150(defun bibtex-member-of-regexp (string list)
cb4ad359 1151 ;; Return non-nil if STRING is exactly matched by an element of
ab2d0cdb 1152 ;; LIST. The value is actually the tail of LIST whose
cb4ad359 1153 ;; car matches STRING.
ab2d0cdb 1154 (let (case-fold-search)
50e4b39e
RS
1155 (while
1156 (and list (not (string-match (concat "^" (car list) "$") string)))
1157 (setq list (cdr list)))
1158 list))
cb4ad359 1159
55fe21fc 1160(defun bibtex-assoc-of-regexp (string alist)
cb4ad359 1161 ;; Return non-nil if STRING is exactly matched by the car of an
50e4b39e
RS
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
55fe21fc 1171(defun bibtex-skip-to-valid-entry (&optional backward)
50e4b39e
RS
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))))))))
9ae11a89 1213
55fe21fc 1214(defun bibtex-map-entries (fun)
cb4ad359
RS
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
50e4b39e
RS
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
f0cb6034 1323 (error "Search of BibTeX reference failed"))
50e4b39e
RS
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
d6f6135e 1359 (set-match-data md)
50e4b39e
RS
1360 ;; to set match-beginning/end again
1361 (point))
1362 (if (equal noerror nil)
1363 ;; yell
f0cb6034 1364 (error "Search of BibTeX reference failed"))
50e4b39e
RS
1365 (if (equal noerror t)
1366 ;; don't move
1367 (goto-char pnt))
1368 nil)))))
e5167999 1369
cb4ad359
RS
1370(defun bibtex-flash-head ()
1371 ;; Flash at BibTeX reference head before point, if exists.
50e4b39e
RS
1372 (let ((case-fold-search t)
1373 flash)
1374 (cond ((re-search-backward bibtex-reference-head nil t)
cb4ad359
RS
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)))))
e5167999 1387
50e4b39e
RS
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
cb4ad359
RS
1394(defun bibtex-move-outside-of-entry ()
1395 ;; Make sure we are outside of a BibTeX entry.
f0cb6034 1396 (let ((orig-point (point)))
50e4b39e 1397 (bibtex-end-of-entry)
f0cb6034
RS
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")))
50e4b39e
RS
1410
1411(defun bibtex-beginning-of-first-entry ()
1412 ;; Go to the beginning of the first BibTeX entry in buffer. Return
1413 ;; point.
e5167999 1414 (goto-char (point-min))
50e4b39e
RS
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))
e5167999 1425
cb4ad359
RS
1426(defun bibtex-inside-field ()
1427 ;; Try to avoid point being at end of a BibTeX field.
1428 (end-of-line)
0640d7bf 1429 (skip-chars-backward " \t")
cb4ad359 1430 (cond ((= (preceding-char) ?,)
0640d7bf
KH
1431 (forward-char -2)))
1432 (cond ((or
1433 (= (preceding-char) ?})
1434 (= (preceding-char) ?\"))
1435 (forward-char -1))))
9ae11a89 1436
50e4b39e 1437(defun bibtex-enclosing-field (&optional noerr)
cb4ad359 1438 ;; Search for BibTeX field enclosing point. Point moves to end of
50e4b39e
RS
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
0640d7bf 1451 (progn
50e4b39e
RS
1452 (goto-char old-point)
1453 (if noerr
1454 (setq success nil)
f0cb6034 1455 (error "Can't find enclosing BibTeX field")))
50e4b39e
RS
1456 (if (or (not (re-search-forward bibtex-field nil t))
1457 (< (match-end 0) old-point)
1458 (> (match-beginning 0) old-point))
0640d7bf 1459 (progn
50e4b39e
RS
1460 (goto-char old-point)
1461 (if noerr
1462 (setq success nil)
f0cb6034 1463 (error "Can't find enclosing BibTeX field")))))
50e4b39e 1464 success))
0640d7bf
KH
1465
1466(defun bibtex-enclosing-reference-maybe-empty-head ()
1467 ;; Search for BibTeX reference enclosing point. Point moves to
50e4b39e
RS
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)))
cb4ad359
RS
1472 (if (not
1473 (re-search-backward
50e4b39e 1474 bibtex-reference-maybe-empty-head nil t))
cb4ad359 1475 (progn
f0cb6034 1476 (error "Can't find enclosing BibTeX reference")
cb4ad359 1477 (goto-char old-point)))
0640d7bf 1478 (goto-char (match-beginning bibtex-type-in-head))
50e4b39e
RS
1479 (if (not
1480 (bibtex-search-reference t nil t))
1481 (progn
f0cb6034 1482 (error "Can't find enclosing BibTeX reference")
50e4b39e
RS
1483 (goto-char old-point)))))
1484
1485(defun bibtex-insert-current-kill (n)
1486 (if (not bibtex-last-kill-command)
f0cb6034 1487 (error "BibTeX kill ring is empty")
50e4b39e
RS
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
f0cb6034 1516 "Unknown tag field: %s. Please submit a bug report"
50e4b39e
RS
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))
f0cb6034 1530 (error "Not on a known BibTeX entry")
50e4b39e
RS
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)
f0cb6034 1560 (error "All alternatives are empty")))
50e4b39e
RS
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
f0cb6034 1631 "Mandatory field ``%s'' is empty" field-name))
50e4b39e
RS
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
f0cb6034 1698 (buffer-substring-no-properties
50e4b39e
RS
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
f0cb6034 1716 "Mandatory field ``%s'' is empty" field-name))))
50e4b39e
RS
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)))))))
e5167999 1761
cb4ad359
RS
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.
50e4b39e
RS
1766 (let (case-fold-search
1767 (return-string string)
cb4ad359
RS
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
f0cb6034
RS
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.
50e4b39e
RS
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
ab2d0cdb
RS
1802 ;; does some modifications based on `bibtex-autokey-name-change-strings'
1803 ;; and removes newlines unconditionally
50e4b39e
RS
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))))
ab2d0cdb 1813 (append bibtex-autokey-name-change-strings '(("\n" " "))))
50e4b39e
RS
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)))
ab2d0cdb 1871 (funcall bibtex-autokey-name-case-convert usename)))
50e4b39e
RS
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
ab2d0cdb
RS
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)))
50e4b39e
RS
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
f0cb6034
RS
1932 (1+ (match-beginning bibtex-text-in-cfield))
1933 (1- (match-end bibtex-text-in-cfield)))
50e4b39e
RS
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)
ab2d0cdb
RS
1948 ;; gathers words from titlestring into a list. Ignores
1949 ;; specific words and uses only a specific amount of words.
50e4b39e
RS
1950 (let (case-fold-search
1951 titlewords
1952 titlewords-extra
ab2d0cdb 1953 (counter 0))
50e4b39e
RS
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))))
ab2d0cdb 1961 (if (string-match "\\b\\w+" titlestring)
50e4b39e
RS
1962 (let* ((end-match (match-end 0))
1963 (titleword
ab2d0cdb
RS
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))
f0cb6034 1969 (setq
ab2d0cdb
RS
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)))))
50e4b39e
RS
1977 (setq
1978 titlestring (substring titlestring end-match)))
1979 (setq titlestring ""))
ab2d0cdb
RS
1980 (setq counter (1+ counter)))
1981 (if (string-match "\\b\\w+" titlestring)
50e4b39e
RS
1982 titlewords
1983 (append titlewords titlewords-extra))))
1984
1985(defun bibtex-autokey-get-titlelist (titlestring)
ab2d0cdb 1986 ;; returns all words in titlestring as a list
50e4b39e
RS
1987 ;; does some abbreviation on the found words
1988 (mapcar
1989 (lambda (titleword)
1990 (let ((abbrev
55fe21fc 1991 (bibtex-assoc-of-regexp
50e4b39e
RS
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)))
cb4ad359
RS
1999
2000(defun bibtex-generate-autokey ()
ab2d0cdb 2001 "Generates automatically a key from the author/editor and the title field.
03dbb1e7
KH
2002This will only work for entries where each field begins on a separate line.
2003The 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.
ab2d0cdb
RS
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
03dbb1e7
KH
2012 `bibtex-autokey-names' + `bibtex-autokey-names-stretch'
2013 names, all names are used.
ab2d0cdb
RS
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'.
03dbb1e7
KH
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'
ab2d0cdb 2026 digits (useful values are 2 and 4). If the year field is
03dbb1e7
KH
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.
ab2d0cdb
RS
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
03dbb1e7
KH
2034 further detail).
203510. Abbreviate the result to the string up to (but not including)
2036 the first occurrence of a regexp matched by the items of
ab2d0cdb
RS
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.
204511. Convert all used titlewords according to the conversion function
2046 `bibtex-autokey-titleword-case-convert'.
204712. 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).
205113. 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).
205414. 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.
205715. 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.
206516. 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.
03dbb1e7 207017. If the value of `bibtex-autokey-edit-before-use' is non-nil,
ab2d0cdb 2071 the key is then presented in the minibuffer to the user,
f0cb6034 2072 where it can be edited. The key given by the user is then
ab2d0cdb 2073 used."
cb4ad359 2074 (let* ((pnt (point))
50e4b39e
RS
2075 (min (bibtex-beginning-of-entry))
2076 (max (bibtex-end-of-entry))
2077 (namefield (bibtex-autokey-get-namefield min max))
2078 (name-etal "")
cb4ad359 2079 (namelist
50e4b39e
RS
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))
cb4ad359
RS
2099 (yearpart
2100 (if (equal yearfield "")
2101 ""
50e4b39e
RS
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))
cb4ad359
RS
2112 (autokey
2113 (concat
50e4b39e 2114 bibtex-autokey-prefix-string
cb4ad359
RS
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)))
ab2d0cdb 2130 (if bibtex-autokey-before-presentation-function
50e4b39e
RS
2131 (setq
2132 autokey
ab2d0cdb 2133 (funcall bibtex-autokey-before-presentation-function autokey)))
cb4ad359
RS
2134 (goto-char pnt)
2135 autokey))
e5167999 2136
50e4b39e 2137(defun bibtex-parse-keys (add verbose &optional abortable)
0640d7bf
KH
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
50e4b39e
RS
2141 ;; simply resetting it. If VERBOSE is non-nil gives messages about
2142 ;; progress. If ABORTABLE is non-nil abort on user input.
0640d7bf 2143 (if bibtex-maintain-sorted-entries
50e4b39e
RS
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)))
0640d7bf
KH
2150 (save-excursion
2151 (goto-char (point-min))
50e4b39e
RS
2152 (if verbose
2153 (bibtex-progress-message
2154 (concat (buffer-name) ": parsing reference keys")))
2155 (if (catch 'userkey
55fe21fc 2156 (bibtex-skip-to-valid-entry)
50e4b39e
RS
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
f0cb6034 2165 label2)
50e4b39e
RS
2166 (cond
2167 ((looking-at bibtex-reference-head)
2168 (setq
2169 label
f0cb6034 2170 (buffer-substring-no-properties
50e4b39e
RS
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)
55fe21fc 2190 (bibtex-skip-to-valid-entry)
50e4b39e
RS
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 ()
ab2d0cdb
RS
2209 ;; Called by bibtex-run-with-idle-timer. Whenever emacs has been idle
2210 ;; for bibtex-parse-keys-timeout seconds, all BibTeX buffers (starting
50e4b39e
RS
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
f0cb6034 2265 ;; remove double-quotes or braces if field is no concatenation
50e4b39e
RS
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))
0640d7bf 2271 (if (and
50e4b39e
RS
2272 (looking-at bibtex-field-string)
2273 (equal (match-end 0) end))
2274 (bibtex-remove-delimiters))))))
2275 ((not completion)
f0cb6034 2276 (error "Can't find completion for \"%s\"" part-of-word))
50e4b39e
RS
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")))))
0640d7bf 2298
50e4b39e
RS
2299(defun bibtex-do-auto-fill ()
2300 (let ((fill-prefix
2301 (make-string
2302 (+ bibtex-entry-offset bibtex-contline-indentation) ? )))
2303 (do-auto-fill)))
cb4ad359 2304
50e4b39e
RS
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
2403To submit a problem report, enter \\[bibtex-submit-bug-report] from a
d0388eac 2404BibTeX mode buffer. This automatically sets up a mail buffer with
31bc4210
RS
2405version information already added. You just need to add a description
2406of the problem, including a reproducable test case and send the
2407message.
cb4ad359 2408
cb4ad359 2409
50e4b39e
RS
2410General information on working with BibTeX mode:
2411
2412You should use commands as \\[bibtex-Book] to get a template for a
2413specific entry. You should then fill in all desired fields using
2414\\[bibtex-next-field] to jump from field to field. After having filled
2415in all desired fields in the entry, you should clean the new entry
2416with command \\[bibtex-clean-entry].
cb4ad359 2417
50e4b39e
RS
2418Some features of BibTeX mode are available only by setting variable
2419bibtex-maintain-sorted-entries to t. However, then BibTeX mode will
2420work with buffer containing only valid (syntactical correct) entries
2421and with entries being sorted. This is usually the case, if you have
2422created a buffer completely with BibTeX mode and finished every new
2423entry with \\[bibtex-clean-entry].
2424
2425For third party BibTeX buffers, please call the function
2426`bibtex-convert-alien' to fully take advantage of all features of
2427BibTeX mode.
2428
2429
2430Special information:
2431
2432A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
cb4ad359 2433
50e4b39e
RS
2434The optional fields start with the string OPT, and are thus ignored by BibTeX.
2435Alternatives from which only one is required start with the string ALT.
2436The 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
2440current 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
2444The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2445from all non-empty optional or alternative fields, checks that no required
2446fields are empty, and does some formatting dependent on the value of
2447bibtex-entry-format.
f0cb6034
RS
2448Note: some functions in BibTeX mode depend on entries being in a special
2449format (all fields beginning on separate lines), so it is usually a bad
50e4b39e 2450idea to remove `realign' from bibtex-entry-format.
cb4ad359
RS
2451
2452Use \\[bibtex-find-text] to position the cursor at the end of the current field.
2453Use \\[bibtex-next-field] to move to end of the next field.
2454
2455The following may be of interest as well:
9ae11a89 2456
cb4ad359
RS
2457 Functions:
2458 bibtex-entry
50e4b39e
RS
2459 bibtex-kill-entry
2460 bibtex-yank-pop
2461 bibtex-pop-previous
2462 bibtex-pop-next
2463 bibtex-complete-string
2464 bibtex-complete-key
cb4ad359 2465 bibtex-print-help-message
50e4b39e 2466 bibtex-generate-autokey
cb4ad359
RS
2467 bibtex-beginning-of-entry
2468 bibtex-end-of-entry
50e4b39e
RS
2469 bibtex-reposition-window
2470 bibtex-mark-entry
cb4ad359 2471 bibtex-ispell-abstract
50e4b39e 2472 bibtex-ispell-entry
cb4ad359
RS
2473 bibtex-narrow-to-entry
2474 bibtex-hide-entry-bodies
50e4b39e
RS
2475 bibtex-sort-buffer
2476 bibtex-validate
2477 bibtex-count
2478 bibtex-fill-entry
2479 bibtex-reformat
2480 bibtex-convert-alien
cb4ad359
RS
2481
2482 Variables:
50e4b39e 2483 bibtex-field-delimiters
cb4ad359
RS
2484 bibtex-include-OPTcrossref
2485 bibtex-include-OPTkey
50e4b39e
RS
2486 bibtex-user-optional-fields
2487 bibtex-entry-format
cb4ad359
RS
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---------------------------------------------------------
d0388eac 2495Entry to BibTeX mode calls the value of `bibtex-mode-hook' if that value is
50e4b39e
RS
2496non-nil.
2497
f0cb6034 2498\\{bibtex-mode-map}"
cb4ad359
RS
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)
50e4b39e 2505 (setq bibtex-strings bibtex-predefined-strings)
cb4ad359 2506 (mapcar
50e4b39e
RS
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
f0cb6034 2560 "File %s not in paths defined by bibtex-string-file-path variable"
50e4b39e 2561 filename))))
cb4ad359 2562 bibtex-string-files)
50e4b39e 2563 (if bibtex-maintain-sorted-entries
ab2d0cdb
RS
2564 (bibtex-run-with-idle-timer
2565 1 nil
50e4b39e
RS
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
ab2d0cdb 2572 (bibtex-run-with-idle-timer
50e4b39e
RS
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]*$")
ab2d0cdb
RS
2577 (set (make-local-variable 'comment-start) bibtex-comment-start)
2578 (set (make-local-variable 'comment-start-skip) bibtex-comment-start)
50e4b39e
RS
2579 (set (make-local-variable 'comment-column) 0)
2580 (set (make-local-variable 'normal-auto-fill-function)
2581 'bibtex-do-auto-fill)
0640d7bf 2582 (set (make-local-variable 'font-lock-defaults)
27abea5a 2583 '(bibtex-font-lock-keywords
31bc4210
RS
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 )))
50e4b39e
RS
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)))
c0b08eb0 2597 (setq imenu-case-fold-search t)
ab2d0cdb
RS
2598 ;; XEmacs needs easy-menu-add, Emacs does not care
2599 (easy-menu-add bibtex-edit-menu)
2600 (easy-menu-add bibtex-entry-menu)
cb4ad359 2601 (run-hooks 'bibtex-mode-hook))
9ae11a89 2602
31bc4210
RS
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
50e4b39e 2612 (concat "bibtex.el " "(emacs 19.35)")
31bc4210
RS
2613 (list
2614 'system-configuration
2615 'system-configuration-options
50e4b39e
RS
2616 'bibtex-mode-hook
2617 'bibtex-parse-keys-timeout
2618 ;; possible general errors
31bc4210
RS
2619 'bibtex-sort-ignore-string-entries
2620 'bibtex-maintain-sorted-entries
50e4b39e
RS
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
31bc4210
RS
2637 'bibtex-predefined-strings
2638 'bibtex-string-files
50e4b39e
RS
2639 'bibtex-string-file-path
2640 ;; possible format error
31bc4210 2641 'bibtex-font-lock-keywords
50e4b39e
RS
2642 ;; possible bugs regarding fontlocking
2643 'bibtex-autokey-prefix-string
31bc4210 2644 'bibtex-autokey-names
50e4b39e
RS
2645 'bibtex-autokey-names-stretch
2646 'bibtex-autokey-additional-names
2647 'bibtex-autokey-transcriptions
31bc4210 2648 'bibtex-autokey-name-change-strings
ab2d0cdb 2649 'bibtex-autokey-name-case-convert
31bc4210
RS
2650 'bibtex-autokey-name-length
2651 'bibtex-autokey-name-separator
2652 'bibtex-autokey-year-length
50e4b39e 2653 'bibtex-autokey-year-use-crossref-entry
31bc4210
RS
2654 'bibtex-autokey-titlewords
2655 'bibtex-autokey-title-terminators
2656 'bibtex-autokey-titlewords-stretch
ab2d0cdb
RS
2657 'bibtex-autokey-titleword-ignore
2658 'bibtex-autokey-titleword-case-convert
31bc4210
RS
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
ab2d0cdb 2666 'bibtex-autokey-before-presentation-function
50e4b39e 2667 ;; possible bugs regarding automatic labels
31bc4210 2668 'bibtex-entry-field-alist
50e4b39e 2669 ;; possible format error
31bc4210
RS
2670 'bibtex-help-message
2671 'bibtex-include-OPTcrossref
2672 'bibtex-include-OPTkey
50e4b39e
RS
2673 'bibtex-field-kill-ring-max
2674 'bibtex-entry-kill-ring-max
2675 ;; user variables which shouldn't cause any errors
31bc4210
RS
2676 )
2677 nil nil
2231645f
RS
2678 (concat "To the bibtex.el maintainer:
2679
31bc4210
RS
2680I want to report a bug on Emacs BibTeX mode.
2681I've read the `Bugs' section in the `Emacs' info page, so I know how
2682to make a clear and unambiguous report. I have started a fresh Emacs
2683via `"invocation-name " --no-init-file --no-site-file', thereafter (in
2684case I'm reporting on a version of `bibtex.el' which is not part of
2685the standard emacs distribution) I loaded the questionable version
2686of `bibtex.el' with `M-x load-file', and then, to produce the buggy
2687behaviour, I did the following:")))
2688 (message nil))))
2689
50e4b39e 2690(defun bibtex-entry (entry-type)
f0cb6034 2691 "Insert a new BibTeX entry.
d0388eac 2692After insertion it calls the functions in `bibtex-add-entry-hook'."
9ae11a89 2693 (interactive (let* ((completion-ignore-case t)
cb4ad359
RS
2694 (e-t (completing-read
2695 "Entry Type: "
2696 bibtex-entry-field-alist
50e4b39e 2697 nil t nil 'bibtex-entry-type-history)))
9ae11a89 2698 (list e-t)))
50e4b39e
RS
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)
f0cb6034 2712 (error "Bibtex entry type %s not defined" entry-type))
50e4b39e
RS
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)))
0640d7bf
KH
2720 (if bibtex-maintain-sorted-entries
2721 (bibtex-find-entry-location key)
2722 (bibtex-move-outside-of-entry))
50e4b39e
RS
2723 (indent-to-column bibtex-entry-offset)
2724 (insert "@" entry-type (bibtex-entry-left-delimiter))
e5167999 2725 (if key
9ae11a89
ER
2726 (insert key))
2727 (save-excursion
2728 (mapcar 'bibtex-make-field required)
cb4ad359
RS
2729 (if (member entry-type bibtex-include-OPTcrossref)
2730 (bibtex-make-optional-field '("crossref")))
9ae11a89 2731 (if bibtex-include-OPTkey
50e4b39e
RS
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))
f0cb6034 2737 (bibtex-make-optional-field '("key"))))
9ae11a89 2738 (mapcar 'bibtex-make-optional-field optional)
50e4b39e
RS
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"))
0640d7bf 2745 (bibtex-next-field t)
9ae11a89 2746 (run-hooks 'bibtex-add-entry-hook)))
e5167999 2747
cb4ad359
RS
2748(defun bibtex-print-help-message ()
2749 "Prints helpful information about current field in current BibTeX entry."
2750 (interactive)
50e4b39e
RS
2751 (let* ((case-fold-search t)
2752 (pnt (point))
cb4ad359
RS
2753 (field-name
2754 (progn
cb4ad359 2755 (condition-case errname
50e4b39e 2756 (bibtex-enclosing-field)
cb4ad359
RS
2757 (search-failed
2758 (goto-char pnt)
f0cb6034 2759 (error "Not on BibTeX field")))
0640d7bf
KH
2760 (let ((mb (match-beginning bibtex-name-in-field))
2761 (me (match-end bibtex-name-in-field)))
2762 (goto-char mb)
5c69dbfc 2763 (buffer-substring-no-properties
50e4b39e 2764 (if (looking-at "OPT\\|ALT")
cb4ad359
RS
2765 (+ 3 mb)
2766 mb)
2767 me))))
2768 (reference-type
2769 (progn
2770 (re-search-backward
0640d7bf 2771 bibtex-reference-maybe-empty-head nil t)
5c69dbfc 2772 (buffer-substring-no-properties
0640d7bf
KH
2773 (1+ (match-beginning bibtex-type-in-head))
2774 (match-end bibtex-type-in-head))))
cb4ad359
RS
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)
50e4b39e 2788 bibtex-user-optional-fields
cb4ad359
RS
2789 (if (member
2790 reference-type
2791 bibtex-include-OPTcrossref)
2792 '(("crossref"
2793 "Label of the crossreferenced entry")))
cb4ad359
RS
2794 (if bibtex-include-OPTkey
2795 '(("key"
2796 "Key used for label creation if author and editor fields are missing"))))))
2797 (goto-char pnt)
31bc4210
RS
2798 (let ((comment (assoc-ignore-case field-name list-of-entries)))
2799 (if comment
2800 (message (elt comment 1))
2801 (message "NO COMMENT AVAILABLE")))))
d30bfc76 2802
50e4b39e
RS
2803(defun bibtex-make-field (e-t &optional called-by-yank)
2804 "Makes a field named E-T in current BibTeX entry.
f0cb6034 2805This function is for interactive and non-interactive purposes. To call
50e4b39e
RS
2806it interactively, just give it no arguments and enter the field name
2807using 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)
0640d7bf 2842 (if (looking-at "[}\"]")
50e4b39e 2843 (forward-char))))
9ae11a89 2844 (insert ",\n")
50e4b39e
RS
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
f0cb6034 2865 (error "%s is neither a string nor a function" init))))
50e4b39e
RS
2866 (if called-by-yank
2867 ""
2868 (bibtex-field-right-delimiter)))
0640d7bf
KH
2869 (if (interactive-p)
2870 (forward-char -1))))
9ae11a89 2871
cb4ad359
RS
2872(defun bibtex-beginning-of-entry ()
2873 "Move to beginning of BibTeX entry.
2874If inside an entry, move to the beginning of it, otherwise move to the
50e4b39e 2875beginning of the previous entry.
d0388eac 2876If called from a program, this function returns the new location of point."
745bc783 2877 (interactive)
50e4b39e
RS
2878 (skip-chars-forward " \t")
2879 (if (looking-at "@")
a9cb9b80 2880 (forward-char))
50e4b39e 2881 (re-search-backward "^[ \t]*@" nil 'move))
e5167999 2882
cb4ad359
RS
2883(defun bibtex-end-of-entry ()
2884 "Move to end of BibTeX entry.
2885If inside an entry, move to the end of it, otherwise move to the end
50e4b39e 2886of the previous entry.
d0388eac 2887If called from a program, this function returns the new location of point."
745bc783 2888 (interactive)
50e4b39e 2889 (let ((case-fold-search t)
f0cb6034 2890 (valid-entry-head
50e4b39e
RS
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)
f0cb6034 2933 (error "Syntactical incorrect entry starts here"))))
50e4b39e 2934 (point))
f0cb6034 2935
50e4b39e
RS
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.
f0cb6034
RS
2952With prefix argument COUNT-STRING-ENTRIES it counts all entries,
2953otherwise it counts all except Strings.
50e4b39e
RS
2954If mark is active it counts entries in region, if not in whole buffer."
2955 (interactive "P")
2956 (let ((pnt (point))
2957 (start-point
ab2d0cdb 2958 (if (bibtex-mark-active)
50e4b39e
RS
2959 (region-beginning)
2960 (bibtex-beginning-of-first-entry)))
2961 (end-point
ab2d0cdb 2962 (if (bibtex-mark-active)
50e4b39e
RS
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)
55fe21fc 2971 (bibtex-map-entries
50e4b39e
RS
2972 (lambda (current)
2973 (setq number (1+ number)))))
ab2d0cdb 2974 (message (concat (if (bibtex-mark-active) "Region" "Buffer")
50e4b39e
RS
2975 " contains %d entries.") number)
2976 (goto-char pnt)))
2977
cb4ad359
RS
2978(defun bibtex-ispell-entry ()
2979 "Spell whole BibTeX entry."
745bc783 2980 (interactive)
50e4b39e 2981 (ispell-region (bibtex-beginning-of-entry) (bibtex-end-of-entry)))
745bc783 2982
cb4ad359
RS
2983(defun bibtex-ispell-abstract ()
2984 "Spell abstract of BibTeX entry."
745bc783 2985 (interactive)
50e4b39e
RS
2986 (let ((case-fold-search t)
2987 (pnt (bibtex-end-of-entry)))
cb4ad359 2988 (bibtex-beginning-of-entry)
50e4b39e
RS
2989 (if (not
2990 (re-search-forward
2991 (bibtex-cfield "abstract" bibtex-field-text) pnt t))
f0cb6034 2992 (error "No abstract in entry")))
50e4b39e
RS
2993 (ispell-region (match-beginning bibtex-text-in-cfield)
2994 (match-end bibtex-text-in-cfield)))
745bc783 2995
cb4ad359
RS
2996(defun bibtex-narrow-to-entry ()
2997 "Narrow buffer to current BibTeX entry."
745bc783 2998 (interactive)
cb4ad359 2999 (save-excursion
50e4b39e
RS
3000 (narrow-to-region
3001 (bibtex-beginning-of-entry) (bibtex-end-of-entry))))
745bc783 3002
cb4ad359
RS
3003(defun bibtex-hide-entry-bodies (&optional arg)
3004 "Hide all lines between first and last BibTeX entries not beginning with @.
f0cb6034 3005With prefix argument ARG, show all text."
cb4ad359
RS
3006 (interactive "P")
3007 (save-excursion
50e4b39e
RS
3008 (bibtex-beginning-of-first-entry)
3009 (let ((buffer-read-only nil))
cb4ad359
RS
3010 (if arg
3011 (subst-char-in-region (point) (point-max) ?\r ?\n t)
50e4b39e
RS
3012 (while (not (eobp))
3013 (subst-char-in-region
3014 (point)
3015 (progn
3016 (re-search-forward "[\n\r]@" nil t)
3017 (forward-line -1)
3018 (point))
3019 ?\n ?\r t)
3020 (forward-line 1)))
3021 (setq selective-display (not arg)))))
3022
3023(defun bibtex-sort-buffer ()
3024 "Sort BibTeX buffer alphabetically by key.
d0388eac
RS
3025Text outside of BibTeX entries is not affected. If
3026`bibtex-sort-ignore-string-entries' is non-nil, @String entries will be
0640d7bf 3027ignored."
745bc783 3028 (interactive)
cb4ad359 3029 (save-restriction
cb4ad359 3030 (narrow-to-region
50e4b39e 3031 (bibtex-beginning-of-first-entry)
cb4ad359
RS
3032 (save-excursion
3033 (goto-char (point-max))
50e4b39e 3034 (bibtex-end-of-entry)))
55fe21fc 3035 (bibtex-skip-to-valid-entry)
cb4ad359
RS
3036 (sort-subr
3037 nil
3038 ;; NEXTREC function
55fe21fc 3039 'bibtex-skip-to-valid-entry
0640d7bf
KH
3040 ;; ENDREC function
3041 'bibtex-end-of-entry
cb4ad359 3042 ;; STARTKEY function
50e4b39e
RS
3043 (lambda ()
3044 (let ((case-fold-search t))
3045 (re-search-forward bibtex-reference-head)
3046 (buffer-substring-no-properties
3047 (match-beginning bibtex-key-in-head)
3048 (match-end bibtex-key-in-head)))))))
f0cb6034 3049
0640d7bf 3050(defun bibtex-find-entry-location (entry-name &optional ignore-dups)
cb4ad359 3051 "Looking for place to put the BibTeX entry named ENTRY-NAME.
0640d7bf 3052Performs a binary search (therefore, buffer is assumed to be in sorted
50e4b39e 3053order, without duplicates (see \\[bibtex-validate]), if it is
f0cb6034
RS
3054not, `bibtex-find-entry-location' will fail). If entry-name is already
3055used as a reference key, an error is signaled. However, if optional
0640d7bf 3056variable IGNORE-DUPS is non-nil, no error messages about duplicate
2e282f92 3057entries are signaled, but the error handling is assumed to be made in
d0388eac
RS
3058the calling function.
3059The value is nil if an duplicate entry error occurred,
3060and t in all other cases."
50e4b39e
RS
3061 (let* ((case-fold-search t)
3062 (left
0640d7bf 3063 (progn
50e4b39e 3064 (bibtex-beginning-of-first-entry)
55fe21fc 3065 (bibtex-skip-to-valid-entry)
50e4b39e 3066 (bibtex-end-of-entry)))
0640d7bf
KH
3067 (right
3068 (progn
50e4b39e 3069 (bibtex-beginning-of-last-entry)
55fe21fc 3070 (bibtex-skip-to-valid-entry t)
0640d7bf
KH
3071 (point)))
3072 actual-point
3073 actual-key
3074 (done (>= left right))
3075 new
3076 dup)
3077 (while (not done)
3078 (setq actual-point (/ (+ left right) 2))
3079 (goto-char actual-point)
55fe21fc 3080 (bibtex-skip-to-valid-entry t)
0640d7bf 3081 (setq actual-key
50e4b39e 3082 (progn
0640d7bf
KH
3083 (re-search-forward bibtex-reference-head)
3084 (buffer-substring-no-properties
3085 (match-beginning bibtex-key-in-head)
3086 (match-end bibtex-key-in-head))))
3087 (cond
3088 ((string-lessp entry-name actual-key)
50e4b39e 3089 (setq new (bibtex-beginning-of-entry))
0640d7bf
KH
3090 (if (equal right new)
3091 (setq done t)
3092 (setq right new)))
3093 ((string-lessp actual-key entry-name)
50e4b39e 3094 (setq new (bibtex-end-of-entry))
0640d7bf
KH
3095 (if (equal left new)
3096 (setq done t)
3097 (setq left new)))
3098 ((string-equal actual-key entry-name)
3099 (setq dup t
3100 done t)
3101 (if (not ignore-dups)
50e4b39e
RS
3102 (progn
3103 (bibtex-beginning-of-entry)
f0cb6034 3104 (error "Entry with key `%s' already exists" entry-name))))))
0640d7bf 3105 (if dup
50e4b39e
RS
3106 (progn
3107 (bibtex-beginning-of-entry)
3108 nil)
0640d7bf 3109 (goto-char right)
50e4b39e
RS
3110 (setq actual-key
3111 (if (looking-at bibtex-reference-head)
3112 (buffer-substring-no-properties
3113 (match-beginning bibtex-key-in-reference)
3114 (match-end bibtex-key-in-reference))))
3115 (if (or
f0cb6034
RS
3116 (not actual-key)
3117 (string-lessp actual-key entry-name))
50e4b39e
RS
3118 ;; buffer contains no valid entries or
3119 ;; greater than last entry --> append
0640d7bf 3120 (progn
50e4b39e
RS
3121 (bibtex-end-of-entry)
3122 (if (not (bobp))
3123 (newline (forward-line 2)))
3124 (beginning-of-line))
3125 (goto-char right))
f0cb6034 3126 t)))
cb4ad359 3127
50e4b39e
RS
3128(defun bibtex-validate (&optional test-thoroughly)
3129 "Validate if buffer or region is syntactically correct.
3130Only known reference types are checked, so you can put comments
3131outside of entries.
3132With optional argument TEST-THOROUGHLY non-nil it checks for absence of
3133required fields and questionable month fields as well.
3134If mark is active, it validates current region, if not whole buffer.
3135Returns t if test was successful, nil otherwise."
31bc4210 3136 (interactive "P")
50e4b39e
RS
3137 (let (error-list
3138 syntax-error
3139 (case-fold-search t)
3140 (valid-bibtex-entry
3141 (concat
3142 "@[ \t]*\\(\\(string\\)\\|"
3143 (mapconcat
3144 (lambda (type)
3145 (concat "\\(" (car type) "\\)"))
3146 bibtex-entry-field-alist
3147 "\\|")
3148 "\\)"))
3149 (pnt (point))
3150 (start-point
ab2d0cdb 3151 (if (bibtex-mark-active)
50e4b39e
RS
3152 (region-beginning)
3153 (bibtex-beginning-of-first-entry)))
3154 (end-point
ab2d0cdb 3155 (if (bibtex-mark-active)
50e4b39e
RS
3156 (region-end)
3157 (point-max))))
3158 (save-restriction
3159 (narrow-to-region start-point end-point)
3160 ;; looking if entries fit syntactical structure
3161 (goto-char start-point)
3162 (bibtex-progress-message "Checking syntactical structure")
3163 (while (re-search-forward "^[ \t]*@" nil t)
3164 (bibtex-progress-message)
3165 (forward-char -1)
3166 (let ((p (point))
3167 (must-match
3168 (looking-at valid-bibtex-entry)))
3169 (if (not must-match)
3170 (forward-char)
3171 (let (bibtex-sort-ignore-string-entries)
55fe21fc 3172 (bibtex-skip-to-valid-entry))
50e4b39e
RS
3173 (if (equal (point) p)
3174 (forward-char)
3175 (goto-char p)
3176 (setq
3177 error-list
3178 (cons (list
55fe21fc 3179 (bibtex-current-line)
f0cb6034 3180 "Syntax error (check esp. commas, braces, and quotes)")
50e4b39e
RS
3181 error-list))
3182 (forward-char)))))
3183 (bibtex-progress-message 'done)
3184 (if error-list
3185 (setq syntax-error t)
3186 ;; looking for correct sort order and duplicates (only if
3187 ;; there were no syntax errors)
3188 (if bibtex-maintain-sorted-entries
3189 (let (previous)
3190 (goto-char start-point)
3191 (bibtex-progress-message "Checking correct sort order")
55fe21fc 3192 (bibtex-map-entries
50e4b39e
RS
3193 (lambda (current)
3194 (bibtex-progress-message)
3195 (cond ((or (not previous)
3196 (string< previous current))
3197 (setq previous current))
3198 ((string-equal previous current)
3199 (setq
3200 error-list
55fe21fc 3201 (cons (list (bibtex-current-line)
50e4b39e
RS
3202 "Duplicate key with previous")
3203 error-list)))
3204 (t
3205 (setq previous current
3206 error-list
55fe21fc 3207 (cons (list (bibtex-current-line)
50e4b39e
RS
3208 "Entries out of order")
3209 error-list))))))
3210 (bibtex-progress-message 'done)))
3211 (if test-thoroughly
3212 (progn
3213 (goto-char start-point)
3214 (bibtex-progress-message
3215 "Checking required fields and month fields")
3216 (let ((bibtex-sort-ignore-string-entries t)
3217 (questionable-month
3218 (concat
3219 "[{\"]\\("
3220 (mapconcat
3221 (lambda (mon)
3222 (concat "\\(" (car mon) "\\)"))
3223 bibtex-predefined-month-strings
3224 "\\|")
3225 "\\)[}\"]")))
55fe21fc 3226 (bibtex-map-entries
50e4b39e
RS
3227 (lambda (current)
3228 (bibtex-progress-message)
3229 (let* ((beg (bibtex-beginning-of-entry))
3230 (end (bibtex-end-of-entry))
3231 (entry-list
3232 (progn
3233 (goto-char beg)
3234 (bibtex-search-reference nil end)
3235 (assoc-ignore-case
3236 (buffer-substring-no-properties
3237 (1+ (match-beginning bibtex-type-in-head))
3238 (match-end bibtex-type-in-head))
3239 bibtex-entry-field-alist)))
3240 (req (copy-sequence (elt (elt entry-list 1) 0)))
3241 (creq (copy-sequence (elt (elt entry-list 2) 0)))
3242 crossref-there)
3243 (goto-char beg)
3244 (while (re-search-forward bibtex-field end t)
3245 (let ((field-name
3246 (buffer-substring-no-properties
3247 (match-beginning bibtex-name-in-field)
3248 (match-end bibtex-name-in-field))))
3249 (if (and (equal (downcase field-name) "month")
3250 (string-match
3251 questionable-month
3252 (buffer-substring-no-properties
3253 (match-beginning bibtex-text-in-field)
3254 (match-end bibtex-text-in-field))))
3255 (setq
3256 error-list
3257 (cons
3258 (list
55fe21fc 3259 (bibtex-current-line)
50e4b39e
RS
3260 "Questionable month field (delimited string)")
3261 error-list)))
3262 (setq
3263 req
3264 (delete (assoc-ignore-case field-name req) req)
3265 creq
3266 (delete (assoc-ignore-case field-name creq) creq))
3267 (if (equal (downcase field-name) "crossref")
3268 (setq crossref-there t))))
3269 (if crossref-there
3270 (setq req creq))
3271 (if (or (> (length req) 1)
3272 (and (= (length req) 1)
3273 (not (elt (car req) 3))))
3274 ;; two (or more) fields missed or one field
3275 ;; missed and this isn't flagged alternative
3276 ;; (notice that this fails if there are more
3277 ;; than two alternatives in a BibTeX entry,
3278 ;; which isn't the case momentarily)
3279 (setq
3280 error-list
3281 (cons
3282 (list (save-excursion
3283 (bibtex-beginning-of-entry)
55fe21fc 3284 (bibtex-current-line))
50e4b39e
RS
3285 (concat
3286 "Required field \""
3287 (car (car req))
3288 "\" missing"))
3289 error-list)))))))
3290 (bibtex-progress-message 'done)))))
cb4ad359 3291 (goto-char pnt)
50e4b39e
RS
3292 (if error-list
3293 (let ((bufnam (buffer-name))
3294 (dir default-directory))
3295 (setq error-list
3296 (sort error-list
3297 (lambda (a b)
3298 (< (car a) (car b)))))
3299 (let ((pop-up-windows t))
3300 (pop-to-buffer nil t))
3301 (switch-to-buffer
3302 (get-buffer-create "*BibTeX validation errors*") t)
3303 ;; don't use switch-to-buffer-other-window, since this
3304 ;; doesn't allow the second parameter NORECORD
3305 (setq default-directory dir)
3306 (toggle-read-only -1)
3307 (compilation-mode)
3308 (delete-region (point-min) (point-max))
3309 (goto-char (point-min))
3310 (insert
3311 "BibTeX mode command `bibtex-validate'\n"
3312 (if syntax-error
3313 "Maybe undetected errors due to syntax errors. Correct and validate again."
3314 "")
3315 "\n")
3316 (while error-list
3317 (insert
3318 bufnam ":" (number-to-string (elt (car error-list) 0))
3319 ": " (elt (car error-list) 1) "\n")
3320 (setq error-list (cdr error-list)))
3321 (compilation-parse-errors nil nil)
3322 (setq compilation-old-error-list compilation-error-list)
3323 ;; this is necessary to avoid reparsing of buffer if you
3324 ;; switch to compilation buffer and enter
3325 ;; `compile-goto-error'
3326 (set-buffer-modified-p nil)
3327 (toggle-read-only 1)
3328 (goto-char (point-min))
3329 (other-window -1)
3330 ;; return nil
3331 nil)
ab2d0cdb 3332 (if (bibtex-mark-active)
50e4b39e
RS
3333 (message "Region is syntactically correct")
3334 (message "Buffer is syntactically correct"))
3335 t)))
745bc783 3336
745bc783 3337(defun bibtex-next-field (arg)
f0cb6034 3338 "Finds end of text of next BibTeX field; with ARG, to its beginning."
745bc783
JB
3339 (interactive "P")
3340 (bibtex-inside-field)
3341 (let ((start (point)))
3342 (condition-case ()
3343 (progn
3344 (bibtex-enclosing-field)
3345 (goto-char (match-end 0))
3346 (forward-char 2))
3347 (error
3348 (goto-char start)
3349 (end-of-line)
50e4b39e
RS
3350 (forward-char))))
3351 (bibtex-find-text arg t))
745bc783 3352
8b3106fa 3353(defun bibtex-find-text (arg &optional as-if-interactive silent)
50e4b39e 3354 "Go to end of text of current field; with ARG, go to beginning."
745bc783
JB
3355 (interactive "P")
3356 (bibtex-inside-field)
50e4b39e 3357 (if (bibtex-enclosing-field (or (interactive-p) as-if-interactive))
745bc783 3358 (progn
50e4b39e
RS
3359 (if arg
3360 (progn
3361 (goto-char (match-beginning bibtex-text-in-field))
3362 (if (looking-at "[{\"]")
3363 (forward-char)))
3364 (goto-char (match-end bibtex-text-in-field))
3365 (if (or
3366 (= (preceding-char) ?})
3367 (= (preceding-char) ?\"))
3368 (forward-char -1)))
3369 (if bibtex-help-message
3370 (bibtex-print-help-message)))
3371 (beginning-of-line)
3372 (cond
3373 ((looking-at bibtex-string)
3374 (goto-char
3375 (if arg
3376 (match-beginning bibtex-text-in-string)
3377 (match-end bibtex-text-in-string))))
3378 ((looking-at bibtex-reference-maybe-empty-head)
3379 (goto-char
3380 (if arg
3381 (match-beginning bibtex-key-in-head)
3382 (match-end 0))))
3383 (t
8b3106fa 3384 (if (not silent)
f0cb6034 3385 (error "Not on BibTeX field"))))))
50e4b39e
RS
3386
3387(defun bibtex-remove-OPT-or-ALT ()
3388 "Removes the string starting optional/alternative fields.
3389Aligns text and goes thereafter to end of text."
745bc783 3390 (interactive)
50e4b39e
RS
3391 (let ((case-fold-search t))
3392 (bibtex-inside-field)
3393 (bibtex-enclosing-field)
3394 (save-excursion
3395 (goto-char (match-beginning bibtex-name-in-field))
3396 (if (looking-at "OPT\\|ALT")
3397 (progn
3398 (delete-char (length "OPT"))
3399 ;; make field non-OPT
3400 (search-forward "=")
3401 (forward-char -1)
3402 (delete-horizontal-space)
3403 (if bibtex-align-at-equal-sign
3404 (indent-to-column (- bibtex-text-indentation 2))
3405 (insert " "))
3406 (search-forward "=")
3407 (delete-horizontal-space)
3408 (if bibtex-align-at-equal-sign
3409 (insert " ")
3410 (indent-to-column bibtex-text-indentation)))))
3411 (bibtex-inside-field)))
3412
3413(defun bibtex-remove-delimiters ()
cb4ad359 3414 "Removes \"\" or {} around string."
745bc783 3415 (interactive)
50e4b39e
RS
3416 (let ((case-fold-search t))
3417 (save-excursion
3418 (bibtex-inside-field)
3419 (bibtex-enclosing-field)
3420 (let ((start (match-beginning bibtex-text-in-field))
3421 (stop (match-end bibtex-text-in-field)))
3422 (goto-char start)
3423 (while (re-search-forward bibtex-field-string stop t)
3424 (let ((beg (copy-marker (match-beginning 0)))
3425 (end (copy-marker (match-end 0))))
3426 (goto-char beg)
3427 (if (looking-at "[{\"]")
3428 (delete-char 1))
3429 (goto-char end)
3430 (forward-char -1)
3431 (if (looking-at "[}\"]")
3432 (delete-char 1))))))))
3433
3434(defun bibtex-kill-field (&optional copy-only)
3435 "Kills the entire enclosing BibTeX field.
f0cb6034 3436With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring,'
d0388eac 3437but do not actually kill it."
50e4b39e
RS
3438 (interactive "P")
3439 (let ((pnt (point))
3440 (case-fold-search t))
745bc783
JB
3441 (bibtex-inside-field)
3442 (bibtex-enclosing-field)
50e4b39e
RS
3443 (let ((the-end (match-end 0))
3444 (the-beginning (match-beginning 0)))
3445 (goto-char the-end)
3446 (skip-chars-forward " \t\n,")
3447 (setq
3448 bibtex-field-kill-ring
3449 (cons
3450 (list
3451 'field
3452 (buffer-substring-no-properties
3453 (match-beginning bibtex-name-in-field)
3454 (match-end bibtex-name-in-field))
3455 (buffer-substring-no-properties
3456 (match-beginning bibtex-text-in-field)
3457 (match-end bibtex-text-in-field)))
3458 bibtex-field-kill-ring))
3459 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
3460 (setcdr
3461 (nthcdr (1- bibtex-field-kill-ring-max) bibtex-field-kill-ring)
3462 nil))
3463 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring)
3464 (if copy-only
3465 (goto-char pnt)
3466 (delete-region the-beginning the-end)
50e4b39e 3467 (let (bibtex-help-message)
8b3106fa 3468 (bibtex-find-text nil t t)))))
50e4b39e
RS
3469 (setq bibtex-last-kill-command 'field))
3470
3471(defun bibtex-copy-field-as-kill ()
3472 (interactive)
3473 (bibtex-kill-field t))
745bc783 3474
50e4b39e 3475(defun bibtex-kill-entry (&optional copy-only)
d0388eac 3476 "Kill the entire enclosing BibTeX reference entry.
f0cb6034 3477With prefix arg COPY-ONLY the current reference entry to
d0388eac 3478`bibtex-entry-kill-ring', but do not actually kill it."
50e4b39e
RS
3479 (interactive "P")
3480 (let ((pnt (point))
3481 (case-fold-search t)
3482 (beg (bibtex-beginning-of-entry))
3483 (end
3484 (progn
3485 (bibtex-end-of-entry)
3486 (if (re-search-forward
3487 bibtex-reference-maybe-empty-head nil 'move)
3488 (goto-char (match-beginning 0)))
3489 (point))))
3490 (setq
3491 bibtex-entry-kill-ring
3492 (cons
3493 (list 'entry (buffer-substring-no-properties beg end))
3494 bibtex-entry-kill-ring))
3495 (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
3496 (setcdr
3497 (nthcdr (1- bibtex-entry-kill-ring-max) bibtex-entry-kill-ring)
3498 nil))
3499 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring)
3500 (if copy-only
3501 (goto-char pnt)
3502 (delete-region beg end)))
3503 (setq bibtex-last-kill-command 'entry))
3504
3505(defun bibtex-copy-entry-as-kill ()
745bc783 3506 (interactive)
50e4b39e
RS
3507 (bibtex-kill-entry t))
3508
3509(defun bibtex-yank (&optional n)
3510 "Reinsert the last BibTeX item.
3511More precisely, reinsert the field or entry killed or yanked most recently.
3512With argument N, reinsert the Nth most recently killed BibTeX item.
3513See also the command \\[bibtex-yank-pop]]."
3514 (interactive "*p")
3515 (bibtex-insert-current-kill (1- n))
f0cb6034 3516 (setq this-command 'bibtex-yank))
50e4b39e
RS
3517
3518(defun bibtex-yank-pop (n)
3519 "Replace just-yanked killed BibTeX item with a different.
3520This command is allowed only immediately after a `bibtex-yank' or a
3521`bibtex-yank-pop'.
3522At such a time, the region contains a reinserted previously killed
f0cb6034 3523BibTeX item. `bibtex-yank-pop' deletes that item and inserts in its
50e4b39e
RS
3524place a different killed BibTeX item.
3525
3526With no argument, the previous kill is inserted.
3527With argument N, insert the Nth previous kill.
3528If N is negative, this is a more recent kill.
3529
3530The sequence of kills wraps around, so that after the oldest one
3531comes the newest one."
3532 (interactive "*p")
3533 (if (not (eq last-command 'bibtex-yank))
3534 (error "Previous command was not a BibTeX yank"))
3535 (setq this-command 'bibtex-yank)
3536 (let ((inhibit-read-only t))
3537 (delete-region (point) (mark t))
3538 (bibtex-insert-current-kill n)))
745bc783
JB
3539
3540(defun bibtex-empty-field ()
cb4ad359 3541 "Delete the text part of the current field, replace with empty text."
745bc783
JB
3542 (interactive)
3543 (bibtex-inside-field)
3544 (bibtex-enclosing-field)
3545 (goto-char (match-beginning bibtex-text-in-field))
50e4b39e
RS
3546 (delete-region (point) (match-end bibtex-text-in-field))
3547 (insert (concat (bibtex-field-left-delimiter)
3548 (bibtex-field-right-delimiter)) )
745bc783
JB
3549 (bibtex-find-text t))
3550
31bc4210 3551(defun bibtex-pop-previous (arg)
d0388eac 3552 "Replace text of current field with the similar field in previous entry.
f0cb6034 3553With arg, goes up ARG entries. Repeated, goes up so many times. May be
31bc4210
RS
3554intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
3555 (interactive "p")
3556 (bibtex-pop arg 'previous))
745bc783
JB
3557
3558(defun bibtex-pop-next (arg)
3559 "Replace text of current field with the text of similar field in next entry.
f0cb6034 3560With arg, goes down ARG entries. Repeated, goes down so many times. May be
745bc783
JB
3561intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3562 (interactive "p")
31bc4210 3563 (bibtex-pop arg 'next))
745bc783 3564
50e4b39e 3565(defun bibtex-clean-entry (&optional new-label called-by-reformat)
cb4ad359 3566 "Finish editing the current BibTeX entry and clean it up.
50e4b39e
RS
3567Checks that no required fields are empty and formats entry dependent
3568on the value of bibtex-entry-format.
3569If label of entry is empty or a prefix argument is given, calculate a
3570new entry label (note: this only will work if fields in entry begin on
3571separate lines prior to calling bibtex-clean-entry or if 'realign is
3572contained in bibtex-entry-format).
3573Don't call this on `string' or `preamble' entries.
3574At end of the cleaning process, the functions in
3575bibtex-clean-entry-hook are called with region narrowed to entry."
cb4ad359 3576 (interactive "P")
50e4b39e
RS
3577 (bibtex-format-entry)
3578 (let* ((case-fold-search t)
3579 (eob (bibtex-end-of-entry))
cb4ad359
RS
3580 (key (progn
3581 (bibtex-beginning-of-entry)
0640d7bf 3582 (if (re-search-forward
cb4ad359 3583 bibtex-reference-head eob t)
5c69dbfc 3584 (buffer-substring-no-properties
cb4ad359
RS
3585 (match-beginning bibtex-key-in-head)
3586 (match-end bibtex-key-in-head))))))
3587 (if (or
50e4b39e 3588 new-label
cb4ad359
RS
3589 (not key))
3590 (progn
3591 (let ((autokey
3592 (if bibtex-autokey-edit-before-use
50e4b39e
RS
3593 (read-from-minibuffer
3594 "Key to use: " (bibtex-generate-autokey) nil nil
3595 'bibtex-key-history)
cb4ad359
RS
3596 (bibtex-generate-autokey))))
3597 (bibtex-beginning-of-entry)
0640d7bf
KH
3598 (re-search-forward bibtex-reference-maybe-empty-head)
3599 (if (match-beginning bibtex-key-in-head)
3600 (delete-region (match-beginning bibtex-key-in-head)
3601 (match-end bibtex-key-in-head)))
cb4ad359 3602 (insert autokey)
50e4b39e
RS
3603 (let* ((start (bibtex-beginning-of-entry))
3604 (end (progn
3605 (bibtex-end-of-entry)
3606 (if (re-search-forward
3607 bibtex-reference-maybe-empty-head nil 'move)
3608 (goto-char (match-beginning 0)))
3609 (point)))
3610 (entry (buffer-substring start end)))
3611 (delete-region start end)
0640d7bf
KH
3612 (let ((success
3613 (or
50e4b39e 3614 called-by-reformat
0640d7bf
KH
3615 (not bibtex-maintain-sorted-entries)
3616 (bibtex-find-entry-location autokey t))))
50e4b39e 3617 (insert entry)
0640d7bf
KH
3618 (forward-char -1)
3619 (bibtex-beginning-of-entry)
3620 (re-search-forward bibtex-reference-head)
cb4ad359
RS
3621 (if (not success)
3622 (error
f0cb6034 3623 "New inserted reference yields duplicate key"))))))))
50e4b39e
RS
3624 (if (not called-by-reformat)
3625 (save-excursion
3626 (save-restriction
3627 (narrow-to-region
3628 (bibtex-beginning-of-entry) (bibtex-end-of-entry))
3629 (bibtex-parse-keys t nil)
3630 (run-hooks 'bibtex-clean-entry-hook)))))
3631
3632(defun bibtex-fill-entry ()
3633 "Fill current entry.
f0cb6034 3634Realigns entry, so that every field starts on a separate line. Field
d0388eac 3635names appear in column `bibtex-field-indentation', field text starts in
f0cb6034 3636column `bibtex-text-indentation' and continuation lines start here, too.
d0388eac 3637If `bibtex-align-at-equal-sign' is non-nil, align equal signs also."
50e4b39e
RS
3638 (interactive "*")
3639 (let ((pnt (copy-marker (point)))
3640 (end (copy-marker (bibtex-end-of-entry))))
3641 (bibtex-beginning-of-entry)
55fe21fc 3642 (bibtex-delete-whitespace)
50e4b39e
RS
3643 (indent-to-column bibtex-entry-offset)
3644 (while (re-search-forward bibtex-field end t)
3645 (let* ((begin-field
3646 (copy-marker (match-beginning 0)))
3647 (end-field
3648 (copy-marker (match-end 0)))
3649 (begin-name
3650 (copy-marker (match-beginning bibtex-name-in-field)))
3651 (end-name
3652 (copy-marker (match-end bibtex-name-in-field))))
3653 (goto-char begin-field)
3654 (forward-char)
55fe21fc 3655 (bibtex-delete-whitespace)
50e4b39e
RS
3656 (open-line 1)
3657 (forward-char)
3658 (indent-to-column
3659 (+ bibtex-entry-offset bibtex-field-indentation))
3660 (re-search-forward "[ \t\n]*=" end)
3661 (replace-match "=")
3662 (forward-char -1)
3663 (if bibtex-align-at-equal-sign
3664 (indent-to-column
3665 (+ bibtex-entry-offset (- bibtex-text-indentation 2)))
3666 (insert " "))
3667 (forward-char)
55fe21fc 3668 (bibtex-delete-whitespace)
50e4b39e
RS
3669 (if bibtex-align-at-equal-sign
3670 (insert " ")
3671 (indent-to-column bibtex-text-indentation))
3672 (while (re-search-forward "[ \t\n]+" end-field 'move)
3673 (replace-match " "))
3674 (bibtex-do-auto-fill)))
3675 (if (looking-at ",")
3676 (forward-char))
55fe21fc 3677 (bibtex-delete-whitespace)
50e4b39e
RS
3678 (open-line 1)
3679 (forward-char)
3680 (indent-to-column bibtex-entry-offset)
3681 (goto-char pnt)))
3682
3683(defun bibtex-reformat (&optional additional-options called-by-convert-alien)
d0388eac
RS
3684 "Reformat all BibTeX entries in buffer or region.
3685With prefix argument, read options for reformatting from minibuffer.
f0cb6034 3686With \\[universal-argument] \\[universal-argument] prefix argument, reuse previous answers (if any) again.
50e4b39e
RS
3687If mark is active it reformats entries in region, if not in whole buffer."
3688 (interactive "*P")
3689 (let* ((pnt (point))
3690 (use-previous-options
3691 (and (equal (prefix-numeric-value additional-options) 16)
3692 (or bibtex-reformat-previous-options
3693 bibtex-reformat-previous-labels)))
3694 (bibtex-entry-format
3695 (if additional-options
3696 (if use-previous-options
3697 bibtex-reformat-previous-options
3698 (setq
3699 bibtex-reformat-previous-options
3700 (delq
3701 nil
3702 (list
3703 (if (or
3704 called-by-convert-alien
3705 (y-or-n-p
3706 "Realign entries (recommended for files not created by BibTeX mode)? "))
3707 'realign)
3708 (if (y-or-n-p
3709 "Remove empty optional and alternative fields? ")
3710 'opts-or-alts)
3711 (if (y-or-n-p
3712 "Remove delimiters around pure numerical fields? ")
3713 'numerical-fields)
3714 (if (y-or-n-p (concat
3715 (if bibtex-comma-after-last-field
3716 "Insert"
3717 "Remove")
3718 " comma at end of entry? "))
3719 'last-comma)
3720 (if (y-or-n-p
8b3106fa 3721 "Replace double page dashes by single ones? ")
50e4b39e
RS
3722 'page-dashes)
3723 (if (y-or-n-p
3724 "Force delimiters? ")
3725 'delimiters)
3726 (if (y-or-n-p
3727 "Unify case of entry types and field names? ")
3728 'unify-case)))))
3729 '(realign)))
3730 (labels
3731 (if additional-options
3732 (if use-previous-options
3733 bibtex-reformat-previous-labels
3734 (setq
3735 bibtex-reformat-previous-labels
3736 (y-or-n-p "Generate automatically new reference labels? ")))))
3737 bibtex-autokey-edit-before-use
3738 (bibtex-sort-ignore-string-entries t)
3739 (start-point
ab2d0cdb 3740 (if (bibtex-mark-active)
50e4b39e
RS
3741 (region-beginning)
3742 (progn
3743 (bibtex-beginning-of-first-entry)
55fe21fc 3744 (bibtex-skip-to-valid-entry)
50e4b39e
RS
3745 (point))))
3746 (end-point
ab2d0cdb 3747 (if (bibtex-mark-active)
50e4b39e
RS
3748 (region-end)
3749 (point-max)))
3750 (valid-bibtex-entry
3751 (concat
3752 "[ \t\n]+\\(@[ \t]*\\("
3753 (mapconcat
3754 (lambda (type)
3755 (concat "\\(" (car type) "\\)"))
3756 bibtex-entry-field-alist
3757 "\\|")
3758 "\\)\\)")))
3759 (save-restriction
3760 (narrow-to-region start-point end-point)
3761 (if (memq 'realign bibtex-entry-format)
3762 (progn
3763 (goto-char (point-min))
3764 (while (re-search-forward valid-bibtex-entry nil t)
3765 (replace-match "\n\\1"))))
3766 (goto-char start-point)
3767 (bibtex-progress-message "Formatting" 1)
55fe21fc 3768 (bibtex-map-entries
50e4b39e
RS
3769 (lambda (current)
3770 (bibtex-progress-message)
3771 (bibtex-clean-entry labels labels)
3772 (if (memq 'realign bibtex-entry-format)
3773 (progn
3774 (bibtex-end-of-entry)
55fe21fc 3775 (bibtex-delete-whitespace)
50e4b39e
RS
3776 (open-line 2)))))
3777 (bibtex-progress-message 'done))
3778 (if (and
3779 labels
3780 bibtex-maintain-sorted-entries
3781 (not called-by-convert-alien))
3782 (progn
3783 (bibtex-sort-buffer)
3784 (setq bibtex-keys nil)
3785 (bibtex-parse-keys nil t t)))
3786 (goto-char pnt)))
3787
3788(defun bibtex-convert-alien (&optional do-additional-reformatting)
3789 "Converts an alien BibTeX buffer to be fully usable by BibTeX mode.
3790If a file doesn't confirm with some standards used by BibTeX mode,
3791some of the high-level features of BibTeX mode won't be available.
d0388eac 3792This function tries to convert current buffer to confirm with these standards.
50e4b39e
RS
3793With prefix argument DO-ADDITIONAL-REFORMATTING
3794non-nil, read options for reformatting entries from minibuffer."
3795 (interactive "*P")
3796 (message "Starting to validate buffer...")
3797 (sit-for 1 nil t)
3798 (goto-char (point-min))
3799 (while (re-search-forward "[ \t\n]+@" nil t)
3800 (replace-match "\n@"))
3801 (message
3802 "If errors occur, correct them and call `bibtex-convert-alien' again")
3803 (sit-for 5 nil t)
ab2d0cdb 3804 (if (let ((bibtex-mark-active)
50e4b39e
RS
3805 bibtex-maintain-sorted-entries)
3806 (bibtex-validate))
3807 (progn
3808 (message "Starting to reformat entries...")
3809 (sit-for 2 nil t)
3810 (bibtex-reformat do-additional-reformatting t)
3811 (if bibtex-maintain-sorted-entries
3812 (progn
3813 (message "Starting to sort buffer...")
3814 (bibtex-sort-buffer)))
3815 (goto-char (point-max))
3816 (message "Buffer is now parsable. Please save it."))))
cb4ad359
RS
3817
3818(defun bibtex-complete-string ()
3819 "Complete word fragment before point to longest prefix of a defined string.
50e4b39e
RS
3820If point is not after the part of a word, all strings are listed.
3821Remove surrounding delimiters if complete string could be expanded."
cb4ad359 3822 (interactive "*")
50e4b39e
RS
3823 (bibtex-complete bibtex-strings t))
3824
3825(defun bibtex-complete-key ()
3826 "Complete word fragment before point to longest prefix of a defined key.
f0cb6034 3827If point is not after the part of a word, all keys are listed. This
50e4b39e
RS
3828function is most useful in completing crossref entries."
3829 (interactive "*")
3830 (if (not bibtex-keys)
3831 (bibtex-parse-keys nil t))
3832 (bibtex-complete bibtex-keys))
745bc783 3833
cb4ad359 3834(defun bibtex-Article ()
f0cb6034 3835 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
cb4ad359
RS
3836 (interactive)
3837 (bibtex-entry "Article"))
2798dfd6 3838
cb4ad359 3839(defun bibtex-Book ()
f0cb6034 3840 "Insert a new BibTeX @Book entry; see also `bibtex-entry'."
cb4ad359
RS
3841 (interactive)
3842 (bibtex-entry "Book"))
2798dfd6 3843
cb4ad359 3844(defun bibtex-Booklet ()
f0cb6034 3845 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'."
cb4ad359
RS
3846 (interactive)
3847 (bibtex-entry "Booklet"))
3848
3849(defun bibtex-InBook ()
f0cb6034 3850 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'."
cb4ad359
RS
3851 (interactive)
3852 (bibtex-entry "InBook"))
3853
3854(defun bibtex-InCollection ()
f0cb6034 3855 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'."
cb4ad359
RS
3856 (interactive)
3857 (bibtex-entry "InCollection"))
3858
3859(defun bibtex-InProceedings ()
f0cb6034 3860 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'."
cb4ad359
RS
3861 (interactive)
3862 (bibtex-entry "InProceedings"))
3863
3864(defun bibtex-Manual ()
f0cb6034 3865 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'."
cb4ad359
RS
3866 (interactive)
3867 (bibtex-entry "Manual"))
3868
3869(defun bibtex-MastersThesis ()
f0cb6034 3870 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'."
cb4ad359
RS
3871 (interactive)
3872 (bibtex-entry "MastersThesis"))
3873
3874(defun bibtex-Misc ()
f0cb6034 3875 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'."
cb4ad359
RS
3876 (interactive)
3877 (bibtex-entry "Misc"))
3878
3879(defun bibtex-PhdThesis ()
f0cb6034 3880 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'."
cb4ad359
RS
3881 (interactive)
3882 (bibtex-entry "PhdThesis"))
3883
3884(defun bibtex-Proceedings ()
f0cb6034 3885 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'."
cb4ad359
RS
3886 (interactive)
3887 (bibtex-entry "Proceedings"))
3888
3889(defun bibtex-TechReport ()
f0cb6034 3890 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'."
cb4ad359
RS
3891 (interactive)
3892 (bibtex-entry "TechReport"))
3893
3894(defun bibtex-Unpublished ()
f0cb6034 3895 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'."
cb4ad359
RS
3896 (interactive)
3897 (bibtex-entry "Unpublished"))
3898
50e4b39e 3899(defun bibtex-String ()
f0cb6034 3900 "Insert a new BibTeX @String entry."
cb4ad359 3901 (interactive)
50e4b39e
RS
3902 (if (not bibtex-keys)
3903 (bibtex-parse-keys nil t))
3904 (let ((key
3905 (if (and
3906 bibtex-maintain-sorted-entries
3907 (not bibtex-sort-ignore-string-entries))
3908 (completing-read
3909 "String key: " bibtex-keys nil nil nil 'bibtex-key-history))))
3910 (if (and
3911 bibtex-maintain-sorted-entries
3912 (not bibtex-sort-ignore-string-entries))
3913 (bibtex-find-entry-location key)
3914 (bibtex-move-outside-of-entry))
3915 (indent-to-column bibtex-entry-offset)
3916 (insert
3917 (concat
3918 "@String"
3919 (bibtex-entry-left-delimiter)
3920 (if (and
3921 bibtex-maintain-sorted-entries
3922 (not bibtex-sort-ignore-string-entries))
3923 key)
3924 " = "
3925 (bibtex-field-left-delimiter)
3926 (bibtex-field-right-delimiter)
3927 (bibtex-entry-right-delimiter)
3928 "\n"))
cb4ad359 3929 (forward-line -1)
50e4b39e
RS
3930 (forward-char
3931 (if (and
3932 bibtex-maintain-sorted-entries
3933 (not bibtex-sort-ignore-string-entries))
3934 (+ (length "@String{") (length key) (length " = {"))
3935 (length "@String{")))))
3936
3937(defun bibtex-Preamble ()
f0cb6034 3938 "Insert a new BibTeX @Preamble entry."
cb4ad359
RS
3939 (interactive)
3940 (bibtex-move-outside-of-entry)
50e4b39e
RS
3941 (indent-to-column bibtex-entry-offset)
3942 (insert
3943 "@Preamble"
3944 (bibtex-entry-left-delimiter)
3945 (bibtex-entry-right-delimiter)
3946 "\n")
cb4ad359
RS
3947 (forward-line -1)
3948 (forward-char 10))
2798dfd6 3949
745bc783 3950\f
5c69dbfc 3951;; Make BibTeX a Feature
cb4ad359
RS
3952
3953(provide 'bibtex)
745bc783 3954
9ae11a89 3955;;; bibtex.el ends here