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

zle autosuggest-clear and _zsh_autosuggest_clear not clearing suggestions when called from a widget #716

Closed
olets opened this issue Dec 16, 2022 · 5 comments
Labels

Comments

@olets
Copy link

olets commented Dec 16, 2022

Describe the bug

I can't clear suggestions from custom widgets.

To Reproduce

Steps to reproduce the behavior:

# setup
% zsh -df
% source path/to/zsh-autosuggestions.zsh
% my_widget() {}
% zle -N my_widget
% bindkey '^[[B' my_widget # bind to down arrow

# zle autosuggest-clear example
% my_widget() { zle autosuggest-clear }
% s # `source path/to/zsh-autosuggestions.zsh` is suggested
# hit down arrow. no change. expected suggestion to be cleared

# _zsh_autosuggest_clear example
% my_widget() { _zsh_autosuggest_clear }
% s # `source path/to/zsh-autosuggestions.zsh` is suggested
# hit down arrow. no change. expected suggestion to be cleared

Expected behavior

Widgets can clear suggestions.

Screenshots

Desktop

  • OS + distribution: macOS 12.6.1 (21G217)
  • Zsh version: 5.9
  • Plugin version: 0.7.0

Additional context

@ericfreese
Copy link
Member

It looks like you have a small typo my_wigdet

% my_widget() {}
% bindkey '^[[B' my_wigdet # bind to down arrow

Does that fix it?

@olets
Copy link
Author

olets commented Dec 16, 2022

Thanks for the quick response!

Good catch but that typo was in the issue writeup only. Also missed creating the keymap. And the example could be more minimal, ls isn't necessary. Edited.

I've found the— intended steps? bug workaround?: add my widget to ZSH_AUTOSUGGEST_IGNORE_WIDGETS (before creating the keymap).

# setup
% zsh -df
% source path/to/zsh-autosuggestions.zsh
% my_widget() {}
% typeset -ga ZSH_AUTOSUGGEST_IGNORE_WIDGETS # new
% ZSH_AUTOSUGGEST_IGNORE_WIDGETS+=(my_widget) # new
% zle -N my_widget
% bindkey '^[[B' my_widget # bind to down arrow

# zle autosuggest-clear example
% my_widget() { zle autosuggest-clear }
% s # `source path/to/zsh-autosuggestions.zsh` is suggested
# down arrow clears the suggestion

# _zsh_autosuggest_clear example
% my_widget() { _zsh_autosuggest_clear }
% s # `source path/to/zsh-autosuggestions.zsh` is suggested
# down arrow clears the suggestion

My own need is cleared up. Still seems like either a bug or something missing from the documentation. The README doesn't suggest that adding to ZSH_AUTOSUGGEST_IGNORE_WIDGETS will be necessary. Maybe the _zsh_autosuggest_bind_widget source suggests it, but it doesn't to my eye.

Happy to PR a clarification to https://github.com/zsh-users/zsh-autosuggestions#key-bindings if that'd be helpful.

@olets
Copy link
Author

olets commented Dec 19, 2022

I had time, so opened a PR with the expectation that you might simply close it.

@ericfreese
Copy link
Member

ericfreese commented Feb 17, 2023

One thing maybe worth noting is that the intended way to use those provided autosuggest-* widgets is by binding keys to them directly e.g. bindkey '^[[B' autosuggest-clear instead of my_widget() { zle autosuggest-clear }; zle -N my_widget; bindkey '^[[B' my_widget.

If you want a custom widget to clear a suggestion rather than have a suggestion fetched after it runs, you should actually add it to ZSH_AUTOSUGGEST_CLEAR_WIDGETS rather than manually calling zle autosuggest-clear and adding it to ZSH_AUTOSUGGEST_IGNORE_WIDGETS

❯ zsh -df
ericfreese% source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
ericfreese% my_widget() {}
ericfreese% zle -N my_widget
ericfreese% bindkey '^[[B' my_widget
ericfreese% ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(my_widget)
ericfreese% s # pressing down arrow clears the suggestion

There's some additional documentation about that here: https://github.com/zsh-users/zsh-autosuggestions#widget-mapping

@olets
Copy link
Author

olets commented Feb 18, 2023

Aha! Missed that. Thank you.

edit oh look at that, sorry for the noise. I'm not sure I've ever referenced a GitHub issue in a commit message body, didn't know it would generate activity.

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

No branches or pull requests

2 participants