about summary refs log tree commit diff stats
path: root/search.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-05-13 17:02:10 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-05-13 17:02:10 -0700
commit8e02c2c0218b4e233f14d130f7d339128886f763 (patch)
tree7cc128ebe9c9ddda77eec2e6f5cd3c22dc591158 /search.lua
parent6a1d8e5164a9bb21c654f4cfa7bc8ff4c43e6c95 (diff)
downloadtext.love-8e02c2c0218b4e233f14d130f7d339128886f763.tar.gz
bugfix: searching files containing unicode
Before this change the cursor was moving, but not being highlighted
properly when the cursor line contained unicode before the cursor.
Diffstat (limited to 'search.lua')
-rw-r--r--search.lua70
1 files changed, 42 insertions, 28 deletions
diff --git a/search.lua b/search.lua
index 7d3d335..c9af187 100644
--- a/search.lua
+++ b/search.lua
@@ -18,38 +18,45 @@ end
 
 function Text.search_next(State)
   -- search current line from cursor
-  local pos = find(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos, --[[literal]] true)
-  if pos then
-    State.cursor1.pos = pos
+  local curr_pos = State.cursor1.pos
+  local curr_line = State.lines[State.cursor1.line].data
+  local curr_offset = Text.offset(curr_line, curr_pos)
+  local offset = find(curr_line, State.search_term, curr_offset, --[[literal]] true)
+  if offset then
+    State.cursor1.pos = utf8.len(curr_line, 1, offset)
   end
-  if pos == nil then
+  if offset == nil then
     -- search lines below cursor
     for i=State.cursor1.line+1,#State.lines do
-      pos = find(State.lines[i].data, State.search_term, --[[from start]] nil, --[[literal]] true)
-      if pos then
-        State.cursor1 = {line=i, pos=pos}
+      local curr_line = State.lines[i].data
+      offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
+      if offset then
+        State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
         break
       end
     end
   end
-  if pos == nil then
+  if offset == nil then
     -- wrap around
     for i=1,State.cursor1.line-1 do
-      pos = find(State.lines[i].data, State.search_term, --[[from start]] nil, --[[literal]] true)
-      if pos then
-        State.cursor1 = {line=i, pos=pos}
+      local curr_line = State.lines[i].data
+      offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
+      if offset then
+        State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
         break
       end
     end
   end
-  if pos == nil then
+  if offset == nil then
     -- search current line until cursor
-    pos = find(State.lines[State.cursor1.line].data, State.search_term, --[[from start]] nil, --[[literal]] true)
+    local curr_line = State.lines[State.cursor1.line].data
+    offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
+    local pos = utf8.len(curr_line, 1, offset)
     if pos and pos < State.cursor1.pos then
       State.cursor1.pos = pos
     end
   end
-  if pos == nil then
+  if offset == nil then
     State.cursor1.line = State.search_backup.cursor.line
     State.cursor1.pos = State.search_backup.cursor.pos
     State.screen_top1.line = State.search_backup.screen_top.line
@@ -64,38 +71,45 @@ end
 
 function Text.search_previous(State)
   -- search current line before cursor
-  local pos = rfind(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos-1, --[[literal]] true)
-  if pos then
-    State.cursor1.pos = pos
+  local curr_pos = State.cursor1.pos
+  local curr_line = State.lines[State.cursor1.line].data
+  local curr_offset = Text.offset(curr_line, curr_pos)
+  local offset = rfind(curr_line, State.search_term, curr_offset-1, --[[literal]] true)
+  if offset then
+    State.cursor1.pos = utf8.len(curr_line, 1, offset)
   end
-  if pos == nil then
+  if offset == nil then
     -- search lines above cursor
     for i=State.cursor1.line-1,1,-1 do
-      pos = rfind(State.lines[i].data, State.search_term, --[[from end]] nil, --[[literal]] true)
-      if pos then
-        State.cursor1 = {line=i, pos=pos}
+      local curr_line = State.lines[i].data
+      offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
+      if offset then
+        State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
         break
       end
     end
   end
-  if pos == nil then
+  if offset == nil then
     -- wrap around
     for i=#State.lines,State.cursor1.line+1,-1 do
-      pos = rfind(State.lines[i].data, State.search_term, --[[from end]] nil, --[[literal]] true)
-      if pos then
-        State.cursor1 = {line=i, pos=pos}
+      local curr_line = State.lines[i].data
+      offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
+      if offset then
+        State.cursor1 = {line=i, pos=utf8.len(curr_line, 1, offset)}
         break
       end
     end
   end
-  if pos == nil then
+  if offset == nil then
     -- search current line after cursor
-    pos = rfind(State.lines[State.cursor1.line].data, State.search_term, --[[from end]] nil, --[[literal]] true)
+    local curr_line = State.lines[State.cursor1.line].data
+    offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
+    local pos = utf8.len(curr_line, 1, offset)
     if pos and pos > State.cursor1.pos then
       State.cursor1.pos = pos
     end
   end
-  if pos == nil then
+  if offset == nil then
     State.cursor1.line = State.search_backup.cursor.line
     State.cursor1.pos = State.search_backup.cursor.pos
     State.screen_top1.line = State.search_backup.screen_top.line