From 45e762567b0ed5c1b2ce834f7ea181492d963153 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 26 Jul 2020 22:13:09 +0200 Subject: [PATCH 1/5] Fix for issue 371, regex search replace crash --- nw/gui/doceditor.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 28edba4e..d961ad8c 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -1202,7 +1202,7 @@ class GuiDocEditor(QTextEdit): if self.docSearch.isWholeWord: findOpt |= QTextDocument.FindWholeWords - searchFor = self.docSearch.getSearchText() + searchFor = self.docSearch.getSearchObject() wasFound = self.find(searchFor, findOpt) if not wasFound: if self.docSearch.doNextFile and not isBackward: @@ -1230,6 +1230,7 @@ class GuiDocEditor(QTextEdit): theCursor = self.textCursor() if not theCursor.hasSelection(): + self._findNext() return searchFor = self.docSearch.getSearchText() @@ -1239,10 +1240,13 @@ class GuiDocEditor(QTextEdit): if self.docSearch.doMatchCap: replWith = transferCase(selText, replWith) - if not self.docSearch.isCaseSense: - isMatch = searchFor.lower() == selText.lower() + if self.docSearch.isRegEx: + isMatch = True else: - isMatch = searchFor == selText + if not self.docSearch.isCaseSense: + isMatch = searchFor.lower() == selText.lower() + else: + isMatch = searchFor == selText if isMatch: theCursor.beginEditBlock() @@ -1540,7 +1544,7 @@ class GuiDocEditSearch(QFrame): self.replaceBox.setText(theText) return True - def getSearchText(self): + def getSearchObject(self): """Return the current search text either as text or as a regular expression object. """ @@ -1568,6 +1572,11 @@ class GuiDocEditSearch(QFrame): return theText + def getSearchText(self): + """Return the current search text. + """ + return self.searchBox.text() + def getReplaceText(self): """Return the current replace text. """ From 41b6ffa67b7504085ca39d4e4548c2ccbb4c33b0 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 26 Jul 2020 22:19:29 +0200 Subject: [PATCH 2/5] Drop the whole case check in replace --- nw/gui/doceditor.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index d961ad8c..7a6a9837 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -1237,18 +1237,10 @@ class GuiDocEditor(QTextEdit): replWith = self.docSearch.getReplaceText() selText = theCursor.selectedText() - if self.docSearch.doMatchCap: - replWith = transferCase(selText, replWith) + if searchFor: + if self.docSearch.doMatchCap: + replWith = transferCase(selText, replWith) - if self.docSearch.isRegEx: - isMatch = True - else: - if not self.docSearch.isCaseSense: - isMatch = searchFor.lower() == selText.lower() - else: - isMatch = searchFor == selText - - if isMatch: theCursor.beginEditBlock() theCursor.removeSelectedText() theCursor.insertText(replWith) @@ -1259,7 +1251,6 @@ class GuiDocEditor(QTextEdit): searchFor, replWith, theCursor.blockNumber() )) - if searchFor: self._findNext() return From 2dc225ae1f9e553e31dd78762fb721cc245863e7 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 26 Jul 2020 22:21:35 +0200 Subject: [PATCH 3/5] Revert that ... --- nw/gui/doceditor.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 7a6a9837..d961ad8c 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -1237,10 +1237,18 @@ class GuiDocEditor(QTextEdit): replWith = self.docSearch.getReplaceText() selText = theCursor.selectedText() - if searchFor: - if self.docSearch.doMatchCap: - replWith = transferCase(selText, replWith) + if self.docSearch.doMatchCap: + replWith = transferCase(selText, replWith) + if self.docSearch.isRegEx: + isMatch = True + else: + if not self.docSearch.isCaseSense: + isMatch = searchFor.lower() == selText.lower() + else: + isMatch = searchFor == selText + + if isMatch: theCursor.beginEditBlock() theCursor.removeSelectedText() theCursor.insertText(replWith) @@ -1251,6 +1259,7 @@ class GuiDocEditor(QTextEdit): searchFor, replWith, theCursor.blockNumber() )) + if searchFor: self._findNext() return From cacb0f2db8fe7b3541548445d24ea995c9c17d09 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 26 Jul 2020 22:24:12 +0200 Subject: [PATCH 4/5] Try to capture some user edge cases --- nw/gui/doceditor.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index d961ad8c..b390fb23 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -1237,16 +1237,18 @@ class GuiDocEditor(QTextEdit): replWith = self.docSearch.getReplaceText() selText = theCursor.selectedText() + if searchFor.strip() == "": + return + if self.docSearch.doMatchCap: replWith = transferCase(selText, replWith) - if self.docSearch.isRegEx: - isMatch = True + # Double check that we have a mach in case this is called on a + # plain word selection and not on a search + if not self.docSearch.isCaseSense: + isMatch = searchFor.lower() == selText.lower() else: - if not self.docSearch.isCaseSense: - isMatch = searchFor.lower() == selText.lower() - else: - isMatch = searchFor == selText + isMatch = searchFor == selText if isMatch: theCursor.beginEditBlock() @@ -1259,8 +1261,7 @@ class GuiDocEditor(QTextEdit): searchFor, replWith, theCursor.blockNumber() )) - if searchFor: - self._findNext() + self._findNext() return From a4db7f934d4389eeb347fb38a4a23401f0e1c198 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 26 Jul 2020 22:25:33 +0200 Subject: [PATCH 5/5] Typo and clarification --- nw/gui/doceditor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index b390fb23..59bb9374 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -1243,8 +1243,8 @@ class GuiDocEditor(QTextEdit): if self.docSearch.doMatchCap: replWith = transferCase(selText, replWith) - # Double check that we have a mach in case this is called on a - # plain word selection and not on a search + # Double check that we have a real match in case this is called + # on a regular word selection and not on a search match if not self.docSearch.isCaseSense: isMatch = searchFor.lower() == selText.lower() else: