vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit c3e2fdeb8a0b87430f33e72a1e3f4fb7a4cf54e7 parent 837e2bda1b28013498a3cbc6df52e8cf8f2cd997 Author: Rudy Dellomas III <dther@dther.xyz> Date: Sun, 21 Apr 2024 20:25:40 +1000 Emit an event (ui_draw) immediately before drawing the screen This allows better control over styling, as well as potential for entirely new UI elements implemented entirely using the Lua API. Diffstat:
| M | lua/vis.lua | | | 2 | ++ |
| M | main.c | | | 1 | + |
| M | ui-terminal.c | | | 1 | + |
| M | vis-core.h | | | 1 | + |
| M | vis-lua.c | | | 14 | +++++++++++++- |
| M | vis-lua.h | | | 1 | + |
| M | vis.c | | | 4 | ++++ |
| M | vis.h | | | 1 | + |
8 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/lua/vis.lua b/lua/vis.lua @@ -167,6 +167,7 @@ local events = { WIN_STATUS = "Event::WIN_STATUS", -- see @{win_status} TERM_CSI = "Event::TERM_CSI", -- see @{term_csi} PROCESS_RESPONSE = "Event::PROCESS_RESPONSE", -- see @{process_response} + UI_DRAW = "Event::UI_DRAW", -- see @{ui_draw} } events.file_close = function(...) events.emit(events.FILE_CLOSE, ...) end @@ -183,6 +184,7 @@ events.win_open = function(...) events.emit(events.WIN_OPEN, ...) end events.win_status = function(...) events.emit(events.WIN_STATUS, ...) end events.term_csi = function(...) events.emit(events.TERM_CSI, ...) end events.process_response = function(...) events.emit(events.PROCESS_RESPONSE, ...) end +events.ui_draw = function(...) events.emit(events.UI_DRAW, ...) end local handlers = {} diff --git a/main.c b/main.c @@ -2229,6 +2229,7 @@ int main(int argc, char *argv[]) { .win_highlight = vis_lua_win_highlight, .win_status = vis_lua_win_status, .term_csi = vis_lua_term_csi, + .ui_draw = vis_lua_ui_draw, }; vis = vis_new(ui_term_new(), &event); diff --git a/ui-terminal.c b/ui-terminal.c @@ -392,6 +392,7 @@ static void ui_draw(Ui *ui) { ui_window_draw((UiWin*)win); if (tui->info[0]) ui_draw_string(tui, 0, tui->height-1, tui->info, NULL, UI_STYLE_INFO); + vis_event_emit(tui->vis, VIS_EVENT_UI_DRAW); ui_term_backend_blit(tui); } diff --git a/vis-core.h b/vis-core.h @@ -236,6 +236,7 @@ enum VisEvents { VIS_EVENT_WIN_HIGHLIGHT, VIS_EVENT_WIN_STATUS, VIS_EVENT_TERM_CSI, + VIS_EVENT_UI_DRAW, }; bool vis_event_emit(Vis*, enum VisEvents, ...); diff --git a/vis-lua.c b/vis-lua.c @@ -166,7 +166,7 @@ void vis_lua_win_status(Vis *vis, Win *win) { window_status_update(vis, win); } void vis_lua_term_csi(Vis *vis, const long *csi) { } void vis_lua_process_response(Vis *vis, const char *name, char *buffer, size_t len, ResponseType rtype) { } - +void vis_lua_ui_draw(Vis *vis) { } #else @@ -3689,4 +3689,16 @@ void vis_lua_process_response(Vis *vis, const char *name, lua_pop(L, 1); } +/*** + * Emitted immediately before the UI is drawn to the screen. + * Allows last-minute overrides to the styling of UI elements. + * + * *WARNING:* This is emitted every screen draw! + * Use sparingly and check for `nil` values! + * @function ui_draw + */ +void vis_lua_ui_draw(Vis *vis) { + vis_lua_event_call(vis, "ui_draw"); +} + #endif diff --git a/vis-lua.h b/vis-lua.h @@ -42,5 +42,6 @@ void vis_lua_win_highlight(Vis*, Win*); void vis_lua_win_status(Vis*, Win*); void vis_lua_term_csi(Vis*, const long *); void vis_lua_process_response(Vis *, const char *, char *, size_t, ResponseType); +void vis_lua_ui_draw(Vis*); #endif diff --git a/vis.c b/vis.c @@ -105,6 +105,10 @@ bool vis_event_emit(Vis *vis, enum VisEvents id, ...) { if (vis->event->term_csi) vis->event->term_csi(vis, va_arg(ap, const long *)); break; + case VIS_EVENT_UI_DRAW: + if (vis->event->ui_draw) + vis->event->ui_draw(vis); + break; } va_end(ap); diff --git a/vis.h b/vis.h @@ -58,6 +58,7 @@ typedef struct { void (*win_highlight)(Vis*, Win*); void (*win_status)(Vis*, Win*); void (*term_csi)(Vis*, const long *); + void (*ui_draw)(Vis*); } VisEvent; /** Union used to pass arguments to key action functions. */