Add a %val{...} expand type

%val{name} gives access to values given in shell through $kak_<name>
This commit is contained in:
Maxime Coste 2014-06-18 19:28:48 +01:00
parent 62707aa82d
commit 9aa8c8a60a
3 changed files with 23 additions and 13 deletions

View File

@ -55,6 +55,7 @@ struct Token
ShellExpand,
RegisterExpand,
OptionExpand,
ValExpand,
CommandSeparator
};
Token() : m_type(Type::Raw) {}
@ -150,6 +151,8 @@ Token::Type token_type(StringView type_name)
return Token::Type::RegisterExpand;
else if (type_name == "opt")
return Token::Type::OptionExpand;
else if (type_name == "val")
return Token::Type::ValExpand;
else if (type_name == "rec")
return Token::Type::RawEval;
else if (throw_on_invalid)
@ -316,6 +319,8 @@ String eval_token(const Token& token, Context& context,
return RegisterManager::instance()[content].values(context)[0];
case Token::Type::OptionExpand:
return context.options()[content].get_as_string();
case Token::Type::ValExpand:
return ShellManager::instance().get_val(content, context);
case Token::Type::RawEval:
return eval(content, context, shell_params, env_vars);
case Token::Type::Raw:

View File

@ -107,23 +107,14 @@ String ShellManager::pipe(StringView input,
if (local_var != env_vars.end())
setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1);
else
{
auto env_var = std::find_if(
m_env_vars.begin(), m_env_vars.end(),
[&](const std::pair<Regex, EnvVarRetriever>& pair)
{ return boost::regex_match(name.begin(), name.end(),
pair.first); });
if (env_var != m_env_vars.end())
{
try
{
String value = env_var->second(name, context);
String value = get_val(name, context);
setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
}
catch (runtime_error&) {}
}
}
++it;
}
@ -149,4 +140,17 @@ void ShellManager::register_env_var(StringView regex,
m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) });
}
String ShellManager::get_val(StringView name, const Context& context) const
{
auto env_var = std::find_if(
m_env_vars.begin(), m_env_vars.end(),
[&](const std::pair<Regex, EnvVarRetriever>& pair)
{ return boost::regex_match(name.begin(), name.end(),
pair.first); });
if (env_var == m_env_vars.end())
throw runtime_error("no such env var: " + name);
return env_var->second(name, context);
}
}

View File

@ -28,6 +28,7 @@ public:
int* exit_status = nullptr);
void register_env_var(StringView regex, EnvVarRetriever retriever);
String get_val(StringView name, const Context& context) const;
private:
std::vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;