Updated layout to handle cursor positions, too
This commit is contained in:
@@ -2,19 +2,26 @@
|
||||
-- inspired by:
|
||||
-- - https://github.com/dedowsdi/.vim/blob/master/autoload/ddd/layout.vim
|
||||
-- - https://vi.stackexchange.com/a/22545/53081
|
||||
--
|
||||
-- added cursor position handling
|
||||
|
||||
local M = {}
|
||||
local layouts = {}
|
||||
local resize_cmds = {}
|
||||
local cursor_positions = {}
|
||||
|
||||
function M.save(name)
|
||||
layouts[name] = vim.fn.winlayout()
|
||||
resize_cmds[name] = vim.fn.winrestcmd()
|
||||
M.add_buf_to_layout(layouts[name])
|
||||
cursor_positions[name] = {}
|
||||
M.add_buf_to_layout(layouts[name], cursor_positions[name])
|
||||
end
|
||||
|
||||
function M.add_buf_to_layout(layout)
|
||||
function M.add_buf_to_layout(layout, cursor_position)
|
||||
if layout[1] == "leaf" then
|
||||
table.insert(layout, vim.fn.winbufnr(layout[2]))
|
||||
local win_id = layout[2]
|
||||
table.insert(layout, vim.fn.winbufnr(win_id))
|
||||
table.insert(cursor_position, vim.api.nvim_win_get_cursor(win_id))
|
||||
else
|
||||
for _, child_layout in ipairs(layout[2]) do
|
||||
M.add_buf_to_layout(child_layout)
|
||||
@@ -31,18 +38,22 @@ function M.restore(name)
|
||||
vim.cmd("new")
|
||||
vim.cmd("wincmd o")
|
||||
|
||||
-- Recursively restore buffers
|
||||
M.apply_layout(layouts[name])
|
||||
-- Recursively restore buffers and cursor positions
|
||||
M.apply_layout(layouts[name], cursor_positions[name], name)
|
||||
|
||||
-- Resize
|
||||
vim.cmd(resize_cmds[name])
|
||||
end
|
||||
|
||||
function M.apply_layout(layout)
|
||||
function M.apply_layout(layout, cursor_position, name)
|
||||
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]))
|
||||
-- Restore cursor position
|
||||
if cursor_position then
|
||||
vim.api.nvim_win_set_cursor(vim.fn.win_getid(), cursor_position[1])
|
||||
end
|
||||
end
|
||||
else
|
||||
-- Split cols or rows, split n-1 times
|
||||
@@ -56,9 +67,10 @@ function M.apply_layout(layout)
|
||||
-- Recursive into child windows
|
||||
for index, win_id in ipairs(wins) do
|
||||
vim.fn.win_gotoid(win_id)
|
||||
M.apply_layout(layout[2][index])
|
||||
M.apply_layout(layout[2][index], name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
|
||||
Reference in New Issue
Block a user