Skip to content

Commit

Permalink
patch 8.0.1783: cannot use 256 colors in a MS-Windows console
Browse files Browse the repository at this point in the history
Problem:    Cannot use 256 colors in a MS-Windows console.
Solution:   Add 256 color support. (Nobuhiro Takasaki, closes vim#2821)
  • Loading branch information
brammool committed May 1, 2018
1 parent d76ce85 commit c5cd885
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/misc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -3723,7 +3723,7 @@ vim_beep(
/* No restore color information, refresh the screen. */
if (has_vtp_working() != 0
# ifdef FEAT_TERMGUICOLORS
&& p_tgc
&& (p_tgc || (!p_tgc && t_colors >= 256))
# endif
)
{
Expand Down
25 changes: 23 additions & 2 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -6082,6 +6082,9 @@ did_set_string_option(
int redraw_gui_only = FALSE;
#endif
int ft_changed = FALSE;
#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
int did_swaptcap = FALSE;
#endif

/* Get the global option to compare with, otherwise we would have to check
* two values for all local options. */
Expand Down Expand Up @@ -6821,6 +6824,13 @@ did_set_string_option(
vim_free(T_CCO);
T_CCO = empty_option;
}
#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
if (is_term_win32())
{
swap_tcap();
did_swaptcap = TRUE;
}
#endif
/* We now have a different color setup, initialize it again. */
init_highlight(TRUE, FALSE);
}
Expand Down Expand Up @@ -7674,6 +7684,16 @@ did_set_string_option(
#endif
check_redraw(options[opt_idx].flags);

#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
if (did_swaptcap)
{
if (t_colors < 256)
p_tgc = 0;
set_termname((char_u *)"win32");
init_highlight(TRUE, FALSE);
}
#endif

return errmsg;
}

Expand Down Expand Up @@ -8716,7 +8736,8 @@ set_bool_option(
p_tgc = 0;
return (char_u*)N_("E954: 24-bit colors are not supported on this environment");
}
swap_tcap();
if (is_term_win32())
swap_tcap();
# endif
# ifdef FEAT_GUI
if (!gui.in_use && !gui.starting)
Expand All @@ -8725,7 +8746,7 @@ set_bool_option(
# ifdef FEAT_VTP
control_console_color_rgb();
/* reset t_Co */
if (STRCMP(T_NAME, "win32") == 0)
if (is_term_win32())
set_termname(T_NAME);
# endif
}
Expand Down
41 changes: 36 additions & 5 deletions src/os_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static guicolor_T save_console_bg_rgb;
static guicolor_T save_console_fg_rgb;

# ifdef FEAT_TERMGUICOLORS
# define USE_VTP (vtp_working && p_tgc)
# define USE_VTP (vtp_working && is_term_win32() && (p_tgc || (!p_tgc && t_colors >= 256)))
# else
# define USE_VTP 0
# endif
Expand Down Expand Up @@ -2630,7 +2630,6 @@ mch_init(void)

/* set termcap codes to current text attributes */
update_tcap(g_attrCurrent);
swap_tcap();

GetConsoleCursorInfo(g_hConOut, &g_cci);
GetConsoleMode(g_hConIn, &g_cmodein);
Expand Down Expand Up @@ -5763,7 +5762,11 @@ clear_chars(
if (!USE_VTP)
FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy);
else
FillConsoleOutputAttribute(g_hConOut, 0, n, coord, &dwDummy);
{
set_console_color_rgb();
gotoxy(coord.X + 1, coord.Y + 1);
vtp_printf("\033[%dX", n);
}
}


Expand Down Expand Up @@ -7653,6 +7656,16 @@ vtp_sgr_bulks(
vtp_printf((char *)buf);
}

static int
ctermtoxterm(
int cterm)
{
uint8_t r, g, b, idx;

cterm_color2rgb(cterm, &r, &g, &b, &idx);
return (((int)r << 16) | ((int)g << 8) | (int)b);
}

static void
set_console_color_rgb(void)
{
Expand All @@ -7661,6 +7674,8 @@ set_console_color_rgb(void)
int id;
guicolor_T fg = INVALCOLOR;
guicolor_T bg = INVALCOLOR;
int ctermfg;
int ctermbg;

if (!USE_VTP)
return;
Expand All @@ -7669,9 +7684,19 @@ set_console_color_rgb(void)
if (id > 0)
syn_id2colors(id, &fg, &bg);
if (fg == INVALCOLOR)
fg = 0xc0c0c0; /* white text */
{
ctermfg = -1;
if (id > 0)
syn_id2cterm_bg(id, &ctermfg, &ctermbg);
fg = ctermfg != -1 ? ctermtoxterm(ctermfg) : 0xc0c0c0; /* white */
}
if (bg == INVALCOLOR)
bg = 0x000000; /* black background */
{
ctermbg = -1;
if (id > 0)
syn_id2cterm_bg(id, &ctermfg, &ctermbg);
bg = ctermbg != -1 ? ctermtoxterm(ctermbg) : 0x000000; /* black */
}
fg = (GetRValue(fg) << 16) | (GetGValue(fg) << 8) | GetBValue(fg);
bg = (GetRValue(bg) << 16) | (GetGValue(bg) << 8) | GetBValue(bg);

Expand Down Expand Up @@ -7730,4 +7755,10 @@ use_vtp(void)
return USE_VTP;
}

int
is_term_win32(void)
{
return T_NAME != NULL && STRCMP(T_NAME, "win32") == 0;
}

#endif
1 change: 1 addition & 0 deletions src/proto/os_win32.pro
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ int mch_setenv(char *var, char *value, int x);
void control_console_color_rgb(void);
int has_vtp_working(void);
int use_vtp(void);
int is_term_win32(void);
/* vim: set ft=c : */
1 change: 1 addition & 0 deletions src/proto/term.pro
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ void update_tcap(int attr);
void swap_tcap(void);
guicolor_T gui_get_color_cmn(char_u *name);
guicolor_T gui_get_rgb_color_cmn(int r, int g, int b);
void cterm_color2rgb(int nr, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *ansi_idx);
/* vim: set ft=c : */
Loading

0 comments on commit c5cd885

Please sign in to comment.