Skip to content

Commit

Permalink
feat: allow manual setup of triggers #30
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 30, 2021
1 parent 01b6676 commit 423a50c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ local defaults = {
},
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "^:", "^ ", "^call ", "^lua " }, -- hide mapping boilerplate
show_help = true, -- show a help message in the command line for using WhichKey
triggers = "auto", -- automatically setup triggers
-- triggers = {"<leader>"} -- or specifiy a list manually

}

---@type Options
Expand Down
16 changes: 16 additions & 0 deletions lua/which-key/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ M.hooked = {}

function M.hook_id(prefix, mode, buf) return mode .. (buf or "") .. prefix end

function M.is_hooked(prefix, mode, buf) return M.hooked[M.hook_id(prefix, mode, buf)] end

function M.hook_del(prefix, mode, buf)
local id = M.hook_id(prefix, mode, buf)
M.hooked[id] = nil
Expand All @@ -236,6 +238,20 @@ function M.hook_del(prefix, mode, buf)
end

function M.hook_add(prefix, mode, buf)
-- Check if we need to create the hook
if type(Config.options.triggers) == "string" and Config.options.triggers ~= "auto" then
if Util.t(prefix) ~= Util.t(Config.options.triggers) then return end
end
if type(Config.options.triggers) == "table" then
local ok = false
for _, trigger in pairs(Config.options.triggers) do
if Util.t(trigger) == Util.t(prefix) then
ok = true
break
end
end
if not ok then return end
end
-- never hook into operator pending mode
-- this is handled differently
if mode == "o" then return end
Expand Down
2 changes: 1 addition & 1 deletion lua/which-key/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function M.execute(prefix, mode, buf)

local function unhook(nodes, nodes_buf)
for _, node in pairs(nodes) do
if node.mapping and node.mapping.group and not node.mapping.cmd then
if Keys.is_hooked(node.mapping.prefix, mode, nodes_buf) then
table.insert(hooks, { node.mapping.prefix, nodes_buf })
Keys.hook_del(node.mapping.prefix, mode, nodes_buf)
end
Expand Down

0 comments on commit 423a50c

Please sign in to comment.