diff options
author | John Ankarström <john@ankarstrom.se> | 2018-10-14 19:05:29 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2023-03-15 14:19:57 +0100 |
commit | 3a9dee34f3f42a3e0177174b9b62d64570533980 (patch) | |
tree | d807ccc0554a6f69b53d707802cad3c5b306b299 | |
parent | 60f8fa3494d1981ede502f4caf8b471d25de759d (diff) | |
download | vis-filetype-settings-3a9dee34f3f42a3e0177174b9b62d64570533980.tar.gz |
added support for all event except INIT, START, QUIT
-rw-r--r-- | README.md | 49 | ||||
-rw-r--r-- | vis-filetype-settings.lua | 75 |
2 files changed, 112 insertions, 12 deletions
@@ -1,4 +1,4 @@ -## vis-filetype-settings +## vis-filetype-settings (move-events branch) This plugin provides a declarative interface for setting vis options depending on filetype. @@ -25,8 +25,51 @@ settings = { } ``` -Be sure not to run commands that open another window with the same -filetype, leading to an infinite loop. +### More events + +By default, all settings are run on the WIN_OPEN event, but if you +want, you can specify your own event. + +For more information about these events, check out the [documentation +for the Lua API][doc]. Below is a description of which events are +supported by this plugin. + +[doc]: http://martanne.github.io/vis/doc/index.html#events + +#### File events + +- FILE_OPEN +- FILE_CLOSE +- FILE_SAVE_POST +- FILE_SAVE_PRE + +```lua +settings = { + go = {"set et off", "set tw 8", FILE_SAVE_PRE = "gofmt"} +} +``` + +This will execute the vis command `gofmt` when Go files are saved. + +**WARNING: The command will run in the current window, regardless +of whether it contains a Go file**. + +### Window events + +- WIN_CLOSE +- WIN_HIGHLIGHT +- WIN_OPEN +- WIN_STATUS + +WIN_OPEN is the default event. + +### Other events + +- INPUT + +**WARNING: If you run a vis command on INPUT, vis will automatically +return to normal mode.** There is currently no workaround, as far +as I am aware. ### Installation diff --git a/vis-filetype-settings.lua b/vis-filetype-settings.lua index d96a0af..9f2665a 100644 --- a/vis-filetype-settings.lua +++ b/vis-filetype-settings.lua @@ -25,15 +25,72 @@ -- Be sure not to run commands that open another window with the same -- filetype, leading to an infinite loop. -vis.events.subscribe(vis.events.WIN_OPEN, function(win) - if settings == nil then return end - local window_settings = settings[win.syntax] - - if type(window_settings) == "table" then - for _, setting in pairs(window_settings) do - vis:command(setting) +function execute(s, arg, arg2) + if type(s) == "table" then + for key, setting in pairs(s) do + if type(key) == "number" then -- ignore EVENT keys + vis:command(setting) + end + end + elseif type(s) == "function" then + if arg2 then + s(arg, arg2) + else + s(arg) end - elseif type(window_settings) == "function" then - window_settings(win) + end +end + +-- Register events + +vis.events.subscribe(vis.events.INPUT, function() + if settings[vis.win.syntax] and settings[vis.win.syntax].INPUT then + execute(settings[vis.win.syntax].INPUT, nil) end end) + +local file_events = { + "FILE_OPEN", + "FILE_CLOSE", + "FILE_SAVE_POST", + "FILE_SAVE_PRE" +} + +for _, event in pairs(file_events) do + vis.events.subscribe(vis.events[event], function(file, path) + for win in vis:windows() do + if win.file == file then + if settings[win.syntax] and settings[win.syntax][event] then + execute(settings[win.syntax][event], file, path) + end + end + end + end) +end + +local win_events = { + "WIN_CLOSE", + "WIN_HIGHLIGHT", + "WIN_OPEN", + "WIN_STATUS" +} + +-- vis.events.subscribe(vis.events.WIN_OPEN, function(win) +-- if settings == nil then return end +-- local window_settings = settings[win.syntax] +-- +-- if type(window_settings) == "table" then +-- for _, setting in pairs(window_settings) do +-- vis:command(setting) + +for _, event in pairs(win_events) do + vis.events.subscribe(vis.events[event], function(win) + if settings[win.syntax] then + if settings[win.syntax][event] then + execute(settings[win.syntax][event], win) + elseif event == "WIN_OPEN" then -- default event + execute(settings[win.syntax], win) + end + end + end) +end |