edit.lua 15 KB

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