How to execute widgets from inside functions? #183

Open
OmeGak opened this Issue Jul 29, 2016 · 3 comments

2 participants

@OmeGak

I'm trying to execute autosuggest-clear and autosuggest-accept from inside a function. This is my most basic case:

foo() {
  # ... other code
  zle autosuggest-clear
}
zle -N foo
bindkey '^K' foo

foo gets executed on Ctrl + K, but zle autosuggest-clear does nothing. Am I missing something?

@ericfreese
zsh-users member
ericfreese commented Jul 29, 2016 edited

Ah, yeah this is kind of confusing behavior, but I think it has to do with the fact that any un-mapped widget (those not included in any of these configured lists) currently triggers the fetching and display of a new suggestion after running.

From the readme:

Widgets not in any of these lists will update the suggestion when invoked.

So your foo widget is wrapped by z-asug, and when invoked with ^K: (1) clears the suggestion via zle autosuggest-clear, and then (2) the modification behavior is triggered, which fetches a new suggestion. That new suggestion will likely be the same as the last, so it appears that the suggestion was not cleared at all.

As a workaround for now, instead of calling zle autosuggest-clear from your widget, I'd suggest adding the foo widget to the list of widgets that clear the autosuggestion:

% function foo() { ... }
% zle -N foo
% bindkey '^K' foo
% ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(foo)

A potential fix to this problem may be to change the logic of the modify widget to only fetch and display a new suggestion if the wrapped widget actually changes the $BUFFER content.

This fix would amend the above statement from the readme to something like:

Widgets not in any of these lists will update the suggestion if they modify the buffer when invoked.

@ericfreese
zsh-users member

The fixes/only_fetch_if_buffer_changed branch implements the fix described at the bottom of my last comment. Try it out and let me know what you think.

@OmeGak

Seems to be working for me. Thanks!

@ericfreese ericfreese referenced this issue Aug 1, 2016
Open

v0.3.3 #169

5 of 9 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment