about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--edit.lua10
-rw-r--r--search.lua2
-rw-r--r--source_edit.lua10
-rw-r--r--source_text_tests.lua14
-rw-r--r--text_tests.lua14
5 files changed, 44 insertions, 6 deletions
diff --git a/edit.lua b/edit.lua
index 9e80cc3..91ea8e0 100644
--- a/edit.lua
+++ b/edit.lua
@@ -426,10 +426,14 @@ function edit.keychord_press(State, chord, key)
       State.screen_top = deepcopy(State.search_backup.screen_top)
       Text.search_next(State)
     elseif chord == 'down' then
-      edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
-      Text.search_next(State)
+      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)
+      if #State.search_term > 0 then
+        Text.search_previous(State)
+      end
     end
     return
   elseif chord == 'C-f' then
diff --git a/search.lua b/search.lua
index d3a5fea..2b4ea99 100644
--- a/search.lua
+++ b/search.lua
@@ -17,6 +17,7 @@ function Text.draw_search_bar(State)
 end
 
 function Text.search_next(State)
+  assert(#State.search_term > 0)
   -- 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)
+  assert(#State.search_term > 0)
   -- 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 90c1171..733ca61 100644
--- a/source_edit.lua
+++ b/source_edit.lua
@@ -432,10 +432,14 @@ function edit.keychord_press(State, chord, key)
       State.screen_top = deepcopy(State.search_backup.screen_top)
       Text.search_next(State)
     elseif chord == 'down' then
-      edit.put_cursor_on_next_text_loc_wrapping_around_if_necessary(State)
-      Text.search_next(State)
+      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)
+      if #State.search_term > 0 then
+        Text.search_previous(State)
+      end
     end
     return
   elseif chord == 'C-f' then
diff --git a/source_text_tests.lua b/source_text_tests.lua
index db3ae44..7ed4caa 100644
--- a/source_text_tests.lua
+++ b/source_text_tests.lua
@@ -2087,3 +2087,17 @@ function test_search_downwards_from_end_of_line()
   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 aebade8..7168daf 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -2087,3 +2087,17 @@ function test_search_downwards_from_end_of_line()
   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