Last active
March 27, 2022 16:15
-
-
Save VonHeikemen/dd2190709302de7ee9943f1b9dfb1933 to your computer and use it in GitHub Desktop.
Allows you to use lua functions with `vim.api.nvim_set_keymap` (for neovim 0.6.1 or lower)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local M = {} | |
local module_name = 'map_utils' | |
local fn_store = {} | |
local function register_fn(fn) | |
table.insert(fn_store, fn) | |
return #fn_store | |
end | |
function M.apply_function(id) | |
fn_store[id]() | |
end | |
function M.apply_expr(id) | |
return vim.api.nvim_replace_termcodes(fn_store[id](), true, true, true) | |
end | |
function M.lua_fn(fn) | |
return string.format( | |
"<cmd>lua require('%s').apply_function(%s)<CR>", | |
module_name, | |
register_fn(fn) | |
) | |
end | |
function M.lua_expr(fn) | |
return string.format( | |
"v:lua.require'%s'.apply_expr(%s)", | |
module_name, | |
register_fn(fn) | |
) | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assuming you save this in
~/.config/nvim/lua/map_utils.lua
, this a basic usage example.