Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add embark-bibtex frontend. #355

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
;;; bibtex-actions.el --- Bibliography manager action
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
;;; bibtex-actions.el --- Bibliography manager action
;;; bibtex-actions.el --- Bibliography manager actions


Copy link

@bdarcus bdarcus Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
;;; Commentary:
;;;
;;; Provides wrappers around bibtex-completion action-functions, to make
;;; them available as standard emacs commands, and within completing-read
;;; compatible completion systems such as selectrum and icomplete-vertical.
:::
::: For additional power and flexibility, install marginalia and embark, and set up
;;; the action keybindings like so:
;;;
;;; (setf (alist-get 'bibtex embark-keymap-alist) 'bibtex-actions-map)
;;;

;;; Code:

(require 'bibtex-completion)

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

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

This comment was marked as outdated.

This comment was marked as outdated.

(cdr (assoc "=key=" cand))))
(bibtex-completion-candidates)))
Comment on lines +21 to +27
Copy link

@bdarcus bdarcus Feb 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion uses cl-loop, as elsewhere in the codebase and for better performance, and also uses propertize to allow us to support bibtex-completion-additional-search-fields.

Suggested change
(defun bibtex-actions--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--get-candidates ()
"Return all keys from bibtex-completion-candidates."
(cl-loop
for candidate in (bibtex-completion-candidates)
collect
(cons
;; Here use one string for display, and the other for search.
(propertize
(car candidate) 'display (bibtex-completion-format-entry candidate (1- (frame-width))))
(cdr (assoc "=key=" candidate)))))


(defun bibtex-actions--read ()
"Read bibtex-completion entries for completion."
(bibtex-completion-init)
(completing-read
"BibTeX entries: "
(lambda (string predicate action)
(if (eq action 'metadata)
'(metadata (category . bibtex))
(complete-with-action action (bibtex-actions--get-candidates) string predicate)))))

(defun bibtex-completion--read ()
"Select BibTeX entries in completion system."
(cdr (assoc (bibtex-actions--read) (bibtex-actions--get-candidates))))

(defmacro bibtex-actions-define-action (action doc)
"Wraps the function ACTION in another function named NAME which
extracts the key from the candidate selected in embark and
passes it to ACTION."
(let* ((old-name (symbol-name action))
(mid-name (substring old-name 17 (length old-name)))
(new-name (intern (concat "bibtex-actions" mid-name))))
`(defun ,new-name (cand)
,doc
(interactive (list (bibtex-completion--read)))
(,action (list cand)))))

(bibtex-actions-define-action
bibtex-completion-open-any
"Open the PDFs associated with the BibTeX entry.
If multiple PDFs are found, ask for the one to open using
‘completion-read’. If no PDF is found, try to open a URL or DOI
in the browser instead.")

(bibtex-actions-define-action
bibtex-completion-open-pdf
"Open the PDFs associated with the BibTeX entry.
If multiple PDFs are found, ask for the one to open using
‘completion-read’.")

(bibtex-actions-define-action
bibtex-completion-open-url-or-doi
"Open the URL or DOI associated with a BibTeX entry in a
browser.")

(bibtex-actions-define-action
bibtex-completion-insert-citation
"Insert citation for BibTeX entry at point.")

(bibtex-actions-define-action
bibtex-completion-insert-reference
"Insert reference for BibTeX entry at point.")

(bibtex-actions-define-action
bibtex-completion-insert-key
"Insert key for BibTeX entry at point.")

(bibtex-actions-define-action
bibtex-completion-insert-bibtex
"Insert entry for BibTeX entry at point.")

(bibtex-actions-define-action
bibtex-completion-add-PDF-attachment
"Attach the PDF of a BibTeX entry where available.")

(bibtex-actions-define-action
bibtex-completion-edit-notes
"Open the notes associated with a BibTeX entry using
‘bibtex-completion-edit-notes-function’.")

(bibtex-actions-define-action
bibtex-completion-show-entry
"Show the selected entry in the relevant BibTeX file.")

(bibtex-actions-define-action
bibtex-completion-add-pdf-to-library
"Add a PDF to the library for the selected BibTeX entry.
The PDF can be added either from an open buffer, a file, or a
URL.")

(provide 'bibtex-actions)

;; Local Variables:
;; byte-compile-warnings: (not cl-functions obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:

;;; bibtex-actions.el ends here