From e9afeaf3a7bb645ca73980cd13048c48c292500c Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 13 Oct 2023 07:57:09 +0200 Subject: [PATCH] feat: don't save the session when no files are open (save_empty = false) --- README.md | 1 + lua/persistence/config.lua | 3 ++- lua/persistence/init.lua | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a87d7be..a5b208c 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Persistence comes with the following defaults: dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving pre_save = nil, -- a function to call before saving the session + save_empty = false, -- don't save if there are no open file buffers } ``` diff --git a/lua/persistence/config.lua b/lua/persistence/config.lua index 41eb06e..3bc4b1b 100644 --- a/lua/persistence/config.lua +++ b/lua/persistence/config.lua @@ -4,7 +4,8 @@ local M = {} ---@field pre_save? fun() local defaults = { dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved - options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving + options = { "buffers", "curdir", "tabpages", "winsize", "skiprtp" }, -- sessionoptions used for saving + save_empty = false, -- don't save if there are no open file buffers } ---@type PersistenceOptions diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua index 7d01808..40f02be 100644 --- a/lua/persistence/init.lua +++ b/lua/persistence/init.lua @@ -34,6 +34,18 @@ function M.start() Config.options.pre_save() end + if not Config.options.save_empty then + local bufs = vim.tbl_filter(function(b) + if vim.bo[b].buftype ~= "" then + return false + end + return vim.api.nvim_buf_get_name(b) ~= "" + end, vim.api.nvim_list_bufs()) + if #bufs == 0 then + return + end + end + M.save() end, })