Add support for subscribing to multiple events in update
Closes #220
Hello, I'm interested in this change, is there anything blocking this PR?
I'm also interested in this PR. The changes don't look too bad in order to support this.
@rebelot Do you mind taking a look when you find time please?
@metalinspired Can you provide some example configuration of how one would use this?
It looks like you can define multiple events with the same pattern and callback.
For example, redrawing the statusline both when the mode changes or text changes:
update = {
'ModeChanged', 'TextChanged',
pattern = '*:*',
callback = vim.schedule_wrap(function()
vim.cmd('redrawstatus')
end),
},
Or define multiple events with a different pattern and callback.
For example, redrawing the statusline both when the mode changes or a user event is triggered:
update = {
{
'ModeChanged',
pattern = '*:*',
callback = vim.schedule_wrap(function()
vim.cmd('redrawstatus')
end),
},
{
'User',
pattern = 'LeapEnter', -- or a table {'LeapEnter', 'LeapLeave'}
callback = vim.schedule_wrap(function()
vim.cmd('redrawstatus')
end),
},
},
However, the following would be invalid:
update = {
{'ModeChanged', 'TextChanged'}, -- ❌ Multiple events can't be nested in a table like this
pattern = '*:*',
callback = vim.schedule_wrap(function()
vim.cmd('redrawstatus')
end),
},
@gbroques you pretty much got it how it works. I completely forgot to add examples 🤦🏻♂️ Basically, the idea was to allow user to repeat the originally supported configuration, hence why your last example does not work. You could even combine it all and do something like this:
update = {
'CursorMoved',
'BufEnter',
pattern = '*:*',
callback = vim.schedule_wrap(function() vim.cmd('redrawstatus') end),
{
'DiagnosticChanged',
},
{
'ModeChanged',
'User',
-- Any other event you wish to subscribe to...
pattern = '*:*',
callback = vim.schedule_wrap(function() vim.cmd('redrawstatus') end),
},
},
Edit: Don't ask me to put this in words as I am terrible with explaining anything. If someone contributes this part I'll gladly add it to the PR.
@gbroques you pretty much got it how it works. I completely forgot to add examples 🤦🏻♂️ Basically, the idea was to allow user to repeat the originally supported configuration, hence why your last example does not work. You could even combine it all and do something like this:
update = { 'CursorMoved', 'BufEnter', pattern = '*:*', callback = vim.schedule_wrap(function() vim.cmd('redrawstatus') end), { 'DiagnosticChanged', }, { 'ModeChanged', 'User', -- Any other event you wish to subscribe to... pattern = '*:*', callback = vim.schedule_wrap(function() vim.cmd('redrawstatus') end), }, },Edit: Don't ask me to put this in words as I am terrible with explaining anything. If someone contributes this part I'll gladly add it to the PR.
Thanks for clarifying this!
I think the following would fail:
{
'DiagnosticChanged',
},
Since you need to specify a callback or command to nvim_create_autocmd, right?
@gbroques according to the docs they are optional
@gbroques according to the docs they are optional
@metalinspired Either callback or command is required.
You can try the following, and should see an error:
vim.api.nvim_create_autocmd({'BufEnter', 'BufWinEnter'}, {
pattern = {'*.c', '*.h'}
})
Error detected while processing init.lua:
E5113: Error while calling lua chunk: autocmd.lua:14: Required: 'command' or 'callback'
stack traceback:
[C]: in function 'nvim_create_autocmd'
autocmd.lua:14: in main chunk
[C]: in function 'require'
init.lua:2: in main chunk
@gbroques did not notice that. But it does not really matter because callback is always defined when calling nvim_create_autocmd. It is just that user provided callback is not called if one was not provided.