Merge pull request #516 from takase1121/keymap-improvements

add keymap.unbind(), update contextmenu to use keymap.get_binding()
This commit is contained in:
Adam 2021-11-15 09:04:17 -05:00 committed by GitHub
commit 0ab0abec4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 4 deletions

View File

@ -49,7 +49,7 @@ function ContextMenu:register(predicate, items)
local width, height = 0, 0 --precalculate the size of context menu local width, height = 0, 0 --precalculate the size of context menu
for i, item in ipairs(items) do for i, item in ipairs(items) do
if item ~= DIVIDER then if item ~= DIVIDER then
item.info = keymap.reverse_map[item.command] item.info = keymap.get_binding(item.command)
end end
local lw, lh = get_item_size(item) local lw, lh = get_item_size(item)
width = math.max(width, lw) width = math.max(width, lw)

View File

@ -30,7 +30,8 @@ function keymap.add_direct(map)
end end
keymap.map[stroke] = commands keymap.map[stroke] = commands
for _, cmd in ipairs(commands) do for _, cmd in ipairs(commands) do
keymap.reverse_map[cmd] = stroke keymap.reverse_map[cmd] = keymap.reverse_map[cmd] or {}
table.insert(keymap.reverse_map[cmd], stroke)
end end
end end
end end
@ -52,14 +53,39 @@ function keymap.add(map, overwrite)
end end
end end
for _, cmd in ipairs(commands) do for _, cmd in ipairs(commands) do
keymap.reverse_map[cmd] = stroke keymap.reverse_map[cmd] = keymap.reverse_map[cmd] or {}
table.insert(keymap.reverse_map[cmd], stroke)
end end
end end
end end
local function remove_only(tbl, k, v)
for key, values in pairs(tbl) do
if key == k then
if v then
for i, value in ipairs(values) do
if value == v then
table.remove(values, i)
end
end
else
tbl[key] = nil
end
break
end
end
end
function keymap.unbind(key, cmd)
remove_only(keymap.map, key, cmd)
remove_only(keymap.reverse_map, cmd, key)
end
function keymap.get_binding(cmd) function keymap.get_binding(cmd)
return keymap.reverse_map[cmd] return table.unpack(keymap.reverse_map[cmd] or {})
end end