feat: added config options

This commit is contained in:
Folke Lemaitre 2021-07-12 10:47:09 +02:00
parent 38203a17a9
commit a39f3f10c8
No known key found for this signature in database
GPG Key ID: 707FE6FEB82F7984
2 changed files with 29 additions and 6 deletions

View File

@ -0,0 +1,17 @@
local M = {}
---@class PersistenceOptions
local defaults = {
dir = vim.fn.expand(vim.fn.stdpath("config") .. "/sessions/"), -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
}
---@type PersistenceOptions
M.options = {}
function M.setup(opts)
M.options = vim.tbl_deep_extend("force", {}, defaults, opts or {})
vim.fn.mkdir(M.options.dir, "p")
end
return M

View File

@ -1,13 +1,12 @@
local Config = require("persistence.config")
local M = {} local M = {}
local e = vim.fn.fnameescape 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() function M.get_current()
local name = vim.fn.getcwd():gsub("/", "%%") local name = vim.fn.getcwd():gsub("/", "%%")
return sessions_dir .. name .. ".vim" return Config.options.dir .. name .. ".vim"
end end
function M.get_last() function M.get_last()
@ -18,12 +17,16 @@ function M.get_last()
return sessions[1] return sessions[1]
end end
function M.setup(opts)
Config.setup(opts)
M.start()
end
function M.start() function M.start()
vim.cmd([[ vim.cmd([[
augroup Persistence augroup Persistence
autocmd! autocmd!
autocmd VimLeavePre * lua require("persistence").save() autocmd VimLeavePre * lua require("persistence").save()
"autocmd BufEnter * lua require("persistence").save()
augroup end augroup end
]]) ]])
end end
@ -36,7 +39,10 @@ function M.stop()
end end
function M.save() function M.save()
local tmp = vim.o.sessionoptions
vim.o.sessionoptions = table.concat(Config.options.options, ",")
vim.cmd("mks! " .. e(M.get_current())) vim.cmd("mks! " .. e(M.get_current()))
vim.o.sessionoptions = tmp
end end
function M.load(opt) function M.load(opt)
@ -48,7 +54,7 @@ function M.load(opt)
end end
function M.list() function M.list()
return vim.fn.glob(sessions_dir .. "*.vim", true, true) return vim.fn.glob(Config.options.dir .. "*.vim", true, true)
end end
return M return M