diff --git a/src/main.cc b/src/main.cc index d068bf27..bb65806c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -433,10 +433,10 @@ int main(int argc, char* argv[]) run_unit_tests(); shell_manager.register_env_var("bufname", - [](const Context& context) + [](const String& name, const Context& context) { return context.buffer().name(); }); shell_manager.register_env_var("selection", - [](const Context& context) + [](const String& name, const Context& context) { return context.window().selections_content().back(); }); register_commands(); register_highlighters(); diff --git a/src/shell_manager.cc b/src/shell_manager.cc index b9f65766..265a8440 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -78,10 +78,15 @@ String ShellManager::pipe(const String& input, setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1); else { - auto env_var = m_env_vars.find(name); + 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()) { - String value = env_var->second(context); + String value = env_var->second(name, context); setenv(("kak_" + name).c_str(), value.c_str(), 1); } } @@ -94,10 +99,10 @@ String ShellManager::pipe(const String& input, return output; } -void ShellManager::register_env_var(const String& name, +void ShellManager::register_env_var(const String& regex, EnvVarRetriever retriever) { - m_env_vars[name] = std::move(retriever); + m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) }); } } diff --git a/src/shell_manager.hh b/src/shell_manager.hh index 0b22da5c..37d47b32 100644 --- a/src/shell_manager.hh +++ b/src/shell_manager.hh @@ -10,7 +10,7 @@ namespace Kakoune { class Context; -typedef std::function EnvVarRetriever; +typedef std::function EnvVarRetriever; typedef std::unordered_map EnvVarMap; class ShellManager : public Singleton @@ -25,11 +25,11 @@ public: const String& cmdline, const Context& context, const EnvVarMap& env_vars); - void register_env_var(const String& name, EnvVarRetriever retriever); + void register_env_var(const String& regex, EnvVarRetriever retriever); private: - Regex m_regex; - std::unordered_map m_env_vars; + Regex m_regex; + std::vector> m_env_vars; }; }