neovim-confs/lua/plex/config/autocmds.lua

169 lines
4.2 KiB
Lua
Raw Normal View History

2023-09-06 00:26:45 +02:00
-- Rafi's Neovim autocmds
2023-09-06 01:26:46 +02:00
-- github.com/rafi/vim-config
2023-09-06 00:26:45 +02:00
-- ===
2023-09-06 01:26:46 +02:00
-- This file is automatically loaded by rafi.config.init
2023-09-06 00:26:45 +02:00
local function augroup(name)
2023-09-06 10:19:56 +02:00
return vim.api.nvim_create_augroup('plex_' .. name, {})
2023-09-06 00:26:45 +02:00
end
-- Check if we need to reload the file when it changed
vim.api.nvim_create_autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, {
group = augroup('checktime'),
command = 'checktime',
})
2023-09-25 13:48:17 +02:00
-- Caution: Causes Errors?
-- -- Open Neotree in new tabs
-- vim.api.nvim_create_autocmd({'TabNewEntered' }, {
-- group = augroup('automatic_neotree'),
-- callback = function()
-- vim.cmd('Neotree')
-- end,
-- })
2023-09-22 14:10:16 +02:00
2023-09-06 00:26:45 +02:00
-- Go to last loc when opening a buffer, see ':h last-position-jump'
vim.api.nvim_create_autocmd('BufReadPost', {
group = augroup('last_loc'),
callback = function()
local exclude = { 'gitcommit', 'commit', 'gitrebase' }
local buf = vim.api.nvim_get_current_buf()
if vim.tbl_contains(exclude, vim.bo[buf].filetype) then
return
end
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,
})
-- Show cursor line only in active window
vim.api.nvim_create_autocmd({ 'InsertLeave', 'WinEnter' }, {
group = augroup('auto_cursorline_show'),
callback = function(event)
if vim.bo[event.buf].buftype == '' then
vim.opt_local.cursorline = true
end
end,
})
vim.api.nvim_create_autocmd({ 'InsertEnter', 'WinLeave' }, {
group = augroup('auto_cursorline_hide'),
callback = function(_)
vim.opt_local.cursorline = false
end,
})
-- Highlight on yank
vim.api.nvim_create_autocmd('TextYankPost', {
group = augroup('highlight_yank'),
callback = function()
vim.highlight.on_yank()
end,
})
-- Automatically set read-only for files being edited elsewhere
2023-09-12 10:11:05 +02:00
-- vim.api.nvim_create_autocmd('SwapExists', {
-- group = augroup('open_swap'),
-- nested = true,
-- callback = function()
-- vim.v.swapchoice = 'o'
-- end,
-- })
2023-09-06 00:26:45 +02:00
-- Create directories when needed, when saving a file (except for URIs "://").
vim.api.nvim_create_autocmd('BufWritePre', {
group = augroup('auto_create_dir'),
callback = function(event)
if event.match:match('^%w%w+://') then
return
end
local file = vim.loop.fs_realpath(event.match) or event.match
vim.fn.mkdir(vim.fn.fnamemodify(file, ':p:h'), 'p')
end,
})
-- Disable conceallevel for specific file-types.
vim.api.nvim_create_autocmd('FileType', {
group = augroup('fix_conceallevel'),
2023-09-22 14:10:16 +02:00
pattern = { 'latex', 'tex' },
2023-09-06 00:26:45 +02:00
callback = function()
vim.opt_local.conceallevel = 0
end,
})
-- Resize splits if window got resized
vim.api.nvim_create_autocmd('VimResized', {
group = augroup('resize_splits'),
callback = function()
vim.cmd('wincmd =')
end,
})
-- Wrap and enable spell-checker in text filetypes
vim.api.nvim_create_autocmd('FileType', {
group = augroup('spell_conceal'),
2023-09-07 15:05:39 +02:00
pattern = { 'gitcommit', 'markdown', 'tex', 'latex', 'norg' },
2023-09-06 00:26:45 +02:00
callback = function()
vim.opt_local.spell = true
vim.opt_local.conceallevel = 0
end,
})
-- Close some filetypes with <q>
vim.api.nvim_create_autocmd('FileType', {
group = augroup('close_with_q'),
pattern = {
'blame',
'checkhealth',
'fugitive',
'fugitiveblame',
'help',
'httpResult',
'lspinfo',
'notify',
'PlenaryTestPopup',
'qf',
'spectre_panel',
'startuptime',
'tsplayground',
},
callback = function(event)
vim.bo[event.buf].buflisted = false
-- stylua: ignore
vim.keymap.set('n', 'q', '<cmd>close<CR>', { buffer = event.buf, silent = true })
end,
})
vim.api.nvim_create_autocmd('BufWritePre', {
group = augroup('undo_disable'),
pattern = { '/tmp/*', '*.tmp', '*.bak', 'COMMIT_EDITMSG', 'MERGE_MSG' },
callback = function(event)
vim.opt_local.undofile = false
if event.file == 'COMMIT_EDITMSG' or event.file == 'MERGE_MSG' then
vim.opt_local.swapfile = false
end
end,
})
-- Disable swap/undo/backup files in temp directories or shm
vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufReadPre' }, {
group = augroup('secure'),
pattern = {
'/tmp/*',
'$TMPDIR/*',
'$TMP/*',
'$TEMP/*',
'*/shm/*',
'/private/var/*',
},
callback = function()
vim.opt_local.undofile = false
vim.opt_local.swapfile = false
vim.opt_global.backup = false
vim.opt_global.writebackup = false
end,
})