.config/nvim/lua/config/util.lua

changeset 1085
c2d11f022cf0
parent 1078
aa4c1aa529a5
child 1095
226221dc1d6b
--- a/.config/nvim/lua/config/util.lua
+++ b/.config/nvim/lua/config/util.lua
@@ -1,5 +1,8 @@
 local M = {}
 
+local api = vim.api
+local fn = vim.fn
+
 function M.calias(abbrev, expand)
   vim.cmd.cnoreabbrev(
     string.format(
@@ -15,12 +18,14 @@ end
 function M.autocmd(group, event, pattern, opts)
   if type(opts) == "function" then
     local func = opts
-    opts = { callback = function (_)
-      func()
-    end }
+    opts = {
+      callback = function(_)
+        func()
+      end,
+    }
   end
 
-  vim.api.nvim_create_autocmd(
+  api.nvim_create_autocmd(
     event,
     vim.tbl_extend("keep", opts, {
       group = group,
@@ -29,4 +34,14 @@ function M.autocmd(group, event, pattern
   )
 end
 
+function M.safe_filter_file(cmd)
+  local errorfile = fn.tempname()
+  vim.cmd([[silent %!]] .. cmd .. [[ 2>]] .. fn.shellescape(errorfile))
+  if vim.v.shell_error ~= 0 then
+    vim.cmd("silent undo")
+    api.nvim_err_write(io.open(errorfile):read("*a"))
+  end
+  fn.delete(errorfile)
+end
+
 return M

mercurial