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

changeset 1075
3b88450bda15
child 1076
15007f695dfb
equal deleted inserted replaced
1074:91b42a87c3d9 1075:3b88450bda15
1 local cmd = vim.cmd
2 local fn = vim.fn
3
4 local vimrc = vim.api.nvim_create_augroup("vimrc", { clear = true })
5
6 local function autocmd(event, pattern, opts)
7 vim.api.nvim_create_autocmd(event,
8 vim.tbl_extend("keep", opts, {
9 group = vimrc,
10 pattern = pattern,
11 })
12 )
13 end
14
15 local function cb(func)
16 return { callback = function(_) func() end }
17 end
18
19 -- >> neovim specific
20 -- Always start terminals in insert/terminal mode
21 autocmd("TermOpen", "*", cb(cmd.startinsert))
22
23 -- neovim's autoread doesn't do this by default.
24 autocmd("FocusGained", "*", cb(cmd.checktime))
25
26 -- >> autowriteall improvment
27 -- Stopinsert on leave, or autowriteall doesn't work.
28 autocmd({"WinLeave", "FocusLost"}, "*", { callback = function(_)
29 if not fn.pumvisible() then
30 fn.stopinsert()
31 end
32 end })
33
34 -- write all on leave
35 autocmd("FocusLost", "*", cb(cmd.wa))
36
37
38 -- >> auto mkpath on write
39 autocmd("BufWritePre", "*", { callback = function(ctx)
40 if vim.bo[ctx.buf].buftype == ""
41 and not string.match(ctx.file, "^[%w]+:")
42 then
43 fn.mkdir(fn.fnamemodify(ctx.file, ":p:h"), "p")
44 end
45 end })
46
47 -- >> auto session ?
48
49 -- >> jump to last position on open
50 autocmd("BufReadPost", "*", { callback = function(_)
51 local ft = vim.bo.filetype
52 if ft == "mail" or string.match(ft, "^git") or string.match(ft, "^hg") then
53 return ''
54 end
55
56 local lastpos = fn.line([['"]])
57 if lastpos >= 1 and lastpos <= fn.line("$") then
58 vim.cmd([[normal! g`"]])
59 end
60 end })
61
62 -- >> simple highlight conflict markers
63 autocmd("BufReadPost", "*", { callback = function(_)
64 fn.matchadd("Error", [[^\([<>|]\)\{7} \@=\|^=\{7}$]])
65 end })
66
67 -- >> nicer quickfix
68 autocmd("BufReadPost", "quickfix", { callback = function(ctx)
69 -- simplify noisy :ltag output
70 if string.match(vim.w.quickfix_title, "^ltag") then
71 fn.matchadd("Conceal", [[|\zs\^\\V\|\\$|.*]])
72 end
73
74 -- easy close
75 vim.keymap.set("n", "q", "<C-w>q", {buffer = true})
76 end })

mercurial