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