diff --git a/README.md b/README.md index 9c552a9..d5e73a3 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,8 @@ The plugin fires events at various points during its lifecycle which users can h - `AutoSaveWritePre` Just before a buffer is getting saved - `AutoSaveWritePost` Just after a buffer is getting saved +- `AutoSaveEnable` Just after enabling the plugin +- `AutoSaveDisable` Just after disabling the plugin It will always supply the current buffer in the `data.saved_buffer` diff --git a/lua/auto-save/init.lua b/lua/auto-save/init.lua index 87d49a1..ab87749 100644 --- a/lua/auto-save/init.lua +++ b/lua/auto-save/init.lua @@ -79,7 +79,7 @@ local function save(buf) return end - autocmds.exec_autocmd("AutoSaveWritePre", buf) + autocmds.exec_autocmd("AutoSaveWritePre", { saved_buffer = buf }) local noautocmd = cnf.opts.noautocmd and "noautocmd " or "" if cnf.opts.write_all_buffers then @@ -90,7 +90,7 @@ local function save(buf) end) end - autocmds.exec_autocmd("AutoSaveWritePost", buf) + autocmds.exec_autocmd("AutoSaveWritePost", { saved_buffer = buf }) logger.log(buf, "Saved buffer") if cnf.opts.execution_message.enabled == true then @@ -163,12 +163,16 @@ function M.on() }) autosave_running = true + + autocmds.exec_autocmd("AutoSaveEnable") end function M.off() autocmds.create_augroup({ clear = true }) autosave_running = false + + autocmds.exec_autocmd("AutoSaveDisable") end function M.toggle() diff --git a/lua/auto-save/utils/autocommands.lua b/lua/auto-save/utils/autocommands.lua index 1d0adf9..7d9518a 100644 --- a/lua/auto-save/utils/autocommands.lua +++ b/lua/auto-save/utils/autocommands.lua @@ -20,9 +20,13 @@ M.create_augroup = function(opts) end --- @param pattern string ---- @param saved_buffer number -M.exec_autocmd = function(pattern, saved_buffer) - api.nvim_exec_autocmds("User", { pattern = pattern, data = { saved_buffer = saved_buffer } }) +--- @param data? table +M.exec_autocmd = function(pattern, data) + local opts = { pattern = pattern } + if data ~= nil then + opts.data = data + end + api.nvim_exec_autocmds("User", opts) end --- @param trigger_events TriggerEvent[]?