aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/keymap.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/keymap.lua')
-rw-r--r--runtime/lua/vim/keymap.lua57
1 files changed, 34 insertions, 23 deletions
diff --git a/runtime/lua/vim/keymap.lua b/runtime/lua/vim/keymap.lua
index d53b790746..f4c2b507a9 100644
--- a/runtime/lua/vim/keymap.lua
+++ b/runtime/lua/vim/keymap.lua
@@ -25,8 +25,8 @@ local keymap = {}
--- vim.keymap.set('n', 'asdf', require('jkl').my_fun)
--- </pre>
---
---- the require('jkl') gets evaluated during this call in order to access the function. If you want to
---- avoid this cost at startup you can wrap it in a function, for example:
+--- the ``require('jkl')`` gets evaluated during this call in order to access the function.
+--- If you want to avoid this cost at startup you can wrap it in a function, for example:
--- <pre>
--- vim.keymap.set('n', 'asdf', function() return require('jkl').my_fun() end)
--- </pre>
@@ -35,39 +35,51 @@ 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` 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:
+--- - buffer: (number or boolean) Add a mapping to the given buffer. When "true"
+--- or 0, use the current buffer.
--- - replace_keycodes: (boolean, default true) When both this and expr is "true",
--- |nvim_replace_termcodes()| is applied to the result of Lua expr maps.
--- - remap: (boolean) Make the mapping recursive. This is the
--- inverse of the "noremap" option from |nvim_set_keymap()|.
---- Default `true` if `lhs` is a string starting with `<plug>` (case-insensitive), `false` otherwise.
+--- Default `false`.
---@see |nvim_set_keymap()|
function keymap.set(mode, lhs, rhs, opts)
- vim.validate {
- mode = {mode, {'s', 't'}},
- lhs = {lhs, 's'},
- rhs = {rhs, {'s', 'f'}},
- opts = {opts, 't', true}
- }
+ vim.validate({
+ mode = { mode, { 's', 't' } },
+ lhs = { lhs, 's' },
+ rhs = { rhs, { 's', 'f' } },
+ opts = { opts, 't', true },
+ })
opts = vim.deepcopy(opts) or {}
- local is_rhs_luaref = type(rhs) == "function"
- mode = type(mode) == 'string' and {mode} or mode
+ 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)
+ rhs = function()
+ local res = user_rhs()
+ if res == nil 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
opts.replace_keycodes = nil
if opts.remap == nil then
- -- remap by default on <plug> mappings and don't otherwise.
- opts.noremap = is_rhs_luaref or rhs:lower():match("^<plug>") == nil
+ -- default remap value is false
+ opts.noremap = true
else
-- remaps behavior is opposite of noremap option.
opts.noremap = not opts.remap
@@ -106,19 +118,18 @@ end
---@see |vim.keymap.set()|
---
function keymap.del(modes, lhs, opts)
- vim.validate {
- mode = {modes, {'s', 't'}},
- lhs = {lhs, 's'},
- opts = {opts, 't', true}
- }
+ vim.validate({
+ mode = { modes, { 's', 't' } },
+ lhs = { lhs, 's' },
+ opts = { opts, 't', true },
+ })
opts = opts or {}
- modes = type(modes) == 'string' and {modes} or modes
+ modes = type(modes) == 'string' and { modes } or modes
local buffer = false
if opts.buffer ~= nil then
buffer = opts.buffer == true and 0 or opts.buffer
- opts.buffer = nil
end
if buffer == false then