Trigger accept on `l` in normal mode? #137

Closed
euclio opened this Issue Mar 14, 2016 · 10 comments

3 participants

@euclio

In fish, if I hit l (move right) at the end of the buffer while a completion is visible, it accepts the entire completion. I am able to get zsh-autosuggestions to complete when using the right arrow key, but it doesn't work with using l at the end of the buffer.

Is it possible to replicate this behavior?

@ericfreese
zsh-users member

vi-forward-char should be bound to autosuggest-accept by default.

https://github.com/zsh-users/zsh-autosuggestions/blob/master/src/config.zsh#L31

@PythonNut

Yes, but for some reason, it doesn't seem to be accepting the suggestion. Could it be because zsh doesn't consider the CURSOR to be at the EOL in vicmd?

@ericfreese
zsh-users member

Yeah, that's definitely the problem. I think this should fix it:

diff --git a/src/widgets.zsh b/src/widgets.zsh
index 3c15788..246283f 100644
--- a/src/widgets.zsh
+++ b/src/widgets.zsh
@@ -30,23 +30,27 @@ _zsh_autosuggest_modify() {
        fi
 }

 # Accept the entire suggestion
 _zsh_autosuggest_accept() {
-       # Only accept if the cursor is at the end of the buffer
-       if [ $CURSOR -eq $#BUFFER ]; then
+       # Save location of the cursor so we can compare later
+       local original_cursor=$CURSOR
+
+       # Original widget moves the cursor
+       _zsh_autosuggest_invoke_original_widget $@
+
+       # Only accept if the cursor has not moved (is at the end of the buffer)
+       if [ $CURSOR -eq $original_cursor ]; then
                # Add the suggestion to the buffer
                BUFFER="$BUFFER$POSTDISPLAY"

                # Remove the suggestion
                unset POSTDISPLAY

                # Move the cursor to the end of the buffer
                CURSOR=${#BUFFER}
        fi
-
-       _zsh_autosuggest_invoke_original_widget $@
 }

 # Accept the entire suggestion and execute it
 _zsh_autosuggest_execute() {
        # Add the suggestion to the buffer
@ericfreese ericfreese added a commit that referenced this issue Mar 15, 2016
@ericfreese ericfreese Fix #137 4394e0d
@ericfreese
zsh-users member
@ericfreese
zsh-users member

Meh... I'm actually not sure how much I like this solution. Is there a good way of checking if the user is currently in vim mode?

@PythonNut

@ericfreese the best way I know is to check if $KEYMAP == vicmd.

@ericfreese
zsh-users member

It looks like $KEYMAP is only available inside zle-keymap-select, called when the keymap changes.

But it looks like we can get what we need from the bindkey command.

‘bindkey -lL main’ shows which keymap is linked to ‘main’, if any, and hence if the standard emacs or vi emulation is in effect.

http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Builtins

% bindkey -v
% bindkey -lL main
bindkey -A viins main
% bindkey -e
% bindkey -lL main
bindkey -A emacs main

Looks like we should be able to see if vicmd is enabled, and change the logic accordingly.

@ericfreese
zsh-users member

Actually it does look like $KEYMAP is available in any zle widget, but it is always set to main, even after setting vi mode with bindkey -v.

@ericfreese
zsh-users member

Ah! When you're actually in vicmd (not viins), $KEYMAP is actually set to vicmd instead of main. Should be able to use that.

@ericfreese ericfreese added a commit that referenced this issue Mar 15, 2016
@ericfreese ericfreese Fix #137 15c5db8
@ericfreese
zsh-users member

Should be fixed with v0.3.1

@ericfreese ericfreese closed this Mar 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment