From 72631e238ee02cbf9444d248dc8b87259b7cd8c3 Mon Sep 17 00:00:00 2001 From: Dom Date: Sun, 13 Sep 2026 13:27:07 -0300 Subject: [PATCH] Keep the undo state in step with a run of typing store_action_state stores a new state when the action changes, and is meant to fold continued editing of the same action into the state that run already created. The guard on that second branch tested current_state < len(history)-1, which is never true while editing forwards: store_state leaves current_state at the end of the history. So only the first keystroke of a run was ever recorded. Typing KEEP, pressing enter and undoing once gave back K rather than KEEP, and the same held for every action that followed a typing run. Fold into the current state whenever the action is unchanged, guarding only against writing over the initial state at index 0. --- CHANGELOG.md | 6 ++++++ suplemon/editor.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca690b9..5075edc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,12 @@ Repository and `curl | sh` installs only; the PyPI package is unaffected. **Fixed** +- Undo stepped back too far after a run of typing. A run was only ever + recorded as its first keystroke, so the undo point left behind held one + character of what had been typed: entering `KEEP`, pressing enter and + undoing once left `K`. The guard that was meant to keep the state in + sync while an action continued tested `current_state < len(history)-1`, + which is never true while editing forwards. - Pasting indented text produced a staircase, each line indented further than the last. A paste arrives as ordinary keystrokes, so auto indent fired on every newline and prepended the previous line's whitespace to diff --git a/suplemon/editor.py b/suplemon/editor.py index 3c21d42..11d2a05 100644 --- a/suplemon/editor.py +++ b/suplemon/editor.py @@ -119,11 +119,15 @@ def store_action_state(self, action, state=None): if self.last_action != action: self.last_action = action self.store_state(state) - else: - # FIXME: This if is here just for safety. - # FIXME: current_state might be wrong ;.< - if self.current_state < len(self.history)-1: - self.history[self.current_state].store(self) + elif self.current_state > 0: + # Same action continuing, so fold it into the state this run + # already created rather than making a new one. The guard used + # to be current_state < len(history)-1, which is never true + # while editing forwards: store_state leaves current_state at + # the end of the history. So a run was only ever recorded as + # its first keystroke, and the next different action left an + # undo point holding one character of what the user had typed. + self.history[self.current_state].store(self) def store_state(self, state=None, action=None): """Store the current editor state for undo/redo."""