summaryrefslogtreecommitdiff
path: root/init.lua
blob: b3f06c69505b253daf5272891a9265171aadb39c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
local M = {snippets={}}

local source_str = debug.getinfo(1, 'S').source:sub(2)
local script_path = source_str:match('(.*/)')
local snippets_path = script_path .. 'snippets'

vis:command_register('snippet', function(argv, _, win, _, _)
	local snippet = argv[1]
	if not snippet then return end
	local pos = win.selection.pos
	if not M.snippets[snippet] then
		local snippet_path = snippets_path .. '/' .. argv[1]
		local file = io.open(snippet_path, "rb") -- r read mode and b binary mode
		if not file then return end
		local content = file:read("*a")
		file:close()
		win.file:insert(pos, content)
	else
		win.file:insert(pos, M.snippets[snippet])
	end
end, 'Insert a snippet after the primary selection')

return M