From 8b32094309ee986066c219d2b4d88a4045fbcb8c Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 2 Jul 2021 08:55:37 +0200 Subject: [PATCH] feat: inital version --- lua/persistence/init.lua | 54 ++++++++++++++++++++++++++++++++++++++++ selene.toml | 1 + stylua.toml | 3 +++ vim.toml | 2 ++ 4 files changed, 60 insertions(+) create mode 100644 lua/persistence/init.lua create mode 100644 selene.toml create mode 100644 stylua.toml create mode 100644 vim.toml diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua new file mode 100644 index 0000000..777fe20 --- /dev/null +++ b/lua/persistence/init.lua @@ -0,0 +1,54 @@ +local M = {} + +local e = vim.fn.fnameescape + +local sessions_dir = vim.fn.expand(vim.fn.stdpath("config") .. "/sessions/") +vim.fn.mkdir(sessions_dir, "p") + +function M.get_current() + local name = vim.fn.getcwd():gsub("/", "%%") + return sessions_dir .. name .. ".vim" +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 + +function M.start() + vim.cmd([[ + augroup Session + autocmd! + autocmd VimLeavePre * lua require("session").save() + "autocmd BufEnter * lua require("session").save() + augroup end + ]]) +end + +function M.stop() + vim.cmd([[ + autocmd! Session + augroup! Session + ]]) +end + +function M.save() + vim.cmd("mks! " .. e(M.get_current())) +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("source " .. e(sfile)) + end +end + +function M.list() + return vim.fn.glob(sessions_dir .. "*.vim", true, true) +end + +return M diff --git a/selene.toml b/selene.toml new file mode 100644 index 0000000..6540d6f --- /dev/null +++ b/selene.toml @@ -0,0 +1 @@ +std="lua51+vim" diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..5d6c50d --- /dev/null +++ b/stylua.toml @@ -0,0 +1,3 @@ +indent_type = "Spaces" +indent_width = 2 +column_width = 120 \ No newline at end of file diff --git a/vim.toml b/vim.toml new file mode 100644 index 0000000..0fa5c4f --- /dev/null +++ b/vim.toml @@ -0,0 +1,2 @@ +[vim] +any = true