Skip to content

Commit

Permalink
add bibtex-completion--read
Browse files Browse the repository at this point in the history
Makes core commands interactions by adding a new
`bibtex-completion--read` function, with configurable completion
systems. The default uses `completing-read`.
  • Loading branch information
bdarcus committed Feb 23, 2021
1 parent 94807a3 commit bb3d3e6
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion bibtex-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,24 @@
(declare-function org-element-property "org-element")

(defgroup bibtex-completion nil
"Helm plugin for searching entries in a BibTeX bibliography."
"Provides searching entries in a BibTeX bibliography, and running actions on them."
:group 'completion)

(defvar bibtex-completion-map
"Keymap for bibtex-completion commands"
(let ((map (make-sparse-keymap)))
(define-key map (kbd "p") 'bibtex-completion-open-pdf)
(define-key map (kbd "u") 'bibtex-completion-open-url-or-doi)
(define-key map (kbd "c") 'bibtex-completion-insert-citation)
(define-key map (kbd "r") 'bibtex-completion-insert-reference)
(define-key map (kbd "k") 'bibtex-completion-insert-key)
(define-key map (kbd "b") 'bibtex-completion-insert-bibtex)
(define-key map (kbd "a") 'bibtex-completion-add-PDF-attachment)
(define-key map (kbd "e") 'bibtex-completion-edit-notes)
(define-key map (kbd "s") 'bibtex-completion-show-entry)
(define-key map (kbd "l") 'bibtex-completion-add-pdf-to-library)
map))

(defcustom bibtex-completion-bibliography nil
"The BibTeX file or list of BibTeX files.
Org-bibtex users can also specify org mode bibliography files, in
Expand Down Expand Up @@ -478,6 +493,23 @@ for string replacement."
(defvar bibtex-completion-cached-notes-keys nil
"A cache storing notes keys obtained when the bibliography was last parsed.")

(defun bibtex-completion--read ()
"Select BibTeX entries in completion system."
;; define a completion function that defaults to completing-read, but can be overridden
(list (cdr (assoc (completing-read-bibtex--read) (bibtex-completion--get-candidates)))))

(defun completing-read-bibtex--read ()
"Read bibtex-completion entries for completion using completing-read."
(bibtex-completion-init)
(completing-read
"BibTeX entries: "
(bibtex-completion--get-candidates)))

(defun bibtex-completion--get-candidates ()
"Return all keys from bibtex-completion-candidates."
(mapcar (lambda (cand) (cons
(bibtex-completion-format-entry cand (1- (frame-width))) (cdr (assoc "=key=" cand)))) (bibtex-completion-candidates)))

(defun bibtex-completion-candidates ()
"Read the BibTeX files and return a list of conses, one for each entry.
The first element of these conses is a string containing authors,
Expand Down Expand Up @@ -1293,11 +1325,13 @@ Surrounding curly braces are stripped."

(defun bibtex-completion-insert-key (keys)
"Insert BibTeX KEYS at point."
(interactive (list (bibtex-completion--read)))
(insert
(funcall 'bibtex-completion-format-citation-default keys)))

(defun bibtex-completion-insert-bibtex (keys)
"Insert BibTeX entries for entries in KEYS at point."
(interactive (list (bibtex-completion--read)))
(insert (s-join "\n" (--map (bibtex-completion-make-bibtex it) keys))))

(defun bibtex-completion-make-bibtex (key)
Expand Down

0 comments on commit bb3d3e6

Please sign in to comment.