about summary refs log tree commit diff stats
path: root/undo.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-07-12 16:55:32 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-07-12 16:55:32 -0700
commit5b91af1a66e1a3a9b52fed67727365217425b502 (patch)
tree8dee5b71c97fd2cbcf93cc362f073a1abd722945 /undo.lua
parent188bbc73cc3c0baddcf48f2501b18248b7ed3fc1 (diff)
downloadview.love-5b91af1a66e1a3a9b52fed67727365217425b502.tar.gz
add state arg to a few functions
  - record_undo_event
  - undo_event
  - redo_event
  - snapshot
Diffstat (limited to 'undo.lua')
-rw-r--r--undo.lua48
1 files changed, 24 insertions, 24 deletions
diff --git a/undo.lua b/undo.lua
index 8e5491c..f56a348 100644
--- a/undo.lua
+++ b/undo.lua
@@ -6,52 +6,52 @@
 -- TODO: highlight stuff inserted by any undo/redo operation
 -- TODO: coalesce multiple similar operations
 
-function record_undo_event(data)
-  Editor_state.history[Editor_state.next_history] = data
-  Editor_state.next_history = Editor_state.next_history+1
-  for i=Editor_state.next_history,#Editor_state.history do
-    Editor_state.history[i] = nil
+function record_undo_event(State, data)
+  State.history[State.next_history] = data
+  State.next_history = State.next_history+1
+  for i=State.next_history,#State.history do
+    State.history[i] = nil
   end
 end
 
-function undo_event()
-  if Editor_state.next_history > 1 then
---?     print('moving to history', Editor_state.next_history-1)
-    Editor_state.next_history = Editor_state.next_history-1
-    local result = Editor_state.history[Editor_state.next_history]
+function undo_event(State)
+  if State.next_history > 1 then
+--?     print('moving to history', State.next_history-1)
+    State.next_history = State.next_history-1
+    local result = State.history[State.next_history]
     return result
   end
 end
 
-function redo_event()
-  if Editor_state.next_history <= #Editor_state.history then
---?     print('restoring history', Editor_state.next_history+1)
-    local result = Editor_state.history[Editor_state.next_history]
-    Editor_state.next_history = Editor_state.next_history+1
+function redo_event(State)
+  if State.next_history <= #State.history then
+--?     print('restoring history', State.next_history+1)
+    local result = State.history[State.next_history]
+    State.next_history = State.next_history+1
     return result
   end
 end
 
 -- Copy all relevant global state.
 -- Make copies of objects; the rest of the app may mutate them in place, but undo requires immutable histories.
-function snapshot(s,e)
+function snapshot(State, s,e)
   -- Snapshot everything by default, but subset if requested.
   assert(s)
   if e == nil then
     e = s
   end
-  assert(#Editor_state.lines > 0)
+  assert(#State.lines > 0)
   if s < 1 then s = 1 end
-  if s > #Editor_state.lines then s = #Editor_state.lines end
+  if s > #State.lines then s = #State.lines end
   if e < 1 then e = 1 end
-  if e > #Editor_state.lines then e = #Editor_state.lines end
+  if e > #State.lines then e = #State.lines end
   -- compare with App.initialize_globals
   local event = {
-    screen_top=deepcopy(Editor_state.screen_top1),
-    selection=deepcopy(Editor_state.selection1),
-    cursor=deepcopy(Editor_state.cursor1),
+    screen_top=deepcopy(State.screen_top1),
+    selection=deepcopy(State.selection1),
+    cursor=deepcopy(State.cursor1),
     current_drawing_mode=Drawing_mode,
-    previous_drawing_mode=Editor_state.previous_drawing_mode,
+    previous_drawing_mode=State.previous_drawing_mode,
     lines={},
     start_line=s,
     end_line=e,
@@ -59,7 +59,7 @@ function snapshot(s,e)
   }
   -- deep copy lines without cached stuff like text fragments
   for i=s,e do
-    local line = Editor_state.lines[i]
+    local line = State.lines[i]
     if line.mode == 'text' then
       table.insert(event.lines, {mode='text', data=line.data})
     elseif line.mode == 'drawing' then