From 7fb49b183ea5caa645b76055d5531846b52f487b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 25 Jun 2012 19:40:18 +0200 Subject: [PATCH] ShellManager environment variable use a regex instead of an exact match when a kakoune releated env var used in a shell command, the ShellManager tries to match it with given regex and the first that matches calls its value retriever. For this to be useful, EnvVarRetrievers now also take the variable name in its parameters. --- src/main.cc | 4 ++-- src/shell_manager.cc | 13 +++++++++---- src/shell_manager.hh | 8 ++++---- 3 files changed, 15 insertions(+), 10 deletions(-) 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; }; }