From 9f979c02f87af9cb43257541607fa577b01f1cf2 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 14 Mar 2021 14:23:44 +0100 Subject: [PATCH] Use free string entries for padding instead of many switches --- nw/config.py | 21 ++--- nw/gui/doceditor.py | 88 +++++++++---------- tests/reference/baseConfig_novelwriter.conf | 5 +- .../reference/guiPreferences_novelwriter.conf | 5 +- 4 files changed, 55 insertions(+), 64 deletions(-) diff --git a/nw/config.py b/nw/config.py index c0466655..64d3b678 100644 --- a/nw/config.py +++ b/nw/config.py @@ -164,10 +164,9 @@ class Config: self.fmtApostrophe = nwUnicode.U_RSQUO self.fmtSingleQuotes = [nwUnicode.U_LSQUO, nwUnicode.U_RSQUO] self.fmtDoubleQuotes = [nwUnicode.U_LDQUO, nwUnicode.U_RDQUO] - self.fmtPadSingle = False - self.fmtPadDouble = False + self.fmtPadBefore = "" + self.fmtPadAfter = "" self.fmtPadThin = False - self.fmtPadPunct = False ## Spell Checking self.spellTool = None @@ -582,18 +581,15 @@ class Config: self.fmtDoubleQuotes = self._parseLine( cnfParse, cnfSec, "fmtdoublequote", self.CNF_S_LST, self.fmtDoubleQuotes ) - self.fmtPadSingle = self._parseLine( - cnfParse, cnfSec, "fmtpadsingle", self.CNF_BOOL, self.fmtPadSingle + self.fmtPadBefore = self._parseLine( + cnfParse, cnfSec, "fmtpadbefore", self.CNF_STR, self.fmtPadBefore ) - self.fmtPadDouble = self._parseLine( - cnfParse, cnfSec, "fmtpaddouble", self.CNF_BOOL, self.fmtPadDouble + self.fmtPadAfter = self._parseLine( + cnfParse, cnfSec, "fmtpadafter", self.CNF_STR, self.fmtPadAfter ) self.fmtPadThin = self._parseLine( cnfParse, cnfSec, "fmtpadthin", self.CNF_BOOL, self.fmtPadThin ) - self.fmtPadPunct = self._parseLine( - cnfParse, cnfSec, "fmtpadpunct", self.CNF_BOOL, self.fmtPadPunct - ) self.spellTool = self._parseLine( cnfParse, cnfSec, "spelltool", self.CNF_STR, self.spellTool ) @@ -762,10 +758,9 @@ class Config: cnfParse.set(cnfSec, "autoscrollpos", str(self.autoScrollPos)) cnfParse.set(cnfSec, "fmtsinglequote", self._packList(self.fmtSingleQuotes)) cnfParse.set(cnfSec, "fmtdoublequote", self._packList(self.fmtDoubleQuotes)) - cnfParse.set(cnfSec, "fmtpadsingle", str(self.fmtPadSingle)) - cnfParse.set(cnfSec, "fmtpaddouble", str(self.fmtPadDouble)) + cnfParse.set(cnfSec, "fmtpadbefore", str(self.fmtPadBefore)) + cnfParse.set(cnfSec, "fmtpadafter", str(self.fmtPadAfter)) cnfParse.set(cnfSec, "fmtpadthin", str(self.fmtPadThin)) - cnfParse.set(cnfSec, "fmtpadpunct", str(self.fmtPadPunct)) cnfParse.set(cnfSec, "spelltool", str(self.spellTool)) cnfParse.set(cnfSec, "spellcheck", str(self.spellLanguage)) cnfParse.set(cnfSec, "showtabsnspaces", str(self.showTabsNSpaces)) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 8f47343a..ca013fac 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -98,11 +98,12 @@ class GuiDocEditor(QTextEdit): self.queuePos = None # Used for delayed change of cursor position # Typography - self.typPadChar = " " self.typDQOpen = '"' self.typDQClose = '"' self.typSQOpen = "'" self.typSQClose = "'" + self.typPadChar = " " + self.addPadding = False # Core Elements and Signals self.qDocument = self.document() @@ -204,19 +205,10 @@ class GuiDocEditor(QTextEdit): else: self.typPadChar = nwUnicode.U_NBSP - if self.mainConf.fmtPadSingle: - self.typSQOpen = self.mainConf.fmtSingleQuotes[0] + self.typPadChar - self.typSQClose = self.typPadChar + self.mainConf.fmtSingleQuotes[1] - else: - self.typSQOpen = self.mainConf.fmtSingleQuotes[0] - self.typSQClose = self.mainConf.fmtSingleQuotes[1] - - if self.mainConf.fmtPadDouble: - self.typDQOpen = self.mainConf.fmtDoubleQuotes[0] + self.typPadChar - self.typDQClose = self.typPadChar + self.mainConf.fmtDoubleQuotes[1] - else: - self.typDQOpen = self.mainConf.fmtDoubleQuotes[0] - self.typDQClose = self.mainConf.fmtDoubleQuotes[1] + self.typSQOpen = self.mainConf.fmtSingleQuotes[0] + self.typSQClose = self.mainConf.fmtSingleQuotes[1] + self.typDQOpen = self.mainConf.fmtDoubleQuotes[0] + self.typDQClose = self.mainConf.fmtDoubleQuotes[1] # Reload spell check and dictionaries self._setupSpellChecking() @@ -1241,56 +1233,62 @@ class GuiDocEditor(QTextEdit): theTwo = theText[thePos-2:thePos] theThree = theText[thePos-3:thePos] - if self.mainConf.doReplaceDQuote and theTwo == " \"": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 1) - theCursor.insertText(self.typDQOpen) + if not theOne: # Makes Neo sad return - elif self.mainConf.doReplaceDQuote and theOne == "\"": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 1) + nDelete = 0 + tInsert = theOne + + if self.mainConf.doReplaceDQuote and theTwo == ' "': + nDelete = 1 + tInsert = self.typDQOpen + + elif self.mainConf.doReplaceDQuote and theOne == '"': + nDelete = 1 if thePos == 1: - theCursor.insertText(self.typDQOpen) + tInsert = self.typDQOpen else: - theCursor.insertText(self.typDQClose) - return + tInsert = self.typDQClose elif self.mainConf.doReplaceSQuote and theTwo == " '": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 1) - theCursor.insertText(self.typSQOpen) - return + nDelete = 1 + tInsert = self.typSQOpen elif self.mainConf.doReplaceSQuote and theOne == "'": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 1) + nDelete = 1 if thePos == 1: - theCursor.insertText(self.typSQOpen) + tInsert = self.typSQOpen else: - theCursor.insertText(self.typSQClose) - return + tInsert = self.typSQClose elif self.mainConf.doReplaceDash and theThree == "---": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 3) - theCursor.insertText(nwUnicode.U_EMDASH) - return + nDelete = 3 + tInsert = nwUnicode.U_EMDASH elif self.mainConf.doReplaceDash and theTwo == "--": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 2) - theCursor.insertText(nwUnicode.U_ENDASH) - return + nDelete = 2 + tInsert = nwUnicode.U_ENDASH elif self.mainConf.doReplaceDash and theTwo == nwUnicode.U_ENDASH + "-": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 2) - theCursor.insertText(nwUnicode.U_EMDASH) - return + nDelete = 2 + tInsert = nwUnicode.U_EMDASH elif self.mainConf.doReplaceDots and theThree == "...": - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 3) - theCursor.insertText(nwUnicode.U_HELLIP) - return + nDelete = 3 + tInsert = nwUnicode.U_HELLIP - if self.mainConf.fmtPadPunct: - if theOne in ("!", "?", ":", ";"): - theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 1) - theCursor.insertText(self.typPadChar + theOne) + tCheck = tInsert + if tCheck in self.mainConf.fmtPadBefore: + nDelete = max(nDelete, 1) + tInsert = self.typPadChar + tInsert + + if tCheck in self.mainConf.fmtPadAfter: + nDelete = max(nDelete, 1) + tInsert = tInsert + self.typPadChar + + if nDelete > 0: + theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, nDelete) + theCursor.insertText(tInsert) return diff --git a/tests/reference/baseConfig_novelwriter.conf b/tests/reference/baseConfig_novelwriter.conf index 715634ef..cfbf4e78 100644 --- a/tests/reference/baseConfig_novelwriter.conf +++ b/tests/reference/baseConfig_novelwriter.conf @@ -48,10 +48,9 @@ autoscroll = False autoscrollpos = 30 fmtsinglequote = ‘, ’ fmtdoublequote = “, ” -fmtpadsingle = False -fmtpaddouble = False +fmtpadbefore = +fmtpadafter = fmtpadthin = False -fmtpadpunct = False spelltool = internal spellcheck = en showtabsnspaces = False diff --git a/tests/reference/guiPreferences_novelwriter.conf b/tests/reference/guiPreferences_novelwriter.conf index c8a2025b..80e77d34 100644 --- a/tests/reference/guiPreferences_novelwriter.conf +++ b/tests/reference/guiPreferences_novelwriter.conf @@ -48,10 +48,9 @@ autoscroll = True autoscrollpos = 30 fmtsinglequote = ‘, ’ fmtdoublequote = “, ” -fmtpadsingle = False -fmtpaddouble = False +fmtpadbefore = +fmtpadafter = fmtpadthin = False -fmtpadpunct = False spelltool = internal spellcheck = en showtabsnspaces = True