Merge lines.love
[view.love.git] / edit.lua
blob97860b7a3ed43821c911e1c04d62485e1546d1b9
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_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_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 if #State.search_term > 0 then
287 Text.right(State)
288 Text.search_next(State)
290 elseif chord == 'up' then
291 Text.search_previous(State)
293 return
294 elseif chord == 'C-f' then
295 State.search_term = ''
296 State.search_backup = {
297 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
298 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
300 -- zoom
301 elseif chord == 'C-=' then
302 edit.update_font_settings(State, State.font_height+2)
303 Text.redraw_all(State)
304 elseif chord == 'C--' then
305 if State.font_height > 2 then
306 edit.update_font_settings(State, State.font_height-2)
307 Text.redraw_all(State)
309 elseif chord == 'C-0' then
310 edit.update_font_settings(State, 20)
311 Text.redraw_all(State)
312 -- undo
313 elseif chord == 'C-z' then
314 local event = undo_event(State)
315 if event then
316 local src = event.before
317 State.screen_top1 = deepcopy(src.screen_top)
318 State.cursor1 = deepcopy(src.cursor)
319 State.selection1 = deepcopy(src.selection)
320 patch(State.lines, event.after, event.before)
321 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
322 schedule_save(State)
324 elseif chord == 'C-y' then
325 local event = redo_event(State)
326 if event then
327 local src = event.after
328 State.screen_top1 = deepcopy(src.screen_top)
329 State.cursor1 = deepcopy(src.cursor)
330 State.selection1 = deepcopy(src.selection)
331 patch(State.lines, event.before, event.after)
332 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
333 schedule_save(State)
335 -- clipboard
336 elseif chord == 'C-a' then
337 State.selection1 = {line=1, pos=1}
338 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
339 elseif chord == 'C-c' then
340 local s = Text.selection(State)
341 if s then
342 App.set_clipboard(s)
344 elseif chord == 'C-x' then
345 local s = Text.cut_selection_and_record_undo_event(State)
346 if s then
347 App.set_clipboard(s)
349 schedule_save(State)
350 elseif chord == 'C-v' then
351 -- We don't have a good sense of when to scroll, so we'll be conservative
352 -- and sometimes scroll when we didn't quite need to.
353 local before_line = State.cursor1.line
354 local before = snapshot(State, before_line)
355 local clipboard_data = App.get_clipboard()
356 for _,code in utf8.codes(clipboard_data) do
357 local c = utf8.char(code)
358 if c == '\n' then
359 Text.insert_return(State)
360 else
361 Text.insert_at_cursor(State, c)
364 if Text.cursor_out_of_screen(State) then
365 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
367 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
368 schedule_save(State)
369 else
370 Text.keychord_press(State, chord)
374 function edit.key_release(State, key, scancode)
377 function edit.update_font_settings(State, font_height, font)
378 State.font_height = font_height
379 State.font = font or love.graphics.newFont(State.font_height)
380 State.line_height = math.floor(font_height*1.3)
383 --== some methods for tests
385 -- Insulate tests from some key globals so I don't have to change the vast
386 -- majority of tests when they're modified for the real app.
387 Test_margin_left = 25
388 Test_margin_right = 0
390 function edit.initialize_test_state()
391 -- if you change these values, tests will start failing
392 return edit.initialize_state(
393 15, -- top margin
394 Test_margin_left,
395 App.screen.width - Test_margin_right,
396 love.graphics.getFont(),
398 15) -- line height
401 -- all text_input events are also keypresses
402 -- TODO: handle chords of multiple keys
403 function edit.run_after_text_input(State, t)
404 edit.keychord_press(State, t)
405 edit.text_input(State, t)
406 edit.key_release(State, t)
407 App.screen.contents = {}
408 edit.update(State, 0)
409 edit.draw(State)
412 -- not all keys are text_input
413 function edit.run_after_keychord(State, chord, key)
414 edit.keychord_press(State, chord, key)
415 edit.key_release(State, key)
416 App.screen.contents = {}
417 edit.update(State, 0)
418 edit.draw(State)
421 function edit.run_after_mouse_click(State, x,y, mouse_button)
422 App.fake_mouse_press(x,y, mouse_button)
423 edit.mouse_press(State, x,y, mouse_button)
424 edit.draw(State)
425 App.fake_mouse_release(x,y, mouse_button)
426 edit.mouse_release(State, x,y, mouse_button)
427 App.screen.contents = {}
428 edit.update(State, 0)
429 edit.draw(State)
432 function edit.run_after_mouse_press(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.screen.contents = {}
436 edit.update(State, 0)
437 edit.draw(State)
440 function edit.run_after_mouse_release(State, x,y, mouse_button)
441 App.fake_mouse_release(x,y, mouse_button)
442 edit.mouse_release(State, x,y, mouse_button)
443 App.screen.contents = {}
444 edit.update(State, 0)
445 edit.draw(State)