diff options
Diffstat (limited to '.config/nvim/lua/au.lua')
-rw-r--r-- | .config/nvim/lua/au.lua | 50 |
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 | -- | ||
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 | ||
14 | cmd('autocmd ' .. e .. ' ' .. pattern .. ' ' .. action) | ||
15 | end | ||
16 | |||
17 | local S = { | ||
18 | __au = {}, | ||
19 | } | ||
20 | |||
21 | local X = setmetatable({}, { | ||
22 | __index = S, | ||
23 | __newindex = autocmd, | ||
24 | __call = autocmd, | ||
25 | }) | ||
26 | |||
27 | function S.exec(id) | ||
28 | S.__au[id]() | ||
29 | end | ||
30 | |||
31 | function 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) | ||
35 | end | ||
36 | |||
37 | function 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') | ||
48 | end | ||
49 | |||
50 | return X | ||