zsh-users / zsh-autosuggestions

Fish-like autosuggestions for zsh
MIT License
30.33k stars 1.85k forks source link

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

Closed olets closed 1 year ago

olets commented 1 year ago

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

Additional context

ericfreese commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

ericfreese commented 1 year ago

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 commented 1 year ago

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.