about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-30 19:42:41 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-30 19:42:41 +0200
commite26ea3aca2c0c5eb4a2678440dbec77105ee8dd4 (patch)
treedc48e835b9c39efd35486db785cda95206d0871d /src
parent83857c03a64312770e0ba0f46b5917dceb23c0ad (diff)
downloadchawan-e26ea3aca2c0c5eb4a2678440dbec77105ee8dd4.tar.gz
buffer: fix cursor unnecessarily jumping to y=0
then() is called even if a nil Promise is returned, so an Opt is
needed here.
Diffstat (limited to 'src')
-rw-r--r--src/local/container.nim5
-rw-r--r--src/server/buffer.nim12
2 files changed, 10 insertions, 7 deletions
diff --git a/src/local/container.nim b/src/local/container.nim
index d3d0b879..6a11e02a 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -809,8 +809,9 @@ proc onload*(container: Container, res: LoadResult) =
         container.triggerEvent(LOADED)
         if not container.hasstart and container.source.location.anchor != "":
           return container.iface.gotoAnchor()
-      ).then(proc(res: tuple[x, y: int]) =
-        if res.x != -1 and res.y != -1:
+      ).then(proc(res: Opt[tuple[x, y: int]]) =
+        if res.isSome:
+          let res = res.get
           container.setCursorXY(res.x, res.y))
 
 proc load(container: Container) =
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 24dc7d3d..49ee97aa 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -536,17 +536,19 @@ proc findNextMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int,
       break
     inc y
 
-proc gotoAnchor*(buffer: Buffer): tuple[x, y: int] {.proxy.} =
-  if buffer.document == nil: return (-1, -1)
+proc gotoAnchor*(buffer: Buffer): Opt[tuple[x, y: int]] {.proxy.} =
+  if buffer.document == nil:
+    return err()
   let anchor = buffer.document.findAnchor(buffer.url.anchor)
-  if anchor == nil: return (-1, -1)
+  if anchor == nil:
+    return err()
   for y in 0 ..< buffer.lines.len:
     let line = buffer.lines[y]
     for i in 0 ..< line.formats.len:
       let format = line.formats[i]
       if format.node != nil and anchor in format.node.node:
-        return (format.pos, y)
-  return (-1, -1)
+        return ok((format.pos, y))
+  return err()
 
 proc do_reshape(buffer: Buffer) =
   if buffer.ishtml: