about summary refs log tree commit diff stats
path: root/source_edit.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2024-10-29 15:43:35 -0700
committerKartik K. Agaram <vc@akkartik.com>2024-10-29 15:58:00 -0700
commitc1a3616964bfdeb11144c0e7289fc16ba563e84a (patch)
tree18a6b93c13b61070de189527a4357bfb4c2d9893 /source_edit.lua
parent1609d79516b2e8f790648fd67614d9edb9c2e24c (diff)
downloadlines.love-c1a3616964bfdeb11144c0e7289fc16ba563e84a.tar.gz
bugfix in search UI
This one is ancient and it affects every single one of my forks,
including the whole lines2 lineage. The corner case: searching for empty
string.

In the process I've also cleaned up edit.check_locs on initialization to
only modify cursor if it can find a legal place for it.

In general I should be more careful about mutating the cursor. Just
adding 1 to it is irresponsible.
Diffstat (limited to 'source_edit.lua')
-rw-r--r--source_edit.lua33
1 files changed, 27 insertions, 6 deletions
diff --git a/source_edit.lua b/source_edit.lua
index 3c0f794..90c1171 100644
--- a/source_edit.lua
+++ b/source_edit.lua
@@ -142,15 +142,36 @@ function edit.cursor_on_text(State)
 end
 
 function edit.put_cursor_on_next_text_line(State)
-  while true do
-    if State.cursor1.line >= #State.lines then
+  local line = State.cursor1.line
+  while line < #State.lines do
+    line = line+1
+    if State.lines[line].mode == 'text' then
+      State.cursor1.line = line
+      State.cursor1.pos = 1
       break
     end
-    if State.lines[State.cursor1.line].mode == 'text' then
+  end
+end
+
+function edit.put_cursor_on_next_text_line_wrapping_around_if_necessary(State)
+  local line = State.cursor1.line
+  local max = #State.lines
+  for _ = 1, max-1 do
+    line = (line+1) % max
+    if State.lines[line].mode == 'text' then
+      State.cursor1.line = line
+      State.cursor1.pos = 1
       break
     end
-    State.cursor1.line = State.cursor1.line+1
-    State.cursor1.pos = 1
+  end
+end
+
+function edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
+  local cursor_line = State.lines[State.cursor1.line].data
+  if State.cursor1.pos <= utf8.len(cursor_line) then
+    State.cursor1.pos = State.cursor1.pos + 1
+  else
+    edit.put_cursor_on_next_text_line_wrapping_around_if_necessary(State)
   end
 end
 
@@ -411,7 +432,7 @@ function edit.keychord_press(State, chord, key)
       State.screen_top = deepcopy(State.search_backup.screen_top)
       Text.search_next(State)
     elseif chord == 'down' then
-      State.cursor1.pos = State.cursor1.pos+1
+      edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
       Text.search_next(State)
     elseif chord == 'up' then
       Text.search_previous(State)