Skip to content

Commit

Permalink
feat: Add filtering on open commands (#118)
Browse files Browse the repository at this point in the history
When running the "open" commands for links, PDFs, or notes, this adds 
an initial input to the completing-read interface, such that it 
pre-narrows the candidate list.

So the user will only see candidates where the open command will 
actually return a result.

But because it uses initial-input, the user can also clear this to see the 
full candidate list as well.
  • Loading branch information
bdarcus authored Apr 28, 2021
1 parent 743f548 commit d6882c2
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 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 (&optional &key initial)
"Read bibtex-completion entries with INITIAL option.
This provides a wrapper around 'completing-read-multiple', with
the following optional arguments:
':initial': provides the initial value, for pre-filtering the
candidate list"
(when-let ((crm-separator "\\s-*&\\s-*")
(candidates (bibtex-actions--get-candidates))
(chosen
Expand All @@ -116,7 +122,8 @@ 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 nil initial nil nil nil)))
(cl-loop for choice in chosen
;; Collect citation keys of selected candidate(s).
collect (cdr (assoc choice candidates)))))
Expand Down Expand Up @@ -279,21 +286,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 +336,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 d6882c2

Please sign in to comment.