2021-07-12 10:47:09 +02:00
|
|
|
local Config = require("persistence.config")
|
|
|
|
|
2024-07-06 21:23:56 +02:00
|
|
|
local uv = vim.uv or vim.loop
|
|
|
|
|
2021-07-02 08:55:37 +02:00
|
|
|
local M = {}
|
2023-10-15 21:40:15 +02:00
|
|
|
---@type string?
|
2024-07-06 21:23:56 +02:00
|
|
|
M._current = nil
|
2021-07-02 08:55:37 +02:00
|
|
|
|
|
|
|
local e = vim.fn.fnameescape
|
|
|
|
|
2024-07-06 21:23:56 +02:00
|
|
|
function M.current()
|
|
|
|
local name = vim.fn.getcwd():gsub("[\\/:]", "%%")
|
2021-07-12 10:47:09 +02:00
|
|
|
return Config.options.dir .. name .. ".vim"
|
2021-07-02 08:55:37 +02:00
|
|
|
end
|
|
|
|
|
2021-07-12 10:47:09 +02:00
|
|
|
function M.setup(opts)
|
|
|
|
Config.setup(opts)
|
|
|
|
M.start()
|
|
|
|
end
|
|
|
|
|
2024-07-06 21:23:56 +02:00
|
|
|
function M.fire(event)
|
|
|
|
vim.api.nvim_exec_autocmds("User", {
|
|
|
|
pattern = "Persistence" .. event,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-07-06 21:31:21 +02:00
|
|
|
-- Check if a session is active
|
|
|
|
function M.active()
|
|
|
|
return M._current ~= nil
|
|
|
|
end
|
|
|
|
|
2021-07-02 08:55:37 +02:00
|
|
|
function M.start()
|
2024-07-06 21:23:56 +02:00
|
|
|
M._current = M.current()
|
2023-01-06 19:28:37 +01:00
|
|
|
vim.api.nvim_create_autocmd("VimLeavePre", {
|
|
|
|
group = vim.api.nvim_create_augroup("persistence", { clear = true }),
|
|
|
|
callback = function()
|
2024-07-06 21:23:56 +02:00
|
|
|
M.fire("SavePre")
|
2023-10-13 07:57:09 +02:00
|
|
|
if not Config.options.save_empty then
|
|
|
|
local bufs = vim.tbl_filter(function(b)
|
2024-07-06 21:23:56 +02:00
|
|
|
if vim.bo[b].buftype ~= "" or vim.bo[b].filetype == "gitcommit" or vim.bo[b].filetype == "gitrebase" then
|
2024-01-19 16:14:29 +01:00
|
|
|
return false
|
|
|
|
end
|
2023-10-13 07:57:09 +02:00
|
|
|
return vim.api.nvim_buf_get_name(b) ~= ""
|
|
|
|
end, vim.api.nvim_list_bufs())
|
|
|
|
if #bufs == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-06 19:28:37 +01:00
|
|
|
M.save()
|
2024-07-06 21:23:56 +02:00
|
|
|
M.fire("SavePost")
|
2023-01-06 19:28:37 +01:00
|
|
|
end,
|
|
|
|
})
|
2021-07-02 08:55:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.stop()
|
2024-07-06 21:23:56 +02:00
|
|
|
M._current = nil
|
2023-01-06 19:28:37 +01:00
|
|
|
pcall(vim.api.nvim_del_augroup_by_name, "persistence")
|
2021-07-02 08:55:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.save()
|
2024-07-06 21:22:40 +02:00
|
|
|
vim.cmd("mks! " .. e(M._current or M.current()))
|
2021-07-02 08:55:37 +02:00
|
|
|
end
|
|
|
|
|
2024-07-06 21:23:56 +02:00
|
|
|
---@param opts? { last?: boolean, file?: string }
|
|
|
|
function M.load(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local file = opts.file or opts.last and M.last() or M.current()
|
|
|
|
if file and vim.fn.filereadable(file) ~= 0 then
|
|
|
|
M.fire("LoadPre")
|
|
|
|
vim.cmd("silent! source " .. e(file))
|
|
|
|
M.fire("LoadPost")
|
|
|
|
if M._current then
|
|
|
|
M.start()
|
2024-05-16 20:03:16 +03:00
|
|
|
end
|
2021-07-02 08:55:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-06 21:23:56 +02:00
|
|
|
---@return string[]
|
2021-07-02 08:55:37 +02:00
|
|
|
function M.list()
|
2024-07-06 21:23:56 +02:00
|
|
|
local sessions = vim.fn.glob(Config.options.dir .. "*.vim", true, true)
|
|
|
|
table.sort(sessions, function(a, b)
|
|
|
|
return uv.fs_stat(a).mtime.sec > uv.fs_stat(b).mtime.sec
|
|
|
|
end)
|
|
|
|
return sessions
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.last()
|
|
|
|
return M.list()[1]
|
2021-07-02 08:55:37 +02:00
|
|
|
end
|
|
|
|
|
2024-07-06 21:24:22 +02:00
|
|
|
function M.select()
|
|
|
|
---@type { session: string, dir: string }[]
|
|
|
|
local items = {}
|
|
|
|
for _, session in ipairs(M.list()) do
|
|
|
|
if uv.fs_stat(session) then
|
|
|
|
local dir = session:sub(#Config.options.dir + 1, -5):gsub("%%", "/")
|
|
|
|
items[#items + 1] = { session = session, dir = dir }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
vim.ui.select(items, {
|
|
|
|
prompt = "Select a session: ",
|
|
|
|
format_item = function(item)
|
|
|
|
return vim.fn.fnamemodify(item.dir, ":p:~")
|
|
|
|
end,
|
|
|
|
}, function(item)
|
|
|
|
if item then
|
|
|
|
M.load({ file = item.session })
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-07-02 08:55:37 +02:00
|
|
|
return M
|