Skip to content

Commit 1c3de13

Browse files
committed
refactor(hijack): consolidate mapping logic. Fixes #28
1 parent ec39aa1 commit 1c3de13

File tree

3 files changed

+28
-75
lines changed

3 files changed

+28
-75
lines changed

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,8 @@ require('tiny-glimmer').setup({
100100
-- },
101101
-- settings needs to respect the animation you choose settings
102102
--
103-
-- All "mapping" can be set in 2 ways:
104-
-- 1. A string with the key you want to map
105-
-- Example:
106-
-- paste_mapping = "p"
107-
-- 2. A table with the key you want to map and its actions
108-
-- Example:
109-
-- paste_mapping = {
110-
-- lhs = "p"
111-
-- rhs = "<Plug>(YankyPutAfter)"
112-
-- }
103+
-- All "mapping" needs to have a correct lhs.
104+
-- It will try to automatically use what you already defined before.
113105
yank = {
114106
enabled = true,
115107
default_animation = "fade",
@@ -119,10 +111,10 @@ require('tiny-glimmer').setup({
119111
default_animation = "pulse",
120112

121113
-- Keys to navigate to the next match
122-
next_mapping = "nzzzv",
114+
next_mapping = "n",
123115

124116
-- Keys to navigate to the previous match
125-
prev_mapping = "Nzzzv",
117+
prev_mapping = "N",
126118
},
127119
paste = {
128120
enabled = true,
@@ -413,6 +405,9 @@ You should disable your own `TextYankPost` autocmd that calls `vim.highlight.on_
413405
### Transparent background issues?
414406
Set the `transparency_color` option to your desired background color.
415407

408+
### How to use it with `yanky.nvim` ?
409+
You should add `yanky.nvim` in `tiny-glimmer` dependecies.
410+
416411
## Thanks
417412

418413
- [EmmanuelOga/easing](https://github.com/EmmanuelOga) - Easing function implementations

lua/tiny-glimmer/hijack.lua

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
11
local M = {}
22

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-
203
---Executes a command multiple times based on count
214
---@param command string|function The command to execute
225
local function execute_with_count(command)
@@ -29,62 +12,37 @@ local function execute_with_count(command)
2912
end
3013
end
3114

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-
7015
---Hijacks a key mapping with custom behavior
7116
---@param mode string The mode to hijack
7217
---@param map string The key mapping to hijack
7318
---@param original_mapping table The original mapping details
7419
---@param command string|function|nil Additional command to execute
7520
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
7921
mode = mode:gsub("%s+", "")
8022
if mode == nil or mode == "" then
8123
mode = "n"
8224
end
25+
local existing_mapping = vim.fn.maparg(lhs, "n", false, true)
8326

84-
-- Set up the new mapping
85-
vim.keymap.set(mode, lhs, execute_mapping, {
27+
vim.api.nvim_set_keymap(mode, lhs, "", {
8628
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,
8846
})
8947
end
9048

lua/tiny-glimmer/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ M.config = {
2929
},
3030
},
3131

32-
next_mapping = "nzzzv",
33-
prev_mapping = "Nzzzv",
32+
next_mapping = "n",
33+
prev_mapping = "N",
3434
},
3535
paste = {
3636
enabled = true,

0 commit comments

Comments
 (0)