remove a duplicate print to screen
[view.love.git] / source_text.lua
blobd02d7d7e8e6a815d5dd86ced624666a74c7e2148
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)
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 initialize_color()
16 assert(#line_cache.screen_line_starting_pos >= 1)
17 for i=1,#line_cache.screen_line_starting_pos do
18 local pos = line_cache.screen_line_starting_pos[i]
19 if pos < startpos then
20 -- render nothing
21 --? print('skipping', screen_line)
22 else
23 final_screen_line_starting_pos = pos
24 local screen_line = Text.screen_line(line, line_cache, i)
25 --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
26 local frag_len = utf8.len(screen_line)
27 -- render any link decorations
28 for _,link_offsets in ipairs(line_cache.link_offsets) do
29 local s,e,filename = unpack(link_offsets)
30 local lo, hi = Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
31 if lo then
32 button(State, 'link', {x=State.left+lo, y=y, w=hi-lo, h=State.line_height, color={1,1,1},
33 icon = icon.hyperlink_decoration,
34 onpress1 = function()
35 if file_exists(filename) then
36 source.switch_to_file(filename)
37 end
38 end,
40 end
41 end
42 -- render any highlights
43 if State.selection1.line then
44 local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
45 Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
46 end
47 if not hide_cursor and line_index == State.cursor1.line then
48 -- render search highlight or cursor
49 if State.search_term then
50 if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos 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 hi = State.cursor1.pos+utf8.len(State.search_term)
55 Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, hi)
56 end
57 end
58 elseif Focus == 'edit' then
59 if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
60 Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
61 elseif pos + frag_len == State.cursor1.pos then
62 -- Show cursor at end of line.
63 -- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
64 -- It seems useful to see a cursor whether your eye is on the left or right margin.
65 Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
66 end
67 end
68 end
69 -- render colorized text
70 local x = State.left
71 for frag in screen_line:gmatch('%S*%s*') do
72 select_color(frag)
73 App.screen.print(frag, x,y)
74 x = x+App.width(frag)
75 end
76 y = y + State.line_height
77 if y >= App.screen.height then
78 break
79 end
80 end
81 end
82 return y, final_screen_line_starting_pos
83 end
85 function Text.screen_line(line, line_cache, i)
86 local pos = line_cache.screen_line_starting_pos[i]
87 local offset = Text.offset(line.data, pos)
88 if i >= #line_cache.screen_line_starting_pos then
89 return line.data:sub(offset)
90 end
91 local endpos = line_cache.screen_line_starting_pos[i+1]-1
92 local end_offset = Text.offset(line.data, endpos)
93 return line.data:sub(offset, end_offset)
94 end
96 function Text.draw_cursor(State, x, y)
97 -- blink every 0.5s
98 if math.floor(Cursor_time*2)%2 == 0 then
99 App.color(Cursor_color)
100 love.graphics.rectangle('fill', x,y, 3,State.line_height)
102 State.cursor_x = x
103 State.cursor_y = y+State.line_height
106 function Text.populate_screen_line_starting_pos(State, line_index)
107 local line = State.lines[line_index]
108 if line.mode ~= 'text' then return end
109 local line_cache = State.line_cache[line_index]
110 if line_cache.screen_line_starting_pos then
111 return
113 line_cache.screen_line_starting_pos = {1}
114 local x = 0
115 local pos = 1
116 -- try to wrap at word boundaries
117 for frag in line.data:gmatch('%S*%s*') do
118 local frag_width = App.width(frag)
119 --? print('-- frag:', frag, pos, x, frag_width, State.width)
120 while x + frag_width > State.width do
121 --? print('frag:', frag, pos, x, frag_width, State.width)
122 if x < 0.8 * State.width then
123 -- long word; chop it at some letter
124 -- We're not going to reimplement TeX here.
125 local bpos = Text.nearest_pos_less_than(frag, State.width - x)
126 -- everything works if bpos == 0, but is a little inefficient
127 pos = pos + bpos
128 local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
129 frag = string.sub(frag, boffset)
130 --? if bpos > 0 then
131 --? print('after chop:', frag)
132 --? end
133 frag_width = App.width(frag)
135 --? print('screen line:', pos)
136 table.insert(line_cache.screen_line_starting_pos, pos)
137 x = 0 -- new screen line
139 x = x + frag_width
140 pos = pos + utf8.len(frag)
144 function Text.populate_link_offsets(State, line_index)
145 local line = State.lines[line_index]
146 if line.mode ~= 'text' then return end
147 local line_cache = State.line_cache[line_index]
148 if line_cache.link_offsets then
149 return
151 line_cache.link_offsets = {}
152 local pos = 1
153 -- try to wrap at word boundaries
154 local s, e = 1, 0
155 while s <= #line.data do
156 s, e = line.data:find('%[%[%S+%]%]', s)
157 if s == nil then break end
158 local word = line.data:sub(s+2, e-2) -- strip out surrounding '[[..]]'
159 --? print('wikiword:', s, e, word)
160 table.insert(line_cache.link_offsets, {s, e, word})
161 s = e + 1
165 -- Intersect the filename between byte offsets s,e with the bounds of screen line i.
166 -- Return the left/right pixel coordinates of of the intersection,
167 -- or nil if it doesn't intersect with screen line i.
168 function Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
169 local spos = line_cache.screen_line_starting_pos[i]
170 local soff = Text.offset(line.data, spos)
171 if e < soff then
172 return
174 local eoff
175 if i < #line_cache.screen_line_starting_pos then
176 local epos = line_cache.screen_line_starting_pos[i+1]
177 eoff = Text.offset(line.data, epos)
178 if s > eoff then
179 return
182 local loff = math.max(s, soff)
183 local hoff
184 if eoff then
185 hoff = math.min(e, eoff)
186 else
187 hoff = e
189 --? print(s, e, soff, eoff, loff, hoff)
190 return App.width(line.data:sub(soff, loff-1)), App.width(line.data:sub(soff, hoff))
193 function Text.text_input(State, t)
194 if App.mouse_down(1) then return end
195 if App.ctrl_down() or App.alt_down() or App.cmd_down() then return end
196 local before = snapshot(State, State.cursor1.line)
197 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
198 Text.insert_at_cursor(State, t)
199 if State.cursor_y > App.screen.height - State.line_height then
200 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
201 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
203 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
206 function Text.insert_at_cursor(State, t)
207 assert(State.lines[State.cursor1.line].mode == 'text')
208 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
209 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)
210 Text.clear_screen_line_cache(State, State.cursor1.line)
211 State.cursor1.pos = State.cursor1.pos+1
214 -- Don't handle any keys here that would trigger text_input above.
215 function Text.keychord_press(State, chord)
216 --? print('chord', chord, State.selection1.line, State.selection1.pos)
217 --== shortcuts that mutate text
218 if chord == 'return' then
219 local before_line = State.cursor1.line
220 local before = snapshot(State, before_line)
221 Text.insert_return(State)
222 State.selection1 = {}
223 if State.cursor_y > App.screen.height - State.line_height then
224 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
226 schedule_save(State)
227 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
228 elseif chord == 'tab' then
229 local before = snapshot(State, State.cursor1.line)
230 --? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
231 Text.insert_at_cursor(State, '\t')
232 if State.cursor_y > App.screen.height - State.line_height then
233 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
234 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
235 --? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
237 schedule_save(State)
238 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
239 elseif chord == 'backspace' then
240 if State.selection1.line then
241 Text.delete_selection(State, State.left, State.right)
242 schedule_save(State)
243 return
245 local before
246 if State.cursor1.pos > 1 then
247 before = snapshot(State, State.cursor1.line)
248 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
249 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
250 if byte_start then
251 if byte_end then
252 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)
253 else
254 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
256 State.cursor1.pos = State.cursor1.pos-1
258 elseif State.cursor1.line > 1 then
259 before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
260 if State.lines[State.cursor1.line-1].mode == 'drawing' then
261 table.remove(State.lines, State.cursor1.line-1)
262 table.remove(State.line_cache, State.cursor1.line-1)
263 else
264 -- join lines
265 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
266 State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
267 table.remove(State.lines, State.cursor1.line)
268 table.remove(State.line_cache, State.cursor1.line)
270 State.cursor1.line = State.cursor1.line-1
272 if State.screen_top1.line > #State.lines then
273 Text.populate_screen_line_starting_pos(State, #State.lines)
274 local line_cache = State.line_cache[#State.line_cache]
275 State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
276 elseif Text.lt1(State.cursor1, State.screen_top1) then
277 State.screen_top1 = {
278 line=State.cursor1.line,
279 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
281 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
283 Text.clear_screen_line_cache(State, State.cursor1.line)
284 assert(Text.le1(State.screen_top1, State.cursor1))
285 schedule_save(State)
286 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
287 elseif chord == 'delete' then
288 if State.selection1.line then
289 Text.delete_selection(State, State.left, State.right)
290 schedule_save(State)
291 return
293 local before
294 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
295 before = snapshot(State, State.cursor1.line)
296 else
297 before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
299 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
300 local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
301 local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
302 if byte_start then
303 if byte_end then
304 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)
305 else
306 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
308 -- no change to State.cursor1.pos
310 elseif State.cursor1.line < #State.lines then
311 if State.lines[State.cursor1.line+1].mode == 'text' then
312 -- join lines
313 State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
315 table.remove(State.lines, State.cursor1.line+1)
316 table.remove(State.line_cache, State.cursor1.line+1)
318 Text.clear_screen_line_cache(State, State.cursor1.line)
319 schedule_save(State)
320 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
321 --== shortcuts that move the cursor
322 elseif chord == 'left' then
323 Text.left(State)
324 State.selection1 = {}
325 elseif chord == 'right' then
326 Text.right(State)
327 State.selection1 = {}
328 elseif chord == 'S-left' then
329 if State.selection1.line == nil then
330 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
332 Text.left(State)
333 elseif chord == 'S-right' then
334 if State.selection1.line == nil then
335 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
337 Text.right(State)
338 -- C- hotkeys reserved for drawings, so we'll use M-
339 elseif chord == 'M-left' then
340 Text.word_left(State)
341 State.selection1 = {}
342 elseif chord == 'M-right' then
343 Text.word_right(State)
344 State.selection1 = {}
345 elseif chord == 'M-S-left' then
346 if State.selection1.line == nil then
347 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
349 Text.word_left(State)
350 elseif chord == 'M-S-right' then
351 if State.selection1.line == nil then
352 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
354 Text.word_right(State)
355 elseif chord == 'home' then
356 Text.start_of_line(State)
357 State.selection1 = {}
358 elseif chord == 'end' then
359 Text.end_of_line(State)
360 State.selection1 = {}
361 elseif chord == 'S-home' then
362 if State.selection1.line == nil then
363 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
365 Text.start_of_line(State)
366 elseif chord == 'S-end' then
367 if State.selection1.line == nil then
368 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
370 Text.end_of_line(State)
371 elseif chord == 'up' then
372 Text.up(State)
373 State.selection1 = {}
374 elseif chord == 'down' then
375 Text.down(State)
376 State.selection1 = {}
377 elseif chord == 'S-up' then
378 if State.selection1.line == nil then
379 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
381 Text.up(State)
382 elseif chord == 'S-down' then
383 if State.selection1.line == nil then
384 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
386 Text.down(State)
387 elseif chord == 'pageup' then
388 Text.pageup(State)
389 State.selection1 = {}
390 elseif chord == 'pagedown' then
391 Text.pagedown(State)
392 State.selection1 = {}
393 elseif chord == 'S-pageup' then
394 if State.selection1.line == nil then
395 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
397 Text.pageup(State)
398 elseif chord == 'S-pagedown' then
399 if State.selection1.line == nil then
400 State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
402 Text.pagedown(State)
406 function Text.insert_return(State)
407 local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
408 table.insert(State.lines, State.cursor1.line+1, {mode='text', data=string.sub(State.lines[State.cursor1.line].data, byte_offset)})
409 table.insert(State.line_cache, State.cursor1.line+1, {})
410 State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)
411 Text.clear_screen_line_cache(State, State.cursor1.line)
412 State.cursor1 = {line=State.cursor1.line+1, pos=1}
415 function Text.pageup(State)
416 --? print('pageup')
417 -- duplicate some logic from love.draw
418 local top2 = Text.to2(State, State.screen_top1)
419 --? print(App.screen.height)
420 local y = App.screen.height - State.line_height
421 while y >= State.top do
422 --? print(y, top2.line, top2.screen_line, top2.screen_pos)
423 if State.screen_top1.line == 1 and State.screen_top1.pos == 1 then break end
424 if State.lines[State.screen_top1.line].mode == 'text' then
425 y = y - State.line_height
426 elseif State.lines[State.screen_top1.line].mode == 'drawing' then
427 y = y - Drawing_padding_height - Drawing.pixels(State.lines[State.screen_top1.line].h, State.width)
429 top2 = Text.previous_screen_line(State, top2)
431 State.screen_top1 = Text.to1(State, top2)
432 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
433 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
434 --? print(State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
435 --? print('pageup end')
438 function Text.pagedown(State)
439 --? print('pagedown')
440 State.screen_top1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
441 --? print('setting top to', State.screen_top1.line, State.screen_top1.pos)
442 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
443 Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
444 --? print('top now', State.screen_top1.line)
445 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
446 --? print('pagedown end')
449 function Text.up(State)
450 assert(State.lines[State.cursor1.line].mode == 'text')
451 --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
452 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
453 if screen_line_starting_pos == 1 then
454 --? print('cursor is at first screen line of its line')
455 -- line is done; skip to previous text line
456 local new_cursor_line = State.cursor1.line
457 while new_cursor_line > 1 do
458 new_cursor_line = new_cursor_line-1
459 if State.lines[new_cursor_line].mode == 'text' then
460 --? print('found previous text line')
461 State.cursor1 = {line=new_cursor_line, pos=nil}
462 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
463 -- previous text line found, pick its final screen line
464 --? print('has multiple screen lines')
465 local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
466 --? print(#screen_line_starting_pos)
467 screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
468 local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
469 local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
470 State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
471 break
474 else
475 -- move up one screen line in current line
476 assert(screen_line_index > 1)
477 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
478 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
479 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
480 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
481 --? print('cursor pos is now '..tostring(State.cursor1.pos))
483 if Text.lt1(State.cursor1, State.screen_top1) then
484 State.screen_top1 = {
485 line=State.cursor1.line,
486 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
488 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
492 function Text.down(State)
493 assert(State.lines[State.cursor1.line].mode == 'text')
494 --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
495 assert(State.cursor1.pos)
496 if Text.cursor_at_final_screen_line(State) then
497 -- line is done, skip to next text line
498 --? print('cursor at final screen line of its line')
499 local new_cursor_line = State.cursor1.line
500 while new_cursor_line < #State.lines do
501 new_cursor_line = new_cursor_line+1
502 if State.lines[new_cursor_line].mode == 'text' then
503 State.cursor1 = {
504 line = new_cursor_line,
505 pos = Text.nearest_cursor_pos(State.lines[new_cursor_line].data, State.cursor_x, State.left),
507 --? print(State.cursor1.pos)
508 break
511 if State.cursor1.line > State.screen_bottom1.line then
512 --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
513 --? print('scroll up preserving cursor')
514 Text.snap_cursor_to_bottom_of_screen(State)
515 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
517 else
518 -- move down one screen line in current line
519 local scroll_down = Text.le1(State.screen_bottom1, State.cursor1)
520 --? print('cursor is NOT at final screen line of its line')
521 local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
522 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
523 local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
524 --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
525 local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
526 local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
527 State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
528 --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
529 if scroll_down then
530 --? print('scroll up preserving cursor')
531 Text.snap_cursor_to_bottom_of_screen(State)
532 --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
535 --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
538 function Text.start_of_line(State)
539 State.cursor1.pos = 1
540 if Text.lt1(State.cursor1, State.screen_top1) then
541 State.screen_top1 = {line=State.cursor1.line, pos=State.cursor1.pos} -- copy
545 function Text.end_of_line(State)
546 State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
547 if Text.cursor_out_of_screen(State) then
548 Text.snap_cursor_to_bottom_of_screen(State)
552 function Text.word_left(State)
553 -- skip some whitespace
554 while true do
555 if State.cursor1.pos == 1 then
556 break
558 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
559 break
561 Text.left(State)
563 -- skip some non-whitespace
564 while true do
565 Text.left(State)
566 if State.cursor1.pos == 1 then
567 break
569 assert(State.cursor1.pos > 1)
570 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
571 break
576 function Text.word_right(State)
577 -- skip some whitespace
578 while true do
579 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
580 break
582 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
583 break
585 Text.right_without_scroll(State)
587 while true do
588 Text.right_without_scroll(State)
589 if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
590 break
592 if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
593 break
596 if Text.cursor_out_of_screen(State) then
597 Text.snap_cursor_to_bottom_of_screen(State)
601 function Text.match(s, pos, pat)
602 local start_offset = Text.offset(s, pos)
603 assert(start_offset)
604 local end_offset = Text.offset(s, pos+1)
605 assert(end_offset > start_offset)
606 local curr = s:sub(start_offset, end_offset-1)
607 return curr:match(pat)
610 function Text.left(State)
611 assert(State.lines[State.cursor1.line].mode == 'text')
612 if State.cursor1.pos > 1 then
613 State.cursor1.pos = State.cursor1.pos-1
614 else
615 local new_cursor_line = State.cursor1.line
616 while new_cursor_line > 1 do
617 new_cursor_line = new_cursor_line-1
618 if State.lines[new_cursor_line].mode == 'text' then
619 State.cursor1 = {
620 line = new_cursor_line,
621 pos = utf8.len(State.lines[new_cursor_line].data) + 1,
623 break
627 if Text.lt1(State.cursor1, State.screen_top1) then
628 State.screen_top1 = {
629 line=State.cursor1.line,
630 pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
632 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
636 function Text.right(State)
637 Text.right_without_scroll(State)
638 if Text.cursor_out_of_screen(State) then
639 Text.snap_cursor_to_bottom_of_screen(State)
643 function Text.right_without_scroll(State)
644 assert(State.lines[State.cursor1.line].mode == 'text')
645 if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
646 State.cursor1.pos = State.cursor1.pos+1
647 else
648 local new_cursor_line = State.cursor1.line
649 while new_cursor_line <= #State.lines-1 do
650 new_cursor_line = new_cursor_line+1
651 if State.lines[new_cursor_line].mode == 'text' then
652 State.cursor1 = {line=new_cursor_line, pos=1}
653 break
659 -- result: pos, index of screen line
660 function Text.pos_at_start_of_screen_line(State, loc1)
661 Text.populate_screen_line_starting_pos(State, loc1.line)
662 local line_cache = State.line_cache[loc1.line]
663 for i=#line_cache.screen_line_starting_pos,1,-1 do
664 local spos = line_cache.screen_line_starting_pos[i]
665 if spos <= loc1.pos then
666 return spos,i
669 assert(false)
672 function Text.pos_at_end_of_screen_line(State, loc1)
673 Text.populate_screen_line_starting_pos(State, loc1.line)
674 local line_cache = State.line_cache[loc1.line]
675 local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
676 for i=#line_cache.screen_line_starting_pos,1,-1 do
677 local spos = line_cache.screen_line_starting_pos[i]
678 if spos <= loc1.pos then
679 return most_recent_final_pos
681 most_recent_final_pos = spos-1
683 assert(false)
686 function Text.cursor_at_final_screen_line(State)
687 Text.populate_screen_line_starting_pos(State, State.cursor1.line)
688 local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos
689 --? print(screen_lines[#screen_lines], State.cursor1.pos)
690 return screen_lines[#screen_lines] <= State.cursor1.pos
693 function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
694 local y = State.top
695 while State.cursor1.line <= #State.lines do
696 if State.lines[State.cursor1.line].mode == 'text' then
697 break
699 --? print('cursor skips', State.cursor1.line)
700 y = y + Drawing_padding_height + Drawing.pixels(State.lines[State.cursor1.line].h, State.width)
701 State.cursor1.line = State.cursor1.line + 1
703 if State.cursor1.pos == nil then
704 State.cursor1.pos = 1
706 -- hack: insert a text line at bottom of file if necessary
707 if State.cursor1.line > #State.lines then
708 assert(State.cursor1.line == #State.lines+1)
709 table.insert(State.lines, {mode='text', data=''})
710 table.insert(State.line_cache, {})
712 --? print(y, App.screen.height, App.screen.height-State.line_height)
713 if y > App.screen.height - State.line_height then
714 --? print('scroll up')
715 Text.snap_cursor_to_bottom_of_screen(State)
719 -- should never modify State.cursor1
720 function Text.snap_cursor_to_bottom_of_screen(State)
721 --? print('to2:', State.cursor1.line, State.cursor1.pos)
722 local top2 = Text.to2(State, State.cursor1)
723 --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
724 -- slide to start of screen line
725 top2.screen_pos = 1 -- start of screen line
726 --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
727 --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
728 local y = App.screen.height - State.line_height
729 -- duplicate some logic from love.draw
730 while true do
731 --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
732 if top2.line == 1 and top2.screen_line == 1 then break end
733 if top2.screen_line > 1 or State.lines[top2.line-1].mode == 'text' then
734 local h = State.line_height
735 if y - h < State.top then
736 break
738 y = y - h
739 else
740 assert(top2.line > 1)
741 assert(State.lines[top2.line-1].mode == 'drawing')
742 -- We currently can't draw partial drawings, so either skip it entirely
743 -- or not at all.
744 local h = Drawing_padding_height + Drawing.pixels(State.lines[top2.line-1].h, State.width)
745 if y - h < State.top then
746 break
748 --? print('skipping drawing of height', h)
749 y = y - h
751 top2 = Text.previous_screen_line(State, top2)
753 --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
754 State.screen_top1 = Text.to1(State, top2)
755 --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
756 --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
757 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
760 function Text.in_line(State, line_index, x,y)
761 local line = State.lines[line_index]
762 local line_cache = State.line_cache[line_index]
763 if line_cache.starty == nil then return false end -- outside current page
764 if y < line_cache.starty then return false end
765 Text.populate_screen_line_starting_pos(State, line_index)
766 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)
769 -- convert mx,my in pixels to schema-1 coordinates
770 function Text.to_pos_on_line(State, line_index, mx, my)
771 local line = State.lines[line_index]
772 local line_cache = State.line_cache[line_index]
773 assert(my >= line_cache.starty)
774 -- duplicate some logic from Text.draw
775 local y = line_cache.starty
776 local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
777 for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
778 local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
779 local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
780 --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
781 local nexty = y + State.line_height
782 if my < nexty then
783 -- On all wrapped screen lines but the final one, clicks past end of
784 -- line position cursor on final character of screen line.
785 -- (The final screen line positions past end of screen line as always.)
786 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
787 --? print('past end of non-final line; return')
788 return line_cache.screen_line_starting_pos[screen_line_index+1]-1
790 local s = string.sub(line.data, screen_line_starting_byte_offset)
791 --? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
792 return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1
794 y = nexty
796 assert(false)
799 function Text.screen_line_width(State, line_index, i)
800 local line = State.lines[line_index]
801 local line_cache = State.line_cache[line_index]
802 local start_pos = line_cache.screen_line_starting_pos[i]
803 local start_offset = Text.offset(line.data, start_pos)
804 local screen_line
805 if i < #line_cache.screen_line_starting_pos then
806 local past_end_pos = line_cache.screen_line_starting_pos[i+1]
807 local past_end_offset = Text.offset(line.data, past_end_pos)
808 screen_line = string.sub(line.data, start_offset, past_end_offset-1)
809 else
810 screen_line = string.sub(line.data, start_pos)
812 return App.width(screen_line)
815 function Text.screen_line_index(screen_line_starting_pos, pos)
816 for i = #screen_line_starting_pos,1,-1 do
817 if screen_line_starting_pos[i] <= pos then
818 return i
823 -- convert x pixel coordinate to pos
824 -- oblivious to wrapping
825 -- result: 1 to len+1
826 function Text.nearest_cursor_pos(line, x, left)
827 if x < left then
828 return 1
830 local len = utf8.len(line)
831 local max_x = left+Text.x(line, len+1)
832 if x > max_x then
833 return len+1
835 local leftpos, rightpos = 1, len+1
836 --? print('-- nearest', x)
837 while true do
838 --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
839 if leftpos == rightpos then
840 return leftpos
842 local curr = math.floor((leftpos+rightpos)/2)
843 local currxmin = left+Text.x(line, curr)
844 local currxmax = left+Text.x(line, curr+1)
845 --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
846 if currxmin <= x and x < currxmax then
847 if x-currxmin < currxmax-x then
848 return curr
849 else
850 return curr+1
853 if leftpos >= rightpos-1 then
854 return rightpos
856 if currxmin > x then
857 rightpos = curr
858 else
859 leftpos = curr
862 assert(false)
865 -- return the nearest index of line (in utf8 code points) which lies entirely
866 -- within x pixels of the left margin
867 -- result: 0 to len+1
868 function Text.nearest_pos_less_than(line, x)
869 --? print('', '-- nearest_pos_less_than', line, x)
870 local len = utf8.len(line)
871 local max_x = Text.x_after(line, len)
872 if x > max_x then
873 return len+1
875 local left, right = 0, len+1
876 while true do
877 local curr = math.floor((left+right)/2)
878 local currxmin = Text.x_after(line, curr+1)
879 local currxmax = Text.x_after(line, curr+2)
880 --? print('', x, left, right, curr, currxmin, currxmax)
881 if currxmin <= x and x < currxmax then
882 return curr
884 if left >= right-1 then
885 return left
887 if currxmin > x then
888 right = curr
889 else
890 left = curr
893 assert(false)
896 function Text.x_after(s, pos)
897 local offset = Text.offset(s, math.min(pos+1, #s+1))
898 local s_before = s:sub(1, offset-1)
899 --? print('^'..s_before..'$')
900 return App.width(s_before)
903 function Text.x(s, pos)
904 local offset = Text.offset(s, pos)
905 local s_before = s:sub(1, offset-1)
906 return App.width(s_before)
909 function Text.to2(State, loc1)
910 if State.lines[loc1.line].mode == 'drawing' then
911 return {line=loc1.line, screen_line=1, screen_pos=1}
913 local result = {line=loc1.line}
914 local line_cache = State.line_cache[loc1.line]
915 Text.populate_screen_line_starting_pos(State, loc1.line)
916 for i=#line_cache.screen_line_starting_pos,1,-1 do
917 local spos = line_cache.screen_line_starting_pos[i]
918 if spos <= loc1.pos then
919 result.screen_line = i
920 result.screen_pos = loc1.pos - spos + 1
921 break
924 assert(result.screen_pos)
925 return result
928 function Text.to1(State, loc2)
929 local result = {line=loc2.line, pos=loc2.screen_pos}
930 if loc2.screen_line > 1 then
931 result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
933 return result
936 function Text.eq1(a, b)
937 return a.line == b.line and a.pos == b.pos
940 function Text.lt1(a, b)
941 if a.line < b.line then
942 return true
944 if a.line > b.line then
945 return false
947 return a.pos < b.pos
950 function Text.le1(a, b)
951 if a.line < b.line then
952 return true
954 if a.line > b.line then
955 return false
957 return a.pos <= b.pos
960 function Text.offset(s, pos1)
961 if pos1 == 1 then return 1 end
962 local result = utf8.offset(s, pos1)
963 if result == nil then
964 print(pos1, #s, s)
966 assert(result)
967 return result
970 function Text.previous_screen_line(State, loc2)
971 if loc2.screen_line > 1 then
972 return {line=loc2.line, screen_line=loc2.screen_line-1, screen_pos=1}
973 elseif loc2.line == 1 then
974 return loc2
975 elseif State.lines[loc2.line-1].mode == 'drawing' then
976 return {line=loc2.line-1, screen_line=1, screen_pos=1}
977 else
978 local l = State.lines[loc2.line-1]
979 Text.populate_screen_line_starting_pos(State, loc2.line-1)
980 return {line=loc2.line-1, screen_line=#State.line_cache[loc2.line-1].screen_line_starting_pos, screen_pos=1}
984 -- resize helper
985 function Text.tweak_screen_top_and_cursor(State)
986 if State.screen_top1.pos == 1 then return end
987 Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
988 local line = State.lines[State.screen_top1.line]
989 local line_cache = State.line_cache[State.screen_top1.line]
990 for i=2,#line_cache.screen_line_starting_pos do
991 local pos = line_cache.screen_line_starting_pos[i]
992 if pos == State.screen_top1.pos then
993 break
995 if pos > State.screen_top1.pos then
996 -- make sure screen top is at start of a screen line
997 local prev = line_cache.screen_line_starting_pos[i-1]
998 if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
999 State.screen_top1.pos = prev
1000 else
1001 State.screen_top1.pos = pos
1003 break
1006 -- make sure cursor is on screen
1007 if Text.lt1(State.cursor1, State.screen_top1) then
1008 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
1009 elseif State.cursor1.line >= State.screen_bottom1.line then
1010 --? print('too low')
1011 if Text.cursor_out_of_screen(State) then
1012 --? print('tweak')
1013 State.cursor1 = {
1014 line=State.screen_bottom1.line,
1015 pos=Text.to_pos_on_line(State, State.screen_bottom1.line, State.right-5, App.screen.height-5),
1021 -- slightly expensive since it redraws the screen
1022 function Text.cursor_out_of_screen(State)
1023 edit.draw(State)
1024 return State.cursor_y == nil
1025 -- this approach is cheaper and almost works, except on the final screen
1026 -- where file ends above bottom of screen
1027 --? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
1028 --? local botline1 = {line=State.cursor1.line, pos=botpos}
1029 --? return Text.lt1(State.screen_bottom1, botline1)
1032 function Text.redraw_all(State)
1033 --? print('clearing fragments')
1034 State.line_cache = {}
1035 for i=1,#State.lines do
1036 State.line_cache[i] = {}
1040 function Text.clear_screen_line_cache(State, line_index)
1041 State.line_cache[line_index].screen_line_starting_pos = nil
1042 State.line_cache[line_index].link_offsets = nil
1045 function trim(s)
1046 return s:gsub('^%s+', ''):gsub('%s+$', '')
1049 function ltrim(s)
1050 return s:gsub('^%s+', '')
1053 function rtrim(s)
1054 return s:gsub('%s+$', '')
1057 function starts_with(s, prefix)
1058 if #s < #prefix then
1059 return false
1061 for i=1,#prefix do
1062 if s:sub(i,i) ~= prefix:sub(i,i) then
1063 return false
1066 return true
1069 function ends_with(s, suffix)
1070 if #s < #suffix then
1071 return false
1073 for i=0,#suffix-1 do
1074 if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
1075 return false
1078 return true