From f58a838282dac1ed33165a5fd03829b036584df2 Mon Sep 17 00:00:00 2001
From: Folke Lemaitre <folke.lemaitre@gmail.com>
Date: Sat, 6 Jul 2024 21:23:56 +0200
Subject: [PATCH] feat!: removed load_pre/load_post/save_pre/save_post in favor
 of events. Check the readme

---
 lua/persistence/init.lua | 81 ++++++++++++++++++----------------------
 1 file changed, 36 insertions(+), 45 deletions(-)

diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua
index 3d2dd9b..ae75c35 100644
--- a/lua/persistence/init.lua
+++ b/lua/persistence/init.lua
@@ -1,51 +1,38 @@
 local Config = require("persistence.config")
 
+local uv = vim.uv or vim.loop
+
 local M = {}
 ---@type string?
-M.current = nil
+M._current = nil
 
 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, "%%")
+function M.current()
+  local name = vim.fn.getcwd():gsub("[\\/:]", "%%")
   return Config.options.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.setup(opts)
   Config.setup(opts)
   M.start()
 end
 
+function M.fire(event)
+  vim.api.nvim_exec_autocmds("User", {
+    pattern = "Persistence" .. event,
+  })
+end
+
 function M.start()
-  M.current = M.get_current()
+  M._current = M.current()
   vim.api.nvim_create_autocmd("VimLeavePre", {
     group = vim.api.nvim_create_augroup("persistence", { clear = true }),
     callback = function()
-      if Config.options.pre_save then
-        Config.options.pre_save()
-      end
-
+      M.fire("SavePre")
       if not Config.options.save_empty then
         local bufs = vim.tbl_filter(function(b)
-          if vim.bo[b].buftype ~= "" then
-            return false
-          end
-          if vim.bo[b].filetype == "gitcommit" then
-            return false
-          end
-          if vim.bo[b].filetype == "gitrebase" then
+          if vim.bo[b].buftype ~= "" or vim.bo[b].filetype == "gitcommit" or vim.bo[b].filetype == "gitrebase" then
             return false
           end
           return vim.api.nvim_buf_get_name(b) ~= ""
@@ -56,16 +43,13 @@ function M.start()
       end
 
       M.save()
-
-      if type(Config.options.post_save) == "function" then
-        Config.options.post_save()
-      end
+      M.fire("SavePost")
     end,
   })
 end
 
 function M.stop()
-  M.current = nil
+  M._current = nil
   pcall(vim.api.nvim_del_augroup_by_name, "persistence")
 end
 
@@ -73,24 +57,31 @@ function M.save()
   vim.cmd("mks! " .. e(M._current or M.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
-    if type(Config.options.pre_load) == "function" then
-      Config.options.pre_load()
-    end
-
-    vim.cmd("silent! source " .. e(sfile))
-
-    if type(Config.options.post_load) == "function" then
-      Config.options.post_load()
+---@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()
     end
   end
 end
 
+---@return string[]
 function M.list()
-  return vim.fn.glob(Config.options.dir .. "*.vim", true, true)
+  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]
 end
 
 return M