nvim/lua/config/autocmds.lua
2024-05-22 13:59:00 +02:00

104 lines
2.7 KiB
Lua

local function augroup(name)
return vim.api.nvim_create_augroup("my_" .. name, { clear = false })
end
vim.api.nvim_create_autocmd(
{ "BufEnter", "FocusGained", "InsertLeave", "WinEnter", "CmdlineLeave" },
{
group = augroup("toggle_relative_number"),
callback = function()
if not vim.opt.number:get() and not vim.opt.relativenumber:get() then
return
end
vim.opt.relativenumber = vim.api.nvim_get_mode().mode ~= "i"
end,
}
)
vim.api.nvim_create_autocmd(
{ "BufLeave", "FocusLost", "InsertEnter", "WinLeave", "CmdlineEnter" },
{
group = augroup("toggle_relative_number"),
callback = function(ev)
vim.opt.relativenumber = false
if ev.event == "CmdlineEnter" then
vim.cmd("redraw")
end
end,
}
)
-- close some filetypes with <q>
vim.api.nvim_create_autocmd({ 'FileType' }, {
pattern = { 'qf', 'help', 'man', 'lspinfo', 'spectre_panel', 'lir' },
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
end,
})
vim.api.nvim_create_autocmd({ 'FileType' }, {
pattern = { 'cpp', 'cc', 'hpp', 'hh', 'md', 'markdown', 'java' },
callback = function()
vim.opt.ts = 4
vim.opt.sw = 4
vim.opt.expandtab = true
end,
})
-- Highlight on yank
vim.api.nvim_create_autocmd({ 'TextYankPost' }, {
callback = function()
vim.highlight.on_yank { higroup = 'Visual', timeout = 200 }
end,
})
-- resize splits if window got resized
vim.api.nvim_create_autocmd({ "VimResized" }, {
callback = function()
local current_tab = vim.fn.tabpagenr()
vim.cmd("tabdo wincmd =")
vim.cmd("tabnext " .. current_tab)
end,
})
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'gitcommit', 'markdown' },
callback = function()
vim.opt_local.wrap = true
vim.opt_local.spell = true
vim.opt_local.textwidth = 80
end,
})
-- go to last loc when opening a buffer
vim.api.nvim_create_autocmd("BufReadPost", {
callback = function(event)
local exclude = { "gitcommit" }
local buf = event.buf
if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].lazyvim_last_loc then
return
end
vim.b[buf].lazyvim_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,
})
vim.cmd [[
augroup change_cursor
au!
au ExitPre * :set guicursor=a:hor90
augroup END
]]
-- Fixes Autocomment
-- vim.api.nvim_create_autocmd({ 'BufWinEnter' }, {
-- callback = function()
-- vim.cmd 'set formatoptions-=cro'
-- end,
-- })