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