aboutsummaryrefslogtreecommitdiffstats
path: root/vis-filetype-settings.lua
diff options
context:
space:
mode:
Diffstat (limited to 'vis-filetype-settings.lua')
-rw-r--r--vis-filetype-settings.lua75
1 files changed, 66 insertions, 9 deletions
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