From 5ad23234d6988b65a4b71ecb10d1570facf84ea2 Mon Sep 17 00:00:00 2001 From: Roland Winkler Date: Sun, 9 Apr 2006 18:29:19 +0000 Subject: [PATCH] (bibtex-entry-update): New optional arg entry-type. Add field delimiters to numerical fields if they are not present. --- lisp/textmodes/bibtex.el | 50 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/lisp/textmodes/bibtex.el b/lisp/textmodes/bibtex.el index a70ab1591ca..ff1c1e48eb0 100644 --- a/lisp/textmodes/bibtex.el +++ b/lisp/textmodes/bibtex.el @@ -2856,25 +2856,45 @@ is non-nil." (bibtex-autofill-entry)) (run-hooks 'bibtex-add-entry-hook))) -(defun bibtex-entry-update () +(defun bibtex-entry-update (&optional entry-type) "Update an existing BibTeX entry. In the BibTeX entry at point, make new fields for those items that may occur -according to `bibtex-field-list', but are not yet present." - (interactive) +according to `bibtex-field-list', but are not yet present. +Also, add field delimiters to numerical fields if they are not present. +If ENTRY-TYPE is non-nil, change first the entry type to ENTRY-TYPE. +When called interactively with a prefix arg, query for a value of ENTRY-TYPE." + (interactive + (list (if current-prefix-arg + (let ((completion-ignore-case t)) + (completing-read "New entry type: " bibtex-entry-field-alist + nil t nil 'bibtex-entry-type-history))))) (save-excursion (bibtex-beginning-of-entry) - ;; 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)))))) (defun bibtex-parse-entry (&optional content) "Parse entry at point, return an alist. -- 2.11.4.GIT