From 9bce2b0d423168b24bc4e855f55a9bd490f6fb79 Mon Sep 17 00:00:00 2001 From: "RingOfStorms (Josh)" Date: Mon, 13 May 2024 10:31:24 -0500 Subject: [PATCH] make my own auto save the auto-save plugin is being weird --- flake.nix | 1 - lua/autocommands.lua | 27 ++++++++++++++++++++++++++- lua/keymaps.lua | 5 +++-- lua/plugins/auto_save.lua | 23 ----------------------- lua/util.lua | 4 ++++ 5 files changed, 33 insertions(+), 27 deletions(-) delete mode 100644 lua/plugins/auto_save.lua diff --git a/flake.nix b/flake.nix index da3766b..cc63278 100644 --- a/flake.nix +++ b/flake.nix @@ -55,7 +55,6 @@ "nvim_plugin-nvim-treesitter/nvim-treesitter" = nvim-treesitter.withAllGrammars; "nvim_plugin-nvim-lua/plenary.nvim" = plenary-nvim; "nvim_plugin-catppuccin/nvim" = catppuccin-nvim; - "nvim_plugin-Pocco81/auto-save.nvim" = auto-save-nvim; "nvim_plugin-MunifTanjim/nui.nvim" = nui-nvim; "nvim_plugin-rcarriga/nvim-notify" = nvim-notify; "nvim_plugin-folke/noice.nvim" = noice-nvim; diff --git a/lua/autocommands.lua b/lua/autocommands.lua index 1b10486..5802124 100644 --- a/lua/autocommands.lua +++ b/lua/autocommands.lua @@ -10,7 +10,6 @@ vim.api.nvim_create_autocmd("TextYankPost", { end, }) - -- TODO is there a better way for these? -- https://www.youtube.com/watch?v=NecszftvMFI vim.filetype.add vim.api.nvim_create_autocmd("BufRead", { @@ -58,3 +57,29 @@ vim.api.nvim_create_autocmd("VimLeavePre", { end end, }) + +local auto_save_disallowed_filetypes = { "TelescopePrompt", "quickfix", "terminal" } +local auto_save_debounce = {} +vim.api.nvim_create_autocmd({ "InsertLeave", "TextChanged", "TextChangedI", "BufLeave" }, { + group = group, + callback = function(event) + local modifiable = vim.api.nvim_buf_get_option(event.buf, "modifiable") + local filetype = vim.api.nvim_buf_get_option(event.buf, "filetype") + local modified = vim.api.nvim_buf_get_option(event.buf, "modified") + if + modifiable + and modified + and U.table_not_contains(auto_save_disallowed_filetypes, filetype) + then + if auto_save_debounce[event.buf] ~= 1 then + auto_save_debounce[event.buf] = 1 + vim.defer_fn(function() + vim.api.nvim_buf_call(event.buf, function() + vim.api.nvim_command("silent! write") + end) + auto_save_debounce[event.buf] = nil + end, 500) + end + end + end, +}) diff --git a/lua/keymaps.lua b/lua/keymaps.lua index 59f77e1..5ec58e2 100644 --- a/lua/keymaps.lua +++ b/lua/keymaps.lua @@ -27,7 +27,7 @@ U.keymaps({ { ";", ":", desc = "No shift to enter command mode with semicolon. Alias ; to :", mode = nvx }, { "Q", "", mode = nvx }, -- don't do normal Q quit { "a", "ggVG", desc = "Select all", mode = nvx }, - { "Q", "SessionSaveqa!", desc = "Quit all", mode = nvx }, + { "Q", "SessionSaveqa", desc = "Quit all", mode = nvx }, { "y", '"+y', desc = "Copy to system clipboard", mode = nvx }, { "p", '"+p', desc = "Paste from system clipboard", mode = nvx }, { "", "nohlsearch", desc = "Clear search on escape" }, @@ -54,7 +54,8 @@ U.keymaps({ { "q", function() - -- Custom close/quit + -- Custom close/quituto + -- -- * if non empty buffer, we will simply open a new empty buffer unless -- it is in the close always list -- * if empty buffer, then we will quit this buffer diff --git a/lua/plugins/auto_save.lua b/lua/plugins/auto_save.lua deleted file mode 100644 index 08aa981..0000000 --- a/lua/plugins/auto_save.lua +++ /dev/null @@ -1,23 +0,0 @@ -return { - "Pocco81/auto-save.nvim", - event = "BufEnter", - opts = { - execution_message = { - message = function() - return "" - end, - }, - trigger_events = { "InsertLeave", "TextChanged", "TextChangedI", "BufLeave" }, - condition = function(buf) - local disallowed_filetypes = { "TelescopePrompt", "quickfix", "terminal" } - local utils = require("auto-save.utils.data") - if - vim.fn.getbufvar(buf, "&modifiable") == 1 - and utils.not_in(vim.fn.getbufvar(buf, "&filetype"), disallowed_filetypes) - then - return true - end - return false - end, - }, -} diff --git a/lua/util.lua b/lua/util.lua index afe5eff..a399d2e 100644 --- a/lua/util.lua +++ b/lua/util.lua @@ -106,6 +106,10 @@ function M.table_contains(table, element) return false end +function M.table_not_contains(table, element) + return not M.table_contains(table, element) +end + -- From https://github.com/lukas-reineke/onedark.nvim/blob/master/lua/onedark.lua function M.highlight(group, options) local guifg = options.fg or "NONE"