.config/nvim/plugin/auto-prune.lua

changeset 1161
2543467f42da
child 1162
e6da5f7156c2
equal deleted inserted replaced
1160:28e64ed03c25 1161:2543467f42da
1 -- Prune old swap, backup, and undo files at startup
2
3 local fn, fs = vim.fn, vim.fs
4
5 local function prune_files(path, days)
6 local sunset = os.time() - (days * 86400)
7 path = fs.normalize(path)
8
9 if fn.getftype(path) == "" then return end
10
11 for fname, type in fs.dir(path) do
12 local fpath = fs.normalize(path .. "/" .. fname)
13 if type == "file" and fn.getftime(fpath) < sunset then
14 os.remove(fpath)
15 end
16 end
17 end
18
19 vim.api.nvim_create_autocmd("VimEnter", {
20 pattern = "*",
21 group = vim.api.nvim_create_augroup("AutoPrune", { clear = true }),
22 callback = function()
23 if vim.v.vim_did_enter then return end
24 if vim.go.swapfile then prune_files(vim.go.directory, 90) end
25 if vim.go.backup then prune_files(vim.go.backupdir, 90) end
26 if vim.go.undofile then prune_files(vim.go.undodir, 90) end
27 end
28 })

mercurial