option to passthrough key event to ignored buffer filetypes

ignore-filetypes
spike 2 years ago
parent 09ee69b196
commit d5cbb8b485

@ -45,6 +45,8 @@ following configuration (default settings included):
['nv'] = {'h', 'j', 'k', 'l'},
['nvi'] = {'<Left>', '<Down>', '<Up>', '<Right>'},
},
ignore_filestypes = {}, -- Example: set to {"help", "NvimTree"} to
-- disable the plugin for help and NvimTree
}
<
Keep in mind that the `delay_ms` timer starts on the FIRST keypress and not the

@ -6,6 +6,7 @@ vim.g.delaytrain_grace_period = 1
-- Map of keys to their individual current grace period
-- This keeps track of how many times a key has been pressed
local current_grace_period_intervals = {}
local ignore_filetypes = {}
local keymaps = {
['nv'] = {'h', 'j', 'k', 'l'},
@ -14,9 +15,32 @@ local keymaps = {
local is_enabled = false
local function has_val(tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
local function sendkeys(key)
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes(key, true, false, true),
'n',
false
)
end
function M.try_delay_keypress(key)
current_interval = current_grace_period_intervals[key]
if has_val(ignore_filetypes, vim.o.filetype) then
sendkeys(key)
return
end
-- Start a timer on the first keypress to reset the interval
if current_interval == 0 then
vim.loop.new_timer():start(vim.g.delaytrain_delay_ms, 0, function()
@ -27,12 +51,7 @@ function M.try_delay_keypress(key)
-- Pass the key through only if we haven't reached the grace period
if current_interval < vim.g.delaytrain_grace_period then
current_grace_period_intervals[key] = current_interval + 1
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes(key, true, false, true),
'n',
false
)
sendkeys(key)
end
end
@ -46,6 +65,8 @@ function M.setup(opts)
vim.g.delaytrain_grace_period = opts.grace_period
end
ignore_filetypes = vim.tbl_extend("force", ignore_filetypes, opts.ignore_filetypes)
if opts.keys then
keymaps = opts.keys
end

Loading…
Cancel
Save