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
, hide_cursor
)
7 local line
= State
.lines
[line_index
]
8 local line_cache
= State
.line_cache
[line_index
]
10 line_cache
.startpos
= startpos
12 local final_screen_line_starting_pos
= startpos
-- track value to return
13 Text
.populate_screen_line_starting_pos(State
, line_index
)
14 Text
.populate_link_offsets(State
, line_index
)
16 assert(#line_cache
.screen_line_starting_pos
>= 1)
17 for i
=1,#line_cache
.screen_line_starting_pos
do
18 local pos
= line_cache
.screen_line_starting_pos
[i
]
19 if pos
< startpos
then
21 --? print('skipping', screen_line)
23 final_screen_line_starting_pos
= pos
24 local screen_line
= Text
.screen_line(line
, line_cache
, i
)
25 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
26 local frag_len
= utf8
.len(screen_line
)
27 -- render any link decorations
28 for _
,link_offsets
in ipairs(line_cache
.link_offsets
) do
29 local s
,e
,filename
= unpack(link_offsets
)
30 local lo
, hi
= Text
.clip_wikiword_with_screen_line(line
, line_cache
, i
, s
, e
)
32 button(State
, 'link', {x
=State
.left
+lo
, y
=y
, w
=hi
-lo
, h
=State
.line_height
, color
={1,1,1},
33 icon
= icon
.hyperlink_decoration
,
35 if file_exists(filename
) then
36 source
.switch_to_file(filename
)
42 -- render any highlights
43 if State
.selection1
.line
then
44 local lo
, hi
= Text
.clip_selection(State
, line_index
, pos
, pos
+frag_len
)
45 Text
.draw_highlight(State
, line
, State
.left
,y
, pos
, lo
,hi
)
47 if not hide_cursor
and line_index
== State
.cursor1
.line
then
48 -- render search highlight or cursor
49 if State
.search_term
then
50 if pos
<= State
.cursor1
.pos
and pos
+ frag_len
> State
.cursor1
.pos
then
51 local data
= State
.lines
[State
.cursor1
.line
].data
52 local cursor_offset
= Text
.offset(data
, State
.cursor1
.pos
)
53 if data
:sub(cursor_offset
, cursor_offset
+#State
.search_term
-1) == State
.search_term
then
54 local hi
= State
.cursor1
.pos
+utf8
.len(State
.search_term
)
55 Text
.draw_highlight(State
, line
, State
.left
,y
, pos
, State
.cursor1
.pos
, hi
)
58 elseif Focus
== 'edit' then
59 if pos
<= State
.cursor1
.pos
and pos
+ frag_len
> State
.cursor1
.pos
then
60 Text
.draw_cursor(State
, State
.left
+Text
.x(screen_line
, State
.cursor1
.pos
-pos
+1), y
)
61 elseif pos
+ frag_len
== State
.cursor1
.pos
then
62 -- Show cursor at end of line.
63 -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
64 -- It seems useful to see a cursor whether your eye is on the left or right margin.
65 Text
.draw_cursor(State
, State
.left
+Text
.x(screen_line
, State
.cursor1
.pos
-pos
+1), y
)
69 -- render colorized text
71 for frag
in screen_line
:gmatch('%S*%s*') do
73 App
.screen
.print(frag
, x
,y
)
76 y
= y
+ State
.line_height
77 if y
>= App
.screen
.height
then
82 return y
, final_screen_line_starting_pos
85 function Text
.screen_line(line
, line_cache
, i
)
86 local pos
= line_cache
.screen_line_starting_pos
[i
]
87 local offset
= Text
.offset(line
.data
, pos
)
88 if i
>= #line_cache
.screen_line_starting_pos
then
89 return line
.data
:sub(offset
)
91 local endpos
= line_cache
.screen_line_starting_pos
[i
+1]-1
92 local end_offset
= Text
.offset(line
.data
, endpos
)
93 return line
.data
:sub(offset
, end_offset
)
96 function Text
.draw_cursor(State
, x
, y
)
98 if math
.floor(Cursor_time
*2)%2 == 0 then
99 App
.color(Cursor_color
)
100 love
.graphics
.rectangle('fill', x
,y
, 3,State
.line_height
)
103 State
.cursor_y
= y
+State
.line_height
106 function Text
.populate_screen_line_starting_pos(State
, line_index
)
107 local line
= State
.lines
[line_index
]
108 if line
.mode
~= 'text' then return end
109 local line_cache
= State
.line_cache
[line_index
]
110 if line_cache
.screen_line_starting_pos
then
113 line_cache
.screen_line_starting_pos
= {1}
116 -- try to wrap at word boundaries
117 for frag
in line
.data
:gmatch('%S*%s*') do
118 local frag_width
= App
.width(frag
)
119 --? print('-- frag:', frag, pos, x, frag_width, State.width)
120 while x
+ frag_width
> State
.width
do
121 --? print('frag:', frag, pos, x, frag_width, State.width)
122 if x
< 0.8 * State
.width
then
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
.width
- x
)
126 -- everything works if bpos == 0, but is a little inefficient
128 local boffset
= Text
.offset(frag
, bpos
+1) -- byte _after_ bpos
129 frag
= string.sub(frag
, boffset
)
131 --? print('after chop:', frag)
133 frag_width
= App
.width(frag
)
135 --? print('screen line:', pos)
136 table.insert(line_cache
.screen_line_starting_pos
, pos
)
137 x
= 0 -- new screen line
140 pos
= pos
+ utf8
.len(frag
)
144 function Text
.populate_link_offsets(State
, line_index
)
145 local line
= State
.lines
[line_index
]
146 if line
.mode
~= 'text' then return end
147 local line_cache
= State
.line_cache
[line_index
]
148 if line_cache
.link_offsets
then
151 line_cache
.link_offsets
= {}
153 -- try to wrap at word boundaries
155 while s
<= #line
.data
do
156 s
, e
= line
.data
:find('%[%[%S+%]%]', s
)
157 if s
== nil then break end
158 local word
= line
.data
:sub(s
+2, e
-2) -- strip out surrounding '[[..]]'
159 --? print('wikiword:', s, e, word)
160 table.insert(line_cache
.link_offsets
, {s
, e
, word
})
165 -- Intersect the filename between byte offsets s,e with the bounds of screen line i.
166 -- Return the left/right pixel coordinates of of the intersection,
167 -- or nil if it doesn't intersect with screen line i.
168 function Text
.clip_wikiword_with_screen_line(line
, line_cache
, i
, s
, e
)
169 local spos
= line_cache
.screen_line_starting_pos
[i
]
170 local soff
= Text
.offset(line
.data
, spos
)
175 if i
< #line_cache
.screen_line_starting_pos
then
176 local epos
= line_cache
.screen_line_starting_pos
[i
+1]
177 eoff
= Text
.offset(line
.data
, epos
)
182 local loff
= math
.max(s
, soff
)
185 hoff
= math
.min(e
, eoff
)
189 --? print(s, e, soff, eoff, loff, hoff)
190 return App
.width(line
.data
:sub(soff
, loff
-1)), App
.width(line
.data
:sub(soff
, hoff
))
193 function Text
.text_input(State
, t
)
194 if App
.mouse_down(1) then return end
195 if App
.ctrl_down() or App
.alt_down() or App
.cmd_down() then return end
196 local before
= snapshot(State
, State
.cursor1
.line
)
197 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
198 Text
.insert_at_cursor(State
, t
)
199 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
200 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
201 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
203 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
206 function Text
.insert_at_cursor(State
, t
)
207 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
208 local byte_offset
= Text
.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_offset
-1)..t
..string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_offset
)
210 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
211 State
.cursor1
.pos
= State
.cursor1
.pos
+1
214 -- Don't handle any keys here that would trigger text_input above.
215 function Text
.keychord_press(State
, chord
)
216 --? print('chord', chord, State.selection1.line, State.selection1.pos)
217 --== shortcuts that mutate text
218 if chord
== 'return' then
219 local before_line
= State
.cursor1
.line
220 local before
= snapshot(State
, before_line
)
221 Text
.insert_return(State
)
222 State
.selection1
= {}
223 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
224 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
227 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
228 elseif chord
== 'tab' then
229 local before
= snapshot(State
, State
.cursor1
.line
)
230 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
231 Text
.insert_at_cursor(State
, '\t')
232 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
233 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
234 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
235 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
238 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
239 elseif chord
== 'backspace' then
240 if State
.selection1
.line
then
241 Text
.delete_selection(State
, State
.left
, State
.right
)
246 if State
.cursor1
.pos
> 1 then
247 before
= snapshot(State
, State
.cursor1
.line
)
248 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1)
249 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
252 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
)
254 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
256 State
.cursor1
.pos
= State
.cursor1
.pos
-1
258 elseif State
.cursor1
.line
> 1 then
259 before
= snapshot(State
, State
.cursor1
.line
-1, State
.cursor1
.line
)
260 if State
.lines
[State
.cursor1
.line
-1].mode
== 'drawing' then
261 table.remove(State
.lines
, State
.cursor1
.line
-1)
262 table.remove(State
.line_cache
, State
.cursor1
.line
-1)
265 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
-1].data
)+1
266 State
.lines
[State
.cursor1
.line
-1].data
= State
.lines
[State
.cursor1
.line
-1].data
..State
.lines
[State
.cursor1
.line
].data
267 table.remove(State
.lines
, State
.cursor1
.line
)
268 table.remove(State
.line_cache
, State
.cursor1
.line
)
270 State
.cursor1
.line
= State
.cursor1
.line
-1
272 if State
.screen_top1
.line
> #State
.lines
then
273 Text
.populate_screen_line_starting_pos(State
, #State
.lines
)
274 local line_cache
= State
.line_cache
[#State
.line_cache
]
275 State
.screen_top1
= {line
=#State
.lines
, pos
=line_cache
.screen_line_starting_pos
[#line_cache
.screen_line_starting_pos
]}
276 elseif Text
.lt1(State
.cursor1
, State
.screen_top1
) then
277 State
.screen_top1
= {
278 line
=State
.cursor1
.line
,
279 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
281 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
283 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
284 assert(Text
.le1(State
.screen_top1
, State
.cursor1
))
286 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
287 elseif chord
== 'delete' then
288 if State
.selection1
.line
then
289 Text
.delete_selection(State
, State
.left
, State
.right
)
294 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
295 before
= snapshot(State
, State
.cursor1
.line
)
297 before
= snapshot(State
, State
.cursor1
.line
, State
.cursor1
.line
+1)
299 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
300 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
301 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
+1)
304 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
)
306 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
308 -- no change to State.cursor1.pos
310 elseif State
.cursor1
.line
< #State
.lines
then
311 if State
.lines
[State
.cursor1
.line
+1].mode
== 'text' then
313 State
.lines
[State
.cursor1
.line
].data
= State
.lines
[State
.cursor1
.line
].data
..State
.lines
[State
.cursor1
.line
+1].data
315 table.remove(State
.lines
, State
.cursor1
.line
+1)
316 table.remove(State
.line_cache
, State
.cursor1
.line
+1)
318 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
320 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
321 --== shortcuts that move the cursor
322 elseif chord
== 'left' then
324 State
.selection1
= {}
325 elseif chord
== 'right' then
327 State
.selection1
= {}
328 elseif chord
== 'S-left' then
329 if State
.selection1
.line
== nil then
330 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
333 elseif chord
== 'S-right' then
334 if State
.selection1
.line
== nil then
335 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
338 -- C- hotkeys reserved for drawings, so we'll use M-
339 elseif chord
== 'M-left' then
340 Text
.word_left(State
)
341 State
.selection1
= {}
342 elseif chord
== 'M-right' then
343 Text
.word_right(State
)
344 State
.selection1
= {}
345 elseif chord
== 'M-S-left' then
346 if State
.selection1
.line
== nil then
347 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
349 Text
.word_left(State
)
350 elseif chord
== 'M-S-right' then
351 if State
.selection1
.line
== nil then
352 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
354 Text
.word_right(State
)
355 elseif chord
== 'home' then
356 Text
.start_of_line(State
)
357 State
.selection1
= {}
358 elseif chord
== 'end' then
359 Text
.end_of_line(State
)
360 State
.selection1
= {}
361 elseif chord
== 'S-home' then
362 if State
.selection1
.line
== nil then
363 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
365 Text
.start_of_line(State
)
366 elseif chord
== 'S-end' then
367 if State
.selection1
.line
== nil then
368 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
370 Text
.end_of_line(State
)
371 elseif chord
== 'up' then
373 State
.selection1
= {}
374 elseif chord
== 'down' then
376 State
.selection1
= {}
377 elseif chord
== 'S-up' then
378 if State
.selection1
.line
== nil then
379 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
382 elseif chord
== 'S-down' then
383 if State
.selection1
.line
== nil then
384 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
387 elseif chord
== 'pageup' then
389 State
.selection1
= {}
390 elseif chord
== 'pagedown' then
392 State
.selection1
= {}
393 elseif chord
== 'S-pageup' then
394 if State
.selection1
.line
== nil then
395 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
398 elseif chord
== 'S-pagedown' then
399 if State
.selection1
.line
== nil then
400 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
406 function Text
.insert_return(State
)
407 local byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
408 table.insert(State
.lines
, State
.cursor1
.line
+1, {mode
='text', data
=string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_offset
)})
409 table.insert(State
.line_cache
, State
.cursor1
.line
+1, {})
410 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_offset
-1)
411 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
412 State
.cursor1
= {line
=State
.cursor1
.line
+1, pos
=1}
415 function Text
.pageup(State
)
417 -- duplicate some logic from love.draw
418 local top2
= Text
.to2(State
, State
.screen_top1
)
419 --? print(App.screen.height)
420 local y
= App
.screen
.height
- State
.line_height
421 while y
>= State
.top
do
422 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
423 if State
.screen_top1
.line
== 1 and State
.screen_top1
.pos
== 1 then break end
424 if State
.lines
[State
.screen_top1
.line
].mode
== 'text' then
425 y
= y
- State
.line_height
426 elseif State
.lines
[State
.screen_top1
.line
].mode
== 'drawing' then
427 y
= y
- Drawing_padding_height
- Drawing
.pixels(State
.lines
[State
.screen_top1
.line
].h
, State
.width
)
429 top2
= Text
.previous_screen_line(State
, top2
)
431 State
.screen_top1
= Text
.to1(State
, top2
)
432 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
433 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
434 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
435 --? print('pageup end')
438 function Text
.pagedown(State
)
439 --? print('pagedown')
440 State
.screen_top1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
441 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
442 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
443 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
444 --? print('top now', State.screen_top1.line)
445 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
446 --? print('pagedown end')
449 function Text
.up(State
)
450 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
451 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
452 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
453 if screen_line_starting_pos
== 1 then
454 --? print('cursor is at first screen line of its line')
455 -- line is done; skip to previous text line
456 local new_cursor_line
= State
.cursor1
.line
457 while new_cursor_line
> 1 do
458 new_cursor_line
= new_cursor_line
-1
459 if State
.lines
[new_cursor_line
].mode
== 'text' then
460 --? print('found previous text line')
461 State
.cursor1
= {line
=new_cursor_line
, pos
=nil}
462 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
463 -- previous text line found, pick its final screen line
464 --? print('has multiple screen lines')
465 local screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
466 --? print(#screen_line_starting_pos)
467 screen_line_starting_pos
= screen_line_starting_pos
[#screen_line_starting_pos
]
468 local screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_pos
)
469 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_byte_offset
)
470 State
.cursor1
.pos
= screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
475 -- move up one screen line in current line
476 assert(screen_line_index
> 1)
477 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
-1]
478 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
479 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
480 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
481 --? print('cursor pos is now '..tostring(State.cursor1.pos))
483 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
484 State
.screen_top1
= {
485 line
=State
.cursor1
.line
,
486 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
488 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
492 function Text
.down(State
)
493 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
494 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
495 assert(State
.cursor1
.pos
)
496 if Text
.cursor_at_final_screen_line(State
) then
497 -- line is done, skip to next text line
498 --? print('cursor at final screen line of its line')
499 local new_cursor_line
= State
.cursor1
.line
500 while new_cursor_line
< #State
.lines
do
501 new_cursor_line
= new_cursor_line
+1
502 if State
.lines
[new_cursor_line
].mode
== 'text' then
504 line
= new_cursor_line
,
505 pos
= Text
.nearest_cursor_pos(State
.lines
[new_cursor_line
].data
, State
.cursor_x
, State
.left
),
507 --? print(State.cursor1.pos)
511 if State
.cursor1
.line
> State
.screen_bottom1
.line
then
512 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
513 --? print('scroll up preserving cursor')
514 Text
.snap_cursor_to_bottom_of_screen(State
)
515 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
518 -- move down one screen line in current line
519 local scroll_down
= Text
.le1(State
.screen_bottom1
, State
.cursor1
)
520 --? print('cursor is NOT at final screen line of its line')
521 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
522 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
523 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
+1]
524 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
525 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
526 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
527 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
528 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
530 --? print('scroll up preserving cursor')
531 Text
.snap_cursor_to_bottom_of_screen(State
)
532 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
535 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
538 function Text
.start_of_line(State
)
539 State
.cursor1
.pos
= 1
540 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
541 State
.screen_top1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
} -- copy
545 function Text
.end_of_line(State
)
546 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) + 1
547 if Text
.cursor_out_of_screen(State
) then
548 Text
.snap_cursor_to_bottom_of_screen(State
)
552 function Text
.word_left(State
)
553 -- skip some whitespace
555 if State
.cursor1
.pos
== 1 then
558 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%S') then
563 -- skip some non-whitespace
566 if State
.cursor1
.pos
== 1 then
569 assert(State
.cursor1
.pos
> 1)
570 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%s') then
576 function Text
.word_right(State
)
577 -- skip some whitespace
579 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
582 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%S') then
585 Text
.right_without_scroll(State
)
588 Text
.right_without_scroll(State
)
589 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
592 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%s') then
596 if Text
.cursor_out_of_screen(State
) then
597 Text
.snap_cursor_to_bottom_of_screen(State
)
601 function Text
.match(s
, pos
, pat
)
602 local start_offset
= Text
.offset(s
, pos
)
604 local end_offset
= Text
.offset(s
, pos
+1)
605 assert(end_offset
> start_offset
)
606 local curr
= s
:sub(start_offset
, end_offset
-1)
607 return curr
:match(pat
)
610 function Text
.left(State
)
611 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
612 if State
.cursor1
.pos
> 1 then
613 State
.cursor1
.pos
= State
.cursor1
.pos
-1
615 local new_cursor_line
= State
.cursor1
.line
616 while new_cursor_line
> 1 do
617 new_cursor_line
= new_cursor_line
-1
618 if State
.lines
[new_cursor_line
].mode
== 'text' then
620 line
= new_cursor_line
,
621 pos
= utf8
.len(State
.lines
[new_cursor_line
].data
) + 1,
627 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
628 State
.screen_top1
= {
629 line
=State
.cursor1
.line
,
630 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
632 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
636 function Text
.right(State
)
637 Text
.right_without_scroll(State
)
638 if Text
.cursor_out_of_screen(State
) then
639 Text
.snap_cursor_to_bottom_of_screen(State
)
643 function Text
.right_without_scroll(State
)
644 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
645 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
646 State
.cursor1
.pos
= State
.cursor1
.pos
+1
648 local new_cursor_line
= State
.cursor1
.line
649 while new_cursor_line
<= #State
.lines
-1 do
650 new_cursor_line
= new_cursor_line
+1
651 if State
.lines
[new_cursor_line
].mode
== 'text' then
652 State
.cursor1
= {line
=new_cursor_line
, pos
=1}
659 -- result: pos, index of screen line
660 function Text
.pos_at_start_of_screen_line(State
, loc1
)
661 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
662 local line_cache
= State
.line_cache
[loc1
.line
]
663 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
664 local spos
= line_cache
.screen_line_starting_pos
[i
]
665 if spos
<= loc1
.pos
then
672 function Text
.pos_at_end_of_screen_line(State
, loc1
)
673 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
674 local line_cache
= State
.line_cache
[loc1
.line
]
675 local most_recent_final_pos
= utf8
.len(State
.lines
[loc1
.line
].data
)+1
676 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
677 local spos
= line_cache
.screen_line_starting_pos
[i
]
678 if spos
<= loc1
.pos
then
679 return most_recent_final_pos
681 most_recent_final_pos
= spos
-1
686 function Text
.cursor_at_final_screen_line(State
)
687 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
688 local screen_lines
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
689 --? print(screen_lines[#screen_lines], State.cursor1.pos)
690 return screen_lines
[#screen_lines
] <= State
.cursor1
.pos
693 function Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
695 while State
.cursor1
.line
<= #State
.lines
do
696 if State
.lines
[State
.cursor1
.line
].mode
== 'text' then
699 --? print('cursor skips', State.cursor1.line)
700 y
= y
+ Drawing_padding_height
+ Drawing
.pixels(State
.lines
[State
.cursor1
.line
].h
, State
.width
)
701 State
.cursor1
.line
= State
.cursor1
.line
+ 1
703 if State
.cursor1
.pos
== nil then
704 State
.cursor1
.pos
= 1
706 -- hack: insert a text line at bottom of file if necessary
707 if State
.cursor1
.line
> #State
.lines
then
708 assert(State
.cursor1
.line
== #State
.lines
+1)
709 table.insert(State
.lines
, {mode
='text', data
=''})
710 table.insert(State
.line_cache
, {})
712 --? print(y, App.screen.height, App.screen.height-State.line_height)
713 if y
> App
.screen
.height
- State
.line_height
then
714 --? print('scroll up')
715 Text
.snap_cursor_to_bottom_of_screen(State
)
719 -- should never modify State.cursor1
720 function Text
.snap_cursor_to_bottom_of_screen(State
)
721 --? print('to2:', State.cursor1.line, State.cursor1.pos)
722 local top2
= Text
.to2(State
, State
.cursor1
)
723 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
724 -- slide to start of screen line
725 top2
.screen_pos
= 1 -- start of screen line
726 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
727 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
728 local y
= App
.screen
.height
- State
.line_height
729 -- duplicate some logic from love.draw
731 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
732 if top2
.line
== 1 and top2
.screen_line
== 1 then break end
733 if top2
.screen_line
> 1 or State
.lines
[top2
.line
-1].mode
== 'text' then
734 local h
= State
.line_height
735 if y
- h
< State
.top
then
740 assert(top2
.line
> 1)
741 assert(State
.lines
[top2
.line
-1].mode
== 'drawing')
742 -- We currently can't draw partial drawings, so either skip it entirely
744 local h
= Drawing_padding_height
+ Drawing
.pixels(State
.lines
[top2
.line
-1].h
, State
.width
)
745 if y
- h
< State
.top
then
748 --? print('skipping drawing of height', h)
751 top2
= Text
.previous_screen_line(State
, top2
)
753 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
754 State
.screen_top1
= Text
.to1(State
, top2
)
755 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
756 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
757 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
760 function Text
.in_line(State
, line_index
, x
,y
)
761 local line
= State
.lines
[line_index
]
762 local line_cache
= State
.line_cache
[line_index
]
763 if line_cache
.starty
== nil then return false end -- outside current page
764 if y
< line_cache
.starty
then return false end
765 Text
.populate_screen_line_starting_pos(State
, line_index
)
766 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)
769 -- convert mx,my in pixels to schema-1 coordinates
770 function Text
.to_pos_on_line(State
, line_index
, mx
, my
)
771 local line
= State
.lines
[line_index
]
772 local line_cache
= State
.line_cache
[line_index
]
773 assert(my
>= line_cache
.starty
)
774 -- duplicate some logic from Text.draw
775 local y
= line_cache
.starty
776 local start_screen_line_index
= Text
.screen_line_index(line_cache
.screen_line_starting_pos
, line_cache
.startpos
)
777 for screen_line_index
= start_screen_line_index
,#line_cache
.screen_line_starting_pos
do
778 local screen_line_starting_pos
= line_cache
.screen_line_starting_pos
[screen_line_index
]
779 local screen_line_starting_byte_offset
= Text
.offset(line
.data
, screen_line_starting_pos
)
780 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
781 local nexty
= y
+ State
.line_height
783 -- On all wrapped screen lines but the final one, clicks past end of
784 -- line position cursor on final character of screen line.
785 -- (The final screen line positions past end of screen line as always.)
786 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
787 --? print('past end of non-final line; return')
788 return line_cache
.screen_line_starting_pos
[screen_line_index
+1]-1
790 local s
= string.sub(line
.data
, screen_line_starting_byte_offset
)
791 --? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
792 return screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, mx
, State
.left
) - 1
799 function Text
.screen_line_width(State
, line_index
, i
)
800 local line
= State
.lines
[line_index
]
801 local line_cache
= State
.line_cache
[line_index
]
802 local start_pos
= line_cache
.screen_line_starting_pos
[i
]
803 local start_offset
= Text
.offset(line
.data
, start_pos
)
805 if i
< #line_cache
.screen_line_starting_pos
then
806 local past_end_pos
= line_cache
.screen_line_starting_pos
[i
+1]
807 local past_end_offset
= Text
.offset(line
.data
, past_end_pos
)
808 screen_line
= string.sub(line
.data
, start_offset
, past_end_offset
-1)
810 screen_line
= string.sub(line
.data
, start_pos
)
812 return App
.width(screen_line
)
815 function Text
.screen_line_index(screen_line_starting_pos
, pos
)
816 for i
= #screen_line_starting_pos
,1,-1 do
817 if screen_line_starting_pos
[i
] <= pos
then
823 -- convert x pixel coordinate to pos
824 -- oblivious to wrapping
825 -- result: 1 to len+1
826 function Text
.nearest_cursor_pos(line
, x
, left
)
830 local len
= utf8
.len(line
)
831 local max_x
= left
+Text
.x(line
, len
+1)
835 local leftpos
, rightpos
= 1, len
+1
836 --? print('-- nearest', x)
838 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
839 if leftpos
== rightpos
then
842 local curr
= math
.floor((leftpos
+rightpos
)/2)
843 local currxmin
= left
+Text
.x(line
, curr
)
844 local currxmax
= left
+Text
.x(line
, curr
+1)
845 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
846 if currxmin
<= x
and x
< currxmax
then
847 if x
-currxmin
< currxmax
-x
then
853 if leftpos
>= rightpos
-1 then
865 -- return the nearest index of line (in utf8 code points) which lies entirely
866 -- within x pixels of the left margin
867 -- result: 0 to len+1
868 function Text
.nearest_pos_less_than(line
, x
)
869 --? print('', '-- nearest_pos_less_than', line, x)
870 local len
= utf8
.len(line
)
871 local max_x
= Text
.x_after(line
, len
)
875 local left
, right
= 0, len
+1
877 local curr
= math
.floor((left
+right
)/2)
878 local currxmin
= Text
.x_after(line
, curr
+1)
879 local currxmax
= Text
.x_after(line
, curr
+2)
880 --? print('', x, left, right, curr, currxmin, currxmax)
881 if currxmin
<= x
and x
< currxmax
then
884 if left
>= right
-1 then
896 function Text
.x_after(s
, pos
)
897 local offset
= Text
.offset(s
, math
.min(pos
+1, #s
+1))
898 local s_before
= s
:sub(1, offset
-1)
899 --? print('^'..s_before..'$')
900 return App
.width(s_before
)
903 function Text
.x(s
, pos
)
904 local offset
= Text
.offset(s
, pos
)
905 local s_before
= s
:sub(1, offset
-1)
906 return App
.width(s_before
)
909 function Text
.to2(State
, loc1
)
910 if State
.lines
[loc1
.line
].mode
== 'drawing' then
911 return {line
=loc1
.line
, screen_line
=1, screen_pos
=1}
913 local result
= {line
=loc1
.line
}
914 local line_cache
= State
.line_cache
[loc1
.line
]
915 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
916 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
917 local spos
= line_cache
.screen_line_starting_pos
[i
]
918 if spos
<= loc1
.pos
then
919 result
.screen_line
= i
920 result
.screen_pos
= loc1
.pos
- spos
+ 1
924 assert(result
.screen_pos
)
928 function Text
.to1(State
, loc2
)
929 local result
= {line
=loc2
.line
, pos
=loc2
.screen_pos
}
930 if loc2
.screen_line
> 1 then
931 result
.pos
= State
.line_cache
[loc2
.line
].screen_line_starting_pos
[loc2
.screen_line
] + loc2
.screen_pos
- 1
936 function Text
.eq1(a
, b
)
937 return a
.line
== b
.line
and a
.pos
== b
.pos
940 function Text
.lt1(a
, b
)
941 if a
.line
< b
.line
then
944 if a
.line
> b
.line
then
950 function Text
.le1(a
, b
)
951 if a
.line
< b
.line
then
954 if a
.line
> b
.line
then
957 return a
.pos
<= b
.pos
960 function Text
.offset(s
, pos1
)
961 if pos1
== 1 then return 1 end
962 local result
= utf8
.offset(s
, pos1
)
963 if result
== nil then
970 function Text
.previous_screen_line(State
, loc2
)
971 if loc2
.screen_line
> 1 then
972 return {line
=loc2
.line
, screen_line
=loc2
.screen_line
-1, screen_pos
=1}
973 elseif loc2
.line
== 1 then
975 elseif State
.lines
[loc2
.line
-1].mode
== 'drawing' then
976 return {line
=loc2
.line
-1, screen_line
=1, screen_pos
=1}
978 local l
= State
.lines
[loc2
.line
-1]
979 Text
.populate_screen_line_starting_pos(State
, loc2
.line
-1)
980 return {line
=loc2
.line
-1, screen_line
=#State
.line_cache
[loc2
.line
-1].screen_line_starting_pos
, screen_pos
=1}
985 function Text
.tweak_screen_top_and_cursor(State
)
986 if State
.screen_top1
.pos
== 1 then return end
987 Text
.populate_screen_line_starting_pos(State
, State
.screen_top1
.line
)
988 local line
= State
.lines
[State
.screen_top1
.line
]
989 local line_cache
= State
.line_cache
[State
.screen_top1
.line
]
990 for i
=2,#line_cache
.screen_line_starting_pos
do
991 local pos
= line_cache
.screen_line_starting_pos
[i
]
992 if pos
== State
.screen_top1
.pos
then
995 if pos
> State
.screen_top1
.pos
then
996 -- make sure screen top is at start of a screen line
997 local prev
= line_cache
.screen_line_starting_pos
[i
-1]
998 if State
.screen_top1
.pos
- prev
< pos
- State
.screen_top1
.pos
then
999 State
.screen_top1
.pos
= prev
1001 State
.screen_top1
.pos
= pos
1006 -- make sure cursor is on screen
1007 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
1008 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
1009 elseif State
.cursor1
.line
>= State
.screen_bottom1
.line
then
1010 --? print('too low')
1011 if Text
.cursor_out_of_screen(State
) then
1014 line
=State
.screen_bottom1
.line
,
1015 pos
=Text
.to_pos_on_line(State
, State
.screen_bottom1
.line
, State
.right
-5, App
.screen
.height
-5),
1021 -- slightly expensive since it redraws the screen
1022 function Text
.cursor_out_of_screen(State
)
1024 return State
.cursor_y
== nil
1025 -- this approach is cheaper and almost works, except on the final screen
1026 -- where file ends above bottom of screen
1027 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
1028 --? local botline1 = {line=State.cursor1.line, pos=botpos}
1029 --? return Text.lt1(State.screen_bottom1, botline1)
1032 function Text
.redraw_all(State
)
1033 --? print('clearing fragments')
1034 State
.line_cache
= {}
1035 for i
=1,#State
.lines
do
1036 State
.line_cache
[i
] = {}
1040 function Text
.clear_screen_line_cache(State
, line_index
)
1041 State
.line_cache
[line_index
].screen_line_starting_pos
= nil
1042 State
.line_cache
[line_index
].link_offsets
= nil
1046 return s
:gsub('^%s+', ''):gsub('%s+$', '')
1050 return s
:gsub('^%s+', '')
1054 return s
:gsub('%s+$', '')
1057 function starts_with(s
, prefix
)
1058 if #s
< #prefix
then
1062 if s
:sub(i
,i
) ~= prefix
:sub(i
,i
) then
1069 function ends_with(s
, suffix
)
1070 if #s
< #suffix
then
1073 for i
=0,#suffix
-1 do
1074 if s
:sub(#s
-i
,#s
-i
) ~= suffix
:sub(#suffix
-i
,#suffix
-i
) then