From 527c33bb0d654a9e560621b8440ce07251188d69 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 12 Nov 2013 13:40:16 +0530 Subject: [PATCH] Search wrapping for current file searches --- src/calibre/gui2/tweak_book/boss.py | 21 +++++++++++++++++++-- src/calibre/gui2/tweak_book/editor/text.py | 22 +++++++++++++++------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index f9906ee0b6..bf67c540b3 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -311,14 +311,31 @@ class Boss(QObject): else: pass # TODO: Find the first name with a match and open its editor else: + files = [name] pass # marked text TODO: Implement this + def no_match(): + return error_dialog( + self.gui, _('Not found'), _( + 'No matches were found for %s') % state['find'], show=True) + pat = sp.get_regex(state) - if action == 'find': + + def do_find(): found = editor.find(pat) if found: return - # TODO: Handle wrapping, depending on state['where'] + if len(files) == 1: + if not state['wrap']: + return no_match() + found = editor.find(pat, wrap=True) + if not found: + return no_match() + else: + pass # TODO: handle multiple file search + + if action == 'find': + return do_find() def save_book(self): c = current_container() diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index f94b9af84a..9aa04d4017 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -154,11 +154,14 @@ class TextEdit(QPlainTextEdit): self.current_search_mark = None self.update_extra_selections() - def find(self, pat): + def find(self, pat, wrap=False): reverse = pat.flags & regex.REVERSE c = self.textCursor() c.clearSelection() - c.movePosition(c.Start if reverse else c.End, c.KeepAnchor) + pos = c.Start if reverse else c.End + if wrap: + pos = c.End if reverse else c.Start + c.movePosition(pos, c.KeepAnchor) raw = unicode(c.selectedText()).replace(PARAGRAPH_SEPARATOR, '\n') m = pat.search(raw) if m is None: @@ -166,12 +169,17 @@ class TextEdit(QPlainTextEdit): start, end = m.span() if start == end: return False - if reverse: - # Put the cursor at the start of the match - start, end = end, start + if wrap: + if reverse: + textpos = c.anchor() + start, end = textpos + end, textpos + start else: - textpos = c.anchor() - start, end = textpos + start, textpos + end + if reverse: + # Put the cursor at the start of the match + start, end = end, start + else: + textpos = c.anchor() + start, end = textpos + start, textpos + end c.clearSelection() c.setPosition(start) c.setPosition(end, c.KeepAnchor)