vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 389101aa1ea435623b981ed678b3b3270179a1e5
parent fa2d7612f12ae488c890b013ed4e9db03a08c713
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Mon, 20 Nov 2017 13:15:01 +0100

vis-lua: implement window:close

Diffstat:
Mvis-lua.c | 28++++++++++++++++++++++++++++
1 file changed, 28 insertions(+), 0 deletions(-)

diff --git 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 }, };