Add argument expansion support
This commit is contained in:
parent
550a95a3d7
commit
b2648053f9
|
@ -629,6 +629,9 @@ Supported types are:
|
||||||
option
|
option
|
||||||
* `val`: value expansion, gives access to the environment variable available
|
* `val`: value expansion, gives access to the environment variable available
|
||||||
to the Shell expansion. The `kak_` prefix is not used there.
|
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
|
for example you can display last search pattern with
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,7 @@ struct Token
|
||||||
RegisterExpand,
|
RegisterExpand,
|
||||||
OptionExpand,
|
OptionExpand,
|
||||||
ValExpand,
|
ValExpand,
|
||||||
|
ArgExpand,
|
||||||
CommandSeparator
|
CommandSeparator
|
||||||
};
|
};
|
||||||
Token() : m_type(Type::Raw) {}
|
Token() : m_type(Type::Raw) {}
|
||||||
|
@ -198,6 +199,8 @@ Token::Type token_type(StringView type_name)
|
||||||
return Token::Type::OptionExpand;
|
return Token::Type::OptionExpand;
|
||||||
else if (type_name == "val")
|
else if (type_name == "val")
|
||||||
return Token::Type::ValExpand;
|
return Token::Type::ValExpand;
|
||||||
|
else if (type_name == "arg")
|
||||||
|
return Token::Type::ArgExpand;
|
||||||
else if (throw_on_invalid)
|
else if (throw_on_invalid)
|
||||||
throw unknown_expand{type_name};
|
throw unknown_expand{type_name};
|
||||||
else
|
else
|
||||||
|
@ -343,6 +346,20 @@ String expand_token(const Token& token, const Context& context,
|
||||||
return it->value;
|
return it->value;
|
||||||
return ShellManager::instance().get_val(content, context);
|
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:
|
case Token::Type::RawEval:
|
||||||
return expand(content, context, shell_context);
|
return expand(content, context, shell_context);
|
||||||
case Token::Type::Raw:
|
case Token::Type::Raw:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user