diff --git a/ui-terminal-curses.c b/ui-terminal-curses.c index 45e8f9e1b..dbe8e0667 100644 --- a/ui-terminal-curses.c +++ b/ui-terminal-curses.c @@ -50,8 +50,6 @@ #define MAX_COLOR_CLOBBER 240 -static short color_clobber_idx = 0; -static uint32_t clobbering_colors[MAX_COLOR_CLOBBER]; static int change_colors = -1; /* Calculate r,g,b components of one of the standard upper 240 colors */ @@ -82,10 +80,13 @@ static void undo_palette(void) /* Work out the nearest color from the 256 color set, or perhaps exactly. */ static CellColor color_rgb(UiTerm *ui, uint8_t r, uint8_t g, uint8_t b) { + static short color_clobber_idx = 0; + static uint32_t clobbering_colors[MAX_COLOR_CLOBBER]; + if (change_colors == -1) change_colors = ui->vis->change_colors && can_change_color() && COLORS >= 256; if (change_colors) { - uint32_t hexrep = ((r << 16) | (g << 8) | b) + 1; + uint32_t hexrep = (r << 16) | (g << 8) | b; for (short i = 0; i < MAX_COLOR_CLOBBER; ++i) { if (clobbering_colors[i] == hexrep) return i + 16; @@ -295,5 +296,7 @@ static void ui_curses_free(UiTerm *term) { } bool is_default_color(CellColor c) { + if (change_colors == 1) + return c == 16; return c == CELL_COLOR_DEFAULT; } diff --git a/vis.c b/vis.c index 028fea3de..9692ae6d0 100644 --- a/vis.c +++ b/vis.c @@ -292,6 +292,18 @@ static void window_free(Win *win) { free(win); } +static void cell_apply_style(Cell *cell, CellStyle *style, bool cursel) { + CellStyle set = { .fg = style->fg, .bg = style->bg, .attr = style->attr }; + if (cursel) { + if (is_default_color(style->fg)) + set.fg = cell->style.fg; + if (is_default_color(style->bg)) + set.bg = cell->style.bg; + set.attr |= cell->style.attr; + } + memcpy(&cell->style, &set, sizeof(CellStyle)); +} + static void window_draw_colorcolumn(Win *win) { View *view = win->view; int cc = view_colorcolumn_get(view); @@ -315,10 +327,7 @@ static void window_draw_colorcolumn(Win *win) { /* This screen line contains the cell we want to highlight */ if (cc <= line_cols + width) { - CellStyle *orig = &l->cells[cc - 1 - line_cols].style; - orig->attr = style.attr; - orig->fg = is_default_color(style.fg) ? orig->fg : style.fg; - orig->bg = is_default_color(style.bg) ? orig->bg : style.bg; + cell_apply_style(&l->cells[cc - 1 - line_cols], &style, true); line_cc_set = true; } else { line_cols += width; @@ -343,10 +352,8 @@ static void window_draw_cursorline(Win *win) { size_t lineno = view_cursors_line_get(sel)->lineno; for (Line *l = view_lines_first(view); l; l = l->next) { if (l->lineno == lineno) { - for (int x = 0; x < width; x++) { - l->cells[x].style.attr |= style.attr; - l->cells[x].style.bg = style.bg; - } + for (int x = 0; x < width; x++) + cell_apply_style(&l->cells[x], &style, true); } else if (l->lineno > lineno) { break; } @@ -376,21 +383,8 @@ static void window_draw_selection(View *view, Selection *cur, CellStyle *style) for (Line *l = start_line; l != end_line->next; l = l->next) { int col = (l == start_line) ? start_col : 0; int end = (l == end_line) ? end_col : l->width; - while (col < end) { - if (cell_color_equal(l->cells[col].style.fg, style->bg)) { - CellStyle old = l->cells[col].style; - if (!cell_color_equal(old.fg, old.bg)) { - l->cells[col].style.fg = old.bg; - l->cells[col].style.bg = old.fg; - } else { - l->cells[col].style.attr = style->attr; - } - } else { - l->cells[col].style.bg = style->bg; - l->cells[col].style.fg = style->fg; - } - col++; - } + while (col < end) + cell_apply_style(&l->cells[col++], style, true); } } @@ -405,14 +399,7 @@ static void window_draw_cursor_matching(Win *win, Selection *cur, CellStyle *sty return; if (!view_coord_get(win->view, pos_match, &line_match, NULL, &col_match)) return; - if (cell_color_equal(line_match->cells[col_match].style.fg, style->fg)) { - CellStyle old = line_match->cells[col_match].style; - line_match->cells[col_match].style.fg = old.bg; - line_match->cells[col_match].style.bg = old.fg; - } else { - line_match->cells[col_match].style.bg = style->bg; - line_match->cells[col_match].style.fg = style->fg; - } + cell_apply_style(&line_match->cells[col_match], style, true); } static void window_draw_cursor(Win *win, Selection *cur, CellStyle *style, CellStyle *sel_style) { @@ -422,7 +409,7 @@ static void window_draw_cursor(Win *win, Selection *cur, CellStyle *style, CellS int col = view_cursors_cell_get(cur); if (!line || col == -1) return; - line->cells[col].style = *style; + cell_apply_style(&line->cells[col], style, true); } static void window_draw_selections(Win *win) {