.config/nvim/lua/config/autocmds.lua

Sat, 11 Mar 2023 21:38:57 -0600

author
Meredith Howard <mhoward@roomag.org>
date
Sat, 11 Mar 2023 21:38:57 -0600
changeset 1078
aa4c1aa529a5
parent 1077
5439ee582f9b
child 1080
83c9f8460bde
permissions
-rw-r--r--

Start porting custom commands to lua

1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
1 local autocmd = require("config.util").autocmd
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
2 local cmd = vim.cmd
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
3 local fn = vim.fn
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
4
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
5 local g = vim.api.nvim_create_augroup("vimrc", { clear = true })
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
6
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
7 -- >> neovim specific
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
8 -- Always start terminals in insert/terminal mode
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
9 autocmd(g, "TermOpen", "*", cmd.startinsert)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
10
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
11 -- neovim's autoread doesn't do this by default.
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
12 autocmd(g, "FocusGained", "*", cmd.checktime)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
13
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
14 -- >> autowriteall improvment
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
15 -- Stopinsert on leave, or autowriteall doesn't work.
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
16 autocmd(g, { "WinLeave", "FocusLost" }, "*", function()
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
17 if not fn.pumvisible() then
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
18 fn.stopinsert()
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
19 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
20 end)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
21
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
22 -- write all on leave
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
23 autocmd(g, "FocusLost", "*", cmd.wa)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
24
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
25 -- >> auto mkpath on write
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
26 autocmd(g, "BufWritePre", "*", {
1076
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
27 callback = function(ctx)
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
28 if vim.bo[ctx.buf].buftype == "" and not string.match(ctx.file, "^[%w]+:") then
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
29 fn.mkdir(fn.fnamemodify(ctx.file, ":p:h"), "p")
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
30 end
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
31 end,
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
32 })
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
33
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34 -- >> auto session ?
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
35
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 -- >> jump to last position on open
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
37 autocmd(g, "BufReadPost", "*", function()
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
38 local ft = vim.bo.filetype
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
39 if ft == "mail" or string.match(ft, "^git") or string.match(ft, "^hg") then
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
40 return ""
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
41 end
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
42
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
43 local lastpos = fn.line([['"]])
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
44 if lastpos >= 1 and lastpos <= fn.line("$") then
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
45 vim.cmd([[normal! g`"]])
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
46 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
47 end)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
48
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
49 -- >> simple highlight conflict markers
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
50 autocmd(g, "BufReadPost", "*", function()
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
51 fn.matchadd("Error", [[\m^\([<>|]\)\{7} \@=\|^=\{7}$]])
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
52 end)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
53
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
54 -- >> nicer quickfix
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
55 autocmd(g, "BufReadPost", "quickfix", {
1076
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
56 callback = function(ctx)
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
57 -- simplify noisy :ltag output
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
58 if string.match(vim.w.quickfix_title, "^ltag") then
1077
5439ee582f9b Highlight the matched part in :ltag
Meredith Howard <mhoward@roomag.org>
parents: 1076
diff changeset
59 -- Hide ctags regex anchors
5439ee582f9b Highlight the matched part in :ltag
Meredith Howard <mhoward@roomag.org>
parents: 1076
diff changeset
60 fn.matchadd("Conceal", [[\m|\zs\^\\V\|\\$\ze|]])
5439ee582f9b Highlight the matched part in :ltag
Meredith Howard <mhoward@roomag.org>
parents: 1076
diff changeset
61
5439ee582f9b Highlight the matched part in :ltag
Meredith Howard <mhoward@roomag.org>
parents: 1076
diff changeset
62 -- highlight match in line. if tagname begins with / the rest is a \V
5439ee582f9b Highlight the matched part in :ltag
Meredith Howard <mhoward@roomag.org>
parents: 1076
diff changeset
63 -- regex. match must be between vertical bars, so its the 2nd column.
5439ee582f9b Highlight the matched part in :ltag
Meredith Howard <mhoward@roomag.org>
parents: 1076
diff changeset
64 local tagmatch = string.gsub(vim.fn.gettagstack().items[1].tagname, "^/", "\\V", 1)
5439ee582f9b Highlight the matched part in :ltag
Meredith Howard <mhoward@roomag.org>
parents: 1076
diff changeset
65 fn.matchadd("Underlined", [[\m|.*\zs]] .. tagmatch .. [[\m\ze.*|]])
1076
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
66 end
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
67
1076
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
68 -- easy close
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
69 vim.keymap.set("n", "q", "<C-w>q", { buffer = true })
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
70 end,
15007f695dfb clean up unused vim fns
Meredith Howard <mhoward@roomag.org>
parents: 1075
diff changeset
71 })

mercurial