about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua57
1 files changed, 28 insertions, 29 deletions
diff --git a/text.lua b/text.lua
index 5814693..dd5d50c 100644
--- a/text.lua
+++ b/text.lua
@@ -47,7 +47,7 @@ function Text.draw(State, line_index, y, startpos)
           end
         end
       end
-      -- render fragment
+      -- render screen line
       App.color(Text_color)
       App.screen.print(screen_line, State.left,y)
       y = y + State.line_height
@@ -123,7 +123,7 @@ function Text.text_input(State, t)
   if App.mouse_down(1) then return end
   if App.any_modifier_down() then
     if App.key_down(t) then
-      -- The modifiers didn't change the key. Handle it in keychord_pressed.
+      -- The modifiers didn't change the key. Handle it in keychord_press.
       return
     else
       -- Key mutated by the keyboard layout. Continue below.
@@ -149,17 +149,16 @@ end
 -- Don't handle any keys here that would trigger text_input above.
 function Text.keychord_press(State, chord)
 --?   print('chord', chord, State.selection1.line, State.selection1.pos)
-  --== shortcuts that mutate text
+  --== shortcuts that mutate text (must schedule_save)
   if chord == 'return' then
     local before_line = State.cursor1.line
     local before = snapshot(State, before_line)
     Text.insert_return(State)
-    State.selection1 = {}
     if State.cursor_y > App.screen.height - State.line_height then
       Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
     end
-    schedule_save(State)
     record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
+    schedule_save(State)
   elseif chord == 'tab' then
     local before = snapshot(State, State.cursor1.line)
 --?     print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
@@ -169,8 +168,8 @@ function Text.keychord_press(State, chord)
       Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
 --?       print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
     end
-    schedule_save(State)
     record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
+    schedule_save(State)
   elseif chord == 'backspace' then
     if State.selection1.line then
       Text.delete_selection(State, State.left, State.right)
@@ -208,12 +207,12 @@ function Text.keychord_press(State, chord)
         line=State.cursor1.line,
         pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
       }
-      Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
+      Text.redraw_all(State)  -- if we're scrolling, reclaim all line caches to avoid memory leaks
     end
     Text.clear_screen_line_cache(State, State.cursor1.line)
     assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
-    schedule_save(State)
     record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
+    schedule_save(State)
   elseif chord == 'delete' then
     if State.selection1.line then
       Text.delete_selection(State, State.left, State.right)
@@ -244,8 +243,8 @@ function Text.keychord_press(State, chord)
       table.remove(State.line_cache, State.cursor1.line+1)
     end
     Text.clear_screen_line_cache(State, State.cursor1.line)
-    schedule_save(State)
     record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
+    schedule_save(State)
   --== shortcuts that move the cursor
   elseif chord == 'left' then
     Text.left(State)
@@ -255,12 +254,12 @@ function Text.keychord_press(State, chord)
     State.selection1 = {}
   elseif chord == 'S-left' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.left(State)
   elseif chord == 'S-right' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.right(State)
   -- C- hotkeys reserved for drawings, so we'll use M-
@@ -272,12 +271,12 @@ function Text.keychord_press(State, chord)
     State.selection1 = {}
   elseif chord == 'M-S-left' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.word_left(State)
   elseif chord == 'M-S-right' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.word_right(State)
   elseif chord == 'home' then
@@ -288,12 +287,12 @@ function Text.keychord_press(State, chord)
     State.selection1 = {}
   elseif chord == 'S-home' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.start_of_line(State)
   elseif chord == 'S-end' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.end_of_line(State)
   elseif chord == 'up' then
@@ -304,12 +303,12 @@ function Text.keychord_press(State, chord)
     State.selection1 = {}
   elseif chord == 'S-up' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.up(State)
   elseif chord == 'S-down' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.down(State)
   elseif chord == 'pageup' then
@@ -320,12 +319,12 @@ function Text.keychord_press(State, chord)
     State.selection1 = {}
   elseif chord == 'S-pageup' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.pageup(State)
   elseif chord == 'S-pagedown' then
     if State.selection1.line == nil then
-      State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
+      State.selection1 = deepcopy(State.cursor1)
     end
     Text.pagedown(State)
   end
@@ -342,9 +341,9 @@ end
 
 function Text.pageup(State)
   State.screen_top1 = Text.previous_screen_top1(State)
-  State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
+  State.cursor1 = deepcopy(State.screen_top1)
   Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
-  Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
+  Text.redraw_all(State)  -- if we're scrolling, reclaim all line caches to avoid memory leaks
 end
 
 -- return the top y coordinate of a given line_index,
@@ -380,9 +379,9 @@ end
 
 function Text.pagedown(State)
   State.screen_top1 = Text.screen_bottom1(State)
-  State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
+  State.cursor1 = deepcopy(State.screen_top1)
   Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
-  Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
+  Text.redraw_all(State)  -- if we're scrolling, reclaim all line caches to avoid memory leaks
 end
 
 -- return the location of the start of the bottom-most line on screen
@@ -435,7 +434,7 @@ function Text.up(State)
       line=State.cursor1.line,
       pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
     }
-    Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
+    Text.redraw_all(State)  -- if we're scrolling, reclaim all line caches to avoid memory leaks
   end
 end
 
@@ -484,7 +483,7 @@ end
 function Text.start_of_line(State)
   State.cursor1.pos = 1
   if Text.lt1(State.cursor1, State.screen_top1) then
-    State.screen_top1 = {line=State.cursor1.line, pos=State.cursor1.pos}  -- copy
+    State.screen_top1 = deepcopy(State.cursor1)
   end
 end
 
@@ -564,7 +563,7 @@ function Text.left(State)
       line=State.cursor1.line,
       pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
     }
-    Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
+    Text.redraw_all(State)  -- if we're scrolling, reclaim all line caches to avoid memory leaks
   end
 end
 
@@ -658,7 +657,7 @@ function Text.snap_cursor_to_bottom_of_screen(State)
   State.screen_top1 = Text.to1(State, top2)
 --?   print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
 --?   print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
-  Text.redraw_all(State)  -- if we're scrolling, reclaim all fragments to avoid memory leaks
+  Text.redraw_all(State)  -- if we're scrolling, reclaim all line caches to avoid memory leaks
 end
 
 function Text.in_line(State, line_index, x,y)
@@ -924,7 +923,7 @@ function Text.tweak_screen_top_and_cursor(State)
   -- make sure cursor is on screen
   local screen_bottom1 = Text.screen_bottom1(State)
   if Text.lt1(State.cursor1, State.screen_top1) then
-    State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
+    State.cursor1 = deepcopy(State.screen_top1)
   elseif State.cursor1.line >= screen_bottom1.line then
     if Text.cursor_out_of_screen(State) then
       State.cursor1 = Text.final_text_loc_on_screen(State)
@@ -939,7 +938,7 @@ function Text.cursor_out_of_screen(State)
 end
 
 function Text.redraw_all(State)
---?   print('clearing fragments')
+--?   print('clearing line caches')
   -- Perform some early sanity checking here, in hopes that we correctly call
   -- this whenever we change editor state.
   if State.right <= State.left then