From 750cc3187fa66c207d8dc9f10389f4f7ef958ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 3 Mar 2017 11:11:03 +0100 Subject: vis-lua: add vis:exit function --- vis-lua.c | 21 +++++++++++++++++++++ vis.c | 3 +++ 2 files changed, 24 insertions(+) diff --git a/vis-lua.c b/vis-lua.c index a818a6a..726f098 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -1100,6 +1100,26 @@ static int replace(lua_State *L) { return 0; } +/*** + * 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 @@ -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); -- cgit v1.2.3