From c00ef937ac0e6bc6391f973783e900ff5691f4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 20 Nov 2017 13:15:01 +0100 Subject: vis-lua: implement window:close --- vis-lua.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/vis-lua.c b/vis-lua.c index 87e7f01..3c998e7 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -1741,6 +1741,33 @@ static int window_draw(lua_State *L) { return 0; } +/*** + * Close window. + * + * After a successful call the Window reference becomes invalid and + * must no longer be used. Attempting to close the last window will + * always fail. + * + * @function close + * @see exit + * @tparam bool force whether unsaved changes should be discarded + * @treturn bool whether the window was closed + */ +static int window_close(lua_State *L) { + Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW); + int count = 0; + for (Win *w = win->vis->windows; w; w = w->next) { + if (!w->file->internal) + count++; + } + bool force = lua_isboolean(L, 2) && lua_toboolean(L, 2); + bool close = count > 1 && (force || vis_window_closable(win)); + if (close) + vis_window_close(win); + lua_pushboolean(L, close); + return 1; +} + static const struct luaL_Reg window_funcs[] = { { "__index", window_index }, { "__newindex", newindex_common }, @@ -1751,6 +1778,7 @@ static const struct luaL_Reg window_funcs[] = { { "style", window_style }, { "status", window_status }, { "draw", window_draw }, + { "close", window_close }, { NULL, NULL }, }; -- cgit v1.2.3