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
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
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
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
40 -- some extra state to compute selection between mouse press and release
43 mousepress_shift
= nil,
45 -- cursor coordinates in pixels
50 font_height
= font_height
,
51 line_height
= line_height
,
54 left
= math
.floor(left
),
55 right
= math
.floor(right
),
58 filename
= love
.filesystem
.getSourceBaseDirectory()..'/lines.txt', -- '/' should work even on Windows
67 search_backup
= nil, -- stuff to restore when cancelling search
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}
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
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
100 -- return y drawn until
101 function edit
.draw(State
)
102 love
.graphics
.setFont(State
.font
)
103 App
.color(Text_color
)
104 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
))
105 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
))
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 --? print('text.draw', y, line_index)
116 if line_index
== State
.screen_top1
.line
then
117 startpos
= State
.screen_top1
.pos
119 y
= Text
.draw(State
, line_index
, y
, startpos
)
122 if State
.search_term
then
123 Text
.draw_search_bar(State
)
128 function edit
.update(State
, dt
)
129 if State
.next_save
and State
.next_save
< Current_time
then
131 State
.next_save
= nil
135 function schedule_save(State
)
136 if State
.next_save
== nil then
137 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
141 function edit
.quit(State
)
142 -- make sure to save before quitting
143 if State
.next_save
then
145 -- give some time for the OS to flush everything to disk
146 love
.timer
.sleep(0.1)
150 function edit
.mouse_press(State
, x
,y
, mouse_button
)
151 love
.keyboard
.setTextInput(true) -- bring up keyboard on touch screen
152 if State
.search_term
then return end
153 State
.mouse_down
= mouse_button
154 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
155 if y
< State
.top
then
156 State
.old_cursor1
= State
.cursor1
157 State
.old_selection1
= State
.selection1
158 State
.mousepress_shift
= App
.shift_down()
160 line
=State
.screen_top1
.line
,
161 pos
=State
.screen_top1
.pos
,
166 for line_index
,line
in ipairs(State
.lines
) do
167 if Text
.in_line(State
, line_index
, x
,y
) then
168 -- delicate dance between cursor, selection and old cursor/selection
170 -- regular press+release: sets cursor, clears selection
171 -- shift press+release:
172 -- sets selection to old cursor if not set otherwise leaves it untouched
174 -- press and hold to start a selection: sets selection on press, cursor on release
175 -- press and hold, then press shift: ignore shift
176 -- i.e. mouse_release should never look at shift state
177 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
178 State
.old_cursor1
= State
.cursor1
179 State
.old_selection1
= State
.selection1
180 State
.mousepress_shift
= App
.shift_down()
183 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
189 -- still here? mouse press is below all screen lines
190 State
.old_cursor1
= State
.cursor1
191 State
.old_selection1
= State
.selection1
192 State
.mousepress_shift
= App
.shift_down()
193 State
.selection1
= Text
.final_text_loc_on_screen(State
)
196 function edit
.mouse_release(State
, x
,y
, mouse_button
)
197 if State
.search_term
then return end
198 --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
199 State
.mouse_down
= nil
200 if y
< State
.top
then
201 State
.cursor1
= deepcopy(State
.screen_top1
)
202 edit
.clean_up_mouse_press(State
)
205 for line_index
,line
in ipairs(State
.lines
) do
206 if Text
.in_line(State
, line_index
, x
,y
) then
207 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
210 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
212 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
213 edit
.clean_up_mouse_press(State
)
218 -- still here? mouse release is below all screen lines
219 State
.cursor1
= Text
.final_text_loc_on_screen(State
)
220 edit
.clean_up_mouse_press(State
)
221 --? 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))
224 function edit
.clean_up_mouse_press(State
)
225 if State
.mousepress_shift
then
226 if State
.old_selection1
.line
== nil then
227 State
.selection1
= State
.old_cursor1
229 State
.selection1
= State
.old_selection1
232 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
233 if eq(State
.cursor1
, State
.selection1
) then
234 State
.selection1
= {}
238 function edit
.mouse_wheel_move(State
, dx
,dy
)
240 State
.cursor1
= deepcopy(State
.screen_top1
)
241 for i
=1,math
.floor(dy
) do
245 State
.cursor1
= Text
.screen_bottom1(State
)
246 for i
=1,math
.floor(-dy
) do
252 function edit
.text_input(State
, t
)
253 --? print('text input', t)
254 if State
.search_term
then
255 State
.search_term
= State
.search_term
..t
256 Text
.search_next(State
)
258 Text
.text_input(State
, t
)
263 function edit
.keychord_press(State
, chord
, key
)
264 if State
.selection1
.line
and
265 -- printable character created using shift key => delete selection
266 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
267 (not App
.shift_down() or utf8
.len(key
) == 1) and
268 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
269 Text
.delete_selection(State
, State
.left
, State
.right
)
271 if State
.search_term
then
272 if chord
== 'escape' then
273 State
.search_term
= nil
274 State
.cursor1
= State
.search_backup
.cursor
275 State
.screen_top1
= State
.search_backup
.screen_top
276 State
.search_backup
= nil
277 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
278 elseif chord
== 'return' then
279 State
.search_term
= nil
280 State
.search_backup
= nil
281 elseif chord
== 'backspace' then
282 local len
= utf8
.len(State
.search_term
)
283 local byte_offset
= Text
.offset(State
.search_term
, len
)
284 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
285 State
.cursor
= deepcopy(State
.search_backup
.cursor
)
286 State
.screen_top
= deepcopy(State
.search_backup
.screen_top
)
287 Text
.search_next(State
)
288 elseif chord
== 'down' then
289 State
.cursor1
.pos
= State
.cursor1
.pos
+1
290 Text
.search_next(State
)
291 elseif chord
== 'up' then
292 Text
.search_previous(State
)
295 elseif chord
== 'C-f' then
296 State
.search_term
= ''
297 State
.search_backup
= {
298 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
299 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
302 elseif chord
== 'C-=' then
303 edit
.update_font_settings(State
, State
.font_height
+2)
304 Text
.redraw_all(State
)
305 elseif chord
== 'C--' then
306 if State
.font_height
> 2 then
307 edit
.update_font_settings(State
, State
.font_height
-2)
308 Text
.redraw_all(State
)
310 elseif chord
== 'C-0' then
311 edit
.update_font_settings(State
, 20)
312 Text
.redraw_all(State
)
314 elseif chord
== 'C-z' then
315 local event
= undo_event(State
)
317 local src
= event
.before
318 State
.screen_top1
= deepcopy(src
.screen_top
)
319 State
.cursor1
= deepcopy(src
.cursor
)
320 State
.selection1
= deepcopy(src
.selection
)
321 patch(State
.lines
, event
.after
, event
.before
)
322 patch_placeholders(State
.line_cache
, event
.after
, event
.before
)
323 -- if we're scrolling, reclaim all fragments to avoid memory leaks
324 Text
.redraw_all(State
)
327 elseif chord
== 'C-y' then
328 local event
= redo_event(State
)
330 local src
= event
.after
331 State
.screen_top1
= deepcopy(src
.screen_top
)
332 State
.cursor1
= deepcopy(src
.cursor
)
333 State
.selection1
= deepcopy(src
.selection
)
334 patch(State
.lines
, event
.before
, event
.after
)
335 -- if we're scrolling, reclaim all fragments to avoid memory leaks
336 Text
.redraw_all(State
)
340 elseif chord
== 'C-a' then
341 State
.selection1
= {line
=1, pos
=1}
342 State
.cursor1
= {line
=#State
.lines
, pos
=utf8
.len(State
.lines
[#State
.lines
].data
)+1}
343 elseif chord
== 'C-c' then
344 local s
= Text
.selection(State
)
348 elseif chord
== 'C-x' then
349 local s
= Text
.cut_selection(State
, State
.left
, State
.right
)
354 elseif chord
== 'C-v' then
355 -- We don't have a good sense of when to scroll, so we'll be conservative
356 -- and sometimes scroll when we didn't quite need to.
357 local before_line
= State
.cursor1
.line
358 local before
= snapshot(State
, before_line
)
359 local clipboard_data
= App
.get_clipboard()
360 for _
,code
in utf8
.codes(clipboard_data
) do
361 local c
= utf8
.char(code
)
363 Text
.insert_return(State
)
365 Text
.insert_at_cursor(State
, c
)
368 if Text
.cursor_out_of_screen(State
) then
369 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
372 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
375 Text
.keychord_press(State
, chord
)
379 function edit
.key_release(State
, key
, scancode
)
382 function edit
.update_font_settings(State
, font_height
)
383 State
.font_height
= font_height
384 State
.font
= love
.graphics
.newFont(State
.font_height
)
385 State
.line_height
= math
.floor(font_height
*1.3)
388 --== some methods for tests
390 -- Insulate tests from some key globals so I don't have to change the vast
391 -- majority of tests when they're modified for the real app.
392 Test_margin_left
= 25
393 Test_margin_right
= 0
395 function edit
.initialize_test_state()
396 -- if you change these values, tests will start failing
397 return edit
.initialize_state(
400 App
.screen
.width
- Test_margin_right
,
401 love
.graphics
.getFont(),
406 -- all text_input events are also keypresses
407 -- TODO: handle chords of multiple keys
408 function edit
.run_after_text_input(State
, t
)
409 edit
.keychord_press(State
, t
)
410 edit
.text_input(State
, t
)
411 edit
.key_release(State
, t
)
412 App
.screen
.contents
= {}
413 edit
.update(State
, 0)
417 -- not all keys are text_input
418 function edit
.run_after_keychord(State
, chord
, key
)
419 edit
.keychord_press(State
, chord
, key
)
420 edit
.key_release(State
, key
)
421 App
.screen
.contents
= {}
422 edit
.update(State
, 0)
426 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
427 App
.fake_mouse_press(x
,y
, mouse_button
)
428 edit
.mouse_press(State
, x
,y
, mouse_button
)
430 App
.fake_mouse_release(x
,y
, mouse_button
)
431 edit
.mouse_release(State
, x
,y
, mouse_button
)
432 App
.screen
.contents
= {}
433 edit
.update(State
, 0)
437 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
438 App
.fake_mouse_press(x
,y
, mouse_button
)
439 edit
.mouse_press(State
, x
,y
, mouse_button
)
440 App
.screen
.contents
= {}
441 edit
.update(State
, 0)
445 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
446 App
.fake_mouse_release(x
,y
, mouse_button
)
447 edit
.mouse_release(State
, x
,y
, mouse_button
)
448 App
.screen
.contents
= {}
449 edit
.update(State
, 0)