ShellManager::eval now takes an additional env_var map

This commit is contained in:
Maxime Coste 2012-05-29 05:19:27 +00:00
parent e57ddd3bab
commit 8fedbbf07b
4 changed files with 17 additions and 8 deletions

View File

@ -99,7 +99,7 @@ static void shell_eval(std::vector<String>& params,
const String& cmdline, const String& cmdline,
const Context& context) const Context& context)
{ {
String output = ShellManager::instance().eval(cmdline, context); String output = ShellManager::instance().eval(cmdline, context, {});
TokenList tokens = split(output); TokenList tokens = split(output);
for (auto it = tokens.begin(); it != tokens.end(); ++it) for (auto it = tokens.begin(); it != tokens.end(); ++it)

View File

@ -215,7 +215,7 @@ void do_pipe(Editor& editor, int count)
editor.buffer().begin_undo_group(); editor.buffer().begin_undo_group();
for (auto& sel : const_cast<const Editor&>(editor).selections()) for (auto& sel : const_cast<const Editor&>(editor).selections())
{ {
String new_content = ShellManager::instance().eval(cmdline, main_context); String new_content = ShellManager::instance().eval(cmdline, main_context, {});
editor.buffer().modify(Modification::make_erase(sel.begin(), sel.end())); editor.buffer().modify(Modification::make_erase(sel.begin(), sel.end()));
editor.buffer().modify(Modification::make_insert(sel.begin(), new_content)); editor.buffer().modify(Modification::make_insert(sel.begin(), new_content));
} }

View File

@ -12,7 +12,8 @@ ShellManager::ShellManager()
{ {
} }
String ShellManager::eval(const String& cmdline, const Context& context) String ShellManager::eval(const String& cmdline, const Context& context,
const EnvVarMap& env_vars)
{ {
int write_pipe[2]; int write_pipe[2];
int read_pipe[2]; int read_pipe[2];
@ -61,11 +62,17 @@ String ShellManager::eval(const String& cmdline, const Context& context)
assert(false); assert(false);
assert(name.length() > 0); assert(name.length() > 0);
auto env_var = m_env_vars.find(name); auto local_var = env_vars.find(name);
if (env_var != m_env_vars.end()) if (local_var != env_vars.end())
setenv(("kak_" + name).c_str(), local_var->second.c_str(), 1);
else
{ {
String value = env_var->second(context); auto env_var = m_env_vars.find(name);
setenv(("kak_" + name).c_str(), value.c_str(), 1); if (env_var != m_env_vars.end())
{
String value = env_var->second(context);
setenv(("kak_" + name).c_str(), value.c_str(), 1);
}
} }
++it; ++it;

View File

@ -11,13 +11,15 @@ namespace Kakoune
class Context; class Context;
typedef std::function<String (const Context&)> EnvVarRetriever; typedef std::function<String (const Context&)> EnvVarRetriever;
typedef std::unordered_map<String, String> EnvVarMap;
class ShellManager : public Singleton<ShellManager> class ShellManager : public Singleton<ShellManager>
{ {
public: public:
ShellManager(); ShellManager();
String eval(const String& cmdline, const Context& context); String eval(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& name, EnvVarRetriever retriever);