Files
nvim/lua/core/misc.lua
2025-09-16 00:34:25 -04:00

74 lines
1.9 KiB
Lua

local M = {}
--- vim.notify title
M.appid = "Nvim Config"
--- extend vim.kemap.set
---@param mode string|table mode for the keymap
---@param bind string keymap
---@param cmd function|string command to run
---@param opts vim.keymap.set.Opts? keymap options
function M.map(mode, bind, cmd, opts)
opts = opts or {}
opts["noremap"] = true
opts["silent"] = true
-- attempt to autogenerate a basic description
if not opts["desc"] then
if type(cmd) == "string" then
opts["desc"] = cmd:gsub("<%a+>", "")
elseif type(cmd) == "function" then
-- TODO: find a way to generate a better name
local file_name = vim.fn.fnamemodify(debug.getinfo(cmd, "S").short_src, ":t")
opts["desc"] = "origin@"..file_name
end
end
vim.keymap.set(mode, bind, cmd, opts)
end
--- a small map wrapper to easily create local buffer mappings
---@param mode string|table mode for the keymap
---@param bind string keymap
---@param cmd function|string command to run
---@param opts table? keymap options
function M.map_local(mode, bind, cmd, opts)
opts = opts or {}
opts["buffer"] = 0
M.map(mode, bind, cmd, opts)
end
--- shorten vim.api.nvim_create_autocmd call
M.auto = vim.api.nvim_create_autocmd
--- extend auto group
---@param name string name of the autogroup
---@param opts table? table of options
---@return integer
function M.augroup(name, opts)
opts = opts or {}
return vim.api.nvim_create_augroup(name, opts)
end
--- Make an action lazy. This is mostly useful for keybinds which do a lot and
--- you want to make sure the screen doesn't flash
---@param txt string|function the action
---@return string|function lazified
---@nodiscard
function M.lz(txt)
if type(txt) == "string" then
return "<cmd>se lz<CR>"..txt.."<cmd>se lz!<CR>"
elseif type(txt) == "function" then
return function()
vim.cmd.se("lz")
-- gotta make sure we can always unset lz
pcall(txt)
vim.cmd.se("lz!")
end
else
return txt
end
end
return M