1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Stefan Schoef <schoef@informatik.uni-oldenburg.de>
6 ;; Bengt Martensson <ubrinf!mond!bengt>
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@informatik.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]
48 ;; 3. Quotes inside quote-parenthesized fields (like
49 ;; `author = "Stefan Sch{\"o}f"') break bibtex-validate-buffer.
50 ;; Further, you must use braces here, if you want to set
51 ;; bibtex-maintain-sorted-entries to a non-nil value.
53 ;; (current keeper: schoef@informatik.uni-oldenburg.de
54 ;; previous: alarson@src.honeywell.com)
60 (defvar bibtex-field-left-delimiter
"{"
61 "*Set this to { or \" according to your personal preferences.
62 This variable is buffer local.")
63 (make-variable-buffer-local 'bibtex-field-left-delimiter
)
65 (defvar bibtex-field-right-delimiter
"}"
66 "*Set this to } or \" according to your personal preferences.
67 This variable is buffer local.")
68 (make-variable-buffer-local 'bibtex-field-right-delimiter
)
70 (defvar bibtex-include-OPTcrossref
'("InProceedings" "InCollection")
71 "*All entries listed here will have an OPTcrossref field.")
73 (defvar bibtex-include-OPTkey t
74 "*If non-nil, all entries will have an OPTkey field.")
76 (defvar bibtex-include-OPTannote t
77 "*If non-nil, all entries will have an OPTannote field.")
79 (defvar bibtex-mode-user-optional-fields nil
80 "*List of optional fields the user wants to have always present.
81 Entries should be lists of strings with two elements (first element =
82 name of the field, second element = comment to appear in the echo area).")
84 (defvar bibtex-clean-entry-zap-empty-opts t
85 "*If non-nil, bibtex-clean-entry will delete all empty optional fields.")
87 (defvar bibtex-sort-ignore-string-entries t
88 "*If non-nil, BibTeX @STRING entries are not sort-significant.
89 That means they are ignored when determining ordering of the buffer
90 (e.g. sorting, locating alphabetical position for new entries, etc.).
91 This variable is buffer local.")
92 (make-variable-buffer-local 'bibtex-sort-ignore-string-entries
)
94 (defvar bibtex-maintain-sorted-entries nil
95 "*If non-nil, bibtex-mode maintains all BibTeX entries in sorted order.
96 Setting this variable to nil will strip off some comfort (e.g. TAB
97 completion for reference keys in minibuffer, automatic detection of
98 duplicates) from bibtex-mode. See also bibtex-sort-ignore-string-entries.
99 This variable is buffer local.")
100 (make-variable-buffer-local 'bibtex-maintain-sorted-entries
)
102 (defvar bibtex-entry-field-alist
104 ("Article" .
(((("author" "Author1 [and Author2 ...] [and others]")
105 ("title" "Title of the article (BibTeX converts it to lowercase)")
106 ("journal" "Name of the journal (use string, remove braces)")
107 ("year" "Year of publication"))
108 (("volume" "Volume of the journal")
109 ("number" "Number of the journal")
110 ("month" "Month of the publication as a string (remove braces)")
111 ("pages" "Pages in the journal")
112 ("note" "Remarks to be put at the end of the \\bibitem")))
113 ((("author" "Author1 [and Author2 ...] [and others]")
114 ("title" "Title of the article (BibTeX converts it to lowercase)"))
115 (("journal" "Name of the journal (use string, remove braces)")
116 ("year" "Year of publication")
117 ("volume" "Volume of the journal")
118 ("number" "Number of the journal")
119 ("month" "Month of the publication as a string (remove braces)")
120 ("pages" "Pages in the journal")
121 ("note" "Remarks to be put at the end of the \\bibitem")))))
122 ("Book" .
(((("author" "Author1 [and Author2 ...] [and others]")
123 ("title" "Title of the book")
124 ("publisher" "Publishing company")
125 ("year" "Year of publication"))
126 (("editor" "Editor1 [and Editor2 ...] [and others]")
127 ("volume" "Volume of the book in the series")
128 ("number" "Number of the book in a small series (overwritten by volume)")
129 ("series" "Series in which the book appeared")
130 ("address" "Address of the publisher")
131 ("edition" "Edition of the book as a capitalized English word")
132 ("month" "Month of the publication as a string (remove braces)")
133 ("note" "Remarks to be put at the end of the \\bibitem")))))
134 ("Booklet" .
(((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
135 (("author" "Author1 [and Author2 ...] [and others]")
136 ("howpublished" "The way in which the booklet was published")
137 ("address" "Address of the publisher")
138 ("year" "Year of publication")
139 ("month" "Month of the publication as a string (remove braces)")
140 ("note" "Remarks to be put at the end of the \\bibitem")))))
141 ("InBook" .
(((("author" "Author1 [and Author2 ...] [and others]")
142 ("title" "Title of the book")
143 ("chapter" "Chapter in the book")
144 ("publisher" "Publishing company")
145 ("year" "Year of publication"))
146 (("editor" "Editor1 [and Editor2 ...] [and others]")
147 ("volume" "Volume of the book in the series")
148 ("number" "Number of the book in a small series (overwritten by volume)")
149 ("series" "Series in which the book appeared")
150 ("address" "Address of the publisher")
151 ("edition" "Edition of the book as a capitalized English word")
152 ("month" "Month of the publication as a string (remove braces)")
153 ("pages" "Pages in the book")
154 ("type" "Word to use instead of \"chapter\"")
155 ("note" "Remarks to be put at the end of the \\bibitem")))
156 ((("author" "Author1 [and Author2 ...] [and others]")
157 ("title" "Title of the book")
158 ("chapter" "Chapter in the book"))
159 (("publisher" "Publishing company")
160 ("year" "Year of publication")
161 ("editor" "Editor1 [and Editor2 ...] [and others]")
162 ("volume" "Volume of the book in the series")
163 ("number" "Number of the book in a small series (overwritten by volume)")
164 ("series" "Series in which the book appeared")
165 ("address" "Address of the publisher")
166 ("edition" "Edition of the book as a capitalized English word")
167 ("month" "Month of the publication as a string (remove braces)")
168 ("pages" "Pages in the book")
169 ("type" "Word to use instead of \"chapter\"")
170 ("note" "Remarks to be put at the end of the \\bibitem")))))
171 ("InCollection" .
(((("author" "Author1 [and Author2 ...] [and others]")
172 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
173 ("booktitle" "Name of the book")
174 ("publisher" "Publishing company")
175 ("year" "Year of publication"))
176 (("editor" "Editor1 [and Editor2 ...] [and others]")
177 ("volume" "Volume of the book in the series")
178 ("number" "Number of the book in a small series (overwritten by volume)")
179 ("series" "Series in which the book appeared")
180 ("chapter" "Chapter in the book")
181 ("type" "Word to use instead of \"chapter\"")
182 ("address" "Address of the publisher")
183 ("edition" "Edition of the book as a capitalized English word")
184 ("month" "Month of the publication as a string (remove braces)")
185 ("pages" "Pages in the book")
186 ("note" "Remarks to be put at the end of the \\bibitem")))
187 ((("author" "Author1 [and Author2 ...] [and others]")
188 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
189 ("booktitle" "Name of the book"))
190 (("publisher" "Publishing company")
191 ("year" "Year of publication")
192 ("editor" "Editor1 [and Editor2 ...] [and others]")
193 ("volume" "Volume of the book in the series")
194 ("number" "Number of the book in a small series (overwritten by volume)")
195 ("series" "Series in which the book appeared")
196 ("chapter" "Chapter in the book")
197 ("type" "Word to use instead of \"chapter\"")
198 ("address" "Address of the publisher")
199 ("edition" "Edition of the book as a capitalized English word")
200 ("month" "Month of the publication as a string (remove braces)")
201 ("pages" "Pages in the book")
202 ("note" "Remarks to be put at the end of the \\bibitem")))))
203 ("InProceedings" .
(((("author" "Author1 [and Author2 ...] [and others]")
204 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
205 ("booktitle" "Name of the conference proceedings")
206 ("year" "Year of publication"))
207 (("editor" "Editor1 [and Editor2 ...] [and others]")
208 ("volume" "Volume of the conference proceedings in the series")
209 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
210 ("series" "Series in which the conference proceedings appeared")
211 ("organization" "Sponsoring organization of the conference")
212 ("publisher" "Publishing company, its location")
213 ("address" "Location of the Proceedings")
214 ("month" "Month of the publication as a string (remove braces)")
215 ("pages" "Pages in the conference proceedings")
216 ("note" "Remarks to be put at the end of the \\bibitem")))
217 ((("author" "Author1 [and Author2 ...] [and others]")
218 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
219 ("booktitle" "Name of the conference proceedings"))
220 (("editor" "Editor1 [and Editor2 ...] [and others]")
221 ("volume" "Volume of the conference proceedings in the series")
222 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
223 ("series" "Series in which the conference proceedings appeared")
224 ("year" "Year of publication")
225 ("organization" "Sponsoring organization of the conference")
226 ("publisher" "Publishing company, its location")
227 ("address" "Location of the Proceedings")
228 ("month" "Month of the publication as a string (remove braces)")
229 ("pages" "Pages in the conference proceedings")
230 ("note" "Remarks to be put at the end of the \\bibitem")))))
231 ("Manual" .
(((("title" "Title of the manual"))
232 (("author" "Author1 [and Author2 ...] [and others]")
233 ("organization" "Publishing organization of the manual")
234 ("address" "Address of the organization")
235 ("edition" "Edition of the manual as a capitalized English word")
236 ("year" "Year of publication")
237 ("month" "Month of the publication as a string (remove braces)")
238 ("note" "Remarks to be put at the end of the \\bibitem")))))
240 ("MastersThesis" .
(((("author" "Author1 [and Author2 ...] [and others]")
241 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
242 ("school" "School where the master\'s thesis was written")
243 ("year" "Year of publication"))
244 (("address" "Address of the school (if not part of field \"school\") or country")
245 ("type" "Type of the master\'s thesis")
246 ("month" "Month of the publication as a string (remove braces)")
247 ("note" "Remarks to be put at the end of the \\bibitem")))))
249 (("author" "Author1 [and Author2 ...] [and others]")
250 ("title" "Title of the reference (BibTeX converts it to lowercase)")
251 ("howpublished" "The way in which the reference was published")
252 ("year" "Year of publication")
253 ("month" "Month of the publication as a string (remove braces)")
254 ("note" "Remarks to be put at the end of the \\bibitem")))))
255 ("PhdThesis" .
(((("author" "Author1 [and Author2 ...] [and others]")
256 ("title" "Title of the PhD. thesis")
257 ("school" "School where the PhD. thesis was written")
258 ("year" "Year of publication"))
259 (("address" "Address of the school (if not part of field \"school\") or country")
260 ("type" "Type of the PhD. thesis")
261 ("month" "Month of the publication as a string (remove braces)")
262 ("note" "Remarks to be put at the end of the \\bibitem")))))
263 ("Proceedings" .
(((("title" "Title of the conference proceedings")
264 ("year" "Year of publication"))
265 (("editor" "Editor1 [and Editor2 ...] [and others]")
266 ("volume" "Volume of the conference proceedings in the series")
267 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
268 ("series" "Series in which the conference proceedings appeared")
269 ("publisher" "Publishing company, its location")
270 ("organization" "Sponsoring organization of the conference")
271 ("address" "Location of the Proceedings")
272 ("month" "Month of the publication as a string (remove braces)")
273 ("note" "Remarks to be put at the end of the \\bibitem")))))
274 ("TechReport" .
(((("author" "Author1 [and Author2 ...] [and others]")
275 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
276 ("institution" "Sponsoring institution of the report")
277 ("year" "Year of publication"))
278 (("type" "Type of the report (if other than \"technical report\")")
279 ("number" "Number of the technical report")
280 ("address" "Address of the institution (if not part of field \"institution\") or country")
281 ("month" "Month of the publication as a string (remove braces)")
282 ("note" "Remarks to be put at the end of the \\bibitem")))))
283 ("Unpublished" .
(((("author" "Author1 [and Author2 ...] [and others]")
284 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)")
285 ("note" "Remarks to be put at the end of the \\bibitem"))
286 (("year" "Year of publication")
287 ("month" "Month of the publication as a string (remove braces)")))))
290 "Defines reference types and their associated fields.
292 (entry-name (required optional) (crossref-required crossref-optional))
294 If the third element is nil, the first pair is always to be used.
295 If not, the second pair is to be used in the case of presence of a
296 crossref field and the third in the case of absence.
297 Required , optional, crossref-required and crossref-optional are lists.
298 Each element of these lists is a list of strings with two elements
299 (first element = name of the field,
300 second element = comment to appear in the echo area).")
302 (defvar bibtex-predefined-strings
304 ("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug")
305 ("sep") ("oct") ("nov") ("dec")
306 ("acmcs") ("acta") ("cacm") ("ibmjrd") ("ibmsj") ("ieeese")
307 ("ieeetc") ("ieeetcad") ("ipl") ("jacm") ("jcss") ("scp")
308 ("sicomp") ("tcs") ("tocs") ("tods") ("tog") ("toms") ("toois")
311 "Alist of string definitions.
312 Should contain the strings defined in the BibTeX style files. Each
313 element is a list with just one element: the string.")
315 (defvar bibtex-string-files nil
316 "*List of BibTeX files containing string definitions.
317 Those files must be specified using pathnames relative to the
318 directories specified in $BIBINPUTS. This variable is only evaluated
319 when bibtex-mode is entered (i. e. when loading the BibTeX file).")
321 (defvar bibtex-help-message t
322 "*If not nil print help messages in the echo area on entering a new field.")
324 (defvar bibtex-autokey-names
1
325 "*Number of names to use for the automatically generated reference key.
326 If this is set to anything but a number, all names are used.
327 See the documentation of function bibtex-generate-autokey for further detail.")
329 (defvar bibtex-autokey-name-change-strings
330 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
332 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
334 "Alist of (old-regexp new-string) pairs.
335 Any part of name matching a old-regexp is replaced by new-string.
336 Case of the old-regexp is significant. All regexps are tried in the
337 order in which they appear in the list, so be sure to avoid recursion here.
338 See the documentation of function bibtex-generate-autokey for further detail.")
340 (defvar bibtex-autokey-name-length
'infty
341 "*Number of characters from name to incorporate into key.
342 If this is set to anything but a number, all characters are used.
343 See the documentation of function bibtex-generate-autokey for further detail.")
345 (defvar bibtex-autokey-name-separator
""
346 "*String that comes between any two names in the key.
347 See the documentation of function bibtex-generate-autokey for further detail.")
349 (defvar bibtex-autokey-year-length
2
350 "*Number of rightmost digits from the year field yo incorporate into key.
351 See the documentation of function bibtex-generate-autokey for further detail.")
353 (defvar bibtex-autokey-titlewords
5
354 "*Number of title words to use for the automatically generated reference key.
355 If this is set to anything but a number, all title words are used.
356 See the documentation of function bibtex-generate-autokey for further detail.")
358 (defvar bibtex-autokey-title-terminators
359 '("\\." "!" "\\?" ":" ";" "---")
360 "*Regexp list defining the termination of the main part of the title.
361 Case of the regexps is ignored.
362 See the documentation of function bibtex-generate-autokey for further detail.")
364 (defvar bibtex-autokey-titlewords-stretch
2
365 "*Number of words that can additionally be used from the title.
366 These words are used only, if a sentence from the title can be ended then.
367 See the documentation of function bibtex-generate-autokey for further detail.")
369 (defvar bibtex-autokey-titleword-first-ignore
370 '("a" "an" "on" "the" "eine?" "der" "die" "das")
371 "*Determines words that may begin a title but are not to be used in the key.
372 Each item of the list is a regexp. If the first word of the title matchs a
373 regexp from that list, it is not included in the title, even if it is
374 capitalized. Regexps in the list must be entered using lowercase letters.")
376 (defvar bibtex-autokey-titleword-abbrevs nil
377 "*Determines exceptions to the usual abbreviation mechanism.
378 A list of (old-regexp new-string) pairs.
379 Use all lowercase letters for old-regexp.
380 See the documentation of function bibtex-generate-autokey for further detail.")
382 (defvar bibtex-autokey-titleword-change-strings
383 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
385 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
387 "Alist of (old-regexp new-string) pairs.
388 Any part of title word matching a old-regexp is replaced by new-string.
389 Case of the old-regexp is significant.
390 See the documentation of function bibtex-generate-autokey for further detail.")
392 (defvar bibtex-autokey-titleword-length
5
393 "*Number of characters from title words to incorporate into key.
394 If this is set to anything but a number, all characters are used.
395 See the documentation of function bibtex-generate-autokey for further detail.")
397 (defvar bibtex-autokey-titleword-separator
"_"
398 "*String to be put between the title words.
399 See the documentation of function bibtex-generate-autokey for further detail.")
401 (defvar bibtex-autokey-name-year-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-year-title-separator
":_"
406 "*String to be put between name part and year part of key.
407 See the documentation of function bibtex-generate-autokey for further detail.")
409 (defvar bibtex-autokey-edit-before-use t
410 "*If non-nil, user is allowed to edit the generated key before it is used.")
412 (defvar bibtex-font-lock-keywords
416 '("^\\([ \t]*OPT[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
417 1 font-lock-comment-face
)
418 ;; optional field names
419 '("^\\([ \t]*[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
420 1 font-lock-function-name-face
)
422 '("^@[A-Za-z]*[({]\\([^\n,]*\\),"
423 1 font-lock-string-face
)
426 "*Fonts to use in BibTeX mode")
429 ;; Syntax Table, Keybindings and BibTeX Entry List
430 (defvar bibtex-mode-syntax-table
431 (let ((st (make-syntax-table)))
432 ;; [alarson:19920214.1004CST] make double quote a string quote
433 (modify-syntax-entry ?
\" "\"" st
)
434 (modify-syntax-entry ?$
"$$ " st
)
435 (modify-syntax-entry ?%
"< " st
)
436 (modify-syntax-entry ?
' "w " st
)
437 (modify-syntax-entry ?
@ "w " st
)
438 (modify-syntax-entry ?
\\ "\\" st
)
439 (modify-syntax-entry ?
\f "> " st
)
440 (modify-syntax-entry ?
\n "> " st
)
441 (modify-syntax-entry ?~
" " st
)
444 (defvar bibtex-mode-map
445 (let ((km (make-sparse-keymap)))
447 (define-key km
"\t" 'bibtex-find-text
)
448 (define-key km
"\n" 'bibtex-next-field
)
449 (define-key km
"\M-\t" 'bibtex-complete-string
)
450 (define-key km
"\C-c\"" 'bibtex-remove-double-quotes-or-braces
)
451 (define-key km
"\C-c{" 'bibtex-remove-double-quotes-or-braces
)
452 (define-key km
"\C-c}" 'bibtex-remove-double-quotes-or-braces
)
453 (define-key km
"\C-c\C-c" 'bibtex-clean-entry
)
454 (define-key km
"\C-c?" 'bibtex-print-help-message
)
455 (define-key km
"\C-c\C-p" 'bibtex-pop-previous
)
456 (define-key km
"\C-c\C-n" 'bibtex-pop-next
)
457 (define-key km
"\C-c\C-k" 'bibtex-kill-optional-field
)
458 (define-key km
"\C-c\C-d" 'bibtex-empty-field
)
459 (define-key km
"\C-c$" 'bibtex-ispell-entry
)
460 (define-key km
"\M-\C-a" 'bibtex-beginning-of-entry
)
461 (define-key km
"\M-\C-e" 'bibtex-end-of-entry
)
462 (define-key km
"\C-c\C-b" 'bibtex-entry
)
463 (define-key km
"\C-c\C-q" 'bibtex-hide-entry-bodies
)
464 (define-key km
"\C-c\C-rn" 'bibtex-narrow-to-entry
)
465 (define-key km
"\C-c\C-rw" 'widen
)
466 (define-key km
"\C-c\C-o" 'bibtex-remove-OPT
)
468 (define-key km
"\C-c\C-e\C-i" 'bibtex-InProceedings
)
469 (define-key km
"\C-c\C-ei" 'bibtex-InCollection
)
470 (define-key km
"\C-c\C-eI" 'bibtex-InBook
)
471 (define-key km
"\C-c\C-e\C-a" 'bibtex-Article
)
472 (define-key km
"\C-c\C-e\C-b" 'bibtex-InBook
)
473 (define-key km
"\C-c\C-eb" 'bibtex-Book
)
474 (define-key km
"\C-c\C-eB" 'bibtex-Booklet
)
475 (define-key km
"\C-c\C-e\C-c" 'bibtex-InCollection
)
476 (define-key km
"\C-c\C-e\C-m" 'bibtex-Manual
)
477 (define-key km
"\C-c\C-em" 'bibtex-MastersThesis
)
478 (define-key km
"\C-c\C-eM" 'bibtex-Misc
)
479 (define-key km
"\C-c\C-e\C-p" 'bibtex-InProceedings
)
480 (define-key km
"\C-c\C-ep" 'bibtex-Proceedings
)
481 (define-key km
"\C-c\C-eP" 'bibtex-PhdThesis
)
482 (define-key km
"\C-c\C-e\M-p" 'bibtex-preamble
)
483 (define-key km
"\C-c\C-e\C-s" 'bibtex-string
)
484 (define-key km
"\C-c\C-e\C-t" 'bibtex-TechReport
)
485 (define-key km
"\C-c\C-e\C-u" 'bibtex-Unpublished
)
488 (define-key bibtex-mode-map
[menu-bar bibtex-edit
]
489 (cons "BibTeX-Edit" (make-sparse-keymap "BibTeX-Edit")))
490 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-print-help-message
]
491 '("Help about Current Field" . bibtex-print-help-message
))
492 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-complete-string
]
493 '("String Complete" . bibtex-complete-string
))
494 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-next-field
]
495 '("Next Field" . bibtex-next-field
))
496 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-find-text
]
497 '("End of Field" . bibtex-find-text
))
498 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-pop-previous
]
499 '("Snatch from Similar Preceding Field" . bibtex-pop-previous
))
500 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-pop-next
]
501 '("Snatch from Similar Following Field" . bibtex-pop-next
))
502 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-remove-OPT
]
503 '("Remove OPT" . bibtex-remove-OPT
))
504 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-remove-double-quotes-or-braces
]
505 '("Remove Quotes or Braces" . bibtex-remove-double-quotes-or-braces
))
506 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-clean-entry
]
507 '("Clean Up Entry" . bibtex-clean-entry
))
508 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-sort-entries
]
509 '("Sort Entries" . bibtex-sort-entries
))
510 (define-key bibtex-mode-map
[menu-bar bibtex-edit bibtex-validate-buffer
]
511 '("Validate Entries" . bibtex-validate-buffer
))
513 (define-key bibtex-mode-map
[menu-bar entry-types
]
514 (cons "Entry-Types" (make-sparse-keymap "Entry-Types")))
515 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-preamble
]
516 '("Preamble" . bibtex-preamble
))
517 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-string
]
518 '("String" . bibtex-string
))
519 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Misc
]
520 '("Miscellaneous" . bibtex-Misc
))
521 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Unpublished
]
522 '("Unpublished" . bibtex-Unpublished
))
523 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Manual
]
524 '("Technical Manual" . bibtex-Manual
))
525 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-TechReport
]
526 '("Technical Report" . bibtex-TechReport
))
527 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-MastersThesis
]
528 '("Master's Thesis" . bibtex-MastersThesis
))
529 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-PhdThesis
]
530 '("PhD. Thesis" . bibtex-PhdThesis
))
531 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Booklet
]
532 '("Booklet (Bound, but no Publisher/Institution)" . bibtex-Booklet
))
533 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Book
]
534 '("Book" . bibtex-Book
))
535 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Proceedings
]
536 '("Conference Proceedings" . bibtex-Proceedings
))
537 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-InBook
]
538 '("Chapter or Pages in a Book" . bibtex-InBook
))
539 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-InCollection
]
540 '("Article in a Collection" . bibtex-InCollection
))
541 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-InProceedings
]
542 '("Article in Conference Proceedings" . bibtex-InProceedings
))
543 (define-key bibtex-mode-map
[menu-bar entry-types bibtex-Article
]
544 '("Article in Journal" . bibtex-Article
))
548 ;; Internal Variables
550 (defvar bibtex-pop-previous-search-point nil
)
551 ;; Next point where bibtex-pop-previous starts looking for a similar
554 (defvar bibtex-pop-next-search-point nil
)
555 ;; Next point where bibtex-pop-next starts looking for a similar entry.
557 (defvar bibtex-completion-candidates nil
)
558 ;; Candidates for bibtex-complete-string. Initialized from
559 ;; bibtex-predefined-strings and bibtex-string-files. This variable is
561 (make-variable-buffer-local 'bibtex-completion-candidates
)
563 (defvar bibtex-keys nil
)
564 ;; Candidates for TAB completion when entering a reference key using
565 ;; the minibuffer. Initialized in bibtex-mode and updated for each
566 ;; new entry. This variable is buffer-local.
567 (make-variable-buffer-local 'bibtex-keys
)
569 (defvar bibtex-buffer-last-parsed-for-keys-tick nil
)
570 ;; Remembers the value returned by buffer-modified-tick when buffer
571 ;; was parsed for keys the last time.
572 (make-variable-buffer-local 'bibtex-keys
)
576 ;; Functions to Parse the BibTeX Entries
578 (defun bibtex-cfield (name text
)
579 ;; Create a regexp for a BibTeX field of name NAME and text TEXT.
580 (concat ",[ \t\n]*\\("
582 "\\)[ \t\n]*=[ \t\n]*\\("
585 (defconst bibtex-name-in-cfield
1)
586 ;; The regexp subexpression number of the name part in bibtex-cfield.
588 (defconst bibtex-text-in-cfield
2)
589 ;; The regexp subexpression number of the text part in bibtex-cfield.
591 (defconst bibtex-field-name
"[A-Za-z_-][A-Za-z0-9_-]*")
592 ;; Regexp defining the name part of a BibTeX field.
594 (defconst bibtex-field-const
"[0-9A-Za-z][A-Za-z0-9:_+-]*"
595 "Format of a bibtex field constant.")
597 (defconst bibtex-field-string
600 "{\\(\\({\\(\\({[^}]*}\\)\\|\\([^{}]\\)\\)*}\\)\\|\\([^{}]\\)\\)*}"
601 ;; maximal twice nested {}
603 "\"[^\"]*[^\\\\]\"\\|\"\""
605 ;; Match either a string or an empty string.
607 (defconst bibtex-field-string-or-const
608 (concat bibtex-field-const
"\\|" bibtex-field-string
))
609 ;; Match either bibtex-field-string or bibtex-field-const.
611 (defconst bibtex-field-text
613 "\\(" bibtex-field-string-or-const
"\\)"
614 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const
"\\)\\)*"))
615 ;; Regexp defining the text part of a BibTeX field: either a string,
616 ;; or an empty string, or a constant followed by one or more # /
619 (defconst bibtex-field
620 (bibtex-cfield bibtex-field-name bibtex-field-text
))
621 ;; Regexp defining the format of a BibTeX field.
623 (defconst bibtex-name-in-field bibtex-name-in-cfield
)
624 ;; The regexp subexpression number of the name part in BibTeX-field.
626 (defconst bibtex-text-in-field bibtex-text-in-cfield
)
627 ;; The regexp subexpression number of the text part in BibTeX-field.
629 (defconst bibtex-reference-type
"@[A-Za-z]+")
630 ;; Regexp defining the type part of a BibTeX reference entry.
632 (defconst bibtex-reference-key
"[A-Za-z][A-Za-z0-9.:;?!`'/*@_+-]*")
633 ;; Regexp defining the label part of a BibTeX reference entry.
635 (defconst bibtex-reference-head
636 (concat "^\\( \\|\t\\)*\\("
637 bibtex-reference-type
641 ;; Regexp defining format of the header line of a BibTeX reference
644 (defconst bibtex-reference-maybe-empty-head
645 (concat bibtex-reference-head
"?"))
646 ;; Regexp defining format of the header line of a maybe empty
647 ;; BibTeX reference entry (without reference key).
649 (defconst bibtex-type-in-head
2)
650 ;; The regexp subexpression number of the type part in
651 ;; bibtex-reference-head.
653 (defconst bibtex-key-in-head
3)
654 ;; The regexp subexpression number of the key part in
655 ;; bibtex-reference-head.
657 (defconst bibtex-reference
658 (concat bibtex-reference-head
659 "\\([ \t\n]*" bibtex-field
"\\)*"
660 "[ \t\n]*,?[ \t\n]*[})]"))
661 ;; Regexp defining the format of a BibTeX reference entry.
663 (defconst bibtex-type-in-reference bibtex-type-in-head
)
664 ;; The regexp subexpression number of the type part in
667 (defconst bibtex-key-in-reference bibtex-key-in-head
)
668 ;; The regexp subexpression number of the key part in
671 (defconst bibtex-string
672 (concat "^[ \t]*@[sS][tT][rR][iI][nN][gG][ \t\n]*[({][ \t\n]*\\("
674 "\\)[ \t\n]*=[ \t\n]*\\("
677 ;; Regexp defining the format of a BibTeX string entry.
679 (defconst bibtex-key-in-string
1)
680 ;; The regexp subexpression of the name part in bibtex-string.
682 (defconst bibtex-text-in-string
2)
683 ;; The regexp subexpression of the text part in bibtex-string.
685 (defconst bibtex-name-alignment
2)
686 ;; Alignment for the name part in BibTeX fields. Chosen on aesthetic
689 (defconst bibtex-text-alignment
(length " organization = "))
690 ;; Alignment for the text part in BibTeX fields. Equal to the space
691 ;; needed for the longest name part.
697 (defun assoc-ignore-case (string alist
)
698 ;; Return non-nil if STRING is `equal' to the car of an element of
699 ;; LIST. Comparison is done with case ignored. The value is actually
700 ;; the element of LIST whose car is `equal' to STRING.
701 (or (assoc string alist
)
705 (downcase (car (car alist
))))))
706 (setq alist
(cdr alist
)))
709 (defun member-of-regexp (string list
)
710 ;; Return non-nil if STRING is exactly matched by an element of
711 ;; LIST. This function is influenced by the actual value of
712 ;; `case-fold-search'. The value is actually the tail of LIST whose
713 ;; car matches STRING.
719 (concat "^" (car list
) "$")
721 (setq list
(cdr list
)))
724 (defun assoc-of-regexp (string alist
)
725 ;; Return non-nil if STRING is exactly matched by the car of an
726 ;; element of LIST. This function is influenced by the actual value
727 ;; of `case-fold-search'. The value is actually the element of LIST
728 ;; whose car matches STRING.
734 (concat "^" (car (car alist
)) "$")
736 (setq alist
(cdr alist
)))
739 (defun skip-whitespace-and-comments ()
740 (let ((md (match-data)))
742 (while (cond ((looking-at "\\s>+\\|\\s +")
744 ;; NOTE: also checked end-comment. In latex and
745 ;; lisp modes, newline is an end comment, but it
746 ;; should also be a whitespace char.
747 (goto-char (match-end 0)))
748 ;; If looking at beginning of comment, skip to end.
750 (re-search-forward "\\s>"))))
751 (store-match-data md
))))
753 (defun map-bibtex-entries (fun)
754 ;; Call FUN for each BibTeX entry starting with the current. Do this
755 ;; to the end of the file. FUN is called with one argument, the key
756 ;; of the entry, and with point inside the entry. If
757 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be called
758 ;; for @string entries.
759 (bibtex-beginning-of-entry)
760 (while (re-search-forward bibtex-reference-head nil t
)
761 (if (and bibtex-sort-ignore-string-entries
762 (string-equal "@string"
763 (downcase (buffer-substring-no-properties
764 (match-beginning bibtex-type-in-head
)
765 (match-end bibtex-type-in-head
)))))
767 (funcall fun
(buffer-substring-no-properties
768 (match-beginning bibtex-key-in-head
)
769 (match-end bibtex-key-in-head
))))))
771 (defun bibtex-flash-head ()
772 ;; Flash at BibTeX reference head before point, if exists.
774 (cond ((re-search-backward bibtex-reference-head
(point-min) t
)
775 (goto-char (match-beginning bibtex-type-in-head
))
776 (setq flash
(match-end bibtex-key-in-reference
)))
779 (skip-chars-backward " \t")
782 (skip-chars-forward " \t")))
783 (if (pos-visible-in-window-p (point))
786 (buffer-substring (point) flash
)))))
788 (defun bibtex-move-outside-of-entry ()
789 ;; Make sure we are outside of a BibTeX entry.
791 (= (point) (point-max))
792 (= (point) (point-min))
793 (looking-at "[ \n]*@")
798 (forward-paragraph)))
799 (re-search-forward "[ \t\n]*" (point-max) t
))
801 (defun beginning-of-first-bibtex-entry ()
802 ;; Go to the beginning of the first BibTeX entry in buffer.
803 (goto-char (point-min))
805 ((re-search-forward "^@" nil
'move
)
810 (message "Warning: No BibTeX entries found!"))))
812 (defun bibtex-inside-field ()
813 ;; Try to avoid point being at end of a BibTeX field.
815 (skip-chars-backward " \t")
816 (cond ((= (preceding-char) ?
,)
819 (= (preceding-char) ?
})
820 (= (preceding-char) ?
\"))
823 (defun bibtex-enclosing-field ()
824 ;; Search for BibTeX field enclosing point. Point moves to end of
825 ;; field; also, use match-beginning and match-end to parse the field.
826 ;; sct@dcs.edinburgh.ac.uk
827 (let ((old-point (point)))
828 (condition-case errname
829 (bibtex-enclosing-regexp bibtex-field
)
831 (goto-char old-point
)
832 (error "Can't find enclosing BibTeX field.")))))
834 (defun bibtex-enclosing-reference ()
835 ;; Search for BibTeX reference enclosing point. Point moves to
836 ;; beginning of reference. Beginning/end of reference is given by
837 ;; (match-beginning/match-end 0).
838 (let ((old-point (point)))
840 (re-search-backward bibtex-reference-head
(point-min) t
))
842 (error "Can't find enclosing BibTeX reference.")
843 (goto-char old-point
)))
844 (goto-char (match-beginning bibtex-type-in-head
))
847 (re-search-forward bibtex-reference
(point-max) t
))
849 (error "Can't find enclosing BibTeX reference.")
850 (goto-char old-point
))
853 (defun bibtex-enclosing-reference-maybe-empty-head ()
854 ;; Search for BibTeX reference enclosing point. Point moves to
855 ;; beginning of reference. Beginning/end of reference is given by
856 ;; (match-beginning/match-end 0).
857 (let ((old-point (point)))
860 bibtex-reference-maybe-empty-head
(point-min) t
))
862 (error "Can't find enclosing BibTeX reference.")
863 (goto-char old-point
)))
864 (goto-char (match-beginning bibtex-type-in-head
))
869 bibtex-reference-maybe-empty-head
870 "\\([ \t\n]*" bibtex-field
"\\)*"
871 "[ \t\n]*,?[ \t\n]*[})]")
874 (error "Can't find enclosing BibTeX reference.")
875 (goto-char old-point
))
878 (defun bibtex-enclosing-regexp (regexp)
879 ;; Search for REGEXP enclosing point. Point moves to end of
880 ;; REGEXP. See also match-beginning and match-end. If an enclosing
881 ;; REGEXP is not found, signals search-failed; point is left in an
882 ;; undefined location.
883 ;; Doesn't something like this exist already?
884 ; compute reasonable limits for the loop
885 (let* ((initial (point))
886 (right (if (re-search-forward regexp
(point-max) t
)
892 (if (re-search-backward regexp
(point-min) t
)
895 ; within the prescribed limits, loop until a match is found
897 (re-search-forward regexp right nil
1)
898 (if (> (match-beginning 0) initial
)
899 (signal 'search-failed
(list regexp
)))
900 (while (<= (match-end 0) initial
)
901 (re-search-forward regexp right nil
1)
902 (if (> (match-beginning 0) initial
)
903 (signal 'search-failed
(list regexp
))))
906 (defun bibtex-autokey-change (string change-list
)
907 ;; Returns a string where some regexps are changed according to
908 ;; change-list. Every item of change-list is an (old-regexp
910 (let ((return-string string
)
913 (len (length change-list
))
916 (setq change-item
(elt change-list index
))
917 (while (string-match (car change-item
) return-string
)
920 (concat (substring return-string
0 (match-beginning 0))
922 (substring return-string
(match-end 0)))))
923 (setq index
(1+ index
)))
926 (defun bibtex-autokey-abbrev (string len
)
927 ;; Returns an abbreviation of string with at least len
928 ;; characters. String is aborted only after a consonant or at the
929 ;; word end. If len is not a number, string is returned unchanged.
930 (let* ((string-length (length string
))
931 (len (if (numberp len
)
932 (min len string-length
)
934 (return-string (if (numberp len
)
935 (substring string
0 len
)))
937 (vowels '(?a ?e ?i ?o ?u ?A ?E ?I ?O ?U
)))
941 (< index string-length
)
942 (member (elt return-string
943 (1- (length return-string
)))
945 (setq return-string
(concat return-string
947 string index
(1+ index
)))
952 (defun bibtex-generate-autokey ()
953 "Generates automatically a key from the author/editor and the title field.
954 The generation algorithm works as follows:
955 1. If there is a non-empty author (preferred) or editor field,
956 use it for the name part of the key.
957 2. Change any substring found in `bibtex-autokey-name-change-strings'
958 to the corresponding new one (see documentation of this variable
960 3. For every of the first `bibtex-autokey-names' names in the
961 \"name\" field, determine the last name.
962 4. From every last name, take at least `bibtex-autokey-name-length'
963 characters (abort only after a consonant or at a word end).
964 5. Build the name part of the key by concatenating all abbreviated last
965 names with the string `bibtex-autokey-name-separator' between
967 6. Build the year part of the key by truncating the contents of the
968 \"year\" field to the rightmost `bibtex-autokey-year-length'
969 digits (useful values are 2 and 4).
970 7. For the title part of the key change the contents of the \"title\"
971 field of the reference according to
972 `bibtex-autokey-titleword-change-strings' to the corresponding
973 new one (see documentation of this variable for further detail).
974 8. Abbreviate the result to the string up to (but not including) the
975 first occurrence of a regexp matched by the items of
976 `bibtex-autokey-title-terminators' and delete the first
977 word if it appears in `bibtex-autokey-titleword-first-ignore'.
978 Build the title part of the key by using at least the first
979 `bibtex-autokey-titlewords' capitalized words from this
980 abbreviated title. If the abbreviated title ends after maximal
981 `bibtex-autokey-titlewords' + `bibtex-autokey-titlewords-stretch'
982 capitalized words, all capitalized words from the abbreviated title
984 9. For every used title word that appears in
985 `bibtex-autokey-titleword-abbrevs' use the corresponding abbreviation
986 (see documentation of this variable for further detail).
987 10. From every title word not generated by an abbreviation, take at
988 least `bibtex-autokey-titleword-length' characters (abort only after
989 a consonant or at a word end).
990 11. Build the title part of the key by concatenating all abbreviated
991 title words with the string `bibtex-autokey-titleword-separator'
993 12. At least, to get the key, concatenate the name part, the year part
994 and the title part with `bibtex-autokey-name-year-separator'
995 between the name and the year if both are non-empty and
996 `bibtex-autokey-year-title-separator' between the year and
997 the title if both are non-empty."
1002 (bibtex-beginning-of-entry)
1006 (bibtex-end-of-entry)
1012 (re-search-forward "^[ \t]*author[ \t]*=" max t
)
1013 (re-search-forward "^[ \t]*editor[ \t]*=" max t
))
1014 (let* (bibtex-help-message
1016 (bibtex-find-text t
)
1019 (bibtex-find-text nil
)
1021 (bibtex-autokey-change
1022 (buffer-substring-no-properties start end
)
1023 bibtex-autokey-name-change-strings
))
1029 (bibtex-autokey-abbrev
1030 (if (string-match "," fullname
)
1031 (substring fullname
0 (match-beginning 0))
1033 (if (string-match " [^ ]*$" fullname
)
1035 fullname
(1+ (match-beginning 0)))
1037 bibtex-autokey-name-length
)))
1038 ;; Gather all names into a list
1042 (not (equal namefield
""))
1044 (not (numberp bibtex-autokey-names
))
1045 (< counter bibtex-autokey-names
)))
1046 (if (string-match " and " namefield
)
1054 namefield
0 (match-beginning 0)))))
1056 (substring namefield
(match-end 0))))
1058 (append names
(list (downcase namefield
)))
1060 (setq counter
(1+ counter
)))
1062 (namepart (mapconcat (function (lambda (name) name
))
1064 bibtex-autokey-name-separator
))
1068 (if (re-search-forward
1069 "^[ \t]*year[ \t]*=[ \t]*\\([0-9]*\\)" max t
)
1070 (buffer-substring-no-properties
1071 (match-beginning 1) (match-end 1))
1074 (if (equal yearfield
"")
1076 (substring yearfield
1077 (- (length yearfield
)
1078 bibtex-autokey-year-length
))))
1080 (let ((case-fold-search t
)
1084 (if (re-search-forward
1085 "^[ \t]*title[ \t]*=" max t
)
1086 (let* (bibtex-help-message
1088 (bibtex-find-text t
)
1091 (bibtex-find-text nil
)
1093 (bibtex-autokey-change
1094 (buffer-substring-no-properties start end
)
1095 bibtex-autokey-titleword-change-strings
))
1100 (length bibtex-autokey-title-terminators
)))
1101 (while (< index numberofitems
)
1103 (elt bibtex-autokey-title-terminators index
)
1106 (substring titlefield
0 (match-beginning 0))))
1107 (setq index
(1+ index
)))
1115 titleword bibtex-autokey-titleword-abbrevs
)))
1118 (bibtex-autokey-abbrev
1120 bibtex-autokey-titleword-length
)))))
1121 ;; Gather all titlewords into a list
1128 (not (equal titlestring
""))
1130 (not (numberp bibtex-autokey-titlewords
))
1132 bibtex-autokey-titlewords
1133 bibtex-autokey-titlewords-stretch
))))
1134 (if (string-match "\\b[A-Z][A-Za-z0-9]*" titlestring
)
1135 (let* ((end-match (match-end 0))
1137 (downcase (substring titlestring
1141 (not (numberp bibtex-autokey-titlewords
))
1142 (< counter bibtex-autokey-titlewords
))
1147 bibtex-autokey-titleword-first-ignore
))
1150 (append titlewords
(list titleword
))))
1153 (append titlewords-extra
(list titleword
))))
1155 (substring titlestring end-match
)))
1156 (setq titlestring
""))
1158 counter
(1+ counter
)))
1159 (if (string-match "\\b[A-Z][^ ]*\\b" titlestring
)
1161 (append titlewords titlewords-extra
)))))
1162 (titlepart (mapconcat (function (lambda (name) name
))
1164 bibtex-autokey-titleword-separator
))
1171 (equal yearpart
"")))
1172 bibtex-autokey-name-year-separator
)
1178 (equal yearpart
""))
1179 (equal titlepart
"")))
1180 bibtex-autokey-year-title-separator
)
1185 (defun bibtex-parse-keys (add &optional abortable
)
1186 ;; Sets bibtex-keys to the keys used in the whole (possibly
1187 ;; restricted) buffer (either as entry keys or as crossref entries).
1188 ;; If ADD is non-nil adds the new keys to bibtex-keys instead of
1189 ;; simply resetting it. If ABORTABLE is non-nil abort on user input.
1190 (if bibtex-maintain-sorted-entries
1191 (let ((labels (if add
1194 (case-fold-search t
))
1196 (goto-char (point-min))
1198 (message "Parsing reference keys..."))
1206 bibtex-reference-head
1208 "^[ \t\n]*crossref[ \t\n]*=[ \t\n]*[{\"]\\([A-Za-z][]A-Za-z0-9.:;?!`'()/*@_+-]*\\)[}\"],?$"
1215 (if (match-beginning (1+ bibtex-key-in-head
))
1218 (buffer-substring-no-properties
1219 (match-beginning (1+ bibtex-key-in-head
))
1220 (match-end (1+ bibtex-key-in-head
))))
1223 (buffer-substring-no-properties
1224 (match-beginning (+ 3 bibtex-key-in-head
))
1225 (match-end (+ 3 bibtex-key-in-head
)))))
1226 (if (not (assoc label labels
))
1228 (cons (list label
) labels
))))))
1231 bibtex-buffer-last-parsed-for-keys-tick
1232 (buffer-modified-tick))
1234 (message "Parsing reference keys... done"))
1235 (setq bibtex-keys labels
)))))))
1237 (defun bibtex-auto-fill-function ()
1238 (let ((fill-prefix (make-string (+ bibtex-text-alignment
1) ?
)))
1243 ;; Interactive Functions:
1246 (defun bibtex-mode ()
1247 "Major mode for editing BibTeX files.
1251 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
1253 The optional fields start with the string OPT, and thus ignored by BibTeX.
1254 The OPT string may be removed from a field with \\[bibtex-remove-OPT].
1255 \\[bibtex-kill-optional-field] kills the current optional field entirely.
1256 \\[bibtex-remove-double-quotes-or-braces] removes the double-quotes or
1257 braces around the text of the current field. \\[bibtex-empty-field]
1258 replaces the text of the current field with the default \"\" or {}.
1260 The command \\[bibtex-clean-entry] cleans the current entry, i.e. (i) removes
1261 double-quotes or braces from entirely numerical fields, (ii) removes
1262 OPT from all non-empty optional fields, (iii) removes all empty
1263 optional fields, and (iv) checks that no non-optional fields are empty.
1265 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
1266 Use \\[bibtex-next-field] to move to end of the next field.
1268 The following may be of interest as well:
1272 bibtex-print-help-message
1273 bibtex-beginning-of-entry
1275 bibtex-ispell-abstract
1276 bibtex-narrow-to-entry
1277 bibtex-hide-entry-bodies
1279 bibtex-validate-buffer
1282 bibtex-complete-string
1285 bibtex-field-left-delimiter
1286 bibtex-field-right-delimiter
1287 bibtex-include-OPTcrossref
1288 bibtex-include-OPTkey
1289 bibtex-include-OPTannote
1290 bibtex-mode-user-optional-fields
1291 bibtex-clean-entry-zap-empty-opts
1292 bibtex-sort-ignore-string-entries
1293 bibtex-maintain-sorted-entries
1294 bibtex-entry-field-alist
1295 bibtex-predefined-strings
1298 ---------------------------------------------------------
1299 Entry to this mode calls the value of bibtex-mode-hook if that value is
1302 (kill-all-local-variables)
1303 (use-local-map bibtex-mode-map
)
1304 (setq major-mode
'bibtex-mode
)
1305 (setq mode-name
"BibTeX")
1306 (set-syntax-table bibtex-mode-syntax-table
)
1307 (setq bibtex-completion-candidates bibtex-predefined-strings
)
1311 ;; collect pathnames
1312 (let* ((bib (getenv "BIBINPUTS"))
1319 (lambda (dirname) ;; strips off trailing slashes
1320 (let ((len (length dirname
)))
1321 (if (equal (elt dirname
(1- len
)) "/")
1322 (substring dirname
0 (1- (1- len
)))
1325 (while (string-match ":" path
)
1330 (1- (match-end 0)))))
1331 path
(substring path
(match-end 0))))
1332 (append actdirs
(list path
)))))
1333 (filename (if (string-match "\.bib$" filename
)
1335 (concat filename
".bib")))
1338 (size (length dirs
)))
1342 (not (file-readable-p
1344 (concat (elt dirs item
) "/" filename
)))))
1345 (setq item
(1+ item
)))
1348 (let ((curbuf (current-buffer))
1349 (bufname (make-temp-name ""))
1350 (compl bibtex-completion-candidates
))
1351 (create-file-buffer bufname
)
1352 (set-buffer bufname
)
1353 (insert-file-contents fullfilename
)
1354 (goto-char (point-min))
1355 (while (re-search-forward bibtex-string nil t
)
1361 (list (buffer-substring-no-properties
1362 (match-beginning bibtex-key-in-string
)
1363 (match-end bibtex-key-in-string
)))))))
1364 (kill-buffer bufname
)
1366 (setq bibtex-completion-candidates compl
))
1367 (error "File %s not in $BIBINPUTS paths" filename
)))))
1368 bibtex-string-files
)
1374 bibtex-maintain-sorted-entries
1375 (eq major-mode
'bibtex-mode
)
1377 (eq (buffer-modified-tick)
1378 bibtex-buffer-last-parsed-for-keys-tick
)))
1379 (bibtex-parse-keys nil t
)))))
1380 (bibtex-parse-keys nil
)
1381 (make-local-variable 'paragraph-start
)
1382 (setq paragraph-start
"[ \f\n\t]*$")
1383 (make-local-variable 'comment-start
)
1384 (setq comment-start
"%")
1386 (setq auto-fill-function
'bibtex-auto-fill-function
)
1387 (set (make-local-variable 'font-lock-defaults
)
1388 '(bibtex-font-lock-keywords nil t
((?$ .
"\""))))
1389 (run-hooks 'bibtex-mode-hook
))
1391 (defun bibtex-entry (entry-type &optional required optional
)
1392 "Inserts a new BibTeX entry.
1393 Calls the value of bibtex-add-entry-hook if that value is non-nil."
1394 (interactive (let* ((completion-ignore-case t
)
1395 (e-t (completing-read
1397 bibtex-entry-field-alist
1400 (if (and (null required
) (null optional
))
1401 (let* ((e (assoc-ignore-case entry-type bibtex-entry-field-alist
))
1405 (error "Bibtex entry type %s not defined!" entry-type
))
1407 (member entry-type bibtex-include-OPTcrossref
)
1409 (setq required
(elt c-ref
0)
1410 optional
(elt c-ref
1))
1411 (setq required
(elt r-n-o
0)
1412 optional
(elt r-n-o
1)))))
1414 (if bibtex-maintain-sorted-entries
1416 (format "%s key: " entry-type
)
1418 (if bibtex-maintain-sorted-entries
1419 (bibtex-find-entry-location key
)
1420 (bibtex-move-outside-of-entry))
1421 (insert "@" entry-type
"{")
1425 (mapcar 'bibtex-make-field required
)
1426 (if (member entry-type bibtex-include-OPTcrossref
)
1427 (bibtex-make-optional-field '("crossref")))
1428 (if bibtex-include-OPTkey
1429 (bibtex-make-optional-field '("key")))
1430 (mapcar 'bibtex-make-optional-field optional
)
1431 (mapcar 'bibtex-make-optional-field
1432 bibtex-mode-user-optional-fields
)
1433 (if bibtex-include-OPTannote
1434 (bibtex-make-optional-field '("annote")))
1436 (bibtex-next-field t
)
1437 (run-hooks 'bibtex-add-entry-hook
)))
1439 (defun bibtex-print-help-message ()
1440 "Prints helpful information about current field in current BibTeX entry."
1442 (let* ((pnt (point))
1446 (condition-case errname
1447 (bibtex-enclosing-regexp bibtex-field
)
1450 (error "Not on BibTeX field")))
1451 (let ((mb (match-beginning bibtex-name-in-field
))
1452 (me (match-end bibtex-name-in-field
)))
1454 (buffer-substring-no-properties
1455 (if (looking-at "OPT")
1462 bibtex-reference-maybe-empty-head nil t
)
1463 (buffer-substring-no-properties
1464 (1+ (match-beginning bibtex-type-in-head
))
1465 (match-end bibtex-type-in-head
))))
1467 (assoc-ignore-case reference-type
1468 bibtex-entry-field-alist
))
1469 (c-r-list (elt entry-list
2))
1472 (member reference-type bibtex-include-OPTcrossref
)
1475 (elt entry-list
1)))
1476 (list-of-entries (append
1477 (elt req-opt-list
0)
1478 (elt req-opt-list
1)
1479 bibtex-mode-user-optional-fields
1482 bibtex-include-OPTcrossref
)
1484 "Label of the crossreferenced entry")))
1485 (if bibtex-include-OPTannote
1487 "Personal annotation (ignored)")))
1488 (if bibtex-include-OPTkey
1490 "Key used for label creation if author and editor fields are missing"))))))
1492 (if (assoc field-name list-of-entries
)
1493 (message (elt (assoc field-name list-of-entries
) 1))
1494 (message "NO COMMENT AVAILABLE"))))
1496 (defun bibtex-make-field (e-t)
1497 "Makes a field named E-T in current BibTeX entry."
1498 (interactive "sBibTeX field name: ")
1499 (let ((name (if (consp e-t
)
1504 (bibtex-find-text nil
)
1505 (if (looking-at "[}\"]")
1508 (indent-to-column bibtex-name-alignment
)
1510 (indent-to-column bibtex-text-alignment
)
1511 (insert bibtex-field-left-delimiter bibtex-field-right-delimiter
)
1513 (forward-char -
1))))
1515 (defun bibtex-make-optional-field (e-t)
1516 "Makes an optional field named E-T in current BibTeX entry."
1518 (setq e-t
(cons (concat "OPT" (car e-t
)) (cdr e-t
)))
1519 (setq e-t
(concat "OPT" e-t
)))
1520 (bibtex-make-field e-t
))
1522 (defun bibtex-beginning-of-entry ()
1523 "Move to beginning of BibTeX entry.
1524 If inside an entry, move to the beginning of it, otherwise move to the
1525 beginning of the previous entry."
1527 (if (looking-at "^@")
1529 (re-search-backward "^@" nil
'move
))
1531 (defun bibtex-end-of-entry ()
1532 "Move to end of BibTeX entry.
1533 If inside an entry, move to the end of it, otherwise move to the end
1534 of the previous entry."
1536 (bibtex-beginning-of-entry)
1537 (let ((parse-sexp-ignore-comments t
))
1538 (forward-sexp 2) ;; skip entry type and body
1541 (defun bibtex-ispell-entry ()
1542 "Spell whole BibTeX entry."
1544 (ispell-region (progn (bibtex-beginning-of-entry) (point))
1545 (progn (bibtex-end-of-entry) (point))))
1547 (defun bibtex-ispell-abstract ()
1548 "Spell abstract of BibTeX entry."
1550 (let ((pnt (bibtex-end-of-entry)))
1551 (bibtex-beginning-of-entry)
1553 (re-search-forward "^[ \t]*[OPT]*abstract[ \t]*=" pnt
))
1554 (error "No abstract in entry.")))
1555 (ispell-region (point)
1556 (save-excursion (forward-sexp) (point))))
1558 (defun bibtex-narrow-to-entry ()
1559 "Narrow buffer to current BibTeX entry."
1562 (narrow-to-region (progn (bibtex-beginning-of-entry) (point))
1563 (progn (bibtex-end-of-entry) (point)))))
1566 (defun bibtex-hide-entry-bodies (&optional arg
)
1567 "Hide all lines between first and last BibTeX entries not beginning with @.
1568 With argument, show all text."
1571 (beginning-of-first-bibtex-entry)
1572 ;; subst-char-in-region modifies the buffer, despite what the
1573 ;; documentation says...
1574 (let ((modifiedp (buffer-modified-p))
1575 (buffer-read-only nil
))
1577 (subst-char-in-region (point) (point-max) ?
\r ?
\n t
)
1578 (while (save-excursion (re-search-forward "\n[^@]" (point-max) t
))
1579 ;; (save-excursion (replace-regexp "\n\\([^@]\\)" "\r\\1"))
1581 (while (re-search-forward "\n\\([^@]\\)" nil t
)
1582 (replace-match "\r\\1" nil nil
)))))
1583 (setq selective-display
(not arg
))
1584 (set-buffer-modified-p modifiedp
))))
1586 (defun bibtex-sort-entries ()
1587 "Sort BibTeX entries alphabetically by key.
1588 Text outside of BibTeX entries is not affected. If
1589 bibtex-sort-ignore-string-entries is non-nil, @string entries will be
1593 (beginning-of-first-bibtex-entry)
1597 (goto-char (point-max))
1598 (bibtex-end-of-entry)
1600 (if bibtex-sort-ignore-string-entries
1601 (if (re-search-forward bibtex-reference nil
'move
)
1602 (goto-char (match-beginning 0))))
1608 (if bibtex-sort-ignore-string-entries
1609 (if (re-search-forward bibtex-reference nil
'move
)
1610 (goto-char (match-beginning 0)))
1611 (if (re-search-forward bibtex-reference-head nil
'move
)
1612 (goto-char (match-beginning 0))))))
1614 'bibtex-end-of-entry
1615 ;; STARTKEY function
1618 (if bibtex-sort-ignore-string-entries
1620 (re-search-forward bibtex-reference
)
1621 (buffer-substring-no-properties
1622 (match-beginning bibtex-key-in-reference
)
1623 (match-end bibtex-key-in-reference
)))
1624 (re-search-forward bibtex-reference-head
)
1625 (buffer-substring-no-properties
1626 (match-beginning bibtex-key-in-head
)
1627 (match-end bibtex-key-in-head
)))))
1631 (defun bibtex-find-entry-location (entry-name &optional ignore-dups
)
1632 "Looking for place to put the BibTeX entry named ENTRY-NAME.
1633 Performs a binary search (therefore, buffer is assumed to be in sorted
1634 order, without duplicates (see \\[bibtex-validate-buffer]), if it is
1635 not, bibtex-find-entry-location will fail). If entry-name is already
1636 used as a reference key, an error is signaled. However, if optional
1637 variable IGNORE-DUPS is non-nil, no error messages about duplicate
1638 entries are signaled, but the error handling is assumed to be made in
1639 the calling function. Nil is returned, if an duplicate entry error
1640 occurred, and t in all other cases."
1643 (beginning-of-first-bibtex-entry)
1644 (if bibtex-sort-ignore-string-entries
1645 (re-search-forward bibtex-reference nil
`move
)
1646 (bibtex-end-of-entry))
1650 (goto-char (point-max))
1651 (if bibtex-sort-ignore-string-entries
1652 (re-search-backward bibtex-reference nil
`move
)
1653 (bibtex-beginning-of-entry))
1657 (done (>= left right
))
1661 (setq actual-point
(/ (+ left right
) 2))
1662 (goto-char actual-point
)
1663 (bibtex-beginning-of-entry)
1665 (if bibtex-sort-ignore-string-entries
1667 (re-search-forward bibtex-reference
)
1668 (buffer-substring-no-properties
1669 (match-beginning bibtex-key-in-reference
)
1670 (match-end bibtex-key-in-reference
)))
1671 (re-search-forward bibtex-reference-head
)
1672 (buffer-substring-no-properties
1673 (match-beginning bibtex-key-in-head
)
1674 (match-end bibtex-key-in-head
))))
1676 ((string-lessp entry-name actual-key
)
1677 (setq new
(match-beginning 0))
1678 (if (equal right new
)
1681 ((string-lessp actual-key entry-name
)
1682 (setq new
(match-end 0))
1683 (if (equal left new
)
1686 ((string-equal actual-key entry-name
)
1689 (if (not ignore-dups
)
1690 (error "Entry with key `%s' already exists!" entry-name
)))))
1694 (if (re-search-forward bibtex-reference nil t
)
1697 (buffer-substring-no-properties
1698 (match-beginning bibtex-key-in-reference
)
1699 (match-end bibtex-key-in-reference
)))
1700 (if (string-lessp actual-key entry-name
)
1701 ;; even greater than last entry --> we must append
1703 (goto-char (match-end 0))
1704 (newline (forward-line 2))
1705 (beginning-of-line))
1706 (goto-char right
))))
1709 (defun bibtex-validate-buffer ()
1710 "Validate if the current BibTeX buffer is syntactically correct.
1711 Any garbage (e.g. comments) before the first \"@\" is not tested (so
1712 you can put comments here)."
1716 ;; looking if entries fit syntactical structure
1717 (goto-char (point-min))
1718 (while (< (re-search-forward "@\\|\\'") max
)
1722 (looking-at "@string")
1723 (looking-at "@preamble"))
1726 (re-search-forward bibtex-reference nil t
)
1727 (equal p
(match-beginning 0))))
1730 (error "Bad entry begins here"))))))
1731 ;; looking if entries are balanced (a single non-escaped quote
1732 ;; inside braces is not detected by the former check, but
1733 ;; bibtex-sort-entries stumbles about it
1734 (goto-char (point-min))
1738 (bibtex-beginning-of-entry)
1740 ;; looking for correct sort order and duplicates
1741 (if bibtex-maintain-sorted-entries
1742 (let ((entry-name (make-string 10 255))
1745 (beginning-of-first-bibtex-entry)
1749 (cond ((or (null previous
)
1750 (string< previous current
))
1751 (setq previous current
1753 ((string-equal previous current
)
1754 (error "Duplicate here with previous!"))
1756 (error "Entries out of order here!"))))))))
1758 (message "BibTeX buffer is syntactically correct")))
1760 (defun bibtex-next-field (arg)
1761 "Finds end of text of next BibTeX field; with arg, to its beginning."
1763 (bibtex-inside-field)
1764 (let ((start (point)))
1767 (bibtex-enclosing-field)
1768 (goto-char (match-end 0))
1774 (bibtex-find-text arg
))
1776 (defun bibtex-find-text (arg)
1777 "Go to end of text of current field; with arg, go to beginning."
1779 (bibtex-inside-field)
1780 (bibtex-enclosing-field)
1783 (goto-char (match-beginning bibtex-text-in-field
))
1784 (if (looking-at "[{\"]")
1786 (goto-char (match-end bibtex-text-in-field
))
1788 (= (preceding-char) ?
})
1789 (= (preceding-char) ?
\"))
1791 (if bibtex-help-message
1792 (bibtex-print-help-message)))
1794 (defun bibtex-remove-OPT ()
1795 "Removes the 'OPT' starting optional arguments and goes to end of text."
1797 (bibtex-inside-field)
1798 (bibtex-enclosing-field)
1800 (goto-char (match-beginning bibtex-name-in-field
))
1801 (if (looking-at "OPT")
1802 ;; sct@dcs.edinburgh.ac.uk
1804 (delete-char (length "OPT"))
1805 (search-forward "=")
1806 (delete-horizontal-space)
1807 (indent-to-column bibtex-text-alignment
))))
1808 (bibtex-inside-field))
1810 (defun bibtex-remove-double-quotes-or-braces ()
1811 "Removes \"\" or {} around string."
1814 (bibtex-inside-field)
1815 (bibtex-enclosing-field)
1816 (let ((start (match-beginning bibtex-text-in-field
))
1817 (stop (match-end bibtex-text-in-field
)))
1820 (if (looking-at "[}\"]")
1823 (if (looking-at "[{\"]")
1826 (defun bibtex-kill-optional-field ()
1827 "Kill the entire enclosing optional BibTeX field."
1829 (bibtex-inside-field)
1830 (bibtex-enclosing-field)
1831 (goto-char (match-beginning bibtex-name-in-field
))
1832 (let ((the-end (match-end 0))
1833 (the-beginning (match-beginning 0)))
1834 (if (looking-at "OPT")
1837 (skip-chars-forward " \t\n,")
1838 (kill-region the-beginning the-end
))
1839 (error "Mandatory fields can't be killed"))))
1841 (defun bibtex-empty-field ()
1842 "Delete the text part of the current field, replace with empty text."
1844 (bibtex-inside-field)
1845 (bibtex-enclosing-field)
1846 (goto-char (match-beginning bibtex-text-in-field
))
1847 (kill-region (point) (match-end bibtex-text-in-field
))
1848 (insert (concat bibtex-field-left-delimiter
1849 bibtex-field-right-delimiter
))
1850 (bibtex-find-text t
))
1852 (defun bibtex-pop-previous (arg)
1853 "Replace text of current field with the text of similar field in previous entry.
1854 With arg, go up ARG entries. Repeated, goes up so many times. May be
1855 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
1857 (bibtex-inside-field)
1859 ; parse current field
1860 (bibtex-enclosing-field)
1861 (let ((start-old-text (match-beginning bibtex-text-in-field
))
1862 (stop-old-text (match-end bibtex-text-in-field
))
1863 (start-name (match-beginning bibtex-name-in-field
))
1864 (stop-name (match-end bibtex-name-in-field
))
1866 (goto-char start-name
)
1867 ; construct regexp for previous field with same name as this one
1868 (let ((matching-entry
1870 (buffer-substring-no-properties (if (looking-at "OPT")
1871 (+ (point) (length "OPT"))
1874 bibtex-field-text
)))
1875 ; if executed several times in a row, start each search where the
1877 (cond ((or (eq last-command
'bibtex-pop-previous
)
1878 (eq last-command
'bibtex-pop-next
))
1882 (bibtex-enclosing-reference-maybe-empty-head)
1883 (setq bibtex-pop-previous-search-point
(point))
1884 (setq bibtex-pop-next-search-point
(match-end 0))))
1885 (goto-char bibtex-pop-previous-search-point
)
1886 ; Now search for arg'th previous similar field
1888 ((re-search-backward matching-entry
(point-min) t arg
)
1890 (buffer-substring-no-properties
1891 (match-beginning bibtex-text-in-cfield
)
1892 (match-end bibtex-text-in-cfield
)))
1893 ;; change delimiters, if any changes needed
1896 (equal bibtex-field-left-delimiter
"{")
1897 (eq (aref new-text
0) ?
\")
1898 (eq (aref new-text
(1- (length new-text
))) ?
\"))
1899 (aset new-text
0 ?\
{)
1900 (aset new-text
(1- (length new-text
)) ?\
}))
1902 (equal bibtex-field-left-delimiter
"\"")
1903 (eq (aref new-text
0) ?\
{)
1904 (eq (aref new-text
(1- (length new-text
))) ?\
}))
1905 (aset new-text
0 ?
\")
1906 (aset new-text
(1- (length new-text
)) ?
\"))
1908 (not (eq (aref new-text
0)
1909 (aref bibtex-field-left-delimiter
0)))
1910 (not (eq (aref new-text
(1- (length new-text
)))
1911 (aref bibtex-field-right-delimiter
0))))
1912 (setq new-text
(concat bibtex-field-left-delimiter
1914 bibtex-field-right-delimiter
))))
1915 ; Found a matching field. Remember boundaries.
1916 (setq bibtex-pop-next-search-point
(match-end 0))
1917 (setq bibtex-pop-previous-search-point
(match-beginning 0))
1919 ; Go back to where we started, delete old text, and pop new.
1920 (goto-char stop-old-text
)
1921 (delete-region start-old-text stop-old-text
)
1924 (error "No previous matching BibTeX field."))))))
1925 (setq this-command
'bibtex-pop-previous
))
1927 (defun bibtex-pop-next (arg)
1928 "Replace text of current field with the text of similar field in next entry.
1929 With arg, go up ARG entries. Repeated, goes up so many times. May be
1930 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
1932 (bibtex-inside-field)
1934 ; parse current field
1935 (bibtex-enclosing-field)
1936 (let ((start-old-text (match-beginning bibtex-text-in-field
))
1937 (stop-old-text (match-end bibtex-text-in-field
))
1938 (start-name (match-beginning bibtex-name-in-field
))
1939 (stop-name (match-end bibtex-name-in-field
))
1941 (goto-char start-name
)
1942 ; construct regexp for next field with same name as this one,
1943 ; ignoring possible OPT's
1944 (let ((matching-entry
1946 (buffer-substring-no-properties (if (looking-at "OPT")
1947 (+ (point) (length "OPT"))
1950 bibtex-field-text
)))
1952 ; if executed several times in a row, start each search where the
1954 (cond ((or (eq last-command
'bibtex-pop-next
)
1955 (eq last-command
'bibtex-pop-previous
))
1959 (bibtex-enclosing-reference-maybe-empty-head)
1960 (setq bibtex-pop-previous-search-point
(point))
1961 (setq bibtex-pop-next-search-point
(match-end 0))))
1962 (goto-char bibtex-pop-next-search-point
)
1964 ; Now search for arg'th next similar field
1966 ((re-search-forward matching-entry
(point-max) t arg
)
1968 (buffer-substring-no-properties
1969 (match-beginning bibtex-text-in-cfield
)
1970 (match-end bibtex-text-in-cfield
)))
1971 ;; change delimiters, if any changes needed
1974 (equal bibtex-field-left-delimiter
"{")
1975 (eq (aref new-text
0) ?
\")
1976 (eq (aref new-text
(1- (length new-text
))) ?
\"))
1977 (aset new-text
0 ?\
{)
1978 (aset new-text
(1- (length new-text
)) ?\
}))
1980 (equal bibtex-field-left-delimiter
"\"")
1981 (eq (aref new-text
0) ?\
{)
1982 (eq (aref new-text
(1- (length new-text
))) ?\
}))
1983 (aset new-text
0 ?
\")
1984 (aset new-text
(1- (length new-text
)) ?
\"))
1986 (not (eq (aref new-text
0)
1987 (aref bibtex-field-left-delimiter
0)))
1988 (not (eq (aref new-text
(1- (length new-text
)))
1989 (aref bibtex-field-right-delimiter
0))))
1990 (setq new-text
(concat bibtex-field-left-delimiter
1992 bibtex-field-right-delimiter
))))
1993 ; Found a matching field. Remember boundaries.
1994 (setq bibtex-pop-next-search-point
(match-end 0))
1995 (setq bibtex-pop-previous-search-point
(match-beginning 0))
1997 ; Go back to where we started, delete old text, and pop new.
1998 (goto-char stop-old-text
)
1999 (delete-region start-old-text stop-old-text
)
2002 (error "No next matching BibTeX field."))))))
2003 (setq this-command
'bibtex-pop-next
))
2005 (defun bibtex-clean-entry (&optional arg
)
2006 "Finish editing the current BibTeX entry and clean it up.
2007 For all optional fields of current BibTeX entry: if empty, kill the
2008 whole field; otherwise, remove the \"OPT\" string in the name; if text
2009 numerical, remove double-quotes. For all mandatory fields: if empty,
2010 signal error. If label of entry is empty or a prefix argument was
2011 given, calculate a new entry label."
2013 (bibtex-beginning-of-entry)
2014 (let ((start (point))
2017 (narrow-to-region start
(save-excursion (bibtex-end-of-entry) (point)))
2019 (re-search-forward bibtex-field
(point-max) t
1)
2020 (not crossref-there
))
2021 ;; determine if reference has crossref entry
2022 (let ((begin-name (match-beginning bibtex-name-in-field
))
2023 (begin-text (match-beginning bibtex-text-in-field
)))
2024 (goto-char begin-name
)
2025 (if (looking-at "\\(OPTcrossref\\)\\|\\(crossref\\)")
2027 (goto-char begin-text
)
2028 (if (not (looking-at
2030 bibtex-field-left-delimiter
2031 bibtex-field-right-delimiter
)))
2032 (setq crossref-there t
))))))
2033 (bibtex-enclosing-reference-maybe-empty-head)
2034 (re-search-forward bibtex-reference-type
)
2035 (let ((begin-type (1+ (match-beginning 0)))
2036 (end-type (match-end 0)))
2038 (while (re-search-forward bibtex-field
(point-max) t
1)
2039 (let ((begin-field (match-beginning 0))
2040 (end-field (match-end 0))
2041 (begin-name (match-beginning bibtex-name-in-field
))
2042 (end-name (match-end bibtex-name-in-field
))
2043 (begin-text (match-beginning bibtex-text-in-field
))
2044 (end-text (match-end bibtex-text-in-field
))
2046 (goto-char begin-name
)
2049 bibtex-clean-entry-zap-empty-opts
)
2050 (goto-char begin-text
)
2053 bibtex-field-left-delimiter
2054 bibtex-field-right-delimiter
))
2055 ;; empty: delete whole field if really optional
2056 ;; (missing crossref handled) or complain
2058 (not crossref-there
)
2061 (buffer-substring-no-properties
2062 (+ (length "OPT") begin-name
) end-name
))
2065 (buffer-substring-no-properties
2066 begin-type end-type
)
2067 bibtex-entry-field-alist
))))))
2068 ;; field is not really optional
2070 (goto-char begin-name
)
2071 (delete-char (length "OPT"))
2072 ;; make field non-OPT
2073 (search-forward "=")
2074 (delete-horizontal-space)
2075 (indent-to-column bibtex-text-alignment
)
2077 ;; and loop to go through next test
2078 (error "Mandatory field ``%s'' is empty"
2079 (buffer-substring-no-properties
2082 ;; field is optional
2083 (delete-region begin-field end-field
))
2084 ;; otherwise: not empty, delete "OPT"
2085 (goto-char begin-name
)
2086 (delete-char (length "OPT"))
2088 ;; fixup alignment. [alarson:19920309.2047CST]
2089 (search-forward "=")
2090 (delete-horizontal-space)
2091 (indent-to-column bibtex-text-alignment
))
2092 (goto-char begin-field
) ; and loop to go through next test
2095 (goto-char begin-text
)
2096 (cond ((looking-at (concat
2097 bibtex-field-left-delimiter
2099 bibtex-field-right-delimiter
))
2101 (goto-char end-text
)
2102 (delete-char -
1) ; delete enclosing double-quotes
2103 (goto-char begin-text
)
2105 (goto-char end-field
) ; go to end for next search
2106 (forward-char -
2) ; to compensate for the 2 quotes deleted
2108 ((looking-at (concat
2109 bibtex-field-left-delimiter
2110 bibtex-field-right-delimiter
))
2111 ;; if empty quotes, complain
2113 (if (not (or (equal (buffer-substring-no-properties
2117 (equal (buffer-substring-no-properties
2121 (error "Mandatory field ``%s'' is empty"
2122 (buffer-substring-no-properties
2123 begin-name end-name
))))
2125 (goto-char end-field
)))))))))
2127 (bibtex-end-of-entry))
2129 (bibtex-end-of-entry)
2132 (bibtex-beginning-of-entry)
2133 (if (re-search-forward
2134 bibtex-reference-head eob t
)
2135 (buffer-substring-no-properties
2136 (match-beginning bibtex-key-in-head
)
2137 (match-end bibtex-key-in-head
))))))
2143 (if bibtex-autokey-edit-before-use
2144 (read-from-minibuffer "Key to use: "
2145 (bibtex-generate-autokey))
2146 (bibtex-generate-autokey))))
2147 (bibtex-beginning-of-entry)
2148 (re-search-forward bibtex-reference-maybe-empty-head
)
2149 (if (match-beginning bibtex-key-in-head
)
2150 (delete-region (match-beginning bibtex-key-in-head
)
2151 (match-end bibtex-key-in-head
)))
2154 (bibtex-beginning-of-entry)
2157 (bibtex-end-of-entry)
2158 (re-search-forward "^@" nil
'move
)
2162 (kill-region start end
)
2165 (not bibtex-maintain-sorted-entries
)
2166 (bibtex-find-entry-location autokey t
))))
2168 (setq kill-ring
(cdr kill-ring
))
2170 (bibtex-beginning-of-entry)
2171 (re-search-forward bibtex-reference-head
)
2174 "New inserted reference may be a duplicate."))))))))
2176 (let ((start (progn (bibtex-beginning-of-entry) (point)))
2177 (end (progn (bibtex-end-of-entry) (point))))
2179 (narrow-to-region start end
)
2180 (bibtex-parse-keys t
)))))
2182 (defun bibtex-complete-string ()
2183 "Complete word fragment before point to longest prefix of a defined string.
2184 If point is not after the part of a word, all strings are listed."
2186 (let* ((end (point))
2187 (beg (save-excursion
2188 (re-search-backward "[ \t{\"]")
2191 (part-of-word (buffer-substring-no-properties beg end
))
2192 (string-list (copy-sequence bibtex-completion-candidates
))
2193 (case-fold-search t
)
2194 (completion (save-excursion
2196 (while (re-search-backward
2197 bibtex-string
(point-min) t
)
2201 (buffer-substring-no-properties
2202 (match-beginning bibtex-key-in-string
)
2203 (match-end bibtex-key-in-string
)))
2211 (try-completion part-of-word string-list
)))))
2212 (cond ((eq completion t
)
2213 (bibtex-remove-double-quotes-or-braces))
2215 (error "Can't find completion for \"%s\"" part-of-word
))
2216 ((not (string= part-of-word completion
))
2217 (delete-region beg end
)
2219 (if (assoc completion string-list
)
2220 (bibtex-remove-double-quotes-or-braces)))
2222 (message "Making completion list...")
2223 (let ((list (all-completions part-of-word string-list
)))
2224 (with-output-to-temp-buffer "*Completions*"
2225 (display-completion-list list
)))
2226 (message "Making completion list...done")))))
2228 (defun bibtex-Article ()
2230 (bibtex-entry "Article"))
2232 (defun bibtex-Book ()
2234 (bibtex-entry "Book"))
2236 (defun bibtex-Booklet ()
2238 (bibtex-entry "Booklet"))
2240 (defun bibtex-InBook ()
2242 (bibtex-entry "InBook"))
2244 (defun bibtex-InCollection ()
2246 (bibtex-entry "InCollection"))
2248 (defun bibtex-InProceedings ()
2250 (bibtex-entry "InProceedings"))
2252 (defun bibtex-Manual ()
2254 (bibtex-entry "Manual"))
2256 (defun bibtex-MastersThesis ()
2258 (bibtex-entry "MastersThesis"))
2260 (defun bibtex-Misc ()
2262 (bibtex-entry "Misc"))
2264 (defun bibtex-PhdThesis ()
2266 (bibtex-entry "PhdThesis"))
2268 (defun bibtex-Proceedings ()
2270 (bibtex-entry "Proceedings"))
2272 (defun bibtex-TechReport ()
2274 (bibtex-entry "TechReport"))
2276 (defun bibtex-Unpublished ()
2278 (bibtex-entry "Unpublished"))
2280 (defun bibtex-string ()
2282 (bibtex-move-outside-of-entry)
2286 bibtex-field-left-delimiter
2287 bibtex-field-right-delimiter
2292 (defun bibtex-preamble ()
2294 (bibtex-move-outside-of-entry)
2295 (insert "@Preamble{}\n")
2301 ;; Make BibTeX a Feature
2306 ;;; bibtex.el ends here