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

Customize initial input #130

Merged
merged 6 commits into from
May 11, 2021
Merged
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
44 changes: 37 additions & 7 deletions bibtex-actions.el
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ manager like Zotero or JabRef."
:group 'bibtex-actions
:type '(repeat function))

(defcustom bibtex-actions-initial-inputs
'((pdf . "has:pdf")
(note . "has:note")
bdarcus marked this conversation as resolved.
Show resolved Hide resolved
(link . "has:link")
(source . "has:link\\|has:pdf"))

This comment was marked as off-topic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Space is added in bibtex-actions-read, which see.

As to the use case, I thought the idea is to just make it customizable anyways. It's Emacs, after all. 😄

For me, at least, there is actually a good use case: I use bibtex-action-open-notes with the fallback mechanism provided by bibtex-completion, which creates a note file if there is none. So the initial input is useless, since for me, bibtex-action-open-notes is supposed to open or create a note with one command (which is very handy, btw). Another use case would be the ability to define what "source" actually means (as discussed in #121 ).

But TBH, I cannot imagine many more use cases right now. 🤷 So it's up to the maintainer!

Copy link
Contributor

@bdarcus bdarcus May 9, 2021

Choose a reason for hiding this comment

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

Space is added in bibtex-actions-read, which see.

Oops! I just hid the comment then.

As to the use case, I thought the idea is to just make it customizable anyways. It's Emacs, after all.

Well, no; customization for the sake of it would be overkill.

But when there's need, that's another matter.

For me, at least, there is actually a good use case: I use bibtex-action-open-notes with the fallback mechanism provided by bibtex-completion, which creates a note file if there is none.

Ooh, that's actually one I care about too; I totally forgot about that!

This does raise a larger design question, which is what @apc and I were going back-and-forth on in #118.

We settled on the initial-input approach, because the goal was filtering, but to make it easy for the user to effectively turn off or modify the filtering mid-stream; in this case to delete the initial-input.

This is not only because of cases like this, but also because with embark, you may open your library with one action in mind, but change your mind, and so need access to a wider list of candidates.

So the big question: did we make the right decision? And if yes, is this the right addition to this?

I'm inclined to say yes on both counts, but curious what you and @apc think.

So the initial input is useless, since for me, bibtex-action-open-notes is supposed to open or create a note with one command (which is very handy, btw). Another use case would be the ability to define what "source" actually means (as discussed in #121 ).

But TBH, I cannot imagine many more use cases right now. So it's up to the maintainer!

No, that's a good enough reason, per above.

"Alist defining the initial input for some bibtex open actions.
Given a flexible completion style, this will restrict the list of
available candidates to those matching the initial input.

The association key can be one of the symbols `pdf', `note',
`link' or `source' and defines the input for the function
`bibtex-action-open-pdf', `bibtex-action-open-link', etc. The
associated value must be nil, meaning that there will be no
initial input, or a string.

To match entries with certain properties (e.g. files attached),
you can use the following initial inputs: \"has:pdf\",
\"has:link\" and \"has:note\"."
:group 'bibtex-actions
:type '(alist :key-type symbol
:value-type (choice string
(const :tag "No initial input" nil))))

;;; History, including future history list.

(defvar bibtex-actions-history nil
Expand Down Expand Up @@ -133,13 +156,15 @@ manager like Zotero or JabRef."
This provides a wrapper around 'completing-read-multiple', with
the following optional arguments:

'INITIAL' provides the initial value, for pre-filtering the
candidate list
If 'INITIAL' matches one of the keys defined in
`bibtex-actions-initial-inputs', use the associated initial
input.

'REBUILD-CACHE' if t, forces rebuilding the cache before
offering the selection candidates"
(let* ((crm-separator "\\s-*&\\s-*")
(candidates (bibtex-actions--get-candidates rebuild-cache))
(initial-input (assoc-default initial bibtex-actions-initial-inputs))
(chosen
(completing-read-multiple
"BibTeX entries: "
Expand All @@ -149,7 +174,12 @@ offering the selection candidates"
(affixation-function . bibtex-actions--affixation)
(category . bibtex))
(complete-with-action action candidates string predicate)))
nil nil initial 'bibtex-actions-history bibtex-actions-presets nil)))
nil
nil
(and initial-input
(stringp initial-input)
(concat initial-input " "))
'bibtex-actions-history bibtex-actions-presets nil)))
(cl-loop for choice in chosen
;; Collect citation keys of selected candidate(s).
collect (cdr (assoc choice candidates)))))
Expand Down Expand Up @@ -330,7 +360,7 @@ 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.
With prefix, rebuild the cache before offering candidates."
(interactive (list (bibtex-actions-read :initial "has:link\\|has:pdf "
(interactive (list (bibtex-actions-read :initial 'source
:rebuild-cache current-prefix-arg)))
(bibtex-completion-open-any keys))

Expand All @@ -340,15 +370,15 @@ With prefix, rebuild the cache before offering candidates."
If multiple PDFs are found, ask for the one to open using
‘completing-read’.
With prefix, rebuild the cache before offering candidates."
(interactive (list (bibtex-actions-read :initial "has:pdf "
(interactive (list (bibtex-actions-read :initial 'pdf
:rebuild-cache current-prefix-arg)))
(bibtex-completion-open-pdf keys))

;;;###autoload
(defun bibtex-actions-open-link (keys)
"Open URL or DOI link associated with the KEYS in a browser.
With prefix, rebuild the cache before offering candidates."
(interactive (list (bibtex-actions-read :initial "has:link "
(interactive (list (bibtex-actions-read :initial 'link
:rebuild-cache current-prefix-arg)))
(bibtex-completion-open-url-or-doi keys))

Expand Down Expand Up @@ -391,7 +421,7 @@ With prefix, rebuild the cache before offering candidates."
(defun bibtex-actions-open-notes (keys)
"Open notes associated with the KEYS.
With prefix, rebuild the cache before offering candidates."
(interactive (list (bibtex-actions-read :initial "has:note "
(interactive (list (bibtex-actions-read :initial 'note
:rebuild-cache current-prefix-arg)))
(bibtex-completion-edit-notes keys))

Expand Down