(bibtex-mode): Add autoload.
[emacs.git] / lisp / textmodes / bibtex.el
blobdb0ba20861ffe71da3a419e48fe059ed769bed6f
1 ;;; bibtex.el --- BibTeX mode for GNU Emacs
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Bengt Martensson <ubrinf!mond!bengt>
6 ;; Mark Shapiro <shapiro@corto.inria.fr>
7 ;; Mike Newton <newton@gumby.cs.caltech.edu>
8 ;; Aaron Larson <alarson@src.honeywell.com>
9 ;; Version: 1.3.1
10 ;; Maintainer:Aaron Larson <alarson@src.honeywell.com>
11 ;; Adapted-By: ESR
12 ;; Keywords: tex, bib
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;;; TODO distribute texinfo file.
32 ;;; LCD Archive Entry:
33 ;;; bibtex-mode|Bengt Martensson, Marc Shapiro, Aaron Larson|
34 ;;; alarson@src.honeywell.com|
35 ;;; Support for maintaining BibTeX format bibliography databases|
36 ;;; 93-03-29|version 1.3|~/modes/bibtex-mode.el.Z|
38 ;;; Commentary:
40 ;;; BUGS:
41 ;;; 1. using regular expressions to match the entire bibtex entry dies
42 ;;; on long bibtex entires (e.g. those containing abstracts) since
43 ;;; the length of regular expression matches is fairly limited.
44 ;;; 2. When inserting a string (with \C-C\C-E\s) hitting a TAB results
45 ;;; in the error message "Can't find enclosing Bibtex field" instead
46 ;;; of moving to the empty string. [reported by gernot@cs.unsw.oz.au]
47 ;;; 3. Function string-equalp should be in a library file, not in this
48 ;;; file.
50 ;;; (current keeper: alarson@src.honeywell.com
51 ;;; previous: shapiro@corto.inria.fr)
53 ;;; Change Log:
55 ;; Mon Mar 29 14:06:06 1993 Aaron Larson (alarson at gendibal)
57 ;; * bibtex.el: V1.3 released Mar 30, 1993
58 ;; (bibtex-field-name): Fix to match definition if latex manual,
59 ;; specifically letters, digits, and punctuation other than comma.
60 ;; Underscore is retained for historical reasons.
61 ;; (bibtex-make-field): Fix to work around bug in Lucid prin1-to-string
62 ;; function as reported by Martin Sjolin <marsj@ida.liu.se>.
63 ;; (bibtex-entry): minor code cleanup.
64 ;; (bibtex-mode-map): Remove key binding (C-c n) for
65 ;; narrow-to-bibtex-entry, previous binding violated emacs policy of
66 ;; reserving C-c followed by a letter for user customization.
67 ;; revise modification history to better conform to FSF changelog
68 ;; standards.
69 ;; (bibtex-refile-entry): Removed. Would need disclaimer papers to
70 ;; incorporate it into official sources, and unable to contact author.
71 ;; Fix minor "syntax" errors in documentation strings and such found
72 ;; by new byte compiler. Funs bibtex-mode, bibtex-remove-double-quotes
75 ;; Fri Jan 15 14:06:06 1993 Aaron Larson (alarson at gendibal)
77 ;; * bibtex.el: V1.2 released Feb 15 1993
78 ;; (find-bibtex-entry-location bibtex-make-field): Fixed placement of
79 ;; "interactive specification". [Bug report from
80 ;; mernst@theory.lcs.mit.edu]
81 ;; Fixed problem where bibtex-entry would fail if user typed entry
82 ;; name in wrong case.
83 ;; (bibtex-inside-field) Position the cursor _before_ the last comma
84 ;; on a line (the comma is not necessarily "inside" the field); this
85 ;; does not seem to break any existing code. ref sct@dcs.edinburgh.ac.uk
86 ;; (bibtex-enclosing-field, bibtex-enclosing-reference): leave
87 ;; point unmoved if no enclosing field/reference is found. As a
88 ;; result of changes (3) and (4) bibtex-next-field works properly,
89 ;; even when called from the entry key position.
90 ;; (bibtex-remove-OPT): realign the '=' after removing the 'opt'.
91 ;; (bibtex-clean-entry): always remove any trailing comma from the
92 ;; end of a bibtex entry (these commas get stripped automatically when
93 ;; optional fields are killed by bibtex-kill-optional-field, but can be
94 ;; left if optional fields are removed by other means).
95 ;; (bibtex-x-help) Replace tab with spaces in X menu as noted by
96 ;; khera@cs.duke.edu
97 ;; (bibtex-refile-entry): Added (from brannon@jove.cs.caltech.edu)
98 ;; (bibtex-sort-ignore-string-entries sort-bibtex-entries,
99 ;; map-bibtex-entries): Added variable as requested by
100 ;; gernot@cs.unsw.oz.au, required changes to funs.
101 ;; (bibtex-current-entry-label): Added at request of
102 ;; yasuro@maekawa.is.uec.ac.jp
103 ;; (bibtex-DEAthesis:) Deleted along with corresponding entry from
104 ;; bibtex-x-help per shapiro@corto.inria.fr
105 ;; Moved narrow-to-bibtex-entry from C-c C-n to C-c n (the previous
106 ;; binding was in conflict with the binding for bibtex-pop-next.
107 ;; bug report from [shapiro@corto.inria.fr]
110 ;;;
111 ;;; alarson@src.honeywell.com 92-Feb-13
112 ;;; 1. Made bibtex-entry user callable, now prompts for entry type (e.g.
113 ;;; Article), with completion, and bound it to a key. This is now my
114 ;;; preferred way to add most entries.
115 ;;; 2. Made fields of a bibtex entry derived from the alist bibtex-entry-
116 ;;; field-alist.
117 ;;; 3. Fixed handling of escaped double quotes, e.g. "Schr{\"o}dinger".
118 ;;; 4. Fixed bug where unhiding bibtex entries moved point.
119 ;;; 5. Made "field name" specs permit (name . value) for defaulting. E.g.
120 ;;; (setq bibtex-mode-user-optional-fields '(("library" . "alarson")))
121 ;;; will generate the field:
122 ;;; library = "alarson",
123 ;;; 6. Added binding for narrow-to-bibtex-entry
124 ;;; 7. Adding a bibtex entry now runs hook: bibtex-add-entry-hook
125 ;;; 8. Made bibtex-clean-entry fixup text alignment, and eliminated the
126 ;;; dependency on bibtex-enclosing-reference which has a problem with
127 ;;; long entries (e.g. those containing abstracts).
128 ;;;
129 ;;; alarson@src.honeywell.com 92-Jan-31
130 ;;; Added support for: ispell, beginning/end of entry movement, a simple
131 ;;; outline like mode (hide the bodies of bibtex entries), support for
132 ;;; sorting bibtex entries, and maintaining them in sorted order, and
133 ;;; simple buffer validation.
134 ;;; User visible functions added:
135 ;;; ispell-{abstract,bibtex-entry}, {beginning,end}-of-bibtex-entry
136 ;;; hide-bibtex-entry-bodies, sort-bibtex-entries, validate-bibtex-
137 ;;; buffer, find-bibtex-duplicates
138 ;;; user visible variables added:
139 ;;; bibtex-maintain-sorted-entries
140 ;;; new local keybindings:
141 ;;; " TeX-insert-quote
142 ;;; C-c$ ispell-bibtex-entry
143 ;;; M-C-a beginning-of-bibtex-entry
144 ;;; M-C-e end-of-bibtex-entry
145 ;;; Mike Newton (newton@gumby.cs.caltech.edu) 90.11.17
146 ;;; * Handle items like
147 ;;; title = poft # "Fifth Tri-quaterly" # random-conf,
148 ;;; and title = {This title is inside curlies}
149 ;;; * added user settable, always present, optional fields
150 ;;; * fixed 'bibtex-find-it's doc string's location
151 ;;; * bibtex-field-text made more general (it wouldnt handle the # construct)
152 ;;; and it now handles a small subset of the {} cases
154 ;;; Bengt Martensson, March 6
155 ;;; Adapted to Bibtex 0.99 by updating the optional fields according
156 ;;; to the document BibTeXing, Oren Patashnik, dated January 31, 1988.
157 ;;; Updated documentation strings accordingly. Added (provide 'bibtex).
158 ;;; If bibtex-include-OPT-crossref is non-nil, every entry will have
159 ;;; an OPTcrossref field, analogously for bibtex-include-OPTkey and
160 ;;; bibtex-include-OPTannote. Added bibtex-preamble, bound to ^C^EP,
161 ;;; and also found in X- and sun-menus. Cleaned up the sun-menu
162 ;;; stuff, and made it more uniform with the X-menu stuff. Marc: I
163 ;;; strongly suspect that I broke your parsing... (Or, more
164 ;;; correctly, BibTeX 0.99 broke it.)
165 ;;; Added bibtex-clean-entry-zap-empty-opts, defvar'd to t. If it
166 ;;; is nil, bibtex-clean-entry will leave empty optional fields alone.
168 ;;; Marc Shapiro 1-feb-89: integrated changes by Bengt Martensson 88-05-06:
169 ;;; Added Sun menu support. Locally bound to right mouse button in
170 ;;; bibtex-mode. Emacs 18.49 allows local mouse bindings!!
171 ;;; Commented out DEAthesis.
173 ;;; Marc Shapiro 6-oct-88
174 ;;; * skip-whitespace replaced by skip-chars-forward
175 ;;; * use indent-to-column instead of inserting tabs (changes to
176 ;;; bibtex-entry, bibtex-make-entry, bibtex-make-OPT-entry, renamed to
177 ;;; bibtex-make-optional-entry)
178 ;;; * C-c C-k deletes the current OPT entry entirely
179 ;;; * C-c C-d replaces text of field with ""
180 ;;; * renamed bibtex-find-it to bibtex-find-text. With arg, now goes to
181 ;;; start of text. Fixed bugs in it.
183 ;;; Marc Shapiro 23-sep-88
184 ;;; * bibtex-clean-entry moves past end of entry.
185 ;;; * bibtex-clean-entry signals mandatory fields left empty.
187 ;;; Marc Shapiro 18-jul-88
188 ;;; * Fixed bug in bibtex-flash-entry
189 ;;; * Moved all the entry type keystrokes to "C-c C-e something" (instead of
190 ;;; "C-c something" previously) to make room for more. C-c C-e is
191 ;;; supposed to stand for "entry" [idea taken from mail-mode]. Moved
192 ;;; bibtex-pop-previous to C-c C-p and bibtex-pop-next to C-c C-n.
193 ;;; * removed binding for "\e[25~"
194 ;;; * replaced bibtex-clean-optionals by bibtex-clean-entry, bound to
195 ;;; C-c C-c
197 ;;; Marc Shapiro 13-jul-88 [based on ideas by Sacha Krakowiak of IMAG]
198 ;;; * bibtex-pop-previous replaces current field with value of
199 ;;; similar field in previous entry. May be called n times in a row
200 ;;; (or with arg n) to pop similar field of n'th previous entry.
201 ;;; There is also a bibtex-pop-next to get similar field of next
202 ;;; entry.
203 ;;; * C-c C-k now kills all empty optional fields of current entry, and
204 ;;; removes "OPT" for those optional fields which have text.
206 ;;; Marc Shapiro 14-dec-87
207 ;;; Cosmetic fixes. Fixed small bug in bibtex-move-outside-of-entry.
208 ;;; Skip Montanaro <steinmetz!sprite!montanaro> 7-dec-87, Shapiro 10-dec-87
209 ;;; before inserting an entry, make sure we are outside of a bib entry
210 ;;; Marc Shapiro 3-nov-87
211 ;;; addition for France: DEAthesis
212 ;;; Marc Shapiro 19-oct-1987
213 ;;; add X window menu option; bug fixes. TAB, LFD, C-c " and C-c C-o now
214 ;;; behave consistently; deletion never occurs blindly.
215 ;;; Marc Shapiro <shapiro@inria.inria.fr> 15-oct-1986
216 ;;; align long lines nicely; C-c C-o checks for the "OPT" string;
217 ;;; TAB goes to the end of the string; use lower case; use
218 ;;; run-hooks
220 ;;; Bengt Martensson <ubrinf!mond!bengt> 87-06-28
221 ;;; Bengt Martensson <bengt@mathematik.uni-Bremen.de> 87-06-28
222 ;;; Original version
224 ;;; Code:
226 ;;; NOTE by Marc Shapiro, 14-dec-87:
227 ;;; (bibtex-x-environment) binds an X menu for bibtex mode to x-button-c-right.
228 ;;; Trouble is, in Emacs 18.44 you can't have a mode-specific mouse binding,
229 ;;; so it will remain active in all windows. Yuck!
231 (provide 'bibtex)
233 ;;; these guys typically don't have autoloads...[alarson:19920131.1548CST]
234 ;;; Check for fboundp first so that if user autoloads them from non standard
235 ;;; places, the users bindings will take precedence.
236 (if (not (fboundp 'TeX-insert-quote))
237 (autoload 'TeX-insert-quote "tex-mode"))
238 (if (not (fboundp 'sort-subr))
239 (autoload 'sort-subr "sort"))
241 ;;; These should be in a more generally accessible location.
243 (defun string-equalp (s1 s2)
244 "Like string= except differences in case are ignored."
245 (let ((ss1 (if (symbolp s1) (symbol-name s1) s1))
246 (ss2 (if (symbolp s2) (symbol-name s2) s2)))
247 (and (= (length ss1) (length ss2))
248 (string-equal (upcase ss1) (upcase ss2)))))
250 ;;; This should be moved into simple.el, and the functions there modified
251 ;;; to call it rather than doing it themselves.
252 (defun put-string-on-kill-ring (string)
253 "Make STRING be the first element of the kill ring."
254 (setq kill-ring (cons string kill-ring))
255 (if (> (length kill-ring) kill-ring-max)
256 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
257 (setq kill-ring-yank-pointer kill-ring))
262 (defvar bibtex-clean-entry-zap-empty-opts t
263 "*If non-nil, bibtex-clean-entry will delete all empty optional fields.")
265 (defvar bibtex-include-OPTcrossref t
266 "*If non-nil, all entries will have an OPTcrossref field.")
267 (defvar bibtex-include-OPTkey t
268 "*If non-nil, all entries will have an OPTkey field.")
269 (defvar bibtex-include-OPTannote t
270 "*If non-nil, all entries will have an OPTannote field.")
272 ;; note: the user should be allowed to have their own list of always
273 ;; available optional fields. exs: "keywords" "categories"
275 (defvar bibtex-mode-user-optional-fields nil ;no default value
276 "*List of optional fields that user want to have as always present
277 when making a bibtex entry. One possibility is for ``keywords''.
278 Entries can be either strings or conses, in which case the car should be
279 string and the cdr the value to be inserted.")
281 (defvar bibtex-mode-syntax-table
282 (let ((st (make-syntax-table)))
283 ;; [alarson:19920214.1004CST] make double quote a string quote
284 (modify-syntax-entry ?\" "\"" st)
285 (modify-syntax-entry ?$ "$$ " st)
286 (modify-syntax-entry ?% "< " st)
287 (modify-syntax-entry ?' "w " st)
288 (modify-syntax-entry ?@ "w " st)
289 (modify-syntax-entry ?\\ "\\" st)
290 (modify-syntax-entry ?\f "> " st)
291 (modify-syntax-entry ?\n "> " st)
292 (modify-syntax-entry ?~ " " st)
293 st))
295 (defvar bibtex-mode-abbrev-table nil "")
296 (define-abbrev-table 'bibtex-mode-abbrev-table ())
297 (defvar bibtex-mode-map
298 (let ((km (make-sparse-keymap)))
300 (define-key km "\t" 'bibtex-find-text)
301 (define-key km "\n" 'bibtex-next-field)
302 (define-key km "\C-c\"" 'bibtex-remove-double-quotes)
303 (define-key km "\C-c\C-c" 'bibtex-clean-entry)
304 (define-key km "\C-c?" 'describe-mode)
305 (define-key km "\C-c\C-p" 'bibtex-pop-previous)
306 (define-key km "\C-c\C-n" 'bibtex-pop-next)
307 (define-key km "\C-c\C-k" 'bibtex-kill-optional-field)
308 (define-key km "\C-c\C-d" 'bibtex-empty-field)
310 ;; [alarson:19920131.1543CST]
311 (define-key km "\"" 'TeX-insert-quote)
312 (define-key km "\C-c$" 'ispell-bibtex-entry)
313 (define-key km "\M-\C-a" 'beginning-of-bibtex-entry)
314 (define-key km "\M-\C-e" 'end-of-bibtex-entry)
315 (define-key km "\C-ce" 'bibtex-entry)
316 ; (define-key km "\C-cn" 'narrow-to-bibtex-entry)
318 (define-key km "\C-c\C-e\C-a" 'bibtex-Article)
319 (define-key km "\C-c\C-e\C-b" 'bibtex-Book)
320 ; (define-key km "\C-c\C-e\C-d" 'bibtex-DEAthesis)
321 (define-key km "\C-c\C-e\C-c" 'bibtex-InProceedings)
322 (define-key km "\C-c\C-e\C-i" 'bibtex-InBook)
323 (define-key km "\C-c\C-ei" 'bibtex-InCollection)
324 (define-key km "\C-c\C-eI" 'bibtex-InProceedings)
325 (define-key km "\C-c\C-e\C-m" 'bibtex-Manual)
326 (define-key km "\C-c\C-em" 'bibtex-MastersThesis)
327 (define-key km "\C-c\C-eM" 'bibtex-Misc)
328 (define-key km "\C-c\C-o" 'bibtex-remove-OPT)
329 (define-key km "\C-c\C-e\C-p" 'bibtex-PhdThesis)
330 (define-key km "\C-c\C-ep" 'bibtex-Proceedings)
331 (define-key km "\C-c\C-eP" 'bibtex-preamble)
332 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
333 (define-key km "\C-c\C-e\C-s" 'bibtex-string)
334 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
335 km))
337 (defvar bibtex-pop-previous-search-point nil
338 "Next point where bibtex-pop-previous should start looking for a similar
339 entry.")
341 (defvar bibtex-pop-next-search-point nil
342 "Next point where bibtex-pop-next should start looking for a similar
343 entry.")
345 (defvar bibtex-entry-field-alist
347 ("Article" . ((("author" "title" "journal" "year")
348 ("volume" "number" "pages" "month" "note"))
349 (("author" "title")
350 ("journal" "year" "volume" "number" "pages"
351 "month" "note"))))
352 ("Book" . ((("author" "title" "publisher" "year")
353 ("editor" "volume" "number" "series" "address"
354 "edition" "month" "note"))))
355 ("Booklet" . ((("title")
356 ("author" "howpublished" "address" "month" "year" "note"))))
358 ;; France: Dipl\^{o}me d'Etudes Approfondies (similar to Master's)
359 ; ("DEAthesis" . ((("author" "title" "school" "year")
360 ; ("address" "month" "note"))))
362 ("InBook" . ((("author" "title" "chapter" "publisher" "year")
363 ("editor" "pages" "volume" "number" "series" "address"
364 "edition" "month" "type" "note"))
365 (("author" "title" "chapter")
366 ("publisher" "year" "editor" "pages" "volume" "number"
367 "series" "address" "edition" "month" "type" "note"))))
370 ("InCollection" . ((("author" "title"
371 "booktitle" "publisher" "year")
372 ("editor" "volume" "number" "series" "type" "chapter"
373 "pages" "address" "edition" "month" "note"))
374 (("author" "title")
375 ("booktitle" "publisher" "year"
376 "editor" "volume" "number" "series" "type" "chapter"
377 "pages" "address" "edition" "month" "note"))))
380 ("InProceedings" . ((("author" "title" "booktitle" "year")
381 ("editor" "volume" "number" "series" "pages"
382 "organization" "publisher" "address" "month" "note"))
383 (("author" "title")
384 ("editor" "volume" "number" "series" "pages"
385 "booktitle" "year"
386 "organization" "publisher" "address" "month" "note"))))
389 ("Manual" . ((("title")
390 ("author" "organization" "address" "edition" "year"
391 "month" "note"))))
393 ("MastersThesis" . ((("author" "title" "school" "year")
394 ("address" "month" "note" "type"))))
396 ("Misc" . ((()
397 ("author" "title" "howpublished" "year" "month" "note"))))
399 ("PhdThesis" . ((("author" "title" "school" "year")
400 ("address" "month" "type" "note"))))
402 ("Proceedings" . ((("title" "year")
403 ("editor" "volume" "number" "series" "publisher"
404 "organization" "address" "month" "note"))))
406 ("TechReport" . ((("author" "title" "institution" "year")
407 ("type" "number" "address" "month" "note"))))
409 ("Unpublished" . ((("author" "title" "note")
410 ("year" "month"))))
413 "List of (entry-name (required optional) (crossref-required crossref-optional))
414 tripples. If the third element is nil, then the first pair can be used. Required
415 and optional are lists of strings. All entry creation functions use this variable
416 to generate entries, and bibtex-entry ensures the entry type is valid. This
417 variable can be used for example to make bibtex manipulate a different set of entry
418 types, e.g. a crossreference document of organization types.")
421 ;;; A bibtex file is a sequence of entries, either string definitions
422 ;;; or reference entries. A reference entry has a type part, a
423 ;;; key part, and a comma-separated sequence of fields. A string
424 ;;; entry has a single field. A field has a left and right part,
425 ;;; separated by a '='. The left part is the name, the right part is
426 ;;; the text. Here come the definitions allowing to create and/or parse
427 ;;; entries and fields:
429 ;;; fields
430 (defun bibtex-cfield (name text)
431 "Create a regexp for a bibtex field of name NAME and text TEXT"
432 (concat ",[ \t\n]*\\("
433 name
434 "\\)[ \t\n]*=[ \t\n]*\\("
435 text
436 "\\)"))
437 (defconst bibtex-name-in-cfield 1
438 "The regexp subexpression number of the name part in bibtex-cfield.")
439 (defconst bibtex-text-in-cfield 2
440 "The regexp subexpression number of the text part in bibtex-cfield.")
442 ;;; KAWATA Yasuro <yasuro@qqqq.maekawa.is.uec.ac.jp> reported bug that "/"
443 ;;; was not premitted in field names. The old value of this var was:
444 ;;; "[A-Za-z][---A-Za-z0-9:_+]*"
445 ;;; According to the LaTeX manual, page 71, the legal values are letters,
446 ;;; digits, and punctuation other than comma. Section 2.1 defines
447 ;;; punctuation as:
448 ;;; .:;,?!`'()[]-/*@
449 ;;; and says that += can be used in normal text. Specifically #$%&~_^\{}
450 ;;; are called out as special chars. Some experimentation with LaTeX
451 ;;; indicates that # and ~ definitely don't work, but that the following
452 ;;; citation does! \cite{a0.:;?!`'()[]-/*@_&$^+=|<>}. I chose here to
453 ;;; permit _ since it was previously allowed, but otherwise to only handle
454 ;;; punc and +=
455 ;;; Amendment: I couldn't get a regexp with both "[]"'s and hyphen to
456 ;;; work. It looks like you need them both to be the first entries in a
457 ;;; regexp pattern. [alarson:19930315.0900CST]
459 (defconst bibtex-field-name "[A-Za-z][---A-Za-z0-9.:;?!`'()/*@_+=]*"
460 "Regexp defining the name part of a bibtex field.")
462 ;; bibtex-field-text must be able to handle
463 ;; title = "Proc. Fifteenth Annual" # STOC,
464 ;; month = "10~" # jan,
465 ;; year = "{\noopsort{1973c}}1981",
466 ;; month = apr # "-" # may,
467 ;; key = {Volume-2},
468 ;; note = "Volume~2 is listed under Knuth \cite{book-full}"
469 ;; i have added a few of these, but not all! -- MON
471 (defconst bibtex-field-const
472 "[0-9A-Za-z][---A-Za-z0-9:_+]*"
473 "Format of a bibtex field constant.")
475 (defconst bibtex-field-string
476 (concat
477 "\"[^\"]*[^\\\\]\"\\|\"\"")
478 "Match either a string or an empty string.")
480 (defconst bibtex-field-string-or-const
481 (concat bibtex-field-const "\\|" bibtex-field-string)
482 "Match either bibtex-field-string or bibtex-field-const.")
484 (defconst bibtex-field-text
485 (concat
486 "\\(" bibtex-field-string-or-const "\\)"
487 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*\\|"
488 "{[^{}]*[^\\\\]}")
489 "Regexp defining the text part of a bibtex field: either a string, or
490 an empty string, or a constant followed by one or more # / constant pairs.
491 Also matches simple {...} patterns.")
493 ;(defconst bibtex-field-text
494 ; "\"[^\"]*[^\\\\]\"\\|\"\"\\|[0-9A-Za-z][---A-Za-z0-9:_+]*"
495 ; "Regexp defining the text part of a bibtex field: either a string, or an empty string, or a constant.")
497 (defconst bibtex-field
498 (bibtex-cfield bibtex-field-name bibtex-field-text)
499 "Regexp defining the format of a bibtex field")
501 (defconst bibtex-name-in-field bibtex-name-in-cfield
502 "The regexp subexpression number of the name part in bibtex-field")
503 (defconst bibtex-text-in-field bibtex-text-in-cfield
504 "The regexp subexpression number of the text part in bibtex-field")
506 ;;; references
507 (defconst bibtex-reference-type
508 "@[A-Za-z]+"
509 "Regexp defining the type part of a bibtex reference entry")
510 (defconst bibtex-reference-head
511 (concat "^[ \t]*\\("
512 bibtex-reference-type
513 "\\)[ \t]*[({]\\("
514 bibtex-field-name
515 "\\)")
516 "Regexp defining format of the header line of a bibtex reference entry")
517 (defconst bibtex-type-in-head 1
518 "The regexp subexpression number of the type part in bibtex-reference-head")
519 (defconst bibtex-key-in-head 2
520 "The regexp subexpression number of the key part in
521 bibtex-reference-head")
523 (defconst bibtex-reference
524 (concat bibtex-reference-head
525 "\\([ \t\n]*" bibtex-field "\\)*"
526 "[ \t\n]*[})]")
527 "Regexp defining the format of a bibtex reference entry")
528 (defconst bibtex-type-in-reference bibtex-type-in-head
529 "The regexp subexpression number of the type part in bibtex-reference")
530 (defconst bibtex-key-in-reference bibtex-key-in-head
531 "The regexp subexpression number of the key part in
532 bibtex-reference")
534 ;;; strings
535 (defconst bibtex-string
536 (concat "^[ \t]*@[sS][tT][rR][iI][nN][gG][ \t\n]*[({][ \t\n]*\\("
537 bibtex-field-name
538 "\\)[ \t\n]*=[ \t\n]*\\("
539 bibtex-field-text
540 "\\)[ \t\n]*[})]")
541 "Regexp defining the format of a bibtex string entry")
542 (defconst bibtex-name-in-string 1
543 "The regexp subexpression of the name part in bibtex-string")
544 (defconst bibtex-text-in-string 2
545 "The regexp subexpression of the text part in bibtex-string")
547 (defconst bibtex-name-alignement 2
548 "Alignment for the name part in BibTeX fields.
549 Chosen on aesthetic grounds only.")
551 (defconst bibtex-text-alignment (length " organization = ")
552 "Alignment for the text part in BibTeX fields.
553 Equal to the space needed for the longest name part.")
555 (defun bibtex-current-entry-label (&optional include-cite kill)
556 "Return the label of the bibtex entry containing, or preceeding point.
557 Optional argument INCLUDE-CITE, if true means put a '\\cite{}' around the
558 returned value. Second optional argument KILL, if true, means place the
559 returned value in the kill buffer. Interactively; providing prefix
560 argument makes INCLUDE-CITE true, and kill is true by default.
562 Rationale:
563 The intention is that someone will write a function that can be bound to
564 a mouse key so that people entering TeX can just mouse on the bibtex entry
565 and have the citation key inserted at the current point (which will almost
566 certainly be in some other bufer). In the interim this function is
567 marginally useful for keyboard binding and is not bound by default.
568 Suggested binding is ^C-k."
569 (interactive (list current-prefix-arg t))
570 (save-excursion
571 (beginning-of-bibtex-entry)
572 (re-search-forward bibtex-reference-head (save-excursion (end-of-bibtex-entry) (point)))
573 (let* ((key (buffer-substring (match-beginning bibtex-key-in-head)
574 (match-end bibtex-key-in-head)))
575 (val (if include-cite
576 (format "\\cite{%s}" key)
577 key)))
578 (if kill
579 (put-string-on-kill-ring val))
580 val)))
582 ;;; bibtex mode:
584 ;;;###autoload
585 (defun bibtex-mode ()
586 "Major mode for editing bibtex files.
588 \\{bibtex-mode-map}
590 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
592 The optional fields start with the string OPT, and thus ignored by BibTeX.
593 The OPT string may be removed from a field with \\[bibtex-remove-OPT].
594 \\[bibtex-kill-optional-field] kills the current optional field entirely.
595 \\[bibtex-remove-double-quotes] removes the double-quotes around the text of
596 the current field. \\[bibtex-empty-field] replaces the text of the current
597 field with the default \"\".
599 The command \\[bibtex-clean-entry] cleans the current entry, i.e. (i) removes
600 double-quotes from entirely numerical fields, (ii) removes OPT from all
601 non-empty optional fields, (iii) removes all empty optional fields, and (iv)
602 checks that no non-optional fields are empty.
604 Use \\[bibtex-find-text] to position the dot at the end of the current field.
605 Use \\[bibtex-next-field] to move to end of the next field.
607 \\[bibtex-x-environment] binds a mode-specific X menu to control+right
608 mouse button.
609 \\[bibtex-sun-environment] binds a mode-specific Sun menu to right
610 mouse button.
612 The following may be of interest as well:
614 Functions:
615 find-bibtex-duplicates
616 find-bibtex-entry-location
617 hide-bibtex-entry-bodies
618 sort-bibtex-entries
619 validate-bibtex-buffer
621 Variables:
622 bibtex-clean-entry-zap-empty-opts
623 bibtex-entry-field-alist
624 bibtex-include-OPTannote
625 bibtex-include-OPTcrossref
626 bibtex-include-OPTkey
627 bibtex-maintain-sorted-entries
628 bibtex-mode-user-optional-fields
630 Fields:
631 address
632 Publisher's address
633 annote
634 Long annotation used for annotated bibliographies (begins sentence)
635 author
636 Name(s) of author(s), in BibTeX name format
637 booktitle
638 Book title when the thing being referenced isn't the whole book.
639 For book entries, the title field should be used instead.
640 chapter
641 Chapter number
642 crossref
643 The database key of the entry being cross referenced.
644 edition
645 Edition of a book (e.g., \"second\")
646 editor
647 Name(s) of editor(s), in BibTeX name format.
648 If there is also an author field, then the editor field should be
649 for the book or collection that the work appears in
650 howpublished
651 How something strange has been published (begins sentence)
652 institution
653 Sponsoring institution
654 journal
655 Journal name (macros are provided for many)
657 Alphabetizing and labeling key (needed when no author or editor)
658 month
659 Month (macros are provided)
660 note
661 To help the reader find a reference (begins sentence)
662 number
663 Number of a journal or technical report
664 organization
665 Organization (sponsoring a conference)
666 pages
667 Page number or numbers (use `--' to separate a range)
668 publisher
669 Publisher name
670 school
671 School name (for theses)
672 series
673 The name of a series or set of books.
674 An individual book will will also have it's own title
675 title
676 The title of the thing being referenced
677 type
678 Type of a technical report (e.g., \"Research Note\") to be used
679 instead of the default \"Technical Report\"
680 volume
681 Volume of a journal or multivolume work
682 year
683 Year---should contain only numerals
684 ---------------------------------------------------------
685 Entry to this mode calls the value of bibtex-mode-hook if that value is
686 non-nil."
687 (interactive)
688 (kill-all-local-variables)
689 (set-syntax-table bibtex-mode-syntax-table)
690 (use-local-map bibtex-mode-map)
691 (setq major-mode 'bibtex-mode)
692 (setq mode-name "BibTeX")
693 (set-syntax-table bibtex-mode-syntax-table)
694 (setq local-abbrev-table bibtex-mode-abbrev-table)
695 (make-local-variable 'paragraph-start)
696 (setq paragraph-start "^[ \f\n\t]*$")
697 (auto-fill-mode 1) ; nice alignements
698 (setq left-margin (+ bibtex-text-alignment 1))
700 (run-hooks 'bibtex-mode-hook))
702 (defun bibtex-move-outside-of-entry ()
703 "Make sure we are outside of a bib entry"
704 (cond ((or
705 (= (point) (point-max))
706 (= (point) (point-min))
707 (looking-at "[ \n]*@")
711 (backward-paragraph)
712 (forward-paragraph)))
713 (re-search-forward "[ \t\n]*" (point-max) t))
715 (defun ispell-abstract ()
716 (interactive)
717 (beginning-of-bibtex-entry)
718 (re-search-forward "^[ \t]*[OPT]*abstract[ \t]*=")
719 (ispell-region (point)
720 (save-excursion (forward-sexp) (point))))
722 (defun beginning-of-bibtex-entry ()
723 (interactive)
724 (re-search-backward "^@" nil 'move))
726 (defun skip-whitespace-and-comments ()
727 ;; It might be a good idea to have forward-sexp with argument 0 do what
728 ;; this function tries to do, namely skip whitespace and comments.
729 ;; Maybe a better name for this would be skip-to-next-sexp.
730 ;; alternative implementation:
731 ;; (let ((parse-sexp-ignore-comments t))
732 ;; (forward-sexp 1)
733 ;; (forward-sexp -1))
734 ;; but I've had problems with this not getting the parse of comments
735 ;; right going backward if they contain unbalanced expressions or string
736 ;; quotes. [alarson:19920217.1021CST]
737 (let ((md (match-data)))
738 (unwind-protect
739 (while (cond ((looking-at "\\s>+\\|\\s +")
740 ;; was whitespace
741 ;; NOTE: also checked end-comment. In latex and
742 ;; lisp modes, newline is an end comment, but it
743 ;; should also be a whitespace char.
744 (goto-char (match-end 0)))
745 ;; If looking at beginning of comment, skip to end.
746 ((looking-at "\\s<")
747 (re-search-forward "\\s>"))))
748 (store-match-data md))))
750 ;;; [alarson:19920214.1007CST]
751 (defun end-of-bibtex-entry ()
752 "If inside an entry, move to the end of it, otherwise move to the end
753 of the next entry."
754 (interactive)
755 ;; if point was previously at the end of an entry, this puts us
756 ;; inside the next entry, otherwise we remain in the current one.
757 (progn
758 (skip-whitespace-and-comments)
759 ;;; (skip-chars-forward " \t\n")
760 (end-of-line))
761 (beginning-of-bibtex-entry)
762 (let ((parse-sexp-ignore-comments t))
763 (forward-sexp) ; skip entry type
764 (forward-sexp) ; skip entry body
766 ;(defun end-of-bibtex-entry ()
767 ; (interactive)
768 ; (re-search-forward "}$" nil 'move))
770 (defun ispell-bibtex-entry ()
771 (interactive)
772 (ispell-region (progn (beginning-of-bibtex-entry) (point))
773 (progn (end-of-bibtex-entry) (point))))
775 (defun narrow-to-bibtex-entry ()
776 (interactive)
777 (save-excursion
778 (narrow-to-region (progn (beginning-of-bibtex-entry) (point))
779 (progn (end-of-bibtex-entry) (point)))))
782 (defun beginning-of-first-bibtex-entry ()
783 (goto-char (point-min))
784 (cond
785 ((re-search-forward "^@" nil 'move)
786 (beginning-of-line))
787 ((and (bobp) (eobp))
788 nil)
790 (message "Warning: No bibtex entries found!"))))
792 (defun hide-bibtex-entry-bodies (&optional arg)
793 "Hide all lines between first and last bibtex entries not beginning with @.
794 With argument, show all text."
795 (interactive "P")
796 (save-excursion
797 (beginning-of-first-bibtex-entry)
798 ;; subst-char-in-region modifies the buffer, despite what the
799 ;; documentation says...
800 (let ((modifiedp (buffer-modified-p))
801 (buffer-read-only nil))
802 (if arg
803 (subst-char-in-region (point) (point-max) ?\r ?\n t)
804 (while (save-excursion (re-search-forward "\n[^@]" (point-max) t))
805 (save-excursion (replace-regexp "\n\\([^@]\\)" "\r\\1"))))
806 (setq selective-display (not arg))
807 (set-buffer-modified-p modifiedp))))
809 (defvar bibtex-sort-ignore-string-entries nil
810 "*If true, bibtex @STRING entries are ignored when determining ordering
811 of the buffer (e.g. sorting, locating alphabetical position for new entries,
812 etc.)")
814 (defun sort-bibtex-entries ()
815 "Sort bibtex entries alphabetically by key.
816 Text before the first bibtex entry, and following the last is not affected.
817 If bibtex-sort-ignore-string-entries is true, @string entries will be ignored.
819 Bugs:
820 1. Text between the closing brace ending one bibtex entry, and the @ starting
821 the next, is considered part of the PRECEEDING entry. Perhaps it should be
822 part of the following entry."
823 (interactive)
824 (save-restriction
825 (beginning-of-first-bibtex-entry)
826 (narrow-to-region (point)
827 (save-excursion
828 (goto-char (point-max))
829 (beginning-of-bibtex-entry)
830 (end-of-bibtex-entry)
831 (point)))
832 (sort-subr nil ; reversep
833 ;; begining of record function
834 'forward-line
835 ;; end of record function
836 (function (lambda () (and (re-search-forward "}\\s-*\n[\n \t]*@" nil 'move)
837 (forward-char -2))))
838 ;; start of key function
839 (if bibtex-sort-ignore-string-entries
840 (function (lambda ()
841 (while (and (re-search-forward "^\\s-*\\([@a-zA-Z]*\\)\\s-*{\\s-*")
842 (string-equalp "@string"
843 (buffer-substring (match-beginning 1)
844 (match-end 1)))))
845 nil))
846 (function (lambda () (re-search-forward "{\\s-*") nil)))
847 ;; end of key function
848 (function (lambda () (search-forward ",")))
851 (defun map-bibtex-entries (fun)
852 "Call FUN for each bibtex entry starting with the current, to the end of the file.
853 FUN is called with one argument, the key of the entry, and with point inside the entry.
854 If bibtex-sort-ignore-string-entries is true, FUN will not be called for @string entries."
855 (beginning-of-bibtex-entry)
856 (while (re-search-forward "^@[^{]*{[ \t]*\\([^, ]*\\)" nil t)
857 (if (and bibtex-sort-ignore-string-entries
858 (string-equalp "@string{"
859 (buffer-substring (match-beginning 0)
860 (match-beginning 1))))
861 nil ; ignore the @string entry.
862 (funcall fun (buffer-substring (match-beginning 1) (match-end 1))))))
864 (defun find-bibtex-entry-location (entry-name)
865 "Searches from beginning of current buffer looking for place to put the
866 bibtex entry named ENTRY-NAME. Buffer is assumed to be in sorted order,
867 without duplicates (see \\[sort-bibtex-entries]), if it is not, an error will
868 be signalled."
869 (interactive "sBibtex entry key: ")
870 (let ((previous nil)
871 point)
872 (beginning-of-first-bibtex-entry)
873 (or (catch 'done
874 (map-bibtex-entries (function (lambda (current)
875 (cond
876 ((string-equal entry-name current)
877 (error "Entry duplicates existing!"))
878 ((or (null previous)
879 (string< previous current))
880 (setq previous current
881 point (point))
882 (if (string< entry-name current)
883 (progn
884 (beginning-of-bibtex-entry)
885 ;; Many schemes append strings to
886 ;; existing entries to resolve them,
887 ;; so initial substring matches may
888 ;; indicate a duplicate entry.
889 (let ((idx (string-match (regexp-quote entry-name) current)))
890 (if (and (integerp idx)
891 (zerop idx))
892 (progn
893 (message "Warning: Entry %s may be a duplicate of %s!"
894 entry-name current)
895 (ding t))))
896 (throw 'done t))))
897 ((string-equal previous current)
898 (error "Duplicate here with previous!"))
899 (t (error "Entries out of order here!")))))))
900 (end-of-bibtex-entry))))
902 (defun validate-bibtex-buffer ()
903 "Find some typical errors in bibtex files.
904 1. At signs (@) not as first char of a line.
905 2. Double quotes (\") inside strings.
906 3. Closing braces (}) not the last character of a line."
907 (interactive)
908 (let ((point (point)))
909 (while (re-search-forward ".@" nil t)
910 (let* ((foo (parse-partial-sexp (save-excursion (beginning-of-bibtex-entry)
911 (point))
912 (point)))
913 (in-a-string (nth 3 foo)))
914 (if (not in-a-string)
915 (error "At sign (@) out of place!"))))
916 (goto-char point)
917 (while (search-forward "\"" nil t)
918 (or (looking-at "[,}][ \t]*$")
919 (char-equal (preceding-char) ?\")
920 ;; some versions put closing brace on separate line.
921 (looking-at "[ \t]*\n}")
922 (save-excursion
923 (save-restriction
924 (narrow-to-region (point)
925 (progn (beginning-of-line) (point)))
926 (looking-at "^[ \t]*[a-zA-Z]+[ \t]*=[ \t]*\"$")))
927 (error "Quote out of place, or missing \",\" or \"}\"!")))
928 (goto-char point)
929 ;; This is only approximate, should actually search for close braces,
930 ;; then see if they are inside a string, or at the end of a line.
931 ;; This just gets the typical case of whitespace after a closing brace.
932 (while (search-forward "}[ \t]+$" nil t)
933 (error "Brace not last char of line!"))
934 (goto-char point)
935 (message "Bibtex buffer appears o.k.")))
937 (defun find-bibtex-duplicates ()
938 "Searches forward in current buffer looking for duplicate bibtex entries.
939 Buffer is assumed to be sorted, see \\[sort-bibtex-entries]"
940 (interactive)
941 (let ((point (point)))
942 ;; errors if things are not right...
943 (find-bibtex-entry-location (make-string 10 255))
944 (goto-char point)
945 (message "No duplicates found!")))
948 ;;; assoc doesn't ignore case, so we need an assoc that does...
949 (defun assoc-string-equalp (thing alist)
950 (or (assoc thing alist)
951 (while (and alist
952 (not (string-equalp thing (car (car alist)))))
953 (setq alist (cdr alist)))
954 (car alist)))
956 (defvar bibtex-maintain-sorted-entries nil
957 "*If true, bibtex-mode will attempt to maintain all bibtex entries in
958 sorted order.
960 Note that this is more a property of a file than a personal preference and
961 as such should normally be set via a file local variable entry.")
963 (defun bibtex-entry (entry-type &optional required optional)
964 (interactive (let* ((completion-ignore-case t)
965 (e-t (completing-read "Entry Type: " bibtex-entry-field-alist
966 nil t)))
967 (list e-t)))
968 (if (and (null required) (null optional))
969 (let* ((e (assoc-string-equalp entry-type bibtex-entry-field-alist))
970 (r-n-o (elt e 1))
971 (c-ref (elt e 2)))
972 (if (null e)
973 (error "Bibtex entry type %s not defined!"))
974 (if (and bibtex-include-OPTcrossref c-ref)
975 (setq required (elt c-ref 0)
976 optional (elt c-ref 1))
977 (setq required (elt r-n-o 0)
978 optional (elt r-n-o 1)))))
979 (let ((key (if bibtex-maintain-sorted-entries
980 (read-string (format "%s key: " entry-type)))))
981 (if key
982 (find-bibtex-entry-location key))
983 (bibtex-move-outside-of-entry)
984 (insert "@" entry-type "{")
985 (if key
986 (insert key))
987 (save-excursion
988 (mapcar 'bibtex-make-field required)
989 (if bibtex-include-OPTcrossref
990 (bibtex-make-optional-field "crossref"))
991 (if bibtex-include-OPTkey
992 (bibtex-make-optional-field "key"))
993 (mapcar 'bibtex-make-optional-field optional)
994 (mapcar 'bibtex-make-optional-field
995 bibtex-mode-user-optional-fields)
996 (if bibtex-include-OPTannote
997 (bibtex-make-optional-field "annote"))
998 (insert "\n}\n\n"))
999 (if key
1000 (bibtex-next-field t))
1001 (run-hooks 'bibtex-add-entry-hook)))
1003 ;; (defun bibtex-entry (entry-type required optional)
1004 ;; (bibtex-move-outside-of-entry)
1005 ;; (insert (concat "@" entry-type "{,\n\n}\n\n"))
1006 ;; (previous-line 3)
1007 ;; (insert (mapconcat 'bibtex-make-entry required ",\n"))
1008 ;; (if required
1009 ;; (if optional
1010 ;; (insert ",\n")))
1011 ;; (insert (mapconcat 'bibtex-make-OPT-entry optional ",\n"))
1012 ;; (if bibtex-mode-user-optional-fields ;MON...
1013 ;; (progn
1014 ;; (if optional
1015 ;; (insert ",\n"))
1016 ;; (insert (mapconcat 'bibtex-make-OPT-entry
1017 ;; bibtex-mode-user-optional-fields
1018 ;; ",\n")))) ;MON
1019 ;; (up-list -1)
1020 ;; (forward-char 1))
1023 (defun bibtex-make-field (e-t)
1024 (interactive "sBibTeX entry type: ")
1025 (let ((name (if (consp e-t) (car e-t) e-t))
1026 (value (if (consp e-t) (cdr e-t) "")))
1027 (insert ",\n")
1028 (indent-to-column bibtex-name-alignement)
1029 (insert name " = ")
1030 (indent-to-column bibtex-text-alignment)
1031 ;; lucid emacs prin1-to-string breaks the undo chain. When they fix
1032 ;; that, the hack can be removed. [alarson:19930316.0805CST]
1033 ; (insert (prin1-to-string value))
1034 ;; begin hack
1035 (insert (format (if (stringp value) "\"%s\"" "%s")
1036 value))
1037 ;; end hack
1038 nil))
1040 (defun bibtex-make-optional-field (e-t)
1041 (interactive "sOptional BibTeX entry type: ")
1042 (if (consp e-t)
1043 (setq e-t (cons (concat "OPT" (car e-t)) (cdr e-t)))
1044 (setq e-t (concat "OPT" e-t)))
1045 (bibtex-make-field e-t))
1047 ;; What to do about crossref? if present, journal and year are
1048 ;; both optional. Due to this, i move all of them into optional. -- MON
1050 (defun bibtex-Article ()
1051 (interactive)
1052 (bibtex-entry "Article"))
1054 (defun bibtex-Book ()
1055 (interactive)
1056 (bibtex-entry "Book"))
1058 (defun bibtex-Booklet ()
1059 (interactive)
1060 (bibtex-entry "Booklet"))
1062 ;(defun bibtex-DEAthesis ()
1063 ; (interactive)
1064 ; (bibtex-entry "DEAthesis"))
1066 (defun bibtex-InBook ()
1067 (interactive)
1068 (bibtex-entry "InBook"))
1070 (defun bibtex-InCollection ()
1071 (interactive)
1072 (bibtex-entry "InCollection"))
1074 (defun bibtex-InProceedings ()
1075 (interactive)
1076 (bibtex-entry "InProceedings"))
1078 (defun bibtex-Manual ()
1079 (interactive)
1080 (bibtex-entry "Manual"))
1082 (defun bibtex-MastersThesis ()
1083 (interactive)
1084 (bibtex-entry "MastersThesis"))
1086 (defun bibtex-Misc ()
1087 (interactive)
1088 (bibtex-entry "Misc"))
1090 (defun bibtex-PhdThesis ()
1091 (interactive)
1092 (bibtex-entry "PhdThesis"))
1094 (defun bibtex-Proceedings ()
1095 (interactive)
1096 (bibtex-entry "Proceedings"))
1098 (defun bibtex-TechReport ()
1099 (interactive)
1100 (bibtex-entry "TechReport"))
1102 (defun bibtex-Unpublished ()
1103 (interactive)
1104 (bibtex-entry "Unpublished"))
1106 (defun bibtex-string ()
1107 (interactive)
1108 (bibtex-move-outside-of-entry)
1109 (insert "@string{ = \"\"}\n")
1110 (previous-line 1)
1111 (forward-char 8))
1113 (defun bibtex-preamble ()
1114 (interactive)
1115 (bibtex-move-outside-of-entry)
1116 (insert "@Preamble{}\n")
1117 (previous-line 1)
1118 (forward-char 10))
1120 (defun bibtex-next-field (arg)
1121 "Finds end of text of next BibTeX field; with arg, to its beginning"
1122 (interactive "P")
1123 (bibtex-inside-field)
1124 (let ((start (point)))
1125 (condition-case ()
1126 (progn
1127 (bibtex-enclosing-field)
1128 (goto-char (match-end 0))
1129 (forward-char 2))
1130 (error
1131 (goto-char start)
1132 (end-of-line)
1133 (forward-char 1))))
1134 (bibtex-find-text arg))
1136 ;; (defun bibtex-next-field ()
1137 ;; "Finds end of text of next field."
1138 ;; (interactive)
1139 ;; (condition-case ()
1140 ;; (progn
1141 ;; (bibtex-inside-field)
1142 ;; (re-search-forward ",[ \t\n]*" (point-max) 1)
1143 ;; (bibtex-enclosing-field)
1144 ;; (bibtex-inside-field))
1145 ;; (error nil)))
1147 (defun bibtex-find-text (arg)
1148 "Go to end of text of current field; with arg, go to beginning."
1149 (interactive "P")
1150 (bibtex-inside-field)
1151 (bibtex-enclosing-field)
1152 (if arg
1153 (progn
1154 (goto-char (match-beginning bibtex-text-in-field))
1155 (if (looking-at "\"")
1156 (forward-char 1)))
1157 (goto-char (match-end bibtex-text-in-field))
1158 (if (= (preceding-char) ?\")
1159 (forward-char -1))))
1161 ;; (defun bibtex-find-text ()
1162 ;; "Go to end of text of current field."
1163 ;; (interactive)
1164 ;; (condition-case ()
1165 ;; (progn
1166 ;; (bibtex-inside-field)
1167 ;; (bibtex-enclosing-field)
1168 ;; (goto-char (match-end bibtex-text-in-field))
1169 ;; (bibtex-inside-field))
1170 ;; (error nil)))
1172 (defun bibtex-remove-OPT ()
1173 "Removes the 'OPT' starting optional arguments and goes to end of text"
1174 (interactive)
1175 (bibtex-inside-field)
1176 (bibtex-enclosing-field)
1177 (save-excursion
1178 (goto-char (match-beginning bibtex-name-in-field))
1179 (if (looking-at "OPT")
1180 ;; sct@dcs.edinburgh.ac.uk
1181 (progn
1182 (delete-char (length "OPT"))
1183 (search-forward "=")
1184 (delete-horizontal-space)
1185 (indent-to-column bibtex-text-alignment))))
1186 (bibtex-inside-field))
1188 (defun bibtex-inside-field ()
1189 "Try to avoid point being at end of a bibtex field."
1190 (interactive)
1191 (end-of-line)
1192 (skip-chars-backward " \t") ;MON - maybe delete these chars?
1193 (cond ((= (preceding-char) ?,)
1194 (forward-char -2))) ; -1 --> -2 sct@dcs.edinburgh.ac.uk
1195 (cond ((= (preceding-char) ?\")
1196 (forward-char -1)))) ;MON - only go back if quote
1198 (defun bibtex-remove-double-quotes ()
1199 "Removes \"\" around string."
1200 (interactive)
1201 (save-excursion
1202 (bibtex-inside-field)
1203 (bibtex-enclosing-field)
1204 (let ((start (match-beginning bibtex-text-in-field))
1205 (stop (match-end bibtex-text-in-field)))
1206 (goto-char stop)
1207 (forward-char -1)
1208 (if (looking-at "\"")
1209 (delete-char 1))
1210 (goto-char start)
1211 (if (looking-at "\"")
1212 (delete-char 1)))))
1214 (defun bibtex-kill-optional-field ()
1215 "Kill the entire enclosing optional BibTeX field"
1216 (interactive)
1217 (bibtex-inside-field)
1218 (bibtex-enclosing-field)
1219 (goto-char (match-beginning bibtex-name-in-field))
1220 (let ((the-end (match-end 0))
1221 (the-beginning (match-beginning 0)))
1222 (if (looking-at "OPT")
1223 (progn
1224 (goto-char the-end)
1225 (skip-chars-forward " \t\n,")
1226 (kill-region the-beginning the-end))
1227 (error "Mandatory fields can't be killed"))))
1229 (defun bibtex-empty-field ()
1230 "Delete the text part of the current field, replace with empty text"
1231 (interactive)
1232 (bibtex-inside-field)
1233 (bibtex-enclosing-field)
1234 (goto-char (match-beginning bibtex-text-in-field))
1235 (kill-region (point) (match-end bibtex-text-in-field))
1236 (insert "\"\"")
1237 (bibtex-find-text t))
1240 (defun bibtex-pop-previous (arg)
1241 "Replace text of current field with the text of similar field in previous entry.
1242 With arg, go up ARG entries. Repeated, goes up so many times. May be
1243 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
1244 (interactive "p")
1245 (bibtex-inside-field)
1246 (save-excursion
1247 ; parse current field
1248 (bibtex-enclosing-field)
1249 (let ((start-old-text (match-beginning bibtex-text-in-field))
1250 (stop-old-text (match-end bibtex-text-in-field))
1251 (start-name (match-beginning bibtex-name-in-field))
1252 (stop-name (match-end bibtex-name-in-field))
1253 (new-text))
1254 (goto-char start-name)
1255 ; construct regexp for previous field with same name as this one
1256 (let ((matching-entry
1257 (bibtex-cfield
1258 (buffer-substring (if (looking-at "OPT")
1259 (+ (point) (length "OPT"))
1260 (point))
1261 stop-name)
1262 bibtex-field-text)))
1264 ; if executed several times in a row, start each search where the
1265 ; last one finished
1266 (cond ((or (eq last-command 'bibtex-pop-previous)
1267 (eq last-command 'bibtex-pop-next))
1271 (bibtex-enclosing-reference)
1272 (setq bibtex-pop-previous-search-point (match-beginning 0))
1273 (setq bibtex-pop-next-search-point (match-end 0))))
1274 (goto-char bibtex-pop-previous-search-point)
1276 ; Now search for arg'th previous similar field
1277 (cond
1278 ((re-search-backward matching-entry (point-min) t arg)
1279 (setq new-text
1280 (buffer-substring (match-beginning bibtex-text-in-cfield)
1281 (match-end bibtex-text-in-cfield)))
1282 ; Found a matching field. Remember boundaries.
1283 (setq bibtex-pop-next-search-point (match-end 0))
1284 (setq bibtex-pop-previous-search-point (match-beginning 0))
1285 (bibtex-flash-head)
1286 ; Go back to where we started, delete old text, and pop new.
1287 (goto-char stop-old-text)
1288 (delete-region start-old-text stop-old-text)
1289 (insert new-text))
1290 (t ; search failed
1291 (error "No previous matching BibTeX field."))))))
1292 (setq this-command 'bibtex-pop-previous))
1294 (defun bibtex-pop-next (arg)
1295 "Replace text of current field with the text of similar field in next entry.
1296 With arg, go up ARG entries. Repeated, goes up so many times. May be
1297 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
1298 (interactive "p")
1299 (bibtex-inside-field)
1300 (save-excursion
1301 ; parse current field
1302 (bibtex-enclosing-field)
1303 (let ((start-old-text (match-beginning bibtex-text-in-field))
1304 (stop-old-text (match-end bibtex-text-in-field))
1305 (start-name (match-beginning bibtex-name-in-field))
1306 (stop-name (match-end bibtex-name-in-field))
1307 (new-text))
1308 (goto-char start-name)
1309 ; construct regexp for next field with same name as this one,
1310 ; ignoring possible OPT's
1311 (let ((matching-entry
1312 (bibtex-cfield
1313 (buffer-substring (if (looking-at "OPT")
1314 (+ (point) (length "OPT"))
1315 (point))
1316 stop-name)
1317 bibtex-field-text)))
1319 ; if executed several times in a row, start each search where the
1320 ; last one finished
1321 (cond ((or (eq last-command 'bibtex-pop-next)
1322 (eq last-command 'bibtex-pop-previous))
1326 (bibtex-enclosing-reference)
1327 (setq bibtex-pop-previous-search-point (match-beginning 0))
1328 (setq bibtex-pop-next-search-point (match-end 0))))
1329 (goto-char bibtex-pop-next-search-point)
1331 ; Now search for arg'th next similar field
1332 (cond
1333 ((re-search-forward matching-entry (point-max) t arg)
1334 (setq new-text
1335 (buffer-substring (match-beginning bibtex-text-in-cfield)
1336 (match-end bibtex-text-in-cfield)))
1337 ; Found a matching field. Remember boundaries.
1338 (setq bibtex-pop-next-search-point (match-end 0))
1339 (setq bibtex-pop-previous-search-point (match-beginning 0))
1340 (bibtex-flash-head)
1341 ; Go back to where we started, delete old text, and pop new.
1342 (goto-char stop-old-text)
1343 (delete-region start-old-text stop-old-text)
1344 (insert new-text))
1345 (t ; search failed
1346 (error "No next matching BibTeX field."))))))
1347 (setq this-command 'bibtex-pop-next))
1349 (defun bibtex-flash-head ()
1350 "Flash at BibTeX reference head before point, if exists. (Moves point)."
1351 (let ((flash))
1352 (cond ((re-search-backward bibtex-reference-head (point-min) t)
1353 (goto-char (match-beginning bibtex-type-in-head))
1354 (setq flash (match-end bibtex-key-in-reference)))
1356 (end-of-line)
1357 (skip-chars-backward " \t")
1358 (setq flash (point))
1359 (beginning-of-line)
1360 (skip-chars-forward " \t")))
1361 (if (pos-visible-in-window-p (point))
1362 (sit-for 1)
1363 (message "From: %s"
1364 (buffer-substring (point) flash)))))
1368 (defun bibtex-enclosing-field ()
1369 "Search for BibTeX field enclosing point.
1370 Point moves to end of field; also, use match-beginning and match-end
1371 to parse the field."
1372 ;; sct@dcs.edinburgh.ac.uk
1373 (let ((old-point (point)))
1374 (condition-case errname
1375 (bibtex-enclosing-regexp bibtex-field)
1376 (search-failed
1377 (goto-char old-point)
1378 (error "Can't find enclosing BibTeX field.")))))
1380 (defun bibtex-enclosing-reference ()
1381 "Search for BibTeX reference enclosing point.
1382 Point moves to end of reference; also, use match-beginning and match-end
1383 to parse the reference."
1384 ;; sct@dcs.edinburgh.ac.uk
1385 (let ((old-point (point)))
1386 (condition-case errname
1387 (bibtex-enclosing-regexp bibtex-reference)
1388 (search-failed
1389 (goto-char old-point)
1390 (error "Can't find enclosing BibTeX reference.")))))
1392 (defun bibtex-enclosing-regexp (regexp)
1393 "Search for REGEXP enclosing point.
1394 Point moves to end of REGEXP. See also match-beginning and match-end.
1395 If an enclosing REGEXP is not found, signals search-failed; point is left in
1396 an undefined location.
1398 [Doesn't something like this exist already?]"
1400 (interactive "sRegexp: ")
1401 ; compute reasonable limits for the loop
1402 (let* ((initial (point))
1403 (right (if (re-search-forward regexp (point-max) t)
1404 (match-end 0)
1405 (point-max)))
1406 (left
1407 (progn
1408 (goto-char initial)
1409 (if (re-search-backward regexp (point-min) t)
1410 (match-beginning 0)
1411 (point-min)))))
1412 ; within the prescribed limits, loop until a match is found
1413 (goto-char left)
1414 (re-search-forward regexp right nil 1)
1415 (if (> (match-beginning 0) initial)
1416 (signal 'search-failed (list regexp)))
1417 (while (<= (match-end 0) initial)
1418 (re-search-forward regexp right nil 1)
1419 (if (> (match-beginning 0) initial)
1420 (signal 'search-failed (list regexp))))
1423 (defun bibtex-clean-entry ()
1424 "For all optional fields of current BibTeX entry: if empty, kill the whole field; otherwise, remove the \"OPT\" string in the name; if text numerical, remove double-quotes. For all mandatory fields: if empty, signal error."
1425 (interactive)
1426 (beginning-of-bibtex-entry)
1427 (let ((start (point)))
1428 (save-restriction
1429 (narrow-to-region start (save-excursion (end-of-bibtex-entry) (point)))
1430 (while (re-search-forward bibtex-field (point-max) t 1)
1431 (let ((begin-field (match-beginning 0))
1432 (end-field (match-end 0))
1433 (begin-name (match-beginning bibtex-name-in-field))
1434 (end-name (match-end bibtex-name-in-field))
1435 (begin-text (match-beginning bibtex-text-in-field))
1436 (end-text (match-end bibtex-text-in-field))
1438 (goto-char begin-name)
1439 (cond ((and
1440 (looking-at "OPT")
1441 bibtex-clean-entry-zap-empty-opts)
1442 (goto-char begin-text)
1443 (if (looking-at "\"\"") ; empty: delete whole field
1444 (delete-region begin-field end-field)
1445 ; otherwise: not empty, delete "OPT"
1446 (goto-char begin-name)
1447 (delete-char (length "OPT"))
1448 (progn
1449 ;; fixup alignment. [alarson:19920309.2047CST]
1450 (search-forward "=")
1451 (delete-horizontal-space)
1452 (indent-to-column bibtex-text-alignment))
1453 (goto-char begin-field) ; and loop to go through next test
1456 (goto-char begin-text)
1457 (cond ((looking-at "\"[0-9]+\"") ; if numerical,
1458 (goto-char end-text)
1459 (delete-char -1) ; delete enclosing double-quotes
1460 (goto-char begin-text)
1461 (delete-char 1)
1462 (goto-char end-field) ; go to end for next search
1463 (forward-char -2) ; to compensate for the 2 quotes deleted
1465 ((looking-at "\"\"") ; if empty quotes, complain
1466 (forward-char 1)
1467 (if (not (or (equal (buffer-substring
1468 begin-name
1469 (+ begin-name 3))
1470 "OPT")
1471 (equal (buffer-substring
1472 begin-name
1473 (+ begin-name 3))
1474 "opt")))
1475 (error "Mandatory field ``%s'' is empty"
1476 (buffer-substring begin-name end-name))))
1478 (goto-char end-field))))))))
1479 (goto-char start)
1480 (end-of-bibtex-entry)
1481 ;; sct@dcs.edinburgh.ac.uk
1482 (save-excursion
1483 (previous-line 1)
1484 (end-of-line)
1485 (if (eq (preceding-char) ?,)
1486 (backward-delete-char 1)))
1487 (skip-whitespace-and-comments)))
1491 ;;; X window menus for bibtex mode
1493 (defun bibtex-x-help (arg)
1494 "Mouse commands for BibTeX mode"
1496 (let ((selection
1497 (x-popup-menu
1499 '("BibTeX commands"
1500 ("BibTeX entry types"
1501 (" article in conference Proceedings " . bibtex-InProceedings)
1502 (" Article in journal " . bibtex-Article)
1503 (" Book " . bibtex-Book)
1504 (" Booklet " . bibtex-Booklet)
1505 (" Conference " . bibtex-InProceedings)
1506 (" Master's Thesis " . bibtex-MastersThesis)
1507 ; (" DEA Thesis " . bibtex-DEAthesis)
1508 (" Phd. Thesis " . bibtex-PhdThesis)
1509 (" Technical Report " . bibtex-TechReport)
1510 (" technical Manual " . bibtex-Manual)
1511 (" conference Proceedings " . bibtex-Proceedings)
1512 (" a chapter in a Book " . bibtex-InBook)
1513 (" an article in a Collection " . bibtex-InCollection)
1514 (" miscellaneous " . bibtex-Misc)
1515 (" unpublished " . bibtex-Unpublished)
1516 (" string " . bibtex-string)
1517 (" preamble " . bibtex-preamble)
1519 ("Moving around and editing"
1520 (" next field " . bibtex-next-field)
1521 (" to end of field " . bibtex-find-text)
1522 ("snatch from similar preceding field" . bibtex-pop-previous)
1523 ("snatch from similar following field" . bibtex-pop-next)
1524 (" remove OPT " . bibtex-remove-OPT)
1525 (" remove quotes "
1526 . bibtex-remove-double-quotes)
1527 (" clean up entry " . bibtex-clean-entry)
1529 ("help"
1530 (" describe BibTeX mode " . describe-mode)
1531 )))))
1532 (and selection (call-interactively selection))))
1534 (defun bibtex-x-environment ()
1535 "Set up X menus for BibTeX mode. Call it as bibtex-mode-hook, or interactively"
1536 (interactive)
1537 (require 'x-mouse)
1538 (define-key mouse-map x-button-c-right 'bibtex-x-help)
1543 ;; Please don't send anything to bug-gnu-emacs about these Sunwindows functions
1544 ;; since we aren't interested. See etc/SUN-SUPPORT for the reasons why
1545 ;; we consider this nothing but a distraction from our work.
1547 ;(defmacro eval-in-menu-window (&rest l)
1548 ; "Evaluates its argument in the window in which the mouse button was pressed."
1549 ; (list 'eval-in-window '*menu-window* l))
1551 ;(defmenu bibtex-sun-entry-menu
1552 ; ("Article In Conf. Proc." eval-in-menu-window bibtex-InProceedings)
1553 ; ("Article In Journal" eval-in-menu-window bibtex-Article)
1554 ; ("Book" eval-in-menu-window bibtex-Book)
1555 ; ("Booklet" eval-in-menu-window bibtex-Booklet)
1556 ; ("Master's Thesis" eval-in-menu-window bibtex-MastersThesis)
1557 ; ("PhD. Thesis" eval-in-menu-window bibtex-PhdThesis)
1558 ; ("Technical Report" eval-in-menu-window bibtex-TechReport)
1559 ; ("Technical Manual" eval-in-menu-window bibtex-Manual)
1560 ; ("Conference Proceedings" eval-in-menu-window bibtex-Proceedings)
1561 ; ("In A Book" eval-in-menu-window bibtex-InBook)
1562 ; ("In A Collection" eval-in-menu-window bibtex-InCollection)
1563 ; ("Miscellaneous" eval-in-menu-window bibtex-Misc)
1564 ; ("Unpublished" eval-in-menu-window bibtex-Unpublished)
1565 ; ("string" eval-in-menu-window bibtex-string)
1566 ; ("preamble" eval-in-menu-window bibtex-preamble))
1568 ;(defmenu bibtex-sun-menu
1569 ; ("BibTeX menu")
1570 ; ("add entry" . bibtex-sun-entry-menu)
1571 ; ("next field" eval-in-menu-window bibtex-next-field nil)
1572 ; ("to end of field" eval-in-menu-window bibtex-find-text nil)
1573 ; ("snatch similar preceding field" eval-in-menu-window bibtex-pop-previous 1)
1574 ; ("snatch similar following field" eval-in-menu-window bibtex-pop-next 1)
1575 ; ("remove OPT" eval-in-menu-window bibtex-remove-OPT)
1576 ; ("remove quotes" eval-in-menu-window bibtex-remove-double-quotes)
1577 ; ("clean entry" eval-in-menu-window bibtex-clean-entry)
1578 ; ("describe BibTeX mode" eval-in-menu-window describe-mode)
1579 ; ("Main Emacs menu" . emacs-menu))
1581 ;(defun bibtex-sun-menu-eval (window x y)
1582 ; "Pop-up menu of BibTeX commands."
1583 ; (sun-menu-evaluate window (1+ x) (1- y) 'bibtex-sun-menu))
1585 ;(defun bibtex-sun-environment ()
1586 ; "Set up sun menus for BibTeX mode. Call it as bibtex-mode-hook, or
1587 ;interactively"
1588 ; (interactive)
1589 ; (local-set-mouse '(text right) 'bibtex-sun-menu-eval))
1592 ;;; bibtex.el ends here