From 368fdf04acd490edfb6561796696ded79c3bf9a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sun, 5 Mar 2017 11:03:43 +0100 Subject: vis-lua: expose vis:pipe function --- vis-lua.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'vis-lua.c') diff --git a/vis-lua.c b/vis-lua.c index 726f098..2186e45 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -1120,6 +1120,40 @@ static int exit_func(lua_State *L) { return 0; } +/*** + * Pipe file range to external process and collect output. + * + * The editor core will be blocked while the external process is running. + * + * @function pipe + * @tparam File file the file to which the range applies + * @tparam Range range the range to pipe + * @tparam string command the command to execute + * @treturn int code the exit status of the executed command + * @treturn string stdout the data written to stdout + * @treturn string stderr the data written to stderr + */ +static int pipe_func(lua_State *L) { + Vis *vis = obj_ref_check(L, 1, "vis"); + File *file = obj_ref_check(L, 2, VIS_LUA_TYPE_FILE); + Filerange range = getrange(L, 3); + const char *cmd = luaL_checkstring(L, 4); + char *out = NULL, *err = NULL; + int status = vis_pipe_collect(vis, file, &range, (const char*[]){ cmd, NULL }, &out, &err); + lua_pushinteger(L, status); + if (out) + lua_pushstring(L, out); + else + lua_pushnil(L); + free(out); + if (err) + lua_pushstring(L, err); + else + lua_pushnil(L); + free(err); + vis_draw(vis); + return 3; +} /*** * Currently active window. * @tfield Window win @@ -1225,6 +1259,7 @@ static const struct luaL_Reg vis_lua[] = { { "replace", replace }, { "action_register", action_register }, { "exit", exit_func }, + { "pipe", pipe_func }, { "__index", vis_index }, { "__newindex", vis_newindex }, { NULL, NULL }, -- cgit v1.2.3