Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinLeave and WinEnter point at the same buffer on tabnew #29430

Open
asmodeus812 opened this issue Jun 20, 2024 · 6 comments
Open

WinLeave and WinEnter point at the same buffer on tabnew #29430

asmodeus812 opened this issue Jun 20, 2024 · 6 comments
Labels
documentation events events, autocommands

Comments

@asmodeus812
Copy link

asmodeus812 commented Jun 20, 2024

Problem

Having registered the following autocmd

vim.api.nvim_create_autocmd({ "WinLeave", "WinEnter" }, {
    pattern = "*",
    callback = function(args)
        vim.print('win - ' .. args.buf)
    end
})

vim.api.nvim_create_autocmd({ "BufLeave", "BufEnter" }, {
        pattern = "*",
        callback = function(args)
        vim.print('buf - ' .. args.buf)
        end
})

vim.cmd([[
    nmap gt <cmd>tabnew<cr>
]])

BufLeave and BufEnter work as expected, however WinLeave and WinEnter do not. Win* events point to the same buffer (from the first tab). Buf* events point at the old and then the new one, as expected.

Both events point at the same buffer, the docs are somewhat unclear, as there is a note pertaining to file but nothing is mentioned what goes on when a new buffer is created as a consequence of tabnew.

							
							*WinEnter*
WinEnter			After entering another window.  Not done for
				the first window, when Vim has just started.
				Useful for setting the window height.
				_**If the window is for another buffer, Vim
				executes the BufEnter autocommands after the**_
				WinEnter autocommands.
				**Note: For split and tabpage commands the
				WinEnter event is triggered after the split
				or tab command but before the file is loaded.**

							*WinLeave*
WinLeave			Before leaving a window.  If the window to be
				entered next is for a different buffer, _**Vim
				executes the BufLeave autocommands before the**_
				WinLeave autocommands (but not for ":new").
				Not used for ":qa" or ":q" when exiting Vim.
				Before WinClosed.

Steps to reproduce

See above

Expected behavior

WinEnter should point to the buf in the new tab not the old one. The way BufEnter points to the buffer we enter, in the new tab, and BufLeave points to the buffer we left in the prev tab

Neovim version (nvim -v)

0.10.0

Vim (not Nvim) behaves the same?

yes

Operating system/version

ubuntu 22

Terminal name/version

alacritty

$TERM environment variable

alacritty

Installation

github

@asmodeus812 asmodeus812 added the bug issues reporting wrong behavior label Jun 20, 2024
@glepnir glepnir added the events events, autocommands label Jun 20, 2024
@zeertzjq zeertzjq added documentation and removed bug issues reporting wrong behavior labels Jun 20, 2024
@asmodeus812
Copy link
Author

@zeertzjq is that trully a documentation issue, or something that does not work as indended ? Seems inconsistent, as WinEnter and Leave are triggered, and produce correct results, if you navigate to already existing tab.

@zeertzjq
Copy link
Member

Yes. "before the file is loaded" means before the buffer is created.

@asmodeus812
Copy link
Author

I see, then in case one would like to cover the true WinEnter use case, one has to use TabNew, WinNew and WinEnter ?

@justinmk
Copy link
Member

justinmk commented Jun 20, 2024

Vim (not Nvim) behaves the same?
no

does vim really differ here?

@asmodeus812
Copy link
Author

asmodeus812 commented Jun 21, 2024

@justinmk, edited the issue, vim behaves the same.

@matveyt
Copy link
Contributor

matveyt commented Jun 27, 2024

Internally tabnew is a shortcut for

tab split
enew

The first command triggers winleave/winenter with old buffer active; then the second one does bufleave/bufenter for a new buffer. This is how virtually all "-new" commands work in Vim.

Btw. There is no such thing as "true winenter". If the current window is changed (for whatever reason) then winleave/winenter is triggered. Events are never about commands used, they are about "state changes".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation events events, autocommands
Projects
None yet
Development

No branches or pull requests

5 participants