InputDialog: add search (#7701)

Searches for a string in the edited text.
Available in the Text editor and other input dialogs with the navigation bar enabled.

Find first searches from the beginning of the text.
Find next searches from the next to cursor position, used for continious search.
By now, the Search input window is closed after the search. You need to press the Find button again for continious search, the search string is kept in the input.
Is it better to keep the dialog open for the comfortable continious search? And close it with the Cancel only?

Case insensitive. Cursor jumps to the beginning of the found string.
Notifications are shown for better results visibility.

Unfortunately, violated our standartization to "search", couldn't invent better short wordings.
This commit is contained in:
hius07
2021-05-19 19:05:16 +03:00
committed by GitHub
parent 4afcca9d6e
commit f3fe643d81
2 changed files with 89 additions and 1 deletions

View File

@@ -533,6 +533,29 @@ function InputText:getLineCharPos(line_num)
return char_pos
end
--- Search for a string.
-- if start_pos not set, starts a search from the next to cursor position
-- returns first found position or 0 if not found
function InputText:searchString(str, start_pos)
local str_len = string.len(str)
local char_pos, found = 0, 0
start_pos = start_pos and (start_pos - 1) or self.charpos
for i = start_pos, #self.charlist - str_len do
for j = 1, str_len do
if string.lower(self.charlist[i + j]) ~= string.lower(string.sub(str, j, j)) then
found = 0
break
end
found = found + 1
end
if found == str_len then
char_pos = i + 1
break
end
end
return char_pos
end
function InputText:addChars(chars)
if not chars then
-- VirtualKeyboard:addChar(key) gave us 'nil' once (?!)