Compare commits

...

2 Commits

2 changed files with 20 additions and 7 deletions

View File

@@ -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" })
------------------------------------------ ------------------------------------------
-- --

View File

@@ -2,6 +2,9 @@
-- 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 = {}
@@ -14,7 +17,9 @@ end
function M.add_buf_to_layout(layout) function M.add_buf_to_layout(layout)
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(layout, 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 +36,20 @@ 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], 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, 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
vim.api.nvim_win_set_cursor(vim.fn.win_getid(), layout[4])
end end
else else
-- Split cols or rows, split n-1 times -- Split cols or rows, split n-1 times
@@ -56,7 +63,7 @@ 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