1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992, 1994, 1995, 1996 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: Stefan Schoef <schoef@offis.uni-oldenburg.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)
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.
32 ;; Major mode for editing and validating BibTeX files.
35 ;; See documentation for function bibtex-mode (or type "\M-x describe-mode"
36 ;; when you are in bibtex-mode).
39 ;; Distribute texinfo file.
42 ;; 1. using regular expressions to match the entire BibTeX entry dies
43 ;; on long entries (e.g. those containing abstracts) since
44 ;; the length of regular expression matches is fairly limited.
45 ;; 2. Calling bibtex-find-text in a string entry results in the
46 ;; error message "Can't find enclosing Bibtex field" instead of
47 ;; moving to the empty string. [reported by gernot@cs.unsw.oz.au]
49 ;; (current keeper: schoef@offis.uni-oldenburg.de
50 ;; previous: alarson@src.honeywell.com)
56 (defvar bibtex-field-left-delimiter
"{"
57 "*Set this to { or \" according to your personal preferences.
58 This variable is buffer local.")
59 (make-variable-buffer-local 'bibtex-field-left-delimiter
)
61 (defvar bibtex-field-right-delimiter
"}"
62 "*Set this to } or \" according to your personal preferences.
63 This variable is buffer local.")
64 (make-variable-buffer-local 'bibtex-field-right-delimiter
)
66 (defvar bibtex-include-OPTcrossref
'("InProceedings" "InCollection")
67 "*All entries listed here will have an OPTcrossref field.")
69 (defvar bibtex-include-OPTkey t
70 "*If non-nil, all entries will have an OPTkey field.")
72 (defvar bibtex-include-OPTannote t
73 "*If non-nil, all entries will have an OPTannote field.")
75 (defvar bibtex-mode-user-optional-fields nil
76 "*List of optional fields the user wants to have always present.
77 Entries should be lists of strings with two elements (first element =
78 name of the field, second element = comment to appear in the echo area).")
80 (defvar bibtex-clean-entry-zap-empty-opts t
81 "*If non-nil, bibtex-clean-entry will delete all empty optional fields.")
83 (defvar bibtex-sort-ignore-string-entries t
84 "*If non-nil, BibTeX @STRING entries are not sort-significant.
85 That means they are ignored when determining ordering of the buffer
86 (e.g. sorting, locating alphabetical position for new entries, etc.).
87 This variable is buffer local.")
88 (make-variable-buffer-local 'bibtex-sort-ignore-string-entries
)
90 (defvar bibtex-maintain-sorted-entries nil
91 "*If non-nil, bibtex-mode maintains all BibTeX entries in sorted order.
92 Setting this variable to nil will strip off some comfort (e.g. TAB
93 completion for reference keys in minibuffer, automatic detection of
94 duplicates) from bibtex-mode. See also bibtex-sort-ignore-string-entries.
95 This variable is buffer local.")
96 (make-variable-buffer-local 'bibtex-maintain-sorted-entries
)
98 (defvar bibtex-entry-field-alist
100 ("Article" .
(((("author" "Author1 [and Author2 ...] [and others]")
101 ("title" "Title of the article (BibTeX converts it to lowercase)")
102 ("journal" "Name of the journal (use string, remove braces)")
103 ("year" "Year of publication"))
104 (("volume" "Volume of the journal")
105 ("number" "Number of the journal")
106 ("month" "Month of the publication as a string (remove braces)")
107 ("pages" "Pages in the journal")
108 ("note" "Remarks to be put at the end of the \\bibitem")))
109 ((("author" "Author1 [and Author2 ...] [and others]")
110 ("title" "Title of the article (BibTeX converts it to lowercase)"))
111 (("journal" "Name of the journal (use string, remove braces)")
112 ("year" "Year of publication")
113 ("volume" "Volume of the journal")
114 ("number" "Number of the journal")
115 ("month" "Month of the publication as a string (remove braces)")
116 ("pages" "Pages in the journal")
117 ("note" "Remarks to be put at the end of the \\bibitem")))))
118 ("Book" .
(((("author" "Author1 [and Author2 ...] [and others]")
119 ("title" "Title of the book")
120 ("publisher" "Publishing company")
121 ("year" "Year of publication"))
122 (("editor" "Editor1 [and Editor2 ...] [and others]")
123 ("volume" "Volume of the book in the series")
124 ("number" "Number of the book in a small series (overwritten by volume)")
125 ("series" "Series in which the book appeared")
126 ("address" "Address of the publisher")
127 ("edition" "Edition of the book as a capitalized English word")
128 ("month" "Month of the publication as a string (remove braces)")
129 ("note" "Remarks to be put at the end of the \\bibitem")))))
130 ("Booklet" .
(((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
131 (("author" "Author1 [and Author2 ...] [and others]")
132 ("howpublished" "The way in which the booklet was published")
133 ("address" "Address of the publisher")
134 ("year" "Year of publication")
135 ("month" "Month of the publication as a string (remove braces)")
136 ("note" "Remarks to be put at the end of the \\bibitem")))))
137 ("InBook" .
(((("author" "Author1 [and Author2 ...] [and others]")
138 ("title" "Title of the book")
139 ("chapter" "Chapter in the book")
140 ("publisher" "Publishing company")
141 ("year" "Year of publication"))
142 (("editor" "Editor1 [and Editor2 ...] [and others]")
143 ("volume" "Volume of the book in the series")
144 ("number" "Number of the book in a small series (overwritten by volume)")
145 ("series" "Series in which the book appeared")
146 ("address" "Address of the publisher")
147 ("edition" "Edition of the book as a capitalized English word")
148 ("month" "Month of the publication as a string (remove braces)")
149 ("pages" "Pages in the book")
150 ("type" "Word to use instead of \"chapter\"")
151 ("note" "Remarks to be put at the end of the \\bibitem")))
152 ((("author" "Author1 [and Author2 ...] [and others]")
153 ("title" "Title of the book")
154 ("chapter" "Chapter in the book"))
155 (("publisher" "Publishing company")
156 ("year" "Year of publication")
157 ("editor" "Editor1 [and Editor2 ...] [and others]")
158 ("volume" "Volume of the book in the series")
159 ("number" "Number of the book in a small series (overwritten by volume)")
160 ("series" "Series in which the book appeared")
161 ("address" "Address of the publisher")
162 ("edition" "Edition of the book as a capitalized English word")
163 ("month" "Month of the publication as a string (remove braces)")
164 ("pages" "Pages in the book")
165 ("type" "Word to use instead of \"chapter\"")
166 ("note" "Remarks to be put at the end of the \\bibitem")))))
167 ("InCollection" .
(((("author" "Author1 [and Author2 ...] [and others]")
168 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
169 ("booktitle" "Name of the book")
170 ("publisher" "Publishing company")
171 ("year" "Year of publication"))
172 (("editor" "Editor1 [and Editor2 ...] [and others]")
173 ("volume" "Volume of the book in the series")
174 ("number" "Number of the book in a small series (overwritten by volume)")
175 ("series" "Series in which the book appeared")
176 ("chapter" "Chapter in the book")
177 ("type" "Word to use instead of \"chapter\"")
178 ("address" "Address of the publisher")
179 ("edition" "Edition of the book as a capitalized English word")
180 ("month" "Month of the publication as a string (remove braces)")
181 ("pages" "Pages in the book")
182 ("note" "Remarks to be put at the end of the \\bibitem")))
183 ((("author" "Author1 [and Author2 ...] [and others]")
184 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
185 ("booktitle" "Name of the book"))
186 (("publisher" "Publishing company")
187 ("year" "Year of publication")
188 ("editor" "Editor1 [and Editor2 ...] [and others]")
189 ("volume" "Volume of the book in the series")
190 ("number" "Number of the book in a small series (overwritten by volume)")
191 ("series" "Series in which the book appeared")
192 ("chapter" "Chapter in the book")
193 ("type" "Word to use instead of \"chapter\"")
194 ("address" "Address of the publisher")
195 ("edition" "Edition of the book as a capitalized English word")
196 ("month" "Month of the publication as a string (remove braces)")
197 ("pages" "Pages in the book")
198 ("note" "Remarks to be put at the end of the \\bibitem")))))
199 ("InProceedings" .
(((("author" "Author1 [and Author2 ...] [and others]")
200 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
201 ("booktitle" "Name of the conference proceedings")
202 ("year" "Year of publication"))
203 (("editor" "Editor1 [and Editor2 ...] [and others]")
204 ("volume" "Volume of the conference proceedings in the series")
205 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
206 ("series" "Series in which the conference proceedings appeared")
207 ("organization" "Sponsoring organization of the conference")
208 ("publisher" "Publishing company, its location")
209 ("address" "Location of the Proceedings")
210 ("month" "Month of the publication as a string (remove braces)")
211 ("pages" "Pages in the conference proceedings")
212 ("note" "Remarks to be put at the end of the \\bibitem")))
213 ((("author" "Author1 [and Author2 ...] [and others]")
214 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
215 ("booktitle" "Name of the conference proceedings"))
216 (("editor" "Editor1 [and Editor2 ...] [and others]")
217 ("volume" "Volume of the conference proceedings in the series")
218 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
219 ("series" "Series in which the conference proceedings appeared")
220 ("year" "Year of publication")
221 ("organization" "Sponsoring organization of the conference")
222 ("publisher" "Publishing company, its location")
223 ("address" "Location of the Proceedings")
224 ("month" "Month of the publication as a string (remove braces)")
225 ("pages" "Pages in the conference proceedings")
226 ("note" "Remarks to be put at the end of the \\bibitem")))))
227 ("Manual" .
(((("title" "Title of the manual"))
228 (("author" "Author1 [and Author2 ...] [and others]")
229 ("organization" "Publishing organization of the manual")
230 ("address" "Address of the organization")
231 ("edition" "Edition of the manual as a capitalized English word")
232 ("year" "Year of publication")
233 ("month" "Month of the publication as a string (remove braces)")
234 ("note" "Remarks to be put at the end of the \\bibitem")))))
236 ("MastersThesis" .
(((("author" "Author1 [and Author2 ...] [and others]")
237 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
238 ("school" "School where the master\'s thesis was written")
239 ("year" "Year of publication"))
240 (("address" "Address of the school (if not part of field \"school\") or country")
241 ("type" "Type of the master\'s thesis")
242 ("month" "Month of the publication as a string (remove braces)")
243 ("note" "Remarks to be put at the end of the \\bibitem")))))
245 (("author" "Author1 [and Author2 ...] [and others]")
246 ("title" "Title of the reference (BibTeX converts it to lowercase)")
247 ("howpublished" "The way in which the reference was published")
248 ("year" "Year of publication")
249 ("month" "Month of the publication as a string (remove braces)")
250 ("note" "Remarks to be put at the end of the \\bibitem")))))
251 ("PhdThesis" .
(((("author" "Author1 [and Author2 ...] [and others]")
252 ("title" "Title of the PhD. thesis")
253 ("school" "School where the PhD. thesis was written")
254 ("year" "Year of publication"))
255 (("address" "Address of the school (if not part of field \"school\") or country")
256 ("type" "Type of the PhD. thesis")
257 ("month" "Month of the publication as a string (remove braces)")
258 ("note" "Remarks to be put at the end of the \\bibitem")))))
259 ("Proceedings" .
(((("title" "Title of the conference proceedings")
260 ("year" "Year of publication"))
261 (("editor" "Editor1 [and Editor2 ...] [and others]")
262 ("volume" "Volume of the conference proceedings in the series")
263 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
264 ("series" "Series in which the conference proceedings appeared")
265 ("publisher" "Publishing company, its location")
266 ("organization" "Sponsoring organization of the conference")
267 ("address" "Location of the Proceedings")
268 ("month" "Month of the publication as a string (remove braces)")
269 ("note" "Remarks to be put at the end of the \\bibitem")))))
270 ("TechReport" .
(((("author" "Author1 [and Author2 ...] [and others]")
271 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
272 ("institution" "Sponsoring institution of the report")
273 ("year" "Year of publication"))
274 (("type" "Type of the report (if other than \"technical report\")")
275 ("number" "Number of the technical report")
276 ("address" "Address of the institution (if not part of field \"institution\") or country")
277 ("month" "Month of the publication as a string (remove braces)")
278 ("note" "Remarks to be put at the end of the \\bibitem")))))
279 ("Unpublished" .
(((("author" "Author1 [and Author2 ...] [and others]")
280 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)")
281 ("note" "Remarks to be put at the end of the \\bibitem"))
282 (("year" "Year of publication")
283 ("month" "Month of the publication as a string (remove braces)")))))
286 "Defines reference types and their associated fields.
288 (entry-name (required optional) (crossref-required crossref-optional))
290 If the third element is nil, the first pair is always to be used.
291 If not, the second pair is to be used in the case of presence of a
292 crossref field and the third in the case of absence.
293 Required , optional, crossref-required and crossref-optional are lists.
294 Each element of these lists is a list of strings with two elements
295 (first element = name of the field,
296 second element = comment to appear in the echo area).")
298 (defvar bibtex-predefined-strings
300 ("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug")
301 ("sep") ("oct") ("nov") ("dec")
302 ("acmcs") ("acta") ("cacm") ("ibmjrd") ("ibmsj") ("ieeese")
303 ("ieeetc") ("ieeetcad") ("ipl") ("jacm") ("jcss") ("scp")
304 ("sicomp") ("tcs") ("tocs") ("tods") ("tog") ("toms") ("toois")
307 "Alist of string definitions.
308 Should contain the strings defined in the BibTeX style files. Each
309 element is a list with just one element: the string.")
311 (defvar bibtex-string-files nil
312 "*List of BibTeX files containing string definitions.
313 Those files must be specified using pathnames relative to the
314 directories specified in $BIBINPUTS. This variable is only evaluated
315 when bibtex-mode is entered (i. e. when loading the BibTeX file).")
317 (defvar bibtex-help-message t
318 "*If not nil print help messages in the echo area on entering a new field.")
320 (defvar bibtex-autokey-names
1
321 "*Number of names to use for the automatically generated reference key.
322 If this is set to anything but a number, all names are used.
323 See the documentation of function bibtex-generate-autokey for further detail.")
325 (defvar bibtex-autokey-name-change-strings
326 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
328 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
330 "Alist of (old-regexp new-string) pairs.
331 Any part of name matching a old-regexp is replaced by new-string.
332 Case of the old-regexp is significant. All regexps are tried in the
333 order in which they appear in the list, so be sure to avoid recursion here.
334 See the documentation of function bibtex-generate-autokey for further detail.")
336 (defvar bibtex-autokey-name-length
'infty
337 "*Number of characters from name to incorporate into key.
338 If this is set to anything but a number, all characters are used.
339 See the documentation of function bibtex-generate-autokey for further detail.")
341 (defvar bibtex-autokey-name-separator
""
342 "*String that comes between any two names in the key.
343 See the documentation of function bibtex-generate-autokey for further detail.")
345 (defvar bibtex-autokey-year-length
2
346 "*Number of rightmost digits from the year field yo incorporate into key.
347 See the documentation of function bibtex-generate-autokey for further detail.")
349 (defvar bibtex-autokey-titlewords
5
350 "*Number of title words to use for the automatically generated reference key.
351 If this is set to anything but a number, all title words are used.
352 See the documentation of function bibtex-generate-autokey for further detail.")
354 (defvar bibtex-autokey-title-terminators
355 '("\\." "!" "\\?" ":" ";" "---")
356 "*Regexp list defining the termination of the main part of the title.
357 Case of the regexps is ignored.
358 See the documentation of function bibtex-generate-autokey for further detail.")
360 (defvar bibtex-autokey-titlewords-stretch
2
361 "*Number of words that can additionally be used from the title.
362 These words are used only, if a sentence from the title can be ended then.
363 See the documentation of function bibtex-generate-autokey for further detail.")
365 (defvar bibtex-autokey-titleword-first-ignore
366 '("a" "an" "on" "the" "eine?" "der" "die" "das")
367 "*Determines words that may begin a title but are not to be used in the key.
368 Each item of the list is a regexp. If the first word of the title matchs a
369 regexp from that list, it is not included in the title, even if it is
370 capitalized. Regexps in the list must be entered using lowercase letters.")
372 (defvar bibtex-autokey-titleword-abbrevs nil
373 "*Determines exceptions to the usual abbreviation mechanism.
374 A list of (old-regexp new-string) pairs.
375 Use all lowercase letters for old-regexp.
376 See the documentation of function bibtex-generate-autokey for further detail.")
378 (defvar bibtex-autokey-titleword-change-strings
379 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
381 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
383 "Alist of (old-regexp new-string) pairs.
384 Any part of title word matching a old-regexp is replaced by new-string.
385 Case of the old-regexp is significant.
386 See the documentation of function bibtex-generate-autokey for further detail.")
388 (defvar bibtex-autokey-titleword-length
5
389 "*Number of characters from title words to incorporate into key.
390 If this is set to anything but a number, all characters are used.
391 See the documentation of function bibtex-generate-autokey for further detail.")
393 (defvar bibtex-autokey-titleword-separator
"_"
394 "*String to be put between the title words.
395 See the documentation of function bibtex-generate-autokey for further detail.")
397 (defvar bibtex-autokey-name-year-separator
""
398 "*String to be put between name part and year part of key.
399 See the documentation of function bibtex-generate-autokey for further detail.")
401 (defvar bibtex-autokey-year-title-separator
":_"
402 "*String to be put between name part and year part of key.
403 See the documentation of function bibtex-generate-autokey for further detail.")
405 (defvar bibtex-autokey-edit-before-use t
406 "*If non-nil, user is allowed to edit the generated key before it is used.")
408 ;; bibtex-font-lock-keywords is a user option as well, but since the
409 ;; patterns used to define this variable are defined in a later
410 ;; section of this file, its definition comes later.
413 ;; Syntax Table, Keybindings and BibTeX Entry List
414 (defvar bibtex-mode-syntax-table
415 (let ((st (make-syntax-table)))
416 ;; [alarson:19920214.1004CST] make double quote a string quote
417 (modify-syntax-entry ?
\" "\"" st
)
418 (modify-syntax-entry ?$
"$$ " st
)
419 (modify-syntax-entry ?%
"< " st
)
420 (modify-syntax-entry ?
' "w " st
)
421 (modify-syntax-entry ?
@ "w " st
)
422 (modify-syntax-entry ?
\\ "\\" st
)
423 (modify-syntax-entry ?
\f "> " st
)
424 (modify-syntax-entry ?
\n "> " st
)
425 (modify-syntax-entry ?~
" " st
)
428 (defvar bibtex-mode-map
429 (let ((km (make-sparse-keymap)))
431 (define-key km
"\t" 'bibtex-find-text
)
432 (define-key km
"\n" 'bibtex-next-field
)
433 (define-key km
"\M-\t" 'bibtex-complete-string
)
434 (define-key km
"\C-c\"" 'bibtex-remove-double-quotes-or-braces
)
435 (define-key km
"\C-c{" 'bibtex-remove-double-quotes-or-braces
)
436 (define-key km
"\C-c}" 'bibtex-remove-double-quotes-or-braces
)
437 (define-key km
"\C-c\C-c" 'bibtex-clean-entry
)
438 (define-key km
"\C-c?" 'bibtex-print-help-message
)
439 (define-key km
"\C-c\C-p" 'bibtex-pop-previous
)
440 (define-key km
"\C-c\C-n" 'bibtex-pop-next
)
441 (define-key km
"\C-c\C-k" 'bibtex-kill-optional-field
)
442 (define-key km
"\C-c\C-d" 'bibtex-empty-field
)
443 (define-key km
"\C-c$" 'bibtex-ispell-entry
)
444 (define-key km
"\M-\C-a" 'bibtex-beginning-of-entry
)
445 (define-key km
"\M-\C-e" 'bibtex-end-of-entry
)
446 (define-key km
"\C-c\C-b" 'bibtex-entry
)
447 (define-key km
"\C-c\C-q" 'bibtex-hide-entry-bodies
)
448 (define-key km
"\C-c\C-rn" 'bibtex-narrow-to-entry
)
449 (define-key km
"\C-c\C-rw" 'widen
)
450 (define-key km
"\C-c\C-o" 'bibtex-remove-OPT
)
452 (define-key km
"\C-c\C-e\C-i" 'bibtex-InProceedings
)
453 (define-key km
"\C-c\C-ei" 'bibtex-InCollection
)
454 (define-key km
"\C-c\C-eI" 'bibtex-InBook
)
455 (define-key km
"\C-c\C-e\C-a" 'bibtex-Article
)
456 (define-key km
"\C-c\C-e\C-b" 'bibtex-InBook
)
457 (define-key km
"\C-c\C-eb" 'bibtex-Book
)
458 (define-key km
"\C-c\C-eB" 'bibtex-Booklet
)
459 (define-key km
"\C-c\C-e\C-c" 'bibtex-InCollection
)
460 (define-key km
"\C-c\C-e\C-m" 'bibtex-Manual
)
461 (define-key km
"\C-c\C-em" 'bibtex-MastersThesis
)
462 (define-key km
"\C-c\C-eM" 'bibtex-Misc
)
463 (define-key km
"\C-c\C-e\C-p" 'bibtex-InProceedings
)
464 (define-key km
"\C-c\C-ep" 'bibtex-Proceedings
)
465 (define-key km
"\C-c\C-eP" 'bibtex-PhdThesis
)
466 (define-key km
"\C-c\C-e\M-p" 'bibtex-preamble
)
467 (define-key km
"\C-c\C-e\C-s" 'bibtex-string
)
468 (define-key km
"\C-c\C-e\C-t" 'bibtex-TechReport
)
469 (define-key km
"\C-c\C-e\C-u" 'bibtex-Unpublished
)
472 (define-key bibtex-mode-map
[menu-bar bibtex-edit
]
473 (cons "BibTeX-Edit" (make-sparse-keymap "BibTeX-Edit")))
474 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-print-help-message
]
475 '("Help about Current Field" . bibtex-print-help-message
))
476 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-complete-string
]
477 '("String Complete" . bibtex-complete-string
))
478 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-next-field
]
479 '("Next Field" . bibtex-next-field
))
480 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-find-text
]
481 '("End of Field" . bibtex-find-text
))
482 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-pop-previous
]
483 '("Snatch from Similar Preceding Field" . bibtex-pop-previous
))
484 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-pop-next
]
485 '("Snatch from Similar Following Field" . bibtex-pop-next
))
486 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-remove-OPT
]
487 '("Remove OPT" . bibtex-remove-OPT
))
488 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-remove-double-quotes-or-braces
]
489 '("Remove Quotes or Braces" . bibtex-remove-double-quotes-or-braces
))
490 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-clean-entry
]
491 '("Clean Up Entry" . bibtex-clean-entry
))
492 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-sort-entries
]
493 '("Sort Entries" . bibtex-sort-entries
))
494 (define-key bibtex-mode-map
495 [menu-bar bibtex-edit bibtex-validate-buffer-from-point
]
496 '("Validate Entries Starting at Point" .
499 (bibtex-validate-buffer t
))))
500 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-validate-buffer
]
501 '("Validate Entries" . bibtex-validate-buffer
))
503 (define-key bibtex-mode-map
[menu-bar entry-types
]
504 (cons "Entry-Types" (make-sparse-keymap "Entry-Types")))
505 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-preamble
]
506 '("Preamble" . bibtex-preamble
))
507 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-string
]
508 '("String" . bibtex-string
))
509 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Misc
]
510 '("Miscellaneous" . bibtex-Misc
))
511 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Unpublished
]
512 '("Unpublished" . bibtex-Unpublished
))
513 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Manual
]
514 '("Technical Manual" . bibtex-Manual
))
515 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-TechReport
]
516 '("Technical Report" . bibtex-TechReport
))
517 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-MastersThesis
]
518 '("Master's Thesis" . bibtex-MastersThesis
))
519 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-PhdThesis
]
520 '("PhD. Thesis" . bibtex-PhdThesis
))
521 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Booklet
]
522 '("Booklet (Bound, but no Publisher/Institution)" . bibtex-Booklet
))
523 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Book
]
524 '("Book" . bibtex-Book
))
525 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Proceedings
]
526 '("Conference Proceedings" . bibtex-Proceedings
))
527 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-InBook
]
528 '("Chapter or Pages in a Book" . bibtex-InBook
))
529 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-InCollection
]
530 '("Article in a Collection" . bibtex-InCollection
))
531 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-InProceedings
]
532 '("Article in Conference Proceedings" . bibtex-InProceedings
))
533 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Article
]
534 '("Article in Journal" . bibtex-Article
))
540 bibtex-maintainer-address
"Stefan Schoef <schoef@offis.uni-oldenburg.de>")
541 ;; current maintainer
544 ;; Internal Variables
546 (defvar bibtex-pop-previous-search-point nil
)
547 ;; Next point where bibtex-pop-previous starts looking for a similar
550 (defvar bibtex-pop-next-search-point nil
)
551 ;; Next point where bibtex-pop-next starts looking for a similar entry.
553 (defvar bibtex-completion-candidates nil
)
554 ;; Candidates for bibtex-complete-string. Initialized from
555 ;; bibtex-predefined-strings and bibtex-string-files. This variable is
557 (make-variable-buffer-local 'bibtex-completion-candidates
)
559 (defvar bibtex-keys nil
)
560 ;; Candidates for TAB completion when entering a reference key using
561 ;; the minibuffer. Initialized in bibtex-mode and updated for each
562 ;; new entry. This variable is buffer-local.
563 (make-variable-buffer-local 'bibtex-keys
)
565 (defvar bibtex-buffer-last-parsed-for-keys-tick nil
)
566 ;; Remembers the value returned by buffer-modified-tick when buffer
567 ;; was parsed for keys the last time.
568 (make-variable-buffer-local 'bibtex-keys
)
571 ;; Functions to Parse the BibTeX Entries
573 (defun bibtex-cfield (name text
)
574 ;; Create a regexp for a BibTeX field of name NAME and text TEXT.
575 (concat ",[ \t\n]*\\("
577 "\\)[ \t\n]*=[ \t\n]*\\("
580 (defconst bibtex-name-in-cfield
1)
581 ;; The regexp subexpression number of the name part in bibtex-cfield.
583 (defconst bibtex-text-in-cfield
2)
584 ;; The regexp subexpression number of the text part in bibtex-cfield.
586 (defconst bibtex-field-name
"[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*")
587 ;; Regexp defining the name part of a BibTeX field.
589 (defconst bibtex-field-const
"[][A-Za-z0-9.:;?!`'()/*@_+=|<>-]+")
590 ;; Format of a bibtex field constant (same as bibtex-reference-key (see below))
592 (defconst bibtex-field-string-part-not-braced
594 ;; Match field string part without braces
596 (defconst bibtex-field-string-part-no-inner-braces
599 "\\(" bibtex-field-string-part-not-braced
"\\)*"
601 ;; Match field string part with no inner braces
603 (defconst bibtex-field-string-part-1-inner-brace
607 "\\(" bibtex-field-string-part-not-braced
"\\)"
609 "\\(" bibtex-field-string-part-no-inner-braces
"\\)"
612 ;; Match field string part with at most 1 inner brace
614 (defconst bibtex-field-string-part-2-inner-braces
618 "\\(" bibtex-field-string-part-not-braced
"\\)"
620 "\\(" bibtex-field-string-part-no-inner-braces
"\\)"
622 "\\(" bibtex-field-string-part-1-inner-brace
"\\)"
625 ;; Match field string part with at most 2 inner braces
627 (defconst bibtex-field-string-part-3-inner-braces
631 "\\(" bibtex-field-string-part-not-braced
"\\)"
633 "\\(" bibtex-field-string-part-no-inner-braces
"\\)"
635 "\\(" bibtex-field-string-part-1-inner-brace
"\\)"
637 "\\(" bibtex-field-string-part-2-inner-braces
"\\)"
640 ;; Match field string part with at most 3 inner braces
642 (defconst bibtex-field-string-braced
643 bibtex-field-string-part-3-inner-braces
)
644 ;; Match braced field string with inner nesting level of braces at most 3
646 (defconst bibtex-field-string-quoted
650 "\\(" "[^\"\\]" "\\)" ;; every character except quote or backslash
652 "\\(" "\"[A-Za-z-]" "\\)" ;; a quote followed by a letter or dash
654 "\\(" "\\\\.\\|\n" "\\)" ;; a backslash followed by any character
657 ;; Match quoted field string
659 (defconst bibtex-field-string
661 "\\(" bibtex-field-string-braced
"\\)"
663 "\\(" bibtex-field-string-quoted
"\\)"))
664 ;; Match a braced or quoted string
666 (defconst bibtex-field-string-or-const
667 (concat bibtex-field-const
"\\|" bibtex-field-string
))
668 ;; Match either bibtex-field-string or bibtex-field-const.
670 (defconst bibtex-field-text
672 "\\(" bibtex-field-string-or-const
"\\)"
673 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const
"\\)\\)*"))
674 ;; Regexp defining the text part of a BibTeX field: either a string,
675 ;; or an empty string, or a constant followed by one or more # /
678 (defconst bibtex-field
679 (bibtex-cfield bibtex-field-name bibtex-field-text
))
680 ;; Regexp defining the format of a BibTeX field.
682 (defconst bibtex-name-in-field bibtex-name-in-cfield
)
683 ;; The regexp subexpression number of the name part in BibTeX-field.
685 (defconst bibtex-text-in-field bibtex-text-in-cfield
)
686 ;; The regexp subexpression number of the text part in BibTeX-field.
688 (defconst bibtex-reference-type
"@[A-Za-z]+")
689 ;; Regexp defining the type part of a BibTeX reference entry.
691 (defconst bibtex-reference-key
"[][A-Za-z0-9.:;?!`'()/*@_+=|<>-]+")
692 ;; Regexp defining the label part of a BibTeX reference entry (same as
693 ;; bibtex-field-const (see above))
695 (defconst bibtex-reference-head
696 (concat "^\\( \\|\t\\)*\\("
697 bibtex-reference-type
698 "\\)[ \t]*[({][ \t]*\\("
701 ;; Regexp defining format of the header line of a BibTeX reference
704 (defconst bibtex-reference-maybe-empty-head
705 (concat bibtex-reference-head
"?"))
706 ;; Regexp defining format of the header line of a maybe empty
707 ;; BibTeX reference entry (without reference key).
709 (defconst bibtex-type-in-head
2)
710 ;; The regexp subexpression number of the type part in
711 ;; bibtex-reference-head.
713 (defconst bibtex-key-in-head
3)
714 ;; The regexp subexpression number of the key part in
715 ;; bibtex-reference-head.
717 (defconst bibtex-reference
718 (concat bibtex-reference-head
719 "\\([ \t\n]*" bibtex-field
"\\)*"
720 "[ \t\n]*,?[ \t\n]*[})]"))
721 ;; Regexp defining the format of a BibTeX reference entry.
723 (defconst bibtex-type-in-reference bibtex-type-in-head
)
724 ;; The regexp subexpression number of the type part in
727 (defconst bibtex-key-in-reference bibtex-key-in-head
)
728 ;; The regexp subexpression number of the key part in
731 (defconst bibtex-string
732 (concat "^[ \t]*@[sS][tT][rR][iI][nN][gG][ \t\n]*[({][ \t\n]*\\("
734 "\\)[ \t\n]*=[ \t\n]*\\("
737 ;; Regexp defining the format of a BibTeX string entry.
739 (defconst bibtex-key-in-string
1)
740 ;; The regexp subexpression of the name part in bibtex-string.
742 (defconst bibtex-text-in-string
2)
743 ;; The regexp subexpression of the text part in bibtex-string.
745 (defvar bibtex-font-lock-keywords
747 (list bibtex-reference-maybe-empty-head
748 (list bibtex-type-in-head
'font-lock-function-name-face
)
749 (list bibtex-key-in-head
'font-lock-reference-face nil t
))
750 ;; reference type and reference label
751 (list (concat "^[ \t]*\\(OPT" bibtex-field-name
"\\)[ \t]*=")
752 1 'font-lock-comment-face
)
753 ;; optional field names (treated as comments)
754 (list (concat "^[ \t]*\\(" bibtex-field-name
"\\)[ \t]*=")
755 1 'font-lock-variable-name-face
)
757 "*Default expressions to highlight in BibTeX mode."))
758 ;; now all needed patterns are defined
760 (defconst bibtex-name-alignment
2)
761 ;; Alignment for the name part in BibTeX fields. Chosen on aesthetic
764 (defconst bibtex-text-alignment
(length " organization = "))
765 ;; Alignment for the text part in BibTeX fields. Equal to the space
766 ;; needed for the longest name part.
771 (defun assoc-ignore-case (string alist
)
772 ;; Return non-nil if STRING is `equal' to the car of an element of
773 ;; LIST. Comparison is done with case ignored. The value is actually
774 ;; the element of LIST whose car is `equal' to STRING.
775 (or (assoc string alist
)
779 (downcase (car (car alist
))))))
780 (setq alist
(cdr alist
)))
783 (defun member-of-regexp (string list
)
784 ;; Return non-nil if STRING is exactly matched by an element of
785 ;; LIST. This function is influenced by the actual value of
786 ;; `case-fold-search'. The value is actually the tail of LIST whose
787 ;; car matches STRING.
793 (concat "^" (car list
) "$")
795 (setq list
(cdr list
)))
798 (defun assoc-of-regexp (string alist
)
799 ;; Return non-nil if STRING is exactly matched by the car of an
800 ;; element of LIST. This function is influenced by the actual value
801 ;; of `case-fold-search'. The value is actually the element of LIST
802 ;; whose car matches STRING.
808 (concat "^" (car (car alist
)) "$")
810 (setq alist
(cdr alist
)))
813 (defun skip-whitespace-and-comments ()
814 (let ((md (match-data)))
816 (while (cond ((looking-at "\\s>+\\|\\s +")
818 ;; NOTE: also checked end-comment. In latex and
819 ;; lisp modes, newline is an end comment, but it
820 ;; should also be a whitespace char.
821 (goto-char (match-end 0)))
822 ;; If looking at beginning of comment, skip to end.
824 (re-search-forward "\\s>"))))
825 (store-match-data md
))))
827 (defun map-bibtex-entries (fun)
828 ;; Call FUN for each BibTeX entry starting with the current. Do this
829 ;; to the end of the file. FUN is called with one argument, the key
830 ;; of the entry, and with point inside the entry. If
831 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be called
832 ;; for @string entries.
833 (bibtex-beginning-of-entry)
834 (while (re-search-forward bibtex-reference-head nil t
)
835 (if (and bibtex-sort-ignore-string-entries
836 (string-equal "@string"
837 (downcase (buffer-substring-no-properties
838 (match-beginning bibtex-type-in-head
)
839 (match-end bibtex-type-in-head
)))))
841 (funcall fun
(buffer-substring-no-properties
842 (match-beginning bibtex-key-in-head
)
843 (match-end bibtex-key-in-head
))))))
845 (defun bibtex-flash-head ()
846 ;; Flash at BibTeX reference head before point, if exists.
848 (cond ((re-search-backward bibtex-reference-head
(point-min) t
)
849 (goto-char (match-beginning bibtex-type-in-head
))
850 (setq flash
(match-end bibtex-key-in-reference
)))
853 (skip-chars-backward " \t")
856 (skip-chars-forward " \t")))
857 (if (pos-visible-in-window-p (point))
860 (buffer-substring (point) flash
)))))
862 (defun bibtex-move-outside-of-entry ()
863 ;; Make sure we are outside of a BibTeX entry.
865 (= (point) (point-max))
866 (= (point) (point-min))
867 (looking-at "[ \n]*@")
872 (forward-paragraph)))
873 (re-search-forward "[ \t\n]*" (point-max) t
))
875 (defun beginning-of-first-bibtex-entry ()
876 ;; Go to the beginning of the first BibTeX entry in buffer.
877 (goto-char (point-min))
879 ((re-search-forward "^@" nil
'move
)
884 (message "Warning: No BibTeX entries found!"))))
886 (defun bibtex-inside-field ()
887 ;; Try to avoid point being at end of a BibTeX field.
889 (skip-chars-backward " \t")
890 (cond ((= (preceding-char) ?
,)
893 (= (preceding-char) ?
})
894 (= (preceding-char) ?
\"))
897 (defun bibtex-enclosing-field ()
898 ;; Search for BibTeX field enclosing point. Point moves to end of
899 ;; field; also, use match-beginning and match-end to parse the field.
900 ;; sct@dcs.edinburgh.ac.uk
901 (let ((old-point (point)))
902 (condition-case errname
903 (bibtex-enclosing-regexp bibtex-field
)
905 (goto-char old-point
)
906 (error "Can't find enclosing BibTeX field.")))))
908 (defun bibtex-enclosing-reference ()
909 ;; Search for BibTeX reference enclosing point. Point moves to
910 ;; beginning of reference. Beginning/end of reference is given by
911 ;; (match-beginning/match-end 0).
912 (let ((old-point (point)))
914 (re-search-backward bibtex-reference-head
(point-min) t
))
916 (error "Can't find enclosing BibTeX reference.")
917 (goto-char old-point
)))
918 (goto-char (match-beginning bibtex-type-in-head
))
921 (re-search-forward bibtex-reference
(point-max) t
))
923 (error "Can't find enclosing BibTeX reference.")
924 (goto-char old-point
))
927 (defun bibtex-enclosing-reference-maybe-empty-head ()
928 ;; Search for BibTeX reference enclosing point. Point moves to
929 ;; beginning of reference. Beginning/end of reference is given by
930 ;; (match-beginning/match-end 0).
931 (let ((old-point (point)))
934 bibtex-reference-maybe-empty-head
(point-min) t
))
936 (error "Can't find enclosing BibTeX reference.")
937 (goto-char old-point
)))
938 (goto-char (match-beginning bibtex-type-in-head
))
943 bibtex-reference-maybe-empty-head
944 "\\([ \t\n]*" bibtex-field
"\\)*"
945 "[ \t\n]*,?[ \t\n]*[})]")
948 (error "Can't find enclosing BibTeX reference.")
949 (goto-char old-point
))
952 (defun bibtex-enclosing-regexp (regexp)
953 ;; Search for REGEXP enclosing point. Point moves to end of
954 ;; REGEXP. See also match-beginning and match-end. If an enclosing
955 ;; REGEXP is not found, signals search-failed; point is left in an
956 ;; undefined location.
957 ;; Doesn't something like this exist already?
958 ;; compute reasonable limits for the loop
959 (let* ((initial (point))
960 (right (if (re-search-forward regexp
(point-max) t
)
966 (if (re-search-backward regexp
(point-min) t
)
969 ; within the prescribed limits, loop until a match is found
971 (re-search-forward regexp right nil
1)
972 (if (> (match-beginning 0) initial
)
973 (signal 'search-failed
(list regexp
)))
974 (while (<= (match-end 0) initial
)
975 (re-search-forward regexp right nil
1)
976 (if (> (match-beginning 0) initial
)
977 (signal 'search-failed
(list regexp
))))
980 (defun bibtex-autokey-change (string change-list
)
981 ;; Returns a string where some regexps are changed according to
982 ;; change-list. Every item of change-list is an (old-regexp
984 (let ((return-string string
)
987 (len (length change-list
))
990 (setq change-item
(elt change-list index
))
991 (while (string-match (car change-item
) return-string
)
994 (concat (substring return-string
0 (match-beginning 0))
996 (substring return-string
(match-end 0)))))
997 (setq index
(1+ index
)))
1000 (defun bibtex-autokey-abbrev (string len
)
1001 ;; Returns an abbreviation of string with at least len
1002 ;; characters. String is aborted only after a consonant or at the
1003 ;; word end. If len is not a number, string is returned unchanged.
1004 (let* ((string-length (length string
))
1005 (len (if (numberp len
)
1006 (min len string-length
)
1008 (return-string (if (numberp len
)
1009 (substring string
0 len
)))
1011 (vowels '(?a ?e ?i ?o ?u ?A ?E ?I ?O ?U
)))
1015 (< index string-length
)
1016 (member (elt return-string
1017 (1- (length return-string
)))
1019 (setq return-string
(concat return-string
1021 string index
(1+ index
)))
1026 (defun bibtex-generate-autokey ()
1027 "Generates automatically a key from the author/editor and the title field.
1028 The generation algorithm works as follows:
1029 1. If there is a non-empty author (preferred) or editor field,
1030 use it for the name part of the key.
1031 2. Change any substring found in `bibtex-autokey-name-change-strings'
1032 to the corresponding new one (see documentation of this variable
1033 for further detail).
1034 3. For every of the first `bibtex-autokey-names' names in the
1035 \"name\" field, determine the last name.
1036 4. From every last name, take at least `bibtex-autokey-name-length'
1037 characters (abort only after a consonant or at a word end).
1038 5. Build the name part of the key by concatenating all abbreviated last
1039 names with the string `bibtex-autokey-name-separator' between
1041 6. Build the year part of the key by truncating the contents of the
1042 \"year\" field to the rightmost `bibtex-autokey-year-length'
1043 digits (useful values are 2 and 4).
1044 7. For the title part of the key change the contents of the \"title\"
1045 field of the reference according to
1046 `bibtex-autokey-titleword-change-strings' to the corresponding
1047 new one (see documentation of this variable for further detail).
1048 8. Abbreviate the result to the string up to (but not including) the
1049 first occurrence of a regexp matched by the items of
1050 `bibtex-autokey-title-terminators' and delete the first
1051 word if it appears in `bibtex-autokey-titleword-first-ignore'.
1052 Build the title part of the key by using at least the first
1053 `bibtex-autokey-titlewords' capitalized words from this
1054 abbreviated title. If the abbreviated title ends after maximal
1055 `bibtex-autokey-titlewords' + `bibtex-autokey-titlewords-stretch'
1056 capitalized words, all capitalized words from the abbreviated title
1058 9. For every used title word that appears in
1059 `bibtex-autokey-titleword-abbrevs' use the corresponding abbreviation
1060 (see documentation of this variable for further detail).
1061 10. From every title word not generated by an abbreviation, take at
1062 least `bibtex-autokey-titleword-length' characters (abort only after
1063 a consonant or at a word end).
1064 11. Build the title part of the key by concatenating all abbreviated
1065 title words with the string `bibtex-autokey-titleword-separator'
1067 12. At least, to get the key, concatenate the name part, the year part
1068 and the title part with `bibtex-autokey-name-year-separator'
1069 between the name and the year if both are non-empty and
1070 `bibtex-autokey-year-title-separator' between the year and
1071 the title if both are non-empty."
1073 (let* ((pnt (point))
1076 (bibtex-beginning-of-entry)
1080 (bibtex-end-of-entry)
1086 (re-search-forward "^[ \t]*author[ \t]*=" max t
)
1087 (re-search-forward "^[ \t]*editor[ \t]*=" max t
))
1088 (let* (bibtex-help-message
1090 (bibtex-find-text t
)
1093 (bibtex-find-text nil
)
1095 (bibtex-autokey-change
1096 (buffer-substring-no-properties start end
)
1097 bibtex-autokey-name-change-strings
))
1103 (bibtex-autokey-abbrev
1104 (if (string-match "," fullname
)
1105 (substring fullname
0 (match-beginning 0))
1107 (if (string-match " [^ ]*$" fullname
)
1109 fullname
(1+ (match-beginning 0)))
1111 bibtex-autokey-name-length
)))
1112 ;; Gather all names into a list
1116 (not (equal namefield
""))
1118 (not (numberp bibtex-autokey-names
))
1119 (< counter bibtex-autokey-names
)))
1120 (if (string-match " and " namefield
)
1128 namefield
0 (match-beginning 0)))))
1130 (substring namefield
(match-end 0))))
1132 (append names
(list (downcase namefield
)))
1134 (setq counter
(1+ counter
)))
1136 (namepart (mapconcat (function (lambda (name) name
))
1138 bibtex-autokey-name-separator
))
1142 (if (re-search-forward
1143 "^[ \t]*year[ \t]*=[ \t]*\\([0-9]*\\)" max t
)
1144 (buffer-substring-no-properties
1145 (match-beginning 1) (match-end 1))
1148 (if (equal yearfield
"")
1150 (substring yearfield
1151 (- (length yearfield
)
1152 bibtex-autokey-year-length
))))
1154 (let ((case-fold-search t
)
1158 (if (re-search-forward
1159 "^[ \t]*title[ \t]*=" max t
)
1160 (let* (bibtex-help-message
1162 (bibtex-find-text t
)
1165 (bibtex-find-text nil
)
1167 (bibtex-autokey-change
1168 (buffer-substring-no-properties start end
)
1169 bibtex-autokey-titleword-change-strings
))
1174 (length bibtex-autokey-title-terminators
)))
1175 (while (< index numberofitems
)
1177 (elt bibtex-autokey-title-terminators index
)
1180 (substring titlefield
0 (match-beginning 0))))
1181 (setq index
(1+ index
)))
1189 titleword bibtex-autokey-titleword-abbrevs
)))
1192 (bibtex-autokey-abbrev
1194 bibtex-autokey-titleword-length
)))))
1195 ;; Gather all titlewords into a list
1202 (not (equal titlestring
""))
1204 (not (numberp bibtex-autokey-titlewords
))
1206 bibtex-autokey-titlewords
1207 bibtex-autokey-titlewords-stretch
))))
1208 (if (string-match "\\b[A-Z][A-Za-z0-9]*" titlestring
)
1209 (let* ((end-match (match-end 0))
1211 (downcase (substring titlestring
1215 (not (numberp bibtex-autokey-titlewords
))
1216 (< counter bibtex-autokey-titlewords
))
1221 bibtex-autokey-titleword-first-ignore
))
1224 (append titlewords
(list titleword
))))
1227 (append titlewords-extra
(list titleword
))))
1229 (substring titlestring end-match
)))
1230 (setq titlestring
""))
1232 counter
(1+ counter
)))
1233 (if (string-match "\\b[A-Z][^ ]*\\b" titlestring
)
1235 (append titlewords titlewords-extra
)))))
1236 (titlepart (mapconcat (function (lambda (name) name
))
1238 bibtex-autokey-titleword-separator
))
1245 (equal yearpart
"")))
1246 bibtex-autokey-name-year-separator
)
1252 (equal yearpart
""))
1253 (equal titlepart
"")))
1254 bibtex-autokey-year-title-separator
)
1259 (defun bibtex-parse-keys (add &optional abortable
)
1260 ;; Sets bibtex-keys to the keys used in the whole (possibly
1261 ;; restricted) buffer (either as entry keys or as crossref entries).
1262 ;; If ADD is non-nil adds the new keys to bibtex-keys instead of
1263 ;; simply resetting it. If ABORTABLE is non-nil abort on user input.
1264 (if bibtex-maintain-sorted-entries
1265 (let ((labels (if add
1268 (case-fold-search t
))
1270 (goto-char (point-min))
1272 (message "Parsing reference keys..."))
1279 "\\(" bibtex-reference-head
"\\)"
1282 "^[ \t]*crossref[ \t\n]*=[ \t\n]*"
1285 bibtex-reference-key
1286 ;; every valid crossref entry must have the
1287 ;; form of a reference key, so we need no
1288 ;; nesting of brace etc. here
1292 bibtex-reference-key
1302 (if (match-beginning (1+ bibtex-key-in-head
))
1305 (buffer-substring-no-properties
1306 (match-beginning (1+ bibtex-key-in-head
))
1307 (match-end (1+ bibtex-key-in-head
))))
1310 (buffer-substring-no-properties
1311 (1+ (match-beginning (+ 3 bibtex-key-in-head
)))
1312 (1- (match-end (+ 3 bibtex-key-in-head
))))))
1313 (if (not (assoc label labels
))
1315 (cons (list label
) labels
))))))
1318 bibtex-buffer-last-parsed-for-keys-tick
1319 (buffer-modified-tick))
1321 (message "Parsing reference keys... done"))
1322 (setq bibtex-keys labels
)))))))
1324 (defun bibtex-auto-fill-function ()
1325 (let ((fill-prefix (make-string (+ bibtex-text-alignment
1) ?
)))
1329 ;; Interactive Functions:
1332 (defun bibtex-mode ()
1333 "Major mode for editing BibTeX files.
1334 To submit a problem report, enter `\\[bibtex-submit-bug-report]' from a
1335 bibtex-mode buffer. This automatically sets up a mail buffer with
1336 version information already added. You just need to add a description
1337 of the problem, including a reproducable test case and send the
1342 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
1344 The optional fields start with the string OPT, and thus ignored by BibTeX.
1345 The OPT string may be removed from a field with \\[bibtex-remove-OPT].
1346 \\[bibtex-kill-optional-field] kills the current optional field entirely.
1347 \\[bibtex-remove-double-quotes-or-braces] removes the double-quotes or
1348 braces around the text of the current field. \\[bibtex-empty-field]
1349 replaces the text of the current field with the default \"\" or {}.
1351 The command \\[bibtex-clean-entry] cleans the current entry, i.e. (i) removes
1352 double-quotes or braces from entirely numerical fields, (ii) removes
1353 OPT from all non-empty optional fields, (iii) removes all empty
1354 optional fields, and (iv) checks that no non-optional fields are empty.
1356 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
1357 Use \\[bibtex-next-field] to move to end of the next field.
1359 The following may be of interest as well:
1363 bibtex-print-help-message
1364 bibtex-beginning-of-entry
1366 bibtex-ispell-abstract
1367 bibtex-narrow-to-entry
1368 bibtex-hide-entry-bodies
1370 bibtex-validate-buffer
1373 bibtex-complete-string
1376 bibtex-field-left-delimiter
1377 bibtex-field-right-delimiter
1378 bibtex-include-OPTcrossref
1379 bibtex-include-OPTkey
1380 bibtex-include-OPTannote
1381 bibtex-mode-user-optional-fields
1382 bibtex-clean-entry-zap-empty-opts
1383 bibtex-sort-ignore-string-entries
1384 bibtex-maintain-sorted-entries
1385 bibtex-entry-field-alist
1386 bibtex-predefined-strings
1389 ---------------------------------------------------------
1390 Entry to this mode calls the value of bibtex-mode-hook if that value is
1393 (kill-all-local-variables)
1394 (use-local-map bibtex-mode-map
)
1395 (setq major-mode
'bibtex-mode
)
1396 (setq mode-name
"BibTeX")
1397 (set-syntax-table bibtex-mode-syntax-table
)
1398 (setq bibtex-completion-candidates bibtex-predefined-strings
)
1402 ;; collect pathnames
1403 (let* ((bib (getenv "BIBINPUTS"))
1410 (lambda (dirname) ;; strips off trailing slashes
1411 (let ((len (length dirname
)))
1412 (if (equal (elt dirname
(1- len
)) "/")
1413 (substring dirname
0 (1- (1- len
)))
1416 (while (string-match ":" path
)
1421 (1- (match-end 0)))))
1422 path
(substring path
(match-end 0))))
1423 (append actdirs
(list path
)))))
1424 (filename (if (string-match "\.bib$" filename
)
1426 (concat filename
".bib")))
1429 (size (length dirs
)))
1433 (not (file-readable-p
1435 (concat (elt dirs item
) "/" filename
)))))
1436 (setq item
(1+ item
)))
1439 (let ((curbuf (current-buffer))
1440 (bufname (make-temp-name ""))
1441 (compl bibtex-completion-candidates
))
1442 (create-file-buffer bufname
)
1443 (set-buffer bufname
)
1444 (insert-file-contents fullfilename
)
1445 (goto-char (point-min))
1446 (while (re-search-forward bibtex-string nil t
)
1452 (list (buffer-substring-no-properties
1453 (match-beginning bibtex-key-in-string
)
1454 (match-end bibtex-key-in-string
)))))))
1455 (kill-buffer bufname
)
1457 (setq bibtex-completion-candidates compl
))
1458 (error "File %s not in $BIBINPUTS paths" filename
)))))
1459 bibtex-string-files
)
1465 bibtex-maintain-sorted-entries
1466 (eq major-mode
'bibtex-mode
)
1468 (eq (buffer-modified-tick)
1469 bibtex-buffer-last-parsed-for-keys-tick
)))
1470 (bibtex-parse-keys nil t
)))))
1471 (bibtex-parse-keys nil
)
1472 (make-local-variable 'paragraph-start
)
1473 (setq paragraph-start
"[ \f\n\t]*$")
1474 (make-local-variable 'comment-start
)
1475 (setq comment-start
"%")
1477 (setq auto-fill-function
'bibtex-auto-fill-function
)
1478 (set (make-local-variable 'font-lock-defaults
)
1479 '(bibtex-font-lock-keywords
1481 ;; Mathematical expressions should be fontified as strings
1483 ;; Quotes are field delimiters and quote-delimited
1484 ;; entries should be fontified in the same way as
1485 ;; brace-delimited ones
1487 (run-hooks 'bibtex-mode-hook
))
1489 (defun bibtex-submit-bug-report ()
1490 "Submit via mail a bug report on bibtex.el."
1492 (if (y-or-n-p "Do you want to submit a bug report on BibTeX mode? ")
1495 (let ((reporter-prompt-for-summary-p t
))
1496 (reporter-submit-bug-report
1497 bibtex-maintainer-address
1500 'system-configuration
1501 'system-configuration-options
1502 'bibtex-sort-ignore-string-entries
1503 'bibtex-maintain-sorted-entries
1504 'bibtex-field-left-delimiter
1505 'bibtex-field-right-delimiter
1506 ;; Possible sorting and parsing bugs
1507 'bibtex-mode-user-optional-fields
1508 ;; Possible format error
1509 'bibtex-predefined-strings
1510 'bibtex-string-files
1511 ;; Possible format error
1512 'bibtex-font-lock-keywords
1513 ;; Possible bugs regarding fontlocking
1514 'bibtex-autokey-names
1515 'bibtex-autokey-name-change-strings
1516 'bibtex-autokey-name-length
1517 'bibtex-autokey-name-separator
1518 'bibtex-autokey-year-length
1519 'bibtex-autokey-titlewords
1520 'bibtex-autokey-title-terminators
1521 'bibtex-autokey-titlewords-stretch
1522 'bibtex-autokey-titleword-first-ignore
1523 'bibtex-autokey-titleword-abbrevs
1524 'bibtex-autokey-titleword-change-strings
1525 'bibtex-autokey-titleword-length
1526 'bibtex-autokey-titleword-separator
1527 'bibtex-autokey-name-year-separator
1528 'bibtex-autokey-year-title-separator
1529 'bibtex-autokey-edit-before-use
1530 ;; Possible bugs regarding automatic labels
1531 'bibtex-entry-field-alist
1532 ;; Possible format error
1533 'bibtex-help-message
1534 'bibtex-include-OPTcrossref
1535 'bibtex-include-OPTkey
1536 'bibtex-include-OPTannote
1537 'bibtex-clean-entry-zap-empty-opts
1538 ;; User variables which shouldn't cause any errors
1543 I want to report a bug on Emacs BibTeX mode.
1544 I've read the `Bugs' section in the `Emacs' info page, so I know how
1545 to make a clear and unambiguous report. I have started a fresh Emacs
1546 via `"invocation-name
" --no-init-file --no-site-file', thereafter (in
1547 case I'm reporting on a version of `bibtex.el' which is not part of
1548 the standard emacs distribution) I loaded the questionable version
1549 of `bibtex.el' with `M-x load-file', and then, to produce the buggy
1550 behaviour, I did the following:")))
1553 (defun bibtex-entry (entry-type &optional required optional
)
1554 "Inserts a new BibTeX entry.
1555 Calls the value of bibtex-add-entry-hook if that value is non-nil."
1556 (interactive (let* ((completion-ignore-case t
)
1557 (e-t (completing-read
1559 bibtex-entry-field-alist
1562 (if (and (null required
) (null optional
))
1563 (let* ((e (assoc-ignore-case entry-type bibtex-entry-field-alist
))
1567 (error "Bibtex entry type %s not defined!" entry-type
))
1569 (member entry-type bibtex-include-OPTcrossref
)
1571 (setq required
(elt c-ref
0)
1572 optional
(elt c-ref
1))
1573 (setq required
(elt r-n-o
0)
1574 optional
(elt r-n-o
1)))))
1576 (if bibtex-maintain-sorted-entries
1578 (format "%s key: " entry-type
)
1580 (if bibtex-maintain-sorted-entries
1581 (bibtex-find-entry-location key
)
1582 (bibtex-move-outside-of-entry))
1583 (insert "@" entry-type
"{")
1587 (mapcar 'bibtex-make-field required
)
1588 (if (member entry-type bibtex-include-OPTcrossref
)
1589 (bibtex-make-optional-field '("crossref")))
1590 (if bibtex-include-OPTkey
1591 (bibtex-make-optional-field '("key")))
1592 (mapcar 'bibtex-make-optional-field optional
)
1593 (mapcar 'bibtex-make-optional-field
1594 bibtex-mode-user-optional-fields
)
1595 (if bibtex-include-OPTannote
1596 (bibtex-make-optional-field '("annote")))
1598 (bibtex-next-field t
)
1599 (run-hooks 'bibtex-add-entry-hook
)))
1601 (defun bibtex-print-help-message ()
1602 "Prints helpful information about current field in current BibTeX entry."
1604 (let* ((pnt (point))
1608 (condition-case errname
1609 (bibtex-enclosing-regexp bibtex-field
)
1612 (error "Not on BibTeX field")))
1613 (let ((mb (match-beginning bibtex-name-in-field
))
1614 (me (match-end bibtex-name-in-field
)))
1616 (buffer-substring-no-properties
1617 (if (looking-at "OPT")
1624 bibtex-reference-maybe-empty-head nil t
)
1625 (buffer-substring-no-properties
1626 (1+ (match-beginning bibtex-type-in-head
))
1627 (match-end bibtex-type-in-head
))))
1629 (assoc-ignore-case reference-type
1630 bibtex-entry-field-alist
))
1631 (c-r-list (elt entry-list
2))
1634 (member reference-type bibtex-include-OPTcrossref
)
1637 (elt entry-list
1)))
1638 (list-of-entries (append
1639 (elt req-opt-list
0)
1640 (elt req-opt-list
1)
1641 bibtex-mode-user-optional-fields
1644 bibtex-include-OPTcrossref
)
1646 "Label of the crossreferenced entry")))
1647 (if bibtex-include-OPTannote
1649 "Personal annotation (ignored)")))
1650 (if bibtex-include-OPTkey
1652 "Key used for label creation if author and editor fields are missing"))))))
1654 (let ((comment (assoc-ignore-case field-name list-of-entries
)))
1656 (message (elt comment
1))
1657 (message "NO COMMENT AVAILABLE")))))
1659 (defun bibtex-make-field (e-t)
1660 "Makes a field named E-T in current BibTeX entry."
1661 (interactive "sBibTeX field name: ")
1662 (let ((name (if (consp e-t
)
1667 (bibtex-find-text nil
)
1668 (if (looking-at "[}\"]")
1671 (indent-to-column bibtex-name-alignment
)
1673 (indent-to-column bibtex-text-alignment
)
1674 (insert bibtex-field-left-delimiter bibtex-field-right-delimiter
)
1676 (forward-char -
1))))
1678 (defun bibtex-make-optional-field (e-t)
1679 "Makes an optional field named E-T in current BibTeX entry."
1681 (setq e-t
(cons (concat "OPT" (car e-t
)) (cdr e-t
)))
1682 (setq e-t
(concat "OPT" e-t
)))
1683 (bibtex-make-field e-t
))
1685 (defun bibtex-beginning-of-entry ()
1686 "Move to beginning of BibTeX entry.
1687 If inside an entry, move to the beginning of it, otherwise move to the
1688 beginning of the previous entry."
1690 (if (looking-at "^@")
1692 (re-search-backward "^@" nil
'move
))
1694 (defun bibtex-end-of-entry ()
1695 "Move to end of BibTeX entry.
1696 If inside an entry, move to the end of it, otherwise move to the end
1697 of the previous entry."
1699 (bibtex-beginning-of-entry)
1700 (let ((parse-sexp-ignore-comments t
))
1701 (forward-sexp 2) ;; skip entry type and body
1704 (defun bibtex-ispell-entry ()
1705 "Spell whole BibTeX entry."
1707 (ispell-region (progn (bibtex-beginning-of-entry) (point))
1708 (progn (bibtex-end-of-entry) (point))))
1710 (defun bibtex-ispell-abstract ()
1711 "Spell abstract of BibTeX entry."
1713 (let ((pnt (bibtex-end-of-entry)))
1714 (bibtex-beginning-of-entry)
1716 (re-search-forward "^[ \t]*[OPT]*abstract[ \t]*=" pnt
))
1717 (error "No abstract in entry.")))
1718 (ispell-region (point)
1719 (save-excursion (forward-sexp) (point))))
1721 (defun bibtex-narrow-to-entry ()
1722 "Narrow buffer to current BibTeX entry."
1725 (narrow-to-region (progn (bibtex-beginning-of-entry) (point))
1726 (progn (bibtex-end-of-entry) (point)))))
1728 (defun bibtex-hide-entry-bodies (&optional arg
)
1729 "Hide all lines between first and last BibTeX entries not beginning with @.
1730 With argument, show all text."
1733 (beginning-of-first-bibtex-entry)
1734 ;; subst-char-in-region modifies the buffer, despite what the
1735 ;; documentation says...
1736 (let ((modifiedp (buffer-modified-p))
1737 (buffer-read-only nil
))
1739 (subst-char-in-region (point) (point-max) ?
\r ?
\n t
)
1740 (while (save-excursion (re-search-forward "\n[^@]" (point-max) t
))
1741 ;; (save-excursion (replace-regexp "\n\\([^@]\\)" "\r\\1"))
1743 (while (re-search-forward "\n\\([^@]\\)" nil t
)
1744 (replace-match "\r\\1" nil nil
)))))
1745 (setq selective-display
(not arg
))
1746 (set-buffer-modified-p modifiedp
))))
1748 (defun bibtex-sort-entries ()
1749 "Sort BibTeX entries alphabetically by key.
1750 Text outside of BibTeX entries is not affected. If
1751 bibtex-sort-ignore-string-entries is non-nil, @string entries will be
1755 (beginning-of-first-bibtex-entry)
1759 (goto-char (point-max))
1760 (bibtex-end-of-entry)
1762 (if bibtex-sort-ignore-string-entries
1763 (if (re-search-forward bibtex-reference nil
'move
)
1764 (goto-char (match-beginning 0))))
1770 (if bibtex-sort-ignore-string-entries
1771 (if (re-search-forward bibtex-reference nil
'move
)
1772 (goto-char (match-beginning 0)))
1773 (if (re-search-forward bibtex-reference-head nil
'move
)
1774 (goto-char (match-beginning 0))))))
1776 'bibtex-end-of-entry
1777 ;; STARTKEY function
1780 (if bibtex-sort-ignore-string-entries
1782 (re-search-forward bibtex-reference
)
1783 (buffer-substring-no-properties
1784 (match-beginning bibtex-key-in-reference
)
1785 (match-end bibtex-key-in-reference
)))
1786 (re-search-forward bibtex-reference-head
)
1787 (buffer-substring-no-properties
1788 (match-beginning bibtex-key-in-head
)
1789 (match-end bibtex-key-in-head
)))))
1793 (defun bibtex-find-entry-location (entry-name &optional ignore-dups
)
1794 "Looking for place to put the BibTeX entry named ENTRY-NAME.
1795 Performs a binary search (therefore, buffer is assumed to be in sorted
1796 order, without duplicates (see \\[bibtex-validate-buffer]), if it is
1797 not, bibtex-find-entry-location will fail). If entry-name is already
1798 used as a reference key, an error is signaled. However, if optional
1799 variable IGNORE-DUPS is non-nil, no error messages about duplicate
1800 entries are signaled, but the error handling is assumed to be made in
1801 the calling function. Nil is returned, if an duplicate entry error
1802 occurred, and t in all other cases."
1805 (beginning-of-first-bibtex-entry)
1806 (if bibtex-sort-ignore-string-entries
1807 (re-search-forward bibtex-reference nil
`move
)
1808 (bibtex-end-of-entry))
1812 (goto-char (point-max))
1813 (if bibtex-sort-ignore-string-entries
1814 (re-search-backward bibtex-reference nil
`move
)
1815 (bibtex-beginning-of-entry))
1819 (done (>= left right
))
1823 (setq actual-point
(/ (+ left right
) 2))
1824 (goto-char actual-point
)
1825 (bibtex-beginning-of-entry)
1827 (if bibtex-sort-ignore-string-entries
1829 (re-search-forward bibtex-reference
)
1830 (buffer-substring-no-properties
1831 (match-beginning bibtex-key-in-reference
)
1832 (match-end bibtex-key-in-reference
)))
1833 (re-search-forward bibtex-reference-head
)
1834 (buffer-substring-no-properties
1835 (match-beginning bibtex-key-in-head
)
1836 (match-end bibtex-key-in-head
))))
1838 ((string-lessp entry-name actual-key
)
1839 (setq new
(match-beginning 0))
1840 (if (equal right new
)
1843 ((string-lessp actual-key entry-name
)
1844 (setq new
(match-end 0))
1845 (if (equal left new
)
1848 ((string-equal actual-key entry-name
)
1851 (if (not ignore-dups
)
1852 (error "Entry with key `%s' already exists!" entry-name
)))))
1856 (if (re-search-forward bibtex-reference nil t
)
1859 (buffer-substring-no-properties
1860 (match-beginning bibtex-key-in-reference
)
1861 (match-end bibtex-key-in-reference
)))
1862 (if (string-lessp actual-key entry-name
)
1863 ;; even greater than last entry --> we must append
1865 (goto-char (match-end 0))
1866 (newline (forward-line 2))
1867 (beginning-of-line))
1868 (goto-char right
))))
1871 (defun bibtex-validate-buffer (&optional from-point
)
1872 "Validate if the current BibTeX buffer is syntactically correct.
1873 Any garbage (e.g. comments) before the first \"@\" is not tested (so
1874 you can put comments here).
1875 With non-nil FROM-POINT it starts with entry enclosing point."
1881 (bibtex-beginning-of-entry)
1882 (beginning-of-first-bibtex-entry))
1884 ;; looking if entries fit syntactical structure
1885 (goto-char starting-point
)
1886 (while (re-search-forward "^@" nil t
)
1890 (looking-at "@string")
1891 (looking-at "@preamble"))
1894 (re-search-forward bibtex-reference nil t
)
1895 (equal p
(match-beginning 0))))
1898 (error "Bad entry begins here"))))))
1899 ;; looking if entries are balanced (a single non-escaped quote
1900 ;; inside braces is not detected by the former check, but
1901 ;; bibtex-sort-entries stumbles about it
1902 (goto-char starting-point
)
1906 (bibtex-beginning-of-entry)
1908 ;; looking for correct sort order and duplicates
1909 (if bibtex-maintain-sorted-entries
1912 (goto-char starting-point
)
1916 (cond ((or (null previous
)
1917 (string< previous current
))
1918 (setq previous current
1920 ((string-equal previous current
)
1921 (error "Duplicate here with previous!"))
1923 (error "Entries out of order here!"))))))))
1926 (message "Part of BibTeX buffer starting at point is syntactically correct")
1927 (message "BibTeX buffer is syntactically correct"))))
1929 (defun bibtex-next-field (arg)
1930 "Finds end of text of next BibTeX field; with arg, to its beginning."
1932 (bibtex-inside-field)
1933 (let ((start (point)))
1936 (bibtex-enclosing-field)
1937 (goto-char (match-end 0))
1943 (bibtex-find-text arg
))
1945 (defun bibtex-find-text (arg)
1946 "Go to end of text of current field; with arg, go to beginning."
1948 (bibtex-inside-field)
1949 (bibtex-enclosing-field)
1952 (goto-char (match-beginning bibtex-text-in-field
))
1953 (if (looking-at "[{\"]")
1955 (goto-char (match-end bibtex-text-in-field
))
1957 (= (preceding-char) ?
})
1958 (= (preceding-char) ?
\"))
1960 (if bibtex-help-message
1961 (bibtex-print-help-message)))
1963 (defun bibtex-remove-OPT ()
1964 "Removes the 'OPT' starting optional arguments and goes to end of text."
1966 (bibtex-inside-field)
1967 (bibtex-enclosing-field)
1969 (goto-char (match-beginning bibtex-name-in-field
))
1970 (if (looking-at "OPT")
1971 ;; sct@dcs.edinburgh.ac.uk
1973 (delete-char (length "OPT"))
1974 (search-forward "=")
1975 (delete-horizontal-space)
1976 (indent-to-column bibtex-text-alignment
))))
1977 (bibtex-inside-field))
1979 (defun bibtex-remove-double-quotes-or-braces ()
1980 "Removes \"\" or {} around string."
1983 (bibtex-inside-field)
1984 (bibtex-enclosing-field)
1985 (let ((start (match-beginning bibtex-text-in-field
))
1986 (stop (match-end bibtex-text-in-field
)))
1988 (while (re-search-forward bibtex-field-string stop t
)
1989 (let ((beg (match-beginning 0))
1990 (end (match-end 0)))
1993 (if (looking-at "[}\"]")
1996 (if (looking-at "[{\"]")
1997 (delete-char 1)))))))
1999 (defun bibtex-kill-optional-field ()
2000 "Kill the entire enclosing optional BibTeX field."
2002 (bibtex-inside-field)
2003 (bibtex-enclosing-field)
2004 (goto-char (match-beginning bibtex-name-in-field
))
2005 (let ((the-end (match-end 0))
2006 (the-beginning (match-beginning 0)))
2007 (if (looking-at "OPT")
2010 (skip-chars-forward " \t\n,")
2011 (kill-region the-beginning the-end
))
2012 (error "Mandatory fields can't be killed"))))
2014 (defun bibtex-empty-field ()
2015 "Delete the text part of the current field, replace with empty text."
2017 (bibtex-inside-field)
2018 (bibtex-enclosing-field)
2019 (goto-char (match-beginning bibtex-text-in-field
))
2020 (kill-region (point) (match-end bibtex-text-in-field
))
2021 (insert (concat bibtex-field-left-delimiter
2022 bibtex-field-right-delimiter
))
2023 (bibtex-find-text t
))
2025 (defun bibtex-pop (arg direction
)
2026 ;; generic function to be used by bibtex-pop-previous and bibtex-pop-next
2027 (let (bibtex-help-message)
2028 (bibtex-find-text nil
))
2030 ;; parse current field
2031 (bibtex-inside-field)
2032 (bibtex-enclosing-field)
2033 (let ((start-old-text (match-beginning bibtex-text-in-field
))
2034 (stop-old-text (match-end bibtex-text-in-field
))
2035 (start-name (match-beginning bibtex-name-in-field
))
2036 (stop-name (match-end bibtex-name-in-field
))
2038 (goto-char start-name
)
2039 ;; construct regexp for field with same name as this one,
2040 ;; ignoring possible OPT's
2041 (let ((matching-entry
2043 (buffer-substring-no-properties (if (looking-at "OPT")
2044 (+ (point) (length "OPT"))
2047 bibtex-field-text
)))
2048 ;; if executed several times in a row, start each search where
2049 ;; the last one was finished
2050 (cond ((eq last-command
'bibtex-pop
)
2054 (bibtex-enclosing-reference-maybe-empty-head)
2055 (setq bibtex-pop-previous-search-point
(point))
2056 (setq bibtex-pop-next-search-point
(match-end 0))))
2057 (if (eq direction
'previous
)
2058 (goto-char bibtex-pop-previous-search-point
)
2059 (goto-char bibtex-pop-next-search-point
))
2060 ;; Now search for arg'th previous/next similar field
2062 ((if (eq direction
'previous
)
2063 (re-search-backward matching-entry
(point-min) t arg
)
2064 (re-search-forward matching-entry
(point-max) t arg
))
2065 ;; Found a matching field. Remember boundaries.
2066 (setq bibtex-pop-previous-search-point
(match-beginning 0))
2067 (setq bibtex-pop-next-search-point
(match-end 0))
2069 (buffer-substring-no-properties
2070 (match-beginning bibtex-text-in-field
)
2071 (match-end bibtex-text-in-field
)))
2072 ;; change delimiters, if any changes needed
2078 (if (equal bibtex-field-left-delimiter
"{")
2087 (while (string-match bibtex-field-string new-text start
)
2088 (let ((beg (match-beginning 0))
2089 (end (1- (match-end 0))))
2091 (eq (aref new-text beg
) old-open
)
2092 (eq (aref new-text end
) old-close
))
2094 (aset new-text beg new-open
)
2095 (aset new-text end new-close
))))
2096 (setq start
(match-end 0))))
2098 ;; Go back to where we started, delete old text, and pop new.
2099 (goto-char stop-old-text
)
2100 (delete-region start-old-text stop-old-text
)
2104 (error (concat "No "
2105 (if (eq direction
'previous
)
2108 " matching BibTeX field.")))))))
2109 (let (bibtex-help-message)
2110 (bibtex-find-text nil
))
2111 (setq this-command
'bibtex-pop
))
2113 (defun bibtex-pop-previous (arg)
2114 "Replace text of current field with the text of similar field in previous entry.
2115 With arg, goes up ARG entries. Repeated, goes up so many times. May be
2116 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
2118 (bibtex-pop arg
'previous
))
2120 (defun bibtex-pop-next (arg)
2121 "Replace text of current field with the text of similar field in next entry.
2122 With arg, goes down ARG entries. Repeated, goes down so many times. May be
2123 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
2125 (bibtex-pop arg
'next
))
2127 (defun bibtex-clean-entry (&optional arg
)
2128 "Finish editing the current BibTeX entry and clean it up.
2129 For all optional fields of current BibTeX entry: if empty, kill the
2130 whole field; otherwise, remove the \"OPT\" string in the name; if text
2131 numerical, remove double-quotes. For all mandatory fields: if empty,
2132 signal error. If label of entry is empty or a prefix argument was
2133 given, calculate a new entry label."
2135 (bibtex-beginning-of-entry)
2136 (let ((start (point))
2139 (narrow-to-region start
(save-excursion (bibtex-end-of-entry) (point)))
2141 (re-search-forward bibtex-field
(point-max) t
1)
2142 (not crossref-there
))
2143 ;; determine if reference has crossref entry
2144 (let ((begin-name (match-beginning bibtex-name-in-field
))
2145 (begin-text (match-beginning bibtex-text-in-field
)))
2146 (goto-char begin-name
)
2147 (if (looking-at "\\(OPTcrossref\\)\\|\\(crossref\\)")
2149 (goto-char begin-text
)
2150 (if (not (looking-at "\\(\"\"\\)\\|\\({}\\)"))
2151 (setq crossref-there t
))))))
2152 (bibtex-enclosing-reference-maybe-empty-head)
2153 (re-search-forward bibtex-reference-type
)
2154 (let ((begin-type (1+ (match-beginning 0)))
2155 (end-type (match-end 0)))
2157 (while (re-search-forward bibtex-field
(point-max) t
1)
2158 (let ((begin-field (match-beginning 0))
2159 (end-field (match-end 0))
2160 (begin-name (match-beginning bibtex-name-in-field
))
2161 (end-name (match-end bibtex-name-in-field
))
2162 (begin-text (match-beginning bibtex-text-in-field
))
2163 (end-text (match-end bibtex-text-in-field
))
2165 (goto-char begin-name
)
2168 bibtex-clean-entry-zap-empty-opts
)
2169 (goto-char begin-text
)
2170 (if (looking-at "\\(\"\"\\)\\|\\({}\\)")
2171 ;; empty: delete whole field if really optional
2172 ;; (missing crossref handled) or complain
2174 (not crossref-there
)
2177 (buffer-substring-no-properties
2178 (+ (length "OPT") begin-name
) end-name
))
2181 (buffer-substring-no-properties
2182 begin-type end-type
)
2183 bibtex-entry-field-alist
))))))
2184 ;; field is not really optional
2186 (goto-char begin-name
)
2187 (delete-char (length "OPT"))
2188 ;; make field non-OPT
2189 (search-forward "=")
2190 (delete-horizontal-space)
2191 (indent-to-column bibtex-text-alignment
)
2193 ;; and loop to go through next test
2194 (error "Mandatory field ``%s'' is empty"
2195 (buffer-substring-no-properties
2198 ;; field is optional
2199 (delete-region begin-field end-field
))
2200 ;; otherwise: not empty, delete "OPT"
2201 (goto-char begin-name
)
2202 (delete-char (length "OPT"))
2204 ;; fixup alignment. [alarson:19920309.2047CST]
2205 (search-forward "=")
2206 (delete-horizontal-space)
2207 (indent-to-column bibtex-text-alignment
))
2208 (goto-char begin-field
)
2209 ;; and loop to go through next test
2212 (goto-char begin-text
)
2213 (cond ((looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")
2215 (goto-char end-text
)
2217 (goto-char begin-text
)
2219 ;; delete enclosing delimiters
2220 (goto-char end-field
)
2221 ;; go to end for next search
2223 ;; to compensate for the 2 delimiters deleted
2225 ((looking-at "\\(\"\"\\)\\|\\({}\\)")
2226 ;; if empty field, complain
2228 (if (not (or (equal (buffer-substring-no-properties
2232 (equal (buffer-substring-no-properties
2236 (error "Mandatory field ``%s'' is empty"
2237 (buffer-substring-no-properties
2238 begin-name end-name
))))
2240 (goto-char end-field
)))))))))
2242 (bibtex-end-of-entry))
2244 (bibtex-end-of-entry)
2247 (bibtex-beginning-of-entry)
2248 (if (re-search-forward
2249 bibtex-reference-head eob t
)
2250 (buffer-substring-no-properties
2251 (match-beginning bibtex-key-in-head
)
2252 (match-end bibtex-key-in-head
))))))
2258 (if bibtex-autokey-edit-before-use
2259 (read-from-minibuffer "Key to use: "
2260 (bibtex-generate-autokey))
2261 (bibtex-generate-autokey))))
2262 (bibtex-beginning-of-entry)
2263 (re-search-forward bibtex-reference-maybe-empty-head
)
2264 (if (match-beginning bibtex-key-in-head
)
2265 (delete-region (match-beginning bibtex-key-in-head
)
2266 (match-end bibtex-key-in-head
)))
2269 (bibtex-beginning-of-entry)
2272 (bibtex-end-of-entry)
2273 (re-search-forward "^@" nil
'move
)
2277 (kill-region start end
)
2280 (not bibtex-maintain-sorted-entries
)
2281 (bibtex-find-entry-location autokey t
))))
2283 (setq kill-ring
(cdr kill-ring
))
2285 (bibtex-beginning-of-entry)
2286 (re-search-forward bibtex-reference-head
)
2289 "New inserted reference may be a duplicate."))))))))
2291 (let ((start (progn (bibtex-beginning-of-entry) (point)))
2292 (end (progn (bibtex-end-of-entry) (point))))
2294 (narrow-to-region start end
)
2295 (bibtex-parse-keys t
)))))
2297 (defun bibtex-complete-string ()
2298 "Complete word fragment before point to longest prefix of a defined string.
2299 If point is not after the part of a word, all strings are listed."
2301 (let* ((end (point))
2302 (beg (save-excursion
2303 (re-search-backward "[ \t{\"]")
2306 (part-of-word (buffer-substring-no-properties beg end
))
2307 (string-list (copy-sequence bibtex-completion-candidates
))
2308 (case-fold-search t
)
2309 (completion (save-excursion
2310 (while (re-search-backward
2311 bibtex-string
(point-min) t
)
2315 (buffer-substring-no-properties
2316 (match-beginning bibtex-key-in-string
)
2317 (match-end bibtex-key-in-string
)))
2325 (try-completion part-of-word string-list
))))
2326 (cond ((eq completion t
)
2327 ;; remove double-quotes or braces if field is no concatenation
2329 (bibtex-inside-field)
2330 (bibtex-enclosing-field)
2331 (let ((end (match-end bibtex-text-in-field
)))
2332 (goto-char (match-beginning bibtex-text-in-field
))
2334 (looking-at bibtex-field-string
)
2335 (equal (match-end 0) end
))
2336 (bibtex-remove-double-quotes-or-braces)))))
2338 (error "Can't find completion for \"%s\"" part-of-word
))
2339 ((not (string= part-of-word completion
))
2340 (delete-region beg end
)
2342 (if (assoc completion string-list
)
2343 ;; remove double-quotes or braces if field is no concatenation
2345 (bibtex-inside-field)
2346 (bibtex-enclosing-field)
2347 (let ((end (match-end bibtex-text-in-field
)))
2348 (goto-char (match-beginning bibtex-text-in-field
))
2350 (looking-at bibtex-field-string
)
2351 (equal (match-end 0) end
))
2352 (bibtex-remove-double-quotes-or-braces))))))
2354 (message "Making completion list...")
2355 (let ((list (all-completions part-of-word string-list
)))
2356 (with-output-to-temp-buffer "*Completions*"
2357 (display-completion-list list
)))
2358 (message "Making completion list...done")))))
2360 (defun bibtex-Article ()
2362 (bibtex-entry "Article"))
2364 (defun bibtex-Book ()
2366 (bibtex-entry "Book"))
2368 (defun bibtex-Booklet ()
2370 (bibtex-entry "Booklet"))
2372 (defun bibtex-InBook ()
2374 (bibtex-entry "InBook"))
2376 (defun bibtex-InCollection ()
2378 (bibtex-entry "InCollection"))
2380 (defun bibtex-InProceedings ()
2382 (bibtex-entry "InProceedings"))
2384 (defun bibtex-Manual ()
2386 (bibtex-entry "Manual"))
2388 (defun bibtex-MastersThesis ()
2390 (bibtex-entry "MastersThesis"))
2392 (defun bibtex-Misc ()
2394 (bibtex-entry "Misc"))
2396 (defun bibtex-PhdThesis ()
2398 (bibtex-entry "PhdThesis"))
2400 (defun bibtex-Proceedings ()
2402 (bibtex-entry "Proceedings"))
2404 (defun bibtex-TechReport ()
2406 (bibtex-entry "TechReport"))
2408 (defun bibtex-Unpublished ()
2410 (bibtex-entry "Unpublished"))
2412 (defun bibtex-string ()
2414 (bibtex-move-outside-of-entry)
2418 bibtex-field-left-delimiter
2419 bibtex-field-right-delimiter
2424 (defun bibtex-preamble ()
2426 (bibtex-move-outside-of-entry)
2427 (insert "@Preamble{}\n")
2432 ;; Make BibTeX a Feature
2436 ;;; bibtex.el ends here