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

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

mercurial