From 7861ad9ddbb457f4d043d637bf3f9831ca4d2c1b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 21 Nov 2011 19:30:44 +0000 Subject: [PATCH] Selectors: add split_selection bound to Alt-s --- src/main.cc | 12 ++++++++++++ src/selectors.cc | 36 ++++++++++++++++++++++++++++-------- src/selectors.hh | 3 +++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main.cc b/src/main.cc index 03b9ebd9..9fa9cd69 100644 --- a/src/main.cc +++ b/src/main.cc @@ -527,6 +527,16 @@ void do_select_regex(Window& window, int count) catch (prompt_aborted&) {} } +void do_split_regex(Window& window, int count) +{ + try + { + std::string ex = prompt("split: "); + window.multi_select(std::bind(split_selection, _1, ex)); + } + catch (prompt_aborted&) {} +} + std::unordered_map> keymap = { { 'h', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1))); } }, @@ -602,6 +612,8 @@ std::unordered_map> alt_ke { 'L', [](Window& window, int count) { do { window.select(select_to_eol, true); } while(--count > 0); } }, { 'h', [](Window& window, int count) { do { window.select(select_to_eol_reverse, false); } while(--count > 0); } }, { 'H', [](Window& window, int count) { do { window.select(select_to_eol_reverse, true); } while(--count > 0); } }, + + { 's', do_split_regex }, }; int main(int argc, char* argv[]) diff --git a/src/selectors.cc b/src/selectors.cc index 1c4eb83d..966281c7 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -340,14 +340,14 @@ Selection select_next_match(const BufferIterator& cursor, return Selection(begin, end - 1, std::move(captures)); } +typedef boost::regex_iterator RegexIterator; + SelectionList select_all_matches(const Selection& selection, const std::string& regex) { boost::regex ex(regex); - boost::regex_iterator re_it(selection.begin(), - selection.end(), - ex); - boost::regex_iterator re_end; + RegexIterator re_it(selection.begin(), selection.end(), ex); + RegexIterator re_end; SelectionList result; for (; re_it != re_end; ++re_it) @@ -355,13 +355,33 @@ SelectionList select_all_matches(const Selection& selection, BufferIterator begin = (*re_it)[0].first; BufferIterator end = (*re_it)[0].second; - if (begin == end) - ++end; - Selection::CaptureList captures(re_it->begin(), re_it->end()); - result.push_back(Selection(begin, end-1, std::move(captures))); + + result.push_back(Selection(begin, begin == end ? end : end-1, + std::move(captures))); } return result; } +SelectionList split_selection(const Selection& selection, + const std::string& separator_regex) +{ + boost::regex ex(separator_regex); + RegexIterator re_it(selection.begin(), selection.end(), ex, + boost::regex_constants::match_nosubs); + RegexIterator re_end; + + SelectionList result; + BufferIterator begin = selection.begin(); + for (; re_it != re_end; ++re_it) + { + BufferIterator end = (*re_it)[0].first; + + result.push_back(Selection(begin, (begin == end) ? end : end-1)); + begin = (*re_it)[0].second; + } + result.push_back(Selection(begin, selection.last())); + return result; +} + } diff --git a/src/selectors.hh b/src/selectors.hh index 79a6dc68..a092e869 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -29,6 +29,9 @@ Selection select_next_match(const BufferIterator& cursor, SelectionList select_all_matches(const Selection& selection, const std::string& regex); +SelectionList split_selection(const Selection& selection, + const std::string& separator_regex); + } #endif // selectors_hh_INCLUDED