Skip to content

Commit

Permalink
refactor: Remove macro and require explicit defuns (#15)
Browse files Browse the repository at this point in the history
Closes #14.
  • Loading branch information
bdarcus authored Mar 6, 2021
1 parent 6f68e3c commit bcb24cf
Showing 1 changed file with 66 additions and 70 deletions.
136 changes: 66 additions & 70 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
;;
;; Copyright (C) 2021 Bruce D'Arcus, Maxime Treca
;;
;; Author: Bruce D'Arcus <https://github.com/bdarcus>
;; Author: Maxime Treca <https://github.com/mtreca>
;; Author: Bruce D'Arcus <https://github.com/bdarcus>, Maxime Treca <https://github.com/mtreca>
;; Created: February 27, 2021
;; Modified: March 3, 2021
;; Version: 0.0.2
;; Version: 0.0
;; Keywords: bib, files
;; Homepage: https://github.com/bdarcus/bibtex-actions
;; Package-Requires: ((emacs "26.3") (bibtex-completion "1.0"))
Expand All @@ -25,6 +24,8 @@

(require 'bibtex-completion)

;;; Keymap

(defvar bibtex-actions-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "o") 'bibtex-actions-open)
Expand All @@ -41,6 +42,8 @@
map)
"Keymap for 'bibtex-actions'.")

;;; Completion functions

;; one can reset the backend function using fset; maybe there's a
;; more elegant way to do this?
(fset 'bibtex-actions-read-backend `bibtex-actions--completing-read)
Expand Down Expand Up @@ -76,76 +79,69 @@
(car candidate) 'display (bibtex-completion-format-entry candidate (1- (frame-width))))
(cdr (assoc "=key=" candidate)))))

(defmacro bibtex-actions-define-action (action doc &optional alt-name)
"A macro to create commands from bibtex-completion functions.
It takes the ACTION and the DOC to create another function named
NAME which extracts the keys from the selected candidates and
passes them to ACTION. Where ALT-NAME is present, use that instead."
(let* ((old-name (symbol-name action))
(mid-name (substring old-name 17 (length old-name)))
(new-name (intern (concat "bibtex-actions" mid-name)))
(name (or alt-name new-name)))
`(defun ,name (cand)
,doc
(interactive (list (bibtex-actions--read)))
(,action (list cand)))))

(bibtex-actions-define-action
bibtex-completion-open-any
"Open PDF, or URL or DOI link.
If multiple PDFs are found, ask for the one to open using
‘completing-read’. If no PDF is found, try to open a URL or DOI
in the browser instead."
bibtex-actions-open)
;;; Command wrappers for bibtex-completion functions

(bibtex-actions-define-action
bibtex-completion-open-pdf
"Open PDF.
(defun bibtex-actions-open (keys)
"Open PDF, or URL or DOI link.
Opens the PDF(s) associated with the KEYS. If multiple PDFs are
found, ask for the one to open using ‘completing-read’. If no
PDF is found, try to open a URL or DOI in the browser instead."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-open-any (list keys)))

(defun bibtex-actions-open-pdf (keys)
"Open PDF associated with the KEYS.
If multiple PDFs are found, ask for the one to open using
‘completing-read’.")

(bibtex-actions-define-action
bibtex-completion-open-url-or-doi
"Open URL or DOI in a browser."
bibtex-actions-open-link)

(bibtex-actions-define-action
bibtex-completion-insert-citation
"Insert citation.")

(bibtex-actions-define-action
bibtex-completion-insert-reference
"Insert formatted reference.")

(bibtex-actions-define-action
bibtex-completion-insert-key
"Insert BibTeX key.")

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

(bibtex-actions-define-action
bibtex-completion-add-PDF-attachment
"Attach PDF to email."
bibtex-actions-add-pdf-attachment)

(bibtex-actions-define-action
bibtex-completion-edit-notes
"Open notes."
bibtex-actions-open-notes)

(bibtex-actions-define-action
bibtex-completion-show-entry
"Open BibTeX entry."
bibtex-actions-open-entry)

(bibtex-actions-define-action
bibtex-completion-add-pdf-to-library
"Add PDF to library.
‘completing-read’."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-open-pdf (list keys)))

(defun bibtex-actions-open-link (keys)
"Open URL or DOI link associated with the KEYS in a browser."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-open-url-or-doi (list keys)))

(defun bibtex-actions-insert-citation (keys)
"Insert citation for the KEYS."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-insert-citation (list keys)))

(defun bibtex-actions-insert-reference (keys)
"Insert formatted reference(s) associated with the KEYS."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-insert-reference (list keys)))

(defun bibtex-actions-insert-key (keys)
"Insert BibTeX KEYS."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-insert-key (list keys)))

(defun bibtex-actions-insert-bibtex (keys)
"Insert BibTeX entry associated with the KEYS."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-insert-bibtex (list keys)))

(defun bibtex-actions-add-PDF-attachment (keys)
"Attach PDF(s) associated with the KEYS to email."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-add-PDF-attachment (list keys)))

(defun bibtex-actions-open-notes (keys)
"Open notes associated with the KEYS."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-edit-notes (list keys)))

(defun bibtex-actions-open-entry (keys)
"Open BibTeX entry associated with the KEYS."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-show-entry (list keys)))

(defun bibtex-actions-add-pdf-to-library (keys)
"Add PDF associated with the KEYS to library.
The PDF can be added either from an open buffer, a file, or a
URL.")

URL."
(interactive (list (bibtex-actions--read)))
(bibtex-completion-add-pdf-to-library (list keys)))

(provide 'bibtex-actions)
;;; bibtex-actions.el ends here

0 comments on commit bcb24cf

Please sign in to comment.