zsh-users / zsh-syntax-highlighting

Fish shell like syntax highlighting for Zsh.
github.com/zsh-users/zsh-syntax-highlighting
BSD 3-Clause "New" or "Revised" License
19.55k stars 1.32k forks source link

Customization of ZLE vi visual mode highlighting #887

Open Iskustvo opened 1 year ago

Iskustvo commented 1 year ago

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:

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!

danielshahaf commented 1 year ago
  • 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 commented 1 year ago

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'.

Iskustvo commented 1 year ago

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)

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

Iskustvo commented 1 year ago
  • 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:

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?

danielshahaf commented 1 year ago

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.

Iskustvo commented 1 year ago

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.

danielshahaf commented 1 year ago

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 :)

Iskustvo commented 1 year ago

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!

danielshahaf commented 1 year ago

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.