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

Sun, 05 May 2024 14:17:22 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Sun, 05 May 2024 14:17:22 -0500
changeset 1161
2543467f42da
child 1162
e6da5f7156c2
permissions
-rw-r--r--

Make auto-prune a standalone file plugin too

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

mercurial