From b2648053f90ae076f94d59f551e512a73db2f8db Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 1 Dec 2015 20:07:14 +0000 Subject: [PATCH] Add argument expansion support --- README.asciidoc | 3 +++ src/command_manager.cc | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/README.asciidoc b/README.asciidoc index 70afe7dd..f9089e5d 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -629,6 +629,9 @@ Supported types are: option * `val`: value expansion, gives access to the environment variable available to the Shell expansion. The `kak_` prefix is not used there. + * `arg`: argument expansion, gives access to the arguments of the current + command, the content can be a number, or `#` for the argument count, + or `@` for all arguments concatenated. for example you can display last search pattern with diff --git a/src/command_manager.cc b/src/command_manager.cc index 0f689fa7..ad7359b5 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -54,6 +54,7 @@ struct Token RegisterExpand, OptionExpand, ValExpand, + ArgExpand, CommandSeparator }; Token() : m_type(Type::Raw) {} @@ -198,6 +199,8 @@ Token::Type token_type(StringView type_name) return Token::Type::OptionExpand; else if (type_name == "val") return Token::Type::ValExpand; + else if (type_name == "arg") + return Token::Type::ArgExpand; else if (throw_on_invalid) throw unknown_expand{type_name}; else @@ -343,6 +346,20 @@ String expand_token(const Token& token, const Context& context, return it->value; return ShellManager::instance().get_val(content, context); } + case Token::Type::ArgExpand: + { + if (content == "#") + return to_string(shell_context.params.size()); + else if (content == "@") + return join(shell_context.params, ' '); + + const int arg = str_to_int(content)-1; + if (arg < 0) + throw runtime_error("invalid argument index"); + if (arg < shell_context.params.size()) + return shell_context.params[arg]; + return {}; + } case Token::Type::RawEval: return expand(content, context, shell_context); case Token::Type::Raw: