Selectors: add split_selection bound to Alt-s

This commit is contained in:
Maxime Coste 2011-11-21 19:30:44 +00:00
parent b67c36358d
commit 7861ad9ddb
3 changed files with 43 additions and 8 deletions

View File

@ -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[])

View File

@ -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,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;
}
}

View File

@ -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