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

changeset 1085
c2d11f022cf0
parent 1078
aa4c1aa529a5
child 1095
226221dc1d6b
equal deleted inserted replaced
1084:76588dcdb04a 1085:c2d11f022cf0
1 local M = {} 1 local M = {}
2
3 local api = vim.api
4 local fn = vim.fn
2 5
3 function M.calias(abbrev, expand) 6 function M.calias(abbrev, expand)
4 vim.cmd.cnoreabbrev( 7 vim.cmd.cnoreabbrev(
5 string.format( 8 string.format(
6 [[<expr> %s (getcmdtype() == ":" && getcmdline() == "%s") ? "%s" : "%s"]], 9 [[<expr> %s (getcmdtype() == ":" && getcmdline() == "%s") ? "%s" : "%s"]],
13 end 16 end
14 17
15 function M.autocmd(group, event, pattern, opts) 18 function M.autocmd(group, event, pattern, opts)
16 if type(opts) == "function" then 19 if type(opts) == "function" then
17 local func = opts 20 local func = opts
18 opts = { callback = function (_) 21 opts = {
19 func() 22 callback = function(_)
20 end } 23 func()
24 end,
25 }
21 end 26 end
22 27
23 vim.api.nvim_create_autocmd( 28 api.nvim_create_autocmd(
24 event, 29 event,
25 vim.tbl_extend("keep", opts, { 30 vim.tbl_extend("keep", opts, {
26 group = group, 31 group = group,
27 pattern = pattern, 32 pattern = pattern,
28 }) 33 })
29 ) 34 )
30 end 35 end
31 36
37 function M.safe_filter_file(cmd)
38 local errorfile = fn.tempname()
39 vim.cmd([[silent %!]] .. cmd .. [[ 2>]] .. fn.shellescape(errorfile))
40 if vim.v.shell_error ~= 0 then
41 vim.cmd("silent undo")
42 api.nvim_err_write(io.open(errorfile):read("*a"))
43 end
44 fn.delete(errorfile)
45 end
46
32 return M 47 return M

mercurial