From c3e9c14186b90a0dbb565547484baea03db6bfa2 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 1 Jan 2023 15:08:45 +0000 Subject: [PATCH] index->string allows printing keystrokes readably --- doc/index.md | 6 ++++++ keymap.fnl | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/index.md b/doc/index.md index 1690e76..e904871 100644 --- a/doc/index.md +++ b/doc/index.md @@ -99,6 +99,12 @@ To see how commands are implemented, read the code in `command.fnl`. There is a simple keymap in `dunlin.fnl`, and you can see the details of how keymaps work in `keymap.fnl` +When writing key bindings or printing errors, Dunlin assumes that the +key producing "Mod 1" (often labelled Alt) is the Meta key, and the +key producing "Mod 4" (on a PC, typically the key with the Windows logo) +is the Super key. For me this matches how Emacs does it, but I would +welcome reports of machines/setups that don't act ths way + ## Contributing diff --git a/keymap.fnl b/keymap.fnl index 12137aa..192798d 100644 --- a/keymap.fnl +++ b/keymap.fnl @@ -1,5 +1,6 @@ (local { : Gdk } (require :lgi)) (local { : view } (require :fennel)) +(local lume (require :lume)) (local modifier-keyvals ;; we need to detect and discard modifier-only key events when @@ -51,6 +52,25 @@ (bor m (. Gdk.ModifierType k)))] (spec->index {:keyval event.keyval : modmask}))) +(fn compact [xs] + (icollect [_ v (ipairs xs)] v)) + +(fn index->string [index] + (let [Mod Gdk.ModifierType + [keyval modmask] (lume.map (lume.split index ":") tonumber) + chars []] + (if (> (band modmask Mod.CONTROL_MASK) 0) (table.insert chars "C")) + (if (> (band modmask Mod.MOD1_MASK) 0) (table.insert chars "M")) + (if (> (band modmask Mod.MOD4_MASK) 0) (table.insert chars "S")) + (table.insert chars (Gdk.keyval_name keyval)) + (table.concat chars "-"))) + +(let [v (index->string "103:0")] (assert (= v "g") v)) +(let [v (index->string "65:0")] (assert (= v "A") v)) +(let [v (index->string "120:4")] (assert (= v "C-x") v)) +(let [v (index->string "100:8")] (assert (= v "M-d") v)) +(let [v (index->string "100:12")] (assert (= v "C-M-d") v)) + (fn command? [tbl] ;; a keymap entry has a string as key, a command ;; definition is a numerically-indexed array @@ -92,7 +112,7 @@ _ (do (set m keymap) - (values nil (.. "No binding for " (view e) " "))))))) + (values nil (.. "No binding for " (index->string c) " "))))))) }))