Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ZyX-I opened this issue Nov 5, 2013 · 16 comments · Fixed by #55
Closed

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

ZyX-I opened this issue Nov 5, 2013 · 16 comments · Fixed by #55

Comments

@ZyX-I
Copy link

ZyX-I commented Nov 5, 2013

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
Copy link
Contributor

tarruda commented Nov 7, 2013

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

@ZyX-I
Copy link
Author

ZyX-I commented Nov 8, 2013

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
Copy link
Author

ZyX-I commented Nov 8, 2013

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
Copy link
Contributor

tarruda commented Nov 8, 2013

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

@ZyX-I
Copy link
Author

ZyX-I commented Nov 9, 2013

Thanks, this works perfect now.

@ZyX-I ZyX-I closed this as completed Nov 9, 2013
@ZyX-I
Copy link
Author

ZyX-I commented Nov 9, 2013

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-owidget that invokes whatever widget is bound to the next key invicmd` 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
Copy link
Contributor

tarruda commented Nov 11, 2013

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
Copy link

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'

@balta2ar
Copy link

@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
Copy link
Contributor

tarruda commented Mar 26, 2015

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

Not in the foreseeable future, sorry.

@faceleg
Copy link
Contributor

faceleg commented Mar 26, 2015

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

@santiycr
Copy link

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

@iamsandesh
Copy link

I was struggling with this issue, then I loaded the 'autosuggestions' module after the 'syntax-highlighting' module then both the modules seems to be working fine.

I also enabled the coloring for 'autosuggestions' by uncommenting the below line
zstyle ':prezto:module:autosuggestions:color' found 'blue'

@schlichtanders
Copy link

schlichtanders commented Feb 26, 2017

@iamsandesh that sounds really good! can you explain where to find this zstyle line?
(I am using oh-my-zsh, maybe it is unfortunately not applicable to my case)

@iamsandesh
Copy link

iamsandesh commented Feb 27, 2017

@schlichtanders I am updating it in the "~/.zprezto/runcoms/.zpreztorc" file which is present in $HOME folder.

The line is already present in the file but commented out, so the line needs to be uncommented and the color has to be specified in that line as it is not already specified.

@psprint
Copy link

psprint commented Oct 20, 2017

I've debugged my fast-syntax-highlighting (z-sy-h fork) and it's like it is described in #260builtin zle "$@" returns 1 and _zsh_highlight isn't run. So I patched my f-sy-h:

 _zsh_highlight_call_widget()
 {
-  builtin zle "$@" && _zsh_highlight
+    # Changed for compatibility with zsh-autosuggestions
+    # Was: builtin zle "$@" && _zsh_highlight
+    builtin zle "$@"
+    _zsh_highlight

I think there is no situation when not-highlighting after user action is a good choice. You can test zdharma/fast-syntax-highlighting, one could then submit to z-sy-h if such change helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants