RegexSelector: support multi selections, bound to s key

This commit is contained in:
Maxime Coste 2011-11-16 14:06:01 +00:00
parent 0203b904e1
commit b1e815a66c
3 changed files with 32 additions and 2 deletions

View File

@ -512,6 +512,16 @@ void do_paste(Window& window, int count)
window.clear_selections(); 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<char, std::function<void (Window& window, int count)>> keymap = 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))); } }, { 'h', [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1))); } },
@ -545,6 +555,8 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> keymap
{ 'p', do_paste<true> }, { 'p', do_paste<true> },
{ 'P', do_paste<false> }, { 'P', do_paste<false> },
{ 's', do_select_regex },
{ '%', [](Window& window, int count) { window.select([](const BufferIterator& cursor) { '%', [](Window& window, int count) { window.select([](const BufferIterator& cursor)
{ return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } }, { return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } },

View File

@ -9,8 +9,6 @@ RegexSelector::RegexSelector(const std::string& exp)
Selection RegexSelector::operator()(const BufferIterator& cursor) const Selection RegexSelector::operator()(const BufferIterator& cursor) const
{ {
BufferIterator line_end = cursor + 1;
try try
{ {
boost::match_results<BufferIterator> matches; boost::match_results<BufferIterator> matches;
@ -28,4 +26,23 @@ Selection RegexSelector::operator()(const BufferIterator& cursor) const
return Selection(cursor, cursor); return Selection(cursor, cursor);
} }
SelectionList RegexSelector::operator()(const Selection& selection) const
{
boost::regex_iterator<BufferIterator> re_it(selection.begin(),
selection.end(),
m_regex, boost::match_nosubs);
boost::regex_iterator<BufferIterator> 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;
}
} }

View File

@ -15,6 +15,7 @@ public:
RegexSelector(const std::string& exp); RegexSelector(const std::string& exp);
Selection operator()(const BufferIterator& cursor) const; Selection operator()(const BufferIterator& cursor) const;
SelectionList operator()(const Selection& selection) const;
private: private:
boost::regex m_regex; boost::regex m_regex;