1
1
local M = {}
2
2
3
- --- Adds count and register information to the key sequence
4
- --- @param key_sequence string The key sequence to modify
5
- --- @return string Modified key sequence with count and register
6
- local function add_count_and_registers (key_sequence )
7
- local modified_keys = vim .api .nvim_replace_termcodes (key_sequence , true , false , true )
8
-
9
- if vim .v .register ~= nil then
10
- modified_keys = ' "' .. vim .v .register .. modified_keys
11
- end
12
-
13
- if vim .v .count > 1 then
14
- modified_keys = vim .v .count .. modified_keys
15
- end
16
-
17
- return modified_keys
18
- end
19
-
20
3
--- Executes a command multiple times based on count
21
4
--- @param command string | function The command to execute
22
5
local function execute_with_count (command )
@@ -29,62 +12,37 @@ local function execute_with_count(command)
29
12
end
30
13
end
31
14
32
- --- Executes the original callback mapping
33
- --- @param callback function The callback to execute
34
- --- @param lhs string The left-hand side mapping
35
- --- @param rhs string The right-hand side mapping
36
- --- @param mapping_mode string The mapping mode
37
- --- @param original_mapping table | string The original mapping details
38
- --- @param command string | function | nil Additional command to execute
39
- local function execute_callback_mapping (callback , lhs , rhs , mapping_mode , original_mapping , command )
40
- execute_with_count (callback )
41
-
42
- -- Check if mapping changed and rehijack if necessary
43
- local current_mapping = vim .fn .maparg (lhs , mapping_mode , false , true )
44
- if not vim .deep_equal (current_mapping , original_mapping .callback ) then
45
- M .hijack (mapping_mode , lhs , rhs , original_mapping , command )
46
- end
47
- end
48
-
49
- --- Creates the mapping execution function
50
- --- @param lhs string The left-hand side mapping
51
- --- @param rhs string The right-hand side mapping
52
- --- @param mode string The mode of the mapping
53
- --- @param original_mapping table The original mapping details
54
- --- @return function The mapping execution function
55
- local function create_mapping_executor (lhs , rhs , mode , original_mapping , command )
56
- return function ()
57
- if rhs then
58
- vim .api .nvim_feedkeys (add_count_and_registers (rhs ), mode , false )
59
- elseif lhs then
60
- vim .api .nvim_feedkeys (add_count_and_registers (lhs ), mode , false )
61
- end
62
-
63
- -- Execute additional command if provided
64
- if command then
65
- execute_with_count (command )
66
- end
67
- end
68
- end
69
-
70
15
--- Hijacks a key mapping with custom behavior
71
16
--- @param mode string The mode to hijack
72
17
--- @param map string The key mapping to hijack
73
18
--- @param original_mapping table The original mapping details
74
19
--- @param command string | function | nil Additional command to execute
75
20
function M .hijack (mode , lhs , rhs , original_mapping , command )
76
- local execute_mapping = create_mapping_executor (lhs , rhs , mode , original_mapping , command )
77
-
78
- -- remove mode whitespaces
79
21
mode = mode :gsub (" %s+" , " " )
80
22
if mode == nil or mode == " " then
81
23
mode = " n"
82
24
end
25
+ local existing_mapping = vim .fn .maparg (lhs , " n" , false , true )
83
26
84
- -- Set up the new mapping
85
- vim .keymap .set (mode , lhs , execute_mapping , {
27
+ vim .api .nvim_set_keymap (mode , lhs , " " , {
86
28
noremap = true ,
87
- silent = true ,
29
+ callback = function ()
30
+ if command then
31
+ execute_with_count (command )
32
+ end
33
+
34
+ if existing_mapping and existing_mapping .callback then
35
+ existing_mapping .callback ()
36
+ elseif existing_mapping and existing_mapping .rhs then
37
+ vim .api .nvim_feedkeys (
38
+ vim .api .nvim_replace_termcodes (existing_mapping .rhs , true , false , true ),
39
+ " n" ,
40
+ true
41
+ )
42
+ else
43
+ vim .api .nvim_feedkeys (vim .api .nvim_replace_termcodes (lhs , true , false , true ), " n" , true )
44
+ end
45
+ end ,
88
46
})
89
47
end
90
48
0 commit comments