Merge lines.love
[view.love.git] / text.lua
blob5e9317ccca023fb753ca5e79886148fa32f21034
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, and position of start of final screen line drawn
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.starty = y
11 line_cache.startpos = startpos
12 -- wrap long lines
13 local final_screen_line_starting_pos = startpos -- track value to return
14 Text.populate_screen_line_starting_pos(State, line_index)
15 assert(#line_cache.screen_line_starting_pos >= 1, 'line cache missing screen line info')
16 for i=1,#line_cache.screen_line_starting_pos do
17 local pos = line_cache.screen_line_starting_pos[i]
18 if pos < startpos then
19 -- render nothing
20 else
21 final_screen_line_starting_pos = pos
22 local screen_line = Text.screen_line(line, line_cache, i)
23 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
24 local frag_len = utf8.len(screen_line)
25 -- render any highlights
26 if State.selection1.line then
27 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
28 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
29 end
30 if line_index == State.cursor1.line then
31 -- render search highlight or cursor
32 if State.search_term then
33 local data = State.lines[State.cursor1.line].data
34 local cursor_offset = Text.offset(data, State.cursor1.pos)
35 if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
36 local save_selection = State.selection1
37 State.selection1 = {line=line_index, pos=State.cursor1.pos+utf8.len(State.search_term)}
38 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
39 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
40 State.selection1 = save_selection
41 end
42 else
43 if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
44 Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
45 elseif pos + frag_len == State.cursor1.pos then
46 -- Show cursor at end of line.
47 -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
48 -- It seems useful to see a cursor whether your eye is on the left or right margin.
49 Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
50 end
51 end
52 end
53 -- render fragment
54 App.color(Text_color)
55 App.screen.print(screen_line, State.left,y)
56 y = y + State.line_height
57 if y >= App.screen.height then
58 break
59 end
60 end
61 end
62 return y, final_screen_line_starting_pos
63 end
65 function Text.screen_line(line, line_cache, i)
66 local pos = line_cache.screen_line_starting_pos[i]
67 local offset = Text.offset(line.data, pos)
68 if i >= #line_cache.screen_line_starting_pos then
69 return line.data:sub(offset)
70 end
71 local endpos = line_cache.screen_line_starting_pos[i+1]-1
72 local end_offset = Text.offset(line.data, endpos)
73 return line.data:sub(offset, end_offset)
74 end
76 function Text.draw_cursor(State, x, y)
77 -- blink every 0.5s
78 if math.floor(Cursor_time*2)%2 == 0 then
79 App.color(Cursor_color)
80 love.graphics.rectangle('fill', x,y, 3,State.line_height)
81 end
82 State.cursor_x = x
83 State.cursor_y = y+State.line_height
84 end
86 function Text.populate_screen_line_starting_pos(State, line_index)
87 local line = State.lines[line_index]
88 local line_cache = State.line_cache[line_index]
89 if line_cache.screen_line_starting_pos then
90 return
91 end
92 line_cache.screen_line_starting_pos = {1}
93 local x = 0
94 local pos = 1
95 -- try to wrap at word boundaries
96 for frag in line.data:gmatch('%S*%s*') do
97 local frag_width = State.font:getWidth(frag)
98 --? print('-- frag:', frag, pos, x, frag_width, State.width)
99 while x + frag_width > State.width do
100 --? print('frag:', frag, pos, x, frag_width, State.width)
101 if x < 0.8 * State.width then
102 -- long word; chop it at some letter
103 -- We're not going to reimplement TeX here.
104 local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
105 if x == 0 and bpos == 0 then
106 assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
108 pos = pos + bpos
109 local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
110 frag = string.sub(frag, boffset)
111 --? if bpos > 0 then
112 --? print('after chop:', frag)
113 --? end
114 frag_width = State.font:getWidth(frag)
116 --? print('screen line:', pos)
117 table.insert(line_cache.screen_line_starting_pos, pos)
118 x = 0 -- new screen line
120 x = x + frag_width
121 pos = pos + utf8.len(frag)
125 function Text.text_input(State, t)
126 if App.mouse_down(1) then return end
127 if App.any_modifier_down() then
128 if App.key_down(t) then
129 -- The modifiers didn't change the key. Handle it in keychord_pressed.
130 return
131 else
132 -- Key mutated by the keyboard layout. Continue below.
135 local before = snapshot(State, State.cursor1.line)
136 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
137 Text.insert_at_cursor(State, t)
138 if State.cursor_y > App.screen.height - State.line_height then
139 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
140 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
142 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
145 function Text.insert_at_cursor(State, t)
146 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
147 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)
148 Text.clear_screen_line_cache(State, State.cursor1.line)
149 State.cursor1.pos = State.cursor1.pos+1
152 -- Don't handle any keys here that would trigger text_input above.
153 function Text.keychord_press(State, chord)
154 --? print('chord', chord, State.selection1.line, State.selection1.pos)
155 --== shortcuts that mutate text
156 if chord == 'return' then
157 local before_line = State.cursor1.line
158 local before = snapshot(State, before_line)
159 Text.insert_return(State)
160 State.selection1 = {}
161 if State.cursor_y > App.screen.height - State.line_height then
162 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
164 schedule_save(State)
165 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
166 elseif chord == 'tab' then
167 local before = snapshot(State, State.cursor1.line)
168 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
169 Text.insert_at_cursor(State, '\t')
170 if State.cursor_y > App.screen.height - State.line_height then
171 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
172 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
173 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
175 schedule_save(State)
176 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
177 elseif chord == 'backspace' then
178 if State.selection1.line then
179 Text.delete_selection(State, State.left, State.right)
180 schedule_save(State)
181 return
183 local before
184 if State.cursor1.pos > 1 then
185 before = snapshot(State, State.cursor1.line)
186 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
187 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
188 if byte_start then
189 if byte_end then
190 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)
191 else
192 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
194 State.cursor1.pos = State.cursor1.pos-1
196 elseif State.cursor1.line > 1 then
197 before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
198 -- join lines
199 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
200 State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
201 table.remove(State.lines, State.cursor1.line)
202 table.remove(State.line_cache, State.cursor1.line)
203 State.cursor1.line = State.cursor1.line-1
205 if State.screen_top1.line > #State.lines then
206 Text.populate_screen_line_starting_pos(State, #State.lines)
207 local line_cache = State.line_cache[#State.line_cache]
208 State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
209 elseif Text.lt1(State.cursor1, State.screen_top1) then
210 State.screen_top1 = {
211 line=State.cursor1.line,
212 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
214 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
216 Text.clear_screen_line_cache(State, State.cursor1.line)
217 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))
218 schedule_save(State)
219 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
220 elseif chord == 'delete' then
221 if State.selection1.line then
222 Text.delete_selection(State, State.left, State.right)
223 schedule_save(State)
224 return
226 local before
227 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
228 before = snapshot(State, State.cursor1.line)
229 else
230 before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
232 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
233 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
234 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
235 if byte_start then
236 if byte_end then
237 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)
238 else
239 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
241 -- no change to State.cursor1.pos
243 elseif State.cursor1.line < #State.lines then
244 -- join lines
245 State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
246 table.remove(State.lines, State.cursor1.line+1)
247 table.remove(State.line_cache, State.cursor1.line+1)
249 Text.clear_screen_line_cache(State, State.cursor1.line)
250 schedule_save(State)
251 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
252 --== shortcuts that move the cursor
253 elseif chord == 'left' then
254 Text.left(State)
255 State.selection1 = {}
256 elseif chord == 'right' then
257 Text.right(State)
258 State.selection1 = {}
259 elseif chord == 'S-left' then
260 if State.selection1.line == nil then
261 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
263 Text.left(State)
264 elseif chord == 'S-right' then
265 if State.selection1.line == nil then
266 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
268 Text.right(State)
269 -- C- hotkeys reserved for drawings, so we'll use M-
270 elseif chord == 'M-left' then
271 Text.word_left(State)
272 State.selection1 = {}
273 elseif chord == 'M-right' then
274 Text.word_right(State)
275 State.selection1 = {}
276 elseif chord == 'M-S-left' then
277 if State.selection1.line == nil then
278 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
280 Text.word_left(State)
281 elseif chord == 'M-S-right' then
282 if State.selection1.line == nil then
283 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
285 Text.word_right(State)
286 elseif chord == 'home' then
287 Text.start_of_line(State)
288 State.selection1 = {}
289 elseif chord == 'end' then
290 Text.end_of_line(State)
291 State.selection1 = {}
292 elseif chord == 'S-home' then
293 if State.selection1.line == nil then
294 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
296 Text.start_of_line(State)
297 elseif chord == 'S-end' then
298 if State.selection1.line == nil then
299 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
301 Text.end_of_line(State)
302 elseif chord == 'up' then
303 Text.up(State)
304 State.selection1 = {}
305 elseif chord == 'down' then
306 Text.down(State)
307 State.selection1 = {}
308 elseif chord == 'S-up' then
309 if State.selection1.line == nil then
310 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
312 Text.up(State)
313 elseif chord == 'S-down' then
314 if State.selection1.line == nil then
315 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
317 Text.down(State)
318 elseif chord == 'pageup' then
319 Text.pageup(State)
320 State.selection1 = {}
321 elseif chord == 'pagedown' then
322 Text.pagedown(State)
323 State.selection1 = {}
324 elseif chord == 'S-pageup' then
325 if State.selection1.line == nil then
326 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
328 Text.pageup(State)
329 elseif chord == 'S-pagedown' then
330 if State.selection1.line == nil then
331 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
333 Text.pagedown(State)
337 function Text.insert_return(State)
338 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
339 table.insert(State.lines, State.cursor1.line+1, {data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
340 table.insert(State.line_cache, State.cursor1.line+1, {})
341 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
342 Text.clear_screen_line_cache(State, State.cursor1.line)
343 State.cursor1 = {line=State.cursor1.line+1, pos=1}
346 function Text.pageup(State)
347 --? print('pageup')
348 -- duplicate some logic from love.draw
349 local top2 = Text.to2(State, State.screen_top1)
350 --? print(App.screen.height)
351 local y = App.screen.height - State.line_height
352 while y >= State.top do
353 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
354 if State.screen_top1.line == 1 and State.screen_top1.pos == 1 then break end
355 y = y - State.line_height
356 top2 = Text.previous_screen_line(State, top2)
358 State.screen_top1 = Text.to1(State, top2)
359 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
360 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
361 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
362 --? print('pageup end')
365 function Text.pagedown(State)
366 --? print('pagedown')
367 -- If a line/paragraph gets to a page boundary, I often want to scroll
368 -- before I get to the bottom.
369 -- However, only do this if it makes forward progress.
370 local bot2 = Text.to2(State, State.screen_bottom1)
371 if bot2.screen_line > 1 then
372 bot2.screen_line = math.max(bot2.screen_line-10, 1)
374 local new_top1 = Text.to1(State, bot2)
375 if Text.lt1(State.screen_top1, new_top1) then
376 State.screen_top1 = new_top1
377 else
378 State.screen_top1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
380 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
381 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
382 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
383 --? print('top now', State.screen_top1.line)
384 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
385 --? print('pagedown end')
388 function Text.up(State)
389 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
390 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
391 if screen_line_starting_pos == 1 then
392 --? print('cursor is at first screen line of its line')
393 -- line is done; skip to previous text line
394 if State.cursor1.line > 1 then
395 local new_cursor_line = State.cursor1.line-1
396 --? print('found previous text line')
397 State.cursor1 = {line=new_cursor_line, pos=nil}
398 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
399 -- previous text line found, pick its final screen line
400 --? print('has multiple screen lines')
401 local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
402 --? print(#screen_line_starting_pos)
403 screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
404 local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
405 local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
406 State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
408 else
409 -- move up one screen line in current line
410 assert(screen_line_index > 1, 'bumped up against top screen line in line')
411 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
412 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
413 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
414 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
415 --? print('cursor pos is now '..tostring(State.cursor1.pos))
417 if Text.lt1(State.cursor1, State.screen_top1) then
418 State.screen_top1 = {
419 line=State.cursor1.line,
420 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
422 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
426 function Text.down(State)
427 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
428 assert(State.cursor1.pos, 'cursor has no pos')
429 if Text.cursor_at_final_screen_line(State) then
430 -- line is done, skip to next text line
431 --? print('cursor at final screen line of its line')
432 if State.cursor1.line < #State.lines then
433 local new_cursor_line = State.cursor1.line+1
434 State.cursor1.line = new_cursor_line
435 State.cursor1.pos = Text.nearest_cursor_pos(State.font, State.lines[State.cursor1.line].data, State.cursor_x, State.left)
436 --? print(State.cursor1.pos)
438 if State.cursor1.line > State.screen_bottom1.line then
439 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
440 --? print('scroll up preserving cursor')
441 Text.snap_cursor_to_bottom_of_screen(State)
442 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
444 else
445 -- move down one screen line in current line
446 local scroll_down = Text.le1(State.screen_bottom1, State.cursor1)
447 --? print('cursor is NOT at final screen line of its line')
448 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
449 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
450 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
451 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
452 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
453 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
454 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
455 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
456 if scroll_down then
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)
462 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
465 function Text.start_of_line(State)
466 State.cursor1.pos = 1
467 if Text.lt1(State.cursor1, State.screen_top1) then
468 State.screen_top1 = {line=State.cursor1.line, pos=State.cursor1.pos} -- copy
472 function Text.end_of_line(State)
473 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
474 if Text.cursor_out_of_screen(State) then
475 Text.snap_cursor_to_bottom_of_screen(State)
479 function Text.word_left(State)
480 -- skip some whitespace
481 while true do
482 if State.cursor1.pos == 1 then
483 break
485 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
486 break
488 Text.left(State)
490 -- skip some non-whitespace
491 while true do
492 Text.left(State)
493 if State.cursor1.pos == 1 then
494 break
496 assert(State.cursor1.pos > 1, 'bumped up against start of line')
497 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
498 break
503 function Text.word_right(State)
504 -- skip some whitespace
505 while true do
506 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
507 break
509 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
510 break
512 Text.right_without_scroll(State)
514 while true do
515 Text.right_without_scroll(State)
516 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
517 break
519 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
520 break
523 if Text.cursor_out_of_screen(State) then
524 Text.snap_cursor_to_bottom_of_screen(State)
528 function Text.match(s, pos, pat)
529 local start_offset = Text.offset(s, pos)
530 local end_offset = Text.offset(s, pos+1)
531 assert(end_offset > start_offset, ('end_offset %d not > start_offset %d'):format(end_offset, start_offset))
532 local curr = s:sub(start_offset, end_offset-1)
533 return curr:match(pat)
536 function Text.left(State)
537 if State.cursor1.pos > 1 then
538 State.cursor1.pos = State.cursor1.pos-1
539 elseif State.cursor1.line > 1 then
540 State.cursor1.line = State.cursor1.line-1
541 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
543 if Text.lt1(State.cursor1, State.screen_top1) then
544 State.screen_top1 = {
545 line=State.cursor1.line,
546 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
548 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
552 function Text.right(State)
553 Text.right_without_scroll(State)
554 if Text.cursor_out_of_screen(State) then
555 Text.snap_cursor_to_bottom_of_screen(State)
559 function Text.right_without_scroll(State)
560 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
561 State.cursor1.pos = State.cursor1.pos+1
562 elseif State.cursor1.line <= #State.lines-1 then
563 State.cursor1.line = State.cursor1.line+1
564 State.cursor1.pos = 1
568 -- result: pos, index of screen line
569 function Text.pos_at_start_of_screen_line(State, loc1)
570 Text.populate_screen_line_starting_pos(State, loc1.line)
571 local line_cache = State.line_cache[loc1.line]
572 for i=#line_cache.screen_line_starting_pos,1,-1 do
573 local spos = line_cache.screen_line_starting_pos[i]
574 if spos <= loc1.pos then
575 return spos,i
578 assert(false, ('invalid pos %d'):format(loc1.pos))
581 function Text.pos_at_end_of_screen_line(State, loc1)
582 Text.populate_screen_line_starting_pos(State, loc1.line)
583 local line_cache = State.line_cache[loc1.line]
584 local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
585 for i=#line_cache.screen_line_starting_pos,1,-1 do
586 local spos = line_cache.screen_line_starting_pos[i]
587 if spos <= loc1.pos then
588 return most_recent_final_pos
590 most_recent_final_pos = spos-1
592 assert(false, ('invalid pos %d'):format(loc1.pos))
595 function Text.cursor_at_final_screen_line(State)
596 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
597 local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos
598 --? print(screen_lines[#screen_lines], State.cursor1.pos)
599 return screen_lines[#screen_lines] <= State.cursor1.pos
602 function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
603 if State.top > App.screen.height - State.line_height then
604 --? print('scroll up')
605 Text.snap_cursor_to_bottom_of_screen(State)
609 -- should never modify State.cursor1
610 function Text.snap_cursor_to_bottom_of_screen(State)
611 --? print('to2:', State.cursor1.line, State.cursor1.pos)
612 local top2 = Text.to2(State, State.cursor1)
613 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
614 -- slide to start of screen line
615 top2.screen_pos = 1 -- start of screen line
616 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
617 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
618 local y = App.screen.height - State.line_height
619 -- duplicate some logic from love.draw
620 while true do
621 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
622 if top2.line == 1 and top2.screen_line == 1 then break end
623 local h = State.line_height
624 if y - h < State.top then
625 break
627 y = y - h
628 top2 = Text.previous_screen_line(State, top2)
630 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
631 State.screen_top1 = Text.to1(State, top2)
632 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
633 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
634 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
637 function Text.in_line(State, line_index, x,y)
638 local line = State.lines[line_index]
639 local line_cache = State.line_cache[line_index]
640 if line_cache.starty == nil then return false end -- outside current page
641 if y < line_cache.starty then return false end
642 Text.populate_screen_line_starting_pos(State, line_index)
643 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)
646 -- convert mx,my in pixels to schema-1 coordinates
647 function Text.to_pos_on_line(State, line_index, mx, my)
648 local line = State.lines[line_index]
649 local line_cache = State.line_cache[line_index]
650 assert(my >= line_cache.starty, 'failed to map y pixel to line')
651 -- duplicate some logic from Text.draw
652 local y = line_cache.starty
653 local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
654 for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
655 local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
656 local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
657 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
658 local nexty = y + State.line_height
659 if my < nexty then
660 -- On all wrapped screen lines but the final one, clicks past end of
661 -- line position cursor on final character of screen line.
662 -- (The final screen line positions past end of screen line as always.)
663 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
664 --? print('past end of non-final line; return')
665 return line_cache.screen_line_starting_pos[screen_line_index+1]
667 local s = string.sub(line.data, screen_line_starting_byte_offset)
668 --? 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)
669 return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
671 y = nexty
673 assert(false, 'failed to map y pixel to line')
676 function Text.screen_line_width(State, line_index, i)
677 local line = State.lines[line_index]
678 local line_cache = State.line_cache[line_index]
679 local start_pos = line_cache.screen_line_starting_pos[i]
680 local start_offset = Text.offset(line.data, start_pos)
681 local screen_line
682 if i < #line_cache.screen_line_starting_pos then
683 local past_end_pos = line_cache.screen_line_starting_pos[i+1]
684 local past_end_offset = Text.offset(line.data, past_end_pos)
685 screen_line = string.sub(line.data, start_offset, past_end_offset-1)
686 else
687 screen_line = string.sub(line.data, start_pos)
689 return State.font:getWidth(screen_line)
692 function Text.screen_line_index(screen_line_starting_pos, pos)
693 for i = #screen_line_starting_pos,1,-1 do
694 if screen_line_starting_pos[i] <= pos then
695 return i
700 -- convert x pixel coordinate to pos
701 -- oblivious to wrapping
702 -- result: 1 to len+1
703 function Text.nearest_cursor_pos(font, line, x, left)
704 if x < left then
705 return 1
707 local len = utf8.len(line)
708 local max_x = left+Text.x(font, line, len+1)
709 if x > max_x then
710 return len+1
712 local leftpos, rightpos = 1, len+1
713 --? print('-- nearest', x)
714 while true do
715 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
716 if leftpos == rightpos then
717 return leftpos
719 local curr = math.floor((leftpos+rightpos)/2)
720 local currxmin = left+Text.x(font, line, curr)
721 local currxmax = left+Text.x(font, line, curr+1)
722 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
723 if currxmin <= x and x < currxmax then
724 if x-currxmin < currxmax-x then
725 return curr
726 else
727 return curr+1
730 if leftpos >= rightpos-1 then
731 return rightpos
733 if currxmin > x then
734 rightpos = curr
735 else
736 leftpos = curr
739 assert(false, 'failed to map x pixel to pos')
742 -- return the nearest index of line (in utf8 code points) which lies entirely
743 -- within x pixels of the left margin
744 -- result: 0 to len+1
745 function Text.nearest_pos_less_than(font, line, x)
746 --? print('', '-- nearest_pos_less_than', line, x)
747 local len = utf8.len(line)
748 local max_x = Text.x_after(font, line, len)
749 if x > max_x then
750 return len+1
752 local left, right = 0, len+1
753 while true do
754 local curr = math.floor((left+right)/2)
755 local currxmin = Text.x_after(font, line, curr+1)
756 local currxmax = Text.x_after(font, line, curr+2)
757 --? print('', x, left, right, curr, currxmin, currxmax)
758 if currxmin <= x and x < currxmax then
759 return curr
761 if left >= right-1 then
762 return left
764 if currxmin > x then
765 right = curr
766 else
767 left = curr
770 assert(false, 'failed to map x pixel to pos')
773 function Text.x_after(font, s, pos)
774 local len = utf8.len(s)
775 local offset = Text.offset(s, math.min(pos+1, len+1))
776 local s_before = s:sub(1, offset-1)
777 --? print('^'..s_before..'$')
778 return font:getWidth(s_before)
781 function Text.x(font, s, pos)
782 local offset = Text.offset(s, pos)
783 local s_before = s:sub(1, offset-1)
784 return font:getWidth(s_before)
787 function Text.to2(State, loc1)
788 local result = {line=loc1.line}
789 local line_cache = State.line_cache[loc1.line]
790 Text.populate_screen_line_starting_pos(State, loc1.line)
791 for i=#line_cache.screen_line_starting_pos,1,-1 do
792 local spos = line_cache.screen_line_starting_pos[i]
793 if spos <= loc1.pos then
794 result.screen_line = i
795 result.screen_pos = loc1.pos - spos + 1
796 break
799 assert(result.screen_pos, 'failed to convert schema-1 coordinate to schema-2')
800 return result
803 function Text.to1(State, loc2)
804 local result = {line=loc2.line, pos=loc2.screen_pos}
805 if loc2.screen_line > 1 then
806 result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
808 return result
811 function Text.eq1(a, b)
812 return a.line == b.line and a.pos == b.pos
815 function Text.lt1(a, b)
816 if a.line < b.line then
817 return true
819 if a.line > b.line then
820 return false
822 return a.pos < b.pos
825 function Text.le1(a, b)
826 if a.line < b.line then
827 return true
829 if a.line > b.line then
830 return false
832 return a.pos <= b.pos
835 function Text.offset(s, pos1)
836 if pos1 == 1 then return 1 end
837 local result = utf8.offset(s, pos1)
838 if result == nil then
839 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))
841 return result
844 function Text.previous_screen_line(State, loc2)
845 if loc2.screen_line > 1 then
846 return {line=loc2.line, screen_line=loc2.screen_line-1, screen_pos=1}
847 elseif loc2.line == 1 then
848 return loc2
849 else
850 local l = State.lines[loc2.line-1]
851 Text.populate_screen_line_starting_pos(State, loc2.line-1)
852 return {line=loc2.line-1, screen_line=#State.line_cache[loc2.line-1].screen_line_starting_pos, screen_pos=1}
856 -- resize helper
857 function Text.tweak_screen_top_and_cursor(State)
858 if State.screen_top1.pos == 1 then return end
859 Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
860 local line = State.lines[State.screen_top1.line]
861 local line_cache = State.line_cache[State.screen_top1.line]
862 for i=2,#line_cache.screen_line_starting_pos do
863 local pos = line_cache.screen_line_starting_pos[i]
864 if pos == State.screen_top1.pos then
865 break
867 if pos > State.screen_top1.pos then
868 -- make sure screen top is at start of a screen line
869 local prev = line_cache.screen_line_starting_pos[i-1]
870 if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
871 State.screen_top1.pos = prev
872 else
873 State.screen_top1.pos = pos
875 break
878 -- make sure cursor is on screen
879 if Text.lt1(State.cursor1, State.screen_top1) then
880 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
881 elseif State.cursor1.line >= State.screen_bottom1.line then
882 --? print('too low')
883 if Text.cursor_out_of_screen(State) then
884 --? print('tweak')
885 State.cursor1 = {
886 line=State.screen_bottom1.line,
887 pos=Text.to_pos_on_line(State, State.screen_bottom1.line, State.right-5, App.screen.height-5),
893 -- slightly expensive since it redraws the screen
894 function Text.cursor_out_of_screen(State)
895 edit.draw(State)
896 return State.cursor_y == nil
897 -- this approach is cheaper and almost works, except on the final screen
898 -- where file ends above bottom of screen
899 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
900 --? local botline1 = {line=State.cursor1.line, pos=botpos}
901 --? return Text.lt1(State.screen_bottom1, botline1)
904 function Text.redraw_all(State)
905 --? print('clearing fragments')
906 -- Perform some early sanity checking here, in hopes that we correctly call
907 -- this whenever we change editor state.
908 if State.right <= State.left then
909 assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left))
912 State.line_cache = {}
913 for i=1,#State.lines do
914 State.line_cache[i] = {}
918 function Text.clear_screen_line_cache(State, line_index)
919 State.line_cache[line_index].screen_line_starting_pos = nil
922 function trim(s)
923 return s:gsub('^%s+', ''):gsub('%s+$', '')
926 function ltrim(s)
927 return s:gsub('^%s+', '')
930 function rtrim(s)
931 return s:gsub('%s+$', '')
934 function starts_with(s, prefix)
935 if #s < #prefix then
936 return false
938 for i=1,#prefix do
939 if s:sub(i,i) ~= prefix:sub(i,i) then
940 return false
943 return true
946 function ends_with(s, suffix)
947 if #s < #suffix then
948 return false
950 for i=0,#suffix-1 do
951 if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
952 return false
955 return true