state validation in source editor as well

This commit is contained in:
Kartik K. Agaram 2023-03-17 10:46:50 -07:00
parent e2c1bbe4e5
commit d65b7950a1
2 changed files with 25 additions and 2 deletions

View File

@ -87,6 +87,7 @@ function source.initialize_edit_side()
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1}
end
edit.check_locs(Editor_state)
-- We currently start out with side B collapsed.
-- Other options:

View File

@ -115,10 +115,32 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
return result
end -- App.initialize_state
function edit.fixup_cursor(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_top1)
or edit.invalid1(State, State.cursor1)
or not edit.cursor_on_text(State)
or not Text.le1(State.screen_top1, State.cursor1) then
State.screen_top1 = {line=1, pos=1}
edit.put_cursor_on_first_text_line(State)
end
end
function edit.invalid1(State, loc1)
return loc1.line > #State.lines
or loc1.pos > #State.lines[loc1.line].data
end
function edit.cursor_on_text(State)
return State.cursor1.line <= #State.lines
and State.lines[State.cursor1.line].mode == 'text'
end
function edit.put_cursor_on_first_text_line(State)
for i,line in ipairs(State.lines) do
if line.mode == 'text' then
State.cursor1.line = i
State.cursor1 = {line=i, pos=1}
break
end
end