From 9b5e9ffee27e6a6a05403a2e9f21315818cca2aa Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 7 Feb 2012 14:26:51 +0000 Subject: [PATCH] selectors now take a Selection as parameter instead of a BufferIterator --- src/editor.cc | 5 +-- src/editor.hh | 2 +- src/main.cc | 7 ++-- src/selectors.cc | 100 ++++++++++++++++++++++++++--------------------- src/selectors.hh | 35 +++++++++-------- 5 files changed, 80 insertions(+), 69 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 4f2f7ba9..fe8187d7 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -149,7 +149,6 @@ void Editor::select(const BufferIterator& iterator) { selections().clear(); selections().push_back(Selection(iterator, iterator)); - } void Editor::select(const Selector& selector, bool append) @@ -159,12 +158,12 @@ void Editor::select(const Selector& selector, bool append) if (not append) { for (auto& sel : selections()) - sel = selector(sel.last()); + sel = selector(sel); } else { for (auto& sel : selections()) - sel.merge_with(selector(sel.last())); + sel.merge_with(selector(sel)); } } diff --git a/src/editor.hh b/src/editor.hh index 565ae81f..3ad15b50 100644 --- a/src/editor.hh +++ b/src/editor.hh @@ -20,7 +20,7 @@ class Editor { public: typedef BufferString String; - typedef std::function Selector; + typedef std::function Selector; typedef std::function MultiSelector; Editor(Buffer& buffer); diff --git a/src/main.cc b/src/main.cc index 6c6ef345..a1d51e0c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -900,7 +900,7 @@ void do_split_regex(Editor& editor, int count) void do_join(Editor& editor, int count) { - editor.multi_select(select_whole_lines); + editor.select(select_whole_lines); editor.select(select_to_eol, true); editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*")); editor.replace(" "); @@ -970,8 +970,7 @@ std::unordered_map> keymap { { Key::Modifiers::None, '.' }, do_repeat_insert }, - { { Key::Modifiers::None, '%' }, [](Editor& editor, int count) { editor.select([](const BufferIterator& cursor) - { return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } }, + { { Key::Modifiers::None, '%' }, [](Editor& editor, int count) { editor.clear_selections(); editor.select(select_whole_buffer); } }, { { Key::Modifiers::None, ':' }, [](Editor& editor, int count) { do_command(); } }, { { Key::Modifiers::None, '|' }, do_pipe }, @@ -1016,7 +1015,7 @@ std::unordered_map> keymap { { Key::Modifiers::Alt, 'j' }, do_join }, - { { Key::Modifiers::Alt, 'x' }, [](Editor& editor, int count) { editor.multi_select(select_whole_lines); } }, + { { Key::Modifiers::Alt, 'x' }, [](Editor& editor, int count) { editor.select(select_whole_lines); } }, }; void exec_keys(const KeyList& keys, diff --git a/src/selectors.cc b/src/selectors.cc index 3aaee02a..293d8a53 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -71,9 +71,9 @@ bool skip_while_reverse(BufferIterator& it, T condition) return condition(*it); } -Selection select_to_next_word(const BufferIterator& cursor) +Selection select_to_next_word(const Selection& selection) { - BufferIterator begin = cursor; + BufferIterator begin = selection.last(); if (categorize(*begin) != categorize(*(begin+1))) ++begin; @@ -91,9 +91,9 @@ Selection select_to_next_word(const BufferIterator& cursor) return Selection(begin, with_end ? end : end-1); } -Selection select_to_next_word_end(const BufferIterator& cursor) +Selection select_to_next_word_end(const Selection& selection) { - BufferIterator begin = cursor; + BufferIterator begin = selection.last(); if (categorize(*begin) != categorize(*(begin+1))) ++begin; @@ -110,9 +110,9 @@ Selection select_to_next_word_end(const BufferIterator& cursor) return Selection(begin, with_end ? end : end-1); } -Selection select_to_previous_word(const BufferIterator& cursor) +Selection select_to_previous_word(const Selection& selection) { - BufferIterator begin = cursor; + BufferIterator begin = selection.last(); if (categorize(*begin) != categorize(*(begin-1))) --begin; @@ -130,9 +130,9 @@ Selection select_to_previous_word(const BufferIterator& cursor) return Selection(begin, with_end ? end : end+1); } -Selection select_to_next_WORD(const BufferIterator& cursor) +Selection select_to_next_WORD(const Selection& selection) { - BufferIterator begin = cursor; + BufferIterator begin = selection.last(); if (categorize(*begin) != categorize(*(begin+1))) ++begin; @@ -146,9 +146,9 @@ Selection select_to_next_WORD(const BufferIterator& cursor) return Selection(begin, with_end ? end : end-1); } -Selection select_to_next_WORD_end(const BufferIterator& cursor) +Selection select_to_next_WORD_end(const Selection& selection) { - BufferIterator begin = cursor; + BufferIterator begin = selection.last(); if (categorize(*begin) != categorize(*(begin+1))) ++begin; @@ -163,9 +163,9 @@ Selection select_to_next_WORD_end(const BufferIterator& cursor) return Selection(begin, with_end ? end : end-1); } -Selection select_to_previous_WORD(const BufferIterator& cursor) +Selection select_to_previous_WORD(const Selection& selection) { - BufferIterator begin = cursor; + BufferIterator begin = selection.last(); if (categorize(*begin) != categorize(*(begin-1))) --begin; @@ -178,9 +178,9 @@ Selection select_to_previous_WORD(const BufferIterator& cursor) return Selection(begin, with_end ? end : end+1); } -Selection select_line(const BufferIterator& cursor) +Selection select_line(const Selection& selection) { - BufferIterator first = cursor; + BufferIterator first = selection.last(); if (*first == '\n' and not (first + 1).is_end()) ++first; @@ -193,10 +193,10 @@ Selection select_line(const BufferIterator& cursor) return Selection(first, last); } -Selection select_matching(const BufferIterator& cursor) +Selection select_matching(const Selection& selection) { std::vector matching_pairs = { '(', ')', '{', '}', '[', ']', '<', '>' }; - BufferIterator it = cursor; + BufferIterator it = selection.last(); std::vector::iterator match = matching_pairs.end(); while (not is_eol(*it)) { @@ -206,7 +206,7 @@ Selection select_matching(const BufferIterator& cursor) ++it; } if (match == matching_pairs.end()) - return Selection(cursor, cursor); + return selection; BufferIterator begin = it; @@ -239,15 +239,15 @@ Selection select_matching(const BufferIterator& cursor) --it; } } - return Selection(cursor, cursor); + return selection; } -Selection select_surrounding(const BufferIterator& cursor, +Selection select_surrounding(const Selection& selection, const std::pair& matching, bool inside) { int level = 0; - BufferIterator first = cursor; + BufferIterator first = selection.last(); while (not first.is_begin()) { if (*first == matching.second) @@ -262,7 +262,7 @@ Selection select_surrounding(const BufferIterator& cursor, --first; } if (level != 0 or *first != matching.first) - return Selection(cursor, cursor); + return selection; level = 0; BufferIterator last = first + 1; @@ -280,7 +280,7 @@ Selection select_surrounding(const BufferIterator& cursor, ++last; } if (level != 0 or *last != matching.second) - return Selection(cursor, cursor); + return selection; if (inside) { @@ -291,51 +291,57 @@ Selection select_surrounding(const BufferIterator& cursor, return Selection(first, last); } -Selection select_to(const BufferIterator& cursor, char c, int count, bool inclusive) +Selection select_to(const Selection& selection, + char c, int count, bool inclusive) { - BufferIterator end = cursor; + BufferIterator begin = selection.last(); + BufferIterator end = begin; do { ++end; skip_while(end, [c](char cur) { return not is_eol(cur) and cur != c; }); if (end.is_end() or is_eol(*end)) - return Selection(cursor, cursor); + return selection; } while (--count > 0); - return Selection(cursor, inclusive ? end : end-1); + return Selection(begin, inclusive ? end : end-1); } -Selection select_to_reverse(const BufferIterator& cursor, char c, int count, bool inclusive) +Selection select_to_reverse(const Selection& selection, + char c, int count, bool inclusive) { - BufferIterator end = cursor; + BufferIterator begin = selection.last(); + BufferIterator end = begin; do { --end; skip_while_reverse(end, [c](char cur) { return not is_eol(cur) and cur != c; }); if (end.is_begin() or is_eol(*end)) - return Selection(cursor, cursor); + return selection; } while (--count > 0); - return Selection(cursor, inclusive ? end : end+1); + return Selection(begin, inclusive ? end : end+1); } -Selection select_to_eol(const BufferIterator& cursor) +Selection select_to_eol(const Selection& selection) { - BufferIterator end = cursor + 1; + BufferIterator begin = selection.last(); + BufferIterator end = begin + 1; skip_while(end, [](char cur) { return not is_eol(cur); }); - return Selection(cursor, end-1); + return Selection(begin, end-1); } -Selection select_to_eol_reverse(const BufferIterator& cursor) +Selection select_to_eol_reverse(const Selection& selection) { - BufferIterator end = cursor - 1; + BufferIterator begin = selection.last(); + BufferIterator end = begin - 1; skip_while_reverse(end, [](char cur) { return not is_eol(cur); }); - return Selection(cursor, end.is_begin() ? end : end+1); + return Selection(begin, end.is_begin() ? end : end+1); } -SelectionList select_whole_lines(const Selection& selection) +Selection select_whole_lines(const Selection& selection) { BufferIterator first = selection.first(); BufferIterator last = selection.last(); @@ -349,25 +355,29 @@ SelectionList select_whole_lines(const Selection& selection) skip_while(to_line_end, [](char cur) { return not is_eol(cur); }); - SelectionList result; - result.push_back(Selection(first, last)); - return result; + return Selection(first, last); } -Selection select_next_match(const BufferIterator& cursor, +Selection select_whole_buffer(const Selection& selection) +{ + const Buffer& buffer = selection.first().buffer(); + return Selection(buffer.begin(), buffer.end()-1); +} + +Selection select_next_match(const Selection& selection, const std::string& regex) { boost::regex ex(regex); - BufferIterator begin = cursor; - BufferIterator end = cursor; + BufferIterator begin = selection.last(); + BufferIterator end = begin; Selection::CaptureList captures; try { boost::match_results matches; - if (boost::regex_search(cursor+1, cursor.buffer().end(), matches, + if (boost::regex_search(begin+1, begin.buffer().end(), matches, ex)) { begin = matches[0].first; @@ -375,7 +385,7 @@ Selection select_next_match(const BufferIterator& cursor, std::copy(matches.begin(), matches.end(), std::back_inserter(captures)); } - else if (boost::regex_search(cursor.buffer().begin(), cursor+1, matches, + else if (boost::regex_search(begin.buffer().begin(), begin+1, matches, ex)) { begin = matches[0].first; diff --git a/src/selectors.hh b/src/selectors.hh index 79fb53e9..0faa5b46 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -1,32 +1,35 @@ #ifndef selectors_hh_INCLUDED #define selectors_hh_INCLUDED -#include "window.hh" +#include "selection.hh" namespace Kakoune { -Selection select_to_next_word(const BufferIterator& cursor); -Selection select_to_next_word_end(const BufferIterator& cursor); -Selection select_to_previous_word(const BufferIterator& cursor); -Selection select_to_next_WORD(const BufferIterator& cursor); -Selection select_to_next_WORD_end(const BufferIterator& cursor); -Selection select_to_previous_WORD(const BufferIterator& cursor); -Selection select_line(const BufferIterator& cursor); -Selection select_matching(const BufferIterator& cursor); -Selection select_surrounding(const BufferIterator& cursor, +Selection select_to_next_word(const Selection& selection); +Selection select_to_next_word_end(const Selection& selection); +Selection select_to_previous_word(const Selection& selection); +Selection select_to_next_WORD(const Selection& selection); +Selection select_to_next_WORD_end(const Selection& selection); +Selection select_to_previous_WORD(const Selection& selection); +Selection select_line(const Selection& selection); +Selection select_matching(const Selection& selection); +Selection select_surrounding(const Selection& selection, const std::pair& matching, bool inside); -Selection select_to(const BufferIterator& cursor, char c, int count, bool inclusive); -Selection select_to_reverse(const BufferIterator& cursor, char c, int count, bool inclusive); +Selection select_to(const Selection& selection, + char c, int count, bool inclusive); +Selection select_to_reverse(const Selection& selection, + char c, int count, bool inclusive); -Selection select_to_eol(const BufferIterator& cursor); -Selection select_to_eol_reverse(const BufferIterator& cursor); +Selection select_to_eol(const Selection& selection); +Selection select_to_eol_reverse(const Selection& selection); -SelectionList select_whole_lines(const Selection& selection); +Selection select_whole_lines(const Selection& selection); +Selection select_whole_buffer(const Selection& selection); -Selection select_next_match(const BufferIterator& cursor, +Selection select_next_match(const Selection& selection, const std::string& regex); SelectionList select_all_matches(const Selection& selection,