diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-03-24 13:22:58 +0000 |
---|---|---|
committer | Lewis Russell <lewis6991@gmail.com> | 2022-03-24 13:59:20 +0000 |
commit | 58140a94283b1c6e45099c89e66a0c94e9d90931 (patch) | |
tree | 5784df502607457f6d34c0ce7280873d59948301 /runtime/lua/vim/keymap.lua | |
parent | 3b28bd57f9131a71b17265d18d5e0a7a8cedb84b (diff) | |
download | rneovim-58140a94283b1c6e45099c89e66a0c94e9d90931.tar.gz rneovim-58140a94283b1c6e45099c89e66a0c94e9d90931.tar.bz2 rneovim-58140a94283b1c6e45099c89e66a0c94e9d90931.zip |
feat(keymap): return nil from an expr keymap
For Lua callback expr keymaps, returning `nil` or `false` is equivalent
to an empty string
Diffstat (limited to 'runtime/lua/vim/keymap.lua')
-rw-r--r-- | runtime/lua/vim/keymap.lua | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/runtime/lua/vim/keymap.lua b/runtime/lua/vim/keymap.lua index 1be40b0081..c193a13a7b 100644 --- a/runtime/lua/vim/keymap.lua +++ b/runtime/lua/vim/keymap.lua @@ -35,6 +35,8 @@ local keymap = {} --- Can also be list of modes to create mapping on multiple modes. ---@param lhs string Left-hand side |{lhs}| of the mapping. ---@param rhs string|function Right-hand side |{rhs}| of the mapping. Can also be a Lua function. +--- If a Lua function and `opts.expr == true`, returning `nil` or `false` +--- is equivalent to an empty string. -- ---@param opts table A table of |:map-arguments| such as "silent". In addition to the options --- listed in |nvim_set_keymap()|, this table also accepts the following keys: @@ -56,10 +58,18 @@ function keymap.set(mode, lhs, rhs, opts) local is_rhs_luaref = type(rhs) == "function" mode = type(mode) == 'string' and {mode} or mode - if is_rhs_luaref and opts.expr and opts.replace_keycodes ~= false then + if is_rhs_luaref and opts.expr then local user_rhs = rhs rhs = function () - return vim.api.nvim_replace_termcodes(user_rhs(), true, true, true) + local res = user_rhs() + if not res then + -- TODO(lewis6991): Handle this in C? + return '' + elseif opts.replace_keycodes ~= false then + return vim.api.nvim_replace_termcodes(res, true, true, true) + else + return res + end end end -- clear replace_keycodes from opts table |