diff options
Diffstat (limited to 'vis-lua.c')
| -rw-r--r-- | vis-lua.c | 30 |
1 files changed, 27 insertions, 3 deletions
@@ -1234,14 +1234,34 @@ static int exit_func(lua_State *L) { * @treturn string stdout the data written to stdout * @treturn string stderr the data written to stderr */ +/*** + * Pipe a string to external process and collect output. + * + * The editor core will be blocked while the external process is running. + * + * @function pipe + * @tparam string text the text written to the external command + * @tparam string command the command to execute + * @tparam[opt] bool fullscreen whether command is a fullscreen program (e.g. curses based) + * @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"); int cmd_idx = 4; char *out = NULL, *err = NULL; + const char *text = NULL; File *file = vis->win ? vis->win->file : NULL; Filerange range = text_range_new(0, 0); - if (lua_gettop(L) <= 3) { + if (lua_gettop(L) == 2) { cmd_idx = 2; + } else if (lua_gettop(L) == 3) { + text = luaL_checkstring(L, 2); + if (text != NULL) + cmd_idx = 3; + else + cmd_idx = 2; } else if (!(lua_isnil(L, 2) && lua_isnil(L, 3))) { file = obj_ref_check(L, 2, VIS_LUA_TYPE_FILE); range = getrange(L, 3); @@ -1249,10 +1269,14 @@ static int pipe_func(lua_State *L) { const char *cmd = luaL_checkstring(L, cmd_idx); bool fullscreen = lua_isboolean(L, cmd_idx + 1) && lua_toboolean(L, cmd_idx + 1); - if (!file) + if (!text && !file) return luaL_error(L, "vis:pipe(cmd = '%s'): win not open, file can't be nil", cmd); - int status = vis_pipe_collect(vis, file, &range, (const char*[]){ cmd, NULL }, &out, &err, fullscreen); + int status; + if (text) + status = vis_pipe_buf_collect(vis, text, (const char*[]){ cmd, NULL }, &out, &err, fullscreen); + else + status = vis_pipe_collect(vis, file, &range, (const char*[]){ cmd, NULL }, &out, &err, fullscreen); lua_pushinteger(L, status); if (out) lua_pushstring(L, out); |
