5

I am a recent and happy user of BibTeX mode (bibtex.el), but there is one thing I can't get to work as I wish. When I create a template, I can enforce insertion of timestamp and owner with:

(defun current-date ()
    (format-time-string "%Y.%m.%d"))    
(setq
    bibtex-user-optional-fields '(
                                 ("timestamp" "Time the entry was created" current-date)
                                 ("owner" "Owner of the entry" user-login-name)
)

Is this possible to have the same behavior with bibtex-clean-entry (C-c C-c), which would automatically add the timestamp and the owner if they are not already set?


The rationale behind that is that I'm using biblio.el for automatic download of references (e.g. via DOI), and I set it to run bibtex-clean-entry right away with:

(setq biblio-cleanup-bibtex-function #'bibtex-clean-entry)

Being able to have the behavior described above for this function would actually save me a lot of time… and make sure I don't forget to add both fields.

1 Answer 1

2

This is something I've wanted too. I've implemented it for the timestamp as follows:

First, we need a function to add the timestamp if it doesn't exist already. In my system I call the time stamp date_added, just swap that out for timestamp for your system:

(defun bibty-add-date ()
  "Adds a date_added field to a bibtex entry.
Checks to make sure it doesn't exist first."
  (interactive)
  (save-excursion
    (bibtex-beginning-of-entry)
    (if (assoc "date_added" (bibtex-parse-entry))
        (message "date_added already exists!")
      (bibtex-end-of-entry) ;; if you want date_added always at the end
      (bibtex-make-field '("date_added" nil current-date) t nil))))
      ;; NB: current-date is @MathieuBasille's custom function from the question

I made this function interactive, as I often want to manually add the date_added, so it's convenient to bind this to C-c d.

To get this function to run whenever you run bibtex-clean-entry, add it to the hook:

(add-hook 'bibtex-clean-entry-hook 'bibty-add-date)

You can use the same approach for your owner field.

I maintain the bibtex-utils package; I should, eventually, put together a more generic approach to accomplish this for arbitrary fields and add it to the package. In the meantime hopefully this works well enough.

3
  • Brilliant, thanks! I had to remove the (bibtex-end-of-entry) which was actually moving out of the entry (right after to be precise). I also adapted to bibtex-add-date-owner with a check and insertion of user-login-name. Commented Dec 12, 2018 at 4:43
  • Also, I have been in contact with Roland Winkler, maintainer of bibtex.el. I just forwarded him your answer, maybe he'll be able to implement it directly in bibtex.el. Commented Dec 12, 2018 at 4:49
  • I found some problems with bibtex-end-of-entry as well. For me it works more consistently if I follow that with a (forward-char -1), which ensures that date_added is the last field in the entry.
    – Tyler
    Commented Dec 12, 2018 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.