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

Can this plugin maintain window sizes when opening new windows?

Open nullromo opened this issue 1 year ago • 0 comments

Hi, great plugin. It works well when I resize Vim itself, but I'm having an issue when opening/closing windows.

Background

Vim has this annoying default behavior where when you open a new window or close an existing window, the rest of the windows resize themselves (as if you pressed <C-w>=). This is a default behavior of Vim, but I would like to see if this plugin can fix it.

Close an existing window

You can see in this video that when I make a bunch of splits and resize them, then close one window, they all resize.

https://github.com/kwkarlwang/bufresize.nvim/assets/8991581/fb73f646-08e7-4d12-9c06-8ba0ab5ffee9

Open a new window

You can see in this video that when I make some splits and resize them, then open a new window, the existing windows resize.

https://github.com/kwkarlwang/bufresize.nvim/assets/8991581/53ae0850-3756-4164-95fa-41ec044ea490

Fix for closing windows

I was able to fix this behavior using this plugin for closing windows only, but I want to fix it for opening windows as well. Here is my code that fixes the behavior while closing windows:

vim.api.nvim_create_autocmd(
    { 'WinClosed' },
    {
        callback = function()
            require('bufresize').register()
            require('bufresize').block_register()
        end
    }
)
vim.api.nvim_create_autocmd(
    { 'WinEnter' },
    {
        callback = function()
            require('bufresize').resize_close()
        end
    }
)

Since WinClosed is fired right before the window is removed, I can save the window layout with register() right before it closes. Then when I hit WinEnter after the window is closed, I can restore the saved layout with resize_close().

Here is a video of this fix working. You can see in the video that when I make a bunch of splits, resize them, and then close one, the other windows maintain their sizes. So this plugin is working here :)

https://github.com/kwkarlwang/bufresize.nvim/assets/8991581/810778c1-20e3-4b0b-9d89-90e4b93f7d33

Fix for opening windows?

I can't get this to work. I tried the same method using WinNew instead of WinClosed. I used a variable to keep track of whether the WinEnter event was triggered after a close vs. an open.

local lastAction = ''
vim.api.nvim_create_autocmd(
    { 'WinClosed' },
    {
        pattern = '*',
        callback = function()
            lastAction = 'close'
            require('bufresize').register()
            require('bufresize').block_register()
        end
    }
)
vim.api.nvim_create_autocmd(
    { 'WinNew' },
    {
        pattern = '*',
        callback = function()
            lastAction = 'open'
            require('bufresize').register()
            require('bufresize').block_register()
        end
    }
)
vim.api.nvim_create_autocmd(
    { 'WinEnter' },
    {
        pattern = { '*' },
        callback = function()
            if lastAction == 'close' then
                vim.notify('doing a close')
                require('bufresize').resize_close()
            elseif lastAction == 'open' then
                vim.notify('doing an open')
                require('bufresize').resize_open()
            end
            lastAction = ''
        end
    }
)

As you can see, if the last action is 'close', then I call resize_close(), which works. When the last action is 'open', then I call resize_open(), but it doesn't seem to work the same way.

Is there a configuration that will fix this behavior in the way that I want? Thanks very much in advance.

nullromo avatar Jan 13 '24 00:01 nullromo