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 4 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
85 changes: 85 additions & 0 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
;;; 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))

(defcustom bibtex-actions-default-action 'bibtex-actions-open-any
"The default action for the `bibtex-actions` command."
:group 'bibtex-completion
:type 'function)

(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 (cdr (assoc (bibtex-actions--read) (bibtex-actions--get-candidates)))))
(,action (list cand)))))

(bibtex-actions-define-action bibtex-completion-open-any "")
(bibtex-actions-define-action bibtex-completion-open-pdf "")
(bibtex-actions-define-action bibtex-completion-open-url-or-doi "")
(bibtex-actions-define-action bibtex-completion-insert-citation "")
(bibtex-actions-define-action bibtex-completion-insert-reference "")
(bibtex-actions-define-action bibtex-completion-insert-key "")
(bibtex-actions-define-action bibtex-completion-insert-bibtex "")
(bibtex-actions-define-action bibtex-completion-add-PDF-attachment "")
(bibtex-actions-define-action bibtex-completion-edit-notes "")
(bibtex-actions-define-action bibtex-completion-show-entry "")
(bibtex-actions-define-action bibtex-completion-add-pdf-to-library "")

(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."
(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)))))

;;;###autoload
(defun bibtex-actions (bib-entry)

This comment was marked as outdated.

"Search BibTeX entries using `completing-read' and actions."
(interactive
(progn
(bibtex-completion-init)
(list (cdr (assoc (bibtex-actions--read) (bibtex-actions--get-candidates))))))
(apply bibtex-actions-default-action (list bib-entry)))

(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