mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	api/vim: allow guis and tests to retrieve the entire color table
This commit is contained in:
		 Björn Linse
					Björn Linse
				
			
				
					committed by
					
						 Thiago de Arruda
						Thiago de Arruda
					
				
			
			
				
	
			
			
			 Thiago de Arruda
						Thiago de Arruda
					
				
			
						parent
						
							ae2b747e64
						
					
				
				
					commit
					f468fb70cb
				
			| @@ -552,6 +552,18 @@ Integer vim_name_to_color(String name) | |||||||
|   return name_to_color((uint8_t *)name.data); |   return name_to_color((uint8_t *)name.data); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | Dictionary vim_get_color_map(void) | ||||||
|  | { | ||||||
|  |   Dictionary colors = ARRAY_DICT_INIT; | ||||||
|  |  | ||||||
|  |   for (int i = 0; color_name_table[i].name != NULL; i++) { | ||||||
|  |     PUT(colors, color_name_table[i].name, | ||||||
|  |         INTEGER_OBJ(color_name_table[i].color)); | ||||||
|  |   } | ||||||
|  |   return colors; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Array vim_get_api_info(uint64_t channel_id) | Array vim_get_api_info(uint64_t channel_id) | ||||||
| { | { | ||||||
|   Array rv = ARRAY_DICT_INIT; |   Array rv = ARRAY_DICT_INIT; | ||||||
|   | |||||||
| @@ -7645,14 +7645,8 @@ char_u *get_highlight_name(expand_T *xp, int idx) | |||||||
|   return HL_TABLE()[idx].sg_name; |   return HL_TABLE()[idx].sg_name; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| RgbValue name_to_color(uint8_t *name) |  | ||||||
| { |  | ||||||
| #define RGB(r, g, b) ((r << 16) | (g << 8) | b) | #define RGB(r, g, b) ((r << 16) | (g << 8) | b) | ||||||
|   static struct { | color_name_table_T color_name_table[] = { | ||||||
|     char *name; |  | ||||||
|     RgbValue color; |  | ||||||
|   } color_name_table[] = { |  | ||||||
|   // Color names taken from |   // Color names taken from | ||||||
|   // http://www.rapidtables.com/web/color/RGB_Color.htm |   // http://www.rapidtables.com/web/color/RGB_Color.htm | ||||||
|   {"Maroon", RGB(0x80, 0x00, 0x00)}, |   {"Maroon", RGB(0x80, 0x00, 0x00)}, | ||||||
| @@ -7824,6 +7818,9 @@ RgbValue name_to_color(uint8_t *name) | |||||||
|   {NULL, 0}, |   {NULL, 0}, | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | RgbValue name_to_color(uint8_t *name) | ||||||
|  | { | ||||||
|  |  | ||||||
|   if (name[0] == '#' && isxdigit(name[1]) && isxdigit(name[2]) |   if (name[0] == '#' && isxdigit(name[1]) && isxdigit(name[2]) | ||||||
|       && isxdigit(name[3]) && isxdigit(name[4]) && isxdigit(name[5]) |       && isxdigit(name[3]) && isxdigit(name[4]) && isxdigit(name[5]) | ||||||
|       && isxdigit(name[6]) && name[7] == NUL) { |       && isxdigit(name[6]) && name[7] == NUL) { | ||||||
|   | |||||||
| @@ -38,6 +38,12 @@ | |||||||
| #define HL_CONCEAL     0x20000 /* can be concealed */ | #define HL_CONCEAL     0x20000 /* can be concealed */ | ||||||
| #define HL_CONCEALENDS 0x40000 /* can be concealed */ | #define HL_CONCEALENDS 0x40000 /* can be concealed */ | ||||||
|  |  | ||||||
|  | typedef struct { | ||||||
|  |   char *name; | ||||||
|  |   RgbValue color; | ||||||
|  | } color_name_table_T; | ||||||
|  | extern color_name_table_T color_name_table[]; | ||||||
|  |  | ||||||
| #ifdef INCLUDE_GENERATED_DECLARATIONS | #ifdef INCLUDE_GENERATED_DECLARATIONS | ||||||
| # include "syntax.h.generated.h" | # include "syntax.h.generated.h" | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -29,14 +29,12 @@ end) | |||||||
| describe('Default highlight groups', function() | describe('Default highlight groups', function() | ||||||
|   -- Test the default attributes for highlight groups shown by the :highlight |   -- Test the default attributes for highlight groups shown by the :highlight | ||||||
|   -- command |   -- command | ||||||
|   local screen, hlgroup_colors |   local screen | ||||||
|  |  | ||||||
|   setup(function() |   local hlgroup_colors = { | ||||||
|     hlgroup_colors = { |     NonText = Screen.colors.Blue, | ||||||
|       NonText = nvim('name_to_color', 'Blue'), |     Question = Screen.colors.SeaGreen | ||||||
|       Question = nvim('name_to_color', 'SeaGreen') |  | ||||||
|   } |   } | ||||||
|   end) |  | ||||||
|  |  | ||||||
|   before_each(function() |   before_each(function() | ||||||
|     clear() |     clear() | ||||||
|   | |||||||
| @@ -4,14 +4,12 @@ local clear, feed, nvim = helpers.clear, helpers.feed, helpers.nvim | |||||||
| local insert, execute = helpers.insert, helpers.execute | local insert, execute = helpers.insert, helpers.execute | ||||||
|  |  | ||||||
| describe('Mouse input', function() | describe('Mouse input', function() | ||||||
|   local screen, hlgroup_colors |   local screen | ||||||
|  |  | ||||||
|   setup(function() |   local hlgroup_colors = { | ||||||
|     hlgroup_colors = { |     NonText = Screen.colors.Blue, | ||||||
|       NonText = nvim('name_to_color', 'Blue'), |     Visual = Screen.colors.LightGrey | ||||||
|       Visual = nvim('name_to_color', 'LightGrey'), |  | ||||||
|   } |   } | ||||||
|   end) |  | ||||||
|  |  | ||||||
|   before_each(function() |   before_each(function() | ||||||
|     clear() |     clear() | ||||||
|   | |||||||
| @@ -65,7 +65,7 @@ | |||||||
| -- attribute(which normally is), here's how the call to "expect" should look | -- attribute(which normally is), here's how the call to "expect" should look | ||||||
| -- like: | -- like: | ||||||
| -- | -- | ||||||
| --     NonText = nvim('name_to_color', 'Blue'), | --     NonText = Screen.colors.Blue | ||||||
| --     screen:expect([[ | --     screen:expect([[ | ||||||
| --       hello screen             \ | --       hello screen             \ | ||||||
| --       ~                        \ | --       ~                        \ | ||||||
| @@ -86,7 +86,7 @@ | |||||||
| -- | -- | ||||||
| -- Multiple expect:s will likely share a group of attribute sets to test. | -- Multiple expect:s will likely share a group of attribute sets to test. | ||||||
| -- Therefore these could be specified at the beginning of a test like this: | -- Therefore these could be specified at the beginning of a test like this: | ||||||
| --    NonText = nvim('name_to_color', 'Blue') | --    NonText = Screen.colors.Blue | ||||||
| --    screen:set_default_attr_ids( { | --    screen:set_default_attr_ids( { | ||||||
| --      [1] = {reverse = true, bold = true}, | --      [1] = {reverse = true, bold = true}, | ||||||
| --      [2] = {reverse = true} | --      [2] = {reverse = true} | ||||||
| @@ -119,6 +119,16 @@ if os.getenv('VALGRIND') then | |||||||
|   default_screen_timeout = 7500 |   default_screen_timeout = 7500 | ||||||
| end | end | ||||||
|  |  | ||||||
|  | local colors = request('vim_get_color_map') | ||||||
|  | local colornames = {} | ||||||
|  | for name, rgb in pairs(colors) do | ||||||
|  |     -- we disregard the case that colornames might not be unique, as | ||||||
|  |     -- this is just a helper to get any canonical name of a color | ||||||
|  |     colornames[rgb] = name | ||||||
|  | end | ||||||
|  |  | ||||||
|  | Screen.colors = colors | ||||||
|  |  | ||||||
| function Screen.debug(command) | function Screen.debug(command) | ||||||
|   if not command then |   if not command then | ||||||
|     command = 'pynvim -n -g -c ' |     command = 'pynvim -n -g -c ' | ||||||
| @@ -479,7 +489,13 @@ end | |||||||
| function pprint_attrs(attrs) | function pprint_attrs(attrs) | ||||||
|     local items = {} |     local items = {} | ||||||
|     for f, v in pairs(attrs) do |     for f, v in pairs(attrs) do | ||||||
|       table.insert(items, f.." = "..tostring(v)) |       local desc = tostring(v) | ||||||
|  |       if f == "foreground" or f == "background" then | ||||||
|  |         if colornames[v] ~= nil then | ||||||
|  |           desc = "Screen.colors."..colornames[v] | ||||||
|  |         end | ||||||
|  |       end | ||||||
|  |       table.insert(items, f.." = "..desc) | ||||||
|     end |     end | ||||||
|     return table.concat(items, ", ") |     return table.concat(items, ", ") | ||||||
| end | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user