1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-19 09:21:30 +03:00

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.
This commit is contained in:
Maxime Coste 2012-06-25 19:40:18 +02:00
parent cffb895797
commit 7fb49b183e
3 changed files with 15 additions and 10 deletions

View File

@ -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();

View File

@ -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<Regex, EnvVarRetriever>& 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) });
}
}

View File

@ -10,7 +10,7 @@ namespace Kakoune
{
class Context;
typedef std::function<String (const Context&)> EnvVarRetriever;
typedef std::function<String (const String& name, const Context&)> EnvVarRetriever;
typedef std::unordered_map<String, String> EnvVarMap;
class ShellManager : public Singleton<ShellManager>
@ -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<String, EnvVarRetriever> m_env_vars;
std::vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;
};
}