heirline.nvim icon indicating copy to clipboard operation
heirline.nvim copied to clipboard

Add support for subscribing to multiple events in update

Open metalinspired opened this issue 1 year ago • 8 comments

Closes #220

metalinspired avatar Mar 16 '25 12:03 metalinspired

Hello, I'm interested in this change, is there anything blocking this PR?

bew avatar Apr 29 '25 05:04 bew

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?

gbroques avatar Jul 31 '25 23:07 gbroques

@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 avatar Aug 06 '25 04:08 gbroques

@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.

metalinspired avatar Aug 06 '25 17:08 metalinspired

@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 avatar Aug 06 '25 21:08 gbroques

@gbroques according to the docs they are optional

metalinspired avatar Aug 11 '25 17:08 metalinspired

@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 avatar Aug 11 '25 20:08 gbroques

@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.

metalinspired avatar Aug 12 '25 06:08 metalinspired