Allow functions in `keymap` (#948)

This allows `keymap.add` to map shortcuts to functions.

If the function returns `false`, the next command is executed (as if the
`predicate` of a `command` failed).
This commit is contained in:
Guldoman 2022-04-26 15:48:59 +02:00 committed by GitHub
parent e572c58f24
commit 0c456eb664
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 2 deletions

View File

@ -1,3 +1,4 @@
local core = require "core"
local command = require "core.command"
local config = require "core.config"
local keymap = {}
@ -42,7 +43,7 @@ function keymap.add(map, overwrite)
if macos then
stroke = stroke:gsub("%f[%a]ctrl%f[%A]", "cmd")
end
if type(commands) == "string" then
if type(commands) == "string" or type(commands) == "function" then
commands = { commands }
end
if overwrite then
@ -103,7 +104,16 @@ function keymap.on_key_pressed(k, ...)
local commands, performed = keymap.map[stroke]
if commands then
for _, cmd in ipairs(commands) do
performed = command.perform(cmd, ...)
if type(cmd) == "function" then
local ok, res = core.try(cmd, ...)
if ok then
performed = not (res == false)
else
performed = true
end
else
performed = command.perform(cmd, ...)
end
if performed then break end
end
return performed