normalize config.util.autocmd

Thu, 16 May 2024 14:37:26 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Thu, 16 May 2024 14:37:26 -0500
changeset 1166
34bf03fa07e4
parent 1165
840f0fde07e2
child 1167
f5f84f890974

normalize config.util.autocmd

.config/nvim/lua/config/autocmds.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/lsp.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/util.lua file | annotate | diff | comparison | revisions
.config/nvim/plugin/gpg-encrypted.lua file | annotate | diff | comparison | revisions
--- a/.config/nvim/lua/config/autocmds.lua
+++ b/.config/nvim/lua/config/autocmds.lua
@@ -6,14 +6,14 @@ local g = vim.api.nvim_create_augroup("v
 
 -- >> neovim specific
 -- Always start terminals in insert/terminal mode
-autocmd(g, "TermOpen", "*", cmd.startinsert)
+autocmd(g, "TermOpen", "*", function(_) cmd.startinsert() end)
 
 -- neovim's autoread doesn't do this by default.
-autocmd(g, "FocusGained", "*", cmd.checktime)
+autocmd(g, "FocusGained", "*", function(_) cmd.checktime() end)
 
 -- >> autowriteall improvment
 -- Stopinsert on leave, or autowriteall doesn't work.
-autocmd(g, { "WinLeave", "FocusLost" }, "*", function()
+autocmd(g, { "WinLeave", "FocusLost" }, "*", function(_)
   if fn.pumvisible() == 0 then
     cmd.stopinsert()
   end
@@ -33,7 +33,7 @@ end)
 local nojump = vim.regex([[mail\|commit\|rebase]])
 assert(nojump, "Couldn't compile nojump regexp?")
 
-autocmd(g, "BufReadPost", "*", function()
+autocmd(g, "BufReadPost", "*", function(_)
   if nojump:match_str(vim.bo.filetype or "") then
     return
   end
@@ -45,6 +45,6 @@ autocmd(g, "BufReadPost", "*", function(
 end)
 
 -- >> simple highlight conflict markers
-autocmd(g, "BufReadPost", "*", function()
+autocmd(g, "BufReadPost", "*", function(_)
   fn.matchadd("Error", [[\m^\([<>|]\)\{7} \@=\|^=\{7}$]])
 end)
--- a/.config/nvim/lua/config/lsp.lua
+++ b/.config/nvim/lua/config/lsp.lua
@@ -17,7 +17,7 @@ vim.diagnostic.config({
 vim.api.nvim_create_autocmd("LspAttach", {
   once = true,
   group = "lsp_attach",
-  callback = function()
+  callback = function(_)
     vim.opt.number = true
     vim.opt.updatetime = 250
   end,
@@ -29,7 +29,7 @@ vim.api.nvim_create_autocmd("LspAttach",
     vim.api.nvim_create_autocmd("CursorHold", {
       group = vim.api.nvim_create_augroup("lsp_buf_diags", { clear = true }),
       buffer = args.buf,
-      callback = vim.diagnostic.open_float,
+      callback = function(_) vim.diagnostic.open_float() end,
     })
   end,
 })
--- a/.config/nvim/lua/config/util.lua
+++ b/.config/nvim/lua/config/util.lua
@@ -2,6 +2,8 @@ local M = {}
 
 local api, fn = vim.api, vim.fn
 
+--- Bulk-define aliases.
+---@param aliases table<string, string>
 function M.calias(aliases)
   for abbrev, expand in pairs(aliases) do
     vim.cmd.cnoreabbrev(
@@ -16,14 +18,16 @@ function M.calias(aliases)
   end
 end
 
+--- Shortcut for autocmd creation.
+--- @param group string | number
+--- @param event string | string[]
+--- @param pattern string | string[]
+--- @param opts table | string | fun(ctx) `nvim_create_autocmd` options OR a function for the `callback` option OR a string for the command option
 function M.autocmd(group, event, pattern, opts)
   if type(opts) == "function" then
-    local func = opts
-    opts = {
-      callback = function(_)
-        func()
-      end,
-    }
+    opts = { callback = opts }
+  elseif type(opts) == "string" then
+    opts = { command = opts }
   end
 
   api.nvim_create_autocmd(
--- a/.config/nvim/plugin/gpg-encrypted.lua
+++ b/.config/nvim/plugin/gpg-encrypted.lua
@@ -9,9 +9,7 @@ local function autocmd(event, opts)
   util.autocmd(g, event, filepattern, opts)
 end
 
-autocmd({ "BufReadPre", "FileReadPre" }, {
-  command = [[ setl noswapfile noundofile nobackup shada= ]],
-})
+autocmd({ "BufReadPre", "FileReadPre" }, [[ setl noswapfile noundofile nobackup shada= ]])
 
 autocmd("BufReadPost", function(_)
   safe_filter_file("gpg -d")
@@ -21,4 +19,4 @@ autocmd("BufWritePre", function(_)
   safe_filter_file("gpg -se -a --default-recipient-self")
 end)
 
-autocmd("BufWritePost", { command = [[ silent undo ]] })
+autocmd("BufWritePost", [[ silent undo ]])

mercurial