Function: bibtex-clean-entry

bibtex-clean-entry is an interactive and byte-compiled function defined in bibtex.el.gz.

Signature

(bibtex-clean-entry &optional NEW-KEY CALLED-BY-REFORMAT)

Documentation

Finish editing the current BibTeX entry and clean it up.

Check that no required fields are empty and format entry dependent on the value of bibtex-entry-format. If the reference key of the entry is empty or a prefix argument is given, calculate a new reference key. (Note: this works only if fields in entry begin on separate lines prior to calling bibtex-clean-entry or if realign is contained in bibtex-entry-format.) Don't call bibtex-clean-entry on @Preamble entries. At end of the cleaning process, the functions in bibtex-clean-entry-hook are called with region narrowed to entry.

Probably introduced at or before Emacs version 19.29.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/bibtex.el.gz
(defun bibtex-clean-entry (&optional new-key called-by-reformat)
  "Finish editing the current BibTeX entry and clean it up.
Check that no required fields are empty and format entry dependent
on the value of `bibtex-entry-format'.
If the reference key of the entry is empty or a prefix argument is given,
calculate a new reference key.  (Note: this works only if fields in entry
begin on separate lines prior to calling `bibtex-clean-entry' or if
`realign' is contained in `bibtex-entry-format'.)
Don't call `bibtex-clean-entry' on @Preamble entries.
At end of the cleaning process, the functions in
`bibtex-clean-entry-hook' are called with region narrowed to entry."
  ;; Opt. arg CALLED-BY-REFORMAT is t if `bibtex-clean-entry'
  ;; is called by `bibtex-reformat'
  (interactive "P")
  (let ((case-fold-search t)
        (start (bibtex-beginning-of-entry))
        (_ (or (looking-at bibtex-any-entry-maybe-empty-head)
	       (user-error "Not inside a BibTeX entry")))
        (entry-type (bibtex-type-in-head))
        (key (bibtex-key-in-head)))
    (cond ((string-equal-ignore-case entry-type "preamble")
           ;; (bibtex-format-preamble)
           (user-error "No clean up of @Preamble entries"))
          ((string-equal-ignore-case entry-type "string")
           (setq entry-type 'string))
          ;; (bibtex-format-string)
          (t (bibtex-format-entry)))
    ;; set key
    (if (or new-key (not key))
        (save-excursion
          ;; First delete the old key so that a customized algorithm
          ;; for generating the new key does not get confused by the
          ;; old key.
          (re-search-forward (if (eq entry-type 'string)
                                 bibtex-string-maybe-empty-head
                               bibtex-entry-maybe-empty-head))
          (if (match-beginning bibtex-key-in-head)
              (delete-region (match-beginning bibtex-key-in-head)
                             (match-end bibtex-key-in-head)))
          (setq key (bibtex-generate-autokey))
          ;; Sometimes `bibtex-generate-autokey' returns an empty string
          (if (or bibtex-autokey-edit-before-use (string= "" key))
              (setq key (if (eq entry-type 'string)
                            (bibtex-read-string-key key)
                          (bibtex-read-key "Key to use: " key))))
          (insert key)))

    (unless called-by-reformat
      (let* ((end (save-excursion
                    (bibtex-end-of-entry)
                    (if (re-search-forward
                         bibtex-entry-maybe-empty-head nil 'move)
                        (goto-char (match-beginning 0)))
                    (point)))
             (entry (buffer-substring start end))
             ;; include the crossref key in index
             (index (let ((bibtex-maintain-sorted-entries 'crossref))
                      (bibtex-entry-index))) ; moves point to end of head
             error)
        ;; sorting
        (if (and bibtex-maintain-sorted-entries
                 (not (and bibtex-sort-ignore-string-entries
                           (eq entry-type 'string))))
            (progn
              (delete-region start end)
              (setq error (not (bibtex-prepare-new-entry index))
                    start (point)) ; update start
              (save-excursion (insert entry)))
          (bibtex-search-entry key)
          (setq error (or (/= (point) start)
                          (bibtex-search-entry key nil end))))
        (if error
            (user-error "New inserted entry yields duplicate key"))
        (dolist (buffer (bibtex-initialize))
          (with-current-buffer buffer
            (if (cdr (assoc-string key bibtex-reference-keys))
                (user-error "Duplicate key in %s" (buffer-file-name)))))

        ;; Only update `bibtex-strings' and `bibtex-reference-keys'
        ;; if they have been built already.
        (cond ((eq entry-type 'string)
               ;; We have a @String entry.
               (unless (or (functionp bibtex-strings)
                           (assoc key bibtex-strings))
                 (push (cons key (bibtex-text-in-string
                                  (bibtex-parse-string) t))
                       bibtex-strings)))
              ;; We have a normal entry.
              ((not (functionp bibtex-reference-keys))
               (let ((found (assoc key bibtex-reference-keys)))
                 (cond ((not found)
                        (push (cons key t) bibtex-reference-keys))
                       ((not (cdr found))
                        ;; Turn a crossref key into a header key
                        (setq bibtex-reference-keys
                              (cons (cons key t)
                                    (delete (list key) bibtex-reference-keys))))))
               ;; If entry has a crossref key, it goes into the list
               ;; `bibtex-reference-keys', too.
               (if (and (nth 1 index)
                        (not (assoc (nth 1 index) bibtex-reference-keys)))
                   (push (list (nth 1 index)) bibtex-reference-keys)))))

      ;; final clean up
      (if bibtex-clean-entry-hook
          (save-excursion
            (save-restriction
              (bibtex-narrow-to-entry)
              (run-hooks 'bibtex-clean-entry-hook)))))))