(bibtex-field-name, bibtex-field-const, bibtex-field-text):
[emacs.git] / lisp / textmodes / bibtex.el
bloba582a0d674674e17792590cfb79b6ade8b1ea093
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-quarterly" # 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 wouldn't 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-c\C-b" '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 ;;; New amendment[MB]: I have corrected the following regexp according to
460 ;;; emacs19 rules. I also added ']' to the list of characters allowed in
461 ;;; a field name.
463 (defconst bibtex-field-name "[A-Za-z][]A-Za-z0-9.:;?!`'()/*@_+=-]*"
464 "Regexp defining the name part of a bibtex field.")
466 ;; bibtex-field-text must be able to handle
467 ;; title = "Proc. Fifteenth Annual" # STOC,
468 ;; month = "10~" # jan,
469 ;; year = "{\noopsort{1973c}}1981",
470 ;; month = apr # "-" # may,
471 ;; key = {Volume-2},
472 ;; note = "Volume~2 is listed under Knuth \cite{book-full}"
473 ;; i have added a few of these, but not all! -- MON
475 (defconst bibtex-field-const
476 "[0-9A-Za-z][A-Za-z0-9:_+-]*"
477 "Format of a bibtex field constant.")
479 (defconst bibtex-field-string
480 (concat
481 "\"[^\"]*[^\\\\]\"\\|\"\"")
482 "Match either a string or an empty string.")
484 (defconst bibtex-field-string-or-const
485 (concat bibtex-field-const "\\|" bibtex-field-string)
486 "Match either bibtex-field-string or bibtex-field-const.")
488 (defconst bibtex-field-text
489 (concat
490 "\\(" bibtex-field-string-or-const "\\)"
491 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*\\|"
492 "{[^{}]*[^\\\\]}")
493 "Regexp defining the text part of a bibtex field: either a string, or
494 an empty string, or a constant followed by one or more # / constant pairs.
495 Also matches simple {...} patterns.")
497 ;(defconst bibtex-field-text
498 ; "\"[^\"]*[^\\\\]\"\\|\"\"\\|[0-9A-Za-z][A-Za-z0-9:_+-]*"
499 ; "Regexp defining the text part of a bibtex field: either a string, or an empty string, or a constant.")
501 (defconst bibtex-field
502 (bibtex-cfield bibtex-field-name bibtex-field-text)
503 "Regexp defining the format of a bibtex field")
505 (defconst bibtex-name-in-field bibtex-name-in-cfield
506 "The regexp subexpression number of the name part in bibtex-field")
507 (defconst bibtex-text-in-field bibtex-text-in-cfield
508 "The regexp subexpression number of the text part in bibtex-field")
510 ;;; references
511 (defconst bibtex-reference-type
512 "@[A-Za-z]+"
513 "Regexp defining the type part of a bibtex reference entry")
514 (defconst bibtex-reference-head
515 (concat "^[ \t]*\\("
516 bibtex-reference-type
517 "\\)[ \t]*[({]\\("
518 bibtex-field-name
519 "\\)")
520 "Regexp defining format of the header line of a bibtex reference entry")
521 (defconst bibtex-type-in-head 1
522 "The regexp subexpression number of the type part in bibtex-reference-head")
523 (defconst bibtex-key-in-head 2
524 "The regexp subexpression number of the key part in
525 bibtex-reference-head")
527 (defconst bibtex-reference
528 (concat
529 ;; This is the unparenthesized equivalent of bibtex-reference-head:
530 "^[ \t]*"
531 bibtex-reference-type
532 "[ \t]*[({]\\("
533 bibtex-field-name
534 "\\)"
535 ;; End of unparenthesized equivalent of bibtex-reference-head
536 "\\([ \t\n]*" bibtex-field "\\)*"
537 "[ \t\n]*[})]")
538 "Regexp defining the format of a bibtex reference entry")
539 (defconst bibtex-type-in-reference bibtex-type-in-head
540 "The regexp subexpression number of the type part in bibtex-reference")
541 (defconst bibtex-key-in-reference bibtex-key-in-head
542 "The regexp subexpression number of the key part in
543 bibtex-reference")
545 ;;; strings
546 (defconst bibtex-string
547 (concat "^[ \t]*@[sS][tT][rR][iI][nN][gG][ \t\n]*[({][ \t\n]*\\("
548 bibtex-field-name
549 "\\)[ \t\n]*=[ \t\n]*\\("
550 bibtex-field-text
551 "\\)[ \t\n]*[})]")
552 "Regexp defining the format of a bibtex string entry")
553 (defconst bibtex-name-in-string 1
554 "The regexp subexpression of the name part in bibtex-string")
555 (defconst bibtex-text-in-string 2
556 "The regexp subexpression of the text part in bibtex-string")
558 (defconst bibtex-name-alignment 2
559 "Alignment for the name part in BibTeX fields.
560 Chosen on aesthetic grounds only.")
562 (defconst bibtex-text-alignment (length " organization = ")
563 "Alignment for the text part in BibTeX fields.
564 Equal to the space needed for the longest name part.")
566 (defun bibtex-current-entry-label (&optional include-cite kill)
567 "Return the label of the bibtex entry containing, or preceding point.
568 Optional argument INCLUDE-CITE, if true means put a '\\cite{}' around the
569 returned value. Second optional argument KILL, if true, means place the
570 returned value in the kill buffer. Interactively; providing prefix
571 argument makes INCLUDE-CITE true, and kill is true by default.
573 Rationale:
574 The intention is that someone will write a function that can be bound to
575 a mouse key so that people entering TeX can just mouse on the bibtex entry
576 and have the citation key inserted at the current point (which will almost
577 certainly be in some other buffer). In the interim this function is
578 marginally useful for keyboard binding and is not bound by default.
579 Suggested binding is ^C-k."
580 (interactive (list current-prefix-arg t))
581 (save-excursion
582 (beginning-of-bibtex-entry)
583 (re-search-forward bibtex-reference-head (save-excursion (end-of-bibtex-entry) (point)))
584 (let* ((key (buffer-substring (match-beginning bibtex-key-in-head)
585 (match-end bibtex-key-in-head)))
586 (val (if include-cite
587 (format "\\cite{%s}" key)
588 key)))
589 (if kill
590 (put-string-on-kill-ring val))
591 val)))
593 ;;; bibtex mode:
595 ;;;###autoload
596 (defun bibtex-mode ()
597 "Major mode for editing bibtex files.
599 \\{bibtex-mode-map}
601 A command such as \\[bibtex-Book] will outline the fields for a BibTeX book entry.
603 The optional fields start with the string OPT, and thus ignored by BibTeX.
604 The OPT string may be removed from a field with \\[bibtex-remove-OPT].
605 \\[bibtex-kill-optional-field] kills the current optional field entirely.
606 \\[bibtex-remove-double-quotes] removes the double-quotes around the text of
607 the current field. \\[bibtex-empty-field] replaces the text of the current
608 field with the default \"\".
610 The command \\[bibtex-clean-entry] cleans the current entry, i.e. (i) removes
611 double-quotes from entirely numerical fields, (ii) removes OPT from all
612 non-empty optional fields, (iii) removes all empty optional fields, and (iv)
613 checks that no non-optional fields are empty.
615 Use \\[bibtex-find-text] to position the dot at the end of the current field.
616 Use \\[bibtex-next-field] to move to end of the next field.
618 The following may be of interest as well:
620 Functions:
621 find-bibtex-duplicates
622 find-bibtex-entry-location
623 hide-bibtex-entry-bodies
624 sort-bibtex-entries
625 validate-bibtex-buffer
627 Variables:
628 bibtex-clean-entry-zap-empty-opts
629 bibtex-entry-field-alist
630 bibtex-include-OPTannote
631 bibtex-include-OPTcrossref
632 bibtex-include-OPTkey
633 bibtex-maintain-sorted-entries
634 bibtex-mode-user-optional-fields
636 Fields:
637 address
638 Publisher's address
639 annote
640 Long annotation used for annotated bibliographies (begins sentence)
641 author
642 Name(s) of author(s), in BibTeX name format
643 booktitle
644 Book title when the thing being referenced isn't the whole book.
645 For book entries, the title field should be used instead.
646 chapter
647 Chapter number
648 crossref
649 The database key of the entry being cross referenced.
650 edition
651 Edition of a book (e.g., \"second\")
652 editor
653 Name(s) of editor(s), in BibTeX name format.
654 If there is also an author field, then the editor field should be
655 for the book or collection that the work appears in
656 howpublished
657 How something strange has been published (begins sentence)
658 institution
659 Sponsoring institution
660 journal
661 Journal name (macros are provided for many)
663 Alphabetizing and labeling key (needed when no author or editor)
664 month
665 Month (macros are provided)
666 note
667 To help the reader find a reference (begins sentence)
668 number
669 Number of a journal or technical report
670 organization
671 Organization (sponsoring a conference)
672 pages
673 Page number or numbers (use `--' to separate a range)
674 publisher
675 Publisher name
676 school
677 School name (for theses)
678 series
679 The name of a series or set of books.
680 An individual book will will also have it's own title
681 title
682 The title of the thing being referenced
683 type
684 Type of a technical report (e.g., \"Research Note\") to be used
685 instead of the default \"Technical Report\"
686 volume
687 Volume of a journal or multivolume work
688 year
689 Year---should contain only numerals
690 ---------------------------------------------------------
691 Entry to this mode calls the value of bibtex-mode-hook if that value is
692 non-nil."
693 (interactive)
694 (kill-all-local-variables)
695 (set-syntax-table bibtex-mode-syntax-table)
696 (use-local-map bibtex-mode-map)
697 (setq major-mode 'bibtex-mode)
698 (setq mode-name "BibTeX")
699 (set-syntax-table bibtex-mode-syntax-table)
700 (setq local-abbrev-table bibtex-mode-abbrev-table)
701 (make-local-variable 'paragraph-start)
702 (setq paragraph-start "^[ \f\n\t]*$")
703 (auto-fill-mode 1) ; nice alignments
704 (setq left-margin (+ bibtex-text-alignment 1))
706 (run-hooks 'bibtex-mode-hook))
708 (defun bibtex-move-outside-of-entry ()
709 "Make sure we are outside of a bib entry"
710 (cond ((or
711 (= (point) (point-max))
712 (= (point) (point-min))
713 (looking-at "[ \n]*@")
717 (backward-paragraph)
718 (forward-paragraph)))
719 (re-search-forward "[ \t\n]*" (point-max) t))
721 (defun ispell-abstract ()
722 (interactive)
723 (beginning-of-bibtex-entry)
724 (re-search-forward "^[ \t]*[OPT]*abstract[ \t]*=")
725 (ispell-region (point)
726 (save-excursion (forward-sexp) (point))))
728 (defun beginning-of-bibtex-entry ()
729 (interactive)
730 (re-search-backward "^@" nil 'move))
732 (defun skip-whitespace-and-comments ()
733 ;; It might be a good idea to have forward-sexp with argument 0 do what
734 ;; this function tries to do, namely skip whitespace and comments.
735 ;; Maybe a better name for this would be skip-to-next-sexp.
736 ;; alternative implementation:
737 ;; (let ((parse-sexp-ignore-comments t))
738 ;; (forward-sexp 1)
739 ;; (forward-sexp -1))
740 ;; but I've had problems with this not getting the parse of comments
741 ;; right going backward if they contain unbalanced expressions or string
742 ;; quotes. [alarson:19920217.1021CST]
743 (let ((md (match-data)))
744 (unwind-protect
745 (while (cond ((looking-at "\\s>+\\|\\s +")
746 ;; was whitespace
747 ;; NOTE: also checked end-comment. In latex and
748 ;; lisp modes, newline is an end comment, but it
749 ;; should also be a whitespace char.
750 (goto-char (match-end 0)))
751 ;; If looking at beginning of comment, skip to end.
752 ((looking-at "\\s<")
753 (re-search-forward "\\s>"))))
754 (store-match-data md))))
756 ;;; [alarson:19920214.1007CST]
757 (defun end-of-bibtex-entry ()
758 "If inside an entry, move to the end of it, otherwise move to the end
759 of the next entry."
760 (interactive)
761 ;; if point was previously at the end of an entry, this puts us
762 ;; inside the next entry, otherwise we remain in the current one.
763 (progn
764 (skip-whitespace-and-comments)
765 ;;; (skip-chars-forward " \t\n")
766 (end-of-line))
767 (beginning-of-bibtex-entry)
768 (let ((parse-sexp-ignore-comments t))
769 (forward-sexp) ; skip entry type
770 (forward-sexp) ; skip entry body
772 ;(defun end-of-bibtex-entry ()
773 ; (interactive)
774 ; (re-search-forward "}$" nil 'move))
776 (defun ispell-bibtex-entry ()
777 (interactive)
778 (ispell-region (progn (beginning-of-bibtex-entry) (point))
779 (progn (end-of-bibtex-entry) (point))))
781 (defun narrow-to-bibtex-entry ()
782 (interactive)
783 (save-excursion
784 (narrow-to-region (progn (beginning-of-bibtex-entry) (point))
785 (progn (end-of-bibtex-entry) (point)))))
788 (defun beginning-of-first-bibtex-entry ()
789 (goto-char (point-min))
790 (cond
791 ((re-search-forward "^@" nil 'move)
792 (beginning-of-line))
793 ((and (bobp) (eobp))
794 nil)
796 (message "Warning: No bibtex entries found!"))))
798 (defun hide-bibtex-entry-bodies (&optional arg)
799 "Hide all lines between first and last bibtex entries not beginning with @.
800 With argument, show all text."
801 (interactive "P")
802 (save-excursion
803 (beginning-of-first-bibtex-entry)
804 ;; subst-char-in-region modifies the buffer, despite what the
805 ;; documentation says...
806 (let ((modifiedp (buffer-modified-p))
807 (buffer-read-only nil))
808 (if arg
809 (subst-char-in-region (point) (point-max) ?\r ?\n t)
810 (while (save-excursion (re-search-forward "\n[^@]" (point-max) t))
811 ;; (save-excursion (replace-regexp "\n\\([^@]\\)" "\r\\1"))
812 (save-excursion
813 (while (re-search-forward "\n\\([^@]\\)" nil t)
814 (replace-match "\r\\1" nil nil)))))
815 (setq selective-display (not arg))
816 (set-buffer-modified-p modifiedp))))
818 (defvar bibtex-sort-ignore-string-entries nil
819 "*If true, bibtex @STRING entries are ignored when determining ordering
820 of the buffer (e.g. sorting, locating alphabetical position for new entries,
821 etc.)")
823 (defun sort-bibtex-entries ()
824 "Sort bibtex entries alphabetically by key.
825 Text before the first bibtex entry, and following the last is not affected.
826 If bibtex-sort-ignore-string-entries is true, @string entries will be ignored.
828 Bugs:
829 1. Text between the closing brace ending one bibtex entry, and the @ starting
830 the next, is considered part of the PRECEDING entry. Perhaps it should be
831 part of the following entry."
832 (interactive)
833 (save-restriction
834 (beginning-of-first-bibtex-entry)
835 (narrow-to-region (point)
836 (save-excursion
837 (goto-char (point-max))
838 (beginning-of-bibtex-entry)
839 (end-of-bibtex-entry)
840 (point)))
841 (sort-subr nil ; reversep
842 ;; beginning of record function
843 'forward-line
844 ;; end of record function
845 (function (lambda () (and (re-search-forward "}\\s-*\n[\n \t]*@" nil 'move)
846 (forward-char -2))))
847 ;; start of key function
848 (if bibtex-sort-ignore-string-entries
849 (function (lambda ()
850 (while (and (re-search-forward "^\\s-*\\([@a-zA-Z]*\\)\\s-*{\\s-*")
851 (string-equalp "@string"
852 (buffer-substring (match-beginning 1)
853 (match-end 1)))))
854 nil))
855 (function (lambda () (re-search-forward "{\\s-*") nil)))
856 ;; end of key function
857 (function (lambda () (search-forward ",")))
860 (defun map-bibtex-entries (fun)
861 "Call FUN for each bibtex entry starting with the current, to the end of the file.
862 FUN is called with one argument, the key of the entry, and with point inside the entry.
863 If bibtex-sort-ignore-string-entries is true, FUN will not be called for @string entries."
864 (beginning-of-bibtex-entry)
865 (while (re-search-forward "^@[^{]*{[ \t]*\\([^, ]*\\)" nil t)
866 (if (and bibtex-sort-ignore-string-entries
867 (string-equalp "@string{"
868 (buffer-substring (match-beginning 0)
869 (match-beginning 1))))
870 nil ; ignore the @string entry.
871 (funcall fun (buffer-substring (match-beginning 1) (match-end 1))))))
873 (defun find-bibtex-entry-location (entry-name)
874 "Searches from beginning of current buffer looking for place to put the
875 bibtex entry named ENTRY-NAME. Buffer is assumed to be in sorted order,
876 without duplicates (see \\[sort-bibtex-entries]), if it is not, an error will
877 be signalled."
878 (interactive "sBibtex entry key: ")
879 (let ((previous nil)
880 point)
881 (beginning-of-first-bibtex-entry)
882 (or (catch 'done
883 (map-bibtex-entries (function (lambda (current)
884 (cond
885 ((string-equal entry-name current)
886 (error "Entry duplicates existing!"))
887 ((or (null previous)
888 (string< previous current))
889 (setq previous current
890 point (point))
891 (if (string< entry-name current)
892 (progn
893 (beginning-of-bibtex-entry)
894 ;; Many schemes append strings to
895 ;; existing entries to resolve them,
896 ;; so initial substring matches may
897 ;; indicate a duplicate entry.
898 (let ((idx (string-match (regexp-quote entry-name) current)))
899 (if (and (integerp idx)
900 (zerop idx))
901 (progn
902 (message "Warning: Entry %s may be a duplicate of %s!"
903 entry-name current)
904 (ding t))))
905 (throw 'done t))))
906 ((string-equal previous current)
907 (error "Duplicate here with previous!"))
908 (t (error "Entries out of order here!")))))))
909 (end-of-bibtex-entry))))
911 (defun validate-bibtex-buffer ()
912 "Find some typical errors in bibtex files.
913 1. At signs (@) not as first char of a line.
914 2. Double quotes (\") inside strings.
915 3. Closing braces (}) not the last character of a line."
916 (interactive)
917 (let ((point (point)))
918 (while (re-search-forward ".@" nil t)
919 (let* ((foo (parse-partial-sexp (save-excursion (beginning-of-bibtex-entry)
920 (point))
921 (point)))
922 (in-a-string (nth 3 foo)))
923 (if (not in-a-string)
924 (error "At sign (@) out of place!"))))
925 (goto-char point)
926 (while (search-forward "\"" nil t)
927 (or (looking-at "[,}][ \t]*$")
928 (char-equal (preceding-char) ?\")
929 ;; some versions put closing brace on separate line.
930 (looking-at "[ \t]*\n}")
931 (save-excursion
932 (save-restriction
933 (narrow-to-region (point)
934 (progn (beginning-of-line) (point)))
935 (looking-at "^[ \t]*[a-zA-Z]+[ \t]*=[ \t]*\"$")))
936 (error "Quote out of place, or missing \",\" or \"}\"!")))
937 (goto-char point)
938 ;; This is only approximate, should actually search for close braces,
939 ;; then see if they are inside a string, or at the end of a line.
940 ;; This just gets the typical case of whitespace after a closing brace.
941 (while (search-forward "}[ \t]+$" nil t)
942 (error "Brace not last char of line!"))
943 (goto-char point)
944 (message "Bibtex buffer appears o.k.")))
946 (defun find-bibtex-duplicates ()
947 "Searches forward in current buffer looking for duplicate bibtex entries.
948 Buffer is assumed to be sorted, see \\[sort-bibtex-entries]"
949 (interactive)
950 (let ((point (point)))
951 ;; errors if things are not right...
952 (find-bibtex-entry-location (make-string 10 255))
953 (goto-char point)
954 (message "No duplicates found!")))
957 ;;; assoc doesn't ignore case, so we need an assoc that does...
958 (defun assoc-string-equalp (thing alist)
959 (or (assoc thing alist)
960 (while (and alist
961 (not (string-equalp thing (car (car alist)))))
962 (setq alist (cdr alist)))
963 (car alist)))
965 (defvar bibtex-maintain-sorted-entries nil
966 "*If true, bibtex-mode will attempt to maintain all bibtex entries in
967 sorted order.
969 Note that this is more a property of a file than a personal preference and
970 as such should normally be set via a file local variable entry.")
972 (defun bibtex-entry (entry-type &optional required optional)
973 (interactive (let* ((completion-ignore-case t)
974 (e-t (completing-read "Entry Type: " bibtex-entry-field-alist
975 nil t)))
976 (list e-t)))
977 (if (and (null required) (null optional))
978 (let* ((e (assoc-string-equalp entry-type bibtex-entry-field-alist))
979 (r-n-o (elt e 1))
980 (c-ref (elt e 2)))
981 (if (null e)
982 (error "Bibtex entry type %s not defined!"))
983 (if (and bibtex-include-OPTcrossref c-ref)
984 (setq required (elt c-ref 0)
985 optional (elt c-ref 1))
986 (setq required (elt r-n-o 0)
987 optional (elt r-n-o 1)))))
988 (let ((key (if bibtex-maintain-sorted-entries
989 (read-string (format "%s key: " entry-type)))))
990 (if key
991 (find-bibtex-entry-location key))
992 (bibtex-move-outside-of-entry)
993 (insert "@" entry-type "{")
994 (if key
995 (insert key))
996 (save-excursion
997 (mapcar 'bibtex-make-field required)
998 (if bibtex-include-OPTcrossref
999 (bibtex-make-optional-field "crossref"))
1000 (if bibtex-include-OPTkey
1001 (bibtex-make-optional-field "key"))
1002 (mapcar 'bibtex-make-optional-field optional)
1003 (mapcar 'bibtex-make-optional-field
1004 bibtex-mode-user-optional-fields)
1005 (if bibtex-include-OPTannote
1006 (bibtex-make-optional-field "annote"))
1007 (insert "\n}\n\n"))
1008 (if key
1009 (bibtex-next-field t))
1010 (run-hooks 'bibtex-add-entry-hook)))
1012 ;; (defun bibtex-entry (entry-type required optional)
1013 ;; (bibtex-move-outside-of-entry)
1014 ;; (insert (concat "@" entry-type "{,\n\n}\n\n"))
1015 ;; (previous-line 3)
1016 ;; (insert (mapconcat 'bibtex-make-entry required ",\n"))
1017 ;; (if required
1018 ;; (if optional
1019 ;; (insert ",\n")))
1020 ;; (insert (mapconcat 'bibtex-make-OPT-entry optional ",\n"))
1021 ;; (if bibtex-mode-user-optional-fields ;MON...
1022 ;; (progn
1023 ;; (if optional
1024 ;; (insert ",\n"))
1025 ;; (insert (mapconcat 'bibtex-make-OPT-entry
1026 ;; bibtex-mode-user-optional-fields
1027 ;; ",\n")))) ;MON
1028 ;; (up-list -1)
1029 ;; (forward-char 1))
1032 (defun bibtex-make-field (e-t)
1033 (interactive "sBibTeX entry type: ")
1034 (let ((name (if (consp e-t) (car e-t) e-t))
1035 (value (if (consp e-t) (cdr e-t) "")))
1036 (insert ",\n")
1037 (indent-to-column bibtex-name-alignment)
1038 (insert name " = ")
1039 (indent-to-column bibtex-text-alignment)
1040 ;; lucid emacs prin1-to-string breaks the undo chain. When they fix
1041 ;; that, the hack can be removed. [alarson:19930316.0805CST]
1042 ; (insert (prin1-to-string value))
1043 ;; begin hack
1044 (insert (format (if (stringp value) "\"%s\"" "%s")
1045 value))
1046 ;; end hack
1047 nil))
1049 (defun bibtex-make-optional-field (e-t)
1050 (interactive "sOptional BibTeX entry type: ")
1051 (if (consp e-t)
1052 (setq e-t (cons (concat "OPT" (car e-t)) (cdr e-t)))
1053 (setq e-t (concat "OPT" e-t)))
1054 (bibtex-make-field e-t))
1056 ;; What to do about crossref? if present, journal and year are
1057 ;; both optional. Due to this, i move all of them into optional. -- MON
1059 (defun bibtex-Article ()
1060 (interactive)
1061 (bibtex-entry "Article"))
1063 (defun bibtex-Book ()
1064 (interactive)
1065 (bibtex-entry "Book"))
1067 (defun bibtex-Booklet ()
1068 (interactive)
1069 (bibtex-entry "Booklet"))
1071 ;(defun bibtex-DEAthesis ()
1072 ; (interactive)
1073 ; (bibtex-entry "DEAthesis"))
1075 (defun bibtex-InBook ()
1076 (interactive)
1077 (bibtex-entry "InBook"))
1079 (defun bibtex-InCollection ()
1080 (interactive)
1081 (bibtex-entry "InCollection"))
1083 (defun bibtex-InProceedings ()
1084 (interactive)
1085 (bibtex-entry "InProceedings"))
1087 (defun bibtex-Manual ()
1088 (interactive)
1089 (bibtex-entry "Manual"))
1091 (defun bibtex-MastersThesis ()
1092 (interactive)
1093 (bibtex-entry "MastersThesis"))
1095 (defun bibtex-Misc ()
1096 (interactive)
1097 (bibtex-entry "Misc"))
1099 (defun bibtex-PhdThesis ()
1100 (interactive)
1101 (bibtex-entry "PhdThesis"))
1103 (defun bibtex-Proceedings ()
1104 (interactive)
1105 (bibtex-entry "Proceedings"))
1107 (defun bibtex-TechReport ()
1108 (interactive)
1109 (bibtex-entry "TechReport"))
1111 (defun bibtex-Unpublished ()
1112 (interactive)
1113 (bibtex-entry "Unpublished"))
1115 (defun bibtex-string ()
1116 (interactive)
1117 (bibtex-move-outside-of-entry)
1118 (insert "@string{ = \"\"}\n")
1119 (previous-line 1)
1120 (forward-char 8))
1122 (defun bibtex-preamble ()
1123 (interactive)
1124 (bibtex-move-outside-of-entry)
1125 (insert "@Preamble{}\n")
1126 (previous-line 1)
1127 (forward-char 10))
1129 (defun bibtex-next-field (arg)
1130 "Finds end of text of next BibTeX field; with arg, to its beginning"
1131 (interactive "P")
1132 (bibtex-inside-field)
1133 (let ((start (point)))
1134 (condition-case ()
1135 (progn
1136 (bibtex-enclosing-field)
1137 (goto-char (match-end 0))
1138 (forward-char 2))
1139 (error
1140 (goto-char start)
1141 (end-of-line)
1142 (forward-char 1))))
1143 (bibtex-find-text arg))
1145 ;; (defun bibtex-next-field ()
1146 ;; "Finds end of text of next field."
1147 ;; (interactive)
1148 ;; (condition-case ()
1149 ;; (progn
1150 ;; (bibtex-inside-field)
1151 ;; (re-search-forward ",[ \t\n]*" (point-max) 1)
1152 ;; (bibtex-enclosing-field)
1153 ;; (bibtex-inside-field))
1154 ;; (error nil)))
1156 (defun bibtex-find-text (arg)
1157 "Go to end of text of current field; with arg, go to beginning."
1158 (interactive "P")
1159 (bibtex-inside-field)
1160 (bibtex-enclosing-field)
1161 (if arg
1162 (progn
1163 (goto-char (match-beginning bibtex-text-in-field))
1164 (if (looking-at "\"")
1165 (forward-char 1)))
1166 (goto-char (match-end bibtex-text-in-field))
1167 (if (= (preceding-char) ?\")
1168 (forward-char -1))))
1170 ;; (defun bibtex-find-text ()
1171 ;; "Go to end of text of current field."
1172 ;; (interactive)
1173 ;; (condition-case ()
1174 ;; (progn
1175 ;; (bibtex-inside-field)
1176 ;; (bibtex-enclosing-field)
1177 ;; (goto-char (match-end bibtex-text-in-field))
1178 ;; (bibtex-inside-field))
1179 ;; (error nil)))
1181 (defun bibtex-remove-OPT ()
1182 "Removes the 'OPT' starting optional arguments and goes to end of text"
1183 (interactive)
1184 (bibtex-inside-field)
1185 (bibtex-enclosing-field)
1186 (save-excursion
1187 (goto-char (match-beginning bibtex-name-in-field))
1188 (if (looking-at "OPT")
1189 ;; sct@dcs.edinburgh.ac.uk
1190 (progn
1191 (delete-char (length "OPT"))
1192 (search-forward "=")
1193 (delete-horizontal-space)
1194 (indent-to-column bibtex-text-alignment))))
1195 (bibtex-inside-field))
1197 (defun bibtex-inside-field ()
1198 "Try to avoid point being at end of a bibtex field."
1199 (interactive)
1200 (end-of-line)
1201 (skip-chars-backward " \t") ;MON - maybe delete these chars?
1202 (cond ((= (preceding-char) ?,)
1203 (forward-char -2))) ; -1 --> -2 sct@dcs.edinburgh.ac.uk
1204 (cond ((= (preceding-char) ?\")
1205 (forward-char -1)))) ;MON - only go back if quote
1207 (defun bibtex-remove-double-quotes ()
1208 "Removes \"\" around string."
1209 (interactive)
1210 (save-excursion
1211 (bibtex-inside-field)
1212 (bibtex-enclosing-field)
1213 (let ((start (match-beginning bibtex-text-in-field))
1214 (stop (match-end bibtex-text-in-field)))
1215 (goto-char stop)
1216 (forward-char -1)
1217 (if (looking-at "\"")
1218 (delete-char 1))
1219 (goto-char start)
1220 (if (looking-at "\"")
1221 (delete-char 1)))))
1223 (defun bibtex-kill-optional-field ()
1224 "Kill the entire enclosing optional BibTeX field"
1225 (interactive)
1226 (bibtex-inside-field)
1227 (bibtex-enclosing-field)
1228 (goto-char (match-beginning bibtex-name-in-field))
1229 (let ((the-end (match-end 0))
1230 (the-beginning (match-beginning 0)))
1231 (if (looking-at "OPT")
1232 (progn
1233 (goto-char the-end)
1234 (skip-chars-forward " \t\n,")
1235 (kill-region the-beginning the-end))
1236 (error "Mandatory fields can't be killed"))))
1238 (defun bibtex-empty-field ()
1239 "Delete the text part of the current field, replace with empty text"
1240 (interactive)
1241 (bibtex-inside-field)
1242 (bibtex-enclosing-field)
1243 (goto-char (match-beginning bibtex-text-in-field))
1244 (kill-region (point) (match-end bibtex-text-in-field))
1245 (insert "\"\"")
1246 (bibtex-find-text t))
1249 (defun bibtex-pop-previous (arg)
1250 "Replace text of current field with the text of similar field in previous entry.
1251 With arg, go up ARG entries. Repeated, goes up so many times. May be
1252 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
1253 (interactive "p")
1254 (bibtex-inside-field)
1255 (save-excursion
1256 ; parse current field
1257 (bibtex-enclosing-field)
1258 (let ((start-old-text (match-beginning bibtex-text-in-field))
1259 (stop-old-text (match-end bibtex-text-in-field))
1260 (start-name (match-beginning bibtex-name-in-field))
1261 (stop-name (match-end bibtex-name-in-field))
1262 (new-text))
1263 (goto-char start-name)
1264 ; construct regexp for previous field with same name as this one
1265 (let ((matching-entry
1266 (bibtex-cfield
1267 (buffer-substring (if (looking-at "OPT")
1268 (+ (point) (length "OPT"))
1269 (point))
1270 stop-name)
1271 bibtex-field-text)))
1273 ; if executed several times in a row, start each search where the
1274 ; last one finished
1275 (cond ((or (eq last-command 'bibtex-pop-previous)
1276 (eq last-command 'bibtex-pop-next))
1280 (bibtex-enclosing-reference)
1281 (setq bibtex-pop-previous-search-point (match-beginning 0))
1282 (setq bibtex-pop-next-search-point (match-end 0))))
1283 (goto-char bibtex-pop-previous-search-point)
1285 ; Now search for arg'th previous similar field
1286 (cond
1287 ((re-search-backward matching-entry (point-min) t arg)
1288 (setq new-text
1289 (buffer-substring (match-beginning bibtex-text-in-cfield)
1290 (match-end bibtex-text-in-cfield)))
1291 ; Found a matching field. Remember boundaries.
1292 (setq bibtex-pop-next-search-point (match-end 0))
1293 (setq bibtex-pop-previous-search-point (match-beginning 0))
1294 (bibtex-flash-head)
1295 ; Go back to where we started, delete old text, and pop new.
1296 (goto-char stop-old-text)
1297 (delete-region start-old-text stop-old-text)
1298 (insert new-text))
1299 (t ; search failed
1300 (error "No previous matching BibTeX field."))))))
1301 (setq this-command 'bibtex-pop-previous))
1303 (defun bibtex-pop-next (arg)
1304 "Replace text of current field with the text of similar field in next entry.
1305 With arg, go up ARG entries. Repeated, goes up so many times. May be
1306 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
1307 (interactive "p")
1308 (bibtex-inside-field)
1309 (save-excursion
1310 ; parse current field
1311 (bibtex-enclosing-field)
1312 (let ((start-old-text (match-beginning bibtex-text-in-field))
1313 (stop-old-text (match-end bibtex-text-in-field))
1314 (start-name (match-beginning bibtex-name-in-field))
1315 (stop-name (match-end bibtex-name-in-field))
1316 (new-text))
1317 (goto-char start-name)
1318 ; construct regexp for next field with same name as this one,
1319 ; ignoring possible OPT's
1320 (let ((matching-entry
1321 (bibtex-cfield
1322 (buffer-substring (if (looking-at "OPT")
1323 (+ (point) (length "OPT"))
1324 (point))
1325 stop-name)
1326 bibtex-field-text)))
1328 ; if executed several times in a row, start each search where the
1329 ; last one finished
1330 (cond ((or (eq last-command 'bibtex-pop-next)
1331 (eq last-command 'bibtex-pop-previous))
1335 (bibtex-enclosing-reference)
1336 (setq bibtex-pop-previous-search-point (match-beginning 0))
1337 (setq bibtex-pop-next-search-point (match-end 0))))
1338 (goto-char bibtex-pop-next-search-point)
1340 ; Now search for arg'th next similar field
1341 (cond
1342 ((re-search-forward matching-entry (point-max) t arg)
1343 (setq new-text
1344 (buffer-substring (match-beginning bibtex-text-in-cfield)
1345 (match-end bibtex-text-in-cfield)))
1346 ; Found a matching field. Remember boundaries.
1347 (setq bibtex-pop-next-search-point (match-end 0))
1348 (setq bibtex-pop-previous-search-point (match-beginning 0))
1349 (bibtex-flash-head)
1350 ; Go back to where we started, delete old text, and pop new.
1351 (goto-char stop-old-text)
1352 (delete-region start-old-text stop-old-text)
1353 (insert new-text))
1354 (t ; search failed
1355 (error "No next matching BibTeX field."))))))
1356 (setq this-command 'bibtex-pop-next))
1358 (defun bibtex-flash-head ()
1359 "Flash at BibTeX reference head before point, if exists. (Moves point)."
1360 (let ((flash))
1361 (cond ((re-search-backward bibtex-reference-head (point-min) t)
1362 (goto-char (match-beginning bibtex-type-in-head))
1363 (setq flash (match-end bibtex-key-in-reference)))
1365 (end-of-line)
1366 (skip-chars-backward " \t")
1367 (setq flash (point))
1368 (beginning-of-line)
1369 (skip-chars-forward " \t")))
1370 (if (pos-visible-in-window-p (point))
1371 (sit-for 1)
1372 (message "From: %s"
1373 (buffer-substring (point) flash)))))
1377 (defun bibtex-enclosing-field ()
1378 "Search for BibTeX field enclosing point.
1379 Point moves to end of field; also, use match-beginning and match-end
1380 to parse the field."
1381 ;; sct@dcs.edinburgh.ac.uk
1382 (let ((old-point (point)))
1383 (condition-case errname
1384 (bibtex-enclosing-regexp bibtex-field)
1385 (search-failed
1386 (goto-char old-point)
1387 (error "Can't find enclosing BibTeX field.")))))
1389 (defun bibtex-enclosing-reference ()
1390 "Search for BibTeX reference enclosing point.
1391 Point moves to end of reference; also, use match-beginning and match-end
1392 to parse the reference."
1393 ;; sct@dcs.edinburgh.ac.uk
1394 (let ((old-point (point)))
1395 (condition-case errname
1396 (bibtex-enclosing-regexp bibtex-reference)
1397 (search-failed
1398 (goto-char old-point)
1399 (error "Can't find enclosing BibTeX reference.")))))
1401 (defun bibtex-enclosing-regexp (regexp)
1402 "Search for REGEXP enclosing point.
1403 Point moves to end of REGEXP. See also match-beginning and match-end.
1404 If an enclosing REGEXP is not found, signals search-failed; point is left in
1405 an undefined location.
1407 [Doesn't something like this exist already?]"
1409 (interactive "sRegexp: ")
1410 ; compute reasonable limits for the loop
1411 (let* ((initial (point))
1412 (right (if (re-search-forward regexp (point-max) t)
1413 (match-end 0)
1414 (point-max)))
1415 (left
1416 (progn
1417 (goto-char initial)
1418 (if (re-search-backward regexp (point-min) t)
1419 (match-beginning 0)
1420 (point-min)))))
1421 ; within the prescribed limits, loop until a match is found
1422 (goto-char left)
1423 (re-search-forward regexp right nil 1)
1424 (if (> (match-beginning 0) initial)
1425 (signal 'search-failed (list regexp)))
1426 (while (<= (match-end 0) initial)
1427 (re-search-forward regexp right nil 1)
1428 (if (> (match-beginning 0) initial)
1429 (signal 'search-failed (list regexp))))
1432 (defun bibtex-clean-entry ()
1433 "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."
1434 (interactive)
1435 (beginning-of-bibtex-entry)
1436 (let ((start (point)))
1437 (save-restriction
1438 (narrow-to-region start (save-excursion (end-of-bibtex-entry) (point)))
1439 (while (re-search-forward bibtex-field (point-max) t 1)
1440 (let ((begin-field (match-beginning 0))
1441 (end-field (match-end 0))
1442 (begin-name (match-beginning bibtex-name-in-field))
1443 (end-name (match-end bibtex-name-in-field))
1444 (begin-text (match-beginning bibtex-text-in-field))
1445 (end-text (match-end bibtex-text-in-field))
1447 (goto-char begin-name)
1448 (cond ((and
1449 (looking-at "OPT")
1450 bibtex-clean-entry-zap-empty-opts)
1451 (goto-char begin-text)
1452 (if (looking-at "\"\"") ; empty: delete whole field
1453 (delete-region begin-field end-field)
1454 ; otherwise: not empty, delete "OPT"
1455 (goto-char begin-name)
1456 (delete-char (length "OPT"))
1457 (progn
1458 ;; fixup alignment. [alarson:19920309.2047CST]
1459 (search-forward "=")
1460 (delete-horizontal-space)
1461 (indent-to-column bibtex-text-alignment))
1462 (goto-char begin-field) ; and loop to go through next test
1465 (goto-char begin-text)
1466 (cond ((looking-at "\"[0-9]+\"") ; if numerical,
1467 (goto-char end-text)
1468 (delete-char -1) ; delete enclosing double-quotes
1469 (goto-char begin-text)
1470 (delete-char 1)
1471 (goto-char end-field) ; go to end for next search
1472 (forward-char -2) ; to compensate for the 2 quotes deleted
1474 ((looking-at "\"\"") ; if empty quotes, complain
1475 (forward-char 1)
1476 (if (not (or (equal (buffer-substring
1477 begin-name
1478 (+ begin-name 3))
1479 "OPT")
1480 (equal (buffer-substring
1481 begin-name
1482 (+ begin-name 3))
1483 "opt")))
1484 (error "Mandatory field ``%s'' is empty"
1485 (buffer-substring begin-name end-name))))
1487 (goto-char end-field))))))))
1488 (goto-char start)
1489 (end-of-bibtex-entry)
1490 ;; sct@dcs.edinburgh.ac.uk
1491 (save-excursion
1492 (previous-line 1)
1493 (end-of-line)
1494 (if (eq (preceding-char) ?,)
1495 (backward-delete-char 1)))
1496 (skip-whitespace-and-comments)))
1499 ;;; Menus for bibtex mode
1501 (define-key bibtex-mode-map [menu-bar entry-types]
1502 (cons "Entry Types" (make-sparse-keymap "Entry Types")))
1504 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InProceedings]
1505 '(" article in conference Proceedings " . bibtex-InProceedings))
1506 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Article]
1507 '(" Article in journal " . bibtex-Article))
1508 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Book]
1509 '(" Book " . bibtex-Book))
1510 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Booklet]
1511 '(" Booklet " . bibtex-Booklet))
1512 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InProceedings]
1513 '(" Conference " . bibtex-InProceedings))
1514 (define-key bibtex-mode-map [menu-bar entry-types bibtex-MastersThesis]
1515 '(" Master's Thesis " . bibtex-MastersThesis))
1516 ;define-key bibtex-mode-map [menu-bar entry-types bibtex-DEAthesis]
1517 ;'((" DEA Thesis " . bibtex-DEAthesis))
1518 (define-key bibtex-mode-map [menu-bar entry-types bibtex-PhdThesis]
1519 '(" Phd. Thesis " . bibtex-PhdThesis))
1520 (define-key bibtex-mode-map [menu-bar entry-types bibtex-TechReport]
1521 '(" Technical Report " . bibtex-TechReport))
1522 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Manual]
1523 '(" technical Manual " . bibtex-Manual))
1524 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Proceedings]
1525 '(" conference Proceedings " . bibtex-Proceedings))
1526 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InBook]
1527 '(" a chapter in a Book " . bibtex-InBook))
1528 (define-key bibtex-mode-map [menu-bar entry-types bibtex-InCollection]
1529 '(" an article in a Collection " . bibtex-InCollection))
1530 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Misc]
1531 '(" miscellaneous " . bibtex-Misc))
1532 (define-key bibtex-mode-map [menu-bar entry-types bibtex-Unpublished]
1533 '(" unpublished " . bibtex-Unpublished))
1534 (define-key bibtex-mode-map [menu-bar entry-types bibtex-string]
1535 '(" string " . bibtex-string))
1536 (define-key bibtex-mode-map [menu-bar entry-types bibtex-preamble]
1537 '(" preamble " . bibtex-preamble))
1539 (define-key bibtex-mode-map [menu-bar move/edit]
1540 (cons "Bibtex Edit" (make-sparse-keymap "Bibtex Edit")))
1542 (define-key bibtex-mode-map [menu-bar move/edit bibtex-next-field]
1543 '(" next field " . bibtex-next-field))
1544 (define-key bibtex-mode-map [menu-bar move/edit bibtex-find-text]
1545 '(" to end of field " . bibtex-find-text))
1546 (define-key bibtex-mode-map [menu-bar move/edit bibtex-pop-previous]
1547 '("snatch from similar preceding field" . bibtex-pop-previous))
1548 (define-key bibtex-mode-map [menu-bar move/edit bibtex-pop-next]
1549 '("snatch from similar following field" . bibtex-pop-next))
1550 (define-key bibtex-mode-map [menu-bar move/edit bibtex-remove-OPT]
1551 '(" remove OPT " . bibtex-remove-OPT))
1552 (define-key bibtex-mode-map [menu-bar move/edit bibtex-remove-double-quotes]
1553 '(" remove quotes " . bibtex-remove-double-quotes))
1554 (define-key bibtex-mode-map [menu-bar move/edit bibtex-clean-entry]
1555 '(" clean up entry " . bibtex-clean-entry))
1556 (define-key bibtex-mode-map [menu-bar move/edit find-bibtex-duplicates]
1557 '(" find duplicates " . find-bibtex-duplicates))
1558 (define-key bibtex-mode-map [menu-bar move/edit sort-bibtex-entries]
1559 '(" sort entries " . sort-bibtex-entries))
1560 (define-key bibtex-mode-map [menu-bar move/edit validate-bibtex-buffer]
1561 '(" validate entries " . validate-bibtex-buffer))
1564 ;; Please don't send anything to bug-gnu-emacs about these Sunwindows functions
1565 ;; since we aren't interested. See etc/SUN-SUPPORT for the reasons why
1566 ;; we consider this nothing but a distraction from our work.
1568 ;(defmacro eval-in-menu-window (&rest l)
1569 ; "Evaluates its argument in the window in which the mouse button was pressed."
1570 ; (list 'eval-in-window '*menu-window* l))
1572 ;(defmenu bibtex-sun-entry-menu
1573 ; ("Article In Conf. Proc." eval-in-menu-window bibtex-InProceedings)
1574 ; ("Article In Journal" eval-in-menu-window bibtex-Article)
1575 ; ("Book" eval-in-menu-window bibtex-Book)
1576 ; ("Booklet" eval-in-menu-window bibtex-Booklet)
1577 ; ("Master's Thesis" eval-in-menu-window bibtex-MastersThesis)
1578 ; ("PhD. Thesis" eval-in-menu-window bibtex-PhdThesis)
1579 ; ("Technical Report" eval-in-menu-window bibtex-TechReport)
1580 ; ("Technical Manual" eval-in-menu-window bibtex-Manual)
1581 ; ("Conference Proceedings" eval-in-menu-window bibtex-Proceedings)
1582 ; ("In A Book" eval-in-menu-window bibtex-InBook)
1583 ; ("In A Collection" eval-in-menu-window bibtex-InCollection)
1584 ; ("Miscellaneous" eval-in-menu-window bibtex-Misc)
1585 ; ("Unpublished" eval-in-menu-window bibtex-Unpublished)
1586 ; ("string" eval-in-menu-window bibtex-string)
1587 ; ("preamble" eval-in-menu-window bibtex-preamble))
1589 ;(defmenu bibtex-sun-menu
1590 ; ("BibTeX menu")
1591 ; ("add entry" . bibtex-sun-entry-menu)
1592 ; ("next field" eval-in-menu-window bibtex-next-field nil)
1593 ; ("to end of field" eval-in-menu-window bibtex-find-text nil)
1594 ; ("snatch similar preceding field" eval-in-menu-window bibtex-pop-previous 1)
1595 ; ("snatch similar following field" eval-in-menu-window bibtex-pop-next 1)
1596 ; ("remove OPT" eval-in-menu-window bibtex-remove-OPT)
1597 ; ("remove quotes" eval-in-menu-window bibtex-remove-double-quotes)
1598 ; ("clean entry" eval-in-menu-window bibtex-clean-entry)
1599 ; ("describe BibTeX mode" eval-in-menu-window describe-mode)
1600 ; ("Main Emacs menu" . emacs-menu))
1602 ;(defun bibtex-sun-menu-eval (window x y)
1603 ; "Pop-up menu of BibTeX commands."
1604 ; (sun-menu-evaluate window (1+ x) (1- y) 'bibtex-sun-menu))
1606 ;(defun bibtex-sun-environment ()
1607 ; "Set up sun menus for BibTeX mode. Call it as bibtex-mode-hook, or
1608 ;interactively"
1609 ; (interactive)
1610 ; (local-set-mouse '(text right) 'bibtex-sun-menu-eval))
1613 ;;; bibtex.el ends here