Add a %val{...} expand type
%val{name} gives access to values given in shell through $kak_<name>
This commit is contained in:
parent
62707aa82d
commit
9aa8c8a60a
|
@ -55,6 +55,7 @@ struct Token
|
||||||
ShellExpand,
|
ShellExpand,
|
||||||
RegisterExpand,
|
RegisterExpand,
|
||||||
OptionExpand,
|
OptionExpand,
|
||||||
|
ValExpand,
|
||||||
CommandSeparator
|
CommandSeparator
|
||||||
};
|
};
|
||||||
Token() : m_type(Type::Raw) {}
|
Token() : m_type(Type::Raw) {}
|
||||||
|
@ -150,6 +151,8 @@ Token::Type token_type(StringView type_name)
|
||||||
return Token::Type::RegisterExpand;
|
return Token::Type::RegisterExpand;
|
||||||
else if (type_name == "opt")
|
else if (type_name == "opt")
|
||||||
return Token::Type::OptionExpand;
|
return Token::Type::OptionExpand;
|
||||||
|
else if (type_name == "val")
|
||||||
|
return Token::Type::ValExpand;
|
||||||
else if (type_name == "rec")
|
else if (type_name == "rec")
|
||||||
return Token::Type::RawEval;
|
return Token::Type::RawEval;
|
||||||
else if (throw_on_invalid)
|
else if (throw_on_invalid)
|
||||||
|
@ -316,6 +319,8 @@ String eval_token(const Token& token, Context& context,
|
||||||
return RegisterManager::instance()[content].values(context)[0];
|
return RegisterManager::instance()[content].values(context)[0];
|
||||||
case Token::Type::OptionExpand:
|
case Token::Type::OptionExpand:
|
||||||
return context.options()[content].get_as_string();
|
return context.options()[content].get_as_string();
|
||||||
|
case Token::Type::ValExpand:
|
||||||
|
return ShellManager::instance().get_val(content, context);
|
||||||
case Token::Type::RawEval:
|
case Token::Type::RawEval:
|
||||||
return eval(content, context, shell_params, env_vars);
|
return eval(content, context, shell_params, env_vars);
|
||||||
case Token::Type::Raw:
|
case Token::Type::Raw:
|
||||||
|
|
|
@ -108,21 +108,12 @@ String ShellManager::pipe(StringView input,
|
||||||
setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1);
|
setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto env_var = std::find_if(
|
try
|
||||||
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 = get_val(name, context);
|
||||||
{
|
setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
|
||||||
String value = env_var->second(name, context);
|
|
||||||
setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
|
|
||||||
}
|
|
||||||
catch (runtime_error&) {}
|
|
||||||
}
|
}
|
||||||
|
catch (runtime_error&) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
++it;
|
++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) });
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ public:
|
||||||
int* exit_status = nullptr);
|
int* exit_status = nullptr);
|
||||||
|
|
||||||
void register_env_var(StringView regex, EnvVarRetriever retriever);
|
void register_env_var(StringView regex, EnvVarRetriever retriever);
|
||||||
|
String get_val(StringView name, const Context& context) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;
|
std::vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user