Merge lines.love
[view.love.git] / source_undo.lua
blobe5dea932f139aa71fa7ab52783afe63a982f0c8b
1 -- undo/redo by managing the sequence of events in the current session
2 -- based on https://github.com/akkartik/mu1/blob/master/edit/012-editor-undo.mu
4 -- Incredibly inefficient; we make a copy of lines on every single keystroke.
5 -- The hope here is that we're either editing small files or just reading large files.
6 -- TODO: highlight stuff inserted by any undo/redo operation
7 -- TODO: coalesce multiple similar operations
9 function record_undo_event(State, data)
10 State.history[State.next_history] = data
11 State.next_history = State.next_history+1
12 for i=State.next_history,#State.history do
13 State.history[i] = nil
14 end
15 end
17 function undo_event(State)
18 if State.next_history > 1 then
19 --? print('moving to history', State.next_history-1)
20 State.next_history = State.next_history-1
21 local result = State.history[State.next_history]
22 return result
23 end
24 end
26 function redo_event(State)
27 if State.next_history <= #State.history then
28 --? print('restoring history', State.next_history+1)
29 local result = State.history[State.next_history]
30 State.next_history = State.next_history+1
31 return result
32 end
33 end
35 -- Copy all relevant global state.
36 -- Make copies of objects; the rest of the app may mutate them in place, but undo requires immutable histories.
37 function snapshot(State, s,e)
38 -- Snapshot everything by default, but subset if requested.
39 assert(s, 'failed to snapshot operation for undo history')
40 if e == nil then
41 e = s
42 end
43 assert(#State.lines > 0, 'failed to snapshot operation for undo history')
44 if s < 1 then s = 1 end
45 if s > #State.lines then s = #State.lines end
46 if e < 1 then e = 1 end
47 if e > #State.lines then e = #State.lines end
48 -- compare with App.initialize_globals
49 local event = {
50 screen_top=deepcopy(State.screen_top1),
51 selection=deepcopy(State.selection1),
52 cursor=deepcopy(State.cursor1),
53 current_drawing_mode=Drawing_mode,
54 previous_drawing_mode=State.previous_drawing_mode,
55 lines={},
56 start_line=s,
57 end_line=e,
58 -- no filename; undo history is cleared when filename changes
60 -- deep copy lines without cached stuff like text fragments
61 for i=s,e do
62 local line = State.lines[i]
63 if line.mode == 'text' then
64 table.insert(event.lines, {mode='text', data=line.data}) -- I've forgotten: should we deepcopy(line.data)?
65 elseif line.mode == 'drawing' then
66 table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}})
67 else
68 assert(false, ('unknown line mode %s'):format(line.mode))
69 end
70 end
71 return event
72 end
74 function patch(lines, from, to)
75 --? if #from.lines == 1 and #to.lines == 1 then
76 --? assert(from.start_line == from.end_line)
77 --? assert(to.start_line == to.end_line)
78 --? assert(from.start_line == to.start_line)
79 --? lines[from.start_line] = to.lines[1]
80 --? return
81 --? end
82 assert(from.start_line == to.start_line, 'failed to patch undo operation')
83 for i=from.end_line,from.start_line,-1 do
84 table.remove(lines, i)
85 end
86 assert(#to.lines == to.end_line-to.start_line+1, 'failed to patch undo operation')
87 for i=1,#to.lines do
88 table.insert(lines, to.start_line+i-1, to.lines[i])
89 end
90 end
92 function patch_placeholders(line_cache, from, to)
93 assert(from.start_line == to.start_line, 'failed to patch undo operation')
94 for i=from.end_line,from.start_line,-1 do
95 table.remove(line_cache, i)
96 end
97 assert(#to.lines == to.end_line-to.start_line+1, 'failed to patch undo operation')
98 for i=1,#to.lines do
99 table.insert(line_cache, to.start_line+i-1, {})
103 -- https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value/26367080#26367080
104 function deepcopy(obj, seen)
105 if type(obj) ~= 'table' then return obj end
106 if seen and seen[obj] then return seen[obj] end
107 local s = seen or {}
108 local result = setmetatable({}, getmetatable(obj))
109 s[obj] = result
110 for k,v in pairs(obj) do
111 result[deepcopy(k, s)] = deepcopy(v, s)
113 return result
116 function minmax(a, b)
117 return math.min(a,b), math.max(a,b)