|
|
@ -6,6 +6,7 @@ vim.g.delaytrain_grace_period = 1
|
|
|
|
-- Map of keys to their individual current grace period
|
|
|
|
-- Map of keys to their individual current grace period
|
|
|
|
-- This keeps track of how many times a key has been pressed
|
|
|
|
-- This keeps track of how many times a key has been pressed
|
|
|
|
local current_grace_period_intervals = {}
|
|
|
|
local current_grace_period_intervals = {}
|
|
|
|
|
|
|
|
local ignore_filetypes = {}
|
|
|
|
|
|
|
|
|
|
|
|
local keymaps = {
|
|
|
|
local keymaps = {
|
|
|
|
['nv'] = {'h', 'j', 'k', 'l'},
|
|
|
|
['nv'] = {'h', 'j', 'k', 'l'},
|
|
|
@ -17,6 +18,20 @@ local is_enabled = false
|
|
|
|
function M.try_delay_keypress(key)
|
|
|
|
function M.try_delay_keypress(key)
|
|
|
|
current_interval = current_grace_period_intervals[key]
|
|
|
|
current_interval = current_grace_period_intervals[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- ignore user defined patterns
|
|
|
|
|
|
|
|
for _,ign_ft in ipairs(ignore_filetypes) do
|
|
|
|
|
|
|
|
if vim.o.filetype:match(ign_ft) then
|
|
|
|
|
|
|
|
M.send_keypress(key)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Ingore on macro execution
|
|
|
|
|
|
|
|
if vim.fn.reg_executing() ~= "" then
|
|
|
|
|
|
|
|
M.send_keypress(key)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Start a timer on the first keypress to reset the interval
|
|
|
|
-- Start a timer on the first keypress to reset the interval
|
|
|
|
if current_interval == 0 then
|
|
|
|
if current_interval == 0 then
|
|
|
|
vim.loop.new_timer():start(vim.g.delaytrain_delay_ms, 0, function()
|
|
|
|
vim.loop.new_timer():start(vim.g.delaytrain_delay_ms, 0, function()
|
|
|
@ -27,14 +42,17 @@ function M.try_delay_keypress(key)
|
|
|
|
-- Pass the key through only if we haven't reached the grace period
|
|
|
|
-- Pass the key through only if we haven't reached the grace period
|
|
|
|
if current_interval < vim.g.delaytrain_grace_period then
|
|
|
|
if current_interval < vim.g.delaytrain_grace_period then
|
|
|
|
current_grace_period_intervals[key] = current_interval + 1
|
|
|
|
current_grace_period_intervals[key] = current_interval + 1
|
|
|
|
|
|
|
|
M.send_keypress(key)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.send_keypress(key)
|
|
|
|
vim.api.nvim_feedkeys(
|
|
|
|
vim.api.nvim_feedkeys(
|
|
|
|
vim.api.nvim_replace_termcodes(key, true, false, true),
|
|
|
|
vim.api.nvim_replace_termcodes(key, true, false, true),
|
|
|
|
'n',
|
|
|
|
'n',
|
|
|
|
false
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.setup(opts)
|
|
|
|
function M.setup(opts)
|
|
|
|
if opts then
|
|
|
|
if opts then
|
|
|
@ -46,6 +64,8 @@ function M.setup(opts)
|
|
|
|
vim.g.delaytrain_grace_period = opts.grace_period
|
|
|
|
vim.g.delaytrain_grace_period = opts.grace_period
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ignore_filetypes = opts.ignore_filetypes or {}
|
|
|
|
|
|
|
|
|
|
|
|
if opts.keys then
|
|
|
|
if opts.keys then
|
|
|
|
keymaps = opts.keys
|
|
|
|
keymaps = opts.keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|