bugfix: don't clear selection on M-arrow
[view.love.git] / source_select.lua
blob6e21c7b9fba1304d1a74078ca7055fddc5342948
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 = State.font:getWidth(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, State.font:getWidth(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 line.mode == 'text' then
77 if Text.in_line(State, line_index, x,y) then
78 return line_index, Text.to_pos_on_line(State, line_index, x,y)
79 end
80 end
81 end
82 return State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1)
83 end
85 function Text.cut_selection(State)
86 if State.selection1.line == nil then return end
87 local result = Text.selection(State)
88 Text.delete_selection(State)
89 return result
90 end
92 function Text.delete_selection(State)
93 if State.selection1.line == nil then return end
94 local minl,maxl = minmax(State.selection1.line, State.cursor1.line)
95 local before = snapshot(State, minl, maxl)
96 Text.delete_selection_without_undo(State)
97 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
98 end
100 function Text.delete_selection_without_undo(State)
101 if State.selection1.line == nil then return end
102 -- min,max = sorted(State.selection1,State.cursor1)
103 local minl,minp = State.selection1.line,State.selection1.pos
104 local maxl,maxp = State.cursor1.line,State.cursor1.pos
105 if minl > maxl then
106 minl,maxl = maxl,minl
107 minp,maxp = maxp,minp
108 elseif minl == maxl then
109 if minp > maxp then
110 minp,maxp = maxp,minp
113 -- update State.cursor1 and State.selection1
114 State.cursor1.line = minl
115 State.cursor1.pos = minp
116 if Text.lt1(State.cursor1, State.screen_top1) then
117 State.screen_top1.line = State.cursor1.line
118 State.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
120 State.selection1 = {}
121 -- delete everything between min (inclusive) and max (exclusive)
122 Text.clear_screen_line_cache(State, minl)
123 local min_offset = Text.offset(State.lines[minl].data, minp)
124 local max_offset = Text.offset(State.lines[maxl].data, maxp)
125 if minl == maxl then
126 --? print('minl == maxl')
127 State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)
128 return
130 assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
131 local rhs = State.lines[maxl].data:sub(max_offset)
132 for i=maxl,minl+1,-1 do
133 table.remove(State.lines, i)
134 table.remove(State.line_cache, i)
136 State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..rhs
139 function Text.selection(State)
140 if State.selection1.line == nil then return end
141 -- min,max = sorted(State.selection1,State.cursor1)
142 local minl,minp = State.selection1.line,State.selection1.pos
143 local maxl,maxp = State.cursor1.line,State.cursor1.pos
144 if minl > maxl then
145 minl,maxl = maxl,minl
146 minp,maxp = maxp,minp
147 elseif minl == maxl then
148 if minp > maxp then
149 minp,maxp = maxp,minp
152 local min_offset = Text.offset(State.lines[minl].data, minp)
153 local max_offset = Text.offset(State.lines[maxl].data, maxp)
154 if minl == maxl then
155 return State.lines[minl].data:sub(min_offset, max_offset-1)
157 assert(minl < maxl, ('minl %d not < maxl %d'):format(minl, maxl))
158 local result = {State.lines[minl].data:sub(min_offset)}
159 for i=minl+1,maxl-1 do
160 if State.lines[i].mode == 'text' then
161 table.insert(result, State.lines[i].data)
164 table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
165 return table.concat(result, '\n')