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