From 0ddb7ad1dec39f5468981b0449477e932dcfb1a1 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 17 Jun 2020 17:15:30 +0200 Subject: [PATCH] Preserve case repalce now works --- nw/common.py | 21 +++++++++++++++++++++ nw/gui/doceditor.py | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/nw/common.py b/nw/common.py index bd535a71..0019c38a 100644 --- a/nw/common.py +++ b/nw/common.py @@ -172,3 +172,24 @@ def splitVersionNumber(vString): vInt = vMajor*10000 + vMinor*100 + vPatch return [vMajor, vMinor, vPatch, vInt] + +def transferCase(theSource, theTarget): + """Transfers the case of the source word to the target word. This + will consider all upper or lower, and first char capitalisation. + """ + theResult = theTarget + + if not isinstance(theSource, str) or not isinstance(theTarget, str): + return theResult + if len(theTarget) < 1 or len(theSource) < 1: + return theResult + + if theSource[0] == theSource[0].upper(): + theResult = theTarget[0].upper() + theTarget[1:] + + if theSource == theSource.upper(): + theResult = theTarget.upper() + elif theSource == theSource.lower(): + theResult = theTarget.lower() + + return theResult diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index baa0b881..950fef73 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -48,6 +48,7 @@ from nw.core import NWDoc from nw.gui.dochighlight import GuiDocHighlighter from nw.core import NWSpellSimple, countWords from nw.constants import nwUnicode, nwDocAction +from nw.common import transferCase logger = logging.getLogger(__name__) @@ -1185,16 +1186,23 @@ class GuiDocEditor(QTextEdit): self._beginSearch() return + if not theCursor.hasSelection(): + return + theCursor = self.textCursor() searchFor = self.docSearch.getSearchText() replWith = self.docSearch.getReplaceText() selText = theCursor.selectedText() - if not self.docSearch.isCaseSense: - searchFor = searchFor.lower() - selText = selText.lower() + if self.docSearch.doPreserve: + replWith = transferCase(selText, replWith) - if theCursor.hasSelection() and selText == searchFor: + if not self.docSearch.isCaseSense: + isMatch = searchFor.lower() == selText.lower() + else: + isMatch = searchFor == selText + + if isMatch: theCursor.beginEditBlock() theCursor.removeSelectedText() theCursor.insertText(replWith)