2019-12-06 21:55:39 +00:00
|
|
|
local BD = require("ui/bidi")
|
2014-11-05 04:28:11 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
2021-07-08 17:30:16 +00:00
|
|
|
local CheckButton = require("ui/widget/checkbutton")
|
|
|
|
local Device = require("device")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2017-04-07 13:20:57 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2021-02-28 03:13:05 +00:00
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
2021-04-01 11:19:36 +00:00
|
|
|
local Notification = require("ui/widget/notification")
|
2014-11-05 04:28:11 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2014-11-05 04:28:11 +00:00
|
|
|
local _ = require("gettext")
|
2021-09-06 15:08:48 +00:00
|
|
|
local Screen = Device.screen
|
2021-07-15 09:51:10 +00:00
|
|
|
local T = require("ffi/util").template
|
2014-11-05 04:28:11 +00:00
|
|
|
|
|
|
|
local ReaderSearch = InputContainer:new{
|
|
|
|
direction = 0, -- 0 for search forward, 1 for search backward
|
2014-11-17 09:58:25 +00:00
|
|
|
case_insensitive = true, -- default to case insensitive
|
2018-02-04 19:57:13 +00:00
|
|
|
|
2021-07-08 17:30:16 +00:00
|
|
|
-- For a regex like [a-z\. ] many many hits are found, maybe the number of chars on a few pages.
|
|
|
|
-- We don't try to catch them all as this is a reader and not a computer science playground. ;)
|
|
|
|
-- So if some regex gets more than max_hits a notification will be shown.
|
|
|
|
-- Increasing max_hits will slow down search for nasty regex. There is no slowdown for friendly
|
|
|
|
-- regexs like `Max|Moritz` for `One|Two|Three`
|
|
|
|
-- The speed of the search depends on the regexs. Complex ones might need some time, easy ones
|
|
|
|
-- go with the speed of light.
|
|
|
|
-- Setting max_hits higher, does not mean to require more memory. More hits means smaller single hits.
|
|
|
|
max_hits = 2048, -- maximum hits for search; timinges tested on a Tolino
|
|
|
|
|
2018-02-04 19:57:13 +00:00
|
|
|
-- internal: whether we expect results on previous pages
|
|
|
|
-- (can be different from self.direction, if, from a page in the
|
|
|
|
-- middle of a book, we search forward from start of book)
|
|
|
|
_expect_back_results = false,
|
2014-11-05 04:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ReaderSearch:init()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
2021-10-23 11:39:53 +00:00
|
|
|
local help_text = _([[
|
|
|
|
Regular expressions allow you to search for a matching pattern in a text. The simplest pattern is a simple sequence of characters, such as `James Bond`. There are many different varieties of regular expressions, but we support the ECMAScript syntax. The basics will be explained below.
|
2021-07-08 17:30:16 +00:00
|
|
|
|
|
|
|
If you want to search for all occurrences of 'Mister Moore', 'Sir Moore' or 'Alfons Moore' but not for 'Lady Moore'.
|
|
|
|
Enter 'Mister Moore|Sir Moore|Alfons Moore'.
|
|
|
|
|
|
|
|
If your search contains a special character from ^$.*+?()[]{}|\/ you have to put a \ before that character.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Words containing 'os' -> '[^ ]+os[^ ]+'
|
|
|
|
Any single character '.' -> 'r.nge'
|
|
|
|
Any characters '.*' -> 'J.*s'
|
|
|
|
Numbers -> '[0-9]+'
|
|
|
|
Character range -> '[a-f]'
|
|
|
|
Not a space -> '[^ ]'
|
|
|
|
A word -> '[^ ]*[^ ]'
|
|
|
|
Last word in a sentence -> '[^ ]*\.'
|
|
|
|
|
2021-10-23 11:39:53 +00:00
|
|
|
Complex expressions may lead to an extremely long search time, in which case not all matches will be shown.]])
|
2021-07-08 17:30:16 +00:00
|
|
|
|
|
|
|
local SRELL_ERROR_CODES = {}
|
2021-07-10 17:03:12 +00:00
|
|
|
SRELL_ERROR_CODES[102] = _("Wrong escape '\\'")
|
2021-07-08 17:30:16 +00:00
|
|
|
SRELL_ERROR_CODES[103] = _("Back reference does not exist.")
|
|
|
|
SRELL_ERROR_CODES[104] = _("Mismatching brackets '[]'")
|
|
|
|
SRELL_ERROR_CODES[105] = _("Mismatched parens '()'")
|
|
|
|
SRELL_ERROR_CODES[106] = _("Mismatched brace '{}'")
|
|
|
|
SRELL_ERROR_CODES[107] = _("Invalid Range in '{}'")
|
|
|
|
SRELL_ERROR_CODES[108] = _("Invalid character range")
|
|
|
|
SRELL_ERROR_CODES[110] = _("No preceding expression in repetition.")
|
|
|
|
SRELL_ERROR_CODES[111] = _("Expression too complex, some hits will not be shown.")
|
|
|
|
SRELL_ERROR_CODES[666] = _("Expression may lead to an extremely long search time.")
|
|
|
|
|
2017-03-04 13:46:38 +00:00
|
|
|
function ReaderSearch:addToMainMenu(menu_items)
|
|
|
|
menu_items.fulltext_search = {
|
2014-11-05 04:28:11 +00:00
|
|
|
text = _("Fulltext search"),
|
2019-03-02 20:36:30 +00:00
|
|
|
callback = function()
|
|
|
|
self:onShowFulltextSearchInput()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-07-15 09:51:10 +00:00
|
|
|
-- if reverse ~= 0 search backwards
|
|
|
|
function ReaderSearch:searchCallback(reverse)
|
2021-11-18 15:54:07 +00:00
|
|
|
local search_text = self.input_dialog:getInputText()
|
|
|
|
if search_text == "" then return end
|
|
|
|
self.last_search_text = search_text
|
2021-07-15 09:51:10 +00:00
|
|
|
self.use_regex = self.check_button_regex.checked
|
|
|
|
self.case_insensitive = not self.check_button_case.checked
|
2021-11-18 15:54:07 +00:00
|
|
|
local regex_error = self.use_regex and self.ui.document:checkRegex(search_text)
|
2021-07-15 09:51:10 +00:00
|
|
|
if self.use_regex and regex_error ~= 0 then
|
|
|
|
logger.dbg("ReaderSearch: regex error", regex_error, SRELL_ERROR_CODES[regex_error])
|
|
|
|
local error_message
|
|
|
|
if SRELL_ERROR_CODES[regex_error] then
|
|
|
|
error_message = T(_("Invalid regular expression:\n%1"), SRELL_ERROR_CODES[regex_error])
|
|
|
|
else
|
|
|
|
error_message = _("Invalid regular expression.")
|
|
|
|
end
|
|
|
|
UIManager:show(InfoMessage:new{ text = error_message })
|
|
|
|
else
|
|
|
|
UIManager:close(self.input_dialog)
|
2021-11-18 15:54:07 +00:00
|
|
|
self:onShowSearchDialog(search_text, reverse, self.use_regex, self.case_insensitive)
|
2021-07-15 09:51:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 20:36:30 +00:00
|
|
|
function ReaderSearch:onShowFulltextSearchInput()
|
2019-12-06 21:55:39 +00:00
|
|
|
local backward_text = "◁"
|
|
|
|
local forward_text = "▷"
|
|
|
|
if BD.mirroredUILayout() then
|
|
|
|
backward_text, forward_text = forward_text, backward_text
|
|
|
|
end
|
2021-02-28 03:13:05 +00:00
|
|
|
self.input_dialog = InputDialog:new{
|
2019-03-02 20:36:30 +00:00
|
|
|
title = _("Enter text to search for"),
|
2021-09-17 17:36:57 +00:00
|
|
|
width = math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * 0.9),
|
2021-07-08 17:30:16 +00:00
|
|
|
input = self.last_search_text,
|
2019-08-30 11:27:03 +00:00
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
2021-02-28 03:13:05 +00:00
|
|
|
UIManager:close(self.input_dialog)
|
2019-08-30 11:27:03 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = backward_text,
|
2019-08-30 11:27:03 +00:00
|
|
|
callback = function()
|
2021-07-15 09:51:10 +00:00
|
|
|
self:searchCallback(1)
|
2019-08-30 11:27:03 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = forward_text,
|
2019-08-30 11:27:03 +00:00
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
2021-07-15 09:51:10 +00:00
|
|
|
self:searchCallback(0)
|
2019-08-30 11:27:03 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-28 21:46:32 +00:00
|
|
|
}
|
2021-07-08 17:30:16 +00:00
|
|
|
|
|
|
|
self.check_button_case = CheckButton:new{
|
|
|
|
text = _("Case sensitive"),
|
|
|
|
checked = not self.case_insensitive,
|
|
|
|
parent = self.input_dialog,
|
2021-08-13 22:49:19 +00:00
|
|
|
max_width = self.input_dialog._input_widget.width,
|
2021-07-08 17:30:16 +00:00
|
|
|
callback = function()
|
2021-08-27 19:21:14 +00:00
|
|
|
self.check_button_case:toggleCheck()
|
2021-07-08 17:30:16 +00:00
|
|
|
end,
|
|
|
|
}
|
2021-09-02 20:53:54 +00:00
|
|
|
self.input_dialog:addWidget(self.check_button_case)
|
2021-07-08 17:30:16 +00:00
|
|
|
self.check_button_regex = CheckButton:new{
|
2021-09-06 15:08:48 +00:00
|
|
|
text = _("Regular expression (long-press for help)"),
|
2021-07-08 17:30:16 +00:00
|
|
|
checked = self.use_regex,
|
|
|
|
parent = self.input_dialog,
|
2021-08-13 22:49:19 +00:00
|
|
|
max_width = self.input_dialog._input_widget.width,
|
2021-07-08 17:30:16 +00:00
|
|
|
callback = function()
|
2021-08-27 19:21:14 +00:00
|
|
|
self.check_button_regex:toggleCheck()
|
2021-07-08 17:30:16 +00:00
|
|
|
end,
|
|
|
|
hold_callback = function()
|
2021-09-17 17:36:57 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = help_text,
|
|
|
|
width = Screen:getWidth() * 0.9,
|
|
|
|
})
|
2021-07-08 17:30:16 +00:00
|
|
|
end,
|
|
|
|
}
|
2021-11-18 15:54:07 +00:00
|
|
|
if self.ui.rolling then
|
|
|
|
self.input_dialog:addWidget(self.check_button_regex)
|
|
|
|
end
|
2021-07-08 17:30:16 +00:00
|
|
|
|
2021-02-28 03:13:05 +00:00
|
|
|
UIManager:show(self.input_dialog)
|
|
|
|
self.input_dialog:onShowKeyboard()
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
2021-07-08 17:30:16 +00:00
|
|
|
function ReaderSearch:onShowSearchDialog(text, direction, regex, case_insensitive)
|
2018-01-31 12:28:26 +00:00
|
|
|
local neglect_current_location = false
|
2018-02-04 19:57:13 +00:00
|
|
|
local current_page
|
2021-07-08 17:30:16 +00:00
|
|
|
|
|
|
|
local function isSlowRegex(pattern)
|
|
|
|
if pattern:find("%[") or pattern:find("%*") or pattern:find("%?") or pattern:find("%.") then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2016-06-27 16:43:23 +00:00
|
|
|
local do_search = function(search_func, _text, param)
|
2014-11-05 04:28:11 +00:00
|
|
|
return function()
|
2021-04-01 11:19:36 +00:00
|
|
|
local no_results = true -- for notification
|
2021-07-08 17:30:16 +00:00
|
|
|
local res = search_func(self, _text, param, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
if res then
|
2014-11-17 09:58:25 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
2021-04-01 11:19:36 +00:00
|
|
|
no_results = false
|
2018-01-31 12:28:26 +00:00
|
|
|
self.ui.link:onGotoLink({page = res.page - 1}, neglect_current_location)
|
2014-11-17 09:58:25 +00:00
|
|
|
self.view.highlight.temp[res.page] = res
|
|
|
|
else
|
2018-02-04 19:57:13 +00:00
|
|
|
-- Was previously just:
|
|
|
|
-- self.ui.link:onGotoLink(res[1].start, neglect_current_location)
|
|
|
|
|
|
|
|
-- To avoid problems with edge cases, crengine may now give us links
|
|
|
|
-- that are on previous/next page of the page we should show. And
|
|
|
|
-- sometimes even xpointers that resolve to no page.
|
|
|
|
-- We need to loop thru all the results until we find one suitable,
|
|
|
|
-- to follow its link and go to the next/prev page with occurences.
|
|
|
|
local valid_link
|
|
|
|
-- If backward search, results are already in a reversed order, so we'll
|
|
|
|
-- start from the nearest to current page one.
|
|
|
|
for _, r in ipairs(res) do
|
|
|
|
-- result's start and end may be on different pages, we must
|
|
|
|
-- consider both
|
|
|
|
local r_start = r["start"]
|
|
|
|
local r_end = r["end"]
|
|
|
|
local r_start_page = self.ui.document:getPageFromXPointer(r_start)
|
|
|
|
local r_end_page = self.ui.document:getPageFromXPointer(r_end)
|
|
|
|
logger.dbg("res.start page & xpointer:", r_start_page, r_start)
|
|
|
|
logger.dbg("res.end page & xpointer:", r_end_page, r_end)
|
|
|
|
local bounds = {}
|
|
|
|
if self._expect_back_results then
|
|
|
|
-- Process end of occurence first, which is nearest to current page
|
|
|
|
table.insert(bounds, {r_end, r_end_page})
|
|
|
|
table.insert(bounds, {r_start, r_start_page})
|
|
|
|
else
|
|
|
|
table.insert(bounds, {r_start, r_start_page})
|
|
|
|
table.insert(bounds, {r_end, r_end_page})
|
|
|
|
end
|
|
|
|
for _, b in ipairs(bounds) do
|
|
|
|
local xpointer = b[1]
|
|
|
|
local page = b[2]
|
|
|
|
-- Look if it is valid for us
|
|
|
|
if page then -- it should resolve to a page
|
|
|
|
if not current_page then -- initial search
|
|
|
|
-- We can (and should if there are) display results on current page
|
|
|
|
current_page = self.ui.document:getCurrentPage()
|
|
|
|
if (self._expect_back_results and page <= current_page) or
|
|
|
|
(not self._expect_back_results and page >= current_page) then
|
|
|
|
valid_link = xpointer
|
|
|
|
end
|
|
|
|
else -- subsequent searches
|
|
|
|
-- We must change page, so only consider results from
|
|
|
|
-- another page, in the adequate search direction
|
|
|
|
current_page = self.ui.document:getCurrentPage()
|
|
|
|
if (self._expect_back_results and page < current_page) or
|
|
|
|
(not self._expect_back_results and page > current_page) then
|
|
|
|
valid_link = xpointer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if valid_link then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if valid_link then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if valid_link then
|
2021-04-01 11:19:36 +00:00
|
|
|
no_results = false
|
2018-02-09 16:06:16 +00:00
|
|
|
self.ui.link:onGotoLink({xpointer=valid_link}, neglect_current_location)
|
2018-02-04 19:57:13 +00:00
|
|
|
end
|
2014-11-17 09:58:25 +00:00
|
|
|
end
|
2018-01-31 12:28:26 +00:00
|
|
|
-- Don't add result pages to location ("Go back") stack
|
|
|
|
neglect_current_location = true
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
2021-04-01 11:19:36 +00:00
|
|
|
if no_results then
|
|
|
|
local notification_text
|
|
|
|
if self._expect_back_results then
|
|
|
|
notification_text = _("No results on preceding pages")
|
|
|
|
else
|
|
|
|
notification_text = _("No results on following pages")
|
|
|
|
end
|
|
|
|
UIManager:show(Notification:new{
|
|
|
|
text = notification_text,
|
|
|
|
})
|
|
|
|
end
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
end
|
2019-12-06 21:55:39 +00:00
|
|
|
local from_start_text = "▕◁"
|
|
|
|
local backward_text = "◁"
|
|
|
|
local forward_text = "▷"
|
|
|
|
local from_end_text = "▷▏"
|
|
|
|
if BD.mirroredUILayout() then
|
|
|
|
backward_text, forward_text = forward_text, backward_text
|
|
|
|
-- Keep the LTR order of |< and >|:
|
|
|
|
from_start_text, from_end_text = BD.ltr(from_end_text), BD.ltr(from_start_text)
|
|
|
|
end
|
2021-07-08 17:30:16 +00:00
|
|
|
self.wait_button = ButtonDialog:new{
|
|
|
|
buttons = {{{ text = "⌛" }}},
|
|
|
|
}
|
|
|
|
local function search(func, pattern, param)
|
|
|
|
if regex and isSlowRegex(pattern) then
|
|
|
|
return function()
|
|
|
|
self.wait_button.alpha = 0.75
|
2021-07-15 09:51:10 +00:00
|
|
|
self.wait_button.movable:setMovedOffset(self.search_dialog.movable:getMovedOffset())
|
2021-07-08 17:30:16 +00:00
|
|
|
UIManager:show(self.wait_button)
|
|
|
|
UIManager:tickAfterNext(function()
|
|
|
|
do_search(func, pattern, param, regex, case_insensitive)()
|
|
|
|
UIManager:close(self.wait_button)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return do_search(func, pattern, param, regex, case_insensitive)
|
|
|
|
end
|
|
|
|
end
|
2014-11-05 04:28:11 +00:00
|
|
|
self.search_dialog = ButtonDialog:new{
|
2018-01-29 20:27:24 +00:00
|
|
|
-- alpha = 0.7,
|
2014-11-05 04:28:11 +00:00
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = from_start_text,
|
Assorted fixes after #7118 (#7161)
* I'd failed to notice that ButtonTable *also* instantiates seven billion Buttons on each update. Unfortunately, that one is way trickier to fix properly, so, work around its behavior in Button. (This fixes multiple issues with stuff using ButtonTable, which is basically anything with a persistent set of buttons. A good and easy test-case is the dictionary popup, e.g., the Highlight button changes text, and the next/prev dic buttons change state. All that, and more, was broken ;p).
* Handle corner-cases related to VirtualKeyboard (e.g., Terminal & Text Editor), which screwed with both TouchMenu & Button heuristics because it's weird.
* Flag a the dictionary switch buttons as vsync
(They trigger a partial repaint of the dictionary content).
* Flag the ReaderSearch buttons as vsync
They very obviously trigger a partial repaint, much like SkimTo ;p.
2021-01-18 15:51:25 +00:00
|
|
|
vsync = true,
|
2021-07-08 17:30:16 +00:00
|
|
|
callback = search(self.searchFromStart, text, nil),
|
2014-11-05 04:28:11 +00:00
|
|
|
},
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = backward_text,
|
Assorted fixes after #7118 (#7161)
* I'd failed to notice that ButtonTable *also* instantiates seven billion Buttons on each update. Unfortunately, that one is way trickier to fix properly, so, work around its behavior in Button. (This fixes multiple issues with stuff using ButtonTable, which is basically anything with a persistent set of buttons. A good and easy test-case is the dictionary popup, e.g., the Highlight button changes text, and the next/prev dic buttons change state. All that, and more, was broken ;p).
* Handle corner-cases related to VirtualKeyboard (e.g., Terminal & Text Editor), which screwed with both TouchMenu & Button heuristics because it's weird.
* Flag a the dictionary switch buttons as vsync
(They trigger a partial repaint of the dictionary content).
* Flag the ReaderSearch buttons as vsync
They very obviously trigger a partial repaint, much like SkimTo ;p.
2021-01-18 15:51:25 +00:00
|
|
|
vsync = true,
|
2021-07-08 17:30:16 +00:00
|
|
|
callback = search(self.searchNext, text, 1),
|
2014-11-05 04:28:11 +00:00
|
|
|
},
|
2021-09-09 23:07:31 +00:00
|
|
|
{
|
|
|
|
icon = "appbar.search",
|
2021-09-11 06:28:44 +00:00
|
|
|
icon_width = Screen:scaleBySize(DGENERIC_ICON_SIZE * 0.8),
|
|
|
|
icon_height = Screen:scaleBySize(DGENERIC_ICON_SIZE * 0.8),
|
2021-09-09 23:07:31 +00:00
|
|
|
callback = function()
|
|
|
|
self.search_dialog:onClose()
|
|
|
|
self.last_search_text = text
|
|
|
|
self:onShowFulltextSearchInput()
|
|
|
|
end,
|
|
|
|
},
|
2014-11-05 04:28:11 +00:00
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = forward_text,
|
Assorted fixes after #7118 (#7161)
* I'd failed to notice that ButtonTable *also* instantiates seven billion Buttons on each update. Unfortunately, that one is way trickier to fix properly, so, work around its behavior in Button. (This fixes multiple issues with stuff using ButtonTable, which is basically anything with a persistent set of buttons. A good and easy test-case is the dictionary popup, e.g., the Highlight button changes text, and the next/prev dic buttons change state. All that, and more, was broken ;p).
* Handle corner-cases related to VirtualKeyboard (e.g., Terminal & Text Editor), which screwed with both TouchMenu & Button heuristics because it's weird.
* Flag a the dictionary switch buttons as vsync
(They trigger a partial repaint of the dictionary content).
* Flag the ReaderSearch buttons as vsync
They very obviously trigger a partial repaint, much like SkimTo ;p.
2021-01-18 15:51:25 +00:00
|
|
|
vsync = true,
|
2021-07-08 17:30:16 +00:00
|
|
|
callback = search(self.searchNext, text, 0),
|
2014-11-05 04:28:11 +00:00
|
|
|
},
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = from_end_text,
|
Assorted fixes after #7118 (#7161)
* I'd failed to notice that ButtonTable *also* instantiates seven billion Buttons on each update. Unfortunately, that one is way trickier to fix properly, so, work around its behavior in Button. (This fixes multiple issues with stuff using ButtonTable, which is basically anything with a persistent set of buttons. A good and easy test-case is the dictionary popup, e.g., the Highlight button changes text, and the next/prev dic buttons change state. All that, and more, was broken ;p).
* Handle corner-cases related to VirtualKeyboard (e.g., Terminal & Text Editor), which screwed with both TouchMenu & Button heuristics because it's weird.
* Flag a the dictionary switch buttons as vsync
(They trigger a partial repaint of the dictionary content).
* Flag the ReaderSearch buttons as vsync
They very obviously trigger a partial repaint, much like SkimTo ;p.
2021-01-18 15:51:25 +00:00
|
|
|
vsync = true,
|
2021-07-08 17:30:16 +00:00
|
|
|
callback = search(self.searchFromEnd, text, nil),
|
2014-11-05 04:28:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tap_close_callback = function()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("highlight clear")
|
2014-11-05 04:28:11 +00:00
|
|
|
self.ui.highlight:clear()
|
2018-03-26 19:10:46 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
2014-11-05 04:28:11 +00:00
|
|
|
end,
|
|
|
|
}
|
2021-07-08 17:30:16 +00:00
|
|
|
if regex and isSlowRegex(text) then
|
|
|
|
self.wait_button.alpha = nil
|
2021-07-15 09:51:10 +00:00
|
|
|
-- initial position: center of the screen
|
2021-07-08 17:30:16 +00:00
|
|
|
UIManager:show(self.wait_button)
|
|
|
|
UIManager:tickAfterNext(function()
|
|
|
|
do_search(self.searchFromCurrent, text, direction, regex, case_insensitive)()
|
|
|
|
UIManager:close(self.wait_button)
|
|
|
|
UIManager:show(self.search_dialog)
|
|
|
|
--- @todo regional
|
|
|
|
UIManager:setDirty(self.dialog, "partial")
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
do_search(self.searchFromCurrent, text, direction, regex, case_insensitive)()
|
|
|
|
UIManager:show(self.search_dialog)
|
|
|
|
--- @todo regional
|
|
|
|
UIManager:setDirty(self.dialog, "partial")
|
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-07-08 17:30:16 +00:00
|
|
|
-- if regex == true, use regular expression in pattern
|
|
|
|
-- if case == true or nil, the search is case insensitive
|
|
|
|
function ReaderSearch:search(pattern, origin, regex, case_insensitive)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("search pattern", pattern)
|
2014-11-05 04:28:11 +00:00
|
|
|
local direction = self.direction
|
2014-11-17 09:58:25 +00:00
|
|
|
local page = self.view.state.page
|
2021-07-08 17:30:16 +00:00
|
|
|
if case_insensitive == nil then
|
|
|
|
case_insensitive = true
|
|
|
|
end
|
|
|
|
Device:setIgnoreInput(true)
|
|
|
|
local retval, words_found = self.ui.document:findText(pattern, origin, direction, case_insensitive, page, regex, self.max_hits)
|
|
|
|
Device:setIgnoreInput(false)
|
2021-07-08 21:30:37 +00:00
|
|
|
local regex_retval = regex and self.ui.document:getAndClearRegexSearchError();
|
|
|
|
if regex and regex_retval ~= 0 then
|
2021-07-08 17:30:16 +00:00
|
|
|
local error_message
|
|
|
|
if SRELL_ERROR_CODES[regex_retval] then
|
|
|
|
error_message = SRELL_ERROR_CODES[regex_retval]
|
|
|
|
else
|
|
|
|
error_message = _("Unspecified error")
|
|
|
|
end
|
|
|
|
UIManager:show(Notification:new{
|
|
|
|
text = error_message,
|
|
|
|
timeout = false,
|
|
|
|
})
|
|
|
|
elseif words_found and words_found > self.max_hits then
|
|
|
|
UIManager:show(Notification:new{
|
|
|
|
text =_("Too many hits"),
|
|
|
|
timeout = 4,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
return retval
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
2021-07-08 17:30:16 +00:00
|
|
|
function ReaderSearch:searchFromStart(pattern, _, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
self.direction = 0
|
2018-02-04 19:57:13 +00:00
|
|
|
self._expect_back_results = true
|
2021-07-08 17:30:16 +00:00
|
|
|
return self:search(pattern, -1, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
2021-07-08 17:30:16 +00:00
|
|
|
function ReaderSearch:searchFromEnd(pattern, _, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
self.direction = 1
|
2018-02-04 19:57:13 +00:00
|
|
|
self._expect_back_results = false
|
2021-07-08 17:30:16 +00:00
|
|
|
return self:search(pattern, -1, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
2021-07-08 17:30:16 +00:00
|
|
|
function ReaderSearch:searchFromCurrent(pattern, direction, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
self.direction = direction
|
2018-02-04 19:57:13 +00:00
|
|
|
self._expect_back_results = direction == 1
|
2021-07-08 17:30:16 +00:00
|
|
|
return self:search(pattern, 0, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- ignore current page and search next occurrence
|
2021-07-08 17:30:16 +00:00
|
|
|
function ReaderSearch:searchNext(pattern, direction, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
self.direction = direction
|
2018-02-04 19:57:13 +00:00
|
|
|
self._expect_back_results = direction == 1
|
2021-07-08 17:30:16 +00:00
|
|
|
return self:search(pattern, 1, regex, case_insensitive)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ReaderSearch
|