1 -- text editor, particularly text drawing, horizontal wrap, vertical scrolling
4 -- draw a line starting from startpos to screen at y between State.left and State.right
5 -- return y for the next line, and position of start of final screen line drawn
6 function Text
.draw(State
, line_index
, y
, startpos
)
7 --? print('text.draw', line_index, y)
8 local line
= State
.lines
[line_index
]
9 local line_cache
= State
.line_cache
[line_index
]
11 line_cache
.startpos
= startpos
13 local final_screen_line_starting_pos
= startpos
-- track value to return
14 Text
.populate_screen_line_starting_pos(State
, line_index
)
15 assert(#line_cache
.screen_line_starting_pos
>= 1)
16 for i
=1,#line_cache
.screen_line_starting_pos
do
17 local pos
= line_cache
.screen_line_starting_pos
[i
]
18 if pos
< startpos
then
21 final_screen_line_starting_pos
= pos
22 local screen_line
= Text
.screen_line(line
, line_cache
, i
)
23 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
24 local frag_len
= utf8
.len(screen_line
)
26 if State
.selection1
.line
then
27 local lo
, hi
= Text
.clip_selection(State
, line_index
, pos
, pos
+frag_len
)
28 Text
.draw_highlight(State
, line
, State
.left
,y
, pos
, lo
,hi
)
31 App
.screen
.print(screen_line
, State
.left
,y
)
32 -- render cursor if necessary
33 if line_index
== State
.cursor1
.line
then
34 if pos
<= State
.cursor1
.pos
and pos
+ frag_len
>= State
.cursor1
.pos
then
35 if State
.search_term
then
36 if State
.lines
[State
.cursor1
.line
].data
:sub(State
.cursor1
.pos
, State
.cursor1
.pos
+utf8
.len(State
.search_term
)-1) == State
.search_term
then
37 local lo_px
= Text
.draw_highlight(State
, line
, State
.left
,y
, pos
, State
.cursor1
.pos
, State
.cursor1
.pos
+utf8
.len(State
.search_term
))
39 love
.graphics
.print(State
.search_term
, State
.left
+lo_px
,y
)
42 Text
.draw_cursor(State
, State
.left
+Text
.x(screen_line
, State
.cursor1
.pos
-pos
+1), y
)
46 y
= y
+ State
.line_height
47 if y
>= App
.screen
.height
then
52 return y
, final_screen_line_starting_pos
55 function Text
.screen_line(line
, line_cache
, i
)
56 local pos
= line_cache
.screen_line_starting_pos
[i
]
57 local offset
= Text
.offset(line
.data
, pos
)
58 if i
>= #line_cache
.screen_line_starting_pos
then
59 return line
.data
:sub(offset
)
61 local endpos
= line_cache
.screen_line_starting_pos
[i
+1]-1
62 local end_offset
= Text
.offset(line
.data
, endpos
)
63 return line
.data
:sub(offset
, end_offset
)
66 function Text
.draw_cursor(State
, x
, y
)
68 if math
.floor(Cursor_time
*2)%2 == 0 then
69 App
.color(Cursor_color
)
70 love
.graphics
.rectangle('fill', x
,y
, 3,State
.line_height
)
73 State
.cursor_y
= y
+State
.line_height
76 function Text
.populate_screen_line_starting_pos(State
, line_index
)
77 local line
= State
.lines
[line_index
]
78 if line
.mode
~= 'text' then return end
79 local line_cache
= State
.line_cache
[line_index
]
80 if line_cache
.screen_line_starting_pos
then
83 line_cache
.screen_line_starting_pos
= {1}
86 -- try to wrap at word boundaries
87 for frag
in line
.data
:gmatch('%S*%s*') do
88 local frag_width
= App
.width(frag
)
89 --? print('-- frag:', frag, pos, x, frag_width, State.width)
90 while x
+ frag_width
> State
.width
do
91 --? print('frag:', frag, pos, x, frag_width, State.width)
92 if x
< 0.8 * State
.width
then
93 -- long word; chop it at some letter
94 -- We're not going to reimplement TeX here.
95 local bpos
= Text
.nearest_pos_less_than(frag
, State
.width
- x
)
96 -- everything works if bpos == 0, but is a little inefficient
98 local boffset
= Text
.offset(frag
, bpos
+1) -- byte _after_ bpos
99 frag
= string.sub(frag
, boffset
)
101 --? print('after chop:', frag)
103 frag_width
= App
.width(frag
)
105 --? print('screen line:', pos)
106 table.insert(line_cache
.screen_line_starting_pos
, pos
)
107 x
= 0 -- new screen line
110 pos
= pos
+ utf8
.len(frag
)
114 function Text
.text_input(State
, t
)
115 if App
.mouse_down(1) then return end
116 if App
.ctrl_down() or App
.alt_down() or App
.cmd_down() then return end
117 local before
= snapshot(State
, State
.cursor1
.line
)
118 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
119 Text
.insert_at_cursor(State
, t
)
120 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
121 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
122 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
124 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
127 function Text
.insert_at_cursor(State
, t
)
128 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
129 local byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
130 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_offset
-1)..t
..string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_offset
)
131 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
132 State
.cursor1
.pos
= State
.cursor1
.pos
+1
135 -- Don't handle any keys here that would trigger text_input above.
136 function Text
.keychord_press(State
, chord
)
137 --? print('chord', chord, State.selection1.line, State.selection1.pos)
138 --== shortcuts that mutate text
139 if chord
== 'return' then
140 local before_line
= State
.cursor1
.line
141 local before
= snapshot(State
, before_line
)
142 Text
.insert_return(State
)
143 State
.selection1
= {}
144 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
145 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
148 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
149 elseif chord
== 'tab' then
150 local before
= snapshot(State
, State
.cursor1
.line
)
151 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
152 Text
.insert_at_cursor(State
, '\t')
153 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
154 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
155 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
156 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
159 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
160 elseif chord
== 'backspace' then
161 if State
.selection1
.line
then
162 Text
.delete_selection(State
, State
.left
, State
.right
)
167 if State
.cursor1
.pos
> 1 then
168 before
= snapshot(State
, State
.cursor1
.line
)
169 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1)
170 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
173 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)..string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_end
)
175 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
177 State
.cursor1
.pos
= State
.cursor1
.pos
-1
179 elseif State
.cursor1
.line
> 1 then
180 before
= snapshot(State
, State
.cursor1
.line
-1, State
.cursor1
.line
)
181 if State
.lines
[State
.cursor1
.line
-1].mode
== 'drawing' then
182 table.remove(State
.lines
, State
.cursor1
.line
-1)
183 table.remove(State
.line_cache
, State
.cursor1
.line
-1)
186 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
-1].data
)+1
187 State
.lines
[State
.cursor1
.line
-1].data
= State
.lines
[State
.cursor1
.line
-1].data
..State
.lines
[State
.cursor1
.line
].data
188 table.remove(State
.lines
, State
.cursor1
.line
)
189 table.remove(State
.line_cache
, State
.cursor1
.line
)
191 State
.cursor1
.line
= State
.cursor1
.line
-1
193 if State
.screen_top1
.line
> #State
.lines
then
194 Text
.populate_screen_line_starting_pos(State
, #State
.lines
)
195 local line_cache
= State
.line_cache
[#State
.line_cache
]
196 State
.screen_top1
= {line
=#State
.lines
, pos
=line_cache
.screen_line_starting_pos
[#line_cache
.screen_line_starting_pos
]}
197 elseif Text
.lt1(State
.cursor1
, State
.screen_top1
) then
198 State
.screen_top1
= {
199 line
=State
.cursor1
.line
,
200 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
202 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
204 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
205 assert(Text
.le1(State
.screen_top1
, State
.cursor1
))
207 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
208 elseif chord
== 'delete' then
209 if State
.selection1
.line
then
210 Text
.delete_selection(State
, State
.left
, State
.right
)
215 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
216 before
= snapshot(State
, State
.cursor1
.line
)
218 before
= snapshot(State
, State
.cursor1
.line
, State
.cursor1
.line
+1)
220 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
221 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
222 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
+1)
225 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)..string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_end
)
227 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
229 -- no change to State.cursor1.pos
231 elseif State
.cursor1
.line
< #State
.lines
then
232 if State
.lines
[State
.cursor1
.line
+1].mode
== 'text' then
234 State
.lines
[State
.cursor1
.line
].data
= State
.lines
[State
.cursor1
.line
].data
..State
.lines
[State
.cursor1
.line
+1].data
236 table.remove(State
.lines
, State
.cursor1
.line
+1)
237 table.remove(State
.line_cache
, State
.cursor1
.line
+1)
239 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
241 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
242 --== shortcuts that move the cursor
243 elseif chord
== 'left' then
245 State
.selection1
= {}
246 elseif chord
== 'right' then
248 State
.selection1
= {}
249 elseif chord
== 'S-left' then
250 if State
.selection1
.line
== nil then
251 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
254 elseif chord
== 'S-right' then
255 if State
.selection1
.line
== nil then
256 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
259 -- C- hotkeys reserved for drawings, so we'll use M-
260 elseif chord
== 'M-left' then
261 Text
.word_left(State
)
262 State
.selection1
= {}
263 elseif chord
== 'M-right' then
264 Text
.word_right(State
)
265 State
.selection1
= {}
266 elseif chord
== 'M-S-left' then
267 if State
.selection1
.line
== nil then
268 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
270 Text
.word_left(State
)
271 elseif chord
== 'M-S-right' then
272 if State
.selection1
.line
== nil then
273 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
275 Text
.word_right(State
)
276 elseif chord
== 'home' then
277 Text
.start_of_line(State
)
278 State
.selection1
= {}
279 elseif chord
== 'end' then
280 Text
.end_of_line(State
)
281 State
.selection1
= {}
282 elseif chord
== 'S-home' then
283 if State
.selection1
.line
== nil then
284 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
286 Text
.start_of_line(State
)
287 elseif chord
== 'S-end' then
288 if State
.selection1
.line
== nil then
289 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
291 Text
.end_of_line(State
)
292 elseif chord
== 'up' then
294 State
.selection1
= {}
295 elseif chord
== 'down' then
297 State
.selection1
= {}
298 elseif chord
== 'S-up' then
299 if State
.selection1
.line
== nil then
300 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
303 elseif chord
== 'S-down' then
304 if State
.selection1
.line
== nil then
305 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
308 elseif chord
== 'pageup' then
310 State
.selection1
= {}
311 elseif chord
== 'pagedown' then
313 State
.selection1
= {}
314 elseif chord
== 'S-pageup' then
315 if State
.selection1
.line
== nil then
316 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
319 elseif chord
== 'S-pagedown' then
320 if State
.selection1
.line
== nil then
321 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
327 function Text
.insert_return(State
)
328 local byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
329 table.insert(State
.lines
, State
.cursor1
.line
+1, {mode
='text', data
=string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_offset
)})
330 table.insert(State
.line_cache
, State
.cursor1
.line
+1, {})
331 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_offset
-1)
332 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
333 State
.cursor1
= {line
=State
.cursor1
.line
+1, pos
=1}
336 function Text
.pageup(State
)
338 -- duplicate some logic from love.draw
339 local top2
= Text
.to2(State
, State
.screen_top1
)
340 --? print(App.screen.height)
341 local y
= App
.screen
.height
- State
.line_height
342 while y
>= State
.top
do
343 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
344 if State
.screen_top1
.line
== 1 and State
.screen_top1
.pos
== 1 then break end
345 if State
.lines
[State
.screen_top1
.line
].mode
== 'text' then
346 y
= y
- State
.line_height
347 elseif State
.lines
[State
.screen_top1
.line
].mode
== 'drawing' then
348 y
= y
- Drawing_padding_height
- Drawing
.pixels(State
.lines
[State
.screen_top1
.line
].h
, State
.width
)
350 top2
= Text
.previous_screen_line(State
, top2
)
352 State
.screen_top1
= Text
.to1(State
, top2
)
353 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
354 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
355 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
356 --? print('pageup end')
359 function Text
.pagedown(State
)
360 --? print('pagedown')
361 -- If a line/paragraph gets to a page boundary, I often want to scroll
362 -- before I get to the bottom.
363 -- However, only do this if it makes forward progress.
364 local bot2
= Text
.to2(State
, State
.screen_bottom1
)
365 if bot2
.screen_line
> 1 then
366 bot2
.screen_line
= math
.max(bot2
.screen_line
-10, 1)
368 local new_top1
= Text
.to1(State
, bot2
)
369 if Text
.lt1(State
.screen_top1
, new_top1
) then
370 State
.screen_top1
= new_top1
372 State
.screen_top1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
374 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
375 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
376 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
377 --? print('top now', State.screen_top1.line)
378 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
379 --? print('pagedown end')
382 function Text
.up(State
)
383 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
384 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
385 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
386 if screen_line_starting_pos
== 1 then
387 --? print('cursor is at first screen line of its line')
388 -- line is done; skip to previous text line
389 local new_cursor_line
= State
.cursor1
.line
390 while new_cursor_line
> 1 do
391 new_cursor_line
= new_cursor_line
-1
392 if State
.lines
[new_cursor_line
].mode
== 'text' then
393 --? print('found previous text line')
394 State
.cursor1
= {line
=new_cursor_line
, pos
=nil}
395 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
396 -- previous text line found, pick its final screen line
397 --? print('has multiple screen lines')
398 local screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
399 --? print(#screen_line_starting_pos)
400 screen_line_starting_pos
= screen_line_starting_pos
[#screen_line_starting_pos
]
401 local screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_pos
)
402 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_byte_offset
)
403 State
.cursor1
.pos
= screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
408 -- move up one screen line in current line
409 assert(screen_line_index
> 1)
410 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
-1]
411 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
412 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
413 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
414 --? print('cursor pos is now '..tostring(State.cursor1.pos))
416 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
417 State
.screen_top1
= {
418 line
=State
.cursor1
.line
,
419 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
421 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
425 function Text
.down(State
)
426 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
427 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
428 if Text
.cursor_at_final_screen_line(State
) then
429 -- line is done, skip to next text line
430 --? print('cursor at final screen line of its line')
431 local new_cursor_line
= State
.cursor1
.line
432 while new_cursor_line
< #State
.lines
do
433 new_cursor_line
= new_cursor_line
+1
434 if State
.lines
[new_cursor_line
].mode
== 'text' then
436 line
= new_cursor_line
,
437 pos
= Text
.nearest_cursor_pos(State
.lines
[new_cursor_line
].data
, State
.cursor_x
, State
.left
),
439 --? print(State.cursor1.pos)
443 if State
.cursor1
.line
> State
.screen_bottom1
.line
then
444 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
445 --? print('scroll up preserving cursor')
446 Text
.snap_cursor_to_bottom_of_screen(State
)
447 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
450 -- move down one screen line in current line
451 local scroll_down
= Text
.le1(State
.screen_bottom1
, State
.cursor1
)
452 --? print('cursor is NOT at final screen line of its line')
453 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
454 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
455 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
+1]
456 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
457 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
458 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
459 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
460 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
462 --? print('scroll up preserving cursor')
463 Text
.snap_cursor_to_bottom_of_screen(State
)
464 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
467 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
470 function Text
.start_of_line(State
)
471 State
.cursor1
.pos
= 1
472 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
473 State
.screen_top1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
} -- copy
477 function Text
.end_of_line(State
)
478 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) + 1
479 if Text
.cursor_out_of_screen(State
) then
480 Text
.snap_cursor_to_bottom_of_screen(State
)
484 function Text
.word_left(State
)
485 -- skip some whitespace
487 if State
.cursor1
.pos
== 1 then
490 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%S') then
495 -- skip some non-whitespace
498 if State
.cursor1
.pos
== 1 then
501 assert(State
.cursor1
.pos
> 1)
502 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%s') then
508 function Text
.word_right(State
)
509 -- skip some whitespace
511 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
514 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%S') then
517 Text
.right_without_scroll(State
)
520 Text
.right_without_scroll(State
)
521 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
524 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%s') then
528 if Text
.cursor_out_of_screen(State
) then
529 Text
.snap_cursor_to_bottom_of_screen(State
)
533 function Text
.match(s
, pos
, pat
)
534 local start_offset
= Text
.offset(s
, pos
)
536 local end_offset
= Text
.offset(s
, pos
+1)
537 assert(end_offset
> start_offset
)
538 local curr
= s
:sub(start_offset
, end_offset
-1)
539 return curr
:match(pat
)
542 function Text
.left(State
)
543 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
544 if State
.cursor1
.pos
> 1 then
545 State
.cursor1
.pos
= State
.cursor1
.pos
-1
547 local new_cursor_line
= State
.cursor1
.line
548 while new_cursor_line
> 1 do
549 new_cursor_line
= new_cursor_line
-1
550 if State
.lines
[new_cursor_line
].mode
== 'text' then
552 line
= new_cursor_line
,
553 pos
= utf8
.len(State
.lines
[new_cursor_line
].data
) + 1,
559 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
560 State
.screen_top1
= {
561 line
=State
.cursor1
.line
,
562 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
564 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
568 function Text
.right(State
)
569 Text
.right_without_scroll(State
)
570 if Text
.cursor_out_of_screen(State
) then
571 Text
.snap_cursor_to_bottom_of_screen(State
)
575 function Text
.right_without_scroll(State
)
576 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
577 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
578 State
.cursor1
.pos
= State
.cursor1
.pos
+1
580 local new_cursor_line
= State
.cursor1
.line
581 while new_cursor_line
<= #State
.lines
-1 do
582 new_cursor_line
= new_cursor_line
+1
583 if State
.lines
[new_cursor_line
].mode
== 'text' then
584 State
.cursor1
= {line
=new_cursor_line
, pos
=1}
591 function Text
.pos_at_start_of_screen_line(State
, loc1
)
592 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
593 local line_cache
= State
.line_cache
[loc1
.line
]
594 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
595 local spos
= line_cache
.screen_line_starting_pos
[i
]
596 if spos
<= loc1
.pos
then
603 function Text
.cursor_at_final_screen_line(State
)
604 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
605 local screen_lines
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
606 --? print(screen_lines[#screen_lines], State.cursor1.pos)
607 return screen_lines
[#screen_lines
] <= State
.cursor1
.pos
610 function Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
612 while State
.cursor1
.line
<= #State
.lines
do
613 if State
.lines
[State
.cursor1
.line
].mode
== 'text' then
616 --? print('cursor skips', State.cursor1.line)
617 y
= y
+ Drawing_padding_height
+ Drawing
.pixels(State
.lines
[State
.cursor1
.line
].h
, State
.width
)
618 State
.cursor1
.line
= State
.cursor1
.line
+ 1
620 -- hack: insert a text line at bottom of file if necessary
621 if State
.cursor1
.line
> #State
.lines
then
622 assert(State
.cursor1
.line
== #State
.lines
+1)
623 table.insert(State
.lines
, {mode
='text', data
=''})
624 table.insert(State
.line_cache
, {})
626 --? print(y, App.screen.height, App.screen.height-State.line_height)
627 if y
> App
.screen
.height
- State
.line_height
then
628 --? print('scroll up')
629 Text
.snap_cursor_to_bottom_of_screen(State
)
633 -- should never modify State.cursor1
634 function Text
.snap_cursor_to_bottom_of_screen(State
)
635 --? print('to2:', State.cursor1.line, State.cursor1.pos)
636 local top2
= Text
.to2(State
, State
.cursor1
)
637 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
638 -- slide to start of screen line
639 top2
.screen_pos
= 1 -- start of screen line
640 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
641 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
642 local y
= App
.screen
.height
- State
.line_height
643 -- duplicate some logic from love.draw
645 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
646 if top2
.line
== 1 and top2
.screen_line
== 1 then break end
647 if top2
.screen_line
> 1 or State
.lines
[top2
.line
-1].mode
== 'text' then
648 local h
= State
.line_height
649 if y
- h
< State
.top
then
654 assert(top2
.line
> 1)
655 assert(State
.lines
[top2
.line
-1].mode
== 'drawing')
656 -- We currently can't draw partial drawings, so either skip it entirely
658 local h
= Drawing_padding_height
+ Drawing
.pixels(State
.lines
[top2
.line
-1].h
, State
.width
)
659 if y
- h
< State
.top
then
662 --? print('skipping drawing of height', h)
665 top2
= Text
.previous_screen_line(State
, top2
)
667 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
668 State
.screen_top1
= Text
.to1(State
, top2
)
669 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
670 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
671 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
674 function Text
.in_line(State
, line_index
, x
,y
)
675 local line
= State
.lines
[line_index
]
676 local line_cache
= State
.line_cache
[line_index
]
677 if line_cache
.starty
== nil then return false end -- outside current page
678 if y
< line_cache
.starty
then return false end
679 Text
.populate_screen_line_starting_pos(State
, line_index
)
680 return y
< line_cache
.starty
+ State
.line_height
*(#line_cache
.screen_line_starting_pos
- Text
.screen_line_index(line_cache
.screen_line_starting_pos
, line_cache
.startpos
) + 1)
683 -- convert mx,my in pixels to schema-1 coordinates
684 function Text
.to_pos_on_line(State
, line_index
, mx
, my
)
685 local line
= State
.lines
[line_index
]
686 local line_cache
= State
.line_cache
[line_index
]
687 assert(my
>= line_cache
.starty
)
688 -- duplicate some logic from Text.draw
689 local y
= line_cache
.starty
690 local start_screen_line_index
= Text
.screen_line_index(line_cache
.screen_line_starting_pos
, line_cache
.startpos
)
691 for screen_line_index
= start_screen_line_index
,#line_cache
.screen_line_starting_pos
do
692 local screen_line_starting_pos
= line_cache
.screen_line_starting_pos
[screen_line_index
]
693 local screen_line_starting_byte_offset
= Text
.offset(line
.data
, screen_line_starting_pos
)
694 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
695 local nexty
= y
+ State
.line_height
697 -- On all wrapped screen lines but the final one, clicks past end of
698 -- line position cursor on final character of screen line.
699 -- (The final screen line positions past end of screen line as always.)
700 if screen_line_index
< #line_cache
.screen_line_starting_pos
and mx
> State
.left
+ Text
.screen_line_width(State
, line_index
, screen_line_index
) then
701 --? print('past end of non-final line; return')
702 return line_cache
.screen_line_starting_pos
[screen_line_index
+1]-1
704 local s
= string.sub(line
.data
, screen_line_starting_byte_offset
)
705 --? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
706 return screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, mx
, State
.left
) - 1
713 function Text
.screen_line_width(State
, line_index
, i
)
714 local line
= State
.lines
[line_index
]
715 local line_cache
= State
.line_cache
[line_index
]
716 local start_pos
= line_cache
.screen_line_starting_pos
[i
]
717 local start_offset
= Text
.offset(line
.data
, start_pos
)
719 if i
< #line_cache
.screen_line_starting_pos
then
720 local past_end_pos
= line_cache
.screen_line_starting_pos
[i
+1]
721 local past_end_offset
= Text
.offset(line
.data
, past_end_pos
)
722 screen_line
= string.sub(line
.data
, start_offset
, past_end_offset
-1)
724 screen_line
= string.sub(line
.data
, start_pos
)
726 return App
.width(screen_line
)
729 function Text
.screen_line_index(screen_line_starting_pos
, pos
)
730 for i
= #screen_line_starting_pos
,1,-1 do
731 if screen_line_starting_pos
[i
] <= pos
then
737 -- convert x pixel coordinate to pos
738 -- oblivious to wrapping
739 -- result: 1 to len+1
740 function Text
.nearest_cursor_pos(line
, x
, left
)
744 local len
= utf8
.len(line
)
745 local max_x
= left
+Text
.x(line
, len
+1)
749 local leftpos
, rightpos
= 1, len
+1
750 --? print('-- nearest', x)
752 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
753 if leftpos
== rightpos
then
756 local curr
= math
.floor((leftpos
+rightpos
)/2)
757 local currxmin
= left
+Text
.x(line
, curr
)
758 local currxmax
= left
+Text
.x(line
, curr
+1)
759 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
760 if currxmin
<= x
and x
< currxmax
then
761 if x
-currxmin
< currxmax
-x
then
767 if leftpos
>= rightpos
-1 then
779 -- return the nearest index of line (in utf8 code points) which lies entirely
780 -- within x pixels of the left margin
781 -- result: 0 to len+1
782 function Text
.nearest_pos_less_than(line
, x
)
783 --? print('', '-- nearest_pos_less_than', line, x)
784 local len
= utf8
.len(line
)
785 local max_x
= Text
.x_after(line
, len
)
789 local left
, right
= 0, len
+1
791 local curr
= math
.floor((left
+right
)/2)
792 local currxmin
= Text
.x_after(line
, curr
+1)
793 local currxmax
= Text
.x_after(line
, curr
+2)
794 --? print('', x, left, right, curr, currxmin, currxmax)
795 if currxmin
<= x
and x
< currxmax
then
798 if left
>= right
-1 then
810 function Text
.x_after(s
, pos
)
811 local offset
= Text
.offset(s
, math
.min(pos
+1, #s
+1))
812 local s_before
= s
:sub(1, offset
-1)
813 --? print('^'..s_before..'$')
814 return App
.width(s_before
)
817 function Text
.x(s
, pos
)
818 local offset
= Text
.offset(s
, pos
)
819 local s_before
= s
:sub(1, offset
-1)
820 return App
.width(s_before
)
823 function Text
.to2(State
, loc1
)
824 if State
.lines
[loc1
.line
].mode
== 'drawing' then
825 return {line
=loc1
.line
, screen_line
=1, screen_pos
=1}
827 local result
= {line
=loc1
.line
}
828 local line_cache
= State
.line_cache
[loc1
.line
]
829 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
830 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
831 local spos
= line_cache
.screen_line_starting_pos
[i
]
832 if spos
<= loc1
.pos
then
833 result
.screen_line
= i
834 result
.screen_pos
= loc1
.pos
- spos
+ 1
838 assert(result
.screen_pos
)
842 function Text
.to1(State
, loc2
)
843 local result
= {line
=loc2
.line
, pos
=loc2
.screen_pos
}
844 if loc2
.screen_line
> 1 then
845 result
.pos
= State
.line_cache
[loc2
.line
].screen_line_starting_pos
[loc2
.screen_line
] + loc2
.screen_pos
- 1
850 function Text
.eq1(a
, b
)
851 return a
.line
== b
.line
and a
.pos
== b
.pos
854 function Text
.lt1(a
, b
)
855 if a
.line
< b
.line
then
858 if a
.line
> b
.line
then
864 function Text
.le1(a
, b
)
865 if a
.line
< b
.line
then
868 if a
.line
> b
.line
then
871 return a
.pos
<= b
.pos
874 function Text
.offset(s
, pos1
)
875 if pos1
== 1 then return 1 end
876 local result
= utf8
.offset(s
, pos1
)
877 if result
== nil then
884 function Text
.previous_screen_line(State
, loc2
)
885 if loc2
.screen_line
> 1 then
886 return {line
=loc2
.line
, screen_line
=loc2
.screen_line
-1, screen_pos
=1}
887 elseif loc2
.line
== 1 then
889 elseif State
.lines
[loc2
.line
-1].mode
== 'drawing' then
890 return {line
=loc2
.line
-1, screen_line
=1, screen_pos
=1}
892 local l
= State
.lines
[loc2
.line
-1]
893 Text
.populate_screen_line_starting_pos(State
, loc2
.line
-1)
894 return {line
=loc2
.line
-1, screen_line
=#State
.line_cache
[loc2
.line
-1].screen_line_starting_pos
, screen_pos
=1}
899 function Text
.tweak_screen_top_and_cursor(State
)
900 if State
.screen_top1
.pos
== 1 then return end
901 Text
.populate_screen_line_starting_pos(State
, State
.screen_top1
.line
)
902 local line
= State
.lines
[State
.screen_top1
.line
]
903 local line_cache
= State
.line_cache
[State
.screen_top1
.line
]
904 for i
=2,#line_cache
.screen_line_starting_pos
do
905 local pos
= line_cache
.screen_line_starting_pos
[i
]
906 if pos
== State
.screen_top1
.pos
then
909 if pos
> State
.screen_top1
.pos
then
910 -- make sure screen top is at start of a screen line
911 local prev
= line_cache
.screen_line_starting_pos
[i
-1]
912 if State
.screen_top1
.pos
- prev
< pos
- State
.screen_top1
.pos
then
913 State
.screen_top1
.pos
= prev
915 State
.screen_top1
.pos
= pos
920 -- make sure cursor is on screen
921 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
922 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
923 elseif State
.cursor1
.line
>= State
.screen_bottom1
.line
then
925 if Text
.cursor_out_of_screen(State
) then
928 line
=State
.screen_bottom1
.line
,
929 pos
=Text
.to_pos_on_line(State
, State
.screen_bottom1
.line
, State
.right
-5, App
.screen
.height
-5),
935 -- slightly expensive since it redraws the screen
936 function Text
.cursor_out_of_screen(State
)
938 return State
.cursor_y
== nil
939 -- this approach is cheaper and almost works, except on the final screen
940 -- where file ends above bottom of screen
941 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
942 --? local botline1 = {line=State.cursor1.line, pos=botpos}
943 --? return Text.lt1(State.screen_bottom1, botline1)
946 function Text
.redraw_all(State
)
947 --? print('clearing fragments')
948 State
.line_cache
= {}
949 for i
=1,#State
.lines
do
950 State
.line_cache
[i
] = {}
954 function Text
.clear_screen_line_cache(State
, line_index
)
955 State
.line_cache
[line_index
].screen_line_starting_pos
= nil
959 return s
:gsub('^%s+', ''):gsub('%s+$', '')
963 return s
:gsub('^%s+', '')
967 return s
:gsub('%s+$', '')
970 function starts_with(s
, prefix
)
975 if s
:sub(i
,i
) ~= prefix
:sub(i
,i
) then
982 function ends_with(s
, suffix
)
987 if s
:sub(#s
-i
,#s
-i
) ~= suffix
:sub(#suffix
-i
,#suffix
-i
) then