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

Thu, 16 May 2024 14:37:26 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Thu, 16 May 2024 14:37:26 -0500
changeset 1166
34bf03fa07e4
parent 1162
e6da5f7156c2
permissions
-rw-r--r--

normalize config.util.autocmd

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
1162
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
19 local function auto_prune()
1161
2543467f42da Make auto-prune a standalone file plugin too
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20 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
21 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
22 if vim.go.undofile then prune_files(vim.go.undodir, 90) end
1162
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
23 end
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
24
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
25 if vim.v.vim_did_enter then
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
26 auto_prune()
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
27 else
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
28 vim.api.nvim_create_autocmd("VimEnter", {
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
29 pattern = "*",
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
30 group = vim.api.nvim_create_augroup("AutoPrune", { clear = true }),
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
31 callback = auto_prune, })
e6da5f7156c2 Fix VimEnter/vim_did_enter behavior
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
32 end

mercurial