// Bound undo traversal by the number of applied edits and clear history at a committed savepoint. use std::cli; pub fn undo_cursor_start() -> vector { [0, 0, 0] } pub fn undo_cursor_step(snapshot: vector, event: str, value: f64) -> vector { if len(snapshot) != 3 { return []; } let phase = at(snapshot, 0); let saved = at(snapshot, 1); let previous = at(snapshot, 2); if !(phase >= 0.0 && phase < 3.0 && phase % 1.0 == 0.0 && previous >= 0.0 && previous < 6.0 && previous % 1.0 == 0.0 && saved >= 0.0 && saved <= 9007199254740991.0 && saved % 1.0 == 0.0) { return []; } if !(value >= 0.0 && value <= 1000000000.0 && value % 1.0 == 0.0) { return [phase, saved, 0]; } // idle if phase == 0.0 { if event == "open" { return [1, value, 1]; // load_history } } // editing if phase == 1.0 { if event == "apply" { if saved > 9007199254740991.0 - 1.0 { return [phase, saved, 0]; } return [1, saved+1.0, 2]; // push_edit } if event == "undo" && saved>0.0 { return [1, saved-1.0, 3]; // pop_edit } if event == "save" { return [1, 0.0, 4]; // reset_history } if event == "close" && saved==0.0 { return [2, 0.0, 5]; // close_clean } } [phase, saved, 0] } pub fn undo_cursor_command(snapshot: vector) -> str { if len(snapshot) != 3 { return "invalid"; } let action = at(snapshot, 2); if action == 0.0 { return "reject"; } if action == 1.0 { return "load_history"; } if action == 2.0 { return "push_edit"; } if action == 3.0 { return "pop_edit"; } if action == 4.0 { return "reset_history"; } if action == 5.0 { return "close_clean"; } "invalid" } // Stop at the first rejected event. No host effects run during replay. pub fn undo_cursor_replay(events: arglist, values: vector) -> vector { if args_len(events) != len(values) { return []; } let mut state = undo_cursor_start(); let mut i = 0; while i < len(values) { state = undo_cursor_step(state, args_at(events, i), at(values, i)); if at(state, 2) == 0.0 { return []; } i += 1; } state }