From 8a1e5d12ac16d083f9f3997735bac8d493e1a77b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 3 Mar 2017 17:05:58 +0000 Subject: [PATCH] Make throw on invalid index or last selection --- src/normal.cc | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index 9eabc20c..ae59d9fc 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1710,13 +1710,16 @@ void remove_selection(Context& context, NormalParams p) { auto& selections = context.selections(); const int index = p.count ? p.count-1 : selections.main_index(); - if (selections.size() > 1 and index < selections.size()) - { - selections.remove(index); - size_t main_index = selections.main_index(); - if (index < main_index or main_index == selections.size()) - selections.set_main_index(main_index - 1); - } + + if (index >= selections.size()) + throw runtime_error{format("There is not {}th selection", index)}; + if (selections.size() == 1) + throw runtime_error{"Cannot remove the last selection"}; + + selections.remove(index); + size_t main_index = selections.main_index(); + if (index < main_index or main_index == selections.size()) + selections.set_main_index(main_index - 1); selections.check_invariant(); }