mirror of
https://github.com/neovim/neovim.git
synced 2025-09-16 16:28:17 +00:00
feat(highlight): support color names for cterm
This commit is contained in:
@@ -821,27 +821,27 @@ HlAttrs dict2hlattrs(Dict(highlight) *dict, bool use_rgb, int *link_id, Error *e
|
||||
CHECK_FLAG(dict, mask, global, , HL_GLOBAL);
|
||||
|
||||
if (HAS_KEY(dict->fg)) {
|
||||
fg = object_to_color(dict->fg, "fg", err);
|
||||
fg = object_to_color(dict->fg, "fg", true, err);
|
||||
} else if (HAS_KEY(dict->foreground)) {
|
||||
fg = object_to_color(dict->foreground, "foreground", err);
|
||||
fg = object_to_color(dict->foreground, "foreground", true, err);
|
||||
}
|
||||
if (ERROR_SET(err)) {
|
||||
return hlattrs;
|
||||
}
|
||||
|
||||
if (HAS_KEY(dict->bg)) {
|
||||
bg = object_to_color(dict->bg, "bg", err);
|
||||
bg = object_to_color(dict->bg, "bg", true, err);
|
||||
} else if (HAS_KEY(dict->background)) {
|
||||
bg = object_to_color(dict->background, "background", err);
|
||||
bg = object_to_color(dict->background, "background", true, err);
|
||||
}
|
||||
if (ERROR_SET(err)) {
|
||||
return hlattrs;
|
||||
}
|
||||
|
||||
if (HAS_KEY(dict->sp)) {
|
||||
sp = object_to_color(dict->sp, "sp", err);
|
||||
sp = object_to_color(dict->sp, "sp", true, err);
|
||||
} else if (HAS_KEY(dict->special)) {
|
||||
sp = object_to_color(dict->special, "special", err);
|
||||
sp = object_to_color(dict->special, "special", true, err);
|
||||
}
|
||||
if (ERROR_SET(err)) {
|
||||
return hlattrs;
|
||||
@@ -882,14 +882,14 @@ HlAttrs dict2hlattrs(Dict(highlight) *dict, bool use_rgb, int *link_id, Error *e
|
||||
#undef CHECK_FLAG
|
||||
|
||||
if (HAS_KEY(dict->ctermfg)) {
|
||||
ctermfg = object_to_color(dict->ctermfg, "ctermfg", err);
|
||||
ctermfg = object_to_color(dict->ctermfg, "ctermfg", false, err);
|
||||
if (ERROR_SET(err)) {
|
||||
return hlattrs;
|
||||
}
|
||||
}
|
||||
|
||||
if (HAS_KEY(dict->ctermbg)) {
|
||||
ctermbg = object_to_color(dict->ctermbg, "ctermbg", err);
|
||||
ctermbg = object_to_color(dict->ctermbg, "ctermbg", false, err);
|
||||
if (ERROR_SET(err)) {
|
||||
return hlattrs;
|
||||
}
|
||||
@@ -916,14 +916,25 @@ HlAttrs dict2hlattrs(Dict(highlight) *dict, bool use_rgb, int *link_id, Error *e
|
||||
return hlattrs;
|
||||
}
|
||||
|
||||
int object_to_color(Object val, char *key, Error *err)
|
||||
int object_to_color(Object val, char *key, bool rgb, Error *err)
|
||||
{
|
||||
if (val.type == kObjectTypeInteger) {
|
||||
return (int)val.data.integer;
|
||||
} else if (val.type == kObjectTypeString) {
|
||||
String str = val.data.string;
|
||||
// TODO(bfredl): be more fancy with "bg", "fg" etc
|
||||
return str.size ? name_to_color(str.data) : 0;
|
||||
int color;
|
||||
if (!str.size) {
|
||||
color = 0;
|
||||
} else if (rgb) {
|
||||
color = name_to_color(str.data);
|
||||
} else {
|
||||
color = name_to_ctermcolor(str.data);
|
||||
}
|
||||
if (color < 0) {
|
||||
api_set_error(err, kErrorTypeValidation, "'%s' is not a valid color", str.data);
|
||||
}
|
||||
return color;
|
||||
} else {
|
||||
api_set_error(err, kErrorTypeValidation, "'%s' must be string or integer", key);
|
||||
return 0;
|
||||
|
@@ -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 *
|
||||
|
@@ -290,6 +290,10 @@ describe("API: set highlight", function()
|
||||
eq('Test_hl3 xxx ctermfg=12 ctermbg=9',
|
||||
exec_capture('highlight Test_hl3'))
|
||||
|
||||
meths.set_hl(0, 'Test_hl3', { ctermbg = 'red' , ctermfg = 'blue'})
|
||||
eq('Test_hl3 xxx ctermfg=12 ctermbg=9',
|
||||
exec_capture('highlight Test_hl3'))
|
||||
|
||||
meths.set_hl(0, 'Test_hl3', { ctermbg = 9 })
|
||||
eq('Test_hl3 xxx ctermbg=9',
|
||||
exec_capture('highlight Test_hl3'))
|
||||
@@ -297,5 +301,18 @@ describe("API: set highlight", function()
|
||||
meths.set_hl(0, 'Test_hl3', {})
|
||||
eq('Test_hl3 xxx cleared',
|
||||
exec_capture('highlight Test_hl3'))
|
||||
|
||||
eq("'redd' is not a valid color",
|
||||
pcall_err(meths.set_hl, 0, 'Test_hl3', {fg='redd'}))
|
||||
|
||||
eq("'bleu' is not a valid color",
|
||||
pcall_err(meths.set_hl, 0, 'Test_hl3', {ctermfg='bleu'}))
|
||||
|
||||
meths.set_hl(0, 'Test_hl3', {fg='#FF00FF'})
|
||||
eq('Test_hl3 xxx guifg=#FF00FF',
|
||||
exec_capture('highlight Test_hl3'))
|
||||
|
||||
eq("'#FF00FF' is not a valid color",
|
||||
pcall_err(meths.set_hl, 0, 'Test_hl3', {ctermfg='#FF00FF'}))
|
||||
end)
|
||||
end)
|
||||
|
Reference in New Issue
Block a user