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, ""