From f2e5c4ffee3352268750861bd995386e1a47ecd5 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 5 Jul 2022 11:24:08 -0700 Subject: [PATCH] bugfix: avoid scrolling on 'end' There's multiple ways to do this, only one of them is right, and I keep forgetting what it is. Turn it into a method. --- main.lua | 3 +-- text.lua | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/main.lua b/main.lua index 0446825..7321256 100644 --- a/main.lua +++ b/main.lua @@ -517,8 +517,7 @@ function App.keychord_pressed(chord, key) Text.insert_at_cursor(c) end end - App.draw() - if Cursor_y >= App.screen.height - Line_height then + if Text.cursor_past_screen_bottom() then Text.snap_cursor_to_bottom_of_screen() end schedule_save() diff --git a/text.lua b/text.lua index 7307056..4072311 100644 --- a/text.lua +++ b/text.lua @@ -509,7 +509,7 @@ function Text.end_of_line() Cursor1.pos = utf8.len(Lines[Cursor1.line].data) + 1 local _,botpos = Text.pos_at_start_of_cursor_screen_line() local botline1 = {line=Cursor1.line, pos=botpos} - if Text.lt1(Screen_bottom1, botline1) then + if Text.cursor_past_screen_bottom() then Text.snap_cursor_to_bottom_of_screen() end end @@ -575,9 +575,7 @@ function Text.right() end end end - local _,botpos = Text.pos_at_start_of_cursor_screen_line() - local botline1 = {line=Cursor1.line, pos=botpos} - if Text.lt1(Screen_bottom1, botline1) then + if Text.cursor_past_screen_bottom() then Text.snap_cursor_to_bottom_of_screen() end end @@ -941,8 +939,7 @@ function Text.tweak_screen_top_and_cursor() Cursor1 = {line=Screen_top1.line, pos=Screen_top1.pos} elseif Cursor1.line >= Screen_bottom1.line then --? print('too low') - App.draw() - if Text.lt1(Screen_bottom1, Cursor1) then + if Text.cursor_past_screen_bottom() then --? print('tweak') local line = Lines[Screen_bottom1.line] Cursor1 = {line=Screen_bottom1.line, pos=Text.to_pos_on_line(line, App.screen.width-5, App.screen.height-5)} @@ -950,6 +947,17 @@ function Text.tweak_screen_top_and_cursor() end end +-- slightly expensive since it redraws the screen +function Text.cursor_past_screen_bottom() + App.draw() + return Cursor_y >= App.screen.height - Line_height + -- this approach is cheaper and almost works, except on the final screen + -- where file ends above bottom of screen +--? local _,botpos = Text.pos_at_start_of_cursor_screen_line() +--? local botline1 = {line=Cursor1.line, pos=botpos} +--? return Text.lt1(Screen_bottom1, botline1) +end + function Text.redraw_all() --? print('clearing fragments') for _,line in ipairs(Lines) do