1
+
local keymap = {}
2
+
3
+
--- Add a new |mapping|.
4
+
--- Examples:
5
+
--- <pre>
6
+
--- -- Can add mapping to Lua functions
7
+
--- vim.keymap.set('n', 'lhs', function() print("real lua function") end)
8
+
---
9
+
--- -- Can use it to map multiple modes
10
+
--- vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, { buffer=true })
11
+
---
12
+
--- -- Can add mapping for specific buffer
13
+
--- vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", { silent = true, buffer = 5 })
14
+
---
15
+
--- -- Expr mappings
16
+
--- vim.keymap.set('i', '<Tab>', function()
17
+
--- return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
18
+
--- end, { expr = true })
19
+
--- -- <Plug> mappings
20
+
--- vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)')
21
+
--- </pre>
22
+
---
23
+
--- Note that in a mapping like:
24
+
--- <pre>
25
+
--- vim.keymap.set('n', 'asdf', require('jkl').my_fun)
26
+
--- </pre>
27
+
---
28
+
--- the require('jkl') gets evaluated during this call in order to access the function. If you want to
29
+
--- avoid this cost at startup you can wrap it in a function, for example:
30
+
--- <pre>
31
+
--- vim.keymap.set('n', 'asdf', function() return require('jkl').my_fun() end)
32
+
--- </pre>
33
+
---
34
+
---@param mode string|table Same mode short names as |nvim_set_keymap()|.
35
+
--- Can also be list of modes to create mapping on multiple modes.
36
+
---@param lhs string Left-hand side |{lhs}| of the mapping.
37
+
---@param rhs string|function Right-hand side |{rhs}| of the mapping. Can also be a Lua function.
38
+
--
39
+
---@param opts table A table of |:map-arguments| such as "silent". In addition to the options
40
+
--- listed in |nvim_set_keymap()|, this table also accepts the following keys:
41
+
--- - replace_keycodes: (boolean, default true) When both this and expr is "true",
42
+
--- |nvim_replace_termcodes()| is applied to the result of Lua expr maps.
43
+
--- - remap: (boolean) Make the mapping recursive. This is the
44
+
--- inverse of the "noremap" option from |nvim_set_keymap()|.
45
+
--- Default `true` if `lhs` is a string starting with `<plug>` (case-insensitive), `false` otherwise.
46
+
---@see |nvim_set_keymap()|
47
+
function keymap.set(mode, lhs, rhs, opts)
48
+
vim.validate {
49
+
mode = {mode, {'s', 't'}},
50
+
lhs = {lhs, 's'},
51
+
rhs = {rhs, {'s', 'f'}},
52
+
opts = {opts, 't', true}
53
+
}
54
+
55
+
opts = vim.deepcopy(opts) or {}
56
+
local is_rhs_luaref = type(rhs) == "function"
57
+
mode = type(mode) == 'string' and {mode} or mode
58
+
59
+
if is_rhs_luaref and opts.expr and opts.replace_keycodes ~= false then
60
+
local user_rhs = rhs
61
+
rhs = function ()
62
+
return vim.api.nvim_replace_termcodes(user_rhs(), true, true, true)
63
+
end
64
+
end
65
+
-- clear replace_keycodes from opts table
66
+
opts.replace_keycodes = nil
67
+
68
+
if opts.remap == nil then
69
+
-- remap by default on <plug> mappings and don't otherwise.
70
+
opts.noremap = is_rhs_luaref or rhs:lower():match("^<plug>") == nil
71
+
else
72
+
-- remaps behavior is opposite of noremap option.
73
+
opts.noremap = not opts.remap
74
+
opts.remap = nil
75
+
end
76
+
77
+
if is_rhs_luaref then
78
+
opts.callback = rhs
79
+
rhs = ''
80
+
end
81
+
82
+
if opts.buffer then
83
+
local bufnr = opts.buffer == true and 0 or opts.buffer
84
+
opts.buffer = nil
85
+
for _, m in ipairs(mode) do
86
+
vim.api.nvim_buf_set_keymap(bufnr, m, lhs, rhs, opts)
87
+
end
88
+
else
89
+
opts.buffer = nil
90
+
for _, m in ipairs(mode) do
91
+
vim.api.nvim_set_keymap(m, lhs, rhs, opts)
92
+
end
93
+
end
94
+
end
95
+
96
+
--- Remove an existing mapping.
97
+
--- Examples:
98
+
--- <pre>
99
+
--- vim.keymap.del('n', 'lhs')
100
+
---
101
+
--- vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buffer = 5 })
102
+
--- </pre>
103
+
---@param opts table A table of optional arguments:
104
+
--- - buffer: (number or boolean) Remove a mapping from the given buffer.
105
+
--- When "true" or 0, use the current buffer.
106
+
---@see |vim.keymap.set()|
107
+
---
108
+
function keymap.del(modes, lhs, opts)
109
+
vim.validate {
110
+
mode = {modes, {'s', 't'}},
111
+
lhs = {lhs, 's'},
112
+
opts = {opts, 't', true}
113
+
}
114
+
115
+
opts = opts or {}
116
+
modes = type(modes) == 'string' and {modes} or modes
117
+
118
+
local buffer = false
119
+
if opts.buffer ~= nil then
120
+
buffer = opts.buffer == true and 0 or opts.buffer
121
+
opts.buffer = nil
122
+
end
123
+
124
+
if buffer == false then
125
+
for _, mode in ipairs(modes) do
126
+
vim.api.nvim_del_keymap(mode, lhs)
127
+
end
128
+
else
129
+
for _, mode in ipairs(modes) do
130
+
vim.api.nvim_buf_del_keymap(buffer, mode, lhs)
131
+
end
132
+
end
133
+
end
134
+
135
+
return keymap
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4