Merge lines.love
[view.love.git] / text.lua
blob82b4ab8314e6b4d7c0a83b35d82f47c62caa92a0
1 -- text editor, particularly text drawing, horizontal wrap, vertical scrolling
2 Text = {}
4 -- draw a line starting from startpos to screen at y between State.left and State.right
5 -- return y for the next line
6 function Text.draw(State, line_index, y, startpos)
7 --? print('text.draw', line_index, y)
8 local line = State.lines[line_index]
9 local line_cache = State.line_cache[line_index]
10 line_cache.startpos = startpos
11 -- wrap long lines
12 Text.populate_screen_line_starting_pos(State, line_index)
13 assert(#line_cache.screen_line_starting_pos >= 1, 'line cache missing screen line info')
14 for i=1,#line_cache.screen_line_starting_pos do
15 local pos = line_cache.screen_line_starting_pos[i]
16 if pos < startpos then
17 -- render nothing
18 else
19 local screen_line = Text.screen_line(line, line_cache, i)
20 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
21 local frag_len = utf8.len(screen_line)
22 -- render any highlights
23 if State.selection1.line then
24 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
25 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
26 end
27 if line_index == State.cursor1.line then
28 -- render search highlight or cursor
29 if State.search_term then
30 local data = State.lines[State.cursor1.line].data
31 local cursor_offset = Text.offset(data, State.cursor1.pos)
32 if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
33 local save_selection = State.selection1
34 State.selection1 = {line=line_index, pos=State.cursor1.pos+utf8.len(State.search_term)}
35 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
36 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
37 State.selection1 = save_selection
38 end
39 else
40 if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
41 Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
42 elseif pos + frag_len == State.cursor1.pos then
43 -- Show cursor at end of line.
44 -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
45 -- It seems useful to see a cursor whether your eye is on the left or right margin.
46 Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
47 end
48 end
49 end
50 -- render screen line
51 App.color(Text_color)
52 App.screen.print(screen_line, State.left,y)
53 y = y + State.line_height
54 if y >= App.screen.height then
55 break
56 end
57 end
58 end
59 return y
60 end
62 function Text.screen_line(line, line_cache, i)
63 local pos = line_cache.screen_line_starting_pos[i]
64 local offset = Text.offset(line.data, pos)
65 if i >= #line_cache.screen_line_starting_pos then
66 return line.data:sub(offset)
67 end
68 local endpos = line_cache.screen_line_starting_pos[i+1]-1
69 local end_offset = Text.offset(line.data, endpos)
70 return line.data:sub(offset, end_offset)
71 end
73 function Text.draw_cursor(State, x, y)
74 -- blink every 0.5s
75 if math.floor(Cursor_time*2)%2 == 0 then
76 App.color(Cursor_color)
77 love.graphics.rectangle('fill', x,y, 3,State.line_height)
78 end
79 State.cursor_x = x
80 State.cursor_y = y+State.line_height
81 end
83 function Text.populate_screen_line_starting_pos(State, line_index)
84 local line = State.lines[line_index]
85 local line_cache = State.line_cache[line_index]
86 if line_cache.screen_line_starting_pos then
87 return
88 end
89 line_cache.screen_line_starting_pos = {1}
90 local x = 0
91 local pos = 1
92 -- try to wrap at word boundaries
93 for frag in line.data:gmatch('%S*%s*') do
94 local frag_width = State.font:getWidth(frag)
95 --? print('-- frag:', frag, pos, x, frag_width, State.width)
96 while x + frag_width > State.width do
97 --? print('frag:', frag, pos, x, frag_width, State.width)
98 if x < 0.8 * State.width then
99 -- long word; chop it at some letter
100 -- We're not going to reimplement TeX here.
101 local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
102 if x == 0 and bpos == 0 then
103 assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
105 pos = pos + bpos
106 local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
107 frag = string.sub(frag, boffset)
108 --? if bpos > 0 then
109 --? print('after chop:', frag)
110 --? end
111 frag_width = State.font:getWidth(frag)
113 --? print('screen line:', pos)
114 table.insert(line_cache.screen_line_starting_pos, pos)
115 x = 0 -- new screen line
117 x = x + frag_width
118 pos = pos + utf8.len(frag)
122 function Text.text_input(State, t)
123 if App.mouse_down(1) then return end
124 if App.any_modifier_down() then
125 if App.key_down(t) then
126 -- The modifiers didn't change the key. Handle it in keychord_press.
127 return
128 else
129 -- Key mutated by the keyboard layout. Continue below.
132 local before = snapshot(State, State.cursor1.line)
133 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
134 Text.insert_at_cursor(State, t)
135 if State.cursor_y > App.screen.height - State.line_height then
136 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
137 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
139 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
142 function Text.insert_at_cursor(State, t)
143 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
144 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)
145 Text.clear_screen_line_cache(State, State.cursor1.line)
146 State.cursor1.pos = State.cursor1.pos+1
149 -- Don't handle any keys here that would trigger text_input above.
150 function Text.keychord_press(State, chord)
151 --? print('chord', chord, State.selection1.line, State.selection1.pos)
152 --== shortcuts that mutate text (must schedule_save)
153 if chord == 'return' then
154 local before_line = State.cursor1.line
155 local before = snapshot(State, before_line)
156 Text.insert_return(State)
157 if State.cursor_y > App.screen.height - State.line_height then
158 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
160 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
161 schedule_save(State)
162 elseif chord == 'tab' then
163 local before = snapshot(State, State.cursor1.line)
164 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
165 Text.insert_at_cursor(State, '\t')
166 if State.cursor_y > App.screen.height - State.line_height then
167 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
168 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
169 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
171 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
172 schedule_save(State)
173 elseif chord == 'backspace' then
174 if State.selection1.line then
175 Text.delete_selection_and_record_undo_event(State)
176 schedule_save(State)
177 return
179 local before
180 if State.cursor1.pos > 1 then
181 before = snapshot(State, State.cursor1.line)
182 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
183 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
184 if byte_start then
185 if byte_end then
186 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)
187 else
188 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
190 State.cursor1.pos = State.cursor1.pos-1
192 elseif State.cursor1.line > 1 then
193 before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
194 -- join lines
195 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
196 State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
197 table.remove(State.lines, State.cursor1.line)
198 table.remove(State.line_cache, State.cursor1.line)
199 State.cursor1.line = State.cursor1.line-1
201 if State.screen_top1.line > #State.lines then
202 Text.populate_screen_line_starting_pos(State, #State.lines)
203 local line_cache = State.line_cache[#State.line_cache]
204 State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
205 elseif Text.lt1(State.cursor1, State.screen_top1) then
206 State.screen_top1 = {
207 line=State.cursor1.line,
208 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
210 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
212 Text.clear_screen_line_cache(State, State.cursor1.line)
213 assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
214 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
215 schedule_save(State)
216 elseif chord == 'delete' then
217 if State.selection1.line then
218 Text.delete_selection_and_record_undo_event(State)
219 schedule_save(State)
220 return
222 local before
223 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
224 before = snapshot(State, State.cursor1.line)
225 else
226 before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
228 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
229 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
230 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
231 if byte_start then
232 if byte_end then
233 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)
234 else
235 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
237 -- no change to State.cursor1.pos
239 elseif State.cursor1.line < #State.lines then
240 -- join lines
241 State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
242 table.remove(State.lines, State.cursor1.line+1)
243 table.remove(State.line_cache, State.cursor1.line+1)
245 Text.clear_screen_line_cache(State, State.cursor1.line)
246 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
247 schedule_save(State)
248 --== shortcuts that move the cursor
249 elseif chord == 'left' then
250 Text.left(State)
251 State.selection1 = {}
252 elseif chord == 'right' then
253 Text.right(State)
254 State.selection1 = {}
255 elseif chord == 'S-left' then
256 if State.selection1.line == nil then
257 State.selection1 = deepcopy(State.cursor1)
259 Text.left(State)
260 elseif chord == 'S-right' then
261 if State.selection1.line == nil then
262 State.selection1 = deepcopy(State.cursor1)
264 Text.right(State)
265 -- C- hotkeys reserved for drawings, so we'll use M-
266 elseif chord == 'M-left' then
267 Text.word_left(State)
268 State.selection1 = {}
269 elseif chord == 'M-right' then
270 Text.word_right(State)
271 State.selection1 = {}
272 elseif chord == 'M-S-left' then
273 if State.selection1.line == nil then
274 State.selection1 = deepcopy(State.cursor1)
276 Text.word_left(State)
277 elseif chord == 'M-S-right' then
278 if State.selection1.line == nil then
279 State.selection1 = deepcopy(State.cursor1)
281 Text.word_right(State)
282 elseif chord == 'home' then
283 Text.start_of_line(State)
284 State.selection1 = {}
285 elseif chord == 'end' then
286 Text.end_of_line(State)
287 State.selection1 = {}
288 elseif chord == 'S-home' then
289 if State.selection1.line == nil then
290 State.selection1 = deepcopy(State.cursor1)
292 Text.start_of_line(State)
293 elseif chord == 'S-end' then
294 if State.selection1.line == nil then
295 State.selection1 = deepcopy(State.cursor1)
297 Text.end_of_line(State)
298 elseif chord == 'up' then
299 Text.up(State)
300 State.selection1 = {}
301 elseif chord == 'down' then
302 Text.down(State)
303 State.selection1 = {}
304 elseif chord == 'S-up' then
305 if State.selection1.line == nil then
306 State.selection1 = deepcopy(State.cursor1)
308 Text.up(State)
309 elseif chord == 'S-down' then
310 if State.selection1.line == nil then
311 State.selection1 = deepcopy(State.cursor1)
313 Text.down(State)
314 elseif chord == 'pageup' then
315 Text.pageup(State)
316 State.selection1 = {}
317 elseif chord == 'pagedown' then
318 Text.pagedown(State)
319 State.selection1 = {}
320 elseif chord == 'S-pageup' then
321 if State.selection1.line == nil then
322 State.selection1 = deepcopy(State.cursor1)
324 Text.pageup(State)
325 elseif chord == 'S-pagedown' then
326 if State.selection1.line == nil then
327 State.selection1 = deepcopy(State.cursor1)
329 Text.pagedown(State)
333 function Text.insert_return(State)
334 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
335 table.insert(State.lines, State.cursor1.line+1, {data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
336 table.insert(State.line_cache, State.cursor1.line+1, {})
337 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
338 Text.clear_screen_line_cache(State, State.cursor1.line)
339 State.cursor1 = {line=State.cursor1.line+1, pos=1}
342 function Text.pageup(State)
343 State.screen_top1 = Text.previous_screen_top1(State)
344 State.cursor1 = deepcopy(State.screen_top1)
345 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
346 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
349 -- return the top y coordinate of a given line_index,
350 -- or nil if no part of it is on screen
351 function Text.starty(State, line_index)
352 -- duplicate some logic from love.draw
353 -- does not modify State (except to populate line_cache)
354 if line_index < State.screen_top1.line then return end
355 local loc2 = Text.to2(State, State.screen_top1)
356 local y = State.top
357 while true do
358 if loc2.line == line_index then return y end
359 y = y + State.line_height
360 if y + State.line_height > App.screen.height then break end
361 local next_loc2 = Text.next_screen_line(State, loc2)
362 if Text.eq2(next_loc2, loc2) then break end -- end of file
363 loc2 = next_loc2
367 function Text.previous_screen_top1(State)
368 -- duplicate some logic from love.draw
369 -- does not modify State (except to populate line_cache)
370 local loc2 = Text.to2(State, State.screen_top1)
371 local y = App.screen.height - State.line_height
372 while y >= State.top do
373 if loc2.line == 1 and loc2.screen_line == 1 and loc2.screen_pos == 1 then break end
374 y = y - State.line_height
375 loc2 = Text.previous_screen_line(State, loc2)
377 return Text.to1(State, loc2)
380 function Text.pagedown(State)
381 State.screen_top1 = Text.screen_bottom1(State)
382 State.cursor1 = deepcopy(State.screen_top1)
383 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
384 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
387 -- return the location of the start of the bottom-most line on screen
388 function Text.screen_bottom1(State)
389 -- duplicate some logic from love.draw
390 -- does not modify State (except to populate line_cache)
391 local loc2 = Text.to2(State, State.screen_top1)
392 local y = State.top
393 while true do
394 y = y + State.line_height
395 if y + State.line_height > App.screen.height then break end
396 local next_loc2 = Text.next_screen_line(State, loc2)
397 if Text.eq2(next_loc2, loc2) then break end
398 loc2 = next_loc2
400 return Text.to1(State, loc2)
403 function Text.up(State)
404 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
405 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
406 if screen_line_starting_pos == 1 then
407 --? print('cursor is at first screen line of its line')
408 -- line is done; skip to previous text line
409 if State.cursor1.line > 1 then
410 local new_cursor_line = State.cursor1.line-1
411 --? print('found previous text line')
412 State.cursor1 = {line=new_cursor_line, pos=nil}
413 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
414 -- previous text line found, pick its final screen line
415 --? print('has multiple screen lines')
416 local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
417 --? print(#screen_line_starting_pos)
418 screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
419 local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
420 local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
421 State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
423 else
424 -- move up one screen line in current line
425 assert(screen_line_index > 1, 'bumped up against top screen line in line')
426 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
427 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
428 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
429 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
430 --? print('cursor pos is now '..tostring(State.cursor1.pos))
432 if Text.lt1(State.cursor1, State.screen_top1) then
433 State.screen_top1 = {
434 line=State.cursor1.line,
435 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
437 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
441 function Text.down(State)
442 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
443 assert(State.cursor1.pos, 'cursor has no pos')
444 if Text.cursor_at_final_screen_line(State) then
445 -- line is done, skip to next text line
446 --? print('cursor at final screen line of its line')
447 if State.cursor1.line < #State.lines then
448 local new_cursor_line = State.cursor1.line+1
449 State.cursor1.line = new_cursor_line
450 State.cursor1.pos = Text.nearest_cursor_pos(State.font, State.lines[State.cursor1.line].data, State.cursor_x, State.left)
451 --? print(State.cursor1.pos)
453 local screen_bottom1 = Text.screen_bottom1(State)
454 --? print('down 2', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, screen_bottom1.line, screen_bottom1.pos)
455 if State.cursor1.line > screen_bottom1.line then
456 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
457 --? print('scroll up preserving cursor')
458 Text.snap_cursor_to_bottom_of_screen(State)
459 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
461 else
462 -- move down one screen line in current line
463 local screen_bottom1 = Text.screen_bottom1(State)
464 local scroll_down = Text.le1(screen_bottom1, State.cursor1)
465 --? print('cursor is NOT at final screen line of its line')
466 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
467 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
468 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
469 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
470 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
471 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
472 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
473 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
474 if scroll_down then
475 --? print('scroll up preserving cursor')
476 Text.snap_cursor_to_bottom_of_screen(State)
477 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
480 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
483 function Text.start_of_line(State)
484 State.cursor1.pos = 1
485 if Text.lt1(State.cursor1, State.screen_top1) then
486 State.screen_top1 = deepcopy(State.cursor1)
490 function Text.end_of_line(State)
491 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
492 if Text.cursor_out_of_screen(State) then
493 Text.snap_cursor_to_bottom_of_screen(State)
497 function Text.word_left(State)
498 -- skip some whitespace
499 while true do
500 if State.cursor1.pos == 1 then
501 break
503 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
504 break
506 Text.left(State)
508 -- skip some non-whitespace
509 while true do
510 Text.left(State)
511 if State.cursor1.pos == 1 then
512 break
514 assert(State.cursor1.pos > 1, 'bumped up against start of line')
515 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
516 break
521 function Text.word_right(State)
522 -- skip some whitespace
523 while true do
524 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
525 break
527 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
528 break
530 Text.right_without_scroll(State)
532 while true do
533 Text.right_without_scroll(State)
534 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
535 break
537 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
538 break
541 if Text.cursor_out_of_screen(State) then
542 Text.snap_cursor_to_bottom_of_screen(State)
546 function Text.match(s, pos, pat)
547 local start_offset = Text.offset(s, pos)
548 local end_offset = Text.offset(s, pos+1)
549 assert(end_offset > start_offset, ('end_offset %d not > start_offset %d'):format(end_offset, start_offset))
550 local curr = s:sub(start_offset, end_offset-1)
551 return curr:match(pat)
554 function Text.left(State)
555 if State.cursor1.pos > 1 then
556 State.cursor1.pos = State.cursor1.pos-1
557 elseif State.cursor1.line > 1 then
558 State.cursor1.line = State.cursor1.line-1
559 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
561 if Text.lt1(State.cursor1, State.screen_top1) then
562 State.screen_top1 = {
563 line=State.cursor1.line,
564 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
566 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
570 function Text.right(State)
571 Text.right_without_scroll(State)
572 if Text.cursor_out_of_screen(State) then
573 Text.snap_cursor_to_bottom_of_screen(State)
577 function Text.right_without_scroll(State)
578 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
579 State.cursor1.pos = State.cursor1.pos+1
580 elseif State.cursor1.line <= #State.lines-1 then
581 State.cursor1.line = State.cursor1.line+1
582 State.cursor1.pos = 1
586 -- result: pos, index of screen line
587 function Text.pos_at_start_of_screen_line(State, loc1)
588 Text.populate_screen_line_starting_pos(State, loc1.line)
589 local line_cache = State.line_cache[loc1.line]
590 for i=#line_cache.screen_line_starting_pos,1,-1 do
591 local spos = line_cache.screen_line_starting_pos[i]
592 if spos <= loc1.pos then
593 return spos,i
596 assert(false, ('invalid pos %d'):format(loc1.pos))
599 function Text.pos_at_end_of_screen_line(State, loc1)
600 Text.populate_screen_line_starting_pos(State, loc1.line)
601 local line_cache = State.line_cache[loc1.line]
602 local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
603 for i=#line_cache.screen_line_starting_pos,1,-1 do
604 local spos = line_cache.screen_line_starting_pos[i]
605 if spos <= loc1.pos then
606 return most_recent_final_pos
608 most_recent_final_pos = spos-1
610 assert(false, ('invalid pos %d'):format(loc1.pos))
613 function Text.final_text_loc_on_screen(State)
614 local screen_bottom1 = Text.screen_bottom1(State)
615 return {
616 line=screen_bottom1.line,
617 pos=Text.pos_at_end_of_screen_line(State, screen_bottom1),
621 function Text.cursor_at_final_screen_line(State)
622 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
623 local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos
624 --? print(screen_lines[#screen_lines], State.cursor1.pos)
625 return screen_lines[#screen_lines] <= State.cursor1.pos
628 function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
629 if State.top > App.screen.height - State.line_height then
630 --? print('scroll up')
631 Text.snap_cursor_to_bottom_of_screen(State)
635 -- should never modify State.cursor1
636 function Text.snap_cursor_to_bottom_of_screen(State)
637 --? print('to2:', State.cursor1.line, State.cursor1.pos)
638 local top2 = Text.to2(State, State.cursor1)
639 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
640 -- slide to start of screen line
641 top2.screen_pos = 1 -- start of screen line
642 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
643 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
644 local y = App.screen.height - State.line_height
645 -- duplicate some logic from love.draw
646 while true do
647 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
648 if top2.line == 1 and top2.screen_line == 1 then break end
649 local h = State.line_height
650 if y - h < State.top then
651 break
653 y = y - h
654 top2 = Text.previous_screen_line(State, top2)
656 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
657 State.screen_top1 = Text.to1(State, top2)
658 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
659 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
660 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
663 function Text.in_line(State, line_index, x,y)
664 local line = State.lines[line_index]
665 local line_cache = State.line_cache[line_index]
666 local starty = Text.starty(State, line_index)
667 if starty == nil then return false end -- outside current page
668 if y < starty then return false end
669 Text.populate_screen_line_starting_pos(State, line_index)
670 return y < starty + State.line_height*(#line_cache.screen_line_starting_pos - Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos) + 1)
673 -- convert mx,my in pixels to schema-1 coordinates
674 function Text.to_pos_on_line(State, line_index, mx, my)
675 local line = State.lines[line_index]
676 local line_cache = State.line_cache[line_index]
677 local starty = Text.starty(State, line_index)
678 assert(my >= starty, 'failed to map y pixel to line')
679 -- duplicate some logic from Text.draw
680 local y = starty
681 local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
682 for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
683 local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
684 local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
685 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
686 local nexty = y + State.line_height
687 if my < nexty then
688 -- On all wrapped screen lines but the final one, clicks past end of
689 -- line position cursor on final character of screen line.
690 -- (The final screen line positions past end of screen line as always.)
691 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
692 --? print('past end of non-final line; return')
693 return line_cache.screen_line_starting_pos[screen_line_index+1]
695 local s = string.sub(line.data, screen_line_starting_byte_offset)
696 --? print('return', mx, Text.nearest_cursor_pos(State.font, s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1)
697 return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
699 y = nexty
701 assert(false, 'failed to map y pixel to line')
704 function Text.screen_line_width(State, line_index, i)
705 local line = State.lines[line_index]
706 local line_cache = State.line_cache[line_index]
707 local start_pos = line_cache.screen_line_starting_pos[i]
708 local start_offset = Text.offset(line.data, start_pos)
709 local screen_line
710 if i < #line_cache.screen_line_starting_pos then
711 local past_end_pos = line_cache.screen_line_starting_pos[i+1]
712 local past_end_offset = Text.offset(line.data, past_end_pos)
713 screen_line = string.sub(line.data, start_offset, past_end_offset-1)
714 else
715 screen_line = string.sub(line.data, start_pos)
717 return State.font:getWidth(screen_line)
720 function Text.screen_line_index(screen_line_starting_pos, pos)
721 for i = #screen_line_starting_pos,1,-1 do
722 if screen_line_starting_pos[i] <= pos then
723 return i
728 -- convert x pixel coordinate to pos
729 -- oblivious to wrapping
730 -- result: 1 to len+1
731 function Text.nearest_cursor_pos(font, line, x, left)
732 if x < left then
733 return 1
735 local len = utf8.len(line)
736 local max_x = left+Text.x(font, line, len+1)
737 if x > max_x then
738 return len+1
740 local leftpos, rightpos = 1, len+1
741 --? print('-- nearest', x)
742 while true do
743 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
744 if leftpos == rightpos then
745 return leftpos
747 local curr = math.floor((leftpos+rightpos)/2)
748 local currxmin = left+Text.x(font, line, curr)
749 local currxmax = left+Text.x(font, line, curr+1)
750 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
751 if currxmin <= x and x < currxmax then
752 if x-currxmin < currxmax-x then
753 return curr
754 else
755 return curr+1
758 if leftpos >= rightpos-1 then
759 return rightpos
761 if currxmin > x then
762 rightpos = curr
763 else
764 leftpos = curr
767 assert(false, 'failed to map x pixel to pos')
770 -- return the nearest index of line (in utf8 code points) which lies entirely
771 -- within x pixels of the left margin
772 -- result: 0 to len+1
773 function Text.nearest_pos_less_than(font, line, x)
774 --? print('', '-- nearest_pos_less_than', line, x)
775 local len = utf8.len(line)
776 local max_x = Text.x_after(font, line, len)
777 if x > max_x then
778 return len+1
780 local left, right = 0, len+1
781 while true do
782 local curr = math.floor((left+right)/2)
783 local currxmin = Text.x_after(font, line, curr+1)
784 local currxmax = Text.x_after(font, line, curr+2)
785 --? print('', x, left, right, curr, currxmin, currxmax)
786 if currxmin <= x and x < currxmax then
787 return curr
789 if left >= right-1 then
790 return left
792 if currxmin > x then
793 right = curr
794 else
795 left = curr
798 assert(false, 'failed to map x pixel to pos')
801 function Text.x_after(font, s, pos)
802 local len = utf8.len(s)
803 local offset = Text.offset(s, math.min(pos+1, len+1))
804 local s_before = s:sub(1, offset-1)
805 --? print('^'..s_before..'$')
806 return font:getWidth(s_before)
809 function Text.x(font, s, pos)
810 local offset = Text.offset(s, pos)
811 local s_before = s:sub(1, offset-1)
812 return font:getWidth(s_before)
815 function Text.to2(State, loc1)
816 local result = {line=loc1.line}
817 local line_cache = State.line_cache[loc1.line]
818 Text.populate_screen_line_starting_pos(State, loc1.line)
819 for i=#line_cache.screen_line_starting_pos,1,-1 do
820 local spos = line_cache.screen_line_starting_pos[i]
821 if spos <= loc1.pos then
822 result.screen_line = i
823 result.screen_pos = loc1.pos - spos + 1
824 break
827 assert(result.screen_pos, 'failed to convert schema-1 coordinate to schema-2')
828 return result
831 function Text.to1(State, loc2)
832 local result = {line=loc2.line, pos=loc2.screen_pos}
833 if loc2.screen_line > 1 then
834 result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
836 return result
839 function Text.eq1(a, b)
840 return a.line == b.line and a.pos == b.pos
843 function Text.lt1(a, b)
844 if a.line < b.line then
845 return true
847 if a.line > b.line then
848 return false
850 return a.pos < b.pos
853 function Text.le1(a, b)
854 if a.line < b.line then
855 return true
857 if a.line > b.line then
858 return false
860 return a.pos <= b.pos
863 function Text.eq2(a, b)
864 return a.line == b.line and a.screen_line == b.screen_line and a.screen_pos == b.screen_pos
867 function Text.offset(s, pos1)
868 if pos1 == 1 then return 1 end
869 local result = utf8.offset(s, pos1)
870 if result == nil then
871 assert(false, ('Text.offset(%d) called on a string of length %d (byte size %d); this is likely a failure to handle utf8\n\n^%s$\n'):format(pos1, utf8.len(s), #s, s))
873 return result
876 function Text.previous_screen_line(State, loc2)
877 if loc2.screen_line > 1 then
878 return {line=loc2.line, screen_line=loc2.screen_line-1, screen_pos=1}
879 elseif loc2.line == 1 then
880 return loc2
881 else
882 local l = State.lines[loc2.line-1]
883 Text.populate_screen_line_starting_pos(State, loc2.line-1)
884 return {line=loc2.line-1, screen_line=#State.line_cache[loc2.line-1].screen_line_starting_pos, screen_pos=1}
888 function Text.next_screen_line(State, loc2)
889 Text.populate_screen_line_starting_pos(State, loc2.line)
890 if loc2.screen_line >= #State.line_cache[loc2.line].screen_line_starting_pos then
891 if loc2.line < #State.lines then
892 return {line=loc2.line+1, screen_line=1, screen_pos=1}
893 else
894 return loc2
896 else
897 return {line=loc2.line, screen_line=loc2.screen_line+1, screen_pos=1}
901 -- resize helper
902 function Text.tweak_screen_top_and_cursor(State)
903 if State.screen_top1.pos == 1 then return end
904 Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
905 local line = State.lines[State.screen_top1.line]
906 local line_cache = State.line_cache[State.screen_top1.line]
907 for i=2,#line_cache.screen_line_starting_pos do
908 local pos = line_cache.screen_line_starting_pos[i]
909 if pos == State.screen_top1.pos then
910 break
912 if pos > State.screen_top1.pos then
913 -- make sure screen top is at start of a screen line
914 local prev = line_cache.screen_line_starting_pos[i-1]
915 if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
916 State.screen_top1.pos = prev
917 else
918 State.screen_top1.pos = pos
920 break
923 -- make sure cursor is on screen
924 local screen_bottom1 = Text.screen_bottom1(State)
925 if Text.lt1(State.cursor1, State.screen_top1) then
926 State.cursor1 = deepcopy(State.screen_top1)
927 elseif State.cursor1.line >= screen_bottom1.line then
928 if Text.cursor_out_of_screen(State) then
929 State.cursor1 = Text.final_text_loc_on_screen(State)
934 -- slightly expensive since it redraws the screen
935 function Text.cursor_out_of_screen(State)
936 edit.draw(State)
937 return State.cursor_y == nil
940 function Text.redraw_all(State)
941 --? print('clearing line caches')
942 -- Perform some early sanity checking here, in hopes that we correctly call
943 -- this whenever we change editor state.
944 if State.right <= State.left then
945 assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left))
948 State.line_cache = {}
949 for i=1,#State.lines do
950 State.line_cache[i] = {}
954 function Text.clear_screen_line_cache(State, line_index)
955 State.line_cache[line_index].screen_line_starting_pos = nil
958 function trim(s)
959 return s:gsub('^%s+', ''):gsub('%s+$', '')
962 function ltrim(s)
963 return s:gsub('^%s+', '')
966 function rtrim(s)
967 return s:gsub('%s+$', '')
970 function starts_with(s, prefix)
971 if #s < #prefix then
972 return false
974 for i=1,#prefix do
975 if s:sub(i,i) ~= prefix:sub(i,i) then
976 return false
979 return true
982 function ends_with(s, suffix)
983 if #s < #suffix then
984 return false
986 for i=0,#suffix-1 do
987 if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
988 return false
991 return true