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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
local function augroup(name)
return vim.api.nvim_create_augroup("TheSiahxyz_" .. name, { clear = true })
end
local autocmd = vim.api.nvim_create_autocmd
-- Change the color of symlink files to distinguish between directory and symlink files
autocmd("FileType", {
group = augroup("highlight_linked_files"),
pattern = "netrw",
callback = function()
vim.cmd("highlight NetrwSymlink guifg=#689D6A ctermfg=214")
end,
})
-- Check if we need to reload the file when it changed
autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
group = augroup("check_time"),
callback = function()
if vim.o.buftype ~= "nofile" then
vim.cmd("checktime")
end
end,
})
-- Highlight on yank
autocmd("TextYankPost", {
group = augroup("highlight_yank"),
callback = function()
vim.highlight.on_yank()
end,
})
-- Resize splits if window got resized
autocmd({ "VimResized" }, {
group = augroup("window_config"),
callback = function()
local current_tab = vim.fn.tabpagenr()
vim.cmd("tabdo wincmd =")
vim.cmd("tabnext " .. current_tab)
end,
})
-- 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].thesiahxyz_last_loc then
return
end
vim.b[buf].thesiahxyz_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 = {
"checkhealth",
"dbout",
"gitsigns-blame",
"grug-far",
"help",
"lspinfo",
"Lazy",
"neotest-output",
"neotest-output-panel",
"neotest-summary",
"notify",
"PlenaryTestPopup",
"qf",
"query",
"spectre_panel",
"startuptime",
"terminal",
"tsplayground",
},
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
end,
})
autocmd("FileType", {
group = augroup("q_as_bd"),
pattern = "netrw",
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set("n", "q", function()
vim.cmd("bd")
end, { buffer = event.buf, silent = true })
end,
})
-- Make it easier to close man-files when opened inline
autocmd("FileType", {
group = augroup("man_close"),
pattern = { "man" },
callback = function(event)
vim.bo[event.buf].buflisted = false
end,
})
-- Fix conceallevel for json files
autocmd({ "FileType" }, {
group = augroup("json_config"),
pattern = { "json", "jsonc", "json5" },
callback = function()
vim.opt_local.conceallevel = 0
end,
})
-- Auto create dir when saving a file, in case some intermediate directory does not exist
autocmd({ "BufWritePre" }, {
group = augroup("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,
})
-- Automatically delete all trailing whitespace and newlines at end of file on save
local file_save = augroup("file_save")
autocmd("BufWritePre", {
group = file_save,
pattern = "*",
callback = function()
local cursor_pos = vim.api.nvim_win_get_cursor(0)
vim.cmd([[ %s/\s\+$//e ]]) -- Remove trailing spaces
vim.cmd([[ %s/\n\+\%$//e ]]) -- Remove trailing newlines
local line_count = vim.api.nvim_buf_line_count(0)
local line = math.min(cursor_pos[1], line_count)
local col = cursor_pos[2]
-- Optional: clamp column if line got shorter
local line_text = vim.api.nvim_buf_get_lines(0, line - 1, line, false)[1] or ""
col = math.min(col, #line_text)
vim.api.nvim_win_set_cursor(0, { line, col })
end,
})
-- Add a trailing newline for C files
autocmd("BufWritePre", {
group = file_save,
pattern = "*.[ch]",
callback = function()
vim.cmd([[ %s/\%$/\r/e ]])
end,
})
-- Go to insert mode in terminal
local win_enter = augroup("win_enter")
autocmd("WinEnter", {
group = win_enter,
callback = function()
if vim.bo.buftype == "terminal" then
vim.cmd("startinsert")
end
end,
})
-- Correct email signature delimiter in neomutt files
autocmd("BufWritePre", {
group = file_save,
pattern = "*neomutt*",
callback = function()
vim.cmd([[ %s/^--$/-- /e ]])
end,
})
local function organize_imports()
local params = {
command = "pyright.organizeimports",
arguments = { vim.uri_from_bufnr(0) },
}
vim.lsp.buf.execute_command(params)
end
autocmd("LspAttach", {
group = augroup("lsp_attach"),
callback = function(e)
local client = vim.lsp.get_client_by_id(e.data.client_id)
if client and client.name == "pyright" then
vim.api.nvim_create_user_command(
"PyrightOrganizeImports",
organize_imports,
{ desc = "Organize imports (lsp)" }
)
end
vim.keymap.set("n", "gD", function()
vim.lsp.buf.definition()
end, { buffer = e.buf, desc = "Go to definition (lsp)" })
vim.keymap.set("n", "K", function()
vim.lsp.buf.hover()
end, { buffer = e.buf, desc = "Go to keywords (lsp)" })
vim.keymap.set("n", "<leader>ls", function()
vim.lsp.buf.workspace_symbol()
end, { buffer = e.buf, desc = "Workspace symbol (lsp)" })
vim.keymap.set("n", "<leader>lo", function()
vim.diagnostic.open_float()
end, { buffer = e.buf, desc = "Open diagnostic" })
vim.keymap.set("n", "<leader>lc", function()
vim.lsp.buf.code_action()
end, { buffer = e.buf, desc = "Code action (lsp)" })
vim.keymap.set("n", "<leader>lr", function()
vim.lsp.buf.references()
end, { buffer = e.buf, desc = "References (lsp)" })
vim.keymap.set("n", "<leader>ln", function()
vim.lsp.buf.rename()
end, { buffer = e.buf, desc = "Rename (lsp)" })
vim.keymap.set("n", "<leader>lh", function()
vim.lsp.buf.signature_help()
end, { buffer = e.buf, desc = "Signature help (lsp)" })
vim.keymap.set("n", "]d", function()
vim.diagnostic.goto_next()
end, { buffer = e.buf, desc = "Next diagnostic" })
vim.keymap.set("n", "[d", function()
vim.diagnostic.goto_prev()
end, { buffer = e.buf, desc = "Previous diagnostic" })
end,
})
-- Save file as sudo on files that require root permission
vim.api.nvim_create_user_command("SudoWrite", function()
vim.cmd([[
write !sudo tee % 2>/dev/null
edit!
]])
end, {})
vim.api.nvim_create_user_command("SudoWritequit", function()
vim.cmd([[
write !sudo tee % 2>/dev/null
edit!
quit!
]])
end, {})
-- Markdown for specific files and directories
autocmd({ "BufRead", "BufNewFile" }, {
pattern = { "/tmp/calcurse*", "~/.calcurse/notes/*" },
command = "set filetype=markdown",
})
-- Groff for specific file extensions
autocmd({ "BufRead", "BufNewFile" }, {
pattern = { "*.ms", "*.me", "*.mom", "*.man" },
callback = function()
vim.cmd([[
set columns=90
set filetype=groff
set linebreak
set textwidth=0
set wrap
set wrapmargin=0
]])
end,
})
-- TeX for .tex files
autocmd({ "BufRead", "BufNewFile" }, {
pattern = { "*.tex" },
command = "set filetype=tex",
})
-- When shortcut files are updated, renew bash and lf configs with new material:
autocmd("BufWritePost", {
group = augroup("bookmarks"),
pattern = { "bm-files", "bm-dirs" },
callback = function()
-- Execute the 'shortcuts' shell command
local result = vim.fn.system("shortcuts")
-- Display an error message only if the 'shortcuts' command fails
if vim.v.shell_error ~= 0 then
-- Display an error message if the 'shortcuts' command fails
vim.api.nvim_echo({ { "failed to update shortcuts: " .. result, "ErrorMsg" } }, true, {})
end
end,
})
-- Source lfrc if it's edited
local lf_config = augroup("lf_config")
autocmd("BufWritePost", {
group = lf_config,
pattern = { "lfrc" },
callback = function()
vim.cmd("silent !source ~/.config/lf/lfrc")
end,
})
-- Set vimwiki's index filetype to vimwiki instead of markdown
local vimwiki_config = augroup("vimwiki_config")
autocmd({ "BufRead", "BufNewFile" }, {
group = vimwiki_config,
pattern = vim.fn.expand("~/.local/share/vimwiki") .. "/**/*.md",
callback = function(args)
vim.bo[args.buf].filetype = "vimwiki"
end,
})
-- Run xrdb whenever Xdefaults or Xresources are updated.
local x_config = augroup("x_config")
autocmd({ "BufRead", "BufNewFile" }, {
group = x_config,
pattern = { "Xresources", "Xdefaults", "xresources", "xdefaults" },
callback = function()
vim.bo.filetype = "xdefaults"
end,
})
autocmd("BufWritePost", {
group = x_config,
pattern = { "Xresources", "Xdefaults", "xresources", "xdefaults" },
callback = function()
vim.cmd("silent !xrdb %")
end,
})
-- Recompile suckless programs on config edit.
local home = os.getenv("HOME")
local suckless_config = vim.api.nvim_create_augroup("suckless_config", { clear = true })
autocmd("BufWritePost", {
group = suckless_config,
pattern = home .. "/.local/src/suckless/dmenu/config.def.h",
callback = function()
vim.cmd("silent !cd " .. home .. "/.local/src/suckless/dmenu/ && sudo make install")
end,
})
autocmd("BufWritePost", {
group = suckless_config,
pattern = home .. "/.local/src/suckless/dwmblocks/config.def.h",
callback = function()
vim.cmd(
"silent !cd "
.. home
.. "/.local/src/suckless/dwmblocks/ && sudo make install && { killall -q dwmblocks; setsid -f dwmblocks; }"
)
end,
})
autocmd("BufWritePost", {
group = suckless_config,
pattern = home .. "/.local/src/suckless/slock/config.def.h",
callback = function()
vim.cmd("silent !cd " .. home .. "/.local/src/suckless/slock/ && sudo make install")
end,
})
local suckless_keys = augroup("suckless_keys")
autocmd("BufWritePost", {
group = suckless_keys,
pattern = home .. "/.local/src/suckless/dwm/config.def.h",
callback = function()
vim.cmd("silent !" .. home .. "/.local/bin/extractkeys")
end,
})
autocmd("BufWritePost", {
group = suckless_keys,
pattern = home .. "/.local/src/suckless/st/config.def.h",
callback = function()
vim.cmd("silent !" .. home .. "/.local/bin/extractkeys")
end,
})
autocmd("BufWritePost", {
group = augroup("suckless_doc"),
pattern = home .. "/.local/src/suckless/dwm/thesiah-default.mom",
callback = function()
vim.cmd("silent !cd " .. home .. "/.local/src/suckless/dwm/ && rm -f thesiah.mom")
end,
})
autocmd({ "BufRead", "BufEnter" }, {
pattern = { "*.c", "*.cpp", "*.h", "*.hpp" },
callback = function()
local suckless_path = vim.fn.expand("~/.local/src/suckless"):gsub("/+$", "")
local file_path = vim.fn.expand("%:p"):gsub("/+$", "")
if file_path:sub(1, #suckless_path) == suckless_path then
vim.b.autoformat = false
end
end,
})
|