text.lua 37 KB

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