Mercurial > dotfiles
comparison .chezmoitemplates/au.lua @ 573:eb6ebdacf201
Use lua api to create autocmds
author | zegervdv <zegervdv@me.com> |
---|---|
date | Thu, 03 Mar 2022 10:55:29 +0100 |
parents | 1635e29e0ed4 |
children | e258ef0fb4cd |
comparison
equal
deleted
inserted
replaced
572:21745760a5dc | 573:eb6ebdacf201 |
---|---|
1 -- | 1 -- |
2 -- Copied from https://gist.github.com/numToStr/1ab83dd2e919de9235f9f774ef8076da | 2 -- Copied from https://gist.github.com/numToStr/1ab83dd2e919de9235f9f774ef8076da |
3 -- | 3 -- |
4 local cmd = vim.api.nvim_command | |
5 | 4 |
6 local function autocmd(this, event, spec) | 5 local function autocmd(this, event, spec) |
7 local is_table = type(spec) == 'table' | 6 local is_table = type(spec) == 'table' |
8 local pattern = is_table and spec[1] or '*' | 7 local pattern = is_table and spec[1] or '*' |
9 local action = is_table and spec[2] or spec | 8 local action = is_table and spec[2] or spec |
9 | |
10 local opts = { pattern = pattern } | |
10 if type(action) == 'function' then | 11 if type(action) == 'function' then |
11 action = this.set(action) | 12 opts.callback = action |
13 else | |
14 opts.command = action | |
12 end | 15 end |
13 local e = type(event) == 'table' and table.concat(event, ',') or event | 16 |
14 local pattern = type(pattern) == 'table' and table.concat(pattern, ',') or pattern | 17 vim.api.nvim_create_autocmd(event, opts) |
15 cmd('autocmd ' .. e .. ' ' .. pattern .. ' ' .. action) | |
16 end | 18 end |
17 | 19 |
18 local S = { | 20 local S = { |
19 __au = {}, | 21 __au = {}, |
20 } | 22 } |
34 S.__au[id] = fn | 36 S.__au[id] = fn |
35 return string.format('lua require("au").exec("%s")', id) | 37 return string.format('lua require("au").exec("%s")', id) |
36 end | 38 end |
37 | 39 |
38 function S.group(grp, cmds) | 40 function S.group(grp, cmds) |
39 cmd('augroup ' .. grp) | 41 vim.api.nvim_create_augroup(grp, { clear = true }) |
40 cmd 'autocmd!' | |
41 if type(cmds) == 'function' then | 42 if type(cmds) == 'function' then |
43 -- TODO set group? | |
42 cmds(X) | 44 cmds(X) |
43 else | 45 else |
44 for _, au in ipairs(cmds) do | 46 for _, au in ipairs(cmds) do |
45 autocmd(S, au[1], { au[2], au[3] }) | 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) | |
46 end | 54 end |
47 end | 55 end |
48 cmd 'augroup END' | |
49 end | 56 end |
50 | 57 |
51 return X | 58 return X |