about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-06-10 11:29:01 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-06-10 11:29:01 -0700
commitfcacb6e63d1aefd7aedc858e6efa39d8102eb6ba (patch)
tree804074d13f166aca1fa6b4163074e50613a008c7
parent31418976d4da1c72ddb17f24dde22db154962554 (diff)
downloadtext.love-fcacb6e63d1aefd7aedc858e6efa39d8102eb6ba.tar.gz
extract scrolling logic out of insert_at_cursor
-rw-r--r--main.lua6
-rw-r--r--text.lua18
2 files changed, 18 insertions, 6 deletions
diff --git a/main.lua b/main.lua
index 8b20ca4..7be15cf 100644
--- a/main.lua
+++ b/main.lua
@@ -425,7 +425,13 @@ function App.keychord_pressed(chord)
         end
         num_newlines = num_newlines+1
       else
+--?         print(Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
         Text.insert_at_cursor(c)
+        if Cursor_y >= App.screen.height - Line_height then
+          Text.populate_screen_line_starting_pos(Cursor1.line)
+          Text.snap_cursor_to_bottom_of_screen()
+--?           print('=>', Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
+        end
       end
     end
     -- hack 1: if we have too many newlines we definitely need to scroll
diff --git a/text.lua b/text.lua
index e7fe598..464e26c 100644
--- a/text.lua
+++ b/text.lua
@@ -144,12 +144,17 @@ function Text.textinput(t)
     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)
+  if Cursor_y >= App.screen.height - Line_height then
+    Text.populate_screen_line_starting_pos(Cursor1.line)
+    Text.snap_cursor_to_bottom_of_screen()
+--?     print('=>', Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
+  end
   record_undo_event({before=before, after=snapshot(Cursor1.line)})
 end
 
 function Text.insert_at_cursor(t)
---?   print(Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
   local byte_offset
   if Cursor1.pos > 1 then
     byte_offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)
@@ -164,11 +169,6 @@ function Text.insert_at_cursor(t)
   Lines[Cursor1.line].fragments = nil
   Lines[Cursor1.line].screen_line_starting_pos = nil
   Cursor1.pos = Cursor1.pos+1
-  if Cursor_y >= App.screen.height - Line_height then
-    Text.populate_screen_line_starting_pos(Cursor1.line)
-    Text.snap_cursor_to_bottom_of_screen()
---?     print('=>', Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
-  end
 end
 
 -- Don't handle any keys here that would trigger love.textinput above.
@@ -186,7 +186,13 @@ function Text.keychord_pressed(chord)
     record_undo_event({before=before, after=snapshot(before_line, Cursor1.line)})
   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)
     Text.insert_at_cursor('\t')
+    if Cursor_y >= App.screen.height - Line_height then
+      Text.populate_screen_line_starting_pos(Cursor1.line)
+      Text.snap_cursor_to_bottom_of_screen()
+--?       print('=>', Screen_top1.line, Screen_top1.pos, Cursor1.line, Cursor1.pos, Screen_bottom1.line, Screen_bottom1.pos)
+    end
     save_to_disk(Lines, Filename)
     record_undo_event({before=before, after=snapshot(Cursor1.line)})
   elseif chord == 'backspace' then