extract a few methods

[?]
May 29, 2022, 5:36 AM
ZPUQSPQPQFVRUIHGLAWW3IDBYODIWDHO62HAC3WWF5TM3CIJGHNQC

Dependencies

  • [2] R53OF3ON one bug I've repeatedly run into while testing with Moby Dick
  • [3] V5MJRFOZ bugfix: down arrow doesn't scroll up unnecessarily
  • [4] CG3264MM move
  • [5] SVJZZDC3 snapshot - no, that's all wrong
  • [6] TRNWIQN6 more precise height calculation when scrolling up as much as possible while keeping cursor on screen
  • [7] MDXGMZU2 disable all debug prints
  • [8] 2RXZ3PGO beginning of a new approach to scroll+wrap
  • [9] ESETRNLB bugfix: printing the first part of a line at the bottom made it seem non-wrapping
  • [10] DLQMM265 scroll past first page
  • [11] 76AIXR7H bugfix
  • [12] XNFTJHC4 split keyboard handling between Text and Drawing
  • [13] PYGMASTV disable some debug prints
  • [14] MGT5FTJ3 first stab at supporting wrapping in cursor up
  • [15] 3CSIZJ33 clearer comments
  • [16] 3OKKTUT4 up and down arrow now moving by screen line where possible
  • [17] KECEMMMR extract couple of functions
  • [18] PR4KIAZD first stab at equally hacky cursor down support
  • [19] KJKKASHZ reduce ambitions a bit: page up/down need not start screen from the middle of a line
  • [20] 537TQ2QN some more logging
  • [21] 2POFQQLW keep cursor on screen when pressing 'down'
  • [22] HBZ2UCUF stop recomputing Cursor_x now that we're saving it
  • [23] C42QQZSF another piece of support for line wrapping in cursor up
  • [24] 5ED3YGHW planning out cursor up/down over wrapped lines
  • [*] BULPIBEG beginnings of a module for the text editor

Change contents

  • edit in text.lua at line 982
    [4.341][4.341:369]()
    -- left/right by one word
  • replacement in text.lua at line 984
    [4.456][4.456:761]()
    while true do
    Text.left()
    if Cursor1.pos == 1 then break end
    assert(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) == ' ' then
    break
    end
    end
    [4.456]
    [4.761]
    Text.word_left()
  • replacement in text.lua at line 986
    [4.794][4.794:1073]()
    while true do
    Text.right()
    if Cursor1.pos > utf8.len(Lines[Cursor1.line].data) then break end
    local offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)
    if Lines[Cursor1.line].data:sub(offset,offset) == ' ' then
    break
    end
    end
    [4.794]
    [4.1073]
    Text.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 line
    local new_cursor_line = Cursor1.line
    while new_cursor_line > 1 do
    new_cursor_line = new_cursor_line-1
    if Lines[new_cursor_line].mode == 'text' then
    --? print('found previous text line')
    Cursor1.line = new_cursor_line
    Text.populate_screen_line_starting_pos(Cursor1.line)
    if Lines[Cursor1.line].screen_line_starting_pos == nil then
    Cursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)
    break
    end
    -- 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 then
    Screen_top1.line = Cursor1.line
    Screen_top1.pos = screen_line_starting_pos
    --? print('pos of top of screen is also '..tostring(Screen_top1.pos)..' of the same line')
    end
    local s = string.sub(Lines[Cursor1.line].data, screen_line_starting_pos)
    Cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1
    break
    end
    end
    if Cursor1.line < Screen_top1.line then
    Screen_top1.line = Cursor1.line
    end
    else
    -- 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 then
    Screen_top1.pos = new_screen_line_starting_pos
    --? print('also setting pos of top of screen to '..tostring(Screen_top1.pos))
    end
    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 '..tostring(Cursor1.pos))
    end
    [5.3984]
    [5.4433]
    Text.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.line
    while new_cursor_line < #Lines do
    new_cursor_line = new_cursor_line+1
    if Lines[new_cursor_line].mode == 'text' then
    Cursor1.line = new_cursor_line
    [5.4463]
    [5.5308]
    Text.down()
    end
    end
    function 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 line
    local new_cursor_line = Cursor1.line
    while new_cursor_line > 1 do
    new_cursor_line = new_cursor_line-1
    if Lines[new_cursor_line].mode == 'text' then
    --? print('found previous text line')
    Cursor1.line = new_cursor_line
    Text.populate_screen_line_starting_pos(Cursor1.line)
    if Lines[Cursor1.line].screen_line_starting_pos == nil then
  • edit in text.lua at line 1014
    [5.5392][5.693:726]()
    --? 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]()
    end
    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)
    end
    else
    -- move down one screen line in current line
    local scroll_up = false
    if Cursor1.line > Screen_bottom1.line or (Cursor1.line == Screen_bottom1.line and Cursor1.pos >= Screen_bottom1.pos) then
    scroll_up = true
    [5.779]
    [3.2443]
    -- 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 then
    Screen_top1.line = Cursor1.line
    Screen_top1.pos = screen_line_starting_pos
    --? print('pos of top of screen is also '..tostring(Screen_top1.pos)..' of the same line')
    end
    local s = string.sub(Lines[Cursor1.line].data, screen_line_starting_pos)
    Cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1
    break
  • 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 then
    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)
    [3.2453]
    [3.2689]
    end
    if Cursor1.line < Screen_top1.line then
    Screen_top1.line = Cursor1.line
    end
    else
    -- 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 then
    Screen_top1.pos = new_screen_line_starting_pos
    --? print('also setting pos of top of screen to '..tostring(Screen_top1.pos))
    end
    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 '..tostring(Cursor1.pos))
    end
    end
    function 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.line
    while new_cursor_line < #Lines do
    new_cursor_line = new_cursor_line+1
    if Lines[new_cursor_line].mode == 'text' then
    Cursor1.line = new_cursor_line
    Cursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)
    --? print(Cursor1.pos)
    break
  • replacement in text.lua at line 1067
    [5.1321][5.1330:1453]()
    --? print('=>', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)
    [5.1321]
    [5.36]
    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)
    end
    else
    -- move down one screen line in current line
    local scroll_up = false
    if Cursor1.line > Screen_bottom1.line or (Cursor1.line == Screen_bottom1.line and Cursor1.pos >= Screen_bottom1.pos) then
    scroll_up = true
    end
    --? 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 then
    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)
    end
    end
    --? print('=>', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)
    end
    function Text.word_left()
    while true do
    Text.left()
    if Cursor1.pos == 1 then break end
    assert(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) == ' ' then
    break
    end
    end
    end
    function Text.word_right()
    while true do
    Text.right()
    if Cursor1.pos > utf8.len(Lines[Cursor1.line].data) then break end
    local offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)
    if Lines[Cursor1.line].data:sub(offset,offset) == ' ' then
    break
    end