diff --git a/lua/snacks/win.lua b/lua/snacks/win.lua index 25c6ee71..309c7a0a 100644 --- a/lua/snacks/win.lua +++ b/lua/snacks/win.lua @@ -83,6 +83,7 @@ M.meta = { ---@field text? string|string[]|fun():(string[]|string) Initial lines to set in the buffer ---@field actions? table Actions that can be used in key mappings ---@field resize? boolean Automatically resize the window when the editor is resized +---@field stack? boolean When enabled, multiple split windows with the same position will be stacked together (useful for terminals) local defaults = { show = true, fixbuf = true, @@ -701,15 +702,18 @@ function M:open_win() elseif position == "current" then self.win = vim.api.nvim_get_current_win() vim.api.nvim_win_set_buf(self.win, self.buf) - else + else --split local parent = self.opts.win and vim.api.nvim_win_is_valid(self.opts.win) and self.opts.win or 0 local vertical = position == "left" or position == "right" - if parent == 0 then + -- When stacking is enabled, find an existing window with the same relative/position + -- and stack the new window perpendicular to it instead of creating a new split + if parent == 0 and self.opts.stack then for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do if vim.w[win].snacks_win and vim.w[win].snacks_win.relative == relative and vim.w[win].snacks_win.position == position + and vim.w[win].snacks_win.stack == true then parent = win relative = "win" @@ -737,6 +741,7 @@ function M:open_win() id = self.id, position = self.opts.position, relative = self.opts.relative, + stack = self.opts.stack, } end