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

Add groups to citar-open functions #491

Closed
wants to merge 8 commits into from

Conversation

localauthor
Copy link
Contributor

@localauthor localauthor commented Dec 17, 2021

Changes:

  • Adds completing-read to citar--open-note, for when multiple notes exist

  • Changes citar-select-files to citar-select-resources, and calls the new function in both citar-open and citar-open-library-files

  • Adds group-function, citar-select-group-related-resources, to citar-select-resources, to group candidates by type

The result of calling citar-open on an entry with associated files, notes, and a link (the following shows vertico with the consult-completing-read-multiple interface):
citar-open file-link-note

Selecting multiple file types, a note and a file:
citar-open file-link-note selected

Todo:
- Somehow set proper category metadata for links in citar-select-resources. I'm actually not sure if this is possible.
- Note that prior to this PR, citar-open did not add any metadata to candidates, so embark-act'ing on a file candidate did not make embark file functions available. Now embark file functions are available, but on both files and links. Neither case seems optimal, if it's not possible (or easy) to set category metadata by group.

Question:
- Does this work as expected with embark-act-all? I've not used that enough to properly test it.

@localauthor localauthor force-pushed the refine-file-prompts branch 2 times, most recently from fc4fcde to fc1759a Compare December 17, 2021 08:45
citar.el Outdated Show resolved Hide resolved
@bdarcus bdarcus mentioned this pull request Dec 19, 2021
@bdarcus bdarcus changed the base branch from main to no-crm December 20, 2021 12:55
@bdarcus
Copy link
Contributor

bdarcus commented Dec 20, 2021

@localauthor - I set this to merge to the no-crm branch, but before doing that, I'll put comments here:

  1. I think the "Files" group maybe should be "LIbrary files"? WDYT?
  2. I realize this when I was playing with this idea earlier: links have no annotations. Should they? If yes, what should they be? (we can worry about this later; just wondering)
  3. What should happen to the existing more specific "open" commands? I guess we still need citar-open-notes at least?

Let me know your thoughts, and if you want to make any changes here before I merge?

Also, note: this now conflicts with the new base branch. You'll need to rebase and maybe fix a conflict or two.

@bdarcus
Copy link
Contributor

bdarcus commented Dec 20, 2021

Hmm ... actually, I wonder: do you think we should merge this to #496? Or should we just evaluate this on its own, and if we want to merge it, then do so, and I can rebase #496 on this, which I can refocus just on citar-insert-citation?

Really the CR vs CRM discussion varies depending on the command in question.

@bdarcus bdarcus changed the base branch from no-crm to main December 20, 2021 14:24
@bdarcus bdarcus marked this pull request as ready for review December 20, 2021 14:25
@bdarcus
Copy link
Contributor

bdarcus commented Dec 20, 2021

Does this work as expected with embark-act-all? I've not used that enough to properly test it.

I am just starting to test this branch. Here's what you see after running embark-act-all, and then embark-act:

image

Two things:

  1. I guess notes should also have a file category, but remain a separate group? If yes, not sure how to do that.
  2. I'm actually not sure how one would then open all of these results from there using embark-act-all, and running RET on a single candidate generates an error.

Any thoughts on the above @minad?

@localauthor
Copy link
Contributor Author

Ok, I just rebased onto main.

I changed citar--open-notes to use citar-select-resources, so now notes are properly categorized as files and grouped as notes.

As for the question of where to merge this (either to main directly or to no-crm) , I suppose it depends on your plans for no-crm. :)

This branch changes CRM to CR in all the open functions anyway.

- simplify 'citar-open'
- edit doc strings
@bdarcus
Copy link
Contributor

bdarcus commented Dec 20, 2021

Let's keep it here for now. There are still some subtleties with embark-act-all to work out. Per my point 2 above, I can't see how to run embark-act-all to open all of the resulting resources, because the default action is then embark-act. I wonder if that needs to be the case, or what a better alternative might be.

@bdarcus
Copy link
Contributor

bdarcus commented Dec 20, 2021

Per my point 2 above, I can't see how to run embark-act-all to open all of the resulting resources, because the default action is then embark-act. I wonder if that needs to be the case, or what a better alternative might be.

There does seem to be a bug here though. If I only select one candidate, and then do embark-act-all on the results, where the default action is citar-open, I get a "no results found" (not sure why).

Even if I only select one resource using completing-read, I get a "wrong type" error (it's expecting a list; probably because citar-open is included in embark-multitarget-actions, since if you remove it from there, it works fine).

citar.el Show resolved Hide resolved
citar.el Outdated Show resolved Hide resolved
citar.el Outdated Show resolved Hide resolved
and change 'citar-select-resources' to 'citar-select-resource'
@localauthor
Copy link
Contributor Author

All the open functions should be working as expected now (hopefully!), with the exception of embark-act-all, which I'm just starting to work through.

@bdarcus
Copy link
Contributor

bdarcus commented Dec 20, 2021

All the open functions should be working as expected now (hopefully!), with the exception of embark-act-all, which I'm just starting to work through.

When I use citar-open, I still get a type error when trying to open any resource.

- Let-bind 'embark-default-action-overrides' for each command

- Add 'citar-open-multi' function, so files are handled appropriately when type
is 'consult-multi'
@localauthor
Copy link
Contributor Author

localauthor commented Dec 21, 2021

Latest commit allows embark-act-all to act appropriately in all contexts.

Each open command now includes a let-binding for embark-default-action-overrides in order to override the default action depending on target type. Before, the default action was always the original command (ie, citar-open, citar-open-notes, etc.). Since those cannot act on files or urls, the default action is now set in each case to a function that can act on files or urls --- namely, citar-file-open for files, find-file for notes, and browse-url for urls.

The edge case here is with citar-open and the matter of acting at the same time on candidates of multiple types. When there are multiple types together, the candidates are not associated with their actual type (file or url n this case) but remain typed as consult-multi. So I've added a function citar-open-multi (which see) and included it in the embark-default-action-overrides list in the let-binding in citar-open. So now running embark-act-all on candidates of multiple types (urls and files) should open all of them as expected.

Hopefully it works for you as it does for me.

@localauthor
Copy link
Contributor Author

localauthor commented Dec 21, 2021

One issue with using consult-multi is that it requires embark-consult.el to be loaded and, specifically, embark-consult--multi-transform to be defined.

If only embark.el is loaded, these are not available.

 - changes 'browse-url-default-browser' to 'browse-url', which defaults to 'browse-url-default-browser' anyway, but might be set otherwise in user's config

- sets 'citar--open-note' to prompt for yes or no before creating a new note
@bdarcus
Copy link
Contributor

bdarcus commented Dec 21, 2021

One issue with using consult-multi is that it requires embark-consult.el to be loaded and, specifically, embark-consult--multi-transform to be defined.

If only embark.el is loaded, these are not available.

What if you define the variable? Do you get an error, or just not the behavior?

@localauthor
Copy link
Contributor Author

localauthor commented Dec 21, 2021

embark-consult--multi-transform is a function, so it really needs to loaded for everything to fully work.

I say "fully" because citar-open still functions reasonably without it, ie, there are no errors and files/links can be opened with "RET", according to type --- even when calling embark-act-all on multiple types.

To explain: without embark-consult--multi-transform, files and urls remain typed as consult-multi. They are therefore handled by the default action associated with that type, namley, the new citar-open-multi, which opens each type appropriately. There aren't file- or url-specific actions available, in each case, but at least there are no errors.

If, as @minad seemed to be suggesting here, consult-multi were changed to multi (or the like) and included in embark.el rather than embark-consult.el, this (very minor, almost non-) issue would be moot.

@bdarcus
Copy link
Contributor

bdarcus commented Dec 21, 2021

Each open command now includes a let-binding for embark-default-action-overrides in order to override the default action depending on target type.

These result in "unused lexical variable" warnings.

On a separate matter, it's getting closer. But if I have this:

image

... and then do embark-act-all, I get this:

image

@minad
Copy link
Contributor

minad commented Dec 21, 2021

@localauthor Yes of course. We should switch to multi from consult-multi to make this general and to avoid the loading issue. But I will wait for a few weeks to let this settle here, since Citar does not yet seem to have converged fully. Then we can change it across the different packages and tag new releases. Probably embark and marginalia should support both multi and consult-multi for some time and treat them as aliases.

@localauthor
Copy link
Contributor Author

localauthor commented Dec 21, 2021

... and then do embark-act-all, I get this:

Before pressing "RET", what does the embark-indicator-menu look like for you? That is: what is the default action you are calling when do embark-act-all? If it is citar-open, then I would expect to see exactly what your screenshot shows (which is a symptom of another issue I'm trying to figure out, about which more soon).

Here is what I see before pressing "RET" in a similar series of steps:
Screen Shot 2021-12-21 at 15 33 20

Followed by this:
Screen Shot 2021-12-21 at 15 37 00

Followed by opened files, notes, and browser.

@localauthor
Copy link
Contributor Author

These result in "unused lexical variable" warnings.

I'm afraid I don't know how to avoid such warnings, or what they mean really.

@bdarcus
Copy link
Contributor

bdarcus commented Dec 21, 2021

Followed by opened files, notes, and browser.

I see the same, except for this last step.

Maybe there's something wrong with my install of the branch. Running eval-buffer on citar.el does result in the correct behavior!

@bdarcus
Copy link
Contributor

bdarcus commented Dec 21, 2021

These result in "unused lexical variable" warnings.

I'm afraid I don't know how to avoid such warnings, or what they mean really.

@minad - any tips? He's let-binding embark-default-action-overrides per #491 (comment).

@minad
Copy link
Contributor

minad commented Dec 21, 2021

Oh. These default action overrides should not be necessary. The default file and url actions should work ootb as should all the other actions. If not something else is wrong and should be fixed.

The correct approach is to define your own category if you want to overwrite or define your own actions.

@minad
Copy link
Contributor

minad commented Dec 21, 2021

Ah I see, you have special open functions. I guess in this case the override is justified. But overriding browse-url for url and the consult-multi override should not be needed.

Probably you miss a defvar to fix the lexical error?

@bdarcus
Copy link
Contributor

bdarcus commented Dec 21, 2021

Probably you miss a defvar to fix the lexical error?

Yes; I just committed that change.

(message "No associated resources"))))

(defun citar-open-multi (selection)
Copy link
Contributor

@minad minad Dec 21, 2021

Choose a reason for hiding this comment

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

This command is unnecessary and should be removed. Embark already dispatches consult-multi to file and url. That's the purpose of consult-multi and the corresponding Embark candidate transformer.

(let* ((key-entry-alist (citar--ensure-entries keys-entries))
(let* ((embark-default-action-overrides '((consult-multi . citar-open-multi)
(file . citar-file-open)
(url . browse-url)))
Copy link
Contributor

@minad minad Dec 21, 2021

Choose a reason for hiding this comment

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

Here the consult-multi override should go away and the url override probably too, in order to allow the user to customize the action on the side of Embark.

Copy link
Contributor Author

@localauthor localauthor Dec 21, 2021

Choose a reason for hiding this comment

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

Thanks for looking at this, and for the comments.

Embark and consult-multi work just as you describe when it comes to categorizing files and urls, but (as I describe here) because the source of the target is minibuffer completion, Embark sets the default action for all targets, regardless of type, to the command that opened the minibuffer in the first place (per the docstring of embark-default-action-overrides). That means that, in this case, the default action for any target type is set to citar open, which is a function that does not take files or urls as arguments. Hence the overrides. It's a bit of a hail mary, I guess, but I'm afraid I'm running up against the limits of my lisp/emacs knowledge here. ;)

As for the citar-open-multi function, it was only added to allow embark-act-all to work on properly on targets of multiple types. When using embark-act on single candidates, consult-multi works just as expected and this function isn't called. When calling embark-act-all on targets of multiple types, they haven't been transformed by the candidate transformer yet --- so they remain typed as consult-multi. Hence, another override. Again, bit of a hail mary -- but functional!

Thanks again for your comments. Really appreciate your taking the time.

Copy link
Contributor

@minad minad Dec 21, 2021

Choose a reason for hiding this comment

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

Okay, maybe I missed something here but this seems wrong. The default action is supposed to be the command that started the completion. Overriding it with something else is counterintuitive and goes against the usual Embark behavior.

Note that Embark has a minibuffer argument injection mechanism which works even if functions don't take arguments. I suggest you dig deeper into the Embark documentation.

Copy link
Contributor

@minad minad Dec 21, 2021

Choose a reason for hiding this comment

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

That means that, in this case, the default action for any target type is set to citar open, which is a function that does not take files or urls as arguments. Hence the overrides.

I am specifically referring to this. This is wrong. There is the minibuffer injection mechanism.

When calling embark-act-all on targets of multiple types, they haven't been transformed by the candidate transformer yet --- so they remain typed as consult-multi.

I will check this, but if this is the case it is a bug or issue in Embark.

I know that oantolin added some target type checking to embark-act-all and there is indeed a problem when acting on candidates of multiple categories at once. Anyways this should be handled properly by Embark itself.

EDIT: Okay, I checked Embark. Embark is doing everything alright. When you act on candidates of multiple categories at once then indeed the candidates are not transformed. Then the candidates remain consult-multi. They are only transformed if you act only on files, only on buffers or only on urls. But still, it should not be necessary to override the default action since the command itself should then be capable of handling these consult-multi candidates. You can look into consult--multi and consult-buffer as examples. There I also don't overwrite the embark default actions and everything works as expected :)

Copy link
Contributor

@bdarcus bdarcus Dec 23, 2021

Choose a reason for hiding this comment

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

I experimented a bit with removing these lines, and things seem to work fine, until I run embark-act-all on the resulting resources. So to recap:

  1. citar-open
  2. narrow candidates and then embark-act-all
  3. from there I get a list of resources, if I run embark-act-all, I then get the error below:
embark PCH: (user-error "No target found")

... which is I think why he introduced the citar-open-multi function.

image

Copy link
Contributor

@minad minad Dec 23, 2021

Choose a reason for hiding this comment

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

Did you load embark-consult? I don't know what is wrong here exactly, but the override should not be needed due to the design of embark. consult-buffer also works without override.

Copy link
Contributor

@bdarcus bdarcus Dec 23, 2021

Choose a reason for hiding this comment

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

Did you load embark-consult?

I did require it now, and get:

(execute-extended-command nil "citar-open" nil)

I don't know what is wrong here exactly, but the override should not be needed due to the design of embark. consult-buffer also works without override.

I'll take another look.

Copy link
Contributor

Choose a reason for hiding this comment

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

@bdarcus What's the status here? Did you figure out the issues?

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you figure out the issues?

Not yet. Likely in the next few days.

@minad
Copy link
Contributor

minad commented Dec 27, 2021

@bdarcus As a small demonstrator, this works for me:

(require 'embark-consult)
(defun open-example ()
  (interactive)
  (let* ((candidates
          (list (propertize "init.el" 'consult-multi
                            '(file . "~/.config/emacs/init.el"))
                (propertize "emacs.org" 'consult-multi
                            '(url . "https://www.gnu.org/software/emacs/"))))
         (result (completing-read "Open: "
                                  (lambda (str pred action)
                                    (if (eq action 'metadata)
                                        '(metadata (category . consult-multi))
                                      (complete-with-action action candidates str pred)))
                                  nil t)))
    (pcase (get-text-property 0 'consult-multi (car (member result candidates)))
      (`(url . ,url) (eww url))
      (`(file . ,file) (find-file file)))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants