From d5cbb8b485054d950ac10bfe4b0cca534fbb557e Mon Sep 17 00:00:00 2001 From: spike Date: Wed, 12 Oct 2022 23:53:51 +0200 Subject: [PATCH] option to passthrough key event to ignored buffer filetypes --- doc/delaytrain.txt | 2 ++ lua/delaytrain/init.lua | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/doc/delaytrain.txt b/doc/delaytrain.txt index 6b19edd..e603cf2 100644 --- a/doc/delaytrain.txt +++ b/doc/delaytrain.txt @@ -45,6 +45,8 @@ following configuration (default settings included): ['nv'] = {'h', 'j', 'k', 'l'}, ['nvi'] = {'', '', '', ''}, }, + 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 diff --git a/lua/delaytrain/init.lua b/lua/delaytrain/init.lua index 7dfe316..b3e26e9 100644 --- a/lua/delaytrain/init.lua +++ b/lua/delaytrain/init.lua @@ -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