From d3f438810e12925b6fcd4718db0bb989a84348ee Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 19 Jul 2017 17:42:41 +0200 Subject: Fix main selection handling in keep pipe ($) $kak_reg_hash will properly contain the current selection index when executing the shell command, fixing its use. Fixes #1503 --- src/normal.cc | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/normal.cc') diff --git a/src/normal.cc b/src/normal.cc index 6932509c..fd44110f 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1003,15 +1003,27 @@ void keep_pipe(Context& context, NormalParams) const Buffer& buffer = context.buffer(); auto& shell_manager = ShellManager::instance(); Vector keep; - for (auto& sel : context.selections()) + + auto& selections = context.selections(); + const size_t old_main = selections.main_index(); + size_t new_main = -1; + for (int i = 0; i < selections.size(); ++i) { + auto& sel = selections[i]; + selections.set_main_index(i); if (shell_manager.eval(cmdline, context, content(buffer, sel), ShellManager::Flags::None).second == 0) + { keep.push_back(sel); + if (i >= old_main and new_main == (size_t)-1) + new_main = keep.size() - 1; + } } if (keep.empty()) throw runtime_error("no selections remaining"); - context.selections_write_only() = std::move(keep); + if (new_main == -1) + new_main = keep.size() - 1; + context.selections_write_only().set(std::move(keep), new_main); }); } template -- cgit v1.2.3