extract a few methods
[?]
May 29, 2022, 5:36 AM
ZPUQSPQPQFVRUIHGLAWW3IDBYODIWDHO62HAC3WWF5TM3CIJGHNQCDependencies
- [2]
R53OF3ONone bug I've repeatedly run into while testing with Moby Dick - [3]
V5MJRFOZbugfix: down arrow doesn't scroll up unnecessarily - [4]
CG3264MMmove - [5]
SVJZZDC3snapshot - no, that's all wrong - [6]
TRNWIQN6more precise height calculation when scrolling up as much as possible while keeping cursor on screen - [7]
MDXGMZU2disable all debug prints - [8]
2RXZ3PGObeginning of a new approach to scroll+wrap - [9]
ESETRNLBbugfix: printing the first part of a line at the bottom made it seem non-wrapping - [10]
DLQMM265scroll past first page - [11]
76AIXR7Hbugfix - [12]
XNFTJHC4split keyboard handling between Text and Drawing - [13]
PYGMASTVdisable some debug prints - [14]
MGT5FTJ3first stab at supporting wrapping in cursor up - [15]
3CSIZJ33clearer comments - [16]
3OKKTUT4up and down arrow now moving by screen line where possible - [17]
KECEMMMRextract couple of functions - [18]
PR4KIAZDfirst stab at equally hacky cursor down support - [19]
KJKKASHZreduce ambitions a bit: page up/down need not start screen from the middle of a line - [20]
537TQ2QNsome more logging - [21]
2POFQQLWkeep cursor on screen when pressing 'down' - [22]
HBZ2UCUFstop recomputing Cursor_x now that we're saving it - [23]
C42QQZSFanother piece of support for line wrapping in cursor up - [24]
5ED3YGHWplanning out cursor up/down over wrapped lines - [*]
BULPIBEGbeginnings of a module for the text editor
Change contents
- edit in text.lua at line 982
-- left/right by one word - replacement in text.lua at line 984
while true doText.left()if Cursor1.pos == 1 then break endassert(Cursor1.pos > 1)local offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)assert(offset > 1)if Lines[Cursor1.line].data:sub(offset-1,offset-1) == ' ' thenbreakendendText.word_left() - replacement in text.lua at line 986
while true doText.right()if Cursor1.pos > utf8.len(Lines[Cursor1.line].data) then break endlocal offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)if Lines[Cursor1.line].data:sub(offset,offset) == ' ' thenbreakendendText.word_right() - replacement in text.lua at line 992[5.3984]→[5.3647:3744](∅→∅),[5.63]→[5.1:140](∅→∅),[5.355]→[5.1:140](∅→∅),[5.3744]→[5.1:140](∅→∅),[5.4030]→[5.1:140](∅→∅),[5.140]→[5.64:126](∅→∅),[5.126]→[5.1:51](∅→∅),[5.198]→[5.1:51](∅→∅),[5.51]→[5.3745:3788](∅→∅),[5.3788]→[5.225:358](∅→∅),[5.225]→[5.225:358](∅→∅),[5.358]→[2.1843:1891](∅→∅),[2.1891]→[5.3789:3830](∅→∅),[5.358]→[5.3789:3830](∅→∅),[5.3830]→[2.1892:1955](∅→∅),[2.1955]→[5.3830:3986](∅→∅),[5.3830]→[5.3830:3986](∅→∅),[5.85]→[5.151:183](∅→∅),[5.3986]→[5.151:183](∅→∅),[5.151]→[5.151:183](∅→∅),[5.183]→[5.52:118](∅→∅),[5.118]→[2.1956:2005](∅→∅),[2.2005]→[5.3987:4075](∅→∅),[5.118]→[5.3987:4075](∅→∅),[5.4075]→[2.2006:2053](∅→∅),[2.2053]→[5.270:359](∅→∅),[5.4075]→[5.270:359](∅→∅),[5.270]→[5.270:359](∅→∅),[5.359]→[5.127:238](∅→∅),[5.238]→[2.2054:2148](∅→∅),[2.2148]→[5.4175:4333](∅→∅),[5.4175]→[5.4175:4333](∅→∅),[5.355]→[5.480:494](∅→∅),[5.576]→[5.480:494](∅→∅),[5.4333]→[5.480:494](∅→∅),[5.480]→[5.480:494](∅→∅),[5.494]→[5.4334:4509](∅→∅),[5.177]→[5.556:594](∅→∅),[5.595]→[5.556:594](∅→∅),[5.4509]→[5.556:594](∅→∅),[5.556]→[5.556:594](∅→∅),[5.594]→[5.4510:4596](∅→∅),[5.676]→[5.4415:4425](∅→∅),[5.4596]→[5.4415:4425](∅→∅),[5.4415]→[5.4415:4425](∅→∅),[5.4425]→[5.677:686](∅→∅),[5.686]→[5.119:168](∅→∅),[5.168]→[5.356:422](∅→∅),[5.422]→[5.860:896](∅→∅),[5.557]→[5.860:896](∅→∅),[5.860]→[5.860:896](∅→∅),[5.896]→[5.4597:4700](∅→∅),[5.4700]→[5.423:571](∅→∅),[5.660]→[5.423:571](∅→∅),[5.571]→[5.4701:4937](∅→∅),[5.671]→[5.1073:1083](∅→∅),[5.4937]→[5.1073:1083](∅→∅),[5.1073]→[5.1073:1083](∅→∅),[5.1083]→[5.4938:5174](∅→∅),[5.732]→[5.268:276](∅→∅),[5.1136]→[5.268:276](∅→∅),[5.1312]→[5.268:276](∅→∅),[5.5174]→[5.268:276](∅→∅),[5.268]→[5.268:276](∅→∅)
assert(Lines[Cursor1.line].mode == 'text')--? print('up', Cursor1.pos, Screen_top1.pos)local screen_line_index,screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line()if screen_line_starting_pos == 1 then--? print('cursor is at first screen line of its line')-- line is done; skip to previous text linelocal new_cursor_line = Cursor1.linewhile new_cursor_line > 1 donew_cursor_line = new_cursor_line-1if Lines[new_cursor_line].mode == 'text' then--? print('found previous text line')Cursor1.line = new_cursor_lineText.populate_screen_line_starting_pos(Cursor1.line)if Lines[Cursor1.line].screen_line_starting_pos == nil thenCursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)breakend-- previous text line found, pick its final screen line--? print('has multiple screen lines')local screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos--? print(#screen_line_starting_pos)screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]--? print('previous screen line starts at pos '..tostring(screen_line_starting_pos)..' of its line')if Screen_top1.line > Cursor1.line thenScreen_top1.line = Cursor1.lineScreen_top1.pos = screen_line_starting_pos--? print('pos of top of screen is also '..tostring(Screen_top1.pos)..' of the same line')endlocal s = string.sub(Lines[Cursor1.line].data, screen_line_starting_pos)Cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1breakendendif Cursor1.line < Screen_top1.line thenScreen_top1.line = Cursor1.lineendelse-- move up one screen line in current line--? print('cursor is NOT at first screen line of its line')assert(screen_line_index > 1)new_screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos[screen_line_index-1]--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))if Screen_top1.line == Cursor1.line and Screen_top1.pos == screen_line_starting_pos thenScreen_top1.pos = new_screen_line_starting_pos--? print('also setting pos of top of screen to '..tostring(Screen_top1.pos))endlocal s = string.sub(Lines[Cursor1.line].data, new_screen_line_starting_pos)Cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1--? print('cursor pos is now '..tostring(Cursor1.pos))endText.up() - replacement in text.lua at line 994[5.4463]→[5.5175:5222](∅→∅),[5.5222]→[5.507:632](∅→∅),[5.420]→[5.267:360](∅→∅),[5.632]→[5.267:360](∅→∅),[5.5222]→[5.267:360](∅→∅),[5.4509]→[5.267:360](∅→∅),[5.360]→[5.633:692](∅→∅),[5.692]→[5.476:497](∅→∅),[5.476]→[5.476:497](∅→∅),[5.497]→[5.5223:5266](∅→∅),[5.798]→[5.5223:5266](∅→∅),[5.5266]→[5.463:601](∅→∅),[5.463]→[5.463:601](∅→∅),[5.601]→[5.5267:5308](∅→∅)
assert(Lines[Cursor1.line].mode == 'text')--? print('down', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)if Text.cursor_at_final_screen_line() then-- line is done, skip to next text line--? print('cursor at final screen line of its line')--? os.exit(1)local new_cursor_line = Cursor1.linewhile new_cursor_line < #Lines donew_cursor_line = new_cursor_line+1if Lines[new_cursor_line].mode == 'text' thenCursor1.line = new_cursor_lineText.down()endendfunction Text.up()assert(Lines[Cursor1.line].mode == 'text')--? print('up', Cursor1.pos, Screen_top1.pos)local screen_line_index,screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line()if screen_line_starting_pos == 1 then--? print('cursor is at first screen line of its line')-- line is done; skip to previous text linelocal new_cursor_line = Cursor1.linewhile new_cursor_line > 1 donew_cursor_line = new_cursor_line-1if Lines[new_cursor_line].mode == 'text' then--? print('found previous text line')Cursor1.line = new_cursor_lineText.populate_screen_line_starting_pos(Cursor1.line)if Lines[Cursor1.line].screen_line_starting_pos == nil then - edit in text.lua at line 1014
--? print(Cursor1.pos) - replacement in text.lua at line 1016[5.779]→[5.779:789](∅→∅),[5.5490]→[5.5490:5539](∅→∅),[5.5539]→[5.727:802](∅→∅),[5.599]→[5.5614:5654](∅→∅),[5.802]→[5.5614:5654](∅→∅),[5.5614]→[5.5614:5654](∅→∅),[5.5654]→[5.803:852](∅→∅),[5.130]→[5.874:922](∅→∅),[5.645]→[5.874:922](∅→∅),[5.852]→[5.874:922](∅→∅),[5.1031]→[5.874:922](∅→∅),[5.874]→[5.874:922](∅→∅),[5.922]→[5.853:927](∅→∅),[5.213]→[5.4899:4909](∅→∅),[5.716]→[5.4899:4909](∅→∅),[5.922]→[5.4899:4909](∅→∅),[5.927]→[5.4899:4909](∅→∅),[5.1118]→[5.4899:4909](∅→∅),[5.5729]→[5.4899:4909](∅→∅),[5.4899]→[5.4899:4909](∅→∅),[5.64]→[5.923:983](∅→∅),[5.1186]→[5.923:983](∅→∅),[5.5800]→[5.923:983](∅→∅),[5.4909]→[5.923:983](∅→∅),[5.983]→[3.2260:2443](∅→∅)
endif Cursor1.line > Screen_bottom1.line then--? print('screen top before:', Screen_top1.line, Screen_top1.pos)Screen_top1.line = Cursor1.line--? print('scroll up preserving cursor')Text.scroll_up_while_cursor_on_screen()--? print('screen top after:', Screen_top1.line, Screen_top1.pos)endelse-- move down one screen line in current linelocal scroll_up = falseif Cursor1.line > Screen_bottom1.line or (Cursor1.line == Screen_bottom1.line and Cursor1.pos >= Screen_bottom1.pos) thenscroll_up = true-- previous text line found, pick its final screen line--? print('has multiple screen lines')local screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos--? print(#screen_line_starting_pos)screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]--? print('previous screen line starts at pos '..tostring(screen_line_starting_pos)..' of its line')if Screen_top1.line > Cursor1.line thenScreen_top1.line = Cursor1.lineScreen_top1.pos = screen_line_starting_pos--? print('pos of top of screen is also '..tostring(Screen_top1.pos)..' of the same line')endlocal s = string.sub(Lines[Cursor1.line].data, screen_line_starting_pos)Cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1break - replacement in text.lua at line 1031[3.2453]→[5.928:994](∅→∅),[5.983]→[5.928:994](∅→∅),[5.779]→[5.1045:1145](∅→∅),[5.994]→[5.1045:1145](∅→∅),[5.1253]→[5.1045:1145](∅→∅),[5.1045]→[5.1045:1145](∅→∅),[5.1145]→[5.5801:5904](∅→∅),[5.5904]→[5.995:1143](∅→∅),[5.924]→[5.5905:6080](∅→∅),[5.1143]→[5.5905:6080](∅→∅),[5.1402]→[5.5905:6080](∅→∅),[5.6080]→[5.1144:1208](∅→∅),[5.1208]→[3.2454:2689](∅→∅)
--? print('cursor is NOT at final screen line of its line')local screen_line_index, screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line()new_screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos[screen_line_index+1]--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))local s = string.sub(Lines[Cursor1.line].data, new_screen_line_starting_pos)Cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1--? print('cursor pos is now', Cursor1.line, Cursor1.pos)if scroll_up thenScreen_top1.line = Cursor1.line--? print('scroll up preserving cursor')Text.scroll_up_while_cursor_on_screen()--? print('screen top after:', Screen_top1.line, Screen_top1.pos)endif Cursor1.line < Screen_top1.line thenScreen_top1.line = Cursor1.lineendelse-- move up one screen line in current line--? print('cursor is NOT at first screen line of its line')assert(screen_line_index > 1)new_screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos[screen_line_index-1]--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))if Screen_top1.line == Cursor1.line and Screen_top1.pos == screen_line_starting_pos thenScreen_top1.pos = new_screen_line_starting_pos--? print('also setting pos of top of screen to '..tostring(Screen_top1.pos))endlocal s = string.sub(Lines[Cursor1.line].data, new_screen_line_starting_pos)Cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1--? print('cursor pos is now '..tostring(Cursor1.pos))endendfunction Text.down()assert(Lines[Cursor1.line].mode == 'text')--? print('down', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)if Text.cursor_at_final_screen_line() then-- line is done, skip to next text line--? print('cursor at final screen line of its line')local new_cursor_line = Cursor1.linewhile new_cursor_line < #Lines donew_cursor_line = new_cursor_line+1if Lines[new_cursor_line].mode == 'text' thenCursor1.line = new_cursor_lineCursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)--? print(Cursor1.pos)break - replacement in text.lua at line 1067
--? print('=>', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)if Cursor1.line > Screen_bottom1.line then--? print('screen top before:', Screen_top1.line, Screen_top1.pos)Screen_top1.line = Cursor1.line--? print('scroll up preserving cursor')Text.scroll_up_while_cursor_on_screen()--? print('screen top after:', Screen_top1.line, Screen_top1.pos)endelse-- move down one screen line in current linelocal scroll_up = falseif Cursor1.line > Screen_bottom1.line or (Cursor1.line == Screen_bottom1.line and Cursor1.pos >= Screen_bottom1.pos) thenscroll_up = trueend--? print('cursor is NOT at final screen line of its line')local screen_line_index, screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line()new_screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos[screen_line_index+1]--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))local s = string.sub(Lines[Cursor1.line].data, new_screen_line_starting_pos)Cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1--? print('cursor pos is now', Cursor1.line, Cursor1.pos)if scroll_up thenScreen_top1.line = Cursor1.line--? print('scroll up preserving cursor')Text.scroll_up_while_cursor_on_screen()--? print('screen top after:', Screen_top1.line, Screen_top1.pos)endend--? print('=>', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)endfunction Text.word_left()while true doText.left()if Cursor1.pos == 1 then break endassert(Cursor1.pos > 1)local offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)assert(offset > 1)if Lines[Cursor1.line].data:sub(offset-1,offset-1) == ' ' thenbreakendendendfunction Text.word_right()while true doText.right()if Cursor1.pos > utf8.len(Lines[Cursor1.line].data) then break endlocal offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)if Lines[Cursor1.line].data:sub(offset,offset) == ' ' thenbreakend