-
Notifications
You must be signed in to change notification settings - Fork 455
/
event.lua
158 lines (151 loc) · 4.38 KB
/
event.lua
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
-- Now use `<A-k>` or `<A-1>` to back to the `dotstutor`.
local autocmd = {}
function autocmd.nvim_create_augroups(definitions)
for group_name, definition in pairs(definitions) do
vim.api.nvim_command("augroup " .. group_name)
vim.api.nvim_command("autocmd!")
for _, def in ipairs(definition) do
local command = table.concat(vim.tbl_flatten({ "autocmd", def }), " ")
vim.api.nvim_command(command)
end
vim.api.nvim_command("augroup END")
end
end
-- defer setting LSP-related keymaps till LspAttach
local mapping = require("keymap.completion")
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("LspKeymapLoader", { clear = true }),
callback = function(event)
if not _G._debugging then
mapping.lsp(event.buf)
end
end,
})
-- auto close NvimTree
vim.api.nvim_create_autocmd("BufEnter", {
group = vim.api.nvim_create_augroup("NvimTreeClose", { clear = true }),
pattern = "NvimTree_*",
callback = function()
local layout = vim.api.nvim_call_function("winlayout", {})
if
layout[1] == "leaf"
and vim.api.nvim_get_option_value("filetype", { buf = vim.api.nvim_win_get_buf(layout[2]) }) == "NvimTree"
and layout[3] == nil
then
vim.api.nvim_command([[confirm quit]])
end
end,
})
-- auto close some filetype with <q>
vim.api.nvim_create_autocmd("FileType", {
pattern = {
"qf",
"help",
"man",
"notify",
"nofile",
"lspinfo",
"terminal",
"prompt",
"toggleterm",
"copilot",
"startuptime",
"tsplayground",
"PlenaryTestPopup",
},
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.api.nvim_buf_set_keymap(event.buf, "n", "q", "<CMD>close<CR>", { silent = true })
end,
})
-- Fix fold issue of files opened by telescope
vim.api.nvim_create_autocmd("BufRead", {
callback = function()
vim.api.nvim_create_autocmd("BufWinEnter", {
once = true,
command = "normal! zx",
})
end,
})
function autocmd.load_autocmds()
local definitions = {
lazy = {},
bufs = {
-- Reload vim config automatically
{
"BufWritePost",
[[$VIM_PATH/{*.vim,*.yaml,vimrc} nested source $MYVIMRC | redraw]],
},
-- Reload Vim script automatically if setlocal autoread
{
"BufWritePost,FileWritePost",
"*.vim",
[[nested if &l:autoread > 0 | source <afile> | echo 'source ' . bufname('%') | endif]],
},
{ "BufWritePre", "/tmp/*", "setlocal noundofile" },
{ "BufWritePre", "COMMIT_EDITMSG", "setlocal noundofile" },
{ "BufWritePre", "MERGE_MSG", "setlocal noundofile" },
{ "BufWritePre", "*.tmp", "setlocal noundofile" },
{ "BufWritePre", "*.bak", "setlocal noundofile" },
-- auto place to last edit
{
"BufReadPost",
"*",
[[if line("'\"") > 1 && line("'\"") <= line("$") | execute "normal! g'\"" | endif]],
},
-- Auto toggle fcitx5
-- {"InsertLeave", "* :silent", "!fcitx5-remote -c"},
-- {"BufCreate", "*", ":silent !fcitx5-remote -c"},
-- {"BufEnter", "*", ":silent !fcitx5-remote -c "},
-- {"BufLeave", "*", ":silent !fcitx5-remote -c "}
},
wins = {
-- Highlight current line only on focused window
{
"WinEnter,BufEnter,InsertLeave",
"*",
[[if ! &cursorline && &filetype !~# '^\(dashboard\|clap_\)' && ! &pvw | setlocal cursorline | endif]],
},
{
"WinLeave,BufLeave,InsertEnter",
"*",
[[if &cursorline && &filetype !~# '^\(dashboard\|clap_\)' && ! &pvw | setlocal nocursorline | endif]],
},
-- Attempt to write shada when leaving nvim
{
"VimLeave",
"*",
[[if has('nvim') | wshada | else | wviminfo! | endif]],
},
-- Check if file changed when its window is focus, more eager than 'autoread'
{ "FocusGained", "* checktime" },
-- Equalize window dimensions when resizing vim window
{ "VimResized", "*", [[tabdo wincmd =]] },
},
ft = {
{ "FileType", "alpha", "set showtabline=0" },
{ "FileType", "markdown", "set wrap" },
{ "FileType", "make", "set noexpandtab shiftwidth=8 softtabstop=0" },
{ "FileType", "dap-repl", "lua require('dap.ext.autocompl').attach()" },
{
"FileType",
"*",
[[setlocal formatoptions-=cro]],
},
{
"FileType",
"c,cpp",
"nnoremap <leader>h :ClangdSwitchSourceHeaderVSplit<CR>",
},
},
yank = {
{
"TextYankPost",
"*",
[[silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=300})]],
},
},
}
autocmd.nvim_create_augroups(require("modules.utils").extend_config(definitions, "user.event"))
end
autocmd.load_autocmds()