about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--app.lua2
-rw-r--r--edit.lua28
-rw-r--r--search.lua2
-rw-r--r--source_edit.lua40
-rw-r--r--source_text_tests.lua28
-rw-r--r--text_tests.lua28
6 files changed, 118 insertions, 10 deletions
diff --git a/app.lua b/app.lua
index 263518c..1bec1f0 100644
--- a/app.lua
+++ b/app.lua
@@ -131,7 +131,7 @@ function App.run_tests()
   end
   table.sort(sorted_names)
 --?   App.initialize_for_test() -- debug: run a single test at a time like these 2 lines
---?   test_click_below_all_lines()
+--?   test_search()
   for _,name in ipairs(sorted_names) do
     App.initialize_for_test()
 --?     print('=== '..name)
diff --git a/edit.lua b/edit.lua
index 4b7a2fc..3001bed 100644
--- a/edit.lua
+++ b/edit.lua
@@ -97,6 +97,28 @@ function edit.invalid_cursor1(State)
   return cursor1.pos > #State.lines[cursor1.line].data + 1
 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
+  end
+end
+
+function edit.put_cursor_on_next_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
+
 function edit.draw(State)
   love.graphics.setFont(State.font)
   App.color(Text_color)
@@ -283,8 +305,10 @@ 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
-      Text.search_next(State)
+      if #State.search_term > 0 then
+        edit.put_cursor_on_next_loc_wrapping_around_if_necessary(State)
+        Text.search_next(State)
+      end
     elseif chord == 'up' then
       Text.search_previous(State)
     end
diff --git a/search.lua b/search.lua
index d3a5fea..4aa1f02 100644
--- a/search.lua
+++ b/search.lua
@@ -17,6 +17,7 @@ function Text.draw_search_bar(State)
 end
 
 function Text.search_next(State)
+  if #State.search_term == 0 then return end
   -- search current line from cursor
   local curr_pos = State.cursor1.pos
   local curr_line = State.lines[State.cursor1.line].data
@@ -71,6 +72,7 @@ function Text.search_next(State)
 end
 
 function Text.search_previous(State)
+  if #State.search_term == 0 then return end
   -- search current line before cursor
   local curr_pos = State.cursor1.pos
   local curr_line = State.lines[State.cursor1.line].data
diff --git a/source_edit.lua b/source_edit.lua
index 53bd16a..7599a4a 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
 
@@ -407,9 +428,14 @@ function edit.keychord_press(State, chord, key)
       local len = utf8.len(State.search_term)
       local byte_offset = Text.offset(State.search_term, len)
       State.search_term = string.sub(State.search_term, 1, byte_offset-1)
-    elseif chord == 'down' then
-      State.cursor1.pos = State.cursor1.pos+1
+      State.cursor = deepcopy(State.search_backup.cursor)
+      State.screen_top = deepcopy(State.search_backup.screen_top)
       Text.search_next(State)
+    elseif chord == 'down' then
+      if #State.search_term > 0 then
+        edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
+        Text.search_next(State)
+      end
     elseif chord == 'up' then
       Text.search_previous(State)
     end
diff --git a/source_text_tests.lua b/source_text_tests.lua
index 11cc823..7ed4caa 100644
--- a/source_text_tests.lua
+++ b/source_text_tests.lua
@@ -2073,3 +2073,31 @@ function test_search_wrap_upwards()
   check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
   check_eq(Editor_state.cursor1.pos, 6, '1/cursor:pos')
 end
+
+function test_search_downwards_from_end_of_line()
+  App.screen.init{width=120, height=60}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.lines = load_array{'abc', 'def', 'ghi'}
+  Text.redraw_all(Editor_state)
+  Editor_state.cursor1 = {line=1, pos=4}
+  Editor_state.screen_top1 = {line=1, pos=1}
+  edit.draw(Editor_state)
+  -- search for empty string
+  edit.run_after_keychord(Editor_state, 'C-f', 'f')
+  edit.run_after_keychord(Editor_state, 'down', 'down')
+  -- no crash
+end
+
+function test_search_downwards_from_final_pos_of_line()
+  App.screen.init{width=120, height=60}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.lines = load_array{'abc', 'def', 'ghi'}
+  Text.redraw_all(Editor_state)
+  Editor_state.cursor1 = {line=1, pos=3}
+  Editor_state.screen_top1 = {line=1, pos=1}
+  edit.draw(Editor_state)
+  -- search for empty string
+  edit.run_after_keychord(Editor_state, 'C-f', 'f')
+  edit.run_after_keychord(Editor_state, 'down', 'down')
+  -- no crash
+end
diff --git a/text_tests.lua b/text_tests.lua
index 9b3b60c..7bf2221 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -1950,3 +1950,31 @@ function test_search_wrap_upwards()
   check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
   check_eq(Editor_state.cursor1.pos, 6, '1/cursor:pos')
 end
+
+function test_search_downwards_from_end_of_line()
+  App.screen.init{width=120, height=60}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.lines = load_array{'abc', 'def', 'ghi'}
+  Text.redraw_all(Editor_state)
+  Editor_state.cursor1 = {line=1, pos=4}
+  Editor_state.screen_top1 = {line=1, pos=1}
+  edit.draw(Editor_state)
+  -- search for empty string
+  edit.run_after_keychord(Editor_state, 'C-f', 'f')
+  edit.run_after_keychord(Editor_state, 'down', 'down')
+  -- no crash
+end
+
+function test_search_downwards_from_final_pos_of_line()
+  App.screen.init{width=120, height=60}
+  Editor_state = edit.initialize_test_state()
+  Editor_state.lines = load_array{'abc', 'def', 'ghi'}
+  Text.redraw_all(Editor_state)
+  Editor_state.cursor1 = {line=1, pos=3}
+  Editor_state.screen_top1 = {line=1, pos=1}
+  edit.draw(Editor_state)
+  -- search for empty string
+  edit.run_after_keychord(Editor_state, 'C-f', 'f')
+  edit.run_after_keychord(Editor_state, 'down', 'down')
+  -- no crash
+end