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_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 -- 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
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
41 -- some extra state to compute selection between mouse press and release
44 mousepress_shift
= nil,
46 -- 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 -- App.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 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
))
108 local screen_bottom1
= {line
=nil, pos
=nil}
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 screen_bottom1
.line
= line_index
115 --? print('text.draw', y, line_index)
117 if line_index
== State
.screen_top1
.line
then
118 startpos
= State
.screen_top1
.pos
120 y
, screen_bottom1
.pos
= Text
.draw(State
, line_index
, y
, startpos
)
123 State
.screen_bottom1
= screen_bottom1
124 if State
.search_term
then
125 Text
.draw_search_bar(State
)
130 function edit
.update(State
, dt
)
131 if State
.next_save
and State
.next_save
< Current_time
then
133 State
.next_save
= nil
137 function schedule_save(State
)
138 if State
.next_save
== nil then
139 State
.next_save
= Current_time
+ 3 -- short enough that you're likely to still remember what you did
143 function edit
.quit(State
)
144 -- make sure to save before quitting
145 if State
.next_save
then
147 -- give some time for the OS to flush everything to disk
148 love
.timer
.sleep(0.1)
152 function edit
.mouse_press(State
, x
,y
, mouse_button
)
153 if State
.search_term
then return end
154 State
.mouse_down
= mouse_button
155 --? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor1.line, State.cursor1.pos))
156 if y
< State
.top
then
157 State
.old_cursor1
= State
.cursor1
158 State
.old_selection1
= State
.selection1
159 State
.mousepress_shift
= App
.shift_down()
161 line
=State
.screen_top1
.line
,
162 pos
=State
.screen_top1
.pos
,
167 for line_index
,line
in ipairs(State
.lines
) do
168 if Text
.in_line(State
, line_index
, x
,y
) then
169 -- delicate dance between cursor, selection and old cursor/selection
171 -- regular press+release: sets cursor, clears selection
172 -- shift press+release:
173 -- sets selection to old cursor if not set otherwise leaves it untouched
175 -- press and hold to start a selection: sets selection on press, cursor on release
176 -- press and hold, then press shift: ignore shift
177 -- i.e. mouse_release should never look at shift state
178 --? print_and_log(('edit.mouse_press: in line %d'):format(line_index))
179 State
.old_cursor1
= State
.cursor1
180 State
.old_selection1
= State
.selection1
181 State
.mousepress_shift
= App
.shift_down()
184 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
190 -- still here? mouse press is below all screen lines
191 State
.old_cursor1
= State
.cursor1
192 State
.old_selection1
= State
.selection1
193 State
.mousepress_shift
= App
.shift_down()
195 line
=State
.screen_bottom1
.line
,
196 pos
=Text
.pos_at_end_of_screen_line(State
, State
.screen_bottom1
),
200 function edit
.mouse_release(State
, x
,y
, mouse_button
)
201 if State
.search_term
then return end
202 --? print_and_log(('edit.mouse_release(%d,%d): cursor at %d,%d'):format(x,y, State.cursor1.line, State.cursor1.pos))
203 State
.mouse_down
= nil
204 if y
< State
.top
then
205 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
206 edit
.clean_up_mouse_press(State
)
209 for line_index
,line
in ipairs(State
.lines
) do
210 if Text
.in_line(State
, line_index
, x
,y
) then
211 --? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
214 pos
=Text
.to_pos_on_line(State
, line_index
, x
, y
),
216 --? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor1.line, State.cursor1.pos))
217 edit
.clean_up_mouse_press(State
)
222 -- still here? mouse release is below all screen lines
223 State
.cursor1
.line
, State
.cursor1
.pos
= State
.screen_bottom1
.line
, Text
.pos_at_end_of_screen_line(State
, State
.screen_bottom1
)
224 edit
.clean_up_mouse_press(State
)
225 --? 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))
228 function edit
.clean_up_mouse_press(State
)
229 if State
.mousepress_shift
then
230 if State
.old_selection1
.line
== nil then
231 State
.selection1
= State
.old_cursor1
233 State
.selection1
= State
.old_selection1
236 State
.old_cursor1
, State
.old_selection1
, State
.mousepress_shift
= nil
237 if eq(State
.cursor1
, State
.selection1
) then
238 State
.selection1
= {}
242 function edit
.mouse_wheel_move(State
, dx
,dy
)
244 State
.cursor1
= {line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
}
245 for i
=1,math
.floor(dy
) do
249 State
.cursor1
= {line
=State
.screen_bottom1
.line
, pos
=State
.screen_bottom1
.pos
}
250 for i
=1,math
.floor(-dy
) do
256 function edit
.text_input(State
, t
)
257 --? print('text input', t)
258 if State
.search_term
then
259 State
.search_term
= State
.search_term
..t
260 Text
.search_next(State
)
262 Text
.text_input(State
, t
)
267 function edit
.keychord_press(State
, chord
, key
)
268 if State
.selection1
.line
and
269 -- printable character created using shift key => delete selection
270 -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
271 (not App
.shift_down() or utf8
.len(key
) == 1) and
272 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(chord
) then
273 Text
.delete_selection(State
, State
.left
, State
.right
)
275 if State
.search_term
then
276 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
277 if chord
== 'escape' then
278 State
.search_term
= nil
279 State
.cursor1
= State
.search_backup
.cursor
280 State
.screen_top1
= State
.search_backup
.screen_top
281 State
.search_backup
= nil
282 Text
.redraw_all(State
) -- if we're scrolling, reclaim all fragments to avoid memory leaks
283 elseif chord
== 'return' then
284 State
.search_term
= nil
285 State
.search_backup
= nil
286 elseif chord
== 'backspace' then
287 local len
= utf8
.len(State
.search_term
)
288 local byte_offset
= Text
.offset(State
.search_term
, len
)
289 State
.search_term
= string.sub(State
.search_term
, 1, byte_offset
-1)
290 elseif chord
== 'down' then
291 State
.cursor1
.pos
= State
.cursor1
.pos
+1
292 Text
.search_next(State
)
293 elseif chord
== 'up' then
294 Text
.search_previous(State
)
297 elseif chord
== 'C-f' then
298 State
.search_term
= ''
299 State
.search_backup
= {
300 cursor
={line
=State
.cursor1
.line
, pos
=State
.cursor1
.pos
},
301 screen_top
={line
=State
.screen_top1
.line
, pos
=State
.screen_top1
.pos
},
304 elseif chord
== 'C-=' then
305 edit
.update_font_settings(State
, State
.font_height
+2)
306 Text
.redraw_all(State
)
307 elseif chord
== 'C--' then
308 if State
.font_height
> 2 then
309 edit
.update_font_settings(State
, State
.font_height
-2)
310 Text
.redraw_all(State
)
312 elseif chord
== 'C-0' then
313 edit
.update_font_settings(State
, 20)
314 Text
.redraw_all(State
)
316 elseif chord
== 'C-z' then
317 local event
= undo_event(State
)
319 local src
= event
.before
320 State
.screen_top1
= deepcopy(src
.screen_top
)
321 State
.cursor1
= deepcopy(src
.cursor
)
322 State
.selection1
= deepcopy(src
.selection
)
323 patch(State
.lines
, event
.after
, event
.before
)
324 patch_placeholders(State
.line_cache
, event
.after
, event
.before
)
325 -- if we're scrolling, reclaim all fragments to avoid memory leaks
326 Text
.redraw_all(State
)
329 elseif chord
== 'C-y' then
330 local event
= redo_event(State
)
332 local src
= event
.after
333 State
.screen_top1
= deepcopy(src
.screen_top
)
334 State
.cursor1
= deepcopy(src
.cursor
)
335 State
.selection1
= deepcopy(src
.selection
)
336 patch(State
.lines
, event
.before
, event
.after
)
337 -- if we're scrolling, reclaim all fragments to avoid memory leaks
338 Text
.redraw_all(State
)
342 elseif chord
== 'C-a' then
343 State
.selection1
= {line
=1, pos
=1}
344 State
.cursor1
= {line
=#State
.lines
, pos
=utf8
.len(State
.lines
[#State
.lines
].data
)+1}
345 elseif chord
== 'C-c' then
346 local s
= Text
.selection(State
)
350 elseif chord
== 'C-x' then
351 local s
= Text
.cut_selection(State
, State
.left
, State
.right
)
356 elseif chord
== 'C-v' then
357 -- We don't have a good sense of when to scroll, so we'll be conservative
358 -- and sometimes scroll when we didn't quite need to.
359 local before_line
= State
.cursor1
.line
360 local before
= snapshot(State
, before_line
)
361 local clipboard_data
= App
.get_clipboard()
362 for _
,code
in utf8
.codes(clipboard_data
) do
363 local c
= utf8
.char(code
)
365 Text
.insert_return(State
)
367 Text
.insert_at_cursor(State
, c
)
370 if Text
.cursor_out_of_screen(State
) then
371 Text
.snap_cursor_to_bottom_of_screen(State
, State
.left
, State
.right
)
374 record_undo_event(State
, {before
=before
, after
=snapshot(State
, before_line
, State
.cursor1
.line
)})
377 for _
,line_cache
in ipairs(State
.line_cache
) do line_cache
.starty
= nil end -- just in case we scroll
378 Text
.keychord_press(State
, chord
)
382 function edit
.key_release(State
, key
, scancode
)
385 function edit
.update_font_settings(State
, font_height
)
386 State
.font_height
= font_height
387 love
.graphics
.setFont(love
.graphics
.newFont(State
.font_height
))
388 State
.line_height
= math
.floor(font_height
*1.3)
391 --== some methods for tests
393 -- Insulate tests from some key globals so I don't have to change the vast
394 -- majority of tests when they're modified for the real app.
395 Test_margin_left
= 25
396 Test_margin_right
= 0
398 function edit
.initialize_test_state()
399 -- if you change these values, tests will start failing
400 return edit
.initialize_state(
403 App
.screen
.width
- Test_margin_right
,
404 14, -- font height assuming default LÖVE font
408 -- all text_input events are also keypresses
409 -- TODO: handle chords of multiple keys
410 function edit
.run_after_text_input(State
, t
)
411 edit
.keychord_press(State
, t
)
412 edit
.text_input(State
, t
)
413 edit
.key_release(State
, t
)
414 App
.screen
.contents
= {}
415 edit
.update(State
, 0)
419 -- not all keys are text_input
420 function edit
.run_after_keychord(State
, chord
)
421 edit
.keychord_press(State
, chord
)
422 edit
.key_release(State
, chord
)
423 App
.screen
.contents
= {}
424 edit
.update(State
, 0)
428 function edit
.run_after_mouse_click(State
, x
,y
, mouse_button
)
429 App
.fake_mouse_press(x
,y
, mouse_button
)
430 edit
.mouse_press(State
, x
,y
, mouse_button
)
431 App
.fake_mouse_release(x
,y
, mouse_button
)
432 edit
.mouse_release(State
, x
,y
, mouse_button
)
433 App
.screen
.contents
= {}
434 edit
.update(State
, 0)
438 function edit
.run_after_mouse_press(State
, x
,y
, mouse_button
)
439 App
.fake_mouse_press(x
,y
, mouse_button
)
440 edit
.mouse_press(State
, x
,y
, mouse_button
)
441 App
.screen
.contents
= {}
442 edit
.update(State
, 0)
446 function edit
.run_after_mouse_release(State
, x
,y
, mouse_button
)
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)