zsh-autosuggestions: Highlight style is not cleared on accept when manually set to a value <8

Describe the bug

If ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE is manually set where fg or bg are less than 8, then the highlighting is not reset when accepting or partially accepting the suggestion.

To Reproduce

% zsh -df
% source path/to/zsh-autosuggestions.zsh
% ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=1'
% ZSH<right arrow key>

(instead of typing <right arrow key> press the right arrow key on your keyboard to accept the autosuggestion, which should be _AUTOSUGGEST_HIGHLIGHT_STYLE='fg=1' in red text). At this point, ZSH should be in the regular color but _AUTOSUGGEST_HIGHLIGHT_STYLE='fg=1' (which was completed) will still be in red.

Note that similar results occur for fg=x or bg=x when x<8, or a combination thereof (fg=x,bg=y where either x or y are <8).

Expected behavior

The color of _AUTOSUGGEST_HIGHLIGHT_STYLE='fg=1' would be reset, like when the variable is unchanged or set to a value >=8.

Screenshots

image Note how the cursor (yellow vertical bar) is ahead of the red text, so it has retained the color even though it was accepted.

Desktop

  • OS + distribution: Arch Linux
  • Zsh version: 5.9
  • Plugin version: a411ef3e0992d4839f0732ebeb9823024afaaaa8
  • Terminal: alacritty 0.11.0-dev (ed4614d0)

Additional context

If I remember correctly, this issue began when I updated to zsh 5.9. I’m not exactly sure whether it was that update, but I know I didn’t use to have this issue and started having it sort of recently (a few weeks or months ago).

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 12
  • Comments: 17 (1 by maintainers)

Commits related to this issue

Most upvoted comments

ok, meanwhile I double-checked, and yes, 5.9 ignores the “memo=token”

It turned out, in my case, the problem was that I set my ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE to custom color code with CAPITAL case hexadecimal values (#A1A2A6) and seems the region_highlight array handling (now?) converts to lower case everything that added to it, therefore the _zsh_autosuggest_highlight_reset() function could not remove the earlier set value via the used region_highlight=("${(@)region_highlight:#$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT_SAVE}") expression

so, take care, either

  • use only lowercase letters when you set ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE via a hexadecimal color code, or
  • as a dirty hack you can replace _zsh_autosuggest_highlight_apply with this tiny modification in zsh-autosuggestions.zsh
_zsh_autosuggest_highlight_apply() {
	typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT_SAVE

	if (( $#POSTDISPLAY )); then
		# Make sure the possibly user defined (or anytime meanwhile changed ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE is lower case,
		# it seems region_highlight items are now all lower case converted
		# NOTE: This quick hack is not a real solution as
		#		- might not be backward compatible
		#		- might not be future compatible
		# TODO: convert both the region_highlight items and the ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE to the same letter case
		ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=${ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE:l}

		typeset -g _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT_SAVE="$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"

		region_highlight+=("$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT_SAVE")
	else
		unset _ZSH_AUTOSUGGEST_LAST_HIGHLIGHT_SAVE
	fi
}

Same problem on Gentoo Linux, ZSH version 5.9.

Only workaround for now for me was to change foreground color id to more than 8; 14 looked similar enough to 6 😃

I don’t use zsh-syntax-highlighting and have this issue too, but not when ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE is set to bold or underline. Setting fg or bg with any other option prevents the style from clearing.

I guess bisecting zsh might be required?