select.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. -- helpers for selecting portions of text
  2. -- Return any intersection of the region from State.selection1 to
  3. -- State.cursor1 (or current mouse, if mouse is pressed) with the region
  4. -- between {line=line_index, pos=apos} and {line=line_index, pos=bpos}.
  5. -- apos must be less than bpos. However State.selection1 and State.cursor1 can be in any order.
  6. -- Result: positions spos,epos between apos,bpos.
  7. function Text.clip_selection(State, line_index, apos, bpos)
  8. if State.selection1.line == nil then return nil,nil end
  9. -- min,max = sorted(State.selection1,State.cursor1)
  10. local minl,minp = State.selection1.line,State.selection1.pos
  11. local maxl,maxp
  12. if State.mouse_down then
  13. maxl,maxp = Text.mouse_pos(State)
  14. else
  15. maxl,maxp = State.cursor1.line,State.cursor1.pos
  16. end
  17. if Text.lt1({line=maxl, pos=maxp},
  18. {line=minl, pos=minp}) then
  19. minl,maxl = maxl,minl
  20. minp,maxp = maxp,minp
  21. end
  22. -- check if intervals are disjoint
  23. if line_index < minl then return nil,nil end
  24. if line_index > maxl then return nil,nil end
  25. if line_index == minl and bpos <= minp then return nil,nil end
  26. if line_index == maxl and apos >= maxp then return nil,nil end
  27. -- compare bounds more carefully (start inclusive, end exclusive)
  28. local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos})
  29. local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp})
  30. if a_ge and b_lt then
  31. -- fully contained
  32. return apos,bpos
  33. elseif a_ge then
  34. assert(maxl == line_index, ('maxl %d not equal to line_index %d'):format(maxl, line_index))
  35. return apos,maxp
  36. elseif b_lt then
  37. assert(minl == line_index, ('minl %d not equal to line_index %d'):format(minl, line_index))
  38. return minp,bpos
  39. else
  40. assert(minl == maxl and minl == line_index, ('minl %d, maxl %d and line_index %d are not all equal'):format(minl, maxl, line_index))
  41. return minp,maxp
  42. end
  43. end
  44. -- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
  45. -- Returns some intermediate computation useful elsewhere.
  46. function Text.draw_highlight(State, line, x,y, pos, lo,hi)
  47. if lo then
  48. local lo_offset = Text.offset(line.data, lo)
  49. local hi_offset = Text.offset(line.data, hi)
  50. local pos_offset = Text.offset(line.data, pos)
  51. local lo_px
  52. if pos == lo then
  53. lo_px = 0
  54. else
  55. local before = line.data:sub(pos_offset, lo_offset-1)
  56. lo_px = State.font:getWidth(before)
  57. end
  58. local s = line.data:sub(lo_offset, hi_offset-1)
  59. App.color(Highlight_color)
  60. love.graphics.rectangle('fill', x+lo_px,y, State.font:getWidth(s),State.line_height)
  61. App.color(Text_color)
  62. return lo_px
  63. end
  64. end
  65. function Text.mouse_pos(State)
  66. local x,y = App.mouse_x(), App.mouse_y()
  67. if y < State.top then
  68. return State.screen_top1.line, State.screen_top1.pos
  69. end
  70. for line_index,line in ipairs(State.lines) do
  71. if Text.in_line(State, line_index, x,y) then
  72. return line_index, Text.to_pos_on_line(State, line_index, x,y)
  73. end
  74. end
  75. local screen_bottom1 = Text.screen_bottom1(State)
  76. return screen_bottom1.line, Text.pos_at_end_of_screen_line(State, screen_bottom1)
  77. end
  78. function Text.selection(State)
  79. if State.selection1.line == nil then return end
  80. -- min,max = sorted(State.selection1,State.cursor1)
  81. local minl,minp = State.selection1.line,State.selection1.pos
  82. local maxl,maxp = State.cursor1.line,State.cursor1.pos
  83. if minl > maxl then
  84. minl,maxl = maxl,minl
  85. minp,maxp = maxp,minp
  86. elseif minl == maxl then
  87. if minp > maxp then
  88. minp,maxp = maxp,minp
  89. end
  90. end
  91. local min_offset = Text.offset(State.lines[minl].data, minp)
  92. local max_offset = Text.offset(State.lines[maxl].data, maxp)
  93. if minl == maxl then
  94. return State.lines[minl].data:sub(min_offset, max_offset-1)
  95. end
  96. assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
  97. local result = {State.lines[minl].data:sub(min_offset)}
  98. for i=minl+1,maxl-1 do
  99. table.insert(result, State.lines[i].data)
  100. end
  101. table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
  102. return table.concat(result, '\n')
  103. end