unnest conditionals in keymap recogniser

main
Daniel Barlow 2022-12-31 23:49:41 +00:00
parent ec391a9e57
commit 39735be891
1 changed files with 22 additions and 17 deletions

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,19 +66,21 @@
: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
(do (set m v)
(set m keymap) (values nil (.. c " ")))
v)
(do (where v (command? v))
(set m v) (do
(values nil (.. c " ")))) (set m keymap)
(do v)
(set m keymap) _
(values nil (.. "No binding for " (view e) " "))))))) (do
(set m keymap)
(values nil (.. "No binding for " (view e) " ")))))))
})) }))