diff --git a/src/main.cc b/src/main.cc index 609efed0..157b5ead 100644 --- a/src/main.cc +++ b/src/main.cc @@ -512,6 +512,16 @@ void do_paste(Window& window, int count) window.clear_selections(); } +void do_select_regex(Window& window, int count) +{ + try + { + RegexSelector selector(prompt("select: ")); + window.multi_select(selector); + } + catch (prompt_aborted&) {} +} + std::unordered_map> keymap = { { 'h', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1))); } }, @@ -545,6 +555,8 @@ std::unordered_map> keymap { 'p', do_paste }, { 'P', do_paste }, + { 's', do_select_regex }, + { '%', [](Window& window, int count) { window.select([](const BufferIterator& cursor) { return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } }, diff --git a/src/regex_selector.cc b/src/regex_selector.cc index a8e68bf2..821f79f3 100644 --- a/src/regex_selector.cc +++ b/src/regex_selector.cc @@ -9,8 +9,6 @@ RegexSelector::RegexSelector(const std::string& exp) Selection RegexSelector::operator()(const BufferIterator& cursor) const { - BufferIterator line_end = cursor + 1; - try { boost::match_results matches; @@ -28,4 +26,23 @@ Selection RegexSelector::operator()(const BufferIterator& cursor) const return Selection(cursor, cursor); } +SelectionList RegexSelector::operator()(const Selection& selection) const +{ + boost::regex_iterator re_it(selection.begin(), + selection.end(), + m_regex, boost::match_nosubs); + boost::regex_iterator re_end; + + SelectionList result; + for (; re_it != re_end; ++re_it) + { + BufferIterator begin = (*re_it)[0].first; + BufferIterator end = (*re_it)[0].second; + assert(begin != end); + + result.push_back(Selection(begin, end-1)); + } + return result; +} + } diff --git a/src/regex_selector.hh b/src/regex_selector.hh index 7489bb72..bb4151f3 100644 --- a/src/regex_selector.hh +++ b/src/regex_selector.hh @@ -15,6 +15,7 @@ public: RegexSelector(const std::string& exp); Selection operator()(const BufferIterator& cursor) const; + SelectionList operator()(const Selection& selection) const; private: boost::regex m_regex;