Sat, 11 Mar 2023 21:38:57 -0600
Start porting custom commands to lua
--- a/.config/nvim/autoload/vimrc.vim +++ b/.config/nvim/autoload/vimrc.vim @@ -1,7 +1,3 @@ -func! vimrc#CommandAlias(abbrev, expand) abort - execute printf('cnoreabbrev <expr> %s (getcmdtype()==":" && getcmdline()=="%s") ? "%s" : "%s"', a:abbrev, a:abbrev, a:expand, a:abbrev) -endfunc - func! vimrc#AutoFmtToggle() abort if &formatoptions =~# 'a' setl formatoptions-=a | echo '-a' @@ -22,22 +18,6 @@ func! vimrc#Grep(...) abort cfirst endfunc -func! vimrc#Gcd() abort - let root = system('git rev-parse --show-toplevel 2>/dev/null')[:-2] - if ! v:shell_error - exec 'cd ' . root - endif - pwd -endfunc - -func! vimrc#Hgcd() abort - let root = system('hg root 2>/dev/null')[:-2] - if ! v:shell_error - exec 'cd ' . root - endif - pwd -endfunc - func! vimrc#SafeFilterFile(cmd) let errors = tempname() try
--- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -5,6 +5,7 @@ require("lazy-bootstrap") require("config.maps") require("config.lsp") require("config.autocmds") +require("config.commands") require("local.tig").setup()
--- a/.config/nvim/lua/config/autocmds.lua +++ b/.config/nvim/lua/config/autocmds.lua @@ -1,48 +1,29 @@ +local autocmd = require("config.util").autocmd 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 +local g = vim.api.nvim_create_augroup("vimrc", { clear = true }) -- >> neovim specific -- Always start terminals in insert/terminal mode -autocmd("TermOpen", "*", cb(cmd.startinsert)) +autocmd(g, "TermOpen", "*", cmd.startinsert) -- neovim's autoread doesn't do this by default. -autocmd("FocusGained", "*", cb(cmd.checktime)) +autocmd(g, "FocusGained", "*", 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, -}) +autocmd(g, { "WinLeave", "FocusLost" }, "*", function() + if not fn.pumvisible() then + fn.stopinsert() + end +end) -- write all on leave -autocmd("FocusLost", "*", cb(cmd.wa)) +autocmd(g, "FocusLost", "*", cmd.wa) -- >> auto mkpath on write -autocmd("BufWritePre", "*", { +autocmd(g, "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") @@ -53,29 +34,25 @@ autocmd("BufWritePre", "*", { -- >> 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 +autocmd(g, "BufReadPost", "*", 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, -}) + 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", [[\m^\([<>|]\)\{7} \@=\|^=\{7}$]]) - end, -}) +autocmd(g, "BufReadPost", "*", function() + fn.matchadd("Error", [[\m^\([<>|]\)\{7} \@=\|^=\{7}$]]) +end) -- >> nicer quickfix -autocmd("BufReadPost", "quickfix", { +autocmd(g, "BufReadPost", "quickfix", { callback = function(ctx) -- simplify noisy :ltag output if string.match(vim.w.quickfix_title, "^ltag") then
new file mode 100644 --- /dev/null +++ b/.config/nvim/lua/config/commands.lua @@ -0,0 +1,32 @@ +local command = vim.api.nvim_create_user_command +local calias = require("config.util").calias +local cmd = vim.cmd +local fn = vim.fn + +command("Hgcd", function() + local root = fn.systemlist("hg root 2>/dev/null")[1] + if vim.v.shell_error == 0 then + cmd.cd(root) + end +end, {}) + +command("Gcd", function() + local root = fn.systemlist("git rev-parse --show-toplevel 2>/dev/null")[1] + if vim.v.shell_error == 0 then + cmd.cd(root) + end +end, {}) + +calias("Q", "q") +calias("Qa", "qa") +calias("W", "w") +calias("grep", "Grep") + +calias("gcd", "Gcd") +calias("hgcd", "Hgcd") + +-- Switch these to default to stay in one window + buffer +calias("doc", "ViewDoc!") +calias("help", "ViewDocHelp!") +calias("man", "ViewDocMan!") +calias("perldoc", "ViewDocPerl!")
new file mode 100644 --- /dev/null +++ b/.config/nvim/lua/config/util.lua @@ -0,0 +1,32 @@ +local M = {} + +function M.calias(abbrev, expand) + vim.cmd.cnoreabbrev( + string.format( + [[<expr> %s (getcmdtype() == ":" && getcmdline() == "%s") ? "%s" : "%s"]], + abbrev, + abbrev, + expand, + abbrev + ) + ) +end + +function M.autocmd(group, event, pattern, opts) + if type(opts) == "function" then + local func = opts + opts = { callback = function (_) + func() + end } + end + + vim.api.nvim_create_autocmd( + event, + vim.tbl_extend("keep", opts, { + group = group, + pattern = pattern, + }) + ) +end + +return M
--- a/.config/nvim/plugin/vimrc/commands.vim +++ b/.config/nvim/plugin/vimrc/commands.vim @@ -1,8 +1,3 @@ -command! -nargs=+ CAlias call vimrc#CommandAlias(<f-args>) - -command! Gcd call vimrc#Gcd() -command! Hgcd call vimrc#Hgcd() - command! SyntaxCompleteOn setl omnifunc=syntaxcomplete#Complete command! Mksession execute "mksession! " . v:this_session @@ -13,19 +8,3 @@ command! MailPreview enew | set bt=n command! MailPreviewHTML enew | set bt=nofile | setf html | 0r # | exe 'norm! 0O```<Esc>}O```' | silent exe '%!mutt-md2html' | 0 command! -nargs=* -complete=file Grep call vimrc#Grep(<f-args>) -CAlias Ag Grep -CAlias grep Grep - -CAlias Q q -CAlias Qa qa -CAlias W w - -CAlias gcd Gcd -CAlias hgcd Hgcd - -" make these default to one window/buffer too -CAlias doc ViewDoc! -CAlias help ViewDocHelp! -CAlias man ViewDocMan! -CAlias perldoc ViewDocPerl! -