selectors now take a Selection as parameter instead of a BufferIterator

This commit is contained in:
Maxime Coste 2012-02-07 14:26:51 +00:00
parent ffaade96ba
commit 9b5e9ffee2
5 changed files with 80 additions and 69 deletions

View File

@ -149,7 +149,6 @@ void Editor::select(const BufferIterator& iterator)
{ {
selections().clear(); selections().clear();
selections().push_back(Selection(iterator, iterator)); selections().push_back(Selection(iterator, iterator));
} }
void Editor::select(const Selector& selector, bool append) void Editor::select(const Selector& selector, bool append)
@ -159,12 +158,12 @@ void Editor::select(const Selector& selector, bool append)
if (not append) if (not append)
{ {
for (auto& sel : selections()) for (auto& sel : selections())
sel = selector(sel.last()); sel = selector(sel);
} }
else else
{ {
for (auto& sel : selections()) for (auto& sel : selections())
sel.merge_with(selector(sel.last())); sel.merge_with(selector(sel));
} }
} }

View File

@ -20,7 +20,7 @@ class Editor
{ {
public: public:
typedef BufferString String; typedef BufferString String;
typedef std::function<Selection (const BufferIterator&)> Selector; typedef std::function<Selection (const Selection&)> Selector;
typedef std::function<SelectionList (const Selection&)> MultiSelector; typedef std::function<SelectionList (const Selection&)> MultiSelector;
Editor(Buffer& buffer); Editor(Buffer& buffer);

View File

@ -900,7 +900,7 @@ void do_split_regex(Editor& editor, int count)
void do_join(Editor& editor, int count) void do_join(Editor& editor, int count)
{ {
editor.multi_select(select_whole_lines); editor.select(select_whole_lines);
editor.select(select_to_eol, true); editor.select(select_to_eol, true);
editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*")); editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*"));
editor.replace(" "); editor.replace(" ");
@ -970,8 +970,7 @@ std::unordered_map<Key, std::function<void (Editor& editor, int count)>> keymap
{ { Key::Modifiers::None, '.' }, do_repeat_insert }, { { Key::Modifiers::None, '.' }, do_repeat_insert },
{ { Key::Modifiers::None, '%' }, [](Editor& editor, int count) { editor.select([](const BufferIterator& cursor) { { Key::Modifiers::None, '%' }, [](Editor& editor, int count) { editor.clear_selections(); editor.select(select_whole_buffer); } },
{ return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } },
{ { Key::Modifiers::None, ':' }, [](Editor& editor, int count) { do_command(); } }, { { Key::Modifiers::None, ':' }, [](Editor& editor, int count) { do_command(); } },
{ { Key::Modifiers::None, '|' }, do_pipe }, { { Key::Modifiers::None, '|' }, do_pipe },
@ -1016,7 +1015,7 @@ std::unordered_map<Key, std::function<void (Editor& editor, int count)>> keymap
{ { Key::Modifiers::Alt, 'j' }, do_join }, { { Key::Modifiers::Alt, 'j' }, do_join },
{ { Key::Modifiers::Alt, 'x' }, [](Editor& editor, int count) { editor.multi_select(select_whole_lines); } }, { { Key::Modifiers::Alt, 'x' }, [](Editor& editor, int count) { editor.select(select_whole_lines); } },
}; };
void exec_keys(const KeyList& keys, void exec_keys(const KeyList& keys,

View File

@ -71,9 +71,9 @@ bool skip_while_reverse(BufferIterator& it, T condition)
return condition(*it); return condition(*it);
} }
Selection select_to_next_word(const BufferIterator& cursor) Selection select_to_next_word(const Selection& selection)
{ {
BufferIterator begin = cursor; BufferIterator begin = selection.last();
if (categorize(*begin) != categorize(*(begin+1))) if (categorize(*begin) != categorize(*(begin+1)))
++begin; ++begin;
@ -91,9 +91,9 @@ Selection select_to_next_word(const BufferIterator& cursor)
return Selection(begin, with_end ? end : end-1); return Selection(begin, with_end ? end : end-1);
} }
Selection select_to_next_word_end(const BufferIterator& cursor) Selection select_to_next_word_end(const Selection& selection)
{ {
BufferIterator begin = cursor; BufferIterator begin = selection.last();
if (categorize(*begin) != categorize(*(begin+1))) if (categorize(*begin) != categorize(*(begin+1)))
++begin; ++begin;
@ -110,9 +110,9 @@ Selection select_to_next_word_end(const BufferIterator& cursor)
return Selection(begin, with_end ? end : end-1); return Selection(begin, with_end ? end : end-1);
} }
Selection select_to_previous_word(const BufferIterator& cursor) Selection select_to_previous_word(const Selection& selection)
{ {
BufferIterator begin = cursor; BufferIterator begin = selection.last();
if (categorize(*begin) != categorize(*(begin-1))) if (categorize(*begin) != categorize(*(begin-1)))
--begin; --begin;
@ -130,9 +130,9 @@ Selection select_to_previous_word(const BufferIterator& cursor)
return Selection(begin, with_end ? end : end+1); return Selection(begin, with_end ? end : end+1);
} }
Selection select_to_next_WORD(const BufferIterator& cursor) Selection select_to_next_WORD(const Selection& selection)
{ {
BufferIterator begin = cursor; BufferIterator begin = selection.last();
if (categorize<false>(*begin) != categorize<false>(*(begin+1))) if (categorize<false>(*begin) != categorize<false>(*(begin+1)))
++begin; ++begin;
@ -146,9 +146,9 @@ Selection select_to_next_WORD(const BufferIterator& cursor)
return Selection(begin, with_end ? end : end-1); return Selection(begin, with_end ? end : end-1);
} }
Selection select_to_next_WORD_end(const BufferIterator& cursor) Selection select_to_next_WORD_end(const Selection& selection)
{ {
BufferIterator begin = cursor; BufferIterator begin = selection.last();
if (categorize<false>(*begin) != categorize<false>(*(begin+1))) if (categorize<false>(*begin) != categorize<false>(*(begin+1)))
++begin; ++begin;
@ -163,9 +163,9 @@ Selection select_to_next_WORD_end(const BufferIterator& cursor)
return Selection(begin, with_end ? end : end-1); return Selection(begin, with_end ? end : end-1);
} }
Selection select_to_previous_WORD(const BufferIterator& cursor) Selection select_to_previous_WORD(const Selection& selection)
{ {
BufferIterator begin = cursor; BufferIterator begin = selection.last();
if (categorize<false>(*begin) != categorize<false>(*(begin-1))) if (categorize<false>(*begin) != categorize<false>(*(begin-1)))
--begin; --begin;
@ -178,9 +178,9 @@ Selection select_to_previous_WORD(const BufferIterator& cursor)
return Selection(begin, with_end ? end : end+1); return Selection(begin, with_end ? end : end+1);
} }
Selection select_line(const BufferIterator& cursor) Selection select_line(const Selection& selection)
{ {
BufferIterator first = cursor; BufferIterator first = selection.last();
if (*first == '\n' and not (first + 1).is_end()) if (*first == '\n' and not (first + 1).is_end())
++first; ++first;
@ -193,10 +193,10 @@ Selection select_line(const BufferIterator& cursor)
return Selection(first, last); return Selection(first, last);
} }
Selection select_matching(const BufferIterator& cursor) Selection select_matching(const Selection& selection)
{ {
std::vector<char> matching_pairs = { '(', ')', '{', '}', '[', ']', '<', '>' }; std::vector<char> matching_pairs = { '(', ')', '{', '}', '[', ']', '<', '>' };
BufferIterator it = cursor; BufferIterator it = selection.last();
std::vector<char>::iterator match = matching_pairs.end(); std::vector<char>::iterator match = matching_pairs.end();
while (not is_eol(*it)) while (not is_eol(*it))
{ {
@ -206,7 +206,7 @@ Selection select_matching(const BufferIterator& cursor)
++it; ++it;
} }
if (match == matching_pairs.end()) if (match == matching_pairs.end())
return Selection(cursor, cursor); return selection;
BufferIterator begin = it; BufferIterator begin = it;
@ -239,15 +239,15 @@ Selection select_matching(const BufferIterator& cursor)
--it; --it;
} }
} }
return Selection(cursor, cursor); return selection;
} }
Selection select_surrounding(const BufferIterator& cursor, Selection select_surrounding(const Selection& selection,
const std::pair<char, char>& matching, const std::pair<char, char>& matching,
bool inside) bool inside)
{ {
int level = 0; int level = 0;
BufferIterator first = cursor; BufferIterator first = selection.last();
while (not first.is_begin()) while (not first.is_begin())
{ {
if (*first == matching.second) if (*first == matching.second)
@ -262,7 +262,7 @@ Selection select_surrounding(const BufferIterator& cursor,
--first; --first;
} }
if (level != 0 or *first != matching.first) if (level != 0 or *first != matching.first)
return Selection(cursor, cursor); return selection;
level = 0; level = 0;
BufferIterator last = first + 1; BufferIterator last = first + 1;
@ -280,7 +280,7 @@ Selection select_surrounding(const BufferIterator& cursor,
++last; ++last;
} }
if (level != 0 or *last != matching.second) if (level != 0 or *last != matching.second)
return Selection(cursor, cursor); return selection;
if (inside) if (inside)
{ {
@ -291,51 +291,57 @@ Selection select_surrounding(const BufferIterator& cursor,
return Selection(first, last); return Selection(first, last);
} }
Selection select_to(const BufferIterator& cursor, char c, int count, bool inclusive) Selection select_to(const Selection& selection,
char c, int count, bool inclusive)
{ {
BufferIterator end = cursor; BufferIterator begin = selection.last();
BufferIterator end = begin;
do do
{ {
++end; ++end;
skip_while(end, [c](char cur) { return not is_eol(cur) and cur != c; }); skip_while(end, [c](char cur) { return not is_eol(cur) and cur != c; });
if (end.is_end() or is_eol(*end)) if (end.is_end() or is_eol(*end))
return Selection(cursor, cursor); return selection;
} }
while (--count > 0); while (--count > 0);
return Selection(cursor, inclusive ? end : end-1); return Selection(begin, inclusive ? end : end-1);
} }
Selection select_to_reverse(const BufferIterator& cursor, char c, int count, bool inclusive) Selection select_to_reverse(const Selection& selection,
char c, int count, bool inclusive)
{ {
BufferIterator end = cursor; BufferIterator begin = selection.last();
BufferIterator end = begin;
do do
{ {
--end; --end;
skip_while_reverse(end, [c](char cur) { return not is_eol(cur) and cur != c; }); skip_while_reverse(end, [c](char cur) { return not is_eol(cur) and cur != c; });
if (end.is_begin() or is_eol(*end)) if (end.is_begin() or is_eol(*end))
return Selection(cursor, cursor); return selection;
} }
while (--count > 0); while (--count > 0);
return Selection(cursor, inclusive ? end : end+1); return Selection(begin, inclusive ? end : end+1);
} }
Selection select_to_eol(const BufferIterator& cursor) Selection select_to_eol(const Selection& selection)
{ {
BufferIterator end = cursor + 1; BufferIterator begin = selection.last();
BufferIterator end = begin + 1;
skip_while(end, [](char cur) { return not is_eol(cur); }); skip_while(end, [](char cur) { return not is_eol(cur); });
return Selection(cursor, end-1); return Selection(begin, end-1);
} }
Selection select_to_eol_reverse(const BufferIterator& cursor) Selection select_to_eol_reverse(const Selection& selection)
{ {
BufferIterator end = cursor - 1; BufferIterator begin = selection.last();
BufferIterator end = begin - 1;
skip_while_reverse(end, [](char cur) { return not is_eol(cur); }); skip_while_reverse(end, [](char cur) { return not is_eol(cur); });
return Selection(cursor, end.is_begin() ? end : end+1); return Selection(begin, end.is_begin() ? end : end+1);
} }
SelectionList select_whole_lines(const Selection& selection) Selection select_whole_lines(const Selection& selection)
{ {
BufferIterator first = selection.first(); BufferIterator first = selection.first();
BufferIterator last = selection.last(); BufferIterator last = selection.last();
@ -349,25 +355,29 @@ SelectionList select_whole_lines(const Selection& selection)
skip_while(to_line_end, [](char cur) { return not is_eol(cur); }); skip_while(to_line_end, [](char cur) { return not is_eol(cur); });
SelectionList result; return Selection(first, last);
result.push_back(Selection(first, last));
return result;
} }
Selection select_next_match(const BufferIterator& cursor, Selection select_whole_buffer(const Selection& selection)
{
const Buffer& buffer = selection.first().buffer();
return Selection(buffer.begin(), buffer.end()-1);
}
Selection select_next_match(const Selection& selection,
const std::string& regex) const std::string& regex)
{ {
boost::regex ex(regex); boost::regex ex(regex);
BufferIterator begin = cursor; BufferIterator begin = selection.last();
BufferIterator end = cursor; BufferIterator end = begin;
Selection::CaptureList captures; Selection::CaptureList captures;
try try
{ {
boost::match_results<BufferIterator> matches; boost::match_results<BufferIterator> matches;
if (boost::regex_search(cursor+1, cursor.buffer().end(), matches, if (boost::regex_search(begin+1, begin.buffer().end(), matches,
ex)) ex))
{ {
begin = matches[0].first; begin = matches[0].first;
@ -375,7 +385,7 @@ Selection select_next_match(const BufferIterator& cursor,
std::copy(matches.begin(), matches.end(), std::copy(matches.begin(), matches.end(),
std::back_inserter(captures)); std::back_inserter(captures));
} }
else if (boost::regex_search(cursor.buffer().begin(), cursor+1, matches, else if (boost::regex_search(begin.buffer().begin(), begin+1, matches,
ex)) ex))
{ {
begin = matches[0].first; begin = matches[0].first;

View File

@ -1,32 +1,35 @@
#ifndef selectors_hh_INCLUDED #ifndef selectors_hh_INCLUDED
#define selectors_hh_INCLUDED #define selectors_hh_INCLUDED
#include "window.hh" #include "selection.hh"
namespace Kakoune namespace Kakoune
{ {
Selection select_to_next_word(const BufferIterator& cursor); Selection select_to_next_word(const Selection& selection);
Selection select_to_next_word_end(const BufferIterator& cursor); Selection select_to_next_word_end(const Selection& selection);
Selection select_to_previous_word(const BufferIterator& cursor); Selection select_to_previous_word(const Selection& selection);
Selection select_to_next_WORD(const BufferIterator& cursor); Selection select_to_next_WORD(const Selection& selection);
Selection select_to_next_WORD_end(const BufferIterator& cursor); Selection select_to_next_WORD_end(const Selection& selection);
Selection select_to_previous_WORD(const BufferIterator& cursor); Selection select_to_previous_WORD(const Selection& selection);
Selection select_line(const BufferIterator& cursor); Selection select_line(const Selection& selection);
Selection select_matching(const BufferIterator& cursor); Selection select_matching(const Selection& selection);
Selection select_surrounding(const BufferIterator& cursor, Selection select_surrounding(const Selection& selection,
const std::pair<char, char>& matching, const std::pair<char, char>& matching,
bool inside); bool inside);
Selection select_to(const BufferIterator& cursor, char c, int count, bool inclusive); Selection select_to(const Selection& selection,
Selection select_to_reverse(const BufferIterator& cursor, char c, int count, bool inclusive); char c, int count, bool inclusive);
Selection select_to_reverse(const Selection& selection,
char c, int count, bool inclusive);
Selection select_to_eol(const BufferIterator& cursor); Selection select_to_eol(const Selection& selection);
Selection select_to_eol_reverse(const BufferIterator& cursor); Selection select_to_eol_reverse(const Selection& selection);
SelectionList select_whole_lines(const Selection& selection); Selection select_whole_lines(const Selection& selection);
Selection select_whole_buffer(const Selection& selection);
Selection select_next_match(const BufferIterator& cursor, Selection select_next_match(const Selection& selection,
const std::string& regex); const std::string& regex);
SelectionList select_all_matches(const Selection& selection, SelectionList select_all_matches(const Selection& selection,