# HG changeset patch # User Meredith Howard # Date 1714936642 18000 # Node ID 2543467f42da682f837c8d3353ab83d51d0b13ab # Parent 28e64ed03c2581611808e32f2f875a6045299d0d Make auto-prune a standalone file plugin too diff --git a/.config/nvim/lua/config/autocmds.lua b/.config/nvim/lua/config/autocmds.lua --- a/.config/nvim/lua/config/autocmds.lua +++ b/.config/nvim/lua/config/autocmds.lua @@ -50,12 +50,3 @@ end) autocmd(g, "BufReadPost", "*", function() fn.matchadd("Error", [[\m^\([<>|]\)\{7} \@=\|^=\{7}$]]) end) - --- >> Prune old backup and undo files at startup -autocmd(g, "User", "VeryLazy", function() - local prune_files = require("config.util").prune_files - if vim.go.swapfile then prune_files(vim.go.directory, 90) end - if vim.go.backup then prune_files(vim.go.backupdir, 90) end - if vim.go.undofile then prune_files(vim.go.undodir, 90) end -end) - diff --git a/.config/nvim/lua/config/util.lua b/.config/nvim/lua/config/util.lua --- a/.config/nvim/lua/config/util.lua +++ b/.config/nvim/lua/config/util.lua @@ -49,18 +49,4 @@ function M.last_modified_days(fname) return (os.time() - vim.fn.getftime(fname)) / 86400 end -function M.prune_files(path, days) - local sunset = os.time() - (days * 86400) - path = vim.fs.normalize(path) - - if fn.getftype(path) == "" then return end - - for fname, type in vim.fs.dir(path) do - local fpath = vim.fs.normalize(path .. "/" .. fname) - if type == "file" and fn.getftime(fpath) < sunset then - os.remove(fpath) - end - end -end - return M diff --git a/.config/nvim/plugin/auto-prune.lua b/.config/nvim/plugin/auto-prune.lua new file mode 100644 --- /dev/null +++ b/.config/nvim/plugin/auto-prune.lua @@ -0,0 +1,28 @@ +-- Prune old swap, backup, and undo files at startup + +local fn, fs = vim.fn, vim.fs + +local function prune_files(path, days) + local sunset = os.time() - (days * 86400) + path = fs.normalize(path) + + if fn.getftype(path) == "" then return end + + for fname, type in fs.dir(path) do + local fpath = fs.normalize(path .. "/" .. fname) + if type == "file" and fn.getftime(fpath) < sunset then + os.remove(fpath) + end + end +end + +vim.api.nvim_create_autocmd("VimEnter", { + pattern = "*", + group = vim.api.nvim_create_augroup("AutoPrune", { clear = true }), + callback = function() + if vim.v.vim_did_enter then return end + if vim.go.swapfile then prune_files(vim.go.directory, 90) end + if vim.go.backup then prune_files(vim.go.backupdir, 90) end + if vim.go.undofile then prune_files(vim.go.undodir, 90) end + end +})