text.lua 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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)
  6. --? print('text.draw', line_index, y)
  7. local line = State.lines[line_index]
  8. local line_cache = State.line_cache[line_index]
  9. line_cache.startpos = startpos
  10. -- wrap long lines
  11. Text.populate_screen_line_starting_pos(State, line_index)
  12. assert(#line_cache.screen_line_starting_pos >= 1, 'line cache missing screen line info')
  13. for i=1,#line_cache.screen_line_starting_pos do
  14. local pos = line_cache.screen_line_starting_pos[i]
  15. if pos < startpos then
  16. -- render nothing
  17. else
  18. local screen_line = Text.screen_line(line, line_cache, i)
  19. --? print('text.draw:', screen_line, 'at', line_index,pos, 'after', x,y)
  20. local frag_len = utf8.len(screen_line)
  21. -- render any highlights
  22. if State.selection1.line then
  23. local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
  24. Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
  25. end
  26. if line_index == State.cursor1.line then
  27. -- render search highlight or cursor
  28. if State.search_term then
  29. local data = State.lines[State.cursor1.line].data
  30. local cursor_offset = Text.offset(data, State.cursor1.pos)
  31. if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
  32. local save_selection = State.selection1
  33. State.selection1 = {line=line_index, pos=State.cursor1.pos+utf8.len(State.search_term)}
  34. local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
  35. Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
  36. State.selection1 = save_selection
  37. end
  38. else
  39. if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
  40. Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
  41. elseif pos + frag_len == State.cursor1.pos then
  42. -- Keep pretend cursor_x/cursor_y in sync with upstream.
  43. Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
  44. end
  45. end
  46. end
  47. -- render screen line
  48. App.color(Text_color)
  49. App.screen.print(screen_line, State.left,y)
  50. y = y + State.line_height
  51. if y >= App.screen.height then
  52. break
  53. end
  54. end
  55. end
  56. return y
  57. end
  58. function Text.screen_line(line, line_cache, i)
  59. local pos = line_cache.screen_line_starting_pos[i]
  60. local offset = Text.offset(line.data, pos)
  61. if i >= #line_cache.screen_line_starting_pos then
  62. return line.data:sub(offset)
  63. end
  64. local endpos = line_cache.screen_line_starting_pos[i+1]
  65. local end_offset = Text.offset(line.data, endpos)
  66. return line.data:sub(offset, end_offset-1)
  67. end
  68. function Text.draw_cursor(State, x, y)
  69. -- blink every 0.5s
  70. if math.floor(Cursor_time*2)%2 == 0 then
  71. App.color(Cursor_color)
  72. love.graphics.rectangle('fill', x,y, 3,State.line_height)
  73. end
  74. State.cursor_x = x
  75. State.cursor_y = y+State.line_height
  76. end
  77. function Text.pretend_draw_cursor(State, x, y)
  78. State.cursor_x = x
  79. State.cursor_y = y+State.line_height
  80. end
  81. function Text.populate_screen_line_starting_pos(State, line_index)
  82. local line = State.lines[line_index]
  83. local line_cache = State.line_cache[line_index]
  84. if line_cache.screen_line_starting_pos then
  85. return
  86. end
  87. line_cache.screen_line_starting_pos = {1}
  88. local x = 0
  89. local pos = 1
  90. -- try to wrap at word boundaries
  91. for frag in line.data:gmatch('%S*%s*') do
  92. local frag_width = State.font:getWidth(frag)
  93. --? print('-- frag:', frag, pos, x, frag_width, State.width)
  94. while x + frag_width > State.width do
  95. --? print('frag:', frag, pos, x, frag_width, State.width)
  96. if x < 0.8 * State.width then
  97. -- long word; chop it at some letter
  98. -- We're not going to reimplement TeX here.
  99. local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
  100. if x == 0 and bpos == 0 then
  101. assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
  102. end
  103. pos = pos + bpos
  104. local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
  105. frag = string.sub(frag, boffset)
  106. --? if bpos > 0 then
  107. --? print('after chop:', frag)
  108. --? end
  109. frag_width = State.font:getWidth(frag)
  110. end
  111. --? print('screen line:', pos)
  112. table.insert(line_cache.screen_line_starting_pos, pos)
  113. x = 0 -- new screen line
  114. end
  115. x = x + frag_width
  116. pos = pos + utf8.len(frag)
  117. end
  118. end
  119. function Text.text_input(State, t)
  120. if App.mouse_down(1) then return end
  121. if App.any_modifier_down() then
  122. if App.key_down(t) then
  123. -- The modifiers didn't change the key. Handle it in keychord_press.
  124. return
  125. else
  126. -- Key mutated by the keyboard layout. Continue below.
  127. end
  128. end
  129. end
  130. -- Don't handle any keys here that would trigger text_input above.
  131. function Text.keychord_press(State, chord)
  132. --? print('chord', chord, State.selection1.line, State.selection1.pos)
  133. --== shortcuts that move the cursor
  134. if chord == 'left' then
  135. Text.left(State)
  136. State.selection1 = {}
  137. elseif chord == 'right' then
  138. Text.right(State)
  139. State.selection1 = {}
  140. elseif chord == 'S-left' then
  141. if State.selection1.line == nil then
  142. State.selection1 = deepcopy(State.cursor1)
  143. end
  144. Text.left(State)
  145. elseif chord == 'S-right' then
  146. if State.selection1.line == nil then
  147. State.selection1 = deepcopy(State.cursor1)
  148. end
  149. Text.right(State)
  150. -- C- hotkeys reserved for drawings, so we'll use M-
  151. elseif chord == 'M-left' then
  152. Text.word_left(State)
  153. State.selection1 = {}
  154. elseif chord == 'M-right' then
  155. Text.word_right(State)
  156. State.selection1 = {}
  157. elseif chord == 'M-S-left' then
  158. if State.selection1.line == nil then
  159. State.selection1 = deepcopy(State.cursor1)
  160. end
  161. Text.word_left(State)
  162. elseif chord == 'M-S-right' then
  163. if State.selection1.line == nil then
  164. State.selection1 = deepcopy(State.cursor1)
  165. end
  166. Text.word_right(State)
  167. elseif chord == 'home' then
  168. Text.start_of_line(State)
  169. State.selection1 = {}
  170. elseif chord == 'end' then
  171. Text.end_of_line(State)
  172. State.selection1 = {}
  173. elseif chord == 'S-home' then
  174. if State.selection1.line == nil then
  175. State.selection1 = deepcopy(State.cursor1)
  176. end
  177. Text.start_of_line(State)
  178. elseif chord == 'S-end' then
  179. if State.selection1.line == nil then
  180. State.selection1 = deepcopy(State.cursor1)
  181. end
  182. Text.end_of_line(State)
  183. elseif chord == 'up' then
  184. Text.up(State)
  185. State.selection1 = {}
  186. elseif chord == 'down' then
  187. Text.down(State)
  188. State.selection1 = {}
  189. elseif chord == 'S-up' then
  190. if State.selection1.line == nil then
  191. State.selection1 = deepcopy(State.cursor1)
  192. end
  193. Text.up(State)
  194. elseif chord == 'S-down' then
  195. if State.selection1.line == nil then
  196. State.selection1 = deepcopy(State.cursor1)
  197. end
  198. Text.down(State)
  199. elseif chord == 'pageup' then
  200. Text.pageup(State)
  201. State.selection1 = {}
  202. elseif chord == 'pagedown' then
  203. Text.pagedown(State)
  204. State.selection1 = {}
  205. elseif chord == 'S-pageup' then
  206. if State.selection1.line == nil then
  207. State.selection1 = deepcopy(State.cursor1)
  208. end
  209. Text.pageup(State)
  210. elseif chord == 'S-pagedown' then
  211. if State.selection1.line == nil then
  212. State.selection1 = deepcopy(State.cursor1)
  213. end
  214. Text.pagedown(State)
  215. end
  216. end
  217. function Text.pageup(State)
  218. State.screen_top1 = Text.previous_screen_top1(State)
  219. State.cursor1 = deepcopy(State.screen_top1)
  220. Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
  221. Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
  222. end
  223. -- return the top y coordinate of a given line_index,
  224. -- or nil if no part of it is on screen
  225. function Text.starty(State, line_index)
  226. -- duplicate some logic from love.draw
  227. -- does not modify State (except to populate line_cache)
  228. if line_index < State.screen_top1.line then return end
  229. local loc2 = Text.to2(State, State.screen_top1)
  230. local y = State.top
  231. while true do
  232. if loc2.line == line_index then return y end
  233. y = y + State.line_height
  234. if y + State.line_height > App.screen.height then break end
  235. local next_loc2 = Text.next_screen_line(State, loc2)
  236. if Text.eq2(next_loc2, loc2) then break end -- end of file
  237. loc2 = next_loc2
  238. end
  239. end
  240. function Text.previous_screen_top1(State)
  241. -- duplicate some logic from love.draw
  242. -- does not modify State (except to populate line_cache)
  243. local loc2 = Text.to2(State, State.screen_top1)
  244. local y = App.screen.height - State.line_height
  245. while y >= State.top do
  246. if loc2.line == 1 and loc2.screen_line == 1 and loc2.screen_pos == 1 then break end
  247. y = y - State.line_height
  248. loc2 = Text.previous_screen_line(State, loc2)
  249. end
  250. return Text.to1(State, loc2)
  251. end
  252. function Text.pagedown(State)
  253. State.screen_top1 = Text.screen_bottom1(State)
  254. State.cursor1 = deepcopy(State.screen_top1)
  255. Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
  256. Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
  257. end
  258. -- return the location of the start of the bottom-most line on screen
  259. function Text.screen_bottom1(State)
  260. -- duplicate some logic from love.draw
  261. -- does not modify State (except to populate line_cache)
  262. local loc2 = Text.to2(State, State.screen_top1)
  263. local y = State.top
  264. while true do
  265. y = y + State.line_height
  266. if y + State.line_height > App.screen.height then break end
  267. local next_loc2 = Text.next_screen_line(State, loc2)
  268. if Text.eq2(next_loc2, loc2) then break end
  269. loc2 = next_loc2
  270. end
  271. return Text.to1(State, loc2)
  272. end
  273. function Text.up(State)
  274. --? print('up', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
  275. local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
  276. if screen_line_starting_pos == 1 then
  277. --? print('cursor is at first screen line of its line')
  278. -- line is done; skip to previous text line
  279. if State.cursor1.line > 1 then
  280. local new_cursor_line = State.cursor1.line-1
  281. --? print('found previous text line')
  282. State.cursor1 = {line=new_cursor_line, pos=nil}
  283. Text.populate_screen_line_starting_pos(State, State.cursor1.line)
  284. -- previous text line found, pick its final screen line
  285. --? print('has multiple screen lines')
  286. local screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos
  287. --? print(#screen_line_starting_pos)
  288. screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
  289. local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
  290. local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
  291. State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
  292. end
  293. else
  294. -- move up one screen line in current line
  295. assert(screen_line_index > 1, 'bumped up against top screen line in line')
  296. local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
  297. local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
  298. local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
  299. State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
  300. --? print('cursor pos is now '..tostring(State.cursor1.pos))
  301. end
  302. if Text.lt1(State.cursor1, State.screen_top1) then
  303. State.screen_top1 = {
  304. line=State.cursor1.line,
  305. pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
  306. }
  307. Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
  308. end
  309. end
  310. function Text.down(State)
  311. --? print('down', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
  312. assert(State.cursor1.pos, 'cursor has no pos')
  313. if Text.cursor_at_final_screen_line(State) then
  314. -- line is done, skip to next text line
  315. --? print('cursor at final screen line of its line')
  316. if State.cursor1.line < #State.lines then
  317. local new_cursor_line = State.cursor1.line+1
  318. State.cursor1.line = new_cursor_line
  319. State.cursor1.pos = Text.nearest_cursor_pos(State.font, State.lines[State.cursor1.line].data, State.cursor_x, State.left)
  320. --? print(State.cursor1.pos)
  321. end
  322. local screen_bottom1 = Text.screen_bottom1(State)
  323. --? print('down 2', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos, screen_bottom1.line, screen_bottom1.pos)
  324. if State.cursor1.line > screen_bottom1.line then
  325. --? print('screen top before:', State.screen_top1.line, State.screen_top1.pos)
  326. --? print('scroll up preserving cursor')
  327. Text.snap_cursor_to_bottom_of_screen(State)
  328. --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
  329. end
  330. else
  331. -- move down one screen line in current line
  332. local screen_bottom1 = Text.screen_bottom1(State)
  333. local scroll_down = Text.le1(screen_bottom1, State.cursor1)
  334. --? print('cursor is NOT at final screen line of its line')
  335. local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor1)
  336. Text.populate_screen_line_starting_pos(State, State.cursor1.line)
  337. local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index+1]
  338. --? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
  339. local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
  340. local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
  341. State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
  342. --? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
  343. if scroll_down then
  344. --? print('scroll up preserving cursor')
  345. Text.snap_cursor_to_bottom_of_screen(State)
  346. --? print('screen top after:', State.screen_top1.line, State.screen_top1.pos)
  347. end
  348. end
  349. --? print('=>', State.cursor1.line, State.cursor1.pos, State.screen_top1.line, State.screen_top1.pos)
  350. end
  351. function Text.start_of_line(State)
  352. State.cursor1.pos = 1
  353. if Text.lt1(State.cursor1, State.screen_top1) then
  354. State.screen_top1 = deepcopy(State.cursor1)
  355. end
  356. end
  357. function Text.end_of_line(State)
  358. State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
  359. if Text.cursor_out_of_screen(State) then
  360. Text.snap_cursor_to_bottom_of_screen(State)
  361. end
  362. end
  363. function Text.word_left(State)
  364. -- skip some whitespace
  365. while true do
  366. if State.cursor1.pos == 1 then
  367. break
  368. end
  369. if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%S') then
  370. break
  371. end
  372. Text.left(State)
  373. end
  374. -- skip some non-whitespace
  375. while true do
  376. Text.left(State)
  377. if State.cursor1.pos == 1 then
  378. break
  379. end
  380. assert(State.cursor1.pos > 1, 'bumped up against start of line')
  381. if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos-1, '%s') then
  382. break
  383. end
  384. end
  385. end
  386. function Text.word_right(State)
  387. -- skip some whitespace
  388. while true do
  389. if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
  390. break
  391. end
  392. if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%S') then
  393. break
  394. end
  395. Text.right_without_scroll(State)
  396. end
  397. while true do
  398. Text.right_without_scroll(State)
  399. if State.cursor1.pos > utf8.len(State.lines[State.cursor1.line].data) then
  400. break
  401. end
  402. if Text.match(State.lines[State.cursor1.line].data, State.cursor1.pos, '%s') then
  403. break
  404. end
  405. end
  406. if Text.cursor_out_of_screen(State) then
  407. Text.snap_cursor_to_bottom_of_screen(State)
  408. end
  409. end
  410. function Text.match(s, pos, pat)
  411. local start_offset = Text.offset(s, pos)
  412. local end_offset = Text.offset(s, pos+1)
  413. assert(end_offset > start_offset, ('end_offset %d not > start_offset %d'):format(end_offset, start_offset))
  414. local curr = s:sub(start_offset, end_offset-1)
  415. return curr:match(pat)
  416. end
  417. function Text.left(State)
  418. if State.cursor1.pos > 1 then
  419. State.cursor1.pos = State.cursor1.pos-1
  420. elseif State.cursor1.line > 1 then
  421. State.cursor1.line = State.cursor1.line-1
  422. State.cursor1.pos = utf8.len(State.lines[State.cursor1.line].data) + 1
  423. end
  424. if Text.lt1(State.cursor1, State.screen_top1) then
  425. State.screen_top1 = {
  426. line=State.cursor1.line,
  427. pos=Text.pos_at_start_of_screen_line(State, State.cursor1),
  428. }
  429. Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
  430. end
  431. end
  432. function Text.right(State)
  433. Text.right_without_scroll(State)
  434. if Text.cursor_out_of_screen(State) then
  435. Text.snap_cursor_to_bottom_of_screen(State)
  436. end
  437. end
  438. function Text.right_without_scroll(State)
  439. if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
  440. State.cursor1.pos = State.cursor1.pos+1
  441. elseif State.cursor1.line <= #State.lines-1 then
  442. State.cursor1.line = State.cursor1.line+1
  443. State.cursor1.pos = 1
  444. end
  445. end
  446. -- result: pos, index of screen line
  447. function Text.pos_at_start_of_screen_line(State, loc1)
  448. Text.populate_screen_line_starting_pos(State, loc1.line)
  449. local line_cache = State.line_cache[loc1.line]
  450. for i=#line_cache.screen_line_starting_pos,1,-1 do
  451. local spos = line_cache.screen_line_starting_pos[i]
  452. if spos <= loc1.pos then
  453. return spos,i
  454. end
  455. end
  456. assert(false, ('invalid pos %d'):format(loc1.pos))
  457. end
  458. function Text.pos_at_end_of_screen_line(State, loc1)
  459. Text.populate_screen_line_starting_pos(State, loc1.line)
  460. local line_cache = State.line_cache[loc1.line]
  461. local most_recent_final_pos = utf8.len(State.lines[loc1.line].data)+1
  462. for i=#line_cache.screen_line_starting_pos,1,-1 do
  463. local spos = line_cache.screen_line_starting_pos[i]
  464. if spos <= loc1.pos then
  465. return most_recent_final_pos
  466. end
  467. most_recent_final_pos = spos-1
  468. end
  469. assert(false, ('invalid pos %d'):format(loc1.pos))
  470. end
  471. function Text.final_loc_on_screen(State)
  472. local screen_bottom1 = Text.screen_bottom1(State)
  473. return {
  474. line=screen_bottom1.line,
  475. pos=Text.pos_at_end_of_screen_line(State, screen_bottom1),
  476. }
  477. end
  478. function Text.cursor_at_final_screen_line(State)
  479. Text.populate_screen_line_starting_pos(State, State.cursor1.line)
  480. local screen_lines = State.line_cache[State.cursor1.line].screen_line_starting_pos
  481. --? print(screen_lines[#screen_lines], State.cursor1.pos)
  482. return screen_lines[#screen_lines] <= State.cursor1.pos
  483. end
  484. function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
  485. if State.top > App.screen.height - State.line_height then
  486. --? print('scroll up')
  487. Text.snap_cursor_to_bottom_of_screen(State)
  488. end
  489. end
  490. -- should never modify State.cursor1
  491. function Text.snap_cursor_to_bottom_of_screen(State)
  492. --? print('to2:', State.cursor1.line, State.cursor1.pos)
  493. local top2 = Text.to2(State, State.cursor1)
  494. --? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
  495. -- slide to start of screen line
  496. top2.screen_pos = 1 -- start of screen line
  497. --? print('snap', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
  498. --? print('cursor pos '..tostring(State.cursor1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
  499. local y = App.screen.height - State.line_height
  500. -- duplicate some logic from love.draw
  501. while true do
  502. --? print(y, 'top2:', top2.line, top2.screen_line, top2.screen_pos)
  503. if top2.line == 1 and top2.screen_line == 1 then break end
  504. local h = State.line_height
  505. if y - h < State.top then
  506. break
  507. end
  508. y = y - h
  509. top2 = Text.previous_screen_line(State, top2)
  510. end
  511. --? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
  512. State.screen_top1 = Text.to1(State, top2)
  513. --? print('top1 finally:', State.screen_top1.line, State.screen_top1.pos)
  514. --? print('snap =>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)
  515. Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
  516. end
  517. function Text.in_line(State, line_index, x,y)
  518. local line = State.lines[line_index]
  519. local line_cache = State.line_cache[line_index]
  520. local starty = Text.starty(State, line_index)
  521. if starty == nil then return false end -- outside current page
  522. if y < starty then return false end
  523. Text.populate_screen_line_starting_pos(State, line_index)
  524. 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)
  525. end
  526. -- convert mx,my in pixels to schema-1 coordinates
  527. function Text.to_pos_on_line(State, line_index, mx, my)
  528. local line = State.lines[line_index]
  529. local line_cache = State.line_cache[line_index]
  530. local starty = Text.starty(State, line_index)
  531. assert(my >= starty, 'failed to map y pixel to line')
  532. -- duplicate some logic from Text.draw
  533. local y = starty
  534. local start_screen_line_index = Text.screen_line_index(line_cache.screen_line_starting_pos, line_cache.startpos)
  535. for screen_line_index = start_screen_line_index,#line_cache.screen_line_starting_pos do
  536. local screen_line_starting_pos = line_cache.screen_line_starting_pos[screen_line_index]
  537. local screen_line_starting_byte_offset = Text.offset(line.data, screen_line_starting_pos)
  538. --? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_byte_offset))
  539. local nexty = y + State.line_height
  540. if my < nexty then
  541. -- On all wrapped screen lines but the final one, clicks past end of
  542. -- line position cursor on final character of screen line.
  543. -- (The final screen line positions past end of screen line as always.)
  544. 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
  545. --? print('past end of non-final line; return')
  546. return line_cache.screen_line_starting_pos[screen_line_index+1]
  547. end
  548. local s = string.sub(line.data, screen_line_starting_byte_offset)
  549. --? 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)
  550. return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
  551. end
  552. y = nexty
  553. end
  554. assert(false, 'failed to map y pixel to line')
  555. end
  556. function Text.screen_line_width(State, line_index, i)
  557. local line = State.lines[line_index]
  558. local line_cache = State.line_cache[line_index]
  559. local start_pos = line_cache.screen_line_starting_pos[i]
  560. local start_offset = Text.offset(line.data, start_pos)
  561. local screen_line
  562. if i < #line_cache.screen_line_starting_pos then
  563. local past_end_pos = line_cache.screen_line_starting_pos[i+1]
  564. local past_end_offset = Text.offset(line.data, past_end_pos)
  565. screen_line = string.sub(line.data, start_offset, past_end_offset-1)
  566. else
  567. screen_line = string.sub(line.data, start_pos)
  568. end
  569. return State.font:getWidth(screen_line)
  570. end
  571. function Text.screen_line_index(screen_line_starting_pos, pos)
  572. for i = #screen_line_starting_pos,1,-1 do
  573. if screen_line_starting_pos[i] <= pos then
  574. return i
  575. end
  576. end
  577. end
  578. -- convert x pixel coordinate to pos
  579. -- oblivious to wrapping
  580. -- result: 1 to len+1
  581. function Text.nearest_cursor_pos(font, line, x, left)
  582. if x < left then
  583. return 1
  584. end
  585. local len = utf8.len(line)
  586. local max_x = left+Text.x(font, line, len+1)
  587. if x > max_x then
  588. return len+1
  589. end
  590. local leftpos, rightpos = 1, len+1
  591. --? print('-- nearest', x)
  592. while true do
  593. --? print('nearest', x, '^'..line..'$', leftpos, rightpos)
  594. if leftpos == rightpos then
  595. return leftpos
  596. end
  597. local curr = math.floor((leftpos+rightpos)/2)
  598. local currxmin = left+Text.x(font, line, curr)
  599. local currxmax = left+Text.x(font, line, curr+1)
  600. --? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
  601. if currxmin <= x and x < currxmax then
  602. if x-currxmin < currxmax-x then
  603. return curr
  604. else
  605. return curr+1
  606. end
  607. end
  608. if leftpos >= rightpos-1 then
  609. return rightpos
  610. end
  611. if currxmin > x then
  612. rightpos = curr
  613. else
  614. leftpos = curr
  615. end
  616. end
  617. assert(false, 'failed to map x pixel to pos')
  618. end
  619. -- return the nearest index of line (in utf8 code points) which lies entirely
  620. -- within x pixels of the left margin
  621. -- result: 0 to len+1
  622. function Text.nearest_pos_less_than(font, line, x)
  623. --? print('', '-- nearest_pos_less_than', line, x)
  624. local len = utf8.len(line)
  625. local max_x = Text.x_after(font, line, len)
  626. if x > max_x then
  627. return len+1
  628. end
  629. local left, right = 0, len+1
  630. while true do
  631. local curr = math.floor((left+right)/2)
  632. local currxmin = Text.x_after(font, line, curr+1)
  633. local currxmax = Text.x_after(font, line, curr+2)
  634. --? print('', x, left, right, curr, currxmin, currxmax)
  635. if currxmin <= x and x < currxmax then
  636. return curr
  637. end
  638. if left >= right-1 then
  639. return left
  640. end
  641. if currxmin > x then
  642. right = curr
  643. else
  644. left = curr
  645. end
  646. end
  647. assert(false, 'failed to map x pixel to pos')
  648. end
  649. function Text.x_after(font, s, pos)
  650. local len = utf8.len(s)
  651. local offset = Text.offset(s, math.min(pos+1, len+1))
  652. local s_before = s:sub(1, offset-1)
  653. --? print('^'..s_before..'$')
  654. return font:getWidth(s_before)
  655. end
  656. function Text.x(font, s, pos)
  657. local offset = Text.offset(s, pos)
  658. local s_before = s:sub(1, offset-1)
  659. return font:getWidth(s_before)
  660. end
  661. function Text.to2(State, loc1)
  662. local result = {line=loc1.line}
  663. local line_cache = State.line_cache[loc1.line]
  664. Text.populate_screen_line_starting_pos(State, loc1.line)
  665. for i=#line_cache.screen_line_starting_pos,1,-1 do
  666. local spos = line_cache.screen_line_starting_pos[i]
  667. if spos <= loc1.pos then
  668. result.screen_line = i
  669. result.screen_pos = loc1.pos - spos + 1
  670. break
  671. end
  672. end
  673. assert(result.screen_pos, 'failed to convert schema-1 coordinate to schema-2')
  674. return result
  675. end
  676. function Text.to1(State, loc2)
  677. local result = {line=loc2.line, pos=loc2.screen_pos}
  678. if loc2.screen_line > 1 then
  679. result.pos = State.line_cache[loc2.line].screen_line_starting_pos[loc2.screen_line] + loc2.screen_pos - 1
  680. end
  681. return result
  682. end
  683. function Text.eq1(a, b)
  684. return a.line == b.line and a.pos == b.pos
  685. end
  686. function Text.lt1(a, b)
  687. if a.line < b.line then
  688. return true
  689. end
  690. if a.line > b.line then
  691. return false
  692. end
  693. return a.pos < b.pos
  694. end
  695. function Text.le1(a, b)
  696. if a.line < b.line then
  697. return true
  698. end
  699. if a.line > b.line then
  700. return false
  701. end
  702. return a.pos <= b.pos
  703. end
  704. function Text.eq2(a, b)
  705. return a.line == b.line and a.screen_line == b.screen_line and a.screen_pos == b.screen_pos
  706. end
  707. function Text.offset(s, pos1)
  708. if pos1 == 1 then return 1 end
  709. local result = utf8.offset(s, pos1)
  710. if result == nil then
  711. 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))
  712. end
  713. return result
  714. end
  715. function Text.previous_screen_line(State, loc2)
  716. if loc2.screen_line > 1 then
  717. return {line=loc2.line, screen_line=loc2.screen_line-1, screen_pos=1}
  718. elseif loc2.line == 1 then
  719. return loc2
  720. else
  721. local l = State.lines[loc2.line-1]
  722. Text.populate_screen_line_starting_pos(State, loc2.line-1)
  723. return {line=loc2.line-1, screen_line=#State.line_cache[loc2.line-1].screen_line_starting_pos, screen_pos=1}
  724. end
  725. end
  726. function Text.next_screen_line(State, loc2)
  727. Text.populate_screen_line_starting_pos(State, loc2.line)
  728. if loc2.screen_line >= #State.line_cache[loc2.line].screen_line_starting_pos then
  729. if loc2.line < #State.lines then
  730. return {line=loc2.line+1, screen_line=1, screen_pos=1}
  731. else
  732. return loc2
  733. end
  734. else
  735. return {line=loc2.line, screen_line=loc2.screen_line+1, screen_pos=1}
  736. end
  737. end
  738. -- resize helper
  739. function Text.tweak_screen_top_and_cursor(State)
  740. if State.screen_top1.pos == 1 then return end
  741. Text.populate_screen_line_starting_pos(State, State.screen_top1.line)
  742. local line = State.lines[State.screen_top1.line]
  743. local line_cache = State.line_cache[State.screen_top1.line]
  744. for i=2,#line_cache.screen_line_starting_pos do
  745. local pos = line_cache.screen_line_starting_pos[i]
  746. if pos == State.screen_top1.pos then
  747. break
  748. end
  749. if pos > State.screen_top1.pos then
  750. -- make sure screen top is at start of a screen line
  751. local prev = line_cache.screen_line_starting_pos[i-1]
  752. if State.screen_top1.pos - prev < pos - State.screen_top1.pos then
  753. State.screen_top1.pos = prev
  754. else
  755. State.screen_top1.pos = pos
  756. end
  757. break
  758. end
  759. end
  760. -- make sure cursor is on screen
  761. local screen_bottom1 = Text.screen_bottom1(State)
  762. if Text.lt1(State.cursor1, State.screen_top1) then
  763. State.cursor1 = deepcopy(State.screen_top1)
  764. elseif State.cursor1.line >= screen_bottom1.line then
  765. if Text.cursor_out_of_screen(State) then
  766. State.cursor1 = Text.final_loc_on_screen(State)
  767. end
  768. end
  769. end
  770. -- slightly expensive since it redraws the screen
  771. function Text.cursor_out_of_screen(State)
  772. edit.draw(State)
  773. return State.cursor_y == nil
  774. end
  775. function Text.redraw_all(State)
  776. --? print('clearing line caches')
  777. -- Perform some early sanity checking here, in hopes that we correctly call
  778. -- this whenever we change editor state.
  779. if State.right <= State.left then
  780. assert(false, ('Right margin %d must be to the right of the left margin %d'):format(State.right, State.left))
  781. end
  782. State.line_cache = {}
  783. for i=1,#State.lines do
  784. State.line_cache[i] = {}
  785. end
  786. end
  787. function Text.clear_screen_line_cache(State, line_index)
  788. State.line_cache[line_index].screen_line_starting_pos = nil
  789. end
  790. function trim(s)
  791. return s:gsub('^%s+', ''):gsub('%s+$', '')
  792. end
  793. function ltrim(s)
  794. return s:gsub('^%s+', '')
  795. end
  796. function rtrim(s)
  797. return s:gsub('%s+$', '')
  798. end
  799. function starts_with(s, prefix)
  800. if #s < #prefix then
  801. return false
  802. end
  803. for i=1,#prefix do
  804. if s:sub(i,i) ~= prefix:sub(i,i) then
  805. return false
  806. end
  807. end
  808. return true
  809. end
  810. function ends_with(s, suffix)
  811. if #s < #suffix then
  812. return false
  813. end
  814. for i=0,#suffix-1 do
  815. if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then
  816. return false
  817. end
  818. end
  819. return true
  820. end