mirror of
https://github.com/neovim/neovim.git
synced 2025-10-16 23:06:14 +00:00
Merge pull request #14243 from shadmansaleh/Allow_cterm_colors_nvim_set_hl
API: Adding cterm support to nvim_set_hl
This commit is contained in:
@@ -221,6 +221,12 @@ Dictionary nvim__get_hl_defs(Integer ns_id, Error *err)
|
|||||||
/// in addition the following keys are also recognized:
|
/// in addition the following keys are also recognized:
|
||||||
/// `default`: don't override existing definition,
|
/// `default`: don't override existing definition,
|
||||||
/// like `hi default`
|
/// like `hi default`
|
||||||
|
/// `ctermfg`: sets foreground of cterm color
|
||||||
|
/// `ctermbg`: sets background of cterm color
|
||||||
|
/// `cterm` : cterm attribute map. sets attributed for
|
||||||
|
/// cterm colors. similer to `hi cterm`
|
||||||
|
/// Note: by default cterm attributes are
|
||||||
|
/// same as attributes of gui color
|
||||||
/// @param[out] err Error details, if any
|
/// @param[out] err Error details, if any
|
||||||
///
|
///
|
||||||
/// TODO: ns_id = 0, should modify :highlight namespace
|
/// TODO: ns_id = 0, should modify :highlight namespace
|
||||||
|
@@ -806,8 +806,11 @@ HlAttrs dict2hlattrs(Dictionary dict, bool use_rgb, int *link_id, Error *err)
|
|||||||
{
|
{
|
||||||
HlAttrs hlattrs = HLATTRS_INIT;
|
HlAttrs hlattrs = HLATTRS_INIT;
|
||||||
|
|
||||||
int32_t fg = -1, bg = -1, sp = -1;
|
int32_t fg = -1, bg = -1, ctermfg = -1, ctermbg = -1, sp = -1;
|
||||||
int16_t mask = 0;
|
int16_t mask = 0;
|
||||||
|
int16_t cterm_mask = 0;
|
||||||
|
bool cterm_mask_provided = false;
|
||||||
|
|
||||||
for (size_t i = 0; i < dict.size; i++) {
|
for (size_t i = 0; i < dict.size; i++) {
|
||||||
char *key = dict.items[i].key.data;
|
char *key = dict.items[i].key.data;
|
||||||
Object val = dict.items[i].value;
|
Object val = dict.items[i].value;
|
||||||
@@ -837,6 +840,25 @@ HlAttrs dict2hlattrs(Dictionary dict, bool use_rgb, int *link_id, Error *err)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle cterm attrs
|
||||||
|
if (strequal(key, "cterm") && val.type == kObjectTypeDictionary) {
|
||||||
|
cterm_mask_provided = true;
|
||||||
|
Dictionary cterm_dict = val.data.dictionary;
|
||||||
|
for (size_t l = 0; l < cterm_dict.size; l++) {
|
||||||
|
char *cterm_dict_key = cterm_dict.items[l].key.data;
|
||||||
|
Object cterm_dict_val = cterm_dict.items[l].value;
|
||||||
|
for (int m = 0; flags[m].name; m++) {
|
||||||
|
if (strequal(flags[m].name, cterm_dict_key)) {
|
||||||
|
if (api_object_to_bool(cterm_dict_val, cterm_dict_key, false,
|
||||||
|
err)) {
|
||||||
|
cterm_mask |= flags[m].flag;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
const char *name;
|
const char *name;
|
||||||
const char *shortname;
|
const char *shortname;
|
||||||
@@ -844,6 +866,8 @@ HlAttrs dict2hlattrs(Dictionary dict, bool use_rgb, int *link_id, Error *err)
|
|||||||
} colors[] = {
|
} colors[] = {
|
||||||
{ "foreground", "fg", &fg },
|
{ "foreground", "fg", &fg },
|
||||||
{ "background", "bg", &bg },
|
{ "background", "bg", &bg },
|
||||||
|
{ "ctermfg", NULL, &ctermfg },
|
||||||
|
{ "ctermbg", NULL, &ctermbg },
|
||||||
{ "special", "sp", &sp },
|
{ "special", "sp", &sp },
|
||||||
{ NULL, NULL, NULL },
|
{ NULL, NULL, NULL },
|
||||||
};
|
};
|
||||||
@@ -867,7 +891,6 @@ HlAttrs dict2hlattrs(Dictionary dict, bool use_rgb, int *link_id, Error *err)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (flags[j].name || colors[k].name) {
|
if (flags[j].name || colors[k].name) {
|
||||||
// handled above
|
// handled above
|
||||||
} else if (link_id && strequal(key, "link")) {
|
} else if (link_id && strequal(key, "link")) {
|
||||||
@@ -888,13 +911,22 @@ HlAttrs dict2hlattrs(Dictionary dict, bool use_rgb, int *link_id, Error *err)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// apply gui mask as default for cterm mask
|
||||||
|
if (!cterm_mask_provided) {
|
||||||
|
cterm_mask = mask;
|
||||||
|
}
|
||||||
if (use_rgb) {
|
if (use_rgb) {
|
||||||
hlattrs.rgb_ae_attr = mask;
|
hlattrs.rgb_ae_attr = mask;
|
||||||
hlattrs.rgb_bg_color = bg;
|
hlattrs.rgb_bg_color = bg;
|
||||||
hlattrs.rgb_fg_color = fg;
|
hlattrs.rgb_fg_color = fg;
|
||||||
hlattrs.rgb_sp_color = sp;
|
hlattrs.rgb_sp_color = sp;
|
||||||
|
hlattrs.cterm_bg_color =
|
||||||
|
ctermbg == -1 ? cterm_normal_bg_color : ctermbg + 1;
|
||||||
|
hlattrs.cterm_fg_color =
|
||||||
|
ctermfg == -1 ? cterm_normal_fg_color : ctermfg + 1;
|
||||||
|
hlattrs.cterm_ae_attr = cterm_mask;
|
||||||
} else {
|
} else {
|
||||||
hlattrs.cterm_ae_attr = mask;
|
hlattrs.cterm_ae_attr = cterm_mask;
|
||||||
hlattrs.cterm_bg_color = bg == -1 ? cterm_normal_bg_color : bg + 1;
|
hlattrs.cterm_bg_color = bg == -1 ? cterm_normal_bg_color : bg + 1;
|
||||||
hlattrs.cterm_fg_color = fg == -1 ? cterm_normal_fg_color : fg + 1;
|
hlattrs.cterm_fg_color = fg == -1 ? cterm_normal_fg_color : fg + 1;
|
||||||
}
|
}
|
||||||
|
@@ -158,3 +158,98 @@ describe('API: highlight',function()
|
|||||||
assert_alive()
|
assert_alive()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("API: set highlight", function()
|
||||||
|
local highlight_color = {
|
||||||
|
fg = tonumber('0xff0000'),
|
||||||
|
bg = tonumber('0x0032aa'),
|
||||||
|
ctermfg = 8,
|
||||||
|
ctermbg = 15,
|
||||||
|
}
|
||||||
|
local highlight1 = {
|
||||||
|
background = highlight_color.bg,
|
||||||
|
foreground = highlight_color.fg,
|
||||||
|
bold = true,
|
||||||
|
italic = true,
|
||||||
|
}
|
||||||
|
local highlight2_config = {
|
||||||
|
ctermbg = highlight_color.ctermbg,
|
||||||
|
ctermfg = highlight_color.ctermfg,
|
||||||
|
underline = true,
|
||||||
|
reverse = true,
|
||||||
|
}
|
||||||
|
local highlight2_result = {
|
||||||
|
background = highlight_color.ctermbg,
|
||||||
|
foreground = highlight_color.ctermfg,
|
||||||
|
underline = true,
|
||||||
|
reverse = true,
|
||||||
|
}
|
||||||
|
local highlight3_config = {
|
||||||
|
background = highlight_color.bg,
|
||||||
|
foreground = highlight_color.fg,
|
||||||
|
ctermbg = highlight_color.ctermbg,
|
||||||
|
ctermfg = highlight_color.ctermfg,
|
||||||
|
bold = true,
|
||||||
|
italic = true,
|
||||||
|
reverse = true,
|
||||||
|
undercurl = true,
|
||||||
|
underline = true,
|
||||||
|
cterm = {
|
||||||
|
italic = true,
|
||||||
|
reverse = true,
|
||||||
|
undercurl = true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local highlight3_result_gui = {
|
||||||
|
background = highlight_color.bg,
|
||||||
|
foreground = highlight_color.fg,
|
||||||
|
bold = true,
|
||||||
|
italic = true,
|
||||||
|
reverse = true,
|
||||||
|
undercurl = true,
|
||||||
|
underline = true,
|
||||||
|
}
|
||||||
|
local highlight3_result_cterm = {
|
||||||
|
background = highlight_color.ctermbg,
|
||||||
|
foreground = highlight_color.ctermfg,
|
||||||
|
italic = true,
|
||||||
|
reverse = true,
|
||||||
|
undercurl = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
local function get_ns()
|
||||||
|
local ns = meths.create_namespace('Test_set_hl')
|
||||||
|
meths._set_hl_ns(ns)
|
||||||
|
return ns
|
||||||
|
end
|
||||||
|
|
||||||
|
before_each(clear)
|
||||||
|
|
||||||
|
it ("can set gui highlight", function()
|
||||||
|
local ns = get_ns()
|
||||||
|
meths.set_hl(ns, 'Test_hl', highlight1)
|
||||||
|
eq(highlight1, meths.get_hl_by_name('Test_hl', true))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it ("can set cterm highlight", function()
|
||||||
|
local ns = get_ns()
|
||||||
|
meths.set_hl(ns, 'Test_hl', highlight2_config)
|
||||||
|
eq(highlight2_result, meths.get_hl_by_name('Test_hl', false))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it ("cterm attr defaults to gui attr", function()
|
||||||
|
local ns = get_ns()
|
||||||
|
meths.set_hl(ns, 'Test_hl', highlight1)
|
||||||
|
eq({
|
||||||
|
bold = true,
|
||||||
|
italic = true,
|
||||||
|
}, meths.get_hl_by_name('Test_hl', false))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it ("can overwrite attr for cterm", function()
|
||||||
|
local ns = get_ns()
|
||||||
|
meths.set_hl(ns, 'Test_hl', highlight3_config)
|
||||||
|
eq(highlight3_result_gui, meths.get_hl_by_name('Test_hl', true))
|
||||||
|
eq(highlight3_result_cterm, meths.get_hl_by_name('Test_hl', false))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
Reference in New Issue
Block a user