From 1aeb66d9af08691a57e3f1437e6e499fdb081740 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:35:00 +0200 Subject: [PATCH] Make sure existing space is stripped before inserting non-breaking space (#1061) --- novelwriter/gui/doceditor.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 134b2e1c..e10f3fb4 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -111,6 +111,7 @@ class GuiDocEditor(QTextEdit): self._typSQOpen = "'" self._typSQClose = "'" self._typPadChar = " " + self._doSpacePad = False # Core Elements and Signals qDoc = self.document() @@ -221,6 +222,7 @@ class GuiDocEditor(QTextEdit): self._nonWord += "".join(self.mainConf.fmtSingleQuotes) # Typography + self._doSpacePad = bool(self.mainConf.fmtPadBefore) or bool(self.mainConf.fmtPadAfter) if self.mainConf.fmtPadThin: self._typPadChar = nwUnicode.U_THNBSP else: @@ -1992,16 +1994,21 @@ class GuiDocEditor(QTextEdit): nDelete = 3 tInsert = nwUnicode.U_HELLIP - tCheck = tInsert - if tCheck in self.mainConf.fmtPadBefore: - if self._allowSpaceBeforeColon(theText, tCheck): - nDelete = max(nDelete, 1) - tInsert = self._typPadChar + tInsert + if self._doSpacePad: + tCheck = tInsert + if tCheck in self.mainConf.fmtPadBefore: + if self._allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + chkPos = thePos - nDelete - 1 + if chkPos >= 0 and theText[chkPos].isspace(): + # Strip existing space before inserting a new (#1061) + nDelete += 1 + tInsert = self._typPadChar + tInsert - if tCheck in self.mainConf.fmtPadAfter: - if self._allowSpaceBeforeColon(theText, tCheck): - nDelete = max(nDelete, 1) - tInsert = tInsert + self._typPadChar + if tCheck in self.mainConf.fmtPadAfter: + if self._allowSpaceBeforeColon(theText, tCheck): + nDelete = max(nDelete, 1) + tInsert = tInsert + self._typPadChar if nDelete > 0: theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, nDelete)