From 876aaf2003d1a6eb8f0701cf11e1834751b28980 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 15 Feb 2022 23:40:06 +0000 Subject: fix(highlight): allow globals to be cleared - and reduce heap allocations Fixes #17420 --- src/nvim/syntax.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/nvim/syntax.c') diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 119f6e811f..962a48822f 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -6758,16 +6758,26 @@ void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id) { NULL, -1, NIL }, }; + char hex_name[8]; + char *name; + for (int j = 0; cattrs[j].dest; j++) { - if (cattrs[j].val != -1) { + if (cattrs[j].val < 0) { + XFREE_CLEAR(*cattrs[j].dest); + continue; + } + + if (cattrs[j].name.type == kObjectTypeString && cattrs[j].name.data.string.size) { + name = cattrs[j].name.data.string.data; + } else { + snprintf(hex_name, sizeof(hex_name), "#%06x", cattrs[j].val); + name = hex_name; + } + + if (!*cattrs[j].dest + || STRCMP(*cattrs[j].dest, name) != 0) { xfree(*cattrs[j].dest); - if (cattrs[j].name.type == kObjectTypeString && cattrs[j].name.data.string.size) { - *cattrs[j].dest = xstrdup(cattrs[j].name.data.string.data); - } else { - char hex_name[8]; - snprintf(hex_name, sizeof(hex_name), "#%06x", cattrs[j].val); - *cattrs[j].dest = xstrdup(hex_name); - } + *cattrs[j].dest = xstrdup(name); } } -- cgit From dc24eeb9febaa331e660e14c3c325fd0977b6b93 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 16 Feb 2022 20:38:40 +0000 Subject: feat(highlight): support color names for cterm --- src/nvim/syntax.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/nvim/syntax.c') diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 962a48822f..2b74c45478 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -8859,6 +8859,22 @@ RgbValue name_to_color(const char *name) return -1; } +int name_to_ctermcolor(const char *name) +{ + int i; + int off = TOUPPER_ASC(*name); + for (i = ARRAY_SIZE(color_names); --i >= 0;) { + if (off == color_names[i][0] + && STRICMP(name+1, color_names[i]+1) == 0) { + break; + } + } + if (i < 0) { + return -1; + } + TriState bold = kNone; + return lookup_color(i, false, &bold); +} /************************************** * End of Highlighting stuff * -- cgit