Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

widgets can only be called when ZLE is active

I have been dealing with this problem for almost a month now, and I feel frustrated, Any help would be greatly appreciated.

I am trying to write a widget for my takenote command. The purpose of the widget is to feed all the markdown files in ~/notes folder into fzf so that the user can select one of them and starts editing it. After the user types takenote and presses <tab> I expect the widget to run.

Here is the _takenote.zsh widget definition:

#compdef takenote
local file=$( find -L "$HOME/notes/" -print 2> /dev/null | fzf-tmux +m )
zle reset-prompt
compadd $file
return 1

Unfortunately, the above code doesn't work because of zle reset-prompt, if I remove it then the result would be like this:

before selection

And after selecting the file it would turn into:

After selecting the file

Which as you see will corrupt the prompt and the command itself. It appears to me that what I need to do is do a zle reset-prompt before calling compadd but this can only work when I bind the function to a key otherwise, I will get the following error:

widgets can only be called when ZLE is active

like image 761
ExistMe Avatar asked Jan 02 '18 04:01

ExistMe


People also ask

What is ZLE in zsh?

The Zsh Line Editor (ZLE) is the command prompt where you can write and edit your commands. The main keymap is the set of keystrokes which is loaded by default when Zsh is launched. The global keymap is the one used to edit commands in Zsh.

What is ZLE?

Zero-latency enterprise (ZLE) is any strategy that exploits the immediate exchange of information across technical and organizational boundaries to achieve business benefit. For example, technical boundaries exist between different operating systems, database management systems and programming languages.


1 Answers

I finally found a workaround for the issue. Although I am not satisfied with the strategy since it is not self contained in the widget itself, but it works. The solution involves trapping fzf-completion after it is invoked and calling zle reset-prompt.

For registering the trap add the following snippet to your .zshrc file (see Zsh menu completion causes problems after zle reset-prompt ):

TMOUT=1
TRAPALRM() {
   if [[ "$WIDGET" =~ ^(complete-word|fzf-completion)$ ]]; then
      # limit the reset-prompt functionality to the `takenote` script
      if [[ "$LBUFFER" == "takenote "* ]]; then
         zle reset-prompt
      fi
   fi
}

The _takenote widget:

#compdef takenote
local file=$( find -L "$HOME/notes/" -print 2> /dev/null | fzf-tmux +m )
compadd $file
return 0

p.s: I would still love to move the trap inside the widget, and avoid registering it in the init script (.zshrc)

like image 75
ExistMe Avatar answered Oct 11 '22 16:10

ExistMe