diff --git a/search.lua b/search.lua index 4ebd8ab..1872b9e 100644 --- a/search.lua +++ b/search.lua @@ -68,12 +68,13 @@ function Text.search_next(State) end function Text.search_previous(State) - -- search current line + -- search current line before cursor local pos = rfind(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos-1) if pos then State.cursor1.pos = pos end if pos == nil then + -- search lines above cursor for i=State.cursor1.line-1,1,-1 do pos = rfind(State.lines[i].data, State.search_term) if pos then @@ -94,6 +95,13 @@ function Text.search_previous(State) end end end + if pos == nil then + -- search current line after cursor + pos = rfind(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 State.cursor1.line = State.search_backup.cursor.line State.cursor1.pos = State.search_backup.cursor.pos diff --git a/text_tests.lua b/text_tests.lua index a37739d..fa4c173 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -2083,3 +2083,22 @@ function test_search_wrap() 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 + +function test_search_wrap_upwards() + io.write('\ntest_search_wrap_upwards') + App.screen.init{width=120, height=60} + Editor_state = edit.initialize_test_state() + Editor_state.lines = load_array{'abc abd'} + Text.redraw_all(Editor_state) + Editor_state.cursor1 = {line=1, pos=1} + Editor_state.screen_top1 = {line=1, pos=1} + Editor_state.screen_bottom1 = {} + edit.draw(Editor_state) + -- search upwards for a string + edit.run_after_keychord(Editor_state, 'C-f') + edit.run_after_textinput(Editor_state, 'a') + edit.run_after_keychord(Editor_state, 'up') + -- cursor wraps + check_eq(Editor_state.cursor1.line, 1, 'F - test_search_wrap_upwards/1/cursor:line') + check_eq(Editor_state.cursor1.pos, 5, 'F - test_search_wrap_upwards/1/cursor:pos') +end