From 3f1c3ee80e46f80d7f1642009c4f9dffa404d935 Mon Sep 17 00:00:00 2001 From: Max Schillinger Date: Mon, 18 Mar 2024 10:08:40 +0100 Subject: Add command completion with tab key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the command prompt, press to get a list of all available commands and pick one (using vis-menu). This works also after typing the first letters of a command (p.e. `:la`). Co-authored-by: Matěj Cepl --- vis-lua.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'vis-lua.c') diff --git a/vis-lua.c b/vis-lua.c index 695ebdb..99d9746 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -1146,6 +1146,41 @@ static int command_register(lua_State *L) { return 1; } +/*** + * Let user pick a command matching the given prefix. + * + * The editor core will be blocked while the external process is running. + * + * @function complete_command + * @tparam string prefix the prefix of the command to be completed + * @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 complete_command(lua_State *L) { + Vis *vis = obj_ref_check(L, 1, "vis"); + const char *prefix = luaL_checkstring(L, 2); + char *out = NULL, *err = NULL; + char cmd[32]; + int max_prefix_len = sizeof(cmd) - sizeof("grep '^' | vis-menu -b"); // including NUL + snprintf(cmd, sizeof(cmd), "grep '^%.*s' | vis-menu -b", max_prefix_len, prefix); + + Buffer buf = {0}; + vis_print_cmds(vis, &buf); + int status = vis_pipe_buf_collect(vis, buffer_content0(&buf), (const char*[]){cmd, NULL}, &out, &err, false); + + lua_pushinteger(L, status); + if (out) lua_pushstring(L, out); + else lua_pushnil(L); + if (err) lua_pushstring(L, err); + else lua_pushnil(L); + + free(out); + free(err); + buffer_release(&buf); + return 3; +} + /*** * Push keys to input queue and interpret them. * @@ -1559,6 +1594,7 @@ static const struct luaL_Reg vis_lua[] = { { "option_register", option_register }, { "option_unregister", option_unregister }, { "command_register", command_register }, + { "complete_command", complete_command }, { "feedkeys", feedkeys }, { "insert", insert }, { "replace", replace }, -- cgit v1.2.3