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

Wed, 25 Sep 2024 15:03:13 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Wed, 25 Sep 2024 15:03:13 -0500
changeset 1179
b0a0ba3240b5
parent 1166
34bf03fa07e4
permissions
-rw-r--r--

No LSP autostart when read-only

I think nvim actually threw out 'view' argc detection, I just use -R and alias
view to it.

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
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1165
diff changeset
9 autocmd(g, "TermOpen", "*", function(_) cmd.startinsert() end)
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.
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1165
diff changeset
12 autocmd(g, "FocusGained", "*", function(_) cmd.checktime() end)
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.
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1165
diff changeset
16 autocmd(g, { "WinLeave", "FocusLost" }, "*", function(_)
1087
bfcf9813c223 try to fix stopinsert/autowrite again
Meredith Howard <mhoward@roomag.org>
parents: 1086
diff changeset
17 if fn.pumvisible() == 0 then
1088
c221227cbba5 Fix stopinsert
Meredith Howard <mhoward@roomag.org>
parents: 1087
diff changeset
18 cmd.stopinsert()
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
19 end
1129
1bd6edeaf6ae fix error when unnamed buffers can't be written
Meredith Howard <mhoward@roomag.org>
parents: 1088
diff changeset
20 pcall(cmd.wa)
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
21 end)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
22
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
23 -- >> auto mkpath on write
1165
840f0fde07e2 set up nvim-dap etc
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
24 autocmd(g, "BufWritePre", "*", function(ctx)
840f0fde07e2 set up nvim-dap etc
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
25 if vim.bo[ctx.buf].buftype == "" and not string.match(ctx.file, "^[%w]+:") then
840f0fde07e2 set up nvim-dap etc
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
26 fn.mkdir(fn.fnamemodify(ctx.file, ":p:h"), "p")
840f0fde07e2 set up nvim-dap etc
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
27 end
840f0fde07e2 set up nvim-dap etc
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
28 end)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
29
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
30 -- >> auto session ?
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32 -- >> jump to last position on open
1086
82f517e1ae76 regexp match "no jump on load" filetypes
Meredith Howard <mhoward@roomag.org>
parents: 1082
diff changeset
33 local nojump = vim.regex([[mail\|commit\|rebase]])
82f517e1ae76 regexp match "no jump on load" filetypes
Meredith Howard <mhoward@roomag.org>
parents: 1082
diff changeset
34 assert(nojump, "Couldn't compile nojump regexp?")
82f517e1ae76 regexp match "no jump on load" filetypes
Meredith Howard <mhoward@roomag.org>
parents: 1082
diff changeset
35
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1165
diff changeset
36 autocmd(g, "BufReadPost", "*", function(_)
1086
82f517e1ae76 regexp match "no jump on load" filetypes
Meredith Howard <mhoward@roomag.org>
parents: 1082
diff changeset
37 if nojump:match_str(vim.bo.filetype or "") then
82f517e1ae76 regexp match "no jump on load" filetypes
Meredith Howard <mhoward@roomag.org>
parents: 1082
diff changeset
38 return
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
39 end
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
40
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
41 local lastpos = fn.line([['"]])
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
42 if lastpos >= 1 and lastpos <= fn.line("$") then
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
43 vim.cmd([[normal! g`"]])
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
44 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
45 end)
1075
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
46
3b88450bda15 port custom autocmds to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
47 -- >> simple highlight conflict markers
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1165
diff changeset
48 autocmd(g, "BufReadPost", "*", function(_)
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
49 fn.matchadd("Error", [[\m^\([<>|]\)\{7} \@=\|^=\{7}$]])
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents: 1077
diff changeset
50 end)

mercurial