about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-07-17 21:05:01 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-07-17 21:05:01 -0700
commit29dac6a6eccf5e5f09debe789fb4f314e460088b (patch)
treee9c7d252374c0c3b1ad6130a07875280d1f6d47e
parent64eed749809a4412a6663d149cd57a41431df91d (diff)
downloadview.love-29dac6a6eccf5e5f09debe789fb4f314e460088b.tar.gz
separate data structure for each line's cache data
I have no idea what the performance implications of this are..
-rw-r--r--drawing_tests.lua30
-rw-r--r--edit.lua21
-rw-r--r--main.lua2
-rw-r--r--select.lua1
-rw-r--r--text.lua108
-rw-r--r--text_tests.lua108
-rw-r--r--undo.lua11
7 files changed, 211 insertions, 70 deletions
diff --git a/drawing_tests.lua b/drawing_tests.lua
index 4ea4b04..4c8c830 100644
--- a/drawing_tests.lua
+++ b/drawing_tests.lua
@@ -8,6 +8,7 @@ function test_creating_drawing_saves()
   Editor_state = edit.initialize_test_state()
   Editor_state.filename = 'foo'
   Editor_state.lines = load_array{}
+  Text.redraw_all(Editor_state)
   edit.draw(Editor_state)
   -- click on button to create drawing
   edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)
@@ -28,6 +29,7 @@ function test_draw_line()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   check_eq(#Editor_state.lines, 2, 'F - test_draw_line/baseline/#lines')
@@ -54,6 +56,7 @@ function test_draw_line()
   -- The format on disk isn't perfectly stable. Table fields can be reordered.
   -- So just reload from disk to verify.
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   local drawing = Editor_state.lines[1]
   check_eq(#drawing.shapes, 1, 'F - test_draw_line/save/#shapes')
   check_eq(#drawing.points, 2, 'F - test_draw_line/save/#points')
@@ -72,6 +75,7 @@ function test_draw_horizontal_line()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'manhattan'
   edit.draw(Editor_state)
   check_eq(#Editor_state.lines, 2, 'F - test_draw_horizontal_line/baseline/#lines')
@@ -100,6 +104,7 @@ function test_draw_circle()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   check_eq(#Editor_state.lines, 2, 'F - test_draw_circle/baseline/#lines')
@@ -129,6 +134,7 @@ function test_cancel_stroke()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   check_eq(#Editor_state.lines, 2, 'F - test_cancel_stroke/baseline/#lines')
@@ -151,6 +157,7 @@ function test_keys_do_not_affect_shape_when_mouse_up()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   -- hover over drawing and press 'o' without holding mouse
@@ -167,6 +174,7 @@ function test_draw_circle_mid_stroke()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   check_eq(#Editor_state.lines, 2, 'F - test_draw_circle_mid_stroke/baseline/#lines')
@@ -195,6 +203,7 @@ function test_draw_arc()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'circle'
   edit.draw(Editor_state)
   check_eq(#Editor_state.lines, 2, 'F - test_draw_arc/baseline/#lines')
@@ -226,6 +235,7 @@ function test_draw_polygon()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   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')
@@ -264,6 +274,7 @@ function test_draw_rectangle()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   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')
@@ -308,6 +319,7 @@ function test_draw_rectangle_intermediate()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   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')
@@ -344,6 +356,7 @@ function test_draw_square()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   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')
@@ -388,6 +401,7 @@ function test_name_point()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   -- draw a line
@@ -420,6 +434,7 @@ function test_name_point()
   edit.update(Editor_state, 0)
   -- change is saved
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
   check_eq(p2.name, 'A', 'F - test_name_point/save')
 end
@@ -431,6 +446,7 @@ function test_move_point()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
@@ -450,6 +466,7 @@ function test_move_point()
   edit.update(Editor_state, 0)
   -- line is saved to disk
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   local drawing = Editor_state.lines[1]
   local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
   check_eq(p2.x, 35, 'F - test_move_point/save/x')
@@ -476,6 +493,7 @@ function test_move_point()
   edit.update(Editor_state, 0)
   -- change is saved
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
   check_eq(p2.x, 26, 'F - test_move_point/save/x')
   check_eq(p2.y, 44, 'F - test_move_point/save/y')
@@ -488,6 +506,7 @@ function test_move_point_on_manhattan_line()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'manhattan'
   edit.draw(Editor_state)
   edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
@@ -514,6 +533,7 @@ function test_delete_lines_at_point()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
@@ -534,6 +554,7 @@ function test_delete_lines_at_point()
   edit.update(Editor_state, 0)
   -- deleted points disappear after file is reloaded
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   check_eq(#Editor_state.lines[1].shapes, 0, 'F - test_delete_lines_at_point/save')
 end
 
@@ -543,6 +564,7 @@ function test_delete_line_under_mouse_pointer()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
@@ -567,6 +589,7 @@ function test_delete_point_from_polygon()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   -- first point
@@ -598,6 +621,7 @@ function test_delete_point_from_polygon()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   -- first point
@@ -626,6 +650,7 @@ function test_undo_name_point()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   -- draw a line
@@ -663,6 +688,7 @@ function test_undo_name_point()
   edit.update(Editor_state, 0)
   -- undo is saved
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
   check_eq(p2.name, '', 'F - test_undo_name_point/save')
 end
@@ -674,6 +700,7 @@ function test_undo_move_point()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
@@ -712,6 +739,7 @@ function test_undo_move_point()
   edit.update(Editor_state, 0)
   -- undo is saved
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   local p2 = Editor_state.lines[1].points[drawing.shapes[1].p2]
   check_eq(p2.x, 35, 'F - test_undo_move_point/save/x')
   check_eq(p2.y, 36, 'F - test_undo_move_point/save/y')
@@ -724,6 +752,7 @@ function test_undo_delete_point()
   App.screen.init{width=Test_margin_left+256, height=300}  -- drawing coordinates 1:1 with pixels
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.current_drawing_mode = 'line'
   edit.draw(Editor_state)
   edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1)
@@ -751,5 +780,6 @@ function test_undo_delete_point()
   edit.update(Editor_state, 0)
   -- undo is saved
   Editor_state.lines = load_from_disk(Editor_state.filename)
+  Text.redraw_all(Editor_state)
   check_eq(#Editor_state.lines[1].shapes, 2, 'F - test_undo_delete_point/save')
 end
diff --git a/edit.lua b/edit.lua
index c0b2f8e..fc5a28f 100644
--- a/edit.lua
+++ b/edit.lua
@@ -38,11 +38,6 @@ function edit.initialize_state(top, left, right, font_height, line_height)  -- c
     -- a text is a table with:
     --    mode = 'text',
     --    string data,
-    --    some cached data that's blown away and recomputed when data changes:
-    --      startpos, the index of data the line starts rendering from (if currently on screen), can only be >1 for topmost line on screen
-    --      starty, the y coord in pixels
-    --      fragments: snippets of rendered love.graphics.Text, guaranteed to not wrap
-    --      screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
     -- a drawing is a table with:
     --    mode = 'drawing'
     --    a (y) coord in pixels (updated while painting screen),
@@ -59,12 +54,19 @@ function edit.initialize_state(top, left, right, font_height, line_height)  -- c
     -- Unless otherwise specified, coord fields are normalized; a drawing is always 256 units wide
     -- The field names are carefully chosen so that switching modes in midstream
     -- remembers previously entered points where that makes sense.
-    lines = {{mode='text', data=''}},
+    lines = {{mode='text', data=''}},  -- array of lines
 
     -- Lines can be too long to fit on screen, in which case they _wrap_ into
     -- multiple _screen lines_.
-    --
-    -- Therefore, any potential location for the cursor can be described in two ways:
+
+    -- rendering wrapped text lines needs some additional short-lived data per line:
+    --   startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
+    --   starty, the y coord in pixels the line starts rendering from
+    --   fragments: snippets of rendered love.graphics.Text, guaranteed to not straddle screen lines
+    --   screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
+    text_line_cache = {},
+
+    -- Given wrapping, any potential location for the text cursor can be described in two ways:
     -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
     -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
     --
@@ -119,6 +121,7 @@ end  -- App.initialize_state
 function edit.draw(State)
   App.color(Text_color)
 --?   print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
+  assert(#State.lines == #State.text_line_cache)
   assert(Text.le1(State.screen_top1, State.cursor1))
   State.cursor_y = -1
   local y = State.top
@@ -141,6 +144,7 @@ function edit.draw(State)
           onpress1 = function()
                        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={}})
+                       table.insert(State.text_line_cache, line_index, {})
                        if State.cursor1.line >= line_index then
                          State.cursor1.line = State.cursor1.line+1
                        end
@@ -343,6 +347,7 @@ function edit.keychord_pressed(State, chord, key)
       State.cursor1 = deepcopy(src.cursor)
       State.selection1 = deepcopy(src.selection)
       patch(State.lines, event.after, event.before)
+      patch_placeholders(State.text_line_cache, event.after, event.before)
       -- invalidate various cached bits of lines
       State.lines.current_drawing = nil
       -- if we're scrolling, reclaim all fragments to avoid memory leaks
diff --git a/main.lua b/main.lua
index 11bf3db..6c5dc9c 100644
--- a/main.lua
+++ b/main.lua
@@ -39,6 +39,7 @@ function App.initialize(arg)
   if #arg > 0 then
     Editor_state.filename = arg[1]
     Editor_state.lines = load_from_disk(Editor_state.filename)
+    Text.redraw_all(Editor_state)
     Editor_state.screen_top1 = {line=1, pos=1}
     Editor_state.cursor1 = {line=1, pos=1}
     for i,line in ipairs(Editor_state.lines) do
@@ -49,6 +50,7 @@ function App.initialize(arg)
     end
   else
     Editor_state.lines = load_from_disk(Editor_state.filename)
+    Text.redraw_all(Editor_state)
     if Editor_state.cursor1.line > #Editor_state.lines or Editor_state.lines[Editor_state.cursor1.line].mode ~= 'text' then
       for i,line in ipairs(Editor_state.lines) do
         if line.mode == 'text' then
diff --git a/select.lua b/select.lua
index a728f84..ec48e3f 100644
--- a/select.lua
+++ b/select.lua
@@ -149,6 +149,7 @@ function Text.delete_selection_without_undo(State)
   local rhs = State.lines[maxl].data:sub(max_offset)
   for i=maxl,minl+1,-1 do
     table.remove(State.lines, i)
+    table.remove(State.text_line_cache, i)
   end
   State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..rhs
 end
diff --git a/text.lua b/text.lua
index 8bd0fac..bafc097 100644
--- a/text.lua
+++ b/text.lua
@@ -12,18 +12,19 @@ function Text.draw(State, line_index, y, startpos)
 --?   print('text.draw', line_index)
   App.color(Text_color)
   local line = State.lines[line_index]
-  line.starty = y
-  line.startpos = startpos
+  local line_cache = State.text_line_cache[line_index]
+  line_cache.starty = y
+  line_cache.startpos = startpos
   -- wrap long lines
   local x = State.left
   local pos = 1
   local screen_line_starting_pos = 1
-  if line.fragments == nil then
+  if line_cache.fragments == nil then
     Text.compute_fragments(State, line_index)
   end
   Text.populate_screen_line_starting_pos(State, line_index)
 --?   print('--')
-  for _, f in ipairs(line.fragments) do
+  for _, f in ipairs(line_cache.fragments) do
     local frag, frag_text = f.data, f.text
     -- render fragment
     local frag_width = App.width(frag_text)
@@ -95,7 +96,8 @@ end
 function Text.compute_fragments(State, line_index)
 --?   print('compute_fragments', State.right)
   local line = State.lines[line_index]
-  line.fragments = {}
+  local line_cache = State.text_line_cache[line_index]
+  line_cache.fragments = {}
   local x = State.left
   -- try to wrap at word boundaries
   for frag in line.data:gmatch('%S*%s*') do
@@ -120,7 +122,7 @@ function Text.compute_fragments(State, line_index)
 --?           print(frag, x, frag1_width, State.right)
           assert(x + frag1_width <= State.right)
 --?           print('inserting '..frag1..' of width '..tostring(frag1_width)..'px')
-          table.insert(line.fragments, {data=frag1, text=frag1_text})
+          table.insert(line_cache.fragments, {data=frag1, text=frag1_text})
           frag = string.sub(frag, boffset)
           frag_text = App.newText(love.graphics.getFont(), frag)
           frag_width = App.width(frag_text)
@@ -130,7 +132,7 @@ function Text.compute_fragments(State, line_index)
     end
     if #frag > 0 then
 --?       print('inserting '..frag..' of width '..tostring(frag_width)..'px')
-      table.insert(line.fragments, {data=frag, text=frag_text})
+      table.insert(line_cache.fragments, {data=frag, text=frag_text})
     end
     x = x + frag_width
   end
@@ -205,11 +207,13 @@ function Text.keychord_pressed(State, chord)
       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)
+        table.remove(State.text_line_cache, State.cursor1.line-1)
       else
         -- join lines
         State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
         State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
         table.remove(State.lines, State.cursor1.line)
+        table.remove(State.text_line_cache, State.cursor1.line)
       end
       State.cursor1.line = State.cursor1.line-1
     end
@@ -247,13 +251,12 @@ function Text.keychord_pressed(State, chord)
         -- no change to State.cursor1.pos
       end
     elseif State.cursor1.line < #State.lines then
-      if State.lines[State.cursor1.line+1].mode == 'drawing' then
-        table.remove(State.lines, State.cursor1.line+1)
-      else
+      if State.lines[State.cursor1.line+1].mode == 'text' then
         -- join lines
         State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
-        table.remove(State.lines, State.cursor1.line+1)
       end
+      table.remove(State.lines, State.cursor1.line+1)
+      table.remove(State.text_line_cache, State.cursor1.line+1)
     end
     Text.clear_screen_line_cache(State, State.cursor1.line)
     schedule_save(State)
@@ -346,9 +349,9 @@ end
 function Text.insert_return(State)
   local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
   table.insert(State.lines, State.cursor1.line+1, {mode='text', data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
+  table.insert(State.text_line_cache, State.cursor1.line+1, {})
   State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
   Text.clear_screen_line_cache(State, State.cursor1.line)
-  Text.clear_screen_line_cache(State, State.cursor1.line+1)
   State.cursor1.line = State.cursor1.line+1
   State.cursor1.pos = 1
 end
@@ -418,7 +421,7 @@ function Text.up(State)
         Text.populate_screen_line_starting_pos(State, State.cursor1.line)
         -- previous text line found, pick its final screen line
 --?         print('has multiple screen lines')
-        local screen_line_starting_pos = State.lines[State.cursor1.line].screen_line_starting_pos
+        local screen_line_starting_pos = State.text_line_cache[State.cursor1.line].screen_line_starting_pos
 --?         print(#screen_line_starting_pos)
         screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
 --?         print('previous screen line starts at pos '..tostring(screen_line_starting_pos)..' of its line')
@@ -440,7 +443,7 @@ function Text.up(State)
     -- move up one screen line in current line
 --?     print('cursor is NOT at first screen line of its line')
     assert(screen_line_index > 1)
-    new_screen_line_starting_pos = State.lines[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
+    new_screen_line_starting_pos = State.text_line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
 --?     print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
     if State.screen_top1.line == State.cursor1.line and State.screen_top1.pos == screen_line_starting_pos then
       State.screen_top1.pos = new_screen_line_starting_pos
@@ -483,7 +486,7 @@ function Text.down(State)
     end
 --?     print('cursor is NOT at final screen line of its line')
     local screen_line_index, screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line(State)
-    new_screen_line_starting_pos = State.lines[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
+    new_screen_line_starting_pos = State.text_line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
 --?     print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
     local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
     local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
@@ -620,8 +623,8 @@ end
 
 function Text.pos_at_start_of_cursor_screen_line(State)
   Text.populate_screen_line_starting_pos(State, State.cursor1.line)
-  for i=#State.lines[State.cursor1.line].screen_line_starting_pos,1,-1 do
-    local spos = State.lines[State.cursor1.line].screen_line_starting_pos[i]
+  for i=#State.text_line_cache[State.cursor1.line].screen_line_starting_pos,1,-1 do
+    local spos = State.text_line_cache[State.cursor1.line].screen_line_starting_pos[i]
     if spos <= State.cursor1.pos then
       return i,spos
     end
@@ -631,7 +634,7 @@ end
 
 function Text.cursor_at_final_screen_line(State)
   Text.populate_screen_line_starting_pos(State, State.cursor1.line)
-  local screen_lines = State.lines[State.cursor1.line].screen_line_starting_pos
+  local screen_lines = State.text_line_cache[State.cursor1.line].screen_line_starting_pos
 --?   print(screen_lines[#screen_lines], State.cursor1.pos)
   return screen_lines[#screen_lines] <= State.cursor1.pos
 end
@@ -650,6 +653,7 @@ function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necess
   if State.cursor1.line > #State.lines then
     assert(State.cursor1.line == #State.lines+1)
     table.insert(State.lines, {mode='text', data=''})
+    table.insert(State.text_line_cache, {})
   end
 --?   print(y, App.screen.height, App.screen.height-State.line_height)
   if y > App.screen.height - State.line_height then
@@ -696,25 +700,27 @@ end
 
 function Text.in_line(State, line_index, x,y)
   local line = State.lines[line_index]
-  if line.starty == nil then return false end  -- outside current page
+  local line_cache = State.text_line_cache[line_index]
+  if line_cache.starty == nil then return false end  -- outside current page
   if x < State.left then return false end
-  if y < line.starty then return false end
+  if y < line_cache.starty then return false end
   Text.populate_screen_line_starting_pos(State, line_index)
-  return y < line.starty + State.line_height*(#line.screen_line_starting_pos - Text.screen_line_index(line.screen_line_starting_pos, line.startpos) + 1)
+  return y < line_cache.starty + State.line_height*(#line_cache.screen_line_starting_pos - Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos) + 1)
 end
 
 -- convert mx,my in pixels to schema-1 coordinates
 function Text.to_pos_on_line(State, line_index, mx, my)
   local line = State.lines[line_index]
-  if line.fragments == nil then
+  local line_cache = State.text_line_cache[line_index]
+  if line_cache.fragments == nil then
     Text.compute_fragments(State, line_index)
   end
-  assert(my >= line.starty)
+  assert(my >= line_cache.starty)
   -- duplicate some logic from Text.draw
-  local y = line.starty
-  local start_screen_line_index = Text.screen_line_index(line.screen_line_starting_pos, line.startpos)
-  for screen_line_index = start_screen_line_index,#line.screen_line_starting_pos do
-    local screen_line_starting_pos = line.screen_line_starting_pos[screen_line_index]
+  local y = line_cache.starty
+  local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
+  for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
+    local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
     local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
 --?     print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
     local nexty = y + State.line_height
@@ -722,9 +728,9 @@ function Text.to_pos_on_line(State, line_index, mx, my)
       -- On all wrapped screen lines but the final one, clicks past end of
       -- line position cursor on final character of screen line.
       -- (The final screen line positions past end of screen line as always.)
-      if screen_line_index < #line.screen_line_starting_pos and mx > Text.screen_line_width(State, line_index, screen_line_index) then
+      if screen_line_index < #line_cache.screen_line_starting_pos and mx > Text.screen_line_width(State, line_index, screen_line_index) then
 --?         print('past end of non-final line; return')
-        return line.screen_line_starting_pos[screen_line_index+1]-1
+        return line_cache.screen_line_starting_pos[screen_line_index+1]-1
       end
       local s = string.sub(line.data, screen_line_starting_byte_offset)
 --?       print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
@@ -737,11 +743,12 @@ end
 
 function Text.screen_line_width(State, line_index, i)
   local line = State.lines[line_index]
-  local start_pos = line.screen_line_starting_pos[i]
+  local line_cache = State.text_line_cache[line_index]
+  local start_pos = line_cache.screen_line_starting_pos[i]
   local start_offset = Text.offset(line.data, start_pos)
   local screen_line
-  if i < #line.screen_line_starting_pos then
-    local past_end_pos = line.screen_line_starting_pos[i+1]
+  if i < #line_cache.screen_line_starting_pos then
+    local past_end_pos = line_cache.screen_line_starting_pos[i+1]
     local past_end_offset = Text.offset(line.data, past_end_pos)
     screen_line = string.sub(line.data, start_offset, past_end_offset-1)
   else
@@ -844,8 +851,8 @@ function Text.to2(State, pos1)
   end
   local result = {line=pos1.line, screen_line=1}
   Text.populate_screen_line_starting_pos(State, pos1.line)
-  for i=#State.lines[pos1.line].screen_line_starting_pos,1,-1 do
-    local spos = State.lines[pos1.line].screen_line_starting_pos[i]
+  for i=#State.text_line_cache[pos1.line].screen_line_starting_pos,1,-1 do
+    local spos = State.text_line_cache[pos1.line].screen_line_starting_pos[i]
     if spos <= pos1.pos then
       result.screen_line = i
       result.screen_pos = pos1.pos - spos + 1
@@ -859,7 +866,7 @@ end
 function Text.to1(State, pos2)
   local result = {line=pos2.line, pos=pos2.screen_pos}
   if pos2.screen_line > 1 then
-    result.pos = State.lines[pos2.line].screen_line_starting_pos[pos2.screen_line] + pos2.screen_pos - 1
+    result.pos = State.text_line_cache[pos2.line].screen_line_starting_pos[pos2.screen_line] + pos2.screen_pos - 1
   end
   return result
 end
@@ -908,29 +915,30 @@ function Text.previous_screen_line(State, pos2)
   else
     local l = State.lines[pos2.line-1]
     Text.populate_screen_line_starting_pos(State, pos2.line-1)
-    return {line=pos2.line-1, screen_line=#State.lines[pos2.line-1].screen_line_starting_pos, screen_pos=1}
+    return {line=pos2.line-1, screen_line=#State.text_line_cache[pos2.line-1].screen_line_starting_pos, screen_pos=1}
   end
 end
 
 function Text.populate_screen_line_starting_pos(State, line_index)
   local line = State.lines[line_index]
-  if line.screen_line_starting_pos then
+  local line_cache = State.text_line_cache[line_index]
+  if line_cache.screen_line_starting_pos then
     return
   end
   -- duplicate some logic from Text.draw
-  if line.fragments == nil then
+  if line_cache.fragments == nil then
     Text.compute_fragments(State, line_index)
   end
-  line.screen_line_starting_pos = {1}
+  line_cache.screen_line_starting_pos = {1}
   local x = State.left
   local pos = 1
-  for _, f in ipairs(line.fragments) do
+  for _, f in ipairs(line_cache.fragments) do
     local frag, frag_text = f.data, f.text
     -- render fragment
     local frag_width = App.width(frag_text)
     if x + frag_width > State.right then
       x = State.left
-      table.insert(line.screen_line_starting_pos, pos)
+      table.insert(line_cache.screen_line_starting_pos, pos)
     end
     x = x + frag_width
     local frag_len = utf8.len(frag)
@@ -944,14 +952,15 @@ function Text.tweak_screen_top_and_cursor(State)
   if State.screen_top1.pos == 1 then return end
   Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
   local line = State.lines[State.screen_top1.line]
-  for i=2,#line.screen_line_starting_pos do
-    local pos = line.screen_line_starting_pos[i]
+  local line_cache = State.text_line_cache[State.screen_top1.line]
+  for i=2,#line_cache.screen_line_starting_pos do
+    local pos = line_cache.screen_line_starting_pos[i]
     if pos == State.screen_top1.pos then
       break
     end
     if pos > State.screen_top1.pos then
       -- make sure screen top is at start of a screen line
-      local prev = line.screen_line_starting_pos[i-1]
+      local prev = line_cache.screen_line_starting_pos[i-1]
       if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
         State.screen_top1.pos = prev
       else
@@ -988,14 +997,13 @@ end
 
 function Text.redraw_all(State)
 --?   print('clearing fragments')
-  for line_index,line in ipairs(State.lines) do
-    line.starty = nil
-    line.startpos = nil
-    Text.clear_screen_line_cache(State, line_index)
+  State.text_line_cache = {}
+  for i=1,#State.lines do
+    State.text_line_cache[i] = {}
   end
 end
 
 function Text.clear_screen_line_cache(State, line_index)
-  State.lines[line_index].fragments = nil
-  State.lines[line_index].screen_line_starting_pos = nil
+  State.text_line_cache[line_index].fragments = nil
+  State.text_line_cache[line_index].screen_line_starting_pos = nil
 end
diff --git a/text_tests.lua b/text_tests.lua
index 0126fef..37deec9 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -5,6 +5,7 @@ function test_initial_state()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{}
+  Text.redraw_all(Editor_state)
   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')
@@ -18,6 +19,7 @@ function test_click_to_create_drawing()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{}
+  Text.redraw_all(Editor_state)
   edit.draw(Editor_state)
   edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)
   -- cursor skips drawing to always remain on text
@@ -31,6 +33,7 @@ function test_backspace_to_delete_drawing()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'```lines', '```', ''}
+  Text.redraw_all(Editor_state)
   -- cursor is on text as always (outside tests this will get initialized correctly)
   Editor_state.cursor1.line = 2
   -- backspacing deletes the drawing
@@ -44,6 +47,7 @@ function test_insert_first_character()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{}
+  Text.redraw_all(Editor_state)
   edit.draw(Editor_state)
   edit.run_after_textinput(Editor_state, 'a')
   local y = Editor_state.top
@@ -56,6 +60,7 @@ function test_press_ctrl()
   App.screen.init{width=50, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{''}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -67,6 +72,7 @@ function test_move_left()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'a'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'left')
@@ -78,6 +84,7 @@ function test_move_right()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'a'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'right')
@@ -89,6 +96,7 @@ function test_move_left_to_previous_line()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'left')
@@ -101,6 +109,7 @@ function test_move_right_to_next_line()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=4}  -- past end of line
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'right')
@@ -113,6 +122,7 @@ function test_move_to_start_of_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=3}
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-left')
@@ -124,6 +134,7 @@ function test_move_to_start_of_previous_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=4}  -- at the space between words
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-left')
@@ -135,6 +146,7 @@ function test_skip_to_previous_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=5}  -- at the start of second word
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-left')
@@ -146,6 +158,7 @@ function test_skip_past_tab_to_previous_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def\tghi'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=10}  -- within third word
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-left')
@@ -157,6 +170,7 @@ function test_skip_multiple_spaces_to_previous_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc  def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=6}  -- at the start of second word
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-left')
@@ -168,6 +182,7 @@ function test_move_to_start_of_word_on_previous_line()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def', 'ghi'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-left')
@@ -180,6 +195,7 @@ function test_move_past_end_of_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-right')
@@ -191,6 +207,7 @@ function test_skip_to_next_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=4}  -- at the space between words
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-right')
@@ -202,6 +219,7 @@ function test_skip_past_tab_to_next_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc\tdef'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}  -- at the space between words
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-right')
@@ -213,6 +231,7 @@ function test_skip_multiple_spaces_to_next_word()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc  def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=4}  -- at the start of second word
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-right')
@@ -224,6 +243,7 @@ function test_move_past_end_of_word_on_next_line()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def', 'ghi'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=8}
   edit.draw(Editor_state)
   edit.run_after_keychord(Editor_state, 'M-right')
@@ -237,6 +257,7 @@ function test_click_with_mouse()
   App.screen.init{width=50, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -254,6 +275,7 @@ function test_click_with_mouse_on_empty_line()
   App.screen.init{width=50, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'', 'def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -269,6 +291,7 @@ function test_draw_text()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -286,6 +309,7 @@ function test_draw_wrapping_text()
   App.screen.init{width=50, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'defgh', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -303,6 +327,7 @@ function test_draw_word_wrapping_text()
   App.screen.init{width=60, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -321,6 +346,7 @@ function test_draw_text_wrapping_within_word()
   App.screen.init{width=60, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abcd e fghijk', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -339,6 +365,7 @@ function test_draw_wrapping_text_containing_non_ascii()
   App.screen.init{width=60, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'madam I’m adam', 'xyz'}  -- notice the non-ASCII apostrophe
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -356,8 +383,9 @@ function test_click_on_wrapping_line()
   -- display a wrapping line
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
-                  --  12345678901234
+                               --  12345678901234
   Editor_state.lines = load_array{"madam I'm adam"}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -379,8 +407,9 @@ function test_click_on_wrapping_line_rendered_from_partway_at_top_of_screen()
   -- display a wrapping line from its second screen line
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
-                  --  12345678901234
+                               --  12345678901234
   Editor_state.lines = load_array{"madam I'm adam"}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=8}
   Editor_state.screen_top1 = {line=1, pos=7}
   Editor_state.screen_bottom1 = {}
@@ -400,8 +429,9 @@ function test_click_past_end_of_wrapping_line()
   -- display a wrapping line
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
-                  --  12345678901234
+                               --  12345678901234
   Editor_state.lines = load_array{"madam I'm adam"}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -424,8 +454,9 @@ function test_click_on_wrapping_line_containing_non_ascii()
   -- display a wrapping line containing non-ASCII
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
-                  --  12345678901234
+                               --  12345678901234
   Editor_state.lines = load_array{'madam I’m adam'}  -- notice the non-ASCII apostrophe
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -448,9 +479,10 @@ function test_click_past_end_of_word_wrapping_line()
   -- display a long line wrapping at a word boundary on a screen of more realistic length
   App.screen.init{width=160, height=80}
   Editor_state = edit.initialize_test_state()
-                   -- 0        1         2
-                   -- 123456789012345678901
+                                -- 0        1         2
+                                -- 123456789012345678901
   Editor_state.lines = load_array{'the quick brown fox jumped over the lazy dog'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -470,6 +502,7 @@ function test_select_text()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -492,6 +525,7 @@ function test_cursor_movement_without_shift_resets_selection()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.selection1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
@@ -510,6 +544,7 @@ function test_edit_deletes_selection()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.selection1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
@@ -527,6 +562,7 @@ function test_edit_with_shift_key_deletes_selection()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.selection1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
@@ -549,6 +585,7 @@ function test_copy_does_not_reset_selection()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.selection1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
@@ -567,6 +604,7 @@ function test_cut()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.selection1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
@@ -585,6 +623,7 @@ function test_paste_replaces_selection()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.selection1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
@@ -605,6 +644,7 @@ function test_deleting_selection_may_scroll()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=2}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -629,6 +669,7 @@ function test_edit_wrapping_text()
   App.screen.init{width=50, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=4}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -651,6 +692,7 @@ function test_insert_newline()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -680,6 +722,7 @@ function test_insert_newline_at_start_of_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -697,6 +740,7 @@ function test_insert_from_clipboard()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -726,6 +770,7 @@ function test_move_cursor_using_mouse()
   App.screen.init{width=50, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -743,6 +788,7 @@ function test_select_text_using_mouse()
   App.screen.init{width=50, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -763,6 +809,7 @@ function test_select_text_using_mouse_and_shift()
   App.screen.init{width=50, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -787,6 +834,8 @@ function test_select_text_repeatedly_using_mouse_and_shift()
   App.screen.init{width=50, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'xyz'}
+  Text.redraw_all(Editor_state)
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -817,6 +866,7 @@ function test_cut_without_selection()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -833,6 +883,7 @@ function test_pagedown()
   App.screen.init{width=120, height=45}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -859,9 +910,10 @@ function test_pagedown_skips_drawings()
   App.screen.init{width=Editor_state.left+drawing_width, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc',               -- height 15
-                     '```lines', '```',   -- height 25
-                     'def',               -- height 15
-                     'ghi'}               -- height 15
+                                  '```lines', '```',   -- height 25
+                                  'def',               -- height 15
+                                  'ghi'}               -- height 15
+  Text.redraw_all(Editor_state)
   check_eq(Editor_state.lines[2].mode, 'drawing', 'F - test_pagedown_skips_drawings/baseline/lines')
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
@@ -887,6 +939,7 @@ function test_pagedown_often_shows_start_of_wrapping_line()
   App.screen.init{width=50, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -917,6 +970,7 @@ function test_pagedown_can_start_from_middle_of_long_wrapping_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -944,6 +998,7 @@ function test_down_arrow_moves_cursor()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -974,6 +1029,7 @@ function test_down_arrow_scrolls_down_by_one_line()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1002,6 +1058,7 @@ function test_down_arrow_scrolls_down_by_one_screen_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1031,6 +1088,7 @@ function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1059,6 +1117,7 @@ function test_page_down_followed_by_down_arrow_does_not_scroll_screen_up()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1093,6 +1152,7 @@ function test_up_arrow_moves_cursor()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=1}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1122,6 +1182,7 @@ function test_up_arrow_scrolls_up_by_one_line()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1150,6 +1211,7 @@ function test_up_arrow_scrolls_up_by_one_screen_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=6}
   Editor_state.screen_top1 = {line=3, pos=5}
   Editor_state.screen_bottom1 = {}
@@ -1178,6 +1240,7 @@ function test_up_arrow_scrolls_up_to_final_screen_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1208,6 +1271,7 @@ function test_up_arrow_scrolls_up_to_empty_line()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'', 'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1235,6 +1299,7 @@ function test_pageup()
   App.screen.init{width=120, height=45}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1260,6 +1325,7 @@ function test_pageup_scrolls_up_by_screen_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1289,6 +1355,7 @@ function test_pageup_scrolls_up_from_middle_screen_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=5}
   Editor_state.screen_top1 = {line=2, pos=5}
   Editor_state.screen_bottom1 = {}
@@ -1316,6 +1383,7 @@ function test_enter_on_bottom_line_scrolls_down()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1345,6 +1413,7 @@ function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=4, pos=2}
   Editor_state.screen_top1 = {line=4, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1368,6 +1437,7 @@ function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bot
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', ''}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1387,6 +1457,7 @@ function test_typing_on_bottom_line_scrolls_down()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=4}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1418,6 +1489,7 @@ function test_left_arrow_scrolls_up_in_wrapped_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.screen_top1 = {line=3, pos=5}
   Editor_state.screen_bottom1 = {}
   -- cursor is at top of screen
@@ -1447,6 +1519,7 @@ function test_right_arrow_scrolls_down_in_wrapped_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
   -- cursor is at bottom right of screen
@@ -1477,6 +1550,7 @@ function test_home_scrolls_up_in_wrapped_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.screen_top1 = {line=3, pos=5}
   Editor_state.screen_bottom1 = {}
   -- cursor is at top of screen
@@ -1506,6 +1580,7 @@ function test_end_scrolls_down_in_wrapped_line()
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
   -- cursor is at bottom right of screen
@@ -1536,6 +1611,7 @@ function test_position_cursor_on_recently_edited_wrapping_line()
   App.screen.init{width=100, height=200}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc def ghi jkl mno pqr ', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=25}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1570,6 +1646,7 @@ function test_backspace_can_scroll_up()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.screen_top1 = {line=2, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1599,6 +1676,7 @@ function test_backspace_can_scroll_up_screen_line()
   Editor_state = edit.initialize_test_state()
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=3, pos=5}
   Editor_state.screen_top1 = {line=3, pos=5}
   Editor_state.screen_bottom1 = {}
@@ -1628,6 +1706,7 @@ function test_backspace_past_line_boundary()
   Editor_state = edit.initialize_test_state()
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   -- backspace joins with previous line
   edit.run_after_keychord(Editor_state, 'backspace')
@@ -1644,6 +1723,7 @@ function test_backspace_over_selection()
   Editor_state = edit.initialize_test_state()
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.selection1 = {line=1, pos=2}
   -- backspace deletes the selected character, even though it's after the cursor
@@ -1663,6 +1743,7 @@ function test_backspace_over_selection_reverse()
   Editor_state = edit.initialize_test_state()
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   Editor_state.selection1 = {line=1, pos=1}
   -- backspace deletes the selected character
@@ -1680,8 +1761,8 @@ function test_backspace_over_multiple_lines()
   -- select just one character within a line with cursor after selection
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
-  Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   Editor_state.selection1 = {line=4, pos=2}
   -- backspace deletes the region and joins the remaining portions of lines on either side
@@ -1700,8 +1781,8 @@ function test_backspace_to_end_of_line()
   -- select region from cursor to end of line
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
-  Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=2}
   Editor_state.selection1 = {line=1, pos=4}
   -- backspace deletes rest of line without joining to any other line
@@ -1720,8 +1801,8 @@ function test_backspace_to_start_of_line()
   -- select region from cursor to start of line
   App.screen.init{width=Editor_state.left+30, height=60}
   Editor_state = edit.initialize_test_state()
-  Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=1}
   Editor_state.selection1 = {line=2, pos=3}
   -- backspace deletes beginning of line without joining to any other line
@@ -1740,6 +1821,7 @@ function test_undo_insert_text()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'def', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=4}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1775,6 +1857,7 @@ function test_undo_delete_text()
   App.screen.init{width=120, height=60}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc', 'defg', 'xyz'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=2, pos=5}
   Editor_state.screen_top1 = {line=1, pos=1}
   Editor_state.screen_bottom1 = {}
@@ -1813,6 +1896,7 @@ function test_undo_restores_selection()
   App.screen.init{width=75, height=80}
   Editor_state = edit.initialize_test_state()
   Editor_state.lines = load_array{'abc'}
+  Text.redraw_all(Editor_state)
   Editor_state.cursor1 = {line=1, pos=1}
   Editor_state.selection1 = {line=1, pos=2}
   Editor_state.screen_top1 = {line=1, pos=1}
diff --git a/undo.lua b/undo.lua
index f56a348..a1ac852 100644
--- a/undo.lua
+++ b/undo.lua
@@ -95,6 +95,17 @@ function patch(lines, from, to)
   end
 end
 
+function patch_placeholders(line_cache, from, to)
+  assert(from.start_line == to.start_line)
+  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)
+  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