Merge lines.love
[view.love.git] / edit.lua
blob5fa0472a8cbaa7c9a578b45428742b2efb5e9041
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 if State.search_term then return end
156 State.mouse_down = mouse_button
157 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
158 if y < State.top then
159 State.old_cursor1 = State.cursor1
160 State.old_selection1 = State.selection1
161 State.mousepress_shift = App.shift_down()
162 State.selection1 = {
163 line=State.screen_top1.line,
164 pos=State.screen_top1.pos,
166 return
169 for line_index,line in ipairs(State.lines) do
170 if Text.in_line(State, line_index, x,y) then
171 -- delicate dance between cursor, selection and old cursor/selection
172 -- scenarios:
173 -- regular press+release: sets cursor, clears selection
174 -- shift press+release:
175 -- sets selection to old cursor if not set otherwise leaves it untouched
176 -- sets cursor
177 -- press and hold to start a selection: sets selection on press, cursor on release
178 -- press and hold, then press shift: ignore shift
179 -- i.e. mouse_release should never look at shift state
180 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
181 State.old_cursor1 = State.cursor1
182 State.old_selection1 = State.selection1
183 State.mousepress_shift = App.shift_down()
184 State.selection1 = {
185 line=line_index,
186 pos=Text.to_pos_on_line(State, line_index, x, y),
188 return
192 -- still here? mouse press is below all screen lines
193 State.old_cursor1 = State.cursor1
194 State.old_selection1 = State.selection1
195 State.mousepress_shift = App.shift_down()
196 State.selection1 = {
197 line=State.screen_bottom1.line,
198 pos=Text.pos_at_end_of_screen_line(State, State.screen_bottom1),
202 function edit.mouse_release(State, x,y, mouse_button)
203 if State.search_term then return end
204 --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
205 State.mouse_down = nil
206 if y < State.top then
207 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
208 edit.clean_up_mouse_press(State)
209 return
211 for line_index,line in ipairs(State.lines) do
212 if Text.in_line(State, line_index, x,y) then
213 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
214 State.cursor1 = {
215 line=line_index,
216 pos=Text.to_pos_on_line(State, line_index, x, y),
218 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
219 edit.clean_up_mouse_press(State)
220 return
224 -- still here? mouse release is below all screen lines
225 State.cursor1.line, State.cursor1.pos = State.screen_bottom1.line, Text.pos_at_end_of_screen_line(State, State.screen_bottom1)
226 edit.clean_up_mouse_press(State)
227 --? 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))
230 function edit.clean_up_mouse_press(State)
231 if State.mousepress_shift then
232 if State.old_selection1.line == nil then
233 State.selection1 = State.old_cursor1
234 else
235 State.selection1 = State.old_selection1
238 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
239 if eq(State.cursor1, State.selection1) then
240 State.selection1 = {}
244 function edit.mouse_wheel_move(State, dx,dy)
245 if dy > 0 then
246 State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
247 for i=1,math.floor(dy) do
248 Text.up(State)
250 elseif dy < 0 then
251 State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
252 for i=1,math.floor(-dy) do
253 Text.down(State)
258 function edit.text_input(State, t)
259 --? print('text input', t)
260 if State.search_term then
261 State.search_term = State.search_term..t
262 Text.search_next(State)
263 else
264 Text.text_input(State, t)
266 schedule_save(State)
269 function edit.keychord_press(State, chord, key)
270 if State.selection1.line and
271 -- printable character created using shift key => delete selection
272 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
273 (not App.shift_down() or utf8.len(key) == 1) and
274 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
275 Text.delete_selection(State, State.left, State.right)
277 if State.search_term then
278 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
279 if chord == 'escape' then
280 State.search_term = nil
281 State.cursor1 = State.search_backup.cursor
282 State.screen_top1 = State.search_backup.screen_top
283 State.search_backup = nil
284 Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
285 elseif chord == 'return' then
286 State.search_term = nil
287 State.search_backup = nil
288 elseif chord == 'backspace' then
289 local len = utf8.len(State.search_term)
290 local byte_offset = Text.offset(State.search_term, len)
291 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
292 elseif chord == 'down' then
293 State.cursor1.pos = State.cursor1.pos+1
294 Text.search_next(State)
295 elseif chord == 'up' then
296 Text.search_previous(State)
298 return
299 elseif chord == 'C-f' then
300 State.search_term = ''
301 State.search_backup = {
302 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
303 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
305 -- zoom
306 elseif chord == 'C-=' then
307 edit.update_font_settings(State, State.font_height+2)
308 Text.redraw_all(State)
309 elseif chord == 'C--' then
310 if State.font_height > 2 then
311 edit.update_font_settings(State, State.font_height-2)
312 Text.redraw_all(State)
314 elseif chord == 'C-0' then
315 edit.update_font_settings(State, 20)
316 Text.redraw_all(State)
317 -- undo
318 elseif chord == 'C-z' then
319 local event = undo_event(State)
320 if event then
321 local src = event.before
322 State.screen_top1 = deepcopy(src.screen_top)
323 State.cursor1 = deepcopy(src.cursor)
324 State.selection1 = deepcopy(src.selection)
325 patch(State.lines, event.after, event.before)
326 patch_placeholders(State.line_cache, event.after, event.before)
327 -- if we're scrolling, reclaim all fragments to avoid memory leaks
328 Text.redraw_all(State)
329 schedule_save(State)
331 elseif chord == 'C-y' then
332 local event = redo_event(State)
333 if event then
334 local src = event.after
335 State.screen_top1 = deepcopy(src.screen_top)
336 State.cursor1 = deepcopy(src.cursor)
337 State.selection1 = deepcopy(src.selection)
338 patch(State.lines, event.before, event.after)
339 -- if we're scrolling, reclaim all fragments to avoid memory leaks
340 Text.redraw_all(State)
341 schedule_save(State)
343 -- clipboard
344 elseif chord == 'C-a' then
345 State.selection1 = {line=1, pos=1}
346 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
347 elseif chord == 'C-c' then
348 local s = Text.selection(State)
349 if s then
350 App.set_clipboard(s)
352 elseif chord == 'C-x' then
353 local s = Text.cut_selection(State, State.left, State.right)
354 if s then
355 App.set_clipboard(s)
357 schedule_save(State)
358 elseif chord == 'C-v' then
359 -- We don't have a good sense of when to scroll, so we'll be conservative
360 -- and sometimes scroll when we didn't quite need to.
361 local before_line = State.cursor1.line
362 local before = snapshot(State, before_line)
363 local clipboard_data = App.get_clipboard()
364 for _,code in utf8.codes(clipboard_data) do
365 local c = utf8.char(code)
366 if c == '\n' then
367 Text.insert_return(State)
368 else
369 Text.insert_at_cursor(State, c)
372 if Text.cursor_out_of_screen(State) then
373 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
375 schedule_save(State)
376 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
377 -- dispatch to text
378 else
379 for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
380 Text.keychord_press(State, chord)
384 function edit.key_release(State, key, scancode)
387 function edit.update_font_settings(State, font_height)
388 State.font_height = font_height
389 State.font = love.graphics.newFont(State.font_height)
390 State.line_height = math.floor(font_height*1.3)
393 --== some methods for tests
395 -- Insulate tests from some key globals so I don't have to change the vast
396 -- majority of tests when they're modified for the real app.
397 Test_margin_left = 25
398 Test_margin_right = 0
400 function edit.initialize_test_state()
401 -- if you change these values, tests will start failing
402 return edit.initialize_state(
403 15, -- top margin
404 Test_margin_left,
405 App.screen.width - Test_margin_right,
406 love.graphics.getFont(),
408 15) -- line height
411 -- all text_input events are also keypresses
412 -- TODO: handle chords of multiple keys
413 function edit.run_after_text_input(State, t)
414 edit.keychord_press(State, t)
415 edit.text_input(State, t)
416 edit.key_release(State, t)
417 App.screen.contents = {}
418 edit.update(State, 0)
419 edit.draw(State)
422 -- not all keys are text_input
423 function edit.run_after_keychord(State, chord, key)
424 edit.keychord_press(State, chord, key)
425 edit.key_release(State, key)
426 App.screen.contents = {}
427 edit.update(State, 0)
428 edit.draw(State)
431 function edit.run_after_mouse_click(State, x,y, mouse_button)
432 App.fake_mouse_press(x,y, mouse_button)
433 edit.mouse_press(State, x,y, mouse_button)
434 App.fake_mouse_release(x,y, mouse_button)
435 edit.mouse_release(State, x,y, mouse_button)
436 App.screen.contents = {}
437 edit.update(State, 0)
438 edit.draw(State)
441 function edit.run_after_mouse_press(State, x,y, mouse_button)
442 App.fake_mouse_press(x,y, mouse_button)
443 edit.mouse_press(State, x,y, mouse_button)
444 App.screen.contents = {}
445 edit.update(State, 0)
446 edit.draw(State)
449 function edit.run_after_mouse_release(State, x,y, mouse_button)
450 App.fake_mouse_release(x,y, mouse_button)
451 edit.mouse_release(State, x,y, mouse_button)
452 App.screen.contents = {}
453 edit.update(State, 0)
454 edit.draw(State)