From 728cfd5aa884cc840d62e29a433f30dd056b6c6e Mon Sep 17 00:00:00 2001 From: Ken Polizzi Date: Fri, 16 Sep 2022 02:50:35 +0200 Subject: [PATCH] add undo and redo --- h-m-m | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- readme.md | 2 ++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/h-m-m b/h-m-m index e9fab76..3ece61c 100755 --- a/h-m-m +++ b/h-m-m @@ -16,6 +16,10 @@ $mm['active_node_color'] = "\033[38;5;0m\033[48;5;172m\033[1m"; $mm['message_color'] = "\033[38;5;0m\033[48;5;141m\033[1m"; $mm['question_color'] = "\033[38;5;168m"; +$mm['changes'] = []; +$mm['change_active_node'] = []; +$mm['change_index'] = 0; + function load_settings(&$mm) { @@ -115,6 +119,7 @@ const insert_child = 1; const ctrl_p = "\020"; const ctrl_c = "\003"; +const ctrl_r = "\022"; const arr_down = "\033\133\102"; const arr_right = "\033\133\103"; @@ -1000,6 +1005,8 @@ function push_node_down(&$mm, $id) { if ($id==0) return; + push_change($mm); + $mm['modified'] = true; if (isset($mm['nodes'][$id+1])) @@ -1033,7 +1040,6 @@ function push_node_down(&$mm, $id) function insert_node(&$mm, $type) { - if ($mm['active_node']==$mm['root']) $type=insert_child; @@ -1105,6 +1111,8 @@ function show_line(&$mm, $title, $cursor, $shift) function edit_node(&$mm, $rewrite = false) { + push_change($mm); + $title = $rewrite ? '' : $mm['nodes'][ $mm['active_node'] ]['title']; if ($mm['active_node']==0 && $title=='root') $title=''; @@ -1412,6 +1420,8 @@ function move_active_node_down(&$mm) { if ($mm['active_node']==0) return; + push_change($mm); + $mm['modified'] = true; $parent_id = $mm['nodes'][ $mm['active_node'] ]['parent']; @@ -1447,6 +1457,8 @@ function move_active_node_up(&$mm) { if ($mm['active_node']==0) return; + push_change($mm); + $mm['modified'] = true; $parent_id = $mm['nodes'][ $mm['active_node'] ]['parent']; @@ -1695,6 +1707,46 @@ function move_window(&$mm) } +// }}} +// {{{ changes + +function push_change(&$mm) +{ + // flush any redo chain + while(count($mm['changes']) > $mm['change_index']) + array_pop($mm['changes']); + + array_push($mm['changes'], $mm['nodes']); + array_push($mm['change_active_node'], $mm['active_node']); + $mm['change_index']++; +} + +function undo(&$mm) +{ + if($mm['change_index'] == 0) + return; + + $mm['nodes'] = $mm['changes'][$mm['change_index'] - 1]; + $mm['active_node'] = $mm['change_active_node'][$mm['change_index'] - 1]; + $mm['change_index']--; + + build_map($mm); + display($mm); +} + +function redo(&$mm) +{ + if(count($mm['changes']) == $mm['change_index']) + return; + + $mm['nodes'] = $mm['changes'][$mm['change_index']]; + $mm['active_node'] = $mm['change_active_node'][$mm['change_index']]; + $mm['change_index']++; + + build_map($mm); + display($mm); +} + // }}} // {{{ change active node @@ -1866,6 +1918,8 @@ function paste_sub_tree(&$mm, $as_sibling ) if ($as_sibling && $mm['active_node']==$mm['root']) return; + push_change($mm); + $mm['modified'] = true; if ($as_sibling) @@ -1979,6 +2033,8 @@ function yank_node(&$mm, $exclude_parent = false ) function delete_node(&$mm, $exclude_parent = false ) { + push_change($mm); + if ($mm['active_node']==$mm['root']) $exclude_parent = true; copy_to_clipboard($mm, encode_tree($mm, $mm['active_node'], $exclude_parent) ); @@ -2463,9 +2519,12 @@ function monitor_key_presses(&$mm) case 'q': quit($mm); break; case 'Q': exit; break; + case ctrl_r: redo($mm); break; + case 's': save($mm); break; case 'S': save($mm, true); break; + case 'u': undo($mm); break; case 'U': debug($mm['nodes']); break; case 'v': collapse_all($mm); break; diff --git a/readme.md b/readme.md index 752946c..3256e40 100644 --- a/readme.md +++ b/readme.md @@ -21,6 +21,8 @@ Adding, removing, and editing nodes: * `ctrl+p` - appends the clipboard text at the end of the active node's title * `e` - edits the active node * `E` - edits the active node, ignoring the existing text +* `u` - undo +* `ctrl+r` - redo Relative navigating and moving: