use new autocommand functions
This commit is contained in:
parent
d7b49c4c4b
commit
4fbe1c4174
@ -1,44 +1,71 @@
|
||||
local function nvim_create_augroups(definitions)
|
||||
for group_name, definition in pairs(definitions) do
|
||||
vim.cmd('augroup ' .. group_name)
|
||||
vim.cmd('autocmd!')
|
||||
for _, def in ipairs(definition) do
|
||||
local command = table.concat(vim.tbl_flatten({ 'autocmd', def }), ' ')
|
||||
vim.cmd(command)
|
||||
end
|
||||
vim.cmd('augroup END')
|
||||
end
|
||||
end
|
||||
local api = vim.api
|
||||
|
||||
local autocmds = {
|
||||
packer = {
|
||||
{ 'BufWritePost', 'plugins.lua', 'PackerCompile' },
|
||||
},
|
||||
restore_cursor = {
|
||||
{ 'BufRead', '*', [[call setpos(".", getpos("'\""))]] },
|
||||
},
|
||||
save_shada = {
|
||||
{ 'FocusGained,FocusLost', '*', 'rshada|wshada' },
|
||||
},
|
||||
resize_windows_proportionally = {
|
||||
{ 'VimResized', '*', ':wincmd =' },
|
||||
},
|
||||
lua_highlight = {
|
||||
{ 'TextYankPost', '*', [[silent! lua vim.highlight.on_yank() {higroup='IncSearch', timeout=400}]] },
|
||||
},
|
||||
file_type = {
|
||||
{ 'BufRead,BufNewFile', '*.simvis', 'set filetype=xml' },
|
||||
{ 'BufRead,BufNewFile', '*.simcfg,*.simcon,*.simudex', 'set filetype=cfg' },
|
||||
{ 'BufRead,BufNewFile', 'Jenkinsfile*', 'set filetype=groovy' },
|
||||
{ 'BufRead,BufNewFile', '*.manifest', 'set filetype=xml' },
|
||||
{ 'BufRead,BufNewFile', 'SConstruct,SConscript', 'set filetype=python' },
|
||||
},
|
||||
file_changed = {
|
||||
{ 'BufEnter,FocusGained', '*', 'checktime' },
|
||||
},
|
||||
no_auto_comment = {
|
||||
{ 'BufEnter', '*', 'set fo-=c fo-=r fo-=o' },
|
||||
},
|
||||
}
|
||||
-- Highlight on yank
|
||||
local yankGrp = api.nvim_create_augroup('YankHighlight', { clear = true })
|
||||
api.nvim_create_autocmd('TextYankPost', {
|
||||
command = 'silent! lua vim.highlight.on_yank()',
|
||||
group = yankGrp,
|
||||
})
|
||||
|
||||
nvim_create_augroups(autocmds)
|
||||
-- Filetypes
|
||||
local fileGrp = api.nvim_create_augroup('file_type', { clear = true })
|
||||
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
|
||||
pattern = { '*.simvis', '*.manifest' },
|
||||
callback = function()
|
||||
vim.bo.filetype = 'xml'
|
||||
end,
|
||||
group = fileGrp,
|
||||
})
|
||||
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
|
||||
pattern = { '*.simcfg', '*.simcon', '*.simudex' },
|
||||
callback = function()
|
||||
vim.bo.filetype = 'xml'
|
||||
end,
|
||||
group = fileGrp,
|
||||
})
|
||||
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
|
||||
pattern = { 'JenkinsFile*' },
|
||||
callback = function()
|
||||
vim.bo.filetype = 'groovy'
|
||||
end,
|
||||
group = fileGrp,
|
||||
})
|
||||
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
|
||||
pattern = { 'SConstruct', 'SConscript' },
|
||||
callback = function()
|
||||
vim.bo.filetype = 'python'
|
||||
end,
|
||||
group = fileGrp,
|
||||
})
|
||||
|
||||
-- Read and write shada file
|
||||
api.nvim_create_autocmd({ 'FocusGained', 'FocusLost' }, { command = [[rshada | wshada]] })
|
||||
|
||||
-- go to last loc when opening a buffer
|
||||
api.nvim_create_autocmd(
|
||||
'BufReadPost',
|
||||
{ command = [[if line("'\"") > 1 && line("'\"") <= line("$") | execute "normal! g`\"" | endif]] }
|
||||
)
|
||||
|
||||
-- Check if we need to reload the file when it changed
|
||||
api.nvim_create_autocmd({ 'FocusGained', 'BufEnter' }, { command = [[:checktime]] })
|
||||
|
||||
-- windows to close with "q"
|
||||
api.nvim_create_autocmd(
|
||||
'FileType',
|
||||
{ pattern = { 'help', 'startuptime', 'qf', 'lspinfo' }, command = [[nnoremap <buffer><silent> q :close<CR>]] }
|
||||
)
|
||||
api.nvim_create_autocmd('FileType', { pattern = 'man', command = [[nnoremap <buffer><silent> q :quit<CR>]] })
|
||||
|
||||
-- show cursor line only in active window
|
||||
local cursorGrp = api.nvim_create_augroup('CursorLine', { clear = true })
|
||||
api.nvim_create_autocmd({ 'InsertLeave', 'WinEnter' }, { pattern = '*', command = 'set cursorline', group = cursorGrp })
|
||||
api.nvim_create_autocmd(
|
||||
{ 'InsertEnter', 'WinLeave' },
|
||||
{ pattern = '*', command = 'set nocursorline', group = cursorGrp }
|
||||
)
|
||||
-- don't auto comment new line
|
||||
api.nvim_create_autocmd('BufEnter', { command = [[set formatoptions-=cro]] })
|
||||
|
||||
-- Keep window ratio when resize
|
||||
api.nvim_create_autocmd('VimResized', {command = [[wincmd =]]})
|
||||
|
Loading…
x
Reference in New Issue
Block a user