edit.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. -- some constants people might like to tweak
  2. Text_color = {r=0, g=0, b=0}
  3. Cursor_color = {r=1, g=0, b=0}
  4. Highlight_color = {r=0.7, g=0.7, b=0.9} -- selected text
  5. Margin_top = 15
  6. Margin_left = 25
  7. Margin_right = 25
  8. edit = {}
  9. -- run in both tests and a real run
  10. function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen
  11. local result = {
  12. lines = {{data=''}}, -- array of strings
  13. -- Lines can be too long to fit on screen, in which case they _wrap_ into
  14. -- multiple _screen lines_.
  15. -- rendering wrapped text lines needs some additional short-lived data per line:
  16. -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
  17. -- starty, the y coord in pixels the line starts rendering from
  18. -- fragments: snippets of the line guaranteed to not straddle screen lines
  19. -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
  20. line_cache = {},
  21. -- Given wrapping, any potential location for the text cursor can be described in two ways:
  22. -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
  23. -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
  24. --
  25. -- Most of the time we'll only persist positions in schema 1, translating to
  26. -- schema 2 when that's convenient.
  27. --
  28. -- Make sure these coordinates are never aliased, so that changing one causes
  29. -- action at a distance.
  30. screen_top1 = {line=1, pos=1}, -- position of start of screen line at top of screen
  31. cursor1 = {line=1, pos=1}, -- position of cursor
  32. screen_bottom1 = {line=1, pos=1}, -- position of start of screen line at bottom of screen
  33. selection1 = {},
  34. -- some extra state to compute selection between mouse press and release
  35. old_cursor1 = nil,
  36. old_selection1 = nil,
  37. mousepress_shift = nil,
  38. -- cursor coordinates in pixels
  39. cursor_x = 0,
  40. cursor_y = 0,
  41. font = font,
  42. font_height = font_height,
  43. line_height = line_height,
  44. top = top,
  45. left = math.floor(left),
  46. right = math.floor(right),
  47. width = right-left,
  48. filename = love.filesystem.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
  49. next_save = nil,
  50. -- undo
  51. history = {},
  52. next_history = 1,
  53. -- search
  54. search_term = nil,
  55. search_backup = nil, -- stuff to restore when cancelling search
  56. }
  57. return result
  58. end -- edit.initialize_state
  59. function edit.check_locs(State)
  60. -- if State is inconsistent (i.e. file changed by some other program),
  61. -- throw away all cursor state entirely
  62. if edit.invalid1(State, State.screen_top1)
  63. or edit.invalid_cursor1(State)
  64. or not Text.le1(State.screen_top1, State.cursor1) then
  65. State.screen_top1 = {line=1, pos=1}
  66. State.cursor1 = {line=1, pos=1}
  67. end
  68. end
  69. function edit.invalid1(State, loc1)
  70. if loc1.line > #State.lines then return true end
  71. local l = State.lines[loc1.line]
  72. if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
  73. return loc1.pos > #State.lines[loc1.line].data
  74. end
  75. -- cursor loc in particular differs from other locs in one way:
  76. -- pos might occur just after end of line
  77. function edit.invalid_cursor1(State)
  78. local cursor1 = State.cursor1
  79. if cursor1.line > #State.lines then return true end
  80. local l = State.lines[cursor1.line]
  81. if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
  82. return cursor1.pos > #State.lines[cursor1.line].data + 1
  83. end
  84. -- return y drawn until
  85. function edit.draw(State)
  86. love.graphics.setFont(State.font)
  87. App.color(Text_color)
  88. assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines))
  89. 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))
  90. State.cursor_x = nil
  91. State.cursor_y = nil
  92. local y = State.top
  93. local screen_bottom1 = {line=nil, pos=nil}
  94. --? print('== draw')
  95. for line_index = State.screen_top1.line,#State.lines do
  96. local line = State.lines[line_index]
  97. --? print('draw:', y, line_index, line)
  98. if y + State.line_height > App.screen.height then break end
  99. screen_bottom1.line = line_index
  100. --? print('text.draw', y, line_index)
  101. local startpos = 1
  102. if line_index == State.screen_top1.line then
  103. startpos = State.screen_top1.pos
  104. end
  105. y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
  106. --? print('=> y', y)
  107. end
  108. State.screen_bottom1 = screen_bottom1
  109. if State.search_term then
  110. Text.draw_search_bar(State)
  111. end
  112. return y
  113. end
  114. function edit.update(State, dt)
  115. if State.next_save and State.next_save < Current_time then
  116. save_to_disk(State)
  117. State.next_save = nil
  118. end
  119. end
  120. function schedule_save(State)
  121. if State.next_save == nil then
  122. State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
  123. end
  124. end
  125. function edit.quit(State)
  126. -- make sure to save before quitting
  127. if State.next_save then
  128. save_to_disk(State)
  129. -- give some time for the OS to flush everything to disk
  130. love.timer.sleep(0.1)
  131. end
  132. end
  133. function edit.mouse_press(State, x,y, mouse_button)
  134. love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
  135. if State.search_term then return end
  136. State.mouse_down = mouse_button
  137. --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
  138. if y < State.top then
  139. State.old_cursor1 = State.cursor1
  140. State.old_selection1 = State.selection1
  141. State.mousepress_shift = App.shift_down()
  142. State.selection1 = {
  143. line=State.screen_top1.line,
  144. pos=State.screen_top1.pos,
  145. }
  146. return
  147. end
  148. for line_index,line in ipairs(State.lines) do
  149. if Text.in_line(State, line_index, x,y) then
  150. -- delicate dance between cursor, selection and old cursor/selection
  151. -- scenarios:
  152. -- regular press+release: sets cursor, clears selection
  153. -- shift press+release:
  154. -- sets selection to old cursor if not set otherwise leaves it untouched
  155. -- sets cursor
  156. -- press and hold to start a selection: sets selection on press, cursor on release
  157. -- press and hold, then press shift: ignore shift
  158. -- i.e. mouse_release should never look at shift state
  159. --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
  160. State.old_cursor1 = State.cursor1
  161. State.old_selection1 = State.selection1
  162. State.mousepress_shift = App.shift_down()
  163. State.selection1 = {
  164. line=line_index,
  165. pos=Text.to_pos_on_line(State, line_index, x, y),
  166. }
  167. return
  168. end
  169. end
  170. -- still here? mouse press is below all screen lines
  171. State.old_cursor1 = State.cursor1
  172. State.old_selection1 = State.selection1
  173. State.mousepress_shift = App.shift_down()
  174. State.selection1 = {
  175. line=State.screen_bottom1.line,
  176. pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
  177. }
  178. end
  179. function edit.mouse_release(State, x,y, mouse_button)
  180. if State.search_term then return end
  181. --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
  182. State.mouse_down = nil
  183. if y < State.top then
  184. State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
  185. edit.clean_up_mouse_press(State)
  186. return
  187. end
  188. for line_index,line in ipairs(State.lines) do
  189. if Text.in_line(State, line_index, x,y) then
  190. --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
  191. State.cursor1 = {
  192. line=line_index,
  193. pos=Text.to_pos_on_line(State, line_index, x, y),
  194. }
  195. --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
  196. edit.clean_up_mouse_press(State)
  197. return
  198. end
  199. end
  200. -- still here? mouse release is below all screen lines
  201. State.cursor1.line, State.cursor1.pos = State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1)
  202. edit.clean_up_mouse_press(State)
  203. --? print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos))
  204. end
  205. function edit.clean_up_mouse_press(State)
  206. if State.mousepress_shift then
  207. if State.old_selection1.line == nil then
  208. State.selection1 = State.old_cursor1
  209. else
  210. State.selection1 = State.old_selection1
  211. end
  212. end
  213. State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
  214. if eq(State.cursor1, State.selection1) then
  215. State.selection1 = {}
  216. end
  217. end
  218. function edit.mouse_wheel_move(State, dx,dy)
  219. if dy > 0 then
  220. State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
  221. for i=1,math.floor(dy) do
  222. Text.up(State)
  223. end
  224. elseif dy < 0 then
  225. State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
  226. for i=1,math.floor(-dy) do
  227. Text.down(State)
  228. end
  229. end
  230. end
  231. function edit.text_input(State, t)
  232. --? print('text input', t)
  233. if State.search_term then
  234. State.search_term = State.search_term..t
  235. Text.search_next(State)
  236. else
  237. Text.text_input(State, t)
  238. end
  239. schedule_save(State)
  240. end
  241. function edit.keychord_press(State, chord, key)
  242. if State.selection1.line and
  243. -- printable character created using shift key => delete selection
  244. -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
  245. (not App.shift_down() or utf8.len(key) == 1) and
  246. chord ~= 'C-a' and chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and chord ~= 'delete' and chord ~= 'C-z' and chord ~= 'C-y' and not App.is_cursor_movement(key) then
  247. Text.delete_selection(State, State.left, State.right)
  248. end
  249. if State.search_term then
  250. for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
  251. if chord == 'escape' then
  252. State.search_term = nil
  253. State.cursor1 = State.search_backup.cursor
  254. State.screen_top1 = State.search_backup.screen_top
  255. State.search_backup = nil
  256. Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
  257. elseif chord == 'return' then
  258. State.search_term = nil
  259. State.search_backup = nil
  260. elseif chord == 'backspace' then
  261. local len = utf8.len(State.search_term)
  262. local byte_offset = Text.offset(State.search_term, len)
  263. State.search_term = string.sub(State.search_term, 1, byte_offset-1)
  264. elseif chord == 'down' then
  265. State.cursor1.pos = State.cursor1.pos+1
  266. Text.search_next(State)
  267. elseif chord == 'up' then
  268. Text.search_previous(State)
  269. end
  270. return
  271. elseif chord == 'C-f' then
  272. State.search_term = ''
  273. State.search_backup = {
  274. cursor={line=State.cursor1.line, pos=State.cursor1.pos},
  275. screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
  276. }
  277. -- zoom
  278. elseif chord == 'C-=' then
  279. edit.update_font_settings(State, State.font_height+2)
  280. Text.redraw_all(State)
  281. elseif chord == 'C--' then
  282. if State.font_height > 2 then
  283. edit.update_font_settings(State, State.font_height-2)
  284. Text.redraw_all(State)
  285. end
  286. elseif chord == 'C-0' then
  287. edit.update_font_settings(State, 20)
  288. Text.redraw_all(State)
  289. -- undo
  290. elseif chord == 'C-z' then
  291. local event = undo_event(State)
  292. if event then
  293. local src = event.before
  294. State.screen_top1 = deepcopy(src.screen_top)
  295. State.cursor1 = deepcopy(src.cursor)
  296. State.selection1 = deepcopy(src.selection)
  297. patch(State.lines, event.after, event.before)
  298. patch_placeholders(State.line_cache, event.after, event.before)
  299. -- if we're scrolling, reclaim all fragments to avoid memory leaks
  300. Text.redraw_all(State)
  301. schedule_save(State)
  302. end
  303. elseif chord == 'C-y' then
  304. local event = redo_event(State)
  305. if event then
  306. local src = event.after
  307. State.screen_top1 = deepcopy(src.screen_top)
  308. State.cursor1 = deepcopy(src.cursor)
  309. State.selection1 = deepcopy(src.selection)
  310. patch(State.lines, event.before, event.after)
  311. -- if we're scrolling, reclaim all fragments to avoid memory leaks
  312. Text.redraw_all(State)
  313. schedule_save(State)
  314. end
  315. -- clipboard
  316. elseif chord == 'C-a' then
  317. State.selection1 = {line=1, pos=1}
  318. State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
  319. elseif chord == 'C-c' then
  320. local s = Text.selection(State)
  321. if s then
  322. App.set_clipboard(s)
  323. end
  324. elseif chord == 'C-x' then
  325. local s = Text.cut_selection(State, State.left, State.right)
  326. if s then
  327. App.set_clipboard(s)
  328. end
  329. schedule_save(State)
  330. elseif chord == 'C-v' then
  331. -- We don't have a good sense of when to scroll, so we'll be conservative
  332. -- and sometimes scroll when we didn't quite need to.
  333. local before_line = State.cursor1.line
  334. local before = snapshot(State, before_line)
  335. local clipboard_data = App.get_clipboard()
  336. for _,code in utf8.codes(clipboard_data) do
  337. local c = utf8.char(code)
  338. if c == '\n' then
  339. Text.insert_return(State)
  340. else
  341. Text.insert_at_cursor(State, c)
  342. end
  343. end
  344. if Text.cursor_out_of_screen(State) then
  345. Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
  346. end
  347. schedule_save(State)
  348. record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
  349. -- dispatch to text
  350. else
  351. for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
  352. Text.keychord_press(State, chord)
  353. end
  354. end
  355. function edit.key_release(State, key, scancode)
  356. end
  357. function edit.update_font_settings(State, font_height)
  358. State.font_height = font_height
  359. State.font = love.graphics.newFont(State.font_height)
  360. State.line_height = math.floor(font_height*1.3)
  361. end
  362. --== some methods for tests
  363. -- Insulate tests from some key globals so I don't have to change the vast
  364. -- majority of tests when they're modified for the real app.
  365. Test_margin_left = 25
  366. Test_margin_right = 0
  367. function edit.initialize_test_state()
  368. -- if you change these values, tests will start failing
  369. return edit.initialize_state(
  370. 15, -- top margin
  371. Test_margin_left,
  372. App.screen.width - Test_margin_right,
  373. love.graphics.getFont(),
  374. 14,
  375. 15) -- line height
  376. end
  377. -- all text_input events are also keypresses
  378. -- TODO: handle chords of multiple keys
  379. function edit.run_after_text_input(State, t)
  380. edit.keychord_press(State, t)
  381. edit.text_input(State, t)
  382. edit.key_release(State, t)
  383. App.screen.contents = {}
  384. edit.update(State, 0)
  385. edit.draw(State)
  386. end
  387. -- not all keys are text_input
  388. function edit.run_after_keychord(State, chord, key)
  389. edit.keychord_press(State, chord, key)
  390. edit.key_release(State, key)
  391. App.screen.contents = {}
  392. edit.update(State, 0)
  393. edit.draw(State)
  394. end
  395. function edit.run_after_mouse_click(State, x,y, mouse_button)
  396. App.fake_mouse_press(x,y, mouse_button)
  397. edit.mouse_press(State, x,y, mouse_button)
  398. App.fake_mouse_release(x,y, mouse_button)
  399. edit.mouse_release(State, x,y, mouse_button)
  400. App.screen.contents = {}
  401. edit.update(State, 0)
  402. edit.draw(State)
  403. end
  404. function edit.run_after_mouse_press(State, x,y, mouse_button)
  405. App.fake_mouse_press(x,y, mouse_button)
  406. edit.mouse_press(State, x,y, mouse_button)
  407. App.screen.contents = {}
  408. edit.update(State, 0)
  409. edit.draw(State)
  410. end
  411. function edit.run_after_mouse_release(State, x,y, mouse_button)
  412. App.fake_mouse_release(x,y, mouse_button)
  413. edit.mouse_release(State, x,y, mouse_button)
  414. App.screen.contents = {}
  415. edit.update(State, 0)
  416. edit.draw(State)
  417. end