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. */
0 commit comments