]> code.delx.au - gnu-emacs/blob - lisp/textmodes/bibtex.el
(describe-mouse-briefly): Fix message spelling.
[gnu-emacs] / lisp / textmodes / bibtex.el
1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
2
3 ;; Copyright (C) 1992, 1994, 1995 Free Software Foundation, Inc.
4
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
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to
27 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28
29 ;;; Commentary:
30 ;; Major mode for editing and validating BibTeX files.
31
32 ;; Usage:
33 ;; See documentation for function bibtex-mode (or type "\M-x describe-mode"
34 ;; when you are in bibtex-mode).
35
36 ;; Todo:
37 ;; Distribute texinfo file.
38
39 ;; Known Bugs:
40 ;; 1. using regular expressions to match the entire BibTeX entry dies
41 ;; on long entries (e.g. those containing abstracts) since
42 ;; the length of regular expression matches is fairly limited.
43 ;; 2. Calling bibtex-find-text in a string entry results in the
44 ;; error message "Can't find enclosing Bibtex field" instead of
45 ;; moving to the empty string. [reported by gernot@cs.unsw.oz.au]
46 ;; 3. Quotes inside quote-parenthesized fields (like
47 ;; `author = "Stefan Sch{\"o}f"') break bibtex-validate-buffer.
48 ;; Further, you must use braces here, if you want to set
49 ;; bibtex-maintain-sorted-entries to a non-nil value.
50
51 ;; (current keeper: schoef@informatik.uni-oldenburg.de
52 ;; previous: alarson@src.honeywell.com)
53
54 ;;; Code:
55 ;; User Options:
56
57 (defvar bibtex-field-left-delimiter "{"
58 "*Set this to { or \" according to your personal preferences.
59 This variable is buffer local.")
60 (make-variable-buffer-local 'bibtex-field-left-delimiter)
61
62 (defvar bibtex-field-right-delimiter "}"
63 "*Set this to } or \" according to your personal preferences.
64 This variable is buffer local.")
65 (make-variable-buffer-local 'bibtex-field-right-delimiter)
66
67 (defvar bibtex-include-OPTcrossref '("InProceedings" "InCollection")
68 "*All entries listed here will have an OPTcrossref field.")
69
70 (defvar bibtex-include-OPTkey t
71 "*If non-nil, all entries will have an OPTkey field.")
72
73 (defvar bibtex-include-OPTannote t
74 "*If non-nil, all entries will have an OPTannote field.")
75
76 (defvar bibtex-mode-user-optional-fields nil
77 "*List of optional fields the user wants to have always present.
78 Entries should be lists of strings with two elements (first element =
79 name of the field, second element = comment to appear in the echo area).")
80
81 (defvar bibtex-clean-entry-zap-empty-opts t
82 "*If non-nil, bibtex-clean-entry will delete all empty optional fields.")
83
84 (defvar bibtex-sort-ignore-string-entries t
85 "*If non-nil, BibTeX @STRING entries are not sort-significant.
86 That means they are ignored when determining ordering of the buffer
87 (e.g. sorting, locating alphabetical position for new entries, etc.).
88 This variable is buffer local.")
89 (make-variable-buffer-local 'bibtex-sort-ignore-string-entries)
90
91 (defvar bibtex-maintain-sorted-entries nil
92 "*If non-nil, bibtex-mode maintains all BibTeX entries in sorted order.
93 Setting this variable to nil will strip off some comfort (e.g. TAB
94 completion for reference keys in minibuffer, automatic detection of
95 duplicates) from bibtex-mode. See also bibtex-sort-ignore-string-entries.
96 This variable is buffer local.")
97 (make-variable-buffer-local 'bibtex-maintain-sorted-entries)
98
99 (defvar bibtex-entry-field-alist
100 '(
101 ("Article" . (((("author" "Author1 [and Author2 ...] [and others]")
102 ("title" "Title of the article (BibTeX converts it to lowercase)")
103 ("journal" "Name of the journal (use string, remove braces)")
104 ("year" "Year of publication"))
105 (("volume" "Volume of the journal")
106 ("number" "Number of the journal")
107 ("month" "Month of the publication as a string (remove braces)")
108 ("pages" "Pages in the journal")
109 ("note" "Remarks to be put at the end of the \\bibitem")))
110 ((("author" "Author1 [and Author2 ...] [and others]")
111 ("title" "Title of the article (BibTeX converts it to lowercase)"))
112 (("journal" "Name of the journal (use string, remove braces)")
113 ("year" "Year of publication")
114 ("volume" "Volume of the journal")
115 ("number" "Number of the journal")
116 ("month" "Month of the publication as a string (remove braces)")
117 ("pages" "Pages in the journal")
118 ("note" "Remarks to be put at the end of the \\bibitem")))))
119 ("Book" . (((("author" "Author1 [and Author2 ...] [and others]")
120 ("title" "Title of the book")
121 ("publisher" "Publishing company")
122 ("year" "Year of publication"))
123 (("editor" "Editor1 [and Editor2 ...] [and others]")
124 ("volume" "Volume of the book in the series")
125 ("number" "Number of the book in a small series (overwritten by volume)")
126 ("series" "Series in which the book appeared")
127 ("address" "Address of the publisher")
128 ("edition" "Edition of the book as a capitalized English word")
129 ("month" "Month of the publication as a string (remove braces)")
130 ("note" "Remarks to be put at the end of the \\bibitem")))))
131 ("Booklet" . (((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
132 (("author" "Author1 [and Author2 ...] [and others]")
133 ("howpublished" "The way in which the booklet was published")
134 ("address" "Address of the publisher")
135 ("year" "Year of publication")
136 ("month" "Month of the publication as a string (remove braces)")
137 ("note" "Remarks to be put at the end of the \\bibitem")))))
138 ("InBook" . (((("author" "Author1 [and Author2 ...] [and others]")
139 ("title" "Title of the book")
140 ("chapter" "Chapter in the book")
141 ("publisher" "Publishing company")
142 ("year" "Year of publication"))
143 (("editor" "Editor1 [and Editor2 ...] [and others]")
144 ("volume" "Volume of the book in the series")
145 ("number" "Number of the book in a small series (overwritten by volume)")
146 ("series" "Series in which the book appeared")
147 ("address" "Address of the publisher")
148 ("edition" "Edition of the book as a capitalized English word")
149 ("month" "Month of the publication as a string (remove braces)")
150 ("pages" "Pages in the book")
151 ("type" "Word to use instead of \"chapter\"")
152 ("note" "Remarks to be put at the end of the \\bibitem")))
153 ((("author" "Author1 [and Author2 ...] [and others]")
154 ("title" "Title of the book")
155 ("chapter" "Chapter in the book"))
156 (("publisher" "Publishing company")
157 ("year" "Year of publication")
158 ("editor" "Editor1 [and Editor2 ...] [and others]")
159 ("volume" "Volume of the book in the series")
160 ("number" "Number of the book in a small series (overwritten by volume)")
161 ("series" "Series in which the book appeared")
162 ("address" "Address of the publisher")
163 ("edition" "Edition of the book as a capitalized English word")
164 ("month" "Month of the publication as a string (remove braces)")
165 ("pages" "Pages in the book")
166 ("type" "Word to use instead of \"chapter\"")
167 ("note" "Remarks to be put at the end of the \\bibitem")))))
168 ("InCollection" . (((("author" "Author1 [and Author2 ...] [and others]")
169 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
170 ("booktitle" "Name of the book")
171 ("publisher" "Publishing company")
172 ("year" "Year of publication"))
173 (("editor" "Editor1 [and Editor2 ...] [and others]")
174 ("volume" "Volume of the book in the series")
175 ("number" "Number of the book in a small series (overwritten by volume)")
176 ("series" "Series in which the book appeared")
177 ("chapter" "Chapter in the book")
178 ("type" "Word to use instead of \"chapter\"")
179 ("address" "Address of the publisher")
180 ("edition" "Edition of the book as a capitalized English word")
181 ("month" "Month of the publication as a string (remove braces)")
182 ("pages" "Pages in the book")
183 ("note" "Remarks to be put at the end of the \\bibitem")))
184 ((("author" "Author1 [and Author2 ...] [and others]")
185 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
186 ("booktitle" "Name of the book"))
187 (("publisher" "Publishing company")
188 ("year" "Year of publication")
189 ("editor" "Editor1 [and Editor2 ...] [and others]")
190 ("volume" "Volume of the book in the series")
191 ("number" "Number of the book in a small series (overwritten by volume)")
192 ("series" "Series in which the book appeared")
193 ("chapter" "Chapter in the book")
194 ("type" "Word to use instead of \"chapter\"")
195 ("address" "Address of the publisher")
196 ("edition" "Edition of the book as a capitalized English word")
197 ("month" "Month of the publication as a string (remove braces)")
198 ("pages" "Pages in the book")
199 ("note" "Remarks to be put at the end of the \\bibitem")))))
200 ("InProceedings" . (((("author" "Author1 [and Author2 ...] [and others]")
201 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
202 ("booktitle" "Name of the conference proceedings")
203 ("year" "Year of publication"))
204 (("editor" "Editor1 [and Editor2 ...] [and others]")
205 ("volume" "Volume of the conference proceedings in the series")
206 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
207 ("series" "Series in which the conference proceedings appeared")
208 ("organization" "Sponsoring organization of the conference")
209 ("publisher" "Publishing company, its location")
210 ("address" "Location of the Proceedings")
211 ("month" "Month of the publication as a string (remove braces)")
212 ("pages" "Pages in the conference proceedings")
213 ("note" "Remarks to be put at the end of the \\bibitem")))
214 ((("author" "Author1 [and Author2 ...] [and others]")
215 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
216 ("booktitle" "Name of the conference proceedings"))
217 (("editor" "Editor1 [and Editor2 ...] [and others]")
218 ("volume" "Volume of the conference proceedings in the series")
219 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
220 ("series" "Series in which the conference proceedings appeared")
221 ("year" "Year of publication")
222 ("organization" "Sponsoring organization of the conference")
223 ("publisher" "Publishing company, its location")
224 ("address" "Location of the Proceedings")
225 ("month" "Month of the publication as a string (remove braces)")
226 ("pages" "Pages in the conference proceedings")
227 ("note" "Remarks to be put at the end of the \\bibitem")))))
228 ("Manual" . (((("title" "Title of the manual"))
229 (("author" "Author1 [and Author2 ...] [and others]")
230 ("organization" "Publishing organization of the manual")
231 ("address" "Address of the organization")
232 ("edition" "Edition of the manual as a capitalized English word")
233 ("year" "Year of publication")
234 ("month" "Month of the publication as a string (remove braces)")
235 ("note" "Remarks to be put at the end of the \\bibitem")))))
236
237 ("MastersThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
238 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
239 ("school" "School where the master\'s thesis was written")
240 ("year" "Year of publication"))
241 (("address" "Address of the school (if not part of field \"school\") or country")
242 ("type" "Type of the master\'s thesis")
243 ("month" "Month of the publication as a string (remove braces)")
244 ("note" "Remarks to be put at the end of the \\bibitem")))))
245 ("Misc" . ((()
246 (("author" "Author1 [and Author2 ...] [and others]")
247 ("title" "Title of the reference (BibTeX converts it to lowercase)")
248 ("howpublished" "The way in which the reference was published")
249 ("year" "Year of publication")
250 ("month" "Month of the publication as a string (remove braces)")
251 ("note" "Remarks to be put at the end of the \\bibitem")))))
252 ("PhdThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
253 ("title" "Title of the PhD. thesis")
254 ("school" "School where the PhD. thesis was written")
255 ("year" "Year of publication"))
256 (("address" "Address of the school (if not part of field \"school\") or country")
257 ("type" "Type of the PhD. thesis")
258 ("month" "Month of the publication as a string (remove braces)")
259 ("note" "Remarks to be put at the end of the \\bibitem")))))
260 ("Proceedings" . (((("title" "Title of the conference proceedings")
261 ("year" "Year of publication"))
262 (("editor" "Editor1 [and Editor2 ...] [and others]")
263 ("volume" "Volume of the conference proceedings in the series")
264 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
265 ("series" "Series in which the conference proceedings appeared")
266 ("publisher" "Publishing company, its location")
267 ("organization" "Sponsoring organization of the conference")
268 ("address" "Location of the Proceedings")
269 ("month" "Month of the publication as a string (remove braces)")
270 ("note" "Remarks to be put at the end of the \\bibitem")))))
271 ("TechReport" . (((("author" "Author1 [and Author2 ...] [and others]")
272 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
273 ("institution" "Sponsoring institution of the report")
274 ("year" "Year of publication"))
275 (("type" "Type of the report (if other than \"technical report\")")
276 ("number" "Number of the technical report")
277 ("address" "Address of the institution (if not part of field \"institution\") or country")
278 ("month" "Month of the publication as a string (remove braces)")
279 ("note" "Remarks to be put at the end of the \\bibitem")))))
280 ("Unpublished" . (((("author" "Author1 [and Author2 ...] [and others]")
281 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)")
282 ("note" "Remarks to be put at the end of the \\bibitem"))
283 (("year" "Year of publication")
284 ("month" "Month of the publication as a string (remove braces)")))))
285 )
286
287 "Defines reference types and their associated fields.
288 List of
289 (entry-name (required optional) (crossref-required crossref-optional))
290 triples.
291 If the third element is nil, the first pair is always to be used.
292 If not, the second pair is to be used in the case of presence of a
293 crossref field and the third in the case of absence.
294 Required , optional, crossref-required and crossref-optional are lists.
295 Each element of these lists is a list of strings with two elements
296 (first element = name of the field,
297 second element = comment to appear in the echo area).")
298
299 (defvar bibtex-predefined-strings
300 '(
301 ("jan") ("feb") ("mar") ("apr") ("may") ("jun") ("jul") ("aug")
302 ("sep") ("oct") ("nov") ("dec")
303 ("acmcs") ("acta") ("cacm") ("ibmjrd") ("ibmsj") ("ieeese")
304 ("ieeetc") ("ieeetcad") ("ipl") ("jacm") ("jcss") ("scp")
305 ("sicomp") ("tcs") ("tocs") ("tods") ("tog") ("toms") ("toois")
306 ("toplas")
307 )
308 "Alist of string definitions.
309 Should contain the strings defined in the BibTeX style files. Each
310 element is a list with just one element: the string.")
311
312 (defvar bibtex-string-files nil
313 "*List of BibTeX files containing string definitions.
314 Those files must be specified using pathnames relative to the
315 directories specified in $BIBINPUTS. This variable is only evaluated
316 when bibtex-mode is entered (i. e. when loading the BibTeX file).")
317
318 (defvar bibtex-help-message t
319 "*If not nil print help messages in the echo area on entering a new field.")
320
321 (defvar bibtex-autokey-names 1
322 "*Number of names to use for the automatically generated reference key.
323 If this is set to anything but a number, all names are used.
324 See the documentation of function bibtex-generate-autokey for further detail.")
325
326 (defvar bibtex-autokey-name-change-strings
327 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
328 ("\\\\\\\"s" "ss")
329 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
330 ("{" "") ("}" ""))
331 "Alist of (old-regexp new-string) pairs.
332 Any part of name matching a old-regexp is replaced by new-string.
333 Case of the old-regexp is significant. All regexps are tried in the
334 order in which they appear in the list, so be sure to avoid recursion here.
335 See the documentation of function bibtex-generate-autokey for further detail.")
336
337 (defvar bibtex-autokey-name-length 'infty
338 "*Number of characters from name to incorporate into key.
339 If this is set to anything but a number, all characters are used.
340 See the documentation of function bibtex-generate-autokey for further detail.")
341
342 (defvar bibtex-autokey-name-separator ""
343 "*String that comes between any two names in the key.
344 See the documentation of function bibtex-generate-autokey for further detail.")
345
346 (defvar bibtex-autokey-year-length 2
347 "*Number of rightmost digits from the year field yo incorporate into key.
348 See the documentation of function bibtex-generate-autokey for further detail.")
349
350 (defvar bibtex-autokey-titlewords 5
351 "*Number of title words to use for the automatically generated reference key.
352 If this is set to anything but a number, all title words are used.
353 See the documentation of function bibtex-generate-autokey for further detail.")
354
355 (defvar bibtex-autokey-title-terminators
356 '("\\." "!" "\\?" ":" ";" "---")
357 "*Regexp list defining the termination of the main part of the title.
358 Case of the regexps is ignored.
359 See the documentation of function bibtex-generate-autokey for further detail.")
360
361 (defvar bibtex-autokey-titlewords-stretch 2
362 "*Number of words that can additionally be used from the title.
363 These words are used only, if a sentence from the title can be ended then.
364 See the documentation of function bibtex-generate-autokey for further detail.")
365
366 (defvar bibtex-autokey-titleword-first-ignore
367 '("a" "an" "on" "the" "eine?" "der" "die" "das")
368 "*Determines words that may begin a title but are not to be used in the key.
369 Each item of the list is a regexp. If the first word of the title matchs a
370 regexp from that list, it is not included in the title, even if it is
371 capitalized. Regexps in the list must be entered using lowercase letters.")
372
373 (defvar bibtex-autokey-titleword-abbrevs nil
374 "*Determines exceptions to the usual abbreviation mechanism.
375 A list of (old-regexp new-string) pairs.
376 Use all lowercase letters for old-regexp.
377 See the documentation of function bibtex-generate-autokey for further detail.")
378
379 (defvar bibtex-autokey-titleword-change-strings
380 '(("\\\\\\\"a" "ae") ("\\\\\\\"o" "oe") ("\\\\\\\"u" "ue")
381 ("\\\\\\\"s" "ss")
382 ("\\\\\\\"A" "Ae") ("\\\\\\\"O" "Oe") ("\\\\\\\"U" "Ue")
383 ("{" "") ("}" ""))
384 "Alist of (old-regexp new-string) pairs.
385 Any part of title word matching a old-regexp is replaced by new-string.
386 Case of the old-regexp is significant.
387 See the documentation of function bibtex-generate-autokey for further detail.")
388
389 (defvar bibtex-autokey-titleword-length 5
390 "*Number of characters from title words to incorporate into key.
391 If this is set to anything but a number, all characters are used.
392 See the documentation of function bibtex-generate-autokey for further detail.")
393
394 (defvar bibtex-autokey-titleword-separator "_"
395 "*String to be put between the title words.
396 See the documentation of function bibtex-generate-autokey for further detail.")
397
398 (defvar bibtex-autokey-name-year-separator ""
399 "*String to be put between name part and year part of key.
400 See the documentation of function bibtex-generate-autokey for further detail.")
401
402 (defvar bibtex-autokey-year-title-separator ":_"
403 "*String to be put between name part and year part of key.
404 See the documentation of function bibtex-generate-autokey for further detail.")
405
406 (defvar bibtex-autokey-edit-before-use t
407 "*If non-nil, user is allowed to edit the generated key before it is used.")
408
409 (defvar bibtex-font-lock-keywords
410 (list
411 "^@[A-Za-z]*[({]"
412 ;; reference type
413 '("^\\([ \t]*OPT[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
414 1 font-lock-comment-face)
415 ;; optional field names
416 '("^\\([ \t]*[A-Za-z_-][A-Za-z0-9_-]*\\)[ \t]*="
417 1 font-lock-function-name-face)
418 ;; field names
419 '("^@[A-Za-z]*[({]\\([^\n,]*\\),"
420 1 font-lock-string-face)
421 ;; reference labels
422 )
423 "*Fonts to use in BibTeX mode")
424
425 \f
426 ;; Syntax Table, Keybindings and BibTeX Entry List
427 (defvar bibtex-mode-syntax-table
428 (let ((st (make-syntax-table)))
429 ;; [alarson:19920214.1004CST] make double quote a string quote
430 (modify-syntax-entry ?\" "\"" st)
431 (modify-syntax-entry ?$ "$$ " st)
432 (modify-syntax-entry ?% "< " st)
433 (modify-syntax-entry ?' "w " st)
434 (modify-syntax-entry ?@ "w " st)
435 (modify-syntax-entry ?\\ "\\" st)
436 (modify-syntax-entry ?\f "> " st)
437 (modify-syntax-entry ?\n "> " st)
438 (modify-syntax-entry ?~ " " st)
439 st))
440
441 (defvar bibtex-mode-map
442 (let ((km (make-sparse-keymap)))
443
444 (define-key km "\t" 'bibtex-find-text)
445 (define-key km "\n" 'bibtex-next-field)
446 (define-key km "\M-\t" 'bibtex-complete-string)
447 (define-key km "\C-c\"" 'bibtex-remove-double-quotes-or-braces)
448 (define-key km "\C-c{" 'bibtex-remove-double-quotes-or-braces)
449 (define-key km "\C-c}" 'bibtex-remove-double-quotes-or-braces)
450 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
451 (define-key km "\C-c?" 'bibtex-print-help-message)
452 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
453 (define-key km "\C-c\C-n" 'bibtex-pop-next)
454 (define-key km "\C-c\C-k" 'bibtex-kill-optional-field)
455 (define-key km "\C-c\C-d" 'bibtex-empty-field)
456 (define-key km "\C-c$" 'bibtex-ispell-entry)
457 (define-key km "\M-\C-a" 'bibtex-beginning-of-entry)
458 (define-key km "\M-\C-e" 'bibtex-end-of-entry)
459 (define-key km "\C-c\C-b" 'bibtex-entry)
460 (define-key km "\C-c\C-q" 'bibtex-hide-entry-bodies)
461 (define-key km "\C-c\C-rn" 'bibtex-narrow-to-entry)
462 (define-key km "\C-c\C-rw" 'widen)
463 (define-key km "\C-c\C-o" 'bibtex-remove-OPT)
464
465 (define-key km "\C-c\C-e\C-i" 'bibtex-InProceedings)
466 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
467 (define-key km "\C-c\C-eI" 'bibtex-InBook)
468 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
469 (define-key km "\C-c\C-e\C-b" 'bibtex-InBook)
470 (define-key km "\C-c\C-eb" 'bibtex-Book)
471 (define-key km "\C-c\C-eB" 'bibtex-Booklet)
472 (define-key km "\C-c\C-e\C-c" 'bibtex-InCollection)
473 (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
474 (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
475 (define-key km "\C-c\C-eM" 'bibtex-Misc)
476 (define-key km "\C-c\C-e\C-p" 'bibtex-InProceedings)
477 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
478 (define-key km "\C-c\C-eP" 'bibtex-PhdThesis)
479 (define-key km "\C-c\C-e\M-p" 'bibtex-preamble)
480 (define-key km "\C-c\C-e\C-s" 'bibtex-string)
481 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
482 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
483 km))
484
485 (define-key bibtex-mode-map [menu-bar bibtex-edit]
486 (cons "BibTeX-Edit" (make-sparse-keymap "BibTeX-Edit")))
487 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-print-help-message]
488 '("Help about Current Field" . bibtex-print-help-message))
489 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-complete-string]
490 '("String Complete" . bibtex-complete-string))
491 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-next-field]
492 '("Next Field" . bibtex-next-field))
493 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-find-text]
494 '("End of Field" . bibtex-find-text))
495 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-pop-previous]
496 '("Snatch from Similar Preceding Field" . bibtex-pop-previous))
497 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-pop-next]
498 '("Snatch from Similar Following Field" . bibtex-pop-next))
499 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-remove-OPT]
500 '("Remove OPT" . bibtex-remove-OPT))
501 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-remove-double-quotes-or-braces]
502 '("Remove Quotes or Braces" . bibtex-remove-double-quotes-or-braces))
503 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-clean-entry]
504 '("Clean Up Entry" . bibtex-clean-entry))
505 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-sort-entries]
506 '("Sort Entries" . bibtex-sort-entries))
507 (define-key bibtex-mode-map [menu-bar bibtex-edit bibtex-validate-buffer]
508 '("Validate Entries" . bibtex-validate-buffer))
509
510 (define-key bibtex-mode-map [menu-bar entry-types]
511 (cons "Entry-Types" (make-sparse-keymap "Entry-Types")))
512 (define-key bibtex-mode-map [menu-bar entry-types bibtex-preamble]
513 '("Preamble" . bibtex-preamble))
514 (define-key bibtex-mode-map [menu-bar entry-types bibtex-string]
515 '("String" . bibtex-string))
516 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Misc]
517 '("Miscellaneous" . bibtex-Misc))
518 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Unpublished]
519 '("Unpublished" . bibtex-Unpublished))
520 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Manual]
521 '("Technical Manual" . bibtex-Manual))
522 (define-key bibtex-mode-map [menu-bar entry-types bibtex-TechReport]
523 '("Technical Report" . bibtex-TechReport))
524 (define-key bibtex-mode-map [menu-bar entry-types bibtex-MastersThesis]
525 '("Master's Thesis" . bibtex-MastersThesis))
526 (define-key bibtex-mode-map [menu-bar entry-types bibtex-PhdThesis]
527 '("PhD. Thesis" . bibtex-PhdThesis))
528 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Booklet]
529 '("Booklet (Bound, but no Publisher/Institution)" . bibtex-Booklet))
530 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Book]
531 '("Book" . bibtex-Book))
532 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Proceedings]
533 '("Conference Proceedings" . bibtex-Proceedings))
534 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InBook]
535 '("Chapter or Pages in a Book" . bibtex-InBook))
536 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InCollection]
537 '("Article in a Collection" . bibtex-InCollection))
538 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InProceedings]
539 '("Article in Conference Proceedings" . bibtex-InProceedings))
540 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Article]
541 '("Article in Journal" . bibtex-Article))
542
543
544 \f
545 ;; Internal Variables
546
547 (defvar bibtex-pop-previous-search-point nil)
548 ;; Next point where bibtex-pop-previous starts looking for a similar
549 ;; entry.
550
551 (defvar bibtex-pop-next-search-point nil)
552 ;; Next point where bibtex-pop-next starts looking for a similar entry.
553
554 (defvar bibtex-completion-candidates nil)
555 ;; Candidates for bibtex-complete-string. Initialized from
556 ;; bibtex-predefined-strings and bibtex-string-files. This variable is
557 ;; buffer-local.
558 (make-variable-buffer-local 'bibtex-completion-candidates)
559
560 (defvar bibtex-keys nil)
561 ;; Candidates for TAB completion when entering a reference key using
562 ;; the minibuffer. Initialized in bibtex-mode and updated for each
563 ;; new entry. This variable is buffer-local.
564 (make-variable-buffer-local 'bibtex-keys)
565
566 (defvar bibtex-buffer-last-parsed-for-keys-tick nil)
567 ;; Remembers the value returned by buffer-modified-tick when buffer
568 ;; was parsed for keys the last time.
569 (make-variable-buffer-local 'bibtex-keys)
570
571
572 \f
573 ;; Functions to Parse the BibTeX Entries
574
575 (defun bibtex-cfield (name text)
576 ;; Create a regexp for a BibTeX field of name NAME and text TEXT.
577 (concat ",[ \t\n]*\\("
578 name
579 "\\)[ \t\n]*=[ \t\n]*\\("
580 text
581 "\\)"))
582 (defconst bibtex-name-in-cfield 1)
583 ;; The regexp subexpression number of the name part in bibtex-cfield.
584
585 (defconst bibtex-text-in-cfield 2)
586 ;; The regexp subexpression number of the text part in bibtex-cfield.
587
588 (defconst bibtex-field-name "[A-Za-z_-][A-Za-z0-9_-]*")
589 ;; Regexp defining the name part of a BibTeX field.
590
591 (defconst bibtex-field-const "[0-9A-Za-z][A-Za-z0-9:_+-]*"
592 "Format of a bibtex field constant.")
593
594 (defconst bibtex-field-string
595 (concat
596 "\\("
597 "{\\(\\({\\(\\({[^}]*}\\)\\|\\([^{}]\\)\\)*}\\)\\|\\([^{}]\\)\\)*}"
598 ;; maximal twice nested {}
599 "\\)\\|\\("
600 "\"[^\"]*[^\\\\]\"\\|\"\""
601 "\\)"))
602 ;; Match either a string or an empty string.
603
604 (defconst bibtex-field-string-or-const
605 (concat bibtex-field-const "\\|" bibtex-field-string))
606 ;; Match either bibtex-field-string or bibtex-field-const.
607
608 (defconst bibtex-field-text
609 (concat
610 "\\(" bibtex-field-string-or-const "\\)"
611 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*"))
612 ;; Regexp defining the text part of a BibTeX field: either a string,
613 ;; or an empty string, or a constant followed by one or more # /
614 ;; constant pairs.
615
616 (defconst bibtex-field
617 (bibtex-cfield bibtex-field-name bibtex-field-text))
618 ;; Regexp defining the format of a BibTeX field.
619
620 (defconst bibtex-name-in-field bibtex-name-in-cfield)
621 ;; The regexp subexpression number of the name part in BibTeX-field.
622
623 (defconst bibtex-text-in-field bibtex-text-in-cfield)
624 ;; The regexp subexpression number of the text part in BibTeX-field.
625
626 (defconst bibtex-reference-type "@[A-Za-z]+")
627 ;; Regexp defining the type part of a BibTeX reference entry.
628
629 (defconst bibtex-reference-key "[A-Za-z][A-Za-z0-9.:;?!`'/*@_+-]*")
630 ;; Regexp defining the label part of a BibTeX reference entry.
631
632 (defconst bibtex-reference-head
633 (concat "^\\( \\|\t\\)*\\("
634 bibtex-reference-type
635 "\\)[ \t]*[({]\\("
636 bibtex-reference-key
637 "\\)"))
638 ;; Regexp defining format of the header line of a BibTeX reference
639 ;; entry.
640
641 (defconst bibtex-reference-maybe-empty-head
642 (concat bibtex-reference-head "?"))
643 ;; Regexp defining format of the header line of a maybe empty
644 ;; BibTeX reference entry (without reference key).
645
646 (defconst bibtex-type-in-head 2)
647 ;; The regexp subexpression number of the type part in
648 ;; bibtex-reference-head.
649
650 (defconst bibtex-key-in-head 3)
651 ;; The regexp subexpression number of the key part in
652 ;; bibtex-reference-head.
653
654 (defconst bibtex-reference
655 (concat bibtex-reference-head
656 "\\([ \t\n]*" bibtex-field "\\)*"
657 "[ \t\n]*,?[ \t\n]*[})]"))
658 ;; Regexp defining the format of a BibTeX reference entry.
659
660 (defconst bibtex-type-in-reference bibtex-type-in-head)
661 ;; The regexp subexpression number of the type part in
662 ;; bibtex-reference.
663
664 (defconst bibtex-key-in-reference bibtex-key-in-head)
665 ;; The regexp subexpression number of the key part in
666 ;; bibtex-reference.
667
668 (defconst bibtex-string
669 (concat "^[ \t]*@[sS][tT][rR][iI][nN][gG][ \t\n]*[({][ \t\n]*\\("
670 bibtex-reference-key
671 "\\)[ \t\n]*=[ \t\n]*\\("
672 bibtex-field-text
673 "\\)[ \t\n]*[})]"))
674 ;; Regexp defining the format of a BibTeX string entry.
675
676 (defconst bibtex-key-in-string 1)
677 ;; The regexp subexpression of the name part in bibtex-string.
678
679 (defconst bibtex-text-in-string 2)
680 ;; The regexp subexpression of the text part in bibtex-string.
681
682 (defconst bibtex-name-alignment 2)
683 ;; Alignment for the name part in BibTeX fields. Chosen on aesthetic
684 ;; grounds only.
685
686 (defconst bibtex-text-alignment (length " organization = "))
687 ;; Alignment for the text part in BibTeX fields. Equal to the space
688 ;; needed for the longest name part.
689
690
691 \f
692 ;; Helper Functions
693
694 (defun assoc-ignore-case (string alist)
695 ;; Return non-nil if STRING is `equal' to the car of an element of
696 ;; LIST. Comparison is done with case ignored. The value is actually
697 ;; the element of LIST whose car is `equal' to STRING.
698 (or (assoc string alist)
699 (while (and alist
700 (not (string-equal
701 (downcase string)
702 (downcase (car (car alist))))))
703 (setq alist (cdr alist)))
704 (car alist)))
705
706 (defun member-of-regexp (string list)
707 ;; Return non-nil if STRING is exactly matched by an element of
708 ;; LIST. This function is influenced by the actual value of
709 ;; `case-fold-search'. The value is actually the tail of LIST whose
710 ;; car matches STRING.
711 (while
712 (and
713 list
714 (not
715 (string-match
716 (concat "^" (car list) "$")
717 string)))
718 (setq list (cdr list)))
719 list)
720
721 (defun assoc-of-regexp (string alist)
722 ;; Return non-nil if STRING is exactly matched by the car of an
723 ;; element of LIST. This function is influenced by the actual value
724 ;; of `case-fold-search'. The value is actually the element of LIST
725 ;; whose car matches STRING.
726 (while
727 (and
728 alist
729 (not
730 (string-match
731 (concat "^" (car (car alist)) "$")
732 string)))
733 (setq alist (cdr alist)))
734 (car alist))
735
736 (defun skip-whitespace-and-comments ()
737 (let ((md (match-data)))
738 (unwind-protect
739 (while (cond ((looking-at "\\s>+\\|\\s +")
740 ;; was whitespace
741 ;; NOTE: also checked end-comment. In latex and
742 ;; lisp modes, newline is an end comment, but it
743 ;; should also be a whitespace char.
744 (goto-char (match-end 0)))
745 ;; If looking at beginning of comment, skip to end.
746 ((looking-at "\\s<")
747 (re-search-forward "\\s>"))))
748 (store-match-data md))))
749
750 (defun map-bibtex-entries (fun)
751 ;; Call FUN for each BibTeX entry starting with the current. Do this
752 ;; to the end of the file. FUN is called with one argument, the key
753 ;; of the entry, and with point inside the entry. If
754 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be called
755 ;; for @string entries.
756 (bibtex-beginning-of-entry)
757 (while (re-search-forward bibtex-reference-head nil t)
758 (if (and bibtex-sort-ignore-string-entries
759 (string-equal "@string"
760 (downcase (buffer-substring-no-properties
761 (match-beginning bibtex-type-in-head)
762 (match-end bibtex-type-in-head)))))
763 nil
764 (funcall fun (buffer-substring-no-properties
765 (match-beginning bibtex-key-in-head)
766 (match-end bibtex-key-in-head))))))
767
768 (defun bibtex-flash-head ()
769 ;; Flash at BibTeX reference head before point, if exists.
770 (let ((flash))
771 (cond ((re-search-backward bibtex-reference-head (point-min) t)
772 (goto-char (match-beginning bibtex-type-in-head))
773 (setq flash (match-end bibtex-key-in-reference)))
774 (t
775 (end-of-line)
776 (skip-chars-backward " \t")
777 (setq flash (point))
778 (beginning-of-line)
779 (skip-chars-forward " \t")))
780 (if (pos-visible-in-window-p (point))
781 (sit-for 1)
782 (message "From: %s"
783 (buffer-substring (point) flash)))))
784
785 (defun bibtex-move-outside-of-entry ()
786 ;; Make sure we are outside of a BibTeX entry.
787 (cond ((or
788 (= (point) (point-max))
789 (= (point) (point-min))
790 (looking-at "[ \n]*@")
791 )
792 t)
793 (t
794 (backward-paragraph)
795 (forward-paragraph)))
796 (re-search-forward "[ \t\n]*" (point-max) t))
797
798 (defun beginning-of-first-bibtex-entry ()
799 ;; Go to the beginning of the first BibTeX entry in buffer.
800 (goto-char (point-min))
801 (cond
802 ((re-search-forward "^@" nil 'move)
803 (beginning-of-line))
804 ((and (bobp) (eobp))
805 nil)
806 (t
807 (message "Warning: No BibTeX entries found!"))))
808
809 (defun bibtex-inside-field ()
810 ;; Try to avoid point being at end of a BibTeX field.
811 (end-of-line)
812 (skip-chars-backward " \t")
813 (cond ((= (preceding-char) ?,)
814 (forward-char -2)))
815 (cond ((or
816 (= (preceding-char) ?})
817 (= (preceding-char) ?\"))
818 (forward-char -1))))
819
820 (defun bibtex-enclosing-field ()
821 ;; Search for BibTeX field enclosing point. Point moves to end of
822 ;; field; also, use match-beginning and match-end to parse the field.
823 ;; sct@dcs.edinburgh.ac.uk
824 (let ((old-point (point)))
825 (condition-case errname
826 (bibtex-enclosing-regexp bibtex-field)
827 (search-failed
828 (goto-char old-point)
829 (error "Can't find enclosing BibTeX field.")))))
830
831 (defun bibtex-enclosing-reference ()
832 ;; Search for BibTeX reference enclosing point. Point moves to
833 ;; beginning of reference. Beginning/end of reference is given by
834 ;; (match-beginning/match-end 0).
835 (let ((old-point (point)))
836 (if (not
837 (re-search-backward bibtex-reference-head (point-min) t))
838 (progn
839 (error "Can't find enclosing BibTeX reference.")
840 (goto-char old-point)))
841 (goto-char (match-beginning bibtex-type-in-head))
842 (let ((pnt (point)))
843 (if (not
844 (re-search-forward bibtex-reference (point-max) t))
845 (progn
846 (error "Can't find enclosing BibTeX reference.")
847 (goto-char old-point))
848 (goto-char pnt)))))
849
850 (defun bibtex-enclosing-reference-maybe-empty-head ()
851 ;; Search for BibTeX reference enclosing point. Point moves to
852 ;; beginning of reference. Beginning/end of reference is given by
853 ;; (match-beginning/match-end 0).
854 (let ((old-point (point)))
855 (if (not
856 (re-search-backward
857 bibtex-reference-maybe-empty-head (point-min) t))
858 (progn
859 (error "Can't find enclosing BibTeX reference.")
860 (goto-char old-point)))
861 (goto-char (match-beginning bibtex-type-in-head))
862 (let ((pnt (point)))
863 (if (not
864 (re-search-forward
865 (concat
866 bibtex-reference-maybe-empty-head
867 "\\([ \t\n]*" bibtex-field "\\)*"
868 "[ \t\n]*,?[ \t\n]*[})]")
869 (point-max) t))
870 (progn
871 (error "Can't find enclosing BibTeX reference.")
872 (goto-char old-point))
873 (goto-char pnt)))))
874
875 (defun bibtex-enclosing-regexp (regexp)
876 ;; Search for REGEXP enclosing point. Point moves to end of
877 ;; REGEXP. See also match-beginning and match-end. If an enclosing
878 ;; REGEXP is not found, signals search-failed; point is left in an
879 ;; undefined location.
880 ;; Doesn't something like this exist already?
881 ; compute reasonable limits for the loop
882 (let* ((initial (point))
883 (right (if (re-search-forward regexp (point-max) t)
884 (match-end 0)
885 (point-max)))
886 (left
887 (progn
888 (goto-char initial)
889 (if (re-search-backward regexp (point-min) t)
890 (match-beginning 0)
891 (point-min)))))
892 ; within the prescribed limits, loop until a match is found
893 (goto-char left)
894 (re-search-forward regexp right nil 1)
895 (if (> (match-beginning 0) initial)
896 (signal 'search-failed (list regexp)))
897 (while (<= (match-end 0) initial)
898 (re-search-forward regexp right nil 1)
899 (if (> (match-beginning 0) initial)
900 (signal 'search-failed (list regexp))))
901 ))
902
903 (defun bibtex-autokey-change (string change-list)
904 ;; Returns a string where some regexps are changed according to
905 ;; change-list. Every item of change-list is an (old-regexp
906 ;; new-string) pair.
907 (let ((return-string string)
908 case-fold-search
909 (index 0)
910 (len (length change-list))
911 change-item)
912 (while (< index len)
913 (setq change-item (elt change-list index))
914 (while (string-match (car change-item) return-string)
915 (setq
916 return-string
917 (concat (substring return-string 0 (match-beginning 0))
918 (elt change-item 1)
919 (substring return-string (match-end 0)))))
920 (setq index (1+ index)))
921 return-string))
922
923 (defun bibtex-autokey-abbrev (string len)
924 ;; Returns an abbreviation of string with at least len
925 ;; characters. String is aborted only after a consonant or at the
926 ;; word end. If len is not a number, string is returned unchanged.
927 (let* ((string-length (length string))
928 (len (if (numberp len)
929 (min len string-length)
930 len))
931 (return-string (if (numberp len)
932 (substring string 0 len)))
933 (index len)
934 (vowels '(?a ?e ?i ?o ?u ?A ?E ?I ?O ?U)))
935 (if (numberp len)
936 (progn
937 (while (and
938 (< index string-length)
939 (member (elt return-string
940 (1- (length return-string)))
941 vowels))
942 (setq return-string (concat return-string
943 (substring
944 string index (1+ index)))
945 index (1+ index)))
946 return-string)
947 string)))
948
949 (defun bibtex-generate-autokey ()
950 "Generates automatically a key from the author/editor and the title field.
951 The generation algorithm works as follows:
952 1. If there is a non-empty author (preferred) or editor field,
953 use it for the name part of the key.
954 2. Change any substring found in `bibtex-autokey-name-change-strings'
955 to the corresponding new one (see documentation of this variable
956 for further detail).
957 3. For every of the first `bibtex-autokey-names' names in the
958 \"name\" field, determine the last name.
959 4. From every last name, take at least `bibtex-autokey-name-length'
960 characters (abort only after a consonant or at a word end).
961 5. Build the name part of the key by concatenating all abbreviated last
962 names with the string `bibtex-autokey-name-separator' between
963 any two.
964 6. Build the year part of the key by truncating the contents of the
965 \"year\" field to the rightmost `bibtex-autokey-year-length'
966 digits (useful values are 2 and 4).
967 7. For the title part of the key change the contents of the \"title\"
968 field of the reference according to
969 `bibtex-autokey-titleword-change-strings' to the corresponding
970 new one (see documentation of this variable for further detail).
971 8. Abbreviate the result to the string up to (but not including) the
972 first occurence of a regexp matched by the items of
973 `bibtex-autokey-title-terminators' and delete the first
974 word if it appears in `bibtex-autokey-titleword-first-ignore'.
975 Build the title part of the key by using at least the first
976 `bibtex-autokey-titlewords' capitalized words from this
977 abbreviated title. If the abbreviated title ends after maximal
978 `bibtex-autokey-titlewords' + `bibtex-autokey-titlewords-stretch'
979 capitalized words, all capitalized words from the abbreviated title
980 are used.
981 9. For every used title word that appears in
982 `bibtex-autokey-titleword-abbrevs' use the corresponding abbreviation
983 (see documentation of this variable for further detail).
984 10. From every title word not generated by an abbreviation, take at
985 least `bibtex-autokey-titleword-length' characters (abort only after
986 a consonant or at a word end).
987 11. Build the title part of the key by concatenating all abbreviated
988 title words with the string `bibtex-autokey-titleword-separator'
989 between any two.
990 12. At least, to get the key, concatenate the name part, the year part
991 and the title part with `bibtex-autokey-name-year-separator'
992 between the name and the year if both are non-empty and
993 `bibtex-autokey-year-title-separator' between the year and
994 the title if both are non-empty."
995
996 (let* ((pnt (point))
997 (min
998 (progn
999 (bibtex-beginning-of-entry)
1000 (point)))
1001 (max
1002 (progn
1003 (bibtex-end-of-entry)
1004 (point)))
1005 (namefield
1006 (progn
1007 (goto-char min)
1008 (if (or
1009 (re-search-forward "^[ \t]*author[ \t]*=" max t)
1010 (re-search-forward "^[ \t]*editor[ \t]*=" max t))
1011 (let* (bibtex-help-message
1012 (start (progn
1013 (bibtex-find-text t)
1014 (point)))
1015 (end (progn
1016 (bibtex-find-text nil)
1017 (point))))
1018 (bibtex-autokey-change
1019 (buffer-substring-no-properties start end)
1020 bibtex-autokey-name-change-strings))
1021 "")))
1022 (namelist
1023 (mapcar
1024 (function
1025 (lambda (fullname)
1026 (bibtex-autokey-abbrev
1027 (if (string-match "," fullname)
1028 (substring fullname 0 (match-beginning 0))
1029 (progn
1030 (if (string-match " [^ ]*$" fullname)
1031 (substring
1032 fullname (1+ (match-beginning 0)))
1033 fullname)))
1034 bibtex-autokey-name-length)))
1035 ;; Gather all names into a list
1036 (let (names
1037 (counter 0))
1038 (while (and
1039 (not (equal namefield ""))
1040 (or
1041 (not (numberp bibtex-autokey-names))
1042 (< counter bibtex-autokey-names)))
1043 (if (string-match " and " namefield)
1044 (progn
1045 (setq
1046 names
1047 (append names
1048 (list
1049 (downcase
1050 (substring
1051 namefield 0 (match-beginning 0)))))
1052 namefield
1053 (substring namefield (match-end 0))))
1054 (setq names
1055 (append names (list (downcase namefield)))
1056 namefield ""))
1057 (setq counter (1+ counter)))
1058 names)))
1059 (namepart (mapconcat (function (lambda (name) name))
1060 namelist
1061 bibtex-autokey-name-separator))
1062 (yearfield
1063 (progn
1064 (goto-char min)
1065 (if (re-search-forward
1066 "^[ \t]*year[ \t]*=[ \t]*\\([0-9]*\\)" max t)
1067 (buffer-substring-no-properties
1068 (match-beginning 1) (match-end 1))
1069 "")))
1070 (yearpart
1071 (if (equal yearfield "")
1072 ""
1073 (substring yearfield
1074 (- (length yearfield)
1075 bibtex-autokey-year-length))))
1076 (titlestring
1077 (let ((case-fold-search t)
1078 (titlefield
1079 (progn
1080 (goto-char min)
1081 (if (re-search-forward
1082 "^[ \t]*title[ \t]*=" max t)
1083 (let* (bibtex-help-message
1084 (start (progn
1085 (bibtex-find-text t)
1086 (point)))
1087 (end (progn
1088 (bibtex-find-text nil)
1089 (point))))
1090 (bibtex-autokey-change
1091 (buffer-substring-no-properties start end)
1092 bibtex-autokey-titleword-change-strings))
1093 "")))
1094 case-fold-search
1095 (index 0)
1096 (numberofitems
1097 (length bibtex-autokey-title-terminators)))
1098 (while (< index numberofitems)
1099 (if (string-match
1100 (elt bibtex-autokey-title-terminators index)
1101 titlefield)
1102 (setq titlefield
1103 (substring titlefield 0 (match-beginning 0))))
1104 (setq index (1+ index)))
1105 titlefield))
1106 (titlelist
1107 (mapcar
1108 (function
1109 (lambda (titleword)
1110 (let ((abbrev
1111 (assoc-of-regexp
1112 titleword bibtex-autokey-titleword-abbrevs)))
1113 (if abbrev
1114 (elt abbrev 1)
1115 (bibtex-autokey-abbrev
1116 titleword
1117 bibtex-autokey-titleword-length)))))
1118 ;; Gather all titlewords into a list
1119 (let (titlewords
1120 titlewords-extra
1121 case-fold-search
1122 (counter 0)
1123 (first t))
1124 (while (and
1125 (not (equal titlestring ""))
1126 (or
1127 (not (numberp bibtex-autokey-titlewords))
1128 (< counter (+
1129 bibtex-autokey-titlewords
1130 bibtex-autokey-titlewords-stretch))))
1131 (if (string-match "\\b[A-Z][A-Za-z0-9]*" titlestring)
1132 (let* ((end-match (match-end 0))
1133 (titleword
1134 (downcase (substring titlestring
1135 (match-beginning 0)
1136 end-match))))
1137 (if (or
1138 (not (numberp bibtex-autokey-titlewords))
1139 (< counter bibtex-autokey-titlewords))
1140 (if (and
1141 first
1142 (member-of-regexp
1143 titleword
1144 bibtex-autokey-titleword-first-ignore))
1145 (setq counter -1)
1146 (setq titlewords
1147 (append titlewords (list titleword))))
1148 (setq
1149 titlewords-extra
1150 (append titlewords-extra (list titleword))))
1151 (setq titlestring
1152 (substring titlestring end-match)))
1153 (setq titlestring ""))
1154 (setq first nil
1155 counter (1+ counter)))
1156 (if (string-match "\\b[A-Z][^ ]*\\b" titlestring)
1157 titlewords
1158 (append titlewords titlewords-extra)))))
1159 (titlepart (mapconcat (function (lambda (name) name))
1160 titlelist
1161 bibtex-autokey-titleword-separator))
1162 (autokey
1163 (concat
1164 namepart
1165 (if (not
1166 (or
1167 (equal namepart "")
1168 (equal yearpart "")))
1169 bibtex-autokey-name-year-separator)
1170 yearpart
1171 (if (not
1172 (or
1173 (and
1174 (equal namepart "")
1175 (equal yearpart ""))
1176 (equal titlepart "")))
1177 bibtex-autokey-year-title-separator)
1178 titlepart)))
1179 (goto-char pnt)
1180 autokey))
1181
1182 (defun bibtex-parse-keys (add &optional abortable)
1183 ;; Sets bibtex-keys to the keys used in the whole (possibly
1184 ;; restricted) buffer (either as entry keys or as crossref entries).
1185 ;; If ADD is non-nil adds the new keys to bibtex-keys instead of
1186 ;; simply resetting it. If ABORTABLE is non-nil abort on user input.
1187 (if bibtex-maintain-sorted-entries
1188 (let ((labels (if add
1189 bibtex-keys))
1190 label
1191 (case-fold-search t))
1192 (save-excursion
1193 (goto-char (point-min))
1194 (if (not add)
1195 (message "Parsing reference keys..."))
1196
1197 (if (not
1198 (catch 'userkey
1199 (while
1200 (re-search-forward
1201 (concat
1202 "\\("
1203 bibtex-reference-head
1204 "\\)\\|\\("
1205 "^[ \t\n]*crossref[ \t\n]*=[ \t\n]*[{\"]\\([A-Za-z][]A-Za-z0-9.:;?!`'()/*@_+-]*\\)[}\"],?$"
1206 "\\)")
1207 nil t)
1208 (if (and
1209 abortable
1210 (input-pending-p))
1211 (throw 'userkey t))
1212 (if (match-beginning (1+ bibtex-key-in-head))
1213 (setq
1214 label
1215 (buffer-substring-no-properties
1216 (match-beginning (1+ bibtex-key-in-head))
1217 (match-end (1+ bibtex-key-in-head))))
1218 (setq
1219 label
1220 (buffer-substring-no-properties
1221 (match-beginning (+ 3 bibtex-key-in-head))
1222 (match-end (+ 3 bibtex-key-in-head)))))
1223 (if (not (assoc label labels))
1224 (setq labels
1225 (cons (list label) labels))))))
1226 (progn
1227 (setq
1228 bibtex-buffer-last-parsed-for-keys-tick
1229 (buffer-modified-tick))
1230 (if (not add)
1231 (message "Parsing reference keys... done"))
1232 (setq bibtex-keys labels)))))))
1233
1234 (defun bibtex-auto-fill-function ()
1235 (let ((fill-prefix (make-string (+ bibtex-text-alignment 1) ? )))
1236 (do-auto-fill)))
1237
1238
1239 \f
1240 ;; Interactive Functions:
1241
1242 ;;;###autoload
1243 (defun bibtex-mode ()
1244 "Major mode for editing BibTeX files.
1245
1246 \\{bibtex-mode-map}
1247
1248 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
1249
1250 The optional fields start with the string OPT, and thus ignored by BibTeX.
1251 The OPT string may be removed from a field with \\[bibtex-remove-OPT].
1252 \\[bibtex-kill-optional-field] kills the current optional field entirely.
1253 \\[bibtex-remove-double-quotes-or-braces] removes the double-quotes or
1254 braces around the text of the current field. \\[bibtex-empty-field]
1255 replaces the text of the current field with the default \"\" or {}.
1256
1257 The command \\[bibtex-clean-entry] cleans the current entry, i.e. (i) removes
1258 double-quotes or braces from entirely numerical fields, (ii) removes
1259 OPT from all non-empty optional fields, (iii) removes all empty
1260 optional fields, and (iv) checks that no non-optional fields are empty.
1261
1262 Use \\[bibtex-find-text] to position the cursor at the end of the current field.
1263 Use \\[bibtex-next-field] to move to end of the next field.
1264
1265 The following may be of interest as well:
1266
1267 Functions:
1268 bibtex-entry
1269 bibtex-print-help-message
1270 bibtex-beginning-of-entry
1271 bibtex-end-of-entry
1272 bibtex-ispell-abstract
1273 bibtex-narrow-to-entry
1274 bibtex-hide-entry-bodies
1275 bibtex-sort-entries
1276 bibtex-validate-buffer
1277 bibtex-pop-previous
1278 bibtex-pop-next
1279 bibtex-complete-string
1280
1281 Variables:
1282 bibtex-field-left-delimiter
1283 bibtex-field-right-delimiter
1284 bibtex-include-OPTcrossref
1285 bibtex-include-OPTkey
1286 bibtex-include-OPTannote
1287 bibtex-mode-user-optional-fields
1288 bibtex-clean-entry-zap-empty-opts
1289 bibtex-sort-ignore-string-entries
1290 bibtex-maintain-sorted-entries
1291 bibtex-entry-field-alist
1292 bibtex-predefined-strings
1293 bibtex-string-files
1294
1295 ---------------------------------------------------------
1296 Entry to this mode calls the value of bibtex-mode-hook if that value is
1297 non-nil."
1298 (interactive)
1299 (kill-all-local-variables)
1300 (use-local-map bibtex-mode-map)
1301 (setq major-mode 'bibtex-mode)
1302 (setq mode-name "BibTeX")
1303 (set-syntax-table bibtex-mode-syntax-table)
1304 (setq bibtex-completion-candidates bibtex-predefined-strings)
1305 (mapcar
1306 (function
1307 (lambda (filename)
1308 ;; collect pathnames
1309 (let* ((bib (getenv "BIBINPUTS"))
1310 (path (if bib
1311 bib
1312 "."))
1313 (dirs
1314 (mapcar
1315 (function
1316 (lambda (dirname) ;; strips off trailing slashes
1317 (let ((len (length dirname)))
1318 (if (equal (elt dirname (1- len)) "/")
1319 (substring dirname 0 (1- (1- len)))
1320 dirname))))
1321 (let (actdirs)
1322 (while (string-match ":" path)
1323 (setq actdirs
1324 (append actdirs
1325 (list (substring
1326 path 0
1327 (1- (match-end 0)))))
1328 path (substring path (match-end 0))))
1329 (append actdirs (list path)))))
1330 (filename (if (string-match "\.bib$" filename)
1331 filename
1332 (concat filename ".bib")))
1333 fullfilename
1334 (item 0)
1335 (size (length dirs)))
1336 ;; test filenames
1337 (while (and
1338 (< item size)
1339 (not (file-readable-p
1340 (setq fullfilename
1341 (concat (elt dirs item) "/" filename)))))
1342 (setq item (1+ item)))
1343 (if (< item size)
1344 ;; file was found
1345 (let ((curbuf (current-buffer))
1346 (bufname (make-temp-name ""))
1347 (compl bibtex-completion-candidates))
1348 (create-file-buffer bufname)
1349 (set-buffer bufname)
1350 (insert-file-contents fullfilename)
1351 (goto-char (point-min))
1352 (while (re-search-forward bibtex-string nil t)
1353 (setq
1354 compl
1355 (append
1356 compl
1357 (list
1358 (list (buffer-substring-no-properties
1359 (match-beginning bibtex-key-in-string)
1360 (match-end bibtex-key-in-string)))))))
1361 (kill-buffer bufname)
1362 (set-buffer curbuf)
1363 (setq bibtex-completion-candidates compl))
1364 (error "File %s not in $BIBINPUTS paths" filename)))))
1365 bibtex-string-files)
1366 (add-hook
1367 'auto-save-hook
1368 (function
1369 (lambda ()
1370 (if (and
1371 bibtex-maintain-sorted-entries
1372 (eq major-mode 'bibtex-mode)
1373 (not
1374 (eq (buffer-modified-tick)
1375 bibtex-buffer-last-parsed-for-keys-tick)))
1376 (bibtex-parse-keys nil t)))))
1377 (bibtex-parse-keys nil)
1378 (make-local-variable 'paragraph-start)
1379 (setq paragraph-start "[ \f\n\t]*$")
1380 (make-local-variable 'comment-start)
1381 (setq comment-start "%")
1382 (auto-fill-mode 1)
1383 (setq auto-fill-function 'bibtex-auto-fill-function)
1384 (set (make-local-variable 'font-lock-defaults)
1385 '(bibtex-font-lock-keywords nil t ((?$ . "\""))))
1386 (run-hooks 'bibtex-mode-hook))
1387
1388 (defun bibtex-entry (entry-type &optional required optional)
1389 "Inserts a new BibTeX entry.
1390 Calls the value of bibtex-add-entry-hook if that value is non-nil."
1391 (interactive (let* ((completion-ignore-case t)
1392 (e-t (completing-read
1393 "Entry Type: "
1394 bibtex-entry-field-alist
1395 nil t)))
1396 (list e-t)))
1397 (if (and (null required) (null optional))
1398 (let* ((e (assoc-ignore-case entry-type bibtex-entry-field-alist))
1399 (r-n-o (elt e 1))
1400 (c-ref (elt e 2)))
1401 (if (null e)
1402 (error "Bibtex entry type %s not defined!" entry-type))
1403 (if (and
1404 (member entry-type bibtex-include-OPTcrossref)
1405 c-ref)
1406 (setq required (elt c-ref 0)
1407 optional (elt c-ref 1))
1408 (setq required (elt r-n-o 0)
1409 optional (elt r-n-o 1)))))
1410 (let ((key
1411 (if bibtex-maintain-sorted-entries
1412 (completing-read
1413 (format "%s key: " entry-type)
1414 bibtex-keys))))
1415 (if bibtex-maintain-sorted-entries
1416 (bibtex-find-entry-location key)
1417 (bibtex-move-outside-of-entry))
1418 (insert "@" entry-type "{")
1419 (if key
1420 (insert key))
1421 (save-excursion
1422 (mapcar 'bibtex-make-field required)
1423 (if (member entry-type bibtex-include-OPTcrossref)
1424 (bibtex-make-optional-field '("crossref")))
1425 (if bibtex-include-OPTkey
1426 (bibtex-make-optional-field '("key")))
1427 (mapcar 'bibtex-make-optional-field optional)
1428 (mapcar 'bibtex-make-optional-field
1429 bibtex-mode-user-optional-fields)
1430 (if bibtex-include-OPTannote
1431 (bibtex-make-optional-field '("annote")))
1432 (insert "\n}\n\n"))
1433 (bibtex-next-field t)
1434 (run-hooks 'bibtex-add-entry-hook)))
1435
1436 (defun bibtex-print-help-message ()
1437 "Prints helpful information about current field in current BibTeX entry."
1438 (interactive)
1439 (let* ((pnt (point))
1440 (field-name
1441 (progn
1442 (beginning-of-line)
1443 (condition-case errname
1444 (bibtex-enclosing-regexp bibtex-field)
1445 (search-failed
1446 (goto-char pnt)
1447 (error "Not on BibTeX field")))
1448 (let ((mb (match-beginning bibtex-name-in-field))
1449 (me (match-end bibtex-name-in-field)))
1450 (goto-char mb)
1451 (buffer-substring-no-properties
1452 (if (looking-at "OPT")
1453 (+ 3 mb)
1454 mb)
1455 me))))
1456 (reference-type
1457 (progn
1458 (re-search-backward
1459 bibtex-reference-maybe-empty-head nil t)
1460 (buffer-substring-no-properties
1461 (1+ (match-beginning bibtex-type-in-head))
1462 (match-end bibtex-type-in-head))))
1463 (entry-list
1464 (assoc-ignore-case reference-type
1465 bibtex-entry-field-alist))
1466 (c-r-list (elt entry-list 2))
1467 (req-opt-list
1468 (if (and
1469 (member reference-type bibtex-include-OPTcrossref)
1470 c-r-list)
1471 c-r-list
1472 (elt entry-list 1)))
1473 (list-of-entries (append
1474 (elt req-opt-list 0)
1475 (elt req-opt-list 1)
1476 bibtex-mode-user-optional-fields
1477 (if (member
1478 reference-type
1479 bibtex-include-OPTcrossref)
1480 '(("crossref"
1481 "Label of the crossreferenced entry")))
1482 (if bibtex-include-OPTannote
1483 '(("annote"
1484 "Personal annotation (ignored)")))
1485 (if bibtex-include-OPTkey
1486 '(("key"
1487 "Key used for label creation if author and editor fields are missing"))))))
1488 (goto-char pnt)
1489 (if (assoc field-name list-of-entries)
1490 (message (elt (assoc field-name list-of-entries) 1))
1491 (message "NO COMMENT AVAILABLE"))))
1492
1493 (defun bibtex-make-field (e-t)
1494 "Makes a field named E-T in current BibTeX entry."
1495 (interactive "sBibTeX field name: ")
1496 (let ((name (if (consp e-t)
1497 (elt e-t 0)
1498 e-t)))
1499 (if (interactive-p)
1500 (progn
1501 (bibtex-find-text nil)
1502 (if (looking-at "[}\"]")
1503 (forward-char 1))))
1504 (insert ",\n")
1505 (indent-to-column bibtex-name-alignment)
1506 (insert name " = ")
1507 (indent-to-column bibtex-text-alignment)
1508 (insert bibtex-field-left-delimiter bibtex-field-right-delimiter)
1509 (if (interactive-p)
1510 (forward-char -1))))
1511
1512 (defun bibtex-make-optional-field (e-t)
1513 "Makes an optional field named E-T in current BibTeX entry."
1514 (if (consp e-t)
1515 (setq e-t (cons (concat "OPT" (car e-t)) (cdr e-t)))
1516 (setq e-t (concat "OPT" e-t)))
1517 (bibtex-make-field e-t))
1518
1519 (defun bibtex-beginning-of-entry ()
1520 "Move to beginning of BibTeX entry.
1521 If inside an entry, move to the beginning of it, otherwise move to the
1522 beginning of the previous entry."
1523 (interactive)
1524 (if (looking-at "^@")
1525 (forward-char))
1526 (re-search-backward "^@" nil 'move))
1527
1528 (defun bibtex-end-of-entry ()
1529 "Move to end of BibTeX entry.
1530 If inside an entry, move to the end of it, otherwise move to the end
1531 of the previous entry."
1532 (interactive)
1533 (bibtex-beginning-of-entry)
1534 (let ((parse-sexp-ignore-comments t))
1535 (forward-sexp 2) ;; skip entry type and body
1536 ))
1537
1538 (defun bibtex-ispell-entry ()
1539 "Spell whole BibTeX entry."
1540 (interactive)
1541 (ispell-region (progn (bibtex-beginning-of-entry) (point))
1542 (progn (bibtex-end-of-entry) (point))))
1543
1544 (defun bibtex-ispell-abstract ()
1545 "Spell abstract of BibTeX entry."
1546 (interactive)
1547 (let ((pnt (bibtex-end-of-entry)))
1548 (bibtex-beginning-of-entry)
1549 (if (null
1550 (re-search-forward "^[ \t]*[OPT]*abstract[ \t]*=" pnt))
1551 (error "No abstract in entry.")))
1552 (ispell-region (point)
1553 (save-excursion (forward-sexp) (point))))
1554
1555 (defun bibtex-narrow-to-entry ()
1556 "Narrow buffer to current BibTeX entry."
1557 (interactive)
1558 (save-excursion
1559 (narrow-to-region (progn (bibtex-beginning-of-entry) (point))
1560 (progn (bibtex-end-of-entry) (point)))))
1561
1562
1563 (defun bibtex-hide-entry-bodies (&optional arg)
1564 "Hide all lines between first and last BibTeX entries not beginning with @.
1565 With argument, show all text."
1566 (interactive "P")
1567 (save-excursion
1568 (beginning-of-first-bibtex-entry)
1569 ;; subst-char-in-region modifies the buffer, despite what the
1570 ;; documentation says...
1571 (let ((modifiedp (buffer-modified-p))
1572 (buffer-read-only nil))
1573 (if arg
1574 (subst-char-in-region (point) (point-max) ?\r ?\n t)
1575 (while (save-excursion (re-search-forward "\n[^@]" (point-max) t))
1576 ;; (save-excursion (replace-regexp "\n\\([^@]\\)" "\r\\1"))
1577 (save-excursion
1578 (while (re-search-forward "\n\\([^@]\\)" nil t)
1579 (replace-match "\r\\1" nil nil)))))
1580 (setq selective-display (not arg))
1581 (set-buffer-modified-p modifiedp))))
1582
1583 (defun bibtex-sort-entries ()
1584 "Sort BibTeX entries alphabetically by key.
1585 Text outside of BibTeX entries is not affected. If
1586 bibtex-sort-ignore-string-entries is non-nil, @string entries will be
1587 ignored."
1588 (interactive)
1589 (save-restriction
1590 (beginning-of-first-bibtex-entry)
1591 (narrow-to-region
1592 (point)
1593 (save-excursion
1594 (goto-char (point-max))
1595 (bibtex-end-of-entry)
1596 (point)))
1597 (if bibtex-sort-ignore-string-entries
1598 (if (re-search-forward bibtex-reference nil 'move)
1599 (goto-char (match-beginning 0))))
1600 (sort-subr
1601 nil
1602 ;; NEXTREC function
1603 (function
1604 (lambda ()
1605 (if bibtex-sort-ignore-string-entries
1606 (if (re-search-forward bibtex-reference nil 'move)
1607 (goto-char (match-beginning 0)))
1608 (if (re-search-forward bibtex-reference-head nil 'move)
1609 (goto-char (match-beginning 0))))))
1610 ;; ENDREC function
1611 'bibtex-end-of-entry
1612 ;; STARTKEY function
1613 (function
1614 (lambda ()
1615 (if bibtex-sort-ignore-string-entries
1616 (progn
1617 (re-search-forward bibtex-reference)
1618 (buffer-substring-no-properties
1619 (match-beginning bibtex-key-in-reference)
1620 (match-end bibtex-key-in-reference)))
1621 (re-search-forward bibtex-reference-head)
1622 (buffer-substring-no-properties
1623 (match-beginning bibtex-key-in-head)
1624 (match-end bibtex-key-in-head)))))
1625 ;; ENDKEY function
1626 nil)))
1627
1628 (defun bibtex-find-entry-location (entry-name &optional ignore-dups)
1629 "Looking for place to put the BibTeX entry named ENTRY-NAME.
1630 Performs a binary search (therefore, buffer is assumed to be in sorted
1631 order, without duplicates (see \\[bibtex-validate-buffer]), if it is
1632 not, bibtex-find-entry-location will fail). If entry-name is already
1633 used as a reference key, an error is signalled. However, if optional
1634 variable IGNORE-DUPS is non-nil, no error messages about duplicate
1635 entries are signalled, but the error handling is assumed to be made in
1636 the calling function. Nil is returned, if an duplicate entry error
1637 occured, and t in all other cases."
1638 (let* ((left
1639 (progn
1640 (beginning-of-first-bibtex-entry)
1641 (if bibtex-sort-ignore-string-entries
1642 (re-search-forward bibtex-reference nil `move)
1643 (bibtex-end-of-entry))
1644 (point)))
1645 (right
1646 (progn
1647 (goto-char (point-max))
1648 (if bibtex-sort-ignore-string-entries
1649 (re-search-backward bibtex-reference nil `move)
1650 (bibtex-beginning-of-entry))
1651 (point)))
1652 actual-point
1653 actual-key
1654 (done (>= left right))
1655 new
1656 dup)
1657 (while (not done)
1658 (setq actual-point (/ (+ left right) 2))
1659 (goto-char actual-point)
1660 (bibtex-beginning-of-entry)
1661 (setq actual-key
1662 (if bibtex-sort-ignore-string-entries
1663 (progn
1664 (re-search-forward bibtex-reference)
1665 (buffer-substring-no-properties
1666 (match-beginning bibtex-key-in-reference)
1667 (match-end bibtex-key-in-reference)))
1668 (re-search-forward bibtex-reference-head)
1669 (buffer-substring-no-properties
1670 (match-beginning bibtex-key-in-head)
1671 (match-end bibtex-key-in-head))))
1672 (cond
1673 ((string-lessp entry-name actual-key)
1674 (setq new (match-beginning 0))
1675 (if (equal right new)
1676 (setq done t)
1677 (setq right new)))
1678 ((string-lessp actual-key entry-name)
1679 (setq new (match-end 0))
1680 (if (equal left new)
1681 (setq done t)
1682 (setq left new)))
1683 ((string-equal actual-key entry-name)
1684 (setq dup t
1685 done t)
1686 (if (not ignore-dups)
1687 (error "Entry with key `%s' already exists!" entry-name)))))
1688 (if dup
1689 nil
1690 (goto-char right)
1691 (if (re-search-forward bibtex-reference nil t)
1692 (progn
1693 (setq actual-key
1694 (buffer-substring-no-properties
1695 (match-beginning bibtex-key-in-reference)
1696 (match-end bibtex-key-in-reference)))
1697 (if (string-lessp actual-key entry-name)
1698 ;; even greater than last entry --> we must append
1699 (progn
1700 (goto-char (match-end 0))
1701 (newline (forward-line 2))
1702 (beginning-of-line))
1703 (goto-char right))))
1704 t)))
1705
1706 (defun bibtex-validate-buffer ()
1707 "Validate if the current BibTeX buffer is syntactically correct.
1708 Any garbage (e.g. comments) before the first \"@\" is not tested (so
1709 you can put comments here)."
1710 (interactive)
1711 (let ((pnt (point))
1712 (max (point-max)))
1713 ;; looking if entries fit syntactical structure
1714 (goto-char (point-min))
1715 (while (< (re-search-forward "@\\|\\'") max)
1716 (forward-char -1)
1717 (let ((p (point)))
1718 (if (or
1719 (looking-at "@string")
1720 (looking-at "@preamble"))
1721 (forward-char)
1722 (if (not (and
1723 (re-search-forward bibtex-reference nil t)
1724 (equal p (match-beginning 0))))
1725 (progn
1726 (goto-char p)
1727 (error "Bad entry begins here"))))))
1728 ;; looking if entries are balanced (a single non-escaped quote
1729 ;; inside braces is not detected by the former check, but
1730 ;; bibtex-sort-entries stumbles about it
1731 (goto-char (point-min))
1732 (map-bibtex-entries
1733 (function
1734 (lambda (current)
1735 (bibtex-beginning-of-entry)
1736 (forward-sexp 2))))
1737 ;; looking for correct sort order and duplicates
1738 (if bibtex-maintain-sorted-entries
1739 (let ((entry-name (make-string 10 255))
1740 (previous nil)
1741 point)
1742 (beginning-of-first-bibtex-entry)
1743 (map-bibtex-entries
1744 (function
1745 (lambda (current)
1746 (cond ((or (null previous)
1747 (string< previous current))
1748 (setq previous current
1749 point (point)))
1750 ((string-equal previous current)
1751 (error "Duplicate here with previous!"))
1752 (t
1753 (error "Entries out of order here!"))))))))
1754 (goto-char pnt)
1755 (message "BibTeX buffer is syntactically correct")))
1756
1757 (defun bibtex-next-field (arg)
1758 "Finds end of text of next BibTeX field; with arg, to its beginning."
1759 (interactive "P")
1760 (bibtex-inside-field)
1761 (let ((start (point)))
1762 (condition-case ()
1763 (progn
1764 (bibtex-enclosing-field)
1765 (goto-char (match-end 0))
1766 (forward-char 2))
1767 (error
1768 (goto-char start)
1769 (end-of-line)
1770 (forward-char 1))))
1771 (bibtex-find-text arg))
1772
1773 (defun bibtex-find-text (arg)
1774 "Go to end of text of current field; with arg, go to beginning."
1775 (interactive "P")
1776 (bibtex-inside-field)
1777 (bibtex-enclosing-field)
1778 (if arg
1779 (progn
1780 (goto-char (match-beginning bibtex-text-in-field))
1781 (if (looking-at "[{\"]")
1782 (forward-char 1)))
1783 (goto-char (match-end bibtex-text-in-field))
1784 (if (or
1785 (= (preceding-char) ?})
1786 (= (preceding-char) ?\"))
1787 (forward-char -1)))
1788 (if bibtex-help-message
1789 (bibtex-print-help-message)))
1790
1791 (defun bibtex-remove-OPT ()
1792 "Removes the 'OPT' starting optional arguments and goes to end of text."
1793 (interactive)
1794 (bibtex-inside-field)
1795 (bibtex-enclosing-field)
1796 (save-excursion
1797 (goto-char (match-beginning bibtex-name-in-field))
1798 (if (looking-at "OPT")
1799 ;; sct@dcs.edinburgh.ac.uk
1800 (progn
1801 (delete-char (length "OPT"))
1802 (search-forward "=")
1803 (delete-horizontal-space)
1804 (indent-to-column bibtex-text-alignment))))
1805 (bibtex-inside-field))
1806
1807 (defun bibtex-remove-double-quotes-or-braces ()
1808 "Removes \"\" or {} around string."
1809 (interactive)
1810 (save-excursion
1811 (bibtex-inside-field)
1812 (bibtex-enclosing-field)
1813 (let ((start (match-beginning bibtex-text-in-field))
1814 (stop (match-end bibtex-text-in-field)))
1815 (goto-char stop)
1816 (forward-char -1)
1817 (if (looking-at "[}\"]")
1818 (delete-char 1))
1819 (goto-char start)
1820 (if (looking-at "[{\"]")
1821 (delete-char 1)))))
1822
1823 (defun bibtex-kill-optional-field ()
1824 "Kill the entire enclosing optional BibTeX field."
1825 (interactive)
1826 (bibtex-inside-field)
1827 (bibtex-enclosing-field)
1828 (goto-char (match-beginning bibtex-name-in-field))
1829 (let ((the-end (match-end 0))
1830 (the-beginning (match-beginning 0)))
1831 (if (looking-at "OPT")
1832 (progn
1833 (goto-char the-end)
1834 (skip-chars-forward " \t\n,")
1835 (kill-region the-beginning the-end))
1836 (error "Mandatory fields can't be killed"))))
1837
1838 (defun bibtex-empty-field ()
1839 "Delete the text part of the current field, replace with empty text."
1840 (interactive)
1841 (bibtex-inside-field)
1842 (bibtex-enclosing-field)
1843 (goto-char (match-beginning bibtex-text-in-field))
1844 (kill-region (point) (match-end bibtex-text-in-field))
1845 (insert (concat bibtex-field-left-delimiter
1846 bibtex-field-right-delimiter))
1847 (bibtex-find-text t))
1848
1849 (defun bibtex-pop-previous (arg)
1850 "Replace text of current field with the text of similar field in previous entry.
1851 With arg, go up ARG entries. Repeated, goes up so many times. May be
1852 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
1853 (interactive "p")
1854 (bibtex-inside-field)
1855 (save-excursion
1856 ; parse current field
1857 (bibtex-enclosing-field)
1858 (let ((start-old-text (match-beginning bibtex-text-in-field))
1859 (stop-old-text (match-end bibtex-text-in-field))
1860 (start-name (match-beginning bibtex-name-in-field))
1861 (stop-name (match-end bibtex-name-in-field))
1862 (new-text))
1863 (goto-char start-name)
1864 ; construct regexp for previous field with same name as this one
1865 (let ((matching-entry
1866 (bibtex-cfield
1867 (buffer-substring-no-properties (if (looking-at "OPT")
1868 (+ (point) (length "OPT"))
1869 (point))
1870 stop-name)
1871 bibtex-field-text)))
1872 ; if executed several times in a row, start each search where the
1873 ; last one finished
1874 (cond ((or (eq last-command 'bibtex-pop-previous)
1875 (eq last-command 'bibtex-pop-next))
1876 t
1877 )
1878 (t
1879 (bibtex-enclosing-reference-maybe-empty-head)
1880 (setq bibtex-pop-previous-search-point (point))
1881 (setq bibtex-pop-next-search-point (match-end 0))))
1882 (goto-char bibtex-pop-previous-search-point)
1883 ; Now search for arg'th previous similar field
1884 (cond
1885 ((re-search-backward matching-entry (point-min) t arg)
1886 (setq new-text
1887 (buffer-substring-no-properties
1888 (match-beginning bibtex-text-in-cfield)
1889 (match-end bibtex-text-in-cfield)))
1890 ;; change delimiters, if any changes needed
1891 (cond
1892 ((and
1893 (equal bibtex-field-left-delimiter "{")
1894 (eq (aref new-text 0) ?\")
1895 (eq (aref new-text (1- (length new-text))) ?\"))
1896 (aset new-text 0 ?\{)
1897 (aset new-text (1- (length new-text)) ?\}))
1898 ((and
1899 (equal bibtex-field-left-delimiter "\"")
1900 (eq (aref new-text 0) ?\{)
1901 (eq (aref new-text (1- (length new-text))) ?\}))
1902 (aset new-text 0 ?\")
1903 (aset new-text (1- (length new-text)) ?\"))
1904 ((or
1905 (not (eq (aref new-text 0)
1906 (aref bibtex-field-left-delimiter 0)))
1907 (not (eq (aref new-text (1- (length new-text)))
1908 (aref bibtex-field-right-delimiter 0))))
1909 (setq new-text (concat bibtex-field-left-delimiter
1910 new-text
1911 bibtex-field-right-delimiter))))
1912 ; Found a matching field. Remember boundaries.
1913 (setq bibtex-pop-next-search-point (match-end 0))
1914 (setq bibtex-pop-previous-search-point (match-beginning 0))
1915 (bibtex-flash-head)
1916 ; Go back to where we started, delete old text, and pop new.
1917 (goto-char stop-old-text)
1918 (delete-region start-old-text stop-old-text)
1919 (insert new-text))
1920 (t ; search failed
1921 (error "No previous matching BibTeX field."))))))
1922 (setq this-command 'bibtex-pop-previous))
1923
1924 (defun bibtex-pop-next (arg)
1925 "Replace text of current field with the text of similar field in next entry.
1926 With arg, go up ARG entries. Repeated, goes up so many times. May be
1927 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
1928 (interactive "p")
1929 (bibtex-inside-field)
1930 (save-excursion
1931 ; parse current field
1932 (bibtex-enclosing-field)
1933 (let ((start-old-text (match-beginning bibtex-text-in-field))
1934 (stop-old-text (match-end bibtex-text-in-field))
1935 (start-name (match-beginning bibtex-name-in-field))
1936 (stop-name (match-end bibtex-name-in-field))
1937 (new-text))
1938 (goto-char start-name)
1939 ; construct regexp for next field with same name as this one,
1940 ; ignoring possible OPT's
1941 (let ((matching-entry
1942 (bibtex-cfield
1943 (buffer-substring-no-properties (if (looking-at "OPT")
1944 (+ (point) (length "OPT"))
1945 (point))
1946 stop-name)
1947 bibtex-field-text)))
1948
1949 ; if executed several times in a row, start each search where the
1950 ; last one finished
1951 (cond ((or (eq last-command 'bibtex-pop-next)
1952 (eq last-command 'bibtex-pop-previous))
1953 t
1954 )
1955 (t
1956 (bibtex-enclosing-reference-maybe-empty-head)
1957 (setq bibtex-pop-previous-search-point (point))
1958 (setq bibtex-pop-next-search-point (match-end 0))))
1959 (goto-char bibtex-pop-next-search-point)
1960
1961 ; Now search for arg'th next similar field
1962 (cond
1963 ((re-search-forward matching-entry (point-max) t arg)
1964 (setq new-text
1965 (buffer-substring-no-properties
1966 (match-beginning bibtex-text-in-cfield)
1967 (match-end bibtex-text-in-cfield)))
1968 ;; change delimiters, if any changes needed
1969 (cond
1970 ((and
1971 (equal bibtex-field-left-delimiter "{")
1972 (eq (aref new-text 0) ?\")
1973 (eq (aref new-text (1- (length new-text))) ?\"))
1974 (aset new-text 0 ?\{)
1975 (aset new-text (1- (length new-text)) ?\}))
1976 ((and
1977 (equal bibtex-field-left-delimiter "\"")
1978 (eq (aref new-text 0) ?\{)
1979 (eq (aref new-text (1- (length new-text))) ?\}))
1980 (aset new-text 0 ?\")
1981 (aset new-text (1- (length new-text)) ?\"))
1982 ((or
1983 (not (eq (aref new-text 0)
1984 (aref bibtex-field-left-delimiter 0)))
1985 (not (eq (aref new-text (1- (length new-text)))
1986 (aref bibtex-field-right-delimiter 0))))
1987 (setq new-text (concat bibtex-field-left-delimiter
1988 new-text
1989 bibtex-field-right-delimiter))))
1990 ; Found a matching field. Remember boundaries.
1991 (setq bibtex-pop-next-search-point (match-end 0))
1992 (setq bibtex-pop-previous-search-point (match-beginning 0))
1993 (bibtex-flash-head)
1994 ; Go back to where we started, delete old text, and pop new.
1995 (goto-char stop-old-text)
1996 (delete-region start-old-text stop-old-text)
1997 (insert new-text))
1998 (t ; search failed
1999 (error "No next matching BibTeX field."))))))
2000 (setq this-command 'bibtex-pop-next))
2001
2002 (defun bibtex-clean-entry (&optional arg)
2003 "Finish editing the current BibTeX entry and clean it up.
2004 For all optional fields of current BibTeX entry: if empty, kill the
2005 whole field; otherwise, remove the \"OPT\" string in the name; if text
2006 numerical, remove double-quotes. For all mandatory fields: if empty,
2007 signal error. If label of entry is empty or a prefix argument was
2008 given, calculate a new entry label."
2009 (interactive "P")
2010 (bibtex-beginning-of-entry)
2011 (let ((start (point))
2012 crossref-there)
2013 (save-restriction
2014 (narrow-to-region start (save-excursion (bibtex-end-of-entry) (point)))
2015 (while (and
2016 (re-search-forward bibtex-field (point-max) t 1)
2017 (not crossref-there))
2018 ;; determine if reference has crossref entry
2019 (let ((begin-name (match-beginning bibtex-name-in-field))
2020 (begin-text (match-beginning bibtex-text-in-field)))
2021 (goto-char begin-name)
2022 (if (looking-at "\\(OPTcrossref\\)\\|\\(crossref\\)")
2023 (progn
2024 (goto-char begin-text)
2025 (if (not (looking-at
2026 (concat
2027 bibtex-field-left-delimiter
2028 bibtex-field-right-delimiter)))
2029 (setq crossref-there t))))))
2030 (bibtex-enclosing-reference-maybe-empty-head)
2031 (re-search-forward bibtex-reference-type)
2032 (let ((begin-type (1+ (match-beginning 0)))
2033 (end-type (match-end 0)))
2034 (goto-char start)
2035 (while (re-search-forward bibtex-field (point-max) t 1)
2036 (let ((begin-field (match-beginning 0))
2037 (end-field (match-end 0))
2038 (begin-name (match-beginning bibtex-name-in-field))
2039 (end-name (match-end bibtex-name-in-field))
2040 (begin-text (match-beginning bibtex-text-in-field))
2041 (end-text (match-end bibtex-text-in-field))
2042 )
2043 (goto-char begin-name)
2044 (cond ((and
2045 (looking-at "OPT")
2046 bibtex-clean-entry-zap-empty-opts)
2047 (goto-char begin-text)
2048 (if (looking-at
2049 (concat
2050 bibtex-field-left-delimiter
2051 bibtex-field-right-delimiter))
2052 ;; empty: delete whole field if really optional
2053 ;; (missing crossref handled) or complain
2054 (if (and
2055 (not crossref-there)
2056 (assoc
2057 (downcase
2058 (buffer-substring-no-properties
2059 (+ (length "OPT") begin-name) end-name))
2060 (car (car (cdr
2061 (assoc-ignore-case
2062 (buffer-substring-no-properties
2063 begin-type end-type)
2064 bibtex-entry-field-alist))))))
2065 ;; field is not really optional
2066 (progn
2067 (goto-char begin-name)
2068 (delete-char (length "OPT"))
2069 ;; make field non-OPT
2070 (search-forward "=")
2071 (delete-horizontal-space)
2072 (indent-to-column bibtex-text-alignment)
2073 (forward-char)
2074 ;; and loop to go through next test
2075 (error "Mandatory field ``%s'' is empty"
2076 (buffer-substring-no-properties
2077 begin-name
2078 end-name)))
2079 ;; field is optional
2080 (delete-region begin-field end-field))
2081 ;; otherwise: not empty, delete "OPT"
2082 (goto-char begin-name)
2083 (delete-char (length "OPT"))
2084 (progn
2085 ;; fixup alignment. [alarson:19920309.2047CST]
2086 (search-forward "=")
2087 (delete-horizontal-space)
2088 (indent-to-column bibtex-text-alignment))
2089 (goto-char begin-field) ; and loop to go through next test
2090 ))
2091 (t
2092 (goto-char begin-text)
2093 (cond ((looking-at (concat
2094 bibtex-field-left-delimiter
2095 "[0-9]+"
2096 bibtex-field-right-delimiter))
2097 ;; if numerical,
2098 (goto-char end-text)
2099 (delete-char -1) ; delete enclosing double-quotes
2100 (goto-char begin-text)
2101 (delete-char 1)
2102 (goto-char end-field) ; go to end for next search
2103 (forward-char -2) ; to compensate for the 2 quotes deleted
2104 )
2105 ((looking-at (concat
2106 bibtex-field-left-delimiter
2107 bibtex-field-right-delimiter))
2108 ;; if empty quotes, complain
2109 (forward-char 1)
2110 (if (not (or (equal (buffer-substring-no-properties
2111 begin-name
2112 (+ begin-name 3))
2113 "OPT")
2114 (equal (buffer-substring-no-properties
2115 begin-name
2116 (+ begin-name 3))
2117 "opt")))
2118 (error "Mandatory field ``%s'' is empty"
2119 (buffer-substring-no-properties
2120 begin-name end-name))))
2121 (t
2122 (goto-char end-field)))))))))
2123 (goto-char start)
2124 (bibtex-end-of-entry))
2125 (let* ((eob (progn
2126 (bibtex-end-of-entry)
2127 (point)))
2128 (key (progn
2129 (bibtex-beginning-of-entry)
2130 (if (re-search-forward
2131 bibtex-reference-head eob t)
2132 (buffer-substring-no-properties
2133 (match-beginning bibtex-key-in-head)
2134 (match-end bibtex-key-in-head))))))
2135 (if (or
2136 arg
2137 (not key))
2138 (progn
2139 (let ((autokey
2140 (if bibtex-autokey-edit-before-use
2141 (read-from-minibuffer "Key to use: "
2142 (bibtex-generate-autokey))
2143 (bibtex-generate-autokey))))
2144 (bibtex-beginning-of-entry)
2145 (re-search-forward bibtex-reference-maybe-empty-head)
2146 (if (match-beginning bibtex-key-in-head)
2147 (delete-region (match-beginning bibtex-key-in-head)
2148 (match-end bibtex-key-in-head)))
2149 (insert autokey)
2150 (let ((start (progn
2151 (bibtex-beginning-of-entry)
2152 (point)))
2153 (end (progn
2154 (bibtex-end-of-entry)
2155 (re-search-forward "^@" nil 'move)
2156 (beginning-of-line)
2157 (point)))
2158 last-command)
2159 (kill-region start end)
2160 (let ((success
2161 (or
2162 (not bibtex-maintain-sorted-entries)
2163 (bibtex-find-entry-location autokey t))))
2164 (yank)
2165 (setq kill-ring (cdr kill-ring))
2166 (forward-char -1)
2167 (bibtex-beginning-of-entry)
2168 (re-search-forward bibtex-reference-head)
2169 (if (not success)
2170 (error
2171 "New inserted reference may be a duplicate."))))))))
2172 (save-excursion
2173 (let ((start (progn (bibtex-beginning-of-entry) (point)))
2174 (end (progn (bibtex-end-of-entry) (point))))
2175 (save-restriction
2176 (narrow-to-region start end)
2177 (bibtex-parse-keys t)))))
2178
2179 (defun bibtex-complete-string ()
2180 "Complete word fragment before point to longest prefix of a defined string.
2181 If point is not after the part of a word, all strings are listed."
2182 (interactive "*")
2183 (let* ((end (point))
2184 (beg (save-excursion
2185 (re-search-backward "[ \t{\"]")
2186 (forward-char 1)
2187 (point)))
2188 (part-of-word (buffer-substring-no-properties beg end))
2189 (string-list (copy-sequence bibtex-completion-candidates))
2190 (case-fold-search t)
2191 (completion (save-excursion
2192 (progn
2193 (while (re-search-backward
2194 bibtex-string (point-min) t)
2195 (setq string-list
2196 (cons
2197 (list
2198 (buffer-substring-no-properties
2199 (match-beginning bibtex-key-in-string)
2200 (match-end bibtex-key-in-string)))
2201 string-list)))
2202 (setq string-list
2203 (sort string-list
2204 (lambda(x y)
2205 (string-lessp
2206 (car x)
2207 (car y)))))
2208 (try-completion part-of-word string-list)))))
2209 (cond ((eq completion t)
2210 (bibtex-remove-double-quotes-or-braces))
2211 ((null completion)
2212 (error "Can't find completion for \"%s\"" part-of-word))
2213 ((not (string= part-of-word completion))
2214 (delete-region beg end)
2215 (insert completion)
2216 (if (assoc completion string-list)
2217 (bibtex-remove-double-quotes-or-braces)))
2218 (t
2219 (message "Making completion list...")
2220 (let ((list (all-completions part-of-word string-list)))
2221 (with-output-to-temp-buffer "*Completions*"
2222 (display-completion-list list)))
2223 (message "Making completion list...done")))))
2224
2225 (defun bibtex-Article ()
2226 (interactive)
2227 (bibtex-entry "Article"))
2228
2229 (defun bibtex-Book ()
2230 (interactive)
2231 (bibtex-entry "Book"))
2232
2233 (defun bibtex-Booklet ()
2234 (interactive)
2235 (bibtex-entry "Booklet"))
2236
2237 (defun bibtex-InBook ()
2238 (interactive)
2239 (bibtex-entry "InBook"))
2240
2241 (defun bibtex-InCollection ()
2242 (interactive)
2243 (bibtex-entry "InCollection"))
2244
2245 (defun bibtex-InProceedings ()
2246 (interactive)
2247 (bibtex-entry "InProceedings"))
2248
2249 (defun bibtex-Manual ()
2250 (interactive)
2251 (bibtex-entry "Manual"))
2252
2253 (defun bibtex-MastersThesis ()
2254 (interactive)
2255 (bibtex-entry "MastersThesis"))
2256
2257 (defun bibtex-Misc ()
2258 (interactive)
2259 (bibtex-entry "Misc"))
2260
2261 (defun bibtex-PhdThesis ()
2262 (interactive)
2263 (bibtex-entry "PhdThesis"))
2264
2265 (defun bibtex-Proceedings ()
2266 (interactive)
2267 (bibtex-entry "Proceedings"))
2268
2269 (defun bibtex-TechReport ()
2270 (interactive)
2271 (bibtex-entry "TechReport"))
2272
2273 (defun bibtex-Unpublished ()
2274 (interactive)
2275 (bibtex-entry "Unpublished"))
2276
2277 (defun bibtex-string ()
2278 (interactive)
2279 (bibtex-move-outside-of-entry)
2280 (insert
2281 (concat
2282 "@string{ = "
2283 bibtex-field-left-delimiter
2284 bibtex-field-right-delimiter
2285 "}\n"))
2286 (forward-line -1)
2287 (forward-char 8))
2288
2289 (defun bibtex-preamble ()
2290 (interactive)
2291 (bibtex-move-outside-of-entry)
2292 (insert "@Preamble{}\n")
2293 (forward-line -1)
2294 (forward-char 10))
2295
2296
2297 \f
2298 ;; Make BibTeX a Feature
2299
2300 (provide 'bibtex)
2301
2302
2303 ;;; bibtex.el ends here