summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2011-11-21 19:30:44 +0000
committerMaxime Coste <frrrwww@gmail.com>2011-11-21 19:30:44 +0000
commit7861ad9ddbb457f4d043d637bf3f9831ca4d2c1b (patch)
treef910bdb04839e9b890b9ea3b45d007932452ab59 /src
parentb67c36358d067bf5621ab07adb0890508b836d84 (diff)
Selectors: add split_selection bound to Alt-s
Diffstat (limited to 'src')
-rw-r--r--src/main.cc12
-rw-r--r--src/selectors.cc36
-rw-r--r--src/selectors.hh3
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<char, std::function<void (Window& window, int count)>> keymap =
{
{ 'h', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1))); } },
@@ -602,6 +612,8 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> 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<BufferIterator> RegexIterator;
+
SelectionList select_all_matches(const Selection& selection,
const std::string& regex)
{
boost::regex ex(regex);
- boost::regex_iterator<BufferIterator> re_it(selection.begin(),
- selection.end(),
- ex);
- boost::regex_iterator<BufferIterator> re_end;
+ RegexIterator re_it(selection.begin(), selection.end(), ex);
+ RegexIterator re_end;
SelectionList result;
for (; re_it != re_end; ++re_it)
@@ -355,12 +355,32 @@ 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