Merge lines.love
[view.love.git] / edit.lua
blob4b7a2fca56ffe6a76ccf76363bdd1c194fefd6c1
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
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 -- screen_line_starting_pos: optional array of codepoint indices if it wraps over more than one screen line
23 line_cache = {},
25 -- Given wrapping, any potential location for the text cursor can be described in two ways:
26 -- * schema 1: As a combination of line index and position within a line (in utf8 codepoint units)
27 -- * schema 2: As a combination of line index, screen line index within the line, and a position within the screen line.
29 -- Most of the time we'll only persist positions in schema 1, translating to
30 -- schema 2 when that's convenient.
32 -- Make sure these coordinates are never aliased, so that changing one causes
33 -- action at a distance.
35 -- On lines that are drawings, pos will be nil.
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; must be on a text line
39 selection1 = {},
40 -- some extra state to compute selection between mouse press and release
41 old_cursor1 = nil,
42 old_selection1 = nil,
43 mousepress_shift = nil,
45 -- cursor coordinates in pixels
46 cursor_x = 0,
47 cursor_y = 0,
49 font = font,
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 -- edit.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 function edit.draw(State)
101 love.graphics.setFont(State.font)
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 --? print('== draw')
109 for line_index = State.screen_top1.line,#State.lines do
110 local line = State.lines[line_index]
111 --? print('draw:', y, line_index, line)
112 if y + State.line_height > App.screen.height then break end
113 --? print('text.draw', y, line_index)
114 local startpos = 1
115 if line_index == State.screen_top1.line then
116 startpos = State.screen_top1.pos
118 y = Text.draw(State, line_index, y, startpos)
119 --? print('=> y', y)
121 if State.search_term then
122 Text.draw_search_bar(State)
126 function edit.update(State, dt)
127 if State.next_save and State.next_save < Current_time then
128 save_to_disk(State)
129 State.next_save = nil
133 function schedule_save(State)
134 if State.next_save == nil then
135 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
139 function edit.quit(State)
140 -- make sure to save before quitting
141 if State.next_save then
142 save_to_disk(State)
143 -- give some time for the OS to flush everything to disk
144 love.timer.sleep(0.1)
148 function edit.mouse_press(State, x,y, mouse_button)
149 if State.search_term then return end
150 State.mouse_down = mouse_button
151 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
152 if y < State.top then
153 State.old_cursor1 = State.cursor1
154 State.old_selection1 = State.selection1
155 State.mousepress_shift = App.shift_down()
156 State.selection1 = {
157 line=State.screen_top1.line,
158 pos=State.screen_top1.pos,
160 return
163 for line_index,line in ipairs(State.lines) do
164 if Text.in_line(State, line_index, x,y) then
165 -- delicate dance between cursor, selection and old cursor/selection
166 -- scenarios:
167 -- regular press+release: sets cursor, clears selection
168 -- shift press+release:
169 -- sets selection to old cursor if not set otherwise leaves it untouched
170 -- sets cursor
171 -- press and hold to start a selection: sets selection on press, cursor on release
172 -- press and hold, then press shift: ignore shift
173 -- i.e. mouse_release should never look at shift state
174 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
175 State.old_cursor1 = State.cursor1
176 State.old_selection1 = State.selection1
177 State.mousepress_shift = App.shift_down()
178 State.selection1 = {
179 line=line_index,
180 pos=Text.to_pos_on_line(State, line_index, x, y),
182 return
186 -- still here? mouse press is below all screen lines
187 State.old_cursor1 = State.cursor1
188 State.old_selection1 = State.selection1
189 State.mousepress_shift = App.shift_down()
190 State.selection1 = Text.final_text_loc_on_screen(State)
193 function edit.mouse_release(State, x,y, mouse_button)
194 if State.search_term then return end
195 --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
196 State.mouse_down = nil
197 if y < State.top then
198 State.cursor1 = deepcopy(State.screen_top1)
199 edit.clean_up_mouse_press(State)
200 return
202 for line_index,line in ipairs(State.lines) do
203 if Text.in_line(State, line_index, x,y) then
204 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
205 State.cursor1 = {
206 line=line_index,
207 pos=Text.to_pos_on_line(State, line_index, x, y),
209 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
210 edit.clean_up_mouse_press(State)
211 return
215 -- still here? mouse release is below all screen lines
216 State.cursor1 = Text.final_text_loc_on_screen(State)
217 edit.clean_up_mouse_press(State)
218 --? 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))
221 function edit.clean_up_mouse_press(State)
222 if State.mousepress_shift then
223 if State.old_selection1.line == nil then
224 State.selection1 = State.old_cursor1
225 else
226 State.selection1 = State.old_selection1
229 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
230 if eq(State.cursor1, State.selection1) then
231 State.selection1 = {}
235 function edit.mouse_wheel_move(State, dx,dy)
236 if dy > 0 then
237 State.cursor1 = deepcopy(State.screen_top1)
238 for i=1,math.floor(dy) do
239 Text.up(State)
241 elseif dy < 0 then
242 State.cursor1 = Text.screen_bottom1(State)
243 for i=1,math.floor(-dy) do
244 Text.down(State)
249 function edit.text_input(State, t)
250 --? print('text input', t)
251 if State.search_term then
252 State.search_term = State.search_term..t
253 Text.search_next(State)
254 else
255 Text.text_input(State, t)
257 schedule_save(State)
260 function edit.keychord_press(State, chord, key)
261 if State.selection1.line and
262 -- printable character created using shift key => delete selection
263 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
264 (not App.shift_down() or utf8.len(key) == 1) and
265 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
266 Text.delete_selection_and_record_undo_event(State)
268 if State.search_term then
269 if chord == 'escape' then
270 State.search_term = nil
271 State.cursor1 = State.search_backup.cursor
272 State.screen_top1 = State.search_backup.screen_top
273 State.search_backup = nil
274 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
275 elseif chord == 'return' then
276 State.search_term = nil
277 State.search_backup = nil
278 elseif chord == 'backspace' then
279 local len = utf8.len(State.search_term)
280 local byte_offset = Text.offset(State.search_term, len)
281 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
282 State.cursor = deepcopy(State.search_backup.cursor)
283 State.screen_top = deepcopy(State.search_backup.screen_top)
284 Text.search_next(State)
285 elseif chord == 'down' then
286 State.cursor1.pos = State.cursor1.pos+1
287 Text.search_next(State)
288 elseif chord == 'up' then
289 Text.search_previous(State)
291 return
292 elseif chord == 'C-f' then
293 State.search_term = ''
294 State.search_backup = {
295 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
296 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
298 -- zoom
299 elseif chord == 'C-=' then
300 edit.update_font_settings(State, State.font_height+2)
301 Text.redraw_all(State)
302 elseif chord == 'C--' then
303 if State.font_height > 2 then
304 edit.update_font_settings(State, State.font_height-2)
305 Text.redraw_all(State)
307 elseif chord == 'C-0' then
308 edit.update_font_settings(State, 20)
309 Text.redraw_all(State)
310 -- undo
311 elseif chord == 'C-z' then
312 local event = undo_event(State)
313 if event then
314 local src = event.before
315 State.screen_top1 = deepcopy(src.screen_top)
316 State.cursor1 = deepcopy(src.cursor)
317 State.selection1 = deepcopy(src.selection)
318 patch(State.lines, event.after, event.before)
319 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
320 schedule_save(State)
322 elseif chord == 'C-y' then
323 local event = redo_event(State)
324 if event then
325 local src = event.after
326 State.screen_top1 = deepcopy(src.screen_top)
327 State.cursor1 = deepcopy(src.cursor)
328 State.selection1 = deepcopy(src.selection)
329 patch(State.lines, event.before, event.after)
330 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
331 schedule_save(State)
333 -- clipboard
334 elseif chord == 'C-a' then
335 State.selection1 = {line=1, pos=1}
336 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
337 elseif chord == 'C-c' then
338 local s = Text.selection(State)
339 if s then
340 App.set_clipboard(s)
342 elseif chord == 'C-x' then
343 local s = Text.cut_selection_and_record_undo_event(State)
344 if s then
345 App.set_clipboard(s)
347 schedule_save(State)
348 elseif chord == 'C-v' then
349 -- We don't have a good sense of when to scroll, so we'll be conservative
350 -- and sometimes scroll when we didn't quite need to.
351 local before_line = State.cursor1.line
352 local before = snapshot(State, before_line)
353 local clipboard_data = App.get_clipboard()
354 for _,code in utf8.codes(clipboard_data) do
355 local c = utf8.char(code)
356 if c == '\n' then
357 Text.insert_return(State)
358 else
359 Text.insert_at_cursor(State, c)
362 if Text.cursor_out_of_screen(State) then
363 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
365 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
366 schedule_save(State)
367 else
368 Text.keychord_press(State, chord)
372 function edit.key_release(State, key, scancode)
375 function edit.update_font_settings(State, font_height, font)
376 State.font_height = font_height
377 State.font = font or love.graphics.newFont(State.font_height)
378 State.line_height = math.floor(font_height*1.3)
381 --== some methods for tests
383 -- Insulate tests from some key globals so I don't have to change the vast
384 -- majority of tests when they're modified for the real app.
385 Test_margin_left = 25
386 Test_margin_right = 0
388 function edit.initialize_test_state()
389 -- if you change these values, tests will start failing
390 return edit.initialize_state(
391 15, -- top margin
392 Test_margin_left,
393 App.screen.width - Test_margin_right,
394 love.graphics.getFont(),
396 15) -- line height
399 -- all text_input events are also keypresses
400 -- TODO: handle chords of multiple keys
401 function edit.run_after_text_input(State, t)
402 edit.keychord_press(State, t)
403 edit.text_input(State, t)
404 edit.key_release(State, t)
405 App.screen.contents = {}
406 edit.update(State, 0)
407 edit.draw(State)
410 -- not all keys are text_input
411 function edit.run_after_keychord(State, chord, key)
412 edit.keychord_press(State, chord, key)
413 edit.key_release(State, key)
414 App.screen.contents = {}
415 edit.update(State, 0)
416 edit.draw(State)
419 function edit.run_after_mouse_click(State, x,y, mouse_button)
420 App.fake_mouse_press(x,y, mouse_button)
421 edit.mouse_press(State, x,y, mouse_button)
422 edit.draw(State)
423 App.fake_mouse_release(x,y, mouse_button)
424 edit.mouse_release(State, x,y, mouse_button)
425 App.screen.contents = {}
426 edit.update(State, 0)
427 edit.draw(State)
430 function edit.run_after_mouse_press(State, x,y, mouse_button)
431 App.fake_mouse_press(x,y, mouse_button)
432 edit.mouse_press(State, x,y, mouse_button)
433 App.screen.contents = {}
434 edit.update(State, 0)
435 edit.draw(State)
438 function edit.run_after_mouse_release(State, x,y, mouse_button)
439 App.fake_mouse_release(x,y, mouse_button)
440 edit.mouse_release(State, x,y, mouse_button)
441 App.screen.contents = {}
442 edit.update(State, 0)
443 edit.draw(State)