From 29558a80c4f330cb318bf1d0c68569230b22bc34 Mon Sep 17 00:00:00 2001 From: kdav5758 Date: Thu, 1 Jul 2021 21:42:52 -0500 Subject: [PATCH] added hooks for saving and fixed issue with get_modified() --- lua/autosave/modules/autocmds.lua | 56 ++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/lua/autosave/modules/autocmds.lua b/lua/autosave/modules/autocmds.lua index 9432ed1..71323b9 100644 --- a/lua/autosave/modules/autocmds.lua +++ b/lua/autosave/modules/autocmds.lua @@ -1,9 +1,10 @@ -local opts = require("autosave.config").options - local api = vim.api local fn = vim.fn local cmd = vim.cmd +local opts = require("autosave.config").options +local autosave = require("autosave") + local M = {} local function table_has_value(tbl, value) @@ -42,19 +43,22 @@ local function assert_user_conditions() if (condition == "exists") then if (value == true) then if (fn.filereadable(fn.expand("%:p")) == 0) then - sc_exists = false; break + sc_exists = false + break end end elseif (condition == "modifiable") then if (value == true) then if (api.nvim_eval([[&modifiable]]) == 0) then - sc_modifiable = false; break + sc_modifiable = false + break end end elseif (condition == "filetype_is_not") then if not (next(opts["conditions"]["filetype_is_not"]) == nil) then if (table_has_value(opts["conditions"]["filetype_is_not"], api.nvim_eval([[&filetype]])) == true) then - sc_filetype = false; break + sc_filetype = false + break end end end @@ -64,36 +68,50 @@ local function assert_user_conditions() end local function assert_return(values, expected) - for key, value in pairs(values) do - if (value ~= expected) then return false end - end + for key, value in pairs(values) do + if (value ~= expected) then + return false + end + end - return true + return true end function M.do_save() - if (assert_return(assert_user_conditions(), true)) then - actual_save() - end + if (assert_return(assert_user_conditions(), true)) then + actual_save() + end end function M.save() + + if (autosave.hook_before_saving ~= nil) then + autosave.hook_before_saving() + end + if (opts["write_all_buffers"] == true) then cmd([[call g:AutoSaveBufDo("lua require'autosave.modules.autocmds'.do_save()")]]) else M.do_save() end - if (opts["execution_message"] ~= "" and get_modified() == true) then - print(opts["execution_message"]) + if (get_modified() == true) then set_modified(false) + if (opts["execution_message"] ~= "") then + print(opts["execution_message"]) + end + + if (opts["clean_command_line_interval"] > 0) then + cmd( + [[call timer_start(]] .. + opts["clean_command_line_interval"] .. [[, funcref('g:AutoSaveClearCommandLine'))]] + ) + end end - if (opts["clean_command_line_interval"] > 0) then - cmd( - [[call timer_start(]] .. opts["clean_command_line_interval"] .. [[, funcref('g:AutoSaveClearCommandLine'))]] - ) - end + if (autosave.hook_after_saving ~= nil) then + autosave.hook_after_saving() + end end local function parse_events()