Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add arbitrary key press detection #93

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lua/smear_cursor/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ M.time_interval = 17 -- milliseconds
-- Increase if the cursor makes weird jumps when hitting keys.
M.delay_event_to_smear = 1 -- milliseconds

-- Delay for `vim.on_key` to avoid redundancy with vim events triggers.
M.delay_after_key = 1 -- milliseconds

-- Smear configuration ---------------------------------------------------------

-- How fast the smear's head moves towards the target.
Expand Down
13 changes: 13 additions & 0 deletions lua/smear_cursor/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local latest_mode = nil
local latest_row = nil
local latest_col = nil
local timer = nil
local cursor_namespace = vim.api.nvim_create_namespace("smear_cursor")

local EVENT_TRIGGER = nil
local AFTER_DELAY = 1
Expand All @@ -29,6 +30,7 @@ local function move_cursor(trigger, jump)
timer:stop()
timer:close()
end
timer = nil

if trigger == AFTER_DELAY and mode == latest_mode and row == latest_row and col == latest_col then
if jump then
Expand Down Expand Up @@ -70,6 +72,12 @@ M.jump_cursor = function()
end, 0)
end

local function on_key(key, typed)
vim.defer_fn(function()
if timer == nil then M.move_cursor() end
end, config.delay_after_key)
end

M.listen = function()
vim.api.nvim_exec2(
[[
Expand All @@ -85,6 +93,9 @@ M.listen = function()
{}
)

-- To catch changes that do not trigger events (e.g. opening/closing folds)
vim.on_key(on_key, cursor_namespace)

if #config.filetypes_disabled > 0 then
vim.api.nvim_exec2(
[[
Expand All @@ -107,6 +118,8 @@ M.unlisten = function()
]],
{}
)

vim.on_key(nil, cursor_namespace)
end

return M
Loading