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 local screen_bottom1
= Text
.screen_bottom1(State
)
83 return screen_bottom1
.line
, Text
.pos_at_end_of_screen_line(State
, screen_bottom1
)
86 function Text
.cut_selection(State
)
87 if State
.selection1
.line
== nil then return end
88 local result
= Text
.selection(State
)
89 Text
.delete_selection(State
)
93 function Text
.delete_selection(State
)
94 if State
.selection1
.line
== nil then return end
95 local minl
,maxl
= minmax(State
.selection1
.line
, State
.cursor1
.line
)
96 local before
= snapshot(State
, minl
, maxl
)
97 Text
.delete_selection_without_undo(State
)
98 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
101 function Text
.delete_selection_without_undo(State
)
102 if State
.selection1
.line
== nil then return end
103 -- min,max = sorted(State.selection1,State.cursor1)
104 local minl
,minp
= State
.selection1
.line
,State
.selection1
.pos
105 local maxl
,maxp
= State
.cursor1
.line
,State
.cursor1
.pos
107 minl
,maxl
= maxl
,minl
108 minp
,maxp
= maxp
,minp
109 elseif minl
== maxl
then
111 minp
,maxp
= maxp
,minp
114 -- update State.cursor1 and State.selection1
115 State
.cursor1
.line
= minl
116 State
.cursor1
.pos
= minp
117 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
118 State
.screen_top1
.line
= State
.cursor1
.line
119 State
.screen_top1
.pos
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
121 State
.selection1
= {}
122 -- delete everything between min (inclusive) and max (exclusive)
123 Text
.clear_screen_line_cache(State
, minl
)
124 local min_offset
= Text
.offset(State
.lines
[minl
].data
, minp
)
125 local max_offset
= Text
.offset(State
.lines
[maxl
].data
, maxp
)
127 --? print('minl == maxl')
128 State
.lines
[minl
].data
= State
.lines
[minl
].data
:sub(1, min_offset
-1)..State
.lines
[minl
].data
:sub(max_offset
)
131 assert(minl
< maxl
, ('minl %d not < maxl %d'):format(minl
, maxl
))
132 local rhs
= State
.lines
[maxl
].data
:sub(max_offset
)
133 for i
=maxl
,minl
+1,-1 do
134 table.remove(State
.lines
, i
)
135 table.remove(State
.line_cache
, i
)
137 State
.lines
[minl
].data
= State
.lines
[minl
].data
:sub(1, min_offset
-1)..rhs
140 function Text
.selection(State
)
141 if State
.selection1
.line
== nil then return end
142 -- min,max = sorted(State.selection1,State.cursor1)
143 local minl
,minp
= State
.selection1
.line
,State
.selection1
.pos
144 local maxl
,maxp
= State
.cursor1
.line
,State
.cursor1
.pos
146 minl
,maxl
= maxl
,minl
147 minp
,maxp
= maxp
,minp
148 elseif minl
== maxl
then
150 minp
,maxp
= maxp
,minp
153 local min_offset
= Text
.offset(State
.lines
[minl
].data
, minp
)
154 local max_offset
= Text
.offset(State
.lines
[maxl
].data
, maxp
)
156 return State
.lines
[minl
].data
:sub(min_offset
, max_offset
-1)
158 assert(minl
< maxl
, ('minl %d not < maxl %d'):format(minl
, maxl
))
159 local result
= {State
.lines
[minl
].data
:sub(min_offset
)}
160 for i
=minl
+1,maxl
-1 do
161 if State
.lines
[i
].mode
== 'text' then
162 table.insert(result
, State
.lines
[i
].data
)
165 table.insert(result
, State
.lines
[maxl
].data
:sub(1, max_offset
-1))
166 return table.concat(result
, '\n')