Function: bibtex-autofill-entry
bibtex-autofill-entry is an interactive and byte-compiled function
defined in bibtex.el.gz.
Signature
(bibtex-autofill-entry)
Documentation
Try to fill fields of current BibTeX entry based on neighboring entries.
The current entry must have a key. Determine the neighboring entry
(previous or next) whose key is more similar to the key of the current
entry. For all empty fields of the current entry insert the corresponding
field contents of the neighboring entry. Finally try to update the text
based on the difference between the keys of the neighboring and the current
entry (for example, the year parts of the keys).
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/bibtex.el.gz
(defun bibtex-autofill-entry ()
"Try to fill fields of current BibTeX entry based on neighboring entries.
The current entry must have a key. Determine the neighboring entry
\(previous or next) whose key is more similar to the key of the current
entry. For all empty fields of the current entry insert the corresponding
field contents of the neighboring entry. Finally try to update the text
based on the difference between the keys of the neighboring and the current
entry (for example, the year parts of the keys)."
(interactive)
(bibtex-beginning-of-entry)
(when (looking-at bibtex-entry-head)
(let ((type (bibtex-type-in-head))
(key (bibtex-key-in-head))
(key-end (match-end bibtex-key-in-head))
(case-fold-search t)
(bibtex-sort-ignore-string-entries t)
tmp other-key other bounds)
;; The fields we want to change start right after the key.
(goto-char key-end)
;; First see whether to use the previous or the next entry
;; for "inspiration".
(save-excursion
(goto-char (1- (match-beginning 0)))
(bibtex-beginning-of-entry)
(if (and (looking-at bibtex-entry-head)
(bibtex-string= type (bibtex-type-in-head))
;; In case we found ourselves :-(
(not (equal key (setq tmp (bibtex-key-in-head)))))
(setq other-key tmp
other (point))))
(save-excursion
(bibtex-end-of-entry)
(bibtex-skip-to-valid-entry)
(if (and (looking-at bibtex-entry-head)
(bibtex-string= type (bibtex-type-in-head))
;; In case we found ourselves :-(
(not (equal key (setq tmp (bibtex-key-in-head))))
(or (not other-key)
;; Check which is the best match.
(< (length (try-completion "" (list key other-key)))
(length (try-completion "" (list key tmp))))))
(setq other-key tmp
other (point))))
;; Then fill the new entry's fields with the chosen other entry.
(when other
(setq other (save-excursion (goto-char other) (bibtex-parse-entry)))
(setq key-end (point)) ;In case parse-entry changed the buffer.
(while (setq bounds (bibtex-parse-field))
(let ((text (assoc-string (bibtex-name-in-field bounds t)
other t)))
(if (not (and text
(equal "" (bibtex-text-in-field-bounds bounds t))))
(goto-char (bibtex-end-of-field bounds))
(goto-char (bibtex-start-of-text-in-field bounds))
(delete-region (point) (bibtex-end-of-text-in-field bounds))
(insert (cdr text)))))
;; Finally try to update the text based on the difference between
;; the two keys.
(let* ((prefix (try-completion "" (list key other-key)))
;; If the keys are foo91 and foo92, don't replace 1 for 2
;; but 91 for 92 instead.
(_ (if (string-match "[0-9]+\\'" prefix)
(setq prefix (substring prefix 0 (match-beginning 0)))))
(suffix (substring key (length prefix)))
(other-suffix (substring other-key (length prefix))))
(while (re-search-backward (regexp-quote other-suffix) key-end 'move)
(replace-match suffix)))))))