diff options
| -rw-r--r-- | lua/plugins/filetype.lua | 4 | ||||
| -rw-r--r-- | lua/vis-std.lua | 35 | ||||
| -rw-r--r-- | lua/vis.lua | 47 | ||||
| -rw-r--r-- | main.c | 1 | ||||
| -rw-r--r-- | sam.c | 6 | ||||
| -rw-r--r-- | vis-cmds.c | 17 | ||||
| -rw-r--r-- | vis-core.h | 1 | ||||
| -rw-r--r-- | vis-lua.c | 56 | ||||
| -rw-r--r-- | vis.c | 18 | ||||
| -rw-r--r-- | vis.h | 4 |
10 files changed, 55 insertions, 134 deletions
diff --git a/lua/plugins/filetype.lua b/lua/plugins/filetype.lua index 3bfda55..4d522e6 100644 --- a/lua/plugins/filetype.lua +++ b/lua/plugins/filetype.lua @@ -405,7 +405,7 @@ vis.events.subscribe(vis.events.WIN_OPEN, function(win) for _, cmd in pairs(filetype.cmd or {}) do vis:command(cmd) end - win.syntax = syntax + win:set_syntax(syntax) end local name = win.file.name @@ -470,6 +470,6 @@ vis.events.subscribe(vis.events.WIN_OPEN, function(win) end end - win.syntax = nil + win:set_syntax(nil) end) diff --git a/lua/vis-std.lua b/lua/vis-std.lua index 4492fcc..845785d 100644 --- a/lua/vis-std.lua +++ b/lua/vis-std.lua @@ -17,39 +17,18 @@ vis.events.subscribe(vis.events.THEME_CHANGE, function(name) if vis.lexers then vis.lexers.lexers = {} end for win in vis:windows() do - win.syntax = win.syntax; + win:set_syntax(win.syntax) end end) -vis.events.subscribe(vis.events.WIN_SYNTAX, function(win, name) - local lexers = vis.lexers - if not lexers then return false end - - win:style_define(win.STYLE_DEFAULT, lexers.STYLE_DEFAULT or '') - win:style_define(win.STYLE_CURSOR, lexers.STYLE_CURSOR or '') - win:style_define(win.STYLE_CURSOR_PRIMARY, lexers.STYLE_CURSOR_PRIMARY or '') - win:style_define(win.STYLE_CURSOR_LINE, lexers.STYLE_CURSOR_LINE or '') - win:style_define(win.STYLE_SELECTION, lexers.STYLE_SELECTION or '') - win:style_define(win.STYLE_LINENUMBER, lexers.STYLE_LINENUMBER or '') - win:style_define(win.STYLE_COLOR_COLUMN, lexers.STYLE_COLOR_COLUMN or '') - win:style_define(win.STYLE_STATUS, lexers.STYLE_STATUS or '') - win:style_define(win.STYLE_STATUS_FOCUSED, lexers.STYLE_STATUS_FOCUSED or '') - win:style_define(win.STYLE_SEPARATOR, lexers.STYLE_SEPARATOR or '') - win:style_define(win.STYLE_INFO, lexers.STYLE_INFO or '') - win:style_define(win.STYLE_EOF, lexers.STYLE_EOF or '') - - if name == nil then return true end - - local lexer = lexers.load(name) - if not lexer then return false end - - for token_name, id in pairs(lexer._TOKENSTYLES) do - local style = lexers['STYLE_'..string.upper(token_name)] or lexer._EXTRASTYLES[token_name] - win:style_define(id, style) +vis:option_register("syntax", "string", function(name) + if not vis.win then return false end + if not vis.win:set_syntax(name) then + vis:info(string.format("Unknown syntax definition: `%s'", name)) + return false end - return true -end) +end, "Syntax highlighting lexer to use") vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win, horizon_max) if win.syntax == nil or vis.lexers == nil then return end diff --git a/lua/vis.lua b/lua/vis.lua index 286af3d..9b45483 100644 --- a/lua/vis.lua +++ b/lua/vis.lua @@ -114,7 +114,6 @@ local events = { WIN_HIGHLIGHT = "Event::WIN_HIGHLIGHT", -- see @{win_highlight} WIN_OPEN = "Event::WIN_OPEN", -- see @{win_open} WIN_STATUS = "Event::WIN_STATUS", -- see @{win_status} - WIN_SYNTAX = "Event::WIN_SYNTAX", -- see @{win_syntax} } events.file_close = function(...) events.emit(events.FILE_CLOSE, ...) end @@ -130,7 +129,6 @@ events.win_close = function(...) events.emit(events.WIN_CLOSE, ...) end events.win_highlight = function(...) events.emit(events.WIN_HIGHLIGHT, ...) end events.win_open = function(...) events.emit(events.WIN_OPEN, ...) end events.win_status = function(...) events.emit(events.WIN_STATUS, ...) end -events.win_syntax = function(...) return events.emit(events.WIN_SYNTAX, ...) end local handlers = {} @@ -191,6 +189,51 @@ end vis.events = events --- +-- @type Window + +--- The file type associated with this window. +-- @tfield string syntax the syntax lexer name or `nil` if unset + +--- Change syntax lexer to use for this window +-- @function set_syntax +-- @tparam string syntax the syntax lexer name or `nil` to disable syntax highlighting +-- @treturn bool whether the lexer could be changed +vis.types.window.set_syntax = function(win, syntax) + + local lexers = vis.lexers + if not lexers then return false end + + win:style_define(win.STYLE_DEFAULT, lexers.STYLE_DEFAULT or '') + win:style_define(win.STYLE_CURSOR, lexers.STYLE_CURSOR or '') + win:style_define(win.STYLE_CURSOR_PRIMARY, lexers.STYLE_CURSOR_PRIMARY or '') + win:style_define(win.STYLE_CURSOR_LINE, lexers.STYLE_CURSOR_LINE or '') + win:style_define(win.STYLE_SELECTION, lexers.STYLE_SELECTION or '') + win:style_define(win.STYLE_LINENUMBER, lexers.STYLE_LINENUMBER or '') + win:style_define(win.STYLE_COLOR_COLUMN, lexers.STYLE_COLOR_COLUMN or '') + win:style_define(win.STYLE_STATUS, lexers.STYLE_STATUS or '') + win:style_define(win.STYLE_STATUS_FOCUSED, lexers.STYLE_STATUS_FOCUSED or '') + win:style_define(win.STYLE_SEPARATOR, lexers.STYLE_SEPARATOR or '') + win:style_define(win.STYLE_INFO, lexers.STYLE_INFO or '') + win:style_define(win.STYLE_EOF, lexers.STYLE_EOF or '') + + if syntax == nil or syntax == 'off' then + win.syntax = nil + return true + end + + local lexer = lexers.load(syntax) + if not lexer then return false end + + for token_name, id in pairs(lexer._TOKENSTYLES) do + local style = lexers['STYLE_'..string.upper(token_name)] or lexer._EXTRASTYLES[token_name] + win:style_define(id, style) + end + + win.syntax = syntax + return true +end + +--- -- @type File --- Check whether LPeg pattern matches at a given file position. @@ -1998,7 +1998,6 @@ int main(int argc, char *argv[]) { .win_open = vis_lua_win_open, .win_close = vis_lua_win_close, .win_highlight = vis_lua_win_highlight, - .win_syntax = vis_lua_win_syntax, .win_status = vis_lua_win_status, }; @@ -281,7 +281,6 @@ enum { OPTION_EXPANDTAB, OPTION_TABWIDTH, OPTION_THEME, - OPTION_SYNTAX, OPTION_SHOW_SPACES, OPTION_SHOW_TABS, OPTION_SHOW_NEWLINES, @@ -325,11 +324,6 @@ static const OptionDef options[] = { VIS_OPTION_TYPE_STRING, VIS_HELP("Color theme to use filename without extension") }, - [OPTION_SYNTAX] = { - { "syntax" }, - VIS_OPTION_TYPE_STRING|VIS_OPTION_VALUE_OPTIONAL|VIS_OPTION_NEED_WINDOW, - VIS_HELP("Syntax highlighting lexer to use filename without extension") - }, [OPTION_SHOW_SPACES] = { { "show-spaces" }, VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW, @@ -262,23 +262,6 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor case OPTION_TABWIDTH: tabwidth_set(vis, arg.i); break; - case OPTION_SYNTAX: - if (!argv[2]) { - const char *syntax = vis_window_syntax_get(win); - if (syntax) - vis_info_show(vis, "Syntax definition in use: `%s'", syntax); - else - vis_info_show(vis, "No syntax definition in use"); - return true; - } - - if (parse_bool(argv[2], &arg.b) && !arg.b) - return vis_window_syntax_set(win, NULL); - if (!vis_window_syntax_set(win, argv[2])) { - vis_info_show(vis, "Unknown syntax definition: `%s'", argv[2]); - return false; - } - break; case OPTION_SHOW_SPACES: case OPTION_SHOW_TABS: case OPTION_SHOW_NEWLINES: @@ -217,7 +217,6 @@ enum VisEvents { VIS_EVENT_WIN_OPEN, VIS_EVENT_WIN_CLOSE, VIS_EVENT_WIN_HIGHLIGHT, - VIS_EVENT_WIN_SYNTAX, VIS_EVENT_WIN_STATUS, }; @@ -160,7 +160,6 @@ void vis_lua_file_close(Vis *vis, File *file) { } void vis_lua_win_open(Vis *vis, Win *win) { } void vis_lua_win_close(Vis *vis, Win *win) { } void vis_lua_win_highlight(Vis *vis, Win *win, size_t horizon) { } -bool vis_lua_win_syntax(Vis *vis, Win *win, const char *syntax) { return true; } bool vis_theme_load(Vis *vis, const char *name) { return true; } void vis_lua_win_status(Vis *vis, Win *win) { window_status_update(vis, win); } @@ -1395,12 +1394,6 @@ static const struct luaL_Reg registers_funcs[] = { * The cursors of this window. * @tfield Array(Cursor) cursors */ -/*** - * The file type associated with this window. - * - * Setting this field to a valid lexer name will automatically active it. - * @tfield string syntax the syntax lexer name or `nil` if unset - */ static int window_index(lua_State *L) { Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW); @@ -1438,33 +1431,11 @@ static int window_index(lua_State *L) { obj_ref_new(L, win->view, VIS_LUA_TYPE_CURSORS); return 1; } - - if (strcmp(key, "syntax") == 0) { - const char *syntax = vis_window_syntax_get(win); - lua_pushstring(L, syntax); - return 1; - } } return index_common(L); } -static int window_newindex(lua_State *L) { - Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW); - - if (lua_isstring(L, 2)) { - const char *key = lua_tostring(L, 2); - if (strcmp(key, "syntax") == 0) { - const char *syntax = NULL; - if (!lua_isnil(L, 3)) - syntax = luaL_checkstring(L, 3); - vis_window_syntax_set(win, syntax); - return 0; - } - } - return newindex_common(L); -} - static int window_cursors_iterator_next(lua_State *L) { Cursor **handle = lua_touserdata(L, lua_upvalueindex(1)); if (!*handle) @@ -1578,7 +1549,7 @@ static int window_draw(lua_State *L) { static const struct luaL_Reg window_funcs[] = { { "__index", window_index }, - { "__newindex", window_newindex }, + { "__newindex", newindex_common }, { "cursors_iterator", window_cursors_iterator }, { "map", window_map }, { "style_define", window_style_define }, @@ -2758,31 +2729,6 @@ void vis_lua_win_highlight(Vis *vis, Win *win, size_t horizon) { } /*** - * Window syntax/filetype change. - * @function win_syntax - * @tparam Window win the affected window - * @tparam string syntax the lexer name or `nil` if syntax highlighting should be disabled for this window - * @treturn bool whether the syntax change was successful - */ -bool vis_lua_win_syntax(Vis *vis, Win *win, const char *syntax) { - lua_State *L = vis->lua; - bool ret = false; - vis_lua_event_get(L, "win_syntax"); - if (lua_isfunction(L, -1)) { - obj_ref_new(L, win, VIS_LUA_TYPE_WINDOW); - if (syntax) - lua_pushstring(L, syntax); - else - lua_pushnil(L); - pcall(vis, L, 2, 1); - ret = lua_toboolean(L, -1); - lua_pop(L, 1); - } - lua_pop(L, 1); - return ret; -} - -/*** * Window status bar redraw. * @function win_status * @tparam Window win the affected window @@ -105,7 +105,6 @@ bool vis_event_emit(Vis *vis, enum VisEvents id, ...) { case VIS_EVENT_WIN_OPEN: case VIS_EVENT_WIN_CLOSE: case VIS_EVENT_WIN_HIGHLIGHT: - case VIS_EVENT_WIN_SYNTAX: case VIS_EVENT_WIN_STATUS: { Win *win = va_arg(ap, Win*); @@ -117,9 +116,6 @@ bool vis_event_emit(Vis *vis, enum VisEvents id, ...) { vis->event->win_close(vis, win); } else if (vis->event->win_highlight && id == VIS_EVENT_WIN_HIGHLIGHT) { vis->event->win_highlight(vis, win, win->horizon); - } else if (vis->event->win_syntax && id == VIS_EVENT_WIN_SYNTAX) { - const char *syntax = va_arg(ap, const char*); - ret = vis->event->win_syntax(vis, win, syntax); } else if (vis->event->win_status && id == VIS_EVENT_WIN_STATUS) { vis->event->win_status(vis, win); } @@ -534,7 +530,6 @@ bool vis_window_split(Win *original) { map_copy(win->modes[i].bindings, original->modes[i].bindings); } win->file = original->file; - vis_window_syntax_set(win, vis_window_syntax_get(original)); view_options_set(win->view, view_options_get(original->view)); view_cursor_to(win->view, view_cursor_get(original->view)); return true; @@ -565,19 +560,6 @@ void vis_window_prev(Vis *vis) { vis_window_focus(sel); } -const char *vis_window_syntax_get(Win *win) { - return win->lexer_name; -} - -bool vis_window_syntax_set(Win *win, const char *syntax) { - if (!vis_event_emit(win->vis, VIS_EVENT_WIN_SYNTAX, win, syntax)) - return false; - view_options_set(win->view, view_options_get(win->view)); - free(win->lexer_name); - win->lexer_name = syntax ? strdup(syntax) : NULL; - return !syntax || win->lexer_name; -} - int vis_window_width_get(const Win *win) { return win->ui->window_width(win->ui); } @@ -52,7 +52,6 @@ typedef struct { void (*win_open)(Vis*, Win*); void (*win_close)(Vis*, Win*); void (*win_highlight)(Vis*, Win*, size_t horizon); - bool (*win_syntax)(Vis*, Win*, const char *syntax); void (*win_status)(Vis*, Win*); } VisEvent; @@ -128,9 +127,6 @@ void vis_window_focus(Win*); /* swap location of two windows */ void vis_window_swap(Win*, Win*); -const char *vis_window_syntax_get(Win*); -bool vis_window_syntax_set(Win*, const char *name); - int vis_window_width_get(const Win*); int vis_window_height_get(const Win*); |
