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
, show_line_numbers
)
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
)
15 if show_line_numbers
then
16 App
.color(Line_number_color
)
17 love
.graphics
.print(line_index
, State
.left
-Line_number_width
*App
.width('m')+10,y
)
20 assert(#line_cache
.screen_line_starting_pos
>= 1)
21 for i
=1,#line_cache
.screen_line_starting_pos
do
22 local pos
= line_cache
.screen_line_starting_pos
[i
]
23 if pos
< startpos
then
25 --? print('skipping', screen_line)
27 final_screen_line_starting_pos
= pos
28 local screen_line
= Text
.screen_line(line
, line_cache
, i
)
29 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
30 local frag_len
= utf8
.len(screen_line
)
31 -- render any highlights
32 for _
,link_offsets
in ipairs(line_cache
.link_offsets
) do
33 -- render link decorations
34 local s
,e
,filename
= unpack(link_offsets
)
35 local lo
, hi
= Text
.clip_wikiword_with_screen_line(line
, line_cache
, i
, s
, e
)
37 button(State
, 'link', {x
=State
.left
+lo
, y
=y
, w
=hi
-lo
, h
=State
.line_height
, bg
={r
=1,g
=1,b
=1},
38 icon
= icon
.hyperlink_decoration
,
40 if file_exists(filename
) then
41 source
.switch_to_file(filename
)
47 if State
.selection1
.line
then
48 local lo
, hi
= Text
.clip_selection(State
, line_index
, pos
, pos
+frag_len
)
49 Text
.draw_highlight(State
, line
, State
.left
,y
, pos
, lo
,hi
)
51 if not hide_cursor
and line_index
== State
.cursor1
.line
then
52 -- render search highlight or cursor
53 if State
.search_term
then
54 local data
= State
.lines
[State
.cursor1
.line
].data
55 local cursor_offset
= Text
.offset(data
, State
.cursor1
.pos
)
56 if data
:sub(cursor_offset
, cursor_offset
+#State
.search_term
-1) == State
.search_term
then
57 local save_selection
= State
.selection1
58 State
.selection1
= {line
=line_index
, pos
=State
.cursor1
.pos
+utf8
.len(State
.search_term
)}
59 local lo
, hi
= Text
.clip_selection(State
, line_index
, pos
, pos
+frag_len
)
60 Text
.draw_highlight(State
, line
, State
.left
,y
, pos
, lo
,hi
)
61 State
.selection1
= save_selection
63 elseif Focus
== 'edit' then
64 if pos
<= State
.cursor1
.pos
and pos
+ frag_len
> State
.cursor1
.pos
then
65 Text
.draw_cursor(State
, State
.left
+Text
.x(screen_line
, State
.cursor1
.pos
-pos
+1), y
)
66 elseif pos
+ frag_len
== State
.cursor1
.pos
then
67 -- Show cursor at end of line.
68 -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
69 -- It seems useful to see a cursor whether your eye is on the left or right margin.
70 Text
.draw_cursor(State
, State
.left
+Text
.x(screen_line
, State
.cursor1
.pos
-pos
+1), y
)
74 -- render colorized text
76 for frag
in screen_line
:gmatch('%S*%s*') do
78 App
.screen
.print(frag
, x
,y
)
81 y
= y
+ State
.line_height
82 if y
>= App
.screen
.height
then
87 return y
, final_screen_line_starting_pos
90 function Text
.screen_line(line
, line_cache
, i
)
91 local pos
= line_cache
.screen_line_starting_pos
[i
]
92 local offset
= Text
.offset(line
.data
, pos
)
93 if i
>= #line_cache
.screen_line_starting_pos
then
94 return line
.data
:sub(offset
)
96 local endpos
= line_cache
.screen_line_starting_pos
[i
+1]-1
97 local end_offset
= Text
.offset(line
.data
, endpos
)
98 return line
.data
:sub(offset
, end_offset
)
101 function Text
.draw_cursor(State
, x
, y
)
103 if math
.floor(Cursor_time
*2)%2 == 0 then
104 App
.color(Cursor_color
)
105 love
.graphics
.rectangle('fill', x
,y
, 3,State
.line_height
)
108 State
.cursor_y
= y
+State
.line_height
111 function Text
.populate_screen_line_starting_pos(State
, line_index
)
112 local line
= State
.lines
[line_index
]
113 if line
.mode
~= 'text' then return end
114 local line_cache
= State
.line_cache
[line_index
]
115 if line_cache
.screen_line_starting_pos
then
118 line_cache
.screen_line_starting_pos
= {1}
121 -- try to wrap at word boundaries
122 for frag
in line
.data
:gmatch('%S*%s*') do
123 local frag_width
= App
.width(frag
)
124 --? print('-- frag:', frag, pos, x, frag_width, State.width)
125 while x
+ frag_width
> State
.width
do
126 --? print('frag:', frag, pos, x, frag_width, State.width)
127 if x
< 0.8 * State
.width
then
128 -- long word; chop it at some letter
129 -- We're not going to reimplement TeX here.
130 local bpos
= Text
.nearest_pos_less_than(frag
, State
.width
- x
)
131 -- everything works if bpos == 0, but is a little inefficient
133 local boffset
= Text
.offset(frag
, bpos
+1) -- byte _after_ bpos
134 frag
= string.sub(frag
, boffset
)
136 --? print('after chop:', frag)
138 frag_width
= App
.width(frag
)
140 --? print('screen line:', pos)
141 table.insert(line_cache
.screen_line_starting_pos
, pos
)
142 x
= 0 -- new screen line
145 pos
= pos
+ utf8
.len(frag
)
149 function Text
.populate_link_offsets(State
, line_index
)
150 local line
= State
.lines
[line_index
]
151 if line
.mode
~= 'text' then return end
152 local line_cache
= State
.line_cache
[line_index
]
153 if line_cache
.link_offsets
then
156 line_cache
.link_offsets
= {}
158 -- try to wrap at word boundaries
160 while s
<= #line
.data
do
161 s
, e
= line
.data
:find('%[%[%S+%]%]', s
)
162 if s
== nil then break end
163 local word
= line
.data
:sub(s
+2, e
-2) -- strip out surrounding '[[..]]'
164 --? print('wikiword:', s, e, word)
165 table.insert(line_cache
.link_offsets
, {s
, e
, word
})
170 -- Intersect the filename between byte offsets s,e with the bounds of screen line i.
171 -- Return the left/right pixel coordinates of of the intersection,
172 -- or nil if it doesn't intersect with screen line i.
173 function Text
.clip_wikiword_with_screen_line(line
, line_cache
, i
, s
, e
)
174 local spos
= line_cache
.screen_line_starting_pos
[i
]
175 local soff
= Text
.offset(line
.data
, spos
)
180 if i
< #line_cache
.screen_line_starting_pos
then
181 local epos
= line_cache
.screen_line_starting_pos
[i
+1]
182 eoff
= Text
.offset(line
.data
, epos
)
187 local loff
= math
.max(s
, soff
)
190 hoff
= math
.min(e
, eoff
)
194 --? print(s, e, soff, eoff, loff, hoff)
195 return App
.width(line
.data
:sub(soff
, loff
-1)), App
.width(line
.data
:sub(soff
, hoff
))
198 function Text
.text_input(State
, t
)
199 if App
.mouse_down(1) then return end
200 if App
.ctrl_down() or App
.alt_down() or App
.cmd_down() then return end
201 local before
= snapshot(State
, State
.cursor1
.line
)
202 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
203 Text
.insert_at_cursor(State
, t
)
204 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
205 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
206 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
208 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
211 function Text
.insert_at_cursor(State
, t
)
212 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
213 local byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
214 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
)
215 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
216 State
.cursor1
.pos
= State
.cursor1
.pos
+1
219 -- Don't handle any keys here that would trigger text_input above.
220 function Text
.keychord_press(State
, chord
)
221 --? print('chord', chord, State.selection1.line, State.selection1.pos)
222 --== shortcuts that mutate text
223 if chord
== 'return' then
224 local before_line
= State
.cursor1
.line
225 local before
= snapshot(State
, before_line
)
226 Text
.insert_return(State
)
227 State
.selection1
= {}
228 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
229 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
232 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
233 elseif chord
== 'tab' then
234 local before
= snapshot(State
, State
.cursor1
.line
)
235 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
236 Text
.insert_at_cursor(State
, '\t')
237 if State
.cursor_y
> App
.screen
.height
- State
.line_height
then
238 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
239 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
240 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
243 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
244 elseif chord
== 'backspace' then
245 if State
.selection1
.line
then
246 Text
.delete_selection(State
, State
.left
, State
.right
)
251 if State
.cursor1
.pos
> 1 then
252 before
= snapshot(State
, State
.cursor1
.line
)
253 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1)
254 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
257 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
)
259 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
261 State
.cursor1
.pos
= State
.cursor1
.pos
-1
263 elseif State
.cursor1
.line
> 1 then
264 before
= snapshot(State
, State
.cursor1
.line
-1, State
.cursor1
.line
)
265 if State
.lines
[State
.cursor1
.line
-1].mode
== 'drawing' then
266 table.remove(State
.lines
, State
.cursor1
.line
-1)
267 table.remove(State
.line_cache
, State
.cursor1
.line
-1)
270 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
-1].data
)+1
271 State
.lines
[State
.cursor1
.line
-1].data
= State
.lines
[State
.cursor1
.line
-1].data
..State
.lines
[State
.cursor1
.line
].data
272 table.remove(State
.lines
, State
.cursor1
.line
)
273 table.remove(State
.line_cache
, State
.cursor1
.line
)
275 State
.cursor1
.line
= State
.cursor1
.line
-1
277 if State
.screen_top1
.line
> #State
.lines
then
278 Text
.populate_screen_line_starting_pos(State
, #State
.lines
)
279 local line_cache
= State
.line_cache
[#State
.line_cache
]
280 State
.screen_top1
= {line
=#State
.lines
, pos
=line_cache
.screen_line_starting_pos
[#line_cache
.screen_line_starting_pos
]}
281 elseif Text
.lt1(State
.cursor1
, State
.screen_top1
) then
282 State
.screen_top1
= {
283 line
=State
.cursor1
.line
,
284 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
286 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
288 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
289 assert(Text
.le1(State
.screen_top1
, State
.cursor1
))
291 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
292 elseif chord
== 'delete' then
293 if State
.selection1
.line
then
294 Text
.delete_selection(State
, State
.left
, State
.right
)
299 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
300 before
= snapshot(State
, State
.cursor1
.line
)
302 before
= snapshot(State
, State
.cursor1
.line
, State
.cursor1
.line
+1)
304 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
305 local byte_start
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
306 local byte_end
= utf8
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
+1)
309 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
)
311 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_start
-1)
313 -- no change to State.cursor1.pos
315 elseif State
.cursor1
.line
< #State
.lines
then
316 if State
.lines
[State
.cursor1
.line
+1].mode
== 'text' then
318 State
.lines
[State
.cursor1
.line
].data
= State
.lines
[State
.cursor1
.line
].data
..State
.lines
[State
.cursor1
.line
+1].data
320 table.remove(State
.lines
, State
.cursor1
.line
+1)
321 table.remove(State
.line_cache
, State
.cursor1
.line
+1)
323 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
325 record_undo_event(State
, {before
=before
, after
=snapshot(State
, State
.cursor1
.line
)})
326 --== shortcuts that move the cursor
327 elseif chord
== 'left' then
329 State
.selection1
= {}
330 elseif chord
== 'right' then
332 State
.selection1
= {}
333 elseif chord
== 'S-left' then
334 if State
.selection1
.line
== nil then
335 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
338 elseif chord
== 'S-right' then
339 if State
.selection1
.line
== nil then
340 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
343 -- C- hotkeys reserved for drawings, so we'll use M-
344 elseif chord
== 'M-left' then
345 Text
.word_left(State
)
346 State
.selection1
= {}
347 elseif chord
== 'M-right' then
348 Text
.word_right(State
)
349 State
.selection1
= {}
350 elseif chord
== 'M-S-left' then
351 if State
.selection1
.line
== nil then
352 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
354 Text
.word_left(State
)
355 elseif chord
== 'M-S-right' then
356 if State
.selection1
.line
== nil then
357 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
359 Text
.word_right(State
)
360 elseif chord
== 'home' then
361 Text
.start_of_line(State
)
362 State
.selection1
= {}
363 elseif chord
== 'end' then
364 Text
.end_of_line(State
)
365 State
.selection1
= {}
366 elseif chord
== 'S-home' then
367 if State
.selection1
.line
== nil then
368 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
370 Text
.start_of_line(State
)
371 elseif chord
== 'S-end' then
372 if State
.selection1
.line
== nil then
373 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
375 Text
.end_of_line(State
)
376 elseif chord
== 'up' then
378 State
.selection1
= {}
379 elseif chord
== 'down' then
381 State
.selection1
= {}
382 elseif chord
== 'S-up' then
383 if State
.selection1
.line
== nil then
384 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
387 elseif chord
== 'S-down' then
388 if State
.selection1
.line
== nil then
389 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
392 elseif chord
== 'pageup' then
394 State
.selection1
= {}
395 elseif chord
== 'pagedown' then
397 State
.selection1
= {}
398 elseif chord
== 'S-pageup' then
399 if State
.selection1
.line
== nil then
400 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
403 elseif chord
== 'S-pagedown' then
404 if State
.selection1
.line
== nil then
405 State
.selection1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
}
411 function Text
.insert_return(State
)
412 local byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
)
413 table.insert(State
.lines
, State
.cursor1
.line
+1, {mode
='text', data
=string.sub(State
.lines
[State
.cursor1
.line
].data
, byte_offset
)})
414 table.insert(State
.line_cache
, State
.cursor1
.line
+1, {})
415 State
.lines
[State
.cursor1
.line
].data
= string.sub(State
.lines
[State
.cursor1
.line
].data
, 1, byte_offset
-1)
416 Text
.clear_screen_line_cache(State
, State
.cursor1
.line
)
417 State
.cursor1
= {line
=State
.cursor1
.line
+1, pos
=1}
420 function Text
.pageup(State
)
422 -- duplicate some logic from love.draw
423 local top2
= Text
.to2(State
, State
.screen_top1
)
424 --? print(App.screen.height)
425 local y
= App
.screen
.height
- State
.line_height
426 while y
>= State
.top
do
427 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
428 if State
.screen_top1
.line
== 1 and State
.screen_top1
.pos
== 1 then break end
429 if State
.lines
[State
.screen_top1
.line
].mode
== 'text' then
430 y
= y
- State
.line_height
431 elseif State
.lines
[State
.screen_top1
.line
].mode
== 'drawing' then
432 y
= y
- Drawing_padding_height
- Drawing
.pixels(State
.lines
[State
.screen_top1
.line
].h
, State
.width
)
434 top2
= Text
.previous_screen_line(State
, top2
)
436 State
.screen_top1
= Text
.to1(State
, top2
)
437 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
438 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
439 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
440 --? print('pageup end')
443 function Text
.pagedown(State
)
444 --? print('pagedown')
445 State
.screen_top1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
446 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
447 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
448 Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
449 --? print('top now', State.screen_top1.line)
450 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
451 --? print('pagedown end')
454 function Text
.up(State
)
455 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
456 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
457 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
458 if screen_line_starting_pos
== 1 then
459 --? print('cursor is at first screen line of its line')
460 -- line is done; skip to previous text line
461 local new_cursor_line
= State
.cursor1
.line
462 while new_cursor_line
> 1 do
463 new_cursor_line
= new_cursor_line
-1
464 if State
.lines
[new_cursor_line
].mode
== 'text' then
465 --? print('found previous text line')
466 State
.cursor1
= {line
=new_cursor_line
, pos
=nil}
467 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
468 -- previous text line found, pick its final screen line
469 --? print('has multiple screen lines')
470 local screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
471 --? print(#screen_line_starting_pos)
472 screen_line_starting_pos
= screen_line_starting_pos
[#screen_line_starting_pos
]
473 local screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_pos
)
474 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, screen_line_starting_byte_offset
)
475 State
.cursor1
.pos
= screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
480 -- move up one screen line in current line
481 assert(screen_line_index
> 1)
482 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
-1]
483 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
484 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
485 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
486 --? print('cursor pos is now '..tostring(State.cursor1.pos))
488 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
489 State
.screen_top1
= {
490 line
=State
.cursor1
.line
,
491 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
493 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
497 function Text
.down(State
)
498 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
499 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
500 assert(State
.cursor1
.pos
)
501 if Text
.cursor_at_final_screen_line(State
) then
502 -- line is done, skip to next text line
503 --? print('cursor at final screen line of its line')
504 local new_cursor_line
= State
.cursor1
.line
505 while new_cursor_line
< #State
.lines
do
506 new_cursor_line
= new_cursor_line
+1
507 if State
.lines
[new_cursor_line
].mode
== 'text' then
509 line
= new_cursor_line
,
510 pos
= Text
.nearest_cursor_pos(State
.lines
[new_cursor_line
].data
, State
.cursor_x
, State
.left
),
512 --? print(State.cursor1.pos)
516 if State
.cursor1
.line
> State
.screen_bottom1
.line
then
517 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
518 --? print('scroll up preserving cursor')
519 Text
.snap_cursor_to_bottom_of_screen(State
)
520 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
523 -- move down one screen line in current line
524 local scroll_down
= Text
.le1(State
.screen_bottom1
, State
.cursor1
)
525 --? print('cursor is NOT at final screen line of its line')
526 local screen_line_starting_pos
, screen_line_index
= Text
.pos_at_start_of_screen_line(State
, State
.cursor1
)
527 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
528 local new_screen_line_starting_pos
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
[screen_line_index
+1]
529 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
530 local new_screen_line_starting_byte_offset
= Text
.offset(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_pos
)
531 local s
= string.sub(State
.lines
[State
.cursor1
.line
].data
, new_screen_line_starting_byte_offset
)
532 State
.cursor1
.pos
= new_screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, State
.cursor_x
, State
.left
) - 1
533 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
535 --? print('scroll up preserving cursor')
536 Text
.snap_cursor_to_bottom_of_screen(State
)
537 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
540 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
543 function Text
.start_of_line(State
)
544 State
.cursor1
.pos
= 1
545 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
546 State
.screen_top1
= {line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
} -- copy
550 function Text
.end_of_line(State
)
551 State
.cursor1
.pos
= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) + 1
552 if Text
.cursor_out_of_screen(State
) then
553 Text
.snap_cursor_to_bottom_of_screen(State
)
557 function Text
.word_left(State
)
558 -- skip some whitespace
560 if State
.cursor1
.pos
== 1 then
563 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%S') then
568 -- skip some non-whitespace
571 if State
.cursor1
.pos
== 1 then
574 assert(State
.cursor1
.pos
> 1)
575 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
-1, '%s') then
581 function Text
.word_right(State
)
582 -- skip some whitespace
584 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
587 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%S') then
590 Text
.right_without_scroll(State
)
593 Text
.right_without_scroll(State
)
594 if State
.cursor1
.pos
> utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
597 if Text
.match(State
.lines
[State
.cursor1
.line
].data
, State
.cursor1
.pos
, '%s') then
601 if Text
.cursor_out_of_screen(State
) then
602 Text
.snap_cursor_to_bottom_of_screen(State
)
606 function Text
.match(s
, pos
, pat
)
607 local start_offset
= Text
.offset(s
, pos
)
609 local end_offset
= Text
.offset(s
, pos
+1)
610 assert(end_offset
> start_offset
)
611 local curr
= s
:sub(start_offset
, end_offset
-1)
612 return curr
:match(pat
)
615 function Text
.left(State
)
616 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
617 if State
.cursor1
.pos
> 1 then
618 State
.cursor1
.pos
= State
.cursor1
.pos
-1
620 local new_cursor_line
= State
.cursor1
.line
621 while new_cursor_line
> 1 do
622 new_cursor_line
= new_cursor_line
-1
623 if State
.lines
[new_cursor_line
].mode
== 'text' then
625 line
= new_cursor_line
,
626 pos
= utf8
.len(State
.lines
[new_cursor_line
].data
) + 1,
632 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
633 State
.screen_top1
= {
634 line
=State
.cursor1
.line
,
635 pos
=Text
.pos_at_start_of_screen_line(State
, State
.cursor1
),
637 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
641 function Text
.right(State
)
642 Text
.right_without_scroll(State
)
643 if Text
.cursor_out_of_screen(State
) then
644 Text
.snap_cursor_to_bottom_of_screen(State
)
648 function Text
.right_without_scroll(State
)
649 assert(State
.lines
[State
.cursor1
.line
].mode
== 'text')
650 if State
.cursor1
.pos
<= utf8
.len(State
.lines
[State
.cursor1
.line
].data
) then
651 State
.cursor1
.pos
= State
.cursor1
.pos
+1
653 local new_cursor_line
= State
.cursor1
.line
654 while new_cursor_line
<= #State
.lines
-1 do
655 new_cursor_line
= new_cursor_line
+1
656 if State
.lines
[new_cursor_line
].mode
== 'text' then
657 State
.cursor1
= {line
=new_cursor_line
, pos
=1}
664 -- result: pos, index of screen line
665 function Text
.pos_at_start_of_screen_line(State
, loc1
)
666 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
667 local line_cache
= State
.line_cache
[loc1
.line
]
668 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
669 local spos
= line_cache
.screen_line_starting_pos
[i
]
670 if spos
<= loc1
.pos
then
677 function Text
.pos_at_end_of_screen_line(State
, loc1
)
678 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
679 local line_cache
= State
.line_cache
[loc1
.line
]
680 local most_recent_final_pos
= utf8
.len(State
.lines
[loc1
.line
].data
)+1
681 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
682 local spos
= line_cache
.screen_line_starting_pos
[i
]
683 if spos
<= loc1
.pos
then
684 return most_recent_final_pos
686 most_recent_final_pos
= spos
-1
691 function Text
.cursor_at_final_screen_line(State
)
692 Text
.populate_screen_line_starting_pos(State
, State
.cursor1
.line
)
693 local screen_lines
= State
.line_cache
[State
.cursor1
.line
].screen_line_starting_pos
694 --? print(screen_lines[#screen_lines], State.cursor1.pos)
695 return screen_lines
[#screen_lines
] <= State
.cursor1
.pos
698 function Text
.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State
)
700 while State
.cursor1
.line
<= #State
.lines
do
701 if State
.lines
[State
.cursor1
.line
].mode
== 'text' then
704 --? print('cursor skips', State.cursor1.line)
705 y
= y
+ Drawing_padding_height
+ Drawing
.pixels(State
.lines
[State
.cursor1
.line
].h
, State
.width
)
706 State
.cursor1
.line
= State
.cursor1
.line
+ 1
708 if State
.cursor1
.pos
== nil then
709 State
.cursor1
.pos
= 1
711 -- hack: insert a text line at bottom of file if necessary
712 if State
.cursor1
.line
> #State
.lines
then
713 assert(State
.cursor1
.line
== #State
.lines
+1)
714 table.insert(State
.lines
, {mode
='text', data
=''})
715 table.insert(State
.line_cache
, {})
717 --? print(y, App.screen.height, App.screen.height-State.line_height)
718 if y
> App
.screen
.height
- State
.line_height
then
719 --? print('scroll up')
720 Text
.snap_cursor_to_bottom_of_screen(State
)
724 -- should never modify State.cursor1
725 function Text
.snap_cursor_to_bottom_of_screen(State
)
726 --? print('to2:', State.cursor1.line, State.cursor1.pos)
727 local top2
= Text
.to2(State
, State
.cursor1
)
728 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
729 -- slide to start of screen line
730 top2
.screen_pos
= 1 -- start of screen line
731 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
732 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
733 local y
= App
.screen
.height
- State
.line_height
734 -- duplicate some logic from love.draw
736 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
737 if top2
.line
== 1 and top2
.screen_line
== 1 then break end
738 if top2
.screen_line
> 1 or State
.lines
[top2
.line
-1].mode
== 'text' then
739 local h
= State
.line_height
740 if y
- h
< State
.top
then
745 assert(top2
.line
> 1)
746 assert(State
.lines
[top2
.line
-1].mode
== 'drawing')
747 -- We currently can't draw partial drawings, so either skip it entirely
749 local h
= Drawing_padding_height
+ Drawing
.pixels(State
.lines
[top2
.line
-1].h
, State
.width
)
750 if y
- h
< State
.top
then
753 --? print('skipping drawing of height', h)
756 top2
= Text
.previous_screen_line(State
, top2
)
758 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
759 State
.screen_top1
= Text
.to1(State
, top2
)
760 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
761 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
762 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
765 function Text
.in_line(State
, line_index
, x
,y
)
766 local line
= State
.lines
[line_index
]
767 local line_cache
= State
.line_cache
[line_index
]
768 if line_cache
.starty
== nil then return false end -- outside current page
769 if y
< line_cache
.starty
then return false end
770 Text
.populate_screen_line_starting_pos(State
, line_index
)
771 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)
774 -- convert mx,my in pixels to schema-1 coordinates
775 function Text
.to_pos_on_line(State
, line_index
, mx
, my
)
776 local line
= State
.lines
[line_index
]
777 local line_cache
= State
.line_cache
[line_index
]
778 assert(my
>= line_cache
.starty
)
779 -- duplicate some logic from Text.draw
780 local y
= line_cache
.starty
781 local start_screen_line_index
= Text
.screen_line_index(line_cache
.screen_line_starting_pos
, line_cache
.startpos
)
782 for screen_line_index
= start_screen_line_index
,#line_cache
.screen_line_starting_pos
do
783 local screen_line_starting_pos
= line_cache
.screen_line_starting_pos
[screen_line_index
]
784 local screen_line_starting_byte_offset
= Text
.offset(line
.data
, screen_line_starting_pos
)
785 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
786 local nexty
= y
+ State
.line_height
788 -- On all wrapped screen lines but the final one, clicks past end of
789 -- line position cursor on final character of screen line.
790 -- (The final screen line positions past end of screen line as always.)
791 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
792 --? print('past end of non-final line; return')
793 return line_cache
.screen_line_starting_pos
[screen_line_index
+1]-1
795 local s
= string.sub(line
.data
, screen_line_starting_byte_offset
)
796 --? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
797 return screen_line_starting_pos
+ Text
.nearest_cursor_pos(s
, mx
, State
.left
) - 1
804 function Text
.screen_line_width(State
, line_index
, i
)
805 local line
= State
.lines
[line_index
]
806 local line_cache
= State
.line_cache
[line_index
]
807 local start_pos
= line_cache
.screen_line_starting_pos
[i
]
808 local start_offset
= Text
.offset(line
.data
, start_pos
)
810 if i
< #line_cache
.screen_line_starting_pos
then
811 local past_end_pos
= line_cache
.screen_line_starting_pos
[i
+1]
812 local past_end_offset
= Text
.offset(line
.data
, past_end_pos
)
813 screen_line
= string.sub(line
.data
, start_offset
, past_end_offset
-1)
815 screen_line
= string.sub(line
.data
, start_pos
)
817 return App
.width(screen_line
)
820 function Text
.screen_line_index(screen_line_starting_pos
, pos
)
821 for i
= #screen_line_starting_pos
,1,-1 do
822 if screen_line_starting_pos
[i
] <= pos
then
828 -- convert x pixel coordinate to pos
829 -- oblivious to wrapping
830 -- result: 1 to len+1
831 function Text
.nearest_cursor_pos(line
, x
, left
)
835 local len
= utf8
.len(line
)
836 local max_x
= left
+Text
.x(line
, len
+1)
840 local leftpos
, rightpos
= 1, len
+1
841 --? print('-- nearest', x)
843 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
844 if leftpos
== rightpos
then
847 local curr
= math
.floor((leftpos
+rightpos
)/2)
848 local currxmin
= left
+Text
.x(line
, curr
)
849 local currxmax
= left
+Text
.x(line
, curr
+1)
850 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
851 if currxmin
<= x
and x
< currxmax
then
852 if x
-currxmin
< currxmax
-x
then
858 if leftpos
>= rightpos
-1 then
870 -- return the nearest index of line (in utf8 code points) which lies entirely
871 -- within x pixels of the left margin
872 -- result: 0 to len+1
873 function Text
.nearest_pos_less_than(line
, x
)
874 --? print('', '-- nearest_pos_less_than', line, x)
875 local len
= utf8
.len(line
)
876 local max_x
= Text
.x_after(line
, len
)
880 local left
, right
= 0, len
+1
882 local curr
= math
.floor((left
+right
)/2)
883 local currxmin
= Text
.x_after(line
, curr
+1)
884 local currxmax
= Text
.x_after(line
, curr
+2)
885 --? print('', x, left, right, curr, currxmin, currxmax)
886 if currxmin
<= x
and x
< currxmax
then
889 if left
>= right
-1 then
901 function Text
.x_after(s
, pos
)
902 local offset
= Text
.offset(s
, math
.min(pos
+1, #s
+1))
903 local s_before
= s
:sub(1, offset
-1)
904 --? print('^'..s_before..'$')
905 return App
.width(s_before
)
908 function Text
.x(s
, pos
)
909 local offset
= Text
.offset(s
, pos
)
910 local s_before
= s
:sub(1, offset
-1)
911 return App
.width(s_before
)
914 function Text
.to2(State
, loc1
)
915 if State
.lines
[loc1
.line
].mode
== 'drawing' then
916 return {line
=loc1
.line
, screen_line
=1, screen_pos
=1}
918 local result
= {line
=loc1
.line
}
919 local line_cache
= State
.line_cache
[loc1
.line
]
920 Text
.populate_screen_line_starting_pos(State
, loc1
.line
)
921 for i
=#line_cache
.screen_line_starting_pos
,1,-1 do
922 local spos
= line_cache
.screen_line_starting_pos
[i
]
923 if spos
<= loc1
.pos
then
924 result
.screen_line
= i
925 result
.screen_pos
= loc1
.pos
- spos
+ 1
929 assert(result
.screen_pos
)
933 function Text
.to1(State
, loc2
)
934 local result
= {line
=loc2
.line
, pos
=loc2
.screen_pos
}
935 if loc2
.screen_line
> 1 then
936 result
.pos
= State
.line_cache
[loc2
.line
].screen_line_starting_pos
[loc2
.screen_line
] + loc2
.screen_pos
- 1
941 function Text
.eq1(a
, b
)
942 return a
.line
== b
.line
and a
.pos
== b
.pos
945 function Text
.lt1(a
, b
)
946 if a
.line
< b
.line
then
949 if a
.line
> b
.line
then
955 function Text
.le1(a
, b
)
956 if a
.line
< b
.line
then
959 if a
.line
> b
.line
then
962 return a
.pos
<= b
.pos
965 function Text
.offset(s
, pos1
)
966 if pos1
== 1 then return 1 end
967 local result
= utf8
.offset(s
, pos1
)
968 if result
== nil then
975 function Text
.previous_screen_line(State
, loc2
)
976 if loc2
.screen_line
> 1 then
977 return {line
=loc2
.line
, screen_line
=loc2
.screen_line
-1, screen_pos
=1}
978 elseif loc2
.line
== 1 then
980 elseif State
.lines
[loc2
.line
-1].mode
== 'drawing' then
981 return {line
=loc2
.line
-1, screen_line
=1, screen_pos
=1}
983 local l
= State
.lines
[loc2
.line
-1]
984 Text
.populate_screen_line_starting_pos(State
, loc2
.line
-1)
985 return {line
=loc2
.line
-1, screen_line
=#State
.line_cache
[loc2
.line
-1].screen_line_starting_pos
, screen_pos
=1}
990 function Text
.tweak_screen_top_and_cursor(State
)
991 if State
.screen_top1
.pos
== 1 then return end
992 Text
.populate_screen_line_starting_pos(State
, State
.screen_top1
.line
)
993 local line
= State
.lines
[State
.screen_top1
.line
]
994 local line_cache
= State
.line_cache
[State
.screen_top1
.line
]
995 for i
=2,#line_cache
.screen_line_starting_pos
do
996 local pos
= line_cache
.screen_line_starting_pos
[i
]
997 if pos
== State
.screen_top1
.pos
then
1000 if pos
> State
.screen_top1
.pos
then
1001 -- make sure screen top is at start of a screen line
1002 local prev
= line_cache
.screen_line_starting_pos
[i
-1]
1003 if State
.screen_top1
.pos
- prev
< pos
- State
.screen_top1
.pos
then
1004 State
.screen_top1
.pos
= prev
1006 State
.screen_top1
.pos
= pos
1011 -- make sure cursor is on screen
1012 if Text
.lt1(State
.cursor1
, State
.screen_top1
) then
1013 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
1014 elseif State
.cursor1
.line
>= State
.screen_bottom1
.line
then
1015 --? print('too low')
1016 if Text
.cursor_out_of_screen(State
) then
1019 line
=State
.screen_bottom1
.line
,
1020 pos
=Text
.to_pos_on_line(State
, State
.screen_bottom1
.line
, State
.right
-5, App
.screen
.height
-5),
1026 -- slightly expensive since it redraws the screen
1027 function Text
.cursor_out_of_screen(State
)
1029 return State
.cursor_y
== nil
1030 -- this approach is cheaper and almost works, except on the final screen
1031 -- where file ends above bottom of screen
1032 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
1033 --? local botline1 = {line=State.cursor1.line, pos=botpos}
1034 --? return Text.lt1(State.screen_bottom1, botline1)
1037 function Text
.redraw_all(State
)
1038 --? print('clearing fragments')
1039 State
.line_cache
= {}
1040 for i
=1,#State
.lines
do
1041 State
.line_cache
[i
] = {}
1045 function Text
.clear_screen_line_cache(State
, line_index
)
1046 State
.line_cache
[line_index
].screen_line_starting_pos
= nil
1047 State
.line_cache
[line_index
].link_offsets
= nil
1051 return s
:gsub('^%s+', ''):gsub('%s+$', '')
1055 return s
:gsub('^%s+', '')
1059 return s
:gsub('%s+$', '')
1062 function starts_with(s
, prefix
)
1063 if #s
< #prefix
then
1067 if s
:sub(i
,i
) ~= prefix
:sub(i
,i
) then
1074 function ends_with(s
, suffix
)
1075 if #s
< #suffix
then
1078 for i
=0,#suffix
-1 do
1079 if s
:sub(#s
-i
,#s
-i
) ~= suffix
:sub(#suffix
-i
,#suffix
-i
) then