about summary refs log tree commit diff stats
path: root/main.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-02 08:28:33 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-02 08:28:33 -0700
commit02307268133e602de0a3c0907e62ee7ef0cc4461 (patch)
treefde99472ec91a42caf7eb47b89f06dae8660eada /main.lua
parent4aa2003c94e8cdadde3d353a8cbc631c3a92833d (diff)
downloadlines.love-02307268133e602de0a3c0907e62ee7ef0cc4461.tar.gz
backspace
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua10
1 files changed, 10 insertions, 0 deletions
diff --git a/main.lua b/main.lua
index e9bccf5..df75189 100644
--- a/main.lua
+++ b/main.lua
@@ -1,4 +1,5 @@
 require 'keychord'
+local utf8 = require 'utf8'
 
 lines = {}
 width, height, flags = 0, 0, nil
@@ -35,6 +36,15 @@ function keychord_pressed(chord)
   -- Don't handle any keys here that would trigger love.textinput above.
   if chord == 'return' then
     table.insert(lines, '')
+  elseif chord == 'backspace' then
+    if #lines > 1 and lines[#lines] == '' then
+      table.remove(lines)
+    else
+      local byteoffset = utf8.offset(lines[#lines], -1)
+      if byteoffset then
+        lines[#lines] = string.sub(lines[#lines], 1, byteoffset-1)
+      end
+    end
   elseif chord == 'C-r' then
     lines[#lines+1] = eval(lines[#lines])[1]
     lines[#lines+1] = ''