express keymap bindings as [command-name args]
instead of using functions directly. This is so that the appropriate frame-relevant commander can be used to execure the command, but also makes the keymaps a little less opaque
This commit is contained in:
parent
a5612fce2a
commit
dd139c9796
13
dunlin.fnl
13
dunlin.fnl
@ -9,17 +9,10 @@
|
||||
;;; when we decide how to do an init file/rc file, this will go in it
|
||||
|
||||
(local my-keymap {
|
||||
"g" #(Command.invoke-interactively
|
||||
"visit-location"
|
||||
{:buffer "main"})
|
||||
"M-q" #(Command.invoke-interactively
|
||||
"quit-browser"
|
||||
{})
|
||||
"g" ["visit-location" {:buffer "main"}]
|
||||
"M-q" ["quit-browser" {}]
|
||||
"C-x" {
|
||||
"C-c"
|
||||
#(Command.invoke-interactively
|
||||
"quit-browser"
|
||||
{})
|
||||
"C-c" ["quit-browser" {}]
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -32,7 +32,7 @@
|
||||
(fn [window event]
|
||||
(when (not (Command.active?))
|
||||
(match (recogniser:accept-event event)
|
||||
c (c)
|
||||
[name params] (Command.invoke-interactively name params)
|
||||
(nil prompt) (print "prompted" prompt)))
|
||||
(when (and event.state.MOD1_MASK
|
||||
(= event.keyval (string.byte "x")))
|
||||
|
31
keymap.fnl
31
keymap.fnl
@ -26,13 +26,17 @@
|
||||
(bor m (. Gdk.ModifierType k)))]
|
||||
(spec->index {:keyval event.keyval : modmask})))
|
||||
|
||||
(fn designates-command? [tbl]
|
||||
;; a keymap entry has a string as key, a command
|
||||
;; definition is a numerically-indexed array
|
||||
(if (. tbl 1) true))
|
||||
|
||||
(fn compile-keymap [input]
|
||||
(collect [k v (pairs input)]
|
||||
(let [f (-> k keychord->spec spec->index)]
|
||||
(match (type v)
|
||||
"function" (values f v)
|
||||
"table" (values f (compile-keymap v))))))
|
||||
(if (designates-command? v)
|
||||
(values f v)
|
||||
(values f (compile-keymap v))))))
|
||||
|
||||
(fn recogniser [source-keymap]
|
||||
(let [keymap (compile-keymap source-keymap)]
|
||||
@ -42,18 +46,17 @@
|
||||
(fn [_ e]
|
||||
(let [c (event->index e)
|
||||
v (. m c)]
|
||||
(match (type v)
|
||||
"table" (do
|
||||
(set m v)
|
||||
(values nil (.. c " ")))
|
||||
"function" (do
|
||||
(set m keymap)
|
||||
v)
|
||||
"nil" (do
|
||||
(if v
|
||||
(if (designates-command? v)
|
||||
(do
|
||||
(set m keymap)
|
||||
(values nil (.. "No binding for " (view e) " ")))
|
||||
|
||||
)))
|
||||
v)
|
||||
(do
|
||||
(set m v)
|
||||
(values nil (.. c " "))))
|
||||
(do
|
||||
(set m keymap)
|
||||
(values nil (.. "No binding for " (view e) " "))))))
|
||||
}))
|
||||
|
||||
|
||||
|
@ -6,13 +6,15 @@
|
||||
|
||||
(local Mod Gdk.ModifierType)
|
||||
|
||||
(local km
|
||||
{"a"
|
||||
{"a" #1
|
||||
"b" #2}
|
||||
"b"
|
||||
{"z" #3}
|
||||
"c" #4
|
||||
(local km {
|
||||
"a" {
|
||||
"a" ["command-1"]
|
||||
"b" ["command-2" {:arg-1 "10" :arg-2 "11"}]
|
||||
}
|
||||
"b" {
|
||||
"z" ["command-3"]
|
||||
}
|
||||
"c" ["command-4"]
|
||||
})
|
||||
|
||||
(fn fake-key-event [c]
|
||||
@ -40,8 +42,16 @@
|
||||
(let [r (keymap.recogniser km)
|
||||
(ok err)
|
||||
(match (r:accept-event (fake-key-event "c"))
|
||||
(where f (= (f) 4)) true
|
||||
["command-4"] true
|
||||
x (values false (view x))
|
||||
nil (values false "???"))]
|
||||
|
||||
(assert ok err))
|
||||
|
||||
(let [r (keymap.recogniser km)
|
||||
(ok err)
|
||||
(match-try
|
||||
(r:accept-event (fake-key-event "a"))
|
||||
nil (r:accept-event (fake-key-event "b"))
|
||||
["command-2" {:arg-1 "10" :arg-2 "11"}] true
|
||||
(catch x (values false (view x))))]
|
||||
(assert ok err))
|
||||
|
Loading…
Reference in New Issue
Block a user