insert a mode into locations
This commit is contained in:
parent
d41f64b8ff
commit
d780bb0175
86
edit.lua
86
edit.lua
|
@ -65,8 +65,8 @@ function edit.initialize_state(top, left, right, font, font_height, line_height)
|
|||
-- action at a distance.
|
||||
--
|
||||
-- On lines that are drawings, pos will be nil.
|
||||
screen_top = {line=1, pos=1}, -- position of start of screen line at top of screen
|
||||
cursor = {line=1, pos=1}, -- position of cursor; must be on a text line
|
||||
screen_top = {mode='text', loc1={line=1, pos=1}}, -- position of start of screen line at top of screen
|
||||
cursor = {mode='text', loc1={line=1, pos=1}}, -- position of cursor; must be on a text line
|
||||
|
||||
selection1 = {},
|
||||
-- some extra state to compute selection between mouse press and release
|
||||
|
@ -107,31 +107,33 @@ end -- edit.initialize_state
|
|||
function edit.check_locs(State)
|
||||
-- if State is inconsistent (i.e. file changed by some other program),
|
||||
-- throw away all cursor state entirely
|
||||
if edit.invalid1(State, State.screen_top)
|
||||
if edit.invalid1(State, State.screen_top.loc1)
|
||||
or edit.invalid_cursor1(State)
|
||||
or not edit.cursor_on_text(State)
|
||||
or not Text.le1(State.screen_top, State.cursor) then
|
||||
State.screen_top = {line=1, pos=1}
|
||||
State.cursor = {line=1, pos=1}
|
||||
or not Text.le1(State.screen_top.loc1, State.cursor.loc1) then
|
||||
State.screen_top.loc1 = {line=1, pos=1}
|
||||
State.cursor.loc1 = {line=1, pos=1}
|
||||
edit.put_cursor_on_next_text_line(State)
|
||||
end
|
||||
end
|
||||
|
||||
function edit.invalid1(State, loc1)
|
||||
if loc1 == nil then return true end
|
||||
if loc1.line > #State.lines then return true end
|
||||
local l = State.lines[loc1.line]
|
||||
if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
|
||||
if l.mode ~= 'text' then return false end
|
||||
return loc1.pos > #State.lines[loc1.line].data
|
||||
end
|
||||
|
||||
-- cursor loc in particular differs from other locs in one way:
|
||||
-- pos might occur just after end of line
|
||||
function edit.invalid_cursor1(State)
|
||||
local cursor = State.cursor
|
||||
if cursor.line > #State.lines then return true end
|
||||
local l = State.lines[cursor.line]
|
||||
if l.mode ~= 'text' then return false end -- pos is irrelevant to validity for a drawing line
|
||||
return cursor.pos > #State.lines[cursor.line].data + 1
|
||||
local cursor1 = State.cursor.loc1
|
||||
if cursor1 == nil then return true end
|
||||
if cursor1.line > #State.lines then return true end
|
||||
local l = State.lines[cursor1.line]
|
||||
if l.mode ~= 'text' then return false end
|
||||
return cursor1.pos > #State.lines[cursor1.line].data + 1
|
||||
end
|
||||
|
||||
function edit.cursor_on_text(State)
|
||||
|
@ -141,14 +143,14 @@ end
|
|||
|
||||
function edit.put_cursor_on_next_text_line(State)
|
||||
while true do
|
||||
if State.cursor.line >= #State.lines then
|
||||
if State.cursor.loc1.line >= #State.lines then
|
||||
break
|
||||
end
|
||||
if State.lines[State.cursor.line].mode == 'text' then
|
||||
if State.lines[State.cursor.loc1.line].mode == 'text' then
|
||||
break
|
||||
end
|
||||
State.cursor.line = State.cursor.line+1
|
||||
State.cursor.pos = 1
|
||||
State.cursor.loc1.line = State.cursor.loc1.line+1
|
||||
State.cursor.loc1.pos = 1
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -158,20 +160,20 @@ function edit.draw(State)
|
|||
love.graphics.setFont(State.font)
|
||||
App.color(Text_color)
|
||||
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))
|
||||
assert(Text.le1(State.screen_top, State.cursor), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top.line, State.screen_top.pos, State.cursor.line, State.cursor.pos))
|
||||
assert(Text.le1(State.screen_top.loc1, State.cursor.loc1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top.loc1.line, State.screen_top.loc1.pos, State.cursor.loc1.line, State.cursor.loc1.pos))
|
||||
State.cursor_x = nil
|
||||
State.cursor_y = nil
|
||||
local y = State.top
|
||||
--? print('== draw')
|
||||
for line_index = State.screen_top.line,#State.lines do
|
||||
for line_index = State.screen_top.loc1.line,#State.lines do
|
||||
local line = State.lines[line_index]
|
||||
--? print('draw:', y, line_index, line)
|
||||
if y + State.line_height > App.screen.height then break end
|
||||
if line.mode == 'text' then
|
||||
--? print('text.draw', y, line_index)
|
||||
local startpos = 1
|
||||
if line_index == State.screen_top.line then
|
||||
startpos = State.screen_top.pos
|
||||
if line_index == State.screen_top.loc1.line then
|
||||
startpos = State.screen_top.loc1.pos
|
||||
end
|
||||
if line.data == '' then
|
||||
-- button to insert new drawing
|
||||
|
@ -181,8 +183,8 @@ function edit.draw(State)
|
|||
Drawing.before = snapshot(State, line_index-1, line_index)
|
||||
table.insert(State.lines, line_index, {mode='drawing', y=y, h=256/2, points={}, shapes={}, pending={}})
|
||||
table.insert(State.line_cache, line_index, {})
|
||||
if State.cursor.line >= line_index then
|
||||
State.cursor.line = State.cursor.line+1
|
||||
if State.cursor.loc1.line >= line_index then
|
||||
State.cursor.loc1.line = State.cursor.loc1.line+1
|
||||
end
|
||||
schedule_save(State)
|
||||
record_undo_event(State, {before=Drawing.before, after=snapshot(State, line_index-1, line_index+1)})
|
||||
|
@ -232,7 +234,7 @@ function edit.mouse_press(State, x,y, mouse_button)
|
|||
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
|
||||
if State.search_term then return end
|
||||
State.mouse_down = mouse_button
|
||||
--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor.line, State.cursor.pos))
|
||||
--? print_and_log(('edit.mouse_press: cursor at %d,%d'):format(State.cursor.loc1.line, State.cursor.loc1.pos))
|
||||
if mouse_press_consumed_by_any_button(State, x,y, mouse_button) then
|
||||
-- press on a button and it returned 'true' to short-circuit
|
||||
return
|
||||
|
@ -243,8 +245,8 @@ function edit.mouse_press(State, x,y, mouse_button)
|
|||
State.old_selection1 = State.selection1
|
||||
State.mousepress_shift = App.shift_down()
|
||||
State.selection1 = {
|
||||
line=State.screen_top.line,
|
||||
pos=State.screen_top.pos,
|
||||
line=State.screen_top.loc1.line,
|
||||
pos=State.screen_top.loc1.pos,
|
||||
}
|
||||
return
|
||||
end
|
||||
|
@ -291,7 +293,7 @@ end
|
|||
|
||||
function edit.mouse_release(State, x,y, mouse_button)
|
||||
if State.search_term then return end
|
||||
--? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor.line, State.cursor.pos))
|
||||
--? print_and_log(('edit.mouse_release: cursor at %d,%d'):format(State.cursor.loc1.line, State.cursor.loc1.pos))
|
||||
State.mouse_down = nil
|
||||
if State.lines.current_drawing then
|
||||
Drawing.mouse_release(State, x,y, mouse_button)
|
||||
|
@ -303,7 +305,7 @@ function edit.mouse_release(State, x,y, mouse_button)
|
|||
else
|
||||
--? print_and_log('edit.mouse_release: no current drawing')
|
||||
if y < State.top then
|
||||
State.cursor = {line=State.screen_top.line, pos=State.screen_top.pos}
|
||||
State.cursor.loc1 = {line=State.screen_top.line, pos=State.screen_top.pos}
|
||||
edit.clean_up_mouse_press(State)
|
||||
return
|
||||
end
|
||||
|
@ -312,11 +314,11 @@ function edit.mouse_release(State, x,y, mouse_button)
|
|||
if line.mode == 'text' then
|
||||
if Text.in_line(State, line_index, x,y) then
|
||||
--? print_and_log(('edit.mouse_release: in line %d'):format(line_index))
|
||||
State.cursor = {
|
||||
State.cursor.loc1 = {
|
||||
line=line_index,
|
||||
pos=Text.to_pos_on_line(State, line_index, x, y),
|
||||
}
|
||||
--? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor.line, State.cursor.pos))
|
||||
--? print_and_log(('edit.mouse_release: cursor now %d,%d'):format(State.cursor.loc1.line, State.cursor.loc1.pos))
|
||||
edit.clean_up_mouse_press(State)
|
||||
return
|
||||
end
|
||||
|
@ -324,9 +326,9 @@ function edit.mouse_release(State, x,y, mouse_button)
|
|||
end
|
||||
|
||||
-- still here? mouse release is below all screen lines
|
||||
State.cursor = Text.final_text_loc_on_screen(State)
|
||||
State.cursor.loc1 = Text.final_text_loc_on_screen(State)
|
||||
edit.clean_up_mouse_press(State)
|
||||
--? print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor.line, State.cursor.pos))
|
||||
--? print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d'):format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor.loc1.line, State.cursor.loc1.pos))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -339,20 +341,20 @@ function edit.clean_up_mouse_press(State)
|
|||
end
|
||||
end
|
||||
State.old_cursor1, State.old_selection1, State.mousepress_shift = nil
|
||||
if Text.eq1(State.cursor, State.selection1) then
|
||||
if Text.eq1(State.cursor.loc1, State.selection1) then
|
||||
State.selection1 = {}
|
||||
end
|
||||
end
|
||||
|
||||
function edit.mouse_wheel_move(State, dx,dy)
|
||||
if dy > 0 then
|
||||
State.cursor = {line=State.screen_top.line, pos=State.screen_top.pos}
|
||||
State.cursor.loc1 = {line=State.screen_top.loc1.line, pos=State.screen_top.loc1.pos}
|
||||
edit.put_cursor_on_next_text_line(State)
|
||||
for i=1,math.floor(dy) do
|
||||
Text.up(State)
|
||||
end
|
||||
elseif dy < 0 then
|
||||
State.cursor = Text.screen_bottom1(State)
|
||||
State.cursor.loc1 = Text.screen_bottom1(State)
|
||||
edit.put_cursor_on_next_text_line(State)
|
||||
for i=1,math.floor(-dy) do
|
||||
Text.down(State)
|
||||
|
@ -392,8 +394,8 @@ function edit.keychord_press(State, chord, key)
|
|||
if State.search_term then
|
||||
if chord == 'escape' then
|
||||
State.search_term = nil
|
||||
State.cursor = State.search_backup.cursor
|
||||
State.screen_top = State.search_backup.screen_top
|
||||
State.cursor.loc1 = State.search_backup.cursor.loc1
|
||||
State.screen_top.loc1 = State.search_backup.screen_top.loc1
|
||||
State.search_backup = nil
|
||||
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
|
||||
elseif chord == 'return' then
|
||||
|
@ -404,7 +406,7 @@ function edit.keychord_press(State, chord, key)
|
|||
local byte_offset = Text.offset(State.search_term, len)
|
||||
State.search_term = string.sub(State.search_term, 1, byte_offset-1)
|
||||
elseif chord == 'down' then
|
||||
State.cursor.pos = State.cursor.pos+1
|
||||
State.cursor.loc1.pos = State.cursor.loc1.pos+1
|
||||
Text.search_next(State)
|
||||
elseif chord == 'up' then
|
||||
Text.search_previous(State)
|
||||
|
@ -413,8 +415,8 @@ function edit.keychord_press(State, chord, key)
|
|||
elseif chord == 'C-f' then
|
||||
State.search_term = ''
|
||||
State.search_backup = {
|
||||
cursor={line=State.cursor.line, pos=State.cursor.pos},
|
||||
screen_top={line=State.screen_top.line, pos=State.screen_top.pos},
|
||||
cursor={mode='text', {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}},
|
||||
screen_top={mode='text', {line=State.screen_top.loc1.line, pos=State.screen_top.loc1.pos}},
|
||||
}
|
||||
-- zoom
|
||||
elseif chord == 'C-=' then
|
||||
|
@ -461,7 +463,7 @@ function edit.keychord_press(State, chord, key)
|
|||
-- clipboard
|
||||
elseif chord == 'C-a' then
|
||||
State.selection1 = {line=1, pos=1}
|
||||
State.cursor = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
|
||||
State.cursor.loc1 = {line=#State.lines, pos=utf8.len(State.lines[#State.lines].data)+1}
|
||||
elseif chord == 'C-c' then
|
||||
local s = Text.selection(State)
|
||||
if s then
|
||||
|
@ -476,7 +478,7 @@ function edit.keychord_press(State, chord, key)
|
|||
elseif chord == 'C-v' then
|
||||
-- We don't have a good sense of when to scroll, so we'll be conservative
|
||||
-- and sometimes scroll when we didn't quite need to.
|
||||
local before_line = State.cursor.line
|
||||
local before_line = State.cursor.loc1.line
|
||||
local before = snapshot(State, before_line)
|
||||
local clipboard_data = love.system.getClipboardText()
|
||||
for _,code in utf8.codes(clipboard_data) do
|
||||
|
@ -491,7 +493,7 @@ function edit.keychord_press(State, chord, key)
|
|||
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
|
||||
end
|
||||
schedule_save(State)
|
||||
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor.line)})
|
||||
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor.loc1.line)})
|
||||
-- dispatch to drawing or text
|
||||
elseif love.mouse.isDown(1) or chord:sub(1,2) == 'C-' then
|
||||
local drawing_index, drawing = Drawing.current_drawing(State)
|
||||
|
|
8
run.lua
8
run.lua
|
@ -19,8 +19,8 @@ function run.initialize(arg)
|
|||
Editor_state.filename = arg[1]
|
||||
load_from_disk(Editor_state)
|
||||
Text.redraw_all(Editor_state)
|
||||
Editor_state.screen_top = {line=1, pos=1}
|
||||
Editor_state.cursor = {line=1, pos=1}
|
||||
Editor_state.screen_top = {mode='text', loc1={line=1, pos=1}}
|
||||
Editor_state.cursor = {mode='text', loc1={line=1, pos=1}}
|
||||
else
|
||||
load_from_disk(Editor_state)
|
||||
Text.redraw_all(Editor_state)
|
||||
|
@ -113,8 +113,8 @@ function run.file_drop(file)
|
|||
Editor_state.lines = load_from_file(file)
|
||||
file:close()
|
||||
Text.redraw_all(Editor_state)
|
||||
Editor_state.screen_top = {line=1, pos=1}
|
||||
Editor_state.cursor = {line=1, pos=1}
|
||||
Editor_state.screen_top = {mode='text', loc1={line=1, pos=1}}
|
||||
Editor_state.cursor = {mode='text', loc1={line=1, pos=1}}
|
||||
|
||||
|
||||
|
||||
|
|
76
search.lua
76
search.lua
|
@ -18,109 +18,113 @@ end
|
|||
|
||||
function Text.search_next(State)
|
||||
-- search current line from cursor
|
||||
local curr_pos = State.cursor.pos
|
||||
local curr_line = State.lines[State.cursor.line].data
|
||||
local curr_pos = State.cursor.loc1.pos
|
||||
local curr_line = State.lines[State.cursor.loc1.line].data
|
||||
local curr_offset = Text.offset(curr_line, curr_pos)
|
||||
local offset = find(curr_line, State.search_term, curr_offset, --[[literal]] true)
|
||||
if offset then
|
||||
State.cursor.pos = utf8.len(curr_line, 1, offset)
|
||||
State.cursor.loc1.pos = utf8.len(curr_line, 1, offset)
|
||||
end
|
||||
if offset == nil then
|
||||
-- search lines below cursor
|
||||
for i=State.cursor.line+1,#State.lines do
|
||||
for i=State.cursor.loc1.line+1,#State.lines do
|
||||
local curr_line = State.lines[i].data
|
||||
offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
|
||||
if offset then
|
||||
State.cursor = {line=i, pos=utf8.len(curr_line, 1, offset)}
|
||||
State.cursor = {mode='text', loc1={line=i, pos=utf8.len(curr_line, 1, offset)}}
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if offset == nil then
|
||||
-- wrap around
|
||||
for i=1,State.cursor.line-1 do
|
||||
for i=1,State.cursor.loc1.line-1 do
|
||||
local curr_line = State.lines[i].data
|
||||
offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
|
||||
if offset then
|
||||
State.cursor = {line=i, pos=utf8.len(curr_line, 1, offset)}
|
||||
State.cursor = {mode='text', {line=i, pos=utf8.len(curr_line, 1, offset)}}
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if offset == nil then
|
||||
-- search current line until cursor
|
||||
local curr_line = State.lines[State.cursor.line].data
|
||||
local curr_line = State.lines[State.cursor.loc1.line].data
|
||||
offset = find(curr_line, State.search_term, --[[from start]] nil, --[[literal]] true)
|
||||
local pos = utf8.len(curr_line, 1, offset)
|
||||
if pos and pos < State.cursor.pos then
|
||||
State.cursor.pos = pos
|
||||
if pos and pos < State.cursor.loc1.pos then
|
||||
State.cursor.loc1.pos = pos
|
||||
end
|
||||
end
|
||||
if offset == nil then
|
||||
State.cursor.line = State.search_backup.cursor.line
|
||||
State.cursor.pos = State.search_backup.cursor.pos
|
||||
State.screen_top.line = State.search_backup.screen_top.line
|
||||
State.screen_top.pos = State.search_backup.screen_top.pos
|
||||
State.cursor.mode = State.search_backup.cursor.mode
|
||||
State.cursor.loc1.line = State.search_backup.cursor.loc1.line
|
||||
State.cursor.loc1.pos = State.search_backup.cursor.loc1.pos
|
||||
State.screen_top.mode = State.search_backup.screen_top.mode
|
||||
State.screen_top.loc1.line = State.search_backup.screen_top.loc1.line
|
||||
State.screen_top.loc1.pos = State.search_backup.screen_top.loc1.pos
|
||||
end
|
||||
local screen_bottom1 = Text.screen_bottom1(State)
|
||||
if Text.lt1(State.cursor, State.screen_top) or Text.lt1(screen_bottom1, State.cursor) then
|
||||
State.screen_top.line = State.cursor.line
|
||||
local pos = Text.pos_at_start_of_screen_line(State, State.cursor)
|
||||
State.screen_top.pos = pos
|
||||
if Text.lt1(State.cursor.loc1, State.screen_top.loc1) or Text.lt1(screen_bottom1, State.cursor.loc1) then
|
||||
State.screen_top.loc1.line = State.cursor.loc1.line
|
||||
local pos = Text.pos_at_start_of_screen_line(State, State.cursor.loc1)
|
||||
State.screen_top.loc1.pos = pos
|
||||
end
|
||||
end
|
||||
|
||||
function Text.search_previous(State)
|
||||
-- search current line before cursor
|
||||
local curr_pos = State.cursor.pos
|
||||
local curr_line = State.lines[State.cursor.line].data
|
||||
local curr_pos = State.cursor.loc1.pos
|
||||
local curr_line = State.lines[State.cursor.loc1.line].data
|
||||
local curr_offset = Text.offset(curr_line, curr_pos)
|
||||
local offset = rfind(curr_line, State.search_term, curr_offset-1, --[[literal]] true)
|
||||
if offset then
|
||||
State.cursor.pos = utf8.len(curr_line, 1, offset)
|
||||
State.cursor.loc1.pos = utf8.len(curr_line, 1, offset)
|
||||
end
|
||||
if offset == nil then
|
||||
-- search lines above cursor
|
||||
for i=State.cursor.line-1,1,-1 do
|
||||
for i=State.cursor.loc1.line-1,1,-1 do
|
||||
local curr_line = State.lines[i].data
|
||||
offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
|
||||
if offset then
|
||||
State.cursor = {line=i, pos=utf8.len(curr_line, 1, offset)}
|
||||
State.cursor = {mode='text', loc1={line=i, pos=utf8.len(curr_line, 1, offset)}}
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if offset == nil then
|
||||
-- wrap around
|
||||
for i=#State.lines,State.cursor.line+1,-1 do
|
||||
for i=#State.lines,State.cursor.loc1.line+1,-1 do
|
||||
local curr_line = State.lines[i].data
|
||||
offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
|
||||
if offset then
|
||||
State.cursor = {line=i, pos=utf8.len(curr_line, 1, offset)}
|
||||
State.cursor = {mode='text', loc1={line=i, pos=utf8.len(curr_line, 1, offset)}}
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if offset == nil then
|
||||
-- search current line after cursor
|
||||
local curr_line = State.lines[State.cursor.line].data
|
||||
local curr_line = State.lines[State.cursor.loc1.line].data
|
||||
offset = rfind(curr_line, State.search_term, --[[from end]] nil, --[[literal]] true)
|
||||
local pos = utf8.len(curr_line, 1, offset)
|
||||
if pos and pos > State.cursor.pos then
|
||||
State.cursor.pos = pos
|
||||
if pos and pos > State.cursor.loc1.pos then
|
||||
State.cursor.loc1.pos = pos
|
||||
end
|
||||
end
|
||||
if offset == nil then
|
||||
State.cursor.line = State.search_backup.cursor.line
|
||||
State.cursor.pos = State.search_backup.cursor.pos
|
||||
State.screen_top.line = State.search_backup.screen_top.line
|
||||
State.screen_top.pos = State.search_backup.screen_top.pos
|
||||
State.cursor.mode = State.search_backup.cursor.mode
|
||||
State.cursor.loc1.line = State.search_backup.cursor.loc1.line
|
||||
State.cursor.loc1.pos = State.search_backup.cursor.loc1.pos
|
||||
State.screen_top.mode = State.search_backup.screen_top.mode
|
||||
State.screen_top.loc1.line = State.search_backup.screen_top.loc1.line
|
||||
State.screen_top.loc1.pos = State.search_backup.screen_top.loc1.pos
|
||||
end
|
||||
local screen_bottom1 = Text.screen_bottom1(State)
|
||||
if Text.lt1(State.cursor, State.screen_top) or Text.lt1(screen_bottom1, State.cursor) then
|
||||
State.screen_top.line = State.cursor.line
|
||||
local pos = Text.pos_at_start_of_screen_line(State, State.cursor)
|
||||
State.screen_top.pos = pos
|
||||
if Text.lt1(State.cursor.loc1, State.screen_top.loc1) or Text.lt1(screen_bottom1, State.cursor.loc1) then
|
||||
State.screen_top.loc1.line = State.cursor.loc1.line
|
||||
local pos = Text.pos_at_start_of_screen_line(State, State.cursor.loc1)
|
||||
State.screen_top.loc1.pos = pos
|
||||
end
|
||||
end
|
||||
|
||||
|
|
22
select.lua
22
select.lua
|
@ -14,7 +14,7 @@ function Text.clip_selection(State, line_index, apos, bpos)
|
|||
if State.mouse_down then
|
||||
maxl,maxp = Text.mouse_pos(State)
|
||||
else
|
||||
maxl,maxp = State.cursor.line,State.cursor.pos
|
||||
maxl,maxp = State.cursor.loc1.line,State.cursor.loc1.pos
|
||||
end
|
||||
if Text.lt1({line=maxl, pos=maxp},
|
||||
{line=minl, pos=minp}) then
|
||||
|
@ -70,7 +70,7 @@ end
|
|||
function Text.mouse_pos(State)
|
||||
local x,y = love.mouse.getPosition()
|
||||
if y < State.top then
|
||||
return State.screen_top.line, State.screen_top.pos
|
||||
return State.screen_top.loc1.line, State.screen_top.loc1.pos
|
||||
end
|
||||
for line_index,line in ipairs(State.lines) do
|
||||
if line.mode == 'text' then
|
||||
|
@ -92,17 +92,17 @@ end
|
|||
|
||||
function Text.delete_selection(State)
|
||||
if State.selection1.line == nil then return end
|
||||
local minl,maxl = minmax(State.selection1.line, State.cursor.line)
|
||||
local minl,maxl = minmax(State.selection1.line, State.cursor.loc1.line)
|
||||
local before = snapshot(State, minl, maxl)
|
||||
Text.delete_selection_without_undo(State)
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.loc1.line)})
|
||||
end
|
||||
|
||||
function Text.delete_selection_without_undo(State)
|
||||
if State.selection1.line == nil then return end
|
||||
-- min,max = sorted(State.selection1,State.cursor)
|
||||
local minl,minp = State.selection1.line,State.selection1.pos
|
||||
local maxl,maxp = State.cursor.line,State.cursor.pos
|
||||
local maxl,maxp = State.cursor.loc1.line,State.cursor.loc1.pos
|
||||
if minl > maxl then
|
||||
minl,maxl = maxl,minl
|
||||
minp,maxp = maxp,minp
|
||||
|
@ -112,11 +112,11 @@ function Text.delete_selection_without_undo(State)
|
|||
end
|
||||
end
|
||||
-- update State.cursor and State.selection1
|
||||
State.cursor.line = minl
|
||||
State.cursor.pos = minp
|
||||
if Text.lt1(State.cursor, State.screen_top) then
|
||||
State.screen_top.line = State.cursor.line
|
||||
State.screen_top.pos = Text.pos_at_start_of_screen_line(State, State.cursor)
|
||||
State.cursor.loc1.line = minl
|
||||
State.cursor.loc1.pos = minp
|
||||
if Text.lt1(State.cursor.loc1, State.screen_top.loc1) then
|
||||
State.screen_top.loc1.line = State.cursor.loc1.line
|
||||
State.screen_top.loc1.pos = Text.pos_at_start_of_screen_line(State, State.cursor.loc1)
|
||||
end
|
||||
State.selection1 = {}
|
||||
-- delete everything between min (inclusive) and max (exclusive)
|
||||
|
@ -141,7 +141,7 @@ function Text.selection(State)
|
|||
if State.selection1.line == nil then return end
|
||||
-- min,max = sorted(State.selection1,State.cursor)
|
||||
local minl,minp = State.selection1.line,State.selection1.pos
|
||||
local maxl,maxp = State.cursor.line,State.cursor.pos
|
||||
local maxl,maxp = State.cursor.loc1.line,State.cursor.loc1.pos
|
||||
if minl > maxl then
|
||||
minl,maxl = maxl,minl
|
||||
minp,maxp = maxp,minp
|
||||
|
|
397
text.lua
397
text.lua
|
@ -24,26 +24,26 @@ function Text.draw(State, line_index, y, startpos)
|
|||
local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
|
||||
Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
|
||||
end
|
||||
if line_index == State.cursor.line then
|
||||
if line_index == State.cursor.loc1.line then
|
||||
-- render search highlight or cursor
|
||||
if State.search_term then
|
||||
local data = State.lines[State.cursor.line].data
|
||||
local cursor_offset = Text.offset(data, State.cursor.pos)
|
||||
local data = State.lines[State.cursor.loc1.line].data
|
||||
local cursor_offset = Text.offset(data, State.cursor.loc1.pos)
|
||||
if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
|
||||
local save_selection = State.selection1
|
||||
State.selection1 = {line=line_index, pos=State.cursor.pos+utf8.len(State.search_term)}
|
||||
State.selection1 = {line=line_index, pos=State.cursor.loc1.pos+utf8.len(State.search_term)}
|
||||
local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
|
||||
Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
|
||||
State.selection1 = save_selection
|
||||
end
|
||||
else
|
||||
if pos <= State.cursor.pos and pos + frag_len > State.cursor.pos then
|
||||
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor.pos-pos+1), y)
|
||||
elseif pos + frag_len == State.cursor.pos then
|
||||
if pos <= State.cursor.loc1.pos and pos + frag_len > State.cursor.loc1.pos then
|
||||
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor.loc1.pos-pos+1), y)
|
||||
elseif pos + frag_len == State.cursor.loc1.pos then
|
||||
-- Show cursor at end of line.
|
||||
-- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
|
||||
-- It seems useful to see a cursor whether your eye is on the left or right margin.
|
||||
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor.pos-pos+1), y)
|
||||
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor.loc1.pos-pos+1), y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -130,22 +130,22 @@ function Text.text_input(State, t)
|
|||
-- Key mutated by the keyboard layout. Continue below.
|
||||
end
|
||||
end
|
||||
local before = snapshot(State, State.cursor.line)
|
||||
--? print(State.screen_top.line, State.screen_top.pos, State.cursor.line, State.cursor.pos)
|
||||
local before = snapshot(State, State.cursor.loc1.line)
|
||||
--? print(State.screen_top.loc1.line, State.screen_top.loc1.pos, State.cursor.loc1.line, State.cursor.loc1.pos)
|
||||
Text.insert_at_cursor(State, t)
|
||||
if State.cursor_y > App.screen.height - State.line_height then
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.line)
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.loc1.line)
|
||||
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
|
||||
end
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.loc1.line)})
|
||||
end
|
||||
|
||||
function Text.insert_at_cursor(State, t)
|
||||
assert(State.lines[State.cursor.line].mode == 'text', 'line is not text')
|
||||
local byte_offset = Text.offset(State.lines[State.cursor.line].data, State.cursor.pos)
|
||||
State.lines[State.cursor.line].data = string.sub(State.lines[State.cursor.line].data, 1, byte_offset-1)..t..string.sub(State.lines[State.cursor.line].data, byte_offset)
|
||||
Text.clear_screen_line_cache(State, State.cursor.line)
|
||||
State.cursor.pos = State.cursor.pos+1
|
||||
assert(State.lines[State.cursor.loc1.line].mode == 'text', 'line is not text')
|
||||
local byte_offset = Text.offset(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos)
|
||||
State.lines[State.cursor.loc1.line].data = string.sub(State.lines[State.cursor.loc1.line].data, 1, byte_offset-1)..t..string.sub(State.lines[State.cursor.loc1.line].data, byte_offset)
|
||||
Text.clear_screen_line_cache(State, State.cursor.loc1.line)
|
||||
State.cursor.loc1.pos = State.cursor.loc1.pos+1
|
||||
end
|
||||
|
||||
-- Don't handle any keys here that would trigger text_input above.
|
||||
|
@ -153,7 +153,7 @@ function Text.keychord_press(State, chord)
|
|||
--? print('chord', chord, State.selection1.line, State.selection1.pos)
|
||||
--== shortcuts that mutate text
|
||||
if chord == 'return' then
|
||||
local before_line = State.cursor.line
|
||||
local before_line = State.cursor.loc1.line
|
||||
local before = snapshot(State, before_line)
|
||||
Text.insert_return(State)
|
||||
State.selection1 = {}
|
||||
|
@ -161,18 +161,18 @@ function Text.keychord_press(State, chord)
|
|||
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
|
||||
end
|
||||
schedule_save(State)
|
||||
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor.line)})
|
||||
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor.loc1.line)})
|
||||
elseif chord == 'tab' then
|
||||
local before = snapshot(State, State.cursor.line)
|
||||
--? print(State.screen_top.line, State.screen_top.pos, State.cursor.line, State.cursor.pos)
|
||||
local before = snapshot(State, State.cursor.loc1.line)
|
||||
--? print(State.screen_top.loc1.line, State.screen_top.loc1.pos, State.cursor.loc1.line, State.cursor.loc1.pos)
|
||||
Text.insert_at_cursor(State, '\t')
|
||||
if State.cursor_y > App.screen.height - State.line_height then
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.line)
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.loc1.line)
|
||||
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
|
||||
--? print('=>', State.screen_top.line, State.screen_top.pos, State.cursor.line, State.cursor.pos)
|
||||
--? print('=>', State.screen_top.loc1.line, State.screen_top.loc1.pos, State.cursor.loc1.line, State.cursor.loc1.pos)
|
||||
end
|
||||
schedule_save(State)
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.loc1.line)})
|
||||
elseif chord == 'backspace' then
|
||||
if State.selection1.line then
|
||||
Text.delete_selection(State, State.left, State.right)
|
||||
|
@ -180,47 +180,54 @@ function Text.keychord_press(State, chord)
|
|||
return
|
||||
end
|
||||
local before
|
||||
if State.cursor.pos > 1 then
|
||||
before = snapshot(State, State.cursor.line)
|
||||
local byte_start = utf8.offset(State.lines[State.cursor.line].data, State.cursor.pos-1)
|
||||
local byte_end = utf8.offset(State.lines[State.cursor.line].data, State.cursor.pos)
|
||||
if State.cursor.loc1.pos > 1 then
|
||||
before = snapshot(State, State.cursor.loc1.line)
|
||||
local byte_start = utf8.offset(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos-1)
|
||||
local byte_end = utf8.offset(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos)
|
||||
if byte_start then
|
||||
if byte_end then
|
||||
State.lines[State.cursor.line].data = string.sub(State.lines[State.cursor.line].data, 1, byte_start-1)..string.sub(State.lines[State.cursor.line].data, byte_end)
|
||||
State.lines[State.cursor.loc1.line].data = string.sub(State.lines[State.cursor.loc1.line].data, 1, byte_start-1)..string.sub(State.lines[State.cursor.loc1.line].data, byte_end)
|
||||
else
|
||||
State.lines[State.cursor.line].data = string.sub(State.lines[State.cursor.line].data, 1, byte_start-1)
|
||||
State.lines[State.cursor.loc1.line].data = string.sub(State.lines[State.cursor.loc1.line].data, 1, byte_start-1)
|
||||
end
|
||||
State.cursor.pos = State.cursor.pos-1
|
||||
State.cursor.loc1.pos = State.cursor.loc1.pos-1
|
||||
end
|
||||
elseif State.cursor.line > 1 then
|
||||
before = snapshot(State, State.cursor.line-1, State.cursor.line)
|
||||
if State.lines[State.cursor.line-1].mode == 'drawing' then
|
||||
table.remove(State.lines, State.cursor.line-1)
|
||||
table.remove(State.line_cache, State.cursor.line-1)
|
||||
elseif State.cursor.loc1.line > 1 then
|
||||
before = snapshot(State, State.cursor.loc1.line-1, State.cursor.loc1.line)
|
||||
if State.lines[State.cursor.loc1.line-1].mode == 'drawing' then
|
||||
table.remove(State.lines, State.cursor.loc1.line-1)
|
||||
table.remove(State.line_cache, State.cursor.loc1.line-1)
|
||||
else
|
||||
-- join lines
|
||||
State.cursor.pos = utf8.len(State.lines[State.cursor.line-1].data)+1
|
||||
State.lines[State.cursor.line-1].data = State.lines[State.cursor.line-1].data..State.lines[State.cursor.line].data
|
||||
table.remove(State.lines, State.cursor.line)
|
||||
table.remove(State.line_cache, State.cursor.line)
|
||||
State.cursor.loc1.pos = utf8.len(State.lines[State.cursor.loc1.line-1].data)+1
|
||||
State.lines[State.cursor.loc1.line-1].data = State.lines[State.cursor.loc1.line-1].data..State.lines[State.cursor.loc1.line].data
|
||||
table.remove(State.lines, State.cursor.loc1.line)
|
||||
table.remove(State.line_cache, State.cursor.loc1.line)
|
||||
end
|
||||
State.cursor.line = State.cursor.line-1
|
||||
State.cursor.loc1.line = State.cursor.loc1.line-1
|
||||
end
|
||||
if State.screen_top.line > #State.lines then
|
||||
if State.screen_top.loc1.line > #State.lines then
|
||||
Text.populate_screen_line_starting_pos(State, #State.lines)
|
||||
local line_cache = State.line_cache[#State.line_cache]
|
||||
State.screen_top = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
|
||||
elseif Text.lt1(State.cursor, State.screen_top) then
|
||||
if State.lines[#State.lines].mode == 'text' then
|
||||
local line_cache = State.line_cache[#State.line_cache]
|
||||
State.screen_top = {mode='text', loc1={line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}}
|
||||
else
|
||||
State.screen_top = {mode='drawing', line=#State.lines, pos=1}
|
||||
end
|
||||
elseif Text.lt1(State.cursor.loc1, State.screen_top.loc1) then
|
||||
State.screen_top = {
|
||||
line=State.cursor.line,
|
||||
pos=Text.pos_at_start_of_screen_line(State, State.cursor),
|
||||
mode='text',
|
||||
loc1={
|
||||
line=State.cursor.line,
|
||||
pos=Text.pos_at_start_of_screen_line(State, State.cursor),
|
||||
},
|
||||
}
|
||||
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
|
||||
end
|
||||
Text.clear_screen_line_cache(State, State.cursor.line)
|
||||
assert(Text.le1(State.screen_top, State.cursor), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top.line, State.screen_top.pos, State.cursor.line, State.cursor.pos))
|
||||
Text.clear_screen_line_cache(State, State.cursor.loc1.line)
|
||||
assert(Text.le1(State.screen_top.loc1, State.cursor.loc1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top.loc1.line, State.screen_top.loc1.pos, State.cursor.loc1.line, State.cursor.loc1.pos))
|
||||
schedule_save(State)
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.loc1.line)})
|
||||
elseif chord == 'delete' then
|
||||
if State.selection1.line then
|
||||
Text.delete_selection(State, State.left, State.right)
|
||||
|
@ -228,33 +235,33 @@ function Text.keychord_press(State, chord)
|
|||
return
|
||||
end
|
||||
local before
|
||||
if State.cursor.pos <= utf8.len(State.lines[State.cursor.line].data) then
|
||||
before = snapshot(State, State.cursor.line)
|
||||
if State.cursor.loc1.pos <= utf8.len(State.lines[State.cursor.loc1.line].data) then
|
||||
before = snapshot(State, State.cursor.loc1.line)
|
||||
else
|
||||
before = snapshot(State, State.cursor.line, State.cursor.line+1)
|
||||
before = snapshot(State, State.cursor.loc1.line, State.cursor.loc1.line+1)
|
||||
end
|
||||
if State.cursor.pos <= utf8.len(State.lines[State.cursor.line].data) then
|
||||
local byte_start = utf8.offset(State.lines[State.cursor.line].data, State.cursor.pos)
|
||||
local byte_end = utf8.offset(State.lines[State.cursor.line].data, State.cursor.pos+1)
|
||||
if State.cursor.loc1.pos <= utf8.len(State.lines[State.cursor.loc1.line].data) then
|
||||
local byte_start = utf8.offset(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos)
|
||||
local byte_end = utf8.offset(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos+1)
|
||||
if byte_start then
|
||||
if byte_end then
|
||||
State.lines[State.cursor.line].data = string.sub(State.lines[State.cursor.line].data, 1, byte_start-1)..string.sub(State.lines[State.cursor.line].data, byte_end)
|
||||
State.lines[State.cursor.loc1.line].data = string.sub(State.lines[State.cursor.loc1.line].data, 1, byte_start-1)..string.sub(State.lines[State.cursor.loc1.line].data, byte_end)
|
||||
else
|
||||
State.lines[State.cursor.line].data = string.sub(State.lines[State.cursor.line].data, 1, byte_start-1)
|
||||
State.lines[State.cursor.loc1.line].data = string.sub(State.lines[State.cursor.loc1.line].data, 1, byte_start-1)
|
||||
end
|
||||
-- no change to State.cursor.pos
|
||||
-- no change to State.cursor.loc1.pos
|
||||
end
|
||||
elseif State.cursor.line < #State.lines then
|
||||
if State.lines[State.cursor.line+1].mode == 'text' then
|
||||
elseif State.cursor.loc1.line < #State.lines then
|
||||
if State.lines[State.cursor.loc1.line+1].mode == 'text' then
|
||||
-- join lines
|
||||
State.lines[State.cursor.line].data = State.lines[State.cursor.line].data..State.lines[State.cursor.line+1].data
|
||||
State.lines[State.cursor.loc1.line].data = State.lines[State.cursor.loc1.line].data..State.lines[State.cursor.loc1.line+1].data
|
||||
end
|
||||
table.remove(State.lines, State.cursor.line+1)
|
||||
table.remove(State.line_cache, State.cursor.line+1)
|
||||
table.remove(State.lines, State.cursor.loc1.line+1)
|
||||
table.remove(State.line_cache, State.cursor.loc1.line+1)
|
||||
end
|
||||
Text.clear_screen_line_cache(State, State.cursor.line)
|
||||
Text.clear_screen_line_cache(State, State.cursor.loc1.line)
|
||||
schedule_save(State)
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.line)})
|
||||
record_undo_event(State, {before=before, after=snapshot(State, State.cursor.loc1.line)})
|
||||
--== shortcuts that move the cursor
|
||||
elseif chord == 'left' then
|
||||
Text.left(State)
|
||||
|
@ -264,12 +271,12 @@ function Text.keychord_press(State, chord)
|
|||
State.selection1 = {}
|
||||
elseif chord == 'S-left' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.left(State)
|
||||
elseif chord == 'S-right' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.right(State)
|
||||
-- C- hotkeys reserved for drawings, so we'll use M-
|
||||
|
@ -281,12 +288,12 @@ function Text.keychord_press(State, chord)
|
|||
State.selection1 = {}
|
||||
elseif chord == 'M-S-left' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.word_left(State)
|
||||
elseif chord == 'M-S-right' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.word_right(State)
|
||||
elseif chord == 'home' then
|
||||
|
@ -297,12 +304,12 @@ function Text.keychord_press(State, chord)
|
|||
State.selection1 = {}
|
||||
elseif chord == 'S-home' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.start_of_line(State)
|
||||
elseif chord == 'S-end' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.end_of_line(State)
|
||||
elseif chord == 'up' then
|
||||
|
@ -313,12 +320,12 @@ function Text.keychord_press(State, chord)
|
|||
State.selection1 = {}
|
||||
elseif chord == 'S-up' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.up(State)
|
||||
elseif chord == 'S-down' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.down(State)
|
||||
elseif chord == 'pageup' then
|
||||
|
@ -329,29 +336,29 @@ function Text.keychord_press(State, chord)
|
|||
State.selection1 = {}
|
||||
elseif chord == 'S-pageup' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.pos}
|
||||
end
|
||||
Text.pageup(State)
|
||||
elseif chord == 'S-pagedown' then
|
||||
if State.selection1.line == nil then
|
||||
State.selection1 = {line=State.cursor.line, pos=State.cursor.pos}
|
||||
State.selection1 = {line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}
|
||||
end
|
||||
Text.pagedown(State)
|
||||
end
|
||||
end
|
||||
|
||||
function Text.insert_return(State)
|
||||
local byte_offset = Text.offset(State.lines[State.cursor.line].data, State.cursor.pos)
|
||||
table.insert(State.lines, State.cursor.line+1, {mode='text', data=string.sub(State.lines[State.cursor.line].data, byte_offset)})
|
||||
table.insert(State.line_cache, State.cursor.line+1, {})
|
||||
State.lines[State.cursor.line].data = string.sub(State.lines[State.cursor.line].data, 1, byte_offset-1)
|
||||
Text.clear_screen_line_cache(State, State.cursor.line)
|
||||
State.cursor = {line=State.cursor.line+1, pos=1}
|
||||
local byte_offset = Text.offset(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos)
|
||||
table.insert(State.lines, State.cursor.loc1.line+1, {mode='text', data=string.sub(State.lines[State.cursor.loc1.line].data, byte_offset)})
|
||||
table.insert(State.line_cache, State.cursor.loc1.line+1, {})
|
||||
State.lines[State.cursor.loc1.line].data = string.sub(State.lines[State.cursor.loc1.line].data, 1, byte_offset-1)
|
||||
Text.clear_screen_line_cache(State, State.cursor.loc1.line)
|
||||
State.cursor.loc1 = {line=State.cursor.loc1.line+1, pos=1}
|
||||
end
|
||||
|
||||
function Text.pageup(State)
|
||||
State.screen_top = Text.previous_screen_top(State)
|
||||
State.cursor = {line=State.screen_top.line, pos=State.screen_top.pos}
|
||||
State.screen_top.loc1 = Text.previous_screen_top(State)
|
||||
State.cursor.loc1 = {line=State.screen_top.loc1.line, pos=State.screen_top.loc1.pos}
|
||||
Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
|
||||
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
|
||||
end
|
||||
|
@ -361,8 +368,8 @@ end
|
|||
function Text.starty(State, line_index)
|
||||
-- duplicate some logic from love.draw
|
||||
-- does not modify State (except to populate line_cache)
|
||||
if line_index < State.screen_top.line then return end
|
||||
local loc2 = Text.to2(State, State.screen_top)
|
||||
if line_index < State.screen_top.loc1.line then return end
|
||||
local loc2 = Text.to2(State, State.screen_top.loc1)
|
||||
local y = State.top
|
||||
while true do
|
||||
if State.lines[loc2.line].mode == 'drawing' then
|
||||
|
@ -384,7 +391,7 @@ end
|
|||
function Text.previous_screen_top(State)
|
||||
-- duplicate some logic from love.draw
|
||||
-- does not modify State (except to populate line_cache)
|
||||
local loc2 = Text.to2(State, State.screen_top)
|
||||
local loc2 = Text.to2(State, State.screen_top.loc1)
|
||||
local y = App.screen.height - State.line_height
|
||||
while y >= State.top do
|
||||
if loc2.line == 1 and loc2.screen_line == 1 and loc2.screen_pos == 1 then break end
|
||||
|
@ -399,8 +406,8 @@ function Text.previous_screen_top(State)
|
|||
end
|
||||
|
||||
function Text.pagedown(State)
|
||||
State.screen_top = Text.screen_bottom1(State)
|
||||
State.cursor = {line=State.screen_top.line, pos=State.screen_top.pos}
|
||||
State.screen_top.loc1 = Text.screen_bottom1(State)
|
||||
State.cursor.loc1 = {line=State.screen_top.line, pos=State.screen_top.pos}
|
||||
Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
|
||||
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
|
||||
end
|
||||
|
@ -409,7 +416,7 @@ end
|
|||
function Text.screen_bottom1(State)
|
||||
-- duplicate some logic from love.draw
|
||||
-- does not modify State (except to populate line_cache)
|
||||
local loc2 = Text.to2(State, State.screen_top)
|
||||
local loc2 = Text.to2(State, State.screen_top.loc1)
|
||||
local y = State.top
|
||||
while true do
|
||||
if State.lines[loc2.line].mode == 'text' then
|
||||
|
@ -426,87 +433,93 @@ function Text.screen_bottom1(State)
|
|||
end
|
||||
|
||||
function Text.up(State)
|
||||
assert(State.lines[State.cursor.line].mode == 'text', 'line is not text')
|
||||
--? print('up', State.cursor.line, State.cursor.pos, State.screen_top.line, State.screen_top.pos)
|
||||
local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor)
|
||||
assert(State.lines[State.cursor.loc1.line].mode == 'text', 'line is not text')
|
||||
--? print('up', State.cursor.loc1.line, State.cursor.loc1.pos, State.screen_top.loc1.line, State.screen_top.loc1.pos)
|
||||
local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor.loc1)
|
||||
if screen_line_starting_pos == 1 then
|
||||
--? print('cursor is at first screen line of its line')
|
||||
-- line is done; skip to previous text line
|
||||
local new_cursor_line = State.cursor.line
|
||||
local new_cursor_line = State.cursor.loc1.line
|
||||
while new_cursor_line > 1 do
|
||||
new_cursor_line = new_cursor_line-1
|
||||
if State.lines[new_cursor_line].mode == 'text' then
|
||||
--? print('found previous text line')
|
||||
State.cursor = {line=new_cursor_line, pos=nil}
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.line)
|
||||
State.cursor.loc1 = {line=new_cursor_line, pos=nil}
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.loc1.line)
|
||||
-- previous text line found, pick its final screen line
|
||||
--? print('has multiple screen lines')
|
||||
local screen_line_starting_pos = State.line_cache[State.cursor.line].screen_line_starting_pos
|
||||
local screen_line_starting_pos = State.line_cache[State.cursor.loc1.line].screen_line_starting_pos
|
||||
--? print(#screen_line_starting_pos)
|
||||
screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
|
||||
local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor.line].data, screen_line_starting_pos)
|
||||
local s = string.sub(State.lines[State.cursor.line].data, screen_line_starting_byte_offset)
|
||||
State.cursor.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||
local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor.loc1.line].data, screen_line_starting_pos)
|
||||
local s = string.sub(State.lines[State.cursor.loc1.line].data, screen_line_starting_byte_offset)
|
||||
State.cursor.loc1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||
break
|
||||
end
|
||||
end
|
||||
else
|
||||
-- move up one screen line in current line
|
||||
assert(screen_line_index > 1, 'bumped up against top screen line in line')
|
||||
local new_screen_line_starting_pos = State.line_cache[State.cursor.line].screen_line_starting_pos[screen_line_index-1]
|
||||
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor.line].data, new_screen_line_starting_pos)
|
||||
local s = string.sub(State.lines[State.cursor.line].data, new_screen_line_starting_byte_offset)
|
||||
State.cursor.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||
--? print('cursor pos is now '..tostring(State.cursor.pos))
|
||||
local new_screen_line_starting_pos = State.line_cache[State.cursor.loc1.line].screen_line_starting_pos[screen_line_index-1]
|
||||
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor.loc1.line].data, new_screen_line_starting_pos)
|
||||
local s = string.sub(State.lines[State.cursor.loc1.line].data, new_screen_line_starting_byte_offset)
|
||||
State.cursor.loc1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||
--? print('cursor pos is now '..tostring(State.cursor.loc1.pos))
|
||||
end
|
||||
if Text.lt1(State.cursor, State.screen_top) then
|
||||
if Text.lt1(State.cursor.loc1, State.screen_top.loc1) then
|
||||
State.screen_top = {
|
||||
line=State.cursor.line,
|
||||
pos=Text.pos_at_start_of_screen_line(State, State.cursor),
|
||||
mode='text',
|
||||
loc1={
|
||||
line=State.cursor.loc1.line,
|
||||
pos=Text.pos_at_start_of_screen_line(State, State.cursor.loc1),
|
||||
},
|
||||
}
|
||||
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
|
||||
end
|
||||
end
|
||||
|
||||
function Text.down(State)
|
||||
assert(State.lines[State.cursor.line].mode == 'text', 'line is not text')
|
||||
--? print('down', State.cursor.line, State.cursor.pos, State.screen_top.line, State.screen_top.pos)
|
||||
assert(State.cursor.pos, 'cursor has no pos')
|
||||
assert(State.lines[State.cursor.loc1.line].mode == 'text', 'line is not text')
|
||||
--? print('down', State.cursor.loc1.line, State.cursor.pos, State.screen_top.loc1.line, State.screen_top.loc1.pos)
|
||||
assert(State.cursor.loc1.pos, 'cursor has no pos')
|
||||
if Text.cursor_at_final_screen_line(State) then
|
||||
-- line is done, skip to next text line
|
||||
--? print('cursor at final screen line of its line')
|
||||
local new_cursor_line = State.cursor.line
|
||||
local new_cursor_line = State.cursor.loc1.line
|
||||
while new_cursor_line < #State.lines do
|
||||
new_cursor_line = new_cursor_line+1
|
||||
if State.lines[new_cursor_line].mode == 'text' then
|
||||
State.cursor = {
|
||||
line = new_cursor_line,
|
||||
pos = Text.nearest_cursor_pos(State.font, State.lines[new_cursor_line].data, State.cursor_x, State.left),
|
||||
mode='text',
|
||||
loc1={
|
||||
line = new_cursor_line,
|
||||
pos = Text.nearest_cursor_pos(State.font, State.lines[new_cursor_line].data, State.cursor_x, State.left),
|
||||
},
|
||||
}
|
||||
--? print(State.cursor.pos)
|
||||
--? print(State.cursor.loc1.pos)
|
||||
break
|
||||
end
|
||||
end
|
||||
local screen_bottom1 = Text.screen_bottom1(State)
|
||||
--? print('down 2', State.cursor.line, State.cursor.pos, State.screen_top.line, State.screen_top.pos, screen_bottom1.line, screen_bottom1.pos)
|
||||
if State.cursor.line > screen_bottom1.line then
|
||||
--? print('screen top before:', State.screen_top.line, State.screen_top.pos)
|
||||
--? print('down 2', State.cursor.loc1.line, State.cursor.loc1.pos, State.screen_top.loc1.line, State.screen_top.loc1.pos, screen_bottom1.line, screen_bottom1.pos)
|
||||
if State.cursor.loc1.line > screen_bottom1.line then
|
||||
--? print('screen top before:', State.screen_top.loc1.line, State.screen_top.loc1.pos)
|
||||
--? print('scroll up preserving cursor')
|
||||
Text.snap_cursor_to_bottom_of_screen(State)
|
||||
--? print('screen top after:', State.screen_top.line, State.screen_top.pos)
|
||||
--? print('screen top after:', State.screen_top.loc1.line, State.screen_top.loc1.pos)
|
||||
end
|
||||
else
|
||||
-- move down one screen line in current line
|
||||
local screen_bottom1 = Text.screen_bottom1(State)
|
||||
local scroll_down = Text.le1(screen_bottom1, State.cursor)
|
||||
local scroll_down = Text.le1(screen_bottom1, State.cursor.loc1)
|
||||
--? print('cursor is NOT at final screen line of its line')
|
||||
local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor)
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.line)
|
||||
local new_screen_line_starting_pos = State.line_cache[State.cursor.line].screen_line_starting_pos[screen_line_index+1]
|
||||
local screen_line_starting_pos, screen_line_index = Text.pos_at_start_of_screen_line(State, State.cursor.loc1)
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.loc1.line)
|
||||
local new_screen_line_starting_pos = State.line_cache[State.cursor.loc1.line].screen_line_starting_pos[screen_line_index+1]
|
||||
--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
|
||||
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor.line].data, new_screen_line_starting_pos)
|
||||
local s = string.sub(State.lines[State.cursor.line].data, new_screen_line_starting_byte_offset)
|
||||
State.cursor.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor.loc1.line].data, new_screen_line_starting_pos)
|
||||
local s = string.sub(State.lines[State.cursor.loc1.line].data, new_screen_line_starting_byte_offset)
|
||||
State.cursor.loc1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||
--? print('cursor pos is now', State.cursor.line, State.cursor.pos)
|
||||
if scroll_down then
|
||||
--? print('scroll up preserving cursor')
|
||||
|
@ -518,14 +531,14 @@ function Text.down(State)
|
|||
end
|
||||
|
||||
function Text.start_of_line(State)
|
||||
State.cursor.pos = 1
|
||||
if Text.lt1(State.cursor, State.screen_top) then
|
||||
State.screen_top = {line=State.cursor.line, pos=State.cursor.pos} -- copy
|
||||
State.cursor.loc1.pos = 1
|
||||
if Text.lt1(State.cursor.loc1, State.screen_top.loc1) then
|
||||
State.screen_top = {mode='text', loc1={line=State.cursor.loc1.line, pos=State.cursor.loc1.pos}} -- copy
|
||||
end
|
||||
end
|
||||
|
||||
function Text.end_of_line(State)
|
||||
State.cursor.pos = utf8.len(State.lines[State.cursor.line].data) + 1
|
||||
State.cursor.loc1.pos = utf8.len(State.lines[State.cursor.loc1.line].data) + 1
|
||||
if Text.cursor_out_of_screen(State) then
|
||||
Text.snap_cursor_to_bottom_of_screen(State)
|
||||
end
|
||||
|
@ -534,10 +547,10 @@ end
|
|||
function Text.word_left(State)
|
||||
-- skip some whitespace
|
||||
while true do
|
||||
if State.cursor.pos == 1 then
|
||||
if State.cursor.loc1.pos == 1 then
|
||||
break
|
||||
end
|
||||
if Text.match(State.lines[State.cursor.line].data, State.cursor.pos-1, '%S') then
|
||||
if Text.match(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos-1, '%S') then
|
||||
break
|
||||
end
|
||||
Text.left(State)
|
||||
|
@ -545,11 +558,11 @@ function Text.word_left(State)
|
|||
-- skip some non-whitespace
|
||||
while true do
|
||||
Text.left(State)
|
||||
if State.cursor.pos == 1 then
|
||||
if State.cursor.loc1.pos == 1 then
|
||||
break
|
||||
end
|
||||
assert(State.cursor.pos > 1, 'bumped up against start of line')
|
||||
if Text.match(State.lines[State.cursor.line].data, State.cursor.pos-1, '%s') then
|
||||
assert(State.cursor.loc1.pos > 1, 'bumped up against start of line')
|
||||
if Text.match(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos-1, '%s') then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -558,20 +571,20 @@ end
|
|||
function Text.word_right(State)
|
||||
-- skip some whitespace
|
||||
while true do
|
||||
if State.cursor.pos > utf8.len(State.lines[State.cursor.line].data) then
|
||||
if State.cursor.loc1.pos > utf8.len(State.lines[State.cursor.loc1.line].data) then
|
||||
break
|
||||
end
|
||||
if Text.match(State.lines[State.cursor.line].data, State.cursor.pos, '%S') then
|
||||
if Text.match(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos, '%S') then
|
||||
break
|
||||
end
|
||||
Text.right_without_scroll(State)
|
||||
end
|
||||
while true do
|
||||
Text.right_without_scroll(State)
|
||||
if State.cursor.pos > utf8.len(State.lines[State.cursor.line].data) then
|
||||
if State.cursor.loc1.pos > utf8.len(State.lines[State.cursor.loc1.line].data) then
|
||||
break
|
||||
end
|
||||
if Text.match(State.lines[State.cursor.line].data, State.cursor.pos, '%s') then
|
||||
if Text.match(State.lines[State.cursor.loc1.line].data, State.cursor.loc1.pos, '%s') then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -589,26 +602,32 @@ function Text.match(s, pos, pat)
|
|||
end
|
||||
|
||||
function Text.left(State)
|
||||
assert(State.lines[State.cursor.line].mode == 'text', 'line is not text')
|
||||
if State.cursor.pos > 1 then
|
||||
State.cursor.pos = State.cursor.pos-1
|
||||
assert(State.lines[State.cursor.loc1.line].mode == 'text', 'line is not text')
|
||||
if State.cursor.loc1.pos > 1 then
|
||||
State.cursor.loc1.pos = State.cursor.loc1.pos-1
|
||||
else
|
||||
local new_cursor_line = State.cursor.line
|
||||
local new_cursor_line = State.cursor.loc1.line
|
||||
while new_cursor_line > 1 do
|
||||
new_cursor_line = new_cursor_line-1
|
||||
if State.lines[new_cursor_line].mode == 'text' then
|
||||
State.cursor = {
|
||||
line = new_cursor_line,
|
||||
pos = utf8.len(State.lines[new_cursor_line].data) + 1,
|
||||
mode='text',
|
||||
loc1={
|
||||
line = new_cursor_line,
|
||||
pos = utf8.len(State.lines[new_cursor_line].data) + 1,
|
||||
},
|
||||
}
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if Text.lt1(State.cursor, State.screen_top) then
|
||||
if Text.lt1(State.cursor.loc1, State.screen_top.loc1) then
|
||||
State.screen_top = {
|
||||
line=State.cursor.line,
|
||||
pos=Text.pos_at_start_of_screen_line(State, State.cursor),
|
||||
mode='text',
|
||||
loc1={
|
||||
line=State.cursor.loc1.line,
|
||||
pos=Text.pos_at_start_of_screen_line(State, State.cursor.loc1),
|
||||
},
|
||||
}
|
||||
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
|
||||
end
|
||||
|
@ -622,15 +641,15 @@ function Text.right(State)
|
|||
end
|
||||
|
||||
function Text.right_without_scroll(State)
|
||||
assert(State.lines[State.cursor.line].mode == 'text', 'line is not text')
|
||||
if State.cursor.pos <= utf8.len(State.lines[State.cursor.line].data) then
|
||||
State.cursor.pos = State.cursor.pos+1
|
||||
assert(State.lines[State.cursor.loc1.line].mode == 'text', 'line is not text')
|
||||
if State.cursor.loc1.pos <= utf8.len(State.lines[State.cursor.loc1.line].data) then
|
||||
State.cursor.loc1.pos = State.cursor.loc1.pos+1
|
||||
else
|
||||
local new_cursor_line = State.cursor.line
|
||||
local new_cursor_line = State.cursor.loc1.line
|
||||
while new_cursor_line <= #State.lines-1 do
|
||||
new_cursor_line = new_cursor_line+1
|
||||
if State.lines[new_cursor_line].mode == 'text' then
|
||||
State.cursor = {line=new_cursor_line, pos=1}
|
||||
State.cursor = {mode='text', loc1={line=new_cursor_line, pos=1}}
|
||||
break
|
||||
end
|
||||
end
|
||||
|
@ -685,28 +704,28 @@ function Text.final_text_loc_on_screen(State)
|
|||
end
|
||||
|
||||
function Text.cursor_at_final_screen_line(State)
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.line)
|
||||
local screen_lines = State.line_cache[State.cursor.line].screen_line_starting_pos
|
||||
--? print(screen_lines[#screen_lines], State.cursor.pos)
|
||||
return screen_lines[#screen_lines] <= State.cursor.pos
|
||||
Text.populate_screen_line_starting_pos(State, State.cursor.loc1.line)
|
||||
local screen_lines = State.line_cache[State.cursor.loc1.line].screen_line_starting_pos
|
||||
--? print(screen_lines[#screen_lines], State.cursor.loc1.pos)
|
||||
return screen_lines[#screen_lines] <= State.cursor.loc1.pos
|
||||
end
|
||||
|
||||
function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necessary(State)
|
||||
local y = State.top
|
||||
while State.cursor.line <= #State.lines do
|
||||
if State.lines[State.cursor.line].mode == 'text' then
|
||||
while State.cursor.loc1.line <= #State.lines do
|
||||
if State.lines[State.cursor.loc1.line].mode == 'text' then
|
||||
break
|
||||
end
|
||||
--? print('cursor skips', State.cursor.line)
|
||||
y = y + Drawing_padding_height + Drawing.pixels(State.lines[State.cursor.line].h, State.width)
|
||||
State.cursor.line = State.cursor.line + 1
|
||||
--? print('cursor skips', State.cursor.loc1.line)
|
||||
y = y + Drawing_padding_height + Drawing.pixels(State.lines[State.cursor.loc1.line].h, State.width)
|
||||
State.cursor.loc1.line = State.cursor.loc1.line + 1
|
||||
end
|
||||
if State.cursor.pos == nil then
|
||||
State.cursor.pos = 1
|
||||
if State.cursor.loc1.pos == nil then
|
||||
State.cursor.loc1.pos = 1
|
||||
end
|
||||
-- hack: insert a text line at bottom of file if necessary
|
||||
if State.cursor.line > #State.lines then
|
||||
assert(State.cursor.line == #State.lines+1, 'tried to ensure bottom line of file is text, but failed')
|
||||
if State.cursor.loc1.line > #State.lines then
|
||||
assert(State.cursor.loc1.line == #State.lines+1, 'tried to ensure bottom line of file is text, but failed')
|
||||
table.insert(State.lines, {mode='text', data=''})
|
||||
table.insert(State.line_cache, {})
|
||||
end
|
||||
|
@ -719,13 +738,13 @@ end
|
|||
|
||||
-- should never modify State.cursor
|
||||
function Text.snap_cursor_to_bottom_of_screen(State)
|
||||
--? print('to2:', State.cursor.line, State.cursor.pos)
|
||||
local top2 = Text.to2(State, State.cursor)
|
||||
--? print('to2:', State.cursor.loc1.line, State.cursor.loc1.pos)
|
||||
local top2 = Text.to2(State, State.cursor.loc1)
|
||||
--? print('to2: =>', top2.line, top2.screen_line, top2.screen_pos)
|
||||
-- slide to start of screen line
|
||||
top2.screen_pos = 1 -- start of screen line
|
||||
--? print('snap', State.screen_top.line, State.screen_top.pos, State.cursor.line, State.cursor.pos)
|
||||
--? print('cursor pos '..tostring(State.cursor.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
|
||||
--? print('snap', State.screen_top.loc1.line, State.screen_top.loc1.pos, State.cursor.loc1.line, State.cursor.loc1.pos)
|
||||
--? print('cursor pos '..tostring(State.cursor.loc1.pos)..' is on the #'..tostring(top2.screen_line)..' screen line down')
|
||||
local y = App.screen.height - State.line_height
|
||||
-- duplicate some logic from love.draw
|
||||
while true do
|
||||
|
@ -752,9 +771,9 @@ function Text.snap_cursor_to_bottom_of_screen(State)
|
|||
top2 = Text.previous_screen_line(State, top2)
|
||||
end
|
||||
--? print('top2 finally:', top2.line, top2.screen_line, top2.screen_pos)
|
||||
State.screen_top = Text.to1(State, top2)
|
||||
--? print('top1 finally:', State.screen_top.line, State.screen_top.pos)
|
||||
--? print('snap =>', State.screen_top.line, State.screen_top.pos, State.cursor.line, State.cursor.pos)
|
||||
State.screen_top.loc1 = Text.to1(State, top2)
|
||||
--? print('top1 finally:', State.screen_top.loc1.line, State.screen_top.loc1.pos)
|
||||
--? print('snap =>', State.screen_top.loc1.line, State.screen_top.loc1.pos, State.cursor.loc1.line, State.cursor.loc1.pos)
|
||||
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
|
||||
end
|
||||
|
||||
|
@ -1006,33 +1025,35 @@ end
|
|||
|
||||
-- resize helper
|
||||
function Text.tweak_screen_top_and_cursor(State)
|
||||
if State.screen_top.pos == 1 then return end
|
||||
Text.populate_screen_line_starting_pos(State, State.screen_top.line)
|
||||
local line = State.lines[State.screen_top.line]
|
||||
local line_cache = State.line_cache[State.screen_top.line]
|
||||
if State.screen_top.loc1.pos == 1 then return end
|
||||
Text.populate_screen_line_starting_pos(State, State.screen_top.loc1.line)
|
||||
local line = State.lines[State.screen_top.loc1.line]
|
||||
local line_cache = State.line_cache[State.screen_top.loc1.line]
|
||||
for i=2,#line_cache.screen_line_starting_pos do
|
||||
local pos = line_cache.screen_line_starting_pos[i]
|
||||
if pos == State.screen_top.pos then
|
||||
if pos == State.screen_top.loc1.pos then
|
||||
break
|
||||
end
|
||||
if pos > State.screen_top.pos then
|
||||
if pos > State.screen_top.loc1.pos then
|
||||
-- make sure screen top is at start of a screen line
|
||||
local prev = line_cache.screen_line_starting_pos[i-1]
|
||||
if State.screen_top.pos - prev < pos - State.screen_top.pos then
|
||||
State.screen_top.pos = prev
|
||||
if State.screen_top.loc1.pos - prev < pos - State.screen_top.loc1.pos then
|
||||
State.screen_top.loc1.pos = prev
|
||||
else
|
||||
State.screen_top.pos = pos
|
||||
State.screen_top.loc1.pos = pos
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
-- make sure cursor is on screen
|
||||
local screen_bottom1 = Text.screen_bottom1(State)
|
||||
if Text.lt1(State.cursor, State.screen_top) then
|
||||
State.cursor = {line=State.screen_top.line, pos=State.screen_top.pos}
|
||||
elseif State.cursor.line >= screen_bottom1.line then
|
||||
if Text.cursor_out_of_screen(State) then
|
||||
State.cursor = Text.final_text_loc_on_screen(State)
|
||||
if Text.lt1(State.cursor.loc1, State.screen_top.loc1) then
|
||||
State.cursor = {mode='text', loc1={line=State.screen_top.line, pos=State.screen_top.pos}}
|
||||
else
|
||||
local screen_bottom1 = Text.screen_bottom1(State)
|
||||
if State.cursor.loc1.line >= screen_bottom1.line then
|
||||
if Text.cursor_out_of_screen(State) then
|
||||
State.cursor.loc1 = Text.final_text_loc_on_screen(State)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue