nvim_config/lua/config/utils.lua

43 lines
1.2 KiB
Lua
Raw Normal View History

2021-09-06 23:34:51 +02:00
local cmd = vim.cmd
local o_s = vim.o
local map_key = vim.api.nvim_set_keymap
local set_g = vim.api.nvim_set_var
local function opt(o, v, scopes)
scopes = scopes or {o_s}
for _, s in ipairs(scopes) do s[o] = v end
end
local function autocmd(group, cmds, clear)
clear = clear == nil and false or clear
if type(cmds) == 'string' then cmds = {cmds} end
cmd('augroup ' .. group)
if clear then cmd [[au!]] end
for _, c in ipairs(cmds) do cmd('autocmd ' .. c) end
cmd [[augroup END]]
end
local function map(modes, lhs, rhs, opts)
opts = opts or {}
opts.noremap = opts.noremap == nil and true or opts.noremap
if type(modes) == 'string' then modes = {modes} end
for _, mode in ipairs(modes) do map_key(mode, lhs, rhs, opts) end
end
local function let(mode, name, value)
local assignment = 'let '
if mode == nil then
assignment = assignment .. name .. ' = '
else
assignment = assignment .. mode .. ':' .. name .. ' = '
end
if type(value) == 'string' then
assignment = assignment .. '"' .. value .. '"'
else
assignment = assignment .. value
end
cmd(assignment)
end
return {opt = opt, autocmd = autocmd, map = map, set_g = set_g, let = let}