Files
neovim/runtime/lua/vim/keymap.lua
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
4.4 KiB
Lua
Raw Normal View History

2021-12-30 13:30:49 +06:00
local keymap = {}
2024-03-16 17:11:42 +00:00
--- Table of |:map-arguments|.
--- Same as |nvim_set_keymap()| {opts}, except:
--- - {replace_keycodes} defaults to `true` if "expr" is `true`.
--- - {noremap} is not supported; use {remap} instead (see below).
2024-03-16 17:11:42 +00:00
---
--- Also accepts:
--- @class vim.keymap.set.Opts : vim.api.keyset.keymap
--- @inlinedoc
---
--- Creates buffer-local mapping, `0` for current buffer.
--- @field buf? integer
2024-03-16 17:11:42 +00:00
---
--- Make the mapping recursive. Inverse of {noremap}.
--- (Default: `false`)
--- @field remap? boolean
--- Defines a |mapping| of |keycodes| to a function or keycodes.
---
2021-12-30 13:30:49 +06:00
--- Examples:
2023-09-14 08:23:01 -05:00
---
--- ```lua
--- -- Map "x" to a Lua function:
2025-06-18 13:39:35 +02:00
--- vim.keymap.set('n', 'x', function() print('real lua function') end)
--- -- Map "<leader>x" to multiple modes for the current buffer:
--- vim.keymap.set({'n', 'v'}, '<leader>x', vim.lsp.buf.references, { buf = 0 })
--- -- Map <Tab> to an expression (|:map-<expr>|):
2023-09-14 08:23:01 -05:00
--- vim.keymap.set('i', '<Tab>', function()
2025-06-18 13:39:35 +02:00
--- return vim.fn.pumvisible() == 1 and '<C-n>' or '<Tab>'
2023-09-14 08:23:01 -05:00
--- end, { expr = true })
--- -- Map "[%%" to a <Plug> mapping:
2023-09-14 08:23:01 -05:00
--- vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)')
2025-10-25 16:46:25 +02:00
---
--- -- Use `getregionpos(getpos('v'))` to get the "current visual selection":
--- vim.keymap.set('x', 'M', function()
--- local region = vim.fn.getregionpos(vim.fn.getpos('v'), vim.fn.getpos('.'), {
--- type = 'v',
--- exclusive = false,
--- eol = false,
--- })
--- local line1 = region[1][1][2]
--- local line2 = region[#region][1][2]
--- vim.print({ line1, line2 })
--- end)
2023-09-14 08:23:01 -05:00
--- ```
2021-12-30 13:30:49 +06:00
---
---@param modes string|string[] Mode "short-name" (see |nvim_set_keymap()|), or a list thereof.
---@param lhs string Left-hand side |{lhs}| of the mapping.
2022-12-14 19:58:18 +01:00
---@param rhs string|function Right-hand side |{rhs}| of the mapping, can be a Lua function.
2024-03-16 17:11:42 +00:00
---@param opts? vim.keymap.set.Opts
---
2021-12-30 13:30:49 +06:00
---@see |nvim_set_keymap()|
---@see |maparg()|
---@see |mapcheck()|
---@see |mapset()|
function keymap.set(modes, lhs, rhs, opts)
vim.validate('modes', modes, { 'string', 'table' })
vim.validate('lhs', lhs, 'string')
vim.validate('rhs', rhs, { 'string', 'function' })
vim.validate('opts', opts, 'table', true)
2021-12-30 13:30:49 +06:00
2024-01-02 15:47:55 +00:00
opts = vim.deepcopy(opts or {}, true)
2023-09-20 19:03:40 -07:00
---@cast modes string[]
modes = type(modes) == 'string' and { modes } or modes
2021-12-30 13:30:49 +06:00
if opts.expr and opts.replace_keycodes ~= false then
opts.replace_keycodes = true
2021-12-30 13:30:49 +06:00
end
if opts.remap == nil then
-- default remap value is false
opts.noremap = true
2021-12-30 13:30:49 +06:00
else
-- remaps behavior is opposite of noremap option.
opts.noremap = not opts.remap
2023-09-20 19:03:40 -07:00
opts.remap = nil ---@type boolean?
2021-12-30 13:30:49 +06:00
end
if type(rhs) == 'function' then
2021-12-30 13:30:49 +06:00
opts.callback = rhs
rhs = ''
end
local buf = opts.buf
opts.buf = nil
--- @cast opts +{buffer?:integer|boolean}
if opts.buffer ~= nil then
-- TODO(skewb1k): soft-deprecate `buffer` option in 0.13, remove in 0.15.
assert(buf == nil, "Conflict: 'buf' not allowed with 'buffer'")
buf = opts.buffer == true and 0 or opts.buffer --[[@as integer?]]
opts.buffer = nil
end
if buf then
for _, m in ipairs(modes) do
vim.api.nvim_buf_set_keymap(buf, m, lhs, rhs, opts)
2021-12-30 13:30:49 +06:00
end
else
for _, m in ipairs(modes) do
2021-12-30 13:30:49 +06:00
vim.api.nvim_set_keymap(m, lhs, rhs, opts)
end
end
end
2024-03-16 17:11:42 +00:00
--- @class vim.keymap.del.Opts
--- @inlinedoc
---
--- Remove a mapping from the given buffer. `0` for current.
--- @field buf? integer
2024-03-16 17:11:42 +00:00
2021-12-30 13:30:49 +06:00
--- Remove an existing mapping.
--- Examples:
---
2023-09-14 08:23:01 -05:00
--- ```lua
--- vim.keymap.del('n', 'lhs')
---
--- vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buf = 5 })
2023-09-14 08:23:01 -05:00
--- ```
---
2024-02-15 17:16:04 +00:00
---@param modes string|string[]
---@param lhs string
2024-03-16 17:11:42 +00:00
---@param opts? vim.keymap.del.Opts
2021-12-30 13:30:49 +06:00
---@see |vim.keymap.set()|
function keymap.del(modes, lhs, opts)
vim.validate('mode', modes, { 'string', 'table' })
vim.validate('lhs', lhs, 'string')
vim.validate('opts', opts, 'table', true)
2021-12-30 13:30:49 +06:00
opts = opts or {}
modes = type(modes) == 'string' and { modes } or modes
2024-03-16 17:11:42 +00:00
--- @cast modes string[]
2021-12-30 13:30:49 +06:00
local buf = opts.buf
--- @cast opts +{buffer?:integer|boolean}
2021-12-30 13:30:49 +06:00
if opts.buffer ~= nil then
-- TODO(skewb1k): soft-deprecate `buffer` option in 0.13, remove in 0.15.
assert(opts.buf == nil, "Conflict: 'buf' not allowed with 'buffer'")
buf = opts.buffer == true and 0 or opts.buffer --[[@as integer?]]
2021-12-30 13:30:49 +06:00
end
if buf then
2021-12-30 13:30:49 +06:00
for _, mode in ipairs(modes) do
vim.api.nvim_buf_del_keymap(buf, mode, lhs)
2021-12-30 13:30:49 +06:00
end
else
for _, mode in ipairs(modes) do
vim.api.nvim_del_keymap(mode, lhs)
2021-12-30 13:30:49 +06:00
end
end
end
return keymap