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