Skip to content

Commit

Permalink
feat: added triggers_blacklist to blacklist certain whichkey hooks #73
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 17, 2021
1 parent bcc8297 commit ec1474b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ WhichKey comes with the following defaults:
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "call", "lua", "^:", "^ "}, -- hide mapping boilerplate
show_help = true, -- show help message on the command line when the popup is visible
triggers = "auto", -- automatically setup triggers
-- triggers = {"<leader>"} -- or specifiy a list manually
-- triggers = {"<leader>"} -- or specify a list manually
triggers_blacklist = {
-- list of mode / prefixes that should never be hooked by WhichKey
-- this is mostly relevant for key maps that start with a native binding
-- most people should not need to change this
i = { "j", "k" },
v = { "j", "k" },
},
}
```

Expand Down
7 changes: 6 additions & 1 deletion lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ local defaults = {
triggers = "auto", -- automatically setup triggers
-- triggers = {"<leader>"} -- or specifiy a list manually
triggers_nowait = {}, -- list of triggers, where WhichKey should not wait for timeoutlen and show immediately
-- triggers_nowait = {"<leader>"} -- shows WK immediately when hitting <leader>
triggers_blacklist = {
-- list of mode / prefixes that should never be hooked by WhichKey
-- this is mostly relevant for keymaps that start with a native binding
i = { "j", "k" },
v = { "j", "k" },
},
}

---@type Options
Expand Down
11 changes: 11 additions & 0 deletions lua/which-key/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local M = {}
M.functions = {}
M.operators = {}
M.nowait = {}
M.blacklist = {}

function M.setup()
local builtin_ops = require("which-key.plugins.presets").operators
Expand All @@ -29,6 +30,12 @@ function M.setup()
end
M.register(mappings, { mode = "n" })
M.register({ i = { name = "inside" }, a = { name = "around" } }, { mode = "v" })
for mode, blacklist in pairs(Config.options.triggers_blacklist) do
for _, prefix in ipairs(blacklist) do
M.blacklist[mode] = M.blacklist[mode] or {}
M.blacklist[mode][prefix] = true
end
end
end

function M.get_operator(prefix)
Expand Down Expand Up @@ -306,6 +313,10 @@ function M.hook_del(prefix, mode, buf)
end

function M.hook_add(prefix, mode, buf, secret_only)
-- check if this trigger is blacklisted
if M.blacklist[mode] and M.blacklist[mode][prefix] then
return
end
-- don't hook to j or k in INSERT mode
if mode == "i" and (prefix == "j" or prefix == "k") then
return
Expand Down

0 comments on commit ec1474b

Please sign in to comment.