about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-14 09:45:47 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-14 09:48:41 -0800
commit7b91691b1422d20c85983abf2e2ad45cb8f4280b (patch)
tree93facb51323c5a057a64a68b5fd1c90bdf9f10d4
parentb3b844c5f7fa87a86533753f894b8f1835bcabdb (diff)
downloadteliva-7b91691b1422d20c85983abf2e2ad45cb8f4280b.tar.gz
remain in editor on error
-rw-r--r--src/kilo.c11
-rw-r--r--src/lua.c25
2 files changed, 31 insertions, 5 deletions
diff --git a/src/kilo.c b/src/kilo.c
index 2c6afbe..d4aa962 100644
--- a/src/kilo.c
+++ b/src/kilo.c
@@ -1123,3 +1123,14 @@ void edit(lua_State* L, char* filename, const char* message) {
         editorProcessKeypress(L);
     }
 }
+
+void resumeEdit(lua_State* L, char* filename, const char* message) {
+    Quit = 0;
+    clearEditor();
+    editorOpen(filename);
+    editorSetStatusMessage(message);
+    while(!Quit) {
+        editorRefreshScreen(editorMenu);
+        editorProcessKeypress(L);
+    }
+}
diff --git a/src/lua.c b/src/lua.c
index cca7027..437b5f5 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -360,29 +360,44 @@ char **Argv = NULL;
 extern void edit (lua_State *L, char *filename, const char *message);
 extern void clearEditor (void);
 extern int editorOpen (char *filename);
-void edit_buffer (lua_State *L, const char *message) {
+inline void edit_buffer (lua_State *L, const char *message) {
   edit(L, "teliva_editbuffer", message);
 }
 void editor_refresh_buffer (void) {
   clearEditor();
   editorOpen("teliva_editbuffer");
 }
+extern void resumeEdit (lua_State *L, char *filename, const char *message);
+inline void editor_resume (lua_State *L, const char *message) {
+  resumeEdit(L, "teliva_editbuffer", message);
+}
 
 
-void load_editor_buffer_to_current_definition_in_image(lua_State *L) {
+int load_editor_buffer_to_current_definition_in_image(lua_State *L) {
   char new_contents[8192] = {0};
   read_contents(L, "teliva_editbuffer", new_contents);
   update_definition(L, Current_definition, new_contents);
   save_image(L);
-  /* reload binding if possible */
-  dostring(L, new_contents, Current_definition);
+  /* reload binding */
+  return luaL_loadbuffer(L, new_contents, strlen(new_contents), Current_definition)
+          || docall(L, 0, 1);
 }
 
 
 void edit_image (lua_State *L, const char *definition) {
   save_to_current_definition_and_editor_buffer(L, definition);
   edit_buffer(L, /*status message*/ "");
-  load_editor_buffer_to_current_definition_in_image(L);
+  // error handling
+  while (1) {
+    int status;
+    status = load_editor_buffer_to_current_definition_in_image(L);
+    if (status == 0 || lua_isnil(L, -1))
+      break;
+    const char *msg = lua_tostring(L, -1);
+    if (msg == NULL) msg = "(error object is not a string)";
+    editor_resume(L, msg);
+    lua_pop(L, 1);
+  }
 }