clean up some issues I now feel confident about
[lines.love.git] / text.lua
blob14eb4b1ddb306011a0a7bfa151fd26df27da32d0
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 if line.mode ~= 'text' then return end
86 local line_cache = State.line_cache[line_index]
87 if line_cache.screen_line_starting_pos then
88 return
89 end
90 line_cache.screen_line_starting_pos = {1}
91 local x = 0
92 local pos = 1
93 -- try to wrap at word boundaries
94 for frag in line.data:gmatch('%S*%s*') do
95 local frag_width = State.font:getWidth(frag)
96 --? print('-- frag:', frag, pos, x, frag_width, State.width)
97 while x + frag_width > State.width do
98 --? print('frag:', frag, pos, x, frag_width, State.width)
99 if x < 0.8 * State.width then
100 -- long word; chop it at some letter
101 -- We're not going to reimplement TeX here.
102 local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
103 if x == 0 and bpos == 0 then
104 assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
106 pos = pos + bpos
107 local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
108 frag = string.sub(frag, boffset)
109 --? if bpos > 0 then
110 --? print('after chop:', frag)
111 --? end
112 frag_width = State.font:getWidth(frag)
114 --? print('screen line:', pos)
115 table.insert(line_cache.screen_line_starting_pos, pos)
116 x = 0 -- new screen line
118 x = x + frag_width
119 pos = pos + utf8.len(frag)
123 function Text.text_input(State, t)
124 if App.mouse_down(1) then return end
125 if App.any_modifier_down() then
126 if App.key_down(t) then
127 -- The modifiers didn't change the key. Handle it in keychord_press.
128 return
129 else
130 -- Key mutated by the keyboard layout. Continue below.
133 local before = snapshot(State, State.cursor1.line)
134 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
135 Text.insert_at_cursor(State, t)
136 if State.cursor_y > App.screen.height - State.line_height then
137 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
138 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
140 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
143 function Text.insert_at_cursor(State, t)
144 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
145 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
146 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)
147 Text.clear_screen_line_cache(State, State.cursor1.line)
148 State.cursor1.pos = State.cursor1.pos+1
151 -- Don't handle any keys here that would trigger text_input above.
152 function Text.keychord_press(State, chord)
153 --? print('chord', chord, State.selection1.line, State.selection1.pos)
154 --== shortcuts that mutate text (must schedule_save)
155 if chord == 'return' then
156 local before_line = State.cursor1.line
157 local before = snapshot(State, before_line)
158 Text.insert_return(State)
159 if State.cursor_y > App.screen.height - State.line_height then
160 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
162 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
163 schedule_save(State)
164 elseif chord == 'tab' then
165 local before = snapshot(State, State.cursor1.line)
166 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
167 Text.insert_at_cursor(State, '\t')
168 if State.cursor_y > App.screen.height - State.line_height then
169 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
170 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
171 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
173 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
174 schedule_save(State)
175 elseif chord == 'backspace' then
176 if State.selection1.line then
177 Text.delete_selection_and_record_undo_event(State)
178 schedule_save(State)
179 return
181 local before
182 if State.cursor1.pos > 1 then
183 before = snapshot(State, State.cursor1.line)
184 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
185 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
186 if byte_start then
187 if byte_end then
188 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)
189 else
190 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
192 State.cursor1.pos = State.cursor1.pos-1
194 elseif State.cursor1.line > 1 then
195 before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
196 if State.lines[State.cursor1.line-1].mode == 'drawing' then
197 table.remove(State.lines, State.cursor1.line-1)
198 table.remove(State.line_cache, State.cursor1.line-1)
199 else
200 -- join lines
201 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
202 State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
203 table.remove(State.lines, State.cursor1.line)
204 table.remove(State.line_cache, State.cursor1.line)
206 State.cursor1.line = State.cursor1.line-1
208 if State.screen_top1.line > #State.lines then
209 Text.populate_screen_line_starting_pos(State, #State.lines)
210 local line_cache = State.line_cache[#State.line_cache]
211 State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
212 elseif Text.lt1(State.cursor1, State.screen_top1) then
213 State.screen_top1 = {
214 line=State.cursor1.line,
215 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
217 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
219 Text.clear_screen_line_cache(State, State.cursor1.line)
220 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))
221 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
222 schedule_save(State)
223 elseif chord == 'delete' then
224 if State.selection1.line then
225 Text.delete_selection_and_record_undo_event(State)
226 schedule_save(State)
227 return
229 local before
230 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
231 before = snapshot(State, State.cursor1.line)
232 else
233 before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
235 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
236 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
237 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
238 if byte_start then
239 if byte_end then
240 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)
241 else
242 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
244 -- no change to State.cursor1.pos
246 elseif State.cursor1.line < #State.lines then
247 if State.lines[State.cursor1.line+1].mode == 'text' then
248 -- join lines
249 State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
251 table.remove(State.lines, State.cursor1.line+1)
252 table.remove(State.line_cache, State.cursor1.line+1)
254 Text.clear_screen_line_cache(State, State.cursor1.line)
255 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
256 schedule_save(State)
257 --== shortcuts that move the cursor
258 elseif chord == 'left' then
259 Text.left(State)
260 State.selection1 = {}
261 elseif chord == 'right' then
262 Text.right(State)
263 State.selection1 = {}
264 elseif chord == 'S-left' then
265 if State.selection1.line == nil then
266 State.selection1 = deepcopy(State.cursor1)
268 Text.left(State)
269 elseif chord == 'S-right' then
270 if State.selection1.line == nil then
271 State.selection1 = deepcopy(State.cursor1)
273 Text.right(State)
274 -- C- hotkeys reserved for drawings, so we'll use M-
275 elseif chord == 'M-left' then
276 Text.word_left(State)
277 State.selection1 = {}
278 elseif chord == 'M-right' then
279 Text.word_right(State)
280 State.selection1 = {}
281 elseif chord == 'M-S-left' then
282 if State.selection1.line == nil then
283 State.selection1 = deepcopy(State.cursor1)
285 Text.word_left(State)
286 elseif chord == 'M-S-right' then
287 if State.selection1.line == nil then
288 State.selection1 = deepcopy(State.cursor1)
290 Text.word_right(State)
291 elseif chord == 'home' then
292 Text.start_of_line(State)
293 State.selection1 = {}
294 elseif chord == 'end' then
295 Text.end_of_line(State)
296 State.selection1 = {}
297 elseif chord == 'S-home' then
298 if State.selection1.line == nil then
299 State.selection1 = deepcopy(State.cursor1)
301 Text.start_of_line(State)
302 elseif chord == 'S-end' then
303 if State.selection1.line == nil then
304 State.selection1 = deepcopy(State.cursor1)
306 Text.end_of_line(State)
307 elseif chord == 'up' then
308 Text.up(State)
309 State.selection1 = {}
310 elseif chord == 'down' then
311 Text.down(State)
312 State.selection1 = {}
313 elseif chord == 'S-up' then
314 if State.selection1.line == nil then
315 State.selection1 = deepcopy(State.cursor1)
317 Text.up(State)
318 elseif chord == 'S-down' then
319 if State.selection1.line == nil then
320 State.selection1 = deepcopy(State.cursor1)
322 Text.down(State)
323 elseif chord == 'pageup' then
324 Text.pageup(State)
325 State.selection1 = {}
326 elseif chord == 'pagedown' then
327 Text.pagedown(State)
328 State.selection1 = {}
329 elseif chord == 'S-pageup' then
330 if State.selection1.line == nil then
331 State.selection1 = deepcopy(State.cursor1)
333 Text.pageup(State)
334 elseif chord == 'S-pagedown' then
335 if State.selection1.line == nil then
336 State.selection1 = deepcopy(State.cursor1)
338 Text.pagedown(State)
342 function Text.insert_return(State)
343 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
344 table.insert(State.lines, State.cursor1.line+1, {mode='text', data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
345 table.insert(State.line_cache, State.cursor1.line+1, {})
346 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
347 Text.clear_screen_line_cache(State, State.cursor1.line)
348 State.cursor1 = {line=State.cursor1.line+1, pos=1}
351 function Text.pageup(State)
352 State.screen_top1 = Text.previous_screen_top1(State)
353 State.cursor1 = deepcopy(State.screen_top1)
354 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
355 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
358 -- return the top y coordinate of a given line_index,
359 -- or nil if no part of it is on screen
360 function Text.starty(State, line_index)
361 -- duplicate some logic from love.draw
362 -- does not modify State (except to populate line_cache)
363 if line_index < State.screen_top1.line then return end
364 local loc2 = Text.to2(State, State.screen_top1)
365 local y = State.top
366 while true do
367 if State.lines[loc2.line].mode == 'drawing' then
368 y = y + Drawing_padding_top
370 if loc2.line == line_index then return y end
371 if State.lines[loc2.line].mode == 'text' then
372 y = y + State.line_height
373 elseif State.lines[loc2.line].mode == 'drawing' then
374 y = y + Drawing.pixels(State.lines[loc2.line].h, State.width) + Drawing_padding_bottom
376 if y + State.line_height > App.screen.height then break end
377 local next_loc2 = Text.next_screen_line(State, loc2)
378 if Text.eq2(next_loc2, loc2) then break end -- end of file
379 loc2 = next_loc2
383 function Text.previous_screen_top1(State)
384 -- duplicate some logic from love.draw
385 -- does not modify State (except to populate line_cache)
386 local loc2 = Text.to2(State, State.screen_top1)
387 local y = App.screen.height - State.line_height
388 while y >= State.top do
389 if loc2.line == 1 and loc2.screen_line == 1 and loc2.screen_pos == 1 then break end
390 if State.lines[loc2.line].mode == 'text' then
391 y = y - State.line_height
392 elseif State.lines[loc2.line].mode == 'drawing' then
393 y = y - Drawing_padding_height - Drawing.pixels(State.lines[loc2.line].h, State.width)
395 loc2 = Text.previous_screen_line(State, loc2)
397 return Text.to1(State, loc2)
400 function Text.pagedown(State)
401 State.screen_top1 = Text.screen_bottom1(State)
402 State.cursor1 = deepcopy(State.screen_top1)
403 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
404 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
407 -- return the location of the start of the bottom-most line on screen
408 function Text.screen_bottom1(State)
409 -- duplicate some logic from love.draw
410 -- does not modify State (except to populate line_cache)
411 local loc2 = Text.to2(State, State.screen_top1)
412 local y = State.top
413 while true do
414 if State.lines[loc2.line].mode == 'text' then
415 y = y + State.line_height
416 elseif State.lines[loc2.line].mode == 'drawing' then
417 y = y + Drawing_padding_height + Drawing.pixels(State.lines[loc2.line].h, State.width)
419 if y + State.line_height > App.screen.height then break end
420 local next_loc2 = Text.next_screen_line(State, loc2)
421 if Text.eq2(next_loc2, loc2) then break end
422 loc2 = next_loc2
424 return Text.to1(State, loc2)
427 function Text.up(State)
428 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
429 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
430 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
431 if screen_line_starting_pos == 1 then
432 --? print('cursor is at first screen line of its line')
433 -- line is done; skip to previous text line
434 local new_cursor_line = State.cursor1.line
435 while new_cursor_line > 1 do
436 new_cursor_line = new_cursor_line-1
437 if State.lines[new_cursor_line].mode == 'text' then
438 --? print('found previous text line')
439 State.cursor1 = {line=new_cursor_line, pos=nil}
440 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
441 -- previous text line found, pick its final screen line
442 --? print('has multiple screen lines')
443 local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
444 --? print(#screen_line_starting_pos)
445 screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
446 local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
447 local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
448 State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
449 break
452 else
453 -- move up one screen line in current line
454 assert(screen_line_index > 1, 'bumped up against top screen line in line')
455 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
456 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
457 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
458 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
459 --? print('cursor pos is now '..tostring(State.cursor1.pos))
461 if Text.lt1(State.cursor1, State.screen_top1) then
462 State.screen_top1 = {
463 line=State.cursor1.line,
464 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
466 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
470 function Text.down(State)
471 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
472 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
473 assert(State.cursor1.pos, 'cursor has no pos')
474 if Text.cursor_at_final_screen_line(State) then
475 -- line is done, skip to next text line
476 --? print('cursor at final screen line of its line')
477 local new_cursor_line = State.cursor1.line
478 while new_cursor_line < #State.lines do
479 new_cursor_line = new_cursor_line+1
480 if State.lines[new_cursor_line].mode == 'text' then
481 State.cursor1 = {
482 line = new_cursor_line,
483 pos = Text.nearest_cursor_pos(State.font, State.lines[new_cursor_line].data, State.cursor_x, State.left),
485 --? print(State.cursor1.pos)
486 break
489 local screen_bottom1 = Text.screen_bottom1(State)
490 --? print('down 2', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, screen_bottom1.line, screen_bottom1.pos)
491 if State.cursor1.line > screen_bottom1.line then
492 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
493 --? print('scroll up preserving cursor')
494 Text.snap_cursor_to_bottom_of_screen(State)
495 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
497 else
498 -- move down one screen line in current line
499 local screen_bottom1 = Text.screen_bottom1(State)
500 local scroll_down = Text.le1(screen_bottom1, State.cursor1)
501 --? print('cursor is NOT at final screen line of its line')
502 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
503 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
504 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
505 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
506 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
507 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
508 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
509 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
510 if scroll_down then
511 --? print('scroll up preserving cursor')
512 Text.snap_cursor_to_bottom_of_screen(State)
513 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
516 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
519 function Text.start_of_line(State)
520 State.cursor1.pos = 1
521 if Text.lt1(State.cursor1, State.screen_top1) then
522 State.screen_top1 = deepcopy(State.cursor1)
526 function Text.end_of_line(State)
527 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
528 if Text.cursor_out_of_screen(State) then
529 Text.snap_cursor_to_bottom_of_screen(State)
533 function Text.word_left(State)
534 -- skip some whitespace
535 while true do
536 if State.cursor1.pos == 1 then
537 break
539 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
540 break
542 Text.left(State)
544 -- skip some non-whitespace
545 while true do
546 Text.left(State)
547 if State.cursor1.pos == 1 then
548 break
550 assert(State.cursor1.pos > 1, 'bumped up against start of line')
551 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
552 break
557 function Text.word_right(State)
558 -- skip some whitespace
559 while true do
560 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
561 break
563 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
564 break
566 Text.right_without_scroll(State)
568 while true do
569 Text.right_without_scroll(State)
570 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
571 break
573 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
574 break
577 if Text.cursor_out_of_screen(State) then
578 Text.snap_cursor_to_bottom_of_screen(State)
582 function Text.match(s, pos, pat)
583 local start_offset = Text.offset(s, pos)
584 local end_offset = Text.offset(s, pos+1)
585 assert(end_offset > start_offset, ('end_offset %d not > start_offset %d'):format(end_offset, start_offset))
586 local curr = s:sub(start_offset, end_offset-1)
587 return curr:match(pat)
590 function Text.left(State)
591 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
592 if State.cursor1.pos > 1 then
593 State.cursor1.pos = State.cursor1.pos-1
594 else
595 local new_cursor_line = State.cursor1.line
596 while new_cursor_line > 1 do
597 new_cursor_line = new_cursor_line-1
598 if State.lines[new_cursor_line].mode == 'text' then
599 State.cursor1 = {
600 line = new_cursor_line,
601 pos = utf8.len(State.lines[new_cursor_line].data) + 1,
603 break
607 if Text.lt1(State.cursor1, State.screen_top1) then
608 State.screen_top1 = {
609 line=State.cursor1.line,
610 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
612 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
616 function Text.right(State)
617 Text.right_without_scroll(State)
618 if Text.cursor_out_of_screen(State) then
619 Text.snap_cursor_to_bottom_of_screen(State)
623 function Text.right_without_scroll(State)
624 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
625 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
626 State.cursor1.pos = State.cursor1.pos+1
627 else
628 local new_cursor_line = State.cursor1.line
629 while new_cursor_line <= #State.lines-1 do
630 new_cursor_line = new_cursor_line+1
631 if State.lines[new_cursor_line].mode == 'text' then
632 State.cursor1 = {line=new_cursor_line, pos=1}
633 break
639 -- result: pos, index of screen line
640 function Text.pos_at_start_of_screen_line(State, loc1)
641 Text.populate_screen_line_starting_pos(State, loc1.line)
642 local line_cache = State.line_cache[loc1.line]
643 for i=#line_cache.screen_line_starting_pos,1,-1 do
644 local spos = line_cache.screen_line_starting_pos[i]
645 if spos <= loc1.pos then
646 return spos,i
649 assert(false, ('invalid pos %d'):format(loc1.pos))
652 function Text.pos_at_end_of_screen_line(State, loc1)
653 assert(State.lines[loc1.line].mode == 'text')
654 Text.populate_screen_line_starting_pos(State, loc1.line)
655 local line_cache = State.line_cache[loc1.line]
656 local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
657 for i=#line_cache.screen_line_starting_pos,1,-1 do
658 local spos = line_cache.screen_line_starting_pos[i]
659 if spos <= loc1.pos then
660 return most_recent_final_pos
662 most_recent_final_pos = spos-1
664 assert(false, ('invalid pos %d'):format(loc1.pos))
667 function Text.final_text_loc_on_screen(State)
668 local screen_bottom1 = Text.screen_bottom1(State)
669 if State.lines[screen_bottom1.line].mode == 'text' then
670 return {
671 line=screen_bottom1.line,
672 pos=Text.pos_at_end_of_screen_line(State, screen_bottom1),
675 local loc2 = Text.to2(State, screen_bottom1)
676 while true do
677 if State.lines[loc2.line].mode == 'text' then break end
678 assert(loc2.line > 1 or loc2.screen_line > 1 and loc2.screen_pos > 1) -- elsewhere we're making sure there's always at least one text line on screen
679 loc2 = Text.previous_screen_line(State, loc2)
681 local result = Text.to1(State, loc2)
682 result.pos = Text.pos_at_end_of_screen_line(State, result)
683 return result
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)
694 local y = State.top
695 while State.cursor1.line <= #State.lines do
696 if State.lines[State.cursor1.line].mode == 'text' then
697 break
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, 'tried to ensure bottom line of file is text, but failed')
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)
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
730 while true do
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
736 break
738 y = y - h
739 else
740 assert(top2.line > 1, 'tried to snap cursor to buttom of screen but failed')
741 assert(State.lines[top2.line-1].mode == 'drawing', "expected a drawing but it's not")
742 -- We currently can't draw partial drawings, so either skip it entirely
743 -- or not at all.
744 local h = Drawing_padding_height + Drawing.pixels(State.lines[top2.line-1].h, State.width)
745 if y - h < State.top then
746 break
748 --? print('skipping drawing of height', h)
749 y = y - 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)
757 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches 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 local starty = Text.starty(State, line_index)
764 if starty == nil then return false end -- outside current page
765 if y < starty then return false end
766 Text.populate_screen_line_starting_pos(State, line_index)
767 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)
770 -- convert mx,my in pixels to schema-1 coordinates
771 function Text.to_pos_on_line(State, line_index, mx, my)
772 local line = State.lines[line_index]
773 local line_cache = State.line_cache[line_index]
774 local starty = Text.starty(State, line_index)
775 assert(my >= starty, 'failed to map y pixel to line')
776 -- duplicate some logic from Text.draw
777 local y = starty
778 local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
779 for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
780 local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
781 local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
782 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
783 local nexty = y + State.line_height
784 if my < nexty then
785 -- On all wrapped screen lines but the final one, clicks past end of
786 -- line position cursor on final character of screen line.
787 -- (The final screen line positions past end of screen line as always.)
788 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
789 --? print('past end of non-final line; return')
790 return line_cache.screen_line_starting_pos[screen_line_index+1]
792 local s = string.sub(line.data, screen_line_starting_byte_offset)
793 --? 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)
794 return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
796 y = nexty
798 assert(false, 'failed to map y pixel to line')
801 function Text.screen_line_width(State, line_index, i)
802 local line = State.lines[line_index]
803 local line_cache = State.line_cache[line_index]
804 local start_pos = line_cache.screen_line_starting_pos[i]
805 local start_offset = Text.offset(line.data, start_pos)
806 local screen_line
807 if i < #line_cache.screen_line_starting_pos then
808 local past_end_pos = line_cache.screen_line_starting_pos[i+1]
809 local past_end_offset = Text.offset(line.data, past_end_pos)
810 screen_line = string.sub(line.data, start_offset, past_end_offset-1)
811 else
812 screen_line = string.sub(line.data, start_pos)
814 return State.font:getWidth(screen_line)
817 function Text.screen_line_index(screen_line_starting_pos, pos)
818 for i = #screen_line_starting_pos,1,-1 do
819 if screen_line_starting_pos[i] <= pos then
820 return i
825 -- convert x pixel coordinate to pos
826 -- oblivious to wrapping
827 -- result: 1 to len+1
828 function Text.nearest_cursor_pos(font, line, x, left)
829 if x < left then
830 return 1
832 local len = utf8.len(line)
833 local max_x = left+Text.x(font, line, len+1)
834 if x > max_x then
835 return len+1
837 local leftpos, rightpos = 1, len+1
838 --? print('-- nearest', x)
839 while true do
840 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
841 if leftpos == rightpos then
842 return leftpos
844 local curr = math.floor((leftpos+rightpos)/2)
845 local currxmin = left+Text.x(font, line, curr)
846 local currxmax = left+Text.x(font, line, curr+1)
847 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
848 if currxmin <= x and x < currxmax then
849 if x-currxmin < currxmax-x then
850 return curr
851 else
852 return curr+1
855 if leftpos >= rightpos-1 then
856 return rightpos
858 if currxmin > x then
859 rightpos = curr
860 else
861 leftpos = curr
864 assert(false, 'failed to map x pixel to pos')
867 -- return the nearest index of line (in utf8 code points) which lies entirely
868 -- within x pixels of the left margin
869 -- result: 0 to len+1
870 function Text.nearest_pos_less_than(font, line, x)
871 --? print('', '-- nearest_pos_less_than', line, x)
872 local len = utf8.len(line)
873 local max_x = Text.x_after(font, line, len)
874 if x > max_x then
875 return len+1
877 local left, right = 0, len+1
878 while true do
879 local curr = math.floor((left+right)/2)
880 local currxmin = Text.x_after(font, line, curr+1)
881 local currxmax = Text.x_after(font, line, curr+2)
882 --? print('', x, left, right, curr, currxmin, currxmax)
883 if currxmin <= x and x < currxmax then
884 return curr
886 if left >= right-1 then
887 return left
889 if currxmin > x then
890 right = curr
891 else
892 left = curr
895 assert(false, 'failed to map x pixel to pos')
898 function Text.x_after(font, s, pos)
899 local len = utf8.len(s)
900 local offset = Text.offset(s, math.min(pos+1, len+1))
901 local s_before = s:sub(1, offset-1)
902 --? print('^'..s_before..'$')
903 return font:getWidth(s_before)
906 function Text.x(font, s, pos)
907 local offset = Text.offset(s, pos)
908 local s_before = s:sub(1, offset-1)
909 return font:getWidth(s_before)
912 function Text.to2(State, loc1)
913 if State.lines[loc1.line].mode == 'drawing' then
914 return {line=loc1.line, screen_line=1, screen_pos=1}
916 local result = {line=loc1.line}
917 local line_cache = State.line_cache[loc1.line]
918 Text.populate_screen_line_starting_pos(State, loc1.line)
919 for i=#line_cache.screen_line_starting_pos,1,-1 do
920 local spos = line_cache.screen_line_starting_pos[i]
921 if spos <= loc1.pos then
922 result.screen_line = i
923 result.screen_pos = loc1.pos - spos + 1
924 break
927 assert(result.screen_pos, 'failed to convert schema-1 coordinate to schema-2')
928 return result
931 function Text.to1(State, loc2)
932 local result = {line=loc2.line, pos=loc2.screen_pos}
933 if loc2.screen_line > 1 then
934 result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
936 return result
939 function Text.eq1(a, b)
940 return a.line == b.line and a.pos == b.pos
943 function Text.lt1(a, b)
944 if a.line < b.line then
945 return true
947 if a.line > b.line then
948 return false
950 return a.pos < b.pos
953 function Text.le1(a, b)
954 if a.line < b.line then
955 return true
957 if a.line > b.line then
958 return false
960 return a.pos <= b.pos
963 function Text.eq2(a, b)
964 return a.line == b.line and a.screen_line == b.screen_line and a.screen_pos == b.screen_pos
967 function Text.offset(s, pos1)
968 if pos1 == 1 then return 1 end
969 local result = utf8.offset(s, pos1)
970 if result == nil then
971 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))
973 return result
976 function Text.previous_screen_line(State, loc2)
977 if loc2.screen_line > 1 then
978 return {line=loc2.line, screen_line=loc2.screen_line-1, screen_pos=1}
979 elseif loc2.line == 1 then
980 return loc2
981 elseif State.lines[loc2.line-1].mode == 'drawing' then
982 return {line=loc2.line-1, screen_line=1, screen_pos=1}
983 else
984 local l = State.lines[loc2.line-1]
985 Text.populate_screen_line_starting_pos(State, loc2.line-1)
986 return {line=loc2.line-1, screen_line=#State.line_cache[loc2.line-1].screen_line_starting_pos, screen_pos=1}
990 function Text.next_screen_line(State, loc2)
991 if State.lines[loc2.line].mode == 'drawing' then
992 return {line=loc2.line+1, screen_line=1, screen_pos=1}
994 Text.populate_screen_line_starting_pos(State, loc2.line)
995 if loc2.screen_line >= #State.line_cache[loc2.line].screen_line_starting_pos then
996 if loc2.line < #State.lines then
997 return {line=loc2.line+1, screen_line=1, screen_pos=1}
998 else
999 return loc2
1001 else
1002 return {line=loc2.line, screen_line=loc2.screen_line+1, screen_pos=1}
1006 -- resize helper
1007 function Text.tweak_screen_top_and_cursor(State)
1008 if State.screen_top1.pos == 1 then return end
1009 Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
1010 local line = State.lines[State.screen_top1.line]
1011 local line_cache = State.line_cache[State.screen_top1.line]
1012 for i=2,#line_cache.screen_line_starting_pos do
1013 local pos = line_cache.screen_line_starting_pos[i]
1014 if pos == State.screen_top1.pos then
1015 break
1017 if pos > State.screen_top1.pos then
1018 -- make sure screen top is at start of a screen line
1019 local prev = line_cache.screen_line_starting_pos[i-1]
1020 if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
1021 State.screen_top1.pos = prev
1022 else
1023 State.screen_top1.pos = pos
1025 break
1028 -- make sure cursor is on screen
1029 local screen_bottom1 = Text.screen_bottom1(State)
1030 if Text.lt1(State.cursor1, State.screen_top1) then
1031 State.cursor1 = deepcopy(State.screen_top1)
1032 elseif State.cursor1.line >= screen_bottom1.line then
1033 if Text.cursor_out_of_screen(State) then
1034 State.cursor1 = Text.final_text_loc_on_screen(State)
1039 -- slightly expensive since it redraws the screen
1040 function Text.cursor_out_of_screen(State)
1041 edit.draw(State)
1042 return State.cursor_y == nil
1045 function Text.redraw_all(State)
1046 --? print('clearing line caches')
1047 -- Perform some early sanity checking here, in hopes that we correctly call
1048 -- this whenever we change editor state.
1049 if State.right <= State.left then
1050 assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left))
1053 State.line_cache = {}
1054 for i=1,#State.lines do
1055 State.line_cache[i] = {}
1059 function Text.clear_screen_line_cache(State, line_index)
1060 State.line_cache[line_index].screen_line_starting_pos = nil
1063 function trim(s)
1064 return s:gsub('^%s+', ''):gsub('%s+$', '')
1067 function ltrim(s)
1068 return s:gsub('^%s+', '')
1071 function rtrim(s)
1072 return s:gsub('%s+$', '')
1075 function starts_with(s, prefix)
1076 if #s < #prefix then
1077 return false
1079 for i=1,#prefix do
1080 if s:sub(i,i) ~= prefix:sub(i,i) then
1081 return false
1084 return true
1087 function ends_with(s, suffix)
1088 if #s < #suffix then
1089 return false
1091 for i=0,#suffix-1 do
1092 if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
1093 return false
1096 return true