Managing bibliographies from Emacs (v2)

This is a tentative blog post about how I am migrating from Mendely to using bibtex files in Emacs, combined with some annotation goodies to be added later on.

My current backend for managing bibliographies is ebib, a rather sleek piece of software to query and reference bibtex databases, either individually, or grouped together.

To install ebib, assuming you already have use-package, add the following lines to your .emacs file

(use-package ebib
  :defer t
  :ensure t
  (ebib-preload-bib-files '("bibtexfile1.bib" "bibtexfile2.bib"))
  (ebib-bib-search-dirs '("/default/path/to/bibtex/files")))

This downloads the library from your default repository, and configures it to use some default bibtex files.

A nice complement to ebib is biblio, a library to query various databases, such as Crossref, the arXiv, etc. This library allows you to grab bibtex records for the articles you search for. The following code links this library with ebib, so that you can press Ctrl-i and the current record is added to your ebib database.

(use-package biblio
  :defer t
  :ensure t
  (biblio-arxiv-bibtex-header "misc")
  (:map biblio-selection-mode-map
        ("C-i" . biblio-to-ebib-and-quit))
  ;; This links biblio to ebib
  (defun biblio-to-ebib-and-quit ()
     (lambda (entry metadata)
       (print entry)
       (ebib-capture-raw-bibtex entry)

In order for this to work, you need the following code, which extends ebib with a function to edit raw Bibtex entries from arbitrary sources

(defvar ebib-capture-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-c\C-c" #'ebib-capture-finalize)
    (define-key map "\C-c\C-k" #'ebib-capture-kill)
  "Keymap for `ebib-capture-mode', a minor mode.
  Use this map to set additional keybindings for when Org mode is used
  for a capture buffer.")

(defvar ebib-capture-mode-hook nil
  "Hook for the `ebib-capture-mode' minor mode.")

(define-minor-mode ebib-capture-mode
  "Minor mode for special key bindings in a capture buffer.

  Turning on this mode runs the normal hook `ebib-capture-mode-hook'."
  nil " Cap" ebib-capture-mode-map
    "\\<ebib-capture-mode-map>Capture buffer.  Finish \
  `\\[ebib-capture-finalize]', abort `\\[ebib-capture-kill]'.")))

(defun ebib-capture-kill ()
  "Abort the current capture process."
  ;; FIXME: This does not do the right thing, we need to remove the
  ;; new stuff by hand it is easy: undo, then kill the buffer
  (kill-buffer (current-buffer))

(defun ebib-capture-finalize ()
  "Save entries and finalize."
    ((or slave-db filtered-db)
     (error "[Ebib] Cannot merge into a filtered or a slave database"))
     (unless (ebib-capture-check-duplicates)
       (let ((result (ebib--bib-find-bibtex-entries ebib--cur-db nil)))
         (ebib--log 'message "%d entries, %d @Strings and %s @Preamble found in file."
                    (car result)
                    (cadr result)
                    (if (nth 2 result) "a" "no")))
       (ebib--set-modified t ebib--cur-db)
    (default (beep))))

(defun ebib-capture-check-duplicates ()
  (let ((db (ebib-db-new-database))
        (duplicates '()))
    (let ((result (ebib--bib-find-bibtex-entries db nil)))
      (if (zerop (car result))
          (message "No entries found")
         (lambda (key value)
           (if (ebib-db-get-entry key ebib--cur-db t)
               (push key duplicates)))
         (ebib-db-val 'entries db))))
    (when duplicates
      (message "Found duplicate keys: %S" duplicates)
      (goto-char 0)
      (search-forward (car duplicates)))))

(defun ebib-capture-raw-bibtex (entry)
  (with-current-buffer (get-buffer-create "*Biblio entry*")
    (insert entry)
    (pop-to-buffer (current-buffer))