65 lines
1.5 KiB
Lua
65 lines
1.5 KiB
Lua
-- save and restore current window layout
|
|
-- inspired by:
|
|
-- - https://github.com/dedowsdi/.vim/blob/master/autoload/ddd/layout.vim
|
|
-- - https://vi.stackexchange.com/a/22545/53081
|
|
local M = {}
|
|
local layouts = {}
|
|
local resize_cmds = {}
|
|
|
|
function M.save(name)
|
|
layouts[name] = vim.fn.winlayout()
|
|
resize_cmds[name] = vim.fn.winrestcmd()
|
|
M.add_buf_to_layout(layouts[name])
|
|
end
|
|
|
|
function M.add_buf_to_layout(layout)
|
|
if layout[1] == "leaf" then
|
|
table.insert(layout, vim.fn.winbufnr(layout[2]))
|
|
else
|
|
for _, child_layout in ipairs(layout[2]) do
|
|
M.add_buf_to_layout(child_layout)
|
|
end
|
|
end
|
|
end
|
|
|
|
function M.restore(name)
|
|
if not layouts[name] then
|
|
return
|
|
end
|
|
|
|
-- Create clean window
|
|
vim.cmd("new")
|
|
vim.cmd("wincmd o")
|
|
|
|
-- Recursively restore buffers
|
|
M.apply_layout(layouts[name])
|
|
|
|
-- Resize
|
|
vim.cmd(resize_cmds[name])
|
|
end
|
|
|
|
function M.apply_layout(layout)
|
|
if layout[1] == "leaf" then
|
|
-- Load buffer for leaf
|
|
if vim.fn.bufexists(layout[3]) == 1 then
|
|
vim.cmd(string.format("b %d", layout[3]))
|
|
end
|
|
else
|
|
-- Split cols or rows, split n-1 times
|
|
local split_method = layout[1] == "col" and "rightbelow split" or "rightbelow vsplit"
|
|
local wins = { vim.fn.win_getid() }
|
|
for i = 2, #layout[2] do
|
|
vim.cmd(split_method)
|
|
table.insert(wins, vim.fn.win_getid())
|
|
end
|
|
|
|
-- Recursive into child windows
|
|
for index, win_id in ipairs(wins) do
|
|
vim.fn.win_gotoid(win_id)
|
|
M.apply_layout(layout[2][index])
|
|
end
|
|
end
|
|
end
|
|
|
|
return M
|