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();
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_insert(sel.begin(), new_content));
}
@ -466,7 +467,7 @@ int main(int argc, char* argv[])
{
command_manager.execute("runtime kakrc", main_context);
}
catch (Kakoune::runtime_error& error)
catch (Kakoune::runtime_error& error)
{
NCurses::print_status(error.description());
}

View File

@ -6,26 +6,37 @@
namespace Kakoune
{
String env_var_regex(R"(\$\{kak_([a-z0-9_]+)[^}]*\}|\$kak_([a-z0-9_]+))");
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,
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 read_pipe[2];
pipe(write_pipe);
pipe(read_pipe);
::pipe(write_pipe);
::pipe(read_pipe);
String output;
if (pid_t pid = fork())
{
close(write_pipe[0]);
close(read_pipe[1]);
memoryview<char> data = input.data();
write(write_pipe[1], data.pointer(), data.size());
close(write_pipe[1]);
char buffer[1024];

View File

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