romainl/Apprentice

Request: Set terminal ansi colors even when 'notermguicolors' set

craigmac opened this issue · 6 comments

if (has('termguicolors') && &termguicolors) || has('gui_running')

Situation which prompted this: I was trying out the newest MacVim release (9.0 support!), and when I started a :term noticed that the colors weren't the apprentice palette - the reason being that I had 'set notermguicolors' set. Is it possible since appentice picks from the 'standard' xterm 256 palette though, that if we have don't have 'termguicolors' set but we either have 256 or GUI then we still set the ansi terminal colors to apprentice palette? Should be fine for GUI but for Terminal.app without profile set to apprentice colours, I'm not sure if it would work? I tried changing profile to Basic on Terminal.app and apprentice still seems to work exactly the same in vim.

Screen Shot 2022-07-25 at 8 53 24 AM

Ok, even weirder and maybe a Vim bug: if seems as long as 'termguicolors' is not set, even if you're running GUI, the ANSI colors defined by this guard aren't used. The docs in Vim say:

In GUI mode or with 'termguicolors', the 16 ANSI colors used by default in new
terminal windows may be configured using the variable
`g:terminal_ansi_colors`, which should be a list of 16 color names or
hexadecimal color codes, similar to those accepted by |highlight-guifg|.  When
not using GUI colors, the terminal window always uses the 16 ANSI colors of
the underlying terminal.
When using `term_start()` the colors can be set with the "ansi_colors" option.
The |term_setansicolors()| function can be used to change the colors, and
|term_getansicolors()| to get the currently used colors.

So it seems it's implicitly checking if has('gui_running') || &termguicolors is true then it should be using the g:terminal_ansi_colors. I think the issue might be the 'not using GUI colors' check, what is the implicit check it's doing there that results in a GUI with 'set notermguicolors' from failing the 'not using GUI colors' check. What a can or worms I've opened here.

What a can or worms I've opened here.

Welcome to this particular corner of hell.

Currently, I do everything I can to avoid the mess you are pointing out by imposing relatively strict requirements for g:terminal_ansi_colors. Last time I checked—and I don't think it has been fixed since—there were situations where Vim would use the colors from g:terminal_ansi_colors even when it wasn't supposed to do so: no GUI and no termguicolors, downsampling the RGB colors to their "nearest neighbour", which is everything but reliable.

Because of that, I decided to hide the very definition of g:terminal_ansi_colors behind a greedy check. Given the current state of affairs, I think that's the best course of action.

Stranger yet, using latest MacVim, and explicitly starting a terminal like so:

call term_start('/usr/local/bin/bash', { 'ansi_colors':  ['#1c1c1c', '#af5f5f', '#5f875f', '#87875f', '#5f87af', '#5f5f87', '#5f8787', '#6c6c6c', '#444444', '#ff8700', '#87af87', '#ffffaf', '#87afd7', '#8787af', '#5fafaf', '#ffffff']})

with 'termguicolors' set as well, it still won't respect the ansi colours I've set. I can do echo term_getansicolors(28) after opening term opens and it will return the correct colours I set in the call, but for some reason doesn't apply them... I'll have to create a small reproducible setup and make an issue in vim/vim. Thanks for the help, and for trying to workaround these hacks on hacks.

More investigation (I have a strange idea of fun...). When remove ~/.vim/gvimrc from the equation to rule that out and keep my ~/.vim/vimrc with colorscheme apprentice and termguicolors are off, this is what it looks like when I run :term after MacVim boot, and then see what g:terminal_ansi_colors are set to:

Screen Shot 2022-07-25 at 11 51 25 AM

It appears the ANSI colors are being set but the background color is the issue. From reading the docs in :help gvimrc I know that GUI options are set/reset after vimrc is done, so I removed my gvimrc to rule that out. From these lines:

When the GUI starts up initializations are carried out, in this order:
- The 'term' option is set to "builtin_gui" and terminal options are reset to
  their default value for the GUI |terminal-options|.

It seems like my issue is that it doesn't pick up the #262626 required for proper background colour in a new :term, and even if I did set that, on GUI initialization it resets the value, so I will need to manually set either t_AB or maybe t_8b with RGB to match apprentice background color I think?

Here's where I'm lost: if I do:

  1. Start MacVim without a gvimrc to rule that out
  2. start :term and see that the background color is wrong, but the rest of the colors look right and report are using the values set for g:terminal_ansi_colors
  3. re-source my $VIMRC
  4. open another :term
  5. background color of the terminal buffer is now correct.
  6. become even more confused...

Result:
Screen Shot 2022-07-25 at 12 04 44 PM

And here's the :set termcap values for when it didn't work (term background color not set, that is):
Screen Shot 2022-07-25 at 12 06 28 PM

Comparing the termcap outputs, I see that on the working one, the t_TI and t_SE are set. This makes sense now that I've read that on GUI init it resets these to default, so when I re-sourced my vimrc after MacVim had started, then they actually applied.

I guess my question at this point is: any idea how I would set my :term background in MacVim to be '#262626' without having to start MacVim, and then re-source my vimrc?

The fix, for MacVim (maybe other GUIs?) is to explicitly set the Terminal highlight bg color. I've just done:

augroup Vimrc
	autocmd ColorScheme apprentice hi! Terminal guibg=#262626
augroup END

For now to fix it, until you might be able to figure out a more permanent fix that doesn't break other fixes.

Answered here

Well, the problem with this is that Terminal is already linked to Normal in all "modes": GUI, 256c, etc. so it is already supposed to have a background color, the same as Normal: #262626.

FWIW, here is how :term looks like, here…

  • in MacVim 172 (GUI):

    Capture d’écran 2022-07-25 à 20 38 12

    I get:

    • the expected Apprentice background color,
    • the expected Apprentice terminal palette.

    So it seems fine to me.

  • in MacVim 172 (TUI) in Terminal.app with a random light grey as background:

    Capture d’écran 2022-07-25 à 20 41 39

    I get:

    • the expected Apprentice Terminal background,
    • the expected palette from my Terminal.app profile

    Again, it seems fine to me.

Maybe it is because you are using a gvimrc? I don't.