aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--vis-lua.c21
-rw-r--r--vis.c3
2 files changed, 24 insertions, 0 deletions
diff --git a/vis-lua.c b/vis-lua.c
index a818a6a..726f098 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -1101,6 +1101,26 @@ static int replace(lua_State *L) {
}
/***
+ * Terminate editor process.
+ *
+ * Termination happens upon the next iteration of the main event loop.
+ * This means the calling Lua code will be executed further until it
+ * eventually hands over control to the editor core. The exit status
+ * of the most recent call is used.
+ *
+ * All unsaved chanes will be lost!
+ *
+ * @function exit
+ * @tparam int code the exit status returned to the operating system
+ */
+static int exit_func(lua_State *L) {
+ Vis *vis = obj_ref_check(L, 1, "vis");
+ int code = luaL_checkint(L, 2);
+ vis_exit(vis, code);
+ return 0;
+}
+
+/***
* Currently active window.
* @tfield Window win
* @see windows
@@ -1204,6 +1224,7 @@ static const struct luaL_Reg vis_lua[] = {
{ "insert", insert },
{ "replace", replace },
{ "action_register", action_register },
+ { "exit", exit_func },
{ "__index", vis_index },
{ "__newindex", vis_newindex },
{ NULL, NULL },
diff --git a/vis.c b/vis.c
index 1595d1e..51ab041 100644
--- a/vis.c
+++ b/vis.c
@@ -529,6 +529,7 @@ Vis *vis_new(Ui *ui, VisEvent *event) {
Vis *vis = calloc(1, sizeof(Vis));
if (!vis)
return NULL;
+ vis->exit_status = -1;
vis->ui = ui;
vis->tabwidth = 8;
vis->expandtab = false;
@@ -1149,6 +1150,8 @@ bool vis_signal_handler(Vis *vis, int signum, const siginfo_t *siginfo, const vo
int vis_run(Vis *vis, int argc, char *argv[]) {
if (!vis->windows)
return EXIT_SUCCESS;
+ if (vis->exit_status != -1)
+ return vis->exit_status;
vis->running = true;
vis_event_emit(vis, VIS_EVENT_START);