Skip to content

Commit

Permalink
Add an option to keep cell's non-default style values
Browse files Browse the repository at this point in the history
  • Loading branch information
infastin committed Jan 19, 2025
1 parent e9e0927 commit a4e7b3b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 38 deletions.
3 changes: 0 additions & 3 deletions lua/themes/base-16.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ lexers.STYLE_COMMAND_SECTION = lexers.STYLE_CLASS
lexers.STYLE_ENVIRONMENT = lexers.STYLE_TYPE
lexers.STYLE_ENVIRONMENT_MATH = lexers.STYLE_NUMBER

-- Makefile
lexers.STYLE_TARGET = ''

-- Markdown
lexers.STYLE_HR = ''
for i = 1,6 do lexers['STYLE_HEADING_H'..i] = lexers.STYLE_HEADING end
Expand Down
3 changes: 0 additions & 3 deletions lua/themes/solarized.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ lexers.STYLE_COMMAND_SECTION = lexers.STYLE_CLASS
lexers.STYLE_ENVIRONMENT = lexers.STYLE_TYPE
lexers.STYLE_ENVIRONMENT_MATH = lexers.STYLE_NUMBER

-- Makefile
lexers.STYLE_TARGET = ''

-- Markdown
lexers.STYLE_HR = ''
for i = 1,6 do lexers['STYLE_HEADING_H'..i] = lexers.STYLE_HEADING end
Expand Down
28 changes: 16 additions & 12 deletions ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@

#define CELL_STYLE_DEFAULT (CellStyle){.fg = CELL_COLOR_DEFAULT, .bg = CELL_COLOR_DEFAULT, .attr = CELL_ATTR_NORMAL}

static bool is_default_fg(CellColor c) {
return is_default_color(c);
static bool is_default_fg(CellColor c, CellColor default_fg) {
return is_default_color(c) || cell_color_equal(c, default_fg);
}

static bool is_default_bg(CellColor c) {
return is_default_color(c);
static bool is_default_bg(CellColor c, CellColor default_bg) {
return is_default_color(c) || cell_color_equal(c, default_bg);
}

void ui_die(Ui *tui, const char *msg, va_list ap) {
Expand Down Expand Up @@ -212,7 +212,7 @@ static void ui_draw_string(Ui *tui, int x, int y, const char *str, int win_id, e
strncpy(cells[x].data, str, len);
cells[x].data[len] = '\0';
cells[x].style = default_style;
ui_window_style_set(tui, win_id, cells + x++, style_id);
ui_window_style_set(tui, win_id, cells + x++, style_id, false);
}
}

Expand Down Expand Up @@ -269,25 +269,29 @@ static void ui_window_draw(Win *win) {
}
}

void ui_window_style_set(Ui *tui, int win_id, Cell *cell, enum UiStyle id) {
void ui_window_style_set(Ui *tui, int win_id, Cell *cell, enum UiStyle id, bool keep) {
CellStyle set = tui->styles[win_id * UI_STYLE_MAX + id];

if (id != UI_STYLE_DEFAULT) {
set.fg = is_default_fg(set.fg)? cell->style.fg : set.fg;
set.bg = is_default_bg(set.bg)? cell->style.bg : set.bg;
CellStyle def = tui->styles[win_id * UI_STYLE_MAX + UI_STYLE_DEFAULT];
if (keep) {
set.fg = is_default_fg(cell->style.fg, def.fg) ? set.fg : cell->style.fg;
set.bg = is_default_bg(cell->style.bg, def.bg) ? set.bg : cell->style.bg;
} else {
set.fg = is_default_fg(set.fg, def.fg) ? cell->style.fg : set.fg;
set.bg = is_default_bg(set.bg, def.bg) ? cell->style.bg : set.bg;
}
set.attr = cell->style.attr | set.attr;
}

cell->style = set;
}

bool ui_window_style_set_pos(Win *win, int x, int y, enum UiStyle id) {
bool ui_window_style_set_pos(Win *win, int x, int y, enum UiStyle id, bool keep) {
Ui *tui = &win->vis->ui;
if (x < 0 || y < 0 || y >= win->height || x >= win->width) {
return false;
}
Cell *cell = CELL_AT_POS(tui, win->x + x, win->y + y)
ui_window_style_set(tui, win->id, cell, id);
ui_window_style_set(tui, win->id, cell, id, keep);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ void ui_window_swap(Win *, Win *);
bool ui_getkey(Ui *, TermKeyKey *);

bool ui_style_define(Win *win, int id, const char *style);
bool ui_window_style_set_pos(Win *win, int x, int y, enum UiStyle id);
void ui_window_style_set(Ui *ui, int win_id, Cell *cell, enum UiStyle id);
void ui_window_style_set(Ui *ui, int win_id, Cell *cell, enum UiStyle id, bool keep);
bool ui_window_style_set_pos(Win *win, int x, int y, enum UiStyle id, bool keep);

void ui_window_options_set(Win *win, enum UiOption options);
void ui_window_status(Win *win, const char *status);
Expand Down
6 changes: 3 additions & 3 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static void view_clear(View *view) {
/* FIXME: awful garbage that only exists because every
* struct in this program is an interdependent hellscape */
Win *win = (Win *)((char *)view - offsetof(Win, view));
ui_window_style_set(&win->vis->ui, win->id, &cell_blank, UI_STYLE_DEFAULT);
ui_window_style_set(&win->vis->ui, win->id, &cell_blank, UI_STYLE_DEFAULT, false);
}

static int view_max_text_width(const View *view) {
Expand Down Expand Up @@ -1335,7 +1335,7 @@ void view_selections_normalize(View *view) {
view_selections_set(prev, &range_prev);
}

void win_style(Win *win, enum UiStyle style, size_t start, size_t end) {
void win_style(Win *win, enum UiStyle style, size_t start, size_t end, bool keep) {
View *view = &win->view;
if (end < view->start || start > view->end)
return;
Expand Down Expand Up @@ -1365,7 +1365,7 @@ void win_style(Win *win, enum UiStyle style, size_t start, size_t end) {
do {
while (pos <= end && col < width) {
pos += line->cells[col].len;
ui_window_style_set(&win->vis->ui, win->id, &line->cells[col++], style);
ui_window_style_set(&win->vis->ui, win->id, &line->cells[col++], style, keep);
}
col = 0;
} while (pos <= end && (line = line->next));
Expand Down
2 changes: 1 addition & 1 deletion view.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ bool view_breakat_set(View*, const char *breakat);
/** Set how many spaces are used to display a tab `\t` character. */
void view_tabwidth_set(View*, int tabwidth);
/** Apply a style to a text range. */
void win_style(struct Win*, enum UiStyle, size_t start, size_t end);
void win_style(struct Win*, enum UiStyle, size_t start, size_t end, bool keep);

/** @} */

Expand Down
20 changes: 12 additions & 8 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -2026,9 +2026,10 @@ static int window_style_define(lua_State *L) {
*
* The style will be cleared after every window redraw.
* @function style
* @tparam int id the display style as registered with @{style_define}
* @tparam int start the absolute file position in bytes
* @tparam int finish the end position
* @tparam int id the display style as registered with @{style_define}
* @tparam int start the absolute file position in bytes
* @tparam int finish the end position
* @tparam[opt] bool keep whether non-default style values should be kept
* @see style_define
* @usage
* win:style(win.STYLE_DEFAULT, 0, 10)
Expand All @@ -2038,7 +2039,8 @@ static int window_style(lua_State *L) {
enum UiStyle style = luaL_checkunsigned(L, 2);
size_t start = checkpos(L, 3);
size_t end = checkpos(L, 4);
win_style(win, style, start, end);
bool keep = lua_isboolean(L, 5) && lua_toboolean(L, 5);
win_style(win, style, start, end, keep);
return 0;
}

Expand All @@ -2049,9 +2051,10 @@ static int window_style(lua_State *L) {
* such as the status bar.
* The style will be cleared after every window redraw.
* @function style_pos
* @tparam int id display style registered with @{style_define}
* @tparam int x 0-based x coordinate within Win, where (0,0) is the top left corner
* @tparam int y See above
* @tparam int id display style registered with @{style_define}
* @tparam int x 0-based x coordinate within Win, where (0,0) is the top left corner
* @tparam int y See above
* @tparam[opt] bool keep whether non-default style values should be kept
* @treturn bool false if the coordinates would be outside the window's dimensions
* @see style_define
* @usage
Expand All @@ -2063,7 +2066,8 @@ static int window_style_pos(lua_State *L) {
enum UiStyle style = luaL_checkunsigned(L, 2);
size_t x = checkpos(L, 3);
size_t y = checkpos(L, 4);
bool ret = ui_window_style_set_pos(win, (int)x, (int)y, style);
bool keep = lua_isboolean(L, 5) && lua_toboolean(L, 5);
bool ret = ui_window_style_set_pos(win, (int)x, (int)y, style, keep);
lua_pushboolean(L, ret);
return 1;
}
Expand Down
12 changes: 6 additions & 6 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ static void window_draw_colorcolumn(Win *win) {

/* This screen line contains the cell we want to highlight */
if (cc <= line_cols + width) {
ui_window_style_set(&win->vis->ui, win->id, &l->cells[cc - 1 - line_cols], UI_STYLE_COLOR_COLUMN);
ui_window_style_set(&win->vis->ui, win->id, &l->cells[cc - 1 - line_cols], UI_STYLE_COLOR_COLUMN, false);
line_cc_set = true;
} else {
line_cols += width;
Expand All @@ -255,7 +255,7 @@ static void window_draw_cursorline(Win *win) {
for (Line *l = win->view.topline; l; l = l->next) {
if (l->lineno == lineno) {
for (int x = 0; x < width; x++)
ui_window_style_set(&vis->ui, win->id, &l->cells[x], UI_STYLE_CURSOR_LINE);
ui_window_style_set(&vis->ui, win->id, &l->cells[x], UI_STYLE_CURSOR_LINE, true);
} else if (l->lineno > lineno) {
break;
}
Expand Down Expand Up @@ -285,7 +285,7 @@ static void window_draw_selection(Win *win, Selection *cur) {
int col = (l == start_line) ? start_col : 0;
int end = (l == end_line) ? end_col : l->width;
while (col < end)
ui_window_style_set(&win->vis->ui, win->id, &l->cells[col++], UI_STYLE_SELECTION);
ui_window_style_set(&win->vis->ui, win->id, &l->cells[col++], UI_STYLE_SELECTION, false);
}
}

Expand All @@ -300,7 +300,7 @@ static void window_draw_cursor_matching(Win *win, Selection *cur) {
return;
if (!view_coord_get(&win->view, pos_match, &line_match, NULL, &col_match))
return;
ui_window_style_set(&win->vis->ui, win->id, &line_match->cells[col_match], UI_STYLE_SELECTION);
ui_window_style_set(&win->vis->ui, win->id, &line_match->cells[col_match], UI_STYLE_SELECTION, false);
}

static void window_draw_cursor(Win *win, Selection *cur) {
Expand All @@ -310,7 +310,7 @@ static void window_draw_cursor(Win *win, Selection *cur) {
if (!line)
return;
Selection *primary = view_selections_primary_get(&win->view);
ui_window_style_set(&win->vis->ui, win->id, &line->cells[cur->col], primary == cur ? UI_STYLE_CURSOR_PRIMARY : UI_STYLE_CURSOR);
ui_window_style_set(&win->vis->ui, win->id, &line->cells[cur->col], primary == cur ? UI_STYLE_CURSOR_PRIMARY : UI_STYLE_CURSOR, false);
window_draw_cursor_matching(win, cur);
return;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ static void window_draw_eof(Win *win) {
return;
for (Line *l = view->lastline->next; l; l = l->next) {
strncpy(l->cells[0].data, view->symbols[SYNTAX_SYMBOL_EOF], sizeof(l->cells[0].data)-1);
ui_window_style_set(&win->vis->ui, win->id, l->cells, UI_STYLE_EOF);
ui_window_style_set(&win->vis->ui, win->id, l->cells, UI_STYLE_EOF, false);
}
}

Expand Down

0 comments on commit a4e7b3b

Please sign in to comment.