460
|
1 --
|
|
2 -- Copied from https://gist.github.com/numToStr/1ab83dd2e919de9235f9f774ef8076da
|
|
3 --
|
|
4
|
|
5 local function autocmd(this, event, spec)
|
|
6 local is_table = type(spec) == 'table'
|
|
7 local pattern = is_table and spec[1] or '*'
|
|
8 local action = is_table and spec[2] or spec
|
573
|
9
|
|
10 local opts = { pattern = pattern }
|
460
|
11 if type(action) == 'function' then
|
573
|
12 opts.callback = action
|
|
13 else
|
|
14 opts.command = action
|
460
|
15 end
|
573
|
16
|
|
17 vim.api.nvim_create_autocmd(event, opts)
|
460
|
18 end
|
|
19
|
|
20 local S = {
|
|
21 __au = {},
|
|
22 }
|
|
23
|
|
24 local X = setmetatable({}, {
|
|
25 __index = S,
|
|
26 __newindex = autocmd,
|
|
27 __call = autocmd,
|
|
28 })
|
|
29
|
|
30 function S.exec(id)
|
|
31 S.__au[id]()
|
|
32 end
|
|
33
|
|
34 function S.set(fn)
|
|
35 local id = string.format('%p', fn)
|
|
36 S.__au[id] = fn
|
|
37 return string.format('lua require("au").exec("%s")', id)
|
|
38 end
|
|
39
|
|
40 function S.group(grp, cmds)
|
573
|
41 vim.api.nvim_create_augroup(grp, { clear = true })
|
460
|
42 if type(cmds) == 'function' then
|
573
|
43 -- TODO set group?
|
460
|
44 cmds(X)
|
|
45 else
|
|
46 for _, au in ipairs(cmds) do
|
573
|
47 local opts = { group = grp, pattern = au[2] }
|
|
48 if type(au[3]) == 'function' then
|
|
49 opts.callback = au[3]
|
|
50 else
|
|
51 opts.command = au[3]
|
|
52 end
|
|
53 vim.api.nvim_create_autocmd(au[1], opts)
|
460
|
54 end
|
|
55 end
|
|
56 end
|
|
57
|
|
58 return X
|