0

As title. I have a pair of autocmds which needs each other:

  1. One on WinLeave, this one is fine since it will be triggered when I call :vsplit.
  2. One on BufEnter, this one is NOT fine for the edge case, i.e. :vsplit.

Every time WinLeave happens, it will call a function, which expects a pairing-function to be called on BufEnter. If I have no buffer opened in many windows, then no problem. But I just realized that calling :vsplit create the only edge case: BufEnter will not be triggered, thus the function from WinLeave will not be paired with the pairing-function from BufEnter.

I did try WinEnter but it doesn't satisfy my requirement. That is: the timing is different. BufEnter is called later than WinEnter. If I use WinEnter then the pairing-function will be called too early. So is there any way so that BufEnter can also be triggered on the same buffer in different window?

2 Answers 2

0

You misuse autocmds. There's no "timing" here. Bufleave/bufenter are triggered if and only if active buffer changes. But when the same buffer is opened in both windows it doesn't change, obviously. And so there is no (and must be no) events other than winnew/winleave/winenter. This is not a sort of corner case. This is how Vim interface works precisely: splitting changes windows but not buffers, editing changes buffers but not windows etc.etc.

This is a user who decides what to do. And events are simply results of decisions he made. You (programmer) must not dictate but comply.

So what you need is to rethink the way events work in Vim and to revise your program logic.

5
  • But with :h WinEnter I see If the window is for another buffer, Vim executes the BufEnter autocommands after the WinEnter autocommands. I'm using neovim though. Dec 29, 2022 at 22:12
  • And I said this in my OP because I already did some testing: if I instead use WinEnter for my point 2., then the animation(created by plugin) did not get played at the right timing as BufEnter. While I haven't read the source, but it seems that BufEnter does get called later than WinEnter. Dec 29, 2022 at 22:16
  • Even without testing: The author(s) of docs did use words like After BufAdd, After BufReadPost(:h BufEnter). Or maybe we have different interpretations regarding the word "timing". Dec 29, 2022 at 22:19
  • @job_start This should be understood as follows: if some action causes both events at once (e.g. both winleave and bufleave, or both bufread and bufenter) then the events will be triggered in this order... It doesn't mean it is always like that. Sometimes, only a single event will be fired.
    – Matt
    Dec 29, 2022 at 22:33
  • OK, while I still don't understand it fully, I agree with your last point that I probably should not rely on these events to implement the function I need. Dec 29, 2022 at 22:35
0

I don't need points but I here to prove that there is indeed timing regarding events:

Logs

Based on the logs, The timing of BufEnter getting called is always after WinEnter.

06:24:00 msg_show WinEnter is called
06:24:00 msg_show BufEnter is called
06:24:01 msg_show WinEnter is called
06:24:01 msg_show BufEnter is called
   Error  06:24:06 msg_show.lua_error WinEnter is called
06:24:06 msg_show BufEnter is called
06:24:08 msg_show WinEnter is called
06:24:08 msg_show BufEnter is called
06:24:11 msg_show WinEnter is called
06:24:11 msg_show BufEnter is called
06:24:12 msg_show WinEnter is called
06:24:12 msg_show BufEnter is called

To reproduce in NeoVim:

  1. Add this to your neovim config:
vim.api.nvim_create_autocmd({ 'BufEnter' }, {
  pattern = '*',
  callback = function ()
    print('BufEnter is called')
  end
})
vim.api.nvim_create_autocmd({ 'WinEnter' }, {
  pattern = '*',
  callback = function ()
    print('WinEnter is called')
  end
})
  1. Create many splits and jump among them.
2
  • And now try to trace edit or buffer commands...
    – Matt
    Dec 29, 2022 at 22:36
  • OK, I assume you're right. But isn't that out of my concern, since in this post I only care about the ordering of BufEtner and WinEnter? I will schedule a time to try it after I finish my plugin. Dec 29, 2022 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.