Merge lines.love
[view.love.git] / select.lua
blob78affdcc7b9ad5547ffed6eed68fb0d43a1c4b76
1 -- helpers for selecting portions of text
3 -- Return any intersection of the region from State.selection1 to
4 -- State.cursor1 (or current mouse, if mouse is pressed) with the region
5 -- between {line=line_index, pos=apos} 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
46 -- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
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
68 function Text.mouse_pos(State)
69 local x,y = App.mouse_x(), App.mouse_y()
70 if y < State.top then
71 return State.screen_top1.line, State.screen_top1.pos
72 end
73 for line_index,line in ipairs(State.lines) do
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 local screen_bottom1 = Text.screen_bottom1(State)
79 return screen_bottom1.line, Text.pos_at_end_of_screen_line(State, screen_bottom1)
80 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
89 function Text.delete_selection_and_record_undo_event(State)
90 if State.selection1.line == nil then return end
91 local minl,maxl = minmax(State.selection1.line, State.cursor1.line)
92 local before = snapshot(State, minl, maxl)
93 Text.delete_selection_without_undo(State)
94 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
95 end
97 function Text.delete_selection_without_undo(State)
98 if State.selection1.line == nil then return end
99 -- min,max = sorted(State.selection1,State.cursor1)
100 local minl,minp = State.selection1.line,State.selection1.pos
101 local maxl,maxp = State.cursor1.line,State.cursor1.pos
102 if minl > maxl then
103 minl,maxl = maxl,minl
104 minp,maxp = maxp,minp
105 elseif minl == maxl then
106 if minp > maxp then
107 minp,maxp = maxp,minp
110 -- update State.cursor1 and State.selection1
111 State.cursor1.line = minl
112 State.cursor1.pos = minp
113 if Text.lt1(State.cursor1, State.screen_top1) then
114 State.screen_top1.line = State.cursor1.line
115 State.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
117 State.selection1 = {}
118 -- delete everything between min (inclusive) and max (exclusive)
119 Text.clear_screen_line_cache(State, minl)
120 local min_offset = Text.offset(State.lines[minl].data, minp)
121 local max_offset = Text.offset(State.lines[maxl].data, maxp)
122 if minl == maxl then
123 --? print('minl == maxl')
124 State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)
125 return
127 assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
128 local rhs = State.lines[maxl].data:sub(max_offset)
129 for i=maxl,minl+1,-1 do
130 table.remove(State.lines, i)
131 table.remove(State.line_cache, i)
133 State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..rhs
136 function Text.selection(State)
137 if State.selection1.line == nil then return end
138 -- min,max = sorted(State.selection1,State.cursor1)
139 local minl,minp = State.selection1.line,State.selection1.pos
140 local maxl,maxp = State.cursor1.line,State.cursor1.pos
141 if minl > maxl then
142 minl,maxl = maxl,minl
143 minp,maxp = maxp,minp
144 elseif minl == maxl then
145 if minp > maxp then
146 minp,maxp = maxp,minp
149 local min_offset = Text.offset(State.lines[minl].data, minp)
150 local max_offset = Text.offset(State.lines[maxl].data, maxp)
151 if minl == maxl then
152 return State.lines[minl].data:sub(min_offset, max_offset-1)
154 assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
155 local result = {State.lines[minl].data:sub(min_offset)}
156 for i=minl+1,maxl-1 do
157 table.insert(result, State.lines[i].data)
159 table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
160 return table.concat(result, '\n')