diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-03-19 11:51:00 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-03-19 14:58:06 +0100 |
| commit | 42f04699d6df2d5b144533737a8f2f7e0814ad85 (patch) | |
| tree | 3d69b94c221e3b91749b7f09b7f4da89839206ca /lua | |
| parent | 8919fd1cbeef89aa3a5fce9c00aa708335e1de85 (diff) | |
| download | vis-42f04699d6df2d5b144533737a8f2f7e0814ad85.tar.gz vis-42f04699d6df2d5b144533737a8f2f7e0814ad85.tar.xz | |
Move :set syntax option implementation to lua
It is no longer possible to change the used syntax by assigning to the
`win.syntax = name` field, instead the function win:set_syntax(name)`
should be called.
The distinction between filetype and syntax lexer to use should probably
be clarified/cleaned up at some point.
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/plugins/filetype.lua | 4 | ||||
| -rw-r--r-- | lua/vis-std.lua | 35 | ||||
| -rw-r--r-- | lua/vis.lua | 47 |
3 files changed, 54 insertions, 32 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. |
