65 lines
1.3 KiB
Lua
Raw Normal View History

2021-07-12 10:47:09 +02:00
local Config = require("persistence.config")
2021-07-02 08:55:37 +02:00
local M = {}
local e = vim.fn.fnameescape
function M.get_current()
local pattern = "/"
if vim.fn.has("win32") == 1 then
pattern = "[\\:]"
end
local name = vim.fn.getcwd():gsub(pattern, "%%")
2021-07-12 10:47:09 +02:00
return Config.options.dir .. name .. ".vim"
2021-07-02 08:55:37 +02:00
end
function M.get_last()
local sessions = M.list()
table.sort(sessions, function(a, b)
return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec
end)
return sessions[1]
end
2021-07-12 10:47:09 +02:00
function M.setup(opts)
Config.setup(opts)
M.start()
end
2021-07-02 08:55:37 +02:00
function M.start()
vim.cmd([[
augroup Persistence
2021-07-02 08:55:37 +02:00
autocmd!
autocmd VimLeavePre * lua require("persistence").save()
2021-07-02 08:55:37 +02:00
augroup end
]])
end
function M.stop()
vim.cmd([[
autocmd! Persistence
augroup! Persistence
2021-07-02 08:55:37 +02:00
]])
end
function M.save()
2021-07-12 10:47:09 +02:00
local tmp = vim.o.sessionoptions
vim.o.sessionoptions = table.concat(Config.options.options, ",")
2021-07-02 08:55:37 +02:00
vim.cmd("mks! " .. e(M.get_current()))
2021-07-12 10:47:09 +02:00
vim.o.sessionoptions = tmp
2021-07-02 08:55:37 +02:00
end
function M.load(opt)
opt = opt or {}
local sfile = opt.last and M.get_last() or M.get_current()
if sfile and vim.fn.filereadable(sfile) ~= 0 then
vim.cmd("silent! source " .. e(sfile))
2021-07-02 08:55:37 +02:00
end
end
function M.list()
2021-07-12 10:47:09 +02:00
return vim.fn.glob(Config.options.dir .. "*.vim", true, true)
2021-07-02 08:55:37 +02:00
end
return M