about summary refs log tree commit diff stats
path: root/src/lua.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua.c')
-rw-r--r--src/lua.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lua.c b/src/lua.c
index e2f8574..7da0f02 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -5,6 +5,7 @@
 */
 
 
+#include <fcntl.h>
 #include <locale.h>
 #include <ncurses.h>
 #include <signal.h>
@@ -330,6 +331,49 @@ void switch_to_editor(lua_State *L, const char *message) {
   /* never returns */
 }
 
+void editString(lua_State *L, char *name) {
+    /* write given definition out to tmp file */
+//?     stackDump(L);
+    teliva_get_definition(L, name);
+//?     stackDump(L);
+    const char *contents = lua_tostring(L, -1);
+    int outfd = open("teliva_editbuffer", O_WRONLY|O_CREAT|O_TRUNC, 0644);
+    write(outfd, contents, strlen(contents));
+    close(outfd);
+
+    /* edit tmp file */
+    edit("teliva_editbuffer", "");
+
+    /* read contents of tmp file */
+    char new_contents[8192] = {0};
+    int infd = open("teliva_editbuffer", O_RDONLY);
+    read(infd, new_contents, 8190);  /* TODO: handle overly large file */
+    close(infd);
+
+    /* save contents back into image */
+    lua_pop(L, 1);
+    lua_pushstring(L, new_contents);
+    lua_setfield(L, -2, name);
+
+    /* save teliva_program to disk */
+    int table = lua_gettop(L);
+    FILE* fp = fopen(Image_name, "w");
+    fprintf(fp, "teliva_program = {\n");
+    for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
+      const char* key = lua_tostring(L, -2);
+      const char* value = lua_tostring(L, -1);
+      fprintf(fp, "  %s = [[", key);
+      fprintf(fp, "%s", value);
+      fprintf(fp, "]],\n");
+    }
+    fprintf(fp, "}\n");
+    fclose(fp);
+
+    /* reload binding */
+    dostring(L, new_contents, name);
+    /* TODO: handle error */
+}
+
 
 const char *Previous_error = NULL;
 static int show_error_in_editor (lua_State *L, int status) {