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

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

mercurial