From 07d8486f657dded76c6317550377962899f9152f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Thu, 21 Apr 2016 09:50:23 +0200 Subject: vis-lua: add utility function to push a Filerange onto the Lua stack --- vis-lua.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'vis-lua.c') diff --git a/vis-lua.c b/vis-lua.c index 6a47a7c..d0d4368 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -232,6 +232,20 @@ static int newindex_common(lua_State *L) { return 0; } +static void pushrange(lua_State *L, Filerange *r) { + if (!text_range_valid(r)) { + lua_pushnil(L); + return; + } + lua_createtable(L, 0, 2); + lua_pushstring(L, "start"); + lua_pushunsigned(L, r->start); + lua_settable(L, -3); + lua_pushstring(L, "finish"); + lua_pushunsigned(L, r->end); + lua_settable(L, -3); +} + static const char *keymapping(Vis *vis, const char *keys, const Arg *arg) { lua_State *L = vis->lua; if (!func_ref_get(L, arg->v)) @@ -658,17 +672,7 @@ static int window_cursor_index(lua_State *L) { if (strcmp(key, "selection") == 0) { Filerange sel = view_cursors_selection_get(cur); - if (text_range_valid(&sel)) { - lua_createtable(L, 0, 2); - lua_pushstring(L, "start"); - lua_pushunsigned(L, sel.start); - lua_settable(L, -3); - lua_pushstring(L, "finish"); - lua_pushunsigned(L, sel.end); - lua_settable(L, -3); - } else { - lua_pushnil(L); - } + pushrange(L, &sel); return 1; } } -- cgit v1.2.3