about summary refs log tree commit diff stats
path: root/edit.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-06-04 15:16:35 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-06-04 15:17:36 -0700
commitf881f19f983e30866c4c97dc9716ffd6b3e9196d (patch)
tree4829900ee043e236d17aaf9e5064c7f66ae9a7b2 /edit.lua
parente3eecdd506c208bf61b04c6ea5bbcdd4894b08e5 (diff)
parentbf2c2555d662865b94256e6e854acc0715fda6d1 (diff)
downloadview.love-f881f19f983e30866c4c97dc9716ffd6b3e9196d.tar.gz
Merge text.love
Diffstat (limited to 'edit.lua')
-rw-r--r--edit.lua44
1 files changed, 32 insertions, 12 deletions
diff --git a/edit.lua b/edit.lua
index 1ab8bad..9e7c229 100644
--- a/edit.lua
+++ b/edit.lua
@@ -43,8 +43,6 @@ function edit.initialize_state(top, left, right, font_height, line_height)  -- c
     old_cursor1 = nil,
     old_selection1 = nil,
     mousepress_shift = nil,
-    -- when selecting text, avoid recomputing some state on every single frame
-    recent_mouse = {},
 
     -- cursor coordinates in pixels
     cursor_x = 0,
@@ -98,20 +96,22 @@ 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
-    State.screen_bottom1 = {line=line_index, pos=nil}
+    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, State.screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
+    y, screen_bottom1.pos = 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
@@ -125,7 +125,18 @@ end
 
 function edit.mouse_press(State, x,y, mouse_button)
   if State.search_term then return end
---?   print('press', State.cursor1.line)
+--?   print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
+  if y < State.top then
+    State.old_cursor1 = State.cursor1
+    State.old_selection1 = State.selection1
+    State.mousepress_shift = App.shift_down()
+    State.selection1 = {
+        line=State.screen_top1.line,
+        pos=State.screen_top1.pos,
+    }
+    return
+  end
+
   for line_index,line in ipairs(State.lines) do
     if Text.in_line(State, line_index, x,y) then
       -- delicate dance between cursor, selection and old cursor/selection
@@ -136,7 +147,8 @@ function edit.mouse_press(State, x,y, mouse_button)
       --    sets cursor
       --  press and hold to start a selection: sets selection on press, cursor on release
       --  press and hold, then press shift: ignore shift
-      --    i.e. mouse_released should never look at shift state
+      --    i.e. mouse_release should never look at shift state
+--?       print_and_log(('edit.mouse_press: in line %d'):format(line_index))
       State.old_cursor1 = State.cursor1
       State.old_selection1 = State.selection1
       State.mousepress_shift = App.shift_down()
@@ -144,23 +156,31 @@ function edit.mouse_press(State, x,y, mouse_button)
           line=line_index,
           pos=Text.to_pos_on_line(State, line_index, x, y),
       }
---?       print('selection', State.selection1.line, State.selection1.pos)
-      break
+      return
     end
   end
+
+  -- still here? click is below all screen lines
+  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),
+  }
 end
 
 function edit.mouse_release(State, x,y, mouse_button)
   if State.search_term then return end
---?   print('release', State.cursor1.line)
+--?   print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
   for line_index,line in ipairs(State.lines) do
     if Text.in_line(State, line_index, x,y) then
---?       print('reset selection')
+--?       print_and_log(('edit.mouse_release: in line %d'):format(line_index))
       State.cursor1 = {
           line=line_index,
           pos=Text.to_pos_on_line(State, line_index, x, y),
       }
---?       print('cursor', State.cursor1.line, State.cursor1.pos)
+--?       print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
       if State.mousepress_shift then
         if State.old_selection1.line == nil then
           State.selection1 = State.old_cursor1
@@ -175,7 +195,7 @@ function edit.mouse_release(State, x,y, mouse_button)
       break
     end
   end
---?   print('selection:', State.selection1.line, State.selection1.pos)
+--?   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
 
 function edit.mouse_wheel_move(State, dx,dy)