zsh-syntax-highlighting icon indicating copy to clipboard operation
zsh-syntax-highlighting copied to clipboard

Customization of ZLE vi visual mode highlighting

Open Iskustvo opened this issue 2 years ago • 9 comments

Is it possible to achieve the following highlighting in ZLE's Vi visual mode? 2022-08-10-213746_1920x1080_scrot (The picture was taken from Vim, not Zsh)

So, to break it down, what I would like is:

  • Custom background color for selected region (this blue color in the picture)
  • Foreground color to remain as it was before selection (after zsh-syntax-highlighting colored it already)
  • Cursor to be visually distinguished from the rest of the selected text. More precisely, it should be inverted/standout.

This seems like it should be configurable solely from ZLE, but I wasn't able to achieve it there and also didn't have much luck with this plugin. Any suggestion is appreciated!

Iskustvo avatar Aug 10 '22 20:08 Iskustvo

  • Custom background color for selected region (this blue color in the picture)

Possible:

https://github.com/zsh-users/zsh-syntax-highlighting/blob/caa749d030d22168445c4cb97befd406d2828db0/zsh-syntax-highlighting.zsh#L269-L288

  • Foreground color to remain as it was before selection (after zsh-syntax-highlighting colored it already)

Good question. If zle's $region_highlight merging rules [the logic that governs what fg and bg colours apply to a character that falls into multiple $region_highlight ranges] don't have the right semantics, then those will have to be implemented in zsh code before zle processes $region_highlight.

  • Cursor to be visually distinguished from the rest of the selected text. More precisely, it should be inverted/standout.
ZSH_HIGHLIGHT_HIGHLIGHTERS+=( cursor )

danielshahaf avatar Aug 11 '22 07:08 danielshahaf

Possible:

https://github.com/zsh-users/zsh-syntax-highlighting/blob/caa749d030d22168445c4cb97befd406d2828db0/zsh-syntax-highlighting.zsh#L269-L288

Which is to say: ZSH_HIGHLIGHT_STYLES[region]='fg=blue'.

danielshahaf avatar Aug 11 '22 07:08 danielshahaf

Hi @danielshahaf, thank you for answering!

Unfortunately, none of the suggested made any visual change :disappointed:

In my setup, the only thing related to highlighting is:

source "/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
  • Just with this, the block cursor is inverted/standout by default (this is done by st terminal, regardless of zsh): 2022-08-11-231130_1920x1080_scrot In ZLE's vi visual mode, everything selected is inverted/standout (this part is done by zsh): 2022-08-11-231609_1920x1080_scrot
  • Adding any of the following didn't change anything visually:
    • ZSH_HIGHLIGHT_STYLES[region]='fg=#264F78'
    • ZSH_HIGHLIGHT_STYLES[region]='bg=#264F78' # I wasn't sure if you made a typo.
    • ZSH_HIGHLIGHT_HIGHLIGHTERS+=( cursor )
  • The only thing that actually changed the look is zle_highlight=(region:bg=#264F78) # Added before sourcing of z-sy-h and that sets the correct background but screws up both the cursor and the foreground color of selected text: 2022-08-11-233739_1920x1080_scrot

Do you maybe have other suggestions after seeing all of this?

Iskustvo avatar Aug 11 '22 21:08 Iskustvo

  • The only thing that actually changed the look is zle_highlight=(region:bg=#264F78) # Added before sourcing of z-sy-h and that sets the correct background but screws up both the cursor and the foreground color of selected text:

Actually, it looks like the only problem is that partially (in my case just the background color) defining a region in zle_highlight ends up defaulting non-specified parts (like fg=white in my case). By doing it that way:

  • I lose previous syntax highlighting.
  • Inverted/Standout cursor actually still works, but because of defaulted foreground color, it ends up being white.

This theory can be confirmed by better defining region in zle_highlight:

zle_highlight=(region:fg=green,bg=#264F78)

Nevertheless, this is still an unwanted behavior because it overrides the syntax highlighting and specifies the single foreground color for selected text (region). For this to work, zsh would need to support region's foreground color value like fg=preserve. Since this is not the case, the question remains - Can we somehow make this work without the need to change the source code of zsh?

Iskustvo avatar Aug 11 '22 23:08 Iskustvo

For this to work, zsh would need to support region's foreground color value like fg=preserve.

IIRC fg=preserve and the aforementioned merging rules have been discussed on zsh-workers@ a few times, and upstream would welcome patches.

That'd be my preferred solution.

Since this is not the case, the question remains - Can we somehow make this work without the need to change the source code of zsh?

Possible? I think so, yes.

Possible in a maintainable manner? I haven't formed an opinion on this matter yet; I'm happy to be convinced either way.

danielshahaf avatar Aug 14 '22 10:08 danielshahaf

IIRC fg=preserve and the aforementioned merging rules have been discussed on zsh-workers@ a few times, and upstream would welcome patches.

That'd be my preferred solution.

Mine too, although I didn't know there were discussions about it. I was curious enough last weekend and downloaded the source code... It really wasn't a pleasant experience, the project doesn't seem developer friendly at all :disappointed:

Possible? I think so, yes.

Possible in a maintainable manner? I haven't formed an opinion on this matter yet; I'm happy to be convinced either way.

I don't have that deep zsh nor z-sy-h knowledge to contribute to this discussion... If you happen to get any new/recent info about progress on either approach, please keep me in the loop here.

Iskustvo avatar Aug 14 '22 18:08 Iskustvo

It really wasn't a pleasant experience, the project doesn't seem developer friendly at all disappointed

How so? The community is very friendly, in my. At the same time, some older parts of the source code do have an unfortunate tendency towards non-best-practice variable names.

If you happen to get any new/recent info about progress on either approach, please keep me in the loop here.

Then remain subscribed to this ticket :)

danielshahaf avatar Aug 19 '22 09:08 danielshahaf

How so? The community is very friendly, in my. At the same time, some older parts of the source code do have an unfortunate tendency towards non-best-practice variable names.

Yeah, I was talking about the code and those, lets call them, not-best-practices :laughing: Didn't have any interaction with the community, so no complaints there...

Then remain subscribed to this ticket :)

Sure thing, thanks!

Iskustvo avatar Aug 20 '22 23:08 Iskustvo

For this to work, zsh would need to support region's foreground color value like fg=preserve. Since this is not the case, the question remains - Can we somehow make this work without the need to change the source code of zsh?

Hmm. Suppose we disabled the "# region" block I linked above and, instead, had _zsh_highlight_add_highlight() munge the fg/bg values in the region_highlight element it adds. Without considering other use-cases for the moment, would that have the right effect in your use-case? Would the answer depend on whether the redrawhook codepath is/isn't being taken?

Regardless, a solution in zsh upstream would be worth pursuing. IIRC, the problem: has been discussed upstream a few times; is outstanding; has to do with https://github.com/zsh-users/zsh/blob/7996fa561e93d9c27919ed4d4257c13d99db3807/Src/prompt.c#L1717-L1768.

danielshahaf avatar Oct 11 '22 09:10 danielshahaf