Merge lines.love
[view.love.git] / edit.lua
blobe4325f48768a00e3be6f4507c287e6ae5cd38ab0
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_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_height = font_height,
51 line_height = line_height,
53 top = top,
54 left = math.floor(left),
55 right = math.floor(right),
56 width = right-left,
58 filename = love.filesystem.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
59 next_save = nil,
61 -- undo
62 history = {},
63 next_history = 1,
65 -- search
66 search_term = nil,
67 search_backup = nil, -- stuff to restore when cancelling search
69 return result
70 end -- App.initialize_state
72 function edit.check_locs(State)
73 -- if State is inconsistent (i.e. file changed by some other program),
74 -- throw away all cursor state entirely
75 if edit.invalid1(State, State.screen_top1)
76 or edit.invalid_cursor1(State)
77 or not Text.le1(State.screen_top1, State.cursor1) then
78 State.screen_top1 = {line=1, pos=1}
79 State.cursor1 = {line=1, pos=1}
80 end
81 end
83 function edit.invalid1(State, loc1)
84 if loc1.line > #State.lines then return true end
85 local l = State.lines[loc1.line]
86 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
87 return loc1.pos > #State.lines[loc1.line].data
88 end
90 -- cursor loc in particular differs from other locs in one way:
91 -- pos might occur just after end of line
92 function edit.invalid_cursor1(State)
93 local cursor1 = State.cursor1
94 if cursor1.line > #State.lines then return true end
95 local l = State.lines[cursor1.line]
96 if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
97 return cursor1.pos > #State.lines[cursor1.line].data + 1
98 end
100 -- return y drawn until
101 function edit.draw(State)
102 App.color(Text_color)
103 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))
104 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))
105 State.cursor_x = nil
106 State.cursor_y = nil
107 local y = State.top
108 local screen_bottom1 = {line=nil, pos=nil}
109 --? print('== draw')
110 for line_index = State.screen_top1.line,#State.lines do
111 local line = State.lines[line_index]
112 --? print('draw:', y, line_index, line)
113 if y + State.line_height > App.screen.height then break end
114 screen_bottom1.line = line_index
115 --? print('text.draw', y, line_index)
116 local startpos = 1
117 if line_index == State.screen_top1.line then
118 startpos = State.screen_top1.pos
120 y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos)
121 --? print('=> y', y)
123 State.screen_bottom1 = screen_bottom1
124 if State.search_term then
125 Text.draw_search_bar(State)
127 return y
130 function edit.update(State, dt)
131 if State.next_save and State.next_save < Current_time then
132 save_to_disk(State)
133 State.next_save = nil
137 function schedule_save(State)
138 if State.next_save == nil then
139 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
143 function edit.quit(State)
144 -- make sure to save before quitting
145 if State.next_save then
146 save_to_disk(State)
147 -- give some time for the OS to flush everything to disk
148 love.timer.sleep(0.1)
152 function edit.mouse_press(State, x,y, mouse_button)
153 if State.search_term then return end
154 State.mouse_down = mouse_button
155 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
156 if y < State.top then
157 State.old_cursor1 = State.cursor1
158 State.old_selection1 = State.selection1
159 State.mousepress_shift = App.shift_down()
160 State.selection1 = {
161 line=State.screen_top1.line,
162 pos=State.screen_top1.pos,
164 return
167 for line_index,line in ipairs(State.lines) do
168 if Text.in_line(State, line_index, x,y) then
169 -- delicate dance between cursor, selection and old cursor/selection
170 -- scenarios:
171 -- regular press+release: sets cursor, clears selection
172 -- shift press+release:
173 -- sets selection to old cursor if not set otherwise leaves it untouched
174 -- sets cursor
175 -- press and hold to start a selection: sets selection on press, cursor on release
176 -- press and hold, then press shift: ignore shift
177 -- i.e. mouse_release should never look at shift state
178 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
179 State.old_cursor1 = State.cursor1
180 State.old_selection1 = State.selection1
181 State.mousepress_shift = App.shift_down()
182 State.selection1 = {
183 line=line_index,
184 pos=Text.to_pos_on_line(State, line_index, x, y),
186 return
190 -- still here? mouse press is below all screen lines
191 State.old_cursor1 = State.cursor1
192 State.old_selection1 = State.selection1
193 State.mousepress_shift = App.shift_down()
194 State.selection1 = {
195 line=State.screen_bottom1.line,
196 pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
200 function edit.mouse_release(State, x,y, mouse_button)
201 if State.search_term then return end
202 --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
203 State.mouse_down = nil
204 if y < State.top then
205 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
206 edit.clean_up_mouse_press(State)
207 return
209 for line_index,line in ipairs(State.lines) do
210 if Text.in_line(State, line_index, x,y) then
211 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
212 State.cursor1 = {
213 line=line_index,
214 pos=Text.to_pos_on_line(State, line_index, x, y),
216 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
217 edit.clean_up_mouse_press(State)
218 return
222 -- still here? mouse release is below all screen lines
223 State.cursor1.line, State.cursor1.pos = State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1)
224 edit.clean_up_mouse_press(State)
225 --? 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))
228 function edit.clean_up_mouse_press(State)
229 if State.mousepress_shift then
230 if State.old_selection1.line == nil then
231 State.selection1 = State.old_cursor1
232 else
233 State.selection1 = State.old_selection1
236 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
237 if eq(State.cursor1, State.selection1) then
238 State.selection1 = {}
242 function edit.mouse_wheel_move(State, dx,dy)
243 if dy > 0 then
244 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
245 for i=1,math.floor(dy) do
246 Text.up(State)
248 elseif dy < 0 then
249 State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
250 for i=1,math.floor(-dy) do
251 Text.down(State)
256 function edit.text_input(State, t)
257 --? print('text input', t)
258 if State.search_term then
259 State.search_term = State.search_term..t
260 Text.search_next(State)
261 else
262 Text.text_input(State, t)
264 schedule_save(State)
267 function edit.keychord_press(State, chord, key)
268 if State.selection1.line and
269 -- printable character created using shift key => delete selection
270 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
271 (not App.shift_down() or utf8.len(key) == 1) and
272 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(chord) then
273 Text.delete_selection(State, State.left, State.right)
275 if State.search_term then
276 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
277 if chord == 'escape' then
278 State.search_term = nil
279 State.cursor1 = State.search_backup.cursor
280 State.screen_top1 = State.search_backup.screen_top
281 State.search_backup = nil
282 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
283 elseif chord == 'return' then
284 State.search_term = nil
285 State.search_backup = nil
286 elseif chord == 'backspace' then
287 local len = utf8.len(State.search_term)
288 local byte_offset = Text.offset(State.search_term, len)
289 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
290 elseif chord == 'down' then
291 State.cursor1.pos = State.cursor1.pos+1
292 Text.search_next(State)
293 elseif chord == 'up' then
294 Text.search_previous(State)
296 return
297 elseif chord == 'C-f' then
298 State.search_term = ''
299 State.search_backup = {
300 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
301 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
303 -- zoom
304 elseif chord == 'C-=' then
305 edit.update_font_settings(State, State.font_height+2)
306 Text.redraw_all(State)
307 elseif chord == 'C--' then
308 if State.font_height > 2 then
309 edit.update_font_settings(State, State.font_height-2)
310 Text.redraw_all(State)
312 elseif chord == 'C-0' then
313 edit.update_font_settings(State, 20)
314 Text.redraw_all(State)
315 -- undo
316 elseif chord == 'C-z' then
317 local event = undo_event(State)
318 if event then
319 local src = event.before
320 State.screen_top1 = deepcopy(src.screen_top)
321 State.cursor1 = deepcopy(src.cursor)
322 State.selection1 = deepcopy(src.selection)
323 patch(State.lines, event.after, event.before)
324 patch_placeholders(State.line_cache, event.after, event.before)
325 -- if we're scrolling, reclaim all fragments to avoid memory leaks
326 Text.redraw_all(State)
327 schedule_save(State)
329 elseif chord == 'C-y' then
330 local event = redo_event(State)
331 if event then
332 local src = event.after
333 State.screen_top1 = deepcopy(src.screen_top)
334 State.cursor1 = deepcopy(src.cursor)
335 State.selection1 = deepcopy(src.selection)
336 patch(State.lines, event.before, event.after)
337 -- if we're scrolling, reclaim all fragments to avoid memory leaks
338 Text.redraw_all(State)
339 schedule_save(State)
341 -- clipboard
342 elseif chord == 'C-a' then
343 State.selection1 = {line=1, pos=1}
344 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
345 elseif chord == 'C-c' then
346 local s = Text.selection(State)
347 if s then
348 App.set_clipboard(s)
350 elseif chord == 'C-x' then
351 local s = Text.cut_selection(State, State.left, State.right)
352 if s then
353 App.set_clipboard(s)
355 schedule_save(State)
356 elseif chord == 'C-v' then
357 -- We don't have a good sense of when to scroll, so we'll be conservative
358 -- and sometimes scroll when we didn't quite need to.
359 local before_line = State.cursor1.line
360 local before = snapshot(State, before_line)
361 local clipboard_data = App.get_clipboard()
362 for _,code in utf8.codes(clipboard_data) do
363 local c = utf8.char(code)
364 if c == '\n' then
365 Text.insert_return(State)
366 else
367 Text.insert_at_cursor(State, c)
370 if Text.cursor_out_of_screen(State) then
371 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
373 schedule_save(State)
374 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
375 -- dispatch to text
376 else
377 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
378 Text.keychord_press(State, chord)
382 function edit.key_release(State, key, scancode)
385 function edit.update_font_settings(State, font_height)
386 State.font_height = font_height
387 love.graphics.setFont(love.graphics.newFont(State.font_height))
388 State.line_height = math.floor(font_height*1.3)
391 --== some methods for tests
393 -- Insulate tests from some key globals so I don't have to change the vast
394 -- majority of tests when they're modified for the real app.
395 Test_margin_left = 25
396 Test_margin_right = 0
398 function edit.initialize_test_state()
399 -- if you change these values, tests will start failing
400 return edit.initialize_state(
401 15, -- top margin
402 Test_margin_left,
403 App.screen.width - Test_margin_right,
404 14, -- font height assuming default LÖVE font
405 15) -- line height
408 -- all text_input events are also keypresses
409 -- TODO: handle chords of multiple keys
410 function edit.run_after_text_input(State, t)
411 edit.keychord_press(State, t)
412 edit.text_input(State, t)
413 edit.key_release(State, t)
414 App.screen.contents = {}
415 edit.update(State, 0)
416 edit.draw(State)
419 -- not all keys are text_input
420 function edit.run_after_keychord(State, chord)
421 edit.keychord_press(State, chord)
422 edit.key_release(State, chord)
423 App.screen.contents = {}
424 edit.update(State, 0)
425 edit.draw(State)
428 function edit.run_after_mouse_click(State, x,y, mouse_button)
429 App.fake_mouse_press(x,y, mouse_button)
430 edit.mouse_press(State, x,y, mouse_button)
431 App.fake_mouse_release(x,y, mouse_button)
432 edit.mouse_release(State, x,y, mouse_button)
433 App.screen.contents = {}
434 edit.update(State, 0)
435 edit.draw(State)
438 function edit.run_after_mouse_press(State, x,y, mouse_button)
439 App.fake_mouse_press(x,y, mouse_button)
440 edit.mouse_press(State, x,y, mouse_button)
441 App.screen.contents = {}
442 edit.update(State, 0)
443 edit.draw(State)
446 function edit.run_after_mouse_release(State, x,y, mouse_button)
447 App.fake_mouse_release(x,y, mouse_button)
448 edit.mouse_release(State, x,y, mouse_button)
449 App.screen.contents = {}
450 edit.update(State, 0)
451 edit.draw(State)