source_text.lua 44 KB

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