some more renames
[view.love.git] / edit.lua
blob12e2053e7952f409f64dfd782a7e026a8abf8736
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.put_cursor_on_next_line_wrapping_around_if_necessary(State)
101 local line = State.cursor1.line
102 local max = #State.lines
103 for _ = 1, max-1 do
104 line = (line+1) % max
105 if State.lines[line].mode == 'text' then
106 State.cursor1.line = line
107 State.cursor1.pos = 1
108 break
113 function edit.put_cursor_on_next_loc_wrapping_around_if_necessary(State)
114 local cursor_line = State.lines[State.cursor1.line].data
115 if State.cursor1.pos <= utf8.len(cursor_line) then
116 State.cursor1.pos = State.cursor1.pos + 1
117 else
118 edit.put_cursor_on_next_line_wrapping_around_if_necessary(State)
122 function edit.draw(State)
123 love.graphics.setFont(State.font)
124 App.color(Text_color)
125 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))
126 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))
127 State.cursor_x = nil
128 State.cursor_y = nil
129 local y = State.top
130 --? print('== draw')
131 for line_index = State.screen_top1.line,#State.lines do
132 local line = State.lines[line_index]
133 --? print('draw:', y, line_index, line)
134 if y + State.line_height > App.screen.height then break end
135 --? print('text.draw', y, line_index)
136 local startpos = 1
137 if line_index == State.screen_top1.line then
138 startpos = State.screen_top1.pos
140 y = Text.draw(State, line_index, y, startpos)
141 --? print('=> y', y)
143 if State.search_term then
144 Text.draw_search_bar(State)
148 function edit.update(State, dt)
149 if State.next_save and State.next_save < Current_time then
150 save_to_disk(State)
151 State.next_save = nil
155 function schedule_save(State)
156 if State.next_save == nil then
157 State.next_save = Current_time + 3 -- short enough that you're likely to still remember what you did
161 function edit.quit(State)
162 -- make sure to save before quitting
163 if State.next_save then
164 save_to_disk(State)
165 -- give some time for the OS to flush everything to disk
166 love.timer.sleep(0.1)
170 function edit.mouse_press(State, x,y, mouse_button)
171 if State.search_term then return end
172 State.mouse_down = mouse_button
173 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
174 if y < State.top then
175 State.old_cursor1 = State.cursor1
176 State.old_selection1 = State.selection1
177 State.mousepress_shift = App.shift_down()
178 State.selection1 = {
179 line=State.screen_top1.line,
180 pos=State.screen_top1.pos,
182 return
185 for line_index,line in ipairs(State.lines) do
186 if Text.in_line(State, line_index, x,y) then
187 -- delicate dance between cursor, selection and old cursor/selection
188 -- scenarios:
189 -- regular press+release: sets cursor, clears selection
190 -- shift press+release:
191 -- sets selection to old cursor if not set otherwise leaves it untouched
192 -- sets cursor
193 -- press and hold to start a selection: sets selection on press, cursor on release
194 -- press and hold, then press shift: ignore shift
195 -- i.e. mouse_release should never look at shift state
196 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
197 State.old_cursor1 = State.cursor1
198 State.old_selection1 = State.selection1
199 State.mousepress_shift = App.shift_down()
200 State.selection1 = {
201 line=line_index,
202 pos=Text.to_pos_on_line(State, line_index, x, y),
204 return
208 -- still here? mouse press is below all screen lines
209 State.old_cursor1 = State.cursor1
210 State.old_selection1 = State.selection1
211 State.mousepress_shift = App.shift_down()
212 State.selection1 = Text.final_loc_on_screen(State)
215 function edit.mouse_release(State, x,y, mouse_button)
216 if State.search_term then return end
217 --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
218 State.mouse_down = nil
219 if y < State.top then
220 State.cursor1 = deepcopy(State.screen_top1)
221 edit.clean_up_mouse_press(State)
222 return
224 for line_index,line in ipairs(State.lines) do
225 if Text.in_line(State, line_index, x,y) then
226 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
227 State.cursor1 = {
228 line=line_index,
229 pos=Text.to_pos_on_line(State, line_index, x, y),
231 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
232 edit.clean_up_mouse_press(State)
233 return
237 -- still here? mouse release is below all screen lines
238 State.cursor1 = Text.final_loc_on_screen(State)
239 edit.clean_up_mouse_press(State)
240 --? 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))
243 function edit.clean_up_mouse_press(State)
244 if State.mousepress_shift then
245 if State.old_selection1.line == nil then
246 State.selection1 = State.old_cursor1
247 else
248 State.selection1 = State.old_selection1
251 State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
252 if eq(State.cursor1, State.selection1) then
253 State.selection1 = {}
257 function edit.mouse_wheel_move(State, dx,dy)
258 if dy > 0 then
259 State.cursor1 = deepcopy(State.screen_top1)
260 for i=1,math.floor(dy) do
261 Text.up(State)
263 elseif dy < 0 then
264 State.cursor1 = Text.screen_bottom1(State)
265 for i=1,math.floor(-dy) do
266 Text.down(State)
271 function edit.text_input(State, t)
272 --? print('text input', t)
273 if State.search_term then
274 State.search_term = State.search_term..t
275 Text.search_next(State)
276 else
277 Text.text_input(State, t)
279 schedule_save(State)
282 function edit.keychord_press(State, chord, key)
283 if State.selection1.line and
284 -- printable character created using shift key => delete selection
285 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
286 (not App.shift_down() or utf8.len(key) == 1) and
287 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
288 Text.delete_selection_and_record_undo_event(State)
290 if State.search_term then
291 if chord == 'escape' then
292 State.search_term = nil
293 State.cursor1 = State.search_backup.cursor
294 State.screen_top1 = State.search_backup.screen_top
295 State.search_backup = nil
296 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
297 elseif chord == 'return' then
298 State.search_term = nil
299 State.search_backup = nil
300 elseif chord == 'backspace' then
301 local len = utf8.len(State.search_term)
302 local byte_offset = Text.offset(State.search_term, len)
303 State.search_term = string.sub(State.search_term, 1, byte_offset-1)
304 State.cursor = deepcopy(State.search_backup.cursor)
305 State.screen_top = deepcopy(State.search_backup.screen_top)
306 Text.search_next(State)
307 elseif chord == 'down' then
308 if #State.search_term > 0 then
309 edit.put_cursor_on_next_loc_wrapping_around_if_necessary(State)
310 Text.search_next(State)
312 elseif chord == 'up' then
313 Text.search_previous(State)
315 return
316 elseif chord == 'C-f' then
317 State.search_term = ''
318 State.search_backup = {
319 cursor={line=State.cursor1.line, pos=State.cursor1.pos},
320 screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
322 -- zoom
323 elseif chord == 'C-=' then
324 edit.update_font_settings(State, State.font_height+2)
325 Text.redraw_all(State)
326 elseif chord == 'C--' then
327 if State.font_height > 2 then
328 edit.update_font_settings(State, State.font_height-2)
329 Text.redraw_all(State)
331 elseif chord == 'C-0' then
332 edit.update_font_settings(State, 20)
333 Text.redraw_all(State)
334 -- undo
335 elseif chord == 'C-z' then
336 local event = undo_event(State)
337 if event then
338 local src = event.before
339 State.screen_top1 = deepcopy(src.screen_top)
340 State.cursor1 = deepcopy(src.cursor)
341 State.selection1 = deepcopy(src.selection)
342 patch(State.lines, event.after, event.before)
343 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
344 schedule_save(State)
346 elseif chord == 'C-y' then
347 local event = redo_event(State)
348 if event then
349 local src = event.after
350 State.screen_top1 = deepcopy(src.screen_top)
351 State.cursor1 = deepcopy(src.cursor)
352 State.selection1 = deepcopy(src.selection)
353 patch(State.lines, event.before, event.after)
354 Text.redraw_all(State) -- if we're scrolling, reclaim all line caches to avoid memory leaks
355 schedule_save(State)
357 -- clipboard
358 elseif chord == 'C-a' then
359 State.selection1 = {line=1, pos=1}
360 State.cursor1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
361 elseif chord == 'C-c' then
362 local s = Text.selection(State)
363 if s then
364 App.set_clipboard(s)
366 elseif chord == 'C-x' then
367 local s = Text.cut_selection_and_record_undo_event(State)
368 if s then
369 App.set_clipboard(s)
371 schedule_save(State)
372 elseif chord == 'C-v' then
373 -- We don't have a good sense of when to scroll, so we'll be conservative
374 -- and sometimes scroll when we didn't quite need to.
375 local before_line = State.cursor1.line
376 local before = snapshot(State, before_line)
377 local clipboard_data = App.get_clipboard()
378 for _,code in utf8.codes(clipboard_data) do
379 local c = utf8.char(code)
380 if c == '\n' then
381 Text.insert_return(State)
382 else
383 Text.insert_at_cursor(State, c)
386 if Text.cursor_out_of_screen(State) then
387 Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
389 record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
390 schedule_save(State)
391 else
392 Text.keychord_press(State, chord)
396 function edit.key_release(State, key, scancode)
399 function edit.update_font_settings(State, font_height, font)
400 State.font_height = font_height
401 State.font = font or love.graphics.newFont(State.font_height)
402 State.line_height = math.floor(font_height*1.3)
405 --== some methods for tests
407 -- Insulate tests from some key globals so I don't have to change the vast
408 -- majority of tests when they're modified for the real app.
409 Test_margin_left = 25
410 Test_margin_right = 0
412 function edit.initialize_test_state()
413 -- if you change these values, tests will start failing
414 return edit.initialize_state(
415 15, -- top margin
416 Test_margin_left,
417 App.screen.width - Test_margin_right,
418 love.graphics.getFont(),
420 15) -- line height
423 -- all text_input events are also keypresses
424 -- TODO: handle chords of multiple keys
425 function edit.run_after_text_input(State, t)
426 edit.keychord_press(State, t)
427 edit.text_input(State, t)
428 edit.key_release(State, t)
429 App.screen.contents = {}
430 edit.update(State, 0)
431 edit.draw(State)
434 -- not all keys are text_input
435 function edit.run_after_keychord(State, chord, key)
436 edit.keychord_press(State, chord, key)
437 edit.key_release(State, key)
438 App.screen.contents = {}
439 edit.update(State, 0)
440 edit.draw(State)
443 function edit.run_after_mouse_click(State, x,y, mouse_button)
444 App.fake_mouse_press(x,y, mouse_button)
445 edit.mouse_press(State, x,y, mouse_button)
446 edit.draw(State)
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)
454 function edit.run_after_mouse_press(State, x,y, mouse_button)
455 App.fake_mouse_press(x,y, mouse_button)
456 edit.mouse_press(State, x,y, mouse_button)
457 App.screen.contents = {}
458 edit.update(State, 0)
459 edit.draw(State)
462 function edit.run_after_mouse_release(State, x,y, mouse_button)
463 App.fake_mouse_release(x,y, mouse_button)
464 edit.mouse_release(State, x,y, mouse_button)
465 App.screen.contents = {}
466 edit.update(State, 0)
467 edit.draw(State)