1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
local function augroup(name)
return vim.api.nvim_create_augroup(name, { clear = true })
end
local autocmd = vim.api.nvim_create_autocmd
-- Check if we need to reload the file when it changed
autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
group = augroup("checktime"),
callback = function()
if vim.o.buftype ~= "nofile" then
vim.cmd("checktime")
end
end,
})
-- Highlight on yank
autocmd("TextYankPost", {
group = augroup("highlight_yank"),
callback = vim.hl.on_yank,
})
-- go to last loc when opening a buffer
autocmd("BufReadPost", {
group = augroup("last_loc"),
callback = function(event)
local exclude = { "gitcommit" }
local buf = event.buf
if
vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].last_loc
then
return
end
vim.b[buf].last_loc = true
local mark = vim.api.nvim_buf_get_mark(buf, '"')
local lcount = vim.api.nvim_buf_line_count(buf)
if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark)
end
end,
})
-- close some filetypes with <q>
autocmd("FileType", {
group = augroup("close_with_q"),
pattern = {
"PlenaryTestPopup",
"checkhealth",
"dbout",
"gitsigns-blame",
"grug-far",
"help",
"lspinfo",
"notify",
"qf",
"spectre_panel",
"startuptime",
"tsplayground",
},
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.schedule(function()
vim.keymap.set("n", "q", function()
vim.cmd("close")
pcall(vim.api.nvim_buf_delete, event.buf, { force = true })
end, {
buffer = event.buf,
silent = true,
desc = "Quit buffer",
})
end)
end,
})
-- make it easier to close man-files when opened inline
autocmd("FileType", {
group = augroup("man_unlisted"),
pattern = { "man" },
callback = function(event)
vim.bo[event.buf].buflisted = false
end,
})
-- Auto create dir when saving a file, in case some intermediate directory does not exist
autocmd({ "BufWritePre" }, {
group = augroup("auto_create_dir"),
callback = function(event)
if event.match:match("^%w%w+:[\\/][\\/]") then
return
end
local file = vim.uv.fs_realpath(event.match) or event.match
vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
end,
})
autocmd("BufWritePost", {
group = augroup("bspwm"),
pattern = "*bspwmrc",
command = "!bspc wm --restart",
})
autocmd("BufWritePost", {
group = augroup("polybar"),
pattern = "*/polybar/config",
command = "!polybar-msg cmd restart",
})
autocmd("BufWritePost", {
group = augroup("xdg-user-dirs"),
pattern = "user-dirs.dirs,user-dirs.locale",
command = "!xdg-user-dirs-update",
})
autocmd("BufWritePost", {
group = augroup("dunst"),
pattern = "dunstrc",
command = "!killall -SIGUSR2 dunst",
})
autocmd("BufWritePost", {
group = augroup("fc-cache"),
pattern = "fonts.conf",
command = "!fc-cache",
})
autocmd("FileType", {
group = augroup("treesitter_start"),
pattern = { "*" },
callback = function()
if pcall(vim.treesitter.start) then
vim.wo.foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.bo.indentexpr = "v:lua.vim.treesitter.indentexpr()"
end
end,
})
|