aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinfastin <infastin@yandex.com>2025-01-19 23:52:17 +0500
committerRandy Palamar <randy@rnpnr.xyz>2025-02-22 12:02:55 -0700
commitb7f4057919da1d249241eeb8b2f06371a031582f (patch)
tree42547e8aeecf8489705f6e56cba9d98418f8c2e6
parent0208d7a9f553b77ab4dfd740e2318f32340b10ea (diff)
downloadvis-b7f4057919da1d249241eeb8b2f06371a031582f.tar.gz
vis-b7f4057919da1d249241eeb8b2f06371a031582f.tar.xz
style_set: add option to keep non-default style values
-rw-r--r--ui-terminal.c15
-rw-r--r--ui.h4
-rw-r--r--view.c6
-rw-r--r--view.h2
-rw-r--r--vis-lua.c20
-rw-r--r--vis.c12
6 files changed, 35 insertions, 24 deletions
diff --git a/ui-terminal.c b/ui-terminal.c
index 573fec2..00636a0 100644
--- a/ui-terminal.c
+++ b/ui-terminal.c
@@ -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);
}
}
@@ -269,10 +269,17 @@ 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_non_default) {
CellStyle set = tui->styles[win_id * UI_STYLE_MAX + id];
if (id != UI_STYLE_DEFAULT) {
+ if (keep_non_default) {
+ CellStyle default_style = tui->styles[win_id * UI_STYLE_MAX + UI_STYLE_DEFAULT];
+ if (!cell_color_equal(cell->style.fg, default_style.fg))
+ set.fg = cell->style.fg;
+ if (!cell_color_equal(cell->style.bg, default_style.bg))
+ set.bg = cell->style.bg;
+ }
set.fg = is_default_fg(set.fg)? cell->style.fg : set.fg;
set.bg = is_default_bg(set.bg)? cell->style.bg : set.bg;
set.attr = cell->style.attr | set.attr;
@@ -281,13 +288,13 @@ void ui_window_style_set(Ui *tui, int win_id, Cell *cell, enum UiStyle id) {
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_non_default) {
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_non_default);
return true;
}
diff --git a/ui.h b/ui.h
index 4558724..c0ca4e3 100644
--- a/ui.h
+++ b/ui.h
@@ -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_non_default);
+bool ui_window_style_set_pos(Win *win, int x, int y, enum UiStyle id, bool keep_non_default);
void ui_window_options_set(Win *win, enum UiOption options);
void ui_window_status(Win *win, const char *status);
diff --git a/view.c b/view.c
index 49f7fc6..28f33e9 100644
--- a/view.c
+++ b/view.c
@@ -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) {
@@ -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_non_default) {
View *view = &win->view;
if (end < view->start || start > view->end)
return;
@@ -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_non_default);
}
col = 0;
} while (pos <= end && (line = line->next));
diff --git a/view.h b/view.h
index 9717465..9767bbe 100644
--- a/view.h
+++ b/view.h
@@ -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_non_default);
/** @} */
diff --git a/vis-lua.c b/vis-lua.c
index 1f19cce..f7e9001 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -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_non_default whether non-default style values should be kept
* @see style_define
* @usage
* win:style(win.STYLE_DEFAULT, 0, 10)
@@ -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_non_default = lua_isboolean(L, 5) && lua_toboolean(L, 5);
+ win_style(win, style, start, end, keep_non_default);
return 0;
}
@@ -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_non_default 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
@@ -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_non_default = lua_isboolean(L, 5) && lua_toboolean(L, 5);
+ bool ret = ui_window_style_set_pos(win, (int)x, (int)y, style, keep_non_default);
lua_pushboolean(L, ret);
return 1;
}
diff --git a/vis.c b/vis.c
index e5b779f..08ee6b2 100644
--- a/vis.c
+++ b/vis.c
@@ -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;
@@ -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;
}
@@ -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);
}
}
@@ -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) {
@@ -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;
}
@@ -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);
}
}