# HG changeset patch # User Meredith Howard # Date 1678517483 18000 # Node ID 3b88450bda157a029771132ba685eff0345cb4a3 # Parent 91b42a87c3d92e16ea32199b1fb61ac2aabee88c port custom autocmds to lua diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua --- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -4,6 +4,8 @@ require("config.options") require("lazy-bootstrap") require("config.maps") require("config.lsp") +require("config.autocmds") + require("local.tig").setup() -- >> Builtin diff --git a/.config/nvim/lua/config/autocmds.lua b/.config/nvim/lua/config/autocmds.lua new file mode 100644 --- /dev/null +++ b/.config/nvim/lua/config/autocmds.lua @@ -0,0 +1,76 @@ +local cmd = vim.cmd +local fn = vim.fn + +local vimrc = vim.api.nvim_create_augroup("vimrc", { clear = true }) + +local function autocmd(event, pattern, opts) + vim.api.nvim_create_autocmd(event, + vim.tbl_extend("keep", opts, { + group = vimrc, + pattern = pattern, + }) + ) +end + +local function cb(func) + return { callback = function(_) func() end } +end + +-- >> neovim specific +-- Always start terminals in insert/terminal mode +autocmd("TermOpen", "*", cb(cmd.startinsert)) + +-- neovim's autoread doesn't do this by default. +autocmd("FocusGained", "*", cb(cmd.checktime)) + +-- >> autowriteall improvment +-- Stopinsert on leave, or autowriteall doesn't work. +autocmd({"WinLeave", "FocusLost"}, "*", { callback = function(_) + if not fn.pumvisible() then + fn.stopinsert() + end +end }) + +-- write all on leave +autocmd("FocusLost", "*", cb(cmd.wa)) + + +-- >> auto mkpath on write +autocmd("BufWritePre", "*", { callback = function(ctx) + if vim.bo[ctx.buf].buftype == "" + and not string.match(ctx.file, "^[%w]+:") + then + fn.mkdir(fn.fnamemodify(ctx.file, ":p:h"), "p") + end +end }) + +-- >> auto session ? + +-- >> jump to last position on open +autocmd("BufReadPost", "*", { callback = function(_) + local ft = vim.bo.filetype + if ft == "mail" or string.match(ft, "^git") or string.match(ft, "^hg") then + return '' + end + + local lastpos = fn.line([['"]]) + if lastpos >= 1 and lastpos <= fn.line("$") then + vim.cmd([[normal! g`"]]) + end +end }) + +-- >> simple highlight conflict markers +autocmd("BufReadPost", "*", { callback = function(_) + fn.matchadd("Error", [[^\([<>|]\)\{7} \@=\|^=\{7}$]]) +end }) + +-- >> nicer quickfix +autocmd("BufReadPost", "quickfix", { callback = function(ctx) + -- simplify noisy :ltag output + if string.match(vim.w.quickfix_title, "^ltag") then + fn.matchadd("Conceal", [[|\zs\^\\V\|\\$|.*]]) + end + + -- easy close + vim.keymap.set("n", "q", "q", {buffer = true}) +end }) diff --git a/.config/nvim/plugin/vimrc/autocmds.vim b/.config/nvim/plugin/vimrc/autocmds.vim --- a/.config/nvim/plugin/vimrc/autocmds.vim +++ b/.config/nvim/plugin/vimrc/autocmds.vim @@ -1,49 +1,3 @@ -augroup vimrc - autocmd! - - autocmd TermOpen * startinsert - - autocmd WinLeave,FocusLost * if !pumvisible() | stopinsert | endif - - " complement to autoread? - autocmd FocusGained * silent! checktime - - " complement to autowriteall - autocmd FocusLost * silent! wa - - " Make paths when writing, as necessary - autocmd BufWritePre * :call vimrc#MkNonExDir(expand(''), +expand('')) - - if ! &diff - " set and load a session based on servername - autocmd VimEnter * nested call vimrc#AutoSessionCheck() - - " Jump to last known pos - autocmd BufReadPost * - \ if &filetype !~# 'mail\|^git\|^hg' && line("'\"") >= 1 && line("'\"") <= line("$") | - \ exe "normal! g`\"" | - \ endif - - " Simple highlight conflict markers - autocmd BufReadPost * - \ match Error "^\([<>|]\)\{7} \@=\|^=\{7}$" - endif - - " Simplify noisy ltag output - autocmd BufReadPost quickfix - \ if w:quickfix_title =~# '^:ltag' | - \ setl modifiable | - \ silent exe ':%s/\^\\V\s*\|\\\$|.*//g' | - \ setl nomodifiable | - \ endif - - " easy close quickfix - autocmd BufReadPost quickfix nmap q c - - " Neomutt changed their tmpfile pattern, ugh - autocmd BufNewFile,BufRead neomutt-*-\w\+ setf mail -augroup END - " https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/ " hacked to work with vimwiki augroup encrypted