Skip to content

Commit b41bf8e

Browse files
committed
patch 8.0.0791: terminal colors depend on the system
Problem: Terminal colors depend on the system. Solution: Use the highlight color lookup tables.
1 parent fa228f7 commit b41bf8e

4 files changed

Lines changed: 136 additions & 108 deletions

File tree

src/proto/syntax.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void ex_syntime(exarg_T *eap);
2323
char_u *get_syntime_arg(expand_T *xp, int idx);
2424
void init_highlight(int both, int reset);
2525
int load_colors(char_u *name);
26+
int lookup_color(int idx, int foreground);
2627
void do_highlight(char_u *line, int forceit, int init);
2728
void free_highlight(void);
2829
void restore_cterm_colors(void);

src/syntax.c

Lines changed: 112 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7221,6 +7221,116 @@ load_colors(char_u *name)
72217221
return retval;
72227222
}
72237223

7224+
static char *(color_names[28]) = {
7225+
"Black", "DarkBlue", "DarkGreen", "DarkCyan",
7226+
"DarkRed", "DarkMagenta", "Brown", "DarkYellow",
7227+
"Gray", "Grey", "LightGray", "LightGrey",
7228+
"DarkGray", "DarkGrey",
7229+
"Blue", "LightBlue", "Green", "LightGreen",
7230+
"Cyan", "LightCyan", "Red", "LightRed", "Magenta",
7231+
"LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
7232+
/* indices:
7233+
* 0, 1, 2, 3,
7234+
* 4, 5, 6, 7,
7235+
* 8, 9, 10, 11,
7236+
* 12, 13,
7237+
* 14, 15, 16, 17,
7238+
* 18, 19, 20, 21, 22,
7239+
* 23, 24, 25, 26, 27 */
7240+
static int color_numbers_16[28] = {0, 1, 2, 3,
7241+
4, 5, 6, 6,
7242+
7, 7, 7, 7,
7243+
8, 8,
7244+
9, 9, 10, 10,
7245+
11, 11, 12, 12, 13,
7246+
13, 14, 14, 15, -1};
7247+
/* for xterm with 88 colors... */
7248+
static int color_numbers_88[28] = {0, 4, 2, 6,
7249+
1, 5, 32, 72,
7250+
84, 84, 7, 7,
7251+
82, 82,
7252+
12, 43, 10, 61,
7253+
14, 63, 9, 74, 13,
7254+
75, 11, 78, 15, -1};
7255+
/* for xterm with 256 colors... */
7256+
static int color_numbers_256[28] = {0, 4, 2, 6,
7257+
1, 5, 130, 130,
7258+
248, 248, 7, 7,
7259+
242, 242,
7260+
12, 81, 10, 121,
7261+
14, 159, 9, 224, 13,
7262+
225, 11, 229, 15, -1};
7263+
/* for terminals with less than 16 colors... */
7264+
static int color_numbers_8[28] = {0, 4, 2, 6,
7265+
1, 5, 3, 3,
7266+
7, 7, 7, 7,
7267+
0+8, 0+8,
7268+
4+8, 4+8, 2+8, 2+8,
7269+
6+8, 6+8, 1+8, 1+8, 5+8,
7270+
5+8, 3+8, 3+8, 7+8, -1};
7271+
7272+
/*
7273+
* Lookup the "cterm" value to be used for color with index "idx" in
7274+
* color_names[].
7275+
*/
7276+
int
7277+
lookup_color(int idx, int foreground)
7278+
{
7279+
int color = color_numbers_16[idx];
7280+
char_u *p;
7281+
7282+
/* Use the _16 table to check if it's a valid color name. */
7283+
if (color < 0)
7284+
return -1;
7285+
7286+
if (t_colors == 8)
7287+
{
7288+
/* t_Co is 8: use the 8 colors table */
7289+
#if defined(__QNXNTO__)
7290+
color = color_numbers_8_qansi[idx];
7291+
#else
7292+
color = color_numbers_8[idx];
7293+
#endif
7294+
if (foreground)
7295+
{
7296+
/* set/reset bold attribute to get light foreground
7297+
* colors (on some terminals, e.g. "linux") */
7298+
if (color & 8)
7299+
{
7300+
HL_TABLE()[idx].sg_cterm |= HL_BOLD;
7301+
HL_TABLE()[idx].sg_cterm_bold = TRUE;
7302+
}
7303+
else
7304+
HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
7305+
}
7306+
color &= 7; /* truncate to 8 colors */
7307+
}
7308+
else if (t_colors == 16 || t_colors == 88
7309+
|| t_colors >= 256)
7310+
{
7311+
/*
7312+
* Guess: if the termcap entry ends in 'm', it is
7313+
* probably an xterm-like terminal. Use the changed
7314+
* order for colors.
7315+
*/
7316+
if (*T_CAF != NUL)
7317+
p = T_CAF;
7318+
else
7319+
p = T_CSF;
7320+
if (*p != NUL && (t_colors > 256
7321+
|| *(p + STRLEN(p) - 1) == 'm'))
7322+
{
7323+
if (t_colors == 88)
7324+
color = color_numbers_88[idx];
7325+
else if (t_colors >= 256)
7326+
color = color_numbers_256[idx];
7327+
else
7328+
color = color_numbers_8[idx];
7329+
}
7330+
}
7331+
return color;
7332+
}
7333+
72247334
/*
72257335
* Handle the ":highlight .." command.
72267336
* When using ":hi clear" this is called recursively for each group with
@@ -7723,45 +7833,6 @@ do_highlight(
77237833
}
77247834
else
77257835
{
7726-
static char *(color_names[28]) = {
7727-
"Black", "DarkBlue", "DarkGreen", "DarkCyan",
7728-
"DarkRed", "DarkMagenta", "Brown", "DarkYellow",
7729-
"Gray", "Grey",
7730-
"LightGray", "LightGrey", "DarkGray", "DarkGrey",
7731-
"Blue", "LightBlue", "Green", "LightGreen",
7732-
"Cyan", "LightCyan", "Red", "LightRed", "Magenta",
7733-
"LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
7734-
static int color_numbers_16[28] = {0, 1, 2, 3,
7735-
4, 5, 6, 6,
7736-
7, 7,
7737-
7, 7, 8, 8,
7738-
9, 9, 10, 10,
7739-
11, 11, 12, 12, 13,
7740-
13, 14, 14, 15, -1};
7741-
/* for xterm with 88 colors... */
7742-
static int color_numbers_88[28] = {0, 4, 2, 6,
7743-
1, 5, 32, 72,
7744-
84, 84,
7745-
7, 7, 82, 82,
7746-
12, 43, 10, 61,
7747-
14, 63, 9, 74, 13,
7748-
75, 11, 78, 15, -1};
7749-
/* for xterm with 256 colors... */
7750-
static int color_numbers_256[28] = {0, 4, 2, 6,
7751-
1, 5, 130, 130,
7752-
248, 248,
7753-
7, 7, 242, 242,
7754-
12, 81, 10, 121,
7755-
14, 159, 9, 224, 13,
7756-
225, 11, 229, 15, -1};
7757-
/* for terminals with less than 16 colors... */
7758-
static int color_numbers_8[28] = {0, 4, 2, 6,
7759-
1, 5, 3, 3,
7760-
7, 7,
7761-
7, 7, 0+8, 0+8,
7762-
4+8, 4+8, 2+8, 2+8,
7763-
6+8, 6+8, 1+8, 1+8, 5+8,
7764-
5+8, 3+8, 3+8, 7+8, -1};
77657836
#if defined(__QNXNTO__)
77667837
static int *color_numbers_8_qansi = color_numbers_8;
77677838
/* On qnx, the 8 & 16 color arrays are the same */
@@ -7782,57 +7853,9 @@ do_highlight(
77827853
break;
77837854
}
77847855

7785-
/* Use the _16 table to check if it's a valid color name. */
7786-
color = color_numbers_16[i];
7787-
if (color >= 0)
7788-
{
7789-
if (t_colors == 8)
7790-
{
7791-
/* t_Co is 8: use the 8 colors table */
7792-
#if defined(__QNXNTO__)
7793-
color = color_numbers_8_qansi[i];
7794-
#else
7795-
color = color_numbers_8[i];
7796-
#endif
7797-
if (key[5] == 'F')
7798-
{
7799-
/* set/reset bold attribute to get light foreground
7800-
* colors (on some terminals, e.g. "linux") */
7801-
if (color & 8)
7802-
{
7803-
HL_TABLE()[idx].sg_cterm |= HL_BOLD;
7804-
HL_TABLE()[idx].sg_cterm_bold = TRUE;
7805-
}
7806-
else
7807-
HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
7808-
}
7809-
color &= 7; /* truncate to 8 colors */
7810-
}
7811-
else if (t_colors == 16 || t_colors == 88
7812-
|| t_colors >= 256)
7813-
{
7814-
/*
7815-
* Guess: if the termcap entry ends in 'm', it is
7816-
* probably an xterm-like terminal. Use the changed
7817-
* order for colors.
7818-
*/
7819-
if (*T_CAF != NUL)
7820-
p = T_CAF;
7821-
else
7822-
p = T_CSF;
7823-
if (*p != NUL && (t_colors > 256
7824-
|| *(p + STRLEN(p) - 1) == 'm'))
7825-
{
7826-
if (t_colors == 88)
7827-
color = color_numbers_88[i];
7828-
else if (t_colors >= 256)
7829-
color = color_numbers_256[i];
7830-
else
7831-
color = color_numbers_8[i];
7832-
}
7833-
}
7834-
}
7856+
color = lookup_color(i, key[5] == 'F');
78357857
}
7858+
78367859
/* Add one to the argument, to avoid zero. Zero is used for
78377860
* "NONE", then "color" is -1. */
78387861
if (key[5] == 'F')

src/terminal.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* while, if the terminal window is visible, the screen contents is drawn.
3434
*
3535
* TODO:
36+
* - if 'term' starts witth "xterm" use it for $TERM.
3637
* - To set BS correctly, check get_stty(); Pass the fd of the pty.
3738
* - include functions from #1871
3839
* - do not store terminal buffer in viminfo. Or prefix term:// ?
@@ -755,81 +756,82 @@ static VTermScreenCallbacks screen_callbacks = {
755756
* First color is 1. Return 0 if no match found.
756757
*/
757758
static int
758-
color2index(VTermColor *color)
759+
color2index(VTermColor *color, int foreground)
759760
{
760761
int red = color->red;
761762
int blue = color->blue;
762763
int green = color->green;
763764

765+
/* The argument for lookup_color() is for the color_names[] table. */
764766
if (red == 0)
765767
{
766768
if (green == 0)
767769
{
768770
if (blue == 0)
769-
return 1; /* black */
771+
return lookup_color(0, foreground) + 1; /* black */
770772
if (blue == 224)
771-
return 5; /* blue */
773+
return lookup_color(1, foreground) + 1; /* dark blue */
772774
}
773775
else if (green == 224)
774776
{
775777
if (blue == 0)
776-
return 3; /* green */
778+
return lookup_color(2, foreground) + 1; /* dark green */
777779
if (blue == 224)
778-
return 7; /* cyan */
780+
return lookup_color(3, foreground) + 1; /* dark cyan */
779781
}
780782
}
781783
else if (red == 224)
782784
{
783785
if (green == 0)
784786
{
785787
if (blue == 0)
786-
return 2; /* red */
788+
return lookup_color(4, foreground) + 1; /* dark red */
787789
if (blue == 224)
788-
return 6; /* magenta */
790+
return lookup_color(5, foreground) + 1; /* dark magenta */
789791
}
790792
else if (green == 224)
791793
{
792794
if (blue == 0)
793-
return 4; /* yellow */
795+
return lookup_color(6, foreground) + 1; /* dark yellow / brown */
794796
if (blue == 224)
795-
return 8; /* white */
797+
return lookup_color(8, foreground) + 1; /* white / light grey */
796798
}
797799
}
798800
else if (red == 128)
799801
{
800802
if (green == 128 && blue == 128)
801-
return 9; /* high intensity black */
803+
return lookup_color(12, foreground) + 1; /* high intensity black / dark grey */
802804
}
803805
else if (red == 255)
804806
{
805807
if (green == 64)
806808
{
807809
if (blue == 64)
808-
return 10; /* high intensity red */
810+
return lookup_color(20, foreground) + 1; /* light red */
809811
if (blue == 255)
810-
return 14; /* high intensity magenta */
812+
return lookup_color(22, foreground) + 1; /* light magenta */
811813
}
812814
else if (green == 255)
813815
{
814816
if (blue == 64)
815-
return 12; /* high intensity yellow */
817+
return lookup_color(24, foreground) + 1; /* yellow */
816818
if (blue == 255)
817-
return 16; /* high intensity white */
819+
return lookup_color(26, foreground) + 1; /* white */
818820
}
819821
}
820822
else if (red == 64)
821823
{
822824
if (green == 64)
823825
{
824826
if (blue == 255)
825-
return 13; /* high intensity blue */
827+
return lookup_color(14, foreground) + 1; /* light blue */
826828
}
827829
else if (green == 255)
828830
{
829831
if (blue == 64)
830-
return 11; /* high intensity green */
832+
return lookup_color(16, foreground) + 1; /* light green */
831833
if (blue == 255)
832-
return 15; /* high intensity cyan */
834+
return lookup_color(18, foreground) + 1; /* light cyan */
833835
}
834836
}
835837
if (t_colors >= 256)
@@ -902,8 +904,8 @@ cell2attr(VTermScreenCell *cell)
902904
else
903905
#endif
904906
{
905-
return get_cterm_attr_idx(attr, color2index(&cell->fg),
906-
color2index(&cell->bg));
907+
return get_cterm_attr_idx(attr, color2index(&cell->fg, TRUE),
908+
color2index(&cell->bg, FALSE));
907909
}
908910
return 0;
909911
}

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+
791,
772774
/**/
773775
790,
774776
/**/

0 commit comments

Comments
 (0)