robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
5.2 KiB172 linesraw
1local keymap = {}
2
3--- Table of |:map-arguments|.
4--- Same as |nvim_set_keymap()| {opts}, except:
5--- - {replace_keycodes} defaults to `true` if "expr" is `true`.
6--- - {noremap} is not supported; use {remap} instead (see below).
7---
8--- Also accepts:
9--- @class vim.keymap.set.Opts : vim.api.keyset.keymap
10--- @inlinedoc
11---
12--- Creates buffer-local mapping, `0` for current buffer.
13--- @field buf? integer
14---
15--- Make the mapping recursive. Inverse of {noremap}.
16--- (Default: `false`)
17--- @field remap? boolean
18
19--- Defines a |mapping| of |keycodes| to a function or keycodes. If `lhs` is a list, defines
20--- a mapping for each (mode, lhs) pair.
21---
22--- Examples:
23---
24--- ```lua
25--- -- Map "x" to a Lua function:
26--- vim.keymap.set('n', 'x', function() print('real lua function') end)
27--- -- Map "<leader>x" to multiple modes for the current buffer:
28--- vim.keymap.set({'n', 'v'}, '<leader>x', vim.lsp.buf.references, { buf = 0 })
29--- -- Map <Tab> to an expression (|:map-<expr>|):
30--- vim.keymap.set('i', '<Tab>', function()
31---   return vim.fn.pumvisible() == 1 and '<C-n>' or '<Tab>'
32--- end, { expr = true })
33--- -- Map "[%%" to a <Plug> mapping:
34--- vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)')
35---
36--- -- Use `getregionpos(getpos('v'))` to get the "current visual selection":
37--- vim.keymap.set('x', 'M', function()
38---   local region = vim.fn.getregionpos(vim.fn.getpos('v'), vim.fn.getpos('.'), {
39---     type = 'v',
40---     exclusive = false,
41---     eol = false,
42---     bounds = true,
43---   })
44---   local line1 = region[1][1][2]
45---   local line2 = region[1][2][2]
46---   vim.print({ line1, line2 })
47--- end)
48---
49--- vim.keymap.set({ 'n', 'i' }, { 'a', 'b' }, '<cmd>echom localtime()<cr>')
50--- -- ... is the same as:
51--- vim.keymap.set('n', 'a', '<cmd>echom localtime()<cr>')
52--- vim.keymap.set('i', 'a', '<cmd>echom localtime()<cr>')
53--- vim.keymap.set('n', 'b', '<cmd>echom localtime()<cr>')
54--- vim.keymap.set('i', 'b', '<cmd>echom localtime()<cr>')
55--- ```
56---
57---@param modes string|string[] Mode "short-name" (see |nvim_set_keymap()|), or a list thereof.
58---@param lhs string|string[]  Left-hand side |{lhs}| of the mapping, or a list thereof.
59---@param rhs string|function  Right-hand side |{rhs}| of the mapping, can be a Lua function.
60---@param opts? vim.keymap.set.Opts
61---
62---@see |nvim_set_keymap()|
63---@see |maparg()|
64---@see |mapcheck()|
65---@see |mapset()|
66function keymap.set(modes, lhs, rhs, opts)
67  vim.validate('modes', modes, { 'string', 'table' })
68  vim.validate('lhs', lhs, { 'string', 'table' })
69  vim.validate('rhs', rhs, { 'string', 'function' })
70  vim.validate('opts', opts, 'table', true)
71
72  opts = vim.deepcopy(opts or {}, true)
73
74  modes = type(modes) == 'string' and { modes } or modes
75  ---@cast modes string[]
76  lhs = type(lhs) == 'string' and { lhs } or lhs
77  ---@cast lhs string[]
78
79  if opts.expr and opts.replace_keycodes ~= false then
80    opts.replace_keycodes = true
81  end
82
83  if opts.remap == nil then
84    -- default remap value is false
85    opts.noremap = true
86  else
87    -- remaps behavior is opposite of noremap option.
88    opts.noremap = not opts.remap
89    opts.remap = nil ---@type boolean?
90  end
91
92  if type(rhs) == 'function' then
93    opts.callback = rhs
94    rhs = ''
95  end
96
97  local buf = opts.buf
98  opts.buf = nil
99  --- @cast opts +{buffer?:integer|boolean}
100  if opts.buffer ~= nil then
101    -- TODO(skewb1k): soft-deprecate `buffer` option in 0.13, remove in 0.15.
102    assert(buf == nil, "Conflict: 'buf' not allowed with 'buffer'")
103    buf = opts.buffer == true and 0 or opts.buffer --[[@as integer?]]
104    opts.buffer = nil
105  end
106
107  for _, m in ipairs(modes) do
108    for _, l in ipairs(lhs) do
109      if buf then
110        vim.api.nvim_buf_set_keymap(buf, m, l, rhs, opts)
111      else
112        vim.api.nvim_set_keymap(m, l, rhs, opts)
113      end
114    end
115  end
116end
117
118--- @class vim.keymap.del.Opts
119--- @inlinedoc
120---
121--- Remove a mapping from the given buffer. `0` for current.
122--- @field buf? integer
123--- When true, only match {lhs}, not {rhs}.
124--- @field lhs? boolean
125
126--- Removes a mapping, or removes each (mode, lhs) pair if `lhs` is a list.
127---
128--- Examples:
129---
130--- ```lua
131--- vim.keymap.del('n', 'lhs')
132--- vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buf = 5 })
133--- ```
134---
135---@param modes string|string[]
136---@param lhs string|string[]
137---@param opts? vim.keymap.del.Opts
138---@see |vim.keymap.set()|
139function keymap.del(modes, lhs, opts)
140  vim.validate('modes', modes, { 'string', 'table' })
141  vim.validate('lhs', lhs, { 'string', 'table' })
142  vim.validate('opts', opts, 'table', true)
143
144  opts = opts or {}
145
146  modes = type(modes) == 'string' and { modes } or modes
147  --- @cast modes string[]
148  lhs = type(lhs) == 'string' and { lhs } or lhs
149  ---@cast lhs string[]
150
151  local buf = opts.buf
152  --- @cast opts +{buffer?:integer|boolean}
153  if opts.buffer ~= nil then
154    -- TODO(skewb1k): soft-deprecate `buffer` option in 0.13, remove in 0.15.
155    assert(opts.buf == nil, "Conflict: 'buf' not allowed with 'buffer'")
156    buf = opts.buffer == true and 0 or opts.buffer --[[@as integer?]]
157  end
158
159  local api_opts = { lhs = opts.lhs }
160
161  for _, m in ipairs(modes) do
162    for _, l in ipairs(lhs) do
163      if buf then
164        vim.api.nvim_buf_del_keymap(buf, m, l, api_opts)
165      else
166        vim.api.nvim_del_keymap(m, l, api_opts)
167      end
168    end
169  end
170end
171
172return keymap