Updated layout to handle cursor positions, too
This commit is contained in:
@@ -71,8 +71,14 @@ vim.keymap.set('n', '<leader>w', workspaces_fzf_picker, { desc = "Workspaces" })
|
|||||||
------------------------------------------
|
------------------------------------------
|
||||||
|
|
||||||
local layout = require('layout')
|
local layout = require('layout')
|
||||||
vim.keymap.set('n', '<leader>ss', function() layout.save('default') end, { desc = "Save window layout" })
|
vim.keymap.set('n', '<leader>ss', function()
|
||||||
vim.keymap.set('n', '<leader>sr', function() layout.restore('default') end, { desc = "Restore window layout" })
|
layout.save('default')
|
||||||
|
print("Window layout saved")
|
||||||
|
end, { desc = "Save window layout" })
|
||||||
|
vim.keymap.set('n', '<leader>sr', function()
|
||||||
|
layout.restore('default')
|
||||||
|
print("Window layout restored")
|
||||||
|
end, { desc = "Restore window layout" })
|
||||||
|
|
||||||
------------------------------------------
|
------------------------------------------
|
||||||
--
|
--
|
||||||
|
|||||||
@@ -2,19 +2,26 @@
|
|||||||
-- inspired by:
|
-- inspired by:
|
||||||
-- - https://github.com/dedowsdi/.vim/blob/master/autoload/ddd/layout.vim
|
-- - https://github.com/dedowsdi/.vim/blob/master/autoload/ddd/layout.vim
|
||||||
-- - https://vi.stackexchange.com/a/22545/53081
|
-- - https://vi.stackexchange.com/a/22545/53081
|
||||||
|
--
|
||||||
|
-- added cursor position handling
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
local layouts = {}
|
local layouts = {}
|
||||||
local resize_cmds = {}
|
local resize_cmds = {}
|
||||||
|
local cursor_positions = {}
|
||||||
|
|
||||||
function M.save(name)
|
function M.save(name)
|
||||||
layouts[name] = vim.fn.winlayout()
|
layouts[name] = vim.fn.winlayout()
|
||||||
resize_cmds[name] = vim.fn.winrestcmd()
|
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
|
end
|
||||||
|
|
||||||
function M.add_buf_to_layout(layout)
|
function M.add_buf_to_layout(layout, cursor_position)
|
||||||
if layout[1] == "leaf" then
|
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
|
else
|
||||||
for _, child_layout in ipairs(layout[2]) do
|
for _, child_layout in ipairs(layout[2]) do
|
||||||
M.add_buf_to_layout(child_layout)
|
M.add_buf_to_layout(child_layout)
|
||||||
@@ -31,18 +38,22 @@ function M.restore(name)
|
|||||||
vim.cmd("new")
|
vim.cmd("new")
|
||||||
vim.cmd("wincmd o")
|
vim.cmd("wincmd o")
|
||||||
|
|
||||||
-- Recursively restore buffers
|
-- Recursively restore buffers and cursor positions
|
||||||
M.apply_layout(layouts[name])
|
M.apply_layout(layouts[name], cursor_positions[name], name)
|
||||||
|
|
||||||
-- Resize
|
-- Resize
|
||||||
vim.cmd(resize_cmds[name])
|
vim.cmd(resize_cmds[name])
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.apply_layout(layout)
|
function M.apply_layout(layout, cursor_position, name)
|
||||||
if layout[1] == "leaf" then
|
if layout[1] == "leaf" then
|
||||||
-- Load buffer for leaf
|
-- Load buffer for leaf
|
||||||
if vim.fn.bufexists(layout[3]) == 1 then
|
if vim.fn.bufexists(layout[3]) == 1 then
|
||||||
vim.cmd(string.format("b %d", layout[3]))
|
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
|
end
|
||||||
else
|
else
|
||||||
-- Split cols or rows, split n-1 times
|
-- Split cols or rows, split n-1 times
|
||||||
@@ -56,9 +67,10 @@ function M.apply_layout(layout)
|
|||||||
-- Recursive into child windows
|
-- Recursive into child windows
|
||||||
for index, win_id in ipairs(wins) do
|
for index, win_id in ipairs(wins) do
|
||||||
vim.fn.win_gotoid(win_id)
|
vim.fn.win_gotoid(win_id)
|
||||||
M.apply_layout(layout[2][index])
|
M.apply_layout(layout[2][index], name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user