summary refs log tree commit diff
path: root/.vim/lua/utils.lua
blob: 271d593dde6b9e741799c29c022b862023473922 (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
-- utils.lua

local M = {}

-- crutch until https://github.com/neovim/neovim/pull/13479
M.opt = function(scope, key, val)
	local scopes = {o = vim.o, b = vim.bo, w = vim.wo}
	if not ( scope and key and val ) then return end
	scopes[scope][key] = val
	if scope ~= 'o' then scopes['o'][key] = val end
end

M.map = function(mode, lhs, rhs, opts)
	if not ( mode and lhs and rhs ) then return end
	local o = {noremap = true, silent = true}
	if opts then o = vim.tbl_extend('force', o, opts) end
	vim.api.nvim_set_keymap(mode, lhs, rhs, o)
end

M.augroup = function(name, autocmds)
	if not ( name and autocmds ) then return end
	vim.cmd('augroup ' .. name)
	vim.cmd('autocmd!')
	for _, au in ipairs(autocmds) do
		vim.cmd('autocmd ' .. table.concat(au, ' '))
	end
	vim.cmd('augroup END')
end
M.augroup2 = function(tbl)
	for name, autocmds in pairs(tbl) do
		vim.cmd('augroup ' .. name)
		vim.cmd('autocmd!')
		for _, au in ipairs(autocmds) do
			vim.cmd('autocmd ' .. table.concat(au, ' '))
		end
		vim.cmd('augroup END')
	end
end

M.def = function(name, cmd, ...)
	local opts = table.concat((... or {}), ' ')
	vim.cmd('command! ' .. opts .. ' ' .. name .. ' ' .. cmd)
end

return M