transparent highlights work with more colors
[view.love.git] / undo.lua
blob59ca48133d8cf572ec6741db2eb8b7a9b8268c90
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 -- makes a copy of lines on every single keystroke; will be inefficient with really long lines.
5 -- TODO: highlight stuff inserted by any undo/redo operation
6 -- TODO: coalesce multiple similar operations
8 function record_undo_event(State, data)
9 State.history[State.next_history] = data
10 State.next_history = State.next_history+1
11 for i=State.next_history,#State.history do
12 State.history[i] = nil
13 end
14 end
16 function undo_event(State)
17 if State.next_history > 1 then
18 --? print('moving to history', State.next_history-1)
19 State.next_history = State.next_history-1
20 local result = State.history[State.next_history]
21 return result
22 end
23 end
25 function redo_event(State)
26 if State.next_history <= #State.history then
27 --? print('restoring history', State.next_history+1)
28 local result = State.history[State.next_history]
29 State.next_history = State.next_history+1
30 return result
31 end
32 end
34 -- Copy all relevant global state.
35 -- Make copies of objects; the rest of the app may mutate them in place, but undo requires immutable histories.
36 function snapshot(State, s,e)
37 -- Snapshot everything by default, but subset if requested.
38 assert(s, 'failed to snapshot operation for undo history')
39 if e == nil then
40 e = s
41 end
42 assert(#State.lines > 0, 'failed to snapshot operation for undo history')
43 if s < 1 then s = 1 end
44 if s > #State.lines then s = #State.lines end
45 if e < 1 then e = 1 end
46 if e > #State.lines then e = #State.lines end
47 -- compare with App.initialize_globals
48 local event = {
49 screen_top=deepcopy(State.screen_top1),
50 selection=deepcopy(State.selection1),
51 cursor=deepcopy(State.cursor1),
52 current_drawing_mode=Drawing_mode,
53 previous_drawing_mode=State.previous_drawing_mode,
54 lines={},
55 start_line=s,
56 end_line=e,
57 -- no filename; undo history is cleared when filename changes
59 for i=s,e do
60 table.insert(event.lines, deepcopy(State.lines[i]))
61 end
62 return event
63 end
65 function patch(lines, from, to)
66 --? if #from.lines == 1 and #to.lines == 1 then
67 --? assert(from.start_line == from.end_line)
68 --? assert(to.start_line == to.end_line)
69 --? assert(from.start_line == to.start_line)
70 --? lines[from.start_line] = to.lines[1]
71 --? return
72 --? end
73 assert(from.start_line == to.start_line, 'failed to patch undo operation')
74 for i=from.end_line,from.start_line,-1 do
75 table.remove(lines, i)
76 end
77 assert(#to.lines == to.end_line-to.start_line+1, 'failed to patch undo operation')
78 for i=1,#to.lines do
79 table.insert(lines, to.start_line+i-1, to.lines[i])
80 end
81 end
83 -- https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value/26367080#26367080
84 function deepcopy(obj, seen)
85 if type(obj) ~= 'table' then return obj end
86 if seen and seen[obj] then return seen[obj] end
87 local s = seen or {}
88 local result = setmetatable({}, getmetatable(obj))
89 s[obj] = result
90 for k,v in pairs(obj) do
91 result[deepcopy(k, s)] = deepcopy(v, s)
92 end
93 return result
94 end
96 function minmax(a, b)
97 return math.min(a,b), math.max(a,b)
98 end