From b67c36358d067bf5621ab07adb0890508b836d84 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 21 Nov 2011 19:08:51 +0000 Subject: [PATCH] Extract RegexSelector methods as plain selectors --- src/main.cc | 9 +++--- src/regex_selector.cc | 71 ------------------------------------------- src/regex_selector.hh | 26 ---------------- src/selectors.cc | 67 ++++++++++++++++++++++++++++++++++++++++ src/selectors.hh | 6 ++++ 5 files changed, 77 insertions(+), 102 deletions(-) delete mode 100644 src/regex_selector.cc delete mode 100644 src/regex_selector.hh diff --git a/src/main.cc b/src/main.cc index e6234f94..03b9ebd9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,7 +1,6 @@ #include "window.hh" #include "buffer.hh" #include "file.hh" -#include "regex_selector.hh" #include "command_manager.hh" #include "buffer_manager.hh" #include "register_manager.hh" @@ -474,7 +473,7 @@ void do_search(Window& window) else RegisterManager::instance()['/'] = ex; - window.select(RegexSelector(ex)); + window.select(std::bind(select_next_match, _1, ex)); } catch (prompt_aborted&) {} } @@ -483,7 +482,7 @@ void do_search_next(Window& window) { std::string& ex = RegisterManager::instance()['/']; if (not ex.empty()) - window.select(RegexSelector(ex)); + window.select(std::bind(select_next_match, _1, ex)); else print_status("no search pattern"); } @@ -522,8 +521,8 @@ void do_select_regex(Window& window, int count) { try { - RegexSelector selector(prompt("select: ")); - window.multi_select(selector); + std::string ex = prompt("select: "); + window.multi_select(std::bind(select_all_matches, _1, ex)); } catch (prompt_aborted&) {} } diff --git a/src/regex_selector.cc b/src/regex_selector.cc deleted file mode 100644 index 0bfce956..00000000 --- a/src/regex_selector.cc +++ /dev/null @@ -1,71 +0,0 @@ -#include "regex_selector.hh" -#include "exception.hh" - -namespace Kakoune -{ - -RegexSelector::RegexSelector(const std::string& exp) - : m_regex(exp) {} - -Selection RegexSelector::operator()(const BufferIterator& cursor) const -{ - BufferIterator begin = cursor; - BufferIterator end = cursor; - Selection::CaptureList captures; - - try - { - boost::match_results matches; - - if (boost::regex_search(cursor, cursor.buffer().end(), matches, - m_regex)) - { - begin = matches[0].first; - end = matches[0].second; - std::copy(matches.begin(), matches.end(), - std::back_inserter(captures)); - } - else if (boost::regex_search(cursor.buffer().begin(), cursor, matches, - m_regex)) - { - begin = matches[0].first; - end = matches[0].second; - std::copy(matches.begin(), matches.end(), - std::back_inserter(captures)); - } - } - catch (boost::regex_error& err) - { - throw runtime_error("regex error"); - } - - - if (begin == end) - ++end; - - return Selection(begin, end - 1, std::move(captures)); -} - -SelectionList RegexSelector::operator()(const Selection& selection) const -{ - boost::regex_iterator re_it(selection.begin(), - selection.end(), - m_regex); - 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; - - if (begin == end) - ++end; - - Selection::CaptureList captures(re_it->begin(), re_it->end()); - result.push_back(Selection(begin, end-1, std::move(captures))); - } - return result; -} - -} diff --git a/src/regex_selector.hh b/src/regex_selector.hh deleted file mode 100644 index bb4151f3..00000000 --- a/src/regex_selector.hh +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef regex_selector_hh_INCLUDED -#define regex_selector_hh_INCLUDED - -#include "buffer.hh" -#include "window.hh" - -#include - -namespace Kakoune -{ - -class RegexSelector -{ -public: - RegexSelector(const std::string& exp); - - Selection operator()(const BufferIterator& cursor) const; - SelectionList operator()(const Selection& selection) const; - -private: - boost::regex m_regex; -}; - -} - -#endif // regex_selector_hh_INCLUDED diff --git a/src/selectors.cc b/src/selectors.cc index 6a0ee218..1c4eb83d 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -1,5 +1,6 @@ #include "selectors.hh" +#include #include namespace Kakoune @@ -297,4 +298,70 @@ SelectionList select_whole_lines(const Selection& selection) return result; } +Selection select_next_match(const BufferIterator& cursor, + const std::string& regex) +{ + boost::regex ex(regex); + + BufferIterator begin = cursor; + BufferIterator end = cursor; + Selection::CaptureList captures; + + try + { + boost::match_results matches; + + if (boost::regex_search(cursor, cursor.buffer().end(), matches, + ex)) + { + begin = matches[0].first; + end = matches[0].second; + std::copy(matches.begin(), matches.end(), + std::back_inserter(captures)); + } + else if (boost::regex_search(cursor.buffer().begin(), cursor, matches, + ex)) + { + begin = matches[0].first; + end = matches[0].second; + std::copy(matches.begin(), matches.end(), + std::back_inserter(captures)); + } + } + catch (boost::regex_error& err) + { + throw runtime_error("regex error"); + } + + + if (begin == end) + ++end; + + return Selection(begin, end - 1, std::move(captures)); +} + +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; + + SelectionList result; + for (; re_it != re_end; ++re_it) + { + 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))); + } + return result; +} + } diff --git a/src/selectors.hh b/src/selectors.hh index 16ff095b..79a6dc68 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -23,6 +23,12 @@ Selection select_to_eol_reverse(const BufferIterator& cursor); SelectionList select_whole_lines(const Selection& selection); +Selection select_next_match(const BufferIterator& cursor, + const std::string& regex); + +SelectionList select_all_matches(const Selection& selection, + const std::string& regex); + } #endif // selectors_hh_INCLUDED