diff options
| author | Johannes Altmanninger <aclopte@gmail.com> | 2023-02-12 20:51:28 +0100 |
|---|---|---|
| committer | Johannes Altmanninger <aclopte@gmail.com> | 2023-02-17 20:50:58 +0100 |
| commit | 213ea922b16bf95c5b441ce2cae2de6d221f638a (patch) | |
| tree | 96cc2e01ea2702c7e9c480e5bee6538fefb6b816 /src/commands.cc | |
| parent | 9e0502a1ca234bd20e17f078e75c3d5da4687a82 (diff) | |
Complete arguments to "echo -to-file"
Including this here because grandparent parent commit broke completions
for "edit -fifo".
Diffstat (limited to 'src/commands.cc')
| -rw-r--r-- | src/commands.cc | 173 |
1 files changed, 97 insertions, 76 deletions
diff --git a/src/commands.cc b/src/commands.cc index 5dc5f3f7..6edc308b 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -128,6 +128,27 @@ auto filename_completer = make_completer( cursor_pos, FilenameFlags::Expand), menu ? Completions::Flags::Menu : Completions::Flags::None}; }); +template<bool menu> +auto filename_arg_completer = + [](const Context& context, CompletionFlags flags, StringView prefix, ByteCount cursor_pos) -> Completions + { return { 0_byte, cursor_pos, + complete_filename(prefix, + context.options()["ignored_files"].get<Regex>(), + cursor_pos, FilenameFlags::OnlyDirectories), + menu ? Completions::Flags::Menu : Completions::Flags::None }; }; + +auto client_arg_completer = + [](const Context& context, CompletionFlags flags, StringView prefix, ByteCount cursor_pos) -> Completions + { return { 0_byte, cursor_pos, + ClientManager::instance().complete_client_name(prefix, cursor_pos), + Completions::Flags::Menu }; }; + +auto arg_completer = [](auto candidates) -> PromptCompleter { + return [=](const Context& context, CompletionFlags flags, StringView prefix, ByteCount cursor_pos) -> Completions { + return Completions{ 0_byte, cursor_pos, complete(prefix, cursor_pos, candidates), Completions::Flags::Menu }; + }; +}; + template<bool ignore_current = false> static Completions complete_buffer_name(const Context& context, CompletionFlags flags, StringView prefix, ByteCount cursor_pos) @@ -444,12 +465,12 @@ void edit(const ParametersParser& parser, Context& context, const ShellContext&) } ParameterDesc edit_params{ - { { "existing", { false, "fail if the file does not exist, do not open a new file" } }, - { "scratch", { false, "create a scratch buffer, not linked to a file" } }, - { "debug", { false, "create buffer as debug output" } }, - { "fifo", { true, "create a buffer reading its content from a named fifo" } }, - { "readonly", { false, "create a buffer in readonly mode" } }, - { "scroll", { false, "place the initial cursor so that the fifo will scroll to show new data" } } }, + { { "existing", { {}, "fail if the file does not exist, do not open a new file" } }, + { "scratch", { {}, "create a scratch buffer, not linked to a file" } }, + { "debug", { {}, "create buffer as debug output" } }, + { "fifo", { {filename_arg_completer<true>}, "create a buffer reading its content from a named fifo" } }, + { "readonly", { {}, "create a buffer in readonly mode" } }, + { "scroll", { {}, "place the initial cursor so that the fifo will scroll to show new data" } } }, ParameterDesc::Flags::None, 0, 3 }; const CommandDesc edit_cmd = { @@ -477,17 +498,17 @@ const CommandDesc force_edit_cmd = { const ParameterDesc write_params = { { - { "sync", { false, "force the synchronization of the file onto the filesystem" } }, - { "method", { true, "explicit writemethod (replace|overwrite)" } }, - { "force", { false, "Allow overwriting existing file with explicit filename" } } + { "sync", { {}, "force the synchronization of the file onto the filesystem" } }, + { "method", { {arg_completer(Array{"replace", "overwrite"})}, "explicit writemethod (replace|overwrite)" } }, + { "force", { {}, "Allow overwriting existing file with explicit filename" } } }, ParameterDesc::Flags::None, 0, 1 }; const ParameterDesc write_params_except_force = { { - { "sync", { false, "force the synchronization of the file onto the filesystem" } }, - { "method", { true, "explicit writemethod (replace|overwrite)" } }, + { "sync", { {}, "force the synchronization of the file onto the filesystem" } }, + { "method", { {arg_completer(Array{"replace", "overwrite"})}, "explicit writemethod (replace|overwrite)" } }, }, ParameterDesc::Flags::None, 0, 1 }; @@ -872,8 +893,8 @@ const CommandDesc rename_buffer_cmd = { "rename-buffer <name>: change current buffer name", ParameterDesc{ { - { "scratch", { false, "convert a file buffer to a scratch buffer" } }, - { "file", { false, "convert a scratch buffer to a file buffer" } } + { "scratch", { {}, "convert a file buffer to a scratch buffer" } }, + { "file", { {}, "convert a scratch buffer to a file buffer" } } }, ParameterDesc::Flags::None, 1, 1 }, @@ -997,7 +1018,7 @@ const CommandDesc add_highlighter_cmd = { " <path> is a '/' delimited path or the parent highlighter, starting with either\n" " 'global', 'buffer', 'window' or 'shared', if <name> is empty, it will be autogenerated", ParameterDesc{ - { { "override", { false, "replace existing highlighter with same path if it exists" } }, }, + { { "override", { {}, "replace existing highlighter with same path if it exists" } }, }, ParameterDesc::Flags::SwitchesOnlyAtStart, 2 }, CommandFlags::None, @@ -1097,9 +1118,9 @@ const CommandDesc add_hook_cmd = { " (and any window for that buffer)\n" " * window: hook is executed only for the current window\n", ParameterDesc{ - { { "group", { true, "set hook group, see remove-hooks" } }, - { "always", { false, "run hook even if hooks are disabled" } }, - { "once", { false, "run the hook only once" } } }, + { { "group", { ArgCompleter{}, "set hook group, see remove-hooks" } }, + { "always", { {}, "run hook even if hooks are disabled" } }, + { "once", { {}, "run the hook only once" } } }, ParameterDesc::Flags::None, 4, 4 }, CommandFlags::None, @@ -1335,19 +1356,19 @@ const CommandDesc define_command_cmd = { "def", "define-command [<switches>] <name> <cmds>: define a command <name> executing <cmds>", ParameterDesc{ - { { "params", { true, "take parameters, accessible to each shell escape as $0..$N\n" + { { "params", { ArgCompleter{}, "take parameters, accessible to each shell escape as $0..$N\n" "parameter should take the form <count> or <min>..<max> (both omittable)" } }, - { "override", { false, "allow overriding an existing command" } }, - { "hidden", { false, "do not display the command in completion candidates" } }, - { "docstring", { true, "define the documentation string for command" } }, - { "menu", { false, "treat completions as the only valid inputs" } }, - { "file-completion", { false, "complete parameters using filename completion" } }, - { "client-completion", { false, "complete parameters using client name completion" } }, - { "buffer-completion", { false, "complete parameters using buffer name completion" } }, - { "command-completion", { false, "complete parameters using kakoune command completion" } }, - { "shell-completion", { false, "complete parameters using shell command completion" } }, - { "shell-script-completion", { true, "complete parameters using the given shell-script" } }, - { "shell-script-candidates", { true, "get the parameter candidates using the given shell-script" } } }, + { "override", { {}, "allow overriding an existing command" } }, + { "hidden", { {}, "do not display the command in completion candidates" } }, + { "docstring", { ArgCompleter{}, "define the documentation string for command" } }, + { "menu", { {}, "treat completions as the only valid inputs" } }, + { "file-completion", { {}, "complete parameters using filename completion" } }, + { "client-completion", { {}, "complete parameters using client name completion" } }, + { "buffer-completion", { {}, "complete parameters using buffer name completion" } }, + { "command-completion", { {}, "complete parameters using kakoune command completion" } }, + { "shell-completion", { {}, "complete parameters using shell command completion" } }, + { "shell-script-completion", { ArgCompleter{}, "complete parameters using the given shell-script" } }, + { "shell-script-candidates", { ArgCompleter{}, "get the parameter candidates using the given shell-script" } } }, ParameterDesc::Flags::None, 2, 2 }, @@ -1408,7 +1429,7 @@ const CommandDesc complete_command_cmd = { "complete-command [<switches>] <name> <type> [<param>]\n" "define command completion", ParameterDesc{ - { { "menu", { false, "treat completions as the only valid inputs" } }, }, + { { "menu", { {}, "treat completions as the only valid inputs" } }, }, ParameterDesc::Flags::None, 2, 3}, CommandFlags::None, CommandHelper{}, @@ -1426,11 +1447,11 @@ const CommandDesc echo_cmd = { nullptr, "echo <params>...: display given parameters in the status line", ParameterDesc{ - { { "markup", { false, "parse markup" } }, - { "quoting", { true, "quote each argument separately using the given style (raw|kakoune|shell)" } }, - { "to-file", { true, "echo contents to given filename" } }, - { "to-shell-script", { true, "pipe contents to given shell script" } }, - { "debug", { false, "write to debug buffer instead of status line" } } }, + { { "markup", { {}, "parse markup" } }, + { "quoting", { {arg_completer(Array{"raw", "kakoune", "shell"})}, "quote each argument separately using the given style (raw|kakoune|shell)" } }, + { "to-file", { {filename_arg_completer<false>}, "echo contents to given filename" } }, + { "to-shell-script", { ArgCompleter{}, "pipe contents to given shell script" } }, + { "debug", { {}, "write to debug buffer instead of status line" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart }, CommandFlags::None, @@ -1666,8 +1687,8 @@ const CommandDesc set_option_cmd = { "<scope> can be global, buffer, window, or current which refers to the narrowest " "scope the option is set in", ParameterDesc{ - { { "add", { false, "add to option rather than replacing it" } }, - { "remove", { false, "remove from option rather than replacing it" } } }, + { { "add", { {}, "add to option rather than replacing it" } }, + { "remove", { {}, "remove from option rather than replacing it" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart, 2, (size_t)-1 }, CommandFlags::None, @@ -1776,8 +1797,8 @@ const CommandDesc declare_option_cmd = { " range-specs: list of range specs\n" " str-to-str-map: map from strings to strings\n", ParameterDesc{ - { { "hidden", { false, "do not display option name when completing" } }, - { "docstring", { true, "specify option description" } } }, + { { "hidden", { {}, "do not display option name when completing" } }, + { "docstring", { ArgCompleter{}, "specify option description" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart, 2, (size_t)-1 }, CommandFlags::None, @@ -1862,7 +1883,7 @@ const CommandDesc map_key_cmd = { nullptr, "map [<switches>] <scope> <mode> <key> <keys>: map <key> to <keys> in given <mode> in <scope>", ParameterDesc{ - { { "docstring", { true, "specify mapping description" } } }, + { { "docstring", { ArgCompleter{}, "specify mapping description" } } }, ParameterDesc::Flags::None, 4, 4 }, CommandFlags::None, @@ -1920,11 +1941,11 @@ template<size_t... P> ParameterDesc make_context_wrap_params_impl(Array<HashItem<String, SwitchDesc>, sizeof...(P)>&& additional_params, std::index_sequence<P...>) { - return { { { "client", { true, "run in given client context" } }, - { "try-client", { true, "run in given client context if it exists, or else in the current one" } }, - { "buffer", { true, "run in a disposable context for each given buffer in the comma separated list argument" } }, - { "draft", { false, "run in a disposable context" } }, - { "itersel", { false, "run once for each selection with that selection as the only one" } }, + return { { { "client", { {client_arg_completer}, "run in given client context" } }, + { "try-client", { {client_arg_completer}, "run in given client context if it exists, or else in the current one" } }, + { "buffer", { {complete_buffer_name<false>}, "run in a disposable context for each given buffer in the comma separated list argument" } }, + { "draft", { {}, "run in a disposable context" } }, + { "itersel", { {}, "run once for each selection with that selection as the only one" } }, std::move(additional_params[P])...}, ParameterDesc::Flags::SwitchesOnlyAtStart, 1 }; @@ -2089,9 +2110,9 @@ const CommandDesc execute_keys_cmd = { "exec", "execute-keys [<switches>] <keys>: execute given keys as if entered by user", make_context_wrap_params<3>({{ - {"save-regs", {true, "restore all given registers after execution (default: '/\"|^@:')"}}, - {"with-maps", {false, "use user defined key mapping when executing keys"}}, - {"with-hooks", {false, "trigger hooks while executing keys"}} + {"save-regs", {ArgCompleter{}, "restore all given registers after execution (default: '/\"|^@:')"}}, + {"with-maps", {{}, "use user defined key mapping when executing keys"}}, + {"with-hooks", {{}, "trigger hooks while executing keys"}} }}), CommandFlags::None, CommandHelper{}, @@ -2113,9 +2134,9 @@ const CommandDesc evaluate_commands_cmd = { "eval", "evaluate-commands [<switches>] <commands>...: execute commands as if entered by user", make_context_wrap_params<3>({{ - {"save-regs", {true, "restore all given registers after execution (default: '')"}}, - {"no-hooks", { false, "disable hooks while executing commands" }}, - {"verbatim", { false, "do not reparse argument" }} + {"save-regs", {ArgCompleter{}, "restore all given registers after execution (default: '')"}}, + {"no-hooks", { {}, "disable hooks while executing commands" }}, + {"verbatim", { {}, "do not reparse argument" }} }}), CommandFlags::None, CommandHelper{}, @@ -2151,18 +2172,18 @@ const CommandDesc prompt_cmd = { "prompt [<switches>] <prompt> <command>: prompt the user to enter a text string " "and then executes <command>, entered text is available in the 'text' value", ParameterDesc{ - { { "init", { true, "set initial prompt content" } }, - { "password", { false, "Do not display entered text and clear reg after command" } }, - { "menu", { false, "treat completions as the only valid inputs" } }, - { "file-completion", { false, "use file completion for prompt" } }, - { "client-completion", { false, "use client completion for prompt" } }, - { "buffer-completion", { false, "use buffer completion for prompt" } }, - { "command-completion", { false, "use command completion for prompt" } }, - { "shell-completion", { false, "use shell command completion for prompt" } }, - { "shell-script-completion", { true, "use shell command completion for prompt" } }, - { "shell-script-candidates", { true, "use shell command completion for prompt" } }, - { "on-change", { true, "command to execute whenever the prompt changes" } }, - { "on-abort", { true, "command to execute whenever the prompt is canceled" } } }, + { { "init", { ArgCompleter{}, "set initial prompt content" } }, + { "password", { {}, "Do not display entered text and clear reg after command" } }, + { "menu", { {}, "treat completions as the only valid inputs" } }, + { "file-completion", { {}, "use file completion for prompt" } }, + { "client-completion", { {}, "use client completion for prompt" } }, + { "buffer-completion", { {}, "use buffer completion for prompt" } }, + { "command-completion", { {}, "use command completion for prompt" } }, + { "shell-completion", { {}, "use shell command completion for prompt" } }, + { "shell-script-completion", { ArgCompleter{}, "use shell command completion for prompt" } }, + { "shell-script-candidates", { ArgCompleter{}, "use shell command completion for prompt" } }, + { "on-change", { ArgCompleter{}, "command to execute whenever the prompt changes" } }, + { "on-abort", { ArgCompleter{}, "command to execute whenever the prompt is canceled" } } }, ParameterDesc::Flags::None, 2, 2 }, CommandFlags::None, @@ -2226,9 +2247,9 @@ const CommandDesc menu_cmd = { "menu [<switches>] <name1> <commands1> <name2> <commands2>...: display a " "menu and execute commands for the selected item", ParameterDesc{ - { { "auto-single", { false, "instantly validate if only one item is available" } }, - { "select-cmds", { false, "each item specify an additional command to run when selected" } }, - { "markup", { false, "parse menu entries as markup text" } } } + { { "auto-single", { {}, "instantly validate if only one item is available" } }, + { "select-cmds", { {}, "each item specify an additional command to run when selected" } }, + { "markup", { {}, "parse menu entries as markup text" } } } }, CommandFlags::None, CommandHelper{}, @@ -2285,7 +2306,7 @@ const CommandDesc on_key_cmd = { "on-key [<switches>] <command>: wait for next user key and then execute <command>, " "with key available in the `key` value", ParameterDesc{ - { { "mode-name", { true, "set mode name to use" } } }, + { { "mode-name", { ArgCompleter{}, "set mode name to use" } } }, ParameterDesc::Flags::None, 1, 1 }, CommandFlags::None, @@ -2312,10 +2333,10 @@ const CommandDesc info_cmd = { nullptr, "info [<switches>] <text>: display an info box containing <text>", ParameterDesc{ - { { "anchor", { true, "set info anchoring <line>.<column>" } }, - { "style", { true, "set info style (above, below, menu, modal)" } }, - { "markup", { false, "parse markup" } }, - { "title", { true, "set info title" } } }, + { { "anchor", { ArgCompleter{}, "set info anchoring <line>.<column>" } }, + { "style", { {arg_completer(Array{"above", "below", "menu", "modal"})}, "set info style (above, below, menu, modal)" } }, + { "markup", { {}, "parse markup" } }, + { "title", { ArgCompleter{}, "set info title" } } }, ParameterDesc::Flags::None, 0, 1 }, CommandFlags::None, @@ -2518,9 +2539,9 @@ const CommandDesc select_cmd = { "\n" "selection_desc format is <anchor_line>.<anchor_column>,<cursor_line>.<cursor_column>", ParameterDesc{{ - {"timestamp", {true, "specify buffer timestamp at which those selections are valid"}}, - {"codepoint", {false, "columns are specified in codepoints, not bytes"}}, - {"display-column", {false, "columns are specified in display columns, not bytes"}} + {"timestamp", {ArgCompleter{}, "specify buffer timestamp at which those selections are valid"}}, + {"codepoint", {{}, "columns are specified in codepoints, not bytes"}}, + {"display-column", {{}, "columns are specified in display columns, not bytes"}} }, ParameterDesc::Flags::SwitchesOnlyAtStart, 1 }, @@ -2642,7 +2663,7 @@ const CommandDesc enter_user_mode_cmd = { nullptr, "enter-user-mode [<switches>] <name>: enable <name> keymap mode for next key", ParameterDesc{ - { { "lock", { false, "stay in mode until <esc> is pressed" } } }, + { { "lock", { {}, "stay in mode until <esc> is pressed" } } }, ParameterDesc::Flags::None, 1, 1 }, CommandFlags::None, @@ -2672,7 +2693,7 @@ const CommandDesc provide_module_cmd = { nullptr, "provide-module [<switches>] <name> <cmds>: declares a module <name> provided by <cmds>", ParameterDesc{ - { { "override", { false, "allow overriding an existing module" } } }, + { { "override", { {}, "allow overriding an existing module" } } }, ParameterDesc::Flags::None, 2, 2 }, |
