Convert backup/undofile pruning to lua

Sun, 05 May 2024 13:24:32 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Sun, 05 May 2024 13:24:32 -0500
changeset 1157
a237720efee9
parent 1156
0a7162df160d
child 1158
0e7310186a57

Convert backup/undofile pruning to lua

.config/nvim/autoload/vimrc.vim file | annotate | diff | comparison | revisions
.config/nvim/lua/config/autocmds.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/commands.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/util.lua file | annotate | diff | comparison | revisions
bin/privim file | annotate | diff | comparison | revisions
--- a/.config/nvim/autoload/vimrc.vim
+++ b/.config/nvim/autoload/vimrc.vim
@@ -19,26 +19,3 @@ func! vimrc#SafeFilterFile(cmd)
     call delete(errors)
   endtry
 endfunc
-
-if has('ruby')
-  func! s:PruneFiles(path, days) abort
-    ruby <<END_RUBY
-      require 'pathname'
-
-      (path, days) = VIM.evaluate('[a:path, a:days]')
-      sunset       = Time.now - (days * 86400)
-
-      Pathname(path).realpath.each_child do |file|
-        file.delete if file.mtime < sunset
-      end
-END_RUBY
-  endfunc
-else
-  func! s:PruneFiles(path, days) abort
-  endfunc
-endif
-
-func! vimrc#PruneFiles(path, days) abort
-  call s:PruneFiles(a:path, a:days)
-endfunc
-
--- a/.config/nvim/lua/config/autocmds.lua
+++ b/.config/nvim/lua/config/autocmds.lua
@@ -50,3 +50,11 @@ 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.backup then prune_files(vim.go.backupdir, 90) end
+  if vim.go.undofile then prune_files(vim.go.undodir, 90) end
+end)
+
--- a/.config/nvim/lua/config/commands.lua
+++ b/.config/nvim/lua/config/commands.lua
@@ -54,16 +54,16 @@ command("PruneSession", function()
   for _, bufnr in ipairs(bufs) do
     local name = vim.api.nvim_buf_get_name(bufnr)
     if name then
-      local type = vim.fn.getftype(name)
+      local type = fn.getftype(name)
       if type == "" or type == "dir"
         or util.last_modified_days(name) > 30
       then
         vim.print("pruned: " .. name)
-        vim.cmd.bwipeout(bufnr)
+        cmd.bwipeout(bufnr)
       end
     end
   end
   if not vim.api.nvim_buf_get_name(0) then
-    vim.cmd.bprev()
+    cmd.bprev()
   end
 end, {})
--- a/.config/nvim/lua/config/util.lua
+++ b/.config/nvim/lua/config/util.lua
@@ -49,4 +49,18 @@ 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
--- a/bin/privim
+++ b/bin/privim
@@ -1,2 +1,2 @@
 #!/bin/sh
-exec nvim -c 'set noswapfile noundofile nobackup viminfo=' $*
+exec nvim -c 'set noswapfile noundofile nobackup shada=' $*

mercurial