Skip to content

Commit d7f5854

Browse files
committed
patch 9.1.1060: Vim always enables 'termguicolors' in a terminal
Problem: Vim always enables 'termguicolors' in a terminal, even when not wanted (after v9.1.1054) Solution: Respect `:set notermguicolors` in vimrc file fixes: #16538 fixes: #16539 closes: #16540 Signed-off-by: Christian Brabandt <[email protected]>
1 parent bfb4eea commit d7f5854

File tree

6 files changed

+67
-21
lines changed

6 files changed

+67
-21
lines changed

runtime/doc/options.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.1. Last change: 2025 Jan 29
1+
*options.txt* For Vim version 9.1. Last change: 2025 Jan 31
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -8500,7 +8500,8 @@ A jump table for the options with a short description can be found at |Q_op|.
85008500
< You need to do this when your system has no locale support for UTF-8.
85018501

85028502
*'termguicolors'* *'tgc'* *'notermguicolors'* *'notgc'* *E954*
8503-
'termguicolors' 'tgc' boolean (default off)
8503+
'termguicolors' 'tgc' boolean (default off unless Vim detects that it runs
8504+
in a capable terminal)
85048505
global
85058506
{not available when compiled without the
85068507
|+termguicolors| feature}
@@ -8510,7 +8511,11 @@ A jump table for the options with a short description can be found at |Q_op|.
85108511
Will automatically be enabled, if Vim detects that it runs in a
85118512
capable terminal (when the terminal supports the RGB terminfo
85128513
capability or when the number of colors |t_Co| supported by the
8513-
terminal is 0x1000000, e.g. with $TERM=xterm-direct).
8514+
terminal is 0x1000000, e.g. with $TERM=xterm-direct). Due to the async
8515+
nature of querying the terminal, enabling this automatically is
8516+
noticable. Use >
8517+
set notermguicolors
8518+
< to explicitly disable.
85148519

85158520
Requires a ISO-8613-3 compatible terminal. If setting this option
85168521
does not work (produces a colorless UI) reading |xterm-true-color|

src/globals.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,3 +2043,7 @@ EXTERN int skip_update_topline INIT(= FALSE);
20432043
// 'showcmd' buffer shared between normal.c and statusline code
20442044
#define SHOWCMD_BUFLEN (SHOWCMD_COLS + 1 + 30)
20452045
EXTERN char_u showcmd_buf[SHOWCMD_BUFLEN];
2046+
2047+
#ifdef FEAT_TERMGUICOLORS
2048+
EXTERN int p_tgc_set INIT(= FALSE);
2049+
#endif

src/option.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,6 +4296,7 @@ did_set_termguicolors(optset_T *args UNUSED)
42964296
# endif
42974297
!has_vtp_working())
42984298
{
4299+
p_tgc_set = TRUE;
42994300
p_tgc = 0;
43004301
return e_24_bit_colors_are_not_supported_on_this_environment;
43014302
}
@@ -4320,6 +4321,7 @@ did_set_termguicolors(optset_T *args UNUSED)
43204321
term_update_palette_all();
43214322
term_update_wincolor_all();
43224323
# endif
4324+
p_tgc_set = TRUE;
43234325

43244326
return NULL;
43254327
}

src/term.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,8 +1662,8 @@ set_color_count(int nr)
16621662
else
16631663
*nr_colors = NUL;
16641664
#ifdef FEAT_TERMGUICOLORS
1665-
// xterm-direct, enable termguicolors
1666-
if (t_colors == 0x1000000 && !p_tgc)
1665+
// xterm-direct, enable termguicolors, when it wasn't set yet
1666+
if (t_colors == 0x1000000 && !p_tgc_set)
16671667
set_option_value((char_u *)"termguicolors", 1L, NULL, 0);
16681668
#endif
16691669
set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
@@ -7199,8 +7199,9 @@ got_code_from_term(char_u *code, int len)
71997199
else if (name[0] == 'R' && name[1] == 'G' && name[2] == 'B' && code[9] == '=')
72007200
{
72017201
int val = atoi((char *)str);
7202-
// 8 bits per color channel
7203-
if (val == 8)
7202+
// only enable it, if termguicolors hasn't been set yet and
7203+
// there are 8 bits per color channel
7204+
if (val == 8 && !p_tgc_set)
72047205
{
72057206
#ifdef FEAT_EVAL
72067207
ch_log(NULL, "got_code_from_term(RGB): xterm-direct colors detected");

src/testdir/test_termcodes.vim

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ source mouse.vim
1010
source view_util.vim
1111
source term_util.vim
1212

13+
func s:TermGuiColorsTest()
14+
CheckNotMSWindows
15+
if !CanRunVimInTerminal()
16+
throw 'Skipped: cannot make screendumps'
17+
endif
18+
if !executable('tput')
19+
throw "Skipped: tput not executable!"
20+
endif
21+
if has("gui_running")
22+
throw "Skipped: does not work in GUI mode"
23+
endif
24+
call system('tput -Txterm-direct RGB 2>/dev/null')
25+
if v:shell_error
26+
throw "Skipped: xterm-direct $TERM has no RGB capability"
27+
endif
28+
endfunc
29+
30+
1331
func Test_term_mouse_left_click()
1432
new
1533
let save_mouse = &mouse
@@ -2740,21 +2758,8 @@ func Test_terminal_builtin_without_gui()
27402758
endfunc
27412759

27422760
func Test_xterm_direct_enables_termguicolors()
2761+
call s:TermGuiColorsTest()
27432762
" TERM=xterm-direct enables termguicolors
2744-
CheckNotMSWindows
2745-
if !CanRunVimInTerminal()
2746-
throw 'Skipped: cannot make screendumps'
2747-
endif
2748-
if !executable('tput')
2749-
throw "Skipped: tput not executable!"
2750-
endif
2751-
if has("gui_running")
2752-
throw "Skipped: does not work in GUI mode"
2753-
endif
2754-
call system('tput -Txterm-direct RGB 2>/dev/null')
2755-
if v:shell_error
2756-
throw "Skipped: xterm-direct $TERM has no RGB capability"
2757-
endif
27582763
let colors = systemlist('tput -Txterm-direct colors')[0]
27592764
defer delete('XTerm-direct.txt')
27602765

@@ -2773,6 +2778,33 @@ func Test_xterm_direct_enables_termguicolors()
27732778
call assert_equal(['', 'TERM: xterm-direct', 'Termguicolors: 1'], result)
27742779
" cleanup
27752780
bw!
2781+
close
2782+
endfunc
2783+
2784+
func Test_xterm_direct_no_termguicolors()
2785+
" unfortunately doesn't work with libvterm
2786+
call s:TermGuiColorsTest()
2787+
2788+
let lines =<< trim END
2789+
set notermguicolors noswapfile
2790+
set t_Co=16777216
2791+
END
2792+
call writefile(lines, 'XtermDirect', 'D')
2793+
defer delete('XTerm-direct2.txt')
2794+
2795+
let buf = RunVimInTerminal('-S XtermDirect --clean XTerm-direct2.txt',
2796+
\ {'rows': 10, 'env': {'TERM': 'xterm-direct'}})
2797+
call TermWait(buf)
2798+
call term_sendkeys(buf, ":$put ='TERM: ' .. &term\<cr>")
2799+
call term_sendkeys(buf, ":$put ='Termguicolors: ' .. &tgc\<cr>")
2800+
call term_sendkeys(buf, ":wq\<cr>")
2801+
call TermWait(buf)
2802+
2803+
let result=readfile('XTerm-direct2.txt')
2804+
call assert_equal(['', 'TERM: xterm-direct', 'Termguicolors: 0'], result)
2805+
" cleanup
2806+
bw!
2807+
close
27762808
endfunc
27772809

27782810
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1060,
707709
/**/
708710
1059,
709711
/**/

0 commit comments

Comments
 (0)