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
14 if App
.mouse_down(1) then
15 maxl
,maxp
= Text
.mouse_pos(State
)
17 maxl
,maxp
= State
.cursor1
.line
,State
.cursor1
.pos
19 if Text
.lt1({line
=maxl
, pos
=maxp
},
20 {line
=minl
, pos
=minp
}) then
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 --? print(minl,line_index,maxl, '--', minp,apos,bpos,maxp, '--', a_ge,b_lt)
37 assert(maxl
== line_index
)
40 assert(minl
== line_index
)
43 assert(minl
== maxl
and minl
== line_index
)
48 -- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
49 -- Creates text objects every time, so use this sparingly.
50 -- Returns some intermediate computation useful elsewhere.
51 function Text
.draw_highlight(State
, line
, x
,y
, pos
, lo
,hi
)
53 local lo_offset
= Text
.offset(line
.data
, lo
)
54 local hi_offset
= Text
.offset(line
.data
, hi
)
55 local pos_offset
= Text
.offset(line
.data
, pos
)
60 local before
= line
.data
:sub(pos_offset
, lo_offset
-1)
61 lo_px
= App
.width(before
)
63 --? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
64 local s
= line
.data
:sub(lo_offset
, hi_offset
-1)
65 App
.color(Highlight_color
)
66 love
.graphics
.rectangle('fill', x
+lo_px
,y
, App
.width(s
),State
.line_height
)
72 -- inefficient for some reason, so don't do it on every frame
73 function Text
.mouse_pos(State
)
74 local time
= love
.timer
.getTime()
75 if State
.recent_mouse
.time
and State
.recent_mouse
.time
> time
-0.1 then
76 return State
.recent_mouse
.line
, State
.recent_mouse
.pos
78 State
.recent_mouse
.time
= time
79 local line
,pos
= Text
.to_pos(State
, App
.mouse_x(), App
.mouse_y())
81 State
.recent_mouse
.line
= line
82 State
.recent_mouse
.pos
= pos
84 return State
.recent_mouse
.line
, State
.recent_mouse
.pos
87 function Text
.to_pos(State
, x
,y
)
88 for line_index
,line
in ipairs(State
.lines
) do
89 if line
.mode
== 'text' then
90 if Text
.in_line(State
, line_index
, x
,y
) then
91 return line_index
, Text
.to_pos_on_line(State
, line_index
, x
,y
)
97 function Text
.cut_selection(State
)
98 if State
.selection1
.line
== nil then return end
99 local result
= Text
.selection(State
)
100 Text
.delete_selection(State
)
104 function Text
.delete_selection(State
)
105 if State
.selection1
.line
== nil then return end
106 local minl
,maxl
= minmax(State
.selection1
.line
, State
.cursor1
.line
)
107 local before
= snapshot(State
, minl
, maxl
)
108 Text
.delete_selection_without_undo(State
)
109 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
112 function Text
.delete_selection_without_undo(State
)
113 if State
.selection1
.line
== nil then return end
114 -- min,max = sorted(State.selection1,State.cursor1)
115 local minl
,minp
= State
.selection1
.line
,State
.selection1
.pos
116 local maxl
,maxp
= State
.cursor1
.line
,State
.cursor1
.pos
118 minl
,maxl
= maxl
,minl
119 minp
,maxp
= maxp
,minp
120 elseif minl
== maxl
then
122 minp
,maxp
= maxp
,minp
125 -- update State.cursor1 and State.selection1
126 State
.cursor1
.line
= minl
127 State
.cursor1
.pos
= minp
128 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
129 State
.screen_top1
.line
= State
.cursor1
.line
130 State
.screen_top1
.pos
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
132 State
.selection1
= {}
133 -- delete everything between min (inclusive) and max (exclusive)
134 Text
.clear_screen_line_cache(State
, minl
)
135 local min_offset
= Text
.offset(State
.lines
[minl
].data
, minp
)
136 local max_offset
= Text
.offset(State
.lines
[maxl
].data
, maxp
)
138 --? print('minl == maxl')
139 State
.lines
[minl
].data
= State
.lines
[minl
].data
:sub(1, min_offset
-1)..State
.lines
[minl
].data
:sub(max_offset
)
143 local rhs
= State
.lines
[maxl
].data
:sub(max_offset
)
144 for i
=maxl
,minl
+1,-1 do
145 table.remove(State
.lines
, i
)
146 table.remove(State
.line_cache
, i
)
148 State
.lines
[minl
].data
= State
.lines
[minl
].data
:sub(1, min_offset
-1)..rhs
151 function Text
.selection(State
)
152 if State
.selection1
.line
== nil then return end
153 -- min,max = sorted(State.selection1,State.cursor1)
154 local minl
,minp
= State
.selection1
.line
,State
.selection1
.pos
155 local maxl
,maxp
= State
.cursor1
.line
,State
.cursor1
.pos
157 minl
,maxl
= maxl
,minl
158 minp
,maxp
= maxp
,minp
159 elseif minl
== maxl
then
161 minp
,maxp
= maxp
,minp
164 local min_offset
= Text
.offset(State
.lines
[minl
].data
, minp
)
165 local max_offset
= Text
.offset(State
.lines
[maxl
].data
, maxp
)
167 return State
.lines
[minl
].data
:sub(min_offset
, max_offset
-1)
170 local result
= {State
.lines
[minl
].data
:sub(min_offset
)}
171 for i
=minl
+1,maxl
-1 do
172 if State
.lines
[i
].mode
== 'text' then
173 table.insert(result
, State
.lines
[i
].data
)
176 table.insert(result
, State
.lines
[maxl
].data
:sub(1, max_offset
-1))
177 return table.concat(result
, '\n')