about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-03-17 11:02:10 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-03-17 11:02:10 -0700
commit4dbc097f835218bc44dd1d96134294a0bc5ce725 (patch)
treea87f0de56d342b79c371aea3cb2d6f51a0e37d60
parent67334814a331d41c43c186adf12ee86717918fd6 (diff)
parentd65b7950a152cf2ffe3149707f5e2040b305d8bd (diff)
downloadtext.love-4dbc097f835218bc44dd1d96134294a0bc5ce725.tar.gz
Merge lines.love
-rw-r--r--edit.lua16
-rw-r--r--run.lua2
-rw-r--r--source.lua1
-rw-r--r--source_edit.lua26
4 files changed, 43 insertions, 2 deletions
diff --git a/edit.lua b/edit.lua
index 7754493..865682d 100644
--- a/edit.lua
+++ b/edit.lua
@@ -74,6 +74,22 @@ function edit.initialize_state(top, left, right, font_height, line_height)  -- c
   return result
 end  -- App.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_top1)
+      or edit.invalid1(State, State.cursor1)
+      or not Text.le1(State.screen_top1, State.cursor1) then
+    State.screen_top1 = {line=1, pos=1}
+    State.cursor1 = {line=1, pos=1}
+  end
+end
+
+function edit.invalid1(State, loc1)
+  return loc1.line > #State.lines
+      or loc1.pos > #State.lines[loc1.line].data
+end
+
 function edit.draw(State)
   App.color(Text_color)
   assert(#State.lines == #State.line_cache)
diff --git a/run.lua b/run.lua
index 3035319..bf21c5a 100644
--- a/run.lua
+++ b/run.lua
@@ -32,6 +32,7 @@ function run.initialize(arg)
     load_from_disk(Editor_state)
     Text.redraw_all(Editor_state)
   end
+  edit.check_locs(Editor_state)
   love.window.setTitle('text.love - '..Editor_state.filename)
 
   if #arg > 1 then
@@ -108,6 +109,7 @@ function run.file_drop(file)
   Editor_state.lines = load_from_file(file)
   file:close()
   Text.redraw_all(Editor_state)
+  edit.check_locs(Editor_state)
   love.window.setTitle('text.love - '..Editor_state.filename)
 end
 
diff --git a/source.lua b/source.lua
index e4e8ec0..3ac51d5 100644
--- a/source.lua
+++ b/source.lua
@@ -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:
diff --git a/source_edit.lua b/source_edit.lua
index e17f2f2..f340ab3 100644
--- a/source_edit.lua
+++ b/source_edit.lua
@@ -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