From 12b0ff02b67bc42e961c2223088887397976dd6b Mon Sep 17 00:00:00 2001 From: jdhao Date: Tue, 27 Aug 2024 23:11:53 +0200 Subject: [PATCH] Move autocmd in viml to Lua --- init.lua | 2 +- lua/custom-autocmd.lua | 168 ++++++++++++++++++++++++++++++++++++- viml_conf/autocommands.vim | 121 -------------------------- 3 files changed, 166 insertions(+), 125 deletions(-) delete mode 100644 viml_conf/autocommands.vim diff --git a/init.lua b/init.lua index 5199cd7..083054a 100644 --- a/init.lua +++ b/init.lua @@ -19,7 +19,7 @@ utils.is_compatible_version(expected_version) local core_conf_files = { "globals.lua", -- some global settings "options.vim", -- setting options in nvim - "autocommands.vim", -- various autocommands + "custom-autocmd.lua", -- various autocommands "mappings.lua", -- all the user-defined mappings "plugins.vim", -- all the plugins installed and their configurations "colorschemes.lua", -- colorscheme settings diff --git a/lua/custom-autocmd.lua b/lua/custom-autocmd.lua index 21fa15c..3da4b40 100644 --- a/lua/custom-autocmd.lua +++ b/lua/custom-autocmd.lua @@ -38,8 +38,8 @@ api.nvim_create_autocmd("TextYankPost", { pattern = "*", group = yank_group, callback = function(ev) - if vim.v.event.operator == 'y' then - vim.fn.setpos('.', vim.g.current_cursor_pos) + if vim.v.event.operator == "y" then + vim.fn.setpos(".", vim.g.current_cursor_pos) end end, }) @@ -102,4 +102,166 @@ local function open_nvim_tree(data) require("nvim-tree.api").tree.open() end -vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree }) +api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree }) + +-- Do not use smart case in command line mode, extracted from https://vi.stackexchange.com/a/16511/15292. +api.nvim_create_augroup("dynamic_smartcase", { clear = true }) +api.nvim_create_autocmd("CmdLineEnter", { + group = "dynamic_smartcase", + pattern = ":", + callback = function() + vim.o.smartcase = false + end, +}) + +api.nvim_create_autocmd("CmdLineLeave", { + group = "dynamic_smartcase", + pattern = ":", + callback = function() + vim.o.smartcase = true + end, +}) + +api.nvim_create_autocmd("TermOpen", { + group = api.nvim_create_augroup("term_start", { clear = true }), + pattern = "*", + callback = function() + -- Do not use number and relative number for terminal inside nvim + vim.wo.relativenumber = false + vim.wo.number = false + + -- Go to insert mode by default to start typing command + vim.cmd("startinsert") + end, +}) + +-- Return to last cursor position when opening a file, note that here we cannot use BufReadPost +-- as event. It seems that when BufReadPost is triggered, FileType event is still not run. +-- So the filetype for this buffer is empty string. +api.nvim_create_autocmd("FileType", { + group = api.nvim_create_augroup("resume_cursor_position", { clear = true }), + pattern = "*", + callback = function(ev) + local mark_pos = api.nvim_buf_get_mark(ev.buf, '"') + local last_cursor_line = mark_pos[1] + + local max_line = vim.fn.line("$") + local buf_filetype = api.nvim_get_option_value("filetype", { buf = ev.buf }) + local buftype = api.nvim_get_option_value("buftype", { buf = ev.buf }) + + -- only handle normal files + if buf_filetype == "" or buftype ~= "" then + return + end + + -- Only resume last cursor position when there is no go-to-line command (something like '+23'). + if vim.fn.match(vim.v.argv, [[\v^\+(\d){1,}$]]) ~= -1 then + return + end + + if last_cursor_line > 1 and last_cursor_line <= max_line then + -- vim.print(string.format("mark_pos: %s", vim.inspect(mark_pos))) + -- it seems that without vim.schedule, the cursor position can not be set correctly + vim.schedule(function() + api.nvim_win_set_cursor(0, mark_pos) + end) + end + end, +}) + +local number_toggle_group = api.nvim_create_augroup("numbertoggle", { clear = true }) +api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "WinEnter" }, { + pattern = "*", + group = number_toggle_group, + desc = "togger line number", + callback = function() + if vim.wo.number then + vim.wo.relativenumber = true + end + end, +}) + +api.nvim_create_autocmd({ "BufLeave", "FocusLost", "InsertEnter", "WinLeave" }, { + group = number_toggle_group, + desc = "togger line number", + callback = function() + if vim.wo.number then + vim.wo.relativenumber = false + end + end, +}) + +api.nvim_create_autocmd("ColorScheme", { + group = api.nvim_create_augroup("custom_highlight", { clear = true }), + pattern = "*", + desc = "Define or overrride some highlight groups", + callback = function() + vim.cmd([[ + " For yank highlight + highlight YankColor ctermfg=59 ctermbg=41 guifg=#34495E guibg=#2ECC71 + + " For cursor colors + highlight Cursor cterm=bold gui=bold guibg=#00c918 guifg=black + highlight Cursor2 guifg=red guibg=red + + " For floating windows border highlight + highlight FloatBorder guifg=LightGreen guibg=NONE + + " highlight for matching parentheses + highlight MatchParen cterm=bold,underline gui=bold,underline + ]]) + end, +}) + +api.nvim_create_autocmd("BufEnter", { + pattern = "*", + group = api.nvim_create_augroup("auto_close_win", { clear = true }), + desc = "Quit Nvim if we have only one window, and its filetype match our pattern", + callback = function(ev) + local quit_filetypes = {'qf', 'vista', 'NvimTree'} + + local should_quit = true + local tabwins = api.nvim_tabpage_list_wins(0) + + for _, win in pairs(tabwins) do + local buf = api.nvim_win_get_buf(win) + local bf = fn.getbufvar(buf, '&filetype') + + if fn.index(quit_filetypes, bf) == -1 then + should_quit = false + end + end + + if should_quit then + vim.cmd("qall") + end + end +}) + +api.nvim_create_autocmd({"VimEnter", "DirChanged"}, { + group = api.nvim_create_augroup("git_repo_check", { clear = true }), + pattern = "*", + desc = "check if we are inside Git repo", + command = "call utils#Inside_git_repo()" +}) + +-- ref: https://vi.stackexchange.com/a/169/15292 +api.nvim_create_autocmd("BufReadPre", { + group = api.nvim_create_augroup("large_file", { clear = true }), + pattern = "*", + desc = "check if we are inside Git repo", + callback = function (ev) + local file_size_limit =524288 -- 0.5MB + local f = ev.file + + if fn.getfsize(f) > file_size_limit or fn.getfsize(f) == -2 then + vim.o.eventignore = "all" + -- turning off relative number helps a lot + vim.bo.relativenumber = false + + vim.bo.swapfile = false + vim.bo.bufhidden = "unload" + vim.bo.undolevels = -1 + end + end +}) diff --git a/viml_conf/autocommands.vim b/viml_conf/autocommands.vim deleted file mode 100644 index 3b4ec10..0000000 --- a/viml_conf/autocommands.vim +++ /dev/null @@ -1,121 +0,0 @@ -" Do not use smart case in command line mode, extracted from https://vi.stackexchange.com/a/16511/15292. -augroup dynamic_smartcase - autocmd! - autocmd CmdLineEnter : set nosmartcase - autocmd CmdLineLeave : set smartcase -augroup END - -augroup term_settings - autocmd! - " Do not use number and relative number for terminal inside nvim - autocmd TermOpen * setlocal norelativenumber nonumber - " Go to insert mode by default to start typing command - autocmd TermOpen * startinsert -augroup END - -" More accurate syntax highlighting? (see `:h syn-sync`) -augroup accurate_syn_highlight - autocmd! - autocmd BufEnter * :syntax sync fromstart -augroup END - -" Return to last cursor position when opening a file -augroup resume_cursor_position - autocmd! - autocmd BufReadPost * call s:resume_cursor_position() -augroup END - -" Only resume last cursor position when there is no go-to-line command (something like '+23'). -function s:resume_cursor_position() abort - if line("'\"") > 1 && line("'\"") <= line("$") && &ft !~# 'commit' - let l:args = v:argv " command line arguments - for l:cur_arg in l:args - " Check if a go-to-line command is given. - let idx = match(l:cur_arg, '\v^\+(\d){1,}$') - if idx != -1 - return - endif - endfor - - execute "normal! g`\"zvzz" - endif -endfunction - -augroup numbertoggle - autocmd! - autocmd BufEnter,FocusGained,InsertLeave,WinEnter * if &nu | set rnu | endif - autocmd BufLeave,FocusLost,InsertEnter,WinLeave * if &nu | set nornu | endif -augroup END - -" Define or override some highlight groups -augroup custom_highlight - autocmd! - autocmd ColorScheme * call s:custom_highlight() -augroup END - -function! s:custom_highlight() abort - " For yank highlight - highlight YankColor ctermfg=59 ctermbg=41 guifg=#34495E guibg=#2ECC71 - - " For cursor colors - highlight Cursor cterm=bold gui=bold guibg=#00c918 guifg=black - highlight Cursor2 guifg=red guibg=red - - " For floating windows border highlight - highlight FloatBorder guifg=LightGreen guibg=NONE - - " highlight for matching parentheses - highlight MatchParen cterm=bold,underline gui=bold,underline -endfunction - -augroup auto_close_win - autocmd! - autocmd BufEnter * call s:quit_current_win() -augroup END - -" Quit Nvim if we have only one window, and its filetype match our pattern. -function! s:quit_current_win() abort - let l:quit_filetypes = ['qf', 'vista', 'NvimTree'] - - let l:should_quit = v:true - - let l:tabwins = nvim_tabpage_list_wins(0) - for w in l:tabwins - let l:buf = nvim_win_get_buf(w) - let l:bf = getbufvar(l:buf, '&filetype') - - if index(l:quit_filetypes, l:bf) == -1 - let l:should_quit = v:false - endif - endfor - - if l:should_quit - qall - endif -endfunction - -augroup git_repo_check - autocmd! - autocmd VimEnter,DirChanged * call utils#Inside_git_repo() -augroup END - -" ref: https://vi.stackexchange.com/a/169/15292 -function! s:handle_large_file() abort - let g:file_size_limit = 524288 " 0.5MB - let f = expand("") - - if getfsize(f) > g:file_size_limit || getfsize(f) == -2 - setlocal eventignore=all - " turning off relative number helps a lot - setlocal norelativenumber - setlocal noswapfile bufhidden=unload undolevels=-1 - endif -endfunction - -augroup LargeFile - autocmd! - autocmd BufReadPre * call s:handle_large_file() -augroup END - -" Load auto-command defined in Lua -lua require("custom-autocmd")