change section delimiters in log for OpenBSD
[view.love.git] / source_text.lua
blobfb5123af5b0940d14aa9e9548e82c0a0798b09b3
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)
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 -- everything works if bpos == 0, but is a little inefficient
132 pos = pos + bpos
133 local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
134 frag = string.sub(frag, boffset)
135 --? if bpos > 0 then
136 --? print('after chop:', frag)
137 --? end
138 frag_width = App.width(frag)
140 --? print('screen line:', pos)
141 table.insert(line_cache.screen_line_starting_pos, pos)
142 x = 0 -- new screen line
144 x = x + frag_width
145 pos = pos + utf8.len(frag)
149 function Text.populate_link_offsets(State, line_index)
150 local line = State.lines[line_index]
151 if line.mode ~= 'text' then return end
152 local line_cache = State.line_cache[line_index]
153 if line_cache.link_offsets then
154 return
156 line_cache.link_offsets = {}
157 local pos = 1
158 -- try to wrap at word boundaries
159 local s, e = 1, 0
160 while s <= #line.data do
161 s, e = line.data:find('%[%[%S+%]%]', s)
162 if s == nil then break end
163 local word = line.data:sub(s+2, e-2) -- strip out surrounding '[[..]]'
164 --? print('wikiword:', s, e, word)
165 table.insert(line_cache.link_offsets, {s, e, word})
166 s = e + 1
170 -- Intersect the filename between byte offsets s,e with the bounds of screen line i.
171 -- Return the left/right pixel coordinates of of the intersection,
172 -- or nil if it doesn't intersect with screen line i.
173 function Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
174 local spos = line_cache.screen_line_starting_pos[i]
175 local soff = Text.offset(line.data, spos)
176 if e < soff then
177 return
179 local eoff
180 if i < #line_cache.screen_line_starting_pos then
181 local epos = line_cache.screen_line_starting_pos[i+1]
182 eoff = Text.offset(line.data, epos)
183 if s > eoff then
184 return
187 local loff = math.max(s, soff)
188 local hoff
189 if eoff then
190 hoff = math.min(e, eoff)
191 else
192 hoff = e
194 --? print(s, e, soff, eoff, loff, hoff)
195 return App.width(line.data:sub(soff, loff-1)), App.width(line.data:sub(soff, hoff))
198 function Text.text_input(State, t)
199 if App.mouse_down(1) then return end
200 if App.ctrl_down() or App.alt_down() or App.cmd_down() then return end
201 local before = snapshot(State, State.cursor1.line)
202 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
203 Text.insert_at_cursor(State, t)
204 if State.cursor_y > App.screen.height - State.line_height then
205 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
206 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
208 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
211 function Text.insert_at_cursor(State, t)
212 assert(State.lines[State.cursor1.line].mode == 'text')
213 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
214 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)
215 Text.clear_screen_line_cache(State, State.cursor1.line)
216 State.cursor1.pos = State.cursor1.pos+1
219 -- Don't handle any keys here that would trigger text_input above.
220 function Text.keychord_press(State, chord)
221 --? print('chord', chord, State.selection1.line, State.selection1.pos)
222 --== shortcuts that mutate text
223 if chord == 'return' then
224 local before_line = State.cursor1.line
225 local before = snapshot(State, before_line)
226 Text.insert_return(State)
227 State.selection1 = {}
228 if State.cursor_y > App.screen.height - State.line_height then
229 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
231 schedule_save(State)
232 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
233 elseif chord == 'tab' then
234 local before = snapshot(State, State.cursor1.line)
235 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
236 Text.insert_at_cursor(State, '\t')
237 if State.cursor_y > App.screen.height - State.line_height then
238 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
239 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
240 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
242 schedule_save(State)
243 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
244 elseif chord == 'backspace' then
245 if State.selection1.line then
246 Text.delete_selection(State, State.left, State.right)
247 schedule_save(State)
248 return
250 local before
251 if State.cursor1.pos > 1 then
252 before = snapshot(State, State.cursor1.line)
253 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
254 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
255 if byte_start then
256 if byte_end then
257 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)
258 else
259 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
261 State.cursor1.pos = State.cursor1.pos-1
263 elseif State.cursor1.line > 1 then
264 before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
265 if State.lines[State.cursor1.line-1].mode == 'drawing' then
266 table.remove(State.lines, State.cursor1.line-1)
267 table.remove(State.line_cache, State.cursor1.line-1)
268 else
269 -- join lines
270 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
271 State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
272 table.remove(State.lines, State.cursor1.line)
273 table.remove(State.line_cache, State.cursor1.line)
275 State.cursor1.line = State.cursor1.line-1
277 if State.screen_top1.line > #State.lines then
278 Text.populate_screen_line_starting_pos(State, #State.lines)
279 local line_cache = State.line_cache[#State.line_cache]
280 State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
281 elseif Text.lt1(State.cursor1, State.screen_top1) then
282 State.screen_top1 = {
283 line=State.cursor1.line,
284 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
286 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
288 Text.clear_screen_line_cache(State, State.cursor1.line)
289 assert(Text.le1(State.screen_top1, State.cursor1))
290 schedule_save(State)
291 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
292 elseif chord == 'delete' then
293 if State.selection1.line then
294 Text.delete_selection(State, State.left, State.right)
295 schedule_save(State)
296 return
298 local before
299 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
300 before = snapshot(State, State.cursor1.line)
301 else
302 before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
304 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
305 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
306 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
307 if byte_start then
308 if byte_end then
309 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)
310 else
311 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
313 -- no change to State.cursor1.pos
315 elseif State.cursor1.line < #State.lines then
316 if State.lines[State.cursor1.line+1].mode == 'text' then
317 -- join lines
318 State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
320 table.remove(State.lines, State.cursor1.line+1)
321 table.remove(State.line_cache, State.cursor1.line+1)
323 Text.clear_screen_line_cache(State, State.cursor1.line)
324 schedule_save(State)
325 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
326 --== shortcuts that move the cursor
327 elseif chord == 'left' then
328 Text.left(State)
329 State.selection1 = {}
330 elseif chord == 'right' then
331 Text.right(State)
332 State.selection1 = {}
333 elseif chord == 'S-left' then
334 if State.selection1.line == nil then
335 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
337 Text.left(State)
338 elseif chord == 'S-right' then
339 if State.selection1.line == nil then
340 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
342 Text.right(State)
343 -- C- hotkeys reserved for drawings, so we'll use M-
344 elseif chord == 'M-left' then
345 Text.word_left(State)
346 State.selection1 = {}
347 elseif chord == 'M-right' then
348 Text.word_right(State)
349 State.selection1 = {}
350 elseif chord == 'M-S-left' then
351 if State.selection1.line == nil then
352 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
354 Text.word_left(State)
355 elseif chord == 'M-S-right' then
356 if State.selection1.line == nil then
357 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
359 Text.word_right(State)
360 elseif chord == 'home' then
361 Text.start_of_line(State)
362 State.selection1 = {}
363 elseif chord == 'end' then
364 Text.end_of_line(State)
365 State.selection1 = {}
366 elseif chord == 'S-home' then
367 if State.selection1.line == nil then
368 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
370 Text.start_of_line(State)
371 elseif chord == 'S-end' then
372 if State.selection1.line == nil then
373 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
375 Text.end_of_line(State)
376 elseif chord == 'up' then
377 Text.up(State)
378 State.selection1 = {}
379 elseif chord == 'down' then
380 Text.down(State)
381 State.selection1 = {}
382 elseif chord == 'S-up' then
383 if State.selection1.line == nil then
384 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
386 Text.up(State)
387 elseif chord == 'S-down' then
388 if State.selection1.line == nil then
389 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
391 Text.down(State)
392 elseif chord == 'pageup' then
393 Text.pageup(State)
394 State.selection1 = {}
395 elseif chord == 'pagedown' then
396 Text.pagedown(State)
397 State.selection1 = {}
398 elseif chord == 'S-pageup' then
399 if State.selection1.line == nil then
400 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
402 Text.pageup(State)
403 elseif chord == 'S-pagedown' then
404 if State.selection1.line == nil then
405 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
407 Text.pagedown(State)
411 function Text.insert_return(State)
412 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
413 table.insert(State.lines, State.cursor1.line+1, {mode='text', data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
414 table.insert(State.line_cache, State.cursor1.line+1, {})
415 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
416 Text.clear_screen_line_cache(State, State.cursor1.line)
417 State.cursor1 = {line=State.cursor1.line+1, pos=1}
420 function Text.pageup(State)
421 --? print('pageup')
422 -- duplicate some logic from love.draw
423 local top2 = Text.to2(State, State.screen_top1)
424 --? print(App.screen.height)
425 local y = App.screen.height - State.line_height
426 while y >= State.top do
427 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
428 if State.screen_top1.line == 1 and State.screen_top1.pos == 1 then break end
429 if State.lines[State.screen_top1.line].mode == 'text' then
430 y = y - State.line_height
431 elseif State.lines[State.screen_top1.line].mode == 'drawing' then
432 y = y - Drawing_padding_height - Drawing.pixels(State.lines[State.screen_top1.line].h, State.width)
434 top2 = Text.previous_screen_line(State, top2)
436 State.screen_top1 = Text.to1(State, top2)
437 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
438 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
439 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
440 --? print('pageup end')
443 function Text.pagedown(State)
444 --? print('pagedown')
445 State.screen_top1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
446 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
447 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
448 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
449 --? print('top now', State.screen_top1.line)
450 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
451 --? print('pagedown end')
454 function Text.up(State)
455 assert(State.lines[State.cursor1.line].mode == 'text')
456 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
457 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
458 if screen_line_starting_pos == 1 then
459 --? print('cursor is at first screen line of its line')
460 -- line is done; skip to previous text line
461 local new_cursor_line = State.cursor1.line
462 while new_cursor_line > 1 do
463 new_cursor_line = new_cursor_line-1
464 if State.lines[new_cursor_line].mode == 'text' then
465 --? print('found previous text line')
466 State.cursor1 = {line=new_cursor_line, pos=nil}
467 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
468 -- previous text line found, pick its final screen line
469 --? print('has multiple screen lines')
470 local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
471 --? print(#screen_line_starting_pos)
472 screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
473 local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
474 local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
475 State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
476 break
479 else
480 -- move up one screen line in current line
481 assert(screen_line_index > 1)
482 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
483 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
484 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
485 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
486 --? print('cursor pos is now '..tostring(State.cursor1.pos))
488 if Text.lt1(State.cursor1, State.screen_top1) then
489 State.screen_top1 = {
490 line=State.cursor1.line,
491 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
493 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
497 function Text.down(State)
498 assert(State.lines[State.cursor1.line].mode == 'text')
499 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
500 assert(State.cursor1.pos)
501 if Text.cursor_at_final_screen_line(State) then
502 -- line is done, skip to next text line
503 --? print('cursor at final screen line of its line')
504 local new_cursor_line = State.cursor1.line
505 while new_cursor_line < #State.lines do
506 new_cursor_line = new_cursor_line+1
507 if State.lines[new_cursor_line].mode == 'text' then
508 State.cursor1 = {
509 line = new_cursor_line,
510 pos = Text.nearest_cursor_pos(State.lines[new_cursor_line].data, State.cursor_x, State.left),
512 --? print(State.cursor1.pos)
513 break
516 if State.cursor1.line > State.screen_bottom1.line then
517 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
518 --? print('scroll up preserving cursor')
519 Text.snap_cursor_to_bottom_of_screen(State)
520 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
522 else
523 -- move down one screen line in current line
524 local scroll_down = Text.le1(State.screen_bottom1, State.cursor1)
525 --? print('cursor is NOT at final screen line of its line')
526 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
527 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
528 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
529 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
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(s, State.cursor_x, State.left) - 1
533 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
534 if scroll_down then
535 --? print('scroll up preserving cursor')
536 Text.snap_cursor_to_bottom_of_screen(State)
537 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
540 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
543 function Text.start_of_line(State)
544 State.cursor1.pos = 1
545 if Text.lt1(State.cursor1, State.screen_top1) then
546 State.screen_top1 = {line=State.cursor1.line, pos=State.cursor1.pos} -- copy
550 function Text.end_of_line(State)
551 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
552 if Text.cursor_out_of_screen(State) then
553 Text.snap_cursor_to_bottom_of_screen(State)
557 function Text.word_left(State)
558 -- skip some whitespace
559 while true do
560 if State.cursor1.pos == 1 then
561 break
563 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
564 break
566 Text.left(State)
568 -- skip some non-whitespace
569 while true do
570 Text.left(State)
571 if State.cursor1.pos == 1 then
572 break
574 assert(State.cursor1.pos > 1)
575 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
576 break
581 function Text.word_right(State)
582 -- skip some whitespace
583 while true do
584 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
585 break
587 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
588 break
590 Text.right_without_scroll(State)
592 while true do
593 Text.right_without_scroll(State)
594 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
595 break
597 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
598 break
601 if Text.cursor_out_of_screen(State) then
602 Text.snap_cursor_to_bottom_of_screen(State)
606 function Text.match(s, pos, pat)
607 local start_offset = Text.offset(s, pos)
608 assert(start_offset)
609 local end_offset = Text.offset(s, pos+1)
610 assert(end_offset > start_offset)
611 local curr = s:sub(start_offset, end_offset-1)
612 return curr:match(pat)
615 function Text.left(State)
616 assert(State.lines[State.cursor1.line].mode == 'text')
617 if State.cursor1.pos > 1 then
618 State.cursor1.pos = State.cursor1.pos-1
619 else
620 local new_cursor_line = State.cursor1.line
621 while new_cursor_line > 1 do
622 new_cursor_line = new_cursor_line-1
623 if State.lines[new_cursor_line].mode == 'text' then
624 State.cursor1 = {
625 line = new_cursor_line,
626 pos = utf8.len(State.lines[new_cursor_line].data) + 1,
628 break
632 if Text.lt1(State.cursor1, State.screen_top1) then
633 State.screen_top1 = {
634 line=State.cursor1.line,
635 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
637 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
641 function Text.right(State)
642 Text.right_without_scroll(State)
643 if Text.cursor_out_of_screen(State) then
644 Text.snap_cursor_to_bottom_of_screen(State)
648 function Text.right_without_scroll(State)
649 assert(State.lines[State.cursor1.line].mode == 'text')
650 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
651 State.cursor1.pos = State.cursor1.pos+1
652 else
653 local new_cursor_line = State.cursor1.line
654 while new_cursor_line <= #State.lines-1 do
655 new_cursor_line = new_cursor_line+1
656 if State.lines[new_cursor_line].mode == 'text' then
657 State.cursor1 = {line=new_cursor_line, pos=1}
658 break
664 -- result: pos, index of screen line
665 function Text.pos_at_start_of_screen_line(State, loc1)
666 Text.populate_screen_line_starting_pos(State, loc1.line)
667 local line_cache = State.line_cache[loc1.line]
668 for i=#line_cache.screen_line_starting_pos,1,-1 do
669 local spos = line_cache.screen_line_starting_pos[i]
670 if spos <= loc1.pos then
671 return spos,i
674 assert(false)
677 function Text.pos_at_end_of_screen_line(State, loc1)
678 Text.populate_screen_line_starting_pos(State, loc1.line)
679 local line_cache = State.line_cache[loc1.line]
680 local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
681 for i=#line_cache.screen_line_starting_pos,1,-1 do
682 local spos = line_cache.screen_line_starting_pos[i]
683 if spos <= loc1.pos then
684 return most_recent_final_pos
686 most_recent_final_pos = spos-1
688 assert(false)
691 function Text.cursor_at_final_screen_line(State)
692 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
693 local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos
694 --? print(screen_lines[#screen_lines], State.cursor1.pos)
695 return screen_lines[#screen_lines] <= State.cursor1.pos
698 function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
699 local y = State.top
700 while State.cursor1.line <= #State.lines do
701 if State.lines[State.cursor1.line].mode == 'text' then
702 break
704 --? print('cursor skips', State.cursor1.line)
705 y = y + Drawing_padding_height + Drawing.pixels(State.lines[State.cursor1.line].h, State.width)
706 State.cursor1.line = State.cursor1.line + 1
708 if State.cursor1.pos == nil then
709 State.cursor1.pos = 1
711 -- hack: insert a text line at bottom of file if necessary
712 if State.cursor1.line > #State.lines then
713 assert(State.cursor1.line == #State.lines+1)
714 table.insert(State.lines, {mode='text', data=''})
715 table.insert(State.line_cache, {})
717 --? print(y, App.screen.height, App.screen.height-State.line_height)
718 if y > App.screen.height - State.line_height then
719 --? print('scroll up')
720 Text.snap_cursor_to_bottom_of_screen(State)
724 -- should never modify State.cursor1
725 function Text.snap_cursor_to_bottom_of_screen(State)
726 --? print('to2:', State.cursor1.line, State.cursor1.pos)
727 local top2 = Text.to2(State, State.cursor1)
728 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
729 -- slide to start of screen line
730 top2.screen_pos = 1 -- start of screen line
731 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
732 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
733 local y = App.screen.height - State.line_height
734 -- duplicate some logic from love.draw
735 while true do
736 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
737 if top2.line == 1 and top2.screen_line == 1 then break end
738 if top2.screen_line > 1 or State.lines[top2.line-1].mode == 'text' then
739 local h = State.line_height
740 if y - h < State.top then
741 break
743 y = y - h
744 else
745 assert(top2.line > 1)
746 assert(State.lines[top2.line-1].mode == 'drawing')
747 -- We currently can't draw partial drawings, so either skip it entirely
748 -- or not at all.
749 local h = Drawing_padding_height + Drawing.pixels(State.lines[top2.line-1].h, State.width)
750 if y - h < State.top then
751 break
753 --? print('skipping drawing of height', h)
754 y = y - h
756 top2 = Text.previous_screen_line(State, top2)
758 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
759 State.screen_top1 = Text.to1(State, top2)
760 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
761 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
762 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
765 function Text.in_line(State, line_index, x,y)
766 local line = State.lines[line_index]
767 local line_cache = State.line_cache[line_index]
768 if line_cache.starty == nil then return false end -- outside current page
769 if y < line_cache.starty then return false end
770 Text.populate_screen_line_starting_pos(State, line_index)
771 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)
774 -- convert mx,my in pixels to schema-1 coordinates
775 function Text.to_pos_on_line(State, line_index, mx, my)
776 local line = State.lines[line_index]
777 local line_cache = State.line_cache[line_index]
778 assert(my >= line_cache.starty)
779 -- duplicate some logic from Text.draw
780 local y = line_cache.starty
781 local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
782 for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
783 local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
784 local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
785 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
786 local nexty = y + State.line_height
787 if my < nexty then
788 -- On all wrapped screen lines but the final one, clicks past end of
789 -- line position cursor on final character of screen line.
790 -- (The final screen line positions past end of screen line as always.)
791 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
792 --? print('past end of non-final line; return')
793 return line_cache.screen_line_starting_pos[screen_line_index+1]-1
795 local s = string.sub(line.data, screen_line_starting_byte_offset)
796 --? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
797 return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1
799 y = nexty
801 assert(false)
804 function Text.screen_line_width(State, line_index, i)
805 local line = State.lines[line_index]
806 local line_cache = State.line_cache[line_index]
807 local start_pos = line_cache.screen_line_starting_pos[i]
808 local start_offset = Text.offset(line.data, start_pos)
809 local screen_line
810 if i < #line_cache.screen_line_starting_pos then
811 local past_end_pos = line_cache.screen_line_starting_pos[i+1]
812 local past_end_offset = Text.offset(line.data, past_end_pos)
813 screen_line = string.sub(line.data, start_offset, past_end_offset-1)
814 else
815 screen_line = string.sub(line.data, start_pos)
817 return App.width(screen_line)
820 function Text.screen_line_index(screen_line_starting_pos, pos)
821 for i = #screen_line_starting_pos,1,-1 do
822 if screen_line_starting_pos[i] <= pos then
823 return i
828 -- convert x pixel coordinate to pos
829 -- oblivious to wrapping
830 -- result: 1 to len+1
831 function Text.nearest_cursor_pos(line, x, left)
832 if x < left then
833 return 1
835 local len = utf8.len(line)
836 local max_x = left+Text.x(line, len+1)
837 if x > max_x then
838 return len+1
840 local leftpos, rightpos = 1, len+1
841 --? print('-- nearest', x)
842 while true do
843 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
844 if leftpos == rightpos then
845 return leftpos
847 local curr = math.floor((leftpos+rightpos)/2)
848 local currxmin = left+Text.x(line, curr)
849 local currxmax = left+Text.x(line, curr+1)
850 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
851 if currxmin <= x and x < currxmax then
852 if x-currxmin < currxmax-x then
853 return curr
854 else
855 return curr+1
858 if leftpos >= rightpos-1 then
859 return rightpos
861 if currxmin > x then
862 rightpos = curr
863 else
864 leftpos = curr
867 assert(false)
870 -- return the nearest index of line (in utf8 code points) which lies entirely
871 -- within x pixels of the left margin
872 -- result: 0 to len+1
873 function Text.nearest_pos_less_than(line, x)
874 --? print('', '-- nearest_pos_less_than', line, x)
875 local len = utf8.len(line)
876 local max_x = Text.x_after(line, len)
877 if x > max_x then
878 return len+1
880 local left, right = 0, len+1
881 while true do
882 local curr = math.floor((left+right)/2)
883 local currxmin = Text.x_after(line, curr+1)
884 local currxmax = Text.x_after(line, curr+2)
885 --? print('', x, left, right, curr, currxmin, currxmax)
886 if currxmin <= x and x < currxmax then
887 return curr
889 if left >= right-1 then
890 return left
892 if currxmin > x then
893 right = curr
894 else
895 left = curr
898 assert(false)
901 function Text.x_after(s, pos)
902 local offset = Text.offset(s, math.min(pos+1, #s+1))
903 local s_before = s:sub(1, offset-1)
904 --? print('^'..s_before..'$')
905 return App.width(s_before)
908 function Text.x(s, pos)
909 local offset = Text.offset(s, pos)
910 local s_before = s:sub(1, offset-1)
911 return App.width(s_before)
914 function Text.to2(State, loc1)
915 if State.lines[loc1.line].mode == 'drawing' then
916 return {line=loc1.line, screen_line=1, screen_pos=1}
918 local result = {line=loc1.line}
919 local line_cache = State.line_cache[loc1.line]
920 Text.populate_screen_line_starting_pos(State, loc1.line)
921 for i=#line_cache.screen_line_starting_pos,1,-1 do
922 local spos = line_cache.screen_line_starting_pos[i]
923 if spos <= loc1.pos then
924 result.screen_line = i
925 result.screen_pos = loc1.pos - spos + 1
926 break
929 assert(result.screen_pos)
930 return result
933 function Text.to1(State, loc2)
934 local result = {line=loc2.line, pos=loc2.screen_pos}
935 if loc2.screen_line > 1 then
936 result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
938 return result
941 function Text.eq1(a, b)
942 return a.line == b.line and a.pos == b.pos
945 function Text.lt1(a, b)
946 if a.line < b.line then
947 return true
949 if a.line > b.line then
950 return false
952 return a.pos < b.pos
955 function Text.le1(a, b)
956 if a.line < b.line then
957 return true
959 if a.line > b.line then
960 return false
962 return a.pos <= b.pos
965 function Text.offset(s, pos1)
966 if pos1 == 1 then return 1 end
967 local result = utf8.offset(s, pos1)
968 if result == nil then
969 print(pos1, #s, s)
971 assert(result)
972 return result
975 function Text.previous_screen_line(State, loc2)
976 if loc2.screen_line > 1 then
977 return {line=loc2.line, screen_line=loc2.screen_line-1, screen_pos=1}
978 elseif loc2.line == 1 then
979 return loc2
980 elseif State.lines[loc2.line-1].mode == 'drawing' then
981 return {line=loc2.line-1, screen_line=1, screen_pos=1}
982 else
983 local l = State.lines[loc2.line-1]
984 Text.populate_screen_line_starting_pos(State, loc2.line-1)
985 return {line=loc2.line-1, screen_line=#State.line_cache[loc2.line-1].screen_line_starting_pos, screen_pos=1}
989 -- resize helper
990 function Text.tweak_screen_top_and_cursor(State)
991 if State.screen_top1.pos == 1 then return end
992 Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
993 local line = State.lines[State.screen_top1.line]
994 local line_cache = State.line_cache[State.screen_top1.line]
995 for i=2,#line_cache.screen_line_starting_pos do
996 local pos = line_cache.screen_line_starting_pos[i]
997 if pos == State.screen_top1.pos then
998 break
1000 if pos > State.screen_top1.pos then
1001 -- make sure screen top is at start of a screen line
1002 local prev = line_cache.screen_line_starting_pos[i-1]
1003 if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
1004 State.screen_top1.pos = prev
1005 else
1006 State.screen_top1.pos = pos
1008 break
1011 -- make sure cursor is on screen
1012 if Text.lt1(State.cursor1, State.screen_top1) then
1013 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
1014 elseif State.cursor1.line >= State.screen_bottom1.line then
1015 --? print('too low')
1016 if Text.cursor_out_of_screen(State) then
1017 --? print('tweak')
1018 State.cursor1 = {
1019 line=State.screen_bottom1.line,
1020 pos=Text.to_pos_on_line(State, State.screen_bottom1.line, State.right-5, App.screen.height-5),
1026 -- slightly expensive since it redraws the screen
1027 function Text.cursor_out_of_screen(State)
1028 edit.draw(State)
1029 return State.cursor_y == nil
1030 -- this approach is cheaper and almost works, except on the final screen
1031 -- where file ends above bottom of screen
1032 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
1033 --? local botline1 = {line=State.cursor1.line, pos=botpos}
1034 --? return Text.lt1(State.screen_bottom1, botline1)
1037 function Text.redraw_all(State)
1038 --? print('clearing fragments')
1039 State.line_cache = {}
1040 for i=1,#State.lines do
1041 State.line_cache[i] = {}
1045 function Text.clear_screen_line_cache(State, line_index)
1046 State.line_cache[line_index].screen_line_starting_pos = nil
1047 State.line_cache[line_index].link_offsets = nil
1050 function trim(s)
1051 return s:gsub('^%s+', ''):gsub('%s+$', '')
1054 function ltrim(s)
1055 return s:gsub('^%s+', '')
1058 function rtrim(s)
1059 return s:gsub('%s+$', '')
1062 function starts_with(s, prefix)
1063 if #s < #prefix then
1064 return false
1066 for i=1,#prefix do
1067 if s:sub(i,i) ~= prefix:sub(i,i) then
1068 return false
1071 return true
1074 function ends_with(s, suffix)
1075 if #s < #suffix then
1076 return false
1078 for i=0,#suffix-1 do
1079 if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
1080 return false
1083 return true