- ;; For inserting new fields, we use the fact that
- ;; `bibtex-parse-entry' moves point to the end of the last field.
- (let* ((fields-alist (bibtex-parse-entry))
- (field-list (bibtex-field-list
- (cdr (assoc "=type=" fields-alist)))))
- (skip-chars-backward " \t\n")
- (dolist (field (car field-list))
- (unless (assoc-string (car field) fields-alist t)
- (bibtex-make-field field)))
- (dolist (field (cdr field-list))
- (unless (assoc-string (car field) fields-alist t)
- (bibtex-make-optional-field field))))))
+ (when (looking-at bibtex-entry-maybe-empty-head)
+ (goto-char (match-end 0))
+ (if entry-type
+ (save-excursion
+ (replace-match (concat "@" entry-type) nil nil nil 1))
+ (setq entry-type (bibtex-type-in-head)))
+ (let* ((field-list (bibtex-field-list entry-type))
+ (required (copy-tree (car field-list)))
+ (optional (copy-tree (cdr field-list)))
+ bounds)
+ (while (setq bounds (bibtex-parse-field))
+ (let ((fname (bibtex-name-in-field bounds t))
+ (end (copy-marker (bibtex-end-of-field bounds) t)))
+ (setq required (delete (assoc-string fname required t) required)
+ optional (delete (assoc-string fname optional t) optional))
+ (when (string-match "\\`[0-9]+\\'"
+ (bibtex-text-in-field-bounds bounds))
+ (goto-char (bibtex-end-of-text-in-field bounds))
+ (insert (bibtex-field-right-delimiter))
+ (goto-char (bibtex-start-of-text-in-field bounds))
+ (insert (bibtex-field-left-delimiter)))
+ (goto-char end)))
+ (skip-chars-backward " \t\n")
+ (dolist (field required) (bibtex-make-field field))
+ (dolist (field optional) (bibtex-make-optional-field field))))))