Skip to content

Commit f738cf6

Browse files
committed
expose require'keymap-amend'.get
1 parent b8bf9d8 commit f738cf6

File tree

3 files changed

+135
-87
lines changed

3 files changed

+135
-87
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
**Neovim v0.7 or higher is required**
44

55
This plugin allows to amend the exisintg keybinding in Neovim. It is done with the
6-
function which is required from the `keymap-amend` module. The signature of this function
6+
function which is required from the `keymap-amend` module. The signature of this function
77
is equal to **vim.keymap.set** function (`:help vim.keymap.set()`) with one exception: the
88
`rhs` parameter should be a function that receives one parameter — a function on call of
99
which the original keymapping will be executed. This function is constructed and passed
10-
automaticaly. You only need to "accept" it in your `rhs` function and call on need.
10+
automaticaly. You only need to "accept" it in your `rhs` function and call on need.
1111

1212
```lua
1313
local keymap = vim.keymap
@@ -21,7 +21,21 @@ end, opts)
2121

2222
You need to watch that the amendment happens after the original keymap is set.
2323

24-
## Instalation
24+
We also provide a helper function for getting the original keymap and a function that
25+
executes the original 'lhs' mapping for use with other methods of creating keymaps
26+
27+
```lua
28+
vim.keymap.get = require('keymap-amend').get
29+
local original = vim.keymap.get(mode, lhs):original()
30+
```
31+
32+
This is equivalent to the `original` parameter in keymap.amend
33+
the get() function also returns other information about the keymapping:
34+
It contains all the field from nvim_get_keymap,
35+
as well as a buffer parameter that is false for global keymaps,
36+
as well as the `original` method to get a callable
37+
38+
## Installation
2539

2640
With [packer](https://github.com/wbthomason/packer.nvim):
2741

@@ -95,5 +109,4 @@ multiple cursors.
95109
This plugin was inspired with [nvim-cmp](https://github.com/hrsh7th/nvim-cmp)
96110
fallback mechanics.
97111

98-
99112
<!-- vim: set tw=90: -->

doc/keymap-amend.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ function and call on need.
1919
original() -- execute the original 'lhs' mapping
2020
end, opts)
2121
<
22+
23+
We also provide a helper function for getting the original keymap and a function that
24+
executes the original 'lhs' mapping for use with other methods of creating keymaps
25+
26+
>
27+
vim.keymap.get = require('keymap-amend').get
28+
local original = vim.keymap.get(mode, lhs):original()
29+
<
30+
31+
This is equivalent to the `original` parameter in keymap.amend
32+
the get() function also returns other information about the keymapping:
33+
It contains all the field from nvim_get_keymap,
34+
as well as a buffer parameter that is false for global keymaps,
35+
as well as the `original` method to get a callable
36+
2237
================================================================================
2338
EXAMPLES
2439
>

lua/keymap-amend.lua

Lines changed: 103 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,105 +4,119 @@ local api = vim.api
44
---@param keys string
55
---@return string
66
local function termcodes(keys)
7-
return api.nvim_replace_termcodes(keys, true, true, true) --[[@as string]]
7+
return api.nvim_replace_termcodes(keys, true, true, true) --[[@as string]]
88
end
99

1010
---Returns if two key sequence are equal or not.
1111
---@param a string
1212
---@param b string
1313
---@return boolean
1414
local function keymap_equals(a, b)
15-
return termcodes(a) == termcodes(b)
15+
return termcodes(a) == termcodes(b)
16+
end
17+
18+
---Returns the function constructed from the passed keymap object on call of
19+
---which the original keymapping will be executed.
20+
---@param map table keymap object
21+
---@return function
22+
local function get_original(map)
23+
return function()
24+
local keys, fmode
25+
if map.expr then
26+
if map.callback then
27+
keys = map.callback()
28+
else
29+
keys = api.nvim_eval(map.rhs)
30+
end
31+
elseif map.callback then
32+
map.callback()
33+
return
34+
else
35+
keys = map.rhs
36+
end
37+
keys = termcodes(keys)
38+
fmode = map.noremap and "in" or "im"
39+
api.nvim_feedkeys(keys, fmode, false)
40+
end
1641
end
1742

1843
---Get map
1944
---@param mode string
2045
---@param lhs string
2146
---@return table
2247
local function get_map(mode, lhs)
23-
for _, map in ipairs(api.nvim_buf_get_keymap(0, mode)) do
24-
if keymap_equals(map.lhs, lhs) then
25-
return {
26-
lhs = map.lhs,
27-
rhs = map.rhs or '',
28-
expr = map.expr == 1,
29-
callback = map.callback,
30-
noremap = map.noremap == 1,
31-
script = map.script == 1,
32-
silent = map.silent == 1,
33-
nowait = map.nowait == 1,
34-
buffer = true,
35-
}
36-
end
37-
end
48+
local res
3849

39-
for _, map in ipairs(api.nvim_get_keymap(mode)) do
40-
if keymap_equals(map.lhs, lhs) then
41-
return {
42-
lhs = map.lhs,
43-
rhs = map.rhs or '',
44-
expr = map.expr == 1,
45-
callback = map.callback,
46-
noremap = map.noremap == 1,
47-
script = map.script == 1,
48-
silent = map.silent == 1,
49-
nowait = map.nowait == 1,
50-
buffer = false,
51-
}
52-
end
53-
end
50+
for _, map in ipairs(api.nvim_buf_get_keymap(0, mode)) do
51+
if keymap_equals(map.lhs, lhs) then
52+
res = {
53+
lhs = map.lhs,
54+
rhs = map.rhs or "",
55+
expr = map.expr == 1,
56+
callback = map.callback,
57+
noremap = map.noremap == 1,
58+
script = map.script == 1,
59+
silent = map.silent == 1,
60+
nowait = map.nowait == 1,
61+
buffer = true,
62+
}
63+
end
64+
end
5465

55-
return {
56-
lhs = lhs,
57-
rhs = lhs,
58-
expr = false,
59-
callback = nil,
60-
noremap = true,
61-
script = false,
62-
silent = true,
63-
nowait = false,
64-
buffer = false,
65-
}
66-
end
66+
if not res then
67+
for _, map in ipairs(api.nvim_get_keymap(mode)) do
68+
if keymap_equals(map.lhs, lhs) then
69+
res = {
70+
lhs = map.lhs,
71+
rhs = map.rhs or "",
72+
expr = map.expr == 1,
73+
callback = map.callback,
74+
noremap = map.noremap == 1,
75+
script = map.script == 1,
76+
silent = map.silent == 1,
77+
nowait = map.nowait == 1,
78+
buffer = false,
79+
}
80+
end
81+
end
82+
end
6783

68-
---Returns the function constructed from the passed keymap object on call of
69-
---which the original keymapping will be executed.
70-
---@param map table keymap object
71-
---@return function
72-
local function get_original(map)
73-
return function()
74-
local keys, fmode
75-
if map.expr then
76-
if map.callback then
77-
keys = map.callback()
78-
else
79-
keys = api.nvim_eval(map.rhs)
80-
end
81-
elseif map.callback then
82-
map.callback()
83-
return
84-
else
85-
keys = map.rhs
86-
end
87-
keys = termcodes(keys)
88-
fmode = map.noremap and 'in' or 'im'
89-
api.nvim_feedkeys(keys, fmode, false)
90-
end
84+
if not res then
85+
res = {
86+
lhs = lhs,
87+
rhs = lhs,
88+
expr = false,
89+
callback = nil,
90+
noremap = true,
91+
script = false,
92+
silent = true,
93+
nowait = false,
94+
buffer = false,
95+
}
96+
end
97+
98+
res.original = get_original
99+
100+
return res
91101
end
92102

93103
---@param mode string
94104
---@param lhs string
95105
---@param rhs string | function
96106
---@param opts? table
97107
local function amend(mode, lhs, rhs, opts)
98-
local map = get_map(mode, lhs)
99-
local original = get_original(map)
100-
opts = opts or {}
101-
opts.desc = table.concat{
102-
'[keymap-amend.nvim', (opts.desc and ': '..opts.desc or ''), '] ',
103-
map.desc or ''
104-
}
105-
vim.keymap.set(mode, lhs, function() rhs(original) end, opts)
108+
local map = get_map(mode, lhs)
109+
local original = map:original()
110+
opts = opts or {}
111+
opts.desc = table.concat({
112+
"[keymap-amend.nvim",
113+
(opts.desc and ": " .. opts.desc or ""),
114+
"] ",
115+
map.desc or "",
116+
})
117+
vim.keymap.set(mode, lhs, function()
118+
rhs(original)
119+
end, opts)
106120
end
107121

108122
---Amend the existing keymap.
@@ -111,13 +125,19 @@ end
111125
---@param rhs string | function
112126
---@param opts? table
113127
local function modes_amend(mode, lhs, rhs, opts)
114-
if type(mode) == 'table' then
115-
for _, m in ipairs(mode) do
116-
amend(m, lhs, rhs, opts)
117-
end
118-
else
119-
amend(mode, lhs, rhs, opts)
120-
end
128+
if type(mode) == "table" then
129+
for _, m in ipairs(mode) do
130+
amend(m, lhs, rhs, opts)
131+
end
132+
else
133+
amend(mode, lhs, rhs, opts)
134+
end
121135
end
122136

123-
return modes_amend
137+
return setmetatable({
138+
get = get_map,
139+
}, {
140+
__call = function(t, ...)
141+
modes_amend(...)
142+
end,
143+
})

0 commit comments

Comments
 (0)