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 the final y, and position of start of final screen line drawn
6 function Text
.draw(State
, line_index
, y
, startpos
)
7 local line
= State
.lines
[line_index
]
8 local line_cache
= State
.line_cache
[line_index
]
10 line_cache
.startpos
= startpos
14 local screen_line_starting_pos
= startpos
15 Text
.compute_fragments(State
, line_index
)
16 for _
, f
in ipairs(line_cache
.fragments
) do
18 local frag
, frag_text
= f
.data
, f
.text
19 local frag_len
= utf8
.len(frag
)
20 --? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y)
21 if pos
< startpos
then
23 --? print('skipping', frag)
26 local frag_width
= App
.width(frag_text
)
27 if x
+ frag_width
> State
.right
then
28 assert(x
> State
.left
) -- no overfull lines
29 y
= y
+ State
.line_height
30 if y
+ State
.line_height
> App
.screen
.height
then
31 return y
, screen_line_starting_pos
33 screen_line_starting_pos
= pos
36 if State
.selection1
.line
then
37 local lo
, hi
= Text
.clip_selection(State
, line_index
, pos
, pos
+frag_len
)
38 Text
.draw_highlight(State
, line
, x
,y
, pos
, lo
,hi
)
40 App
.screen
.draw(frag_text
, x
,y
)
41 -- render cursor if necessary
42 if line_index
== State
.cursor1
.line
then
43 if pos
<= State
.cursor1
.pos
and pos
+ frag_len
> State
.cursor1
.pos
then
44 if State
.search_term
then
45 if State
.lines
[State
.cursor1
.line
].data
:sub(State
.cursor1
.pos
, State
.cursor1
.pos
+utf8
.len(State
.search_term
)-1) == State
.search_term
then
46 local lo_px
= Text
.draw_highlight(State
, line
, x
,y
, pos
, State
.cursor1
.pos
, State
.cursor1
.pos
+utf8
.len(State
.search_term
))
48 love
.graphics
.print(State
.search_term
, x
+lo_px
,y
)
51 Text
.draw_cursor(State
, x
+Text
.x(frag
, State
.cursor1
.pos
-pos
+1), y
)
59 if State
.search_term
== nil then
60 if line_index
== State
.cursor1
.line
and State
.cursor1
.pos
== pos
then
61 Text
.draw_cursor(State
, x
, y
)
64 return y
, screen_line_starting_pos
67 function Text
.draw_cursor(State
, x
, y
)
69 if math
.floor(Cursor_time
*2)%2 == 0 then
70 App
.color(Cursor_color
)
71 love
.graphics
.rectangle('fill', x
,y
, 3,State
.line_height
)
75 State
.cursor_y
= y
+State
.line_height
78 function Text
.populate_screen_line_starting_pos(State
, line_index
)
79 local line
= State
.lines
[line_index
]
80 if line
.mode
~= 'text' then return end
81 local line_cache
= State
.line_cache
[line_index
]
82 if line_cache
.screen_line_starting_pos
then
85 -- duplicate some logic from Text.draw
86 Text
.compute_fragments(State
, line_index
)
87 line_cache
.screen_line_starting_pos
= {1}
90 for _
, f
in ipairs(line_cache
.fragments
) do
91 local frag
, frag_text
= f
.data
, f
.text
93 local frag_width
= App
.width(frag_text
)
94 if x
+ frag_width
> State
.right
then
96 table.insert(line_cache
.screen_line_starting_pos
, pos
)
99 local frag_len
= utf8
.len(frag
)
104 function Text
.compute_fragments(State
, line_index
)
105 --? print('compute_fragments', line_index, 'between', State.left, State.right)
106 local line
= State
.lines
[line_index
]
107 if line
.mode
~= 'text' then return end
108 local line_cache
= State
.line_cache
[line_index
]
109 if line_cache
.fragments
then
112 line_cache
.fragments
= {}
114 -- try to wrap at word boundaries
115 for frag
in line
.data
:gmatch('%S*%s*') do
116 local frag_text
= App
.newText(love
.graphics
.getFont(), frag
)
117 local frag_width
= App
.width(frag_text
)
118 --? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go')
119 while x
+ frag_width
> State
.right
do
120 --? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x))
121 if (x
-State
.left
) < 0.8 * (State
.right
-State
.left
) then
122 --? print('splitting')
123 -- long word; chop it at some letter
124 -- We're not going to reimplement TeX here.
125 local bpos
= Text
.nearest_pos_less_than(frag
, State
.right
- x
)
126 --? print('bpos', bpos)
127 if bpos
== 0 then break end -- avoid infinite loop when window is too narrow
128 local boffset
= Text
.offset(frag
, bpos
+1) -- byte _after_ bpos
129 --? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes')
130 local frag1
= string.sub(frag
, 1, boffset
-1)
131 local frag1_text
= App
.newText(love
.graphics
.getFont(), frag1
)
132 local frag1_width
= App
.width(frag1_text
)
133 --? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px')
134 assert(x
+ frag1_width
<= State
.right
)
135 table.insert(line_cache
.fragments
, {data
=frag1
, text
=frag1_text
})
136 frag
= string.sub(frag
, boffset
)
137 frag_text
= App
.newText(love
.graphics
.getFont(), frag
)
138 frag_width
= App
.width(frag_text
)
140 x
= State
.left
-- new line
143 --? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px')
144 table.insert(line_cache
.fragments
, {data
=frag
, text
=frag_text
})
150 function Text
.textinput(State
, t
)
151 if App
.mouse_down(1) then return end
152 if App
.ctrl_down() or App
.alt_down() or App
.cmd_down() then return end
153 local before
= snapshot(State
, State
.cursor1
.line
)
154 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
155 Text
.insert_at_cursor(State
, t
)
156 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
157 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
158 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
159 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
161 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
164 function Text
.insert_at_cursor(State
, t
)
165 local byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
166 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
)
167 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
168 State
.cursor1
.pos
= State
.cursor1
.pos
+1
171 -- Don't handle any keys here that would trigger love.textinput above.
172 function Text
.keychord_pressed(State
, chord
)
173 --? print('chord', chord, State.selection1.line, State.selection1.pos)
174 --== shortcuts that mutate text
175 if chord
== 'return' then
176 local before_line
= State
.cursor1
.line
177 local before
= snapshot(State
, before_line
)
178 Text
.insert_return(State
)
179 State
.selection1
= {}
180 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
181 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
184 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
185 elseif chord
== 'tab' then
186 local before
= snapshot(State
, State
.cursor1
.line
)
187 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
188 Text
.insert_at_cursor(State
, '\t')
189 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
190 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
191 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
192 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
195 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
196 elseif chord
== 'backspace' then
197 if State
.selection1
.line
then
198 Text
.delete_selection(State
, State
.left
, State
.right
)
203 if State
.cursor1
.pos
> 1 then
204 before
= snapshot(State
, State
.cursor1
.line
)
205 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1)
206 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
209 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
)
211 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
213 State
.cursor1
.pos
= State
.cursor1
.pos
-1
215 elseif State
.cursor1
.line
> 1 then
216 before
= snapshot(State
, State
.cursor1
.line
-1, State
.cursor1
.line
)
217 if State
.lines
[State
.cursor1
.line
-1].mode
== 'drawing' then
218 table.remove(State
.lines
, State
.cursor1
.line
-1)
219 table.remove(State
.line_cache
, State
.cursor1
.line
-1)
222 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
-1].data
)+1
223 State
.lines
[State
.cursor1
.line
-1].data
= State
.lines
[State
.cursor1
.line
-1].data
..State
.lines
[State
.cursor1
.line
].data
224 table.remove(State
.lines
, State
.cursor1
.line
)
225 table.remove(State
.line_cache
, State
.cursor1
.line
)
227 State
.cursor1
.line
= State
.cursor1
.line
-1
229 if State
.screen_top1
.line
> #State
.lines
then
230 Text
.populate_screen_line_starting_pos(State
, #State
.lines
)
231 local line_cache
= State
.line_cache
[#State
.line_cache
]
232 State
.screen_top1
= {line
=#State
.lines
, pos
=line_cache
.screen_line_starting_pos
[#line_cache
.screen_line_starting_pos
]}
233 elseif Text
.lt1(State
.cursor1
, State
.screen_top1
) then
234 local top2
= Text
.to2(State
, State
.screen_top1
)
235 top2
= Text
.previous_screen_line(State
, top2
, State
.left
, State
.right
)
236 State
.screen_top1
= Text
.to1(State
, top2
)
237 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
239 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
240 assert(Text
.le1(State
.screen_top1
, State
.cursor1
))
242 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
243 elseif chord
== 'delete' then
244 if State
.selection1
.line
then
245 Text
.delete_selection(State
, State
.left
, State
.right
)
250 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
251 before
= snapshot(State
, State
.cursor1
.line
)
253 before
= snapshot(State
, State
.cursor1
.line
, State
.cursor1
.line
+1)
255 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
256 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
257 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
+1)
260 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
)
262 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
264 -- no change to State.cursor1.pos
266 elseif State
.cursor1
.line
< #State
.lines
then
267 if State
.lines
[State
.cursor1
.line
+1].mode
== 'text' then
269 State
.lines
[State
.cursor1
.line
].data
= State
.lines
[State
.cursor1
.line
].data
..State
.lines
[State
.cursor1
.line
+1].data
271 table.remove(State
.lines
, State
.cursor1
.line
+1)
272 table.remove(State
.line_cache
, State
.cursor1
.line
+1)
274 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
276 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
277 --== shortcuts that move the cursor
278 elseif chord
== 'left' then
280 State
.selection1
= {}
281 elseif chord
== 'right' then
283 State
.selection1
= {}
284 elseif chord
== 'S-left' then
285 if State
.selection1
.line
== nil then
286 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
289 elseif chord
== 'S-right' then
290 if State
.selection1
.line
== nil then
291 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
294 -- C- hotkeys reserved for drawings, so we'll use M-
295 elseif chord
== 'M-left' then
296 Text
.word_left(State
)
297 State
.selection1
= {}
298 elseif chord
== 'M-right' then
299 Text
.word_right(State
)
300 State
.selection1
= {}
301 elseif chord
== 'M-S-left' then
302 if State
.selection1
.line
== nil then
303 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
305 Text
.word_left(State
)
306 elseif chord
== 'M-S-right' then
307 if State
.selection1
.line
== nil then
308 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
310 Text
.word_right(State
)
311 elseif chord
== 'home' then
312 Text
.start_of_line(State
)
313 State
.selection1
= {}
314 elseif chord
== 'end' then
315 Text
.end_of_line(State
)
316 State
.selection1
= {}
317 elseif chord
== 'S-home' then
318 if State
.selection1
.line
== nil then
319 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
321 Text
.start_of_line(State
)
322 elseif chord
== 'S-end' then
323 if State
.selection1
.line
== nil then
324 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
326 Text
.end_of_line(State
)
327 elseif chord
== 'up' then
329 State
.selection1
= {}
330 elseif chord
== 'down' then
332 State
.selection1
= {}
333 elseif chord
== 'S-up' then
334 if State
.selection1
.line
== nil then
335 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
338 elseif chord
== 'S-down' then
339 if State
.selection1
.line
== nil then
340 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
343 elseif chord
== 'pageup' then
345 State
.selection1
= {}
346 elseif chord
== 'pagedown' then
348 State
.selection1
= {}
349 elseif chord
== 'S-pageup' then
350 if State
.selection1
.line
== nil then
351 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
354 elseif chord
== 'S-pagedown' then
355 if State
.selection1
.line
== nil then
356 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
362 function Text
.insert_return(State
)
363 local byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
364 table.insert(State
.lines
, State
.cursor1
.line
+1, {mode
='text', data
=string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_offset
)})
365 table.insert(State
.line_cache
, State
.cursor1
.line
+1, {})
366 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_offset
-1)
367 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
368 State
.cursor1
= {line
=State
.cursor1
.line
+1, pos
=1}
371 function Text
.pageup(State
)
373 -- duplicate some logic from love.draw
374 local top2
= Text
.to2(State
, State
.screen_top1
)
375 --? print(App.screen.height)
376 local y
= App
.screen
.height
- State
.line_height
377 while y
>= State
.top
do
378 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
379 if State
.screen_top1
.line
== 1 and State
.screen_top1
.pos
== 1 then break end
380 if State
.lines
[State
.screen_top1
.line
].mode
== 'text' then
381 y
= y
- State
.line_height
382 elseif State
.lines
[State
.screen_top1
.line
].mode
== 'drawing' then
383 y
= y
- Drawing_padding_height
- Drawing
.pixels(State
.lines
[State
.screen_top1
.line
].h
, State
.width
)
385 top2
= Text
.previous_screen_line(State
, top2
)
387 State
.screen_top1
= Text
.to1(State
, top2
)
388 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
389 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
390 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
391 --? print('pageup end')
394 function Text
.pagedown(State
)
395 --? print('pagedown')
396 -- If a line/paragraph gets to a page boundary, I often want to scroll
397 -- before I get to the bottom.
398 -- However, only do this if it makes forward progress.
399 local bot2
= Text
.to2(State
, State
.screen_bottom1
)
400 if bot2
.screen_line
> 1 then
401 bot2
.screen_line
= math
.max(bot2
.screen_line
-10, 1)
403 local new_top1
= Text
.to1(State
, bot2
)
404 if Text
.lt1(State
.screen_top1
, new_top1
) then
405 State
.screen_top1
= new_top1
407 State
.screen_top1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
409 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
410 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
411 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
412 --? print('top now', State.screen_top1.line)
413 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
414 --? print('pagedown end')
417 function Text
.up(State
)
418 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
419 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
420 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
421 if screen_line_starting_pos
== 1 then
422 --? print('cursor is at first screen line of its line')
423 -- line is done; skip to previous text line
424 local new_cursor_line
= State
.cursor1
.line
425 while new_cursor_line
> 1 do
426 new_cursor_line
= new_cursor_line
-1
427 if State
.lines
[new_cursor_line
].mode
== 'text' then
428 --? print('found previous text line')
429 State
.cursor1
= {line
=State
.cursor1
.line
-1, pos
=nil}
430 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
431 -- previous text line found, pick its final screen line
432 --? print('has multiple screen lines')
433 local screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
434 --? print(#screen_line_starting_pos)
435 screen_line_starting_pos
= screen_line_starting_pos
[#screen_line_starting_pos
]
436 local screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_pos
)
437 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_byte_offset
)
438 State
.cursor1
.pos
= screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
443 -- move up one screen line in current line
444 assert(screen_line_index
> 1)
445 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
-1]
446 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
447 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
448 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
449 --? print('cursor pos is now '..tostring(State.cursor1.pos))
451 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
452 local top2
= Text
.to2(State
, State
.screen_top1
)
453 top2
= Text
.previous_screen_line(State
, top2
)
454 State
.screen_top1
= Text
.to1(State
, top2
)
458 function Text
.down(State
)
459 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
460 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
461 if Text
.cursor_at_final_screen_line(State
) then
462 -- line is done, skip to next text line
463 --? print('cursor at final screen line of its line')
464 local new_cursor_line
= State
.cursor1
.line
465 while new_cursor_line
< #State
.lines
do
466 new_cursor_line
= new_cursor_line
+1
467 if State
.lines
[new_cursor_line
].mode
== 'text' then
469 line
= new_cursor_line
,
470 pos
= Text
.nearest_cursor_pos(State
.lines
[new_cursor_line
].data
, State
.cursor_x
, State
.left
),
472 --? print(State.cursor1.pos)
476 if State
.cursor1
.line
> State
.screen_bottom1
.line
then
477 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
478 --? print('scroll up preserving cursor')
479 Text
.snap_cursor_to_bottom_of_screen(State
)
480 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
483 -- move down one screen line in current line
484 local scroll_down
= Text
.le1(State
.screen_bottom1
, State
.cursor1
)
485 --? print('cursor is NOT at final screen line of its line')
486 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
487 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
488 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
+1]
489 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
490 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
491 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
492 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
493 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
495 --? print('scroll up preserving cursor')
496 Text
.snap_cursor_to_bottom_of_screen(State
)
497 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
500 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
503 function Text
.start_of_line(State
)
504 State
.cursor1
.pos
= 1
505 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
506 State
.screen_top1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
} -- copy
510 function Text
.end_of_line(State
)
511 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) + 1
512 if Text
.cursor_out_of_screen(State
) then
513 Text
.snap_cursor_to_bottom_of_screen(State
)
517 function Text
.word_left(State
)
518 -- skip some whitespace
520 if State
.cursor1
.pos
== 1 then
523 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%S') then
528 -- skip some non-whitespace
531 if State
.cursor1
.pos
== 1 then
534 assert(State
.cursor1
.pos
> 1)
535 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%s') then
541 function Text
.word_right(State
)
542 -- skip some whitespace
544 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
547 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%S') then
550 Text
.right_without_scroll(State
)
553 Text
.right_without_scroll(State
)
554 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
557 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%s') then
561 if Text
.cursor_out_of_screen(State
) then
562 Text
.snap_cursor_to_bottom_of_screen(State
)
566 function Text
.match(s
, pos
, pat
)
567 local start_offset
= Text
.offset(s
, pos
)
569 local end_offset
= Text
.offset(s
, pos
+1)
570 assert(end_offset
> start_offset
)
571 local curr
= s
:sub(start_offset
, end_offset
-1)
572 return curr
:match(pat
)
575 function Text
.left(State
)
576 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
577 if State
.cursor1
.pos
> 1 then
578 State
.cursor1
.pos
= State
.cursor1
.pos
-1
580 local new_cursor_line
= State
.cursor1
.line
581 while new_cursor_line
> 1 do
582 new_cursor_line
= new_cursor_line
-1
583 if State
.lines
[new_cursor_line
].mode
== 'text' then
585 line
= new_cursor_line
,
586 pos
= utf8
.len(State
.lines
[new_cursor_line
].data
) + 1,
592 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
593 local top2
= Text
.to2(State
, State
.screen_top1
)
594 top2
= Text
.previous_screen_line(State
, top2
)
595 State
.screen_top1
= Text
.to1(State
, top2
)
599 function Text
.right(State
)
600 Text
.right_without_scroll(State
)
601 if Text
.cursor_out_of_screen(State
) then
602 Text
.snap_cursor_to_bottom_of_screen(State
)
606 function Text
.right_without_scroll(State
)
607 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
608 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
609 State
.cursor1
.pos
= State
.cursor1
.pos
+1
611 local new_cursor_line
= State
.cursor1
.line
612 while new_cursor_line
<= #State
.lines
-1 do
613 new_cursor_line
= new_cursor_line
+1
614 if State
.lines
[new_cursor_line
].mode
== 'text' then
615 State
.cursor1
= {line
=new_cursor_line
, pos
=1}
622 function Text
.pos_at_start_of_screen_line(State
, loc1
)
623 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
624 local line_cache
= State
.line_cache
[loc1
.line
]
625 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
626 local spos
= line_cache
.screen_line_starting_pos
[i
]
627 if spos
<= loc1
.pos
then
634 function Text
.cursor_at_final_screen_line(State
)
635 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
636 local screen_lines
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
637 --? print(screen_lines[#screen_lines], State.cursor1.pos)
638 return screen_lines
[#screen_lines
] <= State
.cursor1
.pos
641 function Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
643 while State
.cursor1
.line
<= #State
.lines
do
644 if State
.lines
[State
.cursor1
.line
].mode
== 'text' then
647 --? print('cursor skips', State.cursor1.line)
648 y
= y
+ Drawing_padding_height
+ Drawing
.pixels(State
.lines
[State
.cursor1
.line
].h
, State
.width
)
649 State
.cursor1
.line
= State
.cursor1
.line
+ 1
651 -- hack: insert a text line at bottom of file if necessary
652 if State
.cursor1
.line
> #State
.lines
then
653 assert(State
.cursor1
.line
== #State
.lines
+1)
654 table.insert(State
.lines
, {mode
='text', data
=''})
655 table.insert(State
.line_cache
, {})
657 --? print(y, App.screen.height, App.screen.height-State.line_height)
658 if y
> App
.screen
.height
- State
.line_height
then
659 --? print('scroll up')
660 Text
.snap_cursor_to_bottom_of_screen(State
)
664 -- should never modify State.cursor1
665 function Text
.snap_cursor_to_bottom_of_screen(State
)
666 --? print('to2:', State.cursor1.line, State.cursor1.pos)
667 local top2
= Text
.to2(State
, State
.cursor1
)
668 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
669 -- slide to start of screen line
670 top2
.screen_pos
= 1 -- start of screen line
671 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.screen_top1.posB, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
672 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
673 local y
= App
.screen
.height
- State
.line_height
674 -- duplicate some logic from love.draw
676 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
677 if top2
.line
== 1 and top2
.screen_line
== 1 then break end
678 if top2
.screen_line
> 1 or State
.lines
[top2
.line
-1].mode
== 'text' then
679 local h
= State
.line_height
680 if y
- h
< State
.top
then
685 assert(top2
.line
> 1)
686 assert(State
.lines
[top2
.line
-1].mode
== 'drawing')
687 -- We currently can't draw partial drawings, so either skip it entirely
689 local h
= Drawing_padding_height
+ Drawing
.pixels(State
.lines
[top2
.line
-1].h
, State
.width
)
690 if y
- h
< State
.top
then
693 --? print('skipping drawing of height', h)
696 top2
= Text
.previous_screen_line(State
, top2
)
698 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
699 State
.screen_top1
= Text
.to1(State
, top2
)
700 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
701 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
702 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
705 function Text
.in_line(State
, line_index
, x
,y
)
706 local line
= State
.lines
[line_index
]
707 local line_cache
= State
.line_cache
[line_index
]
708 if line_cache
.starty
== nil then return false end -- outside current page
709 if y
< line_cache
.starty
then return false end
710 Text
.populate_screen_line_starting_pos(State
, line_index
)
711 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)
714 -- convert mx,my in pixels to schema-1 coordinates
715 function Text
.to_pos_on_line(State
, line_index
, mx
, my
)
716 local line
= State
.lines
[line_index
]
717 local line_cache
= State
.line_cache
[line_index
]
718 assert(my
>= line_cache
.starty
)
719 -- duplicate some logic from Text.draw
720 local y
= line_cache
.starty
721 local start_screen_line_index
= Text
.screen_line_index(line_cache
.screen_line_starting_pos
, line_cache
.startpos
)
722 for screen_line_index
= start_screen_line_index
,#line_cache
.screen_line_starting_pos
do
723 local screen_line_starting_pos
= line_cache
.screen_line_starting_pos
[screen_line_index
]
724 local screen_line_starting_byte_offset
= Text
.offset(line
.data
, screen_line_starting_pos
)
725 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
726 local nexty
= y
+ State
.line_height
728 -- On all wrapped screen lines but the final one, clicks past end of
729 -- line position cursor on final character of screen line.
730 -- (The final screen line positions past end of screen line as always.)
731 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
732 --? print('past end of non-final line; return')
733 return line_cache
.screen_line_starting_pos
[screen_line_index
+1]-1
735 local s
= string.sub(line
.data
, screen_line_starting_byte_offset
)
736 --? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
737 return screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, mx
, State
.left
) - 1
744 function Text
.screen_line_width(State
, line_index
, i
)
745 local line
= State
.lines
[line_index
]
746 local line_cache
= State
.line_cache
[line_index
]
747 local start_pos
= line_cache
.screen_line_starting_pos
[i
]
748 local start_offset
= Text
.offset(line
.data
, start_pos
)
750 if i
< #line_cache
.screen_line_starting_pos
then
751 local past_end_pos
= line_cache
.screen_line_starting_pos
[i
+1]
752 local past_end_offset
= Text
.offset(line
.data
, past_end_pos
)
753 screen_line
= string.sub(line
.data
, start_offset
, past_end_offset
-1)
755 screen_line
= string.sub(line
.data
, start_pos
)
757 local screen_line_text
= App
.newText(love
.graphics
.getFont(), screen_line
)
758 return App
.width(screen_line_text
)
761 function Text
.screen_line_index(screen_line_starting_pos
, pos
)
762 for i
= #screen_line_starting_pos
,1,-1 do
763 if screen_line_starting_pos
[i
] <= pos
then
769 -- convert x pixel coordinate to pos
770 -- oblivious to wrapping
771 -- result: 1 to len+1
772 function Text
.nearest_cursor_pos(line
, x
, left
)
776 local len
= utf8
.len(line
)
777 local max_x
= left
+Text
.x(line
, len
+1)
781 local leftpos
, rightpos
= 1, len
+1
782 --? print('-- nearest', x)
784 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
785 if leftpos
== rightpos
then
788 local curr
= math
.floor((leftpos
+rightpos
)/2)
789 local currxmin
= left
+Text
.x(line
, curr
)
790 local currxmax
= left
+Text
.x(line
, curr
+1)
791 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
792 if currxmin
<= x
and x
< currxmax
then
793 if x
-currxmin
< currxmax
-x
then
799 if leftpos
>= rightpos
-1 then
811 -- return the nearest index of line (in utf8 code points) which lies entirely
812 -- within x pixels of the left margin
813 -- result: 0 to len+1
814 function Text
.nearest_pos_less_than(line
, x
)
815 --? print('', '-- nearest_pos_less_than', line, x)
816 local len
= utf8
.len(line
)
817 local max_x
= Text
.x_after(line
, len
)
821 local left
, right
= 0, len
+1
823 local curr
= math
.floor((left
+right
)/2)
824 local currxmin
= Text
.x_after(line
, curr
+1)
825 local currxmax
= Text
.x_after(line
, curr
+2)
826 --? print('', x, left, right, curr, currxmin, currxmax)
827 if currxmin
<= x
and x
< currxmax
then
830 if left
>= right
-1 then
842 function Text
.x_after(s
, pos
)
843 local offset
= Text
.offset(s
, math
.min(pos
+1, #s
+1))
844 local s_before
= s
:sub(1, offset
-1)
845 --? print('^'..s_before..'$')
846 local text_before
= App
.newText(love
.graphics
.getFont(), s_before
)
847 return App
.width(text_before
)
850 function Text
.x(s
, pos
)
851 local offset
= Text
.offset(s
, pos
)
852 local s_before
= s
:sub(1, offset
-1)
853 local text_before
= App
.newText(love
.graphics
.getFont(), s_before
)
854 return App
.width(text_before
)
857 function Text
.to2(State
, loc1
)
858 if State
.lines
[loc1
.line
].mode
== 'drawing' then
859 return {line
=loc1
.line
, screen_line
=1, screen_pos
=1}
861 local result
= {line
=loc1
.line
}
862 local line_cache
= State
.line_cache
[loc1
.line
]
863 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
864 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
865 local spos
= line_cache
.screen_line_starting_pos
[i
]
866 if spos
<= loc1
.pos
then
867 result
.screen_line
= i
868 result
.screen_pos
= loc1
.pos
- spos
+ 1
872 assert(result
.screen_pos
)
876 function Text
.to1(State
, loc2
)
877 local result
= {line
=loc2
.line
, pos
=loc2
.screen_pos
}
878 if loc2
.screen_line
> 1 then
879 result
.pos
= State
.line_cache
[loc2
.line
].screen_line_starting_pos
[loc2
.screen_line
] + loc2
.screen_pos
- 1
884 function Text
.eq1(a
, b
)
885 return a
.line
== b
.line
and a
.pos
== b
.pos
888 function Text
.lt1(a
, b
)
889 if a
.line
< b
.line
then
892 if a
.line
> b
.line
then
898 function Text
.le1(a
, b
)
899 if a
.line
< b
.line
then
902 if a
.line
> b
.line
then
905 return a
.pos
<= b
.pos
908 function Text
.offset(s
, pos1
)
909 if pos1
== 1 then return 1 end
910 local result
= utf8
.offset(s
, pos1
)
911 if result
== nil then
918 function Text
.previous_screen_line(State
, loc2
)
919 if loc2
.screen_line
> 1 then
920 return {line
=loc2
.line
, screen_line
=loc2
.screen_line
-1, screen_pos
=1}
921 elseif loc2
.line
== 1 then
923 elseif State
.lines
[loc2
.line
-1].mode
== 'drawing' then
924 return {line
=loc2
.line
-1, screen_line
=1, screen_pos
=1}
926 local l
= State
.lines
[loc2
.line
-1]
927 Text
.populate_screen_line_starting_pos(State
, loc2
.line
-1)
928 return {line
=loc2
.line
-1, screen_line
=#State
.line_cache
[loc2
.line
-1].screen_line_starting_pos
, screen_pos
=1}
933 function Text
.tweak_screen_top_and_cursor(State
)
934 if State
.screen_top1
.pos
== 1 then return end
935 Text
.populate_screen_line_starting_pos(State
, State
.screen_top1
.line
)
936 local line
= State
.lines
[State
.screen_top1
.line
]
937 local line_cache
= State
.line_cache
[State
.screen_top1
.line
]
938 for i
=2,#line_cache
.screen_line_starting_pos
do
939 local pos
= line_cache
.screen_line_starting_pos
[i
]
940 if pos
== State
.screen_top1
.pos
then
943 if pos
> State
.screen_top1
.pos
then
944 -- make sure screen top is at start of a screen line
945 local prev
= line_cache
.screen_line_starting_pos
[i
-1]
946 if State
.screen_top1
.pos
- prev
< pos
- State
.screen_top1
.pos
then
947 State
.screen_top1
.pos
= prev
949 State
.screen_top1
.pos
= pos
954 -- make sure cursor is on screen
955 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
956 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
957 elseif State
.cursor1
.line
>= State
.screen_bottom1
.line
then
959 if Text
.cursor_out_of_screen(State
) then
962 line
=State
.screen_bottom1
.line
,
963 pos
=Text
.to_pos_on_line(State
, State
.screen_bottom1
.line
, State
.right
-5, App
.screen
.height
-5),
969 -- slightly expensive since it redraws the screen
970 function Text
.cursor_out_of_screen(State
)
972 return State
.cursor_y
== nil
973 -- this approach is cheaper and almost works, except on the final screen
974 -- where file ends above bottom of screen
975 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
976 --? local botline1 = {line=State.cursor1.line, pos=botpos}
977 --? return Text.lt1(State.screen_bottom1, botline1)
980 function Text
.redraw_all(State
)
981 --? print('clearing fragments')
982 State
.line_cache
= {}
983 for i
=1,#State
.lines
do
984 State
.line_cache
[i
] = {}
988 function Text
.clear_screen_line_cache(State
, line_index
)
989 State
.line_cache
[line_index
].fragments
= nil
990 State
.line_cache
[line_index
].screen_line_starting_pos
= nil
994 return s
:gsub('^%s+', ''):gsub('%s+$', '')
998 return s
:gsub('^%s+', '')
1002 return s
:gsub('%s+$', '')