aboutsummaryrefslogtreecommitdiffstats
path: root/init.lua
blob: 0c74216d704abf38426242e291946ae3bd32744e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- vis-filetype-settings
-- (https://github.com/jocap/vis-filetype-settings)

-- This plugin provides a declarative interface for setting vis
-- options depending on filetype.
-- 
-- It expects a global variable called `settings` to be defined:
-- 
-- 	settings = {
-- 		markdown = {"set expandtab on", "set tabwidth 4"}
-- 	}
-- 
-- In this variable, filetypes are mapped to sets of settings that are
-- to be executed when a window containing the specified filetype is
-- opened.
-- 
-- If you want to do more than setting simple options, you can specify a function instead:
-- 
-- 	settings = {
-- 		bash = function(win)
-- 			-- do things for shell scripts
-- 		end
-- 	}
-- 
-- Be sure not to run commands that open another window with the same
-- filetype, leading to an infinite loop.

local M = {}

M.settings = {}

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
	end
end

-- Register events

vis.events.subscribe(vis.events.INPUT, function()
	if M.settings[vis.win.syntax] and M.settings[vis.win.syntax].INPUT then
		execute(M.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 M.settings[win.syntax] and M.settings[win.syntax][event] then
					execute(M.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 M.settings[win.syntax] == nil then return end
		if M.settings[win.syntax] then
			if M.settings[win.syntax][event] then
				execute(M.settings[win.syntax][event], win)
			elseif event == "WIN_OPEN" then -- default event
				execute(M.settings[win.syntax], win)
			end
		end
	end)
end

return M