aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2003-06-02 22:30:09 +0000
committerStefan Monnier2003-06-02 22:30:09 +0000
commit7fbf4804e62c2c3337221ec1c254fc03259fb744 (patch)
tree2d6a8cf17d43131eecc2d5b732f6fc2b2ed5ba50
parente2292b24dbd9f23404b2a524599fbc5a44c9f9f5 (diff)
downloademacs-7fbf4804e62c2c3337221ec1c254fc03259fb744.tar.gz
emacs-7fbf4804e62c2c3337221ec1c254fc03259fb744.zip
Long overdue merge.
Don't require `compile' since it seems unnecessary. For all internal variables and functions the docstring comments have been converted into proper docstrings (bibtex-maintainer-address, bibtex-maintainer-salutation) (bibtex-version): Remove support for bug reporting. (bibtex-field-delimiters, bibtex-entry-delimiters) (bibtex-sort-ignore-string-entries, bibtex-maintain-sorted-entries) Replace make-variable-buffer-local by make-local-variable for (bibtex-entry-format): New tag `required-fields'. (bibtex-maintain-sorted-entries): New var. (bibtex-sort-entry-class, bibtex-sort-entry-class-alist): New vars. (bibtex-predefined-month-strings, bibtex-predefined-strings): Make into alists with pairs (abbreviation expansion). (bibtex-autokey-titleword-change-strings) (bibtex-autokey-transcriptions): Make into alists. Add new replacement pairs. (bibtex-autokey-use-crossref): New var. Replace bibtex-autokey-year-use-crossref-entry because updated code for autokey generation handles crossrefs independently of particular fields. (bibtex-reference-key, bibtex-mode-syntax-table): Remove = because it can't be part of a string's key. (bibtex-complete-key-cleanup): New var. (bibtex-complete): Merge bibtex-complete-string and bibtex-complete-key into it (and bind to M-tab). (bibtex-valid-entry-re, bibtex-any-valid-entry-re) (bibtex-valid-entry-whitespace-re, bibtex-empty-field-re) (bibtex-quoted-string-re): New vars. (bibtex-field-name-for-parsing): Don't make-variable-buffer-local. (zmacs-regions): Declare to quieten the byte-compiler. (bibtex-comment-start): Don't include the space. (bibtex-font-lock-syntactic-keywords): New var. (bibtex-font-lock-keywords): Remove the entry for @Comment. (bibtex-parse-field-string): Merge the functionality of bibtex-parse-field-string-braced, bibtex-parse-quoted-string and bibtex-parse-field-string-quoted. (bibtex-search-forward-field-string): Remove. (bibtex-parse-association): Use when. (bibtex-parse-field-name): Use when. (bibtex-parse-field-text): Use when and cond. (bibtex-parse-field): Use let. (bibtex-search-forward-field, bibtex-search-backward-field): Make bound optional, use let, setq, and cddr. (bibtex-start-of-field, bibtex-start-of-name-in-field) (bibtex-end-of-name-in-field): Use nth. (bibtex-name-in-field, bibtex-text-in-field-bounds) (bibtex-text-in-field, bibtex-type-in-head, bibtex-key-in-head) (bibtex-text-in-string): New functions. (bibtex-reference-key-in-string): New fun. Merge of bibtex-start-of-reference-key-in-string and bibtex-end-of-reference-key-in-string. (bibtex-parse-string-prefix): Use let and when. (bibtex-parse-string-postfix): Use when. (bibtex-search-forward-string, bibtex-search-backward-string): Use save-excursion and setq. (bibtex-member-of-regexp): Use let. (bibtex-assoc-of-regexp): Use caar und let. (bibtex-skip-to-valid-entry): Return buffer position. (bibtex-map-entries): Use save-excursion. (bibtex-progress-message): Simplify. (bibtex-search-entry): Use skip-chars-forward, when, save-match-data. (bibtex-move-outside-of-entry): Handle the case that point is before first entry. (bibtex-enclosing-field): Use save-excursion, when. (bibtex-format-field-delimiters): Merge into bibtex-format-entry. (bibtex-enclosing-entry-maybe-empty-head): Simplify. (bibtex-format-entry): Simplify. Handle new tag required-fields of bibtex-entry-format. (bibtex-autokey-abbrev): Accept negative values of len. (bibtex-autokey-get-field, bibtex-autokey-demangle-title): New funs. (bibtex-autokey-get-namefield, bibtex-autokey-get-namelist) (bibtex-autokey-get-yearfield-digits, bibtex-autokey-get-yearfield) (bibtex-autokey-get-titlestring): Remove. (bibtex-autokey-get-names): Simplify. (bibtex-autokey-get-titles): Rename to bibtex-autokey-get-title. (bibtex-autokey-demangle-name): Simplify, avoid error messages. (bibtex-generate-autokey): Simplify. (bibtex-parse-keys): Simplify, use push. (bibtex-parse-strings): New fun similar to bibtex-parse-keys. (bibtex-string-files-init): New fun. (bibtex-parse-buffers-stealthily): Use bibtex-parse-keys, bibtex-string-files-init and bibtex-parse-strings. (bibtex-complete): Rename to bibtex-complete-internal, use push, bibtex-reference-key-in-string, no sorting. (bibtex-complete-string-cleanup): New fun, displays expansion of completed strings. (bibtex-choose-completion-string): New fun. Required for choose-completion-string-functions. (bibtex-do-auto-fill): Remove. Set fill-prefix in bibtex-mode. (bibtex-pop): Simplify. (bibtex-mode): Move setting of bibtex-string to bibtex-parse-strings. Set choose-completion-string-functions. (bibtex-print-help-message,bibtex-make-field, bibtex-end-of-entry) (bibtex-count-entries): Simplify. (bibtex-entry-index, bibtex-lessp): New funs for generalized sorting scheme of indices, see bibtex-maintain-sorted-entries. (bibtex-sort-buffer): Use bibtex-lessp for sorting. (bibtex-find-crossref, bibtex-find-entry): New funs. (bibtex-find-entry-location): Rename to bibtex-prepare-new-entry, use bibtex-lessp, Simplify. (bibtex-validate): Simplify. Fixe bug of internal variable questionable-month. (bibtex-remove-OPT-or-ALT): Use when. (bibtex-remove-delimiters, bibtex-kill-field, bibtex-kill-entry) (bibtex-clean-entry, bibtex-fill-entry, bibtex-reformat): Simplify. (bibtex-convert-alien): Use deactivate-mark rather than the non-existent bibtex-mark-active variable. (bibtex-complete-string, bibtex-complete-key): Merge into new `smart' defun bibtex-complete. (bibtex-String): Update for new sorting scheme, distinguish empty and non-empty key strings.
-rw-r--r--lisp/textmodes/bibtex.el5226
1 files changed, 2431 insertions, 2795 deletions
diff --git a/lisp/textmodes/bibtex.el b/lisp/textmodes/bibtex.el
index 00f46f0004b..90d0636de09 100644
--- a/lisp/textmodes/bibtex.el
+++ b/lisp/textmodes/bibtex.el
@@ -1,12 +1,13 @@
1;;; bibtex.el --- BibTeX mode for GNU Emacs 1;;; bibtex.el --- BibTeX mode for GNU Emacs
2 2
3;; Copyright (C) 1992, 1994, 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc. 3;; Copyright (C) 1992,94,95,96,97,98,1999,2003 Free Software Foundation, Inc.
4 4
5;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de> 5;; Author: Stefan Schoef <schoef@offis.uni-oldenburg.de>
6;; Bengt Martensson <bengt@mathematik.uni-Bremen.de> 6;; Bengt Martensson <bengt@mathematik.uni-Bremen.de>
7;; Mark Shapiro <shapiro@corto.inria.fr> 7;; Mark Shapiro <shapiro@corto.inria.fr>
8;; Mike Newton <newton@gumby.cs.caltech.edu> 8;; Mike Newton <newton@gumby.cs.caltech.edu>
9;; Aaron Larson <alarson@src.honeywell.com> 9;; Aaron Larson <alarson@src.honeywell.com>
10;; Dirk Herrmann <D.Herrmann@tu-bs.de>
10;; Maintainer: Roland Winkler <roland.winkler@physik.uni-erlangen.de> 11;; Maintainer: Roland Winkler <roland.winkler@physik.uni-erlangen.de>
11;; Keywords: BibTeX, LaTeX, TeX 12;; Keywords: BibTeX, LaTeX, TeX
12 13
@@ -40,34 +41,16 @@
40 41
41;;; Code: 42;;; Code:
42 43
43(eval-when-compile
44 (require 'compile))
45
46
47;; Bug Reporting
48
49(defconst
50 bibtex-maintainer-address "Dirk Herrmann <D.Herrmann@tu-bs.de>")
51;; current maintainer
52
53(defconst
54 bibtex-maintainer-salutation "Hallo Dirk,")
55;; current maintainer
56
57(defconst
58 bibtex-version "(emacs 20.4)")
59;; current version of the bibtex.el file
60
61 44
62;; User Options: 45;; User Options:
63 46
64(defgroup bibtex nil 47(defgroup bibtex nil
65 "BibTeX mode." 48 "BibTeX mode"
66 :group 'tex 49 :group 'tex
67 :prefix "bibtex-") 50 :prefix "bibtex-")
68 51
69(defgroup bibtex-autokey nil 52(defgroup bibtex-autokey nil
70 "Generates automatically a key from the author/editor and the title field" 53 "Generate automatically a key from the author/editor and the title field"
71 :group 'bibtex 54 :group 'bibtex
72 :prefix "bibtex-autokey-") 55 :prefix "bibtex-autokey-")
73 56
@@ -77,25 +60,19 @@
77 :type 'hook) 60 :type 'hook)
78 61
79(defcustom bibtex-field-delimiters 'braces 62(defcustom bibtex-field-delimiters 'braces
80 "*Controls type of field delimiters used. 63 "*Type of field delimiters. Allowed values are `braces' or `double-quotes'."
81Set this to `braces' or `double-quotes' according to your personal
82preferences. This variable is buffer-local."
83 :group 'bibtex 64 :group 'bibtex
84 :type '(choice (const braces) 65 :type '(choice (const braces)
85 (const double-quotes))) 66 (const double-quotes)))
86(make-variable-buffer-local 'bibtex-field-delimiters)
87 67
88(defcustom bibtex-entry-delimiters 'braces 68(defcustom bibtex-entry-delimiters 'braces
89 "*Controls type of entry delimiters used. 69 "*Type of entry delimiters. Allowed values are `braces' or `parentheses'."
90Set this to `braces' or `parentheses' according to your personal
91preferences. This variable is buffer-local."
92 :group 'bibtex 70 :group 'bibtex
93 :type '(choice (const braces) 71 :type '(choice (const braces)
94 (const parentheses))) 72 (const parentheses)))
95(make-variable-buffer-local 'bibtex-entry-delimiters)
96 73
97(defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection") 74(defcustom bibtex-include-OPTcrossref '("InProceedings" "InCollection")
98 "*All entries listed here will have an OPTcrossref field." 75 "*List of entries that get an OPTcrossref field."
99 :group 'bibtex 76 :group 'bibtex
100 :type '(repeat string)) 77 :type '(repeat string))
101 78
@@ -105,9 +82,9 @@ If this is a string, it will be used as the initial field text.
105If this is a function, it will be called to generate the initial field text." 82If this is a function, it will be called to generate the initial field text."
106 :group 'bibtex 83 :group 'bibtex
107 :type '(choice (const :tag "None" nil) 84 :type '(choice (const :tag "None" nil)
108 (string :tag "Initial text") 85 (string :tag "Initial text")
109 (function :tag "Initialize Function" :value fun) 86 (function :tag "Initialize Function" :value fun)
110 (other :tag "Default" t))) 87 (other :tag "Default" t)))
111 88
112(defcustom bibtex-user-optional-fields 89(defcustom bibtex-user-optional-fields
113 '(("annote" "Personal annotation (ignored)")) 90 '(("annote" "Personal annotation (ignored)"))
@@ -116,20 +93,21 @@ Entries should be of the same form as the OPTIONAL and
116CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (see documentation 93CROSSREF-OPTIONAL lists in `bibtex-entry-field-alist' (see documentation
117of this variable for details)." 94of this variable for details)."
118 :group 'bibtex 95 :group 'bibtex
119 :type '(repeat 96 :type '(repeat (group (string :tag "Field")
120 (group (string :tag "Field") 97 (string :tag "Comment")
121 (string :tag "Comment") 98 (option (group :inline t
122 (option (group :inline t 99 :extra-offset -4
123 :extra-offset -4 100 (choice :tag "Init" :value ""
124 (choice :tag "Init" :value "" 101 string
125 string 102 function))))))
126 function)))))) 103
127 104(defcustom bibtex-entry-format
128(defcustom bibtex-entry-format '(opts-or-alts numerical-fields) 105 '(opts-or-alts required-fields numerical-fields)
129 "*Controls type of formatting performed by `bibtex-clean-entry'. 106 "*Type of formatting performed by `bibtex-clean-entry'.
130It may be t, nil, or a list of symbols out of the following: 107It may be t, nil, or a list of symbols out of the following:
131opts-or-alts Delete empty optional and alternative fields and 108opts-or-alts Delete empty optional and alternative fields and
132 remove OPT and ALT prefixes from used fields. 109 remove OPT and ALT prefixes from used fields.
110required-fields Signal an error if a required field is missing.
133numerical-fields Delete delimiters around numeral fields. 111numerical-fields Delete delimiters around numeral fields.
134page-dashes Change double dashes in page field to single dash 112page-dashes Change double dashes in page field to single dash
135 (for scribe compatibility). 113 (for scribe compatibility).
@@ -151,16 +129,17 @@ The value t means do all of the above formatting actions.
151The value nil means do no formatting at all." 129The value nil means do no formatting at all."
152 :group 'bibtex 130 :group 'bibtex
153 :type '(choice (const :tag "None" nil) 131 :type '(choice (const :tag "None" nil)
154 (const :tag "All" t) 132 (const :tag "All" t)
155 (set :menu-tag "Some" 133 (set :menu-tag "Some"
156 (const opts-or-alts) 134 (const opts-or-alts)
157 (const numerical-fields) 135 (const required-fields)
158 (const page-dashes) 136 (const numerical-fields)
159 (const inherit-booktitle) 137 (const page-dashes)
160 (const realign) 138 (const inherit-booktitle)
161 (const last-comma) 139 (const realign)
162 (const delimiters) 140 (const last-comma)
163 (const unify-case)))) 141 (const delimiters)
142 (const unify-case))))
164 143
165(defcustom bibtex-clean-entry-hook nil 144(defcustom bibtex-clean-entry-hook nil
166 "*List of functions to call when entry has been cleaned. 145 "*List of functions to call when entry has been cleaned.
@@ -169,37 +148,51 @@ narrowed to just the entry."
169 :group 'bibtex 148 :group 'bibtex
170 :type 'hook) 149 :type 'hook)
171 150
172(defcustom bibtex-sort-ignore-string-entries t
173 "*If non-nil, BibTeX @String entries are not sort-significant.
174That means they are ignored when determining ordering of the buffer
175\(e.g., sorting, locating alphabetical position for new entries, etc.).
176This variable is buffer-local."
177 :group 'bibtex
178 :type 'boolean)
179(make-variable-buffer-local 'bibtex-sort-ignore-string-entries)
180
181(defcustom bibtex-maintain-sorted-entries nil 151(defcustom bibtex-maintain-sorted-entries nil
182 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order. 152 "*If non-nil, BibTeX mode maintains all BibTeX entries in sorted order.
183Setting this variable to nil will strip off some comfort (e.g., TAB 153If it is nil this will strip off some comfort (e.g., TAB
184completion for reference keys in minibuffer, automatic detection of 154completion for reference keys in minibuffer, automatic detection of
185duplicates) from BibTeX mode. See also `bibtex-sort-ignore-string-entries'. 155duplicates) from BibTeX mode. Allowed non-nil values are:
186This variable is buffer-local." 156plain All entries are sorted alphabetically.
157crossref All entries are sorted alphabetically unless an entry has a
158 crossref field. These crossrefed entries are placed in
159 alphabetical order immediately preceding the main entry.
160entry-class The entries are divided into classes according to their
161 entry name, see `bibtex-sort-entry-class'. Within each class
162 the entries are sorted alphabetically.
163See also `bibtex-sort-ignore-string-entries'."
164 :group 'bibtex
165 :type '(choice (const nil)
166 (const plain)
167 (const crossref)
168 (const entry-class)))
169
170(defvar bibtex-sort-entry-class
171 '(("String")
172 ("Article" "InBook" "InCollection" "InProceedings")
173 ("Book" "Proceedings")
174 ("Booklet" "Manual" "MastersThesis" "PhdThesis" "TechReport" )
175 ("Misc" "Unpublished"))
176 "*List of classes of BibTeX entry names, used for sorting entries.
177Each class contains a list of entry names. If value of
178`bibtex-maintain-sorted-entries' is `entry-class' entries are ordered
179according to the classes they belong to.")
180
181(defcustom bibtex-sort-ignore-string-entries
182 (not (equal bibtex-maintain-sorted-entries 'entry-class))
183 "*If non-nil, BibTeX @String entries are not sort-significant.
184That means they are ignored when determining ordering of the buffer
185\(e.g., sorting, locating alphabetical position for new entries, etc.)."
187 :group 'bibtex 186 :group 'bibtex
188 :type 'boolean) 187 :type 'boolean)
189(make-variable-buffer-local 'bibtex-maintain-sorted-entries)
190 188
191(defcustom bibtex-field-kill-ring-max 20 189(defcustom bibtex-field-kill-ring-max 20
192 "*Max length of `bibtex-field-kill-ring' before discarding oldest elements." 190 "*Max length of `bibtex-field-kill-ring' before discarding oldest elements."
193 :group 'bibtex 191 :group 'bibtex
194 :type 'integer) 192 :type 'integer)
195 193
196(defcustom bibtex-entry-kill-ring-max 20
197 "*Max length of `bibtex-entry-kill-ring' before discarding oldest elements."
198 :group 'bibtex
199 :type 'integer)
200
201(defcustom bibtex-parse-keys-timeout 60 194(defcustom bibtex-parse-keys-timeout 60
202 "*Specifies interval for parsing buffers. 195 "*Specify interval for parsing BibTeX buffers.
203All BibTeX buffers in Emacs are parsed if Emacs has been idle 196All BibTeX buffers in Emacs are parsed if Emacs has been idle
204`bibtex-parse-keys-timeout' seconds. Only buffers which were modified 197`bibtex-parse-keys-timeout' seconds. Only buffers which were modified
205after last parsing and which are maintained in sorted order are parsed." 198after last parsing and which are maintained in sorted order are parsed."
@@ -208,202 +201,215 @@ after last parsing and which are maintained in sorted order are parsed."
208 201
209(defvar bibtex-entry-field-alist 202(defvar bibtex-entry-field-alist
210 '( 203 '(
211 ("Article" . (((("author" "Author1 [and Author2 ...] [and others]") 204 ("Article"
212 ("title" "Title of the article (BibTeX converts it to lowercase)") 205 ((("author" "Author1 [and Author2 ...] [and others]")
213 ("journal" "Name of the journal (use string, remove braces)") 206 ("title" "Title of the article (BibTeX converts it to lowercase)")
214 ("year" "Year of publication")) 207 ("journal" "Name of the journal (use string, remove braces)")
215 (("volume" "Volume of the journal") 208 ("year" "Year of publication"))
216 ("number" "Number of the journal (only allowed if entry contains volume)") 209 (("volume" "Volume of the journal")
217 ("pages" "Pages in the journal") 210 ("number" "Number of the journal (only allowed if entry contains volume)")
218 ("month" "Month of the publication as a string (remove braces)") 211 ("pages" "Pages in the journal")
219 ("note" "Remarks to be put at the end of the \\bibitem"))) 212 ("month" "Month of the publication as a string (remove braces)")
220 ((("author" "Author1 [and Author2 ...] [and others]") 213 ("note" "Remarks to be put at the end of the \\bibitem")))
221 ("title" "Title of the article (BibTeX converts it to lowercase)")) 214 ((("author" "Author1 [and Author2 ...] [and others]")
222 (("pages" "Pages in the journal") 215 ("title" "Title of the article (BibTeX converts it to lowercase)"))
223 ("journal" "Name of the journal (use string, remove braces)") 216 (("pages" "Pages in the journal")
224 ("year" "Year of publication") 217 ("journal" "Name of the journal (use string, remove braces)")
225 ("volume" "Volume of the journal") 218 ("year" "Year of publication")
226 ("number" "Number of the journal") 219 ("volume" "Volume of the journal")
227 ("month" "Month of the publication as a string (remove braces)") 220 ("number" "Number of the journal")
228 ("note" "Remarks to be put at the end of the \\bibitem"))))) 221 ("month" "Month of the publication as a string (remove braces)")
229 ("Book" . (((("author" "Author1 [and Author2 ...] [and others]" "" t) 222 ("note" "Remarks to be put at the end of the \\bibitem"))))
230 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t) 223 ("Book"
231 ("title" "Title of the book") 224 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
232 ("publisher" "Publishing company") 225 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
233 ("year" "Year of publication")) 226 ("title" "Title of the book")
234 (("volume" "Volume of the book in the series") 227 ("publisher" "Publishing company")
235 ("number" "Number of the book in a small series (overwritten by volume)") 228 ("year" "Year of publication"))
236 ("series" "Series in which the book appeared") 229 (("volume" "Volume of the book in the series")
237 ("address" "Address of the publisher") 230 ("number" "Number of the book in a small series (overwritten by volume)")
238 ("edition" "Edition of the book as a capitalized English word") 231 ("series" "Series in which the book appeared")
239 ("month" "Month of the publication as a string (remove braces)") 232 ("address" "Address of the publisher")
240 ("note" "Remarks to be put at the end of the \\bibitem"))) 233 ("edition" "Edition of the book as a capitalized English word")
241 ((("author" "Author1 [and Author2 ...] [and others]" "" t) 234 ("month" "Month of the publication as a string (remove braces)")
242 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t) 235 ("note" "Remarks to be put at the end of the \\bibitem")))
243 ("title" "Title of the book")) 236 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
244 (("publisher" "Publishing company") 237 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
245 ("year" "Year of publication") 238 ("title" "Title of the book"))
246 ("volume" "Volume of the book in the series") 239 (("publisher" "Publishing company")
247 ("number" "Number of the book in a small series (overwritten by volume)") 240 ("year" "Year of publication")
248 ("series" "Series in which the book appeared") 241 ("volume" "Volume of the book in the series")
249 ("address" "Address of the publisher") 242 ("number" "Number of the book in a small series (overwritten by volume)")
250 ("edition" "Edition of the book as a capitalized English word") 243 ("series" "Series in which the book appeared")
251 ("month" "Month of the publication as a string (remove braces)") 244 ("address" "Address of the publisher")
252 ("note" "Remarks to be put at the end of the \\bibitem"))))) 245 ("edition" "Edition of the book as a capitalized English word")
253 ("Booklet" . (((("title" "Title of the booklet (BibTeX converts it to lowercase)")) 246 ("month" "Month of the publication as a string (remove braces)")
254 (("author" "Author1 [and Author2 ...] [and others]") 247 ("note" "Remarks to be put at the end of the \\bibitem"))))
255 ("howpublished" "The way in which the booklet was published") 248 ("Booklet"
256 ("address" "Address of the publisher") 249 ((("title" "Title of the booklet (BibTeX converts it to lowercase)"))
257 ("month" "Month of the publication as a string (remove braces)") 250 (("author" "Author1 [and Author2 ...] [and others]")
258 ("year" "Year of publication") 251 ("howpublished" "The way in which the booklet was published")
259 ("note" "Remarks to be put at the end of the \\bibitem"))))) 252 ("address" "Address of the publisher")
260 ("InBook" . (((("author" "Author1 [and Author2 ...] [and others]" "" t) 253 ("month" "Month of the publication as a string (remove braces)")
261 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t) 254 ("year" "Year of publication")
262 ("title" "Title of the book") 255 ("note" "Remarks to be put at the end of the \\bibitem"))))
263 ("chapter" "Chapter in the book") 256 ("InBook"
264 ("publisher" "Publishing company") 257 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
265 ("year" "Year of publication")) 258 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
266 (("volume" "Volume of the book in the series") 259 ("title" "Title of the book")
267 ("number" "Number of the book in a small series (overwritten by volume)") 260 ("chapter" "Chapter in the book")
268 ("series" "Series in which the book appeared") 261 ("publisher" "Publishing company")
269 ("type" "Word to use instead of \"chapter\"") 262 ("year" "Year of publication"))
270 ("address" "Address of the publisher") 263 (("volume" "Volume of the book in the series")
271 ("edition" "Edition of the book as a capitalized English word") 264 ("number" "Number of the book in a small series (overwritten by volume)")
272 ("month" "Month of the publication as a string (remove braces)") 265 ("series" "Series in which the book appeared")
273 ("pages" "Pages in the book") 266 ("type" "Word to use instead of \"chapter\"")
274 ("note" "Remarks to be put at the end of the \\bibitem"))) 267 ("address" "Address of the publisher")
275 ((("author" "Author1 [and Author2 ...] [and others]" "" t) 268 ("edition" "Edition of the book as a capitalized English word")
276 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t) 269 ("month" "Month of the publication as a string (remove braces)")
277 ("title" "Title of the book") 270 ("pages" "Pages in the book")
278 ("chapter" "Chapter in the book")) 271 ("note" "Remarks to be put at the end of the \\bibitem")))
279 (("pages" "Pages in the book") 272 ((("author" "Author1 [and Author2 ...] [and others]" "" t)
280 ("publisher" "Publishing company") 273 ("editor" "Editor1 [and Editor2 ...] [and others]" "" t)
281 ("year" "Year of publication") 274 ("title" "Title of the book")
282 ("volume" "Volume of the book in the series") 275 ("chapter" "Chapter in the book"))
283 ("number" "Number of the book in a small series (overwritten by volume)") 276 (("pages" "Pages in the book")
284 ("series" "Series in which the book appeared") 277 ("publisher" "Publishing company")
285 ("type" "Word to use instead of \"chapter\"") 278 ("year" "Year of publication")
286 ("address" "Address of the publisher") 279 ("volume" "Volume of the book in the series")
287 ("edition" "Edition of the book as a capitalized English word") 280 ("number" "Number of the book in a small series (overwritten by volume)")
288 ("month" "Month of the publication as a string (remove braces)") 281 ("series" "Series in which the book appeared")
289 ("note" "Remarks to be put at the end of the \\bibitem"))))) 282 ("type" "Word to use instead of \"chapter\"")
290 ("InCollection" . (((("author" "Author1 [and Author2 ...] [and others]") 283 ("address" "Address of the publisher")
291 ("title" "Title of the article in book (BibTeX converts it to lowercase)") 284 ("edition" "Edition of the book as a capitalized English word")
292 ("booktitle" "Name of the book") 285 ("month" "Month of the publication as a string (remove braces)")
293 ("publisher" "Publishing company") 286 ("note" "Remarks to be put at the end of the \\bibitem"))))
294 ("year" "Year of publication")) 287 ("InCollection"
295 (("editor" "Editor1 [and Editor2 ...] [and others]") 288 ((("author" "Author1 [and Author2 ...] [and others]")
296 ("volume" "Volume of the book in the series") 289 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
297 ("number" "Number of the book in a small series (overwritten by volume)") 290 ("booktitle" "Name of the book")
298 ("series" "Series in which the book appeared") 291 ("publisher" "Publishing company")
299 ("type" "Word to use instead of \"chapter\"") 292 ("year" "Year of publication"))
300 ("chapter" "Chapter in the book") 293 (("editor" "Editor1 [and Editor2 ...] [and others]")
301 ("pages" "Pages in the book") 294 ("volume" "Volume of the book in the series")
302 ("address" "Address of the publisher") 295 ("number" "Number of the book in a small series (overwritten by volume)")
303 ("edition" "Edition of the book as a capitalized English word") 296 ("series" "Series in which the book appeared")
304 ("month" "Month of the publication as a string (remove braces)") 297 ("type" "Word to use instead of \"chapter\"")
305 ("note" "Remarks to be put at the end of the \\bibitem"))) 298 ("chapter" "Chapter in the book")
306 ((("author" "Author1 [and Author2 ...] [and others]") 299 ("pages" "Pages in the book")
307 ("title" "Title of the article in book (BibTeX converts it to lowercase)") 300 ("address" "Address of the publisher")
308 ("booktitle" "Name of the book")) 301 ("edition" "Edition of the book as a capitalized English word")
309 (("pages" "Pages in the book") 302 ("month" "Month of the publication as a string (remove braces)")
310 ("publisher" "Publishing company") 303 ("note" "Remarks to be put at the end of the \\bibitem")))
311 ("year" "Year of publication") 304 ((("author" "Author1 [and Author2 ...] [and others]")
312 ("editor" "Editor1 [and Editor2 ...] [and others]") 305 ("title" "Title of the article in book (BibTeX converts it to lowercase)")
313 ("volume" "Volume of the book in the series") 306 ("booktitle" "Name of the book"))
314 ("number" "Number of the book in a small series (overwritten by volume)") 307 (("pages" "Pages in the book")
315 ("series" "Series in which the book appeared") 308 ("publisher" "Publishing company")
316 ("type" "Word to use instead of \"chapter\"") 309 ("year" "Year of publication")
317 ("chapter" "Chapter in the book") 310 ("editor" "Editor1 [and Editor2 ...] [and others]")
318 ("address" "Address of the publisher") 311 ("volume" "Volume of the book in the series")
319 ("edition" "Edition of the book as a capitalized English word") 312 ("number" "Number of the book in a small series (overwritten by volume)")
320 ("month" "Month of the publication as a string (remove braces)") 313 ("series" "Series in which the book appeared")
321 ("note" "Remarks to be put at the end of the \\bibitem"))))) 314 ("type" "Word to use instead of \"chapter\"")
322 ("InProceedings" . (((("author" "Author1 [and Author2 ...] [and others]") 315 ("chapter" "Chapter in the book")
323 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)") 316 ("address" "Address of the publisher")
324 ("booktitle" "Name of the conference proceedings") 317 ("edition" "Edition of the book as a capitalized English word")
325 ("year" "Year of publication")) 318 ("month" "Month of the publication as a string (remove braces)")
326 (("editor" "Editor1 [and Editor2 ...] [and others]") 319 ("note" "Remarks to be put at the end of the \\bibitem"))))
327 ("volume" "Volume of the conference proceedings in the series") 320 ("InProceedings"
328 ("number" "Number of the conference proceedings in a small series (overwritten by volume)") 321 ((("author" "Author1 [and Author2 ...] [and others]")
329 ("series" "Series in which the conference proceedings appeared") 322 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")
330 ("pages" "Pages in the conference proceedings") 323 ("booktitle" "Name of the conference proceedings")
331 ("address" "Location of the Proceedings") 324 ("year" "Year of publication"))
332 ("month" "Month of the publication as a string (remove braces)") 325 (("editor" "Editor1 [and Editor2 ...] [and others]")
333 ("organization" "Sponsoring organization of the conference") 326 ("volume" "Volume of the conference proceedings in the series")
334 ("publisher" "Publishing company, its location") 327 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
335 ("note" "Remarks to be put at the end of the \\bibitem"))) 328 ("series" "Series in which the conference proceedings appeared")
336 ((("author" "Author1 [and Author2 ...] [and others]") 329 ("pages" "Pages in the conference proceedings")
337 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)")) 330 ("address" "Location of the Proceedings")
338 (("booktitle" "Name of the conference proceedings") 331 ("month" "Month of the publication as a string (remove braces)")
339 ("pages" "Pages in the conference proceedings") 332 ("organization" "Sponsoring organization of the conference")
340 ("year" "Year of publication") 333 ("publisher" "Publishing company, its location")
341 ("editor" "Editor1 [and Editor2 ...] [and others]") 334 ("note" "Remarks to be put at the end of the \\bibitem")))
342 ("volume" "Volume of the conference proceedings in the series") 335 ((("author" "Author1 [and Author2 ...] [and others]")
343 ("number" "Number of the conference proceedings in a small series (overwritten by volume)") 336 ("title" "Title of the article in proceedings (BibTeX converts it to lowercase)"))
344 ("series" "Series in which the conference proceedings appeared") 337 (("booktitle" "Name of the conference proceedings")
345 ("address" "Location of the Proceedings") 338 ("pages" "Pages in the conference proceedings")
346 ("month" "Month of the publication as a string (remove braces)") 339 ("year" "Year of publication")
347 ("organization" "Sponsoring organization of the conference") 340 ("editor" "Editor1 [and Editor2 ...] [and others]")
348 ("publisher" "Publishing company, its location") 341 ("volume" "Volume of the conference proceedings in the series")
349 ("note" "Remarks to be put at the end of the \\bibitem"))))) 342 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
350 ("Manual" . (((("title" "Title of the manual")) 343 ("series" "Series in which the conference proceedings appeared")
351 (("author" "Author1 [and Author2 ...] [and others]") 344 ("address" "Location of the Proceedings")
352 ("organization" "Publishing organization of the manual") 345 ("month" "Month of the publication as a string (remove braces)")
353 ("address" "Address of the organization") 346 ("organization" "Sponsoring organization of the conference")
354 ("edition" "Edition of the manual as a capitalized English word") 347 ("publisher" "Publishing company, its location")
355 ("month" "Month of the publication as a string (remove braces)") 348 ("note" "Remarks to be put at the end of the \\bibitem"))))
356 ("year" "Year of publication") 349 ("Manual"
357 ("note" "Remarks to be put at the end of the \\bibitem"))))) 350 ((("title" "Title of the manual"))
358 ("MastersThesis" . (((("author" "Author1 [and Author2 ...] [and others]") 351 (("author" "Author1 [and Author2 ...] [and others]")
359 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)") 352 ("organization" "Publishing organization of the manual")
360 ("school" "School where the master\'s thesis was written") 353 ("address" "Address of the organization")
361 ("year" "Year of publication")) 354 ("edition" "Edition of the manual as a capitalized English word")
362 (("type" "Type of the master\'s thesis (if other than \"Master\'s thesis\")") 355 ("month" "Month of the publication as a string (remove braces)")
363 ("address" "Address of the school (if not part of field \"school\") or country") 356 ("year" "Year of publication")
364 ("month" "Month of the publication as a string (remove braces)") 357 ("note" "Remarks to be put at the end of the \\bibitem"))))
365 ("note" "Remarks to be put at the end of the \\bibitem"))))) 358 ("MastersThesis"
366 ("Misc" . ((() 359 ((("author" "Author1 [and Author2 ...] [and others]")
367 (("author" "Author1 [and Author2 ...] [and others]") 360 ("title" "Title of the master\'s thesis (BibTeX converts it to lowercase)")
368 ("title" "Title of the work (BibTeX converts it to lowercase)") 361 ("school" "School where the master\'s thesis was written")
369 ("howpublished" "The way in which the work was published") 362 ("year" "Year of publication"))
370 ("month" "Month of the publication as a string (remove braces)") 363 (("type" "Type of the master\'s thesis (if other than \"Master\'s thesis\")")
371 ("year" "Year of publication") 364 ("address" "Address of the school (if not part of field \"school\") or country")
372 ("note" "Remarks to be put at the end of the \\bibitem"))))) 365 ("month" "Month of the publication as a string (remove braces)")
373 ("PhdThesis" . (((("author" "Author1 [and Author2 ...] [and others]") 366 ("note" "Remarks to be put at the end of the \\bibitem"))))
374 ("title" "Title of the PhD. thesis") 367 ("Misc"
375 ("school" "School where the PhD. thesis was written") 368 (()
376 ("year" "Year of publication")) 369 (("author" "Author1 [and Author2 ...] [and others]")
377 (("type" "Type of the PhD. thesis") 370 ("title" "Title of the work (BibTeX converts it to lowercase)")
378 ("address" "Address of the school (if not part of field \"school\") or country") 371 ("howpublished" "The way in which the work was published")
379 ("month" "Month of the publication as a string (remove braces)") 372 ("month" "Month of the publication as a string (remove braces)")
380 ("note" "Remarks to be put at the end of the \\bibitem"))))) 373 ("year" "Year of publication")
381 ("Proceedings" . (((("title" "Title of the conference proceedings") 374 ("note" "Remarks to be put at the end of the \\bibitem"))))
382 ("year" "Year of publication")) 375 ("PhdThesis"
383 (("booktitle" "Title of the proceedings for cross references") 376 ((("author" "Author1 [and Author2 ...] [and others]")
384 ("editor" "Editor1 [and Editor2 ...] [and others]") 377 ("title" "Title of the PhD. thesis")
385 ("volume" "Volume of the conference proceedings in the series") 378 ("school" "School where the PhD. thesis was written")
386 ("number" "Number of the conference proceedings in a small series (overwritten by volume)") 379 ("year" "Year of publication"))
387 ("series" "Series in which the conference proceedings appeared") 380 (("type" "Type of the PhD. thesis")
388 ("address" "Location of the Proceedings") 381 ("address" "Address of the school (if not part of field \"school\") or country")
389 ("month" "Month of the publication as a string (remove braces)") 382 ("month" "Month of the publication as a string (remove braces)")
390 ("organization" "Sponsoring organization of the conference") 383 ("note" "Remarks to be put at the end of the \\bibitem"))))
391 ("publisher" "Publishing company, its location") 384 ("Proceedings"
392 ("note" "Remarks to be put at the end of the \\bibitem"))))) 385 ((("title" "Title of the conference proceedings")
393 ("TechReport" . (((("author" "Author1 [and Author2 ...] [and others]") 386 ("year" "Year of publication"))
394 ("title" "Title of the technical report (BibTeX converts it to lowercase)") 387 (("booktitle" "Title of the proceedings for cross references")
395 ("institution" "Sponsoring institution of the report") 388 ("editor" "Editor1 [and Editor2 ...] [and others]")
396 ("year" "Year of publication")) 389 ("volume" "Volume of the conference proceedings in the series")
397 (("type" "Type of the report (if other than \"technical report\")") 390 ("number" "Number of the conference proceedings in a small series (overwritten by volume)")
398 ("number" "Number of the technical report") 391 ("series" "Series in which the conference proceedings appeared")
399 ("address" "Address of the institution (if not part of field \"institution\") or country") 392 ("address" "Location of the Proceedings")
400 ("month" "Month of the publication as a string (remove braces)") 393 ("month" "Month of the publication as a string (remove braces)")
401 ("note" "Remarks to be put at the end of the \\bibitem"))))) 394 ("organization" "Sponsoring organization of the conference")
402 ("Unpublished" . (((("author" "Author1 [and Author2 ...] [and others]") 395 ("publisher" "Publishing company, its location")
403 ("title" "Title of the unpublished work (BibTeX converts it to lowercase)") 396 ("note" "Remarks to be put at the end of the \\bibitem"))))
404 ("note" "Remarks to be put at the end of the \\bibitem")) 397 ("TechReport"
405 (("month" "Month of the publication as a string (remove braces)") 398 ((("author" "Author1 [and Author2 ...] [and others]")
406 ("year" "Year of publication"))))) 399 ("title" "Title of the technical report (BibTeX converts it to lowercase)")
400 ("institution" "Sponsoring institution of the report")
401 ("year" "Year of publication"))
402 (("type" "Type of the report (if other than \"technical report\")")
403 ("number" "Number of the technical report")
404 ("address" "Address of the institution (if not part of field \"institution\") or country")
405 ("month" "Month of the publication as a string (remove braces)")
406 ("note" "Remarks to be put at the end of the \\bibitem"))))
407 ("Unpublished"
408 ((("author" "Author1 [and Author2 ...] [and others]")
409 ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
410 ("note" "Remarks to be put at the end of the \\bibitem"))
411 (("month" "Month of the publication as a string (remove braces)")
412 ("year" "Year of publication"))))
407 ) 413 )
408 414
409 "Defines entry types and their associated fields. 415 "Defines entry types and their associated fields.
@@ -424,7 +430,8 @@ of the field, and ALTERNATIVE-FLAG (either nil or t) marks if the
424field is an alternative. ALTERNATIVE-FLAG may be t only in the 430field is an alternative. ALTERNATIVE-FLAG may be t only in the
425REQUIRED or CROSSREF-REQUIRED lists.") 431REQUIRED or CROSSREF-REQUIRED lists.")
426 432
427(defvar bibtex-comment-start "@Comment ") 433(defvar bibtex-comment-start "@Comment"
434 "String starting a BibTeX comment.")
428 435
429(defcustom bibtex-add-entry-hook nil 436(defcustom bibtex-add-entry-hook nil
430 "List of functions to call when entry has been inserted." 437 "List of functions to call when entry has been inserted."
@@ -432,32 +439,52 @@ REQUIRED or CROSSREF-REQUIRED lists.")
432 :type 'hook) 439 :type 'hook)
433 440
434(defcustom bibtex-predefined-month-strings 441(defcustom bibtex-predefined-month-strings
435 '( 442 '(("jan" . "January")
436 ("jan") ("feb") ("mar") ("apr") ("may") ("jun") 443 ("feb" . "February")
437 ("jul") ("aug") ("sep") ("oct") ("nov") ("dec") 444 ("mar" . "March")
438 ) 445 ("apr" . "April")
439 "Alist of month string definitions. 446 ("may" . "May")
440Should contain all strings used for months in the BibTeX style files. 447 ("jun" . "June")
441Each element is a list with just one element: the string." 448 ("jul" . "July")
449 ("aug" . "August")
450 ("sep" . "September")
451 ("oct" . "October")
452 ("nov" . "November")
453 ("dec" . "December"))
454 "Alist of month string definitions used in the BibTeX style files.
455Each element is a pair of strings (ABBREVIATION EXPANSION)."
442 :group 'bibtex 456 :group 'bibtex
443 :type '(repeat 457 :type '(repeat (cons (string :tag "Month abbreviation")
444 (list string))) 458 (string :tag "Month expansion"))))
445 459
446(defcustom bibtex-predefined-strings 460(defcustom bibtex-predefined-strings
447 (append 461 (append
448 bibtex-predefined-month-strings 462 bibtex-predefined-month-strings
449 '( 463 '(("acmcs" . "ACM Computing Surveys")
450 ("acmcs") ("acta") ("cacm") ("ibmjrd") ("ibmsj") ("ieeese") 464 ("acta" . "Acta Informatica")
451 ("ieeetc") ("ieeetcad") ("ipl") ("jacm") ("jcss") ("scp") 465 ("cacm" . "Communications of the ACM")
452 ("sicomp") ("tcs") ("tocs") ("tods") ("tog") ("toms") ("toois") 466 ("ibmjrd" . "IBM Journal of Research and Development")
453 ("toplas") 467 ("ibmsj" . "IBM Systems Journal")
454 )) 468 ("ieeese" . "IEEE Transactions on Software Engineering")
455 "Alist of string definitions. 469 ("ieeetc" . "IEEE Transactions on Computers")
456Should contain the strings defined in the BibTeX style files. Each 470 ("ieeetcad" . "IEEE Transactions on Computer-Aided Design of Integrated Circuits")
457element is a list with just one element: the string." 471 ("ipl" . "Information Processing Letters")
472 ("jacm" . "Journal of the ACM")
473 ("jcss" . "Journal of Computer and System Sciences")
474 ("scp" . "Science of Computer Programming")
475 ("sicomp" . "SIAM Journal on Computing")
476 ("tcs" . "Theoretical Computer Science")
477 ("tocs" . "ACM Transactions on Computer Systems")
478 ("tods" . "ACM Transactions on Database Systems")
479 ("tog" . "ACM Transactions on Graphics")
480 ("toms" . "ACM Transactions on Mathematical Software")
481 ("toois" . "ACM Transactions on Office Information Systems")
482 ("toplas" . "ACM Transactions on Programming Languages and Systems")))
483 "Alist of string definitions used in the BibTeX style files.
484Each element is a pair of strings (ABBREVIATION EXPANSION)."
458 :group 'bibtex 485 :group 'bibtex
459 :type '(repeat 486 :type '(repeat (cons (string :tag "String")
460 (list string))) 487 (string :tag "String expansion"))))
461 488
462(defcustom bibtex-string-files nil 489(defcustom bibtex-string-files nil
463 "*List of BibTeX files containing string definitions. 490 "*List of BibTeX files containing string definitions.
@@ -484,12 +511,12 @@ See the documentation of function `bibtex-generate-autokey' for more detail."
484 511
485(defcustom bibtex-autokey-names 1 512(defcustom bibtex-autokey-names 1
486 "*Number of names to use for the automatically generated reference key. 513 "*Number of names to use for the automatically generated reference key.
487If this is variable is nil, all names are used. 514If this variable is nil, all names are used.
488Possibly more names are used according to `bibtex-autokey-names-stretch'. 515Possibly more names are used according to `bibtex-autokey-names-stretch'.
489See the documentation of function `bibtex-generate-autokey' for more detail." 516See the documentation of function `bibtex-generate-autokey' for more detail."
490 :group 'bibtex-autokey 517 :group 'bibtex-autokey
491 :type '(choice (const :tag "All" infty) 518 :type '(choice (const :tag "All" infty)
492 integer)) 519 integer))
493 520
494(defcustom bibtex-autokey-names-stretch 0 521(defcustom bibtex-autokey-names-stretch 0
495 "*Number of names that can additionally be used. 522 "*Number of names that can additionally be used.
@@ -505,26 +532,28 @@ See the documentation of function `bibtex-generate-autokey' for details."
505 :type 'string) 532 :type 'string)
506 533
507(defvar bibtex-autokey-transcriptions 534(defvar bibtex-autokey-transcriptions
508 '( 535 '(;; language specific characters
509 ;; language specific characters 536 ("\\\\aa" . "a") ; \aa -> a
510 ("\\\\aa" "a") 537 ("\\\\AA" . "A") ; \AA -> A
511 ("\\\\AA" "A") 538 ("\\\"a\\|\\\\\\\"a\\|\\\\ae" . "ae") ; "a,\"a,\ae -> ae
512 ("\\\"a\\|\\\\\\\"a\\|\\\\ae" "ae") 539 ("\\\"A\\|\\\\\\\"A\\|\\\\AE" . "Ae") ; "A,\"A,\AE -> Ae
513 ("\\\"A\\|\\\\\\\"A\\|\\\\AE" "Ae") 540 ("\\\\i" . "i") ; \i -> i
514 ("\\\\i" "i") 541 ("\\\\j" . "j") ; \j -> j
515 ("\\\\j" "j") 542 ("\\\\l" . "l") ; \l -> l
516 ("\\\\l" "l") 543 ("\\\\L" . "L") ; \L -> L
517 ("\\\\L" "L") 544 ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" . "oe") ; "o,\"o,\o,\oe -> oe
518 ("\\\"o\\|\\\\\\\"o\\|\\\\o\\|\\\\oe" "oe") 545 ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" . "Oe") ; "O,\"O,\O,\OE -> Oe
519 ("\\\"O\\|\\\\\\\"O\\|\\\\O\\|\\\\OE" "Oe") 546 ("\\\"s\\|\\\\\\\"s\\|\\\\3" . "ss") ; "s,\"s,\3 -> ss
520 ("\\\"s\\|\\\\\\\"s" "ss") 547 ("\\\"u\\|\\\\\\\"u" . "ue") ; "u,\"u -> ue
521 ("\\\"u\\|\\\\\\\"u" "ue") 548 ("\\\"U\\|\\\\\\\"U" . "Ue") ; "U,\"U -> Ue
522 ("\\\"U\\|\\\\\\\"U" "Ue")
523 ;; accents 549 ;; accents
524 ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" "") 550 ("\\\\`\\|\\\\'\\|\\\\\\^\\|\\\\~\\|\\\\=\\|\\\\\\.\\|\\\\u\\|\\\\v\\|\\\\H\\|\\\\t\\|\\\\c\\|\\\\d\\|\\\\b" . "")
551 ("`\\|'\\|\\\"" . "")
525 ;; braces 552 ;; braces
526 ("{" "") ("}" "")) 553 ("{" . "") ("}" . "")
527 "Alist of (old-regexp new-string) pairs. 554 ;; spaces
555 ("[ \t\n]+" . " "))
556 "Alist of (OLD-REGEXP NEW-STRING) pairs.
528Used by the default values of `bibtex-autokey-name-change-strings' and 557Used by the default values of `bibtex-autokey-name-change-strings' and
529`bibtex-autokey-titleword-change-strings'. Defaults to translating some 558`bibtex-autokey-titleword-change-strings'. Defaults to translating some
530language specific characters to their ASCII transcriptions, and 559language specific characters to their ASCII transcriptions, and
@@ -535,23 +564,21 @@ removing any character accents.")
535 "Alist of (OLD-REGEXP NEW-STRING) pairs. 564 "Alist of (OLD-REGEXP NEW-STRING) pairs.
536Any part of name matching a OLD-REGEXP is replaced by NEW-STRING. 565Any part of name matching a OLD-REGEXP is replaced by NEW-STRING.
537Case is significant in OLD-REGEXP. All regexps are tried in the 566Case is significant in OLD-REGEXP. All regexps are tried in the
538order in which they appear in the list, so be sure to avoid inifinite 567order in which they appear in the list.
539loops here.
540See the documentation of function `bibtex-generate-autokey' for details." 568See the documentation of function `bibtex-generate-autokey' for details."
541 :group 'bibtex-autokey 569 :group 'bibtex-autokey
542 :type '(repeat 570 :type '(repeat (cons (regexp :tag "Old")
543 (list (regexp :tag "Old") 571 (string :tag "New"))))
544 (string :tag "New"))))
545 572
546(defcustom bibtex-autokey-name-case-convert 'downcase 573(defcustom bibtex-autokey-name-case-convert 'downcase
547 "*Function called for each name to perform case conversion. 574 "*Function called for each name to perform case conversion.
548See the documentation of function `bibtex-generate-autokey' for more detail." 575See the documentation of function `bibtex-generate-autokey' for details."
549 :group 'bibtex-autokey 576 :group 'bibtex-autokey
550 :type '(choice (const :tag "Preserve case" identity) 577 :type '(choice (const :tag "Preserve case" identity)
551 (const :tag "Downcase" downcase) 578 (const :tag "Downcase" downcase)
552 (const :tag "Capitalize" capitalize) 579 (const :tag "Capitalize" capitalize)
553 (const :tag "Upcase" upcase) 580 (const :tag "Upcase" upcase)
554 (function :tag "Conversion function"))) 581 (function :tag "Conversion function")))
555 582
556(defcustom bibtex-autokey-name-length 'infty 583(defcustom bibtex-autokey-name-length 'infty
557 "*Number of characters from name to incorporate into key. 584 "*Number of characters from name to incorporate into key.
@@ -559,7 +586,7 @@ If this is set to anything but a number, all characters are used.
559See the documentation of function `bibtex-generate-autokey' for details." 586See the documentation of function `bibtex-generate-autokey' for details."
560 :group 'bibtex-autokey 587 :group 'bibtex-autokey
561 :type '(choice (const :tag "All" infty) 588 :type '(choice (const :tag "All" infty)
562 integer)) 589 integer))
563 590
564(defcustom bibtex-autokey-name-separator "" 591(defcustom bibtex-autokey-name-separator ""
565 "*String that comes between any two names in the key. 592 "*String that comes between any two names in the key.
@@ -573,11 +600,10 @@ See the documentation of function `bibtex-generate-autokey' for details."
573 :group 'bibtex-autokey 600 :group 'bibtex-autokey
574 :type 'integer) 601 :type 'integer)
575 602
576(defcustom bibtex-autokey-year-use-crossref-entry t 603(defcustom bibtex-autokey-use-crossref t
577 "*If non-nil use year field from crossreferenced entry if necessary. 604 "*If non-nil use fields from crossreferenced entry if necessary.
578If this variable is non-nil and the current entry has no year, but a 605If this variable is non-nil and some field has no entry, but a
579valid crossref entry, the year field from the crossreferenced entry is 606valid crossref entry, the field from the crossreferenced entry is used.
580used.
581See the documentation of function `bibtex-generate-autokey' for details." 607See the documentation of function `bibtex-generate-autokey' for details."
582 :group 'bibtex-autokey 608 :group 'bibtex-autokey
583 :type 'boolean) 609 :type 'boolean)
@@ -590,7 +616,7 @@ Possibly more words from the title are used according to
590See the documentation of function `bibtex-generate-autokey' for details." 616See the documentation of function `bibtex-generate-autokey' for details."
591 :group 'bibtex-autokey 617 :group 'bibtex-autokey
592 :type '(choice (const :tag "All" infty) 618 :type '(choice (const :tag "All" infty)
593 integer)) 619 integer))
594 620
595(defcustom bibtex-autokey-title-terminators 621(defcustom bibtex-autokey-title-terminators
596 '("\\." "!" "\\?" ":" ";" "--") 622 '("\\." "!" "\\?" ":" ";" "--")
@@ -619,32 +645,33 @@ See the documentation of function `bibtex-generate-autokey' for details."
619 645
620(defcustom bibtex-autokey-titleword-case-convert 'downcase 646(defcustom bibtex-autokey-titleword-case-convert 'downcase
621 "*Function called for each titleword to perform case conversion. 647 "*Function called for each titleword to perform case conversion.
622See the documentation of function `bibtex-generate-autokey' for more detail." 648See the documentation of function `bibtex-generate-autokey' for details."
623 :group 'bibtex-autokey 649 :group 'bibtex-autokey
624 :type '(choice (const :tag "Preserve case" identity) 650 :type '(choice (const :tag "Preserve case" identity)
625 (const :tag "Downcase" downcase) 651 (const :tag "Downcase" downcase)
626 (const :tag "Capitalize" capitalize) 652 (const :tag "Capitalize" capitalize)
627 (const :tag "Upcase" upcase) 653 (const :tag "Upcase" upcase)
628 (function :tag "Conversion function"))) 654 (function :tag "Conversion function")))
629 655
630(defcustom bibtex-autokey-titleword-abbrevs nil 656(defcustom bibtex-autokey-titleword-abbrevs nil
631 "*Determines exceptions to the usual abbreviation mechanism. 657 "*Determines exceptions to the usual abbreviation mechanism.
632An alist of (OLD-REGEXP NEW-STRING) pairs. Case is ignored 658An alist of (OLD-REGEXP NEW-STRING) pairs. Case is ignored
633in matching against OLD-REGEXP, and the first matching pair is used. 659in matching against OLD-REGEXP, and the first matching pair is used.
634See the documentation of function `bibtex-generate-autokey' for details.") 660See the documentation of function `bibtex-generate-autokey' for details."
661 :group 'bibtex-autokey
662 :type '(repeat (cons (regexp :tag "Old")
663 (string :tag "New"))))
635 664
636(defcustom bibtex-autokey-titleword-change-strings 665(defcustom bibtex-autokey-titleword-change-strings
637 bibtex-autokey-transcriptions 666 bibtex-autokey-transcriptions
638 "Alist of (OLD-REGEXP NEW-STRING) pairs. 667 "Alist of (OLD-REGEXP NEW-STRING) pairs.
639Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING. 668Any part of title word matching a OLD-REGEXP is replaced by NEW-STRING.
640Case is significant in OLD-REGEXP. All regexps are tried in the 669Case is significant in OLD-REGEXP. All regexps are tried in the
641order in which they appear in the list, so be sure to avoid inifinite 670order in which they appear in the list.
642loops here.
643See the documentation of function `bibtex-generate-autokey' for details." 671See the documentation of function `bibtex-generate-autokey' for details."
644 :group 'bibtex-autokey 672 :group 'bibtex-autokey
645 :type '(repeat 673 :type '(repeat (cons (regexp :tag "Old")
646 (list (regexp :tag "Old") 674 (string :tag "New"))))
647 (string :tag "New"))))
648 675
649(defcustom bibtex-autokey-titleword-length 5 676(defcustom bibtex-autokey-titleword-length 5
650 "*Number of characters from title words to incorporate into key. 677 "*Number of characters from title words to incorporate into key.
@@ -652,7 +679,7 @@ If this is set to anything but a number, all characters are used.
652See the documentation of function `bibtex-generate-autokey' for details." 679See the documentation of function `bibtex-generate-autokey' for details."
653 :group 'bibtex-autokey 680 :group 'bibtex-autokey
654 :type '(choice (const :tag "All" infty) 681 :type '(choice (const :tag "All" infty)
655 integer)) 682 integer))
656 683
657(defcustom bibtex-autokey-titleword-separator "_" 684(defcustom bibtex-autokey-titleword-separator "_"
658 "*String to be put between the title words. 685 "*String to be put between the title words.
@@ -678,7 +705,7 @@ See the documentation of function `bibtex-generate-autokey' for details."
678 :type 'boolean) 705 :type 'boolean)
679 706
680(defcustom bibtex-autokey-before-presentation-function nil 707(defcustom bibtex-autokey-before-presentation-function nil
681 "Function to call before the generated key is presented. 708 "*Function to call before the generated key is presented.
682If non-nil this should be a single function, which is called before 709If non-nil this should be a single function, which is called before
683the generated key is presented (in entry or, if 710the generated key is presented (in entry or, if
684`bibtex-autokey-edit-before-use' is t, in minibuffer). This function 711`bibtex-autokey-edit-before-use' is t, in minibuffer). This function
@@ -699,9 +726,8 @@ Added to the value of all other variables which determine colums."
699 :type 'integer) 726 :type 'integer)
700 727
701(defcustom bibtex-text-indentation 728(defcustom bibtex-text-indentation
702 (+ 729 (+ bibtex-field-indentation
703 bibtex-field-indentation 730 (length "organization = "))
704 (length "organization = "))
705 "*Starting column for the text part in BibTeX fields. 731 "*Starting column for the text part in BibTeX fields.
706Should be equal to the space needed for the longest name part." 732Should be equal to the space needed for the longest name part."
707 :group 'bibtex 733 :group 'bibtex
@@ -715,8 +741,8 @@ Should be equal to the space needed for the longest name part."
715 741
716(defcustom bibtex-align-at-equal-sign nil 742(defcustom bibtex-align-at-equal-sign nil
717 "*If non-nil, align fields at equal sign instead of field text. 743 "*If non-nil, align fields at equal sign instead of field text.
718If non-nil, the column for the equal sign is 744If non-nil, the column for the equal sign is the value of
719the value of `bibtex-text-indentation', minus 2." 745`bibtex-text-indentation', minus 2."
720 :group 'bibtex 746 :group 'bibtex
721 :type 'boolean) 747 :type 'boolean)
722 748
@@ -725,44 +751,15 @@ the value of `bibtex-text-indentation', minus 2."
725 :group 'bibtex 751 :group 'bibtex
726 :type 'boolean) 752 :type 'boolean)
727 753
754(defcustom bibtex-complete-key-cleanup nil
755 "*Function called by `bibtex-complete' after insertion of a key fragment."
756 :group 'bibtex-autokey
757 :type '(choice (const :tag "None" nil)
758 (function :tag "Cleanup function")))
759
728;; bibtex-font-lock-keywords is a user option as well, but since the 760;; bibtex-font-lock-keywords is a user option as well, but since the
729;; patterns used to define this variable are defined in a later 761;; patterns used to define this variable are defined in a later
730;; section of this file, it is defined later. 762;; section of this file, it is defined later.
731
732;; Special support taking care of variants
733(if (boundp 'mark-active)
734 (defun bibtex-mark-active ()
735 ;; In Emacs mark-active indicates if mark is active.
736 mark-active)
737 (defun bibtex-mark-active ()
738 ;; In XEmacs (mark) returns nil when not active.
739 (if zmacs-regions (mark) (mark t))))
740
741(if (fboundp 'run-with-idle-timer)
742 ;; timer.el is distributed with Emacs
743 (fset 'bibtex-run-with-idle-timer 'run-with-idle-timer)
744 ;; timer.el is not distributed with XEmacs
745 ;; Notice that this does not (yet) pass the arguments, but they
746 ;; are not used (yet) in bibtex.el. Fix if needed.
747 (defun bibtex-run-with-idle-timer (secs repeat function &rest args)
748 (start-itimer "bibtex" function secs (if repeat secs nil) t)))
749
750
751;; Support for hideshow minor mode
752(defun bibtex-hs-forward-sexp (arg)
753 "Replacement for `forward-sexp' to be used by `hs-minor-mode'."
754 (if (< arg 0)
755 (backward-sexp 1)
756 (if (looking-at "@\\S(*\\s(")
757 (progn
758 (goto-char (match-end 0))
759 (forward-char -1)
760 (forward-sexp 1))
761 (forward-sexp 1))))
762
763(add-to-list
764 'hs-special-modes-alist
765 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil))
766 763
767 764
768;; Syntax Table, Keybindings and BibTeX Entry List 765;; Syntax Table, Keybindings and BibTeX Entry List
@@ -776,16 +773,18 @@ the value of `bibtex-text-indentation', minus 2."
776 (modify-syntax-entry ?\\ "\\" st) 773 (modify-syntax-entry ?\\ "\\" st)
777 (modify-syntax-entry ?\f "> " st) 774 (modify-syntax-entry ?\f "> " st)
778 (modify-syntax-entry ?\n "> " st) 775 (modify-syntax-entry ?\n "> " st)
776 ;; Keys cannot have = in them (wrong font-lock of @string{foo=bar}).
777 (modify-syntax-entry ?= "." st)
779 (modify-syntax-entry ?~ " " st) 778 (modify-syntax-entry ?~ " " st)
780 st)) 779 st)
780 "Syntax table used in BibTeX mode buffers.")
781 781
782(defvar bibtex-mode-map 782(defvar bibtex-mode-map
783 (let ((km (make-sparse-keymap))) 783 (let ((km (make-sparse-keymap)))
784 ;; The Key `C-c&' is reserved for reftex.el 784 ;; The Key `C-c&' is reserved for reftex.el
785 (define-key km "\t" 'bibtex-find-text) 785 (define-key km "\t" 'bibtex-find-text)
786 (define-key km "\n" 'bibtex-next-field) 786 (define-key km "\n" 'bibtex-next-field)
787 (define-key km "\M-\t" 'bibtex-complete-string) 787 (define-key km "\M-\t" 'bibtex-complete)
788 (define-key km [(meta tab)] 'bibtex-complete-key)
789 (define-key km "\C-c\"" 'bibtex-remove-delimiters) 788 (define-key km "\C-c\"" 'bibtex-remove-delimiters)
790 (define-key km "\C-c{" 'bibtex-remove-delimiters) 789 (define-key km "\C-c{" 'bibtex-remove-delimiters)
791 (define-key km "\C-c}" 'bibtex-remove-delimiters) 790 (define-key km "\C-c}" 'bibtex-remove-delimiters)
@@ -829,682 +828,700 @@ the value of `bibtex-text-indentation', minus 2."
829 (define-key km "\C-c\C-e\C-s" 'bibtex-String) 828 (define-key km "\C-c\C-e\C-s" 'bibtex-String)
830 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport) 829 (define-key km "\C-c\C-e\C-t" 'bibtex-TechReport)
831 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished) 830 (define-key km "\C-c\C-e\C-u" 'bibtex-Unpublished)
832 km)) 831 km)
832 "Keymap used in BibTeX mode.")
833 833
834(easy-menu-define 834(easy-menu-define
835 bibtex-edit-menu bibtex-mode-map "BibTeX-Edit Menu in BibTeX mode" 835 bibtex-edit-menu bibtex-mode-map "BibTeX-Edit Menu in BibTeX mode"
836 '("BibTeX-Edit" 836 '("BibTeX-Edit"
837 ("Moving inside an Entry" 837 ("Moving inside an Entry"
838 ["End of Field" bibtex-find-text t] 838 ["End of Field" bibtex-find-text t]
839 ["Next Field" bibtex-next-field t] 839 ["Next Field" bibtex-next-field t]
840 ["Beginning of Entry" bibtex-beginning-of-entry t] 840 ["Beginning of Entry" bibtex-beginning-of-entry t]
841 ["End of Entry" bibtex-end-of-entry t]) 841 ["End of Entry" bibtex-end-of-entry t])
842 ("Operating on Current Entry" 842 ("Operating on Current Entry"
843 ["Fill Entry" bibtex-fill-entry t] 843 ["Fill Entry" bibtex-fill-entry t]
844 ["Clean Entry" bibtex-clean-entry t] 844 ["Clean Entry" bibtex-clean-entry t]
845 "--" 845 "--"
846 ["Kill Entry" bibtex-kill-entry t] 846 ["Kill Entry" bibtex-kill-entry t]
847 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t] 847 ["Copy Entry to Kill Ring" bibtex-copy-entry-as-kill t]
848 ["Paste Most Recently Killed Entry" bibtex-yank t] 848 ["Paste Most Recently Killed Entry" bibtex-yank t]
849 ["Paste Previously Killed Entry" bibtex-yank-pop t] 849 ["Paste Previously Killed Entry" bibtex-yank-pop t]
850 "--" 850 "--"
851 ["Ispell Entry" bibtex-ispell-entry t] 851 ["Ispell Entry" bibtex-ispell-entry t]
852 ["Ispell Entry Abstract" bibtex-ispell-abstract t] 852 ["Ispell Entry Abstract" bibtex-ispell-abstract t]
853 ["Narrow to Entry" bibtex-narrow-to-entry t] 853 ["Narrow to Entry" bibtex-narrow-to-entry t]
854 "--" 854 "--"
855 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex 855 ["View Cite Locations (RefTeX)" reftex-view-crossref-from-bibtex
856 (fboundp 'reftex-view-crossref-from-bibtex)]) 856 (fboundp 'reftex-view-crossref-from-bibtex)])
857 ("Operating on Current Field" 857 ("Operating on Current Field"
858 ["Remove Delimiters" bibtex-remove-delimiters t] 858 ["Remove Delimiters" bibtex-remove-delimiters t]
859 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t] 859 ["Remove OPT or ALT Prefix" bibtex-remove-OPT-or-ALT t]
860 ["Clear Field" bibtex-empty-field t] 860 ["Clear Field" bibtex-empty-field t]
861 "--" 861 "--"
862 ["Kill Field" bibtex-kill-field t] 862 ["Kill Field" bibtex-kill-field t]
863 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t] 863 ["Copy Field to Kill Ring" bibtex-copy-field-as-kill t]
864 ["Paste Most Recently Killed Field" bibtex-yank t] 864 ["Paste Most Recently Killed Field" bibtex-yank t]
865 ["Paste Previously Killed Field" bibtex-yank-pop t] 865 ["Paste Previously Killed Field" bibtex-yank-pop t]
866 "--" 866 "--"
867 ["Make New Field" bibtex-make-field t] 867 ["Make New Field" bibtex-make-field t]
868 "--" 868 "--"
869 ["Snatch from Similar Following Field" bibtex-pop-next t] 869 ["Snatch from Similar Following Field" bibtex-pop-next t]
870 ["Snatch from Similar Preceding Field" bibtex-pop-previous t] 870 ["Snatch from Similar Preceding Field" bibtex-pop-previous t]
871 "--" 871 "--"
872 ["String Complete" bibtex-complete-string t] 872 ["String or Key Complete" bibtex-complete t]
873 ["Key Complete" bibtex-complete-key t] 873 "--"
874 "--" 874 ["Help about Current Field" bibtex-print-help-message t])
875 ["Help about Current Field" bibtex-print-help-message t]) 875 ("Operating on Buffer or Region"
876 ("Operating on Buffer or Region" 876 ["Validate Entries" bibtex-validate t]
877 ["Validate Entries" bibtex-validate t] 877 ["Sort Entries" bibtex-sort-buffer t]
878 ["Sort Entries" bibtex-sort-buffer t] 878 ["Reformat Entries" bibtex-reformat t]
879 ["Reformat Entries" bibtex-reformat t] 879 ["Count Entries" bibtex-count-entries t])
880 ["Count Entries" bibtex-count-entries t]) 880 ("Miscellaneous"
881 ("Miscellaneous" 881 ["Convert Alien Buffer" bibtex-convert-alien t])))
882 ["Convert Alien Buffer" bibtex-convert-alien t]
883 ["Submit Bug Report" bibtex-submit-bug-report t])))
884 882
885(easy-menu-define 883(easy-menu-define
886 bibtex-entry-menu bibtex-mode-map "Entry-Types Menu in BibTeX mode" 884 bibtex-entry-menu bibtex-mode-map "Entry-Types Menu in BibTeX mode"
887 (list "Entry-Types" 885 (list "Entry-Types"
888 ["Article in Journal" bibtex-Article t] 886 ["Article in Journal" bibtex-Article t]
889 ["Article in Conference Proceedings" bibtex-InProceedings t] 887 ["Article in Conference Proceedings" bibtex-InProceedings t]
890 ["Article in a Collection" bibtex-InCollection t] 888 ["Article in a Collection" bibtex-InCollection t]
891 ["Chapter or Pages in a Book" bibtex-InBook t] 889 ["Chapter or Pages in a Book" bibtex-InBook t]
892 ["Conference Proceedings" bibtex-Proceedings t] 890 ["Conference Proceedings" bibtex-Proceedings t]
893 ["Book" bibtex-Book t] 891 ["Book" bibtex-Book t]
894 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t] 892 ["Booklet (Bound, but no Publisher/Institution)" bibtex-Booklet t]
895 ["PhD. Thesis" bibtex-PhdThesis t] 893 ["PhD. Thesis" bibtex-PhdThesis t]
896 ["Master's Thesis" bibtex-MastersThesis t] 894 ["Master's Thesis" bibtex-MastersThesis t]
897 ["Technical Report" bibtex-TechReport t] 895 ["Technical Report" bibtex-TechReport t]
898 ["Technical Manual" bibtex-Manual t] 896 ["Technical Manual" bibtex-Manual t]
899 ["Unpublished" bibtex-Unpublished t] 897 ["Unpublished" bibtex-Unpublished t]
900 ["Miscellaneous" bibtex-Misc t] 898 ["Miscellaneous" bibtex-Misc t]
901 ["String" bibtex-String t] 899 ["String" bibtex-String t]
902 ["Preamble" bibtex-Preamble t])) 900 ["Preamble" bibtex-Preamble t]))
903 901
904 902
905;; Internal Variables 903;; Internal Variables
906 904
907(defvar bibtex-pop-previous-search-point nil) 905(defvar bibtex-pop-previous-search-point nil
908;; Next point where bibtex-pop-previous starts looking for a similar 906 "Next point where `bibtex-pop-previous' starts looking for a similar entry.")
909;; entry. 907
908(defvar bibtex-pop-next-search-point nil
909 "Next point where `bibtex-pop-next' starts looking for a similar entry.")
910
911(defvar bibtex-field-kill-ring nil
912 "Ring of least recently killed fields.
913At most `bibtex-field-kill-ring-max' items are kept here.")
910 914
911(defvar bibtex-pop-next-search-point nil) 915(defvar bibtex-field-kill-ring-yank-pointer nil
912;; Next point where bibtex-pop-next starts looking for a similar entry. 916 "The tail of `bibtex-field-kill-ring' whose car is the last item yanked.")
913 917
914(defvar bibtex-field-kill-ring nil) 918(defvar bibtex-entry-kill-ring nil
915;; Ring of least recently killed fields. At most 919 "Ring of least recently killed entries.
916;; bibtex-field-kill-ring-max items are kept here. 920At most `bibtex-entry-kill-ring-max' items are kept here.")
917 921
918(defvar bibtex-field-kill-ring-yank-pointer nil) 922(defvar bibtex-entry-kill-ring-yank-pointer nil
919;; The tail of bibtex-field-kill-ring whose car is the last item yanked. 923 "The tail of `bibtex-entry-kill-ring' whose car is the last item yanked.")
920 924
921(defvar bibtex-entry-kill-ring nil) 925(defvar bibtex-last-kill-command nil
922;; Ring of least recently killed entries. At most 926 "Type of the last kill command (either 'field or 'entry).")
923;; bibtex-entry-kill-ring-max items are kept here.
924 927
925(defvar bibtex-entry-kill-ring-yank-pointer nil) 928(defvar bibtex-strings nil
926;; The tail of bibtex-entry-kill-ring whose car is the last item yanked. 929 "Candidates for `bibtex-complete' (string completion).
930Initialized from `bibtex-predefined-strings' and `bibtex-string-files'.")
927 931
928(defvar bibtex-last-kill-command nil) 932(defvar bibtex-reference-keys nil
929;; Holds the type of the last kill command (either 'field or 'entry) 933 "Candidates for TAB completion when entering a reference key using
934the minibuffer. Also used for `bibtex-complete' (key completion).
935Initialized in `bibtex-mode' and updated for each new entry.")
930 936
931(defvar bibtex-strings nil) 937(defvar bibtex-buffer-last-parsed-tick nil
932;; Candidates for bibtex-complete-string. Initialized from 938 "Last value returned by `buffer-modified-tick' when buffer
933;; bibtex-predefined-strings and bibtex-string-files. 939was parsed for keys the last time.")
934(make-variable-buffer-local 'bibtex-strings)
935 940
936(defvar bibtex-reference-keys nil) 941(defvar bibtex-parse-idle-timer nil
937;; Candidates for TAB completion when entering a reference key using 942 "Stores if timer is already installed.")
938;; the minibuffer. Also used for bibtex-complete-key. Initialized in
939;; bibtex-mode and updated for each new entry.
940(make-variable-buffer-local 'bibtex-reference-keys)
941 943
942(defvar bibtex-buffer-last-parsed-tick nil) 944(defvar bibtex-progress-lastperc nil
943;; Remembers the value returned by buffer-modified-tick when buffer 945 "Last reported percentage for the progress message.")
944;; was parsed for keys the last time.
945(make-variable-buffer-local 'bibtex-buffer-last-parsed-tick)
946 946
947(defvar bibtex-parse-idle-timer nil) 947(defvar bibtex-progress-lastmes nil
948;; Stores if timer is already installed 948 "Last reported progress message.")
949 949
950(defvar bibtex-progress-lastperc nil) 950(defvar bibtex-progress-interval nil
951;; Holds the last reported percentage for the progress message 951 "Interval for progress messages.")
952 952
953(defvar bibtex-progress-lastmes nil) 953(defvar bibtex-key-history nil
954;; Holds the last reported progress message 954 "History list for reading keys.")
955 955
956(defvar bibtex-progress-interval nil) 956(defvar bibtex-entry-type-history nil
957;; Holds the chosen interval 957 "Used to store the history list for reading entry types.")
958 958
959(defvar bibtex-key-history nil) 959(defvar bibtex-field-history nil
960;; Used to store the history list for reading keys 960 "History list for reading field names.")
961 961
962(defvar bibtex-entry-type-history nil) 962(defvar bibtex-reformat-previous-options nil
963;; Used to store the history list for reading entry types 963 "Last reformat options given.")
964 964
965(defvar bibtex-field-history nil) 965(defvar bibtex-reformat-previous-reference-keys nil
966;; Used to store the history list for reading field names 966 "Last reformat reference keys option given.")
967 967
968(defvar bibtex-reformat-previous-options nil) 968(defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*"
969;; Used to store the last reformat options given 969 "Regexp matching the name part of a BibTeX field.")
970 970
971(defvar bibtex-reformat-previous-reference-keys nil) 971(defconst bibtex-entry-type (concat "@" bibtex-field-name)
972;; Used to store the last reformat reference keys option given 972 "Regexp matching the type part of a BibTeX entry.")
973
974(defconst bibtex-reference-key "[][a-zA-Z0-9.:;?!`'/*@+|()<>&_^$-]+"
975 "Regexp matching the reference key part of a BibTeX entry.")
976
977(defconst bibtex-field-const "[][a-zA-Z0-9.:;?!`'/*@+=|<>&_^$-]+"
978 "Regexp matching a BibTeX field constant.")
979
980(defconst bibtex-entry-head
981 (concat "^[ \t]*\\("
982 bibtex-entry-type
983 "\\)[ \t]*[({][ \t\n]*\\("
984 bibtex-reference-key
985 "\\)")
986 "Regexp matching the header line of a BibTeX entry.")
987
988(defconst bibtex-entry-maybe-empty-head
989 (concat bibtex-entry-head "?")
990 "Regexp matching the header line of a maybe empty BibTeX entry
991(possibly without reference key).")
992
993(defconst bibtex-type-in-head 1
994 "Regexp subexpression number of the type part in `bibtex-entry-head'.")
995
996(defconst bibtex-key-in-head 2
997 "Regexp subexpression number of the key part in `bibtex-entry-head'.")
998
999(defconst bibtex-entry-postfix "[ \t\n]*,?[ \t\n]*[})]"
1000 "Regexp matching the postfix of a BibTeX entry.")
1001
1002(defvar bibtex-known-entry-type-re
1003 (regexp-opt (mapcar 'car bibtex-entry-field-alist))
1004 "Regexp matching the name of a BibTeX entry type.")
1005
1006(defvar bibtex-valid-entry-re
1007 (concat "@[ \t]*\\(" bibtex-known-entry-type-re "\\)")
1008 "Regexp matching the name of a valid BibTeX entry.")
1009
1010(defvar bibtex-valid-entry-whitespace-re
1011 (concat "[ \t\n]*\\(" bibtex-valid-entry-re "\\)")
1012 "Regexp matching the name of a valid BibTeX entry preceded by whitespace.")
1013
1014(defvar bibtex-any-valid-entry-re
1015 (concat "@[ \t]*"
1016 (regexp-opt (append '("String")
1017 (mapcar 'car bibtex-entry-field-alist))
1018 t))
1019 "Regexp matching the name of any valid BibTeX entry (including string).")
1020
1021
1022(defconst bibtex-empty-field-re "\"\"\\|{}"
1023 "Regexp matching an empty field.")
1024
1025(defconst bibtex-quoted-string-re
1026 (concat "\""
1027 "\\("
1028 "[^\"\\]" ; anything but quote or backslash
1029 "\\|"
1030 "\\("
1031 "\\\\\\(.\\|\n\\)" ; any backslash quoted character
1032 "\\)"
1033 "\\)*"
1034 "\"")
1035 "Regexp matching a field string enclosed by quotes.")
1036
1037(defconst bibtex-font-lock-syntactic-keywords
1038 `((,(concat "^[ \t]*\\(" (substring bibtex-comment-start 0 1) "\\)"
1039 (substring bibtex-comment-start 1) "\\>")
1040 1 '(11))))
1041
1042(defvar bibtex-font-lock-keywords
1043 (list
1044 ;; entry type and reference key
1045 (list bibtex-entry-maybe-empty-head
1046 (list bibtex-type-in-head 'font-lock-function-name-face)
1047 (list bibtex-key-in-head 'font-lock-constant-face nil t))
1048 ;; optional field names (treated as comments)
1049 (list
1050 (concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
1051 1 'font-lock-comment-face)
1052 ;; field names
1053 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1054 1 'font-lock-variable-name-face))
1055 "*Default expressions to highlight in BibTeX mode.")
1056
1057(defvar bibtex-field-name-for-parsing nil
1058 "Temporary variable storing the name string to be parsed by the callback
1059function `bibtex-parse-field-name'.")
1060
1061(defvar bibtex-sort-entry-class-alist
1062 (let ((i -1) alist)
1063 (dolist (class bibtex-sort-entry-class alist)
1064 (setq i (1+ i))
1065 (dolist (entry class)
1066 (push (cons entry i) alist))))
1067 "Alist for the classes of the entry types if the value of
1068`bibtex-maintain-sorted-entries' is `entry-class'.")
973 1069
974 1070
975;; Functions to Parse the BibTeX Entries 1071;; Special support taking care of variants
1072(defvar zmacs-regions)
1073(if (boundp 'mark-active)
1074 (defun bibtex-mark-active ()
1075 ;; In Emacs mark-active indicates if mark is active.
1076 mark-active)
1077 (defun bibtex-mark-active ()
1078 ;; In XEmacs (mark) returns nil when not active.
1079 (if zmacs-regions (mark) (mark t))))
976 1080
977(defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*") 1081(if (fboundp 'run-with-idle-timer)
978;; Regexp defining the name part of a BibTeX field. 1082 ;; timer.el is distributed with Emacs
1083 (fset 'bibtex-run-with-idle-timer 'run-with-idle-timer)
1084 ;; timer.el is not distributed with XEmacs
1085 ;; Notice that this does not (yet) pass the arguments, but they
1086 ;; are not used (yet) in bibtex.el. Fix if needed.
1087 (defun bibtex-run-with-idle-timer (secs repeat function &rest args)
1088 (start-itimer "bibtex" function secs (if repeat secs nil) t)))
979 1089
980(defconst bibtex-entry-type (concat "@" bibtex-field-name)) 1090
981;; Regexp defining the type part of a BibTeX entry. 1091;; Support for hideshow minor mode
1092(defun bibtex-hs-forward-sexp (arg)
1093 "Replacement for `forward-sexp' to be used by `hs-minor-mode'."
1094 (if (< arg 0)
1095 (backward-sexp 1)
1096 (if (looking-at "@\\S(*\\s(")
1097 (progn
1098 (goto-char (match-end 0))
1099 (forward-char -1)
1100 (forward-sexp 1))
1101 (forward-sexp 1))))
982 1102
983(defconst bibtex-reference-key "[][a-zA-Z0-9.:;?!`'/*@+=|()<>&_^$-]+") 1103(add-to-list
984;; Regexp defining the reference key part of a BibTeX entry 1104 'hs-special-modes-alist
1105 '(bibtex-mode "@\\S(*\\s(" "\\s)" nil bibtex-hs-forward-sexp nil))
1106
1107
1108;; Functions to Parse the BibTeX Entries
985 1109
986(defun bibtex-parse-nested-braces (nesting-level) 1110(defun bibtex-parse-nested-braces (nesting-level)
987 "*Starting on an opening brace, find the corresponding closing brace. 1111 "Starting on an opening brace, find the corresponding closing brace.
988When the function is called, NESTING-LEVEL has to be set to `0'." 1112When the function is called, NESTING-LEVEL has to be set to `0'."
989 (cond ((looking-at "{") 1113 (cond ((looking-at "{")
990 (search-forward-regexp "{[^{}]*") 1114 (search-forward-regexp "{[^{}]*")
991 (bibtex-parse-nested-braces (+ nesting-level 1))) 1115 (bibtex-parse-nested-braces (+ nesting-level 1)))
992 ((looking-at "}") 1116 ((looking-at "}")
993 (forward-char 1) 1117 (forward-char 1)
994 (if (= nesting-level 1) 1118 (if (= nesting-level 1)
995 (point) 1119 (point)
996 (search-forward-regexp "[^{}]*") 1120 (search-forward-regexp "[^{}]*")
997 (bibtex-parse-nested-braces (- nesting-level 1)))) 1121 (bibtex-parse-nested-braces (- nesting-level 1))))
998 (t nil))) 1122 (t nil)))
999
1000(defun bibtex-parse-field-string-braced ()
1001 "*Parse a field string enclosed by braces.
1002The field string has to be syntactically correct, which means that the number
1003of opening and closing braces has to match. If this is the case, a pair
1004containing the start and end position of the field string is returned, nil
1005otherwise."
1006 (save-match-data
1007 (let ((starting-point (point))
1008 (end-point nil))
1009 (if (looking-at "{")
1010 (setq end-point (bibtex-parse-nested-braces 0)))
1011 (goto-char starting-point)
1012 (if end-point
1013 (cons starting-point end-point)
1014 nil))))
1015
1016(defun bibtex-parse-quoted-string ()
1017 "*Starting on an opening quote, find the corresponding closing quote."
1018 (let ((rx (concat "\""
1019 "\\("
1020 "[^\"\\]" ;; anything but quote or backslash
1021 "\\|"
1022 "\\("
1023 "\\\\\\(.\\|\n\\)" ;; any backslash quoted character
1024 "\\)"
1025 "\\)*"
1026 "\"")))
1027 (if (looking-at rx)
1028 (search-forward-regexp rx nil t)
1029 nil)))
1030
1031(defun bibtex-parse-field-string-quoted ()
1032 "*Parse a field string enclosed by quotes.
1033If a syntactically correct string is found, a pair containing the start and
1034end position of the field string is returned, nil otherwise."
1035 (save-match-data
1036 (let ((starting-point (point))
1037 (end-point nil))
1038 (if (looking-at "\"")
1039 (setq end-point (bibtex-parse-quoted-string)))
1040 (goto-char starting-point)
1041 (if end-point
1042 (cons starting-point end-point)
1043 nil))))
1044 1123
1045(defun bibtex-parse-field-string () 1124(defun bibtex-parse-field-string ()
1046 "*Parse a field string enclosed by braces or quotes. 1125 "Parse a field string enclosed by braces or quotes.
1047If a syntactically correct string is found, a pair containing the start and 1126If a syntactically correct string is found, a pair containing the start and
1048end position of the field string is returned, nil otherwise." 1127end position of the field string is returned, nil otherwise."
1049 (save-match-data 1128 (save-match-data
1050 (let ((starting-point (point)) 1129 (let ((end-point (or (and (looking-at bibtex-quoted-string-re)
1051 (boundaries (or (bibtex-parse-field-string-braced) 1130 (match-end 0))
1052 (bibtex-parse-field-string-quoted)))) 1131 (and (looking-at "{")
1053 (goto-char starting-point) 1132 (save-excursion (bibtex-parse-nested-braces 0))))))
1054 boundaries))) 1133 (if end-point
1055 1134 (cons (point) end-point)))))
1056(defun bibtex-search-forward-field-string (bound)
1057 "*Search forward to find a field string enclosed by braces or quotes.
1058If a syntactically correct string is found, a pair containing the start and
1059end position of the field string is returned, nil otherwise. The search is
1060delimited by BOUND."
1061 (save-match-data
1062 (let ((starting-point (point))
1063 (boundaries nil))
1064 (while (and (not boundaries) (< (point) bound))
1065 (if (search-forward-regexp "[{\"]" bound 'move)
1066 (progn
1067 (goto-char (match-beginning 0))
1068 (let ((temp-boundaries (or (bibtex-parse-field-string-braced)
1069 (bibtex-parse-field-string-quoted))))
1070 (if (and temp-boundaries (<= (cdr temp-boundaries) bound))
1071 (setq boundaries temp-boundaries)
1072 (forward-char 1))))))
1073 (goto-char starting-point)
1074 boundaries)))
1075 1135
1076(defun bibtex-parse-association (parse-lhs parse-rhs) 1136(defun bibtex-parse-association (parse-lhs parse-rhs)
1077 "*Parse a string of the format <left hand side = right-hand-side>. 1137 "Parse a string of the format <left-hand-side = right-hand-side>.
1078The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding 1138The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
1079substrings. These functions are expected to return nil if parsing is not 1139substrings. These functions are expected to return nil if parsing is not
1080successfull. If both functions return non-nil, a pair containing the returned 1140successfull. If both functions return non-nil, a pair containing the returned
1081values of the functions PARSE-LHS and PARSE-RHSis returned." 1141values of the functions PARSE-LHS and PARSE-RHS is returned."
1082 (save-match-data 1142 (save-match-data
1083 (let ((starting-point (point)) 1143 (save-excursion
1084 (left (funcall parse-lhs)) 1144 (let (left right)
1085 (right nil)) 1145 (if (and (setq left (funcall parse-lhs))
1086 (if (and left (looking-at "[ \t\n]*=[ \t\n]*")) 1146 (looking-at "[ \t\n]*=[ \t\n]*")
1087 (progn 1147 (goto-char (match-end 0))
1088 (goto-char (match-end 0)) 1148 (setq right (funcall parse-rhs)))
1089 (setq right (funcall parse-rhs)))) 1149 (cons left right))))))
1090 (goto-char starting-point)
1091 (if (and left right)
1092 (cons left right)
1093 nil))))
1094
1095(defvar bibtex-field-name-for-parsing nil)
1096;; Temporary variable storing the name string to be parsed by the callback
1097;; function bibtex-parse-field-name.
1098(make-variable-buffer-local 'bibtex-field-name-for-parsing)
1099 1150
1100(defun bibtex-parse-field-name () 1151(defun bibtex-parse-field-name ()
1101 "*Parse the field name stored in bibtex-field-name-for-parsing. 1152 "Parse the field name stored in `bibtex-field-name-for-parsing'.
1102If the field name is found, return a triple consisting of the position of the 1153If the field name is found, return a triple consisting of the position of the
1103very first character of the match, the actual starting position of the name 1154very first character of the match, the actual starting position of the name
1104part and end position of the match." 1155part and end position of the match. Move point to end of field name."
1105 (if (looking-at ",[ \t\n]*") 1156 (if (looking-at ",[ \t\n]*")
1106 (let ((start (point))) 1157 (let ((start (point)))
1107 (goto-char (match-end 0)) 1158 (goto-char (match-end 0))
1108 (if (looking-at bibtex-field-name-for-parsing) 1159 (when (looking-at bibtex-field-name-for-parsing)
1109 (let ((boundaries (list start (match-beginning 0) (match-end 0)))) 1160 (goto-char (match-end 0))
1110 (goto-char (match-end 0)) 1161 (list start (match-beginning 0) (match-end 0))))))
1111 boundaries)))))
1112
1113(defconst bibtex-field-const "[][a-zA-Z0-9.:;?!`'/*@+=|<>&_^$-]+")
1114;; Regexp defining a bibtex field constant
1115 1162
1116(defun bibtex-parse-field-text () 1163(defun bibtex-parse-field-text ()
1117 "*Parse the text part of a BibTeX field. 1164 "Parse the text part of a BibTeX field.
1118The text part is either a string, or an empty string, or a constant followed 1165The text part is either a string, or an empty string, or a constant followed
1119by one or more <# (string|constant)> pairs. If a syntactically correct text 1166by one or more <# (string|constant)> pairs. If a syntactically correct text
1120is found, a pair containing the start and end position of the text is 1167is found, a pair containing the start and end position of the text is
1121returned, nil otherwise." 1168returned, nil otherwise. Move point to end of field text."
1122 (let ((starting-point (point)) 1169 (let ((starting-point (point))
1123 (end-point nil) 1170 end-point failure boundaries)
1124 (failure nil)) 1171 (while (and (not end-point)
1125 (while (and (not end-point) (not failure)) 1172 (not failure))
1126 (if (looking-at bibtex-field-const) 1173 (cond ((looking-at bibtex-field-const)
1127 (goto-char (match-end 0)) 1174 (goto-char (match-end 0)))
1128 (let ((boundaries (bibtex-parse-field-string))) 1175 ((setq boundaries (bibtex-parse-field-string))
1129 (if boundaries 1176 (goto-char (cdr boundaries)))
1130 (goto-char (cdr boundaries)) 1177 ((setq failure t)))
1131 (setq failure t))))
1132 (if (not (looking-at "[ \t\n]*#[ \t\n]*")) 1178 (if (not (looking-at "[ \t\n]*#[ \t\n]*"))
1133 (setq end-point (point)) 1179 (setq end-point (point))
1134 (goto-char (match-end 0)))) 1180 (goto-char (match-end 0))))
1135 (if (and (not failure) end-point) 1181 (if (and (not failure)
1136 (cons starting-point end-point) 1182 end-point)
1137 nil))) 1183 (cons starting-point end-point))))
1138 1184
1139(defun bibtex-parse-field (name) 1185(defun bibtex-parse-field (name)
1140 "*Parse a BibTeX field of regexp NAME. 1186 "Parse a BibTeX field of regexp NAME.
1141If a syntactically correct field is found, a pair containing the boundaries of 1187If a syntactically correct field is found, a pair containing the boundaries of
1142the name and text parts of the field is returned." 1188the name and text parts of the field is returned."
1143 (setq bibtex-field-name-for-parsing name) 1189 (let ((bibtex-field-name-for-parsing name))
1144 (bibtex-parse-association 'bibtex-parse-field-name 1190 (bibtex-parse-association 'bibtex-parse-field-name
1145 'bibtex-parse-field-text)) 1191 'bibtex-parse-field-text)))
1146 1192
1147(defun bibtex-search-forward-field (name bound) 1193(defun bibtex-search-forward-field (name &optional bound)
1148 "*Search forward to find a field of name NAME. 1194 "Search forward to find a field of name NAME.
1149If a syntactically correct field is found, a pair containing the boundaries of 1195If a syntactically correct field is found, a pair containing the boundaries of
1150the name and text parts of the field is returned. The search is limited by 1196the name and text parts of the field is returned. The search is limited by
1151BOUND." 1197optional arg BOUND. Do not move point."
1152 (save-match-data 1198 (save-match-data
1153 (setq bibtex-field-name-for-parsing name) 1199 (save-excursion
1154 (let ((starting-point (point)) 1200 (unless bound (setq bound (point-max)))
1155 (boundaries nil)) 1201 (let ((case-fold-search t)
1156 (while (and (not boundaries) 1202 (bibtex-field-name-for-parsing name)
1157 (< (point) bound) 1203 boundaries temp-boundaries)
1158 (search-forward "," bound t)) 1204 (while (and (not boundaries)
1159 (goto-char (match-beginning 0)) 1205 (< (point) bound)
1160 (let ((temp-boundaries 1206 (search-forward "," bound t))
1161 (bibtex-parse-association 'bibtex-parse-field-name 1207 (goto-char (match-beginning 0))
1162 'bibtex-parse-field-text))) 1208 (if (and (setq temp-boundaries
1163 (if (and temp-boundaries (<= (cdr (cdr temp-boundaries)) bound)) 1209 (bibtex-parse-association 'bibtex-parse-field-name
1164 (setq boundaries temp-boundaries) 1210 'bibtex-parse-field-text))
1165 (forward-char 1)))) 1211 (<= (cddr temp-boundaries) bound))
1166 (goto-char starting-point) 1212 (setq boundaries temp-boundaries)
1167 boundaries))) 1213 (forward-char 1)))
1168 1214 boundaries))))
1169(defun bibtex-search-backward-field (name bound) 1215
1170 "*Search backward to find a field of name NAME. 1216(defun bibtex-search-backward-field (name &optional bound)
1217 "Search backward to find a field of name NAME.
1171If a syntactically correct field is found, a pair containing the boundaries of 1218If a syntactically correct field is found, a pair containing the boundaries of
1172the name and text parts of the field is returned. The search is limited by 1219the name and text parts of the field is returned. The search is limited by
1173BOUND." 1220optional arg BOUND. Do not move point."
1174 (save-match-data 1221 (save-match-data
1175 (setq bibtex-field-name-for-parsing name) 1222 (save-excursion
1176 (let ((starting-point (point)) 1223 (unless bound (setq bound (point-min)))
1177 (boundaries nil)) 1224 (let ((case-fold-search t)
1178 (while (and (not boundaries) 1225 (bibtex-field-name-for-parsing name)
1179 (>= (point) bound) 1226 boundaries temp-boundaries)
1180 (search-backward "," bound t)) 1227 (while (and (not boundaries)
1181 (let ((temp-boundaries 1228 (>= (point) bound)
1182 (bibtex-parse-association 'bibtex-parse-field-name 1229 (search-backward "," bound t))
1183 'bibtex-parse-field-text))) 1230 (if (setq temp-boundaries
1184 (if temp-boundaries 1231 (bibtex-parse-association 'bibtex-parse-field-name
1185 (setq boundaries temp-boundaries)))) 1232 'bibtex-parse-field-text))
1186 (goto-char starting-point) 1233 (setq boundaries temp-boundaries)))
1187 boundaries))) 1234 boundaries))))
1188 1235
1189(defun bibtex-start-of-field (bounds) 1236(defun bibtex-start-of-field (bounds)
1190 (car (car bounds))) 1237 (nth 0 (car bounds)))
1191(defun bibtex-end-of-field (bounds)
1192 (cdr (cdr bounds)))
1193(defun bibtex-start-of-name-in-field (bounds) 1238(defun bibtex-start-of-name-in-field (bounds)
1194 (car (cdr (car bounds)))) 1239 (nth 1 (car bounds)))
1195(defun bibtex-end-of-name-in-field (bounds) 1240(defun bibtex-end-of-name-in-field (bounds)
1196 (car (cdr (cdr (car bounds))))) 1241 (nth 2 (car bounds)))
1242(defun bibtex-end-of-field (bounds)
1243 (cddr bounds))
1197(defun bibtex-start-of-text-in-field (bounds) 1244(defun bibtex-start-of-text-in-field (bounds)
1198 (car (cdr bounds))) 1245 (cadr bounds))
1199(defun bibtex-end-of-text-in-field (bounds) 1246(defun bibtex-end-of-text-in-field (bounds)
1200 (cdr (cdr bounds))) 1247 (cddr bounds))
1248
1249(defun bibtex-name-in-field (bounds)
1250 "Get content of name in BibTeX field defined via BOUNDS."
1251 (buffer-substring-no-properties (nth 1 (car bounds))
1252 (nth 2 (car bounds))))
1253
1254(defun bibtex-text-in-field-bounds (bounds &optional remove-delim)
1255 "Get content of text in BibTeX field defined via BOUNDS.
1256If optional arg REMOVE-DELIM is non-nil remove enclosing field delimiters
1257if present."
1258 (let ((content (buffer-substring-no-properties (cadr bounds)
1259 (cddr bounds))))
1260 (if (and remove-delim
1261 (string-match "\\`[{\"]\\(.*\\)[}\"]\\'" content))
1262 (substring content (match-beginning 1) (match-end 1))
1263 content)))
1264
1265(defun bibtex-text-in-field (field &optional follow-crossref)
1266 "Get content of field FIELD of current BibTeX entry. Return nil if not found.
1267If optional arg FOLLOW-CROSSREF is non-nil, follow crossref."
1268 (save-excursion
1269 (save-restriction
1270 (bibtex-narrow-to-entry)
1271 (goto-char (point-min))
1272 (let ((bounds (bibtex-search-forward-field field))
1273 crossref-field)
1274 (cond (bounds (bibtex-text-in-field-bounds bounds t))
1275 ((and follow-crossref
1276 (progn (goto-char (point-min))
1277 (setq bounds (bibtex-search-forward-field
1278 "\\(OPT\\)?crossref"))))
1279 (setq crossref-field (bibtex-text-in-field-bounds bounds t))
1280 (widen)
1281 (if (bibtex-find-crossref crossref-field)
1282 ;; Do not pass FOLLOW-CROSSREF because we want
1283 ;; to follow crossrefs only one level of recursion.
1284 (bibtex-text-in-field field))))))))
1201 1285
1202(defun bibtex-parse-string-prefix () 1286(defun bibtex-parse-string-prefix ()
1203 "*Parse the prefix part of a bibtex string, including the reference key. 1287 "Parse the prefix part of a BibTeX string entry, including reference key.
1204If the string prefix is found, return a triple consisting of the position of 1288If the string prefix is found, return a triple consisting of the position of
1205the very first character of the match, the actual starting position of the 1289the very first character of the match, the actual starting position of the
1206reference key and the end position of the match." 1290reference key and the end position of the match."
1207 (let* ((case-fold-search t)) 1291 (let ((case-fold-search t))
1208 (if (looking-at "^[ \t]*@string[ \t\n]*[({][ \t\n]*") 1292 (if (looking-at "^[ \t]*@string[ \t\n]*[({][ \t\n]*")
1209 (let ((start (point))) 1293 (let ((start (point)))
1210 (goto-char (match-end 0)) 1294 (goto-char (match-end 0))
1211 (if (looking-at bibtex-reference-key) 1295 (when (looking-at bibtex-reference-key)
1212 (let ((boundaries (list start (match-beginning 0) (match-end 0)))) 1296 (goto-char (match-end 0))
1213 (goto-char (match-end 0)) 1297 (list start
1214 boundaries)))))) 1298 (match-beginning 0)
1299 (match-end 0)))))))
1215 1300
1216(defun bibtex-parse-string-postfix () 1301(defun bibtex-parse-string-postfix ()
1217 "*Parse the postfix part of a bibtex string, including the text. 1302 "Parse the postfix part of a BibTeX string entry, including the text.
1218If the string postfix is found, return a triple consisting of the position of 1303If the string postfix is found, return a triple consisting of the position of
1219the actual starting and ending position of the text and the very last 1304the actual starting and ending position of the text and the very last
1220character of the string entry." 1305character of the string entry. Move point past BibTeX string entry."
1221 (let* ((case-fold-search t) 1306 (let* ((case-fold-search t)
1222 (text-boundaries (bibtex-parse-field-text))) 1307 (text-boundaries (bibtex-parse-field-text)))
1223 (if text-boundaries 1308 (when text-boundaries
1224 (progn 1309 (goto-char (cdr text-boundaries))
1225 (goto-char (cdr text-boundaries)) 1310 (when (looking-at "[ \t\n]*[})]")
1226 (if (looking-at "[ \t\n]*[})]") 1311 (goto-char (match-end 0))
1227 (let ((boundaries (list (car text-boundaries) 1312 (list (car text-boundaries)
1228 (cdr text-boundaries) 1313 (cdr text-boundaries)
1229 (match-end 0)))) 1314 (match-end 0))))))
1230 (goto-char (match-end 0))
1231 boundaries))))))
1232 1315
1233(defun bibtex-parse-string () 1316(defun bibtex-parse-string ()
1234 "*Parse a BibTeX string entry. 1317 "Parse a BibTeX string entry.
1235If a syntactically correct entry is found, a pair containing the boundaries of 1318If a syntactically correct entry is found, a pair containing the boundaries of
1236the reference key and text parts of the entry is returned." 1319the reference key and text parts of the entry is returned.
1320Move point past BibTeX string entry."
1237 (bibtex-parse-association 'bibtex-parse-string-prefix 1321 (bibtex-parse-association 'bibtex-parse-string-prefix
1238 'bibtex-parse-string-postfix)) 1322 'bibtex-parse-string-postfix))
1239 1323
1240(defun bibtex-search-forward-string () 1324(defun bibtex-search-forward-string ()
1241 "*Search forward to find a bibtex string entry. 1325 "Search forward to find a BibTeX string entry.
1242If a syntactically correct entry is found, a pair containing the boundaries of 1326If a syntactically correct entry is found, a pair containing the boundaries of
1243the reference key and text parts of the string is returned." 1327the reference key and text parts of the string is returned. Do not move point."
1244 (save-match-data 1328 (save-excursion
1245 (let* ((case-fold-search t) 1329 (save-match-data
1246 (starting-point (point)) 1330 (let ((case-fold-search t)
1247 (boundaries nil)) 1331 boundaries)
1248 (while (and (not boundaries) 1332 (while (and (not boundaries)
1249 (search-forward-regexp 1333 (search-forward-regexp
1250 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t)) 1334 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t))
1251 (goto-char (match-beginning 0)) 1335 (goto-char (match-beginning 0))
1252 (let ((temp-boundaries (bibtex-parse-string))) 1336 (unless (setq boundaries (bibtex-parse-string))
1253 (if temp-boundaries 1337 (forward-char 1)))
1254 (setq boundaries temp-boundaries) 1338 boundaries))))
1255 (forward-char 1))))
1256 (goto-char starting-point)
1257 boundaries)))
1258 1339
1259(defun bibtex-search-backward-string () 1340(defun bibtex-search-backward-string ()
1260 "*Search backward to find a bibtex string entry. 1341 "Search backward to find a BibTeX string entry.
1261If a syntactically correct entry is found, a pair containing the boundaries of 1342If a syntactically correct entry is found, a pair containing the boundaries of
1262the reference key and text parts of the field is returned." 1343the reference key and text parts of the field is returned. Do not move point."
1263 (save-match-data 1344 (save-excursion
1264 (let* ((case-fold-search t) 1345 (save-match-data
1265 (starting-point (point)) 1346 (let ((case-fold-search t)
1266 (boundaries nil)) 1347 boundaries)
1267 (while (and (not boundaries) 1348 (while (and (not boundaries)
1268 (search-backward-regexp 1349 (search-backward-regexp
1269 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t)) 1350 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t))
1270 (goto-char (match-beginning 0)) 1351 (goto-char (match-beginning 0))
1271 (let ((temp-boundaries (bibtex-parse-string))) 1352 (setq boundaries (bibtex-parse-string)))
1272 (if temp-boundaries 1353 boundaries))))
1273 (setq boundaries temp-boundaries)))) 1354
1274 (goto-char starting-point) 1355(defun bibtex-reference-key-in-string (bounds)
1275 boundaries))) 1356 (buffer-substring-no-properties (nth 1 (car bounds))
1357 (nth 2 (car bounds))))
1358
1359(defun bibtex-text-in-string (bounds &optional remove-delim)
1360 "Get content of text in BibTeX string field defined via BOUNDS.
1361If optional arg REMOVE-DELIM is non-nil remove enclosing field
1362delimiters if present."
1363 (let ((content (buffer-substring-no-properties (nth 0 (cdr bounds))
1364 (nth 1 (cdr bounds)))))
1365 (if (and remove-delim
1366 (string-match "\\`{\\(.*\\)}\\'" content))
1367 (substring content (match-beginning 1) (match-end 1))
1368 content)))
1276 1369
1277(defun bibtex-end-of-string (bounds)
1278 (car (cdr (cdr (cdr bounds)))))
1279(defun bibtex-start-of-reference-key-in-string (bounds)
1280 (car (cdr (car bounds))))
1281(defun bibtex-end-of-reference-key-in-string (bounds)
1282 (car (cdr (cdr (car bounds)))))
1283(defun bibtex-start-of-text-in-string (bounds) 1370(defun bibtex-start-of-text-in-string (bounds)
1284 (car (cdr bounds))) 1371 (nth 0 (cdr bounds)))
1285(defun bibtex-end-of-text-in-string (bounds) 1372(defun bibtex-end-of-text-in-string (bounds)
1286 (car (cdr (cdr bounds)))) 1373 (nth 1 (cdr bounds)))
1287 1374(defun bibtex-end-of-string (bounds)
1288(defconst bibtex-entry-head 1375 (nth 2 (cdr bounds)))
1289 (concat "^[ \t]*\\("
1290 bibtex-entry-type
1291 "\\)[ \t]*[({][ \t\n]*\\("
1292 bibtex-reference-key
1293 "\\)"))
1294;; Regexp defining format of the header line of a BibTeX entry.
1295
1296(defconst bibtex-entry-maybe-empty-head
1297 (concat bibtex-entry-head "?"))
1298;; Regexp defining format of the header line of a maybe empty
1299;; BibTeX entry (possibly without reference key).
1300
1301(defconst bibtex-type-in-head 1)
1302;; The regexp subexpression number of the type part in
1303;; bibtex-entry-head.
1304
1305(defconst bibtex-key-in-head 2)
1306;; The regexp subexpression number of the key part in
1307;; bibtex-entry-head.
1308
1309(defconst bibtex-entry-postfix "[ \t\n]*,?[ \t\n]*[})]")
1310;; Regexp defining the postfix of a bibtex entry
1311
1312(defconst bibtex-key-in-entry bibtex-key-in-head)
1313;; The regexp subexpression number of the key part in a bibtex entry.
1314 1376
1315(defvar bibtex-font-lock-keywords 1377(defun bibtex-type-in-head ()
1316 (list 1378 "Extract BibTeX type in head."
1317 ;; entry type and reference key 1379 ;; ignore @
1318 (list bibtex-entry-maybe-empty-head 1380 (buffer-substring-no-properties (1+ (match-beginning bibtex-type-in-head))
1319 (list bibtex-type-in-head 'font-lock-function-name-face) 1381 (match-end bibtex-type-in-head)))
1320 (list bibtex-key-in-head 'font-lock-constant-face nil t))
1321 ;; comments
1322 (list
1323 (concat "^\\([ \t]*" bibtex-comment-start ".*\\)$")
1324 1 'font-lock-comment-face)
1325 ;; optional field names (treated as comments)
1326 (list
1327 (concat "^[ \t]*\\(OPT" bibtex-field-name "\\)[ \t]*=")
1328 1 'font-lock-comment-face)
1329 ;; field names
1330 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1331 1 'font-lock-variable-name-face)
1332 "*Default expressions to highlight in BibTeX mode."))
1333;; now all needed patterns are defined
1334 1382
1383(defun bibtex-key-in-head (&optional empty)
1384 "Extract BibTeX key in head. Return optional arg EMPTY if key is empty."
1385 (if (match-beginning bibtex-key-in-head)
1386 (buffer-substring-no-properties (match-beginning bibtex-key-in-head)
1387 (match-end bibtex-key-in-head))
1388 empty))
1335 1389
1336
1337;; Helper Functions 1390;; Helper Functions
1338 1391
1339(defun bibtex-delete-whitespace () 1392(defun bibtex-delete-whitespace ()
1340 ;; Delete all whitespace starting at point 1393 "Delete all whitespace starting at point."
1341 (if (looking-at "[ \t\n]+") 1394 (if (looking-at "[ \t\n]+")
1342 (delete-region (point) (match-end 0)))) 1395 (delete-region (point) (match-end 0))))
1343 1396
1344(defun bibtex-current-line () 1397(defun bibtex-current-line ()
1345 ;; this computes line number of point regardless whether the buffer 1398 "Compute line number of point regardless whether the buffer is narrowed."
1346 ;; is narrowed
1347 (+ (count-lines 1 (point)) 1399 (+ (count-lines 1 (point))
1348 (if (equal (current-column) 0) 1 0))) 1400 (if (equal (current-column) 0) 1 0)))
1349 1401
1350(defun bibtex-member-of-regexp (string list) 1402(defun bibtex-member-of-regexp (string list)
1351 ;; Return non-nil if STRING is exactly matched by an element of 1403 "Return non-nil if STRING is exactly matched by an element of LIST.
1352 ;; LIST. The value is actually the tail of LIST whose 1404The value is actually the tail of LIST whose car matches STRING."
1353 ;; car matches STRING. 1405 (let (case-fold-search)
1354 (let* (case-fold-search) 1406 (while (and list
1355 (while 1407 (not (string-match (concat "^" (car list) "$") string)))
1356 (and list (not (string-match (concat "^" (car list) "$") string)))
1357 (setq list (cdr list))) 1408 (setq list (cdr list)))
1358 list)) 1409 list))
1359 1410
1360(defun bibtex-assoc-of-regexp (string alist) 1411(defun bibtex-assoc-of-regexp (string alist)
1361 ;; Return non-nil if STRING is exactly matched by the car of an 1412 "Return non-nil if STRING is exactly matched by the car of an
1362 ;; element of LIST (case ignored). The value is actually the element 1413element of ALIST (case ignored). The value is actually the element
1363 ;; of LIST whose car matches STRING. 1414of LIST whose car matches STRING."
1364 (let* ((case-fold-search t)) 1415 (let ((case-fold-search t))
1365 (while 1416 (while (and alist
1366 (and alist 1417 (not (string-match (concat "^" (caar alist) "$") string)))
1367 (not (string-match (concat "^" (car (car alist)) "$") string)))
1368 (setq alist (cdr alist))) 1418 (setq alist (cdr alist)))
1369 (car alist))) 1419 (car alist)))
1370 1420
1371(defun bibtex-skip-to-valid-entry (&optional backward) 1421(defun bibtex-skip-to-valid-entry (&optional backward)
1372 ;; If not at beginning of valid BibTeX entry, move to beginning of 1422 "Unless at beginning of a valid BibTeX entry, move point to beginning of the
1373 ;; the next valid one. With argument backward non-nil, move backward 1423next valid one. With optional argument BACKWARD non-nil, move backward to
1374 ;; to beginning of previous valid one. A valid entry is a 1424beginning of previous valid one. A valid entry is a syntactical correct one
1375 ;; syntactical correct one with type contained in 1425with type contained in `bibtex-entry-field-alist' or, if
1376 ;; bibtex-entry-field-alist or, if bibtex-sort-ignore-string-entries 1426`bibtex-sort-ignore-string-entries' is nil, a syntactical correct string
1377 ;; is nil, a syntactical correct string entry. 1427entry. Return buffer position if a valid entry is found, nil otherwise."
1378 (let* ((case-fold-search t) 1428 (let ((case-fold-search t)
1379 (valid-bibtex-entry 1429 found)
1380 (concat
1381 "@[ \t]*\\("
1382 (mapconcat
1383 (lambda (type)
1384 (concat "\\(" (car type) "\\)"))
1385 bibtex-entry-field-alist
1386 "\\|")
1387 "\\)"))
1388 found)
1389 (while (and (not found) 1430 (while (and (not found)
1390 (not (if backward 1431 (not (if backward (bobp) (eobp))))
1391 (bobp)
1392 (eobp))))
1393 (let ((pnt (point))) 1432 (let ((pnt (point)))
1394 (cond 1433 (cond ((or (and (looking-at bibtex-valid-entry-re)
1395 ((looking-at valid-bibtex-entry) 1434 (bibtex-search-entry nil nil t)
1396 (if (and 1435 (equal (match-beginning 0) pnt))
1397 (bibtex-search-entry nil nil t) 1436 (and (not bibtex-sort-ignore-string-entries)
1398 (equal (match-beginning 0) pnt)) 1437 (bibtex-parse-string)))
1399 (setq found t))) 1438 (setq found pnt)
1400 ((and (not bibtex-sort-ignore-string-entries) 1439 (goto-char pnt))
1401 (bibtex-parse-string)) 1440 (backward
1402 (setq found t))) 1441 (goto-char (1- pnt))
1403 (if found 1442 (if (re-search-backward "^[ \t]*\\(@\\)" nil 'move)
1404 (goto-char pnt) 1443 (goto-char (match-beginning 1))))
1405 (if backward 1444 (t (goto-char (1+ pnt))
1406 (progn 1445 (if (re-search-forward "^[ \t]*@" nil 'move)
1407 (goto-char (1- pnt)) 1446 (forward-char -1))))))
1408 (if (re-search-backward "^[ \t]*\\(@\\)" nil 'move) 1447 found))
1409 (goto-char (match-beginning 1))))
1410 (goto-char (1+ pnt))
1411 (if (re-search-forward "^[ \t]*@" nil 'move)
1412 (forward-char -1))))))))
1413 1448
1414(defun bibtex-map-entries (fun) 1449(defun bibtex-map-entries (fun)
1415 ;; Call FUN for each BibTeX entry starting with the current. Do this 1450 "Call FUN for each BibTeX entry starting with the current.
1416 ;; to the end of the file. FUN is called with one argument, the key 1451Do this to the end of the file. FUN is called with one argument, the key
1417 ;; of the entry, and with point inside the entry. If 1452of the entry, and with point inside the entry.
1418 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be 1453If `bibtex-sort-ignore-string-entries' is non-nil, FUN will not be
1419 ;; called for @String entries. 1454called for @String entries."
1420 (let* ((case-fold-search t)) 1455 (let ((case-fold-search t))
1421 (bibtex-beginning-of-entry) 1456 (bibtex-beginning-of-entry)
1422 (while (re-search-forward bibtex-entry-maybe-empty-head nil t) 1457 (while (re-search-forward bibtex-entry-maybe-empty-head nil t)
1423 (let ((pnt (point)) 1458 (save-excursion
1424 (entry-type 1459 (let ((entry-type (downcase (bibtex-type-in-head)))
1425 (downcase (buffer-substring-no-properties 1460 (key (bibtex-key-in-head "")))
1426 (1+ (match-beginning bibtex-type-in-head)) 1461 (if (or (and (not bibtex-sort-ignore-string-entries)
1427 (match-end bibtex-type-in-head)))) 1462 (string-equal "string" entry-type))
1428 (reference-key 1463 (assoc-ignore-case entry-type bibtex-entry-field-alist))
1429 (if (match-beginning bibtex-key-in-head) 1464 (funcall fun key))))
1430 (buffer-substring-no-properties 1465 (bibtex-end-of-entry))))
1431 (match-beginning bibtex-key-in-head)
1432 (match-end bibtex-key-in-head))
1433 "")))
1434 (if (or
1435 (and
1436 (not bibtex-sort-ignore-string-entries)
1437 (string-equal "string" (downcase entry-type)))
1438 (assoc-ignore-case entry-type bibtex-entry-field-alist))
1439 (funcall fun reference-key))
1440 (goto-char pnt)
1441 (bibtex-end-of-entry)))))
1442 1466
1443(defun bibtex-progress-message (&optional flag interval) 1467(defun bibtex-progress-message (&optional flag interval)
1444 ;; echos a message about progress of current buffer 1468 "Echo a message about progress of current buffer.
1445 ;; if flag is a string, the message is initialized (in this case a 1469If FLAG is a string, the message is initialized (in this case a
1446 ;; value for INTERVAL may be given as well (if not this is set to 5)) 1470value for INTERVAL may be given as well (if not this is set to 5)).
1447 ;; if flag is done, the message is deinitialized 1471If FLAG is done, the message is deinitialized.
1448 ;; if flag is absent, a message is echoed if point was incremented 1472If FLAG is absent, a message is echoed if point was incremented
1449 ;; at least INTERVAL percent since last message was echoed 1473at least INTERVAL percent since last message was echoed."
1450 (let* ((size (- (point-max) (point-min))) 1474 (cond ((stringp flag)
1451 (perc (if (= size 0) 1475 (setq bibtex-progress-lastmes flag)
1452 100 1476 (setq bibtex-progress-interval (or interval 5)
1453 (/ (* 100 (- (point) (point-min))) size)))) 1477 bibtex-progress-lastperc 0))
1454 (if (or (and (not flag) 1478 ((equal flag 'done)
1455 (>= perc 1479 (message "%s (done)" bibtex-progress-lastmes)
1456 (+ bibtex-progress-interval bibtex-progress-lastperc))) 1480 (setq bibtex-progress-lastmes nil))
1457 (stringp flag)) 1481 (t
1458 (progn 1482 (let* ((size (- (point-max) (point-min)))
1459 (if (stringp flag) 1483 (perc (if (= size 0)
1460 (progn 1484 100
1461 (setq bibtex-progress-lastmes flag) 1485 (/ (* 100 (- (point) (point-min))) size))))
1462 (if interval 1486 (when (>= perc (+ bibtex-progress-lastperc
1463 (setq bibtex-progress-interval interval) 1487 bibtex-progress-interval))
1464 (setq bibtex-progress-interval 5)))) 1488 (setq bibtex-progress-lastperc perc)
1465 (setq bibtex-progress-lastperc perc) 1489 (message "%s (%d%%)" bibtex-progress-lastmes perc))))))
1466 (message (concat bibtex-progress-lastmes " (%d%%)") perc))
1467 (if (equal flag 'done)
1468 (progn
1469 (message (concat bibtex-progress-lastmes " (done)"))
1470 (setq bibtex-progress-lastmes nil))))))
1471
1472 1490
1473(defun bibtex-field-left-delimiter () 1491(defun bibtex-field-left-delimiter ()
1474 ;; returns a string dependent on bibtex-field-delimiters 1492 "Return a string dependent on `bibtex-field-delimiters'."
1475 (if (equal bibtex-field-delimiters 'braces) 1493 (if (equal bibtex-field-delimiters 'braces)
1476 "{" 1494 "{"
1477 "\"")) 1495 "\""))
1478 1496
1479(defun bibtex-field-right-delimiter () 1497(defun bibtex-field-right-delimiter ()
1480 ;; returns a string dependent on bibtex-field-delimiters 1498 "Return a string dependent on `bibtex-field-delimiters'."
1481 (if (equal bibtex-field-delimiters 'braces) 1499 (if (equal bibtex-field-delimiters 'braces)
1482 "}" 1500 "}"
1483 "\"")) 1501 "\""))
1484 1502
1485(defun bibtex-entry-left-delimiter () 1503(defun bibtex-entry-left-delimiter ()
1486 ;; returns a string dependent on bibtex-field-delimiters 1504 "Return a string dependent on `bibtex-field-delimiters'."
1487 (if (equal bibtex-entry-delimiters 'braces) 1505 (if (equal bibtex-entry-delimiters 'braces)
1488 "{" 1506 "{"
1489 "(")) 1507 "("))
1490 1508
1491(defun bibtex-entry-right-delimiter () 1509(defun bibtex-entry-right-delimiter ()
1492 ;; returns a string dependent on bibtex-field-delimiters 1510 "Return a string dependent on `bibtex-field-delimiters'."
1493 (if (equal bibtex-entry-delimiters 'braces) 1511 (if (equal bibtex-entry-delimiters 'braces)
1494 "}" 1512 "}"
1495 ")")) 1513 ")"))
1496 1514
1497(defun bibtex-search-entry 1515(defun bibtex-search-entry (empty-head &optional bound noerror backward)
1498 (empty-head &optional bound noerror backward) 1516 "A helper function necessary since the failure stack size limit for
1499 ;; A helper function necessary since the failure stack size limit for 1517regexps was reduced in emacs 19.32.
1500 ;; regexps was reduced in emacs 19.32. 1518It searches for a BibTeX entry (maybe without a reference key if
1501 ;; It searches for a BibTeX entry (maybe without a reference key if 1519EMPTY-HEAD is t).
1502 ;; EMPTY-HEAD is t). 1520BOUND and NOERROR are exactly as in `re-search-forward'. If
1503 ;; BOUND and NOERROR are exactly as in re-search-forward. If 1521BACKWARD is non-nil, search is done in reverse direction. After
1504 ;; BACKWARD is non-nil, search is done in reverse direction. After 1522call to this function MATCH-BEGINNING and MATCH-END functions are
1505 ;; call to this function MATCH-BEGINNING and MATCH-END functions are 1523defined, but only for the head part of the entry (especially
1506 ;; defined, but only for the head part of the entry (especially 1524\(match-end 0) just gives the end of the head part)."
1507 ;; (match-end 0) just gives the end of the head part).
1508 (let ((pnt (point)) 1525 (let ((pnt (point))
1509 (prefix (if empty-head 1526 (prefix (if empty-head
1510 bibtex-entry-maybe-empty-head 1527 bibtex-entry-maybe-empty-head
@@ -1516,167 +1533,143 @@ the reference key and text parts of the field is returned."
1516 (setq found (bibtex-search-entry empty-head pnt t))) 1533 (setq found (bibtex-search-entry empty-head pnt t)))
1517 (if found 1534 (if found
1518 (goto-char (match-beginning 0)) 1535 (goto-char (match-beginning 0))
1519 (if (equal noerror nil) 1536 (cond ((equal noerror nil)
1520 ;; yell 1537 ;; yell
1521 (error "Search of BibTeX entry failed")) 1538 (error "Search of BibTeX entry failed"))
1522 (if (equal noerror t) 1539 ((equal noerror t)
1523 ;; don't move 1540 ;; don't move
1524 (goto-char pnt)) 1541 (goto-char pnt)))
1525 nil)) 1542 nil))
1526 (let ((limit (if bound bound (point-max))) 1543 (let ((limit (or bound (point-max)))
1527 md
1528 found) 1544 found)
1529 (while (and (not found) 1545 (while (and (not found)
1530 (re-search-forward prefix bound noerror)) 1546 (re-search-forward prefix bound noerror))
1531 (setq md (match-data)) 1547 (save-match-data
1532 ;; save match-data of prefix regexp 1548 (let ((entry-closer
1533 (let ((entry-closer 1549 (if (save-excursion
1534 (if (save-excursion 1550 (goto-char (match-end bibtex-type-in-head))
1535 (goto-char (match-end bibtex-type-in-head)) 1551 (looking-at "[ \t]*("))
1536 (looking-at "[ \t]*(")) 1552 ;; entry opened with parenthesis
1537 ;; entry opened with parenthesis 1553 ?\)
1538 ")" 1554 ?\}))
1539 "}"))) 1555 (infix-start (point))
1540 (let ((infix-start (point)) 1556 finished bounds)
1541 (finished nil)) 1557 (while (not finished)
1542 (while (not finished) 1558 (skip-chars-forward " \t\n" limit)
1543 (if (and (looking-at "[ \t\n]*") 1559 (if (and (setq bounds (bibtex-parse-field bibtex-field-name))
1544 (<= (match-end 0) limit)) 1560 (<= (bibtex-end-of-field bounds) limit))
1545 (goto-char (match-end 0))) 1561 (setq infix-start (bibtex-end-of-field bounds))
1546 (let ((bounds (bibtex-parse-field bibtex-field-name))) 1562 (setq finished t))
1547 (if (and bounds (<= (bibtex-end-of-field bounds) limit)) 1563 (goto-char infix-start))
1548 (progn 1564 ;; This matches the infix* part. The AND construction assures
1549 (goto-char (bibtex-end-of-field bounds)) 1565 ;; that BOUND is respected.
1550 (setq infix-start (point))) 1566 (when (and (looking-at bibtex-entry-postfix)
1551 (goto-char infix-start) 1567 (eq (char-before (match-end 0)) entry-closer)
1552 (setq finished t))))) 1568 (<= (match-end 0) limit))
1553 ;; This matches the infix* part. The AND construction assures 1569 (goto-char (match-end 0))
1554 ;; that BOUND is respected. 1570 (setq found t)))))
1555 (if (and (looking-at bibtex-entry-postfix)
1556 (string-equal
1557 (buffer-substring-no-properties
1558 (1- (match-end 0)) (match-end 0))
1559 entry-closer)
1560 (<= (match-end 0) limit))
1561 (progn
1562 (re-search-forward bibtex-entry-postfix)
1563 (setq found t)))))
1564 (if found 1571 (if found
1565 (progn 1572 (point)
1566 (set-match-data md) 1573 (cond ((not noerror)
1567 ;; to set match-beginning/end again 1574 ;; yell
1568 (point)) 1575 (error "Search of BibTeX entry failed"))
1569 (if (equal noerror nil) 1576 ((equal noerror t)
1570 ;; yell 1577 ;; don't move
1571 (error "Search of BibTeX entry failed")) 1578 (goto-char pnt)))
1572 (if (equal noerror t)
1573 ;; don't move
1574 (goto-char pnt))
1575 nil))))) 1579 nil)))))
1576 1580
1577(defun bibtex-flash-head () 1581(defun bibtex-flash-head ()
1578 ;; Flash at BibTeX entry head before point, if exists. 1582 "Flash at BibTeX entry head before point, if exists."
1579 (let* ((case-fold-search t) 1583 (let ((case-fold-search t)
1580 flash) 1584 flash)
1581 (cond ((re-search-backward bibtex-entry-head nil t) 1585 (cond ((re-search-backward bibtex-entry-head nil t)
1582 (goto-char (match-beginning bibtex-type-in-head)) 1586 (goto-char (match-beginning bibtex-type-in-head))
1583 (setq flash (match-end bibtex-key-in-entry))) 1587 (setq flash (match-end bibtex-key-in-head)))
1584 (t 1588 (t
1585 (end-of-line) 1589 (end-of-line)
1586 (skip-chars-backward " \t") 1590 (skip-chars-backward " \t")
1587 (setq flash (point)) 1591 (setq flash (point))
1588 (beginning-of-line) 1592 (beginning-of-line)
1589 (skip-chars-forward " \t"))) 1593 (skip-chars-forward " \t")))
1590 (if (pos-visible-in-window-p (point)) 1594 (if (pos-visible-in-window-p (point))
1591 (sit-for 1) 1595 (sit-for 1)
1592 (message "From: %s" 1596 (message "From: %s"
1593 (buffer-substring (point) flash))))) 1597 (buffer-substring (point) flash)))))
1594 1598
1595(defun bibtex-make-optional-field (e-t) 1599(defun bibtex-make-optional-field (e-t)
1596 "Makes an optional field named E-T in current BibTeX entry." 1600 "Make an optional field named E-T in current BibTeX entry."
1597 (if (consp e-t) 1601 (if (consp e-t)
1598 (bibtex-make-field (cons (concat "OPT" (car e-t)) (cdr e-t))) 1602 (bibtex-make-field (cons (concat "OPT" (car e-t)) (cdr e-t)))
1599 (bibtex-make-field (concat "OPT" e-t)))) 1603 (bibtex-make-field (concat "OPT" e-t))))
1600 1604
1601(defun bibtex-move-outside-of-entry () 1605(defun bibtex-move-outside-of-entry ()
1602 ;; Make sure we are outside of a BibTeX entry. 1606 "Make sure point is outside of a BibTeX entry."
1603 (let ((orig-point (point))) 1607 (let ((orig-point (point)))
1604 (bibtex-end-of-entry) 1608 (bibtex-end-of-entry)
1605 (if (< (point) orig-point) 1609 (when (<= (point) orig-point)
1606 ;; We moved backward, so we weren't inside an entry to begin with. 1610 ;; We moved backward, so we weren't inside an entry to begin with.
1607 ;; Leave point at the beginning of a line, and preferably 1611 ;; Leave point at the beginning of a line, and preferably
1608 ;; at the beginning of a paragraph. 1612 ;; at the beginning of a paragraph.
1609 (progn 1613 (goto-char orig-point)
1610 (goto-char orig-point) 1614 (beginning-of-line 1)
1611 (beginning-of-line 1) 1615 (let ((cb (char-before (1- (point)))))
1612 (if (not (= ?\n (char-before (1- (point))))) 1616 (unless (and cb (= ?\n cb))
1613 (progn 1617 (re-search-forward "^[ \t]*[@\n]" nil 'move)
1614 (re-search-forward "^[ \t]*[@\n]" nil 'move) 1618 (backward-char 1))))
1615 (backward-char 1)))))
1616 (skip-chars-forward " \t\n"))) 1619 (skip-chars-forward " \t\n")))
1617 1620
1618(defun bibtex-beginning-of-first-entry () 1621(defun bibtex-beginning-of-first-entry ()
1619 ;; Go to the beginning of the first BibTeX entry in buffer. Return 1622 "Go to the beginning of the first BibTeX entry in buffer. Return point."
1620 ;; point.
1621 (goto-char (point-min)) 1623 (goto-char (point-min))
1622 (if (re-search-forward "^[ \t]*@" nil 'move) 1624 (if (re-search-forward "^[ \t]*@" nil 'move)
1623 (beginning-of-line)) 1625 (beginning-of-line))
1624 (point)) 1626 (point))
1625 1627
1626(defun bibtex-beginning-of-last-entry () 1628(defun bibtex-beginning-of-last-entry ()
1627 ;; Go to the beginning of the last BibTeX entry in buffer. 1629 "Go to the beginning of the last BibTeX entry in buffer."
1628 (goto-char (point-max)) 1630 (goto-char (point-max))
1629 (if (re-search-backward "^[ \t]*@" nil 'move) 1631 (if (re-search-backward "^[ \t]*@" nil 'move)
1630 (beginning-of-line)) 1632 (beginning-of-line))
1631 (point)) 1633 (point))
1632 1634
1633(defun bibtex-inside-field () 1635(defun bibtex-inside-field ()
1634 ;; Try to avoid point being at end of a BibTeX field. 1636 "Try to avoid point being at end of a BibTeX field."
1635 (end-of-line) 1637 (end-of-line)
1636 (skip-chars-backward " \t") 1638 (skip-chars-backward " \t")
1637 (cond ((= (preceding-char) ?,) 1639 (cond ((= (preceding-char) ?,)
1638 (forward-char -2))) 1640 (forward-char -2)))
1639 (cond ((or 1641 (cond ((or (= (preceding-char) ?})
1640 (= (preceding-char) ?}) 1642 (= (preceding-char) ?\"))
1641 (= (preceding-char) ?\"))
1642 (forward-char -1)))) 1643 (forward-char -1))))
1643 1644
1644(defun bibtex-enclosing-field (&optional noerr) 1645(defun bibtex-enclosing-field (&optional noerr)
1645 ;; Search for BibTeX field enclosing point. Point moves to end of 1646 "Search for BibTeX field enclosing point. Point moves to end of field.
1646 ;; field. Use match-beginning and match-end to parse the field. If 1647Use `match-beginning' and `match-end' to parse the field. If NOERR is non-nil,
1647 ;; NOERR is non-nil, no error is signalled. In this case, t is 1648no error is signalled. In this case, bounds are returned on success,
1648 ;; returned on success, nil otherwise. 1649nil otherwise."
1649 (let* ((case-fold-search t) 1650 (let* ((case-fold-search t)
1650 (old-point (point)) 1651 (old-point (point))
1651 (boe (bibtex-beginning-of-entry))) 1652 (boe (save-excursion (bibtex-beginning-of-entry)))
1652 (goto-char old-point) 1653 (bounds (bibtex-search-backward-field bibtex-field-name boe)))
1653 (let ((bounds (bibtex-search-backward-field bibtex-field-name boe))) 1654 (if (and bounds
1654 (if (and bounds 1655 (<= (bibtex-start-of-field bounds) old-point)
1655 (<= (bibtex-start-of-field bounds) old-point) 1656 (>= (bibtex-end-of-field bounds) old-point))
1656 (>= (bibtex-end-of-field bounds) old-point)) 1657 bounds
1657 bounds 1658 (unless noerr
1658 (if noerr 1659 (error "Can't find enclosing BibTeX field")))))
1659 nil
1660 (error "Can't find enclosing BibTeX field"))))))
1661 1660
1662(defun bibtex-enclosing-entry-maybe-empty-head () 1661(defun bibtex-enclosing-entry-maybe-empty-head ()
1663 ;; Search for BibTeX entry enclosing point. Point moves to 1662 "Search for BibTeX entry enclosing point. Point moves to end of entry.
1664 ;; end of entry. Beginning (but not end) of entry is given 1663Beginning (but not end) of entry is given by (`match-beginning' 0)."
1665 ;; by (match-beginning 0). 1664 (let ((case-fold-search t)
1666 (let* ((case-fold-search t) 1665 (old-point (point)))
1667 (old-point (point))) 1666 (unless (re-search-backward bibtex-entry-maybe-empty-head nil t)
1668 (if (not 1667 (error "Can't find enclosing BibTeX entry")
1669 (re-search-backward 1668 (goto-char old-point))
1670 bibtex-entry-maybe-empty-head nil t))
1671 (progn
1672 (error "Can't find enclosing BibTeX entry")
1673 (goto-char old-point)))
1674 (goto-char (match-beginning bibtex-type-in-head)) 1669 (goto-char (match-beginning bibtex-type-in-head))
1675 (if (not 1670 (unless (bibtex-search-entry t nil t)
1676 (bibtex-search-entry t nil t)) 1671 (error "Can't find enclosing BibTeX entry")
1677 (progn 1672 (goto-char old-point))))
1678 (error "Can't find enclosing BibTeX entry")
1679 (goto-char old-point)))))
1680 1673
1681(defun bibtex-insert-current-kill (n) 1674(defun bibtex-insert-current-kill (n)
1682 (if (not bibtex-last-kill-command) 1675 (if (not bibtex-last-kill-command)
@@ -1687,10 +1680,9 @@ the reference key and text parts of the field is returned."
1687 (kryp (if (equal bibtex-last-kill-command 'field) 1680 (kryp (if (equal bibtex-last-kill-command 'field)
1688 'bibtex-field-kill-ring-yank-pointer 1681 'bibtex-field-kill-ring-yank-pointer
1689 'bibtex-entry-kill-ring-yank-pointer)) 1682 'bibtex-entry-kill-ring-yank-pointer))
1690 (ARGth-kill-element 1683 (ARGth-kill-element (nthcdr (mod (- n (length (eval kryp)))
1691 (nthcdr 1684 (length (eval kr)))
1692 (mod (- n (length (eval kryp))) (length (eval kr))) 1685 (eval kr)))
1693 (eval kr)))
1694 (current (car (set kryp ARGth-kill-element)))) 1686 (current (car (set kryp ARGth-kill-element))))
1695 (cond 1687 (cond
1696 ((equal bibtex-last-kill-command 'field) 1688 ((equal bibtex-last-kill-command 'field)
@@ -1708,477 +1700,340 @@ the reference key and text parts of the field is returned."
1708 (message "Mark set") 1700 (message "Mark set")
1709 (insert (elt current 1))) 1701 (insert (elt current 1)))
1710 (t 1702 (t
1711 (error 1703 (error "Unknown tag field: %s. Please submit a bug report"
1712 "Unknown tag field: %s. Please submit a bug report" 1704 bibtex-last-kill-command))))))
1713 bibtex-last-kill-command))))))
1714
1715(defun bibtex-format-field-delimiters (start stop)
1716 "*Replaces delimiters for field strings between START and STOP.
1717If the current delimiters equal the new delimiters, the buffer is not
1718changed."
1719 (goto-char start)
1720 (let ((boundaries (bibtex-search-forward-field-string stop)))
1721 (while boundaries
1722 (goto-char (car boundaries))
1723 (if (not (looking-at (bibtex-field-left-delimiter)))
1724 (progn
1725 (delete-char 1)
1726 (insert (bibtex-field-left-delimiter))))
1727 (goto-char (- (cdr boundaries) 1))
1728 (if (not (looking-at (bibtex-field-right-delimiter)))
1729 (progn
1730 (delete-char 1)
1731 (insert (bibtex-field-right-delimiter))))
1732 (setq boundaries (bibtex-search-forward-field-string stop)))))
1733 1705
1734(defun bibtex-format-entry () 1706(defun bibtex-format-entry ()
1735 ;; Helper function for bibtex-clean-entry. Formats current entry 1707 "Helper function for `bibtex-clean-entry'.
1736 ;; according to variable bibtex-entry-format. 1708Formats current entry according to variable `bibtex-entry-format'."
1737 (let* ((case-fold-search t) 1709 (save-excursion
1738 (beg (point)) 1710 (save-restriction
1739 (start (bibtex-beginning-of-entry)) 1711 (bibtex-narrow-to-entry)
1740 crossref-there 1712 (let ((case-fold-search t)
1741 alternatives-there 1713 (format (if (equal bibtex-entry-format t)
1742 non-empty-alternative) 1714 '(realign opts-or-alts required-fields
1743 (let ((end (copy-marker (bibtex-end-of-entry)))) 1715 numerical-fields
1744 (if (equal start (marker-position end)) 1716 last-comma page-dashes delimiters
1745 (error "Not on a known BibTeX entry") 1717 unify-case inherit-booktitle)
1746 (goto-char start) 1718 bibtex-entry-format))
1747 (let ((bounds (bibtex-search-forward-field bibtex-field-name end))) 1719 crossref-key bounds alternatives-there non-empty-alternative
1748 (while bounds 1720 entry-list req creq field-done field-list)
1749 ;; determine if entry has crossref field and if at least 1721
1750 ;; one alternative is non-empty 1722 ;; identify entry type
1751 (let ((begin-name (bibtex-start-of-name-in-field bounds)) 1723 (goto-char (point-min))
1752 (end-name (bibtex-end-of-name-in-field bounds)) 1724 (re-search-forward bibtex-entry-type)
1753 (begin-text (bibtex-start-of-text-in-field bounds)) 1725 (let ((beg-type (1+ (match-beginning 0)))
1754 (end-text (bibtex-end-of-text-in-field bounds))) 1726 (end-type (match-end 0)))
1755 (goto-char begin-name) 1727 (setq entry-list (assoc-ignore-case (buffer-substring-no-properties
1756 (if (looking-at "ALT") 1728 beg-type end-type)
1757 (progn 1729 bibtex-entry-field-alist)
1758 (setq alternatives-there t) 1730 req (nth 0 (nth 1 entry-list)) ; required part
1759 (goto-char begin-text) 1731 creq (nth 0 (nth 2 entry-list))) ; crossref part
1760 (if (not (looking-at "\\(\"\"\\)\\|\\({}\\)")) 1732
1761 (setq non-empty-alternative t)))) 1733 ;; unify case of entry name
1762 (if (string-match 1734 (when (memq 'unify-case format)
1763 "\\(OPT\\)?crossref" 1735 (delete-region beg-type end-type)
1764 (buffer-substring-no-properties begin-name end-name)) 1736 (insert (car entry-list)))
1765 (progn 1737
1766 (setq 1738 ;; update left entry delimiter
1767 crossref-there 1739 (when (memq 'delimiters format)
1768 (buffer-substring-no-properties 1740 (goto-char end-type)
1769 (1+ begin-text) (1- end-text))) 1741 (skip-chars-forward " \t\n")
1770 (if (equal crossref-there "") 1742 (delete-char 1)
1771 (setq crossref-there nil))))) 1743 (insert (bibtex-entry-left-delimiter))))
1772 (goto-char (bibtex-end-of-field bounds)) 1744
1773 (setq bounds (bibtex-search-forward-field bibtex-field-name end)))) 1745 ;; determine if entry has crossref field and if at least
1746 ;; one alternative is non-empty
1747 (goto-char (point-min))
1748 (while (setq bounds (bibtex-search-forward-field
1749 bibtex-field-name))
1750 (goto-char (bibtex-start-of-name-in-field bounds))
1751 (cond ((looking-at "ALT")
1752 (setq alternatives-there t)
1753 (goto-char (bibtex-start-of-text-in-field bounds))
1754 (if (not (looking-at bibtex-empty-field-re))
1755 (setq non-empty-alternative t)))
1756 ((and (looking-at "\\(OPT\\)?crossref\\>")
1757 (progn (goto-char (bibtex-start-of-text-in-field bounds))
1758 (not (looking-at bibtex-empty-field-re))))
1759 (setq crossref-key
1760 (bibtex-text-in-field-bounds bounds t))))
1761 (goto-char (bibtex-end-of-field bounds)))
1774 (if (and alternatives-there 1762 (if (and alternatives-there
1775 (not non-empty-alternative)) 1763 (not non-empty-alternative)
1776 (progn 1764 (memq 'required-fields format))
1777 (goto-char beg) 1765 (error "All alternatives are empty"))
1778 (error "All alternatives are empty"))) 1766
1779 (goto-char start) 1767 ;; process all fields
1780 (re-search-forward bibtex-entry-type end) 1768 (goto-char (point-min))
1781 (let* ((begin-type (1+ (match-beginning 0))) 1769 (while (setq bounds (bibtex-search-forward-field bibtex-field-name))
1782 (end-type (match-end 0)) 1770 (let* ((beg-field (copy-marker (bibtex-start-of-field bounds)))
1783 (entry-type 1771 (end-field (copy-marker (bibtex-end-of-field bounds)))
1784 (downcase 1772 (beg-name (copy-marker (bibtex-start-of-name-in-field bounds)))
1785 (buffer-substring-no-properties begin-type end-type))) 1773 (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))
1786 (entry-list 1774 (beg-text (copy-marker (bibtex-start-of-text-in-field bounds)))
1787 (assoc-ignore-case entry-type bibtex-entry-field-alist)) 1775 (end-text (copy-marker (bibtex-end-of-text-in-field bounds)))
1788 (req (elt (elt entry-list 1) 0)) 1776 (opt-alt (string-match "OPT\\|ALT"
1789 (creq (elt (elt entry-list 2) 0)) 1777 (buffer-substring-no-properties beg-name (+ beg-name 3))))
1790 (format (if (equal bibtex-entry-format t) 1778 (field-name (buffer-substring-no-properties
1791 '(realign opts-or-alts numerical-fields 1779 (if opt-alt (+ beg-name 3) beg-name) end-name))
1792 last-comma page-dashes delimiters 1780 (empty-field (string-match bibtex-empty-field-re
1793 unify-case inherit-booktitle) 1781 (buffer-substring-no-properties beg-field end-field)))
1794 bibtex-entry-format)) 1782 deleted)
1795 field-done) 1783
1796 (if (memq 'unify-case format) 1784 ;; We have more elegant high-level functions for several
1797 (progn 1785 ;; tasks done by bibtex-format-entry. However, they contain
1798 (delete-region begin-type end-type) 1786 ;; quite some redundancy compared with what we need to do
1799 (insert (car entry-list)))) 1787 ;; anyway. So for speed-up we avoid using them.
1800 (if (memq 'delimiters format) 1788
1801 (progn 1789 (when (and opt-alt
1802 (goto-char end-type) 1790 (memq 'opts-or-alts format))
1803 (skip-chars-forward " \t\n") 1791 (if empty-field
1804 (delete-char 1) 1792 ;; Either it is an empty ALT field. Then we have checked
1805 (insert (bibtex-entry-left-delimiter)))) 1793 ;; already that we have one non-empty alternative.
1806 (goto-char start) 1794 ;; Or it is an empty OPT field that we do not miss anyway.
1807 (let ((bounds (bibtex-search-forward-field bibtex-field-name end))) 1795 ;; So we can safely delete this field.
1808 (while bounds 1796 (progn (delete-region beg-field end-field)
1809 (let* ((begin-field (copy-marker (bibtex-start-of-field bounds))) 1797 (setq deleted t))
1810 (end-field (copy-marker (bibtex-end-of-field bounds))) 1798 ;; otherwise: not empty, delete "OPT" or "ALT"
1811 (begin-name (copy-marker (bibtex-start-of-name-in-field bounds))) 1799 (goto-char beg-name)
1812 (end-name (copy-marker (bibtex-end-of-name-in-field bounds))) 1800 (delete-char 3)))
1813 (begin-text (copy-marker (bibtex-start-of-text-in-field bounds))) 1801
1814 (end-text (copy-marker (bibtex-end-of-text-in-field bounds))) 1802 (unless deleted
1815 (field-name 1803 (push field-name field-list)
1816 (buffer-substring-no-properties 1804
1817 (if (string-match 1805 ;; remove delimiters from purely numerical fields
1818 "^\\(OPT\\)\\|\\(ALT\\)$" 1806 (when (and (memq 'numerical-fields format)
1819 (buffer-substring-no-properties 1807 (progn (goto-char beg-text)
1820 begin-name (+ begin-name 3))) 1808 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1821 (+ begin-name 3) 1809 (goto-char end-text)
1822 begin-name) 1810 (delete-char -1)
1823 end-name))) 1811 (goto-char beg-text)
1824 (cond 1812 (delete-char 1))
1825 ((and 1813
1826 (memq 'opts-or-alts format) 1814 ;; update delimiters
1827 (progn (goto-char begin-name) 1815 (when (memq 'delimiters format)
1828 (looking-at "OPT\\|ALT"))) 1816 (goto-char beg-text)
1829 (goto-char begin-text) 1817 (when (looking-at "[{\"]")
1830 (if (looking-at "\\(\"\"\\)\\|\\({}\\)") 1818 (delete-char 1)
1831 ;; empty: delete whole field if really optional 1819 (insert (bibtex-field-left-delimiter)))
1832 ;; (missing crossref handled) or complain 1820 (goto-char (1- (marker-position end-text)))
1833 (if (and 1821 (when (looking-at "[}\"]")
1834 (progn 1822 (delete-char 1)
1835 (goto-char begin-name) 1823 (insert (bibtex-field-right-delimiter))))
1836 (looking-at "OPT")) 1824
1837 (not crossref-there) 1825 ;; update page dashes
1838 (assoc-ignore-case field-name req)) 1826 (if (and (memq 'page-dashes format)
1839 ;; field is not really optional 1827 (string-match "^\\(OPT\\)?pages\\'" field-name)
1840 (progn 1828 (progn (goto-char beg-text)
1841 (goto-char begin-name) 1829 (looking-at
1842 (bibtex-remove-OPT-or-ALT) 1830 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1843 (error 1831 (replace-match "\\1-\\2"))
1844 "Mandatory field ``%s'' is empty" field-name)) 1832
1845 ;; field is optional 1833 ;; use book title of crossrefed entry
1846 (delete-region begin-field end-field)) 1834 (if (and (memq 'inherit-booktitle format)
1847 ;; otherwise: not empty, delete "OPT" 1835 (equal (downcase field-name) "booktitle")
1848 (goto-char begin-name) 1836 empty-field
1849 (bibtex-remove-OPT-or-ALT))) 1837 crossref-key)
1850 ((and 1838 (let ((title (save-restriction
1851 (memq 'numerical-fields format) 1839 (widen)
1852 (progn 1840 (if (bibtex-find-entry crossref-key)
1853 (goto-char begin-text) 1841 (bibtex-text-in-field "title")))))
1854 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)"))) 1842 (when title
1855 (goto-char end-text) 1843 (setq empty-field nil)
1856 (delete-char -1) 1844 (goto-char (1+ beg-text))
1857 (goto-char begin-text) 1845 (insert title))))
1858 (delete-char 1)) 1846
1859 (t 1847
1860 (if (memq 'delimiters format) 1848 ;; if empty field, complain
1861 (bibtex-format-field-delimiters begin-text end-text)) 1849 (if (and empty-field
1862 (if (and 1850 (memq 'required-fields format)
1863 (memq 'page-dashes format) 1851 (assoc-ignore-case field-name
1864 (string-match "^\\(OPT\\)?pages$" (downcase field-name)) 1852 (if crossref-key creq req)))
1865 (progn 1853 (error "Mandatory field `%s' is empty" field-name))
1866 (goto-char begin-text) 1854
1867 (looking-at 1855 ;; unify case of field name
1868 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)"))) 1856 (if (memq 'unify-case format)
1869 (replace-match "\\1-\\2")) 1857 (let ((fname (car (assoc-ignore-case
1870 (if (and 1858 field-name (append (nth 0 (nth 1 entry-list))
1871 (memq 'inherit-booktitle format) 1859 (nth 1 (nth 1 entry-list))
1872 (equal (downcase field-name) "booktitle") 1860 bibtex-user-optional-fields)))))
1873 (progn 1861 (if fname
1874 (goto-char begin-text) 1862 (progn
1875 (looking-at "\\(\"\"\\)\\|\\({}\\)")) 1863 (delete-region beg-name end-name)
1876 crossref-there 1864 (goto-char beg-name)
1877 (not (bibtex-find-entry-location crossref-there t))) 1865 (insert fname))
1878 ;; booktitle field empty and crossref entry found 1866 ;; there are no rules we could follow
1879 ;; --> insert title field of crossreferenced entry if there 1867 (downcase-region beg-name end-name))))
1880 (let ((stop (bibtex-end-of-entry))) 1868
1881 (bibtex-beginning-of-entry) 1869 ;; update point
1882 (let ((bounds (bibtex-search-forward-field "title" stop))) 1870 (goto-char end-field))))
1883 (if bounds 1871
1884 (progn 1872 ;; check whether all required fields are present
1885 (goto-char begin-text) 1873 (if (memq 'required-fields format)
1886 (forward-char) 1874 (let (altlist (found 0))
1887 (insert 1875 (dolist (fname (if crossref-key creq req))
1888 (buffer-substring-no-properties 1876 (if (nth 3 fname)
1889 (1+ (bibtex-start-of-text-in-field bounds)) 1877 (push (car fname) altlist))
1890 (1- (bibtex-end-of-text-in-field bounds))))))))) 1878 (unless (or (member (car fname) field-list)
1891 (if (progn 1879 (nth 3 fname))
1892 (goto-char begin-text) 1880 (error "Mandatory field `%s' is missing" (car fname))))
1893 (looking-at "\\(\"\"\\)\\|\\({}\\)")) 1881 (when altlist
1894 ;; if empty field, complain 1882 (dolist (fname altlist)
1895 (progn 1883 (if (member fname field-list)
1896 (forward-char) 1884 (setq found (1+ found))))
1897 (if (or (and 1885 (cond ((= found 0)
1898 crossref-there 1886 (error "Alternative mandatory field `%s' is missing"
1899 (assoc-ignore-case 1887 altlist))
1900 field-name creq)) 1888 ((> found 1)
1901 (and 1889 (error "Alternative fields `%s' is defined %s times"
1902 (not crossref-there) 1890 altlist found))))))
1903 (assoc-ignore-case 1891
1904 field-name req))) 1892 ;; update point
1905 (error 1893 (if (looking-at (bibtex-field-right-delimiter))
1906 "Mandatory field ``%s'' is empty" field-name)))) 1894 (forward-char))
1907 (if (memq 'unify-case format) 1895
1908 (let* ((fl 1896 ;; update comma after last field
1909 (car (cdr (assoc-ignore-case 1897 (if (memq 'last-comma format)
1910 entry-type 1898 (cond ((and bibtex-comma-after-last-field
1911 bibtex-entry-field-alist)))) 1899 (not (looking-at ",")))
1912 (field-list 1900 (insert ","))
1913 (append 1901 ((and (not bibtex-comma-after-last-field)
1914 (elt fl 0) 1902 (looking-at ","))
1915 (elt fl 1) 1903 (delete-char 1))))
1916 bibtex-user-optional-fields)) 1904
1917 (new-field-name 1905 ;; update right entry delimiter
1918 (car 1906 (if (looking-at ",")
1919 (assoc-ignore-case field-name field-list)))) 1907 (forward-char))
1920 (goto-char begin-name) 1908 (when (memq 'delimiters format)
1921 (if new-field-name 1909 (skip-chars-forward " \t\n")
1922 (progn 1910 (delete-char 1)
1923 (delete-region begin-name end-name) 1911 (insert (bibtex-entry-right-delimiter)))
1924 (insert new-field-name)) 1912
1925 (downcase-region begin-name end-name)))) 1913 ;; fill entry
1926 (setq field-done t))) 1914 (if (memq 'realign format)
1927 (if (not field-done) 1915 (bibtex-fill-entry))))))
1928 (goto-char begin-field)
1929 (setq field-done nil)
1930 (goto-char end-field)))
1931 (setq bounds (bibtex-search-forward-field bibtex-field-name end))))
1932 (if (looking-at (bibtex-field-right-delimiter))
1933 (forward-char))
1934 (if (memq 'last-comma format)
1935 (cond ((and
1936 bibtex-comma-after-last-field
1937 (not (looking-at ",")))
1938 (insert ","))
1939 ((and
1940 (not bibtex-comma-after-last-field)
1941 (looking-at ","))
1942 (delete-char 1))))
1943 (if (looking-at ",")
1944 (forward-char))
1945 (if (memq 'delimiters format)
1946 (progn
1947 (skip-chars-forward " \t\n")
1948 (delete-char 1)
1949 (insert (bibtex-entry-right-delimiter))))
1950 (if (memq 'realign format)
1951 (bibtex-fill-entry)))))))
1952
1953(defun bibtex-autokey-change (string change-list)
1954 ;; Returns a string where some regexps are changed according to
1955 ;; change-list. Every item of change-list is an (old-regexp
1956 ;; new-string) pair.
1957 (let* (case-fold-search
1958 (return-string string)
1959 (index 0)
1960 (len (length change-list))
1961 change-item)
1962 (while (< index len)
1963 (setq change-item (elt change-list index))
1964 (while (string-match (car change-item) return-string)
1965 (setq
1966 return-string
1967 (concat (substring return-string 0 (match-beginning 0))
1968 (elt change-item 1)
1969 (substring return-string (match-end 0)))))
1970 (setq index (1+ index)))
1971 return-string))
1972 1916
1917
1973(defun bibtex-autokey-abbrev (string len) 1918(defun bibtex-autokey-abbrev (string len)
1974 ;; Returns an abbreviation of string with at least len 1919 "Return an abbreviation of STRING with at least LEN characters.
1975 ;; characters. String is aborted only after a consonant or at the 1920If LEN is positive the abbreviation is terminated only after a consonant
1976 ;; word end. If len is not a number, string is returned unchanged. 1921or at the word end. If LEN is negative the abbreviation is strictly
1977 (cond ((or 1922enforced using abs (LEN) characters. If LEN is not a number, STRING
1978 (not (numberp len)) 1923is returned unchanged."
1979 (<= (length string) len)) 1924 (cond ((or (not (numberp len))
1925 (<= (length string) (abs len)))
1980 string) 1926 string)
1981 ((equal len 0) 1927 ((equal len 0)
1982 "") 1928 "")
1983 (t 1929 ((< len 0)
1984 (let* ((case-fold-search t) 1930 (substring string 0 (abs len)))
1985 (abort-char 1931 (t (let* ((case-fold-search t)
1986 (string-match "[^aeiou]" string (1- len)))) 1932 (abort-char (string-match "[^aeiou]" string (1- len))))
1987 (if abort-char 1933 (if abort-char
1988 (substring string 0 (1+ abort-char)) 1934 (substring string 0 (1+ abort-char))
1989 string))))) 1935 string)))))
1990 1936
1991(defun bibtex-autokey-get-namefield (min max) 1937(defun bibtex-autokey-get-field (field &optional change-list)
1992 ;; returns the contents of the name field of the current entry 1938 "Get content of BibTeX field FIELD. Return empty string if not found.
1993 ;; does some modifications based on `bibtex-autokey-name-change-strings' 1939Optional arg CHANGE-LIST is a list of substitution patterns that is
1994 ;; and removes newlines unconditionally 1940applied to the content of FIELD. It is an alist with pairs
1995 (goto-char min) 1941\(OLD-REGEXP . NEW-STRING\)."
1996 (let* ((case-fold-search t) 1942 (let ((content (bibtex-text-in-field field bibtex-autokey-use-crossref))
1997 (author-or-editor "\\(author\\)\\|\\(editor\\)") 1943 case-fold-search)
1998 (bounds (bibtex-search-forward-field author-or-editor max))) 1944 (unless content (setq content ""))
1999 (if bounds 1945 (dolist (pattern change-list content)
2000 (bibtex-autokey-change 1946 (setq content (replace-regexp-in-string (car pattern)
2001 (buffer-substring-no-properties 1947 (cdr pattern)
2002 (1+ (bibtex-start-of-text-in-field bounds)) 1948 content)))))
2003 (1- (bibtex-end-of-text-in-field bounds))) 1949
2004 (append bibtex-autokey-name-change-strings '(("\n" " ")))) 1950(defun bibtex-autokey-get-names ()
2005 ""))) 1951 "Get contents of the name field of the current entry.
2006 1952Do some modifications based on `bibtex-autokey-name-change-strings'
2007(defun bibtex-autokey-get-names (namefield) 1953and return results as a list."
2008 ;; gathers all names in namefield into a list 1954 (let ((case-fold-search t))
2009 (let* ((case-fold-search t) 1955 (mapcar 'bibtex-autokey-demangle-name
2010 names) 1956 (split-string (bibtex-autokey-get-field
2011 (while (not (equal namefield "")) 1957 "\\(author\\)\\|\\(editor\\)"
2012 (let (name) 1958 bibtex-autokey-name-change-strings)
2013 (if (string-match "[ \t\n]and[ \t\n]" namefield) 1959 "[ ]+and[ ]+"))))
2014 (setq name (substring namefield 0 (match-beginning 0))
2015 namefield (substring namefield (match-end 0)))
2016 (setq name namefield
2017 namefield ""))
2018 (setq names (append names (list name)))))
2019 names))
2020 1960
2021(defun bibtex-autokey-demangle-name (fullname) 1961(defun bibtex-autokey-demangle-name (fullname)
2022 ;; gets the `last' part from a well-formed name 1962 "Get the last part from a well-formed name and perform abbreviations."
2023 (let* (case-fold-search
2024 (lastname
2025 (if (string-match "," fullname)
2026 ;; name is of the form "von Last, First" or
2027 ;; "von Last, Jr, First"
2028 ;; --> take only the part before the comma
2029 (let ((von-last
2030 (substring fullname 0 (match-beginning 0))))
2031 (if (string-match "^[a-z]" von-last)
2032 ;; von-last has a "von" part --> take the "last" part
2033 (if (string-match "[ \t][A-Z]" von-last)
2034 (substring von-last (1+ (match-beginning 0)))
2035 (error
2036 "Name %s is incorrectly formed" fullname))
2037 ;; von-last has no "von" part --> take all
2038 von-last))
2039 ;; name is of the form "First von Last"
2040 (if (string-match "[ \t]" fullname)
2041 ;; more than one token
2042 (if (string-match "[ \t][a-z]" fullname)
2043 ;; there is a "von" part
2044 ;; --> take everything after that
2045 (if (string-match
2046 "[ \t][A-Z]" fullname (match-end 0))
2047 (substring fullname (1+ (match-beginning 0)))
2048 (error
2049 "Name %s is incorrectly formed" fullname))
2050 ;; there is no "von" part --> take only the last token
2051 (if (string-match " [^ ]*$" fullname)
2052 (substring fullname (1+ (match-beginning 0)))
2053 (error "Name %s is incorrectly formed" fullname)))
2054 ;; only one token --> take it
2055 fullname)))
2056 (usename
2057 (if (string-match "[ \t]+" lastname)
2058 ;; lastname consists of two or more tokens
2059 ;; --> take only the first one
2060 (substring lastname 0 (match-beginning 0))
2061 lastname)))
2062 (funcall bibtex-autokey-name-case-convert usename)))
2063
2064(defun bibtex-autokey-get-namelist (namefield)
2065 ;; gets namefield, performs abbreviations on the last parts, and
2066 ;; return results as a list
2067 (mapcar
2068 (lambda (fullname)
2069 (setq
2070 fullname (substring fullname (string-match "[^ \t]" fullname)))
2071 (bibtex-autokey-abbrev
2072 (bibtex-autokey-demangle-name fullname)
2073 bibtex-autokey-name-length))
2074 (bibtex-autokey-get-names namefield)))
2075
2076(defun bibtex-autokey-get-yearfield-digits (max)
2077 ;; get digit substring from year field.
2078 (let ((bounds (bibtex-search-forward-field "year" max)))
2079 (if bounds
2080 (let ((year (buffer-substring-no-properties
2081 (bibtex-start-of-text-in-field bounds)
2082 (bibtex-end-of-text-in-field bounds))))
2083 (string-match "[^0-9]*\\([0-9]+\\)" year)
2084 (substring year (match-beginning 1) (match-end 1)))
2085 "")))
2086
2087(defun bibtex-autokey-get-yearfield (min max)
2088 ;; get year field from current or maybe crossreferenced entry
2089 (goto-char min)
2090 (let* ((case-fold-search t)
2091 (year (bibtex-autokey-get-yearfield-digits max)))
2092 (if (and (string= year "") bibtex-autokey-year-use-crossref-entry)
2093 (let* ((bounds
2094 (bibtex-search-forward-field "\\(OPT\\)?crossref" max))
2095 (crossref-field
2096 (if bounds
2097 (buffer-substring-no-properties
2098 (1+ (bibtex-start-of-text-in-field bounds))
2099 (1- (bibtex-end-of-text-in-field bounds))))))
2100 (if (not (bibtex-find-entry-location crossref-field t))
2101 (let ((stop (bibtex-end-of-entry)))
2102 (bibtex-beginning-of-entry)
2103 (bibtex-autokey-get-yearfield-digits stop))
2104 ""))
2105 year)))
2106
2107(defun bibtex-autokey-get-titlestring (min max)
2108 ;; get title field contents up to a terminator
2109 (goto-char min)
2110 (let* ((case-fold-search t)
2111 (bounds (bibtex-search-forward-field "title" max))
2112 (titlefield
2113 (if bounds
2114 (bibtex-autokey-change
2115 (buffer-substring-no-properties
2116 (1+ (bibtex-start-of-text-in-field bounds))
2117 (1- (bibtex-end-of-text-in-field bounds)))
2118 bibtex-autokey-titleword-change-strings)
2119 ""))
2120 (title-terminators bibtex-autokey-title-terminators))
2121 (while (not (null title-terminators))
2122 (if (string-match (car title-terminators) titlefield)
2123 (setq titlefield (substring titlefield 0 (match-beginning 0))))
2124 (setq title-terminators (cdr title-terminators)))
2125 titlefield))
2126
2127(defun bibtex-autokey-get-titles (titlestring)
2128 ;; gathers words from titlestring into a list. Ignores
2129 ;; specific words and uses only a specific amount of words.
2130 (let* (case-fold-search 1963 (let* (case-fold-search
2131 titlewords 1964 (name (cond ((string-match "\\([A-Z][^, ]*\\)[^,]*," fullname)
2132 titlewords-extra 1965 ;; Name is of the form "von Last, First" or
2133 (counter 0)) 1966 ;; "von Last, Jr, First"
2134 (while (and 1967 ;; --> Take the first capital part before the comma
2135 (not (equal titlestring "")) 1968 (substring fullname (match-beginning 1) (match-end 1)))
2136 (or 1969 ((string-match "\\([^, ]*\\)," fullname)
2137 (not (numberp bibtex-autokey-titlewords)) 1970 ;; Strange name: we have a comma, but nothing capital
2138 (< counter 1971 ;; So we accept even lowercase names
2139 (+ bibtex-autokey-titlewords 1972 (substring fullname (match-beginning 1) (match-end 1)))
2140 bibtex-autokey-titlewords-stretch)))) 1973 ((string-match "\\(\\<[a-z][^ ]*[ ]+\\)+\\([A-Z][^ ]*\\)"
2141 (if (string-match "\\b\\w+" titlestring) 1974 fullname)
2142 (let* ((end-match (match-end 0)) 1975 ;; name is of the form "First von Last", "von Last",
2143 (titleword 1976 ;; "First von von Last", or "d'Last"
2144 (substring titlestring (match-beginning 0) end-match))) 1977 ;; --> take the first capital part after the "von" parts
2145 (if (bibtex-member-of-regexp 1978 (substring fullname (match-beginning 2) (match-end 2)))
2146 titleword 1979 ((string-match "\\([^ ]+\\)[ ]*$" fullname)
2147 bibtex-autokey-titleword-ignore) 1980 ;; name is of the form "First Middle Last" or "Last"
2148 (setq counter (1- counter)) 1981 ;; --> take the last token
2149 (setq 1982 (substring fullname (match-beginning 1) (match-end 1)))
2150 titleword 1983 (t (error "Name `%s' is incorrectly formed" fullname)))))
2151 (funcall bibtex-autokey-titleword-case-convert titleword)) 1984 (bibtex-autokey-abbrev
2152 (if (or (not (numberp bibtex-autokey-titlewords)) 1985 (funcall bibtex-autokey-name-case-convert name)
2153 (< counter bibtex-autokey-titlewords)) 1986 bibtex-autokey-name-length)))
2154 (setq titlewords (append titlewords (list titleword))) 1987
2155 (setq titlewords-extra 1988(defun bibtex-autokey-get-title ()
2156 (append titlewords-extra (list titleword))))) 1989 "Get title field contents up to a terminator."
2157 (setq 1990 (let ((titlestring
2158 titlestring (substring titlestring end-match))) 1991 (bibtex-autokey-get-field "title"
2159 (setq titlestring "")) 1992 bibtex-autokey-titleword-change-strings)))
2160 (setq counter (1+ counter))) 1993 ;; ignore everything past a terminator
2161 (if (string-match "\\b\\w+" titlestring) 1994 (let ((case-fold-search t))
2162 titlewords 1995 (dolist (terminator bibtex-autokey-title-terminators)
2163 (append titlewords titlewords-extra)))) 1996 (if (string-match terminator titlestring)
2164 1997 (setq titlestring (substring titlestring 0 (match-beginning 0))))))
2165(defun bibtex-autokey-get-titlelist (titlestring) 1998 ;; gather words from titlestring into a list. Ignore
2166 ;; returns all words in titlestring as a list 1999 ;; specific words and use only a specific amount of words.
2167 ;; does some abbreviation on the found words 2000 (let (case-fold-search titlewords titlewords-extra titleword end-match
2168 (mapcar 2001 (counter 0))
2169 (lambda (titleword) 2002 (while (and (or (not (numberp bibtex-autokey-titlewords))
2170 (let ((abbrev 2003 (< counter (+ bibtex-autokey-titlewords
2171 (bibtex-assoc-of-regexp 2004 bibtex-autokey-titlewords-stretch)))
2172 titleword bibtex-autokey-titleword-abbrevs))) 2005 (string-match "\\b\\w+" titlestring))
2173 (if abbrev 2006 (setq end-match (match-end 0)
2174 (elt abbrev 1) 2007 titleword (substring titlestring
2175 (bibtex-autokey-abbrev 2008 (match-beginning 0) end-match))
2176 titleword 2009 (unless (bibtex-member-of-regexp titleword
2177 bibtex-autokey-titleword-length)))) 2010 bibtex-autokey-titleword-ignore)
2178 (bibtex-autokey-get-titles titlestring))) 2011 (setq titleword
2012 (funcall bibtex-autokey-titleword-case-convert titleword))
2013 (if (or (not (numberp bibtex-autokey-titlewords))
2014 (< counter bibtex-autokey-titlewords))
2015 (setq titlewords (append titlewords (list titleword)))
2016 (setq titlewords-extra
2017 (append titlewords-extra (list titleword))))
2018 (setq counter (1+ counter)))
2019 (setq titlestring (substring titlestring end-match)))
2020 (unless (string-match "\\b\\w+" titlestring)
2021 (setq titlewords (append titlewords titlewords-extra)))
2022 (mapcar 'bibtex-autokey-demangle-title titlewords))))
2023
2024(defun bibtex-autokey-demangle-title (titleword)
2025 "Do some abbreviations on TITLEWORD.
2026The rules are defined in `bibtex-autokey-titleword-abbrevs'
2027and `bibtex-autokey-titleword-length'."
2028 (let ((abbrev (bibtex-assoc-of-regexp
2029 titleword bibtex-autokey-titleword-abbrevs)))
2030 (if abbrev
2031 (cdr abbrev)
2032 (bibtex-autokey-abbrev titleword
2033 bibtex-autokey-titleword-length))))
2179 2034
2180(defun bibtex-generate-autokey () 2035(defun bibtex-generate-autokey ()
2181 "Generates automatically a key from the author/editor and the title field. 2036 "Generate automatically a key from the author/editor and the title field.
2182This will only work for entries where each field begins on a separate line. 2037This will only work for entries where each field begins on a separate line.
2183The generation algorithm works as follows: 2038The generation algorithm works as follows:
2184 1. Use the value of `bibtex-autokey-prefix-string' as a prefix. 2039 1. Use the value of `bibtex-autokey-prefix-string' as a prefix.
@@ -2191,9 +2046,8 @@ The generation algorithm works as follows:
2191 the name field, determine the last name. If there are maximal 2046 the name field, determine the last name. If there are maximal
2192 `bibtex-autokey-names' + `bibtex-autokey-names-stretch' 2047 `bibtex-autokey-names' + `bibtex-autokey-names-stretch'
2193 names, all names are used. 2048 names, all names are used.
2194 5. From every last name, take at least 2049 5. From every last name, take at least `bibtex-autokey-name-length'
2195 `bibtex-autokey-name-length' characters (abort only after a 2050 characters (abort only after a consonant or at a word end).
2196 consonant or at a word end).
2197 6. Convert all last names according to the conversion function 2051 6. Convert all last names according to the conversion function
2198 `bibtex-autokey-name-case-convert'. 2052 `bibtex-autokey-name-case-convert'.
2199 7. Build the name part of the key by concatenating all 2053 7. Build the name part of the key by concatenating all
@@ -2203,10 +2057,11 @@ The generation algorithm works as follows:
2203 contained in `bibtex-autokey-additional-names'. 2057 contained in `bibtex-autokey-additional-names'.
2204 8. Build the year part of the key by truncating the contents of 2058 8. Build the year part of the key by truncating the contents of
2205 the year field to the rightmost `bibtex-autokey-year-length' 2059 the year field to the rightmost `bibtex-autokey-year-length'
2206 digits (useful values are 2 and 4). If the year field is 2060 digits (useful values are 2 and 4). If the year field (or any
2207 absent, but the entry has a valid crossref field and the 2061 other field required to generate the key) is absent, but the entry
2208 variable `bibtex-autokey-year-use-crossref-entry' is non-nil, 2062 has a valid crossref field and the variable
2209 use the year field of the crossreferenced entry instead. 2063 `bibtex-autokey-use-crossref' is non-nil, use the field of the
2064 crossreferenced entry instead.
2210 9. For the title part of the key change the contents of the 2065 9. For the title part of the key change the contents of the
2211 title field of the entry according to 2066 title field of the entry according to
2212 `bibtex-autokey-titleword-change-strings' to the 2067 `bibtex-autokey-titleword-change-strings' to the
@@ -2251,298 +2106,303 @@ The generation algorithm works as follows:
2251 the key is then presented in the minibuffer to the user, 2106 the key is then presented in the minibuffer to the user,
2252 where it can be edited. The key given by the user is then 2107 where it can be edited. The key given by the user is then
2253 used." 2108 used."
2254 (let* ((pnt (point)) 2109 (let* ((name-etal "")
2255 (min (bibtex-beginning-of-entry))
2256 (max (bibtex-end-of-entry))
2257 (namefield (bibtex-autokey-get-namefield min max))
2258 (name-etal "")
2259 (namelist 2110 (namelist
2260 (let ((nl (bibtex-autokey-get-namelist namefield))) 2111 (let ((nl (bibtex-autokey-get-names))
2112 nnl)
2261 (if (or (not (numberp bibtex-autokey-names)) 2113 (if (or (not (numberp bibtex-autokey-names))
2262 (<= (length nl) 2114 (<= (length nl)
2263 (+ bibtex-autokey-names 2115 (+ bibtex-autokey-names
2264 bibtex-autokey-names-stretch))) 2116 bibtex-autokey-names-stretch)))
2265 nl 2117 nl
2266 (setq name-etal bibtex-autokey-additional-names) 2118 (setq name-etal bibtex-autokey-additional-names)
2267 (let (nnl) 2119 (while (< (length nnl) bibtex-autokey-names)
2268 (while (< (length nnl) bibtex-autokey-names) 2120 (setq nnl (append nnl (list (car nl)))
2269 (setq nnl (append nnl (list (car nl))) 2121 nl (cdr nl)))
2270 nl (cdr nl))) 2122 nnl)))
2271 nnl)))) 2123 (namepart (concat (mapconcat (lambda (name) name)
2272 (namepart 2124 namelist
2273 (concat 2125 bibtex-autokey-name-separator)
2274 (mapconcat (lambda (name) name) 2126 name-etal))
2275 namelist 2127 (yearfield (bibtex-autokey-get-field "year"))
2276 bibtex-autokey-name-separator) 2128 (yearpart (if (equal yearfield "")
2277 name-etal)) 2129 ""
2278 (yearfield (bibtex-autokey-get-yearfield min max)) 2130 (substring yearfield
2279 (yearpart 2131 (- (length yearfield)
2280 (if (equal yearfield "") 2132 bibtex-autokey-year-length))))
2281 "" 2133 (titlepart (mapconcat (lambda (name) name)
2282 (substring 2134 (bibtex-autokey-get-title)
2283 yearfield 2135 bibtex-autokey-titleword-separator))
2284 (- (length yearfield) bibtex-autokey-year-length)))) 2136 (autokey (concat bibtex-autokey-prefix-string
2285 (titlestring (bibtex-autokey-get-titlestring min max)) 2137 namepart
2286 (titlelist (bibtex-autokey-get-titlelist titlestring)) 2138 (unless (or (equal namepart "")
2287 (titlepart 2139 (equal yearpart ""))
2288 (mapconcat 2140 bibtex-autokey-name-year-separator)
2289 (lambda (name) name) 2141 yearpart
2290 titlelist 2142 (unless (or (and (equal namepart "")
2291 bibtex-autokey-titleword-separator)) 2143 (equal yearpart ""))
2292 (autokey 2144 (equal titlepart ""))
2293 (concat 2145 bibtex-autokey-year-title-separator)
2294 bibtex-autokey-prefix-string 2146 titlepart)))
2295 namepart
2296 (if (not
2297 (or
2298 (equal namepart "")
2299 (equal yearpart "")))
2300 bibtex-autokey-name-year-separator)
2301 yearpart
2302 (if (not
2303 (or
2304 (and
2305 (equal namepart "")
2306 (equal yearpart ""))
2307 (equal titlepart "")))
2308 bibtex-autokey-year-title-separator)
2309 titlepart)))
2310 (if bibtex-autokey-before-presentation-function 2147 (if bibtex-autokey-before-presentation-function
2311 (setq 2148 (funcall bibtex-autokey-before-presentation-function autokey)
2312 autokey 2149 autokey)))
2313 (funcall bibtex-autokey-before-presentation-function autokey)))
2314 (goto-char pnt)
2315 autokey))
2316 2150
2151
2317(defun bibtex-parse-keys (add verbose &optional abortable) 2152(defun bibtex-parse-keys (add verbose &optional abortable)
2318 ;; Sets bibtex-reference-keys to the keys used in the whole (possibly 2153 "Set `bibtex-reference-keys' to the keys used in the whole buffer.
2319 ;; restricted) buffer (either as entry keys or as crossref entries). 2154The buffer might possibly be restricted.
2320 ;; If ADD is non-nil adds the new keys to bibtex-reference-keys instead of 2155Find both entry keys and crossref entries.
2321 ;; simply resetting it. If VERBOSE is non-nil gives messages about 2156If ADD is non-nil adds the new keys to `bibtex-reference-keys' instead of
2322 ;; progress. If ABORTABLE is non-nil abort on user input. 2157simply resetting it. If VERBOSE is non-nil gives messages about
2323 (if bibtex-maintain-sorted-entries 2158progress. If ABORTABLE is non-nil abort on user input.
2324 (let* ((case-fold-search t) 2159Return t if parsing was completed, nil if aborted."
2325 (reference-keys (if add bibtex-reference-keys))) 2160 (let ((reference-keys (if add bibtex-reference-keys)))
2326 (save-excursion 2161 (save-excursion
2327 (save-match-data 2162 (save-match-data
2328 (goto-char (point-min)) 2163 (goto-char (point-min))
2329 (if verbose 2164 (if verbose
2330 (bibtex-progress-message 2165 (bibtex-progress-message
2331 (concat (buffer-name) ": parsing reference keys"))) 2166 (concat (buffer-name) ": parsing reference keys")))
2332 (if (catch 'userkey 2167 (if (catch 'userkey
2333 (bibtex-skip-to-valid-entry) 2168 (while (bibtex-skip-to-valid-entry)
2334 (while (not (eobp)) 2169 (if (and abortable
2335 (if (and
2336 abortable
2337 (input-pending-p)) 2170 (input-pending-p))
2338 (throw 'userkey t)) 2171 (throw 'userkey t))
2339 (if verbose 2172 (if verbose
2340 (bibtex-progress-message)) 2173 (bibtex-progress-message))
2341 (let (reference-key 2174 (let ((case-fold-search t)
2342 xrefd-reference-key) 2175 key crossref-key bounds)
2343 (cond 2176 (cond ((looking-at bibtex-entry-head)
2344 ((looking-at bibtex-entry-head) 2177 (setq key (bibtex-key-in-head))
2345 (setq 2178 (let ((p (point))
2346 reference-key 2179 (m (bibtex-end-of-entry)))
2347 (buffer-substring-no-properties 2180 (goto-char p)
2348 (match-beginning bibtex-key-in-head) 2181 (if (setq bounds (bibtex-search-forward-field "crossref" m))
2349 (match-end bibtex-key-in-head))) 2182 (setq crossref-key (bibtex-text-in-field-bounds bounds t)))))
2350 (let ((p (point)) 2183 ((setq bounds (bibtex-parse-string))
2351 (m (bibtex-end-of-entry))) 2184 (setq key (bibtex-reference-key-in-string bounds))))
2352 (goto-char p) 2185 (forward-char)
2353 (let ((bounds (bibtex-search-forward-field "crossref" m))) 2186 (unless (assoc key reference-keys)
2354 (if bounds 2187 (push (list key) reference-keys))
2355 (setq 2188 (if (and crossref-key
2356 xrefd-reference-key 2189 (not (assoc crossref-key reference-keys)))
2357 (buffer-substring-no-properties 2190 (push (list crossref-key) reference-keys)))))
2358 (1+ (bibtex-start-of-text-in-field bounds)) 2191 ;; user has aborted by typing a key --> return nil
2359 (1- (bibtex-end-of-text-in-field bounds)))))))) 2192 nil
2360 ((bibtex-parse-string) 2193 ;; successful operation --> return t
2361 (let ((bounds (bibtex-parse-string))) 2194 (setq bibtex-reference-keys reference-keys)
2362 (setq 2195 (if verbose
2363 reference-key 2196 (bibtex-progress-message 'done))
2364 (buffer-substring-no-properties 2197 t)))))
2365 (bibtex-start-of-reference-key-in-string bounds) 2198
2366 (bibtex-end-of-reference-key-in-string bounds)))))) 2199(defun bibtex-parse-strings (&optional init abortable)
2367 (forward-char) 2200 "Set `bibtex-strings' to the string definitions in the whole buffer.
2368 (bibtex-skip-to-valid-entry) 2201The buffer might possibly be restricted.
2369 (if (not (assoc reference-key reference-keys)) 2202Optional arg INIT is used to initialize `bibtex-strings'.
2370 (setq reference-keys 2203If ABORTABLE is non-nil abort on user input.
2371 (cons (list reference-key) reference-keys))) 2204Return t if parsing was completed, nil if aborted."
2372 (if (and xrefd-reference-key 2205 (save-excursion
2373 (not (assoc xrefd-reference-key reference-keys))) 2206 (save-match-data
2374 (setq reference-keys 2207 (goto-char (point-min))
2375 (cons (list xrefd-reference-key) reference-keys)))))) 2208 (let ((strings init)
2376 ;; user has aborted by typing a key --> return nil 2209 bounds key)
2377 nil 2210 (if (catch 'userkey
2378 ;; successful operation --> return t 2211 (while (setq bounds (bibtex-search-forward-string))
2379 (setq 2212 (if (and abortable
2380 bibtex-buffer-last-parsed-tick (buffer-modified-tick) 2213 (input-pending-p))
2381 bibtex-reference-keys reference-keys) 2214 (throw 'userkey t))
2382 (if verbose 2215 (setq key (bibtex-reference-key-in-string bounds))
2383 (bibtex-progress-message 'done)) 2216 (if (not (assoc-ignore-case key strings))
2384 t)))))) 2217 (push (cons key (bibtex-text-in-string bounds t))
2218 strings))
2219 (goto-char (bibtex-end-of-text-in-string bounds))))
2220 ;; user has aborted by typing a key --> return nil
2221 nil
2222 ;; successful operation --> return t
2223 (setq bibtex-strings strings)
2224 t)))))
2225
2226(defun bibtex-string-files-init ()
2227 "Return initialization for `bibtex-strings'.
2228Use `bibtex-predefined-strings' and bib files `bibtex-string-files'."
2229 (save-match-data
2230 ;; collect pathnames
2231 (let ((dirlist (split-string (or bibtex-string-file-path ".")
2232 ":+"))
2233 (case-fold-search)
2234 compl)
2235 (dolist (filename bibtex-string-files)
2236 (unless (string-match "\.bib$" filename)
2237 (setq filename (concat filename ".bib")))
2238 ;; test filenames
2239 (let (fullfilename bounds found)
2240 (dolist (dir dirlist)
2241 (when (file-readable-p
2242 (setq fullfilename (expand-file-name filename dir)))
2243 ;; file was found
2244 (with-temp-buffer
2245 (insert-file-contents fullfilename)
2246 (goto-char (point-min))
2247 (while (setq bounds (bibtex-search-forward-string))
2248 (push (cons (bibtex-reference-key-in-string bounds)
2249 (bibtex-text-in-string bounds t))
2250 compl)
2251 (goto-char (bibtex-end-of-string bounds))))
2252 (setq found t)))
2253 (unless found
2254 (error "File %s not in paths defined by bibtex-string-file-path variable"
2255 filename))))
2256 (append bibtex-predefined-strings (nreverse compl)))))
2385 2257
2386(defun bibtex-parse-buffers-stealthily () 2258(defun bibtex-parse-buffers-stealthily ()
2387 ;; Called by bibtex-run-with-idle-timer. Whenever emacs has been idle 2259 "Called by `bibtex-run-with-idle-timer'. Whenever emacs has been idle
2388 ;; for bibtex-parse-keys-timeout seconds, all BibTeX buffers (starting 2260for `bibtex-parse-keys-timeout' seconds, all BibTeX buffers (starting
2389 ;; with the current) are parsed. 2261with the current) are parsed."
2390 (let ((buffers (buffer-list))) 2262 (save-excursion
2391 (save-excursion 2263 (let ((buffers (buffer-list))
2264 (strings-init (bibtex-string-files-init)))
2392 (while (and buffers (not (input-pending-p))) 2265 (while (and buffers (not (input-pending-p)))
2393 (set-buffer (car buffers)) 2266 (set-buffer (car buffers))
2394 (save-restriction 2267 (if (and (eq major-mode 'bibtex-mode)
2395 (widen) 2268 (not (eq (buffer-modified-tick)
2396 (if (and 2269 bibtex-buffer-last-parsed-tick)))
2397 (eq major-mode 'bibtex-mode) 2270 (save-restriction
2398 bibtex-maintain-sorted-entries 2271 (widen)
2399 (not 2272 ;; Output no progress messages in bibtex-parse-keys
2400 (eq (buffer-modified-tick) 2273 ;; because when in y-or-n-p that can hide the question.
2401 bibtex-buffer-last-parsed-tick))) 2274 (if (and (if bibtex-maintain-sorted-entries
2402 ;; Output no progress messages in bibtex-parse-keys 2275 (bibtex-parse-keys nil nil t)
2403 ;; because when in y-or-n-p that can hide the question. 2276 t)
2404 (if (bibtex-parse-keys nil nil t) 2277 ;; update bibtex-strings
2405 ;; successful operation --> remove buffer from list 2278 (bibtex-parse-strings strings-init t))
2406 (setq buffers (cdr buffers))) 2279
2407 ;; buffer is no BibTeX buffer or needs no parsing 2280 ;; remember that parsing was successful
2408 (setq buffers (cdr buffers)))))))) 2281 (setq bibtex-buffer-last-parsed-tick (buffer-modified-tick)))))
2409 2282 (setq buffers (cdr buffers))))))
2410(defun bibtex-complete (string-list &optional complete-strings) 2283
2411 ;; Complete word fragment before point to longest prefix of one 2284(defun bibtex-complete-internal (completions)
2412 ;; string defined in STRING-LIST. If point is not after the part of 2285 "Complete word fragment before point to longest prefix of one
2413 ;; a word, all strings are listed. If COMPLETE-STRINGS is non-nil, 2286string defined in list COMPLETIONS. If point is not after the part
2414 ;; add the strings defined in this buffer before cursor to 2287of a word, all strings are listed. Return completion."
2415 ;; STRING-LIST and remove surrounding delimiters if complete string
2416 ;; could be expanded.
2417 (let* ((case-fold-search t) 2288 (let* ((case-fold-search t)
2418 (end (point))
2419 (beg (save-excursion 2289 (beg (save-excursion
2420 (re-search-backward "[ \t{\"]") 2290 (re-search-backward "[ \t{\"]")
2421 (forward-char) 2291 (forward-char)
2422 (point))) 2292 (point)))
2293 (end (point))
2423 (part-of-word (buffer-substring-no-properties beg end)) 2294 (part-of-word (buffer-substring-no-properties beg end))
2424 (completions (copy-sequence string-list)) 2295 (completion (try-completion part-of-word completions)))
2425 (completion (save-excursion 2296 (cond ((not completion)
2426 (if complete-strings 2297 (error "Can't find completion for `%s'" part-of-word))
2427 (while (bibtex-search-backward-string) 2298 ((eq completion t)
2428 (let ((bounds (bibtex-search-backward-string))) 2299 part-of-word)
2429 (setq completions
2430 (cons
2431 (list
2432 (buffer-substring-no-properties
2433 (bibtex-start-of-reference-key-in-string bounds)
2434 (bibtex-end-of-reference-key-in-string bounds)))
2435 completions)))))
2436 (setq completions
2437 (sort completions
2438 (lambda(x y)
2439 (string-lessp
2440 (car x)
2441 (car y)))))
2442 (try-completion part-of-word completions))))
2443 (cond ((eq completion t)
2444 (if complete-strings
2445 ;; remove double-quotes or braces if field is no concatenation
2446 (save-excursion
2447 (bibtex-inside-field)
2448 (let* ((bounds (bibtex-enclosing-field)))
2449 (goto-char (bibtex-start-of-text-in-field bounds))
2450 (let ((boundaries (bibtex-parse-field-string)))
2451 (if (and boundaries (equal (cdr boundaries) (bibtex-end-of-text-in-field bounds)))
2452 (bibtex-remove-delimiters)))))))
2453 ((not completion)
2454 (error "Can't find completion for \"%s\"" part-of-word))
2455 ((not (string= part-of-word completion)) 2300 ((not (string= part-of-word completion))
2456 (delete-region beg end) 2301 (delete-region beg end)
2457 (insert completion) 2302 (insert completion)
2458 (if (and (assoc completion completions) 2303 completion)
2459 complete-strings)
2460 ;; remove double-quotes or braces if field is no concatenation
2461 (save-excursion
2462 (bibtex-inside-field)
2463 (let* ((bounds (bibtex-enclosing-field)))
2464 (goto-char (bibtex-start-of-text-in-field bounds))
2465 (let ((boundaries (bibtex-parse-field-string)))
2466 (if (and boundaries (equal (cdr boundaries) (bibtex-end-of-text-in-field bounds)))
2467 (bibtex-remove-delimiters)))))))
2468 (t 2304 (t
2469 (message "Making completion list...") 2305 (message "Making completion list...")
2470 (let ((list (all-completions part-of-word completions))) 2306 (with-output-to-temp-buffer "*Completions*"
2471 (with-output-to-temp-buffer "*Completions*" 2307 (display-completion-list (all-completions part-of-word
2472 (display-completion-list list))) 2308 completions)))
2473 (message "Making completion list...done"))))) 2309 (message "Making completion list...done")
2474 2310 nil))))
2475(defun bibtex-do-auto-fill () 2311
2476 (let ((fill-prefix 2312(defun bibtex-complete-string-cleanup (str)
2477 (make-string 2313 "Remove enclosing field delimiters."
2478 (+ bibtex-entry-offset bibtex-contline-indentation) ? ))) 2314 (let ((pair (assoc str bibtex-strings)))
2479 (do-auto-fill))) 2315 (when pair
2316 (if (cdr pair)
2317 (message "Abbreviation for `%s'" (cdr pair)))
2318 (save-excursion
2319 (bibtex-inside-field)
2320 (let ((bounds (bibtex-enclosing-field)))
2321 (goto-char (bibtex-start-of-text-in-field bounds))
2322 (let ((boundaries (bibtex-parse-field-string)))
2323 (if (and boundaries
2324 (equal (cdr boundaries)
2325 (bibtex-end-of-text-in-field bounds)))
2326 (bibtex-remove-delimiters))))))))
2327
2328(defun bibtex-choose-completion-string (choice buffer mini-p base-size)
2329 ;; Code borrowed from choose-completion-string:
2330 ;; We must duplicate the code from choose-completion-string
2331 ;; because it runs the hook choose-completion-string-functions
2332 ;; before it inserts the completion. But we want to do something
2333 ;; after the completion has been inserted.
2334 ;;
2335 ;; Insert the completion into the buffer where it was requested.
2336 (set-buffer buffer)
2337 (if base-size
2338 (delete-region (+ base-size (point-min))
2339 (point))
2340 ;; Delete the longest partial match for CHOICE
2341 ;; that can be found before point.
2342 (choose-completion-delete-max-match choice))
2343 (insert choice)
2344 (remove-text-properties (- (point) (length choice)) (point)
2345 '(mouse-face nil))
2346 ;; Update point in the window that BUFFER is showing in.
2347 (let ((window (get-buffer-window buffer t)))
2348 (set-window-point window (point))))
2480 2349
2481(defun bibtex-pop (arg direction) 2350(defun bibtex-pop (arg direction)
2482 ;; generic function to be used by bibtex-pop-previous and bibtex-pop-next 2351 "Generic function to be used by `bibtex-pop-previous' and `bibtex-pop-next'."
2483 (let (bibtex-help-message) 2352 (let (bibtex-help-message)
2484 (bibtex-find-text nil)) 2353 (bibtex-find-text nil))
2485 (save-excursion 2354 (save-excursion
2486 ;; parse current field 2355 ;; parse current field
2487 (bibtex-inside-field) 2356 (bibtex-inside-field)
2488 (let* ((case-fold-search t) 2357 (let* ((case-fold-search t)
2489 (bounds (bibtex-enclosing-field)) 2358 (bounds (bibtex-enclosing-field))
2490 (start-old-text (bibtex-start-of-text-in-field bounds)) 2359 (start-old-text (bibtex-start-of-text-in-field bounds))
2491 (stop-old-text (bibtex-end-of-text-in-field bounds)) 2360 (stop-old-text (bibtex-end-of-text-in-field bounds))
2492 (start-name (bibtex-start-of-name-in-field bounds)) 2361 (start-name (bibtex-start-of-name-in-field bounds))
2493 (stop-name (bibtex-end-of-name-in-field bounds)) 2362 (stop-name (bibtex-end-of-name-in-field bounds))
2494 (new-text)) 2363 ;; construct regexp for field with same name as this one,
2495 (goto-char start-name) 2364 ;; ignoring possible OPT's or ALT's
2496 ;; construct regexp for field with same name as this one, 2365 (field-name (progn
2497 ;; ignoring possible OPT's or ALT's 2366 (goto-char start-name)
2498 (let* ((field-name 2367 (buffer-substring-no-properties
2499 (buffer-substring-no-properties 2368 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)")
2500 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)") 2369 (match-end 0)
2501 (match-end 0) 2370 (point))
2502 (point)) 2371 stop-name))))
2503 stop-name))) 2372 ;; if executed several times in a row, start each search where
2504 ;; if executed several times in a row, start each search where 2373 ;; the last one was finished
2505 ;; the last one was finished 2374 (unless (eq last-command 'bibtex-pop)
2506 (cond ((eq last-command 'bibtex-pop) t) 2375 (bibtex-enclosing-entry-maybe-empty-head)
2507 (t 2376 (setq bibtex-pop-previous-search-point (match-beginning 0)
2508 (bibtex-enclosing-entry-maybe-empty-head) 2377 bibtex-pop-next-search-point (point)))
2509 (setq 2378 (if (eq direction 'previous)
2510 bibtex-pop-previous-search-point (match-beginning 0) 2379 (goto-char bibtex-pop-previous-search-point)
2511 bibtex-pop-next-search-point (point)))) 2380 (goto-char bibtex-pop-next-search-point))
2512 (if (eq direction 'previous) 2381 ;; Now search for arg'th previous/next similar field
2513 (goto-char bibtex-pop-previous-search-point) 2382 (let (bounds failure new-text)
2514 (goto-char bibtex-pop-next-search-point)) 2383 (while (and (not failure)
2515 ;; Now search for arg'th previous/next similar field 2384 (> arg 0))
2516 (let ((bounds nil) 2385 (cond ((eq direction 'previous)
2517 (failure nil)) 2386 (if (setq bounds (bibtex-search-backward-field field-name))
2518 (while (and (not failure) (> arg 0)) 2387 (goto-char (bibtex-start-of-field bounds))
2519 (cond ((eq direction 'previous) 2388 (setq failure t)))
2520 (setq bounds (bibtex-search-backward-field field-name (point-min))) 2389 ((eq direction 'next)
2521 (if bounds 2390 (if (setq bounds (bibtex-search-forward-field field-name))
2522 (goto-char (bibtex-start-of-field bounds)) 2391 (goto-char (bibtex-end-of-field bounds))
2523 (setq failure t))) 2392 (setq failure t))))
2524 ((eq direction 'next) 2393 (setq arg (- arg 1)))
2525 (setq bounds (bibtex-search-forward-field field-name (point-max))) 2394 (if failure
2526 (if bounds 2395 (error "No %s matching BibTeX field"
2527 (goto-char (bibtex-end-of-field bounds)) 2396 (if (eq direction 'previous) "previous" "next"))
2528 (setq failure t)))) 2397 ;; Found a matching field. Remember boundaries.
2529 (setq arg (- arg 1))) 2398 (setq bibtex-pop-previous-search-point (bibtex-start-of-field bounds)
2530 (if failure 2399 bibtex-pop-next-search-point (bibtex-end-of-field bounds)
2531 (error (if (eq direction 'previous) 2400 new-text (bibtex-text-in-field-bounds bounds))
2532 "No previous matching BibTeX field." 2401 (bibtex-flash-head)
2533 "No next matching BibTeX field.")) 2402 ;; Go back to where we started, delete old text, and pop new.
2534 ;; Found a matching field. Remember boundaries. 2403 (goto-char stop-old-text)
2535 (setq bibtex-pop-previous-search-point (bibtex-start-of-field bounds)) 2404 (delete-region start-old-text stop-old-text)
2536 (setq bibtex-pop-next-search-point (bibtex-end-of-field bounds)) 2405 (insert new-text)))))
2537 (setq new-text
2538 (buffer-substring-no-properties
2539 (bibtex-start-of-text-in-field bounds)
2540 (bibtex-end-of-text-in-field bounds)))
2541 (bibtex-flash-head)
2542 ;; Go back to where we started, delete old text, and pop new.
2543 (goto-char stop-old-text)
2544 (delete-region start-old-text stop-old-text)
2545 (insert new-text))))))
2546 (let (bibtex-help-message) 2406 (let (bibtex-help-message)
2547 (bibtex-find-text nil)) 2407 (bibtex-find-text nil))
2548 (setq this-command 'bibtex-pop)) 2408 (setq this-command 'bibtex-pop))
@@ -2554,13 +2414,6 @@ The generation algorithm works as follows:
2554(defun bibtex-mode () 2414(defun bibtex-mode ()
2555 "Major mode for editing BibTeX files. 2415 "Major mode for editing BibTeX files.
2556 2416
2557To submit a problem report, enter \\[bibtex-submit-bug-report] from a
2558BibTeX mode buffer. This automatically sets up a mail buffer with
2559version information already added. You just need to add a description
2560of the problem, including a reproducible test case and send the
2561message.
2562
2563
2564General information on working with BibTeX mode: 2417General information on working with BibTeX mode:
2565 2418
2566You should use commands as \\[bibtex-Book] to get a template for a 2419You should use commands as \\[bibtex-Book] to get a template for a
@@ -2570,7 +2423,7 @@ in all desired fields in the entry, you should clean the new entry
2570with command \\[bibtex-clean-entry]. 2423with command \\[bibtex-clean-entry].
2571 2424
2572Some features of BibTeX mode are available only by setting variable 2425Some features of BibTeX mode are available only by setting variable
2573bibtex-maintain-sorted-entries to t. However, then BibTeX mode will 2426`bibtex-maintain-sorted-entries' to t. However, then BibTeX mode will
2574work with buffer containing only valid (syntactical correct) entries 2427work with buffer containing only valid (syntactical correct) entries
2575and with entries being sorted. This is usually the case, if you have 2428and with entries being sorted. This is usually the case, if you have
2576created a buffer completely with BibTeX mode and finished every new 2429created a buffer completely with BibTeX mode and finished every new
@@ -2598,10 +2451,10 @@ current field.
2598The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT 2451The command \\[bibtex-clean-entry] cleans the current entry, i.e. it removes OPT/ALT
2599from all non-empty optional or alternative fields, checks that no required 2452from all non-empty optional or alternative fields, checks that no required
2600fields are empty, and does some formatting dependent on the value of 2453fields are empty, and does some formatting dependent on the value of
2601bibtex-entry-format. 2454`bibtex-entry-format'.
2602Note: some functions in BibTeX mode depend on entries being in a special 2455Note: some functions in BibTeX mode depend on entries being in a special
2603format (all fields beginning on separate lines), so it is usually a bad 2456format (all fields beginning on separate lines), so it is usually a bad
2604idea to remove `realign' from bibtex-entry-format. 2457idea to remove `realign' from `bibtex-entry-format'.
2605 2458
2606Use \\[bibtex-find-text] to position the cursor at the end of the current field. 2459Use \\[bibtex-find-text] to position the cursor at the end of the current field.
2607Use \\[bibtex-next-field] to move to end of the next field. 2460Use \\[bibtex-next-field] to move to end of the next field.
@@ -2609,40 +2462,39 @@ Use \\[bibtex-next-field] to move to end of the next field.
2609The following may be of interest as well: 2462The following may be of interest as well:
2610 2463
2611 Functions: 2464 Functions:
2612 bibtex-entry 2465 `bibtex-entry'
2613 bibtex-kill-entry 2466 `bibtex-kill-entry'
2614 bibtex-yank-pop 2467 `bibtex-yank-pop'
2615 bibtex-pop-previous 2468 `bibtex-pop-previous'
2616 bibtex-pop-next 2469 `bibtex-pop-next'
2617 bibtex-complete-string 2470 `bibtex-complete'
2618 bibtex-complete-key 2471 `bibtex-print-help-message'
2619 bibtex-print-help-message 2472 `bibtex-generate-autokey'
2620 bibtex-generate-autokey 2473 `bibtex-beginning-of-entry'
2621 bibtex-beginning-of-entry 2474 `bibtex-end-of-entry'
2622 bibtex-end-of-entry 2475 `bibtex-reposition-window'
2623 bibtex-reposition-window 2476 `bibtex-mark-entry'
2624 bibtex-mark-entry 2477 `bibtex-ispell-abstract'
2625 bibtex-ispell-abstract 2478 `bibtex-ispell-entry'
2626 bibtex-ispell-entry 2479 `bibtex-narrow-to-entry'
2627 bibtex-narrow-to-entry 2480 `bibtex-sort-buffer'
2628 bibtex-sort-buffer 2481 `bibtex-validate'
2629 bibtex-validate 2482 `bibtex-count'
2630 bibtex-count 2483 `bibtex-fill-entry'
2631 bibtex-fill-entry 2484 `bibtex-reformat'
2632 bibtex-reformat 2485 `bibtex-convert-alien'
2633 bibtex-convert-alien
2634 2486
2635 Variables: 2487 Variables:
2636 bibtex-field-delimiters 2488 `bibtex-field-delimiters'
2637 bibtex-include-OPTcrossref 2489 `bibtex-include-OPTcrossref'
2638 bibtex-include-OPTkey 2490 `bibtex-include-OPTkey'
2639 bibtex-user-optional-fields 2491 `bibtex-user-optional-fields'
2640 bibtex-entry-format 2492 `bibtex-entry-format'
2641 bibtex-sort-ignore-string-entries 2493 `bibtex-sort-ignore-string-entries'
2642 bibtex-maintain-sorted-entries 2494 `bibtex-maintain-sorted-entries'
2643 bibtex-entry-field-alist 2495 `bibtex-entry-field-alist'
2644 bibtex-predefined-strings 2496 `bibtex-predefined-strings'
2645 bibtex-string-files 2497 `bibtex-string-files'
2646 2498
2647--------------------------------------------------------- 2499---------------------------------------------------------
2648Entry to BibTeX mode calls the value of `bibtex-mode-hook' if that value is 2500Entry to BibTeX mode calls the value of `bibtex-mode-hook' if that value is
@@ -2655,231 +2507,91 @@ non-nil.
2655 (setq major-mode 'bibtex-mode) 2507 (setq major-mode 'bibtex-mode)
2656 (setq mode-name "BibTeX") 2508 (setq mode-name "BibTeX")
2657 (set-syntax-table bibtex-mode-syntax-table) 2509 (set-syntax-table bibtex-mode-syntax-table)
2658 (setq bibtex-strings bibtex-predefined-strings) 2510 (make-local-variable 'bibtex-strings)
2659 (mapcar 2511 (make-local-variable 'bibtex-reference-keys)
2660 (lambda (filename) 2512 (make-local-variable 'bibtex-buffer-last-parsed-tick)
2661 ;; collect pathnames 2513 (bibtex-parse-strings (bibtex-string-files-init))
2662 (let* ((path (if bibtex-string-file-path
2663 bibtex-string-file-path
2664 "."))
2665 (dirs (split-string path ":+"))
2666 (filename (if (string-match "\.bib$" filename)
2667 filename
2668 (concat filename ".bib")))
2669 fullfilename
2670 (item 0)
2671 (size (length dirs)))
2672 ;; test filenames
2673 (while (and
2674 (< item size)
2675 (not (file-readable-p
2676 (setq fullfilename
2677 (concat (elt dirs item) "/" filename)))))
2678 (setq item (1+ item)))
2679 (if (< item size)
2680 ;; file was found
2681 (let* ((case-fold-search t)
2682 (compl nil))
2683 (with-temp-buffer
2684 (insert-file-contents fullfilename)
2685 (goto-char (point-min))
2686 (let ((bounds (bibtex-search-forward-string)))
2687 (while bounds
2688 (setq compl
2689 (cons (list (buffer-substring-no-properties
2690 (bibtex-start-of-reference-key-in-string bounds)
2691 (bibtex-end-of-reference-key-in-string bounds)))
2692 compl))
2693 (goto-char (bibtex-end-of-string bounds))
2694 (setq bounds (bibtex-search-forward-string)))))
2695 (setq bibtex-strings (append bibtex-strings (nreverse compl))))
2696 (error
2697 "File %s not in paths defined by bibtex-string-file-path variable"
2698 filename))))
2699 bibtex-string-files)
2700 (if bibtex-maintain-sorted-entries 2514 (if bibtex-maintain-sorted-entries
2701 (bibtex-run-with-idle-timer 2515 (bibtex-parse-keys nil nil t))
2702 1 nil
2703 (lambda ()
2704 (bibtex-parse-keys nil nil t))))
2705 ;; to get buffer parsed once if everything else (including things
2706 ;; installed in bibtex-mode-hook) has done its work
2707 (if (not bibtex-parse-idle-timer)
2708 (setq bibtex-parse-idle-timer
2709 (bibtex-run-with-idle-timer
2710 bibtex-parse-keys-timeout t
2711 'bibtex-parse-buffers-stealthily)))
2712 ;; Install stealthy parse function if not already installed 2516 ;; Install stealthy parse function if not already installed
2713 (make-local-variable 'paragraph-start) 2517 (unless bibtex-parse-idle-timer
2714 (setq paragraph-start "[ \f\n\t]*$") 2518 (setq bibtex-parse-idle-timer (bibtex-run-with-idle-timer
2715 (make-local-variable 'comment-start) 2519 bibtex-parse-keys-timeout t
2716 (setq comment-start bibtex-comment-start) 2520 'bibtex-parse-buffers-stealthily)))
2717 (make-local-variable 'comment-start-skip) 2521 (set (make-local-variable 'paragraph-start) "[ \f\n\t]*$")
2718 (setq comment-start-skip bibtex-comment-start) 2522 (set (make-local-variable 'comment-start) bibtex-comment-start)
2719 (make-local-variable 'comment-column) 2523 (set (make-local-variable 'comment-start-skip)
2720 (setq comment-column 0) 2524 (concat (regexp-quote bibtex-comment-start) "\\>[ \t]*"))
2721 (make-local-variable 'defun-prompt-regexp) 2525 (set (make-local-variable 'comment-column) 0)
2722 (setq defun-prompt-regexp "^@[a-zA-Z0-9]+") 2526 (set (make-local-variable 'defun-prompt-regexp) "^[ \t]*@[a-zA-Z0-9]+")
2723 (make-local-variable 'outline-regexp) 2527 (set (make-local-variable 'outline-regexp) "[ \t]*@")
2724 (setq outline-regexp "@") 2528 (set (make-local-variable 'fill-prefix) (make-string (+ bibtex-entry-offset
2725 (make-local-variable 'normal-auto-fill-function) 2529 bibtex-contline-indentation)
2726 (setq normal-auto-fill-function 'bibtex-do-auto-fill) 2530 ? ))
2727 (make-local-variable 'font-lock-defaults) 2531 (set (make-local-variable 'font-lock-defaults)
2728 (setq font-lock-defaults 2532 '(bibtex-font-lock-keywords
2729 '(bibtex-font-lock-keywords 2533 nil t ((?$ . "\"")
2730 nil t ((?$ . "\"") 2534 ;; Mathematical expressions should be fontified as strings
2731 ;; Mathematical expressions should be fontified as strings 2535 (?\" . ".")
2732 (?\" . ".") 2536 ;; Quotes are field delimiters and quote-delimited
2733 ;; Quotes are field delimiters and quote-delimited 2537 ;; entries should be fontified in the same way as
2734 ;; entries should be fontified in the same way as 2538 ;; brace-delimited ones
2735 ;; brace-delimited ones 2539 )
2736 ))) 2540 nil
2737 (make-local-variable 'font-lock-mark-block-function) 2541 (font-lock-syntactic-keywords . bibtex-font-lock-syntactic-keywords)))
2738 (setq font-lock-mark-block-function 2542 (set (make-local-variable 'font-lock-mark-block-function)
2739 (lambda () 2543 (lambda ()
2740 (set-mark (bibtex-end-of-entry)) 2544 (set-mark (bibtex-end-of-entry))
2741 (bibtex-beginning-of-entry))) 2545 (bibtex-beginning-of-entry)))
2742 (setq imenu-generic-expression 2546 (setq imenu-generic-expression
2743 (list (list nil bibtex-entry-head bibtex-key-in-head))) 2547 (list (list nil bibtex-entry-head bibtex-key-in-head)))
2548 (make-local-variable 'choose-completion-string-functions)
2744 (setq imenu-case-fold-search t) 2549 (setq imenu-case-fold-search t)
2745 ;; XEmacs needs easy-menu-add, Emacs does not care 2550 ;; XEmacs needs easy-menu-add, Emacs does not care
2746 (easy-menu-add bibtex-edit-menu) 2551 (easy-menu-add bibtex-edit-menu)
2747 (easy-menu-add bibtex-entry-menu) 2552 (easy-menu-add bibtex-entry-menu)
2748 (run-hooks 'bibtex-mode-hook)) 2553 (run-hooks 'bibtex-mode-hook))
2749 2554
2750(defun bibtex-submit-bug-report ()
2751 "Submit via mail a bug report on bibtex.el."
2752 (interactive)
2753 (if (y-or-n-p "Do you want to submit a bug report on BibTeX mode? ")
2754 (progn
2755 (require 'reporter)
2756 (let ((reporter-prompt-for-summary-p t))
2757 (reporter-submit-bug-report
2758 bibtex-maintainer-address
2759 "bibtex.el"
2760 (list
2761 'system-configuration
2762 'system-configuration-options
2763 'bibtex-mode-hook
2764 'bibtex-parse-keys-timeout
2765 ;; possible general errors
2766 'bibtex-sort-ignore-string-entries
2767 'bibtex-maintain-sorted-entries
2768 'bibtex-entry-delimiters
2769 'bibtex-field-delimiters
2770 'bibtex-comma-after-last-field
2771 'bibtex-entry-offset
2772 'bibtex-field-indentation
2773 'bibtex-text-indentation
2774 'bibtex-contline-indentation
2775 'bibtex-align-at-equal-sign
2776 ;; possible sorting and parsing bugs
2777 'bibtex-entry-format
2778 'bibtex-add-entry-hook
2779 'bibtex-clean-entry-hook
2780 ;; possible cleaning error
2781 'bibtex-user-optional-fields
2782 ;; possible format error
2783 'bibtex-predefined-month-strings
2784 'bibtex-predefined-strings
2785 'bibtex-string-files
2786 'bibtex-string-file-path
2787 ;; possible format error
2788 'bibtex-font-lock-keywords
2789 ;; possible bugs regarding fontlocking
2790 'bibtex-autokey-prefix-string
2791 'bibtex-autokey-names
2792 'bibtex-autokey-names-stretch
2793 'bibtex-autokey-additional-names
2794 'bibtex-autokey-transcriptions
2795 'bibtex-autokey-name-change-strings
2796 'bibtex-autokey-name-case-convert
2797 'bibtex-autokey-name-length
2798 'bibtex-autokey-name-separator
2799 'bibtex-autokey-year-length
2800 'bibtex-autokey-year-use-crossref-entry
2801 'bibtex-autokey-titlewords
2802 'bibtex-autokey-title-terminators
2803 'bibtex-autokey-titlewords-stretch
2804 'bibtex-autokey-titleword-ignore
2805 'bibtex-autokey-titleword-case-convert
2806 'bibtex-autokey-titleword-abbrevs
2807 'bibtex-autokey-titleword-change-strings
2808 'bibtex-autokey-titleword-length
2809 'bibtex-autokey-titleword-separator
2810 'bibtex-autokey-name-year-separator
2811 'bibtex-autokey-year-title-separator
2812 'bibtex-autokey-edit-before-use
2813 'bibtex-autokey-before-presentation-function
2814 ;; possible bugs regarding automatic reference keys
2815 'bibtex-entry-field-alist
2816 ;; possible format error
2817 'bibtex-help-message
2818 'bibtex-include-OPTcrossref
2819 'bibtex-include-OPTkey
2820 'bibtex-field-kill-ring-max
2821 'bibtex-entry-kill-ring-max
2822 ;; user variables which shouldn't cause any errors
2823 )
2824 nil nil
2825 (concat bibtex-maintainer-salutation "
2826
2827I want to report a bug on Emacs BibTeX mode.
2828
2829I've read the `Bugs' section in the `Emacs' info page, so I know how
2830to make a clear and unambiguous report. I have started a fresh Emacs
2831via `"invocation-name " --no-init-file --no-site-file', thereafter (in
2832case I'm reporting on a version of `bibtex.el' which is not part of
2833the standard emacs distribution) I loaded the questionable version
2834of `bibtex.el' with `M-x load-file', and then, to produce the buggy
2835behaviour, I did the following:")))
2836 (message nil))))
2837
2838(defun bibtex-entry (entry-type) 2555(defun bibtex-entry (entry-type)
2839 "Insert a new BibTeX entry. 2556 "Insert a new BibTeX entry.
2840After insertion it calls the functions in `bibtex-add-entry-hook'." 2557After insertion it calls the functions in `bibtex-add-entry-hook'."
2841 (interactive (let* ((completion-ignore-case t) 2558 (interactive (let* ((completion-ignore-case t)
2842 (e-t (completing-read 2559 (e-t (completing-read
2843 "Entry Type: " 2560 "Entry Type: "
2844 bibtex-entry-field-alist 2561 bibtex-entry-field-alist
2845 nil t nil 'bibtex-entry-type-history))) 2562 nil t nil 'bibtex-entry-type-history)))
2846 (list e-t))) 2563 (list e-t)))
2847 (if (not bibtex-reference-keys) 2564 (unless (consp bibtex-reference-keys) (bibtex-parse-keys nil t))
2848 (bibtex-parse-keys nil t))
2849 (let* (required 2565 (let* (required
2850 optional 2566 optional
2851 (key 2567 (key (if bibtex-maintain-sorted-entries
2852 (if bibtex-maintain-sorted-entries 2568 (completing-read (format "%s key: " entry-type)
2853 (completing-read 2569 bibtex-reference-keys
2854 (format "%s key: " entry-type) 2570 nil nil nil 'bibtex-key-history)))
2855 bibtex-reference-keys nil nil nil 'bibtex-key-history)))
2856 (e (assoc-ignore-case entry-type bibtex-entry-field-alist)) 2571 (e (assoc-ignore-case entry-type bibtex-entry-field-alist))
2857 (r-n-o (elt e 1)) 2572 (r-n-o (elt e 1))
2858 (c-ref (elt e 2))) 2573 (c-ref (elt e 2)))
2859 (if (not e) 2574 (if (not e)
2860 (error "Bibtex entry type %s not defined" entry-type)) 2575 (error "Bibtex entry type %s not defined" entry-type))
2861 (if (and 2576 (if (and (member entry-type bibtex-include-OPTcrossref)
2862 (member entry-type bibtex-include-OPTcrossref) 2577 c-ref)
2863 c-ref)
2864 (setq required (elt c-ref 0) 2578 (setq required (elt c-ref 0)
2865 optional (elt c-ref 1)) 2579 optional (elt c-ref 1))
2866 (setq required (elt r-n-o 0) 2580 (setq required (elt r-n-o 0)
2867 optional (elt r-n-o 1))) 2581 optional (elt r-n-o 1)))
2868 (if bibtex-maintain-sorted-entries 2582 (unless (bibtex-prepare-new-entry (list key nil entry-type))
2869 (bibtex-find-entry-location key) 2583 (error "Entry with key `%s' already exists" key))
2870 (bibtex-move-outside-of-entry))
2871 (indent-to-column bibtex-entry-offset) 2584 (indent-to-column bibtex-entry-offset)
2872 (insert "@" entry-type (bibtex-entry-left-delimiter)) 2585 (insert "@" entry-type (bibtex-entry-left-delimiter))
2873 (if key 2586 (if key
2874 (insert key)) 2587 (insert key))
2875 (save-excursion 2588 (save-excursion
2876 (mapcar 'bibtex-make-field required) 2589 (mapcar 'bibtex-make-field required)
2877 (if (member entry-type bibtex-include-OPTcrossref) 2590 (if (member entry-type bibtex-include-OPTcrossref)
2878 (bibtex-make-optional-field '("crossref"))) 2591 (bibtex-make-optional-field '("crossref")))
2879 (if bibtex-include-OPTkey 2592 (if bibtex-include-OPTkey
2880 (if (or 2593 (if (or (stringp bibtex-include-OPTkey)
2881 (stringp bibtex-include-OPTkey) 2594 (fboundp bibtex-include-OPTkey))
2882 (fboundp bibtex-include-OPTkey))
2883 (bibtex-make-optional-field 2595 (bibtex-make-optional-field
2884 (list "key" nil bibtex-include-OPTkey)) 2596 (list "key" nil bibtex-include-OPTkey))
2885 (bibtex-make-optional-field '("key")))) 2597 (bibtex-make-optional-field '("key"))))
@@ -2894,88 +2606,62 @@ After insertion it calls the functions in `bibtex-add-entry-hook'."
2894 (run-hooks 'bibtex-add-entry-hook))) 2606 (run-hooks 'bibtex-add-entry-hook)))
2895 2607
2896(defun bibtex-print-help-message () 2608(defun bibtex-print-help-message ()
2897 "Prints helpful information about current field in current BibTeX entry." 2609 "Print helpful information about current field in current BibTeX entry."
2898 (interactive) 2610 (interactive)
2899 (let* ((case-fold-search t) 2611 (save-excursion
2900 (pnt (point)) 2612 (let* ((case-fold-search t)
2901 (field-name 2613 (bounds (bibtex-enclosing-field))
2902 (let* ((bounds (bibtex-enclosing-field)) 2614 (mb (bibtex-start-of-name-in-field bounds))
2903 (mb (bibtex-start-of-name-in-field bounds)) 2615 (field-name (buffer-substring-no-properties
2904 (me (bibtex-end-of-name-in-field bounds))) 2616 (if (progn (goto-char mb)
2905 (goto-char mb) 2617 (looking-at "OPT\\|ALT"))
2906 (buffer-substring-no-properties 2618 (+ 3 mb) mb)
2907 (if (looking-at "OPT\\|ALT") 2619 (bibtex-end-of-name-in-field bounds)))
2908 (+ 3 mb) 2620 (entry-type (progn (re-search-backward
2909 mb) 2621 bibtex-entry-maybe-empty-head nil t)
2910 me))) 2622 (bibtex-type-in-head)))
2911 (entry-type 2623 (entry-list (assoc-ignore-case entry-type
2912 (progn 2624 bibtex-entry-field-alist))
2913 (re-search-backward 2625 (c-r-list (elt entry-list 2))
2914 bibtex-entry-maybe-empty-head nil t) 2626 (req-opt-list (if (and (member entry-type
2915 (buffer-substring-no-properties 2627 bibtex-include-OPTcrossref)
2916 (1+ (match-beginning bibtex-type-in-head)) 2628 c-r-list)
2917 (match-end bibtex-type-in-head)))) 2629 c-r-list
2918 (entry-list 2630 (elt entry-list 1)))
2919 (assoc-ignore-case entry-type 2631 (list-of-entries (append (elt req-opt-list 0)
2920 bibtex-entry-field-alist)) 2632 (elt req-opt-list 1)
2921 (c-r-list (elt entry-list 2)) 2633 bibtex-user-optional-fields
2922 (req-opt-list 2634 (if (member entry-type
2923 (if (and 2635 bibtex-include-OPTcrossref)
2924 (member entry-type bibtex-include-OPTcrossref) 2636 '(("crossref" "Reference key of the cross-referenced entry")))
2925 c-r-list) 2637 (if bibtex-include-OPTkey
2926 c-r-list 2638 '(("key" "Used for reference key creation if author and editor fields are missing")))))
2927 (elt entry-list 1))) 2639 (comment (assoc-ignore-case field-name list-of-entries)))
2928 (list-of-entries (append
2929 (elt req-opt-list 0)
2930 (elt req-opt-list 1)
2931 bibtex-user-optional-fields
2932 (if (member
2933 entry-type
2934 bibtex-include-OPTcrossref)
2935 '(("crossref"
2936 "Reference key of the crossreferenced entry")))
2937 (if bibtex-include-OPTkey
2938 '(("key"
2939 "Used for reference key creation if author and editor fields are missing"))))))
2940 (goto-char pnt)
2941 (let ((comment (assoc-ignore-case field-name list-of-entries)))
2942 (if comment 2640 (if comment
2943 (message (elt comment 1)) 2641 (message (elt comment 1))
2944 (message "NO COMMENT AVAILABLE"))))) 2642 (message "No comment available")))))
2945 2643
2946(defun bibtex-make-field (e-t &optional called-by-yank) 2644(defun bibtex-make-field (e-t &optional called-by-yank)
2947 "Makes a field named E-T in current BibTeX entry. 2645 "Make a field named E-T in current BibTeX entry."
2948This function is for interactive and non-interactive purposes. To call 2646 (interactive
2949it interactively, just give it no arguments and enter the field name 2647 (list (let* ((entry-type
2950using the minibuffer." 2648 (save-excursion
2951 (interactive "*P") 2649 (bibtex-enclosing-entry-maybe-empty-head)
2952 (if (not e-t) 2650 (bibtex-type-in-head)))
2953 (setq 2651 (fl (cadr (assoc-ignore-case
2954 e-t 2652 entry-type bibtex-entry-field-alist)))
2955 (let* ((entry-type 2653 (field-list (append (elt fl 0)
2956 (save-excursion 2654 (elt fl 1)
2957 (bibtex-enclosing-entry-maybe-empty-head) 2655 bibtex-user-optional-fields
2958 (buffer-substring-no-properties 2656 (if bibtex-include-OPTcrossref
2959 (1+ (match-beginning bibtex-type-in-head)) 2657 '(("crossref" nil)))
2960 (match-end bibtex-type-in-head)))) 2658 (if bibtex-include-OPTkey
2961 (fl 2659 '(("key" nil)))))
2962 (car (cdr (assoc-ignore-case 2660 (completion-ignore-case t))
2963 entry-type bibtex-entry-field-alist)))) 2661 (completing-read "BibTeX field name: " field-list
2964 (field-list 2662 nil nil nil bibtex-field-history))))
2965 (append 2663 (unless (consp e-t)
2966 (elt fl 0) (elt fl 1) bibtex-user-optional-fields 2664 (setq e-t (list e-t)))
2967 (if bibtex-include-OPTcrossref '(("crossref" nil)))
2968 (if bibtex-include-OPTkey '(("key" nil)))))
2969 (completion-ignore-case t))
2970 (completing-read
2971 "BibTeX field name: " field-list
2972 nil nil nil bibtex-field-history))))
2973 (if (not (consp e-t))
2974 (setq e-t (list e-t)))
2975 (if (equal (length e-t) 1)
2976 (setq e-t (append e-t (list ""))))
2977 (if (equal (length e-t) 2)
2978 (setq e-t (append e-t (list ""))))
2979 (let ((name (if (elt e-t 3) 2665 (let ((name (if (elt e-t 3)
2980 (concat "ALT" (car e-t)) 2666 (concat "ALT" (car e-t))
2981 (car e-t)))) 2667 (car e-t))))
@@ -2985,27 +2671,24 @@ using the minibuffer."
2985 (if (looking-at "[}\"]") 2671 (if (looking-at "[}\"]")
2986 (forward-char)))) 2672 (forward-char))))
2987 (insert ",\n") 2673 (insert ",\n")
2988 (indent-to-column 2674 (indent-to-column (+ bibtex-entry-offset bibtex-field-indentation))
2989 (+ bibtex-entry-offset bibtex-field-indentation))
2990 (insert name " ") 2675 (insert name " ")
2991 (if bibtex-align-at-equal-sign 2676 (if bibtex-align-at-equal-sign
2992 (indent-to-column 2677 (indent-to-column (+ bibtex-entry-offset
2993 (+ bibtex-entry-offset (- bibtex-text-indentation 2)))) 2678 (- bibtex-text-indentation 2))))
2994 (insert "= ") 2679 (insert "= ")
2995 (if (not bibtex-align-at-equal-sign) 2680 (if (not bibtex-align-at-equal-sign)
2996 (indent-to-column 2681 (indent-to-column (+ bibtex-entry-offset
2997 (+ bibtex-entry-offset bibtex-text-indentation))) 2682 bibtex-text-indentation)))
2998 (insert (if called-by-yank 2683 (insert (if called-by-yank
2999 "" 2684 ""
3000 (bibtex-field-left-delimiter)) 2685 (bibtex-field-left-delimiter))
3001 (let ((init (elt e-t 2))) 2686 (let ((init (elt e-t 2)))
3002 (cond 2687 (cond ((stringp init)
3003 ((stringp init) 2688 init)
3004 init) 2689 ((fboundp init)
3005 ((fboundp init) 2690 (funcall init))
3006 (funcall init)) 2691 (t "")))
3007 (t
3008 (error "%s is neither a string nor a function" init))))
3009 (if called-by-yank 2692 (if called-by-yank
3010 "" 2693 ""
3011 (bibtex-field-right-delimiter))) 2694 (bibtex-field-right-delimiter)))
@@ -3013,7 +2696,7 @@ using the minibuffer."
3013 (forward-char -1)))) 2696 (forward-char -1))))
3014 2697
3015(defun bibtex-beginning-of-entry () 2698(defun bibtex-beginning-of-entry ()
3016 "Move to beginning of BibTeX entry. 2699 "Move to beginning of BibTeX entry (beginning of line).
3017If inside an entry, move to the beginning of it, otherwise move to the 2700If inside an entry, move to the beginning of it, otherwise move to the
3018beginning of the previous entry. 2701beginning of the previous entry.
3019If called from a program, this function returns the new location of point." 2702If called from a program, this function returns the new location of point."
@@ -3024,58 +2707,46 @@ If called from a program, this function returns the new location of point."
3024 (re-search-backward "^[ \t]*@" nil 'move)) 2707 (re-search-backward "^[ \t]*@" nil 'move))
3025 2708
3026(defun bibtex-end-of-entry () 2709(defun bibtex-end-of-entry ()
3027 "Move to end of BibTeX entry. 2710 "Move to end of BibTeX entry (past the closing brace).
3028If inside an entry, move to the end of it, otherwise move to the end 2711If inside an entry, move to the end of it, otherwise move to the end
3029of the previous entry. 2712of the previous entry. Do not move if ahead of first entry.
3030If called from a program, this function returns the new location of point." 2713If called from a program, this function returns the new location of point."
3031 (interactive) 2714 (interactive)
3032 (let* ((case-fold-search t) 2715 (let ((case-fold-search t)
3033 (valid-entry-head 2716 (org (point))
3034 (concat "[ \t]*@[ \t]*\\(" 2717 (pnt (bibtex-beginning-of-entry))
3035 (mapconcat 2718 err bounds)
3036 (lambda (type) 2719 (cond ((setq bounds (bibtex-parse-string))
3037 (concat "\\(" (car type) "\\)")) 2720 (goto-char (bibtex-end-of-string bounds)))
3038 bibtex-entry-field-alist 2721 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")
3039 "\\|") 2722 (goto-char (match-end 0))
3040 "\\)")) 2723 (cond ((looking-at "(")
3041 (org (point)) 2724 (unless (re-search-forward ")[ \t]*\n\n" nil 'move)
3042 (pnt (bibtex-beginning-of-entry)) 2725 (setq err t)))
3043 err) 2726 ((looking-at "{")
3044 (cond 2727 (unless (re-search-forward "}[ \t]*\n\n" nil 'move)
3045 ((bibtex-parse-string) 2728 (setq err t)))
3046 (let ((bounds (bibtex-parse-string))) 2729 (t
3047 (goto-char (bibtex-end-of-string bounds)))) 2730 (setq err t)))
3048 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*") 2731 (unless err
3049 (goto-char (match-end 0)) 2732 (goto-char (match-beginning 0))
3050 (cond 2733 (forward-char)))
3051 ((looking-at "(") 2734 ((looking-at bibtex-valid-entry-whitespace-re)
3052 (if (not (re-search-forward ")[ \t]*\n\n" nil 'move)) 2735 (bibtex-search-entry t nil t)
3053 (setq err t))) 2736 (unless (equal (match-beginning 0) pnt)
3054 ((looking-at "{") 2737 (setq err t)))
3055 (if (not (re-search-forward "}[ \t]*\n\n" nil 'move)) 2738 (t
3056 (setq err t))) 2739 (if (interactive-p)
3057 (t 2740 (message "Not on a known BibTeX entry."))
3058 (setq err t))) 2741 (goto-char org)))
3059 (if (not err) 2742 (when err
3060 (progn 2743 (goto-char pnt)
3061 (goto-char (match-beginning 0)) 2744 (error "Syntactically incorrect BibTeX entry starts here")))
3062 (forward-char))))
3063 ((looking-at valid-entry-head)
3064 (bibtex-search-entry t nil t)
3065 (if (not (equal (match-beginning 0) pnt))
3066 (setq err t)))
3067 (t
3068 (if (interactive-p)
3069 (message "Not on a known BibTeX entry."))
3070 (goto-char org)))
3071 (if err
3072 (progn
3073 (goto-char pnt)
3074 (error "Syntactical incorrect entry starts here"))))
3075 (point)) 2745 (point))
3076 2746
3077(defun bibtex-reposition-window (arg) 2747(defun bibtex-reposition-window (&optional arg)
3078 "Make the current BibTeX entry visible." 2748 "Make the current BibTeX entry visible.
2749Optional argument ARG is exactly as in `recenter'."
3079 (interactive "P") 2750 (interactive "P")
3080 (save-excursion 2751 (save-excursion
3081 (goto-char 2752 (goto-char
@@ -3094,155 +2765,210 @@ With prefix argument COUNT-STRING-ENTRIES it counts all entries,
3094otherwise it counts all except Strings. 2765otherwise it counts all except Strings.
3095If mark is active it counts entries in region, if not in whole buffer." 2766If mark is active it counts entries in region, if not in whole buffer."
3096 (interactive "P") 2767 (interactive "P")
3097 (let ((pnt (point)) 2768 (let ((number 0)
3098 (start-point
3099 (if (bibtex-mark-active)
3100 (region-beginning)
3101 (bibtex-beginning-of-first-entry)))
3102 (end-point
3103 (if (bibtex-mark-active)
3104 (region-end)
3105 (point-max)))
3106 (number 0)
3107 (bibtex-sort-ignore-string-entries 2769 (bibtex-sort-ignore-string-entries
3108 (not count-string-entries))) 2770 (not count-string-entries)))
3109 (save-restriction 2771 (save-excursion
3110 (narrow-to-region start-point end-point) 2772 (save-restriction
3111 (goto-char start-point) 2773 (narrow-to-region (if (bibtex-mark-active)
3112 (bibtex-map-entries 2774 (region-beginning)
3113 (lambda (current) 2775 (bibtex-beginning-of-first-entry))
3114 (setq number (1+ number))))) 2776 (if (bibtex-mark-active)
3115 (message (concat (if (bibtex-mark-active) "Region" "Buffer") 2777 (region-end)
3116 " contains %d entries.") number) 2778 (point-max)))
3117 (goto-char pnt))) 2779 (goto-char (point-min))
2780 (bibtex-map-entries (lambda (current)
2781 (setq number (1+ number))))))
2782 (message "%s contains %d entries."
2783 (if (bibtex-mark-active) "Region" "Buffer")
2784 number)))
3118 2785
3119(defun bibtex-ispell-entry () 2786(defun bibtex-ispell-entry ()
3120 "Spell whole BibTeX entry." 2787 "Spell whole BibTeX entry."
3121 (interactive) 2788 (interactive)
3122 (ispell-region (bibtex-beginning-of-entry) (bibtex-end-of-entry))) 2789 (ispell-region (bibtex-beginning-of-entry)
2790 (bibtex-end-of-entry)))
3123 2791
3124(defun bibtex-ispell-abstract () 2792(defun bibtex-ispell-abstract ()
3125 "Spell abstract of BibTeX entry." 2793 "Spell abstract of BibTeX entry."
3126 (interactive) 2794 (interactive)
3127 (let* ((case-fold-search t) 2795 (bibtex-beginning-of-entry)
3128 (pnt (bibtex-end-of-entry))) 2796 (let ((bounds (bibtex-search-forward-field "abstract"
3129 (bibtex-beginning-of-entry) 2797 (bibtex-end-of-entry))))
3130 (let ((bounds (bibtex-search-forward-field "abstract" pnt))) 2798 (if bounds
3131 (if bounds 2799 (ispell-region (bibtex-start-of-text-in-field bounds)
3132 (ispell-region (bibtex-start-of-text-in-field bounds) (bibtex-end-of-text-in-field bounds)) 2800 (bibtex-end-of-text-in-field bounds))
3133 (error "No abstract in entry"))))) 2801 (error "No abstract in entry"))))
3134 2802
3135(defun bibtex-narrow-to-entry () 2803(defun bibtex-narrow-to-entry ()
3136 "Narrow buffer to current BibTeX entry." 2804 "Narrow buffer to current BibTeX entry."
3137 (interactive) 2805 (interactive)
3138 (save-excursion 2806 (save-excursion
3139 (narrow-to-region 2807 (widen)
3140 (bibtex-beginning-of-entry) (bibtex-end-of-entry)))) 2808 (narrow-to-region (bibtex-beginning-of-entry)
2809 (bibtex-end-of-entry))))
2810
2811(defun bibtex-entry-index ()
2812 "Return the index of the BibTeX entry at point. Move point.
2813The index is a list (KEY CROSSREF-KEY ENTRY-NAME) that is used for sorting
2814the entries of the BibTeX buffer. Return nil if no entry found."
2815 (let ((case-fold-search t))
2816 (if (re-search-forward bibtex-entry-maybe-empty-head nil t)
2817 (let ((key (bibtex-key-in-head))
2818 (entry-name (bibtex-type-in-head)))
2819 ;; Don't search CROSSREF-KEY if we don't need it.
2820 (if (equal bibtex-maintain-sorted-entries 'crossref)
2821 (save-excursion
2822 (save-restriction
2823 (bibtex-narrow-to-entry)
2824 (goto-char (point-min))
2825 (let ((bounds (bibtex-search-forward-field
2826 "\\(OPT\\)?crossref")))
2827 (list key
2828 (if bounds (bibtex-text-in-field-bounds bounds t))
2829 entry-name))))
2830 (list key nil entry-name))))))
2831
2832(defun bibtex-lessp (index1 index2)
2833 "Predicate for sorting BibTeX entries with indices INDEX1 and INDEX2.
2834Each index is a list (KEY CROSSREF-KEY ENTRY-NAME).
2835The predicate depends on the variable `bibtex-maintain-sorted-entries'."
2836 (cond ((not index1) (not index2)) ; indices can be nil
2837 ((not index2) nil)
2838 ((equal bibtex-maintain-sorted-entries 'crossref)
2839 (if (nth 1 index1)
2840 (if (nth 1 index2)
2841 (or (string-lessp (nth 1 index1) (nth 1 index2))
2842 (and (string-equal (nth 1 index1) (nth 1 index2))
2843 (string-lessp (nth 0 index1) (nth 0 index2))))
2844 (not (string-lessp (nth 0 index2) (nth 1 index1))))
2845 (if (nth 1 index2)
2846 (string-lessp (nth 0 index1) (nth 1 index2))
2847 (string-lessp (nth 0 index1) (nth 0 index2)))))
2848 ((equal bibtex-maintain-sorted-entries 'entry-class)
2849 (let ((n1 (cdr (assoc-ignore-case (nth 2 index1)
2850 bibtex-sort-entry-class-alist)))
2851 (n2 (cdr (assoc-ignore-case (nth 2 index2)
2852 bibtex-sort-entry-class-alist))))
2853 (or (< n1 n2)
2854 (and (= n1 n2)
2855 (string-lessp (car index1) (car index2))))))
2856 (t ; (equal bibtex-maintain-sorted-entries 'plain)
2857 (string-lessp (car index1) (car index2)))))
3141 2858
3142(defun bibtex-sort-buffer () 2859(defun bibtex-sort-buffer ()
3143 "Sort BibTeX buffer alphabetically by key. 2860 "Sort BibTeX buffer alphabetically by key.
2861The predicate for sorting is defined via `bibtex-maintain-sorted-entries'.
3144Text outside of BibTeX entries is not affected. If 2862Text outside of BibTeX entries is not affected. If
3145`bibtex-sort-ignore-string-entries' is non-nil, @String entries will be 2863`bibtex-sort-ignore-string-entries' is non-nil, @String entries will be
3146ignored." 2864ignored."
3147 (interactive) 2865 (interactive)
2866 (unless bibtex-maintain-sorted-entries
2867 (error "You must choose a sorting scheme"))
3148 (save-restriction 2868 (save-restriction
3149 (narrow-to-region 2869 (narrow-to-region (bibtex-beginning-of-first-entry)
3150 (bibtex-beginning-of-first-entry) 2870 (save-excursion (goto-char (point-max))
3151 (save-excursion 2871 (bibtex-end-of-entry)))
3152 (goto-char (point-max))
3153 (bibtex-end-of-entry)))
3154 (bibtex-skip-to-valid-entry) 2872 (bibtex-skip-to-valid-entry)
3155 (sort-subr 2873 (sort-subr nil
3156 nil 2874 'bibtex-skip-to-valid-entry ; NEXTREC function
3157 ;; NEXTREC function 2875 'bibtex-end-of-entry ; ENDREC function
3158 'bibtex-skip-to-valid-entry 2876 'bibtex-entry-index ; STARTKEY function
3159 ;; ENDREC function 2877 nil ; ENDKEY function
3160 'bibtex-end-of-entry 2878 'bibtex-lessp))) ; PREDICATE
3161 ;; STARTKEY function 2879
3162 (lambda () 2880(defun bibtex-find-crossref (crossref-key)
3163 (let* ((case-fold-search t)) 2881 "Move point to the beginning of BibTeX entry CROSSREF-KEY.
3164 (re-search-forward bibtex-entry-head) 2882Return position of entry if CROSSREF-KEY is found and nil otherwise.
3165 (buffer-substring-no-properties 2883If position of current entry is after CROSSREF-KEY an error is signaled."
3166 (match-beginning bibtex-key-in-head) 2884 (let ((pos (save-excursion (bibtex-find-entry crossref-key))))
3167 (match-end bibtex-key-in-head))))))) 2885 (if (and pos (> (point) pos))
3168 2886 (error "This entry must not follow the crossrefed entry!"))
3169(defun bibtex-find-entry-location (entry-name &optional ignore-dups) 2887 (goto-char pos)))
3170 "Looking for place to put the BibTeX entry named ENTRY-NAME. 2888
3171Performs a binary search (therefore, buffer is assumed to be in sorted 2889(defun bibtex-find-entry (key)
3172order, without duplicates (see \\[bibtex-validate]), if it is 2890 "Move point to the beginning of BibTeX entry named KEY.
3173not, `bibtex-find-entry-location' will fail). If entry-name is already 2891Return position of entry if KEY is found or nil if not found."
3174used as a reference key, an error is signaled. However, if optional 2892 (interactive
3175variable IGNORE-DUPS is non-nil, no error messages about duplicate 2893 (list (if bibtex-maintain-sorted-entries
3176entries are signaled, but the error handling is assumed to be made in 2894 (completing-read "Find key: "
3177the calling function. 2895 bibtex-reference-keys
3178The value is nil if a duplicate entry error occurred, 2896 nil nil nil 'bibtex-key-history)
3179and t in all other cases." 2897 (read-from-minibuffer "Find key: " nil nil nil
3180 (let* ((case-fold-search t) 2898 'bibtex-key-history))))
3181 (left 2899 (let* (case-fold-search
3182 (progn 2900 (pnt (save-excursion
3183 (bibtex-beginning-of-first-entry) 2901 (goto-char (point-min))
3184 (bibtex-skip-to-valid-entry) 2902 (when (re-search-forward (concat "^[ \t]*\\("
3185 (bibtex-end-of-entry))) 2903 bibtex-entry-type
3186 (right 2904 "\\)[ \t]*[({][ \t\n]*\\("
3187 (progn 2905 key "\\)")
3188 (bibtex-beginning-of-last-entry) 2906 nil t)
3189 (bibtex-skip-to-valid-entry t) 2907 (match-beginning 2)))))
3190 (point))) 2908 (cond (pnt
3191 actual-point 2909 (goto-char pnt)
3192 actual-key 2910 (bibtex-beginning-of-entry))
3193 (done (>= left right)) 2911 ((interactive-p)
3194 new 2912 (message "Key `%s' not found" key)))))
3195 dup) 2913
3196 (while (not done) 2914(defun bibtex-prepare-new-entry (index)
3197 (setq actual-point (/ (+ left right) 2)) 2915 "Prepare a new BibTeX entry with index INDEX.
3198 (goto-char actual-point) 2916INDEX is a list (KEY CROSSREF-KEY ENTRY-NAME).
3199 (bibtex-skip-to-valid-entry t) 2917Move point where the entry KEY should be placed.
3200 (setq actual-key 2918If `bibtex-maintain-sorted-entries' is non-nil, perform a binary
3201 (progn 2919search to look for place for KEY. This will fail if buffer is not in
3202 (re-search-forward bibtex-entry-head) 2920sorted order, see \\[bibtex-validate].)
3203 (buffer-substring-no-properties 2921Return t if preparation was successful or nil if entry KEY already exists."
3204 (match-beginning bibtex-key-in-head) 2922 (let ((key (nth 0 index))
3205 (match-end bibtex-key-in-head)))) 2923 key-exist)
3206 (cond 2924 (cond ((or (null key)
3207 ((string-lessp entry-name actual-key) 2925 (and (stringp key)
3208 (setq new (bibtex-beginning-of-entry)) 2926 (string-equal key ""))
3209 (if (equal right new) 2927 (and (not (setq key-exist (bibtex-find-entry key)))
3210 (setq done t) 2928 (not bibtex-maintain-sorted-entries)))
3211 (setq right new))) 2929 (bibtex-move-outside-of-entry))
3212 ((string-lessp actual-key entry-name) 2930 ;; if key-exist is non-nil due to the previous cond clause
3213 (setq new (bibtex-end-of-entry)) 2931 ;; then point will be at beginning of entry named key.
3214 (if (equal left new) 2932 (key-exist)
3215 (setq done t) 2933 (t ; bibtex-maintain-sorted-entries is non-nil
3216 (setq left new))) 2934 (let* ((case-fold-search t)
3217 ((string-equal actual-key entry-name) 2935 (left (save-excursion (bibtex-beginning-of-first-entry)
3218 (setq dup t 2936 (bibtex-skip-to-valid-entry)
3219 done t) 2937 (point)))
3220 (if (not ignore-dups) 2938 (right (save-excursion (bibtex-beginning-of-last-entry)
3221 (progn 2939 (bibtex-end-of-entry)))
3222 (bibtex-beginning-of-entry) 2940 (found (if (>= left right) left))
3223 (error "Entry with key `%s' already exists" entry-name)))))) 2941 actual-index new)
3224 (if dup 2942 (save-excursion
3225 (progn 2943 ;; Binary search
3226 (bibtex-beginning-of-entry) 2944 (while (not found)
3227 nil) 2945 (goto-char (/ (+ left right) 2))
3228 (goto-char right) 2946 (bibtex-skip-to-valid-entry t)
3229 (setq actual-key 2947 (setq actual-index (bibtex-entry-index))
3230 (if (looking-at bibtex-entry-head) 2948 (cond ((bibtex-lessp index actual-index)
3231 (buffer-substring-no-properties 2949 (setq new (bibtex-beginning-of-entry))
3232 (match-beginning bibtex-key-in-entry) 2950 (if (equal right new)
3233 (match-end bibtex-key-in-entry)))) 2951 (setq found right)
3234 (if (or 2952 (setq right new)))
3235 (not actual-key) 2953 (t
3236 (string-lessp actual-key entry-name)) 2954 (bibtex-end-of-entry)
3237 ;; buffer contains no valid entries or 2955 (bibtex-skip-to-valid-entry)
3238 ;; greater than last entry --> append 2956 (setq new (point))
3239 (progn 2957 (if (equal left new)
3240 (bibtex-end-of-entry) 2958 (setq found right)
3241 (if (not (bobp)) 2959 (setq left new))))))
3242 (newline (forward-line 2))) 2960 (goto-char found)
3243 (beginning-of-line)) 2961 (bibtex-beginning-of-entry)
3244 (goto-char right)) 2962 (setq actual-index (save-excursion (bibtex-entry-index)))
3245 t))) 2963 (when (or (not actual-index)
2964 (bibtex-lessp actual-index index))
2965 ;; buffer contains no valid entries or
2966 ;; greater than last entry --> append
2967 (bibtex-end-of-entry)
2968 (if (not (bobp))
2969 (newline (forward-line 2)))
2970 (beginning-of-line)))))
2971 (unless key-exist t)))
3246 2972
3247(defun bibtex-validate (&optional test-thoroughly) 2973(defun bibtex-validate (&optional test-thoroughly)
3248 "Validate if buffer or region is syntactically correct. 2974 "Validate if buffer or region is syntactically correct.
@@ -3250,167 +2976,119 @@ Only known entry types are checked, so you can put comments
3250outside of entries. 2976outside of entries.
3251With optional argument TEST-THOROUGHLY non-nil it checks for absence of 2977With optional argument TEST-THOROUGHLY non-nil it checks for absence of
3252required fields and questionable month fields as well. 2978required fields and questionable month fields as well.
3253If mark is active, it validates current region, if not whole buffer. 2979If mark is active, validate current region, if not the whole buffer.
3254Returns t if test was successful, nil otherwise." 2980Returns t if test was successful, nil otherwise."
3255 (interactive "P") 2981 (interactive "P")
3256 (let* (error-list 2982 (let* ((case-fold-search t)
3257 syntax-error 2983 error-list syntax-error)
3258 (case-fold-search t) 2984 (save-excursion
3259 (valid-bibtex-entry 2985 (save-restriction
3260 (concat 2986 (narrow-to-region (if (bibtex-mark-active)
3261 "@[ \t]*\\(\\(string\\)\\|" 2987 (region-beginning)
3262 (mapconcat 2988 (bibtex-beginning-of-first-entry))
3263 (lambda (type) 2989 (if (bibtex-mark-active)
3264 (concat "\\(" (car type) "\\)")) 2990 (region-end)
3265 bibtex-entry-field-alist 2991 (point-max)))
3266 "\\|") 2992
3267 "\\)")) 2993 ;; looking if entries fit syntactical structure
3268 (pnt (point)) 2994 (goto-char (point-min))
3269 (start-point 2995 (bibtex-progress-message "Checking syntactical structure")
3270 (if (bibtex-mark-active) 2996 (let (bibtex-sort-ignore-string-entries)
3271 (region-beginning) 2997 (while (re-search-forward "^[ \t]*@" nil t)
3272 (bibtex-beginning-of-first-entry))) 2998 (bibtex-progress-message)
3273 (end-point 2999 (forward-char -1)
3274 (if (bibtex-mark-active) 3000 (let ((pnt (point)))
3275 (region-end) 3001 (if (not (looking-at bibtex-any-valid-entry-re))
3276 (point-max)))) 3002 (forward-char)
3277 (save-restriction 3003 (bibtex-skip-to-valid-entry)
3278 (narrow-to-region start-point end-point) 3004 (if (equal (point) pnt)
3279 ;; looking if entries fit syntactical structure 3005 (forward-char)
3280 (goto-char start-point) 3006 (goto-char pnt)
3281 (bibtex-progress-message "Checking syntactical structure") 3007 (push (list (bibtex-current-line)
3282 (while (re-search-forward "^[ \t]*@" nil t) 3008 "Syntax error (check esp. commas, braces, and quotes)")
3283 (bibtex-progress-message) 3009 error-list)
3284 (forward-char -1) 3010 (forward-char))))))
3285 (let ((p (point)) 3011 (bibtex-progress-message 'done)
3286 (must-match 3012
3287 (looking-at valid-bibtex-entry))) 3013 (if error-list
3288 (if (not must-match) 3014 (setq syntax-error t)
3289 (forward-char) 3015 ;; looking for correct sort order and duplicates (only if
3290 (let (bibtex-sort-ignore-string-entries) 3016 ;; there were no syntax errors)
3291 (bibtex-skip-to-valid-entry)) 3017 (if bibtex-maintain-sorted-entries
3292 (if (equal (point) p) 3018 (let (previous current)
3293 (forward-char) 3019 (goto-char (point-min))
3294 (goto-char p) 3020 (bibtex-progress-message "Checking correct sort order")
3295 (setq 3021 (bibtex-map-entries
3296 error-list 3022 (lambda (dummy)
3297 (cons (list 3023 (bibtex-progress-message)
3298 (bibtex-current-line) 3024 (bibtex-beginning-of-entry)
3299 "Syntax error (check esp. commas, braces, and quotes)") 3025 (setq current (bibtex-entry-index))
3300 error-list)) 3026 (cond ((or (not previous)
3301 (forward-char))))) 3027 (bibtex-lessp previous current))
3302 (bibtex-progress-message 'done) 3028 (setq previous current))
3303 (if error-list 3029 ((string-equal (car previous) (car current))
3304 (setq syntax-error t) 3030 (push (list (bibtex-current-line)
3305 ;; looking for correct sort order and duplicates (only if 3031 "Duplicate key with previous")
3306 ;; there were no syntax errors) 3032 error-list))
3307 (if bibtex-maintain-sorted-entries 3033 (t
3308 (let (previous) 3034 (setq previous current)
3309 (goto-char start-point) 3035 (push (list (bibtex-current-line)
3310 (bibtex-progress-message "Checking correct sort order") 3036 "Entries out of order")
3037 error-list)))))
3038 (bibtex-progress-message 'done)))
3039
3040 (when test-thoroughly
3041 (goto-char (point-min))
3042 (bibtex-progress-message
3043 "Checking required fields and month fields")
3044 (let ((bibtex-sort-ignore-string-entries t)
3045 (questionable-month
3046 (regexp-opt (mapcar 'car bibtex-predefined-month-strings))))
3311 (bibtex-map-entries 3047 (bibtex-map-entries
3312 (lambda (current) 3048 (lambda (current)
3313 (bibtex-progress-message) 3049 (bibtex-progress-message)
3314 (cond ((or (not previous) 3050 (let* ((beg (bibtex-beginning-of-entry))
3315 (string< previous current)) 3051 (end (bibtex-end-of-entry))
3316 (setq previous current)) 3052 (entry-list (progn
3317 ((string-equal previous current) 3053 (goto-char beg)
3318 (setq 3054 (bibtex-search-entry nil end)
3319 error-list 3055 (assoc-ignore-case (bibtex-type-in-head)
3320 (cons (list (bibtex-current-line) 3056 bibtex-entry-field-alist)))
3321 "Duplicate key with previous") 3057 (req (copy-sequence (elt (elt entry-list 1) 0)))
3322 error-list))) 3058 (creq (copy-sequence (elt (elt entry-list 2) 0)))
3323 (t 3059 crossref-there bounds)
3324 (setq previous current 3060 (goto-char beg)
3325 error-list 3061 (while (setq bounds (bibtex-search-forward-field
3326 (cons (list (bibtex-current-line) 3062 bibtex-field-name end))
3327 "Entries out of order") 3063 (goto-char (bibtex-start-of-text-in-field bounds))
3328 error-list)))))) 3064 (let ((field-name (downcase (bibtex-name-in-field bounds)))
3329 (bibtex-progress-message 'done))) 3065 case-fold-search)
3330 (if test-thoroughly 3066 (if (and (equal field-name "month")
3331 (progn 3067 (not (string-match questionable-month
3332 (goto-char start-point) 3068 (bibtex-text-in-field-bounds bounds))))
3333 (bibtex-progress-message 3069 (push (list (bibtex-current-line)
3334 "Checking required fields and month fields") 3070 "Questionable month field")
3335 (let ((bibtex-sort-ignore-string-entries t) 3071 error-list))
3336 (questionable-month 3072 (setq req (delete (assoc-ignore-case field-name req) req)
3337 (concat 3073 creq (delete (assoc-ignore-case field-name creq) creq))
3338 "[{\"]\\(" 3074 (if (equal field-name "crossref")
3339 (mapconcat 3075 (setq crossref-there t))))
3340 (lambda (mon) 3076 (if crossref-there
3341 (concat "\\(" (car mon) "\\)")) 3077 (setq req creq))
3342 bibtex-predefined-month-strings 3078 (if (or (> (length req) 1)
3343 "\\|") 3079 (and (= (length req) 1)
3344 "\\)[}\"]"))) 3080 (not (elt (car req) 3))))
3345 (bibtex-map-entries 3081 ;; two (or more) fields missed or one field
3346 (lambda (current) 3082 ;; missed and this isn't flagged alternative
3347 (bibtex-progress-message) 3083 ;; (notice that this fails if there are more
3348 (let* ((beg (bibtex-beginning-of-entry)) 3084 ;; than two alternatives in a BibTeX entry,
3349 (end (bibtex-end-of-entry)) 3085 ;; which isn't the case momentarily)
3350 (entry-list 3086 (push (list (save-excursion
3351 (progn 3087 (bibtex-beginning-of-entry)
3352 (goto-char beg) 3088 (bibtex-current-line))
3353 (bibtex-search-entry nil end) 3089 (concat "Required field `" (caar req) "' missing"))
3354 (assoc-ignore-case 3090 error-list))))))
3355 (buffer-substring-no-properties 3091 (bibtex-progress-message 'done)))))
3356 (1+ (match-beginning bibtex-type-in-head))
3357 (match-end bibtex-type-in-head))
3358 bibtex-entry-field-alist)))
3359 (req (copy-sequence (elt (elt entry-list 1) 0)))
3360 (creq (copy-sequence (elt (elt entry-list 2) 0)))
3361 crossref-there)
3362 (goto-char beg)
3363 (let ((bounds (bibtex-search-forward-field bibtex-field-name end)))
3364 (while bounds
3365 (let ((field-name
3366 (buffer-substring-no-properties
3367 (bibtex-start-of-name-in-field bounds)
3368 (bibtex-end-of-name-in-field bounds))))
3369 (if (and (equal (downcase field-name) "month")
3370 (string-match
3371 questionable-month
3372 (buffer-substring-no-properties
3373 (bibtex-start-of-text-in-field bounds)
3374 (bibtex-end-of-text-in-field bounds))))
3375 (setq
3376 error-list
3377 (cons
3378 (list
3379 (bibtex-current-line)
3380 "Questionable month field (delimited string)")
3381 error-list)))
3382 (setq
3383 req
3384 (delete (assoc-ignore-case field-name req) req)
3385 creq
3386 (delete (assoc-ignore-case field-name creq) creq))
3387 (if (equal (downcase field-name) "crossref")
3388 (setq crossref-there t)))
3389 (goto-char (bibtex-end-of-field bounds))
3390 (setq bounds (bibtex-search-forward-field bibtex-field-name end))))
3391 (if crossref-there
3392 (setq req creq))
3393 (if (or (> (length req) 1)
3394 (and (= (length req) 1)
3395 (not (elt (car req) 3))))
3396 ;; two (or more) fields missed or one field
3397 ;; missed and this isn't flagged alternative
3398 ;; (notice that this fails if there are more
3399 ;; than two alternatives in a BibTeX entry,
3400 ;; which isn't the case momentarily)
3401 (setq
3402 error-list
3403 (cons
3404 (list (save-excursion
3405 (bibtex-beginning-of-entry)
3406 (bibtex-current-line))
3407 (concat
3408 "Required field \""
3409 (car (car req))
3410 "\" missing"))
3411 error-list)))))))
3412 (bibtex-progress-message 'done)))))
3413 (goto-char pnt)
3414 (if error-list 3092 (if error-list
3415 (let ((bufnam (buffer-name)) 3093 (let ((bufnam (buffer-name))
3416 (dir default-directory)) 3094 (dir default-directory))
@@ -3429,22 +3107,18 @@ Returns t if test was successful, nil otherwise."
3429 (compilation-mode) 3107 (compilation-mode)
3430 (delete-region (point-min) (point-max)) 3108 (delete-region (point-min) (point-max))
3431 (goto-char (point-min)) 3109 (goto-char (point-min))
3432 (insert 3110 (insert "BibTeX mode command `bibtex-validate'\n"
3433 "BibTeX mode command `bibtex-validate'\n" 3111 (if syntax-error
3434 (if syntax-error 3112 "Maybe undetected errors due to syntax errors. Correct and validate again."
3435 "Maybe undetected errors due to syntax errors. Correct and validate again." 3113 "")
3436 "") 3114 "\n")
3437 "\n") 3115 (dolist (err error-list)
3438 (while error-list 3116 (insert bufnam ":" (number-to-string (elt err 0))
3439 (insert 3117 ": " (elt err 1) "\n"))
3440 bufnam ":" (number-to-string (elt (car error-list) 0))
3441 ": " (elt (car error-list) 1) "\n")
3442 (setq error-list (cdr error-list)))
3443 (compilation-parse-errors nil nil) 3118 (compilation-parse-errors nil nil)
3444 (setq compilation-old-error-list compilation-error-list) 3119 (setq compilation-old-error-list compilation-error-list)
3445 ;; this is necessary to avoid reparsing of buffer if you 3120 ;; this is necessary to avoid reparsing of buffer if you
3446 ;; switch to compilation buffer and enter 3121 ;; switch to compilation buffer and enter `compile-goto-error'
3447 ;; `compile-goto-error'
3448 (set-buffer-modified-p nil) 3122 (set-buffer-modified-p nil)
3449 (toggle-read-only 1) 3123 (toggle-read-only 1)
3450 (goto-char (point-min)) 3124 (goto-char (point-min))
@@ -3457,135 +3131,109 @@ Returns t if test was successful, nil otherwise."
3457 t))) 3131 t)))
3458 3132
3459(defun bibtex-next-field (arg) 3133(defun bibtex-next-field (arg)
3460 "Finds end of text of next BibTeX field; with ARG, to its beginning." 3134 "Find end of text of next BibTeX field; with ARG, to its beginning."
3461 (interactive "P") 3135 (interactive "P")
3462 (bibtex-inside-field) 3136 (bibtex-inside-field)
3463 (let ((start (point))) 3137 (let ((start (point)))
3464 (condition-case () 3138 (condition-case ()
3465 (let ((bounds (bibtex-enclosing-field))) 3139 (let ((bounds (bibtex-enclosing-field)))
3466 (goto-char (bibtex-end-of-field bounds)) 3140 (goto-char (bibtex-end-of-field bounds))
3467 (forward-char 2)) 3141 (forward-char 2))
3468 (error 3142 (error
3469 (goto-char start) 3143 (goto-char start)
3470 (end-of-line) 3144 (end-of-line)
3471 (forward-char)))) 3145 (forward-char))))
3472 (bibtex-find-text arg t)) 3146 (bibtex-find-text arg t))
3473 3147
3474(defun bibtex-find-text (arg &optional as-if-interactive silent) 3148(defun bibtex-find-text (arg &optional as-if-interactive no-error)
3475 "Go to end of text of current field; with ARG, go to beginning." 3149 "Go to end of text of current field; with ARG, go to beginning."
3476 (interactive "P") 3150 (interactive "P")
3477 (bibtex-inside-field) 3151 (bibtex-inside-field)
3478 (let ((bounds (bibtex-enclosing-field (or (interactive-p) as-if-interactive)))) 3152 (let ((bounds (bibtex-enclosing-field (or (interactive-p)
3153 as-if-interactive))))
3479 (if bounds 3154 (if bounds
3480 (progn 3155 (progn (if arg
3481 (if arg 3156 (progn (goto-char (bibtex-start-of-text-in-field bounds))
3482 (progn 3157 (if (looking-at "[{\"]")
3483 (goto-char (bibtex-start-of-text-in-field bounds)) 3158 (forward-char)))
3484 (if (looking-at "[{\"]") 3159 (goto-char (bibtex-end-of-text-in-field bounds))
3485 (forward-char))) 3160 (if (or (= (preceding-char) ?})
3486 (goto-char (bibtex-end-of-text-in-field bounds)) 3161 (= (preceding-char) ?\"))
3487 (if (or 3162 (forward-char -1)))
3488 (= (preceding-char) ?}) 3163 (if bibtex-help-message
3489 (= (preceding-char) ?\")) 3164 (bibtex-print-help-message)))
3490 (forward-char -1)))
3491 (if bibtex-help-message
3492 (bibtex-print-help-message)))
3493 (beginning-of-line) 3165 (beginning-of-line)
3494 (cond 3166 (cond ((setq bounds (bibtex-parse-string))
3495 ((bibtex-parse-string) 3167 (goto-char (if arg
3496 (let ((bounds (bibtex-parse-string))) 3168 (bibtex-start-of-text-in-string bounds)
3497 (goto-char 3169 (bibtex-end-of-text-in-string bounds))))
3498 (if arg 3170 ((looking-at bibtex-entry-maybe-empty-head)
3499 (bibtex-start-of-text-in-string bounds) 3171 (goto-char (if arg
3500 (bibtex-end-of-text-in-string bounds))))) 3172 (match-beginning bibtex-key-in-head)
3501 ((looking-at bibtex-entry-maybe-empty-head) 3173 (match-end 0))))
3502 (goto-char 3174 (t
3503 (if arg 3175 (unless no-error
3504 (match-beginning bibtex-key-in-head) 3176 (error "Not on BibTeX field")))))))
3505 (match-end 0))))
3506 (t
3507 (if (not silent)
3508 (error "Not on BibTeX field")))))))
3509 3177
3510(defun bibtex-remove-OPT-or-ALT () 3178(defun bibtex-remove-OPT-or-ALT ()
3511 "Removes the string starting optional/alternative fields. 3179 "Remove the string starting optional/alternative fields.
3512Aligns text and goes thereafter to end of text." 3180Align text and go thereafter to end of text."
3513 (interactive) 3181 (interactive)
3514 (bibtex-inside-field) 3182 (bibtex-inside-field)
3515 (let* ((case-fold-search t) 3183 (let ((case-fold-search t)
3516 (bounds (bibtex-enclosing-field))) 3184 (bounds (bibtex-enclosing-field)))
3517 (save-excursion 3185 (save-excursion
3518 (goto-char (bibtex-start-of-name-in-field bounds)) 3186 (goto-char (bibtex-start-of-name-in-field bounds))
3519 (if (looking-at "OPT\\|ALT") 3187 (when (looking-at "OPT\\|ALT")
3520 (progn 3188 (delete-char (length "OPT"))
3521 (delete-char (length "OPT")) 3189 ;; make field non-OPT
3522 ;; make field non-OPT 3190 (search-forward "=")
3523 (search-forward "=") 3191 (forward-char -1)
3524 (forward-char -1) 3192 (delete-horizontal-space)
3525 (delete-horizontal-space) 3193 (if bibtex-align-at-equal-sign
3526 (if bibtex-align-at-equal-sign 3194 (indent-to-column (- bibtex-text-indentation 2))
3527 (indent-to-column (- bibtex-text-indentation 2)) 3195 (insert " "))
3528 (insert " ")) 3196 (search-forward "=")
3529 (search-forward "=") 3197 (delete-horizontal-space)
3530 (delete-horizontal-space) 3198 (if bibtex-align-at-equal-sign
3531 (if bibtex-align-at-equal-sign 3199 (insert " ")
3532 (insert " ") 3200 (indent-to-column bibtex-text-indentation))))
3533 (indent-to-column bibtex-text-indentation)))))
3534 (bibtex-inside-field))) 3201 (bibtex-inside-field)))
3535 3202
3536(defun bibtex-remove-delimiters () 3203(defun bibtex-remove-delimiters ()
3537 "Removes \"\" or {} around string." 3204 "Remove \"\" or {} around string."
3538 (interactive) 3205 (interactive)
3539 (save-excursion 3206 (save-excursion
3540 (bibtex-inside-field) 3207 (bibtex-inside-field)
3541 (let* ((case-fold-search t) 3208 (let ((bounds (bibtex-enclosing-field)))
3542 (bounds (bibtex-enclosing-field))
3543 (stop (copy-marker (bibtex-end-of-text-in-field bounds))))
3544 (goto-char (bibtex-start-of-text-in-field bounds)) 3209 (goto-char (bibtex-start-of-text-in-field bounds))
3545 (let* ((boundaries (bibtex-search-forward-field-string stop))) 3210 (delete-char 1)
3546 (while boundaries 3211 (goto-char (1- (bibtex-end-of-text-in-field bounds)))
3547 (let ((text-end (copy-marker (cdr boundaries)))) 3212 (delete-backward-char 1))))
3548 (goto-char (car boundaries))
3549 (delete-char 1)
3550 (goto-char text-end)
3551 (delete-backward-char 1)
3552 (setq boundaries (bibtex-search-forward-field-string stop))))))))
3553 3213
3554(defun bibtex-kill-field (&optional copy-only) 3214(defun bibtex-kill-field (&optional copy-only)
3555 "Kills the entire enclosing BibTeX field. 3215 "Kill the entire enclosing BibTeX field.
3556With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring,' 3216With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring',
3557but do not actually kill it." 3217but do not actually kill it."
3558 (interactive "P") 3218 (interactive "P")
3559 (let* ((pnt (point)) 3219 (save-excursion
3560 (case-fold-search t))
3561 (bibtex-inside-field) 3220 (bibtex-inside-field)
3562 (let* ((bounds (bibtex-enclosing-field)) 3221 (let* ((case-fold-search t)
3563 (the-end (bibtex-end-of-field bounds)) 3222 (bounds (bibtex-enclosing-field))
3564 (the-beginning (bibtex-start-of-field bounds))) 3223 (end (bibtex-end-of-field bounds))
3565 (goto-char the-end) 3224 (beg (bibtex-start-of-field bounds)))
3225 (goto-char end)
3566 (skip-chars-forward " \t\n,") 3226 (skip-chars-forward " \t\n,")
3567 (setq 3227 (push (list 'field (bibtex-name-in-field bounds)
3568 bibtex-field-kill-ring 3228 (bibtex-text-in-field-bounds bounds))
3569 (cons 3229 bibtex-field-kill-ring)
3570 (list
3571 'field
3572 (buffer-substring-no-properties
3573 (bibtex-start-of-name-in-field bounds)
3574 (bibtex-end-of-name-in-field bounds))
3575 (buffer-substring-no-properties
3576 (bibtex-start-of-text-in-field bounds)
3577 (bibtex-end-of-text-in-field bounds)))
3578 bibtex-field-kill-ring))
3579 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max) 3230 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
3580 (setcdr 3231 (setcdr (nthcdr (1- bibtex-field-kill-ring-max)
3581 (nthcdr (1- bibtex-field-kill-ring-max) bibtex-field-kill-ring) 3232 bibtex-field-kill-ring)
3582 nil)) 3233 nil))
3583 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring) 3234 (setq bibtex-field-kill-ring-yank-pointer bibtex-field-kill-ring)
3584 (if copy-only 3235 (unless copy-only
3585 (goto-char pnt) 3236 (delete-region beg end))))
3586 (delete-region the-beginning the-end)
3587 (let (bibtex-help-message)
3588 (bibtex-find-text nil t t)))))
3589 (setq bibtex-last-kill-command 'field)) 3237 (setq bibtex-last-kill-command 'field))
3590 3238
3591(defun bibtex-copy-field-as-kill () 3239(defun bibtex-copy-field-as-kill ()
@@ -3597,29 +3245,23 @@ but do not actually kill it."
3597With prefix arg COPY-ONLY the current entry to 3245With prefix arg COPY-ONLY the current entry to
3598`bibtex-entry-kill-ring', but do not actually kill it." 3246`bibtex-entry-kill-ring', but do not actually kill it."
3599 (interactive "P") 3247 (interactive "P")
3600 (let* ((pnt (point)) 3248 (save-excursion
3601 (case-fold-search t) 3249 (let* ((case-fold-search t)
3602 (beg (bibtex-beginning-of-entry)) 3250 (beg (bibtex-beginning-of-entry))
3603 (end 3251 (end (progn (bibtex-end-of-entry)
3604 (progn 3252 (if (re-search-forward
3605 (bibtex-end-of-entry) 3253 bibtex-entry-maybe-empty-head nil 'move)
3606 (if (re-search-forward 3254 (goto-char (match-beginning 0)))
3607 bibtex-entry-maybe-empty-head nil 'move) 3255 (point))))
3608 (goto-char (match-beginning 0))) 3256 (push (list 'entry (buffer-substring-no-properties beg end))
3609 (point)))) 3257 bibtex-entry-kill-ring)
3610 (setq 3258 (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
3611 bibtex-entry-kill-ring 3259 (setcdr (nthcdr (1- bibtex-entry-kill-ring-max)
3612 (cons 3260 bibtex-entry-kill-ring)
3613 (list 'entry (buffer-substring-no-properties beg end)) 3261 nil))
3614 bibtex-entry-kill-ring))
3615 (if (> (length bibtex-entry-kill-ring) bibtex-entry-kill-ring-max)
3616 (setcdr
3617 (nthcdr (1- bibtex-entry-kill-ring-max) bibtex-entry-kill-ring)
3618 nil))
3619 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring) 3262 (setq bibtex-entry-kill-ring-yank-pointer bibtex-entry-kill-ring)
3620 (if copy-only 3263 (unless copy-only
3621 (goto-char pnt) 3264 (delete-region beg end))))
3622 (delete-region beg end)))
3623 (setq bibtex-last-kill-command 'entry)) 3265 (setq bibtex-last-kill-command 'entry))
3624 3266
3625(defun bibtex-copy-entry-as-kill () 3267(defun bibtex-copy-entry-as-kill ()
@@ -3665,7 +3307,7 @@ comes the newest one."
3665 (goto-char (bibtex-start-of-text-in-field bounds)) 3307 (goto-char (bibtex-start-of-text-in-field bounds))
3666 (delete-region (point) (bibtex-end-of-text-in-field bounds)) 3308 (delete-region (point) (bibtex-end-of-text-in-field bounds))
3667 (insert (concat (bibtex-field-left-delimiter) 3309 (insert (concat (bibtex-field-left-delimiter)
3668 (bibtex-field-right-delimiter)) ) 3310 (bibtex-field-right-delimiter)) )
3669 (bibtex-find-text t))) 3311 (bibtex-find-text t)))
3670 3312
3671(defun bibtex-pop-previous (arg) 3313(defun bibtex-pop-previous (arg)
@@ -3682,115 +3324,105 @@ intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3682 (interactive "p") 3324 (interactive "p")
3683 (bibtex-pop arg 'next)) 3325 (bibtex-pop arg 'next))
3684 3326
3685(defun bibtex-clean-entry (&optional new-reference-key called-by-reformat) 3327(defun bibtex-clean-entry (&optional new-key called-by-reformat)
3686 "Finish editing the current BibTeX entry and clean it up. 3328 "Finish editing the current BibTeX entry and clean it up.
3687Checks that no required fields are empty and formats entry dependent 3329Checks that no required fields are empty and formats entry dependent
3688on the value of bibtex-entry-format. 3330on the value of `bibtex-entry-format'.
3689If the reference key of the entry is empty or a prefix argument is given, 3331If the reference key of the entry is empty or a prefix argument is given,
3690calculate a new reference key (note: this only will work if fields in entry 3332calculate a new reference key. (Note: this only will work if fields in entry
3691begin on separate lines prior to calling bibtex-clean-entry or if 'realign is 3333begin on separate lines prior to calling `bibtex-clean-entry' or if
3692contained in bibtex-entry-format). 3334'realign is contained in `bibtex-entry-format'.)
3693Don't call this on `string' or `preamble' entries. 3335Don't call this on @String or @Preamble entries.
3694At end of the cleaning process, the functions in 3336At end of the cleaning process, the functions in
3695bibtex-clean-entry-hook are called with region narrowed to entry." 3337`bibtex-clean-entry-hook' are called with region narrowed to entry."
3338 ;; Opt. arg called-by-reformat is t if bibtex-clean-entry
3339 ;; is called by bibtex-reformat
3696 (interactive "P") 3340 (interactive "P")
3697 (bibtex-format-entry) 3341 (bibtex-format-entry)
3698 (let* ((case-fold-search t) 3342 (let ((case-fold-search t)
3699 (eob (bibtex-end-of-entry)) 3343 key)
3700 (key (progn 3344 (bibtex-beginning-of-entry)
3701 (bibtex-beginning-of-entry) 3345 (setq key (save-excursion
3702 (if (re-search-forward 3346 (if (re-search-forward bibtex-entry-maybe-empty-head nil t)
3703 bibtex-entry-head eob t) 3347 (bibtex-key-in-head))))
3704 (buffer-substring-no-properties 3348 (when (or new-key (not key))
3705 (match-beginning bibtex-key-in-head) 3349 (setq key (if bibtex-autokey-edit-before-use
3706 (match-end bibtex-key-in-head)))))) 3350 (read-from-minibuffer
3707 (if (or 3351 "Key to use: " (bibtex-generate-autokey) nil nil
3708 new-reference-key 3352 'bibtex-key-history)
3709 (not key)) 3353 (bibtex-generate-autokey)))
3710 (progn 3354 (bibtex-beginning-of-entry)
3711 (let ((autokey 3355 (re-search-forward bibtex-entry-maybe-empty-head)
3712 (if bibtex-autokey-edit-before-use 3356 (if (match-beginning bibtex-key-in-head)
3713 (read-from-minibuffer 3357 (delete-region (match-beginning bibtex-key-in-head)
3714 "Key to use: " (bibtex-generate-autokey) nil nil 3358 (match-end bibtex-key-in-head)))
3715 'bibtex-key-history) 3359 (insert key))
3716 (bibtex-generate-autokey)))) 3360 (let* ((start (bibtex-beginning-of-entry))
3717 (bibtex-beginning-of-entry) 3361 (end (progn (bibtex-end-of-entry)
3718 (re-search-forward bibtex-entry-maybe-empty-head) 3362 (if (re-search-forward
3719 (if (match-beginning bibtex-key-in-head) 3363 bibtex-entry-maybe-empty-head nil 'move)
3720 (delete-region (match-beginning bibtex-key-in-head) 3364 (goto-char (match-beginning 0)))
3721 (match-end bibtex-key-in-head))) 3365 (point)))
3722 (insert autokey) 3366 (entry (buffer-substring start end))
3723 (let* ((start (bibtex-beginning-of-entry)) 3367 (index (progn (goto-char start)
3724 (end (progn 3368 (bibtex-entry-index))))
3725 (bibtex-end-of-entry) 3369 (delete-region start end)
3726 (if (re-search-forward 3370 (unless (prog1 (or called-by-reformat
3727 bibtex-entry-maybe-empty-head nil 'move) 3371 (if bibtex-maintain-sorted-entries
3728 (goto-char (match-beginning 0))) 3372 (bibtex-prepare-new-entry index)
3729 (point))) 3373 (not (bibtex-find-entry (car index)))))
3730 (entry (buffer-substring start end)))
3731 (delete-region start end)
3732 (let ((success
3733 (or
3734 called-by-reformat
3735 (not bibtex-maintain-sorted-entries)
3736 (bibtex-find-entry-location autokey t))))
3737 (insert entry) 3374 (insert entry)
3738 (forward-char -1) 3375 (forward-char -1)
3739 (bibtex-beginning-of-entry) 3376 (bibtex-beginning-of-entry) ; moves backward
3740 (re-search-forward bibtex-entry-head) 3377 (re-search-forward bibtex-entry-head))
3741 (if (not success) 3378 (error "New inserted entry yields duplicate key")))
3742 (error 3379 (unless called-by-reformat
3743 "New inserted entry yields duplicate key"))))))))
3744 (if (not called-by-reformat)
3745 (save-excursion 3380 (save-excursion
3746 (save-restriction 3381 (save-restriction
3747 (narrow-to-region 3382 (bibtex-narrow-to-entry)
3748 (bibtex-beginning-of-entry) (bibtex-end-of-entry)) 3383 ;; Only update the list of keys if it's been built already.
3749 (bibtex-parse-keys t nil) 3384 (if (consp bibtex-reference-keys) (bibtex-parse-keys t nil))
3750 (run-hooks 'bibtex-clean-entry-hook))))) 3385 (run-hooks 'bibtex-clean-entry-hook))))))
3751 3386
3752(defun bibtex-fill-entry () 3387(defun bibtex-fill-entry ()
3753 "Fill current entry. 3388 "Fill current BibTeX entry.
3754Realigns entry, so that every field starts on a separate line. Field 3389Realign entry, so that every field starts on a separate line. Field
3755names appear in column `bibtex-field-indentation', field text starts in 3390names appear in column `bibtex-field-indentation', field text starts in
3756column `bibtex-text-indentation' and continuation lines start here, too. 3391column `bibtex-text-indentation' and continuation lines start here, too.
3757If `bibtex-align-at-equal-sign' is non-nil, align equal signs also." 3392If `bibtex-align-at-equal-sign' is non-nil, align equal signs also."
3758 (interactive "*") 3393 (interactive "*")
3759 (let ((pnt (copy-marker (point))) 3394 (let ((pnt (copy-marker (point)))
3760 (end (copy-marker (bibtex-end-of-entry)))) 3395 (end (copy-marker (bibtex-end-of-entry)))
3396 bounds)
3761 (bibtex-beginning-of-entry) 3397 (bibtex-beginning-of-entry)
3762 (bibtex-delete-whitespace) 3398 (bibtex-delete-whitespace)
3763 (indent-to-column bibtex-entry-offset) 3399 (indent-to-column bibtex-entry-offset)
3764 (let ((bounds (bibtex-search-forward-field bibtex-field-name end))) 3400 (while (setq bounds (bibtex-search-forward-field bibtex-field-name end))
3765 (while bounds 3401 (let* ((begin-field (copy-marker (bibtex-start-of-field bounds)))
3766 (let* ((begin-field (copy-marker (bibtex-start-of-field bounds))) 3402 (end-field (copy-marker (bibtex-end-of-field bounds))))
3767 (end-field (copy-marker (bibtex-end-of-field bounds))) 3403 (goto-char begin-field)
3768 (begin-name (copy-marker (bibtex-start-of-name-in-field bounds))) 3404 (forward-char)
3769 (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))) 3405 (bibtex-delete-whitespace)
3770 (goto-char begin-field) 3406 (open-line 1)
3771 (forward-char) 3407 (forward-char)
3772 (bibtex-delete-whitespace) 3408 (indent-to-column (+ bibtex-entry-offset
3773 (open-line 1) 3409 bibtex-field-indentation))
3774 (forward-char) 3410 (re-search-forward "[ \t\n]*=" end)
3775 (indent-to-column 3411 (replace-match "=")
3776 (+ bibtex-entry-offset bibtex-field-indentation)) 3412 (forward-char -1)
3777 (re-search-forward "[ \t\n]*=" end) 3413 (if bibtex-align-at-equal-sign
3778 (replace-match "=") 3414 (indent-to-column
3779 (forward-char -1) 3415 (+ bibtex-entry-offset (- bibtex-text-indentation 2)))
3780 (if bibtex-align-at-equal-sign 3416 (insert " "))
3781 (indent-to-column 3417 (forward-char)
3782 (+ bibtex-entry-offset (- bibtex-text-indentation 2))) 3418 (bibtex-delete-whitespace)
3783 (insert " ")) 3419 (if bibtex-align-at-equal-sign
3784 (forward-char) 3420 (insert " ")
3785 (bibtex-delete-whitespace) 3421 (indent-to-column bibtex-text-indentation))
3786 (if bibtex-align-at-equal-sign 3422 (while (re-search-forward "[ \t\n]+" end-field 'move)
3787 (insert " ") 3423 (replace-match " "))
3788 (indent-to-column bibtex-text-indentation)) 3424 (do-auto-fill)
3789 (while (re-search-forward "[ \t\n]+" end-field 'move) 3425 (goto-char end-field)))
3790 (replace-match " "))
3791 (bibtex-do-auto-fill)
3792 (goto-char end-field))
3793 (setq bounds (bibtex-search-forward-field bibtex-field-name end))))
3794 (if (looking-at ",") 3426 (if (looking-at ",")
3795 (forward-char)) 3427 (forward-char))
3796 (bibtex-delete-whitespace) 3428 (bibtex-delete-whitespace)
@@ -3814,101 +3446,69 @@ If mark is active it reformats entries in region, if not in whole buffer."
3814 (if additional-options 3446 (if additional-options
3815 (if use-previous-options 3447 (if use-previous-options
3816 bibtex-reformat-previous-options 3448 bibtex-reformat-previous-options
3817 (setq 3449 (setq bibtex-reformat-previous-options
3818 bibtex-reformat-previous-options 3450 (delq nil (list
3819 (delq 3451 (if (or called-by-convert-alien
3820 nil 3452 (y-or-n-p "Realign entries (recommended)? "))
3821 (list 3453 'realign)
3822 (if (or 3454 (if (y-or-n-p "Remove empty optional and alternative fields? ")
3823 called-by-convert-alien 3455 'opts-or-alts)
3824 (y-or-n-p 3456 (if (y-or-n-p "Remove delimiters around pure numerical fields? ")
3825 "Realign entries (recommended for files not created by BibTeX mode)? ")) 3457 'numerical-fields)
3826 'realign) 3458 (if (y-or-n-p (concat (if bibtex-comma-after-last-field "Insert" "Remove")
3827 (if (y-or-n-p 3459 " comma at end of entry? "))
3828 "Remove empty optional and alternative fields? ") 3460 'last-comma)
3829 'opts-or-alts) 3461 (if (y-or-n-p "Replace double page dashes by single ones? ")
3830 (if (y-or-n-p 3462 'page-dashes)
3831 "Remove delimiters around pure numerical fields? ") 3463 (if (y-or-n-p "Force delimiters? ")
3832 'numerical-fields) 3464 'delimiters)
3833 (if (y-or-n-p (concat 3465 (if (y-or-n-p "Unify case of entry types and field names? ")
3834 (if bibtex-comma-after-last-field 3466 'unify-case)))))
3835 "Insert"
3836 "Remove")
3837 " comma at end of entry? "))
3838 'last-comma)
3839 (if (y-or-n-p
3840 "Replace double page dashes by single ones? ")
3841 'page-dashes)
3842 (if (y-or-n-p
3843 "Force delimiters? ")
3844 'delimiters)
3845 (if (y-or-n-p
3846 "Unify case of entry types and field names? ")
3847 'unify-case)))))
3848 '(realign))) 3467 '(realign)))
3849 (reformat-reference-keys 3468 (reformat-reference-keys (if additional-options
3850 (if additional-options 3469 (if use-previous-options
3851 (if use-previous-options 3470 bibtex-reformat-previous-reference-keys
3852 bibtex-reformat-previous-reference-keys 3471 (setq bibtex-reformat-previous-reference-keys
3853 (setq 3472 (y-or-n-p "Generate new reference keys automatically? ")))))
3854 bibtex-reformat-previous-reference-keys
3855 (y-or-n-p "Generate new reference keys automatically? ")))))
3856 bibtex-autokey-edit-before-use 3473 bibtex-autokey-edit-before-use
3857 (bibtex-sort-ignore-string-entries t) 3474 (bibtex-sort-ignore-string-entries t)
3858 (start-point 3475 (start-point (if (bibtex-mark-active)
3859 (if (bibtex-mark-active) 3476 (region-beginning)
3860 (region-beginning) 3477 (bibtex-beginning-of-first-entry)
3861 (progn 3478 (bibtex-skip-to-valid-entry)
3862 (bibtex-beginning-of-first-entry) 3479 (point)))
3863 (bibtex-skip-to-valid-entry) 3480 (end-point (if (bibtex-mark-active)
3864 (point)))) 3481 (region-end)
3865 (end-point 3482 (point-max))))
3866 (if (bibtex-mark-active)
3867 (region-end)
3868 (point-max)))
3869 (valid-bibtex-entry
3870 (concat
3871 "[ \t\n]+\\(@[ \t]*\\("
3872 (mapconcat
3873 (lambda (type)
3874 (concat "\\(" (car type) "\\)"))
3875 bibtex-entry-field-alist
3876 "\\|")
3877 "\\)\\)")))
3878 (save-restriction 3483 (save-restriction
3879 (narrow-to-region start-point end-point) 3484 (narrow-to-region start-point end-point)
3880 (if (memq 'realign bibtex-entry-format) 3485 (when (memq 'realign bibtex-entry-format)
3881 (progn 3486 (goto-char (point-min))
3882 (goto-char (point-min)) 3487 (while (re-search-forward bibtex-valid-entry-whitespace-re nil t)
3883 (while (re-search-forward valid-bibtex-entry nil t) 3488 (replace-match "\n\\1")))
3884 (replace-match "\n\\1"))))
3885 (goto-char start-point) 3489 (goto-char start-point)
3886 (bibtex-progress-message "Formatting" 1) 3490 (bibtex-progress-message "Formatting" 1)
3887 (bibtex-map-entries 3491 (bibtex-map-entries (lambda (current)
3888 (lambda (current) 3492 (bibtex-progress-message)
3889 (bibtex-progress-message) 3493 (bibtex-clean-entry reformat-reference-keys t)
3890 (bibtex-clean-entry reformat-reference-keys reformat-reference-keys) 3494 (when (memq 'realign bibtex-entry-format)
3891 (if (memq 'realign bibtex-entry-format) 3495 (bibtex-end-of-entry)
3892 (progn 3496 (bibtex-delete-whitespace)
3893 (bibtex-end-of-entry) 3497 (open-line 2))))
3894 (bibtex-delete-whitespace)
3895 (open-line 2)))))
3896 (bibtex-progress-message 'done)) 3498 (bibtex-progress-message 'done))
3897 (if (and 3499 (when (and reformat-reference-keys
3898 reformat-reference-keys 3500 bibtex-maintain-sorted-entries
3899 bibtex-maintain-sorted-entries 3501 (not called-by-convert-alien))
3900 (not called-by-convert-alien)) 3502 (bibtex-sort-buffer)
3901 (progn 3503 (setq bibtex-reference-keys nil)
3902 (bibtex-sort-buffer) 3504 (bibtex-parse-keys nil t t))
3903 (setq bibtex-reference-keys nil)
3904 (bibtex-parse-keys nil t t)))
3905 (goto-char pnt))) 3505 (goto-char pnt)))
3906 3506
3907(defun bibtex-convert-alien (&optional do-additional-reformatting) 3507(defun bibtex-convert-alien (&optional do-additional-reformatting)
3908 "Converts an alien BibTeX buffer to be fully usable by BibTeX mode. 3508 "Convert an alien BibTeX buffer to be fully usable by BibTeX mode.
3909If a file doesn't confirm with some standards used by BibTeX mode, 3509If a file does not conform with some standards used by BibTeX mode,
3910some of the high-level features of BibTeX mode won't be available. 3510some of the high-level features of BibTeX mode will not be available.
3911This function tries to convert current buffer to confirm with these standards. 3511This function tries to convert current buffer to conform with these standards.
3912With prefix argument DO-ADDITIONAL-REFORMATTING 3512With prefix argument DO-ADDITIONAL-REFORMATTING
3913non-nil, read options for reformatting entries from minibuffer." 3513non-nil, read options for reformatting entries from minibuffer."
3914 (interactive "*P") 3514 (interactive "*P")
@@ -3920,149 +3520,185 @@ non-nil, read options for reformatting entries from minibuffer."
3920 (message 3520 (message
3921 "If errors occur, correct them and call `bibtex-convert-alien' again") 3521 "If errors occur, correct them and call `bibtex-convert-alien' again")
3922 (sit-for 5 nil t) 3522 (sit-for 5 nil t)
3923 (if (let ((bibtex-mark-active) 3523 (deactivate-mark) ; So bibtex-validate works on the whole buffer.
3924 bibtex-maintain-sorted-entries) 3524 (when (let (bibtex-maintain-sorted-entries)
3925 (bibtex-validate)) 3525 (bibtex-validate))
3926 (progn 3526 (message "Starting to reformat entries...")
3927 (message "Starting to reformat entries...") 3527 (sit-for 2 nil t)
3928 (sit-for 2 nil t) 3528 (bibtex-reformat do-additional-reformatting t)
3929 (bibtex-reformat do-additional-reformatting t) 3529 (when bibtex-maintain-sorted-entries
3930 (if bibtex-maintain-sorted-entries 3530 (message "Starting to sort buffer...")
3931 (progn 3531 (bibtex-sort-buffer))
3932 (message "Starting to sort buffer...") 3532 (goto-char (point-max))
3933 (bibtex-sort-buffer))) 3533 (message "Buffer is now parsable. Please save it.")))
3934 (goto-char (point-max)) 3534
3935 (message "Buffer is now parsable. Please save it.")))) 3535(defun bibtex-complete ()
3936 3536 "Complete word fragment before point according to context.
3937(defun bibtex-complete-string () 3537If inside key or crossref field complete key based on
3938 "Complete word fragment before point to longest prefix of a defined string. 3538`bibtex-reference-keys'. Inside any other field perform string
3939If point is not after the part of a word, all strings are listed. 3539completion based on `bibtex-strings'. An error is signaled if point
3940Remove surrounding delimiters if complete string could be expanded." 3540is outside key or BibTeX field."
3941 (interactive "*") 3541 (interactive)
3942 (bibtex-complete bibtex-strings t)) 3542 (let* ((pnt (point))
3943 3543 (case-fold-search t)
3944(defun bibtex-complete-key () 3544 bounds compl)
3945 "Complete word fragment before point to longest prefix of a defined key. 3545 (save-excursion
3946If point is not after the part of a word, all keys are listed. This 3546 (if (and (setq bounds (bibtex-enclosing-field t))
3947function is most useful in completing crossref entries." 3547 (>= pnt (bibtex-start-of-text-in-field bounds))
3948 (interactive "*") 3548 (<= pnt (bibtex-end-of-text-in-field bounds)))
3949 (if (not bibtex-reference-keys) 3549 (progn
3950 (bibtex-parse-keys nil t)) 3550 (goto-char (bibtex-start-of-name-in-field bounds))
3951 (bibtex-complete bibtex-reference-keys)) 3551 (setq compl (if (string= "crossref"
3552 (downcase
3553 (buffer-substring-no-properties
3554 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)")
3555 (match-end 0)
3556 (point))
3557 (bibtex-end-of-name-in-field bounds))))
3558 'key
3559 'str)))
3560 (bibtex-beginning-of-entry)
3561 (if (and (re-search-forward bibtex-entry-maybe-empty-head nil t)
3562 ;; point is inside a key
3563 (or (and (match-beginning bibtex-key-in-head)
3564 (>= pnt (match-beginning bibtex-key-in-head))
3565 (<= pnt (match-end bibtex-key-in-head)))
3566 ;; or point is on empty key
3567 (and (not (match-beginning bibtex-key-in-head))
3568 (= pnt (match-end 0)))))
3569 (setq compl 'key))))
3570
3571 (cond ((equal compl 'key)
3572 ;; key completion
3573 (unless (consp bibtex-reference-keys) (bibtex-parse-keys nil t))
3574 (setq choose-completion-string-functions
3575 (lambda (choice buffer mini-p base-size)
3576 (bibtex-choose-completion-string choice buffer mini-p base-size)
3577 (if bibtex-complete-key-cleanup
3578 (funcall bibtex-complete-key-cleanup choice))
3579 ;; return t (required by choose-completion-string-functions)
3580 t))
3581 (let ((choice (bibtex-complete-internal bibtex-reference-keys)))
3582 (if bibtex-complete-key-cleanup
3583 (funcall bibtex-complete-key-cleanup choice))))
3584
3585 ((equal compl 'str)
3586 ;; string completion
3587 (setq choose-completion-string-functions
3588 (lambda (choice buffer mini-p base-size)
3589 (bibtex-choose-completion-string choice buffer mini-p base-size)
3590 (bibtex-complete-string-cleanup choice)
3591 ;; return t (required by choose-completion-string-functions)
3592 t))
3593 (bibtex-complete-string-cleanup (bibtex-complete-internal bibtex-strings)))
3594
3595 (t (error "Point outside key or BibTeX field")))))
3952 3596
3953(defun bibtex-Article () 3597(defun bibtex-Article ()
3954 "Insert a new BibTeX @Article entry; see also `bibtex-entry'." 3598 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
3955 (interactive) 3599 (interactive "*")
3956 (bibtex-entry "Article")) 3600 (bibtex-entry "Article"))
3957 3601
3958(defun bibtex-Book () 3602(defun bibtex-Book ()
3959 "Insert a new BibTeX @Book entry; see also `bibtex-entry'." 3603 "Insert a new BibTeX @Book entry; see also `bibtex-entry'."
3960 (interactive) 3604 (interactive "*")
3961 (bibtex-entry "Book")) 3605 (bibtex-entry "Book"))
3962 3606
3963(defun bibtex-Booklet () 3607(defun bibtex-Booklet ()
3964 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'." 3608 "Insert a new BibTeX @Booklet entry; see also `bibtex-entry'."
3965 (interactive) 3609 (interactive "*")
3966 (bibtex-entry "Booklet")) 3610 (bibtex-entry "Booklet"))
3967 3611
3968(defun bibtex-InBook () 3612(defun bibtex-InBook ()
3969 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'." 3613 "Insert a new BibTeX @InBook entry; see also `bibtex-entry'."
3970 (interactive) 3614 (interactive "*")
3971 (bibtex-entry "InBook")) 3615 (bibtex-entry "InBook"))
3972 3616
3973(defun bibtex-InCollection () 3617(defun bibtex-InCollection ()
3974 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'." 3618 "Insert a new BibTeX @InCollection entry; see also `bibtex-entry'."
3975 (interactive) 3619 (interactive "*")
3976 (bibtex-entry "InCollection")) 3620 (bibtex-entry "InCollection"))
3977 3621
3978(defun bibtex-InProceedings () 3622(defun bibtex-InProceedings ()
3979 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'." 3623 "Insert a new BibTeX @InProceedings entry; see also `bibtex-entry'."
3980 (interactive) 3624 (interactive "*")
3981 (bibtex-entry "InProceedings")) 3625 (bibtex-entry "InProceedings"))
3982 3626
3983(defun bibtex-Manual () 3627(defun bibtex-Manual ()
3984 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'." 3628 "Insert a new BibTeX @Manual entry; see also `bibtex-entry'."
3985 (interactive) 3629 (interactive "*")
3986 (bibtex-entry "Manual")) 3630 (bibtex-entry "Manual"))
3987 3631
3988(defun bibtex-MastersThesis () 3632(defun bibtex-MastersThesis ()
3989 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'." 3633 "Insert a new BibTeX @MastersThesis entry; see also `bibtex-entry'."
3990 (interactive) 3634 (interactive "*")
3991 (bibtex-entry "MastersThesis")) 3635 (bibtex-entry "MastersThesis"))
3992 3636
3993(defun bibtex-Misc () 3637(defun bibtex-Misc ()
3994 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'." 3638 "Insert a new BibTeX @Misc entry; see also `bibtex-entry'."
3995 (interactive) 3639 (interactive "*")
3996 (bibtex-entry "Misc")) 3640 (bibtex-entry "Misc"))
3997 3641
3998(defun bibtex-PhdThesis () 3642(defun bibtex-PhdThesis ()
3999 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'." 3643 "Insert a new BibTeX @PhdThesis entry; see also `bibtex-entry'."
4000 (interactive) 3644 (interactive "*")
4001 (bibtex-entry "PhdThesis")) 3645 (bibtex-entry "PhdThesis"))
4002 3646
4003(defun bibtex-Proceedings () 3647(defun bibtex-Proceedings ()
4004 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'." 3648 "Insert a new BibTeX @Proceedings entry; see also `bibtex-entry'."
4005 (interactive) 3649 (interactive "*")
4006 (bibtex-entry "Proceedings")) 3650 (bibtex-entry "Proceedings"))
4007 3651
4008(defun bibtex-TechReport () 3652(defun bibtex-TechReport ()
4009 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'." 3653 "Insert a new BibTeX @TechReport entry; see also `bibtex-entry'."
4010 (interactive) 3654 (interactive "*")
4011 (bibtex-entry "TechReport")) 3655 (bibtex-entry "TechReport"))
4012 3656
4013(defun bibtex-Unpublished () 3657(defun bibtex-Unpublished ()
4014 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'." 3658 "Insert a new BibTeX @Unpublished entry; see also `bibtex-entry'."
4015 (interactive) 3659 (interactive "*")
4016 (bibtex-entry "Unpublished")) 3660 (bibtex-entry "Unpublished"))
4017 3661
4018(defun bibtex-String () 3662(defun bibtex-String (&optional key)
4019 "Insert a new BibTeX @String entry." 3663 "Insert a new BibTeX @String entry with key KEY."
4020 (interactive) 3664 (interactive
4021 (if (not bibtex-reference-keys) 3665 (progn (unless (consp bibtex-reference-keys) (bibtex-parse-keys nil t))
4022 (bibtex-parse-keys nil t)) 3666 (list (completing-read "String key: " bibtex-reference-keys
4023 (let ((key 3667 nil nil nil 'bibtex-key-history))))
4024 (if (and 3668 ;; If we want to sort String entries only the sorting scheme
4025 bibtex-maintain-sorted-entries 3669 ;; entry-class is meaningful
4026 (not bibtex-sort-ignore-string-entries)) 3670 (let ((bibtex-maintain-sorted-entries
4027 (completing-read 3671 (if (and (not bibtex-sort-ignore-string-entries)
4028 "String key: " bibtex-reference-keys nil nil nil 'bibtex-key-history)))) 3672 bibtex-maintain-sorted-entries)
4029 (if (and 3673 'entry-class))
4030 bibtex-maintain-sorted-entries 3674 endpos)
4031 (not bibtex-sort-ignore-string-entries)) 3675 (unless (bibtex-prepare-new-entry (list key nil "String"))
4032 (bibtex-find-entry-location key) 3676 (error "Entry with key `%s' already exists" key))
4033 (bibtex-move-outside-of-entry)) 3677 (if (zerop (length key)) (setq key nil))
4034 (indent-to-column bibtex-entry-offset) 3678 (indent-to-column bibtex-entry-offset)
4035 (insert 3679 (insert "@String"
4036 (concat 3680 (bibtex-entry-left-delimiter))
4037 "@String" 3681 (if key
4038 (bibtex-entry-left-delimiter) 3682 (insert key)
4039 (if (and 3683 (setq endpos (point)))
4040 bibtex-maintain-sorted-entries 3684 (insert " = "
4041 (not bibtex-sort-ignore-string-entries)) 3685 (bibtex-field-left-delimiter))
4042 key) 3686 (if key
4043 " = " 3687 (setq endpos (point)))
4044 (bibtex-field-left-delimiter) 3688 (insert (bibtex-field-right-delimiter)
4045 (bibtex-field-right-delimiter) 3689 (bibtex-entry-right-delimiter)
4046 (bibtex-entry-right-delimiter) 3690 "\n")
4047 "\n")) 3691 (goto-char endpos)))
4048 (forward-line -1)
4049 (forward-char
4050 (if (and
4051 bibtex-maintain-sorted-entries
4052 (not bibtex-sort-ignore-string-entries))
4053 (+ (length "@String{") (length key) (length " = {"))
4054 (length "@String{")))))
4055 3692
4056(defun bibtex-Preamble () 3693(defun bibtex-Preamble ()
4057 "Insert a new BibTeX @Preamble entry." 3694 "Insert a new BibTeX @Preamble entry."
4058 (interactive) 3695 (interactive "*")
4059 (bibtex-move-outside-of-entry) 3696 (bibtex-move-outside-of-entry)
4060 (indent-to-column bibtex-entry-offset) 3697 (indent-to-column bibtex-entry-offset)
4061 (insert 3698 (insert "@Preamble"
4062 "@Preamble" 3699 (bibtex-entry-left-delimiter)
4063 (bibtex-entry-left-delimiter) 3700 (bibtex-entry-right-delimiter)
4064 (bibtex-entry-right-delimiter) 3701 "\n")
4065 "\n")
4066 (forward-line -1) 3702 (forward-line -1)
4067 (forward-char 10)) 3703 (forward-char 10))
4068 3704