From dd139c9796800c73ca722e320622b536c0ae6a70 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 26 Dec 2022 16:53:41 +0000 Subject: [PATCH] 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 --- dunlin.fnl | 13 +++---------- frame.fnl | 2 +- keymap.fnl | 31 +++++++++++++++++-------------- test/keymap.fnl | 28 +++++++++++++++++++--------- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/dunlin.fnl b/dunlin.fnl index 3beb469..3daa961 100644 --- a/dunlin.fnl +++ b/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" {}] } }) diff --git a/frame.fnl b/frame.fnl index e90d07d..79668b2 100644 --- a/frame.fnl +++ b/frame.fnl @@ -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"))) diff --git a/keymap.fnl b/keymap.fnl index 3f0f1f1..252cb30 100644 --- a/keymap.fnl +++ b/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) " ")))))) })) diff --git a/test/keymap.fnl b/test/keymap.fnl index 502d4ad..afc035f 100644 --- a/test/keymap.fnl +++ b/test/keymap.fnl @@ -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))