From c56b57fc58c73e96def91f4904d5b6e7c50e3de9 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 22 May 2024 11:37:50 +0200 Subject: support piping a buffer to an external process Currently only Text objects can be piped to external commands. This is tedious if data not available in any file should be passed to an external process (e.g. building options and passing them to vis-menu). This adds the option to pass a buffer to _vis_pipe and provides wrapper functions for the original behavior and the new one. --- vis-lua.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'vis-lua.c') diff --git a/vis-lua.c b/vis-lua.c index e3e85c3..0a435b4 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -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); -- cgit v1.2.3