about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-28 11:58:13 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-28 12:04:16 -0800
commit570af7c2550c77489d455297b9a1d7ae4baa674a (patch)
tree88f4c46a3a7301ac200c65b75d105235f85e01e6 /src
parent462a45a39f8d6ce6194702270f3ce4f7f99d95a6 (diff)
downloadteliva-570af7c2550c77489d455297b9a1d7ae4baa674a.tar.gz
fix going to big picture after saving editor state
My code is already at spaghetti levels. Some coping mechanisms.

===
The big problem with the Teliva approach compared to my previous Mu
project: no tests. At this point I should document the growing list of
manual tests I've been maintaining:

  run a program
  run a program, edit
  run a program, edit, make an edit, run | edit takes effect
  run a program with error
  run a program, edit, make an error, run
  run a program, edit, ^g to a different definition, make an edit, ^e to run again
  run a program, edit, ^g to a non-existent definition
  run a program, edit, ^g to a different definition, ^g to a different definition, ^e to run again
  start -> big picture -> edit -> move cursor -> run -> edit | cursor preserved
  start -> big picture -> edit A -> move cursor -> big picture -> edit B | cursor initialized
  start -> big picture -> edit A -> move cursor -> run -> exit -> start -> big picture -> edit B | cursor initialized
  start -> big picture -> edit A -> move cursor -> run -> exit -> start -> big picture -> edit B -> big picture (*)

  syntax highlighting for line comments
  syntax highlighting for multiline comments

(*) - fixed in this commit

===
Coarse-grained state diagram (ignoring recent_changes_view):
  app -> big picture on ^e
  big picture -> editor when selecting a definition
  editor -> app on e
  editor -> big picture on ^b

Fine-grained sequence diagram:
  main -> pmain -> ... -> Wgetch -> switch_to_editor -> select_view
  select_view -> load_editor_state, falling through to big_picture_view if needed
  load_editor_state -> edit_from -> editorProcessKeypress

The consequence I hadn't fully internalized was the return path:
  editorProcessKeypress -> edit_from -> big_picture_view

Which implies that load_editor_state fails in two ways:
  - when the state doesn't exist or is not applicable or is corrupted
  - when editing from the state explicitly requested the big picture view

Switching the return value semantics for load_editor_state now supports
both ways.
Diffstat (limited to 'src')
-rw-r--r--src/lua.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/lua.c b/src/lua.c
index e4a4394..029b351 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -920,7 +920,7 @@ restart:
 }
 
 
-/* return true if:
+/* return false if:
  *  - editor_state was successfully loaded, and
  *  - editor_state is applicable to this run, and
  *  - we successfully switched to the desired view */
@@ -928,16 +928,16 @@ extern int edit_from(lua_State* L, char* filename, const char* message, int rowo
 int load_view_from_editor_state (lua_State *L) {
   int status;
   status = luaL_loadfile(L, "teliva_editor_state");
-  if (status != 0) return 0;
+  if (status != 0) return 1;
   status = docall(L, 0, 0);
-  if (status != 0) return 0;
+  if (status != 0) return 1;
   lua_getglobal(L, "__teliva_editor_state");
   int editor_state_index = lua_gettop(L);
   lua_getfield(L, editor_state_index, "image");
   const char *image_name = lua_tostring(L, -1);
   if (strcmp(image_name, Image_name) != 0) {
     lua_settop(L, editor_state_index);
-    return 0;
+    return 1;
   }
   lua_getfield(L, editor_state_index, "definition");
   const char *definition = lua_tostring(L, -1);
@@ -950,14 +950,14 @@ int load_view_from_editor_state (lua_State *L) {
   int cy = lua_tointeger(L, -1);
   lua_getfield(L, editor_state_index, "cx");
   int cx = lua_tointeger(L, -1);
-  edit_from(L, "teliva_editor_buffer", /*error message*/ "", rowoff, coloff, cy, cx);
+  int back_to_big_picture = edit_from(L, "teliva_editor_buffer", /*error message*/ "", rowoff, coloff, cy, cx);
   lua_settop(L, editor_state_index);
-  return 1;
+  return back_to_big_picture;
 }
 
 
 void select_view (lua_State *L) {
-  if (!load_view_from_editor_state(L))
+  if (load_view_from_editor_state(L))
     big_picture_view(L);
 }