mirror of
https://github.com/neovim/neovim.git
synced 2024-09-17 20:58:20 -04:00
Merge 7acdf7be85
into 3b54adc6c6
This commit is contained in:
commit
4808220955
@ -2809,7 +2809,7 @@ vim.keymap.del({modes}, {lhs}, {opts}) *vim.keymap.del()*
|
||||
See also: ~
|
||||
• |vim.keymap.set()|
|
||||
|
||||
vim.keymap.set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
|
||||
vim.keymap.set({mode}, {lhs}, {opts}, {rhs}) *vim.keymap.set()*
|
||||
Adds a new |mapping|. Examples: >lua
|
||||
-- Map to a Lua function:
|
||||
vim.keymap.set('n', 'lhs', function() print("real lua function") end)
|
||||
@ -2828,9 +2828,9 @@ vim.keymap.set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
|
||||
Parameters: ~
|
||||
• {mode} (`string|string[]`) Mode short-name, see |nvim_set_keymap()|.
|
||||
Can also be list of modes to create mapping on multiple modes.
|
||||
• {lhs} (`string`) Left-hand side |{lhs}| of the mapping.
|
||||
• {rhs} (`string|function`) Right-hand side |{rhs}| of the mapping,
|
||||
can be a Lua function.
|
||||
• {lhs} (`string|string[]`) Left-hand side |{lhs}| of the mapping. Can
|
||||
also be list of strings to map different key sequences to the
|
||||
same rhs.
|
||||
• {opts} (`table?`) Table of |:map-arguments|. Same as
|
||||
|nvim_set_keymap()| {opts}, except:
|
||||
• {replace_keycodes} defaults to `true` if "expr" is `true`.
|
||||
@ -2840,6 +2840,8 @@ vim.keymap.set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
|
||||
`0` or `true` for current buffer.
|
||||
• {remap}? (`boolean`, default: `false`) Make the mapping
|
||||
recursive. Inverse of {noremap}.
|
||||
• {rhs} (`string|function`) Right-hand side |{rhs}| of the mapping,
|
||||
can be a Lua function.
|
||||
|
||||
See also: ~
|
||||
• |nvim_set_keymap()|
|
||||
|
@ -148,7 +148,9 @@ LSP
|
||||
|
||||
LUA
|
||||
|
||||
• TODO
|
||||
• |vim.keymap.set()| now allows passing the rhs as last parameter.
|
||||
• |vim.keymap.set()| now allows passing a table of strings as lhs to map
|
||||
multiple keys to the same rhs.
|
||||
|
||||
OPTIONS
|
||||
|
||||
|
@ -35,27 +35,38 @@ local keymap = {}
|
||||
---
|
||||
---@param mode string|string[] Mode short-name, see |nvim_set_keymap()|.
|
||||
--- Can also be list of modes to create mapping on multiple modes.
|
||||
---@param lhs string Left-hand side |{lhs}| of the mapping.
|
||||
---@param lhs string|string[] Left-hand side |{lhs}| of the mapping.
|
||||
--- Can also be list of strings to map different key sequences to the
|
||||
--- same rhs.
|
||||
---@param opts? vim.keymap.set.Opts
|
||||
---@param rhs string|function Right-hand side |{rhs}| of the mapping, can be a Lua function.
|
||||
---
|
||||
---@param opts? vim.keymap.set.Opts
|
||||
---@see |nvim_set_keymap()|
|
||||
---@see |maparg()|
|
||||
---@see |mapcheck()|
|
||||
---@see |mapset()|
|
||||
function keymap.set(mode, lhs, rhs, opts)
|
||||
---@overload fun(mode: string|string[], lhs: string|string[], rhs: string|function, opts: vim.keymap.set.Opts?)
|
||||
function keymap.set(mode, lhs, opts, rhs)
|
||||
vim.validate({
|
||||
mode = { mode, { 's', 't' } },
|
||||
lhs = { lhs, 's' },
|
||||
rhs = { rhs, { 's', 'f' } },
|
||||
opts = { opts, 't', true },
|
||||
lhs = { lhs, { 's', 't' } },
|
||||
opts = { opts, { 't', 'f', 's' }, true },
|
||||
rhs = { rhs, { 's', 'f', 't' }, true },
|
||||
})
|
||||
|
||||
if type(opts) == 'function' or type(opts) == 'string' then
|
||||
rhs, opts = opts, rhs
|
||||
end
|
||||
|
||||
---@type vim.keymap.set.Opts
|
||||
opts = vim.deepcopy(opts or {}, true)
|
||||
|
||||
---@cast mode string[]
|
||||
mode = type(mode) == 'string' and { mode } or mode
|
||||
|
||||
---@cast lhs string[]
|
||||
lhs = type(lhs) == 'string' and { lhs } or lhs
|
||||
|
||||
if opts.expr and opts.replace_keycodes ~= false then
|
||||
opts.replace_keycodes = true
|
||||
end
|
||||
@ -78,12 +89,16 @@ function keymap.set(mode, lhs, rhs, opts)
|
||||
local bufnr = opts.buffer == true and 0 or opts.buffer --[[@as integer]]
|
||||
opts.buffer = nil ---@type integer?
|
||||
for _, m in ipairs(mode) do
|
||||
vim.api.nvim_buf_set_keymap(bufnr, m, lhs, rhs, opts)
|
||||
for _, l in ipairs(lhs) do
|
||||
vim.api.nvim_buf_set_keymap(bufnr, m, l, rhs, opts)
|
||||
end
|
||||
end
|
||||
else
|
||||
opts.buffer = nil
|
||||
for _, m in ipairs(mode) do
|
||||
vim.api.nvim_set_keymap(m, lhs, rhs, opts)
|
||||
for _, l in ipairs(lhs) do
|
||||
vim.api.nvim_set_keymap(m, l, rhs, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -4058,6 +4058,37 @@ describe('vim.keymap', function()
|
||||
eq(1, exec_lua [[return GlobalCount]])
|
||||
end)
|
||||
|
||||
it('can make a mapping with multiple lhs at once', function()
|
||||
eq(
|
||||
0,
|
||||
exec_lua [[
|
||||
GlobalCount = 0
|
||||
vim.keymap.set('n', { 'asdf', 'ghjk' }, function() GlobalCount = GlobalCount + 1 end)
|
||||
return GlobalCount
|
||||
]]
|
||||
)
|
||||
|
||||
feed('asdf\n')
|
||||
feed('ghjk\n')
|
||||
|
||||
eq(2, exec_lua [[return GlobalCount]])
|
||||
end)
|
||||
|
||||
it('can make a mapping with rhs as last parameter', function()
|
||||
eq(
|
||||
0,
|
||||
exec_lua [[
|
||||
GlobalCount = 0
|
||||
vim.keymap.set('n', 'asdf',, {}, function() GlobalCount = GlobalCount + 1 end)
|
||||
return GlobalCount
|
||||
]]
|
||||
)
|
||||
|
||||
feed('asdf\n')
|
||||
|
||||
eq(1, exec_lua [[return GlobalCount]])
|
||||
end)
|
||||
|
||||
it('can make an expr mapping', function()
|
||||
exec_lua [[
|
||||
vim.keymap.set('n', 'aa', function() return '<Insert>π<C-V><M-π>foo<lt><Esc>' end, {expr = true})
|
||||
|
Loading…
Reference in New Issue
Block a user