From f8a333c4ea7aff7a8ee62bbbcec26aade071fb6f Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 5 Jul 2025 01:50:03 +0200 Subject: [PATCH] Make editor auto-replace work with 4 byte Unicode --- novelwriter/gui/doceditor.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 846b6317..1c900ce7 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -2278,11 +2278,15 @@ class TextAutoReplace: Returns True if anything was changed. """ pos = cursor.positionInBlock() - length = len(text) + apos = cursor.position() + block = cursor.block() + length = block.length() - 1 if length < 1 or pos-1 > length: return False - delete, insert = self._determine(text, pos) + cursor.movePosition(QtMoveLeft, QtKeepAnchor, min(4, pos)) + last = cursor.selectedText() + delete, insert = self._determine(last, pos) if insert == "": return False @@ -2290,8 +2294,8 @@ class TextAutoReplace: if self._doPadBefore and check in self._padBefore: if not (check == ":" and length > 1 and text[0] == "@"): delete = max(delete, 1) - chkPos = pos - delete - 1 - if chkPos >= 0 and text[chkPos].isspace(): + chkPos = len(last) - delete - 1 + if chkPos >= 0 and last[chkPos].isspace(): # Strip existing space before inserting a new (#1061) delete += 1 insert = self._padChar + insert @@ -2302,6 +2306,7 @@ class TextAutoReplace: insert = insert + self._padChar if delete > 0: + cursor.setPosition(apos) cursor.movePosition(QtMoveLeft, QtKeepAnchor, delete) cursor.insertText(insert) return True @@ -2310,10 +2315,10 @@ class TextAutoReplace: def _determine(self, text: str, pos: int) -> tuple[int, str]: """Determine what to replace, if anything.""" - t1 = text[pos-1:pos] - t2 = text[pos-2:pos] - t3 = text[pos-3:pos] - t4 = text[pos-4:pos] + t1 = text[-1:] + t2 = text[-2:] + t3 = text[-3:] + t4 = text[-4:] if t1 == "": # Return early if there is nothing to check return 0, ""