bugfix in search UI
[view.love.git] / source_text.lua
blob284ed007c83f4f9b7f6881f09a931f83a3a8e1e7
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, hide_cursor, show_line_numbers)
7 local line = State.lines[line_index]
8 local line_cache = State.line_cache[line_index]
9 line_cache.startpos = startpos
10 -- wrap long lines
11 Text.populate_screen_line_starting_pos(State, line_index)
12 Text.populate_link_offsets(State, line_index)
13 if show_line_numbers then
14 App.color(Line_number_color)
15 love.graphics.print(line_index, State.left-Line_number_width*State.font:getWidth('m')+10,y)
16 end
17 initialize_color()
18 assert(#line_cache.screen_line_starting_pos >= 1, 'line cache missing screen line info')
19 for i=1,#line_cache.screen_line_starting_pos do
20 local pos = line_cache.screen_line_starting_pos[i]
21 if pos < startpos then
22 -- render nothing
23 --? print('skipping', screen_line)
24 else
25 local screen_line = Text.screen_line(line, line_cache, i)
26 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
27 local frag_len = utf8.len(screen_line)
28 -- render any highlights
29 for _,link_offsets in ipairs(line_cache.link_offsets) do
30 -- render link decorations
31 local s,e,filename = unpack(link_offsets)
32 local lo, hi = Text.clip_wikiword_with_screen_line(State.font, line, line_cache, i, s, e)
33 if lo then
34 button(State, 'link', {x=State.left+lo, y=y, w=hi-lo, h=State.line_height,
35 icon = icon.hyperlink_decoration,
36 onpress1 = function()
37 if file_exists(filename) then
38 source.switch_to_file(filename)
39 end
40 end,
42 end
43 end
44 if State.selection1.line then
45 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
46 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
47 end
48 if not hide_cursor and line_index == State.cursor1.line then
49 -- render search highlight or cursor
50 if State.search_term then
51 local data = State.lines[State.cursor1.line].data
52 local cursor_offset = Text.offset(data, State.cursor1.pos)
53 if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
54 local save_selection = State.selection1
55 State.selection1 = {line=line_index, pos=State.cursor1.pos+utf8.len(State.search_term)}
56 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
57 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
58 State.selection1 = save_selection
59 end
60 elseif Focus == 'edit' then
61 if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
62 Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
63 elseif pos + frag_len == State.cursor1.pos then
64 -- Show cursor at end of line.
65 -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
66 -- It seems useful to see a cursor whether your eye is on the left or right margin.
67 Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
68 end
69 end
70 end
71 -- render colorized text
72 local x = State.left
73 for frag in screen_line:gmatch('%S*%s*') do
74 select_color(frag)
75 App.screen.print(frag, x,y)
76 x = x+State.font:getWidth(frag)
77 end
78 y = y + State.line_height
79 if y >= App.screen.height then
80 break
81 end
82 end
83 end
84 return y
85 end
87 function Text.screen_line(line, line_cache, i)
88 local pos = line_cache.screen_line_starting_pos[i]
89 local offset = Text.offset(line.data, pos)
90 if i >= #line_cache.screen_line_starting_pos then
91 return line.data:sub(offset)
92 end
93 local endpos = line_cache.screen_line_starting_pos[i+1]
94 local end_offset = Text.offset(line.data, endpos)
95 return line.data:sub(offset, end_offset-1)
96 end
98 function Text.draw_cursor(State, x, y)
99 -- blink every 0.5s
100 if math.floor(Cursor_time*2)%2 == 0 then
101 App.color(Cursor_color)
102 love.graphics.rectangle('fill', x,y, 3,State.line_height)
104 State.cursor_x = x
105 State.cursor_y = y+State.line_height
108 function Text.populate_screen_line_starting_pos(State, line_index)
109 local line = State.lines[line_index]
110 if line.mode ~= 'text' then return end
111 local line_cache = State.line_cache[line_index]
112 if line_cache.screen_line_starting_pos then
113 return
115 line_cache.screen_line_starting_pos = {1}
116 local x = 0
117 local pos = 1
118 -- try to wrap at word boundaries
119 for frag in line.data:gmatch('%S*%s*') do
120 local frag_width = State.font:getWidth(frag)
121 --? print('-- frag:', frag, pos, x, frag_width, State.width)
122 while x + frag_width > State.width do
123 --? print('frag:', frag, pos, x, frag_width, State.width)
124 if x < 0.8 * State.width then
125 -- long word; chop it at some letter
126 -- We're not going to reimplement TeX here.
127 local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
128 if x == 0 and bpos == 0 then
129 assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
131 pos = pos + bpos
132 local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
133 frag = string.sub(frag, boffset)
134 --? if bpos > 0 then
135 --? print('after chop:', frag)
136 --? end
137 frag_width = State.font:getWidth(frag)
139 --? print('screen line:', pos)
140 table.insert(line_cache.screen_line_starting_pos, pos)
141 x = 0 -- new screen line
143 x = x + frag_width
144 pos = pos + utf8.len(frag)
148 function Text.populate_link_offsets(State, line_index)
149 local line = State.lines[line_index]
150 if line.mode ~= 'text' then return end
151 local line_cache = State.line_cache[line_index]
152 if line_cache.link_offsets then
153 return
155 line_cache.link_offsets = {}
156 local pos = 1
157 -- try to wrap at word boundaries
158 local s, e = 1, 0
159 while s <= #line.data do
160 s, e = line.data:find('%[%[%S+%]%]', s)
161 if s == nil then break end
162 local word = line.data:sub(s+2, e-2) -- strip out surrounding '[[..]]'
163 --? print('wikiword:', s, e, word)
164 table.insert(line_cache.link_offsets, {s, e, word})
165 s = e + 1
169 -- Intersect the filename between byte offsets s,e with the bounds of screen line i.
170 -- Return the left/right pixel coordinates of of the intersection,
171 -- or nil if it doesn't intersect with screen line i.
172 function Text.clip_wikiword_with_screen_line(font, line, line_cache, i, s, e)
173 local spos = line_cache.screen_line_starting_pos[i]
174 local soff = Text.offset(line.data, spos)
175 if e < soff then
176 return
178 local eoff
179 if i < #line_cache.screen_line_starting_pos then
180 local epos = line_cache.screen_line_starting_pos[i+1]
181 eoff = Text.offset(line.data, epos)
182 if s > eoff then
183 return
186 local loff = math.max(s, soff)
187 local hoff
188 if eoff then
189 hoff = math.min(e, eoff)
190 else
191 hoff = e
193 --? print(s, e, soff, eoff, loff, hoff)
194 return font:getWidth(line.data:sub(soff, loff-1)), font:getWidth(line.data:sub(soff, hoff))
197 function Text.text_input(State, t)
198 if App.mouse_down(1) then return end
199 if App.any_modifier_down() then
200 if App.key_down(t) then
201 -- The modifiers didn't change the key. Handle it in keychord_press.
202 return
203 else
204 -- Key mutated by the keyboard layout. Continue below.
207 local before = snapshot(State, State.cursor1.line)
208 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
209 Text.insert_at_cursor(State, t)
210 if State.cursor_y > App.screen.height - State.line_height then
211 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
212 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
214 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
217 function Text.insert_at_cursor(State, t)
218 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
219 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
220 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)
221 Text.clear_screen_line_cache(State, State.cursor1.line)
222 State.cursor1.pos = State.cursor1.pos+1
225 -- Don't handle any keys here that would trigger text_input above.
226 function Text.keychord_press(State, chord)
227 --? print('chord', chord, State.selection1.line, State.selection1.pos)
228 --== shortcuts that mutate text
229 if chord == 'return' then
230 local before_line = State.cursor1.line
231 local before = snapshot(State, before_line)
232 Text.insert_return(State)
233 if State.cursor_y > App.screen.height - State.line_height then
234 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
236 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
237 schedule_save(State)
238 elseif chord == 'tab' then
239 local before = snapshot(State, State.cursor1.line)
240 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
241 Text.insert_at_cursor(State, '\t')
242 if State.cursor_y > App.screen.height - State.line_height then
243 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
244 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
245 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
247 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
248 schedule_save(State)
249 elseif chord == 'backspace' then
250 if State.selection1.line then
251 Text.delete_selection_and_record_undo_event(State)
252 schedule_save(State)
253 return
255 local before
256 if State.cursor1.pos > 1 then
257 before = snapshot(State, State.cursor1.line)
258 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
259 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
260 if byte_start then
261 if byte_end then
262 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)
263 else
264 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
266 State.cursor1.pos = State.cursor1.pos-1
268 elseif State.cursor1.line > 1 then
269 before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
270 if State.lines[State.cursor1.line-1].mode == 'drawing' then
271 table.remove(State.lines, State.cursor1.line-1)
272 table.remove(State.line_cache, State.cursor1.line-1)
273 else
274 -- join lines
275 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
276 State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
277 table.remove(State.lines, State.cursor1.line)
278 table.remove(State.line_cache, State.cursor1.line)
280 State.cursor1.line = State.cursor1.line-1
282 if State.screen_top1.line > #State.lines then
283 Text.populate_screen_line_starting_pos(State, #State.lines)
284 local line_cache = State.line_cache[#State.line_cache]
285 State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
286 elseif Text.lt1(State.cursor1, State.screen_top1) then
287 State.screen_top1 = {
288 line=State.cursor1.line,
289 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
291 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
293 Text.clear_screen_line_cache(State, State.cursor1.line)
294 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))
295 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
296 schedule_save(State)
297 elseif chord == 'delete' then
298 if State.selection1.line then
299 Text.delete_selection_and_record_undo_event(State)
300 schedule_save(State)
301 return
303 local before
304 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
305 before = snapshot(State, State.cursor1.line)
306 else
307 before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
309 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
310 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
311 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
312 if byte_start then
313 if byte_end then
314 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)
315 else
316 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
318 -- no change to State.cursor1.pos
320 elseif State.cursor1.line < #State.lines then
321 if State.lines[State.cursor1.line+1].mode == 'text' then
322 -- join lines
323 State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
325 table.remove(State.lines, State.cursor1.line+1)
326 table.remove(State.line_cache, State.cursor1.line+1)
328 Text.clear_screen_line_cache(State, State.cursor1.line)
329 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
330 schedule_save(State)
331 --== shortcuts that move the cursor
332 elseif chord == 'left' then
333 Text.left(State)
334 State.selection1 = {}
335 elseif chord == 'right' then
336 Text.right(State)
337 State.selection1 = {}
338 elseif chord == 'S-left' then
339 if State.selection1.line == nil then
340 State.selection1 = deepcopy(State.cursor1)
342 Text.left(State)
343 elseif chord == 'S-right' then
344 if State.selection1.line == nil then
345 State.selection1 = deepcopy(State.cursor1)
347 Text.right(State)
348 -- C- hotkeys reserved for drawings, so we'll use M-
349 elseif chord == 'M-left' then
350 Text.word_left(State)
351 State.selection1 = {}
352 elseif chord == 'M-right' then
353 Text.word_right(State)
354 State.selection1 = {}
355 elseif chord == 'M-S-left' then
356 if State.selection1.line == nil then
357 State.selection1 = deepcopy(State.cursor1)
359 Text.word_left(State)
360 elseif chord == 'M-S-right' then
361 if State.selection1.line == nil then
362 State.selection1 = deepcopy(State.cursor1)
364 Text.word_right(State)
365 elseif chord == 'home' then
366 Text.start_of_line(State)
367 State.selection1 = {}
368 elseif chord == 'end' then
369 Text.end_of_line(State)
370 State.selection1 = {}
371 elseif chord == 'S-home' then
372 if State.selection1.line == nil then
373 State.selection1 = deepcopy(State.cursor1)
375 Text.start_of_line(State)
376 elseif chord == 'S-end' then
377 if State.selection1.line == nil then
378 State.selection1 = deepcopy(State.cursor1)
380 Text.end_of_line(State)
381 elseif chord == 'up' then
382 Text.up(State)
383 State.selection1 = {}
384 elseif chord == 'down' then
385 Text.down(State)
386 State.selection1 = {}
387 elseif chord == 'S-up' then
388 if State.selection1.line == nil then
389 State.selection1 = deepcopy(State.cursor1)
391 Text.up(State)
392 elseif chord == 'S-down' then
393 if State.selection1.line == nil then
394 State.selection1 = deepcopy(State.cursor1)
396 Text.down(State)
397 elseif chord == 'pageup' then
398 Text.pageup(State)
399 State.selection1 = {}
400 elseif chord == 'pagedown' then
401 Text.pagedown(State)
402 State.selection1 = {}
403 elseif chord == 'S-pageup' then
404 if State.selection1.line == nil then
405 State.selection1 = deepcopy(State.cursor1)
407 Text.pageup(State)
408 elseif chord == 'S-pagedown' then
409 if State.selection1.line == nil then
410 State.selection1 = deepcopy(State.cursor1)
412 Text.pagedown(State)
416 function Text.insert_return(State)
417 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
418 table.insert(State.lines, State.cursor1.line+1, {mode='text', data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
419 table.insert(State.line_cache, State.cursor1.line+1, {})
420 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
421 Text.clear_screen_line_cache(State, State.cursor1.line)
422 State.cursor1 = {line=State.cursor1.line+1, pos=1}
425 function Text.pageup(State)
426 State.screen_top1 = Text.previous_screen_top1(State)
427 State.cursor1 = deepcopy(State.screen_top1)
428 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
429 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
432 -- return the top y coordinate of a given line_index,
433 -- or nil if no part of it is on screen
434 function Text.starty(State, line_index)
435 -- duplicate some logic from love.draw
436 -- does not modify State (except to populate line_cache)
437 if line_index < State.screen_top1.line then return end
438 local loc2 = Text.to2(State, State.screen_top1)
439 local y = State.top
440 while true do
441 if State.lines[loc2.line].mode == 'drawing' then
442 y = y + Drawing_padding_top
444 if loc2.line == line_index then return y end
445 if State.lines[loc2.line].mode == 'text' then
446 y = y + State.line_height
447 elseif State.lines[loc2.line].mode == 'drawing' then
448 y = y + Drawing.pixels(State.lines[loc2.line].h, State.width) + Drawing_padding_bottom
450 if y + State.line_height > App.screen.height then break end
451 local next_loc2 = Text.next_screen_line(State, loc2)
452 if Text.eq2(next_loc2, loc2) then break end -- end of file
453 loc2 = next_loc2
457 function Text.previous_screen_top1(State)
458 -- duplicate some logic from love.draw
459 -- does not modify State (except to populate line_cache)
460 local loc2 = Text.to2(State, State.screen_top1)
461 local y = App.screen.height - State.line_height
462 while y >= State.top do
463 if loc2.line == 1 and loc2.screen_line == 1 and loc2.screen_pos == 1 then break end
464 if State.lines[loc2.line].mode == 'text' then
465 y = y - State.line_height
466 elseif State.lines[loc2.line].mode == 'drawing' then
467 y = y - Drawing_padding_height - Drawing.pixels(State.lines[loc2.line].h, State.width)
469 loc2 = Text.previous_screen_line(State, loc2)
471 return Text.to1(State, loc2)
474 function Text.pagedown(State)
475 State.screen_top1 = Text.screen_bottom1(State)
476 State.cursor1 = deepcopy(State.screen_top1)
477 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
478 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
481 -- return the location of the start of the bottom-most line on screen
482 function Text.screen_bottom1(State)
483 -- duplicate some logic from love.draw
484 -- does not modify State (except to populate line_cache)
485 local loc2 = Text.to2(State, State.screen_top1)
486 local y = State.top
487 while true do
488 if State.lines[loc2.line].mode == 'text' then
489 y = y + State.line_height
490 elseif State.lines[loc2.line].mode == 'drawing' then
491 y = y + Drawing_padding_height + Drawing.pixels(State.lines[loc2.line].h, State.width)
493 if y + State.line_height > App.screen.height then break end
494 local next_loc2 = Text.next_screen_line(State, loc2)
495 if Text.eq2(next_loc2, loc2) then break end
496 loc2 = next_loc2
498 return Text.to1(State, loc2)
501 function Text.up(State)
502 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
503 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
504 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
505 if screen_line_starting_pos == 1 then
506 --? print('cursor is at first screen line of its line')
507 -- line is done; skip to previous text line
508 local new_cursor_line = State.cursor1.line
509 while new_cursor_line > 1 do
510 new_cursor_line = new_cursor_line-1
511 if State.lines[new_cursor_line].mode == 'text' then
512 --? print('found previous text line')
513 State.cursor1 = {line=new_cursor_line, pos=nil}
514 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
515 -- previous text line found, pick its final screen line
516 --? print('has multiple screen lines')
517 local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
518 --? print(#screen_line_starting_pos)
519 screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
520 local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
521 local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
522 State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
523 break
526 else
527 -- move up one screen line in current line
528 assert(screen_line_index > 1, 'bumped up against top screen line in line')
529 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
530 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
531 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
532 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
533 --? print('cursor pos is now '..tostring(State.cursor1.pos))
535 if Text.lt1(State.cursor1, State.screen_top1) then
536 State.screen_top1 = {
537 line=State.cursor1.line,
538 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
540 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
544 function Text.down(State)
545 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
546 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
547 assert(State.cursor1.pos, 'cursor has no pos')
548 if Text.cursor_at_final_screen_line(State) then
549 -- line is done, skip to next text line
550 --? print('cursor at final screen line of its line')
551 local new_cursor_line = State.cursor1.line
552 while new_cursor_line < #State.lines do
553 new_cursor_line = new_cursor_line+1
554 if State.lines[new_cursor_line].mode == 'text' then
555 State.cursor1 = {
556 line = new_cursor_line,
557 pos = Text.nearest_cursor_pos(State.font, State.lines[new_cursor_line].data, State.cursor_x, State.left),
559 --? print(State.cursor1.pos)
560 break
563 local screen_bottom1 = Text.screen_bottom1(State)
564 --? print('down 2', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, screen_bottom1.line, screen_bottom1.pos)
565 if State.cursor1.line > screen_bottom1.line then
566 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
567 --? print('scroll up preserving cursor')
568 Text.snap_cursor_to_bottom_of_screen(State)
569 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
571 else
572 -- move down one screen line in current line
573 local screen_bottom1 = Text.screen_bottom1(State)
574 local scroll_down = Text.le1(screen_bottom1, State.cursor1)
575 --? print('cursor is NOT at final screen line of its line')
576 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
577 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
578 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
579 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
580 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
581 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
582 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
583 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
584 if scroll_down then
585 --? print('scroll up preserving cursor')
586 Text.snap_cursor_to_bottom_of_screen(State)
587 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
590 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
593 function Text.start_of_line(State)
594 State.cursor1.pos = 1
595 if Text.lt1(State.cursor1, State.screen_top1) then
596 State.screen_top1 = deepcopy(State.cursor1)
600 function Text.end_of_line(State)
601 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
602 if Text.cursor_out_of_screen(State) then
603 Text.snap_cursor_to_bottom_of_screen(State)
607 function Text.word_left(State)
608 -- skip some whitespace
609 while true do
610 if State.cursor1.pos == 1 then
611 break
613 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
614 break
616 Text.left(State)
618 -- skip some non-whitespace
619 while true do
620 Text.left(State)
621 if State.cursor1.pos == 1 then
622 break
624 assert(State.cursor1.pos > 1, 'bumped up against start of line')
625 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
626 break
631 function Text.word_right(State)
632 -- skip some whitespace
633 while true do
634 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
635 break
637 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
638 break
640 Text.right_without_scroll(State)
642 while true do
643 Text.right_without_scroll(State)
644 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
645 break
647 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
648 break
651 if Text.cursor_out_of_screen(State) then
652 Text.snap_cursor_to_bottom_of_screen(State)
656 function Text.match(s, pos, pat)
657 local start_offset = Text.offset(s, pos)
658 local end_offset = Text.offset(s, pos+1)
659 assert(end_offset > start_offset, ('end_offset %d not > start_offset %d'):format(end_offset, start_offset))
660 local curr = s:sub(start_offset, end_offset-1)
661 return curr:match(pat)
664 function Text.left(State)
665 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
666 if State.cursor1.pos > 1 then
667 State.cursor1.pos = State.cursor1.pos-1
668 else
669 local new_cursor_line = State.cursor1.line
670 while new_cursor_line > 1 do
671 new_cursor_line = new_cursor_line-1
672 if State.lines[new_cursor_line].mode == 'text' then
673 State.cursor1 = {
674 line = new_cursor_line,
675 pos = utf8.len(State.lines[new_cursor_line].data) + 1,
677 break
681 if Text.lt1(State.cursor1, State.screen_top1) then
682 State.screen_top1 = {
683 line=State.cursor1.line,
684 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
686 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
690 function Text.right(State)
691 Text.right_without_scroll(State)
692 if Text.cursor_out_of_screen(State) then
693 Text.snap_cursor_to_bottom_of_screen(State)
697 function Text.right_without_scroll(State)
698 assert(State.lines[State.cursor1.line].mode == 'text', 'line is not text')
699 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
700 State.cursor1.pos = State.cursor1.pos+1
701 else
702 local new_cursor_line = State.cursor1.line
703 while new_cursor_line <= #State.lines-1 do
704 new_cursor_line = new_cursor_line+1
705 if State.lines[new_cursor_line].mode == 'text' then
706 State.cursor1 = {line=new_cursor_line, pos=1}
707 break
713 -- result: pos, index of screen line
714 function Text.pos_at_start_of_screen_line(State, loc1)
715 Text.populate_screen_line_starting_pos(State, loc1.line)
716 local line_cache = State.line_cache[loc1.line]
717 for i=#line_cache.screen_line_starting_pos,1,-1 do
718 local spos = line_cache.screen_line_starting_pos[i]
719 if spos <= loc1.pos then
720 return spos,i
723 assert(false, ('invalid pos %d'):format(loc1.pos))
726 function Text.pos_at_end_of_screen_line(State, loc1)
727 assert(State.lines[loc1.line].mode == 'text')
728 Text.populate_screen_line_starting_pos(State, loc1.line)
729 local line_cache = State.line_cache[loc1.line]
730 local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
731 for i=#line_cache.screen_line_starting_pos,1,-1 do
732 local spos = line_cache.screen_line_starting_pos[i]
733 if spos <= loc1.pos then
734 return most_recent_final_pos
736 most_recent_final_pos = spos-1
738 assert(false, ('invalid pos %d'):format(loc1.pos))
741 function Text.final_text_loc_on_screen(State)
742 local screen_bottom1 = Text.screen_bottom1(State)
743 if State.lines[screen_bottom1.line].mode == 'text' then
744 return {
745 line=screen_bottom1.line,
746 pos=Text.pos_at_end_of_screen_line(State, screen_bottom1),
749 local loc2 = Text.to2(State, screen_bottom1)
750 while true do
751 if State.lines[loc2.line].mode == 'text' then break end
752 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
753 loc2 = Text.previous_screen_line(State, loc2)
755 local result = Text.to1(State, loc2)
756 result.pos = Text.pos_at_end_of_screen_line(State, result)
757 return result
760 function Text.cursor_at_final_screen_line(State)
761 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
762 local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos
763 --? print(screen_lines[#screen_lines], State.cursor1.pos)
764 return screen_lines[#screen_lines] <= State.cursor1.pos
767 function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
768 local y = State.top
769 while State.cursor1.line <= #State.lines do
770 if State.lines[State.cursor1.line].mode == 'text' then
771 break
773 --? print('cursor skips', State.cursor1.line)
774 y = y + Drawing_padding_height + Drawing.pixels(State.lines[State.cursor1.line].h, State.width)
775 State.cursor1.line = State.cursor1.line + 1
777 if State.cursor1.pos == nil then
778 State.cursor1.pos = 1
780 -- hack: insert a text line at bottom of file if necessary
781 if State.cursor1.line > #State.lines then
782 assert(State.cursor1.line == #State.lines+1, 'tried to ensure bottom line of file is text, but failed')
783 table.insert(State.lines, {mode='text', data=''})
784 table.insert(State.line_cache, {})
786 --? print(y, App.screen.height, App.screen.height-State.line_height)
787 if y > App.screen.height - State.line_height then
788 --? print('scroll up')
789 Text.snap_cursor_to_bottom_of_screen(State)
793 -- should never modify State.cursor1
794 function Text.snap_cursor_to_bottom_of_screen(State)
795 --? print('to2:', State.cursor1.line, State.cursor1.pos)
796 local top2 = Text.to2(State, State.cursor1)
797 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
798 -- slide to start of screen line
799 top2.screen_pos = 1 -- start of screen line
800 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
801 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
802 local y = App.screen.height - State.line_height
803 -- duplicate some logic from love.draw
804 while true do
805 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
806 if top2.line == 1 and top2.screen_line == 1 then break end
807 if top2.screen_line > 1 or State.lines[top2.line-1].mode == 'text' then
808 local h = State.line_height
809 if y - h < State.top then
810 break
812 y = y - h
813 else
814 assert(top2.line > 1, 'tried to snap cursor to buttom of screen but failed')
815 assert(State.lines[top2.line-1].mode == 'drawing', "expected a drawing but it's not")
816 -- We currently can't draw partial drawings, so either skip it entirely
817 -- or not at all.
818 local h = Drawing_padding_height + Drawing.pixels(State.lines[top2.line-1].h, State.width)
819 if y - h < State.top then
820 break
822 --? print('skipping drawing of height', h)
823 y = y - h
825 top2 = Text.previous_screen_line(State, top2)
827 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
828 State.screen_top1 = Text.to1(State, top2)
829 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
830 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
831 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
834 function Text.in_line(State, line_index, x,y)
835 local line = State.lines[line_index]
836 local line_cache = State.line_cache[line_index]
837 local starty = Text.starty(State, line_index)
838 if starty == nil then return false end -- outside current page
839 if y < starty then return false end
840 Text.populate_screen_line_starting_pos(State, line_index)
841 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)
844 -- convert mx,my in pixels to schema-1 coordinates
845 function Text.to_pos_on_line(State, line_index, mx, my)
846 local line = State.lines[line_index]
847 local line_cache = State.line_cache[line_index]
848 local starty = Text.starty(State, line_index)
849 assert(my >= starty, 'failed to map y pixel to line')
850 -- duplicate some logic from Text.draw
851 local y = starty
852 local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
853 for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
854 local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
855 local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
856 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
857 local nexty = y + State.line_height
858 if my < nexty then
859 -- On all wrapped screen lines but the final one, clicks past end of
860 -- line position cursor on final character of screen line.
861 -- (The final screen line positions past end of screen line as always.)
862 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
863 --? print('past end of non-final line; return')
864 return line_cache.screen_line_starting_pos[screen_line_index+1]
866 local s = string.sub(line.data, screen_line_starting_byte_offset)
867 --? 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)
868 return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
870 y = nexty
872 assert(false, 'failed to map y pixel to line')
875 function Text.screen_line_width(State, line_index, i)
876 local line = State.lines[line_index]
877 local line_cache = State.line_cache[line_index]
878 local start_pos = line_cache.screen_line_starting_pos[i]
879 local start_offset = Text.offset(line.data, start_pos)
880 local screen_line
881 if i < #line_cache.screen_line_starting_pos then
882 local past_end_pos = line_cache.screen_line_starting_pos[i+1]
883 local past_end_offset = Text.offset(line.data, past_end_pos)
884 screen_line = string.sub(line.data, start_offset, past_end_offset-1)
885 else
886 screen_line = string.sub(line.data, start_pos)
888 return State.font:getWidth(screen_line)
891 function Text.screen_line_index(screen_line_starting_pos, pos)
892 for i = #screen_line_starting_pos,1,-1 do
893 if screen_line_starting_pos[i] <= pos then
894 return i
899 -- convert x pixel coordinate to pos
900 -- oblivious to wrapping
901 -- result: 1 to len+1
902 function Text.nearest_cursor_pos(font, line, x, left)
903 if x < left then
904 return 1
906 local len = utf8.len(line)
907 local max_x = left+Text.x(font, line, len+1)
908 if x > max_x then
909 return len+1
911 local leftpos, rightpos = 1, len+1
912 --? print('-- nearest', x)
913 while true do
914 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
915 if leftpos == rightpos then
916 return leftpos
918 local curr = math.floor((leftpos+rightpos)/2)
919 local currxmin = left+Text.x(font, line, curr)
920 local currxmax = left+Text.x(font, line, curr+1)
921 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
922 if currxmin <= x and x < currxmax then
923 if x-currxmin < currxmax-x then
924 return curr
925 else
926 return curr+1
929 if leftpos >= rightpos-1 then
930 return rightpos
932 if currxmin > x then
933 rightpos = curr
934 else
935 leftpos = curr
938 assert(false, 'failed to map x pixel to pos')
941 -- return the nearest index of line (in utf8 code points) which lies entirely
942 -- within x pixels of the left margin
943 -- result: 0 to len+1
944 function Text.nearest_pos_less_than(font, line, x)
945 --? print('', '-- nearest_pos_less_than', line, x)
946 local len = utf8.len(line)
947 local max_x = Text.x_after(font, line, len)
948 if x > max_x then
949 return len+1
951 local left, right = 0, len+1
952 while true do
953 local curr = math.floor((left+right)/2)
954 local currxmin = Text.x_after(font, line, curr+1)
955 local currxmax = Text.x_after(font, line, curr+2)
956 --? print('', x, left, right, curr, currxmin, currxmax)
957 if currxmin <= x and x < currxmax then
958 return curr
960 if left >= right-1 then
961 return left
963 if currxmin > x then
964 right = curr
965 else
966 left = curr
969 assert(false, 'failed to map x pixel to pos')
972 function Text.x_after(font, s, pos)
973 local len = utf8.len(s)
974 local offset = Text.offset(s, math.min(pos+1, len+1))
975 local s_before = s:sub(1, offset-1)
976 --? print('^'..s_before..'$')
977 return font:getWidth(s_before)
980 function Text.x(font, s, pos)
981 local offset = Text.offset(s, pos)
982 local s_before = s:sub(1, offset-1)
983 return font:getWidth(s_before)
986 function Text.to2(State, loc1)
987 if State.lines[loc1.line].mode == 'drawing' then
988 return {line=loc1.line, screen_line=1, screen_pos=1}
990 local result = {line=loc1.line}
991 local line_cache = State.line_cache[loc1.line]
992 Text.populate_screen_line_starting_pos(State, loc1.line)
993 for i=#line_cache.screen_line_starting_pos,1,-1 do
994 local spos = line_cache.screen_line_starting_pos[i]
995 if spos <= loc1.pos then
996 result.screen_line = i
997 result.screen_pos = loc1.pos - spos + 1
998 break
1001 assert(result.screen_pos, 'failed to convert schema-1 coordinate to schema-2')
1002 return result
1005 function Text.to1(State, loc2)
1006 local result = {line=loc2.line, pos=loc2.screen_pos}
1007 if loc2.screen_line > 1 then
1008 result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
1010 return result
1013 function Text.eq1(a, b)
1014 return a.line == b.line and a.pos == b.pos
1017 function Text.lt1(a, b)
1018 if a.line < b.line then
1019 return true
1021 if a.line > b.line then
1022 return false
1024 return a.pos < b.pos
1027 function Text.le1(a, b)
1028 if a.line < b.line then
1029 return true
1031 if a.line > b.line then
1032 return false
1034 return a.pos <= b.pos
1037 function Text.eq2(a, b)
1038 return a.line == b.line and a.screen_line == b.screen_line and a.screen_pos == b.screen_pos
1041 function Text.offset(s, pos1)
1042 if pos1 == 1 then return 1 end
1043 local result = utf8.offset(s, pos1)
1044 if result == nil then
1045 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))
1047 return result
1050 function Text.previous_screen_line(State, loc2)
1051 if loc2.screen_line > 1 then
1052 return {line=loc2.line, screen_line=loc2.screen_line-1, screen_pos=1}
1053 elseif loc2.line == 1 then
1054 return loc2
1055 elseif State.lines[loc2.line-1].mode == 'drawing' then
1056 return {line=loc2.line-1, screen_line=1, screen_pos=1}
1057 else
1058 local l = State.lines[loc2.line-1]
1059 Text.populate_screen_line_starting_pos(State, loc2.line-1)
1060 return {line=loc2.line-1, screen_line=#State.line_cache[loc2.line-1].screen_line_starting_pos, screen_pos=1}
1064 function Text.next_screen_line(State, loc2)
1065 if State.lines[loc2.line].mode == 'drawing' then
1066 return {line=loc2.line+1, screen_line=1, screen_pos=1}
1068 Text.populate_screen_line_starting_pos(State, loc2.line)
1069 if loc2.screen_line >= #State.line_cache[loc2.line].screen_line_starting_pos then
1070 if loc2.line < #State.lines then
1071 return {line=loc2.line+1, screen_line=1, screen_pos=1}
1072 else
1073 return loc2
1075 else
1076 return {line=loc2.line, screen_line=loc2.screen_line+1, screen_pos=1}
1080 -- resize helper
1081 function Text.tweak_screen_top_and_cursor(State)
1082 if State.screen_top1.pos == 1 then return end
1083 Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
1084 local line = State.lines[State.screen_top1.line]
1085 local line_cache = State.line_cache[State.screen_top1.line]
1086 for i=2,#line_cache.screen_line_starting_pos do
1087 local pos = line_cache.screen_line_starting_pos[i]
1088 if pos == State.screen_top1.pos then
1089 break
1091 if pos > State.screen_top1.pos then
1092 -- make sure screen top is at start of a screen line
1093 local prev = line_cache.screen_line_starting_pos[i-1]
1094 if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
1095 State.screen_top1.pos = prev
1096 else
1097 State.screen_top1.pos = pos
1099 break
1102 -- make sure cursor is on screen
1103 local screen_bottom1 = Text.screen_bottom1(State)
1104 if Text.lt1(State.cursor1, State.screen_top1) then
1105 State.cursor1 = deepcopy(State.screen_top1)
1106 elseif State.cursor1.line >= screen_bottom1.line then
1107 if Text.cursor_out_of_screen(State) then
1108 State.cursor1 = Text.final_text_loc_on_screen(State)
1113 -- slightly expensive since it redraws the screen
1114 function Text.cursor_out_of_screen(State)
1115 edit.draw(State)
1116 return State.cursor_y == nil
1119 function Text.redraw_all(State)
1120 --? print('clearing line caches')
1121 -- Perform some early sanity checking here, in hopes that we correctly call
1122 -- this whenever we change editor state.
1123 if State.right <= State.left then
1124 assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left))
1127 State.line_cache = {}
1128 for i=1,#State.lines do
1129 State.line_cache[i] = {}
1133 function Text.clear_screen_line_cache(State, line_index)
1134 State.line_cache[line_index].screen_line_starting_pos = nil
1135 State.line_cache[line_index].link_offsets = nil
1138 function trim(s)
1139 return s:gsub('^%s+', ''):gsub('%s+$', '')
1142 function ltrim(s)
1143 return s:gsub('^%s+', '')
1146 function rtrim(s)
1147 return s:gsub('%s+$', '')
1150 function starts_with(s, prefix)
1151 if #s < #prefix then
1152 return false
1154 for i=1,#prefix do
1155 if s:sub(i,i) ~= prefix:sub(i,i) then
1156 return false
1159 return true
1162 function ends_with(s, suffix)
1163 if #s < #suffix then
1164 return false
1166 for i=0,#suffix-1 do
1167 if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
1168 return false
1171 return true