unnest conditionals in keymap recogniser

This commit is contained in:
Daniel Barlow 2022-12-31 23:49:41 +00:00
parent ec391a9e57
commit 39735be891

View File

@ -44,17 +44,20 @@
(bor m (. Gdk.ModifierType k)))] (bor m (. Gdk.ModifierType k)))]
(spec->index {:keyval event.keyval : modmask}))) (spec->index {:keyval event.keyval : modmask})))
(fn designates-command? [tbl] (fn command? [tbl]
;; a keymap entry has a string as key, a command ;; a keymap entry has a string as key, a command
;; definition is a numerically-indexed array ;; definition is a numerically-indexed array
(if (. tbl 1) true)) (if (. tbl 1) true))
(fn keymap? [tbl]
(not (. tbl 1)))
(fn compile-keymap [input] (fn compile-keymap [input]
(collect [k v (pairs input)] (collect [k v (pairs input)]
(let [f (-> k keychord->spec spec->index)] (let [f (-> k keychord->spec spec->index)]
(if (designates-command? v) (if (keymap? v)
(values f v) (values f (compile-keymap v))
(values f (compile-keymap v)))))) (values f v)))))
(fn recogniser [source-keymap] (fn recogniser [source-keymap]
(let [keymap (compile-keymap source-keymap)] (let [keymap (compile-keymap source-keymap)]
@ -63,16 +66,18 @@
:accept-event :accept-event
(fn [_ e] (fn [_ e]
(when (not (modifier? e.keyval)) (when (not (modifier? e.keyval))
(let [c (event->index e) (let [c (event->index e)]
v (. m c)] (match (. m c)
(if v (where v (keymap? v))
(if (designates-command? v) (do
(set m v)
(values nil (.. c " ")))
(where v (command? v))
(do (do
(set m keymap) (set m keymap)
v) v)
(do _
(set m v)
(values nil (.. c " "))))
(do (do
(set m keymap) (set m keymap)
(values nil (.. "No binding for " (view e) " "))))))) (values nil (.. "No binding for " (view e) " ")))))))