comparison lisp/textmodes/bibtex.el @ 27701:5e135f0faf1d

(bibtex-mode): Replaced manual splitting of path at ':' characters by call to split-string. Added RCS version identification. Some temporary comments removed. (bibtex-field-name, bibtex-entry-type): Made the relationship explicit. (bibtex-field-const): Allow capital letters. (bibtex-start-of-string): Deleted because unused. Unified some nomenclature. We no longer use the term 'reference' to describe a bibtex entry as a whole. Further, reference keys are no longer called 'labels'. (bibtex-keys): Renamed to bibtex-reference-keys. (bibtex-reformat-previous-labels): Renamed to bibtex-reformat-previous-reference-keys. (bibtex-reference-type): Renamed to bibtex-entry-type. (bibtex-reference-head): Renamed to bibtex-entry-head. (bibtex-reference-maybe-empty-head): Renamed to bibtex-entry-maybe-empty-head. (bibtex-key-in-reference): Renamed to bibtex-key-in-entry. (bibtex-search-reference): Renamed to bibtex-search-entry. (bibtex-enclosing-reference-maybe-empty-head): Renamed to bibtex-enclosing-entry-maybe-empty-head. (bibtex-entry-field-alist, bibtex-entry-head, bibtex-font-lock-keywords, bibtex-skip-to-valid-entry, bibtex-map-entries, bibtex-search-entry, bibtex-enclosing-entry-maybe-empty-head, bibtex-format-entry, bibtex-generate-autokey, bibtex-parse-keys, bibtex-pop, bibtex-mode, bibtex-submit-bug-report, bibtex-entry, bibtex-print-help-message, bibtex-make-field, bibtex-end-of-entry, bibtex-sort-buffer, bibtex-find-entry-location, bibtex-validate, bibtex-find-text, bibtex-kill-entry, bibtex-clean-entry, bibtex-reformat, bibtex-complete-key, bibtex-String) : Use the new nomenclature. (bibtex-strings, bibtex-keys): Removed redundant comment. (bibtex-format-field-delimiters): New function, functionality extracted from bibtex-format-entry. (bibtex-autokey-get-yearfield-digits): New function, functionality extracted from bibtex-autokey-get-yearfield. Completely reimplemented parsing of bibtex entries in order to avoid stack overflow in the regexp matcher if field contents become large. (bibtex-cfield, bibtex-name-in-cfield, bibtex-text-in-cfield, bibtex-field-string-part-not-braced, bibtex-field-string-part-no-inner-braces, bibtex-field-string-part-1-inner-brace, bibtex-field-string-part-2-inner-braces, bibtex-field-string-part-3-inner-braces, bibtex-field-string-braced, bibtex-field-string-quoted, bibtex-field-string, bibtex-field-string-or-const, bibtex-field-text, bibtex-field, bibtex-name-in-field, bibtex-text-in-field, bibtex-reference-infix, bibtex-string, bibtex-key-in-string, bibtex-text-in-string): Deleted as parsing is now performed by the following functions. (bibtex-parse-nested-braces, bibtex-parse-field-string-braced, bibtex-parse-quoted-string, bibtex-parse-field-string-quoted, bibtex-parse-field-string, bibtex-search-forward-field-string, bibtex-parse-association, bibtex-field-name-for-parsing, bibtex-parse-field-name, bibtex-parse-field-text, bibtex-parse-field, bibtex-search-forward-field, bibtex-search-backward-field, bibtex-start-of-field, bibtex-end-of-field, bibtex-start-of-name-in-field, bibtex-end-of-name-in-field, bibtex-start-of-text-in-field, bibtex-end-of-text-in-field, bibtex-parse-string-prefix, bibtex-parse-string-postfix, bibtex-parse-string, bibtex-search-forward-string, bibtex-search-backward-string, bibtex-start-of-string, bibtex-end-of-string, bibtex-start-of-reference-key-in-string, bibtex-end-of-reference-key-in-string, bibtex-start-of-text-in-string, bibtex-end-of-text-in-string): New functions for the parsing of bibtex entries. Instead of reporting the results of the parsing by match-beginning or match-end, these functions return data structures that hold the corresponding positions. (bibtex-enclosing-field): Changed to also report field boundaries by return values rather than by match-beginning or match-end. The following functions have been adapted to use the new parsing functions. (bibtex-skip-to-valid-entry, bibtex-search-reference, bibtex-enclosing-field, bibtex-format-entry, bibtex-autokey-get-namefield, bibtex-autokey-get-titlestring, bibtex-parse-keys, bibtex-complete, bibtex-pop, bibtex-mode, bibtex-print-help-message, bibtex-end-of-entry, bibtex-ispell-abstract, bibtex-validate, bibtex-next-field, bibtex-find-text, bibtex-remove-OPT-or-ALT, bibtex-remove-delimiters, bibtex-kill-field, bibtex-empty-field, bibtex-fill-entry): Use the new method for parsing. (bibtex-assoc-of-regexp, bibtex-skip-to-valid-entry, bibtex-map-entries, bibtex-flash-head, bibtex-enclosing-reference-maybe-empty-head, bibtex-format-entry, bibtex-autokey-change, bibtex-autokey-get-namefield, bibtex-autokey-get-names, bibtex-autokey-get-titlestring, bibtex-autokey-get-titles, bibtex-parse-keys, bibtex-pop, bibtex-mode, bibtex-end-of-entry, bibtex-ispell-abstract, bibtex-sort-buffer, bibtex-validate, bibtex-remove-OPT-or-ALT, bibtex-remove-delimiters, bibtex-kill-field, bibtex-kill-entry): Use let* instead of let in order to make the new binding of case-fold-search immediately visible. Copyright notice is up to date. Added constant 'bibtex-maintainer-salutation. (bibtex-mode): use with-temp-buffer rather than make-temp-name, use match-string-no-properties and eliminate a quadratic behavior when building bibtex-strings. (bibtex-reference-key): Accept string entries whose reference key contains upper case letters. (bibtex-reference-head): Allow entries to start with a new line. Hiding of entry bodies is not longer provided by bibtex.el directly. Instead the hideshow package can be used. Added a special bibtex entry to hs-special-modes-alist. (bibtex-hs-forward-sexp): Added for hideshow.el. (bibtex-entry-field-alist): Added booktitle field to proceedings entry type (for cross referencing). Thanks to Wagner Toledo Correa for the suggestion. Added `reftex-view-crossref-from-bibtex' to menu.
author Gerd Moellmann <gerd@gnu.org>
date Tue, 15 Feb 2000 09:38:09 +0000
parents eac840d283b6
children ad7104908c16
comparison
equal deleted inserted replaced
27700:f1f85d7d12c1 27701:5e135f0faf1d
363 ("address" "Address of the school (if not part of field \"school\") or country") 363 ("address" "Address of the school (if not part of field \"school\") or country")
364 ("month" "Month of the publication as a string (remove braces)") 364 ("month" "Month of the publication as a string (remove braces)")
365 ("note" "Remarks to be put at the end of the \\bibitem"))))) 365 ("note" "Remarks to be put at the end of the \\bibitem")))))
366 ("Misc" . ((() 366 ("Misc" . ((()
367 (("author" "Author1 [and Author2 ...] [and others]") 367 (("author" "Author1 [and Author2 ...] [and others]")
368 ("title" "Title of the reference (BibTeX converts it to lowercase)") 368 ("title" "Title of the work (BibTeX converts it to lowercase)")
369 ("howpublished" "The way in which the reference was published") 369 ("howpublished" "The way in which the work was published")
370 ("month" "Month of the publication as a string (remove braces)") 370 ("month" "Month of the publication as a string (remove braces)")
371 ("year" "Year of publication") 371 ("year" "Year of publication")
372 ("note" "Remarks to be put at the end of the \\bibitem"))))) 372 ("note" "Remarks to be put at the end of the \\bibitem")))))
373 ("PhdThesis" . (((("author" "Author1 [and Author2 ...] [and others]") 373 ("PhdThesis" . (((("author" "Author1 [and Author2 ...] [and others]")
374 ("title" "Title of the PhD. thesis") 374 ("title" "Title of the PhD. thesis")
398 ("number" "Number of the technical report") 398 ("number" "Number of the technical report")
399 ("address" "Address of the institution (if not part of field \"institution\") or country") 399 ("address" "Address of the institution (if not part of field \"institution\") or country")
400 ("month" "Month of the publication as a string (remove braces)") 400 ("month" "Month of the publication as a string (remove braces)")
401 ("note" "Remarks to be put at the end of the \\bibitem"))))) 401 ("note" "Remarks to be put at the end of the \\bibitem")))))
402 ("Unpublished" . (((("author" "Author1 [and Author2 ...] [and others]") 402 ("Unpublished" . (((("author" "Author1 [and Author2 ...] [and others]")
403 ("title" "Title of the unpublished reference (BibTeX converts it to lowercase)") 403 ("title" "Title of the unpublished work (BibTeX converts it to lowercase)")
404 ("note" "Remarks to be put at the end of the \\bibitem")) 404 ("note" "Remarks to be put at the end of the \\bibitem"))
405 (("month" "Month of the publication as a string (remove braces)") 405 (("month" "Month of the publication as a string (remove braces)")
406 ("year" "Year of publication"))))) 406 ("year" "Year of publication")))))
407 ) 407 )
408 408
409 "Defines reference types and their associated fields. 409 "Defines entry types and their associated fields.
410 List of 410 List of
411 (ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL)) 411 (ENTRY-NAME (REQUIRED OPTIONAL) (CROSSREF-REQUIRED CROSSREF-OPTIONAL))
412 triples. 412 triples.
413 If the third element is nil, the first pair is always used. 413 If the third element is nil, the first pair is always used.
414 If not, the second pair is used in the case of presence of a crossref 414 If not, the second pair is used in the case of presence of a crossref
928 (defvar bibtex-last-kill-command nil) 928 (defvar bibtex-last-kill-command nil)
929 ;; Holds the type of the last kill command (either 'field or 'entry) 929 ;; Holds the type of the last kill command (either 'field or 'entry)
930 930
931 (defvar bibtex-strings nil) 931 (defvar bibtex-strings nil)
932 ;; Candidates for bibtex-complete-string. Initialized from 932 ;; Candidates for bibtex-complete-string. Initialized from
933 ;; bibtex-predefined-strings and bibtex-string-files. This variable is 933 ;; bibtex-predefined-strings and bibtex-string-files.
934 ;; buffer-local.
935 (make-variable-buffer-local 'bibtex-strings) 934 (make-variable-buffer-local 'bibtex-strings)
936 935
937 (defvar bibtex-keys nil) 936 (defvar bibtex-reference-keys nil)
938 ;; Candidates for TAB completion when entering a reference key using 937 ;; Candidates for TAB completion when entering a reference key using
939 ;; the minibuffer. Also used for bibtex-complete-key. Initialized in 938 ;; the minibuffer. Also used for bibtex-complete-key. Initialized in
940 ;; bibtex-mode and updated for each new entry. This variable is 939 ;; bibtex-mode and updated for each new entry.
941 ;; buffer-local. 940 (make-variable-buffer-local 'bibtex-reference-keys)
942 (make-variable-buffer-local 'bibtex-keys)
943 941
944 (defvar bibtex-buffer-last-parsed-tick nil) 942 (defvar bibtex-buffer-last-parsed-tick nil)
945 ;; Remembers the value returned by buffer-modified-tick when buffer 943 ;; Remembers the value returned by buffer-modified-tick when buffer
946 ;; was parsed for keys the last time. 944 ;; was parsed for keys the last time.
947 (make-variable-buffer-local 'bibtex-buffer-last-parsed-tick) 945 (make-variable-buffer-local 'bibtex-buffer-last-parsed-tick)
968 ;; Used to store the history list for reading field names 966 ;; Used to store the history list for reading field names
969 967
970 (defvar bibtex-reformat-previous-options nil) 968 (defvar bibtex-reformat-previous-options nil)
971 ;; Used to store the last reformat options given 969 ;; Used to store the last reformat options given
972 970
973 (defvar bibtex-reformat-previous-labels nil) 971 (defvar bibtex-reformat-previous-reference-keys nil)
974 ;; Used to store the last reformat label option given 972 ;; Used to store the last reformat reference keys option given
975 973
976 974
977 ;; Functions to Parse the BibTeX Entries 975 ;; Functions to Parse the BibTeX Entries
978 976
979 (defun bibtex-cfield (name text) 977 (defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*")
980 ;; Create a regexp for a BibTeX field of name NAME and text TEXT. 978 ;; Regexp defining the name part of a BibTeX field.
981 (concat ",[ \t\n]*\\(" 979
982 name 980 (defconst bibtex-entry-type (concat "@" bibtex-field-name))
983 "\\)[ \t\n]*=[ \t\n]*\\(" 981 ;; Regexp defining the type part of a BibTeX entry.
984 text
985 "\\)"))
986 (defconst bibtex-name-in-cfield 1)
987 ;; The regexp subexpression number of the name part in bibtex-cfield.
988
989 (defconst bibtex-text-in-cfield 2)
990 ;; The regexp subexpression number of the text part in bibtex-cfield.
991
992 (defconst bibtex-reference-type "@[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*")
993 ;; Regexp defining the type part of a BibTeX reference entry (almost
994 ;; the same as bibtex-field-name)
995 982
996 (defconst bibtex-reference-key "[][a-zA-Z0-9.:;?!`'/*@+=|()<>&_^$-]+") 983 (defconst bibtex-reference-key "[][a-zA-Z0-9.:;?!`'/*@+=|()<>&_^$-]+")
997 ;; Regexp defining the label part of a BibTeX reference entry 984 ;; Regexp defining the reference key part of a BibTeX entry
998 985
999 (defconst bibtex-field-name "[^\"#%'(),={} \t\n0-9][^\"#%'(),={} \t\n]*") 986 (defun bibtex-parse-nested-braces (nesting-level)
1000 ;; Regexp defining the name part of a BibTeX field (almost the same as 987 "*Starting on an opening brace, find the corresponding closing brace.
1001 ;; bibtex-reference-type) 988 When the function is called, NESTING-LEVEL has to be set to `0'."
1002 989 (cond ((looking-at "{")
1003 (defconst bibtex-field-const "[][a-z0-9.:;?!`'/*@+=|<>&_^$-]+") 990 (search-forward-regexp "{[^{}]*")
991 (bibtex-parse-nested-braces (+ nesting-level 1)))
992 ((looking-at "}")
993 (forward-char 1)
994 (if (= nesting-level 1)
995 (point)
996 (search-forward-regexp "[^{}]*")
997 (bibtex-parse-nested-braces (- nesting-level 1))))
998 (t nil)))
999
1000 (defun bibtex-parse-field-string-braced ()
1001 "*Parse a field string enclosed by braces.
1002 The field string has to be syntactically correct, which means that the number
1003 of opening and closing braces has to match. If this is the case, a pair
1004 containing the start and end position of the field string is returned, nil
1005 otherwise."
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.
1033 If a syntactically correct string is found, a pair containing the start and
1034 end 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
1045 (defun bibtex-parse-field-string ()
1046 "*Parse a field string enclosed by braces or quotes.
1047 If a syntactically correct string is found, a pair containing the start and
1048 end position of the field string is returned, nil otherwise."
1049 (save-match-data
1050 (let ((starting-point (point))
1051 (boundaries (or (bibtex-parse-field-string-braced)
1052 (bibtex-parse-field-string-quoted))))
1053 (goto-char starting-point)
1054 boundaries)))
1055
1056 (defun bibtex-search-forward-field-string (bound)
1057 "*Search forward to find a field string enclosed by braces or quotes.
1058 If a syntactically correct string is found, a pair containing the start and
1059 end position of the field string is returned, nil otherwise. The search is
1060 delimited 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
1076 (defun bibtex-parse-association (parse-lhs parse-rhs)
1077 "*Parse a string of the format <left hand side = right-hand-side>.
1078 The functions PARSE-LHS and PARSE-RHS are used to parse the corresponding
1079 substrings. These functions are expected to return nil if parsing is not
1080 successfull. If both functions return non-nil, a pair containing the returned
1081 values of the functions PARSE-LHS and PARSE-RHSis returned."
1082 (save-match-data
1083 (let ((starting-point (point))
1084 (left (funcall parse-lhs))
1085 (right nil))
1086 (if (and left (looking-at "[ \t\n]*=[ \t\n]*"))
1087 (progn
1088 (goto-char (match-end 0))
1089 (setq right (funcall parse-rhs))))
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
1100 (defun bibtex-parse-field-name ()
1101 "*Parse the field name stored in bibtex-field-name-for-parsing.
1102 If the field name is found, return a triple consisting of the position of the
1103 very first character of the match, the actual starting position of the name
1104 part and end position of the match."
1105 (if (looking-at ",[ \t\n]*")
1106 (let ((start (point)))
1107 (goto-char (match-end 0))
1108 (if (looking-at bibtex-field-name-for-parsing)
1109 (let ((boundaries (list start (match-beginning 0) (match-end 0))))
1110 (goto-char (match-end 0))
1111 boundaries)))))
1112
1113 (defconst bibtex-field-const "[][a-zA-Z0-9.:;?!`'/*@+=|<>&_^$-]+")
1004 ;; Regexp defining a bibtex field constant 1114 ;; Regexp defining a bibtex field constant
1005 1115
1006 (defconst bibtex-field-string-part-not-braced 1116 (defun bibtex-parse-field-text ()
1007 "[^{}]") 1117 "*Parse the text part of a BibTeX field.
1008 ;; Match field string part without braces 1118 The text part is either a string, or an empty string, or a constant followed
1009 1119 by one or more <# (string|constant)> pairs. If a syntactically correct text
1010 (defconst bibtex-field-string-part-no-inner-braces 1120 is found, a pair containing the start and end position of the text is
1011 (concat 1121 returned, nil otherwise."
1012 "{" 1122 (let ((starting-point (point))
1013 bibtex-field-string-part-not-braced "*" 1123 (end-point nil)
1014 "}")) 1124 (failure nil))
1015 ;; Match field string part with no inner braces 1125 (while (and (not end-point) (not failure))
1016 1126 (if (looking-at bibtex-field-const)
1017 (defconst bibtex-field-string-part-1-inner-brace 1127 (goto-char (match-end 0))
1018 (concat 1128 (let ((boundaries (bibtex-parse-field-string)))
1019 "{" 1129 (if boundaries
1020 "\\(" 1130 (goto-char (cdr boundaries))
1021 bibtex-field-string-part-not-braced 1131 (setq failure t))))
1022 "\\|" 1132 (if (not (looking-at "[ \t\n]*#[ \t\n]*"))
1023 "\\(" bibtex-field-string-part-no-inner-braces "\\)" 1133 (setq end-point (point))
1024 "\\)*" 1134 (goto-char (match-end 0))))
1025 "}")) 1135 (if (and (not failure) end-point)
1026 ;; Match field string part with at most 1 inner brace 1136 (cons starting-point end-point)
1027 1137 nil)))
1028 (defconst bibtex-field-string-part-2-inner-braces 1138
1029 (concat 1139 (defun bibtex-parse-field (name)
1030 "{" 1140 "*Parse a BibTeX field of regexp NAME.
1031 "\\(" 1141 If a syntactically correct field is found, a pair containing the boundaries of
1032 bibtex-field-string-part-not-braced 1142 the name and text parts of the field is returned."
1033 "\\|" 1143 (setq bibtex-field-name-for-parsing name)
1034 "\\(" bibtex-field-string-part-1-inner-brace "\\)" 1144 (bibtex-parse-association 'bibtex-parse-field-name
1035 "\\)*" 1145 'bibtex-parse-field-text))
1036 "}")) 1146
1037 ;; Match field string part with at most 2 inner braces 1147 (defun bibtex-search-forward-field (name bound)
1038 1148 "*Search forward to find a field of name NAME.
1039 (defconst bibtex-field-string-part-3-inner-braces 1149 If a syntactically correct field is found, a pair containing the boundaries of
1040 (concat 1150 the name and text parts of the field is returned. The search is limited by
1041 "{" 1151 BOUND."
1042 "\\(" 1152 (save-match-data
1043 bibtex-field-string-part-not-braced 1153 (setq bibtex-field-name-for-parsing name)
1044 "\\|" 1154 (let ((starting-point (point))
1045 "\\(" bibtex-field-string-part-2-inner-braces "\\)" 1155 (boundaries nil))
1046 "\\)*" 1156 (while (and (not boundaries)
1047 "}")) 1157 (< (point) bound)
1048 ;; Match field string part with at most 3 inner braces 1158 (search-forward "," bound t))
1049 1159 (goto-char (match-beginning 0))
1050 (defconst bibtex-field-string-braced 1160 (let ((temp-boundaries
1051 bibtex-field-string-part-3-inner-braces) 1161 (bibtex-parse-association 'bibtex-parse-field-name
1052 ;; Match braced field string with inner nesting level of braces at most 3 1162 'bibtex-parse-field-text)))
1053 1163 (if (and temp-boundaries (<= (cdr (cdr temp-boundaries)) bound))
1054 (defconst bibtex-field-string-quoted 1164 (setq boundaries temp-boundaries)
1055 (concat 1165 (forward-char 1))))
1056 "\"" 1166 (goto-char starting-point)
1057 "\\(" 1167 boundaries)))
1058 "[^\"\\]" ;; every character except quote or backslash 1168
1059 "\\|" 1169 (defun bibtex-search-backward-field (name bound)
1060 ;; "\\(" "\"[a-z-]" "\\)" ;; a quote followed by a letter or dash 1170 "*Search backward to find a field of name NAME.
1061 ;; "\\|" 1171 If a syntactically correct field is found, a pair containing the boundaries of
1062 ;; last two lines commented out until lines like 1172 the name and text parts of the field is returned. The search is limited by
1063 ;; author = "Stefan Sch"of" 1173 BOUND."
1064 ;; are supported by BibTeX 1174 (save-match-data
1065 "\\(" "\\\\\\(.\\|\n\\)" "\\)" ;; a backslash followed by any character 1175 (setq bibtex-field-name-for-parsing name)
1066 "\\)*" 1176 (let ((starting-point (point))
1067 "\"")) 1177 (boundaries nil))
1068 ;; Match quoted field string 1178 (while (and (not boundaries)
1069 1179 (>= (point) bound)
1070 (defconst bibtex-field-string 1180 (search-backward "," bound t))
1071 (concat 1181 (let ((temp-boundaries
1072 "\\(" bibtex-field-string-braced "\\)" 1182 (bibtex-parse-association 'bibtex-parse-field-name
1073 "\\|" 1183 'bibtex-parse-field-text)))
1074 "\\(" bibtex-field-string-quoted "\\)")) 1184 (if temp-boundaries
1075 ;; Match a braced or quoted string 1185 (setq boundaries temp-boundaries))))
1076 1186 (goto-char starting-point)
1077 (defconst bibtex-field-string-or-const 1187 boundaries)))
1078 (concat bibtex-field-const "\\|" bibtex-field-string)) 1188
1079 ;; Match either bibtex-field-string or bibtex-field-const. 1189 (defun bibtex-start-of-field (bounds)
1080 1190 (car (car bounds)))
1081 (defconst bibtex-field-text 1191 (defun bibtex-end-of-field (bounds)
1082 (concat 1192 (cdr (cdr bounds)))
1083 "\\(" bibtex-field-string-or-const "\\)" 1193 (defun bibtex-start-of-name-in-field (bounds)
1084 "\\([ \t\n]+#[ \t\n]+\\(" bibtex-field-string-or-const "\\)\\)*")) 1194 (car (cdr (car bounds))))
1085 ;; Regexp defining the text part of a BibTeX field: either a string, 1195 (defun bibtex-end-of-name-in-field (bounds)
1086 ;; or an empty string, or a constant followed by one or more # / 1196 (car (cdr (cdr (car bounds)))))
1087 ;; constant pairs. 1197 (defun bibtex-start-of-text-in-field (bounds)
1088 1198 (car (cdr bounds)))
1089 (defconst bibtex-field 1199 (defun bibtex-end-of-text-in-field (bounds)
1090 (bibtex-cfield bibtex-field-name bibtex-field-text)) 1200 (cdr (cdr bounds)))
1091 ;; Regexp defining the format of a BibTeX field. 1201
1092 1202 (defun bibtex-parse-string-prefix ()
1093 (defconst bibtex-name-in-field bibtex-name-in-cfield) 1203 "*Parse the prefix part of a bibtex string, including the reference key.
1094 ;; The regexp subexpression number of the name part in BibTeX-field. 1204 If the string prefix is found, return a triple consisting of the position of
1095 1205 the very first character of the match, the actual starting position of the
1096 (defconst bibtex-text-in-field bibtex-text-in-cfield) 1206 reference key and the end position of the match."
1097 ;; The regexp subexpression number of the text part in BibTeX-field. 1207 (let* ((case-fold-search t))
1098 1208 (if (looking-at "^[ \t]*@string[ \t\n]*[({][ \t\n]*")
1099 (defconst bibtex-reference-head 1209 (let ((start (point)))
1210 (goto-char (match-end 0))
1211 (if (looking-at bibtex-reference-key)
1212 (let ((boundaries (list start (match-beginning 0) (match-end 0))))
1213 (goto-char (match-end 0))
1214 boundaries))))))
1215
1216 (defun bibtex-parse-string-postfix ()
1217 "*Parse the postfix part of a bibtex string, including the text.
1218 If the string postfix is found, return a triple consisting of the position of
1219 the actual starting and ending position of the text and the very last
1220 character of the string entry."
1221 (let* ((case-fold-search t)
1222 (text-boundaries (bibtex-parse-field-text)))
1223 (if text-boundaries
1224 (progn
1225 (goto-char (cdr text-boundaries))
1226 (if (looking-at "[ \t\n]*[})]")
1227 (let ((boundaries (list (car text-boundaries)
1228 (cdr text-boundaries)
1229 (match-end 0))))
1230 (goto-char (match-end 0))
1231 boundaries))))))
1232
1233 (defun bibtex-parse-string ()
1234 "*Parse a BibTeX string entry.
1235 If a syntactically correct entry is found, a pair containing the boundaries of
1236 the reference key and text parts of the entry is returned."
1237 (bibtex-parse-association 'bibtex-parse-string-prefix
1238 'bibtex-parse-string-postfix))
1239
1240 (defun bibtex-search-forward-string ()
1241 "*Search forward to find a bibtex string entry.
1242 If a syntactically correct entry is found, a pair containing the boundaries of
1243 the reference key and text parts of the string is returned."
1244 (save-match-data
1245 (let* ((case-fold-search t)
1246 (starting-point (point))
1247 (boundaries nil))
1248 (while (and (not boundaries)
1249 (search-forward-regexp
1250 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t))
1251 (goto-char (match-beginning 0))
1252 (let ((temp-boundaries (bibtex-parse-string)))
1253 (if temp-boundaries
1254 (setq boundaries temp-boundaries)
1255 (forward-char 1))))
1256 (goto-char starting-point)
1257 boundaries)))
1258
1259 (defun bibtex-search-backward-string ()
1260 "*Search backward to find a bibtex string entry.
1261 If a syntactically correct entry is found, a pair containing the boundaries of
1262 the reference key and text parts of the field is returned."
1263 (save-match-data
1264 (let* ((case-fold-search t)
1265 (starting-point (point))
1266 (boundaries nil))
1267 (while (and (not boundaries)
1268 (search-backward-regexp
1269 "^[ \t]*@string[ \t\n]*[({][ \t\n]*" nil t))
1270 (goto-char (match-beginning 0))
1271 (let ((temp-boundaries (bibtex-parse-string)))
1272 (if temp-boundaries
1273 (setq boundaries temp-boundaries))))
1274 (goto-char starting-point)
1275 boundaries)))
1276
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)
1284 (car (cdr bounds)))
1285 (defun bibtex-end-of-text-in-string (bounds)
1286 (car (cdr (cdr bounds))))
1287
1288 (defconst bibtex-entry-head
1100 (concat "^[ \t]*\\(" 1289 (concat "^[ \t]*\\("
1101 bibtex-reference-type 1290 bibtex-entry-type
1102 "\\)[ \t]*[({][ \t\n]*\\(" 1291 "\\)[ \t]*[({][ \t\n]*\\("
1103 bibtex-reference-key 1292 bibtex-reference-key
1104 "\\)")) 1293 "\\)"))
1105 ;; Regexp defining format of the header line of a BibTeX reference 1294 ;; Regexp defining format of the header line of a BibTeX entry.
1106 ;; entry. 1295
1107 1296 (defconst bibtex-entry-maybe-empty-head
1108 (defconst bibtex-reference-maybe-empty-head 1297 (concat bibtex-entry-head "?"))
1109 (concat bibtex-reference-head "?"))
1110 ;; Regexp defining format of the header line of a maybe empty 1298 ;; Regexp defining format of the header line of a maybe empty
1111 ;; BibTeX reference entry (without reference key). 1299 ;; BibTeX entry (possibly without reference key).
1112 1300
1113 (defconst bibtex-type-in-head 1) 1301 (defconst bibtex-type-in-head 1)
1114 ;; The regexp subexpression number of the type part in 1302 ;; The regexp subexpression number of the type part in
1115 ;; bibtex-reference-head. 1303 ;; bibtex-entry-head.
1116 1304
1117 (defconst bibtex-key-in-head 2) 1305 (defconst bibtex-key-in-head 2)
1118 ;; The regexp subexpression number of the key part in 1306 ;; The regexp subexpression number of the key part in
1119 ;; bibtex-reference-head. 1307 ;; bibtex-entry-head.
1120 1308
1121 (defconst bibtex-reference-infix (concat "[ \t\n]*" bibtex-field)) 1309 (defconst bibtex-entry-postfix "[ \t\n]*,?[ \t\n]*[})]")
1122 ;; Regexp defining the (repeatable) infix of a bibtex reference 1310 ;; Regexp defining the postfix of a bibtex entry
1123 1311
1124 (defconst bibtex-reference-postfix "[ \t\n]*,?[ \t\n]*[})]") 1312 (defconst bibtex-key-in-entry bibtex-key-in-head)
1125 ;; Regexp defining the postfix of a bibtex reference 1313 ;; The regexp subexpression number of the key part in a bibtex entry.
1126
1127 (defconst bibtex-key-in-reference bibtex-key-in-head)
1128 ;; The regexp subexpression number of the key part in
1129 ;; bibtex-reference.
1130
1131 (defconst bibtex-string
1132 (concat "^[ \t]*@string[ \t\n]*[({][ \t\n]*\\("
1133 bibtex-reference-key
1134 "\\)[ \t\n]*=[ \t\n]*\\("
1135 bibtex-field-text
1136 "\\)[ \t\n]*[})]"))
1137 ;; Regexp defining the format of a BibTeX string entry.
1138
1139 (defconst bibtex-key-in-string 1)
1140 ;; The regexp subexpression of the name part in bibtex-string.
1141
1142 (defconst bibtex-text-in-string 2)
1143 ;; The regexp subexpression of the text part in bibtex-string.
1144 1314
1145 (defvar bibtex-font-lock-keywords 1315 (defvar bibtex-font-lock-keywords
1146 (list 1316 (list
1147 ;; reference type and reference label 1317 ;; entry type and reference key
1148 (list bibtex-reference-maybe-empty-head 1318 (list bibtex-entry-maybe-empty-head
1149 (list bibtex-type-in-head 'font-lock-function-name-face) 1319 (list bibtex-type-in-head 'font-lock-function-name-face)
1150 (list bibtex-key-in-head 'font-lock-constant-face nil t)) 1320 (list bibtex-key-in-head 'font-lock-constant-face nil t))
1151 ;; comments 1321 ;; comments
1152 (list 1322 (list
1153 (concat "^\\([ \t]*" bibtex-comment-start ".*\\)$") 1323 (concat "^\\([ \t]*" bibtex-comment-start ".*\\)$")
1160 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=") 1330 (list (concat "^[ \t]*\\(" bibtex-field-name "\\)[ \t]*=")
1161 1 'font-lock-variable-name-face) 1331 1 'font-lock-variable-name-face)
1162 "*Default expressions to highlight in BibTeX mode.")) 1332 "*Default expressions to highlight in BibTeX mode."))
1163 ;; now all needed patterns are defined 1333 ;; now all needed patterns are defined
1164 1334
1335
1165 1336
1166 ;; Helper Functions 1337 ;; Helper Functions
1167 1338
1168 (defun bibtex-delete-whitespace () 1339 (defun bibtex-delete-whitespace ()
1169 ;; Delete all whitespace starting at point 1340 ;; Delete all whitespace starting at point
1178 1349
1179 (defun bibtex-member-of-regexp (string list) 1350 (defun bibtex-member-of-regexp (string list)
1180 ;; Return non-nil if STRING is exactly matched by an element of 1351 ;; Return non-nil if STRING is exactly matched by an element of
1181 ;; LIST. The value is actually the tail of LIST whose 1352 ;; LIST. The value is actually the tail of LIST whose
1182 ;; car matches STRING. 1353 ;; car matches STRING.
1183 (let (case-fold-search) 1354 (let* (case-fold-search)
1184 (while 1355 (while
1185 (and list (not (string-match (concat "^" (car list) "$") string))) 1356 (and list (not (string-match (concat "^" (car list) "$") string)))
1186 (setq list (cdr list))) 1357 (setq list (cdr list)))
1187 list)) 1358 list))
1188 1359
1189 (defun bibtex-assoc-of-regexp (string alist) 1360 (defun bibtex-assoc-of-regexp (string alist)
1190 ;; Return non-nil if STRING is exactly matched by the car of an 1361 ;; Return non-nil if STRING is exactly matched by the car of an
1191 ;; element of LIST (case ignored). The value is actually the element 1362 ;; element of LIST (case ignored). The value is actually the element
1192 ;; of LIST whose car matches STRING. 1363 ;; of LIST whose car matches STRING.
1193 (let ((case-fold-search t)) 1364 (let* ((case-fold-search t))
1194 (while 1365 (while
1195 (and alist 1366 (and alist
1196 (not (string-match (concat "^" (car (car alist)) "$") string))) 1367 (not (string-match (concat "^" (car (car alist)) "$") string)))
1197 (setq alist (cdr alist))) 1368 (setq alist (cdr alist)))
1198 (car alist))) 1369 (car alist)))
1202 ;; the next valid one. With argument backward non-nil, move backward 1373 ;; the next valid one. With argument backward non-nil, move backward
1203 ;; to beginning of previous valid one. A valid entry is a 1374 ;; to beginning of previous valid one. A valid entry is a
1204 ;; syntactical correct one with type contained in 1375 ;; syntactical correct one with type contained in
1205 ;; bibtex-entry-field-alist or, if bibtex-sort-ignore-string-entries 1376 ;; bibtex-entry-field-alist or, if bibtex-sort-ignore-string-entries
1206 ;; is nil, a syntactical correct string entry. 1377 ;; is nil, a syntactical correct string entry.
1207 (let ((case-fold-search t) 1378 (let* ((case-fold-search t)
1208 (valid-bibtex-entry 1379 (valid-bibtex-entry
1209 (concat 1380 (concat
1210 "@[ \t]*\\(" 1381 "@[ \t]*\\("
1211 (mapconcat 1382 (mapconcat
1212 (lambda (type) 1383 (lambda (type)
1213 (concat "\\(" (car type) "\\)")) 1384 (concat "\\(" (car type) "\\)"))
1214 bibtex-entry-field-alist 1385 bibtex-entry-field-alist
1215 "\\|") 1386 "\\|")
1216 "\\)")) 1387 "\\)"))
1217 found) 1388 found)
1218 (while (and (not found) 1389 (while (and (not found)
1219 (not (if backward 1390 (not (if backward
1220 (bobp) 1391 (bobp)
1221 (eobp)))) 1392 (eobp))))
1222 (let ((pnt (point))) 1393 (let ((pnt (point)))
1223 (cond 1394 (cond
1224 ((looking-at valid-bibtex-entry) 1395 ((looking-at valid-bibtex-entry)
1225 (if (and 1396 (if (and
1226 (bibtex-search-reference nil nil t) 1397 (bibtex-search-entry nil nil t)
1227 (equal (match-beginning 0) pnt)) 1398 (equal (match-beginning 0) pnt))
1228 (setq found t))) 1399 (setq found t)))
1229 ((and (not bibtex-sort-ignore-string-entries) 1400 ((and (not bibtex-sort-ignore-string-entries)
1230 (looking-at bibtex-string)) 1401 (bibtex-parse-string))
1231 (setq found t))) 1402 (setq found t)))
1232 (if found 1403 (if found
1233 (goto-char pnt) 1404 (goto-char pnt)
1234 (if backward 1405 (if backward
1235 (progn 1406 (progn
1244 ;; Call FUN for each BibTeX entry starting with the current. Do this 1415 ;; Call FUN for each BibTeX entry starting with the current. Do this
1245 ;; to the end of the file. FUN is called with one argument, the key 1416 ;; to the end of the file. FUN is called with one argument, the key
1246 ;; of the entry, and with point inside the entry. If 1417 ;; of the entry, and with point inside the entry. If
1247 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be 1418 ;; bibtex-sort-ignore-string-entries is non-nil, FUN will not be
1248 ;; called for @String entries. 1419 ;; called for @String entries.
1249 (let ((case-fold-search t)) 1420 (let* ((case-fold-search t))
1250 (bibtex-beginning-of-entry) 1421 (bibtex-beginning-of-entry)
1251 (while (re-search-forward bibtex-reference-maybe-empty-head nil t) 1422 (while (re-search-forward bibtex-entry-maybe-empty-head nil t)
1252 (let ((pnt (point)) 1423 (let ((pnt (point))
1253 (reference-type 1424 (entry-type
1254 (downcase (buffer-substring-no-properties 1425 (downcase (buffer-substring-no-properties
1255 (1+ (match-beginning bibtex-type-in-head)) 1426 (1+ (match-beginning bibtex-type-in-head))
1256 (match-end bibtex-type-in-head)))) 1427 (match-end bibtex-type-in-head))))
1257 (reference-key 1428 (reference-key
1258 (if (match-beginning bibtex-key-in-head) 1429 (if (match-beginning bibtex-key-in-head)
1261 (match-end bibtex-key-in-head)) 1432 (match-end bibtex-key-in-head))
1262 ""))) 1433 "")))
1263 (if (or 1434 (if (or
1264 (and 1435 (and
1265 (not bibtex-sort-ignore-string-entries) 1436 (not bibtex-sort-ignore-string-entries)
1266 (string-equal "string" (downcase reference-type))) 1437 (string-equal "string" (downcase entry-type)))
1267 (assoc-ignore-case reference-type bibtex-entry-field-alist)) 1438 (assoc-ignore-case entry-type bibtex-entry-field-alist))
1268 (funcall fun reference-key)) 1439 (funcall fun reference-key))
1269 (goto-char pnt) 1440 (goto-char pnt)
1270 (bibtex-end-of-entry))))) 1441 (bibtex-end-of-entry)))))
1271 1442
1272 (defun bibtex-progress-message (&optional flag interval) 1443 (defun bibtex-progress-message (&optional flag interval)
1321 ;; returns a string dependent on bibtex-field-delimiters 1492 ;; returns a string dependent on bibtex-field-delimiters
1322 (if (equal bibtex-entry-delimiters 'braces) 1493 (if (equal bibtex-entry-delimiters 'braces)
1323 "}" 1494 "}"
1324 ")")) 1495 ")"))
1325 1496
1326 (defun bibtex-search-reference 1497 (defun bibtex-search-entry
1327 (empty-head &optional bound noerror backward) 1498 (empty-head &optional bound noerror backward)
1328 ;; A helper function necessary since the failure stack size limit for 1499 ;; A helper function necessary since the failure stack size limit for
1329 ;; regexps was reduced in emacs 19.32. 1500 ;; regexps was reduced in emacs 19.32.
1330 ;; It searches for a BibTeX reference (maybe with an empty head if 1501 ;; It searches for a BibTeX entry (maybe without a reference key if
1331 ;; EMPTY-HEAD is t). 1502 ;; EMPTY-HEAD is t).
1332 ;; BOUND and NOERROR are exactly as in re-search-forward. If 1503 ;; BOUND and NOERROR are exactly as in re-search-forward. If
1333 ;; BACKWARD is non-nil, search is done in reverse direction. After 1504 ;; BACKWARD is non-nil, search is done in reverse direction. After
1334 ;; call to this function MATCH-BEGINNING and MATCH-END functions are 1505 ;; call to this function MATCH-BEGINNING and MATCH-END functions are
1335 ;; defined, but only for the head part of the reference (especially 1506 ;; defined, but only for the head part of the entry (especially
1336 ;; (match-end 0) just gives the end of the head part). 1507 ;; (match-end 0) just gives the end of the head part).
1337 (let ((pnt (point)) 1508 (let ((pnt (point))
1338 (prefix (if empty-head 1509 (prefix (if empty-head
1339 bibtex-reference-maybe-empty-head 1510 bibtex-entry-maybe-empty-head
1340 bibtex-reference-head)) 1511 bibtex-entry-head)))
1341 (infix bibtex-reference-infix)
1342 (postfix bibtex-reference-postfix))
1343 (if backward 1512 (if backward
1344 (let (found) 1513 (let (found)
1345 (while (and (not found) 1514 (while (and (not found)
1346 (re-search-backward prefix bound noerror)) 1515 (re-search-backward prefix bound noerror))
1347 (setq found (bibtex-search-reference empty-head pnt t))) 1516 (setq found (bibtex-search-entry empty-head pnt t)))
1348 (if found 1517 (if found
1349 (goto-char (match-beginning 0)) 1518 (goto-char (match-beginning 0))
1350 (if (equal noerror nil) 1519 (if (equal noerror nil)
1351 ;; yell 1520 ;; yell
1352 (error "Search of BibTeX reference failed")) 1521 (error "Search of BibTeX entry failed"))
1353 (if (equal noerror t) 1522 (if (equal noerror t)
1354 ;; don't move 1523 ;; don't move
1355 (goto-char pnt)) 1524 (goto-char pnt))
1356 nil)) 1525 nil))
1357 (let ((limit (if bound bound (point-max))) 1526 (let ((limit (if bound bound (point-max)))
1366 (goto-char (match-end bibtex-type-in-head)) 1535 (goto-char (match-end bibtex-type-in-head))
1367 (looking-at "[ \t]*(")) 1536 (looking-at "[ \t]*("))
1368 ;; entry opened with parenthesis 1537 ;; entry opened with parenthesis
1369 ")" 1538 ")"
1370 "}"))) 1539 "}")))
1371 (while (and 1540 (let ((infix-start (point))
1372 (looking-at infix) 1541 (finished nil))
1373 (<= (match-end 0) limit)) 1542 (while (not finished)
1374 (goto-char (match-end 0))) 1543 (if (and (looking-at "[ \t\n]*")
1544 (<= (match-end 0) limit))
1545 (goto-char (match-end 0)))
1546 (let ((bounds (bibtex-parse-field bibtex-field-name)))
1547 (if (and bounds (<= (bibtex-end-of-field bounds) limit))
1548 (progn
1549 (goto-char (bibtex-end-of-field bounds))
1550 (setq infix-start (point)))
1551 (goto-char infix-start)
1552 (setq finished t)))))
1375 ;; This matches the infix* part. The AND construction assures 1553 ;; This matches the infix* part. The AND construction assures
1376 ;; that BOUND is respected. 1554 ;; that BOUND is respected.
1377 (if (and (looking-at postfix) 1555 (if (and (looking-at bibtex-entry-postfix)
1378 (string-equal 1556 (string-equal
1379 (buffer-substring-no-properties 1557 (buffer-substring-no-properties
1380 (1- (match-end 0)) (match-end 0)) 1558 (1- (match-end 0)) (match-end 0))
1381 entry-closer) 1559 entry-closer)
1382 (<= (match-end 0) limit)) 1560 (<= (match-end 0) limit))
1383 (progn 1561 (progn
1384 (re-search-forward postfix) 1562 (re-search-forward bibtex-entry-postfix)
1385 (setq found t))))) 1563 (setq found t)))))
1386 (if found 1564 (if found
1387 (progn 1565 (progn
1388 (set-match-data md) 1566 (set-match-data md)
1389 ;; to set match-beginning/end again 1567 ;; to set match-beginning/end again
1390 (point)) 1568 (point))
1391 (if (equal noerror nil) 1569 (if (equal noerror nil)
1392 ;; yell 1570 ;; yell
1393 (error "Search of BibTeX reference failed")) 1571 (error "Search of BibTeX entry failed"))
1394 (if (equal noerror t) 1572 (if (equal noerror t)
1395 ;; don't move 1573 ;; don't move
1396 (goto-char pnt)) 1574 (goto-char pnt))
1397 nil))))) 1575 nil)))))
1398 1576
1399 (defun bibtex-flash-head () 1577 (defun bibtex-flash-head ()
1400 ;; Flash at BibTeX reference head before point, if exists. 1578 ;; Flash at BibTeX entry head before point, if exists.
1401 (let ((case-fold-search t) 1579 (let* ((case-fold-search t)
1402 flash) 1580 flash)
1403 (cond ((re-search-backward bibtex-reference-head nil t) 1581 (cond ((re-search-backward bibtex-entry-head nil t)
1404 (goto-char (match-beginning bibtex-type-in-head)) 1582 (goto-char (match-beginning bibtex-type-in-head))
1405 (setq flash (match-end bibtex-key-in-reference))) 1583 (setq flash (match-end bibtex-key-in-entry)))
1406 (t 1584 (t
1407 (end-of-line) 1585 (end-of-line)
1408 (skip-chars-backward " \t") 1586 (skip-chars-backward " \t")
1409 (setq flash (point)) 1587 (setq flash (point))
1410 (beginning-of-line) 1588 (beginning-of-line)
1466 (defun bibtex-enclosing-field (&optional noerr) 1644 (defun bibtex-enclosing-field (&optional noerr)
1467 ;; Search for BibTeX field enclosing point. Point moves to end of 1645 ;; Search for BibTeX field enclosing point. Point moves to end of
1468 ;; field. Use match-beginning and match-end to parse the field. If 1646 ;; field. Use match-beginning and match-end to parse the field. If
1469 ;; NOERR is non-nil, no error is signalled. In this case, t is 1647 ;; NOERR is non-nil, no error is signalled. In this case, t is
1470 ;; returned on success, nil otherwise. 1648 ;; returned on success, nil otherwise.
1471 (let ((case-fold-search t) 1649 (let* ((case-fold-search t)
1472 (old-point (point)) 1650 (old-point (point))
1473 (boe (bibtex-beginning-of-entry)) 1651 (boe (bibtex-beginning-of-entry)))
1474 (success t))
1475 (goto-char old-point) 1652 (goto-char old-point)
1476 (if (not (re-search-backward 1653 (let ((bounds (bibtex-search-backward-field bibtex-field-name boe)))
1477 (bibtex-cfield bibtex-field-name "") 1654 (if (and bounds
1478 boe t)) 1655 (<= (bibtex-start-of-field bounds) old-point)
1479 ;; Search possible beginning of field 1656 (>= (bibtex-end-of-field bounds) old-point))
1480 (progn 1657 bounds
1481 (goto-char old-point) 1658 (if noerr
1482 (if noerr 1659 nil
1483 (setq success nil) 1660 (error "Can't find enclosing BibTeX field"))))))
1484 (error "Can't find enclosing BibTeX field"))) 1661
1485 (if (or (not (re-search-forward bibtex-field nil t)) 1662 (defun bibtex-enclosing-entry-maybe-empty-head ()
1486 (< (match-end 0) old-point) 1663 ;; Search for BibTeX entry enclosing point. Point moves to
1487 (> (match-beginning 0) old-point)) 1664 ;; end of entry. Beginning (but not end) of entry is given
1488 (progn
1489 (goto-char old-point)
1490 (if noerr
1491 (setq success nil)
1492 (error "Can't find enclosing BibTeX field")))))
1493 success))
1494
1495 (defun bibtex-enclosing-reference-maybe-empty-head ()
1496 ;; Search for BibTeX reference enclosing point. Point moves to
1497 ;; end of reference. Beginning (but not end) of reference is given
1498 ;; by (match-beginning 0). 1665 ;; by (match-beginning 0).
1499 (let ((case-fold-search t) 1666 (let* ((case-fold-search t)
1500 (old-point (point))) 1667 (old-point (point)))
1501 (if (not 1668 (if (not
1502 (re-search-backward 1669 (re-search-backward
1503 bibtex-reference-maybe-empty-head nil t)) 1670 bibtex-entry-maybe-empty-head nil t))
1504 (progn 1671 (progn
1505 (error "Can't find enclosing BibTeX reference") 1672 (error "Can't find enclosing BibTeX entry")
1506 (goto-char old-point))) 1673 (goto-char old-point)))
1507 (goto-char (match-beginning bibtex-type-in-head)) 1674 (goto-char (match-beginning bibtex-type-in-head))
1508 (if (not 1675 (if (not
1509 (bibtex-search-reference t nil t)) 1676 (bibtex-search-entry t nil t))
1510 (progn 1677 (progn
1511 (error "Can't find enclosing BibTeX reference") 1678 (error "Can't find enclosing BibTeX entry")
1512 (goto-char old-point))))) 1679 (goto-char old-point)))))
1513 1680
1514 (defun bibtex-insert-current-kill (n) 1681 (defun bibtex-insert-current-kill (n)
1515 (if (not bibtex-last-kill-command) 1682 (if (not bibtex-last-kill-command)
1516 (error "BibTeX kill ring is empty") 1683 (error "BibTeX kill ring is empty")
1543 (t 1710 (t
1544 (error 1711 (error
1545 "Unknown tag field: %s. Please submit a bug report" 1712 "Unknown tag field: %s. Please submit a bug report"
1546 bibtex-last-kill-command)))))) 1713 bibtex-last-kill-command))))))
1547 1714
1715 (defun bibtex-format-field-delimiters (start stop)
1716 "*Replaces delimiters for field strings between START and STOP.
1717 If the current delimiters equal the new delimiters, the buffer is not
1718 changed."
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
1548 (defun bibtex-format-entry () 1734 (defun bibtex-format-entry ()
1549 ;; Helper function for bibtex-clean-entry. Formats current entry 1735 ;; Helper function for bibtex-clean-entry. Formats current entry
1550 ;; according to variable bibtex-entry-format. 1736 ;; according to variable bibtex-entry-format.
1551 (let ((case-fold-search t) 1737 (let* ((case-fold-search t)
1552 (beg (point)) 1738 (beg (point))
1553 (start (bibtex-beginning-of-entry)) 1739 (start (bibtex-beginning-of-entry))
1554 crossref-there 1740 crossref-there
1555 alternatives-there 1741 alternatives-there
1556 non-empty-alternative) 1742 non-empty-alternative)
1557 (let ((end (copy-marker (bibtex-end-of-entry)))) 1743 (let ((end (copy-marker (bibtex-end-of-entry))))
1558 (if (equal start (marker-position end)) 1744 (if (equal start (marker-position end))
1559 (error "Not on a known BibTeX entry") 1745 (error "Not on a known BibTeX entry")
1560 (goto-char start) 1746 (goto-char start)
1561 (while (re-search-forward bibtex-field end t) 1747 (let ((bounds (bibtex-search-forward-field bibtex-field-name end)))
1562 ;; determine if reference has crossref entry and if at least 1748 (while bounds
1563 ;; one alternative is non-empty 1749 ;; determine if entry has crossref field and if at least
1564 (let ((begin-name (match-beginning bibtex-name-in-field)) 1750 ;; one alternative is non-empty
1565 (end-name (match-end bibtex-name-in-field)) 1751 (let ((begin-name (bibtex-start-of-name-in-field bounds))
1566 (begin-text (match-beginning bibtex-text-in-field)) 1752 (end-name (bibtex-end-of-name-in-field bounds))
1567 (end-text (match-end bibtex-text-in-field))) 1753 (begin-text (bibtex-start-of-text-in-field bounds))
1568 (goto-char begin-name) 1754 (end-text (bibtex-end-of-text-in-field bounds)))
1569 (if (looking-at "ALT") 1755 (goto-char begin-name)
1570 (progn 1756 (if (looking-at "ALT")
1571 (setq alternatives-there t) 1757 (progn
1572 (goto-char begin-text) 1758 (setq alternatives-there t)
1573 (if (not (looking-at "\\(\"\"\\)\\|\\({}\\)")) 1759 (goto-char begin-text)
1574 (setq non-empty-alternative t)))) 1760 (if (not (looking-at "\\(\"\"\\)\\|\\({}\\)"))
1575 (if (string-match 1761 (setq non-empty-alternative t))))
1576 "\\(OPT\\)?crossref" 1762 (if (string-match
1577 (buffer-substring-no-properties begin-name end-name)) 1763 "\\(OPT\\)?crossref"
1578 (progn 1764 (buffer-substring-no-properties begin-name end-name))
1579 (setq 1765 (progn
1580 crossref-there 1766 (setq
1581 (buffer-substring-no-properties 1767 crossref-there
1582 (1+ begin-text) (1- end-text))) 1768 (buffer-substring-no-properties
1583 (if (equal crossref-there "") 1769 (1+ begin-text) (1- end-text)))
1584 (setq crossref-there nil)))))) 1770 (if (equal crossref-there "")
1771 (setq crossref-there nil)))))
1772 (goto-char (bibtex-end-of-field bounds))
1773 (setq bounds (bibtex-search-forward-field bibtex-field-name end))))
1585 (if (and alternatives-there 1774 (if (and alternatives-there
1586 (not non-empty-alternative)) 1775 (not non-empty-alternative))
1587 (progn 1776 (progn
1588 (goto-char beg) 1777 (goto-char beg)
1589 (error "All alternatives are empty"))) 1778 (error "All alternatives are empty")))
1590 (goto-char start) 1779 (goto-char start)
1591 (re-search-forward bibtex-reference-type end) 1780 (re-search-forward bibtex-entry-type end)
1592 (let* ((begin-type (1+ (match-beginning 0))) 1781 (let* ((begin-type (1+ (match-beginning 0)))
1593 (end-type (match-end 0)) 1782 (end-type (match-end 0))
1594 (reference-type 1783 (entry-type
1595 (downcase 1784 (downcase
1596 (buffer-substring-no-properties begin-type end-type))) 1785 (buffer-substring-no-properties begin-type end-type)))
1597 (entry-list 1786 (entry-list
1598 (assoc-ignore-case reference-type bibtex-entry-field-alist)) 1787 (assoc-ignore-case entry-type bibtex-entry-field-alist))
1599 (req (elt (elt entry-list 1) 0)) 1788 (req (elt (elt entry-list 1) 0))
1600 (creq (elt (elt entry-list 2) 0)) 1789 (creq (elt (elt entry-list 2) 0))
1601 (format (if (equal bibtex-entry-format t) 1790 (format (if (equal bibtex-entry-format t)
1602 '(realign opts-or-alts numerical-fields 1791 '(realign opts-or-alts numerical-fields
1603 last-comma page-dashes delimiters 1792 last-comma page-dashes delimiters
1613 (goto-char end-type) 1802 (goto-char end-type)
1614 (skip-chars-forward " \t\n") 1803 (skip-chars-forward " \t\n")
1615 (delete-char 1) 1804 (delete-char 1)
1616 (insert (bibtex-entry-left-delimiter)))) 1805 (insert (bibtex-entry-left-delimiter))))
1617 (goto-char start) 1806 (goto-char start)
1618 (while (re-search-forward bibtex-field end t) 1807 (let ((bounds (bibtex-search-forward-field bibtex-field-name end)))
1619 (let* ((begin-field 1808 (while bounds
1620 (copy-marker (match-beginning 0))) 1809 (let* ((begin-field (copy-marker (bibtex-start-of-field bounds)))
1621 (end-field 1810 (end-field (copy-marker (bibtex-end-of-field bounds)))
1622 (copy-marker (match-end 0))) 1811 (begin-name (copy-marker (bibtex-start-of-name-in-field bounds)))
1623 (begin-name 1812 (end-name (copy-marker (bibtex-end-of-name-in-field bounds)))
1624 (copy-marker (match-beginning bibtex-name-in-field))) 1813 (begin-text (copy-marker (bibtex-start-of-text-in-field bounds)))
1625 (end-name 1814 (end-text (copy-marker (bibtex-end-of-text-in-field bounds)))
1626 (copy-marker (match-end bibtex-name-in-field))) 1815 (field-name
1627 (begin-text 1816 (buffer-substring-no-properties
1628 (copy-marker (match-beginning bibtex-text-in-field))) 1817 (if (string-match
1629 (end-text 1818 "^\\(OPT\\)\\|\\(ALT\\)$"
1630 (copy-marker (match-end bibtex-text-in-field))) 1819 (buffer-substring-no-properties
1631 (field-name 1820 begin-name (+ begin-name 3)))
1632 (buffer-substring-no-properties 1821 (+ begin-name 3)
1633 (if (string-match 1822 begin-name)
1634 "^OPT\\|ALT$" 1823 end-name)))
1635 (buffer-substring-no-properties 1824 (cond
1636 begin-name (+ begin-name 3))) 1825 ((and
1637 (+ begin-name 3) 1826 (memq 'opts-or-alts format)
1638 begin-name) 1827 (progn (goto-char begin-name)
1639 end-name))) 1828 (looking-at "OPT\\|ALT")))
1640 (cond 1829 (goto-char begin-text)
1641 ((and 1830 (if (looking-at "\\(\"\"\\)\\|\\({}\\)")
1642 (memq 'opts-or-alts format) 1831 ;; empty: delete whole field if really optional
1643 (progn (goto-char begin-name) 1832 ;; (missing crossref handled) or complain
1644 (looking-at "OPT\\|ALT"))) 1833 (if (and
1645 (goto-char begin-text) 1834 (progn
1646 (if (looking-at "\\(\"\"\\)\\|\\({}\\)") 1835 (goto-char begin-name)
1647 ;; empty: delete whole field if really optional 1836 (looking-at "OPT"))
1648 ;; (missing crossref handled) or complain 1837 (not crossref-there)
1649 (if (and 1838 (assoc-ignore-case field-name req))
1650 (progn 1839 ;; field is not really optional
1651 (goto-char begin-name) 1840 (progn
1652 (looking-at "OPT")) 1841 (goto-char begin-name)
1653 (not crossref-there) 1842 (bibtex-remove-OPT-or-ALT)
1654 (assoc-ignore-case field-name req)) 1843 (error
1655 ;; field is not really optional 1844 "Mandatory field ``%s'' is empty" field-name))
1656 (progn 1845 ;; field is optional
1657 (goto-char begin-name) 1846 (delete-region begin-field end-field))
1658 (bibtex-remove-OPT-or-ALT) 1847 ;; otherwise: not empty, delete "OPT"
1659 (error 1848 (goto-char begin-name)
1660 "Mandatory field ``%s'' is empty" field-name)) 1849 (bibtex-remove-OPT-or-ALT)))
1661 ;; field is optional 1850 ((and
1662 (delete-region begin-field end-field)) 1851 (memq 'numerical-fields format)
1663 ;; otherwise: not empty, delete "OPT" 1852 (progn
1664 (goto-char begin-name) 1853 (goto-char begin-text)
1665 (bibtex-remove-OPT-or-ALT))) 1854 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)")))
1666 ((and 1855 (goto-char end-text)
1667 (memq 'numerical-fields format) 1856 (delete-char -1)
1668 (progn 1857 (goto-char begin-text)
1669 (goto-char begin-text) 1858 (delete-char 1))
1670 (looking-at "\\(\"[0-9]+\"\\)\\|\\({[0-9]+}\\)"))) 1859 (t
1671 (goto-char end-text) 1860 (if (memq 'delimiters format)
1672 (delete-char -1) 1861 (bibtex-format-field-delimiters begin-text end-text))
1673 (goto-char begin-text) 1862 (if (and
1674 (delete-char 1)) 1863 (memq 'page-dashes format)
1675 (t 1864 (string-match "^\\(OPT\\)?pages$" (downcase field-name))
1676 (if (memq 'delimiters format) 1865 (progn
1677 (progn 1866 (goto-char begin-text)
1678 (goto-char begin-text) 1867 (looking-at
1679 (while (and 1868 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)")))
1680 (<= (point) end-text) 1869 (replace-match "\\1-\\2"))
1681 (re-search-forward 1870 (if (and
1682 bibtex-field-string-or-const end-text t)) 1871 (memq 'inherit-booktitle format)
1683 (let ((end (point))) 1872 (equal (downcase field-name) "booktitle")
1684 (goto-char (match-beginning 0)) 1873 (progn
1685 (if (or 1874 (goto-char begin-text)
1686 (and 1875 (looking-at "\\(\"\"\\)\\|\\({}\\)"))
1687 (equal bibtex-field-delimiters 'double-quotes) 1876 crossref-there
1688 (looking-at bibtex-field-string-braced)) 1877 (not (bibtex-find-entry-location crossref-there t)))
1689 (and 1878 ;; booktitle field empty and crossref entry found
1690 (equal bibtex-field-delimiters 'braces) 1879 ;; --> insert title field of crossreferenced entry if there
1691 (looking-at bibtex-field-string-quoted))) 1880 (let ((stop (bibtex-end-of-entry)))
1692 (progn 1881 (bibtex-beginning-of-entry)
1693 (goto-char (match-end 0)) 1882 (let ((bounds (bibtex-search-forward-field "title" stop)))
1694 (delete-backward-char 1) 1883 (if bounds
1695 (insert (bibtex-field-right-delimiter)) 1884 (progn
1696 (goto-char (match-beginning 0)) 1885 (goto-char begin-text)
1697 (delete-char 1) 1886 (forward-char)
1698 (insert (bibtex-field-left-delimiter)))) 1887 (insert
1699 (goto-char end))))) 1888 (buffer-substring-no-properties
1700 (if (and 1889 (1+ (bibtex-start-of-text-in-field bounds))
1701 (memq 'page-dashes format) 1890 (1- (bibtex-end-of-text-in-field bounds)))))))))
1702 (string-match "^\\(OPT\\)?pages$" (downcase field-name)) 1891 (if (progn
1703 (progn 1892 (goto-char begin-text)
1704 (goto-char begin-text) 1893 (looking-at "\\(\"\"\\)\\|\\({}\\)"))
1705 (looking-at 1894 ;; if empty field, complain
1706 "\\([\"{][0-9]+\\)[ \t\n]*--?[ \t\n]*\\([0-9]+[\"}]\\)"))) 1895 (progn
1707 (replace-match "\\1-\\2")) 1896 (forward-char)
1708 (if (and 1897 (if (or (and
1709 (memq 'inherit-booktitle format) 1898 crossref-there
1710 (equal (downcase field-name) "booktitle") 1899 (assoc-ignore-case
1711 (progn 1900 field-name creq))
1712 (goto-char begin-text) 1901 (and
1713 (looking-at "\\(\"\"\\)\\|\\({}\\)")) 1902 (not crossref-there)
1714 crossref-there 1903 (assoc-ignore-case
1715 (not (bibtex-find-entry-location crossref-there t))) 1904 field-name req)))
1716 ;; booktitle field empty and crossref entry found 1905 (error
1717 ;; --> insert title field of crossreferenced entry if there 1906 "Mandatory field ``%s'' is empty" field-name))))
1718 (let ((end-of-crefd-entry (bibtex-end-of-entry))) 1907 (if (memq 'unify-case format)
1719 (bibtex-beginning-of-entry) 1908 (let* ((fl
1720 (if (re-search-forward 1909 (car (cdr (assoc-ignore-case
1721 (bibtex-cfield "title" bibtex-field-text) 1910 entry-type
1722 end-of-crefd-entry t) 1911 bibtex-entry-field-alist))))
1723 (progn 1912 (field-list
1724 (goto-char begin-text) 1913 (append
1725 (forward-char) 1914 (elt fl 0)
1726 (insert 1915 (elt fl 1)
1727 (buffer-substring-no-properties 1916 bibtex-user-optional-fields))
1728 (1+ (match-beginning bibtex-text-in-field)) 1917 (new-field-name
1729 (1- (match-end bibtex-text-in-field)))))))) 1918 (car
1730 (if (progn 1919 (assoc-ignore-case field-name field-list))))
1731 (goto-char begin-text) 1920 (goto-char begin-name)
1732 (looking-at "\\(\"\"\\)\\|\\({}\\)")) 1921 (if new-field-name
1733 ;; if empty field, complain 1922 (progn
1734 (progn 1923 (delete-region begin-name end-name)
1735 (forward-char) 1924 (insert new-field-name))
1736 (if (or (and 1925 (downcase-region begin-name end-name))))
1737 crossref-there 1926 (setq field-done t)))
1738 (assoc-ignore-case 1927 (if (not field-done)
1739 field-name creq)) 1928 (goto-char begin-field)
1740 (and 1929 (setq field-done nil)
1741 (not crossref-there) 1930 (goto-char end-field)))
1742 (assoc-ignore-case 1931 (setq bounds (bibtex-search-forward-field bibtex-field-name end))))
1743 field-name req)))
1744 (error
1745 "Mandatory field ``%s'' is empty" field-name))))
1746 (if (memq 'unify-case format)
1747 (let* ((fl
1748 (car (cdr (assoc-ignore-case
1749 reference-type
1750 bibtex-entry-field-alist))))
1751 (field-list
1752 (append
1753 (elt fl 0)
1754 (elt fl 1)
1755 bibtex-user-optional-fields))
1756 (new-field-name
1757 (car
1758 (assoc-ignore-case field-name field-list))))
1759 (goto-char begin-name)
1760 (if new-field-name
1761 (progn
1762 (delete-region begin-name end-name)
1763 (insert new-field-name))
1764 (downcase-region begin-name end-name))))
1765 (setq field-done t)))
1766 (if (not field-done)
1767 (goto-char begin-field)
1768 (setq field-done nil)
1769 (goto-char end-field))))
1770 (if (looking-at (bibtex-field-right-delimiter)) 1932 (if (looking-at (bibtex-field-right-delimiter))
1771 (forward-char)) 1933 (forward-char))
1772 (if (memq 'last-comma format) 1934 (if (memq 'last-comma format)
1773 (cond ((and 1935 (cond ((and
1774 bibtex-comma-after-last-field 1936 bibtex-comma-after-last-field
1790 1952
1791 (defun bibtex-autokey-change (string change-list) 1953 (defun bibtex-autokey-change (string change-list)
1792 ;; Returns a string where some regexps are changed according to 1954 ;; Returns a string where some regexps are changed according to
1793 ;; change-list. Every item of change-list is an (old-regexp 1955 ;; change-list. Every item of change-list is an (old-regexp
1794 ;; new-string) pair. 1956 ;; new-string) pair.
1795 (let (case-fold-search 1957 (let* (case-fold-search
1796 (return-string string) 1958 (return-string string)
1797 (index 0) 1959 (index 0)
1798 (len (length change-list)) 1960 (len (length change-list))
1799 change-item) 1961 change-item)
1800 (while (< index len) 1962 (while (< index len)
1801 (setq change-item (elt change-list index)) 1963 (setq change-item (elt change-list index))
1802 (while (string-match (car change-item) return-string) 1964 (while (string-match (car change-item) return-string)
1803 (setq 1965 (setq
1804 return-string 1966 return-string
1829 (defun bibtex-autokey-get-namefield (min max) 1991 (defun bibtex-autokey-get-namefield (min max)
1830 ;; returns the contents of the name field of the current entry 1992 ;; returns the contents of the name field of the current entry
1831 ;; does some modifications based on `bibtex-autokey-name-change-strings' 1993 ;; does some modifications based on `bibtex-autokey-name-change-strings'
1832 ;; and removes newlines unconditionally 1994 ;; and removes newlines unconditionally
1833 (goto-char min) 1995 (goto-char min)
1834 (let ((case-fold-search t)) 1996 (let* ((case-fold-search t)
1835 (if (re-search-forward 1997 (author-or-editor "\\(author\\)\\|\\(editor\\)")
1836 (bibtex-cfield "\\(author\\)\\|\\(editor\\)" bibtex-field-text) 1998 (bounds (bibtex-search-forward-field author-or-editor max)))
1837 max t) 1999 (if bounds
1838 (bibtex-autokey-change 2000 (bibtex-autokey-change
1839 (buffer-substring-no-properties 2001 (buffer-substring-no-properties
1840 (1+ (match-beginning (+ bibtex-text-in-cfield 2))) 2002 (1+ (bibtex-start-of-text-in-field bounds))
1841 (1- (match-end (+ bibtex-text-in-cfield 2)))) 2003 (1- (bibtex-end-of-text-in-field bounds)))
1842 (append bibtex-autokey-name-change-strings '(("\n" " ")))) 2004 (append bibtex-autokey-name-change-strings '(("\n" " "))))
1843 ""))) 2005 "")))
1844 2006
1845 (defun bibtex-autokey-get-names (namefield) 2007 (defun bibtex-autokey-get-names (namefield)
1846 ;; gathers all names in namefield into a list 2008 ;; gathers all names in namefield into a list
1847 (let ((case-fold-search t) 2009 (let* ((case-fold-search t)
1848 names) 2010 names)
1849 (while (not (equal namefield "")) 2011 (while (not (equal namefield ""))
1850 (let (name) 2012 (let (name)
1851 (if (string-match "[ \t\n]and[ \t\n]" namefield) 2013 (if (string-match "[ \t\n]and[ \t\n]" namefield)
1852 (setq name (substring namefield 0 (match-beginning 0)) 2014 (setq name (substring namefield 0 (match-beginning 0))
1853 namefield (substring namefield (match-end 0))) 2015 namefield (substring namefield (match-end 0)))
1909 (bibtex-autokey-abbrev 2071 (bibtex-autokey-abbrev
1910 (bibtex-autokey-demangle-name fullname) 2072 (bibtex-autokey-demangle-name fullname)
1911 bibtex-autokey-name-length)) 2073 bibtex-autokey-name-length))
1912 (bibtex-autokey-get-names namefield))) 2074 (bibtex-autokey-get-names namefield)))
1913 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
1914 (defun bibtex-autokey-get-yearfield (min max) 2087 (defun bibtex-autokey-get-yearfield (min max)
1915 ;; get year field from current or maybe crossreferenced entry 2088 ;; get year field from current or maybe crossreferenced entry
1916 (let ((case-fold-search t)) 2089 (goto-char min)
1917 (goto-char min) 2090 (let* ((case-fold-search t)
1918 (if (re-search-forward 2091 (year (bibtex-autokey-get-yearfield-digits max)))
1919 (bibtex-cfield "year" bibtex-field-text) max t) 2092 (if (and (string= year "") bibtex-autokey-year-use-crossref-entry)
1920 (let ((year (buffer-substring-no-properties 2093 (let* ((bounds
1921 (match-beginning bibtex-text-in-cfield) 2094 (bibtex-search-forward-field "\\(OPT\\)?crossref" max))
1922 (match-end bibtex-text-in-cfield)))) 2095 (crossref-field
1923 (string-match "[^0-9]*\\([0-9]+\\)" year) 2096 (if bounds
1924 (substring year (match-beginning 1) (match-end 1))) 2097 (buffer-substring-no-properties
1925 (if bibtex-autokey-year-use-crossref-entry 2098 (1+ (bibtex-start-of-text-in-field bounds))
1926 (let ((crossref-field 2099 (1- (bibtex-end-of-text-in-field bounds))))))
1927 (progn 2100 (if (not (bibtex-find-entry-location crossref-field t))
1928 (goto-char min) 2101 (let ((stop (bibtex-end-of-entry)))
1929 (if (re-search-forward 2102 (bibtex-beginning-of-entry)
1930 (bibtex-cfield 2103 (bibtex-autokey-get-yearfield-digits stop))
1931 "\\(OPT\\)?crossref" bibtex-field-text) 2104 ""))
1932 max t) 2105 year)))
1933 (buffer-substring-no-properties
1934 (1+
1935 (match-beginning (+ bibtex-text-in-cfield 1)))
1936 (1-
1937 (match-end (+ bibtex-text-in-cfield 1))))))))
1938 (if (not (bibtex-find-entry-location crossref-field t))
1939 (let ((end-of-crefd-entry (bibtex-end-of-entry)))
1940 (bibtex-beginning-of-entry)
1941 (if (re-search-forward
1942 (bibtex-cfield "year" "[0-9]+")
1943 end-of-crefd-entry t)
1944 (buffer-substring-no-properties
1945 (match-beginning bibtex-text-in-cfield)
1946 (match-end bibtex-text-in-cfield))
1947 ""))
1948 ""))
1949 ""))))
1950 2106
1951 (defun bibtex-autokey-get-titlestring (min max) 2107 (defun bibtex-autokey-get-titlestring (min max)
1952 ;; get title field contents up to a terminator 2108 ;; get title field contents up to a terminator
1953 (let ((case-fold-search t)) 2109 (goto-char min)
1954 (let ((titlefield 2110 (let* ((case-fold-search t)
1955 (progn 2111 (bounds (bibtex-search-forward-field "title" max))
1956 (goto-char min) 2112 (titlefield
1957 (if (re-search-forward 2113 (if bounds
1958 (bibtex-cfield "title" bibtex-field-text) max t) 2114 (bibtex-autokey-change
1959 (bibtex-autokey-change 2115 (buffer-substring-no-properties
1960 (buffer-substring-no-properties 2116 (1+ (bibtex-start-of-text-in-field bounds))
1961 (1+ (match-beginning bibtex-text-in-cfield)) 2117 (1- (bibtex-end-of-text-in-field bounds)))
1962 (1- (match-end bibtex-text-in-cfield))) 2118 bibtex-autokey-titleword-change-strings)
1963 bibtex-autokey-titleword-change-strings) 2119 ""))
1964 ""))) 2120 (title-terminators bibtex-autokey-title-terminators))
1965 (index 0) 2121 (while (not (null title-terminators))
1966 (numberofitems 2122 (if (string-match (car title-terminators) titlefield)
1967 (length bibtex-autokey-title-terminators))) 2123 (setq titlefield (substring titlefield 0 (match-beginning 0))))
1968 (while (< index numberofitems) 2124 (setq title-terminators (cdr title-terminators)))
1969 (if (string-match 2125 titlefield))
1970 (elt bibtex-autokey-title-terminators index) titlefield)
1971 (setq
1972 titlefield (substring titlefield 0 (match-beginning 0))))
1973 (setq index (1+ index)))
1974 titlefield)))
1975 2126
1976 (defun bibtex-autokey-get-titles (titlestring) 2127 (defun bibtex-autokey-get-titles (titlestring)
1977 ;; gathers words from titlestring into a list. Ignores 2128 ;; gathers words from titlestring into a list. Ignores
1978 ;; specific words and uses only a specific amount of words. 2129 ;; specific words and uses only a specific amount of words.
1979 (let (case-fold-search 2130 (let* (case-fold-search
1980 titlewords 2131 titlewords
1981 titlewords-extra 2132 titlewords-extra
1982 (counter 0)) 2133 (counter 0))
1983 (while (and 2134 (while (and
1984 (not (equal titlestring "")) 2135 (not (equal titlestring ""))
1985 (or 2136 (or
1986 (not (numberp bibtex-autokey-titlewords)) 2137 (not (numberp bibtex-autokey-titlewords))
1987 (< counter 2138 (< counter
2055 digits (useful values are 2 and 4). If the year field is 2206 digits (useful values are 2 and 4). If the year field is
2056 absent, but the entry has a valid crossref field and the 2207 absent, but the entry has a valid crossref field and the
2057 variable `bibtex-autokey-year-use-crossref-entry' is non-nil, 2208 variable `bibtex-autokey-year-use-crossref-entry' is non-nil,
2058 use the year field of the crossreferenced entry instead. 2209 use the year field of the crossreferenced entry instead.
2059 9. For the title part of the key change the contents of the 2210 9. For the title part of the key change the contents of the
2060 title field of the reference according to 2211 title field of the entry according to
2061 `bibtex-autokey-titleword-change-strings' to the 2212 `bibtex-autokey-titleword-change-strings' to the
2062 corresponding new one (see documentation of this variable for 2213 corresponding new one (see documentation of this variable for
2063 further detail). 2214 further detail).
2064 10. Abbreviate the result to the string up to (but not including) 2215 10. Abbreviate the result to the string up to (but not including)
2065 the first occurrence of a regexp matched by the items of 2216 the first occurrence of a regexp matched by the items of
2162 (funcall bibtex-autokey-before-presentation-function autokey))) 2313 (funcall bibtex-autokey-before-presentation-function autokey)))
2163 (goto-char pnt) 2314 (goto-char pnt)
2164 autokey)) 2315 autokey))
2165 2316
2166 (defun bibtex-parse-keys (add verbose &optional abortable) 2317 (defun bibtex-parse-keys (add verbose &optional abortable)
2167 ;; Sets bibtex-keys to the keys used in the whole (possibly 2318 ;; Sets bibtex-reference-keys to the keys used in the whole (possibly
2168 ;; restricted) buffer (either as entry keys or as crossref entries). 2319 ;; restricted) buffer (either as entry keys or as crossref entries).
2169 ;; If ADD is non-nil adds the new keys to bibtex-keys instead of 2320 ;; If ADD is non-nil adds the new keys to bibtex-reference-keys instead of
2170 ;; simply resetting it. If VERBOSE is non-nil gives messages about 2321 ;; simply resetting it. If VERBOSE is non-nil gives messages about
2171 ;; progress. If ABORTABLE is non-nil abort on user input. 2322 ;; progress. If ABORTABLE is non-nil abort on user input.
2172 (if bibtex-maintain-sorted-entries 2323 (if bibtex-maintain-sorted-entries
2173 (let ((case-fold-search t) 2324 (let* ((case-fold-search t)
2174 (crossref-field 2325 (reference-keys (if add bibtex-reference-keys)))
2175 (bibtex-cfield
2176 "crossref" (concat "[{\"]" bibtex-reference-key "[}\"]")))
2177 (labels (if add
2178 bibtex-keys)))
2179 (save-excursion 2326 (save-excursion
2180 (goto-char (point-min)) 2327 (goto-char (point-min))
2181 (if verbose 2328 (if verbose
2182 (bibtex-progress-message 2329 (bibtex-progress-message
2183 (concat (buffer-name) ": parsing reference keys"))) 2330 (concat (buffer-name) ": parsing reference keys")))
2188 abortable 2335 abortable
2189 (input-pending-p)) 2336 (input-pending-p))
2190 (throw 'userkey t)) 2337 (throw 'userkey t))
2191 (if verbose 2338 (if verbose
2192 (bibtex-progress-message)) 2339 (bibtex-progress-message))
2193 (let (label 2340 (let (reference-key
2194 label2) 2341 xrefd-reference-key)
2195 (cond 2342 (cond
2196 ((looking-at bibtex-reference-head) 2343 ((looking-at bibtex-entry-head)
2197 (setq 2344 (setq
2198 label 2345 reference-key
2199 (buffer-substring-no-properties 2346 (buffer-substring-no-properties
2200 (match-beginning bibtex-key-in-head) 2347 (match-beginning bibtex-key-in-head)
2201 (match-end bibtex-key-in-head))) 2348 (match-end bibtex-key-in-head)))
2202 (let ((p (point)) 2349 (let ((p (point))
2203 (m (bibtex-end-of-entry))) 2350 (m (bibtex-end-of-entry)))
2204 (goto-char p) 2351 (goto-char p)
2205 (if (re-search-forward crossref-field m t) 2352 (let ((bounds (bibtex-search-forward-field "crossref" m)))
2206 (setq 2353 (if bounds
2207 label2 2354 (setq
2208 (buffer-substring-no-properties 2355 xrefd-reference-key
2209 (1+ (match-beginning bibtex-text-in-cfield)) 2356 (buffer-substring-no-properties
2210 (1- (match-end bibtex-text-in-cfield))))) 2357 (1+ (bibtex-start-of-text-in-field bounds))
2211 (goto-char p))) 2358 (1- (bibtex-end-of-text-in-field bounds))))))))
2212 ((looking-at bibtex-string) 2359 ((bibtex-parse-string)
2213 (setq 2360 (let ((bounds (bibtex-parse-string)))
2214 label 2361 (setq
2215 (buffer-substring-no-properties 2362 reference-key
2216 (match-beginning bibtex-key-in-string) 2363 (buffer-substring-no-properties
2217 (match-end bibtex-key-in-string))))) 2364 (bibtex-start-of-reference-key-in-string bounds)
2365 (bibtex-end-of-reference-key-in-string bounds))))))
2218 (forward-char) 2366 (forward-char)
2219 (bibtex-skip-to-valid-entry) 2367 (bibtex-skip-to-valid-entry)
2220 (if (not (assoc label labels)) 2368 (if (not (assoc reference-key reference-keys))
2221 (setq labels 2369 (setq reference-keys
2222 (cons (list label) labels))) 2370 (cons (list reference-key) reference-keys)))
2223 (if (and label2 2371 (if (and xrefd-reference-key
2224 (not (assoc label2 labels))) 2372 (not (assoc xrefd-reference-key reference-keys)))
2225 (setq labels 2373 (setq reference-keys
2226 (cons (list label2) labels)))))) 2374 (cons (list xrefd-reference-key) reference-keys))))))
2227 ;; user has aborted by typing a key --> return nil 2375 ;; user has aborted by typing a key --> return nil
2228 nil 2376 nil
2229 ;; successful operation --> return t 2377 ;; successful operation --> return t
2230 (setq 2378 (setq
2231 bibtex-buffer-last-parsed-tick (buffer-modified-tick) 2379 bibtex-buffer-last-parsed-tick (buffer-modified-tick)
2232 bibtex-keys labels) 2380 bibtex-reference-keys reference-keys)
2233 (if verbose 2381 (if verbose
2234 (bibtex-progress-message 'done)) 2382 (bibtex-progress-message 'done))
2235 t))))) 2383 t)))))
2236 2384
2237 (defun bibtex-parse-buffers-stealthily () 2385 (defun bibtex-parse-buffers-stealthily ()
2271 (point))) 2419 (point)))
2272 (part-of-word (buffer-substring-no-properties beg end)) 2420 (part-of-word (buffer-substring-no-properties beg end))
2273 (completions (copy-sequence string-list)) 2421 (completions (copy-sequence string-list))
2274 (completion (save-excursion 2422 (completion (save-excursion
2275 (if complete-strings 2423 (if complete-strings
2276 (while (re-search-backward 2424 (while (bibtex-search-backward-string)
2277 bibtex-string nil t) 2425 (let ((bounds (bibtex-search-backward-string)))
2278 (setq completions 2426 (setq completions
2279 (cons 2427 (cons
2280 (list 2428 (list
2281 (buffer-substring-no-properties 2429 (buffer-substring-no-properties
2282 (match-beginning bibtex-key-in-string) 2430 (bibtex-start-of-reference-key-in-string bounds)
2283 (match-end bibtex-key-in-string))) 2431 (bibtex-end-of-reference-key-in-string bounds)))
2284 completions)))) 2432 completions)))))
2285 (setq completions 2433 (setq completions
2286 (sort completions 2434 (sort completions
2287 (lambda(x y) 2435 (lambda(x y)
2288 (string-lessp 2436 (string-lessp
2289 (car x) 2437 (car x)
2292 (cond ((eq completion t) 2440 (cond ((eq completion t)
2293 (if complete-strings 2441 (if complete-strings
2294 ;; remove double-quotes or braces if field is no concatenation 2442 ;; remove double-quotes or braces if field is no concatenation
2295 (save-excursion 2443 (save-excursion
2296 (bibtex-inside-field) 2444 (bibtex-inside-field)
2297 (bibtex-enclosing-field) 2445 (let* ((bounds (bibtex-enclosing-field)))
2298 (let ((end (match-end bibtex-text-in-field))) 2446 (goto-char (bibtex-start-of-text-in-field bounds))
2299 (goto-char (match-beginning bibtex-text-in-field)) 2447 (let ((boundaries (bibtex-parse-field-string)))
2300 (if (and 2448 (if (and boundaries (equal (cdr boundaries) (bibtex-end-of-text-in-field bounds)))
2301 (looking-at bibtex-field-string) 2449 (bibtex-remove-delimiters)))))))
2302 (equal (match-end 0) end))
2303 (bibtex-remove-delimiters))))))
2304 ((not completion) 2450 ((not completion)
2305 (error "Can't find completion for \"%s\"" part-of-word)) 2451 (error "Can't find completion for \"%s\"" part-of-word))
2306 ((not (string= part-of-word completion)) 2452 ((not (string= part-of-word completion))
2307 (delete-region beg end) 2453 (delete-region beg end)
2308 (insert completion) 2454 (insert completion)
2309 (if (and (assoc completion completions) 2455 (if (and (assoc completion completions)
2310 complete-strings) 2456 complete-strings)
2311 ;; remove double-quotes or braces if field is no concatenation 2457 ;; remove double-quotes or braces if field is no concatenation
2312 (save-excursion 2458 (save-excursion
2313 (bibtex-inside-field) 2459 (bibtex-inside-field)
2314 (bibtex-enclosing-field) 2460 (let* ((bounds (bibtex-enclosing-field)))
2315 (let ((end (match-end bibtex-text-in-field))) 2461 (goto-char (bibtex-start-of-text-in-field bounds))
2316 (goto-char (match-beginning bibtex-text-in-field)) 2462 (let ((boundaries (bibtex-parse-field-string)))
2317 (if (and 2463 (if (and boundaries (equal (cdr boundaries) (bibtex-end-of-text-in-field bounds)))
2318 (looking-at bibtex-field-string) 2464 (bibtex-remove-delimiters)))))))
2319 (equal (match-end 0) end))
2320 (bibtex-remove-delimiters))))))
2321 (t 2465 (t
2322 (message "Making completion list...") 2466 (message "Making completion list...")
2323 (let ((list (all-completions part-of-word completions))) 2467 (let ((list (all-completions part-of-word completions)))
2324 (with-output-to-temp-buffer "*Completions*" 2468 (with-output-to-temp-buffer "*Completions*"
2325 (display-completion-list list))) 2469 (display-completion-list list)))
2336 (let (bibtex-help-message) 2480 (let (bibtex-help-message)
2337 (bibtex-find-text nil)) 2481 (bibtex-find-text nil))
2338 (save-excursion 2482 (save-excursion
2339 ;; parse current field 2483 ;; parse current field
2340 (bibtex-inside-field) 2484 (bibtex-inside-field)
2341 (bibtex-enclosing-field) 2485 (let* ((case-fold-search t)
2342 (let ((case-fold-search t) 2486 (bounds (bibtex-enclosing-field))
2343 (start-old-text (match-beginning bibtex-text-in-field)) 2487 (start-old-text (bibtex-start-of-text-in-field bounds))
2344 (stop-old-text (match-end bibtex-text-in-field)) 2488 (stop-old-text (bibtex-end-of-text-in-field bounds))
2345 (start-name (match-beginning bibtex-name-in-field)) 2489 (start-name (bibtex-start-of-name-in-field bounds))
2346 (stop-name (match-end bibtex-name-in-field)) 2490 (stop-name (bibtex-end-of-name-in-field bounds))
2347 (new-text)) 2491 (new-text))
2348 (goto-char start-name) 2492 (goto-char start-name)
2349 ;; construct regexp for field with same name as this one, 2493 ;; construct regexp for field with same name as this one,
2350 ;; ignoring possible OPT's or ALT's 2494 ;; ignoring possible OPT's or ALT's
2351 (let ((matching-entry 2495 (let* ((field-name
2352 (bibtex-cfield
2353 (buffer-substring-no-properties 2496 (buffer-substring-no-properties
2354 (if (looking-at "OPT\\|ALT") 2497 (if (looking-at "\\(OPT\\)\\|\\(ALT\\)")
2355 (+ (point) (length "OPT")) 2498 (match-end 0)
2356 (point)) 2499 (point))
2357 stop-name) 2500 stop-name)))
2358 bibtex-field-text)))
2359 ;; if executed several times in a row, start each search where 2501 ;; if executed several times in a row, start each search where
2360 ;; the last one was finished 2502 ;; the last one was finished
2361 (cond ((eq last-command 'bibtex-pop) 2503 (cond ((eq last-command 'bibtex-pop) t)
2362 t
2363 )
2364 (t 2504 (t
2365 (bibtex-enclosing-reference-maybe-empty-head) 2505 (bibtex-enclosing-entry-maybe-empty-head)
2366 (setq 2506 (setq
2367 bibtex-pop-previous-search-point (match-beginning 0) 2507 bibtex-pop-previous-search-point (match-beginning 0)
2368 bibtex-pop-next-search-point (point)))) 2508 bibtex-pop-next-search-point (point))))
2369 (if (eq direction 'previous) 2509 (if (eq direction 'previous)
2370 (goto-char bibtex-pop-previous-search-point) 2510 (goto-char bibtex-pop-previous-search-point)
2371 (goto-char bibtex-pop-next-search-point)) 2511 (goto-char bibtex-pop-next-search-point))
2372 ;; Now search for arg'th previous/next similar field 2512 ;; Now search for arg'th previous/next similar field
2373 (cond 2513 (let ((bounds nil)
2374 ((if (eq direction 'previous) 2514 (failure nil))
2375 (re-search-backward matching-entry nil t arg) 2515 (while (and (not failure) (> arg 0))
2376 (re-search-forward matching-entry nil t arg)) 2516 (cond ((eq direction 'previous)
2377 ;; Found a matching field. Remember boundaries. 2517 (setq bounds (bibtex-search-backward-field field-name (point-min)))
2378 (setq bibtex-pop-previous-search-point (match-beginning 0)) 2518 (if bounds
2379 (setq bibtex-pop-next-search-point (match-end 0)) 2519 (goto-char (bibtex-start-of-field bounds))
2380 (setq new-text 2520 (setq failure t)))
2381 (buffer-substring-no-properties 2521 ((eq direction 'next)
2382 (match-beginning bibtex-text-in-field) 2522 (setq bounds (bibtex-search-forward-field field-name (point-max)))
2383 (match-end bibtex-text-in-field))) 2523 (if bounds
2384 ;; change delimiters, if any changes needed 2524 (goto-char (bibtex-end-of-field bounds))
2385 (let ((start 0) 2525 (setq failure t))))
2386 old-open 2526 (setq arg (- arg 1)))
2387 new-open 2527 (if failure
2388 old-close 2528 (error (if (eq direction 'previous)
2389 new-close) 2529 "No previous matching BibTeX field."
2390 (if (equal bibtex-field-delimiters 'braces) 2530 "No next matching BibTeX field."))
2391 (setq old-open ?\" 2531 ;; Found a matching field. Remember boundaries.
2392 new-open ?\{ 2532 (setq bibtex-pop-previous-search-point (bibtex-start-of-field bounds))
2393 old-close ?\" 2533 (setq bibtex-pop-next-search-point (bibtex-end-of-field bounds))
2394 new-close ?\}) 2534 (setq new-text
2395 (setq old-open ?\{ 2535 (buffer-substring-no-properties
2396 new-open ?\" 2536 (bibtex-start-of-text-in-field bounds)
2397 old-close ?\} 2537 (bibtex-end-of-text-in-field bounds)))
2398 new-close ?\")) 2538 (bibtex-flash-head)
2399 (while (string-match bibtex-field-string new-text start) 2539 ;; Go back to where we started, delete old text, and pop new.
2400 (let ((beg (match-beginning 0)) 2540 (goto-char stop-old-text)
2401 (end (1- (match-end 0)))) 2541 (delete-region start-old-text stop-old-text)
2402 (if (and 2542 (insert new-text))))))
2403 (eq (aref new-text beg) old-open)
2404 (eq (aref new-text end) old-close))
2405 (progn
2406 (aset new-text beg new-open)
2407 (aset new-text end new-close))))
2408 (setq start (match-end 0))))
2409 (bibtex-flash-head)
2410 ;; Go back to where we started, delete old text, and pop new.
2411 (goto-char stop-old-text)
2412 (delete-region start-old-text stop-old-text)
2413 (insert new-text))
2414 (t
2415 ;; search failed
2416 (error (concat "No "
2417 (if (eq direction 'previous)
2418 "previous"
2419 "next")
2420 " matching BibTeX field.")))))))
2421 (let (bibtex-help-message) 2543 (let (bibtex-help-message)
2422 (bibtex-find-text nil)) 2544 (bibtex-find-text nil))
2423 (setq this-command 'bibtex-pop)) 2545 (setq this-command 'bibtex-pop))
2424 2546
2425 2547
2535 (lambda (filename) 2657 (lambda (filename)
2536 ;; collect pathnames 2658 ;; collect pathnames
2537 (let* ((path (if bibtex-string-file-path 2659 (let* ((path (if bibtex-string-file-path
2538 bibtex-string-file-path 2660 bibtex-string-file-path
2539 ".")) 2661 "."))
2540 (dirs 2662 (dirs (split-string path ":+"))
2541 (mapcar
2542 (lambda (dirname) ;; strips off trailing slashes
2543 (let ((len (length dirname)))
2544 (if (equal (elt dirname (1- len)) "/")
2545 (substring dirname 0 (1- (1- len)))
2546 dirname)))
2547 (let (actdirs)
2548 (while (string-match ":" path)
2549 (setq actdirs
2550 (append actdirs
2551 (list (substring path 0 (1- (match-end 0)))))
2552 path (substring path (match-end 0))))
2553 (append actdirs (list path)))))
2554 (filename (if (string-match "\.bib$" filename) 2663 (filename (if (string-match "\.bib$" filename)
2555 filename 2664 filename
2556 (concat filename ".bib"))) 2665 (concat filename ".bib")))
2557 fullfilename 2666 fullfilename
2558 (item 0) 2667 (item 0)
2564 (setq fullfilename 2673 (setq fullfilename
2565 (concat (elt dirs item) "/" filename))))) 2674 (concat (elt dirs item) "/" filename)))))
2566 (setq item (1+ item))) 2675 (setq item (1+ item)))
2567 (if (< item size) 2676 (if (< item size)
2568 ;; file was found 2677 ;; file was found
2569 (let ((case-fold-search t) 2678 (let* ((case-fold-search t)
2570 (compl nil)) 2679 (compl nil))
2571 (with-temp-buffer 2680 (with-temp-buffer
2572 (insert-file-contents fullfilename) 2681 (insert-file-contents fullfilename)
2573 (goto-char (point-min)) 2682 (goto-char (point-min))
2574 (while (re-search-forward bibtex-string nil t) 2683 (let ((bounds (bibtex-search-forward-string)))
2575 (setq compl 2684 (while bounds
2576 (cons (list (match-string-no-properties 2685 (setq compl
2577 bibtex-key-in-string)) 2686 (cons (list (buffer-substring-no-properties
2578 compl)))) 2687 (bibtex-start-of-reference-key-in-string bounds)
2579 (setq bibtex-strings (append bibtex-strings (nreverse compl)))) 2688 (bibtex-end-of-reference-key-in-string bounds)))
2689 compl))
2690 (goto-char (bibtex-end-of-string bounds))
2691 (setq bounds (bibtex-search-forward-string)))))
2692 (setq bibtex-strings (append bibtex-strings (nreverse compl))))
2580 (error 2693 (error
2581 "File %s not in paths defined by bibtex-string-file-path variable" 2694 "File %s not in paths defined by bibtex-string-file-path variable"
2582 filename)))) 2695 filename))))
2583 bibtex-string-files) 2696 bibtex-string-files)
2584 (if bibtex-maintain-sorted-entries 2697 (if bibtex-maintain-sorted-entries
2622 (setq font-lock-mark-block-function 2735 (setq font-lock-mark-block-function
2623 (lambda () 2736 (lambda ()
2624 (set-mark (bibtex-end-of-entry)) 2737 (set-mark (bibtex-end-of-entry))
2625 (bibtex-beginning-of-entry))) 2738 (bibtex-beginning-of-entry)))
2626 (setq imenu-generic-expression 2739 (setq imenu-generic-expression
2627 (list (list nil bibtex-reference-head bibtex-key-in-head))) 2740 (list (list nil bibtex-entry-head bibtex-key-in-head)))
2628 (setq imenu-case-fold-search t) 2741 (setq imenu-case-fold-search t)
2629 ;; XEmacs needs easy-menu-add, Emacs does not care 2742 ;; XEmacs needs easy-menu-add, Emacs does not care
2630 (easy-menu-add bibtex-edit-menu) 2743 (easy-menu-add bibtex-edit-menu)
2631 (easy-menu-add bibtex-entry-menu) 2744 (easy-menu-add bibtex-entry-menu)
2632 (run-hooks 'bibtex-mode-hook)) 2745 (run-hooks 'bibtex-mode-hook))
2638 (progn 2751 (progn
2639 (require 'reporter) 2752 (require 'reporter)
2640 (let ((reporter-prompt-for-summary-p t)) 2753 (let ((reporter-prompt-for-summary-p t))
2641 (reporter-submit-bug-report 2754 (reporter-submit-bug-report
2642 bibtex-maintainer-address 2755 bibtex-maintainer-address
2643 (concat "bibtex.el " bibtex-version) 2756 "bibtex.el"
2644 (list 2757 (list
2645 'system-configuration 2758 'system-configuration
2646 'system-configuration-options 2759 'system-configuration-options
2647 'bibtex-mode-hook 2760 'bibtex-mode-hook
2648 'bibtex-parse-keys-timeout 2761 'bibtex-parse-keys-timeout
2693 'bibtex-autokey-titleword-separator 2806 'bibtex-autokey-titleword-separator
2694 'bibtex-autokey-name-year-separator 2807 'bibtex-autokey-name-year-separator
2695 'bibtex-autokey-year-title-separator 2808 'bibtex-autokey-year-title-separator
2696 'bibtex-autokey-edit-before-use 2809 'bibtex-autokey-edit-before-use
2697 'bibtex-autokey-before-presentation-function 2810 'bibtex-autokey-before-presentation-function
2698 ;; possible bugs regarding automatic labels 2811 ;; possible bugs regarding automatic reference keys
2699 'bibtex-entry-field-alist 2812 'bibtex-entry-field-alist
2700 ;; possible format error 2813 ;; possible format error
2701 'bibtex-help-message 2814 'bibtex-help-message
2702 'bibtex-include-OPTcrossref 2815 'bibtex-include-OPTcrossref
2703 'bibtex-include-OPTkey 2816 'bibtex-include-OPTkey
2706 ;; user variables which shouldn't cause any errors 2819 ;; user variables which shouldn't cause any errors
2707 ) 2820 )
2708 nil nil 2821 nil nil
2709 (concat bibtex-maintainer-salutation " 2822 (concat bibtex-maintainer-salutation "
2710 2823
2711 I want to report a bug on Emacs BibTeX mode " bibtex-version ". 2824 I want to report a bug on Emacs BibTeX mode.
2825
2712 I've read the `Bugs' section in the `Emacs' info page, so I know how 2826 I've read the `Bugs' section in the `Emacs' info page, so I know how
2713 to make a clear and unambiguous report. I have started a fresh Emacs 2827 to make a clear and unambiguous report. I have started a fresh Emacs
2714 via `"invocation-name " --no-init-file --no-site-file', thereafter (in 2828 via `"invocation-name " --no-init-file --no-site-file', thereafter (in
2715 case I'm reporting on a version of `bibtex.el' which is not part of 2829 case I'm reporting on a version of `bibtex.el' which is not part of
2716 the standard emacs distribution) I loaded the questionable version 2830 the standard emacs distribution) I loaded the questionable version
2725 (e-t (completing-read 2839 (e-t (completing-read
2726 "Entry Type: " 2840 "Entry Type: "
2727 bibtex-entry-field-alist 2841 bibtex-entry-field-alist
2728 nil t nil 'bibtex-entry-type-history))) 2842 nil t nil 'bibtex-entry-type-history)))
2729 (list e-t))) 2843 (list e-t)))
2730 (if (not bibtex-keys) 2844 (if (not bibtex-reference-keys)
2731 (bibtex-parse-keys nil t)) 2845 (bibtex-parse-keys nil t))
2732 (let* (required 2846 (let* (required
2733 optional 2847 optional
2734 (key 2848 (key
2735 (if bibtex-maintain-sorted-entries 2849 (if bibtex-maintain-sorted-entries
2736 (completing-read 2850 (completing-read
2737 (format "%s key: " entry-type) 2851 (format "%s key: " entry-type)
2738 bibtex-keys nil nil nil 'bibtex-key-history))) 2852 bibtex-reference-keys nil nil nil 'bibtex-key-history)))
2739 (e (assoc-ignore-case entry-type bibtex-entry-field-alist)) 2853 (e (assoc-ignore-case entry-type bibtex-entry-field-alist))
2740 (r-n-o (elt e 1)) 2854 (r-n-o (elt e 1))
2741 (c-ref (elt e 2))) 2855 (c-ref (elt e 2)))
2742 (if (not e) 2856 (if (not e)
2743 (error "Bibtex entry type %s not defined" entry-type)) 2857 (error "Bibtex entry type %s not defined" entry-type))
2780 "Prints helpful information about current field in current BibTeX entry." 2894 "Prints helpful information about current field in current BibTeX entry."
2781 (interactive) 2895 (interactive)
2782 (let* ((case-fold-search t) 2896 (let* ((case-fold-search t)
2783 (pnt (point)) 2897 (pnt (point))
2784 (field-name 2898 (field-name
2785 (progn 2899 (let* ((bounds (bibtex-enclosing-field))
2786 (condition-case errname 2900 (mb (bibtex-start-of-name-in-field bounds))
2787 (bibtex-enclosing-field) 2901 (me (bibtex-end-of-name-in-field bounds)))
2788 (search-failed 2902 (goto-char mb)
2789 (goto-char pnt) 2903 (buffer-substring-no-properties
2790 (error "Not on BibTeX field"))) 2904 (if (looking-at "OPT\\|ALT")
2791 (let ((mb (match-beginning bibtex-name-in-field)) 2905 (+ 3 mb)
2792 (me (match-end bibtex-name-in-field))) 2906 mb)
2793 (goto-char mb) 2907 me)))
2794 (buffer-substring-no-properties 2908 (entry-type
2795 (if (looking-at "OPT\\|ALT")
2796 (+ 3 mb)
2797 mb)
2798 me))))
2799 (reference-type
2800 (progn 2909 (progn
2801 (re-search-backward 2910 (re-search-backward
2802 bibtex-reference-maybe-empty-head nil t) 2911 bibtex-entry-maybe-empty-head nil t)
2803 (buffer-substring-no-properties 2912 (buffer-substring-no-properties
2804 (1+ (match-beginning bibtex-type-in-head)) 2913 (1+ (match-beginning bibtex-type-in-head))
2805 (match-end bibtex-type-in-head)))) 2914 (match-end bibtex-type-in-head))))
2806 (entry-list 2915 (entry-list
2807 (assoc-ignore-case reference-type 2916 (assoc-ignore-case entry-type
2808 bibtex-entry-field-alist)) 2917 bibtex-entry-field-alist))
2809 (c-r-list (elt entry-list 2)) 2918 (c-r-list (elt entry-list 2))
2810 (req-opt-list 2919 (req-opt-list
2811 (if (and 2920 (if (and
2812 (member reference-type bibtex-include-OPTcrossref) 2921 (member entry-type bibtex-include-OPTcrossref)
2813 c-r-list) 2922 c-r-list)
2814 c-r-list 2923 c-r-list
2815 (elt entry-list 1))) 2924 (elt entry-list 1)))
2816 (list-of-entries (append 2925 (list-of-entries (append
2817 (elt req-opt-list 0) 2926 (elt req-opt-list 0)
2818 (elt req-opt-list 1) 2927 (elt req-opt-list 1)
2819 bibtex-user-optional-fields 2928 bibtex-user-optional-fields
2820 (if (member 2929 (if (member
2821 reference-type 2930 entry-type
2822 bibtex-include-OPTcrossref) 2931 bibtex-include-OPTcrossref)
2823 '(("crossref" 2932 '(("crossref"
2824 "Label of the crossreferenced entry"))) 2933 "Reference key of the crossreferenced entry")))
2825 (if bibtex-include-OPTkey 2934 (if bibtex-include-OPTkey
2826 '(("key" 2935 '(("key"
2827 "Key used for label creation if author and editor fields are missing")))))) 2936 "Used for reference key creation if author and editor fields are missing"))))))
2828 (goto-char pnt) 2937 (goto-char pnt)
2829 (let ((comment (assoc-ignore-case field-name list-of-entries))) 2938 (let ((comment (assoc-ignore-case field-name list-of-entries)))
2830 (if comment 2939 (if comment
2831 (message (elt comment 1)) 2940 (message (elt comment 1))
2832 (message "NO COMMENT AVAILABLE"))))) 2941 (message "NO COMMENT AVAILABLE")))))
2838 using the minibuffer." 2947 using the minibuffer."
2839 (interactive "*P") 2948 (interactive "*P")
2840 (if (not e-t) 2949 (if (not e-t)
2841 (setq 2950 (setq
2842 e-t 2951 e-t
2843 (let* ((reference-type 2952 (let* ((entry-type
2844 (save-excursion 2953 (save-excursion
2845 (bibtex-enclosing-reference-maybe-empty-head) 2954 (bibtex-enclosing-entry-maybe-empty-head)
2846 (buffer-substring-no-properties 2955 (buffer-substring-no-properties
2847 (1+ (match-beginning bibtex-type-in-head)) 2956 (1+ (match-beginning bibtex-type-in-head))
2848 (match-end bibtex-type-in-head)))) 2957 (match-end bibtex-type-in-head))))
2849 (fl 2958 (fl
2850 (car (cdr (assoc-ignore-case 2959 (car (cdr (assoc-ignore-case
2851 reference-type bibtex-entry-field-alist)))) 2960 entry-type bibtex-entry-field-alist))))
2852 (field-list 2961 (field-list
2853 (append 2962 (append
2854 (elt fl 0) (elt fl 1) bibtex-user-optional-fields 2963 (elt fl 0) (elt fl 1) bibtex-user-optional-fields
2855 (if bibtex-include-OPTcrossref '(("crossref" nil))) 2964 (if bibtex-include-OPTcrossref '(("crossref" nil)))
2856 (if bibtex-include-OPTkey '(("key" nil))))) 2965 (if bibtex-include-OPTkey '(("key" nil)))))
2915 "Move to end of BibTeX entry. 3024 "Move to end of BibTeX entry.
2916 If inside an entry, move to the end of it, otherwise move to the end 3025 If inside an entry, move to the end of it, otherwise move to the end
2917 of the previous entry. 3026 of the previous entry.
2918 If called from a program, this function returns the new location of point." 3027 If called from a program, this function returns the new location of point."
2919 (interactive) 3028 (interactive)
2920 (let ((case-fold-search t) 3029 (let* ((case-fold-search t)
2921 (valid-entry-head 3030 (valid-entry-head
2922 (concat "[ \t]*@[ \t]*\\(" 3031 (concat "[ \t]*@[ \t]*\\("
2923 (mapconcat 3032 (mapconcat
2924 (lambda (type) 3033 (lambda (type)
2925 (concat "\\(" (car type) "\\)")) 3034 (concat "\\(" (car type) "\\)"))
2926 bibtex-entry-field-alist 3035 bibtex-entry-field-alist
2927 "\\|") 3036 "\\|")
2928 "\\)")) 3037 "\\)"))
2929 (org (point)) 3038 (org (point))
2930 (pnt (bibtex-beginning-of-entry)) 3039 (pnt (bibtex-beginning-of-entry))
2931 err) 3040 err)
2932 (cond 3041 (cond
2933 ((looking-at "[ \t]*@[ \t]*string[ \t\n]*[({]") 3042 ((bibtex-parse-string)
2934 (if (not (and 3043 (let ((bounds (bibtex-parse-string)))
2935 (re-search-forward bibtex-string nil t) 3044 (goto-char (bibtex-end-of-string bounds))))
2936 (equal (match-beginning 0) pnt)))
2937 (setq err t)))
2938 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*") 3045 ((looking-at "[ \t]*@[ \t]*preamble[ \t\n]*")
2939 (goto-char (match-end 0)) 3046 (goto-char (match-end 0))
2940 (cond 3047 (cond
2941 ((looking-at "(") 3048 ((looking-at "(")
2942 (if (not (re-search-forward ")[ \t]*\n\n" nil 'move)) 3049 (if (not (re-search-forward ")[ \t]*\n\n" nil 'move))
2949 (if (not err) 3056 (if (not err)
2950 (progn 3057 (progn
2951 (goto-char (match-beginning 0)) 3058 (goto-char (match-beginning 0))
2952 (forward-char)))) 3059 (forward-char))))
2953 ((looking-at valid-entry-head) 3060 ((looking-at valid-entry-head)
2954 (bibtex-search-reference t nil t) 3061 (bibtex-search-entry t nil t)
2955 (if (not (equal (match-beginning 0) pnt)) 3062 (if (not (equal (match-beginning 0) pnt))
2956 (setq err t))) 3063 (setq err t)))
2957 (t 3064 (t
2958 (if (interactive-p) 3065 (if (interactive-p)
2959 (message "Not on a known BibTeX entry.")) 3066 (message "Not on a known BibTeX entry."))
3012 (ispell-region (bibtex-beginning-of-entry) (bibtex-end-of-entry))) 3119 (ispell-region (bibtex-beginning-of-entry) (bibtex-end-of-entry)))
3013 3120
3014 (defun bibtex-ispell-abstract () 3121 (defun bibtex-ispell-abstract ()
3015 "Spell abstract of BibTeX entry." 3122 "Spell abstract of BibTeX entry."
3016 (interactive) 3123 (interactive)
3017 (let ((case-fold-search t) 3124 (let* ((case-fold-search t)
3018 (pnt (bibtex-end-of-entry))) 3125 (pnt (bibtex-end-of-entry)))
3019 (bibtex-beginning-of-entry) 3126 (bibtex-beginning-of-entry)
3020 (if (not 3127 (let ((bounds (bibtex-search-forward-field "abstract" pnt)))
3021 (re-search-forward 3128 (if bounds
3022 (bibtex-cfield "abstract" bibtex-field-text) pnt t)) 3129 (ispell-region (bibtex-start-of-text-in-field bounds) (bibtex-end-of-text-in-field bounds))
3023 (error "No abstract in entry"))) 3130 (error "No abstract in entry")))))
3024 (ispell-region (match-beginning bibtex-text-in-cfield)
3025 (match-end bibtex-text-in-cfield)))
3026 3131
3027 (defun bibtex-narrow-to-entry () 3132 (defun bibtex-narrow-to-entry ()
3028 "Narrow buffer to current BibTeX entry." 3133 "Narrow buffer to current BibTeX entry."
3029 (interactive) 3134 (interactive)
3030 (save-excursion 3135 (save-excursion
3050 'bibtex-skip-to-valid-entry 3155 'bibtex-skip-to-valid-entry
3051 ;; ENDREC function 3156 ;; ENDREC function
3052 'bibtex-end-of-entry 3157 'bibtex-end-of-entry
3053 ;; STARTKEY function 3158 ;; STARTKEY function
3054 (lambda () 3159 (lambda ()
3055 (let ((case-fold-search t)) 3160 (let* ((case-fold-search t))
3056 (re-search-forward bibtex-reference-head) 3161 (re-search-forward bibtex-entry-head)
3057 (buffer-substring-no-properties 3162 (buffer-substring-no-properties
3058 (match-beginning bibtex-key-in-head) 3163 (match-beginning bibtex-key-in-head)
3059 (match-end bibtex-key-in-head))))))) 3164 (match-end bibtex-key-in-head)))))))
3060 3165
3061 (defun bibtex-find-entry-location (entry-name &optional ignore-dups) 3166 (defun bibtex-find-entry-location (entry-name &optional ignore-dups)
3089 (setq actual-point (/ (+ left right) 2)) 3194 (setq actual-point (/ (+ left right) 2))
3090 (goto-char actual-point) 3195 (goto-char actual-point)
3091 (bibtex-skip-to-valid-entry t) 3196 (bibtex-skip-to-valid-entry t)
3092 (setq actual-key 3197 (setq actual-key
3093 (progn 3198 (progn
3094 (re-search-forward bibtex-reference-head) 3199 (re-search-forward bibtex-entry-head)
3095 (buffer-substring-no-properties 3200 (buffer-substring-no-properties
3096 (match-beginning bibtex-key-in-head) 3201 (match-beginning bibtex-key-in-head)
3097 (match-end bibtex-key-in-head)))) 3202 (match-end bibtex-key-in-head))))
3098 (cond 3203 (cond
3099 ((string-lessp entry-name actual-key) 3204 ((string-lessp entry-name actual-key)
3117 (progn 3222 (progn
3118 (bibtex-beginning-of-entry) 3223 (bibtex-beginning-of-entry)
3119 nil) 3224 nil)
3120 (goto-char right) 3225 (goto-char right)
3121 (setq actual-key 3226 (setq actual-key
3122 (if (looking-at bibtex-reference-head) 3227 (if (looking-at bibtex-entry-head)
3123 (buffer-substring-no-properties 3228 (buffer-substring-no-properties
3124 (match-beginning bibtex-key-in-reference) 3229 (match-beginning bibtex-key-in-entry)
3125 (match-end bibtex-key-in-reference)))) 3230 (match-end bibtex-key-in-entry))))
3126 (if (or 3231 (if (or
3127 (not actual-key) 3232 (not actual-key)
3128 (string-lessp actual-key entry-name)) 3233 (string-lessp actual-key entry-name))
3129 ;; buffer contains no valid entries or 3234 ;; buffer contains no valid entries or
3130 ;; greater than last entry --> append 3235 ;; greater than last entry --> append
3136 (goto-char right)) 3241 (goto-char right))
3137 t))) 3242 t)))
3138 3243
3139 (defun bibtex-validate (&optional test-thoroughly) 3244 (defun bibtex-validate (&optional test-thoroughly)
3140 "Validate if buffer or region is syntactically correct. 3245 "Validate if buffer or region is syntactically correct.
3141 Only known reference types are checked, so you can put comments 3246 Only known entry types are checked, so you can put comments
3142 outside of entries. 3247 outside of entries.
3143 With optional argument TEST-THOROUGHLY non-nil it checks for absence of 3248 With optional argument TEST-THOROUGHLY non-nil it checks for absence of
3144 required fields and questionable month fields as well. 3249 required fields and questionable month fields as well.
3145 If mark is active, it validates current region, if not whole buffer. 3250 If mark is active, it validates current region, if not whole buffer.
3146 Returns t if test was successful, nil otherwise." 3251 Returns t if test was successful, nil otherwise."
3147 (interactive "P") 3252 (interactive "P")
3148 (let (error-list 3253 (let* (error-list
3149 syntax-error 3254 syntax-error
3150 (case-fold-search t) 3255 (case-fold-search t)
3151 (valid-bibtex-entry 3256 (valid-bibtex-entry
3152 (concat 3257 (concat
3153 "@[ \t]*\\(\\(string\\)\\|" 3258 "@[ \t]*\\(\\(string\\)\\|"
3154 (mapconcat 3259 (mapconcat
3155 (lambda (type) 3260 (lambda (type)
3156 (concat "\\(" (car type) "\\)")) 3261 (concat "\\(" (car type) "\\)"))
3157 bibtex-entry-field-alist 3262 bibtex-entry-field-alist
3158 "\\|") 3263 "\\|")
3159 "\\)")) 3264 "\\)"))
3160 (pnt (point)) 3265 (pnt (point))
3161 (start-point 3266 (start-point
3162 (if (bibtex-mark-active) 3267 (if (bibtex-mark-active)
3163 (region-beginning) 3268 (region-beginning)
3164 (bibtex-beginning-of-first-entry))) 3269 (bibtex-beginning-of-first-entry)))
3165 (end-point 3270 (end-point
3166 (if (bibtex-mark-active) 3271 (if (bibtex-mark-active)
3167 (region-end) 3272 (region-end)
3168 (point-max)))) 3273 (point-max))))
3169 (save-restriction 3274 (save-restriction
3170 (narrow-to-region start-point end-point) 3275 (narrow-to-region start-point end-point)
3171 ;; looking if entries fit syntactical structure 3276 ;; looking if entries fit syntactical structure
3172 (goto-char start-point) 3277 (goto-char start-point)
3173 (bibtex-progress-message "Checking syntactical structure") 3278 (bibtex-progress-message "Checking syntactical structure")
3240 (let* ((beg (bibtex-beginning-of-entry)) 3345 (let* ((beg (bibtex-beginning-of-entry))
3241 (end (bibtex-end-of-entry)) 3346 (end (bibtex-end-of-entry))
3242 (entry-list 3347 (entry-list
3243 (progn 3348 (progn
3244 (goto-char beg) 3349 (goto-char beg)
3245 (bibtex-search-reference nil end) 3350 (bibtex-search-entry nil end)
3246 (assoc-ignore-case 3351 (assoc-ignore-case
3247 (buffer-substring-no-properties 3352 (buffer-substring-no-properties
3248 (1+ (match-beginning bibtex-type-in-head)) 3353 (1+ (match-beginning bibtex-type-in-head))
3249 (match-end bibtex-type-in-head)) 3354 (match-end bibtex-type-in-head))
3250 bibtex-entry-field-alist))) 3355 bibtex-entry-field-alist)))
3251 (req (copy-sequence (elt (elt entry-list 1) 0))) 3356 (req (copy-sequence (elt (elt entry-list 1) 0)))
3252 (creq (copy-sequence (elt (elt entry-list 2) 0))) 3357 (creq (copy-sequence (elt (elt entry-list 2) 0)))
3253 crossref-there) 3358 crossref-there)
3254 (goto-char beg) 3359 (goto-char beg)
3255 (while (re-search-forward bibtex-field end t) 3360 (let ((bounds (bibtex-search-forward-field bibtex-field-name end)))
3256 (let ((field-name 3361 (while bounds
3257 (buffer-substring-no-properties 3362 (let ((field-name
3258 (match-beginning bibtex-name-in-field) 3363 (buffer-substring-no-properties
3259 (match-end bibtex-name-in-field)))) 3364 (bibtex-start-of-name-in-field bounds)
3260 (if (and (equal (downcase field-name) "month") 3365 (bibtex-end-of-name-in-field bounds))))
3261 (string-match 3366 (if (and (equal (downcase field-name) "month")
3262 questionable-month 3367 (string-match
3263 (buffer-substring-no-properties 3368 questionable-month
3264 (match-beginning bibtex-text-in-field) 3369 (buffer-substring-no-properties
3265 (match-end bibtex-text-in-field)))) 3370 (bibtex-start-of-text-in-field bounds)
3266 (setq 3371 (bibtex-end-of-text-in-field bounds))))
3267 error-list 3372 (setq
3268 (cons 3373 error-list
3269 (list 3374 (cons
3270 (bibtex-current-line) 3375 (list
3271 "Questionable month field (delimited string)") 3376 (bibtex-current-line)
3272 error-list))) 3377 "Questionable month field (delimited string)")
3273 (setq 3378 error-list)))
3274 req 3379 (setq
3275 (delete (assoc-ignore-case field-name req) req) 3380 req
3276 creq 3381 (delete (assoc-ignore-case field-name req) req)
3277 (delete (assoc-ignore-case field-name creq) creq)) 3382 creq
3278 (if (equal (downcase field-name) "crossref") 3383 (delete (assoc-ignore-case field-name creq) creq))
3279 (setq crossref-there t)))) 3384 (if (equal (downcase field-name) "crossref")
3385 (setq crossref-there t)))
3386 (goto-char (bibtex-end-of-field bounds))
3387 (setq bounds (bibtex-search-forward-field bibtex-field-name end))))
3280 (if crossref-there 3388 (if crossref-there
3281 (setq req creq)) 3389 (setq req creq))
3282 (if (or (> (length req) 1) 3390 (if (or (> (length req) 1)
3283 (and (= (length req) 1) 3391 (and (= (length req) 1)
3284 (not (elt (car req) 3)))) 3392 (not (elt (car req) 3))))
3349 "Finds end of text of next BibTeX field; with ARG, to its beginning." 3457 "Finds end of text of next BibTeX field; with ARG, to its beginning."
3350 (interactive "P") 3458 (interactive "P")
3351 (bibtex-inside-field) 3459 (bibtex-inside-field)
3352 (let ((start (point))) 3460 (let ((start (point)))
3353 (condition-case () 3461 (condition-case ()
3354 (progn 3462 (let ((bounds (bibtex-enclosing-field)))
3355 (bibtex-enclosing-field) 3463 (goto-char (bibtex-end-of-field bounds))
3356 (goto-char (match-end 0))
3357 (forward-char 2)) 3464 (forward-char 2))
3358 (error 3465 (error
3359 (goto-char start) 3466 (goto-char start)
3360 (end-of-line) 3467 (end-of-line)
3361 (forward-char)))) 3468 (forward-char))))
3363 3470
3364 (defun bibtex-find-text (arg &optional as-if-interactive silent) 3471 (defun bibtex-find-text (arg &optional as-if-interactive silent)
3365 "Go to end of text of current field; with ARG, go to beginning." 3472 "Go to end of text of current field; with ARG, go to beginning."
3366 (interactive "P") 3473 (interactive "P")
3367 (bibtex-inside-field) 3474 (bibtex-inside-field)
3368 (if (bibtex-enclosing-field (or (interactive-p) as-if-interactive)) 3475 (let ((bounds (bibtex-enclosing-field (or (interactive-p) as-if-interactive))))
3369 (progn 3476 (if bounds
3370 (if arg 3477 (progn
3371 (progn 3478 (if arg
3372 (goto-char (match-beginning bibtex-text-in-field)) 3479 (progn
3373 (if (looking-at "[{\"]") 3480 (goto-char (bibtex-start-of-text-in-field bounds))
3374 (forward-char))) 3481 (if (looking-at "[{\"]")
3375 (goto-char (match-end bibtex-text-in-field)) 3482 (forward-char)))
3376 (if (or 3483 (goto-char (bibtex-end-of-text-in-field bounds))
3377 (= (preceding-char) ?}) 3484 (if (or
3378 (= (preceding-char) ?\")) 3485 (= (preceding-char) ?})
3379 (forward-char -1))) 3486 (= (preceding-char) ?\"))
3380 (if bibtex-help-message 3487 (forward-char -1)))
3381 (bibtex-print-help-message))) 3488 (if bibtex-help-message
3382 (beginning-of-line) 3489 (bibtex-print-help-message)))
3383 (cond 3490 (beginning-of-line)
3384 ((looking-at bibtex-string) 3491 (cond
3385 (goto-char 3492 ((bibtex-parse-string)
3386 (if arg 3493 (let ((bounds (bibtex-parse-string)))
3387 (match-beginning bibtex-text-in-string) 3494 (goto-char
3388 (match-end bibtex-text-in-string)))) 3495 (if arg
3389 ((looking-at bibtex-reference-maybe-empty-head) 3496 (bibtex-start-of-text-in-string bounds)
3390 (goto-char 3497 (bibtex-end-of-text-in-string bounds)))))
3391 (if arg 3498 ((looking-at bibtex-entry-maybe-empty-head)
3392 (match-beginning bibtex-key-in-head) 3499 (goto-char
3393 (match-end 0)))) 3500 (if arg
3394 (t 3501 (match-beginning bibtex-key-in-head)
3395 (if (not silent) 3502 (match-end 0))))
3396 (error "Not on BibTeX field")))))) 3503 (t
3504 (if (not silent)
3505 (error "Not on BibTeX field")))))))
3397 3506
3398 (defun bibtex-remove-OPT-or-ALT () 3507 (defun bibtex-remove-OPT-or-ALT ()
3399 "Removes the string starting optional/alternative fields. 3508 "Removes the string starting optional/alternative fields.
3400 Aligns text and goes thereafter to end of text." 3509 Aligns text and goes thereafter to end of text."
3401 (interactive) 3510 (interactive)
3402 (let ((case-fold-search t)) 3511 (bibtex-inside-field)
3403 (bibtex-inside-field) 3512 (let* ((case-fold-search t)
3404 (bibtex-enclosing-field) 3513 (bounds (bibtex-enclosing-field)))
3405 (save-excursion 3514 (save-excursion
3406 (goto-char (match-beginning bibtex-name-in-field)) 3515 (goto-char (bibtex-start-of-name-in-field bounds))
3407 (if (looking-at "OPT\\|ALT") 3516 (if (looking-at "OPT\\|ALT")
3408 (progn 3517 (progn
3409 (delete-char (length "OPT")) 3518 (delete-char (length "OPT"))
3410 ;; make field non-OPT 3519 ;; make field non-OPT
3411 (search-forward "=") 3520 (search-forward "=")
3422 (bibtex-inside-field))) 3531 (bibtex-inside-field)))
3423 3532
3424 (defun bibtex-remove-delimiters () 3533 (defun bibtex-remove-delimiters ()
3425 "Removes \"\" or {} around string." 3534 "Removes \"\" or {} around string."
3426 (interactive) 3535 (interactive)
3427 (let ((case-fold-search t)) 3536 (save-excursion
3428 (save-excursion 3537 (bibtex-inside-field)
3429 (bibtex-inside-field) 3538 (let* ((case-fold-search t)
3430 (bibtex-enclosing-field) 3539 (bounds (bibtex-enclosing-field))
3431 (let ((start (match-beginning bibtex-text-in-field)) 3540 (stop (copy-marker (bibtex-end-of-text-in-field bounds))))
3432 (stop (match-end bibtex-text-in-field))) 3541 (goto-char (bibtex-start-of-text-in-field bounds))
3433 (goto-char start) 3542 (let* ((boundaries (bibtex-search-forward-field-string stop)))
3434 (while (re-search-forward bibtex-field-string stop t) 3543 (while boundaries
3435 (let ((beg (copy-marker (match-beginning 0))) 3544 (let ((text-end (copy-marker (cdr boundaries))))
3436 (end (copy-marker (match-end 0)))) 3545 (goto-char (car boundaries))
3437 (goto-char beg) 3546 (delete-char 1)
3438 (if (looking-at "[{\"]") 3547 (goto-char text-end)
3439 (delete-char 1)) 3548 (delete-backward-char 1)
3440 (goto-char end) 3549 (setq boundaries (bibtex-search-forward-field-string stop))))))))
3441 (forward-char -1)
3442 (if (looking-at "[}\"]")
3443 (delete-char 1))))))))
3444 3550
3445 (defun bibtex-kill-field (&optional copy-only) 3551 (defun bibtex-kill-field (&optional copy-only)
3446 "Kills the entire enclosing BibTeX field. 3552 "Kills the entire enclosing BibTeX field.
3447 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring,' 3553 With prefix arg COPY-ONLY, copy the current field to `bibtex-field-kill-ring,'
3448 but do not actually kill it." 3554 but do not actually kill it."
3449 (interactive "P") 3555 (interactive "P")
3450 (let ((pnt (point)) 3556 (let* ((pnt (point))
3451 (case-fold-search t)) 3557 (case-fold-search t))
3452 (bibtex-inside-field) 3558 (bibtex-inside-field)
3453 (bibtex-enclosing-field) 3559 (let* ((bounds (bibtex-enclosing-field))
3454 (let ((the-end (match-end 0)) 3560 (the-end (bibtex-end-of-field bounds))
3455 (the-beginning (match-beginning 0))) 3561 (the-beginning (bibtex-start-of-field bounds)))
3456 (goto-char the-end) 3562 (goto-char the-end)
3457 (skip-chars-forward " \t\n,") 3563 (skip-chars-forward " \t\n,")
3458 (setq 3564 (setq
3459 bibtex-field-kill-ring 3565 bibtex-field-kill-ring
3460 (cons 3566 (cons
3461 (list 3567 (list
3462 'field 3568 'field
3463 (buffer-substring-no-properties 3569 (buffer-substring-no-properties
3464 (match-beginning bibtex-name-in-field) 3570 (bibtex-start-of-name-in-field bounds)
3465 (match-end bibtex-name-in-field)) 3571 (bibtex-end-of-name-in-field bounds))
3466 (buffer-substring-no-properties 3572 (buffer-substring-no-properties
3467 (match-beginning bibtex-text-in-field) 3573 (bibtex-start-of-text-in-field bounds)
3468 (match-end bibtex-text-in-field))) 3574 (bibtex-end-of-text-in-field bounds)))
3469 bibtex-field-kill-ring)) 3575 bibtex-field-kill-ring))
3470 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max) 3576 (if (> (length bibtex-field-kill-ring) bibtex-field-kill-ring-max)
3471 (setcdr 3577 (setcdr
3472 (nthcdr (1- bibtex-field-kill-ring-max) bibtex-field-kill-ring) 3578 (nthcdr (1- bibtex-field-kill-ring-max) bibtex-field-kill-ring)
3473 nil)) 3579 nil))
3482 (defun bibtex-copy-field-as-kill () 3588 (defun bibtex-copy-field-as-kill ()
3483 (interactive) 3589 (interactive)
3484 (bibtex-kill-field t)) 3590 (bibtex-kill-field t))
3485 3591
3486 (defun bibtex-kill-entry (&optional copy-only) 3592 (defun bibtex-kill-entry (&optional copy-only)
3487 "Kill the entire enclosing BibTeX reference entry. 3593 "Kill the entire enclosing BibTeX entry.
3488 With prefix arg COPY-ONLY the current reference entry to 3594 With prefix arg COPY-ONLY the current entry to
3489 `bibtex-entry-kill-ring', but do not actually kill it." 3595 `bibtex-entry-kill-ring', but do not actually kill it."
3490 (interactive "P") 3596 (interactive "P")
3491 (let ((pnt (point)) 3597 (let* ((pnt (point))
3492 (case-fold-search t) 3598 (case-fold-search t)
3493 (beg (bibtex-beginning-of-entry)) 3599 (beg (bibtex-beginning-of-entry))
3494 (end 3600 (end
3495 (progn 3601 (progn
3496 (bibtex-end-of-entry) 3602 (bibtex-end-of-entry)
3497 (if (re-search-forward 3603 (if (re-search-forward
3498 bibtex-reference-maybe-empty-head nil 'move) 3604 bibtex-entry-maybe-empty-head nil 'move)
3499 (goto-char (match-beginning 0))) 3605 (goto-char (match-beginning 0)))
3500 (point)))) 3606 (point))))
3501 (setq 3607 (setq
3502 bibtex-entry-kill-ring 3608 bibtex-entry-kill-ring
3503 (cons 3609 (cons
3504 (list 'entry (buffer-substring-no-properties beg end)) 3610 (list 'entry (buffer-substring-no-properties beg end))
3505 bibtex-entry-kill-ring)) 3611 bibtex-entry-kill-ring))
3550 3656
3551 (defun bibtex-empty-field () 3657 (defun bibtex-empty-field ()
3552 "Delete the text part of the current field, replace with empty text." 3658 "Delete the text part of the current field, replace with empty text."
3553 (interactive) 3659 (interactive)
3554 (bibtex-inside-field) 3660 (bibtex-inside-field)
3555 (bibtex-enclosing-field) 3661 (let ((bounds (bibtex-enclosing-field)))
3556 (goto-char (match-beginning bibtex-text-in-field)) 3662 (goto-char (bibtex-start-of-text-in-field bounds))
3557 (delete-region (point) (match-end bibtex-text-in-field)) 3663 (delete-region (point) (bibtex-end-of-text-in-field bounds))
3558 (insert (concat (bibtex-field-left-delimiter) 3664 (insert (concat (bibtex-field-left-delimiter)
3559 (bibtex-field-right-delimiter)) ) 3665 (bibtex-field-right-delimiter)) )
3560 (bibtex-find-text t)) 3666 (bibtex-find-text t)))
3561 3667
3562 (defun bibtex-pop-previous (arg) 3668 (defun bibtex-pop-previous (arg)
3563 "Replace text of current field with the similar field in previous entry. 3669 "Replace text of current field with the similar field in previous entry.
3564 With arg, goes up ARG entries. Repeated, goes up so many times. May be 3670 With arg, goes up ARG entries. Repeated, goes up so many times. May be
3565 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)." 3671 intermixed with \\[bibtex-pop-next] (bibtex-pop-next)."
3571 With arg, goes down ARG entries. Repeated, goes down so many times. May be 3677 With arg, goes down ARG entries. Repeated, goes down so many times. May be
3572 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)." 3678 intermixed with \\[bibtex-pop-previous] (bibtex-pop-previous)."
3573 (interactive "p") 3679 (interactive "p")
3574 (bibtex-pop arg 'next)) 3680 (bibtex-pop arg 'next))
3575 3681
3576 (defun bibtex-clean-entry (&optional new-label called-by-reformat) 3682 (defun bibtex-clean-entry (&optional new-reference-key called-by-reformat)
3577 "Finish editing the current BibTeX entry and clean it up. 3683 "Finish editing the current BibTeX entry and clean it up.
3578 Checks that no required fields are empty and formats entry dependent 3684 Checks that no required fields are empty and formats entry dependent
3579 on the value of bibtex-entry-format. 3685 on the value of bibtex-entry-format.
3580 If label of entry is empty or a prefix argument is given, calculate a 3686 If the reference key of the entry is empty or a prefix argument is given,
3581 new entry label (note: this only will work if fields in entry begin on 3687 calculate a new reference key (note: this only will work if fields in entry
3582 separate lines prior to calling bibtex-clean-entry or if 'realign is 3688 begin on separate lines prior to calling bibtex-clean-entry or if 'realign is
3583 contained in bibtex-entry-format). 3689 contained in bibtex-entry-format).
3584 Don't call this on `string' or `preamble' entries. 3690 Don't call this on `string' or `preamble' entries.
3585 At end of the cleaning process, the functions in 3691 At end of the cleaning process, the functions in
3586 bibtex-clean-entry-hook are called with region narrowed to entry." 3692 bibtex-clean-entry-hook are called with region narrowed to entry."
3587 (interactive "P") 3693 (interactive "P")
3589 (let* ((case-fold-search t) 3695 (let* ((case-fold-search t)
3590 (eob (bibtex-end-of-entry)) 3696 (eob (bibtex-end-of-entry))
3591 (key (progn 3697 (key (progn
3592 (bibtex-beginning-of-entry) 3698 (bibtex-beginning-of-entry)
3593 (if (re-search-forward 3699 (if (re-search-forward
3594 bibtex-reference-head eob t) 3700 bibtex-entry-head eob t)
3595 (buffer-substring-no-properties 3701 (buffer-substring-no-properties
3596 (match-beginning bibtex-key-in-head) 3702 (match-beginning bibtex-key-in-head)
3597 (match-end bibtex-key-in-head)))))) 3703 (match-end bibtex-key-in-head))))))
3598 (if (or 3704 (if (or
3599 new-label 3705 new-reference-key
3600 (not key)) 3706 (not key))
3601 (progn 3707 (progn
3602 (let ((autokey 3708 (let ((autokey
3603 (if bibtex-autokey-edit-before-use 3709 (if bibtex-autokey-edit-before-use
3604 (read-from-minibuffer 3710 (read-from-minibuffer
3605 "Key to use: " (bibtex-generate-autokey) nil nil 3711 "Key to use: " (bibtex-generate-autokey) nil nil
3606 'bibtex-key-history) 3712 'bibtex-key-history)
3607 (bibtex-generate-autokey)))) 3713 (bibtex-generate-autokey))))
3608 (bibtex-beginning-of-entry) 3714 (bibtex-beginning-of-entry)
3609 (re-search-forward bibtex-reference-maybe-empty-head) 3715 (re-search-forward bibtex-entry-maybe-empty-head)
3610 (if (match-beginning bibtex-key-in-head) 3716 (if (match-beginning bibtex-key-in-head)
3611 (delete-region (match-beginning bibtex-key-in-head) 3717 (delete-region (match-beginning bibtex-key-in-head)
3612 (match-end bibtex-key-in-head))) 3718 (match-end bibtex-key-in-head)))
3613 (insert autokey) 3719 (insert autokey)
3614 (let* ((start (bibtex-beginning-of-entry)) 3720 (let* ((start (bibtex-beginning-of-entry))
3615 (end (progn 3721 (end (progn
3616 (bibtex-end-of-entry) 3722 (bibtex-end-of-entry)
3617 (if (re-search-forward 3723 (if (re-search-forward
3618 bibtex-reference-maybe-empty-head nil 'move) 3724 bibtex-entry-maybe-empty-head nil 'move)
3619 (goto-char (match-beginning 0))) 3725 (goto-char (match-beginning 0)))
3620 (point))) 3726 (point)))
3621 (entry (buffer-substring start end))) 3727 (entry (buffer-substring start end)))
3622 (delete-region start end) 3728 (delete-region start end)
3623 (let ((success 3729 (let ((success
3626 (not bibtex-maintain-sorted-entries) 3732 (not bibtex-maintain-sorted-entries)
3627 (bibtex-find-entry-location autokey t)))) 3733 (bibtex-find-entry-location autokey t))))
3628 (insert entry) 3734 (insert entry)
3629 (forward-char -1) 3735 (forward-char -1)
3630 (bibtex-beginning-of-entry) 3736 (bibtex-beginning-of-entry)
3631 (re-search-forward bibtex-reference-head) 3737 (re-search-forward bibtex-entry-head)
3632 (if (not success) 3738 (if (not success)
3633 (error 3739 (error
3634 "New inserted reference yields duplicate key")))))))) 3740 "New inserted entry yields duplicate key"))))))))
3635 (if (not called-by-reformat) 3741 (if (not called-by-reformat)
3636 (save-excursion 3742 (save-excursion
3637 (save-restriction 3743 (save-restriction
3638 (narrow-to-region 3744 (narrow-to-region
3639 (bibtex-beginning-of-entry) (bibtex-end-of-entry)) 3745 (bibtex-beginning-of-entry) (bibtex-end-of-entry))
3650 (let ((pnt (copy-marker (point))) 3756 (let ((pnt (copy-marker (point)))
3651 (end (copy-marker (bibtex-end-of-entry)))) 3757 (end (copy-marker (bibtex-end-of-entry))))
3652 (bibtex-beginning-of-entry) 3758 (bibtex-beginning-of-entry)
3653 (bibtex-delete-whitespace) 3759 (bibtex-delete-whitespace)
3654 (indent-to-column bibtex-entry-offset) 3760 (indent-to-column bibtex-entry-offset)
3655 (while (re-search-forward bibtex-field end t) 3761 (let ((bounds (bibtex-search-forward-field bibtex-field-name end)))
3656 (let* ((begin-field 3762 (while bounds
3657 (copy-marker (match-beginning 0))) 3763 (let* ((begin-field (copy-marker (bibtex-start-of-field bounds)))
3658 (end-field 3764 (end-field (copy-marker (bibtex-end-of-field bounds)))
3659 (copy-marker (match-end 0))) 3765 (begin-name (copy-marker (bibtex-start-of-name-in-field bounds)))
3660 (begin-name 3766 (end-name (copy-marker (bibtex-end-of-name-in-field bounds))))
3661 (copy-marker (match-beginning bibtex-name-in-field))) 3767 (goto-char begin-field)
3662 (end-name 3768 (forward-char)
3663 (copy-marker (match-end bibtex-name-in-field)))) 3769 (bibtex-delete-whitespace)
3664 (goto-char begin-field) 3770 (open-line 1)
3665 (forward-char) 3771 (forward-char)
3666 (bibtex-delete-whitespace) 3772 (indent-to-column
3667 (open-line 1) 3773 (+ bibtex-entry-offset bibtex-field-indentation))
3668 (forward-char) 3774 (re-search-forward "[ \t\n]*=" end)
3669 (indent-to-column 3775 (replace-match "=")
3670 (+ bibtex-entry-offset bibtex-field-indentation)) 3776 (forward-char -1)
3671 (re-search-forward "[ \t\n]*=" end) 3777 (if bibtex-align-at-equal-sign
3672 (replace-match "=") 3778 (indent-to-column
3673 (forward-char -1) 3779 (+ bibtex-entry-offset (- bibtex-text-indentation 2)))
3674 (if bibtex-align-at-equal-sign 3780 (insert " "))
3675 (indent-to-column 3781 (forward-char)
3676 (+ bibtex-entry-offset (- bibtex-text-indentation 2))) 3782 (bibtex-delete-whitespace)
3677 (insert " ")) 3783 (if bibtex-align-at-equal-sign
3678 (forward-char) 3784 (insert " ")
3679 (bibtex-delete-whitespace) 3785 (indent-to-column bibtex-text-indentation))
3680 (if bibtex-align-at-equal-sign 3786 (while (re-search-forward "[ \t\n]+" end-field 'move)
3681 (insert " ") 3787 (replace-match " "))
3682 (indent-to-column bibtex-text-indentation)) 3788 (bibtex-do-auto-fill)
3683 (while (re-search-forward "[ \t\n]+" end-field 'move) 3789 (goto-char end-field))
3684 (replace-match " ")) 3790 (setq bounds (bibtex-search-forward-field bibtex-field-name end))))
3685 (bibtex-do-auto-fill)))
3686 (if (looking-at ",") 3791 (if (looking-at ",")
3687 (forward-char)) 3792 (forward-char))
3688 (bibtex-delete-whitespace) 3793 (bibtex-delete-whitespace)
3689 (open-line 1) 3794 (open-line 1)
3690 (forward-char) 3795 (forward-char)
3699 (interactive "*P") 3804 (interactive "*P")
3700 (let* ((pnt (point)) 3805 (let* ((pnt (point))
3701 (use-previous-options 3806 (use-previous-options
3702 (and (equal (prefix-numeric-value additional-options) 16) 3807 (and (equal (prefix-numeric-value additional-options) 16)
3703 (or bibtex-reformat-previous-options 3808 (or bibtex-reformat-previous-options
3704 bibtex-reformat-previous-labels))) 3809 bibtex-reformat-previous-reference-keys)))
3705 (bibtex-entry-format 3810 (bibtex-entry-format
3706 (if additional-options 3811 (if additional-options
3707 (if use-previous-options 3812 (if use-previous-options
3708 bibtex-reformat-previous-options 3813 bibtex-reformat-previous-options
3709 (setq 3814 (setq
3736 'delimiters) 3841 'delimiters)
3737 (if (y-or-n-p 3842 (if (y-or-n-p
3738 "Unify case of entry types and field names? ") 3843 "Unify case of entry types and field names? ")
3739 'unify-case))))) 3844 'unify-case)))))
3740 '(realign))) 3845 '(realign)))
3741 (labels 3846 (reformat-reference-keys
3742 (if additional-options 3847 (if additional-options
3743 (if use-previous-options 3848 (if use-previous-options
3744 bibtex-reformat-previous-labels 3849 bibtex-reformat-previous-reference-keys
3745 (setq 3850 (setq
3746 bibtex-reformat-previous-labels 3851 bibtex-reformat-previous-reference-keys
3747 (y-or-n-p "Generate automatically new reference labels? "))))) 3852 (y-or-n-p "Generate new reference keys automatically? ")))))
3748 bibtex-autokey-edit-before-use 3853 bibtex-autokey-edit-before-use
3749 (bibtex-sort-ignore-string-entries t) 3854 (bibtex-sort-ignore-string-entries t)
3750 (start-point 3855 (start-point
3751 (if (bibtex-mark-active) 3856 (if (bibtex-mark-active)
3752 (region-beginning) 3857 (region-beginning)
3777 (goto-char start-point) 3882 (goto-char start-point)
3778 (bibtex-progress-message "Formatting" 1) 3883 (bibtex-progress-message "Formatting" 1)
3779 (bibtex-map-entries 3884 (bibtex-map-entries
3780 (lambda (current) 3885 (lambda (current)
3781 (bibtex-progress-message) 3886 (bibtex-progress-message)
3782 (bibtex-clean-entry labels labels) 3887 (bibtex-clean-entry reformat-reference-keys reformat-reference-keys)
3783 (if (memq 'realign bibtex-entry-format) 3888 (if (memq 'realign bibtex-entry-format)
3784 (progn 3889 (progn
3785 (bibtex-end-of-entry) 3890 (bibtex-end-of-entry)
3786 (bibtex-delete-whitespace) 3891 (bibtex-delete-whitespace)
3787 (open-line 2))))) 3892 (open-line 2)))))
3788 (bibtex-progress-message 'done)) 3893 (bibtex-progress-message 'done))
3789 (if (and 3894 (if (and
3790 labels 3895 reformat-reference-keys
3791 bibtex-maintain-sorted-entries 3896 bibtex-maintain-sorted-entries
3792 (not called-by-convert-alien)) 3897 (not called-by-convert-alien))
3793 (progn 3898 (progn
3794 (bibtex-sort-buffer) 3899 (bibtex-sort-buffer)
3795 (setq bibtex-keys nil) 3900 (setq bibtex-reference-keys nil)
3796 (bibtex-parse-keys nil t t))) 3901 (bibtex-parse-keys nil t t)))
3797 (goto-char pnt))) 3902 (goto-char pnt)))
3798 3903
3799 (defun bibtex-convert-alien (&optional do-additional-reformatting) 3904 (defun bibtex-convert-alien (&optional do-additional-reformatting)
3800 "Converts an alien BibTeX buffer to be fully usable by BibTeX mode. 3905 "Converts an alien BibTeX buffer to be fully usable by BibTeX mode.
3836 (defun bibtex-complete-key () 3941 (defun bibtex-complete-key ()
3837 "Complete word fragment before point to longest prefix of a defined key. 3942 "Complete word fragment before point to longest prefix of a defined key.
3838 If point is not after the part of a word, all keys are listed. This 3943 If point is not after the part of a word, all keys are listed. This
3839 function is most useful in completing crossref entries." 3944 function is most useful in completing crossref entries."
3840 (interactive "*") 3945 (interactive "*")
3841 (if (not bibtex-keys) 3946 (if (not bibtex-reference-keys)
3842 (bibtex-parse-keys nil t)) 3947 (bibtex-parse-keys nil t))
3843 (bibtex-complete bibtex-keys)) 3948 (bibtex-complete bibtex-reference-keys))
3844 3949
3845 (defun bibtex-Article () 3950 (defun bibtex-Article ()
3846 "Insert a new BibTeX @Article entry; see also `bibtex-entry'." 3951 "Insert a new BibTeX @Article entry; see also `bibtex-entry'."
3847 (interactive) 3952 (interactive)
3848 (bibtex-entry "Article")) 3953 (bibtex-entry "Article"))
3908 (bibtex-entry "Unpublished")) 4013 (bibtex-entry "Unpublished"))
3909 4014
3910 (defun bibtex-String () 4015 (defun bibtex-String ()
3911 "Insert a new BibTeX @String entry." 4016 "Insert a new BibTeX @String entry."
3912 (interactive) 4017 (interactive)
3913 (if (not bibtex-keys) 4018 (if (not bibtex-reference-keys)
3914 (bibtex-parse-keys nil t)) 4019 (bibtex-parse-keys nil t))
3915 (let ((key 4020 (let ((key
3916 (if (and 4021 (if (and
3917 bibtex-maintain-sorted-entries 4022 bibtex-maintain-sorted-entries
3918 (not bibtex-sort-ignore-string-entries)) 4023 (not bibtex-sort-ignore-string-entries))
3919 (completing-read 4024 (completing-read
3920 "String key: " bibtex-keys nil nil nil 'bibtex-key-history)))) 4025 "String key: " bibtex-reference-keys nil nil nil 'bibtex-key-history))))
3921 (if (and 4026 (if (and
3922 bibtex-maintain-sorted-entries 4027 bibtex-maintain-sorted-entries
3923 (not bibtex-sort-ignore-string-entries)) 4028 (not bibtex-sort-ignore-string-entries))
3924 (bibtex-find-entry-location key) 4029 (bibtex-find-entry-location key)
3925 (bibtex-move-outside-of-entry)) 4030 (bibtex-move-outside-of-entry))