@@ -4,105 +4,119 @@ local api = vim.api
44--- @param keys string
55--- @return string
66local 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]]
88end
99
1010--- Returns if two key sequence are equal or not.
1111--- @param a string
1212--- @param b string
1313--- @return boolean
1414local 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
1641end
1742
1843--- Get map
1944--- @param mode string
2045--- @param lhs string
2146--- @return table
2247local 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
91101end
92102
93103--- @param mode string
94104--- @param lhs string
95105--- @param rhs string | function
96106--- @param opts ? table
97107local 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 )
106120end
107121
108122--- Amend the existing keymap.
@@ -111,13 +125,19 @@ end
111125--- @param rhs string | function
112126--- @param opts ? table
113127local 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
121135end
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