0

I would like to get the bibtex key of references in my .bib database as a string via helm-bibtex.

(defun get-bibtex-key ()
  (interactive)
  (with-temp-buffer
    (helm-bibtex)
    (if (string-prefix-p "cite" (buffer-string))
        (substring (buffer-string) 5) ;; Remove "cite: " from string.
      (buffer-string))
    (buffer-string)))

The code above correctly returns the desired value, but inserts text into the current buffer instead of storing its value within the get-bibtex-key function-variable as a string, which makes further deployment such as this impossible:

(bibtex-completion-get-entry (get-bibtex-key))

The above code when run just dumps the value returned by (get-bibtex-key) into the current buffer.

How can I get output from (helm-bibtex) behave like the code below?

(with-temp-buffer
  (insert "Hi!")
  (buffer-string))
2
  • Other approaches (such as ready-made functions) to getting bibtex key from a .bib database would also be appreciated.
    – Sati
    Oct 11, 2020 at 15:55
  • 1
    See bibtex-map-entries.
    – jagrg
    Oct 12, 2020 at 13:28

2 Answers 2

2

You can also get the keys using helm-marked-candidates:

(defun get-bibtex-key (_)
  (let ((keys (helm-marked-candidates)))
    (print keys)))

(helm-add-action-to-source "Get bibtex keys" 'get-bibtex-key helm-source-bibtex 0)

Then:

  1. M-x helm-bibtex
  2. M-a
  3. RET
1

If I understand correctly, you can use the (bibtex-completion-candidates) function, which returns a list of bibtex items. You then get the list containing the "=key=" string using assoc, taking the cdr (last) element of the alist which is the citation key. Finally pass this list of keys through completing-read so you can select one from the list, returning it as a string:

(defun get-bibtex-key ()
  (interactive)
  (completing-read "Citation key: "
                  (mapcar #'(lambda (x) (cdr (assoc "=key=" x)))
                          (bibtex-completion-candidates))))
1
  • Can a search be done on the author-title instead, like what we would do in helm?
    – Sati
    Oct 12, 2020 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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