Skip to content

Commit

Permalink
add filtering on open commands
Browse files Browse the repository at this point in the history
Change read defun to cl-defun, add :initial and :predicate.
  • Loading branch information
bdarcus committed Apr 28, 2021
1 parent 743f548 commit 2533b71
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ may be indicated with the same icon but a different face."
"Keymap for 'bibtex-actions'.")

;;; Completion functions
(defun bibtex-actions-read ()
"Read bibtex-completion entries for completion using 'completing-read-multiple'."
(cl-defun bibtex-actions-read (&rest options &key initial filter)
"Read bibtex-completion entries with INITIAL or FILTER OPTIONS.
This provides a wrapper around 'completing-read-multiple', with
the following optional arguments:
':initial': provides the initial value
':filter': a 'predicate' function to filter the candidate list"
(when-let ((crm-separator "\\s-*&\\s-*")
(candidates (bibtex-actions--get-candidates))
(chosen
Expand All @@ -116,11 +122,19 @@ may be indicated with the same icon but a different face."
`(metadata
(affixation-function . bibtex-actions--affixation)
(category . bibtex))
(complete-with-action action candidates string predicate))))))
(complete-with-action action candidates string predicate))
nil initial nil nil nil))))
(cl-loop for choice in chosen
;; Collect citation keys of selected candidate(s).
collect (cdr (assoc choice candidates)))))

(cl-defun bibtex-actions--filter (str)
"A predicate function to filter candidates not containing STR."
;; want to use 'get-property' to grab the 'hidden' string
;; or to generalize, may need more
;; along with 'seq-filter' or '-keep' or '-filter'
(message str))

(defun bibtex-actions--format-candidates ()
"Transform candidates from 'bibtex-completion-candidates'.
This both propertizes the candidates for display, and grabs the
Expand Down Expand Up @@ -279,21 +293,21 @@ TEMPLATE."
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)))
(interactive (list (bibtex-actions-read :initial "has:link has:pdf")))
(bibtex-completion-open-any keys))

;;;###autoload
(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’."
(interactive (list (bibtex-actions-read)))
(interactive (list (bibtex-actions-read :initial "has:pdf")))
(bibtex-completion-open-pdf keys))

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

;;;###autoload
Expand Down Expand Up @@ -329,7 +343,7 @@ If multiple PDFs are found, ask for the one to open using
;;;###autoload
(defun bibtex-actions-open-notes (keys)
"Open notes associated with the KEYS."
(interactive (list (bibtex-actions-read)))
(interactive (list (bibtex-actions-read :initial "has:note")))
(bibtex-completion-edit-notes keys))

;;;###autoload
Expand Down

0 comments on commit 2533b71

Please sign in to comment.