select.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.cut_selection_and_record_undo_event(State)
  79. if State.selection1.line == nil then return end
  80. local result = Text.selection(State)
  81. Text.delete_selection_and_record_undo_event(State)
  82. return result
  83. end
  84. function Text.delete_selection_and_record_undo_event(State)
  85. if State.selection1.line == nil then return end
  86. local minl,maxl = minmax(State.selection1.line, State.cursor1.line)
  87. local before = snapshot(State, minl, maxl)
  88. Text.delete_selection_without_undo(State)
  89. record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
  90. end
  91. function Text.delete_selection_without_undo(State)
  92. if State.selection1.line == nil then return end
  93. -- min,max = sorted(State.selection1,State.cursor1)
  94. local minl,minp = State.selection1.line,State.selection1.pos
  95. local maxl,maxp = State.cursor1.line,State.cursor1.pos
  96. if minl > maxl then
  97. minl,maxl = maxl,minl
  98. minp,maxp = maxp,minp
  99. elseif minl == maxl then
  100. if minp > maxp then
  101. minp,maxp = maxp,minp
  102. end
  103. end
  104. -- update State.cursor1 and State.selection1
  105. State.cursor1.line = minl
  106. State.cursor1.pos = minp
  107. if Text.lt1(State.cursor1, State.screen_top1) then
  108. State.screen_top1.line = State.cursor1.line
  109. State.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
  110. end
  111. State.selection1 = {}
  112. -- delete everything between min (inclusive) and max (exclusive)
  113. Text.clear_screen_line_cache(State, minl)
  114. local min_offset = Text.offset(State.lines[minl].data, minp)
  115. local max_offset = Text.offset(State.lines[maxl].data, maxp)
  116. if minl == maxl then
  117. --? print('minl == maxl')
  118. State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)
  119. return
  120. end
  121. assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
  122. local rhs = State.lines[maxl].data:sub(max_offset)
  123. for i=maxl,minl+1,-1 do
  124. table.remove(State.lines, i)
  125. table.remove(State.line_cache, i)
  126. end
  127. State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..rhs
  128. end
  129. function Text.selection(State)
  130. if State.selection1.line == nil then return end
  131. -- min,max = sorted(State.selection1,State.cursor1)
  132. local minl,minp = State.selection1.line,State.selection1.pos
  133. local maxl,maxp = State.cursor1.line,State.cursor1.pos
  134. if minl > maxl then
  135. minl,maxl = maxl,minl
  136. minp,maxp = maxp,minp
  137. elseif minl == maxl then
  138. if minp > maxp then
  139. minp,maxp = maxp,minp
  140. end
  141. end
  142. local min_offset = Text.offset(State.lines[minl].data, minp)
  143. local max_offset = Text.offset(State.lines[maxl].data, maxp)
  144. if minl == maxl then
  145. return State.lines[minl].data:sub(min_offset, max_offset-1)
  146. end
  147. assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
  148. local result = {State.lines[minl].data:sub(min_offset)}
  149. for i=minl+1,maxl-1 do
  150. table.insert(result, State.lines[i].data)
  151. end
  152. table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
  153. return table.concat(result, '\n')
  154. end