From f53ba0baede2aade92b4267f416bf1a4965064f6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 13 Dec 2013 09:11:48 +0000 Subject: [PATCH] MultiSelectors takes a reference to the current selection and mutate it --- src/editor.cc | 2 +- src/editor.hh | 2 +- src/normal.cc | 29 +++++++++++++++-------------- src/selectors.cc | 14 ++++++++------ src/selectors.hh | 9 ++++----- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 2e39bacf..f4153122 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -282,7 +282,7 @@ struct nothing_selected : public runtime_error void Editor::multi_select(const MultiSelector& selector) { - m_selections = selector(*m_buffer, std::move(m_selections)); + selector(*m_buffer, m_selections); check_invariant(); } diff --git a/src/editor.hh b/src/editor.hh index be832da3..8e04b153 100644 --- a/src/editor.hh +++ b/src/editor.hh @@ -40,7 +40,7 @@ class Editor : public SafeCountable { public: typedef std::function Selector; - typedef std::function MultiSelector; + typedef std::function MultiSelector; Editor(Buffer& buffer); virtual ~Editor() {} diff --git a/src/normal.cc b/src/normal.cc index 08d4d990..14ef013d 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -497,12 +497,15 @@ void split_regex(Context& context, int) void split_lines(Context& context, int) { context.editor().multi_select([](const Buffer& buffer, - SelectionList selections) { + SelectionList& selections) { SelectionList res; for (auto& sel : selections) { if (sel.first().line == sel.last().line) - return SelectionList{ sel }; + { + res.push_back(std::move(sel)); + continue; + } auto min = sel.min(); auto max = sel.max(); res.push_back({min, {min.line, buffer[min.line].length()-1}}); @@ -510,7 +513,7 @@ void split_lines(Context& context, int) res.push_back({line, {line, buffer[line].length()-1}}); res.push_back({max.line, max}); } - return res; + selections = std::move(res); }); } @@ -519,17 +522,15 @@ void join_select_spaces(Context& context, int) Editor& editor = context.editor(); editor.select(select_whole_lines); editor.select(select_to_eol, SelectMode::Extend); - editor.multi_select([](const Buffer& buffer, SelectionList sel) + editor.multi_select([](const Buffer& buffer, SelectionList& sel) { - SelectionList res = select_all_matches(buffer, std::move(sel), - Regex{"(\n\\h*)+"}); + select_all_matches(buffer, sel, Regex{"(\n\\h*)+"}); // remove last end of line if selected - kak_assert(std::is_sorted(res.begin(), res.end(), + kak_assert(std::is_sorted(sel.begin(), sel.end(), [](const Selection& lhs, const Selection& rhs) { return lhs.min() < rhs.min(); })); - if (not res.empty() and res.back().max() == buffer.back_coord()) - res.pop_back(); - return res; + if (not sel.empty() and sel.back().max() == buffer.back_coord()) + sel.pop_back(); }); editor.insert(" ", InsertMode::Replace); } @@ -573,7 +574,7 @@ void indent(Context& context, int) Editor& editor = context.editor(); DynamicSelectionList sels{editor.buffer(), editor.selections()}; auto restore_sels = on_scope_end([&]{ editor.select((SelectionList)std::move(sels)); }); - editor.multi_select([&indent](const Buffer& buf, SelectionList selections) { + editor.multi_select([&indent](const Buffer& buf, SelectionList& selections) { SelectionList res; for (auto& sel : selections) { @@ -583,7 +584,7 @@ void indent(Context& context, int) res.emplace_back(line, line); } } - return res; + selections = std::move(res); }); editor.insert(indent, InsertMode::Insert); } @@ -600,7 +601,7 @@ void deindent(Context& context, int) DynamicSelectionList sels{editor.buffer(), editor.selections()}; auto restore_sels = on_scope_end([&]{ editor.select((SelectionList)std::move(sels)); }); - editor.multi_select([indent_width,tabstop](const Buffer& buf, SelectionList selections) { + editor.multi_select([indent_width,tabstop](const Buffer& buf, SelectionList& selections) { SelectionList res; for (auto& sel : selections) { @@ -629,7 +630,7 @@ void deindent(Context& context, int) } } } - return res; + selections = std::move(res); }); editor.erase(); } diff --git a/src/selectors.cc b/src/selectors.cc index 2e71b014..31a2da6a 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -597,8 +597,8 @@ Selection select_whole_buffer(const Buffer& buffer, const Selection&) return Selection({0,0}, buffer.back_coord()); } -SelectionList select_all_matches(const Buffer& buffer, SelectionList selections, - const Regex& regex) +void select_all_matches(const Buffer& buffer, SelectionList& selections, + const Regex& regex) { SelectionList result; for (auto& sel : selections) @@ -624,11 +624,13 @@ SelectionList select_all_matches(const Buffer& buffer, SelectionList selections, std::move(captures)); } } - return result; + if (result.empty()) + throw runtime_error("nothing selected"); + selections = std::move(result); } -SelectionList split_selection(const Buffer& buffer, SelectionList selections, - const Regex& regex) +void split_selection(const Buffer& buffer, SelectionList& selections, + const Regex& regex) { SelectionList result; for (auto& sel : selections) @@ -648,7 +650,7 @@ SelectionList split_selection(const Buffer& buffer, SelectionList selections, } result.emplace_back(begin.coord(), sel.max()); } - return result; + selections = std::move(result); } } diff --git a/src/selectors.hh b/src/selectors.hh index 684562c3..54008cc7 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -88,7 +88,7 @@ bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, } template -SelectionList select_next_match(const Buffer& buffer, SelectionList selections, +void select_next_match(const Buffer& buffer, SelectionList& selections, const Regex& regex) { auto& sel = selections.main(); @@ -115,7 +115,7 @@ SelectionList select_next_match(const Buffer& buffer, SelectionList selections, Selection res{begin.coord(), end.coord(), std::move(captures)}; if (mode == SelectMode::Replace) - return SelectionList{ std::move(res) }; + selections = SelectionList{ std::move(res) }; else if (mode == SelectMode::ReplaceMain) sel = std::move(res); else if (mode == SelectMode::Append) @@ -124,13 +124,12 @@ SelectionList select_next_match(const Buffer& buffer, SelectionList selections, selections.set_main_index(selections.size() - 1); } selections.sort_and_merge_overlapping(); - return selections; } -SelectionList select_all_matches(const Buffer& buffer, SelectionList selection, +void select_all_matches(const Buffer& buffer, SelectionList& selection, const Regex& regex); -SelectionList split_selection(const Buffer& buffer, SelectionList selection, +void split_selection(const Buffer& buffer, SelectionList& selection, const Regex& separator_regex); using CodepointPair = std::pair;