about summary refs log tree commit diff stats
path: root/src/server
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-26 16:52:57 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-26 16:54:46 +0200
commit2b47c6f5ac8d41a8025c10d02da4d6090e616a68 (patch)
treee97afd43206d14b058813d159dc1f53c59b8f20b /src/server
parentee17e946c53e6ca807dea2df04f8f2b9eedd754f (diff)
downloadchawan-2b47c6f5ac8d41a8025c10d02da4d6090e616a68.tar.gz
Add precnum support to more functions
Diffstat (limited to 'src/server')
-rw-r--r--src/server/buffer.nim24
1 files changed, 18 insertions, 6 deletions
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 4ab09c20..d6c00ef9 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -463,16 +463,20 @@ proc findNextLink*(buffer: Buffer, cursorx, cursory: int): tuple[x, y: int] {.pr
       inc i
   return (-1, -1)
 
-proc findPrevMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: bool): BufferMatch {.proxy.} =
+proc findPrevMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int,
+    wrap: bool, n: int): BufferMatch {.proxy.} =
   if cursory >= buffer.lines.len: return
   var y = cursory
   let b = buffer.cursorBytes(y, cursorx)
   let res = regex.exec(buffer.lines[y].str, 0, b)
+  var numfound = 0
   if res.success and res.captures.len > 0:
     let cap = res.captures[^1]
     let x = buffer.lines[y].str.width(0, cap.s)
     let str = buffer.lines[y].str.substr(cap.s, cap.e - 1)
-    return BufferMatch(success: true, x: x, y: y, str: str)
+    inc numfound
+    if numfound >= n:
+      return BufferMatch(success: true, x: x, y: y, str: str)
   dec y
   while true:
     if y < 0:
@@ -485,21 +489,27 @@ proc findPrevMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: b
       let cap = res.captures[^1]
       let x = buffer.lines[y].str.width(0, cap.s)
       let str = buffer.lines[y].str.substr(cap.s, cap.e - 1)
-      return BufferMatch(success: true, x: x, y: y, str: str)
+      inc numfound
+      if numfound >= n:
+        return BufferMatch(success: true, x: x, y: y, str: str)
     if y == cursory:
       break
     dec y
 
-proc findNextMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: bool): BufferMatch {.proxy.} =
+proc findNextMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int,
+    wrap: bool, n: int): BufferMatch {.proxy.} =
   if cursory >= buffer.lines.len: return
   var y = cursory
   let b = buffer.cursorBytes(y, cursorx + 1)
   let res = regex.exec(buffer.lines[y].str, b, buffer.lines[y].str.len)
+  var numfound = 0
   if res.success and res.captures.len > 0:
     let cap = res.captures[0]
     let x = buffer.lines[y].str.width(0, cap.s)
     let str = buffer.lines[y].str.substr(cap.s, cap.e - 1)
-    return BufferMatch(success: true, x: x, y: y, str: str)
+    inc numfound
+    if numfound >= n:
+      return BufferMatch(success: true, x: x, y: y, str: str)
   inc y
   while true:
     if y > buffer.lines.high:
@@ -512,7 +522,9 @@ proc findNextMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: b
       let cap = res.captures[0]
       let x = buffer.lines[y].str.width(0, cap.s)
       let str = buffer.lines[y].str.substr(cap.s, cap.e - 1)
-      return BufferMatch(success: true, x: x, y: y, str: str)
+      inc numfound
+      if numfound >= n:
+        return BufferMatch(success: true, x: x, y: y, str: str)
     if y == cursory:
       break
     inc y