selecting text and deleting selections

[?]
May 29, 2022, 3:12 PM
DHI6IJCNSTHGED67T6H5X6Y636C7PIDGIJD32HBEKLT5WIMRS5MAC

Dependencies

  • [2] 2ZRC7FUL .
  • [3] KOYAJWE4 extract a couple more methods
  • [4] AYE2VEGJ extract a couple of methods
  • [5] H2DPLWMV snapshot: wrapping long lines at word boundaries
  • [6] XNFTJHC4 split keyboard handling between Text and Drawing
  • [7] 5DOC2CBM extract a function
  • [8] 3TFEAQSW start using some globals
  • [9] KECEMMMR extract couple of functions
  • [10] 2RXZ3PGO beginning of a new approach to scroll+wrap
  • [11] ZPUQSPQP extract a few methods
  • [12] WLHI7KD3 new globals: draw partial screen line up top
  • [*] BULPIBEG beginnings of a module for the text editor
  • [*] QYIFOHW3 first test!
  • [*] OYXDYPGS get rid of debug variables
  • [*] HMODUNJE scroll on backspace
  • [*] WY3JD6W6 bugfix
  • [*] PFT5Y2ZY move
  • [*] CG3264MM move
  • [*] AMSESRTH move some code
  • [*] AVTNUQYR basic test-enabled framework
  • [*] OTIBCAUJ love2d scaffold
  • [*] OIB2QPRC start remembering where the cursor is drawn in px
  • [*] HYEAFRZ2 split mouse_pressed events between Text and Drawing
  • [*] BOFNXP5G clicking now moves the cursor even on long, wrapped lines

Change contents

  • edit in text.lua at line 28
    [15.62]
    [2.1]
    local frag_len = utf8.len(frag)
  • edit in text.lua at line 48
    [4.128]
    [16.20]
    if Selection1.line then
    local lo, hi = Text.clip_selection(line_index, pos, pos+frag_len)
    if lo then
    local lo_offset = utf8.offset(line.data, lo)
    local hi_offset = utf8.offset(line.data, hi)
    local pos_offset = utf8.offset(line.data, pos)
    local lo_px
    if pos == lo then
    lo_px = 0
    else
    local before = line.data:sub(pos_offset, lo_offset-1)
    local before_text = App.newText(love.graphics.getFont(), before)
    lo_px = App.width(before_text)*Zoom
    end
    --? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
    local s = line.data:sub(lo_offset, hi_offset-1)
    local text = App.newText(love.graphics.getFont(), s)
    local text_width = App.width(text)*Zoom
    love.graphics.setColor(0.7,0.7,0.9)
    love.graphics.rectangle('fill', x+lo_px,y, text_width,math.floor(15*Zoom))
    love.graphics.setColor(0,0,0)
    end
    end
  • edit in text.lua at line 75
    [5.2288][5.2288:2324]()
    local frag_len = utf8.len(frag)
  • replacement in text.lua at line 90
    [5.2728][5.2728:2796]()
    -- short words break on spaces
    -- long words break when they must
    [5.2728]
    [5.100]
    -- Return any intersection of the region from Selection1 to Cursor1 with the
    -- region between {line=line_index, pos=apos} and {line=line_index, pos=bpos}.
    -- apos must be less than bpos. However Selection1 and Cursor1 can be in any order.
    -- Result: positions spos,epos between apos,bpos.
    function Text.clip_selection(line_index, apos, bpos)
    if Selection1.line == nil then return nil,nil end
    -- min,max = sorted(Selection1,Cursor1)
    local minl,minp = Selection1.line,Selection1.pos
    local maxl,maxp = Cursor1.line,Cursor1.pos
    if minl > maxl then
    minl,maxl = maxl,minl
    minp,maxp = maxp,minp
    elseif minl == maxl then
    if minp > maxp then
    minp,maxp = maxp,minp
    end
    end
    -- check if intervals are disjoint
    if line_index < minl then return nil,nil end
    if line_index > maxl then return nil,nil end
    if line_index == minl and bpos <= minp then return nil,nil end
    if line_index == maxl and apos >= maxp then return nil,nil end
    -- compare bounds more carefully (start inclusive, end exclusive)
    local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos})
    local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp})
    --? print(minl,line_index,maxl, '--', minp,apos,bpos,maxp, '--', a_ge,b_lt)
    if a_ge and b_lt then
    -- fully contained
    return apos,bpos
    elseif a_ge then
    assert(maxl == line_index)
    return apos,maxp
    elseif b_lt then
    assert(minl == line_index)
    return minp,bpos
    else
    assert(minl == maxl and minl == line_index)
    return minp,maxp
    end
    end
    function Text.delete_selection()
    if Selection1.line == nil then return end
    -- min,max = sorted(Selection1,Cursor1)
    local minl,minp = Selection1.line,Selection1.pos
    local maxl,maxp = Cursor1.line,Cursor1.pos
    if minl > maxl then
    minl,maxl = maxl,minl
    minp,maxp = maxp,minp
    elseif minl == maxl then
    if minp > maxp then
    minp,maxp = maxp,minp
    end
    end
    -- update Cursor1 and Selection1
    Cursor1.line = minl
    Cursor1.pos = minp
    Selection1 = {}
    -- delete everything between min (inclusive) and max (exclusive)
    Lines[minl].fragments = nil
    Lines[minl].screen_line_starting_pos = nil
    local min_offset = utf8.offset(Lines[minl].data, minp)
    local max_offset = utf8.offset(Lines[maxl].data, maxp)
    if minl == maxl then
    --? print('minl == maxl')
    Lines[minl].data = Lines[minl].data:sub(1, min_offset-1)..Lines[minl].data:sub(max_offset)
    return
    end
    assert(minl < maxl)
    local rhs = Lines[maxl].data:sub(max_offset)
    for i=maxl,minl+1,-1 do
    table.remove(Lines, i)
    end
    Lines[minl].data = Lines[minl].data:sub(1, min_offset-1)..rhs
    end
  • edit in text.lua at line 930
    [17.2857]
    [18.2046]
    end
    -- some tests for operating over selections created using Shift- chords
    -- we're just testing delete_selection, and it works the same for all keys
    function test_backspace_over_selection()
    io.write('\ntest_backspace_over_selection')
    -- select just one character within a line with cursor before selection
    App.screen.init{width=25+30, height=60}
    Lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
    Line_width = App.screen.width
    Cursor1 = {line=1, pos=1}
    Selection1 = {line=1, pos=2}
    Zoom = 1
    -- backspace deletes the selected character, even though it's after the cursor
    App.run_after_keychord('backspace')
    check_eq(Lines[1].data, 'bc', "F - test_backspace_over_selection/data")
    -- cursor (remains) at start of selection
    check_eq(Cursor1.line, 1, "F - test_backspace_over_selection/cursor:line")
    check_eq(Cursor1.pos, 1, "F - test_backspace_over_selection/cursor:pos")
    -- selection is cleared
    check_nil(Selection1.line, "F - test_backspace_over_selection/selection")
  • edit in text.lua at line 954
    [18.2051]
    [19.1]
    function test_backspace_over_selection_reverse()
    io.write('\ntest_backspace_over_selection')
    -- select just one character within a line with cursor after selection
    App.screen.init{width=25+30, height=60}
    Lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
    Line_width = App.screen.width
    Cursor1 = {line=1, pos=2}
    Selection1 = {line=1, pos=1}
    Zoom = 1
    -- backspace deletes the selected character
    App.run_after_keychord('backspace')
    check_eq(Lines[1].data, 'bc', "F - test_backspace_over_selection_reverse/data")
    -- cursor moves to start of selection
    check_eq(Cursor1.line, 1, "F - test_backspace_over_selection_reverse/cursor:line")
    check_eq(Cursor1.pos, 1, "F - test_backspace_over_selection_reverse/cursor:pos")
    -- selection is cleared
    check_nil(Selection1.line, "F - test_backspace_over_selection_reverse/selection")
    end
    function test_backspace_over_multiple_lines()
    io.write('\ntest_backspace_over_selection')
    -- select just one character within a line with cursor after selection
    App.screen.init{width=25+30, height=60}
    Lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
    Line_width = App.screen.width
    Cursor1 = {line=1, pos=2}
    Selection1 = {line=4, pos=2}
    Zoom = 1
    -- backspace deletes the region and joins the remaining portions of lines on either side
    App.run_after_keychord('backspace')
    check_eq(Lines[1].data, 'akl', "F - test_backspace_over_multiple_lines/data:1")
    check_eq(Lines[2].data, 'mno', "F - test_backspace_over_multiple_lines/data:2")
    -- cursor remains at start of selection
    check_eq(Cursor1.line, 1, "F - test_backspace_over_multiple_lines/cursor:line")
    check_eq(Cursor1.pos, 2, "F - test_backspace_over_multiple_lines/cursor:pos")
    -- selection is cleared
    check_nil(Selection1.line, "F - test_backspace_over_multiple_lines/selection")
    end
    function test_backspace_to_end_of_line()
    io.write('\ntest_backspace_over_selection')
    -- select region from cursor to end of line
    App.screen.init{width=25+30, height=60}
    Lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
    Line_width = App.screen.width
    Cursor1 = {line=1, pos=2}
    Selection1 = {line=1, pos=4}
    Zoom = 1
    -- backspace deletes rest of line without joining to any other line
    App.run_after_keychord('backspace')
    check_eq(Lines[1].data, 'a', "F - test_backspace_to_start_of_line/data:1")
    check_eq(Lines[2].data, 'def', "F - test_backspace_to_start_of_line/data:2")
    -- cursor remains at start of selection
    check_eq(Cursor1.line, 1, "F - test_backspace_to_start_of_line/cursor:line")
    check_eq(Cursor1.pos, 2, "F - test_backspace_to_start_of_line/cursor:pos")
    -- selection is cleared
    check_nil(Selection1.line, "F - test_backspace_to_start_of_line/selection")
    end
    function test_backspace_to_start_of_line()
    io.write('\ntest_backspace_over_selection')
    -- select region from cursor to start of line
    App.screen.init{width=25+30, height=60}
    Lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
    Line_width = App.screen.width
    Cursor1 = {line=2, pos=1}
    Selection1 = {line=2, pos=3}
    Zoom = 1
    -- backspace deletes beginning of line without joining to any other line
    App.run_after_keychord('backspace')
    check_eq(Lines[1].data, 'abc', "F - test_backspace_to_start_of_line/data:1")
    check_eq(Lines[2].data, 'f', "F - test_backspace_to_start_of_line/data:2")
    -- cursor remains at start of selection
    check_eq(Cursor1.line, 2, "F - test_backspace_to_start_of_line/cursor:line")
    check_eq(Cursor1.pos, 1, "F - test_backspace_to_start_of_line/cursor:pos")
    -- selection is cleared
    check_nil(Selection1.line, "F - test_backspace_to_start_of_line/selection")
    end
  • edit in text.lua at line 1098
    [5.627]
    [20.1]
    --? print(chord)
  • edit in text.lua at line 1116
    [5.2081]
    [5.1902]
    if Selection1.line then
    Text.delete_selection()
    return
    end
  • edit in text.lua at line 1152
    [5.3066]
    [5.2805]
    if Selection1.line then
    Text.delete_selection()
    return
    end
  • edit in text.lua at line 1187
    [20.277]
    [20.277]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1192
    [20.324]
    [20.324]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1196
    [20.341]
    [20.369]
    elseif chord == 'S-left' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Text.left()
    elseif chord == 'S-right' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Text.right()
  • edit in text.lua at line 1208
    [20.456]
    [5.1]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1213
    [20.794]
    [5.23]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1217
    [5.45]
    [20.1073]
    elseif chord == 'M-S-left' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Text.word_left()
    elseif chord == 'M-S-right' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Text.word_right()
  • edit in text.lua at line 1228
    [20.1103]
    [20.1103]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1233
    [20.1152]
    [20.1152]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1237
    [20.1209]
    [5.3956]
    elseif chord == 'S-home' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Cursor1.pos = 1
    elseif chord == 'S-end' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Cursor1.pos = utf8.len(Lines[Cursor1.line].data) + 1
  • edit in text.lua at line 1248
    [5.3984]
    [5.46]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1253
    [5.4463]
    [5.61]
    if Selection1.line then
    Selection1 = {}
    end
    Text.down()
    elseif chord == 'S-up' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Text.up()
    elseif chord == 'S-down' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
  • edit in text.lua at line 1268
    [21.33]
    [3.1]
    if Selection1.line then
    Selection1 = {}
    end
  • edit in text.lua at line 1273
    [21.893]
    [3.20]
    if Selection1.line then
    Selection1 = {}
    end
    Text.pagedown()
    elseif chord == 'S-pageup' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    Text.pageup()
    elseif chord == 'S-pagedown' then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
  • replacement in text.lua at line 1439
    [5.5453][5.5453:5516]()
    if Lines[Cursor1.line].data:sub(offset,offset) == ' ' then
    [5.5453]
    [5.5516]
    if Lines[Cursor1.line].data:sub(offset,offset) == ' ' then -- TODO: other space characters
  • edit in test.lua at line 11
    [22.214]
    [22.214]
    end
    end
    function check_nil(x, msg)
    if x == nil then
    io.write('.')
    else
    error(msg..'; should be nil but got "'..x..'"')
  • edit in main.lua at line 62
    [5.9198]
    [24.54]
    Selection1 = {}
  • edit in main.lua at line 183
    [25.492]
    [26.1788]
    if love.keyboard.isDown('lshift') or love.keyboard.isDown('rshift') then
    if Selection1.line == nil then
    Selection1 = {line=Cursor1.line, pos=Cursor1.pos}
    end
    else
    if Selection1.line then
    Selection1 = {}
    end
    end