about summary refs log tree commit diff stats
path: root/search.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-08-11 22:17:21 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-08-11 22:23:16 -0700
commit8b880f4fe8d0c631975063c20217dbf44de6bc1d (patch)
tree533c8938eafc6bcee7323f8bde7a7c0c89f113f4 /search.lua
parente85a7e73d0f4992b775d1848f7343e5da6b225bd (diff)
downloadlines.love-8b880f4fe8d0c631975063c20217dbf44de6bc1d.tar.gz
search: transparently handle drawings everywhere
Diffstat (limited to 'search.lua')
-rw-r--r--search.lua40
1 files changed, 18 insertions, 22 deletions
diff --git a/search.lua b/search.lua
index 72ee30c..4ebd8ab 100644
--- a/search.lua
+++ b/search.lua
@@ -20,33 +20,26 @@ function Text.draw_search_bar(State)
 end
 
 function Text.search_next(State)
-  local pos
   -- search current line from cursor
-  local line = State.lines[State.cursor1.line]
-  if line.mode == 'text' then
-    pos = line.data:find(State.search_term, State.cursor1.pos)
-    if pos then
-      State.cursor1.pos = pos
-    end
+  local pos = find(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos)
+  if pos then
+    State.cursor1.pos = pos
   end
   if pos == nil then
     -- search lines below cursor
     for i=State.cursor1.line+1,#State.lines do
-      local line = State.lines[i]
-      if line.mode == 'text' then
-        pos = State.lines[i].data:find(State.search_term)
-        if pos then
-          State.cursor1.line = i
-          State.cursor1.pos = pos
-          break
-        end
+      pos = find(State.lines[i].data, State.search_term)
+      if pos then
+        State.cursor1.line = i
+        State.cursor1.pos = pos
+        break
       end
     end
   end
   if pos == nil then
     -- wrap around
     for i=1,State.cursor1.line-1 do
-      pos = State.lines[i].data:find(State.search_term)
+      pos = find(State.lines[i].data, State.search_term)
       if pos then
         State.cursor1.line = i
         State.cursor1.pos = pos
@@ -56,12 +49,9 @@ function Text.search_next(State)
   end
   if pos == nil then
     -- search current line until cursor
-    local line = State.lines[State.cursor1.line]
-    if line.mode == 'text' then
-      pos = line.data:find(State.search_term)
-      if pos and pos < State.cursor1.pos then
-        State.cursor1.pos = pos
-      end
+    pos = find(State.lines[State.cursor1.line].data, State.search_term)
+    if pos and pos < State.cursor1.pos then
+      State.cursor1.pos = pos
     end
   end
   if pos == nil then
@@ -117,7 +107,13 @@ function Text.search_previous(State)
   end
 end
 
+function find(s, pat, i)
+  if s == nil then return end
+  return s:find(pat, i)
+end
+
 function rfind(s, pat, i)
+  if s == nil then return end
   local rs = s:reverse()
   local rpat = pat:reverse()
   if i == nil then i = #s end