Trame

undo: linear stacks and branching undo trees

trame:undo (#import "trame:undo") keeps the steps of a model as snapshots: a text field's text (ui.Line_Edit, ui.Text_Edit), a graph (flowgraph.Graph), a drawing, or live designer edits (trame:designer). The owner defines each snapshot and its optional free procedure. The package provides two data structures: undo.Stack($T) for linear history and undo.Tree($T) for branching history.

Linear stack (undo.Stack)

Formula
#import "trame:undo"

history: undo.Stack(flowgraph.Graph)

// before a change
undo.push(&history, flowgraph.graph_clone(app.graph), 100, flowgraph.graph_destroy)

// Ctrl+Z
if undo.can_undo(&history) {
	back, _ := undo.undo(&history, app.graph)   // the graph as it is goes to the redo steps
	app.graph = back
}
ProcedureWhat it does
push(s, before, limit, free_step)Stores before as the previous undo step. Drops oldest steps past limit (0: no limit) and clears redo steps.
forget_redo(s, free_step)Clears redo steps without pushing a new step when an edit merges into the last step.
undo(s, now) -> (back, ok)Returns the last undo step and moves now onto the redo stack. When ok is false, now is not taken.
redo(s, now) -> (again, ok)Returns the last redo step and moves now onto the undo stack.
drop_last(s, free_step)Drops the last pushed step when a tentative edit is rejected.
can_undo(s), can_redo(s)Reports whether an undo or redo step exists.
reset(s, free_step)Frees every step for a new document.
destroy(s, free_step)Frees every step and the stack's own backing storage.

A step is taken whole and returned whole. The stack calls free_step only when it drops a step. Merge rules stay with the owner: text fields merge characters typed within Feel.undo_merge_ms, and ui.node_inspector sets new_step when another parameter changes.

Text fields store their steps in Line_Edit.history and Text_Edit.history (ui.Edit_History, a stack of Edit_Snapshot). Each step's text is zeroed on free because a field can hold a secret.

Branching undo tree (undo.Tree)

undo.Tree($T) keeps every alternate future as a sibling branch instead of discarding redo history when you edit after an undo. Normal Undo and Redo follow each node's active_child, so linear editing behaves like undo.Stack($T). Every alternate branch stays reachable by undo.tree_jump and inspectable with undo.tree_place and ui.history_tree.

Formula
#import "trame:undo"

tree: undo.Tree(My_State)
undo.tree_init(&tree, state_clone(app.state), "Initial", "defaults")

// Commit an edit as a new child of the current node
branch_a := undo.tree_commit(&tree, state_clone(app.state), "Coral accent", "#EB6152", limit = 64, free_step = state_free)

// Undo back to parent, then commit an alternate branch without losing branch_a
if back, ok := undo.tree_undo(&tree); ok { app.state = state_clone(back^) }
_ = undo.tree_commit(&tree, state_clone(app.state), "Emerald compact", "#29B885", limit = 64, free_step = state_free)

// Jump directly back to any live node in any branch
if target, ok := undo.tree_jump(&tree, branch_a); ok { app.state = state_clone(target^) }
ProcedureWhat it does
tree_init(t, initial, label, detail, allocator)Initializes t with initial as root node 0.
tree_commit(t, next_value, label, detail, limit, free_step) -> i32Appends next_value as a child of t.current and updates parent.active_child. When limit > 0 and alive_count >= limit, evicts the oldest non-ancestor leaf without touching root, current, or any ancestor of current.
tree_undo(t) -> (val, ok)Moves t.current to its parent and returns a pointer to that node's stored snapshot.
tree_redo(t) -> (val, ok)Moves t.current to its active_child and returns a pointer to that node's stored snapshot.
tree_jump(t, id) -> (val, ok)Moves t.current to any live node id, updating active_child links along the path from root to id.
tree_prune(t, id, free_step) -> boolRemoves an entire inactive subtree rooted at id and frees its snapshots. Refuses root, current, and ancestors of current.
tree_can_undo(t), tree_can_redo(t)Reports whether a parent or active_child exists.
tree_current(t) -> (val, ok)Returns a pointer to the current node's stored snapshot.
tree_node_count(t), tree_branch_count(t)Reports the count of live nodes and the count of leaf branches.
tree_reset(t, initial, label, detail, free_step)Frees all live nodes and reinitializes the tree at a new root.
tree_destroy(t, free_step)Frees all live nodes and the tree's backing storage.
tree_place(t, allocator) -> (placed, rows, cols)Computes a deterministic grid layout ([]undo.Placed_Node) with (col, row) coordinates, parent indices, and active-path flags.

Visualizing a tree (ui.history_tree)

ui.history_tree (src/ui/history_tree.fm) renders a branching history graph from []ui.History_Tree_Node slices mapped from undo.tree_place. It supports horizontal and vertical layouts, pan and zoom, a minimap, click-to-jump (History_Tree_Result.clicked), hover inspection (hovered), and pruning inactive branches (pruned).

Formula
placed, rows, cols := undo.tree_place(&tree, context.temp_allocator)
nodes := make([]ui.History_Tree_Node, len(placed), context.temp_allocator)
for p, i in placed {
	nodes[i] = {id = p.id, parent_idx = p.parent_idx, col = p.col, row = p.row, on_path = p.on_path, current = p.current, label = p.label, detail = p.detail}
}
res := ui.history_tree("###history", &app.history_view, nodes, rows, cols, tree.serial)
if res.clicked >= 0 {
	if snap, ok := undo.tree_jump(&tree, res.clicked); ok { apply_state(app, snap^) }
}
if res.pruned >= 0 {
	_ = undo.tree_prune(&tree, res.pruned, state_free)
}

Tests

  • src/undo/undo_test.fm: linear stack undo, redo, branch overwrite, limits, drop_last, and branching Tree commits, sibling preservation, tree_jump, tree_prune, eviction invariants, and tree_place coordinates (./build/run_tests undo).
  • tests/scenarios/node_inspector_test.fm: undoing node parameters.

Edit this page on GitHub