start passing in Editor_state explicitly

In this commit, top-level edit functions:
  - edit.draw
  - edit.update
  - edit.quit
  - edit.mouse_pressed
  - edit.mouse_released
  - edit.textinput
  - edit.keychord_pressed
  - edit.key_released
This commit is contained in:
Kartik K. Agaram 2022-07-12 15:18:45 -07:00
parent e95b4fec12
commit 81ecca89ff
4 changed files with 232 additions and 231 deletions

View File

@ -7,7 +7,7 @@ function test_creating_drawing_saves()
App.screen.init{width=120, height=60}
Editor_state.filename = 'foo'
Editor_state.lines = load_array{}
edit.draw()
edit.draw(Editor_state)
-- click on button to create drawing
App.run_after_mouse_click(8,Editor_state.margin_top+8, 1)
-- file not immediately saved
@ -27,7 +27,7 @@ function test_draw_line()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_line/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_line/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_line/baseline/y')
@ -70,7 +70,7 @@ function test_draw_horizontal_line()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'manhattan'
edit.draw()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_horizontal_line/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_horizontal_line/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_horizontal_line/baseline/y')
@ -97,7 +97,7 @@ function test_draw_circle()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_circle/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_circle/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_circle/baseline/y')
@ -125,7 +125,7 @@ function test_cancel_stroke()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_cancel_stroke/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_cancel_stroke/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_cancel_stroke/baseline/y')
@ -146,7 +146,7 @@ function test_keys_do_not_affect_shape_when_mouse_up()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
-- hover over drawing and press 'o' without holding mouse
App.mouse_move(Editor_state.margin_left+4, Editor_state.margin_top+Editor_state.drawing_padding_top+4) -- hover on drawing
App.run_after_keychord('o')
@ -161,7 +161,7 @@ function test_draw_circle_mid_stroke()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_circle_mid_stroke/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_circle_mid_stroke/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_circle_mid_stroke/baseline/y')
@ -188,7 +188,7 @@ function test_draw_arc()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'circle'
edit.draw()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 2, 'F - test_draw_arc/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_arc/baseline/mode')
check_eq(Editor_state.lines[1].y, Editor_state.margin_top+Editor_state.drawing_padding_top, 'F - test_draw_arc/baseline/y')
@ -217,7 +217,7 @@ function test_draw_polygon()
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw()
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_polygon/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_polygon/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_polygon/baseline/mode')
@ -254,7 +254,7 @@ function test_draw_rectangle()
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw()
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_rectangle/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_rectangle/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_rectangle/baseline/mode')
@ -297,7 +297,7 @@ function test_draw_rectangle_intermediate()
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw()
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_rectangle_intermediate/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_rectangle_intermediate/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_rectangle_intermediate/baseline/mode')
@ -332,7 +332,7 @@ function test_draw_square()
-- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
edit.draw()
edit.draw(Editor_state)
check_eq(Editor_state.current_drawing_mode, 'line', 'F - test_draw_square/baseline/drawing_mode')
check_eq(#Editor_state.lines, 2, 'F - test_draw_square/baseline/#lines')
check_eq(Editor_state.lines[1].mode, 'drawing', 'F - test_draw_square/baseline/mode')
@ -376,7 +376,7 @@ function test_name_point()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
-- draw a line
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
@ -418,7 +418,7 @@ function test_move_point()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
@ -440,7 +440,7 @@ function test_move_point()
local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
check_eq(p2.x, 35, 'F - test_move_point/save/x')
check_eq(p2.y, 36, 'F - test_move_point/save/y')
edit.draw()
edit.draw(Editor_state)
-- enter 'move' mode without moving the mouse
App.run_after_keychord('C-u')
check_eq(Editor_state.current_drawing_mode, 'move', 'F - test_move_point/mode:1')
@ -474,14 +474,14 @@ function test_move_point_on_manhattan_line()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'manhattan'
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+46, 1)
local drawing = Editor_state.lines[1]
check_eq(#drawing.shapes, 1, 'F - test_move_point_on_manhattan_line/baseline/#shapes')
check_eq(#drawing.points, 2, 'F - test_move_point_on_manhattan_line/baseline/#points')
check_eq(drawing.shapes[1].mode, 'manhattan', 'F - test_move_point_on_manhattan_line/baseline/shape:1')
edit.draw()
edit.draw(Editor_state)
-- enter 'move' mode
App.run_after_keychord('C-u')
check_eq(Editor_state.current_drawing_mode, 'move', 'F - test_move_point_on_manhattan_line/mode:1')
@ -499,7 +499,7 @@ function test_delete_lines_at_point()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
App.run_after_mouse_press(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
@ -527,7 +527,7 @@ function test_delete_line_under_mouse_pointer()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
App.run_after_mouse_press(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
@ -550,7 +550,7 @@ function test_delete_point_from_polygon()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
-- first point
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_keychord('g') -- polygon mode
@ -580,7 +580,7 @@ function test_delete_point_from_polygon()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
-- first point
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_keychord('g') -- polygon mode
@ -607,7 +607,7 @@ function test_undo_name_point()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
-- draw a line
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
@ -652,7 +652,7 @@ function test_undo_move_point()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
local drawing = Editor_state.lines[1]
@ -701,7 +701,7 @@ function test_undo_delete_point()
App.screen.init{width=Editor_state.margin_width+256, height=300} -- drawing coordinates 1:1 with pixels
Editor_state.lines = load_array{'```lines', '```', ''}
Editor_state.current_drawing_mode = 'line'
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_press(Editor_state.margin_left+5, Editor_state.margin_top+Editor_state.drawing_padding_top+6, 1)
App.run_after_mouse_release(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)
App.run_after_mouse_press(Editor_state.margin_left+35, Editor_state.margin_top+Editor_state.drawing_padding_top+36, 1)

246
edit.lua
View File

@ -114,18 +114,18 @@ function edit.initialize_state()
return result
end -- App.initialize_state
function edit.draw()
function edit.draw(State)
App.color(Text_color)
--? print(Editor_state.screen_top1.line, Editor_state.screen_top1.pos, Editor_state.cursor1.line, Editor_state.cursor1.pos)
assert(Text.le1(Editor_state.screen_top1, Editor_state.cursor1))
Editor_state.cursor_y = -1
local y = Editor_state.margin_top
--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
assert(Text.le1(State.screen_top1, State.cursor1))
State.cursor_y = -1
local y = State.margin_top
--? print('== draw')
for line_index = Editor_state.screen_top1.line,#Editor_state.lines do
local line = Editor_state.lines[line_index]
for line_index = State.screen_top1.line,#State.lines do
local line = State.lines[line_index]
--? print('draw:', y, line_index, line)
if y + Editor_state.line_height > App.screen.height then break end
Editor_state.screen_bottom1.line = line_index
if y + State.line_height > App.screen.height then break end
State.screen_bottom1.line = line_index
if line.mode == 'text' and line.data == '' then
line.starty = y
line.startpos = 1
@ -134,52 +134,52 @@ function edit.draw()
icon = icon.insert_drawing,
onpress1 = function()
Drawing.before = snapshot(line_index-1, line_index)
table.insert(Editor_state.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
if Editor_state.cursor1.line >= line_index then
Editor_state.cursor1.line = Editor_state.cursor1.line+1
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()
record_undo_event({before=Drawing.before, after=snapshot(line_index-1, line_index+1)})
end
})
if Editor_state.search_term == nil then
if line_index == Editor_state.cursor1.line then
Text.draw_cursor(Editor_state.margin_left, y)
if State.search_term == nil then
if line_index == State.cursor1.line then
Text.draw_cursor(State.margin_left, y)
end
end
Editor_state.screen_bottom1.pos = Editor_state.screen_top1.pos
y = y + Editor_state.line_height
State.screen_bottom1.pos = State.screen_top1.pos
y = y + State.line_height
elseif line.mode == 'drawing' then
y = y+Editor_state.drawing_padding_top
y = y+State.drawing_padding_top
line.y = y
Drawing.draw(line)
y = y + Drawing.pixels(line.h) + Editor_state.drawing_padding_bottom
y = y + Drawing.pixels(line.h) + State.drawing_padding_bottom
else
line.starty = y
line.startpos = 1
if line_index == Editor_state.screen_top1.line then
line.startpos = Editor_state.screen_top1.pos
if line_index == State.screen_top1.line then
line.startpos = State.screen_top1.pos
end
--? print('text.draw', y, line_index)
y, Editor_state.screen_bottom1.pos = Text.draw(line, line_index, line.starty, Editor_state.margin_left, App.screen.width-Editor_state.margin_right)
y = y + Editor_state.line_height
y, State.screen_bottom1.pos = Text.draw(line, line_index, line.starty, State.margin_left, App.screen.width-State.margin_right)
y = y + State.line_height
--? print('=> y', y)
end
end
if Editor_state.cursor_y == -1 then
Editor_state.cursor_y = App.screen.height
if State.cursor_y == -1 then
State.cursor_y = App.screen.height
end
--? print('screen bottom: '..tostring(Editor_state.screen_bottom1.pos)..' in '..tostring(Editor_state.lines[Editor_state.screen_bottom1.line].data))
if Editor_state.search_term then
--? print('screen bottom: '..tostring(State.screen_bottom1.pos)..' in '..tostring(State.lines[State.screen_bottom1.line].data))
if State.search_term then
Text.draw_search_bar()
end
end
function edit.update(dt)
function edit.update(State, dt)
Drawing.update(dt)
if Editor_state.next_save and Editor_state.next_save < App.getTime() then
save_to_disk(Editor_state.lines, Editor_state.filename)
Editor_state.next_save = nil
if State.next_save and State.next_save < App.getTime() then
save_to_disk(State.lines, State.filename)
State.next_save = nil
end
end
@ -189,21 +189,21 @@ function schedule_save()
end
end
function edit.quit()
function edit.quit(State)
-- make sure to save before quitting
if Editor_state.next_save then
save_to_disk(Editor_state.lines, Editor_state.filename)
if State.next_save then
save_to_disk(State.lines, State.filename)
end
end
function edit.mouse_pressed(x,y, mouse_button)
if Editor_state.search_term then return end
--? print('press', Editor_state.selection1.line, Editor_state.selection1.pos)
function edit.mouse_pressed(State, x,y, mouse_button)
if State.search_term then return end
--? print('press', State.selection1.line, State.selection1.pos)
propagate_to_button_handlers(x,y, mouse_button)
for line_index,line in ipairs(Editor_state.lines) do
for line_index,line in ipairs(State.lines) do
if line.mode == 'text' then
if Text.in_line(line, x,y, Editor_state.margin_left, App.screen.width-Editor_state.margin_right) then
if Text.in_line(line, x,y, State.margin_left, App.screen.width-State.margin_right) then
-- delicate dance between cursor, selection and old cursor/selection
-- scenarios:
-- regular press+release: sets cursor, clears selection
@ -213,20 +213,20 @@ function edit.mouse_pressed(x,y, mouse_button)
-- press and hold to start a selection: sets selection on press, cursor on release
-- press and hold, then press shift: ignore shift
-- i.e. mousereleased should never look at shift state
Editor_state.old_cursor1 = Editor_state.cursor1
Editor_state.old_selection1 = Editor_state.selection1
Editor_state.mousepress_shift = App.shift_down()
Editor_state.selection1 = {
State.old_cursor1 = State.cursor1
State.old_selection1 = State.selection1
State.mousepress_shift = App.shift_down()
State.selection1 = {
line=line_index,
pos=Text.to_pos_on_line(line, x, y, Editor_state.margin_left, App.screen.width-Editor_state.margin_right),
pos=Text.to_pos_on_line(line, x, y, State.margin_left, App.screen.width-State.margin_right),
}
--? print('selection', Editor_state.selection1.line, Editor_state.selection1.pos)
--? print('selection', State.selection1.line, State.selection1.pos)
break
end
elseif line.mode == 'drawing' then
if Drawing.in_drawing(line, x, y) then
Editor_state.lines.current_drawing_index = line_index
Editor_state.lines.current_drawing = line
State.lines.current_drawing_index = line_index
State.lines.current_drawing = line
Drawing.before = snapshot(line_index)
Drawing.mouse_pressed(line, x,y, mouse_button)
break
@ -235,152 +235,152 @@ function edit.mouse_pressed(x,y, mouse_button)
end
end
function edit.mouse_released(x,y, mouse_button)
if Editor_state.search_term then return end
function edit.mouse_released(State, x,y, mouse_button)
if State.search_term then return end
--? print('release')
if Editor_state.lines.current_drawing then
if State.lines.current_drawing then
Drawing.mouse_released(x,y, mouse_button)
schedule_save()
if Drawing.before then
record_undo_event({before=Drawing.before, after=snapshot(Editor_state.lines.current_drawing_index)})
record_undo_event({before=Drawing.before, after=snapshot(State.lines.current_drawing_index)})
Drawing.before = nil
end
else
for line_index,line in ipairs(Editor_state.lines) do
for line_index,line in ipairs(State.lines) do
if line.mode == 'text' then
if Text.in_line(line, x,y, Editor_state.margin_left, App.screen.width-Editor_state.margin_right) then
if Text.in_line(line, x,y, State.margin_left, App.screen.width-State.margin_right) then
--? print('reset selection')
Editor_state.cursor1 = {
State.cursor1 = {
line=line_index,
pos=Text.to_pos_on_line(line, x, y, Editor_state.margin_left, App.screen.width-Editor_state.margin_right),
pos=Text.to_pos_on_line(line, x, y, State.margin_left, App.screen.width-State.margin_right),
}
--? print('cursor', Editor_state.cursor1.line, Editor_state.cursor1.pos)
if Editor_state.mousepress_shift then
if Editor_state.old_selection1.line == nil then
Editor_state.selection1 = Editor_state.old_cursor1
--? print('cursor', State.cursor1.line, State.cursor1.pos)
if State.mousepress_shift then
if State.old_selection1.line == nil then
State.selection1 = State.old_cursor1
else
Editor_state.selection1 = Editor_state.old_selection1
State.selection1 = State.old_selection1
end
end
Editor_state.old_cursor1, Editor_state.old_selection1, Editor_state.mousepress_shift = nil
if eq(Editor_state.cursor1, Editor_state.selection1) then
Editor_state.selection1 = {}
State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
if eq(State.cursor1, State.selection1) then
State.selection1 = {}
end
break
end
end
end
--? print('selection:', Editor_state.selection1.line, Editor_state.selection1.pos)
--? print('selection:', State.selection1.line, State.selection1.pos)
end
end
function edit.textinput(t)
for _,line in ipairs(Editor_state.lines) do line.y = nil end -- just in case we scroll
if Editor_state.search_term then
Editor_state.search_term = Editor_state.search_term..t
Editor_state.search_text = nil
function edit.textinput(State, t)
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
if State.search_term then
State.search_term = State.search_term..t
State.search_text = nil
Text.search_next()
elseif Editor_state.current_drawing_mode == 'name' then
local before = snapshot(Editor_state.lines.current_drawing_index)
local drawing = Editor_state.lines.current_drawing
elseif State.current_drawing_mode == 'name' then
local before = snapshot(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(Editor_state.lines.current_drawing_index)})
record_undo_event({before=before, after=snapshot(State.lines.current_drawing_index)})
else
Text.textinput(t)
end
schedule_save()
end
function edit.keychord_pressed(chord, key)
if Editor_state.selection1.line and
not Editor_state.lines.current_drawing and
function edit.keychord_pressed(State, chord, key)
if State.selection1.line and
not State.lines.current_drawing and
-- printable character created using shift key => delete selection
-- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
(not App.shift_down() or utf8.len(key) == 1) and
chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and backspace ~= 'delete' and not App.is_cursor_movement(chord) then
Text.delete_selection(Editor_state.margin_left, App.screen.width-Editor_state.margin_right)
Text.delete_selection(State.margin_left, App.screen.width-State.margin_right)
end
if Editor_state.search_term then
if State.search_term then
if chord == 'escape' then
Editor_state.search_term = nil
Editor_state.search_text = nil
Editor_state.cursor1 = Editor_state.search_backup.cursor
Editor_state.screen_top1 = Editor_state.search_backup.screen_top
Editor_state.search_backup = nil
State.search_term = nil
State.search_text = nil
State.cursor1 = State.search_backup.cursor
State.screen_top1 = State.search_backup.screen_top
State.search_backup = nil
Text.redraw_all() -- if we're scrolling, reclaim all fragments to avoid memory leaks
elseif chord == 'return' then
Editor_state.search_term = nil
Editor_state.search_text = nil
Editor_state.search_backup = nil
State.search_term = nil
State.search_text = nil
State.search_backup = nil
elseif chord == 'backspace' then
local len = utf8.len(Editor_state.search_term)
local byte_offset = Text.offset(Editor_state.search_term, len)
Editor_state.search_term = string.sub(Editor_state.search_term, 1, byte_offset-1)
Editor_state.search_text = nil
local len = utf8.len(State.search_term)
local byte_offset = Text.offset(State.search_term, len)
State.search_term = string.sub(State.search_term, 1, byte_offset-1)
State.search_text = nil
elseif chord == 'down' then
Editor_state.cursor1.pos = Editor_state.cursor1.pos+1
State.cursor1.pos = State.cursor1.pos+1
Text.search_next()
elseif chord == 'up' then
Text.search_previous()
end
return
elseif chord == 'C-f' then
Editor_state.search_term = ''
Editor_state.search_backup = {cursor={line=Editor_state.cursor1.line, pos=Editor_state.cursor1.pos}, screen_top={line=Editor_state.screen_top1.line, pos=Editor_state.screen_top1.pos}}
assert(Editor_state.search_text == nil)
State.search_term = ''
State.search_backup = {cursor={line=State.cursor1.line, pos=State.cursor1.pos}, screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos}}
assert(State.search_text == nil)
elseif chord == 'C-=' then
initialize_font_settings(Editor_state.font_height+2)
initialize_font_settings(State.font_height+2)
Text.redraw_all()
elseif chord == 'C--' then
initialize_font_settings(Editor_state.font_height-2)
initialize_font_settings(State.font_height-2)
Text.redraw_all()
elseif chord == 'C-0' then
initialize_font_settings(20)
Text.redraw_all()
elseif chord == 'C-z' then
for _,line in ipairs(Editor_state.lines) do line.y = nil end -- just in case we scroll
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
local event = undo_event()
if event then
local src = event.before
Editor_state.screen_top1 = deepcopy(src.screen_top)
Editor_state.cursor1 = deepcopy(src.cursor)
Editor_state.selection1 = deepcopy(src.selection)
patch(Editor_state.lines, event.after, event.before)
State.screen_top1 = deepcopy(src.screen_top)
State.cursor1 = deepcopy(src.cursor)
State.selection1 = deepcopy(src.selection)
patch(State.lines, event.after, event.before)
Text.redraw_all() -- if we're scrolling, reclaim all fragments to avoid memory leaks
schedule_save()
end
elseif chord == 'C-y' then
for _,line in ipairs(Editor_state.lines) do line.y = nil end -- just in case we scroll
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
local event = redo_event()
if event then
local src = event.after
Editor_state.screen_top1 = deepcopy(src.screen_top)
Editor_state.cursor1 = deepcopy(src.cursor)
Editor_state.selection1 = deepcopy(src.selection)
patch(Editor_state.lines, event.before, event.after)
State.screen_top1 = deepcopy(src.screen_top)
State.cursor1 = deepcopy(src.cursor)
State.selection1 = deepcopy(src.selection)
patch(State.lines, event.before, event.after)
Text.redraw_all() -- if we're scrolling, reclaim all fragments to avoid memory leaks
schedule_save()
end
-- clipboard
elseif chord == 'C-c' then
for _,line in ipairs(Editor_state.lines) do line.y = nil end -- just in case we scroll
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
local s = Text.selection()
if s then
App.setClipboardText(s)
end
elseif chord == 'C-x' then
for _,line in ipairs(Editor_state.lines) do line.y = nil end -- just in case we scroll
local s = Text.cut_selection(Editor_state.margin_left, App.screen.width-Editor_state.margin_right)
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
local s = Text.cut_selection(State.margin_left, App.screen.width-State.margin_right)
if s then
App.setClipboardText(s)
end
schedule_save()
elseif chord == 'C-v' then
for _,line in ipairs(Editor_state.lines) do line.y = nil end -- just in case we scroll
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
-- 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 = Editor_state.cursor1.line
local before_line = State.cursor1.line
local before = snapshot(before_line)
local clipboard_data = App.getClipboardText()
for _,code in utf8.codes(clipboard_data) do
@ -392,10 +392,10 @@ function edit.keychord_pressed(chord, key)
end
end
if Text.cursor_past_screen_bottom() then
Text.snap_cursor_to_bottom_of_screen(Editor_state.margin_left, App.screen.height-Editor_state.margin_right)
Text.snap_cursor_to_bottom_of_screen(State.margin_left, App.screen.height-State.margin_right)
end
schedule_save()
record_undo_event({before=before, after=snapshot(before_line, Editor_state.cursor1.line)})
record_undo_event({before=before, after=snapshot(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
@ -407,36 +407,36 @@ function edit.keychord_pressed(chord, key)
schedule_save()
end
elseif chord == 'escape' and not App.mouse_down(1) then
for _,line in ipairs(Editor_state.lines) do
for _,line in ipairs(State.lines) do
if line.mode == 'drawing' then
line.show_help = false
end
end
elseif Editor_state.current_drawing_mode == 'name' then
elseif State.current_drawing_mode == 'name' then
if chord == 'return' then
Editor_state.current_drawing_mode = Editor_state.previous_drawing_mode
Editor_state.previous_drawing_mode = nil
State.current_drawing_mode = State.previous_drawing_mode
State.previous_drawing_mode = nil
else
local before = snapshot(Editor_state.lines.current_drawing_index)
local drawing = Editor_state.lines.current_drawing
local before = snapshot(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(Editor_state.lines.current_drawing_index)})
record_undo_event({before=before, after=snapshot(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(Editor_state.lines.current_drawing_index)})
record_undo_event({before=before, after=snapshot(State.lines.current_drawing_index)})
end
end
schedule_save()
else
for _,line in ipairs(Editor_state.lines) do line.y = nil end -- just in case we scroll
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
Text.keychord_pressed(chord)
end
end
function edit.key_released(key, scancode)
function edit.key_released(State, key, scancode)
end

View File

@ -149,7 +149,7 @@ end
function App.draw()
Button_handlers = {}
edit.draw()
edit.draw(Editor_state)
end
function App.update(dt)
@ -162,11 +162,11 @@ function App.update(dt)
Last_resize_time = nil
end
end
edit.update(dt)
edit.update(Editor_state, dt)
end
function love.quit()
edit.quit()
edit.quit(Editor_state)
-- save some important settings
local x,y,displayindex = love.window.getPosition()
local filename = Editor_state.filename
@ -184,25 +184,25 @@ end
function App.mousepressed(x,y, mouse_button)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.mouse_pressed(x,y, mouse_button)
return edit.mouse_pressed(Editor_state, x,y, mouse_button)
end
function App.mousereleased(x,y, mouse_button)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.mouse_released(x,y, mouse_button)
return edit.mouse_released(Editor_state, x,y, mouse_button)
end
function App.textinput(t)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.textinput(t)
return edit.textinput(Editor_state, t)
end
function App.keychord_pressed(chord, key)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.keychord_pressed(chord, key)
return edit.keychord_pressed(Editor_state, chord, key)
end
function App.keyreleased(key, scancode)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.key_released(key, scancode)
return edit.key_released(Editor_state, key, scancode)
end

View File

@ -4,7 +4,7 @@ function test_initial_state()
io.write('\ntest_initial_state')
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{}
edit.draw()
edit.draw(Editor_state)
check_eq(#Editor_state.lines, 1, 'F - test_initial_state/#lines')
check_eq(Editor_state.cursor1.line, 1, 'F - test_initial_state/cursor:line')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_initial_state/cursor:pos')
@ -16,7 +16,7 @@ function test_click_to_create_drawing()
io.write('\ntest_click_to_create_drawing')
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{}
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_click(8,Editor_state.margin_top+8, 1)
-- cursor skips drawing to always remain on text
check_eq(#Editor_state.lines, 2, 'F - test_click_to_create_drawing/#lines')
@ -40,7 +40,7 @@ function test_insert_first_character()
io.write('\ntest_insert_first_character')
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{}
edit.draw()
edit.draw(Editor_state)
App.run_after_textinput('a')
local y = Editor_state.margin_top
App.screen.check(y, 'a', 'F - test_insert_first_character/screen:1')
@ -59,9 +59,10 @@ end
function test_move_left()
io.write('\ntest_move_left')
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'a'}
Editor_state.cursor1 = {line=1, pos=2}
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('left')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_move_left')
end
@ -71,7 +72,7 @@ function test_move_right()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'a'}
Editor_state.cursor1 = {line=1, pos=1}
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('right')
check_eq(Editor_state.cursor1.pos, 2, 'F - test_move_right')
end
@ -81,7 +82,7 @@ function test_move_left_to_previous_line()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc', 'def'}
Editor_state.cursor1 = {line=2, pos=1}
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('left')
check_eq(Editor_state.cursor1.line, 1, 'F - test_move_left_to_previous_line/line')
check_eq(Editor_state.cursor1.pos, 4, 'F - test_move_left_to_previous_line/pos') -- past end of line
@ -92,7 +93,7 @@ function test_move_right_to_next_line()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc', 'def'}
Editor_state.cursor1 = {line=1, pos=4} -- past end of line
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('right')
check_eq(Editor_state.cursor1.line, 2, 'F - test_move_right_to_next_line/line')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_move_right_to_next_line/pos')
@ -103,7 +104,7 @@ function test_move_to_start_of_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc'}
Editor_state.cursor1 = {line=1, pos=3}
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-left')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_move_to_start_of_word')
end
@ -113,7 +114,7 @@ function test_move_to_start_of_previous_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def'}
Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-left')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_move_to_start_of_previous_word')
end
@ -123,7 +124,7 @@ function test_skip_to_previous_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def'}
Editor_state.cursor1 = {line=1, pos=5} -- at the start of second word
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-left')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_skip_to_previous_word')
end
@ -133,7 +134,7 @@ function test_skip_past_tab_to_previous_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def\tghi'}
Editor_state.cursor1 = {line=1, pos=10} -- within third word
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-left')
check_eq(Editor_state.cursor1.pos, 9, 'F - test_skip_past_tab_to_previous_word')
end
@ -143,7 +144,7 @@ function test_skip_multiple_spaces_to_previous_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def'}
Editor_state.cursor1 = {line=1, pos=6} -- at the start of second word
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-left')
check_eq(Editor_state.cursor1.pos, 1, 'F - test_skip_multiple_spaces_to_previous_word')
end
@ -153,7 +154,7 @@ function test_move_to_start_of_word_on_previous_line()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def', 'ghi'}
Editor_state.cursor1 = {line=2, pos=1}
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-left')
check_eq(Editor_state.cursor1.line, 1, 'F - test_move_to_start_of_word_on_previous_line/line')
check_eq(Editor_state.cursor1.pos, 5, 'F - test_move_to_start_of_word_on_previous_line/pos')
@ -164,7 +165,7 @@ function test_move_past_end_of_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def'}
Editor_state.cursor1 = {line=1, pos=1}
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-right')
check_eq(Editor_state.cursor1.pos, 4, 'F - test_move_past_end_of_word')
end
@ -174,7 +175,7 @@ function test_skip_to_next_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def'}
Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-right')
check_eq(Editor_state.cursor1.pos, 8, 'F - test_skip_to_next_word')
end
@ -184,7 +185,7 @@ function test_skip_past_tab_to_next_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc\tdef'}
Editor_state.cursor1 = {line=1, pos=1} -- at the space between words
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-right')
check_eq(Editor_state.cursor1.pos, 4, 'F - test_skip_past_tab_to_next_word')
end
@ -194,7 +195,7 @@ function test_skip_multiple_spaces_to_next_word()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def'}
Editor_state.cursor1 = {line=1, pos=4} -- at the start of second word
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-right')
check_eq(Editor_state.cursor1.pos, 9, 'F - test_skip_multiple_spaces_to_next_word')
end
@ -204,7 +205,7 @@ function test_move_past_end_of_word_on_next_line()
App.screen.init{width=120, height=60}
Editor_state.lines = load_array{'abc def', 'ghi'}
Editor_state.cursor1 = {line=1, pos=8}
edit.draw()
edit.draw(Editor_state)
App.run_after_keychord('M-right')
check_eq(Editor_state.cursor1.line, 2, 'F - test_move_past_end_of_word_on_next_line/line')
check_eq(Editor_state.cursor1.pos, 4, 'F - test_move_past_end_of_word_on_next_line/pos')
@ -219,7 +220,7 @@ function test_click_with_mouse()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- click on the other line
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_click(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
-- cursor moves
check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse/cursor')
@ -235,7 +236,7 @@ function test_click_with_mouse_on_empty_line()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- click on the empty line
edit.draw()
edit.draw(Editor_state)
App.run_after_mouse_click(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
-- cursor moves
check_eq(Editor_state.cursor1.line, 1, 'F - test_click_with_mouse_on_empty_line/cursor')
@ -248,7 +249,7 @@ function test_draw_text()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_draw_text/screen:1')
y = y + Editor_state.line_height
@ -264,7 +265,7 @@ function test_draw_wrapping_text()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_draw_wrapping_text/screen:1')
y = y + Editor_state.line_height
@ -280,7 +281,7 @@ function test_draw_word_wrapping_text()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc ', 'F - test_draw_word_wrapping_text/screen:1')
y = y + Editor_state.line_height
@ -297,7 +298,7 @@ function test_draw_text_wrapping_within_word()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abcd ', 'F - test_draw_text_wrapping_within_word/screen:1')
y = y + Editor_state.line_height
@ -314,7 +315,7 @@ function test_draw_wrapping_text_containing_non_ascii()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'mada', 'F - test_draw_wrapping_text_containing_non_ascii/screen:1')
y = y + Editor_state.line_height
@ -332,7 +333,7 @@ function test_click_on_wrapping_line()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'madam ', 'F - test_click_on_wrapping_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -354,7 +355,7 @@ function test_click_on_wrapping_line_rendered_from_partway_at_top_of_screen()
Editor_state.cursor1 = {line=1, pos=8}
Editor_state.screen_top1 = {line=1, pos=7}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, "I'm ada", 'F - test_click_on_wrapping_line_rendered_from_partway_at_top_of_screen/baseline/screen:2')
y = y + Editor_state.line_height
@ -374,7 +375,7 @@ function test_click_past_end_of_wrapping_line()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'madam ', 'F - test_click_past_end_of_wrapping_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -397,7 +398,7 @@ function test_click_on_wrapping_line_containing_non_ascii()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'madam ', 'F - test_click_on_wrapping_line_containing_non_ascii/baseline/screen:1')
y = y + Editor_state.line_height
@ -421,7 +422,7 @@ function test_click_past_end_of_word_wrapping_line()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'the quick brown fox ', 'F - test_click_past_end_of_word_wrapping_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -439,7 +440,7 @@ function test_select_text()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- select a letter
App.fake_key_press('lshift')
App.run_after_keychord('S-right')
@ -461,7 +462,7 @@ function test_cursor_movement_without_shift_resets_selection()
Editor_state.selection1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- press an arrow key without shift
App.run_after_keychord('right')
-- no change to data, selection is reset
@ -478,7 +479,7 @@ function test_edit_deletes_selection()
Editor_state.selection1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- press a key
App.run_after_textinput('x')
-- selected text is deleted and replaced with the key
@ -494,7 +495,7 @@ function test_edit_with_shift_key_deletes_selection()
Editor_state.selection1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- mimic precise keypresses for a capital letter
App.fake_key_press('lshift')
App.keypressed('d')
@ -515,7 +516,7 @@ function test_copy_does_not_reset_selection()
Editor_state.selection1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- copy selection
App.run_after_keychord('C-c')
check_eq(App.clipboard, 'a', 'F - test_copy_does_not_reset_selection/clipboard')
@ -532,7 +533,7 @@ function test_cut()
Editor_state.selection1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- press a key
App.run_after_keychord('C-x')
check_eq(App.clipboard, 'a', 'F - test_cut/clipboard')
@ -549,7 +550,7 @@ function test_paste_replaces_selection()
Editor_state.selection1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- set clipboard
App.clipboard = 'xyz'
-- paste selection
@ -567,7 +568,7 @@ function test_deleting_selection_may_scroll()
Editor_state.cursor1 = {line=3, pos=2}
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'def', 'F - test_deleting_selection_may_scroll/baseline/screen:1')
y = y + Editor_state.line_height
@ -590,7 +591,7 @@ function test_edit_wrapping_text()
Editor_state.cursor1 = {line=2, pos=4}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
App.run_after_textinput('g')
App.run_after_textinput('h')
App.run_after_textinput('i')
@ -611,7 +612,7 @@ function test_insert_newline()
Editor_state.cursor1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_insert_newline/baseline/screen:1')
y = y + Editor_state.line_height
@ -655,7 +656,7 @@ function test_insert_from_clipboard()
Editor_state.cursor1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_insert_from_clipboard/baseline/screen:1')
y = y + Editor_state.line_height
@ -684,7 +685,7 @@ function test_move_cursor_using_mouse()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
Editor_state.selection1 = {}
edit.draw() -- populate line.y for each line in Editor_state.lines
edit.draw(Editor_state) -- populate line.y for each line in Editor_state.lines
App.run_after_mouse_release(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
check_eq(Editor_state.cursor1.line, 1, 'F - test_move_cursor_using_mouse/cursor:line')
check_eq(Editor_state.cursor1.pos, 2, 'F - test_move_cursor_using_mouse/cursor:pos')
@ -700,7 +701,7 @@ function test_select_text_using_mouse()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
Editor_state.selection1 = {}
edit.draw() -- populate line.y for each line in Editor_state.lines
edit.draw(Editor_state) -- populate line.y for each line in Editor_state.lines
-- press and hold on first location
App.run_after_mouse_press(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
-- drag and release somewhere else
@ -719,7 +720,7 @@ function test_select_text_using_mouse_and_shift()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
Editor_state.selection1 = {}
edit.draw() -- populate line.y for each line in Editor_state.lines
edit.draw(Editor_state) -- populate line.y for each line in Editor_state.lines
-- click on first location
App.run_after_mouse_press(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
App.run_after_mouse_release(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
@ -742,7 +743,7 @@ function test_select_text_repeatedly_using_mouse_and_shift()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
Editor_state.selection1 = {}
edit.draw() -- populate line.y for each line in Editor_state.lines
edit.draw(Editor_state) -- populate line.y for each line in Editor_state.lines
-- click on first location
App.run_after_mouse_press(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
App.run_after_mouse_release(Editor_state.margin_left+8,Editor_state.margin_top+5, 1)
@ -771,7 +772,7 @@ function test_cut_without_selection()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
Editor_state.selection1 = {}
edit.draw()
edit.draw(Editor_state)
-- try to cut without selecting text
App.run_after_keychord('C-x')
-- no crash
@ -786,7 +787,7 @@ function test_pagedown()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- initially the first two lines are displayed
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_pagedown/baseline/screen:1')
y = y + Editor_state.line_height
@ -817,7 +818,7 @@ function test_pagedown_skips_drawings()
local drawing_height = Editor_state.drawing_padding_height + drawing_width/2 -- default
-- initially the screen displays the first line and the drawing
-- 15px margin + 15px line1 + 10px margin + 25px drawing + 10px margin = 75px < screen height 80px
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_pagedown_skips_drawings/baseline/screen:1')
-- after pagedown the screen draws the drawing up top
@ -837,7 +838,7 @@ function test_pagedown_often_shows_start_of_wrapping_line()
Editor_state.cursor1 = {line=1, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_pagedown_often_shows_start_of_wrapping_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -866,7 +867,7 @@ function test_pagedown_can_start_from_middle_of_long_wrapping_line()
Editor_state.cursor1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc ', 'F - test_pagedown_can_start_from_middle_of_long_wrapping_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -893,7 +894,7 @@ function test_down_arrow_moves_cursor()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- initially the first three lines are displayed
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_down_arrow_moves_cursor/baseline/screen:1')
y = y + Editor_state.line_height
@ -921,7 +922,7 @@ function test_down_arrow_scrolls_down_by_one_line()
Editor_state.cursor1 = {line=3, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_down_arrow_scrolls_down_by_one_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -948,7 +949,7 @@ function test_down_arrow_scrolls_down_by_one_screen_line()
Editor_state.cursor1 = {line=3, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_down_arrow_scrolls_down_by_one_screen_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -976,7 +977,7 @@ function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_
Editor_state.cursor1 = {line=3, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word/baseline/screen:1')
y = y + Editor_state.line_height
@ -1003,7 +1004,7 @@ function test_page_down_followed_by_down_arrow_does_not_scroll_screen_up()
Editor_state.cursor1 = {line=3, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_page_down_followed_by_down_arrow_does_not_scroll_screen_up/baseline/screen:1')
y = y + Editor_state.line_height
@ -1036,7 +1037,7 @@ function test_up_arrow_moves_cursor()
Editor_state.cursor1 = {line=3, pos=1}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_up_arrow_moves_cursor/baseline/screen:1')
y = y + Editor_state.line_height
@ -1064,7 +1065,7 @@ function test_up_arrow_scrolls_up_by_one_line()
Editor_state.cursor1 = {line=2, pos=1}
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'def', 'F - test_up_arrow_scrolls_up_by_one_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1091,7 +1092,7 @@ function test_up_arrow_scrolls_up_by_one_screen_line()
Editor_state.cursor1 = {line=3, pos=6}
Editor_state.screen_top1 = {line=3, pos=5}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'jkl', 'F - test_up_arrow_scrolls_up_by_one_screen_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1118,7 +1119,7 @@ function test_up_arrow_scrolls_up_to_final_screen_line()
Editor_state.cursor1 = {line=2, pos=1}
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'ghi', 'F - test_up_arrow_scrolls_up_to_final_screen_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1147,7 +1148,7 @@ function test_up_arrow_scrolls_up_to_empty_line()
Editor_state.cursor1 = {line=2, pos=1}
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_up_arrow_scrolls_up_to_empty_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1174,7 +1175,7 @@ function test_pageup()
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
-- initially the last two lines are displayed
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'def', 'F - test_pageup/baseline/screen:1')
y = y + Editor_state.line_height
@ -1197,7 +1198,7 @@ function test_pageup_scrolls_up_by_screen_line()
Editor_state.cursor1 = {line=2, pos=1}
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'ghi', 'F - test_pageup_scrolls_up_by_screen_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1225,7 +1226,7 @@ function test_pageup_scrolls_up_from_middle_screen_line()
Editor_state.cursor1 = {line=2, pos=5}
Editor_state.screen_top1 = {line=2, pos=5}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'jkl', 'F - test_pageup_scrolls_up_from_middle_screen_line/baseline/screen:2')
y = y + Editor_state.line_height
@ -1251,7 +1252,7 @@ function test_enter_on_bottom_line_scrolls_down()
Editor_state.cursor1 = {line=3, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_enter_on_bottom_line_scrolls_down/baseline/screen:1')
y = y + Editor_state.line_height
@ -1279,7 +1280,7 @@ function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
Editor_state.cursor1 = {line=4, pos=2}
Editor_state.screen_top1 = {line=4, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'jkl', 'F - test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom/baseline/screen:1')
-- after hitting the enter key the screen does not scroll down
@ -1301,7 +1302,7 @@ function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bot
Editor_state.cursor1 = {line=2, pos=1}
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- after hitting the inserting_text key the screen does not scroll down
App.run_after_textinput('a')
check_eq(Editor_state.screen_top1.line, 2, 'F - test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bottom/screen_top')
@ -1319,7 +1320,7 @@ function test_typing_on_bottom_line_scrolls_down()
Editor_state.cursor1 = {line=3, pos=4}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_typing_on_bottom_line_scrolls_down/baseline/screen:1')
y = y + Editor_state.line_height
@ -1350,7 +1351,7 @@ function test_left_arrow_scrolls_up_in_wrapped_line()
Editor_state.screen_bottom1 = {}
-- cursor is at top of screen
Editor_state.cursor1 = {line=3, pos=5}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'jkl', 'F - test_left_arrow_scrolls_up_in_wrapped_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1378,7 +1379,7 @@ function test_right_arrow_scrolls_down_in_wrapped_line()
Editor_state.screen_bottom1 = {}
-- cursor is at bottom right of screen
Editor_state.cursor1 = {line=3, pos=5}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_right_arrow_scrolls_down_in_wrapped_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1407,7 +1408,7 @@ function test_home_scrolls_up_in_wrapped_line()
Editor_state.screen_bottom1 = {}
-- cursor is at top of screen
Editor_state.cursor1 = {line=3, pos=5}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'jkl', 'F - test_home_scrolls_up_in_wrapped_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1435,7 +1436,7 @@ function test_end_scrolls_down_in_wrapped_line()
Editor_state.screen_bottom1 = {}
-- cursor is at bottom right of screen
Editor_state.cursor1 = {line=3, pos=5}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc', 'F - test_end_scrolls_down_in_wrapped_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1463,7 +1464,7 @@ function test_position_cursor_on_recently_edited_wrapping_line()
Editor_state.cursor1 = {line=1, pos=25}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'abc def ghi ', 'F - test_position_cursor_on_recently_edited_wrapping_line/baseline1/screen:1')
y = y + Editor_state.line_height
@ -1496,7 +1497,7 @@ function test_backspace_can_scroll_up()
Editor_state.cursor1 = {line=2, pos=1}
Editor_state.screen_top1 = {line=2, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'def', 'F - test_backspace_can_scroll_up/baseline/screen:1')
y = y + Editor_state.line_height
@ -1523,7 +1524,7 @@ function test_backspace_can_scroll_up_screen_line()
Editor_state.cursor1 = {line=3, pos=5}
Editor_state.screen_top1 = {line=3, pos=5}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
local y = Editor_state.margin_top
App.screen.check(y, 'jkl', 'F - test_backspace_can_scroll_up_screen_line/baseline/screen:1')
y = y + Editor_state.line_height
@ -1652,7 +1653,7 @@ function test_undo_insert_text()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
-- insert a character
edit.draw()
edit.draw(Editor_state)
App.run_after_textinput('g')
check_eq(Editor_state.cursor1.line, 2, 'F - test_undo_insert_text/baseline/cursor:line')
check_eq(Editor_state.cursor1.pos, 5, 'F - test_undo_insert_text/baseline/cursor:pos')
@ -1723,7 +1724,7 @@ function test_undo_restores_selection()
Editor_state.selection1 = {line=1, pos=2}
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.screen_bottom1 = {}
edit.draw()
edit.draw(Editor_state)
-- delete selected text
App.run_after_textinput('x')
check_eq(Editor_state.lines[1].data, 'xbc', 'F - test_undo_restores_selection/baseline')