about summary refs log tree commit diff stats
path: root/edit.lua
diff options
context:
space:
mode:
Diffstat (limited to 'edit.lua')
-rw-r--r--edit.lua24
1 files changed, 9 insertions, 15 deletions
diff --git a/edit.lua b/edit.lua
index 79a2fc3..6a7d708 100644
--- a/edit.lua
+++ b/edit.lua
@@ -19,7 +19,6 @@ function edit.initialize_state(top, left, right, font, font_height, line_height)
 
     -- 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 the line guaranteed to not straddle screen lines
     --   screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
     line_cache = {},
@@ -33,9 +32,10 @@ function edit.initialize_state(top, left, right, font, font_height, line_height)
     --
     -- Make sure these coordinates are never aliased, so that changing one causes
     -- action at a distance.
+    --
+    -- On lines that are drawings, pos will be nil.
     screen_top1 = {line=1, pos=1},  -- position of start of screen line at top of screen
-    cursor1 = {line=1, pos=1},  -- position of cursor
-    screen_bottom1 = {line=1, pos=1},  -- position of start of screen line at bottom of screen
+    cursor1 = {line=1, pos=1},  -- position of cursor; must be on a text line
 
     selection1 = {},
     -- some extra state to compute selection between mouse press and release
@@ -102,22 +102,19 @@ function edit.draw(State)
   State.cursor_x = nil
   State.cursor_y = nil
   local y = State.top
-  local screen_bottom1 = {line=nil, pos=nil}
 --?   print('== draw')
   for line_index = State.screen_top1.line,#State.lines do
     local line = State.lines[line_index]
 --?     print('draw:', y, line_index, line)
     if y + State.line_height > App.screen.height then break end
-    screen_bottom1.line = line_index
 --?     print('text.draw', y, line_index)
     local startpos = 1
     if line_index == State.screen_top1.line then
       startpos = State.screen_top1.pos
     end
-    y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
+    y = Text.draw(State, line_index, y, startpos)
 --?     print('=> y', y)
   end
-  State.screen_bottom1 = screen_bottom1
   if State.search_term then
     Text.draw_search_bar(State)
   end
@@ -173,10 +170,7 @@ function edit.mouse_press(State, x,y, mouse_button)
   State.old_cursor1 = State.cursor1
   State.old_selection1 = State.selection1
   State.mousepress_shift = App.shift_down()
-  State.selection1 = {
-      line=State.screen_bottom1.line,
-      pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
-  }
+  State.selection1 = Text.final_text_loc_on_screen(State)
 end
 
 function edit.mouse_release(State, x,y, mouse_button)
@@ -202,7 +196,7 @@ function edit.mouse_release(State, x,y, mouse_button)
   end
 
   -- still here? mouse release is below all screen lines
-  State.cursor1.line, State.cursor1.pos = State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1)
+  State.cursor1 = Text.final_text_loc_on_screen(State)
   edit.clean_up_mouse_press(State)
 --?   print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos))
 end
@@ -228,7 +222,8 @@ function edit.mouse_wheel_move(State, dx,dy)
       Text.up(State)
     end
   elseif dy < 0 then
-    State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
+    State.cursor1 = Text.screen_bottom1(State)
+    edit.put_cursor_on_next_text_line(State)
     for i=1,math.floor(-dy) do
       Text.down(State)
     end
@@ -252,7 +247,6 @@ function edit.keychord_press(State, chord, key)
     Text.delete_selection(State, State.left, State.right)
   end
   if State.search_term then
-    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
     if chord == 'escape' then
       State.search_term = nil
       State.cursor1 = State.search_backup.cursor
@@ -302,7 +296,6 @@ function edit.keychord_press(State, chord, key)
     end
   -- dispatch to text
   else
-    for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end  -- just in case we scroll
     Text.keychord_press(State, chord)
   end
 end
@@ -357,6 +350,7 @@ end
 function edit.run_after_mouse_click(State, x,y, mouse_button)
   App.fake_mouse_press(x,y, mouse_button)
   edit.mouse_press(State, x,y, mouse_button)
+  edit.draw(State)
   App.fake_mouse_release(x,y, mouse_button)
   edit.mouse_release(State, x,y, mouse_button)
   App.screen.contents = {}