source_select.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. -- helpers for selecting portions of text
  2. -- Return any intersection of the region from State.selection1 to State.cursor1 (or
  3. -- current mouse, if mouse is pressed; or recent mouse if mouse is pressed and
  4. -- currently over a drawing) with the region between {line=line_index, pos=apos}
  5. -- and {line=line_index, pos=bpos}.
  6. -- apos must be less than bpos. However State.selection1 and State.cursor1 can be in any order.
  7. -- Result: positions spos,epos between apos,bpos.
  8. function Text.clip_selection(State, line_index, apos, bpos)
  9. if State.selection1.line == nil then return nil,nil end
  10. -- min,max = sorted(State.selection1,State.cursor1)
  11. local minl,minp = State.selection1.line,State.selection1.pos
  12. local maxl,maxp
  13. if State.mouse_down then
  14. maxl,maxp = Text.mouse_pos(State)
  15. else
  16. maxl,maxp = State.cursor1.line,State.cursor1.pos
  17. end
  18. if Text.lt1({line=maxl, pos=maxp},
  19. {line=minl, pos=minp}) then
  20. minl,maxl = maxl,minl
  21. minp,maxp = maxp,minp
  22. end
  23. -- check if intervals are disjoint
  24. if line_index < minl then return nil,nil end
  25. if line_index > maxl then return nil,nil end
  26. if line_index == minl and bpos <= minp then return nil,nil end
  27. if line_index == maxl and apos >= maxp then return nil,nil end
  28. -- compare bounds more carefully (start inclusive, end exclusive)
  29. local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos})
  30. local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp})
  31. if a_ge and b_lt then
  32. -- fully contained
  33. return apos,bpos
  34. elseif a_ge then
  35. assert(maxl == line_index, ('maxl %d not equal to line_index %d'):format(maxl, line_index))
  36. return apos,maxp
  37. elseif b_lt then
  38. assert(minl == line_index, ('minl %d not equal to line_index %d'):format(minl, line_index))
  39. return minp,bpos
  40. else
  41. assert(minl == maxl and minl == line_index, ('minl %d, maxl %d and line_index %d are not all equal'):format(minl, maxl, line_index))
  42. return minp,maxp
  43. end
  44. end
  45. -- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
  46. -- Creates text objects every time, so use this sparingly.
  47. -- Returns some intermediate computation useful elsewhere.
  48. function Text.draw_highlight(State, line, x,y, pos, lo,hi)
  49. if lo then
  50. local lo_offset = Text.offset(line.data, lo)
  51. local hi_offset = Text.offset(line.data, hi)
  52. local pos_offset = Text.offset(line.data, pos)
  53. local lo_px
  54. if pos == lo then
  55. lo_px = 0
  56. else
  57. local before = line.data:sub(pos_offset, lo_offset-1)
  58. lo_px = State.font:getWidth(before)
  59. end
  60. local s = line.data:sub(lo_offset, hi_offset-1)
  61. App.color(Highlight_color)
  62. love.graphics.rectangle('fill', x+lo_px,y, State.font:getWidth(s),State.line_height)
  63. App.color(Text_color)
  64. return lo_px
  65. end
  66. end
  67. function Text.mouse_pos(State)
  68. local x,y = App.mouse_x(), App.mouse_y()
  69. if y < State.top then
  70. return State.screen_top1.line, State.screen_top1.pos
  71. end
  72. for line_index,line in ipairs(State.lines) do
  73. if line.mode == 'text' then
  74. if Text.in_line(State, line_index, x,y) then
  75. return line_index, Text.to_pos_on_line(State, line_index, x,y)
  76. end
  77. end
  78. end
  79. local screen_bottom1 = Text.screen_bottom1(State)
  80. return screen_bottom1.line, Text.pos_at_end_of_screen_line(State, screen_bottom1)
  81. end
  82. function Text.cut_selection_and_record_undo_event(State)
  83. if State.selection1.line == nil then return end
  84. local result = Text.selection(State)
  85. Text.delete_selection_and_record_undo_event(State)
  86. return result
  87. end
  88. function Text.delete_selection_and_record_undo_event(State)
  89. if State.selection1.line == nil then return end
  90. local minl,maxl = minmax(State.selection1.line, State.cursor1.line)
  91. local before = snapshot(State, minl, maxl)
  92. Text.delete_selection_without_undo(State)
  93. record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
  94. end
  95. function Text.delete_selection_without_undo(State)
  96. if State.selection1.line == nil then return end
  97. -- min,max = sorted(State.selection1,State.cursor1)
  98. local minl,minp = State.selection1.line,State.selection1.pos
  99. local maxl,maxp = State.cursor1.line,State.cursor1.pos
  100. if minl > maxl then
  101. minl,maxl = maxl,minl
  102. minp,maxp = maxp,minp
  103. elseif minl == maxl then
  104. if minp > maxp then
  105. minp,maxp = maxp,minp
  106. end
  107. end
  108. -- update State.cursor1 and State.selection1
  109. State.cursor1.line = minl
  110. State.cursor1.pos = minp
  111. if Text.lt1(State.cursor1, State.screen_top1) then
  112. State.screen_top1.line = State.cursor1.line
  113. State.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
  114. end
  115. State.selection1 = {}
  116. -- delete everything between min (inclusive) and max (exclusive)
  117. Text.clear_screen_line_cache(State, minl)
  118. local min_offset = Text.offset(State.lines[minl].data, minp)
  119. local max_offset = Text.offset(State.lines[maxl].data, maxp)
  120. if minl == maxl then
  121. --? print('minl == maxl')
  122. State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)
  123. return
  124. end
  125. assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
  126. local rhs = State.lines[maxl].data:sub(max_offset)
  127. for i=maxl,minl+1,-1 do
  128. table.remove(State.lines, i)
  129. table.remove(State.line_cache, i)
  130. end
  131. State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..rhs
  132. end
  133. function Text.selection(State)
  134. if State.selection1.line == nil then return end
  135. -- min,max = sorted(State.selection1,State.cursor1)
  136. local minl,minp = State.selection1.line,State.selection1.pos
  137. local maxl,maxp = State.cursor1.line,State.cursor1.pos
  138. if minl > maxl then
  139. minl,maxl = maxl,minl
  140. minp,maxp = maxp,minp
  141. elseif minl == maxl then
  142. if minp > maxp then
  143. minp,maxp = maxp,minp
  144. end
  145. end
  146. local min_offset = Text.offset(State.lines[minl].data, minp)
  147. local max_offset = Text.offset(State.lines[maxl].data, maxp)
  148. if minl == maxl then
  149. return State.lines[minl].data:sub(min_offset, max_offset-1)
  150. end
  151. assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
  152. local result = {State.lines[minl].data:sub(min_offset)}
  153. for i=minl+1,maxl-1 do
  154. if State.lines[i].mode == 'text' then
  155. table.insert(result, State.lines[i].data)
  156. end
  157. end
  158. table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
  159. return table.concat(result, '\n')
  160. end