about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-02-22 23:25:52 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-02-22 23:25:52 -0800
commit3bfac642791f327f47caee6ba31b5c93ed08686d (patch)
treec0b447285de3003e3a362eb9493066aa678250a1
parent7440ff0292060746049c82c71feb57920cf37b6d (diff)
downloadmu-3bfac642791f327f47caee6ba31b5c93ed08686d.tar.gz
7791
Reorganize keyboard handling.
-rw-r--r--baremetal/shell/gap-buffer.mu22
-rw-r--r--baremetal/shell/sandbox.mu65
2 files changed, 37 insertions, 50 deletions
diff --git a/baremetal/shell/gap-buffer.mu b/baremetal/shell/gap-buffer.mu
index 5b6591db..7fd961b2 100644
--- a/baremetal/shell/gap-buffer.mu
+++ b/baremetal/shell/gap-buffer.mu
@@ -752,6 +752,28 @@ fn skip-whitespace-from-gap-buffer self: (addr gap-buffer) {
 }
 
 fn edit-gap-buffer self: (addr gap-buffer), key: grapheme {
+  var g/edx: grapheme <- copy key
+  {
+    compare g, 8/backspace
+    break-if-!=
+    delete-before-gap self
+    return
+  }
+  # arrow keys
+  {
+    compare g, 0x4/ctrl-d
+    break-if-!=
+    # ctrl-d: cursor down
+    return
+  }
+  {
+    compare g, 0x15/ctrl-u
+    break-if-!=
+    # ctrl-u: cursor up
+    return
+  }
+  # default: insert character
+  add-grapheme-at-gap self, g
 }
 
 fn cursor-on-final-line? self: (addr gap-buffer) -> _/eax: boolean {
diff --git a/baremetal/shell/sandbox.mu b/baremetal/shell/sandbox.mu
index 18bc1b09..f67a7c0b 100644
--- a/baremetal/shell/sandbox.mu
+++ b/baremetal/shell/sandbox.mu
@@ -36,19 +36,7 @@ fn allocate-sandbox-with _out: (addr handle sandbox), s: (addr array byte) {
   initialize-sandbox-with out-addr, s
 }
 
-fn add-grapheme-to-sandbox _self: (addr sandbox), c: grapheme {
-  var self/esi: (addr sandbox) <- copy _self
-  var data-ah/eax: (addr handle gap-buffer) <- get self, data
-  var data/eax: (addr gap-buffer) <- lookup *data-ah
-  add-grapheme-at-gap data, c
-}
-
-fn delete-grapheme-before-cursor _self: (addr sandbox) {
-  var self/esi: (addr sandbox) <- copy _self
-  var data-ah/eax: (addr handle gap-buffer) <- get self, data
-  var data/eax: (addr gap-buffer) <- lookup *data-ah
-  delete-before-gap data
-}
+##
 
 fn render-sandbox screen: (addr screen), _self: (addr sandbox), _x: int, _y: int {
   clear-screen screen
@@ -93,12 +81,6 @@ fn render-sandbox screen: (addr screen), _self: (addr sandbox), _x: int, _y: int
 fn edit-sandbox _self: (addr sandbox), key: byte {
   var self/esi: (addr sandbox) <- copy _self
   var g/edx: grapheme <- copy key
-  {
-    compare g, 8/backspace
-    break-if-!=
-    delete-grapheme-before-cursor self
-    return
-  }
   # running code
   {
     compare g, 0x12/ctrl-r
@@ -125,11 +107,11 @@ fn edit-sandbox _self: (addr sandbox), key: byte {
     return
   }
   # arrow keys
+  var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
   {
     compare g, 0x4/ctrl-d
     break-if-!=
     # ctrl-d: cursor down (into trace if it makes sense)
-    var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
     # if cursor in input, check if we need to switch to trace
     {
       compare *cursor-in-trace?, 0/false
@@ -143,44 +125,27 @@ fn edit-sandbox _self: (addr sandbox), key: byte {
       copy-to *cursor-in-trace?, 1/true
       return
     }
-    # if cursor in trace, send cursor to trace
-    {
-      compare cursor-in-trace?, 0/false
-      break-if-=
-      var trace-ah/eax: (addr handle trace) <- get self, trace
-      var trace/eax: (addr trace) <- lookup *trace-ah
-      edit-trace trace, g
-      return
-    }
-    # otherwise send cursor to input
-    var data-ah/eax: (addr handle gap-buffer) <- get self, data
-    var data/eax: (addr gap-buffer) <- lookup *data-ah
-    edit-gap-buffer data, g
-    return
   }
   {
     compare g, 0x15/ctrl-u
     break-if-!=
     # ctrl-u: cursor up
-    var cursor-in-trace?/eax: (addr boolean) <- get self, cursor-in-trace?
     # if cursor in trace, check if we need to switch to trace
-    # if cursor in trace, send cursor to trace
-    {
-      compare cursor-in-trace?, 0/false
-      break-if-=
-      var trace-ah/eax: (addr handle trace) <- get self, trace
-      var trace/eax: (addr trace) <- lookup *trace-ah
-      edit-trace trace, g
-      return
-    }
-    # otherwise send cursor to input
-    var data-ah/eax: (addr handle gap-buffer) <- get self, data
-    var data/eax: (addr gap-buffer) <- lookup *data-ah
-    edit-gap-buffer data, g
+  }
+  # if cursor in trace, send cursor to trace
+  {
+    compare *cursor-in-trace?, 0/false
+    break-if-=
+    var trace-ah/eax: (addr handle trace) <- get self, trace
+    var trace/eax: (addr trace) <- lookup *trace-ah
+    edit-trace trace, g
     return
   }
-  # default: insert character
-  add-grapheme-to-sandbox self, g
+  # otherwise send cursor to input
+  var data-ah/eax: (addr handle gap-buffer) <- get self, data
+  var data/eax: (addr gap-buffer) <- lookup *data-ah
+  edit-gap-buffer data, g
+  return
 }
 
 fn run in: (addr gap-buffer), out: (addr stream byte), trace: (addr trace) {