about summary refs log tree commit diff stats
path: root/undo.lua
diff options
context:
space:
mode:
Diffstat (limited to 'undo.lua')
-rw-r--r--undo.lua26
1 files changed, 6 insertions, 20 deletions
diff --git a/undo.lua b/undo.lua
index a41ba38..69f7c31 100644
--- a/undo.lua
+++ b/undo.lua
@@ -1,8 +1,7 @@
 -- undo/redo by managing the sequence of events in the current session
 -- based on https://github.com/akkartik/mu1/blob/master/edit/012-editor-undo.mu
 
--- Incredibly inefficient; we make a copy of lines on every single keystroke.
--- The hope here is that we're either editing small files or just reading large files.
+-- makes a copy of lines on every single keystroke; will be inefficient with really long lines.
 -- TODO: highlight stuff inserted by any undo/redo operation
 -- TODO: coalesce multiple similar operations
 
@@ -55,10 +54,8 @@ function snapshot(State, s,e)
     end_line=e,
     -- no filename; undo history is cleared when filename changes
   }
-  -- deep copy lines without cached stuff like text fragments
   for i=s,e do
-    local line = State.lines[i]
-    table.insert(event.lines, {data=line.data})  -- I've forgotten: should we deepcopy(line.data)?
+    table.insert(event.lines, deepcopy(State.lines[i]))
   end
   return event
 end
@@ -81,26 +78,15 @@ function patch(lines, from, to)
   end
 end
 
-function patch_placeholders(line_cache, from, to)
-  assert(from.start_line == to.start_line, 'failed to patch undo operation')
-  for i=from.end_line,from.start_line,-1 do
-    table.remove(line_cache, i)
-  end
-  assert(#to.lines == to.end_line-to.start_line+1, 'failed to patch undo operation')
-  for i=1,#to.lines do
-    table.insert(line_cache, to.start_line+i-1, {})
-  end
-end
-
 -- https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value/26367080#26367080
 function deepcopy(obj, seen)
   if type(obj) ~= 'table' then return obj end
-  if seen and seen[obj] then return seen[obj] end
-  local s = seen or {}
+  seen = seen or {}
+  if seen[obj] then return seen[obj] end
   local result = setmetatable({}, getmetatable(obj))
-  s[obj] = result
+  seen[obj] = result
   for k,v in pairs(obj) do
-    result[deepcopy(k, s)] = deepcopy(v, s)
+    result[deepcopy(k, seen)] = deepcopy(v, seen)
   end
   return result
 end