diff options
-rw-r--r-- | edit.lua | 30 | ||||
-rw-r--r-- | select.lua | 4 | ||||
-rw-r--r-- | text.lua | 24 | ||||
-rw-r--r-- | undo.lua | 48 |
4 files changed, 53 insertions, 53 deletions
diff --git a/edit.lua b/edit.lua index 566efb7..f5ea9bd 100644 --- a/edit.lua +++ b/edit.lua @@ -133,13 +133,13 @@ function edit.draw(State) button('draw', {x=4,y=y+4, w=12,h=12, color={1,1,0}, icon = icon.insert_drawing, onpress1 = function() - Drawing.before = snapshot(line_index-1, line_index) + Drawing.before = snapshot(State, line_index-1, line_index) table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}}) if State.cursor1.line >= line_index then State.cursor1.line = State.cursor1.line+1 end schedule_save(State) - record_undo_event({before=Drawing.before, after=snapshot(line_index-1, line_index+1)}) + record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)}) end }) if State.search_term == nil then @@ -227,7 +227,7 @@ function edit.mouse_pressed(State, x,y, mouse_button) if Drawing.in_drawing(line, x, y) then State.lines.current_drawing_index = line_index State.lines.current_drawing = line - Drawing.before = snapshot(line_index) + Drawing.before = snapshot(State, line_index) Drawing.mouse_pressed(State, line, x,y, mouse_button) break end @@ -242,7 +242,7 @@ function edit.mouse_released(State, x,y, mouse_button) Drawing.mouse_released(State, x,y, mouse_button) schedule_save(State) if Drawing.before then - record_undo_event({before=Drawing.before, after=snapshot(State.lines.current_drawing_index)}) + record_undo_event(State, {before=Drawing.before, after=snapshot(State, State.lines.current_drawing_index)}) Drawing.before = nil end else @@ -281,11 +281,11 @@ function edit.textinput(State, t) State.search_text = nil Text.search_next() elseif State.current_drawing_mode == 'name' then - local before = snapshot(State.lines.current_drawing_index) + local before = snapshot(State, State.lines.current_drawing_index) local drawing = State.lines.current_drawing local p = drawing.points[drawing.pending.target_point] p.name = p.name..t - record_undo_event({before=before, after=snapshot(State.lines.current_drawing_index)}) + record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)}) else Text.textinput(State, t) end @@ -340,7 +340,7 @@ function edit.keychord_pressed(State, chord, key) Text.redraw_all() elseif chord == 'C-z' then for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll - local event = undo_event() + local event = undo_event(State) if event then local src = event.before State.screen_top1 = deepcopy(src.screen_top) @@ -352,7 +352,7 @@ function edit.keychord_pressed(State, chord, key) end elseif chord == 'C-y' then for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll - local event = redo_event() + local event = redo_event(State) if event then local src = event.after State.screen_top1 = deepcopy(src.screen_top) @@ -381,7 +381,7 @@ function edit.keychord_pressed(State, chord, key) -- We don't have a good sense of when to scroll, so we'll be conservative -- and sometimes scroll when we didn't quite need to. local before_line = State.cursor1.line - local before = snapshot(before_line) + local before = snapshot(State, before_line) local clipboard_data = App.getClipboardText() for _,code in utf8.codes(clipboard_data) do local c = utf8.char(code) @@ -395,15 +395,15 @@ function edit.keychord_pressed(State, chord, key) Text.snap_cursor_to_bottom_of_screen(State.margin_left, App.screen.height-State.margin_right) end schedule_save(State) - record_undo_event({before=before, after=snapshot(before_line, State.cursor1.line)}) + record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)}) -- dispatch to drawing or text elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then -- DON'T reset line.y here local drawing_index, drawing = Drawing.current_drawing() if drawing_index then - local before = snapshot(drawing_index) + local before = snapshot(State, drawing_index) Drawing.keychord_pressed(State, chord) - record_undo_event({before=before, after=snapshot(drawing_index)}) + record_undo_event(State, {before=before, after=snapshot(State, drawing_index)}) schedule_save(State) end elseif chord == 'escape' and not App.mouse_down(1) then @@ -417,18 +417,18 @@ function edit.keychord_pressed(State, chord, key) State.current_drawing_mode = State.previous_drawing_mode State.previous_drawing_mode = nil else - local before = snapshot(State.lines.current_drawing_index) + local before = snapshot(State, State.lines.current_drawing_index) local drawing = State.lines.current_drawing local p = drawing.points[drawing.pending.target_point] if chord == 'escape' then p.name = nil - record_undo_event({before=before, after=snapshot(State.lines.current_drawing_index)}) + record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)}) elseif chord == 'backspace' then local len = utf8.len(p.name) local byte_offset = Text.offset(p.name, len-1) if len == 1 then byte_offset = 0 end p.name = string.sub(p.name, 1, byte_offset) - record_undo_event({before=before, after=snapshot(State.lines.current_drawing_index)}) + record_undo_event(State, {before=before, after=snapshot(State, State.lines.current_drawing_index)}) end end schedule_save(State) diff --git a/select.lua b/select.lua index dfa7f45..1b74858 100644 --- a/select.lua +++ b/select.lua @@ -110,9 +110,9 @@ end function Text.delete_selection(State, left, right) if State.selection1.line == nil then return end local minl,maxl = minmax(State.selection1.line, State.cursor1.line) - local before = snapshot(minl, maxl) + local before = snapshot(State, minl, maxl) Text.delete_selection_without_undo(State, left, right) - record_undo_event({before=before, after=snapshot(State.cursor1.line)}) + record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)}) end function Text.delete_selection_without_undo(State, left, right) diff --git a/text.lua b/text.lua index edab9f5..74cf2a6 100644 --- a/text.lua +++ b/text.lua @@ -137,7 +137,7 @@ end function Text.textinput(State, t) if App.mouse_down(1) then return end if App.ctrl_down() or App.alt_down() or App.cmd_down() then return end - local before = snapshot(State.cursor1.line) + local before = snapshot(State, State.cursor1.line) --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos) Text.insert_at_cursor(State, t) if State.cursor_y >= App.screen.height - State.line_height then @@ -145,7 +145,7 @@ function Text.textinput(State, t) Text.snap_cursor_to_bottom_of_screen(State.margin_left, App.screen.width-State.margin_right) --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos) end - record_undo_event({before=before, after=snapshot(State.cursor1.line)}) + record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)}) end function Text.insert_at_cursor(State, t) @@ -161,16 +161,16 @@ function Text.keychord_pressed(State, chord) --== shortcuts that mutate text if chord == 'return' then local before_line = State.cursor1.line - local before = snapshot(before_line) + local before = snapshot(State, before_line) Text.insert_return(State) State.selection1 = {} if (State.cursor_y + State.line_height) > App.screen.height then Text.snap_cursor_to_bottom_of_screen(State.margin_left, App.screen.width-State.margin_right) end schedule_save(State) - record_undo_event({before=before, after=snapshot(before_line, State.cursor1.line)}) + record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)}) elseif chord == 'tab' then - local before = snapshot(State.cursor1.line) + local before = snapshot(State, State.cursor1.line) --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos) Text.insert_at_cursor(State, '\t') if State.cursor_y >= App.screen.height - State.line_height then @@ -179,7 +179,7 @@ function Text.keychord_pressed(State, chord) --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos) end schedule_save(State) - record_undo_event({before=before, after=snapshot(State.cursor1.line)}) + record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)}) elseif chord == 'backspace' then if State.selection1.line then Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right) @@ -188,7 +188,7 @@ function Text.keychord_pressed(State, chord) end local before if State.cursor1.pos > 1 then - before = snapshot(State.cursor1.line) + before = snapshot(State, State.cursor1.line) local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1) local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos) if byte_start then @@ -200,7 +200,7 @@ function Text.keychord_pressed(State, chord) State.cursor1.pos = State.cursor1.pos-1 end elseif State.cursor1.line > 1 then - before = snapshot(State.cursor1.line-1, State.cursor1.line) + before = snapshot(State, State.cursor1.line-1, State.cursor1.line) if State.lines[State.cursor1.line-1].mode == 'drawing' then table.remove(State.lines, State.cursor1.line-1) else @@ -220,7 +220,7 @@ function Text.keychord_pressed(State, chord) Text.clear_cache(State.lines[State.cursor1.line]) assert(Text.le1(State.screen_top1, State.cursor1)) schedule_save(State) - record_undo_event({before=before, after=snapshot(State.cursor1.line)}) + record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)}) elseif chord == 'delete' then if State.selection1.line then Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right) @@ -229,9 +229,9 @@ function Text.keychord_pressed(State, chord) end local before if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then - before = snapshot(State.cursor1.line) + before = snapshot(State, State.cursor1.line) else - before = snapshot(State.cursor1.line, State.cursor1.line+1) + before = snapshot(State, State.cursor1.line, State.cursor1.line+1) end if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos) @@ -255,7 +255,7 @@ function Text.keychord_pressed(State, chord) end Text.clear_cache(State.lines[State.cursor1.line]) schedule_save(State) - record_undo_event({before=before, after=snapshot(State.cursor1.line)}) + record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)}) --== shortcuts that move the cursor elseif chord == 'left' then Text.left(State, State.margin_left, App.screen.width-State.margin_right) 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 |