comparison src/term.c @ 9591:201773c00b96 v7.4.2073

commit https://github.com/vim/vim/commit/68015bbd846181d49842d6ef60246c4195d20b89 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jul 19 21:05:21 2016 +0200 patch 7.4.2073 Problem: rgb.txt is read for every color name. Solution: Load rgb.txt once. (Christian Brabandt) Add a test.
author Christian Brabandt <cb@256bit.org>
date Tue, 19 Jul 2016 21:15:05 +0200
parents b2aada04d84e
children 9cc49b86f613
comparison
equal deleted inserted replaced
9590:628cf2af3da0 9591:201773c00b96
6084 struct rgbcolor_table_S { 6084 struct rgbcolor_table_S {
6085 char_u *color_name; 6085 char_u *color_name;
6086 guicolor_T color; 6086 guicolor_T color;
6087 }; 6087 };
6088 6088
6089 /* Only non X11 colors (not present in rgb.txt) and colors in
6090 * color_names[], useful when $VIMRUNTIME is not found,. */
6089 static struct rgbcolor_table_S rgb_table[] = { 6091 static struct rgbcolor_table_S rgb_table[] = {
6090 {(char_u *)"black", RGB(0x00, 0x00, 0x00)}, 6092 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6091 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)}, 6093 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6092 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)}, 6094 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6093 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)}, 6095 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6098 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)}, 6100 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6099 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)}, 6101 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6100 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)}, 6102 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
6101 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */ 6103 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
6102 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)}, 6104 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
6103 {(char_u *)"gray10", RGB(0x1A, 0x1A, 0x1A)},
6104 {(char_u *)"gray20", RGB(0x33, 0x33, 0x33)},
6105 {(char_u *)"gray30", RGB(0x4D, 0x4D, 0x4D)},
6106 {(char_u *)"gray40", RGB(0x66, 0x66, 0x66)},
6107 {(char_u *)"gray50", RGB(0x7F, 0x7F, 0x7F)},
6108 {(char_u *)"gray60", RGB(0x99, 0x99, 0x99)},
6109 {(char_u *)"gray70", RGB(0xB3, 0xB3, 0xB3)},
6110 {(char_u *)"gray80", RGB(0xCC, 0xCC, 0xCC)},
6111 {(char_u *)"gray90", RGB(0xE5, 0xE5, 0xE5)},
6112 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)}, 6105 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6113 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)}, 6106 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
6114 {(char_u *)"grey10", RGB(0x1A, 0x1A, 0x1A)},
6115 {(char_u *)"grey20", RGB(0x33, 0x33, 0x33)},
6116 {(char_u *)"grey30", RGB(0x4D, 0x4D, 0x4D)},
6117 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
6118 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
6119 {(char_u *)"grey60", RGB(0x99, 0x99, 0x99)},
6120 {(char_u *)"grey70", RGB(0xB3, 0xB3, 0xB3)},
6121 {(char_u *)"grey80", RGB(0xCC, 0xCC, 0xCC)},
6122 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
6123 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)}, 6107 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6124 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)}, 6108 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6125 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)}, 6109 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6126 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)}, 6110 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6127 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)}, 6111 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
6128 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */ 6112 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
6129 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */ 6113 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
6130 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)}, 6114 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6131 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)}, 6115 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
6132 {(char_u *)"orange", RGB(0xFF, 0xA5, 0x00)},
6133 {(char_u *)"purple", RGB(0xA0, 0x20, 0xF0)},
6134 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)}, 6116 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
6135 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
6136 {(char_u *)"slateblue", RGB(0x6A, 0x5A, 0xCD)},
6137 {(char_u *)"violet", RGB(0xEE, 0x82, 0xEE)},
6138 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)}, 6117 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6139 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)}, 6118 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
6140 }; 6119 };
6141 6120
6121 static struct rgbcolor_table_S *colornames_table;
6122 static int size = 0;
6142 6123
6143 if (name[0] == '#' && STRLEN(name) == 7) 6124 if (name[0] == '#' && STRLEN(name) == 7)
6144 { 6125 {
6145 /* Name is in "#rrggbb" format */ 6126 /* Name is in "#rrggbb" format */
6146 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])), 6127 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
6157 return rgb_table[i].color; 6138 return rgb_table[i].color;
6158 6139
6159 /* 6140 /*
6160 * Last attempt. Look in the file "$VIM/rgb.txt". 6141 * Last attempt. Look in the file "$VIM/rgb.txt".
6161 */ 6142 */
6162 6143 if (size == 0)
6163 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt"); 6144 {
6164 if (fname == NULL) 6145 int counting;
6165 return INVALCOLOR; 6146
6166 6147 /* colornames_table not yet initialized */
6167 fd = fopen((char *)fname, "rt"); 6148 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6168 vim_free(fname); 6149 if (fname == NULL)
6169 if (fd == NULL) 6150 return INVALCOLOR;
6170 { 6151
6171 if (p_verbose > 1) 6152 fd = fopen((char *)fname, "rt");
6172 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt")); 6153 vim_free(fname);
6173 return INVALCOLOR; 6154 if (fd == NULL)
6174 } 6155 {
6175 6156 if (p_verbose > 1)
6176 while (!feof(fd)) 6157 verb_msg((char_u *)_("Cannot open $VIMRUNTIME/rgb.txt"));
6177 { 6158 return INVALCOLOR;
6178 size_t len; 6159 }
6179 int pos; 6160
6180 6161 for (counting = 1; counting >= 0; --counting)
6181 ignoredp = fgets(line, LINE_LEN, fd); 6162 {
6182 len = strlen(line); 6163 if (!counting)
6183 6164 {
6184 if (len <= 1 || line[len - 1] != '\n') 6165 colornames_table = (struct rgbcolor_table_S *)alloc(
6185 continue; 6166 (unsigned)(sizeof(struct rgbcolor_table_S) * size));
6186 6167 if (colornames_table == NULL)
6187 line[len - 1] = '\0'; 6168 {
6188 6169 fclose(fd);
6189 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos); 6170 return INVALCOLOR;
6190 if (i != 3) 6171 }
6191 continue; 6172 rewind(fd);
6192 6173 }
6193 if (STRICMP(line + pos, name) == 0) 6174 size = 0;
6194 { 6175
6195 fclose(fd); 6176 while (!feof(fd))
6196 return (guicolor_T)RGB(r, g, b); 6177 {
6197 } 6178 size_t len;
6198 } 6179 int pos;
6199 fclose(fd); 6180
6181 ignoredp = fgets(line, LINE_LEN, fd);
6182 len = strlen(line);
6183
6184 if (len <= 1 || line[len - 1] != '\n')
6185 continue;
6186
6187 line[len - 1] = '\0';
6188
6189 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6190 if (i != 3)
6191 continue;
6192
6193 if (!counting)
6194 {
6195 char_u *s = vim_strsave((char_u *)line + pos);
6196
6197 if (s == NULL)
6198 return INVALCOLOR;
6199 colornames_table[size].color_name = s;
6200 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6201 }
6202 size++;
6203 }
6204 }
6205 fclose(fd);
6206 }
6207
6208 for (i = 0; i < size; i++)
6209 if (STRICMP(name, colornames_table[i].color_name) == 0)
6210 return colornames_table[i].color;
6211
6200 return INVALCOLOR; 6212 return INVALCOLOR;
6201 } 6213 }
6202 #endif 6214 #endif