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

Sun, 05 May 2024 13:39:31 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Sun, 05 May 2024 13:39:31 -0500
changeset 1158
0e7310186a57
parent 1157
a237720efee9
child 1161
2543467f42da
permissions
-rw-r--r--

luafy AutoFmtToggle

1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
1 local M = {}
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
2
1095
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
3 local api, fn = vim.api, vim.fn
1085
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
4
1095
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
5 function M.calias(aliases)
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
6 for abbrev, expand in pairs(aliases) do
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
7 vim.cmd.cnoreabbrev(
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
8 string.format(
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
9 [[<expr> %s (getcmdtype() == ":" && getcmdline() == "%s") ? "%s" : "%s"]],
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
10 abbrev,
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
11 abbrev,
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
12 expand,
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
13 abbrev
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
14 )
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
15 )
1095
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
16 end
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
17 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
18
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
19 function M.autocmd(group, event, pattern, opts)
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20 if type(opts) == "function" then
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
21 local func = opts
1085
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
22 opts = {
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
23 callback = function(_)
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
24 func()
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
25 end,
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
26 }
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
27 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
28
1085
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
29 api.nvim_create_autocmd(
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
30 event,
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31 vim.tbl_extend("keep", opts, {
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32 group = group,
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
33 pattern = pattern,
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34 })
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
35 )
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37
1085
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
38 function M.safe_filter_file(cmd)
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
39 local errorfile = fn.tempname()
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
40 vim.cmd([[silent %!]] .. cmd .. [[ 2>]] .. fn.shellescape(errorfile))
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
41 if vim.v.shell_error ~= 0 then
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
42 vim.cmd("silent undo")
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
43 api.nvim_err_write(io.open(errorfile):read("*a"))
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
44 end
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
45 fn.delete(errorfile)
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
46 end
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
47
1156
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
48 function M.last_modified_days(fname)
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
49 return (os.time() - vim.fn.getftime(fname)) / 86400
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
50 end
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
51
1157
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
52 function M.prune_files(path, days)
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
53 local sunset = os.time() - (days * 86400)
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
54 path = vim.fs.normalize(path)
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
55
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
56 if fn.getftype(path) == "" then return end
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
57
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
58 for fname, type in vim.fs.dir(path) do
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
59 local fpath = vim.fs.normalize(path .. "/" .. fname)
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
60 if type == "file" and fn.getftime(fpath) < sunset then
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
61 os.remove(fpath)
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
62 end
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
63 end
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
64 end
a237720efee9 Convert backup/undofile pruning to lua
Meredith Howard <mhoward@roomag.org>
parents: 1156
diff changeset
65
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
66 return M

mercurial