vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 836817bfaec0b1b6c3d663bbfed5e0c35903690c parent f6b36604068148ac7b0bca17fc6c3b9106fd0d66 Author: Randy Palamar <randy@rnpnr.xyz> Date: Wed, 8 Jan 2025 20:13:53 -0700 ui: pass window id when setting style There are a couple times when we want to set a style without an active window. In those cases we just want to use base UI_STYLE_*s and (Win *) is not needed. This fixes a crash when trying to do a vis:info() from lua during an initial file open event. Note that this code is due for a serious refactor, ui styles should be stored in Ui and window specific styles should be stored in Win. Then we won't need any of this difficult to follow indexing into the styles array based on window id and we will never have to realloc when a new window opens. Just another thing to add to my list. Diffstat:
| M | ui-terminal.c | | | 19 | +++++++++---------- |
| M | ui.h | | | 2 | +- |
| M | view.c | | | 4 | ++-- |
| M | vis.c | | | 12 | ++++++------ |
4 files changed, 18 insertions(+), 19 deletions(-)
diff --git a/ui-terminal.c b/ui-terminal.c @@ -192,14 +192,14 @@ static void ui_draw_line(Ui *tui, int x, int y, char c, enum UiStyle style_id) { } } -static void ui_draw_string(Ui *tui, int x, int y, const char *str, Win *win, enum UiStyle style_id) { +static void ui_draw_string(Ui *tui, int x, int y, const char *str, int win_id, enum UiStyle style_id) { debug("draw-string: [%d][%d]\n", y, x); if (x < 0 || x >= tui->width || y < 0 || y >= tui->height) return; /* NOTE: the style that style_id refers to may contain unset values; we need to properly * clear the cell first then go through ui_window_style_set to get the correct style */ - CellStyle default_style = tui->styles[UI_STYLE_MAX * win->id + UI_STYLE_DEFAULT]; + CellStyle default_style = tui->styles[UI_STYLE_MAX * win_id + UI_STYLE_DEFAULT]; // FIXME: does not handle double width characters etc, share code with view.c? Cell *cells = tui->cells + y * tui->width; const size_t cell_size = sizeof(cells[0].data)-1; @@ -212,7 +212,7 @@ static void ui_draw_string(Ui *tui, int x, int y, const char *str, Win *win, enu strncpy(cells[x].data, str, len); cells[x].data[len] = '\0'; cells[x].style = default_style; - ui_window_style_set(win, cells + x++, style_id); + ui_window_style_set(tui, win_id, cells + x++, style_id); } } @@ -258,7 +258,7 @@ static void ui_window_draw(Win *win) { } snprintf(buf, sizeof buf, "%*zu ", sidebar_width-1, number); } - ui_draw_string(ui, x, y, buf, win, + ui_draw_string(ui, x, y, buf, win->id, (l->lineno == cursor_lineno) ? UI_STYLE_LINENUMBER_CURSOR : UI_STYLE_LINENUMBER); prev_lineno = l->lineno; @@ -269,9 +269,8 @@ static void ui_window_draw(Win *win) { } } -void ui_window_style_set(Win *win, Cell *cell, enum UiStyle id) { - Ui *tui = &win->vis->ui; - CellStyle set = tui->styles[win->id * UI_STYLE_MAX + id]; +void ui_window_style_set(Ui *tui, int win_id, Cell *cell, enum UiStyle id) { + 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; @@ -288,7 +287,7 @@ bool ui_window_style_set_pos(Win *win, int x, int y, enum UiStyle id) { return false; } Cell *cell = CELL_AT_POS(tui, win->x + x, win->y + y) - ui_window_style_set(win, cell, id); + ui_window_style_set(tui, win->id, cell, id); return true; } @@ -297,7 +296,7 @@ void ui_window_status(Win *win, const char *status) { return; Ui *ui = &win->vis->ui; enum UiStyle style = ui->selwin == win ? UI_STYLE_STATUS_FOCUSED : UI_STYLE_STATUS; - ui_draw_string(ui, win->x, win->y + win->height - 1, status, win, style); + ui_draw_string(ui, win->x, win->y + win->height - 1, status, win->id, style); } void ui_arrange(Ui *tui, enum UiLayout layout) { @@ -356,7 +355,7 @@ void ui_draw(Ui *tui) { for (Win *win = tui->windows; win; win = win->next) ui_window_draw(win); if (tui->info[0]) - ui_draw_string(tui, 0, tui->height-1, tui->info, tui->vis->win, UI_STYLE_INFO); + ui_draw_string(tui, 0, tui->height-1, tui->info, 0, UI_STYLE_INFO); vis_event_emit(tui->vis, VIS_EVENT_UI_DRAW); ui_term_backend_blit(tui); } diff --git a/ui.h b/ui.h @@ -129,7 +129,7 @@ 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(Win *win, Cell *cell, enum UiStyle id); +void ui_window_style_set(Ui *ui, int win_id, Cell *cell, enum UiStyle id); 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 @@ -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, &cell_blank, UI_STYLE_DEFAULT); + ui_window_style_set(&win->vis->ui, win->id, &cell_blank, UI_STYLE_DEFAULT); } static int view_max_text_width(const View *view) { @@ -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, &line->cells[col++], style); + ui_window_style_set(&win->vis->ui, win->id, &line->cells[col++], style); } col = 0; } while (pos <= end && (line = line->next)); diff --git 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, &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); 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(win, &l->cells[x], UI_STYLE_CURSOR_LINE); + ui_window_style_set(&vis->ui, win->id, &l->cells[x], UI_STYLE_CURSOR_LINE); } 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, &l->cells[col++], UI_STYLE_SELECTION); + ui_window_style_set(&win->vis->ui, win->id, &l->cells[col++], UI_STYLE_SELECTION); } } @@ -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, &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); } 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, &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); 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, &l->cells[0], UI_STYLE_EOF); + ui_window_style_set(&win->vis->ui, win->id, l->cells, UI_STYLE_EOF); } }