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

Wed, 15 May 2024 22:44:16 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Wed, 15 May 2024 22:44:16 -0500
changeset 1165
840f0fde07e2
parent 1161
2543467f42da
child 1166
34bf03fa07e4
permissions
-rw-r--r--

set up nvim-dap etc

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
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
52 return M

mercurial