1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-09 20:39:59 +03:00

Add argument expansion support

This commit is contained in:
Maxime Coste 2015-12-01 20:07:14 +00:00
parent 550a95a3d7
commit b2648053f9
2 changed files with 20 additions and 0 deletions

View File

@ -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

View File

@ -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: