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