about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-11 15:50:52 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-11 15:50:52 -0800
commit3e7ff252af32b2bd481d045f4b732acbdbe1b612 (patch)
tree1706ee1f7f1f927bb2b29ef04de2ca170c948334
parent1f6ac6597c460bd4a0b4b1be0a298f46fe4bb8f6 (diff)
downloadteliva-3e7ff252af32b2bd481d045f4b732acbdbe1b612.tar.gz
reorg
-rw-r--r--src/kilo.c48
-rw-r--r--src/lua.c44
2 files changed, 44 insertions, 48 deletions
diff --git a/src/kilo.c b/src/kilo.c
index b341a86..e8cb618 100644
--- a/src/kilo.c
+++ b/src/kilo.c
@@ -53,7 +53,6 @@
 #include <stdarg.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <stdio.h>
 
 #include "lua.h"
 
@@ -1267,50 +1266,3 @@ void edit(char* filename, char* message) {
     }
     disableRawMode(STDIN_FILENO);
 }
-
-extern void teliva_get_definition(lua_State *L, const char *name);
-extern void stackDump (lua_State *L);
-extern int dostring (lua_State *L, const char *s, const char *name);
-extern char *Image_name;
-void editString(lua_State *L, char *name) {
-    /* write given definition out to tmp file */
-//?     stackDump(L);
-    teliva_get_definition(L, name);
-//?     stackDump(L);
-    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 */
-}
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) {