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