about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-06-20 11:58:26 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-06-20 11:58:26 -0700
commitc1b6bac187cf8597da3cc0d3dc1caa269d3c13c5 (patch)
tree1710f1a892a567b596de5f97f03742416a577ae1
parent861c57b5337150034c8f44af1b5224950276b83b (diff)
downloadview.love-c1b6bac187cf8597da3cc0d3dc1caa269d3c13c5.tar.gz
much simpler
We just need to ensure textinput events never make use of selection
state.

All tests are passing, but I'm aware of a couple of issues. But now we
can keep all the special cases in one place.
-rw-r--r--main.lua16
-rw-r--r--text.lua23
2 files changed, 8 insertions, 31 deletions
diff --git a/main.lua b/main.lua
index d4fd32a..270e0b7 100644
--- a/main.lua
+++ b/main.lua
@@ -391,6 +391,7 @@ function App.mousereleased(x,y, button)
   end
 end
 
+-- don't depend on state of Selection1; use keychord_pressed for that
 function App.textinput(t)
   for _,line in ipairs(Lines) do line.y = nil end  -- just in case we scroll
   if Search_term then
@@ -407,9 +408,6 @@ function App.textinput(t)
     Text.textinput(t)
   end
   schedule_save()
-  if not App.shift_down() then
-    Selection1 = {}
-  end
 end
 
 function App.keychord_pressed(chord)
@@ -441,19 +439,15 @@ function App.keychord_pressed(chord)
     Search_term = ''
     Search_backup = {cursor={line=Cursor1.line, pos=Cursor1.pos}, screen_top={line=Screen_top1.line, pos=Screen_top1.pos}}
     assert(Search_text == nil)
-    Selection1 = {}
   elseif chord == 'C-=' then
     initialize_font_settings(Font_height+2)
     Text.redraw_all()
-    Selection1 = {}
   elseif chord == 'C--' then
     initialize_font_settings(Font_height-2)
     Text.redraw_all()
-    Selection1 = {}
   elseif chord == 'C-0' then
     initialize_font_settings(20)
     Text.redraw_all()
-    Selection1 = {}
   elseif chord == 'C-z' then
     for _,line in ipairs(Lines) do line.y = nil end  -- just in case we scroll
     local event = undo_event()
@@ -466,7 +460,6 @@ function App.keychord_pressed(chord)
       Text.redraw_all()  -- if we're scrolling, reclaim all fragments to avoid memory leaks
       schedule_save()
     end
-    Selection1 = {}
   elseif chord == 'C-y' then
     for _,line in ipairs(Lines) do line.y = nil end  -- just in case we scroll
     local event = redo_event()
@@ -479,7 +472,6 @@ function App.keychord_pressed(chord)
       Text.redraw_all()  -- if we're scrolling, reclaim all fragments to avoid memory leaks
       schedule_save()
     end
-    Selection1 = {}
   -- clipboard
   elseif chord == 'C-c' then
     for _,line in ipairs(Lines) do line.y = nil end  -- just in case we scroll
@@ -487,7 +479,6 @@ function App.keychord_pressed(chord)
     if s then
       App.setClipboardText(s)
     end
-    Selection1 = {}
   elseif chord == 'C-x' then
     for _,line in ipairs(Lines) do line.y = nil end  -- just in case we scroll
     local s = Text.cut_selection()
@@ -495,7 +486,6 @@ function App.keychord_pressed(chord)
       App.setClipboardText(s)
     end
     schedule_save()
-    Selection1 = {}
   elseif chord == 'C-v' then
     for _,line in ipairs(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
@@ -517,7 +507,6 @@ function App.keychord_pressed(chord)
     end
     schedule_save()
     record_undo_event({before=before, after=snapshot(before_line, Cursor1.line)})
-    Selection1 = {}
   -- dispatch to drawing or text
   elseif App.mouse_down(1) or chord:sub(1,2) == 'C-' then
     -- DON'T reset line.y here
@@ -562,6 +551,9 @@ function App.keychord_pressed(chord)
     for _,line in ipairs(Lines) do line.y = nil end  -- just in case we scroll
     Text.keychord_pressed(chord)
   end
+  if not App.shift_down() then
+    Selection1 = {}
+  end
 end
 
 function App.keyreleased(key, scancode)
diff --git a/text.lua b/text.lua
index be7ecf7..a3dd4c1 100644
--- a/text.lua
+++ b/text.lua
@@ -137,14 +137,12 @@ function Text.compute_fragments(line, line_width)
   end
 end
 
+-- don't depend on state of Selection1; use keychord_pressed for that
 function Text.textinput(t)
   if App.mouse_down(1) then return end
   assert(not App.ctrl_down())
   if App.alt_down() then return end
   assert(not App.cmd_down())
-  if Selection1.line then
-    Text.delete_selection()
-  end
   local before = snapshot(Cursor1.line)
 --?   print(Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
   Text.insert_at_cursor(t)
@@ -177,7 +175,6 @@ function Text.keychord_pressed(chord)
     end
     schedule_save()
     record_undo_event({before=before, after=snapshot(before_line, Cursor1.line)})
-    Selection1 = {}
   elseif chord == 'tab' then
     local before = snapshot(Cursor1.line)
 --?     print(Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
@@ -189,12 +186,10 @@ function Text.keychord_pressed(chord)
     end
     schedule_save()
     record_undo_event({before=before, after=snapshot(Cursor1.line)})
-    Selection1 = {}
   elseif chord == 'backspace' then
     if Selection1.line then
       Text.delete_selection()
       schedule_save()
-      Selection1 = {}
       return
     end
     local before
@@ -233,12 +228,10 @@ function Text.keychord_pressed(chord)
     assert(Text.le1(Screen_top1, Cursor1))
     schedule_save()
     record_undo_event({before=before, after=snapshot(Cursor1.line)})
-    Selection1 = {}
   elseif chord == 'delete' then
     if Selection1.line then
       Text.delete_selection()
       schedule_save()
-      Selection1 = {}
       return
     end
     local before
@@ -271,14 +264,11 @@ function Text.keychord_pressed(chord)
     end
     schedule_save()
     record_undo_event({before=before, after=snapshot(Cursor1.line)})
-    Selection1 = {}
   --== shortcuts that move the cursor
   elseif chord == 'left' then
     Text.left()
-    Selection1 = {}
   elseif chord == 'right' then
     Text.right()
-    Selection1 = {}
   elseif chord == 'S-left' then
     if Selection1.line == nil then
       Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
@@ -292,10 +282,8 @@ function Text.keychord_pressed(chord)
   -- C- hotkeys reserved for drawings, so we'll use M-
   elseif chord == 'M-left' then
     Text.word_left()
-    Selection1 = {}
   elseif chord == 'M-right' then
     Text.word_right()
-    Selection1 = {}
   elseif chord == 'M-S-left' then
     if Selection1.line == nil then
       Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
@@ -308,10 +296,8 @@ function Text.keychord_pressed(chord)
     Text.word_right()
   elseif chord == 'home' then
     Cursor1.pos = 1
-    Selection1 = {}
   elseif chord == 'end' then
     Cursor1.pos = utf8.len(Lines[Cursor1.line].data) + 1
-    Selection1 = {}
   elseif chord == 'S-home' then
     if Selection1.line == nil then
       Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
@@ -324,10 +310,8 @@ function Text.keychord_pressed(chord)
     Cursor1.pos = utf8.len(Lines[Cursor1.line].data) + 1
   elseif chord == 'up' then
     Text.up()
-    Selection1 = {}
   elseif chord == 'down' then
     Text.down()
-    Selection1 = {}
   elseif chord == 'S-up' then
     if Selection1.line == nil then
       Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
@@ -340,10 +324,8 @@ function Text.keychord_pressed(chord)
     Text.down()
   elseif chord == 'pageup' then
     Text.pageup()
-    Selection1 = {}
   elseif chord == 'pagedown' then
     Text.pagedown()
-    Selection1 = {}
   elseif chord == 'S-pageup' then
     if Selection1.line == nil then
       Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
@@ -355,6 +337,9 @@ function Text.keychord_pressed(chord)
     end
     Text.pagedown()
   end
+  if Selection1.line and not App.shift_down() then
+    Text.delete_selection()
+  end
 end
 
 function Text.insert_return()