about summary refs log tree commit diff stats
path: root/src/lua.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-06 13:10:22 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-11-06 13:10:22 -0700
commit3305ac0b90ae7f41de7f288fb0fea72f616f617c (patch)
tree0c1cae0278c7e1961e7856a2641a0841a3fe0cc7 /src/lua.c
parent396684ebf1f8bb7751d14452f1b56fe6a27eb1f1 (diff)
downloadteliva-3305ac0b90ae7f41de7f288fb0fea72f616f617c.tar.gz
start showing error messages in editor
Before we'd end up in cryptic situations where error messages would get
hidden when the program got out of ncurses mode.

Now it's a little nicer with error messages showing up at the bottom of
the editor.

But there's still a problem: there's no way to abort without fixing an
error.
Diffstat (limited to 'src/lua.c')
-rw-r--r--src/lua.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/lua.c b/src/lua.c
index f2676c0..1c7cdfd 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -10,6 +10,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #define lua_c
 
@@ -73,6 +74,21 @@ static int report (lua_State *L, int status) {
 }
 
 
+char *Script_name = NULL;
+char **Argv = NULL;
+extern void edit(char *filename, const char *status);
+static int show_error_in_editor (lua_State *L, int status) {
+  if (status && !lua_isnil(L, -1)) {
+    const char *msg = lua_tostring(L, -1);
+    if (msg == NULL) msg = "(error object is not a string)";
+    endwin();
+    edit(Script_name, msg);
+    execv(Argv[0], Argv);
+  }
+  return status;
+}
+
+
 static int traceback (lua_State *L) {
   if (!lua_isstring(L, 1))  /* 'message' not a string? */
     return 1;  /* keep it intact */
@@ -234,7 +250,6 @@ static void dotty (lua_State *L) {
 }
 
 
-const char *Script_name = NULL;
 static int handle_script (lua_State *L, char **argv, int n) {
   int status;
   int narg = getargs(L, argv, n);  /* collect arguments */
@@ -246,7 +261,7 @@ static int handle_script (lua_State *L, char **argv, int n) {
     status = docall(L, narg, 0);
   else
     lua_pop(L, narg);
-  return report(L, status);
+  return show_error_in_editor(L, status);
 }
 
 
@@ -369,7 +384,6 @@ static int pmain (lua_State *L) {
 
 
 void draw_menu(lua_State *);
-char **Argv = NULL;
 
 
 int main (int argc, char **argv) {
@@ -390,7 +404,6 @@ int main (int argc, char **argv) {
   status = lua_cpcall(L, &pmain, &s);
   report(L, status);
   lua_close(L);
-//?   getch();  /* uncomment this to see some common errors if teliva exits cryptically with a non-zero error code. */
   endwin();
   return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
 }