summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/au.lua
diff options
context:
space:
mode:
Diffstat (limited to '.config/nvim/lua/au.lua')
-rw-r--r--.config/nvim/lua/au.lua50
1 files changed, 50 insertions, 0 deletions
diff --git a/.config/nvim/lua/au.lua b/.config/nvim/lua/au.lua
new file mode 100644
index 0000000..305169c
--- /dev/null
+++ b/.config/nvim/lua/au.lua
@@ -0,0 +1,50 @@
1--
2-- https://gist.github.com/numToStr/1ab83dd2e919de9235f9f774ef8076da
3--
4local cmd = vim.api.nvim_command
5
6local function autocmd(this, event, spec)
7 local is_table = type(spec) == 'table'
8 local pattern = is_table and spec[1] or '*'
9 local action = is_table and spec[2] or spec
10 if type(action) == 'function' then
11 action = this.set(action)
12 end
13 local e = type(event) == 'table' and table.concat(event, ',') or event
14 cmd('autocmd ' .. e .. ' ' .. pattern .. ' ' .. action)
15end
16
17local S = {
18 __au = {},
19}
20
21local X = setmetatable({}, {
22 __index = S,
23 __newindex = autocmd,
24 __call = autocmd,
25})
26
27function S.exec(id)
28 S.__au[id]()
29end
30
31function S.set(fn)
32 local id = string.format('%p', fn)
33 S.__au[id] = fn
34 return string.format('lua require("au").exec("%s")', id)
35end
36
37function S.group(grp, cmds)
38 cmd('augroup ' .. grp)
39 cmd('autocmd!')
40 if type(cmds) == 'function' then
41 cmds(X)
42 else
43 for _, au in ipairs(cmds) do
44 autocmd(S, au[1], { au[2], au[3] })
45 end
46 end
47 cmd('augroup END')
48end
49
50return X