diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index c32a76b632..c365bf7b40 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -503,20 +503,20 @@ class TextEdit(PlainTextEdit): if wrap and not complete: pos = QTextCursor.MoveOperation.End if reverse else QTextCursor.MoveOperation.Start c.movePosition(pos, QTextCursor.MoveMode.KeepAnchor) - raw = str(c.selectedText()).replace(PARAGRAPH_SEPARATOR, '\n').rstrip('\0') if hasattr(self.smarts, 'find_text'): self.highlighter.join() found, start, end = self.smarts.find_text(pat, c, reverse) if not found: return False else: + raw = str(c.selectedText()).replace(PARAGRAPH_SEPARATOR, '\n').rstrip('\0') m = pat.search(raw) if m is None: return False start, end = m.span() if start == end: return False - end = start + utf16_length(raw[start:end]) + end = start + utf16_length(raw[start:end]) if reverse: start, end = end, start c.clearSelection() diff --git a/src/calibre/gui2/tweak_book/text_search.py b/src/calibre/gui2/tweak_book/text_search.py index d67c712dc8..aab65326a1 100644 --- a/src/calibre/gui2/tweak_book/text_search.py +++ b/src/calibre/gui2/tweak_book/text_search.py @@ -17,6 +17,7 @@ from calibre.gui2.tweak_book.search import ( from calibre.gui2.widgets import BusyCursor from calibre.gui2.widgets2 import HistoryComboBox from calibre.startup import connect_lambda +from calibre.utils.icu import utf16_length from polyglot.builtins import error_message, iteritems # UI {{{ @@ -219,7 +220,7 @@ def find_text_in_chunks(pat, chunks): start_pos = chunk_start + (start - offset) if start_pos is not None: if contains(clen, after-1): - end_pos = chunk_start + (after - offset) + end_pos = chunk_start + utf16_length(chunk[:after-offset]) return start_pos, end_pos offset += clen if offset > after: