From 0afd03e72128f691d702ec67f317e6fbf407724e Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 11 Aug 2022 20:09:59 -0700 Subject: [PATCH] bugfix: check before cursor on same line --- search.lua | 13 ++++++++++++- text_tests.lua | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/search.lua b/search.lua index b578668..95a7f32 100644 --- a/search.lua +++ b/search.lua @@ -21,7 +21,7 @@ end function Text.search_next(State) local pos - -- search current line + -- 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) @@ -30,6 +30,7 @@ function Text.search_next(State) end 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 @@ -54,6 +55,16 @@ function Text.search_next(State) end 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 + end + end + if pos == 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 diff --git a/text_tests.lua b/text_tests.lua index e5e7823..1167355 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -2045,3 +2045,22 @@ function test_search() check_eq(Editor_state.cursor1.line, 4, 'F - test_search/2/cursor:line') check_eq(Editor_state.cursor1.pos, 1, 'F - test_search/2/cursor:pos') end + +function test_search_wrap() + io.write('\ntest_search_wrap') + App.screen.init{width=120, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=3} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + edit.draw(Editor_state) + -- search for a string + edit.run_after_keychord(Editor_state, 'C-f') + edit.run_after_textinput(Editor_state, 'a') + edit.run_after_keychord(Editor_state, 'return') + -- cursor wraps + check_eq(Editor_state.cursor1.line, 1, 'F - test_search_wrap/1/cursor:line') + check_eq(Editor_state.cursor1.pos, 1, 'F - test_search_wrap/1/cursor:pos') +end -- 2.11.4.GIT