about summary refs log tree commit diff stats
path: root/edit.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-03-17 09:36:38 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-03-17 10:28:10 -0700
commite2c1bbe4e53bb0369471296caedf81ff6a059ae0 (patch)
treeadd8700c3d57ca09ad6dc4d37a164dff868ef8f4 /edit.lua
parent224e8fe85a55671139527b1e77c88a1f0d629313 (diff)
downloadtext.love-e2c1bbe4e53bb0369471296caedf81ff6a059ae0.tar.gz
more robust state validation
Diffstat (limited to 'edit.lua')
-rw-r--r--edit.lua26
1 files changed, 24 insertions, 2 deletions
diff --git a/edit.lua b/edit.lua
index 8f629a4..1d8e0b3 100644
--- a/edit.lua
+++ b/edit.lua
@@ -109,10 +109,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