Skip to content

Commit c71053c

Browse files
committed
patch 8.0.1102: terminal window does not use Normal colors
Problem: Terminal window does not use Normal colors. Solution: For the GUI and when 'termguicolors' is enabled, use the actual foreground and background colors for the terminal. (Yasuhiro Matsumoto, closes #2067) Use the "Terminal" highlight group if defined.
1 parent 71eeb74 commit c71053c

4 files changed

Lines changed: 151 additions & 5 deletions

File tree

src/proto/syntax.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ int syn_namen2id(char_u *linep, int len);
5252
int syn_check_group(char_u *pp, int len);
5353
int syn_id2attr(int hl_id);
5454
int syn_id2colors(int hl_id, guicolor_T *fgp, guicolor_T *bgp);
55+
void syn_id2cterm_bg(int hl_id, int *fgp, int *bgp);
5556
int syn_get_final_id(int hl_id);
5657
void highlight_gui_started(void);
5758
int highlight_changed(void);

src/syntax.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9709,7 +9709,7 @@ syn_id2attr(int hl_id)
97099709
return attr;
97109710
}
97119711

9712-
#ifdef FEAT_GUI
9712+
#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
97139713
/*
97149714
* Get the GUI colors and attributes for a group ID.
97159715
* NOTE: the colors will be INVALCOLOR when not set, the color otherwise.
@@ -9728,6 +9728,19 @@ syn_id2colors(int hl_id, guicolor_T *fgp, guicolor_T *bgp)
97289728
}
97299729
#endif
97309730

9731+
#if defined(FEAT_TERMINAL) || defined(PROT)
9732+
void
9733+
syn_id2cterm_bg(int hl_id, int *fgp, int *bgp)
9734+
{
9735+
struct hl_group *sgp;
9736+
9737+
hl_id = syn_get_final_id(hl_id);
9738+
sgp = &HL_TABLE()[hl_id - 1]; /* index is ID minus one */
9739+
*fgp = sgp->sg_cterm_fg - 1;
9740+
*bgp = sgp->sg_cterm_bg - 1;
9741+
}
9742+
#endif
9743+
97319744
/*
97329745
* Translate a group ID to the final group ID (following links).
97339746
*/

src/terminal.c

Lines changed: 134 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
* in tl_scrollback are no longer used.
3939
*
4040
* TODO:
41-
* - patch to use GUI or cterm colors for vterm. Yasuhiro, #2067
4241
* - patch to add tmap, jakalope (Jacob Askeland) #2073
4342
* - Redirecting output does not work on MS-Windows, Test_terminal_redir_file()
4443
* is disabled.
@@ -1739,7 +1738,7 @@ color2index(VTermColor *color, int fg, int *boldp)
17391738
else if (red == 128)
17401739
{
17411740
if (green == 128 && blue == 128)
1742-
return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1741+
return lookup_color(12, fg, boldp) + 1; /* dark grey */
17431742
}
17441743
else if (red == 255)
17451744
{
@@ -2385,6 +2384,65 @@ term_get_attr(buf_T *buf, linenr_T lnum, int col)
23852384
return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
23862385
}
23872386

2387+
static VTermColor ansi_table[16] = {
2388+
{ 0, 0, 0}, /* black */
2389+
{224, 0, 0}, /* dark red */
2390+
{ 0, 224, 0}, /* dark green */
2391+
{224, 224, 0}, /* dark yellow / brown */
2392+
{ 0, 0, 224}, /* dark blue */
2393+
{224, 0, 224}, /* dark magenta */
2394+
{ 0, 224, 224}, /* dark cyan */
2395+
{224, 224, 224}, /* light grey */
2396+
2397+
{128, 128, 128}, /* dark grey */
2398+
{255, 64, 64}, /* light red */
2399+
{ 64, 255, 64}, /* light green */
2400+
{255, 255, 64}, /* yellow */
2401+
{ 64, 64, 255}, /* light blue */
2402+
{255, 64, 255}, /* light magenta */
2403+
{ 64, 255, 255}, /* light cyan */
2404+
{255, 255, 255}, /* white */
2405+
};
2406+
2407+
static int cube_value[] = {
2408+
0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF,
2409+
};
2410+
2411+
static int grey_ramp[] = {
2412+
0x00, 0x0B, 0x16, 0x21, 0x2C, 0x37, 0x42, 0x4D, 0x58, 0x63, 0x6E, 0x79,
2413+
0x85, 0x90, 0x9B, 0xA6, 0xB1, 0xBC, 0xC7, 0xD2, 0xDD, 0xE8, 0xF3, 0xFF,
2414+
};
2415+
2416+
/*
2417+
* Convert a cterm color number 0 - 255 to RGB.
2418+
*/
2419+
static void
2420+
cterm_color2rgb(int nr, VTermColor *rgb)
2421+
{
2422+
int idx;
2423+
2424+
if (nr < 16)
2425+
{
2426+
*rgb = ansi_table[nr];
2427+
}
2428+
else if (nr < 232)
2429+
{
2430+
/* 216 color cube */
2431+
idx = nr - 16;
2432+
rgb->blue = cube_value[idx % 6];
2433+
rgb->green = cube_value[idx / 6 % 6];
2434+
rgb->red = cube_value[idx / 36 % 6];
2435+
}
2436+
else if (nr < 256)
2437+
{
2438+
/* 24 grey scale ramp */
2439+
idx = nr - 232;
2440+
rgb->blue = grey_ramp[nr];
2441+
rgb->green = grey_ramp[nr];
2442+
rgb->red = grey_ramp[nr];
2443+
}
2444+
}
2445+
23882446
/*
23892447
* Create a new vterm and initialize it.
23902448
*/
@@ -2396,6 +2454,7 @@ create_vterm(term_T *term, int rows, int cols)
23962454
VTermValue value;
23972455
VTermColor *fg, *bg;
23982456
int fgval, bgval;
2457+
int id;
23992458

24002459
vterm = vterm_new(rows, cols);
24012460
term->tl_vterm = vterm;
@@ -2404,12 +2463,13 @@ create_vterm(term_T *term, int rows, int cols)
24042463
/* TODO: depends on 'encoding'. */
24052464
vterm_set_utf8(vterm, 1);
24062465

2407-
/* Vterm uses a default black background. Set it to white when
2408-
* 'background' is "light". */
24092466
vim_memset(&term->tl_default_color.attrs, 0, sizeof(VTermScreenCellAttrs));
24102467
term->tl_default_color.width = 1;
24112468
fg = &term->tl_default_color.fg;
24122469
bg = &term->tl_default_color.bg;
2470+
2471+
/* Vterm uses a default black background. Set it to white when
2472+
* 'background' is "light". */
24132473
if (*p_bg == 'l')
24142474
{
24152475
fgval = 0;
@@ -2422,6 +2482,76 @@ create_vterm(term_T *term, int rows, int cols)
24222482
}
24232483
fg->red = fg->green = fg->blue = fgval;
24242484
bg->red = bg->green = bg->blue = bgval;
2485+
2486+
/* The "Terminal" highlight group overrules the defaults. */
2487+
id = syn_name2id((char_u *)"Terminal");
2488+
2489+
/* Use the actual color for the GUI and when 'guitermcolors' is set. */
2490+
#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
2491+
if (0
2492+
# ifdef FEAT_GUI
2493+
|| gui.in_use
2494+
# endif
2495+
# ifdef FEAT_TERMGUICOLORS
2496+
|| p_tgc
2497+
# endif
2498+
)
2499+
{
2500+
guicolor_T fg_rgb, bg_rgb;
2501+
2502+
if (id != 0)
2503+
syn_id2colors(id, &fg_rgb, &bg_rgb);
2504+
2505+
# ifdef FEAT_GUI
2506+
if (gui.in_use)
2507+
{
2508+
if (fg_rgb == INVALCOLOR)
2509+
fg_rgb = gui.norm_pixel;
2510+
if (bg_rgb == INVALCOLOR)
2511+
bg_rgb = gui.back_pixel;
2512+
}
2513+
# ifdef FEAT_TERMGUICOLORS
2514+
else
2515+
# endif
2516+
# endif
2517+
# ifdef FEAT_TERMGUICOLORS
2518+
{
2519+
if (fg_rgb == INVALCOLOR)
2520+
fg_rgb = cterm_normal_fg_gui_color;
2521+
if (bg_rgb == INVALCOLOR)
2522+
bg_rgb = cterm_normal_bg_gui_color;
2523+
}
2524+
# endif
2525+
if (fg_rgb != INVALCOLOR)
2526+
{
2527+
long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
2528+
2529+
fg->red = (unsigned)(rgb >> 16);
2530+
fg->green = (unsigned)(rgb >> 8) & 255;
2531+
fg->blue = (unsigned)rgb & 255;
2532+
}
2533+
if (bg_rgb != INVALCOLOR)
2534+
{
2535+
long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
2536+
2537+
bg->red = (unsigned)(rgb >> 16);
2538+
bg->green = (unsigned)(rgb >> 8) & 255;
2539+
bg->blue = (unsigned)rgb & 255;
2540+
}
2541+
}
2542+
else
2543+
#endif
2544+
if (id != 0 && t_colors >= 16)
2545+
{
2546+
int cterm_fg, cterm_bg;
2547+
2548+
syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
2549+
if (cterm_fg >= 0)
2550+
cterm_color2rgb(cterm_fg, fg);
2551+
if (cterm_bg >= 0)
2552+
cterm_color2rgb(cterm_bg, bg);
2553+
}
2554+
24252555
vterm_state_set_default_colors(vterm_obtain_state(vterm), fg, bg);
24262556

24272557
/* Required to initialize most things. */

src/version.c

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

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
1102,
772774
/**/
773775
1101,
774776
/**/

0 commit comments

Comments
 (0)