Make <a-space> throw on invalid index or last selection

This commit is contained in:
Maxime Coste 2017-03-03 17:05:58 +00:00
parent e3cfde6d07
commit 8a1e5d12ac

View File

@ -1710,13 +1710,16 @@ void remove_selection(Context& context, NormalParams p)
{ {
auto& selections = context.selections(); auto& selections = context.selections();
const int index = p.count ? p.count-1 : selections.main_index(); const int index = p.count ? p.count-1 : selections.main_index();
if (selections.size() > 1 and index < selections.size())
{ 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); selections.remove(index);
size_t main_index = selections.main_index(); size_t main_index = selections.main_index();
if (index < main_index or main_index == selections.size()) if (index < main_index or main_index == selections.size())
selections.set_main_index(main_index - 1); selections.set_main_index(main_index - 1);
}
selections.check_invariant(); selections.check_invariant();
} }