From 9aa8c8a60a03396b7ed679537e3ed6b6de02ce8e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 18 Jun 2014 19:28:48 +0100 Subject: [PATCH] Add a %val{...} expand type %val{name} gives access to values given in shell through $kak_ --- src/command_manager.cc | 5 +++++ src/shell_manager.cc | 30 +++++++++++++++++------------- src/shell_manager.hh | 1 + 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index a99f89b3..874d0290 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -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: diff --git a/src/shell_manager.cc b/src/shell_manager.cc index d7caceb5..08c34160 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -108,21 +108,12 @@ String ShellManager::pipe(StringView input, 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& pair) - { return boost::regex_match(name.begin(), name.end(), - pair.first); }); - - if (env_var != m_env_vars.end()) + try { - try - { - String value = env_var->second(name, context); - setenv(("kak_"_str + name).c_str(), value.c_str(), 1); - } - catch (runtime_error&) {} + 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& 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); +} + } diff --git a/src/shell_manager.hh b/src/shell_manager.hh index 16a5f27e..f5d27a03 100644 --- a/src/shell_manager.hh +++ b/src/shell_manager.hh @@ -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> m_env_vars;