2022-12-23 14:43:29 +00:00
|
|
|
(local { : Gdk } (require :lgi))
|
|
|
|
(local { : view } (require :fennel))
|
2022-12-22 22:42:45 +00:00
|
|
|
|
2022-12-23 14:43:29 +00:00
|
|
|
(fn keychord->spec [keychord]
|
|
|
|
(let [Mod Gdk.ModifierType
|
|
|
|
symbol (keychord:match "(%w+)$")
|
|
|
|
upper? (and (symbol:match "%u") true)
|
|
|
|
modmask (accumulate [m (if upper? Mod.SHIFT_MASK 0)
|
|
|
|
v (keychord:gmatch "(%w+)-")]
|
|
|
|
(match (v:lower)
|
|
|
|
"m" (bor m Mod.MOD1_MASK)
|
|
|
|
"c" (bor m Mod.CONTROL_MASK)
|
|
|
|
"s" (bor m Mod.MOD4_MASK)))]
|
|
|
|
{
|
|
|
|
:keyval (string.byte (symbol:lower))
|
|
|
|
: modmask
|
|
|
|
}))
|
2022-12-22 22:42:45 +00:00
|
|
|
|
2022-12-23 14:43:29 +00:00
|
|
|
(fn spec->index [spec]
|
|
|
|
(string.format "%d:%d" spec.keyval spec.modmask))
|
2022-12-22 22:42:45 +00:00
|
|
|
|
2022-12-23 14:43:29 +00:00
|
|
|
(fn event->index [event]
|
|
|
|
(let [modmask
|
|
|
|
(accumulate [m 0
|
|
|
|
k _ (pairs event.state)]
|
|
|
|
(bor m (. Gdk.ModifierType k)))]
|
|
|
|
(spec->index {:keyval event.keyval : modmask})))
|
2022-12-22 22:42:45 +00:00
|
|
|
|
2022-12-26 16:53:41 +00:00
|
|
|
(fn designates-command? [tbl]
|
|
|
|
;; a keymap entry has a string as key, a command
|
|
|
|
;; definition is a numerically-indexed array
|
|
|
|
(if (. tbl 1) true))
|
2022-12-23 14:43:29 +00:00
|
|
|
|
|
|
|
(fn compile-keymap [input]
|
|
|
|
(collect [k v (pairs input)]
|
|
|
|
(let [f (-> k keychord->spec spec->index)]
|
2022-12-26 16:53:41 +00:00
|
|
|
(if (designates-command? v)
|
|
|
|
(values f v)
|
|
|
|
(values f (compile-keymap v))))))
|
2022-12-23 14:43:29 +00:00
|
|
|
|
|
|
|
(fn recogniser [source-keymap]
|
|
|
|
(let [keymap (compile-keymap source-keymap)]
|
|
|
|
(var m keymap)
|
|
|
|
{
|
|
|
|
:accept-event
|
|
|
|
(fn [_ e]
|
|
|
|
(let [c (event->index e)
|
|
|
|
v (. m c)]
|
2022-12-26 16:53:41 +00:00
|
|
|
(if v
|
|
|
|
(if (designates-command? v)
|
|
|
|
(do
|
2022-12-23 14:43:29 +00:00
|
|
|
(set m keymap)
|
2022-12-26 16:53:41 +00:00
|
|
|
v)
|
|
|
|
(do
|
|
|
|
(set m v)
|
|
|
|
(values nil (.. c " "))))
|
|
|
|
(do
|
|
|
|
(set m keymap)
|
|
|
|
(values nil (.. "No binding for " (view e) " "))))))
|
2022-12-23 14:43:29 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
{ : recogniser
|
|
|
|
:_ {
|
|
|
|
;; symbols in _ are exported only for testing
|
|
|
|
: keychord->spec
|
|
|
|
} }
|