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