Autosuggestions do not work with zsh-syntax-highlighting #6

Closed
ZyX-I opened this Issue Nov 5, 2013 · 12 comments

6 participants

@ZyX-I

It is somewhat expected since both plugins are using the same zle widgets. I guess you need to cooperate and work out some standard that will allow using both plugins.

Fish has both completion and highlighting and it looks nice. I do not like much more limited capabilities of it though.

@tarruda

I have added some integration code, can you test it again to verify?

@ZyX-I

It seems to work now, but with a small problem: after line is run it is no longer highlighted.

I.e. with zsh-autosuggestnions it looks like this:

fg not highligted

Without:

fg highligted

@ZyX-I

Also when I move the cursor with forward-char or vi-add-eol part that was (but after move is not) highlighted in gray is still highlighted in gray.

@tarruda

Ok, those two cases should be fixed now. Can you update and try again?

@ZyX-I

Thanks, this works perfect now.

@ZyX-I ZyX-I closed this Nov 9, 2013
@ZyX-I

No. It appears that there are other widgets for moving around that you did not cover.
ZLE_AUTOSUGGEST_ACCEPT_WIDGETS should probably also contain end-of-line, vi-end-of-line, vi-forward-blank-word, vi-forward-blank-word-end, vi-find-next-char, vi-find-next-char-skip, emacs-forward-word, vi-forward-word-end, vi-goto-column, vi-goto-mark, vi-goto-mark-line, vi-repeat-find, vi-rev-repeat-find. I have listed here all widgets that seem to be (not sure about marks, otherwise that “are” and not “seem to be”) able to move cursor forward.

But if I add all these widgets with

diff --git a/autosuggestions.zsh b/autosuggestions.zsh
index f650486..1ece5ba 100644
--- a/autosuggestions.zsh
+++ b/autosuggestions.zsh
@@ -37,7 +37,10 @@ accept-and-menu-complete

 ZLE_AUTOSUGGEST_ACCEPT_WIDGETS=(
 vi-forward-char forward-char vi-forward-word forward-word vi-add-eol
-vi-add-next vi-forward-blank-word
+vi-add-next vi-forward-blank-word end-of-line vi-end-of-line
+vi-forward-blank-word vi-forward-blank-word-end vi-find-next-char
+vi-find-next-char-skip emacs-forward-word vi-forward-word-end vi-goto-column
+vi-goto-mark vi-goto-mark-line vi-repeat-find vi-rev-repeat-find
 )

 autosuggest-pause() {
@@ -52,7 +55,7 @@ autosuggest-pause() {
    zle -A autosuggest-accept-line-orig accept-line
    for widget in $ZLE_AUTOSUGGEST_ACCEPT_WIDGETS $ZLE_AUTOSUGGEST_SUSPEND_WIDGETS $ZLE_AUTOSUGGEST_COMPLETION_WIDGETS; do
        [[ -z $widgets[$widget] || -z $widgets[autosuggest-${widget}-orig] ]] &&\
-           continue
+           continue
        eval "zle -A autosuggest-${widget}-orig ${widget}"
    done
    autosuggest-highlight-suggested-text

I have a problem with custom vi-ctrl-o widget (I get “No such widget `autosuggest-vi-ctrl-o-orig'”) (I move cursor with a vi-ctrl-o widget that invokes whatever widget is bound to the next key in vicmd keymap). Interesting that zsh-syntax-highlighting backups this widget (I guess it just hooks every widget). My widget looks like this:

function _-vi-digit-arg ()
{
    emulate -L zsh
    [[ -z "${NUMERIC}" ]] && NUMERIC=0 
    [[ -z "$1" ]] && num=0  || num=$(( $1 )) 
    shift
    (( NUMERIC=NUMERIC*10+num )) && $@
}

function _-vi-ctrl-o()
{
    emulate -L zsh
    local keystr
    read -k keystr
    local -r keystr
    local -ri key=$(( #keystr ))
      if (( key==##A )) ; then zle end-of-line
    elif (( key==##$ )) ; then zle end-of-line
    elif (( key==##I )) ; then zle vi-first-non-blank
    elif (( key==##d )) ; then _-vi-delete
    elif (( key==##0 )) && [[ -z $NUMERIC ]] ;
                          then zle beginning-of-line
    elif (( key>=##0 && key<=##9 ))
                          then _-vi-digit-arg $(( key - ##0 )) _-vi-ctrl-o
    elif (( key==##s )) ; then zle perlsubstitute
    elif (( key==##= )) ; then zle tailfor
    else
        zle ${${(z)$(bindkey -M vicmd $keystr)}[2]}
    fi
}
@ZyX-I ZyX-I reopened this Nov 9, 2013
@tarruda

Yes, I've read zsh-syntax-highlighting source code and it binds to all widgets, even user-defined one. Unfortunately I cant do that because it would break a lot of editing behavior. Eg: go in normal mode and edit a previous character would 'unmaterialize' whatever is to the right of the cursor(accept-line would kill that part unless the cursor was explicitly moved to the end of the line).

The best solution I found to cover most cases is simple:If any of the widgets in ZLE_AUTOSUGGEST_SUSPEND_WIDGETS are executed, autosuggestions is temporarily disabled until a character is inserted at the end of the line.

Clearly we need a more generic/better logic to decide when pausing/resuming autosuggestions should happen. I'm gonna try to come up with something this week, but if you have ideas I'm all ears :)

@sandikata

hello, how can fix this issue for me ?

roko__@CLDX [ 18:36:26 ] [ 08/07/14 ] [ pts/3 ] ~ % nano .zshrc

No such widget `.autosuggest-forward-char-orig'

@randomize randomize referenced this issue in zsh-users/zsh-syntax-highlighting Mar 26, 2015
Closed

Play well with zsh-autosuggestions or regression in 7edd081 #164

@balta2ar

@tarruda I'm experiencing the same problem :( Have to live on detached HEAD because up-to-date versions of zsh-autosuggestions and zsh-syntax-highlighting don't play well together. Is there any chance you could look into this problem once again, please?

@tarruda

Is there any chance you could look into this problem once again, please?

Not in the foreseeable future, sorry.

@faceleg

@balta2ar we need someone with time & zsh experience to refactor some of this 🎱

@faceleg faceleg closed this in #55 May 16, 2015
@santiycr

Just in case others are struggling with this one while using prezto. If when you submit commands with active auto-suggestions, they loose their highlighting and get confusing with the actual command that was run:
2016-05-19_16-53-36

(notice, I had typed and submitted git a, while the rest was grayed out before submitting)

The following syntax-highlighting cleared the issue for me:
https://github.com/sorin-ionescu/prezto/tree/master/modules/syntax-highlighting#highlighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment