Restore piping support.

Add a ShellManager::pipe method, which pipes a string into the
command. Not quite satisfied with this interface.
This commit is contained in:
Maxime Coste 2012-05-29 10:39:03 +00:00
parent 84c1cad3d5
commit 3bfd774f49
3 changed files with 21 additions and 5 deletions

View File

@ -215,7 +215,8 @@ 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().pipe(String(sel.begin(), sel.end()),
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

@ -6,26 +6,37 @@
namespace Kakoune namespace Kakoune
{ {
String env_var_regex(R"(\$\{kak_([a-z0-9_]+)[^}]*\}|\$kak_([a-z0-9_]+))");
ShellManager::ShellManager() ShellManager::ShellManager()
: m_regex(LR"(\$\{kak_([a-z0-9_]+)[^}]*\}|\$kak_([a-z0-9_]+))") : m_regex(env_var_regex.begin(), env_var_regex.end())
{ {
} }
String ShellManager::eval(const String& cmdline, const Context& context, String ShellManager::eval(const String& cmdline, const Context& context,
const EnvVarMap& env_vars) const EnvVarMap& env_vars)
{
return pipe("", cmdline, context, env_vars);
}
String ShellManager::pipe(const String& input,
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];
pipe(write_pipe); ::pipe(write_pipe);
pipe(read_pipe); ::pipe(read_pipe);
String output; String output;
if (pid_t pid = fork()) if (pid_t pid = fork())
{ {
close(write_pipe[0]); close(write_pipe[0]);
close(read_pipe[1]); close(read_pipe[1]);
memoryview<char> data = input.data();
write(write_pipe[1], data.pointer(), data.size());
close(write_pipe[1]); close(write_pipe[1]);
char buffer[1024]; char buffer[1024];

View File

@ -21,6 +21,10 @@ public:
String eval(const String& cmdline, const Context& context, String eval(const String& cmdline, const Context& context,
const EnvVarMap& env_vars); const EnvVarMap& env_vars);
String pipe(const String& input,
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);
private: private: