diff options
-rw-r--r-- | src/nvim/syntax.c | 24 | ||||
-rw-r--r-- | test/functional/terminal/highlight_spec.lua | 54 |
2 files changed, 76 insertions, 2 deletions
diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index bec2b5c5b0..c753c6fabd 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -6597,6 +6597,9 @@ do_highlight ( else { if (is_normal_group) { HL_TABLE()[idx].sg_attr = 0; + // Need to update all groups, because they might be using "bg" and/or + // "fg", which have been changed now. + highlight_attr_set_all(); // If the normal group has changed, it is simpler to refresh every UI ui_refresh(); } else @@ -7261,6 +7264,23 @@ int syn_get_final_id(int hl_id) return hl_id; } +/// Refresh the color attributes of all highlight groups. +static void highlight_attr_set_all(void) +{ + for (int idx = 0; idx < highlight_ga.ga_len; idx++) { + struct hl_group *sgp = &HL_TABLE()[idx]; + if (sgp->sg_rgb_bg_name != NULL) { + sgp->sg_rgb_bg = name_to_color(sgp->sg_rgb_bg_name); + } + if (sgp->sg_rgb_fg_name != NULL) { + sgp->sg_rgb_fg = name_to_color(sgp->sg_rgb_fg_name); + } + if (sgp->sg_rgb_sp_name != NULL) { + sgp->sg_rgb_sp = name_to_color(sgp->sg_rgb_sp_name); + } + set_hl_attr(idx); + } +} /* * Translate the 'highlight' option into attributes in highlight_attr[] and @@ -7702,6 +7722,10 @@ RgbValue name_to_color(uint8_t *name) && isxdigit(name[6]) && name[7] == NUL) { // rgb hex string return strtol((char *)(name + 1), NULL, 16); + } else if (!STRICMP(name, "bg") || !STRICMP(name, "background")) { + return normal_bg; + } else if (!STRICMP(name, "fg") || !STRICMP(name, "foreground")) { + return normal_fg; } for (int i = 0; color_name_table[i].name != NULL; i++) { diff --git a/test/functional/terminal/highlight_spec.lua b/test/functional/terminal/highlight_spec.lua index 30377d51bb..b3ac60314f 100644 --- a/test/functional/terminal/highlight_spec.lua +++ b/test/functional/terminal/highlight_spec.lua @@ -195,8 +195,8 @@ describe('synIDattr()', function() it('returns #RRGGBB value for fg#/bg#/sp#', function() screen:attach({rgb=true}) - eq('#ff0000', eval('synIDattr(hlID("Normal"), "fg#")')) - eq('#000000', eval('synIDattr(hlID("Normal"), "bg#")')) + eq('#ff0000', eval('synIDattr(hlID("Normal"), "fg#")')) + eq('#000000', eval('synIDattr(hlID("Normal"), "bg#")')) eq('#fa8072', eval('synIDattr(hlID("Keyword"), "fg#")')) eq('#800000', eval('synIDattr(hlID("Keyword"), "sp#")')) end) @@ -207,3 +207,53 @@ describe('synIDattr()', function() eq('79', eval('synIDattr(hlID("Keyword"), "fg")')) end) end) + +describe('fg/bg special colors', function() + local screen + before_each(function() + clear() + screen = Screen.new(50, 7) + execute('highlight Normal ctermfg=145 ctermbg=16 guifg=#ff0000 guibg=Black') + execute('highlight Visual ctermfg=bg ctermbg=fg guifg=bg guibg=fg guisp=bg') + end) + + it('resolve to "Normal" values', function() + eq(eval('synIDattr(hlID("Normal"), "bg")'), + eval('synIDattr(hlID("Visual"), "fg")')) + eq(eval('synIDattr(hlID("Normal"), "bg#")'), + eval('synIDattr(hlID("Visual"), "fg#")')) + eq(eval('synIDattr(hlID("Normal"), "fg")'), + eval('synIDattr(hlID("Visual"), "bg")')) + eq(eval('synIDattr(hlID("Normal"), "fg#")'), + eval('synIDattr(hlID("Visual"), "bg#")')) + eq('bg', eval('synIDattr(hlID("Visual"), "fg", "gui")')) + eq('bg', eval('synIDattr(hlID("Visual"), "fg#", "gui")')) + eq('fg', eval('synIDattr(hlID("Visual"), "bg", "gui")')) + eq('fg', eval('synIDattr(hlID("Visual"), "bg#", "gui")')) + eq('bg', eval('synIDattr(hlID("Visual"), "sp", "gui")')) + eq('bg', eval('synIDattr(hlID("Visual"), "sp#", "gui")')) + end) + + it('resolve to "Normal" values in RGB-capable UI', function() + screen:attach({rgb=true}) + eq('bg', eval('synIDattr(hlID("Visual"), "fg")')) + eq(eval('synIDattr(hlID("Normal"), "bg#")'), + eval('synIDattr(hlID("Visual"), "fg#")')) + eq('fg', eval('synIDattr(hlID("Visual"), "bg")')) + eq(eval('synIDattr(hlID("Normal"), "fg#")'), + eval('synIDattr(hlID("Visual"), "bg#")')) + eq('bg', eval('synIDattr(hlID("Visual"), "sp")')) + eq(eval('synIDattr(hlID("Normal"), "bg#")'), + eval('synIDattr(hlID("Visual"), "sp#")')) + end) + + it('resolve after the "Normal" group is modified', function() + screen:attach({rgb=true}) + local new_guibg = '#282c34' + local new_guifg = '#abb2bf' + execute('highlight Normal guifg='..new_guifg..' guibg='..new_guibg) + eq(new_guibg, eval('synIDattr(hlID("Visual"), "fg#")')) + eq(new_guifg, eval('synIDattr(hlID("Visual"), "bg#")')) + eq(new_guibg, eval('synIDattr(hlID("Visual"), "sp#")')) + end) +end) |