Skip to content

Commit

Permalink
patch 8.0.0791: terminal colors depend on the system
Browse files Browse the repository at this point in the history
Problem:    Terminal colors depend on the system.
Solution:   Use the highlight color lookup tables.
  • Loading branch information
brammool committed Jul 28, 2017
1 parent fa228f7 commit b41bf8e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 108 deletions.
1 change: 1 addition & 0 deletions src/proto/syntax.pro
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void ex_syntime(exarg_T *eap);
char_u *get_syntime_arg(expand_T *xp, int idx);
void init_highlight(int both, int reset);
int load_colors(char_u *name);
int lookup_color(int idx, int foreground);
void do_highlight(char_u *line, int forceit, int init);
void free_highlight(void);
void restore_cterm_colors(void);
Expand Down
201 changes: 112 additions & 89 deletions src/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -7221,6 +7221,116 @@ load_colors(char_u *name)
return retval;
}

static char *(color_names[28]) = {
"Black", "DarkBlue", "DarkGreen", "DarkCyan",
"DarkRed", "DarkMagenta", "Brown", "DarkYellow",
"Gray", "Grey", "LightGray", "LightGrey",
"DarkGray", "DarkGrey",
"Blue", "LightBlue", "Green", "LightGreen",
"Cyan", "LightCyan", "Red", "LightRed", "Magenta",
"LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
/* indices:
* 0, 1, 2, 3,
* 4, 5, 6, 7,
* 8, 9, 10, 11,
* 12, 13,
* 14, 15, 16, 17,
* 18, 19, 20, 21, 22,
* 23, 24, 25, 26, 27 */
static int color_numbers_16[28] = {0, 1, 2, 3,
4, 5, 6, 6,
7, 7, 7, 7,
8, 8,
9, 9, 10, 10,
11, 11, 12, 12, 13,
13, 14, 14, 15, -1};
/* for xterm with 88 colors... */
static int color_numbers_88[28] = {0, 4, 2, 6,
1, 5, 32, 72,
84, 84, 7, 7,
82, 82,
12, 43, 10, 61,
14, 63, 9, 74, 13,
75, 11, 78, 15, -1};
/* for xterm with 256 colors... */
static int color_numbers_256[28] = {0, 4, 2, 6,
1, 5, 130, 130,
248, 248, 7, 7,
242, 242,
12, 81, 10, 121,
14, 159, 9, 224, 13,
225, 11, 229, 15, -1};
/* for terminals with less than 16 colors... */
static int color_numbers_8[28] = {0, 4, 2, 6,
1, 5, 3, 3,
7, 7, 7, 7,
0+8, 0+8,
4+8, 4+8, 2+8, 2+8,
6+8, 6+8, 1+8, 1+8, 5+8,
5+8, 3+8, 3+8, 7+8, -1};

/*
* Lookup the "cterm" value to be used for color with index "idx" in
* color_names[].
*/
int
lookup_color(int idx, int foreground)
{
int color = color_numbers_16[idx];
char_u *p;

/* Use the _16 table to check if it's a valid color name. */
if (color < 0)
return -1;

if (t_colors == 8)
{
/* t_Co is 8: use the 8 colors table */
#if defined(__QNXNTO__)
color = color_numbers_8_qansi[idx];
#else
color = color_numbers_8[idx];
#endif
if (foreground)
{
/* set/reset bold attribute to get light foreground
* colors (on some terminals, e.g. "linux") */
if (color & 8)
{
HL_TABLE()[idx].sg_cterm |= HL_BOLD;
HL_TABLE()[idx].sg_cterm_bold = TRUE;
}
else
HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
}
color &= 7; /* truncate to 8 colors */
}
else if (t_colors == 16 || t_colors == 88
|| t_colors >= 256)
{
/*
* Guess: if the termcap entry ends in 'm', it is
* probably an xterm-like terminal. Use the changed
* order for colors.
*/
if (*T_CAF != NUL)
p = T_CAF;
else
p = T_CSF;
if (*p != NUL && (t_colors > 256
|| *(p + STRLEN(p) - 1) == 'm'))
{
if (t_colors == 88)
color = color_numbers_88[idx];
else if (t_colors >= 256)
color = color_numbers_256[idx];
else
color = color_numbers_8[idx];
}
}
return color;
}

/*
* Handle the ":highlight .." command.
* When using ":hi clear" this is called recursively for each group with
Expand Down Expand Up @@ -7723,45 +7833,6 @@ do_highlight(
}
else
{
static char *(color_names[28]) = {
"Black", "DarkBlue", "DarkGreen", "DarkCyan",
"DarkRed", "DarkMagenta", "Brown", "DarkYellow",
"Gray", "Grey",
"LightGray", "LightGrey", "DarkGray", "DarkGrey",
"Blue", "LightBlue", "Green", "LightGreen",
"Cyan", "LightCyan", "Red", "LightRed", "Magenta",
"LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
static int color_numbers_16[28] = {0, 1, 2, 3,
4, 5, 6, 6,
7, 7,
7, 7, 8, 8,
9, 9, 10, 10,
11, 11, 12, 12, 13,
13, 14, 14, 15, -1};
/* for xterm with 88 colors... */
static int color_numbers_88[28] = {0, 4, 2, 6,
1, 5, 32, 72,
84, 84,
7, 7, 82, 82,
12, 43, 10, 61,
14, 63, 9, 74, 13,
75, 11, 78, 15, -1};
/* for xterm with 256 colors... */
static int color_numbers_256[28] = {0, 4, 2, 6,
1, 5, 130, 130,
248, 248,
7, 7, 242, 242,
12, 81, 10, 121,
14, 159, 9, 224, 13,
225, 11, 229, 15, -1};
/* for terminals with less than 16 colors... */
static int color_numbers_8[28] = {0, 4, 2, 6,
1, 5, 3, 3,
7, 7,
7, 7, 0+8, 0+8,
4+8, 4+8, 2+8, 2+8,
6+8, 6+8, 1+8, 1+8, 5+8,
5+8, 3+8, 3+8, 7+8, -1};
#if defined(__QNXNTO__)
static int *color_numbers_8_qansi = color_numbers_8;
/* On qnx, the 8 & 16 color arrays are the same */
Expand All @@ -7782,57 +7853,9 @@ do_highlight(
break;
}

/* Use the _16 table to check if it's a valid color name. */
color = color_numbers_16[i];
if (color >= 0)
{
if (t_colors == 8)
{
/* t_Co is 8: use the 8 colors table */
#if defined(__QNXNTO__)
color = color_numbers_8_qansi[i];
#else
color = color_numbers_8[i];
#endif
if (key[5] == 'F')
{
/* set/reset bold attribute to get light foreground
* colors (on some terminals, e.g. "linux") */
if (color & 8)
{
HL_TABLE()[idx].sg_cterm |= HL_BOLD;
HL_TABLE()[idx].sg_cterm_bold = TRUE;
}
else
HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
}
color &= 7; /* truncate to 8 colors */
}
else if (t_colors == 16 || t_colors == 88
|| t_colors >= 256)
{
/*
* Guess: if the termcap entry ends in 'm', it is
* probably an xterm-like terminal. Use the changed
* order for colors.
*/
if (*T_CAF != NUL)
p = T_CAF;
else
p = T_CSF;
if (*p != NUL && (t_colors > 256
|| *(p + STRLEN(p) - 1) == 'm'))
{
if (t_colors == 88)
color = color_numbers_88[i];
else if (t_colors >= 256)
color = color_numbers_256[i];
else
color = color_numbers_8[i];
}
}
}
color = lookup_color(i, key[5] == 'F');
}

/* Add one to the argument, to avoid zero. Zero is used for
* "NONE", then "color" is -1. */
if (key[5] == 'F')
Expand Down
40 changes: 21 additions & 19 deletions src/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* while, if the terminal window is visible, the screen contents is drawn.
*
* TODO:
* - if 'term' starts witth "xterm" use it for $TERM.
* - To set BS correctly, check get_stty(); Pass the fd of the pty.
* - include functions from #1871
* - do not store terminal buffer in viminfo. Or prefix term:// ?
Expand Down Expand Up @@ -755,81 +756,82 @@ static VTermScreenCallbacks screen_callbacks = {
* First color is 1. Return 0 if no match found.
*/
static int
color2index(VTermColor *color)
color2index(VTermColor *color, int foreground)
{
int red = color->red;
int blue = color->blue;
int green = color->green;

/* The argument for lookup_color() is for the color_names[] table. */
if (red == 0)
{
if (green == 0)
{
if (blue == 0)
return 1; /* black */
return lookup_color(0, foreground) + 1; /* black */
if (blue == 224)
return 5; /* blue */
return lookup_color(1, foreground) + 1; /* dark blue */
}
else if (green == 224)
{
if (blue == 0)
return 3; /* green */
return lookup_color(2, foreground) + 1; /* dark green */
if (blue == 224)
return 7; /* cyan */
return lookup_color(3, foreground) + 1; /* dark cyan */
}
}
else if (red == 224)
{
if (green == 0)
{
if (blue == 0)
return 2; /* red */
return lookup_color(4, foreground) + 1; /* dark red */
if (blue == 224)
return 6; /* magenta */
return lookup_color(5, foreground) + 1; /* dark magenta */
}
else if (green == 224)
{
if (blue == 0)
return 4; /* yellow */
return lookup_color(6, foreground) + 1; /* dark yellow / brown */
if (blue == 224)
return 8; /* white */
return lookup_color(8, foreground) + 1; /* white / light grey */
}
}
else if (red == 128)
{
if (green == 128 && blue == 128)
return 9; /* high intensity black */
return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
}
else if (red == 255)
{
if (green == 64)
{
if (blue == 64)
return 10; /* high intensity red */
return lookup_color(20, foreground) + 1; /* light red */
if (blue == 255)
return 14; /* high intensity magenta */
return lookup_color(22, foreground) + 1; /* light magenta */
}
else if (green == 255)
{
if (blue == 64)
return 12; /* high intensity yellow */
return lookup_color(24, foreground) + 1; /* yellow */
if (blue == 255)
return 16; /* high intensity white */
return lookup_color(26, foreground) + 1; /* white */
}
}
else if (red == 64)
{
if (green == 64)
{
if (blue == 255)
return 13; /* high intensity blue */
return lookup_color(14, foreground) + 1; /* light blue */
}
else if (green == 255)
{
if (blue == 64)
return 11; /* high intensity green */
return lookup_color(16, foreground) + 1; /* light green */
if (blue == 255)
return 15; /* high intensity cyan */
return lookup_color(18, foreground) + 1; /* light cyan */
}
}
if (t_colors >= 256)
Expand Down Expand Up @@ -902,8 +904,8 @@ cell2attr(VTermScreenCell *cell)
else
#endif
{
return get_cterm_attr_idx(attr, color2index(&cell->fg),
color2index(&cell->bg));
return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
color2index(&cell->bg, FALSE));
}
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
791,
/**/
790,
/**/
Expand Down

0 comments on commit b41bf8e

Please sign in to comment.