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 10:50:13 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-28 10:50:13 -0800
commit2b8b811175223762f802dd4d7c570b0c95227a64 (patch)
tree69436ca73727286d87c60a2e069921cb8db17e95 /src
parentd3d8c13828b78d4252b5a2d530b4b0dd38babcb7 (diff)
downloadteliva-2b8b811175223762f802dd4d7c570b0c95227a64.tar.gz
restore editor state from snapshot
Diffstat (limited to 'src')
-rw-r--r--src/kilo.c18
-rw-r--r--src/lua.c45
2 files changed, 62 insertions, 1 deletions
diff --git a/src/kilo.c b/src/kilo.c
index 45d10c4..3ecef1b 100644
--- a/src/kilo.c
+++ b/src/kilo.c
@@ -1132,6 +1132,24 @@ int edit(lua_State* L, char* filename, const char* message) {
     return Back_to_big_picture;
 }
 
+/* return true if user chose to back into the big picture view */
+int edit_from(lua_State* L, char* filename, const char* message, int rowoff, int coloff, int cy, int cx) {
+    Quit = 0;
+    Back_to_big_picture = 0;
+    initEditor();
+    E.rowoff = rowoff;
+    E.coloff = coloff;
+    E.cy = cy;
+    E.cx = cx;
+    editorOpen(filename);
+    editorSetStatusMessage(message);
+    while(!Quit) {
+        editorRefreshScreen(editorMenu);
+        editorProcessKeypress(L);
+    }
+    return Back_to_big_picture;
+}
+
 int resumeEdit(lua_State* L) {
     Quit = 0;
     Back_to_big_picture = 0;
diff --git a/src/lua.c b/src/lua.c
index 71fee90..8aa9901 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -920,6 +920,49 @@ restart:
 }
 
 
+/* return true if:
+ *  - snapshot was successfully loaded, and
+ *  - snapshot is applicable to this run, and
+ *  - we successfully switched to the desired view */
+extern int edit_from(lua_State* L, char* filename, const char* message, int rowoff, int coloff, int cy, int cx);
+int load_view_from_snapshot (lua_State *L) {
+  int status;
+  status = luaL_loadfile(L, "teliva_snapshot");
+  if (status != 0) return 0;
+  status = docall(L, 0, 0);
+  if (status != 0) return 0;
+  lua_getglobal(L, "__teliva_snapshot");
+  int snapshot_index = lua_gettop(L);
+  lua_getfield(L, snapshot_index, "image");
+  const char *image_name = lua_tostring(L, -1);
+  if (strcmp(image_name, Image_name) != 0) {
+    lua_settop(L, snapshot_index);
+    return 0;
+  }
+  lua_getfield(L, snapshot_index, "definition");
+  const char *definition = lua_tostring(L, -1);
+  int before = lua_gettop(L);
+  save_to_current_definition_and_editor_buffer(L, definition);
+  lua_getfield(L, snapshot_index, "rowoff");
+  int rowoff = lua_tointeger(L, -1);
+  lua_getfield(L, snapshot_index, "coloff");
+  int coloff = lua_tointeger(L, -1);
+  lua_getfield(L, snapshot_index, "cy");
+  int cy = lua_tointeger(L, -1);
+  lua_getfield(L, snapshot_index, "cx");
+  int cx = lua_tointeger(L, -1);
+  edit_from(L, "teliva_editbuffer", /*error message*/ "", rowoff, coloff, cy, cx);
+  lua_settop(L, snapshot_index);
+  return 1;
+}
+
+
+void select_view (lua_State *L) {
+  if (!load_view_from_snapshot(L))
+    big_picture_view(L);
+}
+
+
 extern void cleanup_curses (void);
 void switch_to_editor (lua_State *L) {
   /* clobber the app's ncurses colors; we'll restart the app when we rerun it. */
@@ -928,7 +971,7 @@ void switch_to_editor (lua_State *L) {
   for (int i = 0; i < 8; ++i)
     init_pair(i+8, -1, i);
   nodelay(stdscr, 0);
-  big_picture_view(L);
+  select_view(L);
   cleanup_curses();
   execv(Argv[0], Argv);
   /* never returns */