diff --git a/src/normal.cc b/src/normal.cc index eec19752..89195b08 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1355,27 +1355,53 @@ void select_whole_buffer(Context& context, NormalParams) void keep_selection(Context& context, NormalParams p) { - keep_selection(context.selections(), p.count ? p.count-1 : context.selections().main_index()); + auto& selections = context.selections(); + const int index = p.count ? p.count-1 : selections.main_index(); + if (index < selections.size()) + selections = SelectionList{ selections.buffer(), std::move(selections[index]) }; + selections.check_invariant(); } void remove_selection(Context& context, NormalParams p) { - remove_selection(context.selections(), p.count ? p.count-1 : context.selections().main_index()); + 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); + } + selections.check_invariant(); } void clear_selections(Context& context, NormalParams) { - clear_selections(context.selections()); + for (auto& sel : context.selections()) + sel.anchor() = sel.cursor(); } void flip_selections(Context& context, NormalParams) { - flip_selections(context.selections()); + for (auto& sel : context.selections()) + { + const ByteCoord tmp = sel.anchor(); + sel.anchor() = sel.cursor(); + sel.cursor() = tmp; + } + context.selections().check_invariant(); } void ensure_forward(Context& context, NormalParams) { - ensure_forward(context.selections()); + for (auto& sel : context.selections()) + { + const ByteCoord min = sel.min(), max = sel.max(); + sel.anchor() = min; + sel.cursor() = max; + } + context.selections().check_invariant(); } static NormalCmdDesc cmds[] = diff --git a/src/selectors.hh b/src/selectors.hh index e03c479c..e3270716 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -18,53 +18,6 @@ inline Selection keep_direction(Selection res, const Selection& ref) return res; } -inline void clear_selections(SelectionList& selections) -{ - for (auto& sel : selections) - sel.anchor() = sel.cursor(); -} - -inline void flip_selections(SelectionList& selections) -{ - for (auto& sel : selections) - { - const ByteCoord tmp = sel.anchor(); - sel.anchor() = sel.cursor(); - sel.cursor() = tmp; - } - selections.check_invariant(); -} - -inline void ensure_forward(SelectionList& selections) -{ - for (auto& sel : selections) - { - const ByteCoord min = sel.min(), max = sel.max(); - sel.anchor() = min; - sel.cursor() = max; - } - selections.check_invariant(); -} - -inline void keep_selection(SelectionList& selections, int index) -{ - if (index < selections.size()) - selections = SelectionList{ selections.buffer(), std::move(selections[index]) }; - selections.check_invariant(); -} - -inline void remove_selection(SelectionList& selections, int 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); - } - selections.check_invariant(); -} - using Utf8Iterator = utf8::iterator; inline Selection utf8_range(const Utf8Iterator& first, const Utf8Iterator& last)