about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-06-21 09:53:38 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-06-21 09:56:04 -0700
commit8e68c699f50121db306ff552d16ea3e10bd77b19 (patch)
treec580d8146a91eeaa5d7eefe1b03922e329afe5b5
parent9c8285bf08656bc1b74c6798b326be7145965170 (diff)
downloadtext.love-8e68c699f50121db306ff552d16ea3e10bd77b19.tar.gz
bugfix: delete selection before pasting
-rw-r--r--main.lua3
-rw-r--r--text.lua3
-rw-r--r--text_tests.lua38
3 files changed, 41 insertions, 3 deletions
diff --git a/main.lua b/main.lua
index 712d44c..fcc65ee 100644
--- a/main.lua
+++ b/main.lua
@@ -410,6 +410,9 @@ function App.textinput(t)
 end
 
 function App.keychord_pressed(chord)
+  if Selection1.line and not App.shift_down() and chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and backspace ~= 'delete' then
+    Text.delete_selection()
+  end
   if Search_term then
     if chord == 'escape' then
       Search_term = nil
diff --git a/text.lua b/text.lua
index 84f3000..e80cd03 100644
--- a/text.lua
+++ b/text.lua
@@ -142,9 +142,6 @@ function Text.textinput(t)
   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)
diff --git a/text_tests.lua b/text_tests.lua
index 4221aa6..0119223 100644
--- a/text_tests.lua
+++ b/text_tests.lua
@@ -261,6 +261,44 @@ function test_copy_does_not_reset_selection()
   check(Selection1.line, 'F - test_copy_does_not_reset_selection')
 end
 
+function test_cut()
+  io.write('\ntest_cut')
+  -- display a line of text with some part selected
+  App.screen.init{width=80, height=80}
+  Lines = load_array{'abc'}
+  Line_width = 75
+  Cursor1 = {line=1, pos=1}
+  Selection1 = {line=1, pos=2}
+  Screen_top1 = {line=1, pos=1}
+  Screen_bottom1 = {}
+  App.draw()
+  -- press a key
+  App.run_after_keychord('C-x')
+  check_eq(App.clipboard, 'a', 'F - test_cut/clipboard')
+  -- selected text is deleted
+  check_eq(Lines[1].data, 'bc', 'F - test_cut/data')
+end
+
+function test_paste_replaces_selection()
+  io.write('\ntest_paste_replaces_selection')
+  -- display a line of text with a selection
+  App.screen.init{width=80, height=80}
+  Lines = load_array{'abc', 'def'}
+  Line_width = 75
+  Cursor1 = {line=2, pos=1}
+  Selection1 = {line=1, pos=1}
+  Screen_top1 = {line=1, pos=1}
+  Screen_bottom1 = {}
+  App.draw()
+  -- set clipboard
+  App.clipboard = 'xyz'
+  -- paste selection
+  App.run_after_keychord('C-v')
+  -- selection is reset since shift key is not pressed
+  -- selection includes the newline, so it's also deleted
+  check_eq(Lines[1].data, 'xyzdef', 'F - test_paste_replaces_selection')
+end
+
 function test_edit_wrapping_text()
   io.write('\ntest_edit_wrapping_text')
   App.screen.init{width=50, height=60}