From 3bfd774f4912c2397734076ec70d298dae1fe51e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 29 May 2012 10:39:03 +0000 Subject: [PATCH] Restore piping support. Add a ShellManager::pipe method, which pipes a string into the command. Not quite satisfied with this interface. --- src/main.cc | 5 +++-- src/shell_manager.cc | 17 ++++++++++++++--- src/shell_manager.hh | 4 ++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main.cc b/src/main.cc index afde43be8..41e4595d0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -215,7 +215,8 @@ void do_pipe(Editor& editor, int count) editor.buffer().begin_undo_group(); for (auto& sel : const_cast(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()); } diff --git a/src/shell_manager.cc b/src/shell_manager.cc index 48688494f..b9f65766f 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -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 data = input.data(); + write(write_pipe[1], data.pointer(), data.size()); close(write_pipe[1]); char buffer[1024]; diff --git a/src/shell_manager.hh b/src/shell_manager.hh index 661e9a4f7..0b22da5c2 100644 --- a/src/shell_manager.hh +++ b/src/shell_manager.hh @@ -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: