Closed mawkler closed 8 months ago
You might be able to use something like this:
--- Checks if `fn` is a callback, or a "mode callback" (meaning it uses `self`)
--- @param fn function
--- @return boolean
local function is_mode_callback(fn)
local info = debug.getinfo(fn, 'Su')
return
info.nparams == 1 -- takes one parameter
and info.what == 'Lua' -- is a lua function
and not info.source:find '^@vim' -- is not a builtin
end
Unfortunately, this kind of thing seems prevalent when it comes to callbacks. For example:
-- Template from `:h nvim_create_autocmd`
vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, {
pattern = {"*.c", "*.h"},
callback = vim.cmd.undo, -- ← this also doesn't work
})
I could provide a helper, e.g.
function libmodal.mode.fn(f, ...)
local args = { ... }
return function()
f(unpack(args))
end
end
And then the example becomes:
local fn = libmodal.mode.fn
local options = {
mode_keymaps = {
n = function() vim.notify('foo') end,
u = fn(vim.cmd.undo),
e = fn(vim.cmd.edit, 'foo'), -- you can also pass args
}
}
Ok thank you for your response. I guess that I could expose my own fn
and tell users to use that 🙂
I've merged #36 which adds libmodal.mode.map
, a namespace including fn
(the utility we discussed) and also switch
(the utility implemented previously, just soft-moved to create emphasis that it can only be used as a mapping).
I'll close this for now, but if anything else comes up let me know :)
Hi again! I'm trying to expose the keymaps table to the consumers of my plugin. Here's a simplified example:
The problem is the line
u = vim.cmd.undo
. Since libmodal passes the current mode as an agrument, that argument gets passed tovim.cmd.undo
, causing the following error:I could solve this by requiring my users to wrap each callback in a
function
like this:But it feels like an ugly solution. I would prefer if I could abstract away libmodal. So I tried creating a function that iterates over all the mappings and wraps them with a function:
This solves the agove problem. However, I also want to use a mapping to switch mode with
libmodal.mode.switch
. That means that theMode
type argument passed toswitch
es returned function gets lost.Do you have any suggestions as to how I could solve this? Or is there any way for libmodal to determine whether it should or shouldn't pass an argument to the callback when calling it?