highlight: HlAttrs is a value type; treat it like such

This commit is contained in:
Björn Linse 2018-07-26 20:36:24 +02:00
parent ee5cc88a73
commit fab555e59c
4 changed files with 48 additions and 76 deletions

View File

@ -351,12 +351,8 @@ static void remote_ui_hl_attr_define(UI *ui, Integer id, HlAttrs rgb_attrs,
Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(id));
Dictionary rgb_hl = hlattrs2dict(&rgb_attrs, true);
ADD(args, DICTIONARY_OBJ(rgb_hl));
Dictionary cterm_hl = hlattrs2dict(&cterm_attrs, false);
ADD(args, DICTIONARY_OBJ(cterm_hl));
ADD(args, DICTIONARY_OBJ(hlattrs2dict(rgb_attrs, true)));
ADD(args, DICTIONARY_OBJ(hlattrs2dict(cterm_attrs, false)));
if (ui->ui_ext[kUIHlState]) {
ADD(args, ARRAY_OBJ(copy_array(info)));
@ -372,21 +368,12 @@ static void remote_ui_highlight_set(UI *ui, int id)
Array args = ARRAY_DICT_INIT;
UIData *data = ui->data;
HlAttrs attrs = HLATTRS_INIT;
if (data->hl_id == id) {
return;
}
data->hl_id = id;
if (id != 0) {
HlAttrs *aep = syn_attr2entry(id);
if (aep) {
attrs = *aep;
}
}
Dictionary hl = hlattrs2dict(&attrs, ui->rgb);
Dictionary hl = hlattrs2dict(syn_attr2entry(id), ui->rgb);
ADD(args, DICTIONARY_OBJ(hl));
push_call(ui, "highlight_set", args);
@ -524,8 +511,7 @@ static void remote_ui_cmdline_show(UI *ui, Array args)
Array new_item = ARRAY_DICT_INIT;
int attr = (int)item.items[0].data.integer;
if (attr) {
HlAttrs *aep = syn_attr2entry(attr);
Dictionary rgb_attrs = hlattrs2dict(aep, ui->rgb ? kTrue : kFalse);
Dictionary rgb_attrs = hlattrs2dict(syn_attr2entry(attr), ui->rgb);
ADD(new_item, DICTIONARY_OBJ(rgb_attrs));
} else {
ADD(new_item, DICTIONARY_OBJ((Dictionary)ARRAY_DICT_INIT));

View File

@ -135,11 +135,8 @@ int hl_get_ui_attr(int idx, int final_id, bool optional)
int syn_attr = syn_id2attr(final_id);
if (syn_attr != 0) {
HlAttrs *aep = syn_attr2entry(syn_attr);
if (aep) {
attrs = *aep;
available = true;
}
attrs = syn_attr2entry(syn_attr);
available = true;
}
if (optional && !available) {
return 0;
@ -232,42 +229,33 @@ int hl_combine_attr(int char_attr, int prim_attr)
return id;
}
HlAttrs *char_aep, *spell_aep;
HlAttrs new_en = HLATTRS_INIT;
HlAttrs char_aep = syn_attr2entry(char_attr);
HlAttrs spell_aep = syn_attr2entry(prim_attr);
// start with low-priority attribute, and override colors if present below.
HlAttrs new_en = char_aep;
// Find the entry for char_attr
char_aep = syn_attr2entry(char_attr);
new_en.cterm_ae_attr |= spell_aep.cterm_ae_attr;
new_en.rgb_ae_attr |= spell_aep.rgb_ae_attr;
if (char_aep != NULL) {
// Copy all attributes from char_aep to the new entry
new_en = *char_aep;
if (spell_aep.cterm_fg_color > 0) {
new_en.cterm_fg_color = spell_aep.cterm_fg_color;
}
spell_aep = syn_attr2entry(prim_attr);
if (spell_aep != NULL) {
new_en.cterm_ae_attr |= spell_aep->cterm_ae_attr;
new_en.rgb_ae_attr |= spell_aep->rgb_ae_attr;
if (spell_aep.cterm_bg_color > 0) {
new_en.cterm_bg_color = spell_aep.cterm_bg_color;
}
if (spell_aep->cterm_fg_color > 0) {
new_en.cterm_fg_color = spell_aep->cterm_fg_color;
}
if (spell_aep.rgb_fg_color >= 0) {
new_en.rgb_fg_color = spell_aep.rgb_fg_color;
}
if (spell_aep->cterm_bg_color > 0) {
new_en.cterm_bg_color = spell_aep->cterm_bg_color;
}
if (spell_aep.rgb_bg_color >= 0) {
new_en.rgb_bg_color = spell_aep.rgb_bg_color;
}
if (spell_aep->rgb_fg_color >= 0) {
new_en.rgb_fg_color = spell_aep->rgb_fg_color;
}
if (spell_aep->rgb_bg_color >= 0) {
new_en.rgb_bg_color = spell_aep->rgb_bg_color;
}
if (spell_aep->rgb_sp_color >= 0) {
new_en.rgb_sp_color = spell_aep->rgb_sp_color;
}
if (spell_aep.rgb_sp_color >= 0) {
new_en.rgb_sp_color = spell_aep.rgb_sp_color;
}
id = get_attr_entry((HlEntry){ .attr = new_en, .kind = kHlCombine,
@ -280,44 +268,41 @@ int hl_combine_attr(int char_attr, int prim_attr)
}
/// Get highlight attributes for a attribute code
HlAttrs *syn_attr2entry(int attr)
HlAttrs syn_attr2entry(int attr)
{
if (attr <= 0 || attr >= (int)kv_size(attr_entries)) {
// invalid attribute code, or the tables were cleared
return NULL;
return HLATTRS_INIT;
}
return &(kv_A(attr_entries, attr).attr);
return kv_A(attr_entries, attr).attr;
}
/// Gets highlight description for id `attr_id` as a map.
Dictionary hl_get_attr_by_id(Integer attr_id, Boolean rgb, Error *err)
{
HlAttrs *aep = NULL;
Dictionary dic = ARRAY_DICT_INIT;
if (attr_id == 0) {
return dic;
}
aep = syn_attr2entry((int)attr_id);
if (!aep) {
if (attr_id <= 0 || attr_id >= (int)kv_size(attr_entries)) {
api_set_error(err, kErrorTypeException,
"Invalid attribute id: %" PRId64, attr_id);
return dic;
}
return hlattrs2dict(aep, rgb);
return hlattrs2dict(syn_attr2entry((int)attr_id), rgb);
}
/// Converts an HlAttrs into Dictionary
///
/// @param[in] aep data to convert
/// @param use_rgb use 'gui*' settings if true, else resorts to 'cterm*'
Dictionary hlattrs2dict(const HlAttrs *aep, bool use_rgb)
Dictionary hlattrs2dict(HlAttrs ae, bool use_rgb)
{
assert(aep);
Dictionary hl = ARRAY_DICT_INIT;
int mask = use_rgb ? aep->rgb_ae_attr : aep->cterm_ae_attr;
int mask = use_rgb ? ae.rgb_ae_attr : ae.cterm_ae_attr;
if (mask & HL_BOLD) {
PUT(hl, "bold", BOOLEAN_OBJ(true));
@ -344,24 +329,24 @@ Dictionary hlattrs2dict(const HlAttrs *aep, bool use_rgb)
}
if (use_rgb) {
if (aep->rgb_fg_color != -1) {
PUT(hl, "foreground", INTEGER_OBJ(aep->rgb_fg_color));
if (ae.rgb_fg_color != -1) {
PUT(hl, "foreground", INTEGER_OBJ(ae.rgb_fg_color));
}
if (aep->rgb_bg_color != -1) {
PUT(hl, "background", INTEGER_OBJ(aep->rgb_bg_color));
if (ae.rgb_bg_color != -1) {
PUT(hl, "background", INTEGER_OBJ(ae.rgb_bg_color));
}
if (aep->rgb_sp_color != -1) {
PUT(hl, "special", INTEGER_OBJ(aep->rgb_sp_color));
if (ae.rgb_sp_color != -1) {
PUT(hl, "special", INTEGER_OBJ(ae.rgb_sp_color));
}
} else {
if (cterm_normal_fg_color != aep->cterm_fg_color) {
PUT(hl, "foreground", INTEGER_OBJ(aep->cterm_fg_color - 1));
if (cterm_normal_fg_color != ae.cterm_fg_color) {
PUT(hl, "foreground", INTEGER_OBJ(ae.cterm_fg_color - 1));
}
if (cterm_normal_bg_color != aep->cterm_bg_color) {
PUT(hl, "background", INTEGER_OBJ(aep->cterm_bg_color - 1));
if (cterm_normal_bg_color != ae.cterm_bg_color) {
PUT(hl, "background", INTEGER_OBJ(ae.cterm_bg_color - 1));
}
}

View File

@ -2424,12 +2424,12 @@ win_line (
if (wp->w_p_cul && lnum == wp->w_cursor.lnum
&& !(wp == curwin && VIsual_active)) {
int cul_attr = win_hl_attr(wp, HLF_CUL);
HlAttrs *aep = syn_attr2entry(cul_attr);
HlAttrs ae = syn_attr2entry(cul_attr);
// We make a compromise here (#7383):
// * low-priority CursorLine if fg is not set
// * high-priority ("same as Vim" priority) CursorLine if fg is set
if (aep->rgb_fg_color == -1 && aep->cterm_fg_color == 0) {
if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
line_attr_lowprio = cul_attr;
} else {
if (line_attr != 0 && !(State & INSERT) && bt_quickfix(wp->w_buffer)

View File

@ -943,10 +943,11 @@ static void tui_set_mode(UI *ui, ModeShape mode)
cursorentry_T c = data->cursor_shapes[mode];
if (c.id != 0 && ui->rgb) {
// TODO(bfredl): NOT threadsafe, include attr in cursor_shape already.
int attr = syn_id2attr(c.id);
if (attr > 0) {
HlAttrs *aep = syn_attr2entry(attr);
UNIBI_SET_NUM_VAR(data->params[0], aep->rgb_bg_color);
if (attr > 0 && attr < (int)kv_size(data->attrs)) {
int color = kv_A(data->attrs, attr).rgb_bg_color;
UNIBI_SET_NUM_VAR(data->params[0], color);
unibi_out_ext(ui, data->unibi_ext.set_cursor_color);
}
}