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 State
.mouse_down
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
})
36 assert(maxl
== line_index
, ('maxl %d not equal to line_index %d'):format(maxl
, line_index
))
39 assert(minl
== line_index
, ('minl %d not equal to line_index %d'):format(minl
, line_index
))
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
))
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
)
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
)
59 local before
= line
.data
:sub(pos_offset
, lo_offset
-1)
60 lo_px
= State
.font
:getWidth(before
)
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
)
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
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
)
82 return State
.screen_bottom1
.line
, Text
.pos_at_end_of_screen_line(State
, State
.screen_bottom1
)
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
)
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
)})
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
106 minl
,maxl
= maxl
,minl
107 minp
,maxp
= maxp
,minp
108 elseif minl
== maxl
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
)
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
)
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
145 minl
,maxl
= maxl
,minl
146 minp
,maxp
= maxp
,minp
147 elseif minl
== maxl
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
)
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')