Compare commits
2 Commits
3e6c115673
...
3b7c034926
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b7c034926 | |||
| 12152a20db |
@@ -71,8 +71,14 @@ vim.keymap.set('n', '<leader>w', workspaces_fzf_picker, { desc = "Workspaces" })
|
||||
------------------------------------------
|
||||
|
||||
local layout = require('layout')
|
||||
vim.keymap.set('n', '<leader>ss', function() layout.save('default') end, { desc = "Save window layout" })
|
||||
vim.keymap.set('n', '<leader>sr', function() layout.restore('default') end, { desc = "Restore window layout" })
|
||||
vim.keymap.set('n', '<leader>ss', function()
|
||||
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,6 +2,9 @@
|
||||
-- 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 = {}
|
||||
@@ -14,7 +17,9 @@ end
|
||||
|
||||
function M.add_buf_to_layout(layout)
|
||||
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
|
||||
for _, child_layout in ipairs(layout[2]) do
|
||||
M.add_buf_to_layout(child_layout)
|
||||
@@ -31,18 +36,20 @@ 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], name)
|
||||
|
||||
-- Resize
|
||||
vim.cmd(resize_cmds[name])
|
||||
end
|
||||
|
||||
function M.apply_layout(layout)
|
||||
function M.apply_layout(layout, 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
|
||||
vim.api.nvim_win_set_cursor(vim.fn.win_getid(), layout[4])
|
||||
end
|
||||
else
|
||||
-- Split cols or rows, split n-1 times
|
||||
@@ -56,7 +63,7 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user