From 5484ba1eb82953be1b26fec3f0bf0989874df54f Mon Sep 17 00:00:00 2001 From: punkfairie Date: Thu, 28 Nov 2024 21:07:10 -0800 Subject: [PATCH] feat: autocmds --- nvim/init.lua | 2 + nvim/lua/autocmds.lua | 128 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 nvim/lua/autocmds.lua diff --git a/nvim/init.lua b/nvim/init.lua index 8399c54..5128603 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -2,10 +2,12 @@ vim.cmd.filetype('plugin', 'indent', 'on') -- let sqlite.lua (which some plugins depend on) know where to find sqlite +---@diagnostic disable-next-line:missing-parameter vim.g.sqlite_clib_path = require('luv').os_getenv('LIBSQLITE') require('options') require('keymaps') +require('autocmds') require('snacks-nvim') diff --git a/nvim/lua/autocmds.lua b/nvim/lua/autocmds.lua new file mode 100644 index 0000000..36502f3 --- /dev/null +++ b/nvim/lua/autocmds.lua @@ -0,0 +1,128 @@ +local function augroup(name) + return vim.api.nvim_create_augroup('marleyvim_' .. name, { clear = true }) +end + +local autocmd = vim.api.nvim_create_autocmd + +autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, { + desc = 'Reload file when changed if needed', + group = augroup('checktime'), + callback = function() + if vim.o.buftype ~= 'nofile' then + vim.cmd('checktime') + end + end, +}) + +autocmd({ 'TextYankPost' }, { + desc = 'Highlight on yank', + group = augroup('highlight_yank'), + callback = function() + (vim.hl or vim.highlight).on_yank() + end, +}) + +autocmd({ 'VimResized' }, { + desc = 'Resize splits on window resize', + group = augroup('resize_splits'), + callback = function() + local current_tab = vim.fn.tabpagenr() + + vim.cmd('tabdo wincmd =') + vim.cmd('tabnext ' .. current_tab) + end, +}) + +autocmd({ 'BufReadPost' }, { + desc = 'Go to last location when opening a buffer', + 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].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, +}) + +autocmd({ 'FileType' }, { + desc = 'Close some filetypes with ', + group = augroup('close_with_q'), + pattern = { + 'PlenaryTestPopup', + 'checkhealth', + 'dbout', + 'gitsigns-blame', + 'grug-far', + 'help', + 'lspinfo', + 'neotest-output', + 'neotest-output-panel', + 'neotest-summary', + 'notify', + 'qf', + 'snacks_win', + '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, { + desc = 'Quit buffer', + buffer = event.buf, + silent = true, + }) + end) + end, +}) + +autocmd({ 'FileType' }, { + desc = 'Make it easier to close man-files', + group = augroup('man_unlisted'), + pattern = { 'man' }, + callback = function(event) + vim.bo[event.buf].buflisted = false + end, +}) + +autocmd({ 'FileType' }, { + desc = 'Fix conceallevel for JSON files', + group = augroup('json_conceal'), + pattern = { 'json', 'jsonc', 'json5' }, + callback = function() + vim.opt_local.conceallevel = 0 + end, +}) + +autocmd({ 'BufWritePre' }, { + desc = 'Auto create missing directories when saving a file', + 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, +})