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; Array args = ARRAY_DICT_INIT;
ADD(args, INTEGER_OBJ(id)); ADD(args, INTEGER_OBJ(id));
ADD(args, DICTIONARY_OBJ(hlattrs2dict(rgb_attrs, true)));
Dictionary rgb_hl = hlattrs2dict(&rgb_attrs, true); ADD(args, DICTIONARY_OBJ(hlattrs2dict(cterm_attrs, false)));
ADD(args, DICTIONARY_OBJ(rgb_hl));
Dictionary cterm_hl = hlattrs2dict(&cterm_attrs, false);
ADD(args, DICTIONARY_OBJ(cterm_hl));
if (ui->ui_ext[kUIHlState]) { if (ui->ui_ext[kUIHlState]) {
ADD(args, ARRAY_OBJ(copy_array(info))); 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; Array args = ARRAY_DICT_INIT;
UIData *data = ui->data; UIData *data = ui->data;
HlAttrs attrs = HLATTRS_INIT;
if (data->hl_id == id) { if (data->hl_id == id) {
return; return;
} }
data->hl_id = id; data->hl_id = id;
Dictionary hl = hlattrs2dict(syn_attr2entry(id), ui->rgb);
if (id != 0) {
HlAttrs *aep = syn_attr2entry(id);
if (aep) {
attrs = *aep;
}
}
Dictionary hl = hlattrs2dict(&attrs, ui->rgb);
ADD(args, DICTIONARY_OBJ(hl)); ADD(args, DICTIONARY_OBJ(hl));
push_call(ui, "highlight_set", args); 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; Array new_item = ARRAY_DICT_INIT;
int attr = (int)item.items[0].data.integer; int attr = (int)item.items[0].data.integer;
if (attr) { if (attr) {
HlAttrs *aep = syn_attr2entry(attr); Dictionary rgb_attrs = hlattrs2dict(syn_attr2entry(attr), ui->rgb);
Dictionary rgb_attrs = hlattrs2dict(aep, ui->rgb ? kTrue : kFalse);
ADD(new_item, DICTIONARY_OBJ(rgb_attrs)); ADD(new_item, DICTIONARY_OBJ(rgb_attrs));
} else { } else {
ADD(new_item, DICTIONARY_OBJ((Dictionary)ARRAY_DICT_INIT)); ADD(new_item, DICTIONARY_OBJ((Dictionary)ARRAY_DICT_INIT));

View File

@@ -135,12 +135,9 @@ int hl_get_ui_attr(int idx, int final_id, bool optional)
int syn_attr = syn_id2attr(final_id); int syn_attr = syn_id2attr(final_id);
if (syn_attr != 0) { if (syn_attr != 0) {
HlAttrs *aep = syn_attr2entry(syn_attr); attrs = syn_attr2entry(syn_attr);
if (aep) {
attrs = *aep;
available = true; available = true;
} }
}
if (optional && !available) { if (optional && !available) {
return 0; return 0;
} }
@@ -232,42 +229,33 @@ int hl_combine_attr(int char_attr, int prim_attr)
return id; return id;
} }
HlAttrs *char_aep, *spell_aep; HlAttrs char_aep = syn_attr2entry(char_attr);
HlAttrs new_en = HLATTRS_INIT; 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 new_en.cterm_ae_attr |= spell_aep.cterm_ae_attr;
char_aep = syn_attr2entry(char_attr); new_en.rgb_ae_attr |= spell_aep.rgb_ae_attr;
if (char_aep != NULL) { if (spell_aep.cterm_fg_color > 0) {
// Copy all attributes from char_aep to the new entry new_en.cterm_fg_color = spell_aep.cterm_fg_color;
new_en = *char_aep;
} }
spell_aep = syn_attr2entry(prim_attr); if (spell_aep.cterm_bg_color > 0) {
if (spell_aep != NULL) { new_en.cterm_bg_color = spell_aep.cterm_bg_color;
new_en.cterm_ae_attr |= spell_aep->cterm_ae_attr;
new_en.rgb_ae_attr |= spell_aep->rgb_ae_attr;
if (spell_aep->cterm_fg_color > 0) {
new_en.cterm_fg_color = spell_aep->cterm_fg_color;
} }
if (spell_aep->cterm_bg_color > 0) { if (spell_aep.rgb_fg_color >= 0) {
new_en.cterm_bg_color = spell_aep->cterm_bg_color; new_en.rgb_fg_color = spell_aep.rgb_fg_color;
} }
if (spell_aep->rgb_fg_color >= 0) { if (spell_aep.rgb_bg_color >= 0) {
new_en.rgb_fg_color = spell_aep->rgb_fg_color; new_en.rgb_bg_color = spell_aep.rgb_bg_color;
} }
if (spell_aep->rgb_bg_color >= 0) { if (spell_aep.rgb_sp_color >= 0) {
new_en.rgb_bg_color = spell_aep->rgb_bg_color; 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, 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 /// 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)) { if (attr <= 0 || attr >= (int)kv_size(attr_entries)) {
// invalid attribute code, or the tables were cleared // 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. /// Gets highlight description for id `attr_id` as a map.
Dictionary hl_get_attr_by_id(Integer attr_id, Boolean rgb, Error *err) Dictionary hl_get_attr_by_id(Integer attr_id, Boolean rgb, Error *err)
{ {
HlAttrs *aep = NULL;
Dictionary dic = ARRAY_DICT_INIT; Dictionary dic = ARRAY_DICT_INIT;
if (attr_id == 0) { if (attr_id == 0) {
return dic; return dic;
} }
aep = syn_attr2entry((int)attr_id); if (attr_id <= 0 || attr_id >= (int)kv_size(attr_entries)) {
if (!aep) {
api_set_error(err, kErrorTypeException, api_set_error(err, kErrorTypeException,
"Invalid attribute id: %" PRId64, attr_id); "Invalid attribute id: %" PRId64, attr_id);
return dic; return dic;
} }
return hlattrs2dict(aep, rgb); return hlattrs2dict(syn_attr2entry((int)attr_id), rgb);
} }
/// Converts an HlAttrs into Dictionary /// Converts an HlAttrs into Dictionary
/// ///
/// @param[in] aep data to convert /// @param[in] aep data to convert
/// @param use_rgb use 'gui*' settings if true, else resorts to 'cterm*' /// @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; 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) { if (mask & HL_BOLD) {
PUT(hl, "bold", BOOLEAN_OBJ(true)); PUT(hl, "bold", BOOLEAN_OBJ(true));
@@ -344,24 +329,24 @@ Dictionary hlattrs2dict(const HlAttrs *aep, bool use_rgb)
} }
if (use_rgb) { if (use_rgb) {
if (aep->rgb_fg_color != -1) { if (ae.rgb_fg_color != -1) {
PUT(hl, "foreground", INTEGER_OBJ(aep->rgb_fg_color)); PUT(hl, "foreground", INTEGER_OBJ(ae.rgb_fg_color));
} }
if (aep->rgb_bg_color != -1) { if (ae.rgb_bg_color != -1) {
PUT(hl, "background", INTEGER_OBJ(aep->rgb_bg_color)); PUT(hl, "background", INTEGER_OBJ(ae.rgb_bg_color));
} }
if (aep->rgb_sp_color != -1) { if (ae.rgb_sp_color != -1) {
PUT(hl, "special", INTEGER_OBJ(aep->rgb_sp_color)); PUT(hl, "special", INTEGER_OBJ(ae.rgb_sp_color));
} }
} else { } else {
if (cterm_normal_fg_color != aep->cterm_fg_color) { if (cterm_normal_fg_color != ae.cterm_fg_color) {
PUT(hl, "foreground", INTEGER_OBJ(aep->cterm_fg_color - 1)); PUT(hl, "foreground", INTEGER_OBJ(ae.cterm_fg_color - 1));
} }
if (cterm_normal_bg_color != aep->cterm_bg_color) { if (cterm_normal_bg_color != ae.cterm_bg_color) {
PUT(hl, "background", INTEGER_OBJ(aep->cterm_bg_color - 1)); 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 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
&& !(wp == curwin && VIsual_active)) { && !(wp == curwin && VIsual_active)) {
int cul_attr = win_hl_attr(wp, HLF_CUL); 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): // We make a compromise here (#7383):
// * low-priority CursorLine if fg is not set // * low-priority CursorLine if fg is not set
// * high-priority ("same as Vim" priority) CursorLine if fg is 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; line_attr_lowprio = cul_attr;
} else { } else {
if (line_attr != 0 && !(State & INSERT) && bt_quickfix(wp->w_buffer) 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]; cursorentry_T c = data->cursor_shapes[mode];
if (c.id != 0 && ui->rgb) { if (c.id != 0 && ui->rgb) {
// TODO(bfredl): NOT threadsafe, include attr in cursor_shape already.
int attr = syn_id2attr(c.id); int attr = syn_id2attr(c.id);
if (attr > 0) { if (attr > 0 && attr < (int)kv_size(data->attrs)) {
HlAttrs *aep = syn_attr2entry(attr); int color = kv_A(data->attrs, attr).rgb_bg_color;
UNIBI_SET_NUM_VAR(data->params[0], aep->rgb_bg_color); UNIBI_SET_NUM_VAR(data->params[0], color);
unibi_out_ext(ui, data->unibi_ext.set_cursor_color); unibi_out_ext(ui, data->unibi_ext.set_cursor_color);
} }
} }