mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.83k stars 710 forks source link

Default Go syntax highlighting makes comments hard to read with new termui #4274

Open whereswaldon opened 3 years ago

whereswaldon commented 3 years ago

Steps

Open a go source code file that contains comments in kakoune built from the master branch.

Switch to one of these colorschemes:

Try this one:

package main

// here is a comment
func main() {}

Outcome

The comment is shown in inverted colors. I believe this is because the highlighter makes the comment text italic, and the new termui either doesn't support that or my terminal (alacritty) is somehow not playing nicely.

Here's an example of the inverted colors: image

It's very difficult to see the selection when it overlaps with the inverted colors.

Expected

The default colorschemes probably shouldn't invert colors like this. I think the simplest fix would be to remove italics from those faces to prevent the inversion (if I'm right about why it's getting inverted).

It's also possible that this is related to my terminal, in which case I'm happy to be educated about what I've done wrong.

Either way, congratulations on merging the new termui support! What an accomplishment, to whittle the dependencies down to just a compiler and stdlib. Well done!

jwhett commented 3 years ago

Also seen on ac6420ee with Python; assuming all lang-independent comments. @whereswaldon and I have similar stacks if that type of information would be valuable.

It's also possible that this is related to my terminal, in which case I'm happy to be educated about what I've done wrong.

Also happy to follow up here as well. My configuration is pretty static. The config itself hasn't changed since December and current terminal build is from ~April.

Either way, congratulations on merging the new termui support!

Second this :partying_face:

Screwtapello commented 3 years ago

This command uses the same escape sequence that Kakoune uses to produce italic text:

$ printf '\e[3mhello\e[0m world\n'

It should produce output like this:

hello world

If it doesn't, does this command work?

$ tput sitm; printf 'hello'; tput ritm; printf ' world\n'

If that does work, then what does this command produce?

$ tput sitm | hexdump -C
whereswaldon commented 3 years ago

image

Seems like neither kakoune's escape sequence nor the second option produce italics when I run kak within tmux in alacritty.

Interestingly, I can detach from tmux and I get the expected behavior:

image

So I guess this has something to do with how tmux is presenting its terminal capabilities?

Somehow, this did work in tmux prior to the new termui. Not sure what sorcery was employed there though.

Within tmux, TERM=screen-256color.


Actually, I found a way to fix it. If you force tmux to advertise itself as TERM=tmux-256color, the italic escape sequences start working. This isn't tmux's default TERM value though, so I'm surprised more people aren't running into it. Perhaps there's some other factor involved.

Anyway, add this line to ~/.tmux.conf to fix it:

set -g default-terminal tmux-256color

I guess we can close this? Or is it considered a bug that italic support used to work in this configuration and now does not?

Screwtapello commented 3 years ago

If you compare the terminfo database entries for screen-256color and tmux-256color, and look for mentions of the \e[3m escape sequence:

$ infocmp -1x screen-256color tmux-256color | grep -F '[3m'
    sitm: NULL, '\E[3m'.
    smso: '\E[3m', '\E[7m'.

..we see that screen has no "Set ITalic Mode" option (it's "NULL"), but tmux uses the expected \e[3m sequence. Meanwhile, screen uses \e[3m for "Set Mode Stand-Out" (usually reverse-video). But tmux isn't screen, so why does it sometimes behave like screen? Turns out, tmux deliberately implements screen's behaviour if the default-terminal option starts with "screen":

https://github.com/tmux/tmux/blob/607e6b1c3339b97408be3d6c2c93a82fcee5251a/tty.c#L624-L630

tmux was originally implemented as a compatible replacement for screen, which is why it uses screen-256color by default, and implements its historical warts like interpreting italics as reverse-video.

whereswaldon commented 3 years ago

Thank you for the detailed explanation! I understand why it doesn't work when tmux is advertising itself as screen now.

My one remaining question is "how on earth did this work before?" I guess ncurses has a special case that handles this particular thing? "If term is screen but actually we're in tmux, do X for italics"? I don't even know what ncurses would do to detect tmux unless it snoops for the TMUX environment variable.

Either way, you're welcome to close this and pursue it no further. You've spent enough time answering my questions, and the above isn't an especially important one.

Thanks again for taking the time!

whereswaldon commented 3 years ago

It just hit me why this "worked" before. It's because before comments weren't italic at all. Now that they are, it looks really weird to me, which made me realize that before they weren't. Ncurses detected that TERM=screen-256color didn't support italics, so it just didn't try to emit that escape sequence. Now that it's being emitted unconditionally, it breaks because tmux is emulating screen.

I take it that the new termui doesn't rely on terminfo? Is there somewhere I can read about it? Seems like a cool effort, and I know it's taken a long time to create. I'd love to hear the story behind it and the factors that motivated its design.