bugfix: infinite loop inside a very narrow window
[view.love.git] / source_text.lua
blobf60123ed54b043c790e0ba8950572f0f7abc5c75
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, hide_cursor, show_line_numbers)
7 local line = State.lines[line_index]
8 local line_cache = State.line_cache[line_index]
9 line_cache.starty = y
10 line_cache.startpos = startpos
11 -- wrap long lines
12 local final_screen_line_starting_pos = startpos -- track value to return
13 Text.populate_screen_line_starting_pos(State, line_index)
14 Text.populate_link_offsets(State, line_index)
15 if show_line_numbers then
16 App.color(Line_number_color)
17 love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y)
18 end
19 initialize_color()
20 assert(#line_cache.screen_line_starting_pos >= 1, 'line cache missing screen line info')
21 for i=1,#line_cache.screen_line_starting_pos do
22 local pos = line_cache.screen_line_starting_pos[i]
23 if pos < startpos then
24 -- render nothing
25 --? print('skipping', screen_line)
26 else
27 final_screen_line_starting_pos = pos
28 local screen_line = Text.screen_line(line, line_cache, i)
29 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
30 local frag_len = utf8.len(screen_line)
31 -- render any highlights
32 for _,link_offsets in ipairs(line_cache.link_offsets) do
33 -- render link decorations
34 local s,e,filename = unpack(link_offsets)
35 local lo, hi = Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
36 if lo then
37 button(State, 'link', {x=State.left+lo, y=y, w=hi-lo, h=State.line_height, bg={r=1,g=1,b=1},
38 icon = icon.hyperlink_decoration,
39 onpress1 = function()
40 if file_exists(filename) then
41 source.switch_to_file(filename)
42 end
43 end,
45 end
46 end
47 if State.selection1.line then
48 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
49 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
50 end
51 if not hide_cursor and line_index == State.cursor1.line then
52 -- render search highlight or cursor
53 if State.search_term then
54 local data = State.lines[State.cursor1.line].data
55 local cursor_offset = Text.offset(data, State.cursor1.pos)
56 if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
57 local save_selection = State.selection1
58 State.selection1 = {line=line_index, pos=State.cursor1.pos+utf8.len(State.search_term)}
59 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
60 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
61 State.selection1 = save_selection
62 end
63 elseif Focus == 'edit' then
64 if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
65 Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
66 elseif pos + frag_len == State.cursor1.pos then
67 -- Show cursor at end of line.
68 -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
69 -- It seems useful to see a cursor whether your eye is on the left or right margin.
70 Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
71 end
72 end
73 end
74 -- render colorized text
75 local x = State.left
76 for frag in screen_line:gmatch('%S*%s*') do
77 select_color(frag)
78 App.screen.print(frag, x,y)
79 x = x+App.width(frag)
80 end
81 y = y + State.line_height
82 if y >= App.screen.height then
83 break
84 end
85 end
86 end
87 return y, final_screen_line_starting_pos
88 end
90 function Text.screen_line(line, line_cache, i)
91 local pos = line_cache.screen_line_starting_pos[i]
92 local offset = Text.offset(line.data, pos)
93 if i >= #line_cache.screen_line_starting_pos then
94 return line.data:sub(offset)
95 end
96 local endpos = line_cache.screen_line_starting_pos[i+1]-1
97 local end_offset = Text.offset(line.data, endpos)
98 return line.data:sub(offset, end_offset)
99 end
101 function Text.draw_cursor(State, x, y)
102 -- blink every 0.5s
103 if math.floor(Cursor_time*2)%2 == 0 then
104 App.color(Cursor_color)
105 love.graphics.rectangle('fill', x,y, 3,State.line_height)
107 State.cursor_x = x
108 State.cursor_y = y+State.line_height
111 function Text.populate_screen_line_starting_pos(State, line_index)
112 local line = State.lines[line_index]
113 if line.mode ~= 'text' then return end
114 local line_cache = State.line_cache[line_index]
115 if line_cache.screen_line_starting_pos then
116 return
118 line_cache.screen_line_starting_pos = {1}
119 local x = 0
120 local pos = 1
121 -- try to wrap at word boundaries
122 for frag in line.data:gmatch('%S*%s*') do
123 local frag_width = App.width(frag)
124 --? print('-- frag:', frag, pos, x, frag_width, State.width)
125 while x + frag_width > State.width do
126 --? print('frag:', frag, pos, x, frag_width, State.width)
127 if x < 0.8 * State.width then
128 -- long word; chop it at some letter
129 -- We're not going to reimplement TeX here.
130 local bpos = Text.nearest_pos_less_than(frag, State.width - x)
131 if x == 0 and bpos == 0 then
132 assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
134 pos = pos + bpos
135 local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
136 frag = string.sub(frag, boffset)
137 --? if bpos > 0 then
138 --? print('after chop:', frag)
139 --? end
140 frag_width = App.width(frag)
142 --? print('screen line:', pos)
143 table.insert(line_cache.screen_line_starting_pos, pos)
144 x = 0 -- new screen line
146 x = x + frag_width
147 pos = pos + utf8.len(frag)
151 function Text.populate_link_offsets(State, line_index)
152 local line = State.lines[line_index]
153 if line.mode ~= 'text' then return end
154 local line_cache = State.line_cache[line_index]
155 if line_cache.link_offsets then
156 return
158 line_cache.link_offsets = {}
159 local pos = 1
160 -- try to wrap at word boundaries
161 local s, e = 1, 0
162 while s <= #line.data do
163 s, e = line.data:find('%[%[%S+%]%]', s)
164 if s == nil then break end
165 local word = line.data:sub(s+2, e-2) -- strip out surrounding '[[..]]'
166 --? print('wikiword:', s, e, word)
167 table.insert(line_cache.link_offsets, {s, e, word})
168 s = e + 1
172 -- Intersect the filename between byte offsets s,e with the bounds of screen line i.
173 -- Return the left/right pixel coordinates of of the intersection,
174 -- or nil if it doesn't intersect with screen line i.
175 function Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
176 local spos = line_cache.screen_line_starting_pos[i]
177 local soff = Text.offset(line.data, spos)
178 if e < soff then
179 return
181 local eoff
182 if i < #line_cache.screen_line_starting_pos then
183 local epos = line_cache.screen_line_starting_pos[i+1]
184 eoff = Text.offset(line.data, epos)
185 if s > eoff then
186 return
189 local loff = math.max(s, soff)
190 local hoff
191 if eoff then
192 hoff = math.min(e, eoff)
193 else
194 hoff = e
196 --? print(s, e, soff, eoff, loff, hoff)
197 return App.width(line.data:sub(soff, loff-1)), App.width(line.data:sub(soff, hoff))
200 function Text.text_input(State, t)
201 if App.mouse_down(1) then return end
202 if App.ctrl_down() or App.alt_down() or App.cmd_down() then return end
203 local before = snapshot(State, State.cursor1.line)
204 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
205 Text.insert_at_cursor(State, t)
206 if State.cursor_y > App.screen.height - State.line_height then
207 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
208 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
210 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
213 function Text.insert_at_cursor(State, t)
214 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
215 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
216 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)
217 Text.clear_screen_line_cache(State, State.cursor1.line)
218 State.cursor1.pos = State.cursor1.pos+1
221 -- Don't handle any keys here that would trigger text_input above.
222 function Text.keychord_press(State, chord)
223 --? print('chord', chord, State.selection1.line, State.selection1.pos)
224 --== shortcuts that mutate text
225 if chord == 'return' then
226 local before_line = State.cursor1.line
227 local before = snapshot(State, before_line)
228 Text.insert_return(State)
229 State.selection1 = {}
230 if State.cursor_y > App.screen.height - State.line_height then
231 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
233 schedule_save(State)
234 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
235 elseif chord == 'tab' then
236 local before = snapshot(State, State.cursor1.line)
237 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
238 Text.insert_at_cursor(State, '\t')
239 if State.cursor_y > App.screen.height - State.line_height then
240 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
241 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
242 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
244 schedule_save(State)
245 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
246 elseif chord == 'backspace' then
247 if State.selection1.line then
248 Text.delete_selection(State, State.left, State.right)
249 schedule_save(State)
250 return
252 local before
253 if State.cursor1.pos > 1 then
254 before = snapshot(State, State.cursor1.line)
255 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
256 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
257 if byte_start then
258 if byte_end then
259 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)
260 else
261 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
263 State.cursor1.pos = State.cursor1.pos-1
265 elseif State.cursor1.line > 1 then
266 before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
267 if State.lines[State.cursor1.line-1].mode == 'drawing' then
268 table.remove(State.lines, State.cursor1.line-1)
269 table.remove(State.line_cache, State.cursor1.line-1)
270 else
271 -- join lines
272 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
273 State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
274 table.remove(State.lines, State.cursor1.line)
275 table.remove(State.line_cache, State.cursor1.line)
277 State.cursor1.line = State.cursor1.line-1
279 if State.screen_top1.line > #State.lines then
280 Text.populate_screen_line_starting_pos(State, #State.lines)
281 local line_cache = State.line_cache[#State.line_cache]
282 State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
283 elseif Text.lt1(State.cursor1, State.screen_top1) then
284 State.screen_top1 = {
285 line=State.cursor1.line,
286 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
288 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
290 Text.clear_screen_line_cache(State, State.cursor1.line)
291 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))
292 schedule_save(State)
293 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
294 elseif chord == 'delete' then
295 if State.selection1.line then
296 Text.delete_selection(State, State.left, State.right)
297 schedule_save(State)
298 return
300 local before
301 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
302 before = snapshot(State, State.cursor1.line)
303 else
304 before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
306 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
307 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
308 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
309 if byte_start then
310 if byte_end then
311 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)
312 else
313 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
315 -- no change to State.cursor1.pos
317 elseif State.cursor1.line < #State.lines then
318 if State.lines[State.cursor1.line+1].mode == 'text' then
319 -- join lines
320 State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
322 table.remove(State.lines, State.cursor1.line+1)
323 table.remove(State.line_cache, State.cursor1.line+1)
325 Text.clear_screen_line_cache(State, State.cursor1.line)
326 schedule_save(State)
327 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
328 --== shortcuts that move the cursor
329 elseif chord == 'left' then
330 Text.left(State)
331 State.selection1 = {}
332 elseif chord == 'right' then
333 Text.right(State)
334 State.selection1 = {}
335 elseif chord == 'S-left' then
336 if State.selection1.line == nil then
337 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
339 Text.left(State)
340 elseif chord == 'S-right' then
341 if State.selection1.line == nil then
342 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
344 Text.right(State)
345 -- C- hotkeys reserved for drawings, so we'll use M-
346 elseif chord == 'M-left' then
347 Text.word_left(State)
348 State.selection1 = {}
349 elseif chord == 'M-right' then
350 Text.word_right(State)
351 State.selection1 = {}
352 elseif chord == 'M-S-left' then
353 if State.selection1.line == nil then
354 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
356 Text.word_left(State)
357 elseif chord == 'M-S-right' then
358 if State.selection1.line == nil then
359 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
361 Text.word_right(State)
362 elseif chord == 'home' then
363 Text.start_of_line(State)
364 State.selection1 = {}
365 elseif chord == 'end' then
366 Text.end_of_line(State)
367 State.selection1 = {}
368 elseif chord == 'S-home' then
369 if State.selection1.line == nil then
370 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
372 Text.start_of_line(State)
373 elseif chord == 'S-end' then
374 if State.selection1.line == nil then
375 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
377 Text.end_of_line(State)
378 elseif chord == 'up' then
379 Text.up(State)
380 State.selection1 = {}
381 elseif chord == 'down' then
382 Text.down(State)
383 State.selection1 = {}
384 elseif chord == 'S-up' then
385 if State.selection1.line == nil then
386 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
388 Text.up(State)
389 elseif chord == 'S-down' then
390 if State.selection1.line == nil then
391 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
393 Text.down(State)
394 elseif chord == 'pageup' then
395 Text.pageup(State)
396 State.selection1 = {}
397 elseif chord == 'pagedown' then
398 Text.pagedown(State)
399 State.selection1 = {}
400 elseif chord == 'S-pageup' then
401 if State.selection1.line == nil then
402 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
404 Text.pageup(State)
405 elseif chord == 'S-pagedown' then
406 if State.selection1.line == nil then
407 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
409 Text.pagedown(State)
413 function Text.insert_return(State)
414 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
415 table.insert(State.lines, State.cursor1.line+1, {mode='text', data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
416 table.insert(State.line_cache, State.cursor1.line+1, {})
417 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
418 Text.clear_screen_line_cache(State, State.cursor1.line)
419 State.cursor1 = {line=State.cursor1.line+1, pos=1}
422 function Text.pageup(State)
423 --? print('pageup')
424 -- duplicate some logic from love.draw
425 local top2 = Text.to2(State, State.screen_top1)
426 --? print(App.screen.height)
427 local y = App.screen.height - State.line_height
428 while y >= State.top do
429 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
430 if State.screen_top1.line == 1 and State.screen_top1.pos == 1 then break end
431 if State.lines[State.screen_top1.line].mode == 'text' then
432 y = y - State.line_height
433 elseif State.lines[State.screen_top1.line].mode == 'drawing' then
434 y = y - Drawing_padding_height - Drawing.pixels(State.lines[State.screen_top1.line].h, State.width)
436 top2 = Text.previous_screen_line(State, top2)
438 State.screen_top1 = Text.to1(State, top2)
439 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
440 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
441 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
442 --? print('pageup end')
445 function Text.pagedown(State)
446 --? print('pagedown')
447 State.screen_top1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
448 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
449 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
450 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
451 --? print('top now', State.screen_top1.line)
452 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
453 --? print('pagedown end')
456 function Text.up(State)
457 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
458 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
459 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
460 if screen_line_starting_pos == 1 then
461 --? print('cursor is at first screen line of its line')
462 -- line is done; skip to previous text line
463 local new_cursor_line = State.cursor1.line
464 while new_cursor_line > 1 do
465 new_cursor_line = new_cursor_line-1
466 if State.lines[new_cursor_line].mode == 'text' then
467 --? print('found previous text line')
468 State.cursor1 = {line=new_cursor_line, pos=nil}
469 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
470 -- previous text line found, pick its final screen line
471 --? print('has multiple screen lines')
472 local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
473 --? print(#screen_line_starting_pos)
474 screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
475 local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
476 local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
477 State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
478 break
481 else
482 -- move up one screen line in current line
483 assert(screen_line_index > 1, 'bumped up against top screen line in line')
484 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
485 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
486 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
487 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
488 --? print('cursor pos is now '..tostring(State.cursor1.pos))
490 if Text.lt1(State.cursor1, State.screen_top1) then
491 State.screen_top1 = {
492 line=State.cursor1.line,
493 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
495 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
499 function Text.down(State)
500 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
501 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
502 assert(State.cursor1.pos, 'cursor has no pos')
503 if Text.cursor_at_final_screen_line(State) then
504 -- line is done, skip to next text line
505 --? print('cursor at final screen line of its line')
506 local new_cursor_line = State.cursor1.line
507 while new_cursor_line < #State.lines do
508 new_cursor_line = new_cursor_line+1
509 if State.lines[new_cursor_line].mode == 'text' then
510 State.cursor1 = {
511 line = new_cursor_line,
512 pos = Text.nearest_cursor_pos(State.lines[new_cursor_line].data, State.cursor_x, State.left),
514 --? print(State.cursor1.pos)
515 break
518 if State.cursor1.line > State.screen_bottom1.line then
519 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
520 --? print('scroll up preserving cursor')
521 Text.snap_cursor_to_bottom_of_screen(State)
522 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
524 else
525 -- move down one screen line in current line
526 local scroll_down = Text.le1(State.screen_bottom1, State.cursor1)
527 --? print('cursor is NOT at final screen line of its line')
528 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
529 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
530 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
531 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
532 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
533 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
534 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
535 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
536 if scroll_down then
537 --? print('scroll up preserving cursor')
538 Text.snap_cursor_to_bottom_of_screen(State)
539 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
542 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
545 function Text.start_of_line(State)
546 State.cursor1.pos = 1
547 if Text.lt1(State.cursor1, State.screen_top1) then
548 State.screen_top1 = {line=State.cursor1.line, pos=State.cursor1.pos} -- copy
552 function Text.end_of_line(State)
553 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
554 if Text.cursor_out_of_screen(State) then
555 Text.snap_cursor_to_bottom_of_screen(State)
559 function Text.word_left(State)
560 -- skip some whitespace
561 while true do
562 if State.cursor1.pos == 1 then
563 break
565 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
566 break
568 Text.left(State)
570 -- skip some non-whitespace
571 while true do
572 Text.left(State)
573 if State.cursor1.pos == 1 then
574 break
576 assert(State.cursor1.pos > 1, 'bumped up against start of line')
577 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
578 break
583 function Text.word_right(State)
584 -- skip some whitespace
585 while true do
586 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
587 break
589 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
590 break
592 Text.right_without_scroll(State)
594 while true do
595 Text.right_without_scroll(State)
596 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
597 break
599 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
600 break
603 if Text.cursor_out_of_screen(State) then
604 Text.snap_cursor_to_bottom_of_screen(State)
608 function Text.match(s, pos, pat)
609 local start_offset = Text.offset(s, pos)
610 local end_offset = Text.offset(s, pos+1)
611 assert(end_offset > start_offset, ('end_offset %d not > start_offset %d'):format(end_offset, start_offset))
612 local curr = s:sub(start_offset, end_offset-1)
613 return curr:match(pat)
616 function Text.left(State)
617 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
618 if State.cursor1.pos > 1 then
619 State.cursor1.pos = State.cursor1.pos-1
620 else
621 local new_cursor_line = State.cursor1.line
622 while new_cursor_line > 1 do
623 new_cursor_line = new_cursor_line-1
624 if State.lines[new_cursor_line].mode == 'text' then
625 State.cursor1 = {
626 line = new_cursor_line,
627 pos = utf8.len(State.lines[new_cursor_line].data) + 1,
629 break
633 if Text.lt1(State.cursor1, State.screen_top1) then
634 State.screen_top1 = {
635 line=State.cursor1.line,
636 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
638 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
642 function Text.right(State)
643 Text.right_without_scroll(State)
644 if Text.cursor_out_of_screen(State) then
645 Text.snap_cursor_to_bottom_of_screen(State)
649 function Text.right_without_scroll(State)
650 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
651 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
652 State.cursor1.pos = State.cursor1.pos+1
653 else
654 local new_cursor_line = State.cursor1.line
655 while new_cursor_line <= #State.lines-1 do
656 new_cursor_line = new_cursor_line+1
657 if State.lines[new_cursor_line].mode == 'text' then
658 State.cursor1 = {line=new_cursor_line, pos=1}
659 break
665 -- result: pos, index of screen line
666 function Text.pos_at_start_of_screen_line(State, loc1)
667 Text.populate_screen_line_starting_pos(State, loc1.line)
668 local line_cache = State.line_cache[loc1.line]
669 for i=#line_cache.screen_line_starting_pos,1,-1 do
670 local spos = line_cache.screen_line_starting_pos[i]
671 if spos <= loc1.pos then
672 return spos,i
675 assert(false, ('invalid pos %d'):format(loc1.pos))
678 function Text.pos_at_end_of_screen_line(State, loc1)
679 Text.populate_screen_line_starting_pos(State, loc1.line)
680 local line_cache = State.line_cache[loc1.line]
681 local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
682 for i=#line_cache.screen_line_starting_pos,1,-1 do
683 local spos = line_cache.screen_line_starting_pos[i]
684 if spos <= loc1.pos then
685 return most_recent_final_pos
687 most_recent_final_pos = spos-1
689 assert(false, ('invalid pos %d'):format(loc1.pos))
692 function Text.cursor_at_final_screen_line(State)
693 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
694 local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos
695 --? print(screen_lines[#screen_lines], State.cursor1.pos)
696 return screen_lines[#screen_lines] <= State.cursor1.pos
699 function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
700 local y = State.top
701 while State.cursor1.line <= #State.lines do
702 if State.lines[State.cursor1.line].mode == 'text' then
703 break
705 --? print('cursor skips', State.cursor1.line)
706 y = y + Drawing_padding_height + Drawing.pixels(State.lines[State.cursor1.line].h, State.width)
707 State.cursor1.line = State.cursor1.line + 1
709 if State.cursor1.pos == nil then
710 State.cursor1.pos = 1
712 -- hack: insert a text line at bottom of file if necessary
713 if State.cursor1.line > #State.lines then
714 assert(State.cursor1.line == #State.lines+1, 'tried to ensure bottom line of file is text, but failed')
715 table.insert(State.lines, {mode='text', data=''})
716 table.insert(State.line_cache, {})
718 --? print(y, App.screen.height, App.screen.height-State.line_height)
719 if y > App.screen.height - State.line_height then
720 --? print('scroll up')
721 Text.snap_cursor_to_bottom_of_screen(State)
725 -- should never modify State.cursor1
726 function Text.snap_cursor_to_bottom_of_screen(State)
727 --? print('to2:', State.cursor1.line, State.cursor1.pos)
728 local top2 = Text.to2(State, State.cursor1)
729 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
730 -- slide to start of screen line
731 top2.screen_pos = 1 -- start of screen line
732 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
733 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
734 local y = App.screen.height - State.line_height
735 -- duplicate some logic from love.draw
736 while true do
737 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
738 if top2.line == 1 and top2.screen_line == 1 then break end
739 if top2.screen_line > 1 or State.lines[top2.line-1].mode == 'text' then
740 local h = State.line_height
741 if y - h < State.top then
742 break
744 y = y - h
745 else
746 assert(top2.line > 1, 'tried to snap cursor to buttom of screen but failed')
747 assert(State.lines[top2.line-1].mode == 'drawing', "expected a drawing but it's not")
748 -- We currently can't draw partial drawings, so either skip it entirely
749 -- or not at all.
750 local h = Drawing_padding_height + Drawing.pixels(State.lines[top2.line-1].h, State.width)
751 if y - h < State.top then
752 break
754 --? print('skipping drawing of height', h)
755 y = y - h
757 top2 = Text.previous_screen_line(State, top2)
759 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
760 State.screen_top1 = Text.to1(State, top2)
761 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
762 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
763 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
766 function Text.in_line(State, line_index, x,y)
767 local line = State.lines[line_index]
768 local line_cache = State.line_cache[line_index]
769 if line_cache.starty == nil then return false end -- outside current page
770 if y < line_cache.starty then return false end
771 Text.populate_screen_line_starting_pos(State, line_index)
772 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)
775 -- convert mx,my in pixels to schema-1 coordinates
776 function Text.to_pos_on_line(State, line_index, mx, my)
777 local line = State.lines[line_index]
778 local line_cache = State.line_cache[line_index]
779 assert(my >= line_cache.starty, 'failed to map y pixel to line')
780 -- duplicate some logic from Text.draw
781 local y = line_cache.starty
782 local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
783 for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
784 local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
785 local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
786 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
787 local nexty = y + State.line_height
788 if my < nexty then
789 -- On all wrapped screen lines but the final one, clicks past end of
790 -- line position cursor on final character of screen line.
791 -- (The final screen line positions past end of screen line as always.)
792 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
793 --? print('past end of non-final line; return')
794 return line_cache.screen_line_starting_pos[screen_line_index+1]-1
796 local s = string.sub(line.data, screen_line_starting_byte_offset)
797 --? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
798 return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1
800 y = nexty
802 assert(false, 'failed to map y pixel to line')
805 function Text.screen_line_width(State, line_index, i)
806 local line = State.lines[line_index]
807 local line_cache = State.line_cache[line_index]
808 local start_pos = line_cache.screen_line_starting_pos[i]
809 local start_offset = Text.offset(line.data, start_pos)
810 local screen_line
811 if i < #line_cache.screen_line_starting_pos then
812 local past_end_pos = line_cache.screen_line_starting_pos[i+1]
813 local past_end_offset = Text.offset(line.data, past_end_pos)
814 screen_line = string.sub(line.data, start_offset, past_end_offset-1)
815 else
816 screen_line = string.sub(line.data, start_pos)
818 return App.width(screen_line)
821 function Text.screen_line_index(screen_line_starting_pos, pos)
822 for i = #screen_line_starting_pos,1,-1 do
823 if screen_line_starting_pos[i] <= pos then
824 return i
829 -- convert x pixel coordinate to pos
830 -- oblivious to wrapping
831 -- result: 1 to len+1
832 function Text.nearest_cursor_pos(line, x, left)
833 if x < left then
834 return 1
836 local len = utf8.len(line)
837 local max_x = left+Text.x(line, len+1)
838 if x > max_x then
839 return len+1
841 local leftpos, rightpos = 1, len+1
842 --? print('-- nearest', x)
843 while true do
844 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
845 if leftpos == rightpos then
846 return leftpos
848 local curr = math.floor((leftpos+rightpos)/2)
849 local currxmin = left+Text.x(line, curr)
850 local currxmax = left+Text.x(line, curr+1)
851 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
852 if currxmin <= x and x < currxmax then
853 if x-currxmin < currxmax-x then
854 return curr
855 else
856 return curr+1
859 if leftpos >= rightpos-1 then
860 return rightpos
862 if currxmin > x then
863 rightpos = curr
864 else
865 leftpos = curr
868 assert(false, 'failed to map x pixel to pos')
871 -- return the nearest index of line (in utf8 code points) which lies entirely
872 -- within x pixels of the left margin
873 -- result: 0 to len+1
874 function Text.nearest_pos_less_than(line, x)
875 --? print('', '-- nearest_pos_less_than', line, x)
876 local len = utf8.len(line)
877 local max_x = Text.x_after(line, len)
878 if x > max_x then
879 return len+1
881 local left, right = 0, len+1
882 while true do
883 local curr = math.floor((left+right)/2)
884 local currxmin = Text.x_after(line, curr+1)
885 local currxmax = Text.x_after(line, curr+2)
886 --? print('', x, left, right, curr, currxmin, currxmax)
887 if currxmin <= x and x < currxmax then
888 return curr
890 if left >= right-1 then
891 return left
893 if currxmin > x then
894 right = curr
895 else
896 left = curr
899 assert(false, 'failed to map x pixel to pos')
902 function Text.x_after(s, pos)
903 local offset = Text.offset(s, math.min(pos+1, #s+1))
904 local s_before = s:sub(1, offset-1)
905 --? print('^'..s_before..'$')
906 return App.width(s_before)
909 function Text.x(s, pos)
910 local offset = Text.offset(s, pos)
911 local s_before = s:sub(1, offset-1)
912 return App.width(s_before)
915 function Text.to2(State, loc1)
916 if State.lines[loc1.line].mode == 'drawing' then
917 return {line=loc1.line, screen_line=1, screen_pos=1}
919 local result = {line=loc1.line}
920 local line_cache = State.line_cache[loc1.line]
921 Text.populate_screen_line_starting_pos(State, loc1.line)
922 for i=#line_cache.screen_line_starting_pos,1,-1 do
923 local spos = line_cache.screen_line_starting_pos[i]
924 if spos <= loc1.pos then
925 result.screen_line = i
926 result.screen_pos = loc1.pos - spos + 1
927 break
930 assert(result.screen_pos, 'failed to convert schema-1 coordinate to schema-2')
931 return result
934 function Text.to1(State, loc2)
935 local result = {line=loc2.line, pos=loc2.screen_pos}
936 if loc2.screen_line > 1 then
937 result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
939 return result
942 function Text.eq1(a, b)
943 return a.line == b.line and a.pos == b.pos
946 function Text.lt1(a, b)
947 if a.line < b.line then
948 return true
950 if a.line > b.line then
951 return false
953 return a.pos < b.pos
956 function Text.le1(a, b)
957 if a.line < b.line then
958 return true
960 if a.line > b.line then
961 return false
963 return a.pos <= b.pos
966 function Text.offset(s, pos1)
967 if pos1 == 1 then return 1 end
968 local result = utf8.offset(s, pos1)
969 if result == nil then
970 print(pos1, #s, s)
972 assert(result, "Text.offset returned nil; this is likely a failure to handle utf8")
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 -- resize helper
991 function Text.tweak_screen_top_and_cursor(State)
992 if State.screen_top1.pos == 1 then return end
993 Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
994 local line = State.lines[State.screen_top1.line]
995 local line_cache = State.line_cache[State.screen_top1.line]
996 for i=2,#line_cache.screen_line_starting_pos do
997 local pos = line_cache.screen_line_starting_pos[i]
998 if pos == State.screen_top1.pos then
999 break
1001 if pos > State.screen_top1.pos then
1002 -- make sure screen top is at start of a screen line
1003 local prev = line_cache.screen_line_starting_pos[i-1]
1004 if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
1005 State.screen_top1.pos = prev
1006 else
1007 State.screen_top1.pos = pos
1009 break
1012 -- make sure cursor is on screen
1013 if Text.lt1(State.cursor1, State.screen_top1) then
1014 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
1015 elseif State.cursor1.line >= State.screen_bottom1.line then
1016 --? print('too low')
1017 if Text.cursor_out_of_screen(State) then
1018 --? print('tweak')
1019 State.cursor1 = {
1020 line=State.screen_bottom1.line,
1021 pos=Text.to_pos_on_line(State, State.screen_bottom1.line, State.right-5, App.screen.height-5),
1027 -- slightly expensive since it redraws the screen
1028 function Text.cursor_out_of_screen(State)
1029 edit.draw(State)
1030 return State.cursor_y == nil
1031 -- this approach is cheaper and almost works, except on the final screen
1032 -- where file ends above bottom of screen
1033 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
1034 --? local botline1 = {line=State.cursor1.line, pos=botpos}
1035 --? return Text.lt1(State.screen_bottom1, botline1)
1038 function Text.redraw_all(State)
1039 --? print('clearing fragments')
1040 -- Perform some early sanity checking here, in hopes that we correctly call
1041 -- this whenever we change editor state.
1042 if State.right <= State.left then
1043 assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left))
1046 State.line_cache = {}
1047 for i=1,#State.lines do
1048 State.line_cache[i] = {}
1052 function Text.clear_screen_line_cache(State, line_index)
1053 State.line_cache[line_index].screen_line_starting_pos = nil
1054 State.line_cache[line_index].link_offsets = nil
1057 function trim(s)
1058 return s:gsub('^%s+', ''):gsub('%s+$', '')
1061 function ltrim(s)
1062 return s:gsub('^%s+', '')
1065 function rtrim(s)
1066 return s:gsub('%s+$', '')
1069 function starts_with(s, prefix)
1070 if #s < #prefix then
1071 return false
1073 for i=1,#prefix do
1074 if s:sub(i,i) ~= prefix:sub(i,i) then
1075 return false
1078 return true
1081 function ends_with(s, suffix)
1082 if #s < #suffix then
1083 return false
1085 for i=0,#suffix-1 do
1086 if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
1087 return false
1090 return true