diff options
-rw-r--r-- | modules/textadept/find.lua | 11 | ||||
-rw-r--r-- | test/test.lua | 17 |
2 files changed, 24 insertions, 4 deletions
diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua index 59d2c002..b8d37eed 100644 --- a/modules/textadept/find.lua +++ b/modules/textadept/find.lua @@ -166,6 +166,7 @@ events.connect(events.KEYPRESS, function(code) clear_highlighted_matches() end, 1) +local incremental_orig_pos -- Finds and selects text in the current buffer. -- @param text The text to find. -- @param next Flag indicating whether or not the search direction is forward. @@ -186,15 +187,17 @@ local function find(text, next, flags, no_wrap, wrapped) local first_visible_line = view.first_visible_line -- for 'no results found' if M.incremental and not wrapped then + local pos = buffer.current_pos if type(M.incremental) == 'boolean' then -- Starting a new incremental search, anchor at current pos. - M.incremental = buffer.current_pos + M.incremental, incremental_orig_pos = pos, pos elseif text == find_text then -- "Find Next" or "Find Prev" clicked, anchor at new current pos. - M.incremental = buffer:position_relative( - buffer.current_pos, next and 1 or -1) + M.incremental = buffer:position_relative(pos, next and 1 or -1) end buffer:goto_pos(M.incremental or 1) + elseif not M.incremental then + incremental_orig_pos = nil end -- If text is selected, assume it is from the current search and move the @@ -221,7 +224,7 @@ local function find(text, next, flags, no_wrap, wrapped) if pos == -1 then ui.statusbar_text = _L['No results found'] view:line_scroll(0, first_visible_line - view.first_visible_line) - buffer:goto_pos(anchor) + buffer:goto_pos(incremental_orig_pos or anchor) end elseif not wrapped then ui.statusbar_text = '' diff --git a/test/test.lua b/test/test.lua index d89937a7..be2d432f 100644 --- a/test/test.lua +++ b/test/test.lua @@ -2310,6 +2310,23 @@ function test_ui_find_incremental_highlight() buffer:close(true) end +function test_ui_find_incremental_not_found() + buffer.new() + buffer:set_text('foobar') + ui.find.incremental = true + ui.find.find_entry_text = 'b' + if CURSES then events.emit(events.FIND_TEXT_CHANGED) end -- simulate + assert_equal(buffer.current_pos, 4) + ui.find.find_entry_text = 'bb' + if CURSES then events.emit(events.FIND_TEXT_CHANGED) end -- simulate + assert_equal(buffer.current_pos, 1) + events.emit(events.FIND, ui.find.find_entry_text, true) -- simulate Find Next + assert_equal(buffer.current_pos, 1) -- cursor did not advance + ui.find.find_entry_text = '' + ui.find.incremental = false + buffer:close(true) +end + function test_ui_find_find_in_files() ui.find.find_entry_text = 'foo' ui.find.match_case = true |