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
Show file tree
Hide file tree
Changes from 5 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
32 changes: 31 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ Here's how to configure it to use =all-the-icons=:
:group 'all-the-icons-faces)
#+END_SRC

*** History and Predefined searches
*** History and predefined searches
:PROPERTIES:
:CUSTOM_ID: history-and-predefined-searches
:END:

=Bibtex-actions= has functionality similar to the [[https://github.com/tmalsburg/helm-bibtex#p][predefined search]] functionality in =helm-bibtex= and =ivy-bibtex=, but with a different implementation.
Rather than create a new command with the search terms as argument, you just set the =bibtex-actions-presets= variable, and add the strings you want to access:
Expand All @@ -171,6 +174,33 @@ You then have two ways to access these strings from the completion prompt:
=Bibtex-actions= also preserves the history of your selections (see caveat below about multiple candidate selection though), which are also accessible in your completion UI, but by using =M-p=.
You can save this history across sessions by adding =bibtex-actions-history= to =savehist-additional-variables=.

*** Pre-filtering entries
:PROPERTIES:
:CUSTOM_ID: prefiltering-entries
:END:

By default, =bibtex-actions= will, assuming you are using =orderless= or =prescient= to filter candidates, pre-filter entries for the following commands.

1. =bibtex-actions-open=: pre-narrows the list to those which have associated pdf or links
2. =bibtex-actions-open-link=: pre-narrows the list to those which have associated links
3. =bibtex-actions-open-pdf=: -pre-narrows the list to those which have associated pdf(s)

That is, upon running the command, an =initial-input= value will be inserted to narrow the results.
You can also delete that if you prefer to see the full list of candidates.

By default, pre-filtering of =bibtex-actions-open-notes= is off, because the command by default will create a new note if none is available, and therefore it makes sense to have access to your full library.
But you can customize this to pre-filter if you prefer.

If you want to modify those values, or remove them entirely, you can set =bibtex-actions-initial-inputs= like so; in this case turning off pre-filtering for =bibtex-actions-open-pdf=:

#+begin_src elisp
(setq bibtex-actions-initial-inputs
'((pdf . nil)
(note . nil)
(link . "has:link")
(source . "has:link\\|has:pdf"))
#+end_src

*** Refreshing the library display
:PROPERTIES:
:CUSTOM_ID: refreshing-the-library-display
Expand Down
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