about summary refs log tree commit diff stats
path: root/prototypes
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-06-05 23:41:13 -0700
committerKartik Agaram <vc@akkartik.com>2020-06-05 23:41:13 -0700
commitb244fe88b38c3a4c9cb8ccb6211aa0c33e392d6c (patch)
treebe4d94e091dabc95c1dfcc61e59d154ab3305d28 /prototypes
parent3e33219b0701b330359f140b03da5cea10bae804 (diff)
downloadmu-b244fe88b38c3a4c9cb8ccb6211aa0c33e392d6c.tar.gz
6487 - browse: support soft newlines
I'm not bothering with this for bold regions just yet. Might need rethinking,
given how ugly this is.
Diffstat (limited to 'prototypes')
-rw-r--r--prototypes/browse/25-soft-newlines/README.md1
-rw-r--r--prototypes/browse/25-soft-newlines/main.mu47
-rw-r--r--prototypes/browse/25-soft-newlines/screen-position-state.mu13
3 files changed, 55 insertions, 6 deletions
diff --git a/prototypes/browse/25-soft-newlines/README.md b/prototypes/browse/25-soft-newlines/README.md
index bcd26fab..df2a2ba9 100644
--- a/prototypes/browse/25-soft-newlines/README.md
+++ b/prototypes/browse/25-soft-newlines/README.md
@@ -2,3 +2,4 @@ Support soft newlines.
 
 Single newline = ignore.
 Two newlines = print both.
+Newline then space = print both.
diff --git a/prototypes/browse/25-soft-newlines/main.mu b/prototypes/browse/25-soft-newlines/main.mu
index 8dfc2b9d..ec573e9a 100644
--- a/prototypes/browse/25-soft-newlines/main.mu
+++ b/prototypes/browse/25-soft-newlines/main.mu
@@ -29,7 +29,8 @@ fn render fs: (addr file-state), state: (addr screen-position-state) {
 }
 
 fn render-normal fs: (addr file-state), state: (addr screen-position-state) {
-$render-normal:body: {
+    var newline-seen?/esi: boolean <- copy 0  # false
+$render-normal:loop: {
     # if done-drawing?(state) break
     var done?/eax: boolean <- done-drawing? state
     compare done?, 0  # false
@@ -39,6 +40,46 @@ $render-normal:body: {
     # if (c == EOF) break
     compare c, 0xffffffff  # EOF marker
     break-if-=
+    # if (c == newline)
+    compare c, 0xa  # newline
+    {
+      break-if-!=
+      # if it's the first newline, buffer it
+      compare newline-seen?, 0
+      {
+        break-if-!=
+        newline-seen? <- copy 1  # true
+        loop $render-normal:loop
+      }
+      # otherwise render two newlines
+      {
+        break-if-=
+        add-char state, 0xa  # newline
+        add-char state, 0xa  # newline
+        newline-seen? <- copy 0  # false
+        loop $render-normal:loop
+      }
+    }
+    # if c is unprintable (particularly a '\r' CR), skip it
+    compare c, 0x20
+    loop-if-<
+    # If there's a newline buffered and c is a space, print the buffered
+    # newline (hard newline).
+    # If there's a newline buffered and c is not a newline or space, print a
+    # space (soft newline).
+    compare newline-seen?, 0  # false
+$render-normal:flush-buffered-newline: {
+      break-if-=
+      newline-seen? <- copy 0  # false
+      {
+        compare c, 0x20
+        break-if-!=
+        add-char state, 0xa  # newline
+        break $render-normal:flush-buffered-newline
+      }
+      add-char state, 0x20  # space
+      # fall through to print c
+    }
     # if (c == '*') switch to bold
     compare c, 0x2a  # '*'
     {
@@ -46,7 +87,7 @@ $render-normal:body: {
       start-bold
         render-until-asterisk fs, state
       normal-text
-      loop $render-normal:body
+      loop $render-normal:loop
     }
     # if (c == '_') switch to bold
     compare c, 0x5f  # '_'
@@ -57,7 +98,7 @@ $render-normal:body: {
         render-until-underscore fs, state
       reset-formatting
       start-color 0xec, 7  # 236 = darkish gray
-      loop $render-normal:body
+      loop $render-normal:loop
     }
     #
     add-char state, c
diff --git a/prototypes/browse/25-soft-newlines/screen-position-state.mu b/prototypes/browse/25-soft-newlines/screen-position-state.mu
index 62161108..dcf3722a 100644
--- a/prototypes/browse/25-soft-newlines/screen-position-state.mu
+++ b/prototypes/browse/25-soft-newlines/screen-position-state.mu
@@ -15,9 +15,9 @@ fn init-screen-position-state _self: (addr screen-position-state) {
   #   page-margin
   #   page-width
   var self/esi: (addr screen-position-state) <- copy _self
-  var nrows/eax: int <- copy 0
-  var ncols/ecx: int <- copy 0
-  nrows, ncols <- screen-size
+  var nrows/eax: int <- copy 0xa
+  var ncols/ecx: int <- copy 0x20
+  nrows, ncols <- screen-size  # Comment this out to debug with a tiny page. You'll also need to adjust rightcol below.
   var dest/edx: (addr int) <- copy 0
   # self->nrows = nrows
   dest <- get self, nrows
@@ -45,6 +45,7 @@ fn start-drawing _self: (addr screen-position-state) {
   copy-to *tmp, 5  # left-margin
   # self->rightcol = self->leftcol + page-width
   tmp <- get self, rightcol
+#?   copy-to *tmp, 0x1f  # ncols - 1
   copy-to *tmp, 0x45  # left-margin + page-width
   # self->row = self->toprow
   tmp <- get self, toprow
@@ -113,6 +114,12 @@ fn next-page _self: (addr screen-position-state) {
   var self/esi: (addr screen-position-state) <- copy _self
   var tmp/eax: (addr int) <- copy 0
   var tmp2/ecx: int <- copy 0
+#?   # temporary: stop
+#?   tmp <- get self, ncols
+#?   tmp2 <- copy *tmp
+#?   tmp <- get self, rightcol
+#?   copy-to *tmp, tmp2
+  # real: multiple pages
   # self->leftcol = self->rightcol + page-margin
   tmp <- get self, rightcol
   tmp2 <- copy *tmp