about summary refs log tree commit diff stats
path: root/src/io/buffer.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-06-17 19:06:46 +0200
committerbptato <nincsnevem662@gmail.com>2022-06-17 19:06:46 +0200
commit571a539d88e72587ea070e685442d32609f050cb (patch)
treecce3475e29f0d6b566c4608aed91e49dd0eca701 /src/io/buffer.nim
parent4d2f3c55f7e880814c1d05a6e1137df63374a1b1 (diff)
downloadchawan-571a539d88e72587ea070e685442d32609f050cb.tar.gz
Fix cursorPrevLink
Diffstat (limited to 'src/io/buffer.nim')
-rw-r--r--src/io/buffer.nim46
1 files changed, 32 insertions, 14 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index e3858908..654bdbe7 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -515,11 +515,41 @@ proc cursorPrevLink*(buffer: Buffer) =
     link = line.formats[i].node.getClickable()
   dec i
 
+  var ly = 0 #last y
+  var lx = 0 #last x
+  template link_beginning() =
+    #go to beginning of link
+    ly = y #last y
+    lx = format.pos #last x
+
+    #on the current line
+    let line = buffer.lines[y]
+    while i >= 0:
+      let format = line.formats[i]
+      let nl = format.node.getClickable()
+      if nl == fl:
+        lx = format.pos
+      dec i
+
+    #on previous lines
+    for iy in countdown(ly - 1, 0):
+      let line = buffer.lines[iy]
+      i = line.formats.len - 1
+      while i >= 0:
+        let format = line.formats[i]
+        let nl = format.node.getClickable()
+        if nl == fl:
+          ly = iy
+          lx = format.pos
+        dec i
+
   while i >= 0:
     let format = line.formats[i]
     let fl = format.node.getClickable()
     if fl != nil and fl != link:
-      buffer.setCursorX(format.pos)
+      let y = buffer.cursory
+      link_beginning
+      buffer.setCursorXY(lx, ly)
       return
     dec i
 
@@ -530,19 +560,7 @@ proc cursorPrevLink*(buffer: Buffer) =
       let format = line.formats[i]
       let fl = format.node.getClickable()
       if fl != nil and fl != link:
-        #go to beginning of link
-        var ly = y #last y
-        var lx = format.pos #last x
-        for iy in countdown(ly - 1, 0):
-          let line = buffer.lines[iy]
-          i = line.formats.len - 1
-          while i >= 0:
-            let format = line.formats[i]
-            let nl = format.node.getClickable()
-            if nl == fl:
-              ly = iy
-              lx = format.pos
-            dec i
+        link_beginning
         buffer.setCursorXY(lx, ly)
         return
       dec i