about summary refs log tree commit diff stats
path: root/edit.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-06-08 01:02:54 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-06-08 01:02:54 -0700
commit14c08f9bd9a45d2f05727ea47b128afdbdff1d24 (patch)
tree3e146695f66458990d2c07602a728faf0e3aa46c /edit.lua
parent68eaba7d3db72dda06c4504ec744f1d2f47f994c (diff)
downloadview.love-14c08f9bd9a45d2f05727ea47b128afdbdff1d24.tar.gz
several bugfixes in saving/loading cursor position
Diffstat (limited to 'edit.lua')
-rw-r--r--edit.lua12
1 files changed, 11 insertions, 1 deletions
diff --git a/edit.lua b/edit.lua
index 442b986..ef1cda4 100644
--- a/edit.lua
+++ b/edit.lua
@@ -109,7 +109,7 @@ 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 edit.invalid_cursor1(State)
       or not edit.cursor_on_text(State)
       or not Text.le1(State.screen_top1, State.cursor1) then
     State.screen_top1 = {line=1, pos=1}
@@ -124,6 +124,16 @@ function edit.invalid1(State, loc1)
   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 cursor1 = State.cursor1
+  if cursor1.line > #State.lines then return true end
+  local l = State.lines[cursor1.line]
+  if l.mode ~= 'text' then return false end  -- pos is irrelevant to validity for a drawing line
+  return cursor1.pos > #State.lines[cursor1.line].data + 1
+end
+
 function edit.cursor_on_text(State)
   return State.cursor1.line <= #State.lines
       and State.lines[State.cursor1.line].mode == 'text'