Skip to content

Add multi-level undo and redo - #8

Open
sigmanor wants to merge 3 commits into
rabfulton:masterfrom
sigmanor:undo-redo
Open

Add multi-level undo and redo#8
sigmanor wants to merge 3 commits into
rabfulton:masterfrom
sigmanor:undo-redo

Conversation

@sigmanor

@sigmanor sigmanor commented Aug 8, 2026

Copy link
Copy Markdown

There is no undo in the editor. Ctrl+Z only reverted the last paste, so typing and deletions could not be taken back at all, and GTK 3 gives GtkTextBuffer nothing to fall back on.

An undo step keeps a snapshot of the note rather than a list of offsets, and restoring one replaces only the part that actually differs. Rewriting the whole buffer would reset the scroll position and strip every tag until the next idle render, so a long note would jump to the top and the raw markdown would flash for a moment. The re-render also runs in the same main loop iteration as the edit, so no frame is ever drawn with the syntax showing.

Edits are grouped into word-sized steps: a new one starts on a space, after a pause, when the edit kind changes, or when it lands somewhere else. Typing or pasting over a selection reaches the buffer as a delete plus an insert, and those are kept together so one Ctrl+Z brings the original text back.

Changes:

  • New src/undo.c / src/undo.h holding the history, capped at 100 steps
  • Edits are recorded from insert-text / delete-range; the markdown renderer and note loading are excluded, since they rewrite the buffer themselves
  • Ctrl+Z undoes, Ctrl+Shift+Z and Ctrl+Y redo. They match on the physical key, so they still work on a Cyrillic layout where Z reports as Cyrillic_ya
  • Switching notes clears the history
  • markyd_editor_get_content now copies the runs between anchors instead of one character at a time, since the history reads it far more often than autosave did
  • The last commit drops the old single-level paste undo, which the general history replaces. Ctrl+V and middle-click paste go back to plain GTK behaviour. It is a separate commit, so it is easy to leave out if you would rather keep that code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant