Skip to content

Commit 3bbf014

Browse files
committed
feat: add arbitrary key press detection
1 parent 58c25d6 commit 3bbf014

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lua/smear_cursor/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ M.time_interval = 17 -- milliseconds
5757
-- Increase if the cursor makes weird jumps when hitting keys.
5858
M.delay_event_to_smear = 1 -- milliseconds
5959

60+
-- Delay for `vim.on_key` to avoid redundancy with vim events triggers.
61+
M.delay_after_key = 1 -- milliseconds
62+
6063
-- Smear configuration ---------------------------------------------------------
6164

6265
-- How fast the smear's head moves towards the target.

lua/smear_cursor/events.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local latest_mode = nil
77
local latest_row = nil
88
local latest_col = nil
99
local timer = nil
10+
local cursor_namespace = vim.api.nvim_create_namespace("smear_cursor")
1011

1112
local EVENT_TRIGGER = nil
1213
local AFTER_DELAY = 1
@@ -29,6 +30,7 @@ local function move_cursor(trigger, jump)
2930
timer:stop()
3031
timer:close()
3132
end
33+
timer = nil
3234

3335
if trigger == AFTER_DELAY and mode == latest_mode and row == latest_row and col == latest_col then
3436
if jump then
@@ -70,6 +72,12 @@ M.jump_cursor = function()
7072
end, 0)
7173
end
7274

75+
local function on_key(key, typed)
76+
vim.defer_fn(function()
77+
if timer == nil then M.move_cursor() end
78+
end, config.delay_after_key)
79+
end
80+
7381
M.listen = function()
7482
vim.api.nvim_exec2(
7583
[[
@@ -85,6 +93,9 @@ M.listen = function()
8593
{}
8694
)
8795

96+
-- To catch changes that do not trigger events (e.g. opening/closing folds)
97+
vim.on_key(on_key, cursor_namespace)
98+
8899
if #config.filetypes_disabled > 0 then
89100
vim.api.nvim_exec2(
90101
[[
@@ -107,6 +118,8 @@ M.unlisten = function()
107118
]],
108119
{}
109120
)
121+
122+
vim.on_key(nil, cursor_namespace)
110123
end
111124

112125
return M

0 commit comments

Comments
 (0)