marleyvim/modules/nixvim/autocommands/default.nix
2024-11-25 20:22:29 -08:00

150 lines
3.8 KiB
Nix

{
lib,
helpers,
...
}: let
inherit (lib.marleyos) mapListToAttrs autocmds;
inherit (helpers) mkRaw;
in {
autoGroups =
mapListToAttrs (i: {
name = "marleyos_${i}";
value = {clear = true;};
}) [
"checktime"
"highlight_yank"
"resize_splits"
"last_loc"
"close_with_q"
"man_unlisted"
"wrap_spell"
"json_conceal"
"auto_create_dir"
];
autoCmd = [
(autocmds.mk' ["FocusGained" "TermClose" "TermLeave"] "checktime" (mkRaw
# lua
''
function()
if vim.o.buftype ~= "nofile" then
vim.cmd("checktime")
end
end
'') "Check if file needs to be reloaded when changed")
(autocmds.mk' ["TextYankPost"] "highlight_yank" (mkRaw
# lua
''
function()
(vim.hl or vim.highlight).on_yank()
end
'') "Highlight on yank")
(autocmds.mk' ["VimResized"] "resize_splits" (mkRaw
# lua
''
function()
local current_tab = vim.fn.tabpagenr()
vim.cmd("tabdo wincmd =")
vim.cmd("tabnext " .. current_tab)
end
'') "Resize splits on window resize")
(autocmds.mk' ["BufReadPost"] "last_loc" (mkRaw
# lua
''
function(event)
local exclude = { "gitcommit" }
local buf = event.buf
if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].marleyvim_last_loc then
return
end
vim.b[buf].marleyvim_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
'') "Go to last location when opening a buffer")
(autocmds.mk ["FileType"] "close_with_q" [
"PlenaryTestPopup"
"checkhealth"
"dbout"
"gitsigns-blame"
"grug-far"
"help"
"lspinfo"
"neotest-output"
"neotest-output-panel"
"neotest-summary"
"notify"
"qf"
"snacks_win"
"spectre_panel"
"startuptime"
"tsplayground"
] (mkRaw
# lua
''
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
'') "Close some filetypes with <q>")
(autocmds.mk ["FileType"] "man_unlisted" ["man"] (mkRaw
# lua
''
function(event)
vim.bo[event.buf].buflisted = false
end
'') "Easy-close man files")
(autocmds.mk ["FileType"] "wrap_spell" [
"text"
"plaintex"
"typst"
"gitcommit"
"markdown"
] (mkRaw
# lua
''
function()
vim.opt_local.wrap = true
vim.opt_local.spell = true
end
'') "Wrap & check spelling in text files")
(autocmds.mk ["FileType"] "json_conceal" ["json" "jsonc" "json5"] (mkRaw
# lua
''
function()
vim.opt_local.conceallevel = 0
end
'') "Fix conceallevel for json files")
(autocmds.mk' ["BufWritePre"] "auto_create_dir" (mkRaw
# lua
''
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
'') "Auto create missing parent dirs when saving")
];
}