460
|
1 --
|
|
2 -- Copied from https://gist.github.com/numToStr/1ab83dd2e919de9235f9f774ef8076da
|
|
3 --
|
|
4 local cmd = vim.api.nvim_command
|
|
5
|
|
6 local 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
|
489
|
14 local pattern = type(pattern) == 'table' and table.concat(pattern, ',') or pattern
|
460
|
15 cmd('autocmd ' .. e .. ' ' .. pattern .. ' ' .. action)
|
|
16 end
|
|
17
|
|
18 local S = {
|
|
19 __au = {},
|
|
20 }
|
|
21
|
|
22 local X = setmetatable({}, {
|
|
23 __index = S,
|
|
24 __newindex = autocmd,
|
|
25 __call = autocmd,
|
|
26 })
|
|
27
|
|
28 function S.exec(id)
|
|
29 S.__au[id]()
|
|
30 end
|
|
31
|
|
32 function S.set(fn)
|
|
33 local id = string.format('%p', fn)
|
|
34 S.__au[id] = fn
|
|
35 return string.format('lua require("au").exec("%s")', id)
|
|
36 end
|
|
37
|
|
38 function S.group(grp, cmds)
|
|
39 cmd('augroup ' .. grp)
|
|
40 cmd 'autocmd!'
|
|
41 if type(cmds) == 'function' then
|
|
42 cmds(X)
|
|
43 else
|
|
44 for _, au in ipairs(cmds) do
|
|
45 autocmd(S, au[1], { au[2], au[3] })
|
|
46 end
|
|
47 end
|
|
48 cmd 'augroup END'
|
|
49 end
|
|
50
|
|
51 return X
|