feat: autocmds

This commit is contained in:
punkfairie 2024-11-28 21:07:10 -08:00
parent e62575407d
commit 5484ba1eb8
Signed by: punkfairie
GPG key ID: A509E8F77FB9D696
2 changed files with 130 additions and 0 deletions

View file

@ -2,10 +2,12 @@
vim.cmd.filetype('plugin', 'indent', 'on') vim.cmd.filetype('plugin', 'indent', 'on')
-- let sqlite.lua (which some plugins depend on) know where to find sqlite -- 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') vim.g.sqlite_clib_path = require('luv').os_getenv('LIBSQLITE')
require('options') require('options')
require('keymaps') require('keymaps')
require('autocmds')
require('snacks-nvim') require('snacks-nvim')

128
nvim/lua/autocmds.lua Normal file
View file

@ -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 <q>',
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,
})