2011-09-21 16:37:09 +02:00
|
|
|
#ifndef selectors_hh_INCLUDED
|
|
|
|
#define selectors_hh_INCLUDED
|
|
|
|
|
2012-02-07 15:26:51 +01:00
|
|
|
#include "selection.hh"
|
2013-01-07 18:53:27 +01:00
|
|
|
#include "unicode.hh"
|
2013-12-13 00:56:53 +01:00
|
|
|
#include "editor.hh"
|
2013-12-14 15:49:10 +01:00
|
|
|
#include "utf8_iterator.hh"
|
2011-09-21 16:37:09 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-12-14 15:08:26 +01:00
|
|
|
inline void clear_selections(const Buffer& buffer, SelectionList& selections)
|
|
|
|
{
|
|
|
|
auto& sel = selections.main();
|
|
|
|
auto& pos = sel.last();
|
|
|
|
avoid_eol(buffer, pos);
|
|
|
|
sel.first() = pos;
|
|
|
|
|
|
|
|
selections = SelectionList{ std::move(sel) };
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void flip_selections(const Buffer&, SelectionList& selections)
|
|
|
|
{
|
|
|
|
for (auto& sel : selections)
|
|
|
|
std::swap(sel.first(), sel.last());
|
|
|
|
selections.check_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void keep_selection(const Buffer&, SelectionList& selections, int index)
|
|
|
|
{
|
|
|
|
if (index < selections.size())
|
|
|
|
{
|
|
|
|
size_t real_index = (index + selections.main_index() + 1) % selections.size();
|
|
|
|
selections = SelectionList{ std::move(selections[real_index]) };
|
|
|
|
}
|
|
|
|
selections.check_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void remove_selection(const Buffer&, SelectionList& selections, int index)
|
|
|
|
{
|
|
|
|
if (selections.size() > 1 and index < selections.size())
|
|
|
|
{
|
|
|
|
size_t real_index = (index + selections.main_index() + 1) % selections.size();
|
|
|
|
selections.erase(selections.begin() + real_index);
|
|
|
|
size_t main_index = selections.main_index();
|
|
|
|
if (real_index <= main_index)
|
|
|
|
selections.set_main_index((main_index > 0 ? main_index
|
|
|
|
: selections.size()) - 1);
|
|
|
|
}
|
|
|
|
selections.check_invariant();
|
|
|
|
}
|
|
|
|
|
2013-12-14 15:49:10 +01:00
|
|
|
using Utf8Iterator = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pass>;
|
|
|
|
|
|
|
|
inline Range utf8_range(const Utf8Iterator& first, const Utf8Iterator& last)
|
|
|
|
{
|
|
|
|
return {first.base().coord(), last.base().coord()};
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef boost::regex_iterator<BufferIterator> RegexIterator;
|
2013-10-07 19:44:22 +02:00
|
|
|
|
|
|
|
template<WordType word_type>
|
2013-12-14 15:49:10 +01:00
|
|
|
Selection select_to_next_word(const Buffer& buffer, const Selection& selection)
|
|
|
|
{
|
|
|
|
Utf8Iterator begin = buffer.iterator_at(selection.last());
|
|
|
|
if (begin+1 == buffer.end())
|
|
|
|
return selection;
|
|
|
|
if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
|
|
|
|
++begin;
|
|
|
|
|
|
|
|
skip_while(begin, buffer.end(), is_eol);
|
|
|
|
if (begin == buffer.end())
|
|
|
|
return selection;
|
|
|
|
Utf8Iterator end = begin+1;
|
|
|
|
|
|
|
|
if (word_type == Word and is_punctuation(*begin))
|
|
|
|
skip_while(end, buffer.end(), is_punctuation);
|
|
|
|
else if (is_word<word_type>(*begin))
|
|
|
|
skip_while(end, buffer.end(), is_word<word_type>);
|
|
|
|
|
|
|
|
skip_while(end, buffer.end(), is_blank);
|
|
|
|
|
|
|
|
return utf8_range(begin, end-1);
|
|
|
|
}
|
|
|
|
|
2013-10-07 19:44:22 +02:00
|
|
|
template<WordType word_type>
|
2013-12-14 15:49:10 +01:00
|
|
|
Selection select_to_next_word_end(const Buffer& buffer, const Selection& selection)
|
|
|
|
{
|
|
|
|
Utf8Iterator begin = buffer.iterator_at(selection.last());
|
|
|
|
if (begin+1 == buffer.end())
|
|
|
|
return selection;
|
|
|
|
if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
|
|
|
|
++begin;
|
|
|
|
|
|
|
|
skip_while(begin, buffer.end(), is_eol);
|
|
|
|
if (begin == buffer.end())
|
|
|
|
return selection;
|
|
|
|
Utf8Iterator end = begin;
|
|
|
|
skip_while(end, buffer.end(), is_blank);
|
|
|
|
|
|
|
|
if (word_type == Word and is_punctuation(*end))
|
|
|
|
skip_while(end, buffer.end(), is_punctuation);
|
|
|
|
else if (is_word<word_type>(*end))
|
|
|
|
skip_while(end, buffer.end(), is_word<word_type>);
|
|
|
|
|
|
|
|
return utf8_range(begin, end-1);
|
|
|
|
}
|
|
|
|
|
2013-10-07 19:44:22 +02:00
|
|
|
template<WordType word_type>
|
2013-12-14 15:49:10 +01:00
|
|
|
Selection select_to_previous_word(const Buffer& buffer, const Selection& selection)
|
|
|
|
{
|
|
|
|
Utf8Iterator begin = buffer.iterator_at(selection.last());
|
|
|
|
if (begin == buffer.begin())
|
|
|
|
return selection;
|
|
|
|
if (categorize<word_type>(*begin) != categorize<word_type>(*(begin-1)))
|
|
|
|
--begin;
|
|
|
|
|
|
|
|
skip_while_reverse(begin, buffer.begin(), is_eol);
|
|
|
|
Utf8Iterator end = begin;
|
|
|
|
skip_while_reverse(end, buffer.begin(), is_blank);
|
|
|
|
|
|
|
|
bool with_end = false;
|
|
|
|
if (word_type == Word and is_punctuation(*end))
|
|
|
|
{
|
|
|
|
skip_while_reverse(end, buffer.begin(), is_punctuation);
|
|
|
|
with_end = is_punctuation(*end);
|
|
|
|
}
|
|
|
|
else if (is_word<word_type>(*end))
|
|
|
|
{
|
|
|
|
skip_while_reverse(end, buffer.begin(), is_word<word_type>);
|
|
|
|
with_end = is_word<word_type>(*end);
|
|
|
|
}
|
|
|
|
|
|
|
|
return utf8_range(begin, with_end ? end : end+1);
|
|
|
|
}
|
2012-10-08 19:12:09 +02:00
|
|
|
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_line(const Buffer& buffer,
|
|
|
|
const Selection& selection);
|
|
|
|
Selection select_matching(const Buffer& buffer,
|
|
|
|
const Selection& selection);
|
2012-10-08 19:12:09 +02:00
|
|
|
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_to(const Buffer& buffer, const Selection& selection,
|
2012-11-30 18:32:49 +01:00
|
|
|
Codepoint c, int count, bool inclusive);
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_to_reverse(const Buffer& buffer, const Selection& selection,
|
2012-11-30 18:32:49 +01:00
|
|
|
Codepoint c, int count, bool inclusive);
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_to_eol(const Buffer& buffer, const Selection& selection);
|
|
|
|
Selection select_to_eol_reverse(const Buffer& buffer, const Selection& selection);
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2013-05-15 14:24:09 +02:00
|
|
|
enum class ObjectFlags
|
|
|
|
{
|
|
|
|
ToBegin = 1,
|
|
|
|
ToEnd = 2,
|
|
|
|
Inner = 4
|
|
|
|
};
|
|
|
|
constexpr bool operator&(ObjectFlags lhs, ObjectFlags rhs)
|
|
|
|
{ return (bool)((int)lhs & (int) rhs); }
|
|
|
|
constexpr ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs)
|
|
|
|
{ return (ObjectFlags)((int)lhs | (int) rhs); }
|
|
|
|
|
2013-10-07 19:44:22 +02:00
|
|
|
template<WordType word_type>
|
2013-12-14 15:49:10 +01:00
|
|
|
Selection select_whole_word(const Buffer& buffer, const Selection& selection, ObjectFlags flags)
|
|
|
|
{
|
|
|
|
Utf8Iterator first = buffer.iterator_at(selection.last());
|
|
|
|
Utf8Iterator last = first;
|
|
|
|
if (is_word<word_type>(*first))
|
|
|
|
{
|
|
|
|
if (flags & ObjectFlags::ToBegin)
|
|
|
|
{
|
|
|
|
skip_while_reverse(first, buffer.begin(), is_word<word_type>);
|
|
|
|
if (not is_word<word_type>(*first))
|
|
|
|
++first;
|
|
|
|
}
|
|
|
|
if (flags & ObjectFlags::ToEnd)
|
|
|
|
{
|
|
|
|
skip_while(last, buffer.end(), is_word<word_type>);
|
|
|
|
if (not (flags & ObjectFlags::Inner))
|
|
|
|
skip_while(last, buffer.end(), is_blank);
|
|
|
|
--last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (not (flags & ObjectFlags::Inner))
|
|
|
|
{
|
|
|
|
if (flags & ObjectFlags::ToBegin)
|
|
|
|
{
|
|
|
|
skip_while_reverse(first, buffer.begin(), is_blank);
|
|
|
|
if (not is_word<word_type>(*first))
|
|
|
|
return selection;
|
|
|
|
skip_while_reverse(first, buffer.begin(), is_word<word_type>);
|
|
|
|
if (not is_word<word_type>(*first))
|
|
|
|
++first;
|
|
|
|
}
|
|
|
|
if (flags & ObjectFlags::ToEnd)
|
|
|
|
{
|
|
|
|
skip_while(last, buffer.end(), is_blank);
|
|
|
|
--last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (flags & ObjectFlags::ToEnd) ? utf8_range(first, last)
|
|
|
|
: utf8_range(last, first);
|
|
|
|
}
|
|
|
|
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_whole_sentence(const Buffer& buffer, const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
|
|
|
Selection select_whole_paragraph(const Buffer& buffer, const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
2013-07-22 19:58:16 +02:00
|
|
|
Selection select_whole_indent(const Buffer& buffer, const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_whole_lines(const Buffer& buffer, const Selection& selection);
|
2013-12-14 14:21:07 +01:00
|
|
|
void select_whole_buffer(const Buffer& buffer, SelectionList& selections);
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection trim_partial_lines(const Buffer& buffer, const Selection& selection);
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2013-07-02 14:55:34 +02:00
|
|
|
enum Direction { Forward, Backward };
|
|
|
|
|
2013-12-13 00:56:53 +01:00
|
|
|
using MatchResults = boost::match_results<BufferIterator>;
|
|
|
|
|
|
|
|
static bool find_last_match(BufferIterator begin, const BufferIterator& end,
|
|
|
|
MatchResults& res, const Regex& regex)
|
|
|
|
{
|
|
|
|
MatchResults matches;
|
|
|
|
while (boost::regex_search(begin, end, matches, regex))
|
|
|
|
{
|
|
|
|
if (begin == matches[0].second)
|
|
|
|
break;
|
|
|
|
begin = matches[0].second;
|
|
|
|
res.swap(matches);
|
|
|
|
}
|
|
|
|
return not res.empty();
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:55:34 +02:00
|
|
|
template<Direction direction>
|
2013-12-13 00:56:53 +01:00
|
|
|
bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos,
|
|
|
|
MatchResults& matches, const Regex& ex)
|
|
|
|
{
|
|
|
|
if (direction == Forward)
|
|
|
|
return (boost::regex_search(pos, buffer.end(), matches, ex) or
|
|
|
|
boost::regex_search(buffer.begin(), pos, matches, ex));
|
|
|
|
else
|
|
|
|
return (find_last_match(buffer.begin(), pos, matches, ex) or
|
|
|
|
find_last_match(pos, buffer.end(), matches, ex));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Direction direction, SelectMode mode>
|
2013-12-13 10:11:48 +01:00
|
|
|
void select_next_match(const Buffer& buffer, SelectionList& selections,
|
2013-12-13 00:56:53 +01:00
|
|
|
const Regex& regex)
|
|
|
|
{
|
|
|
|
auto& sel = selections.main();
|
|
|
|
auto begin = buffer.iterator_at(sel.last());
|
|
|
|
auto end = begin;
|
|
|
|
CaptureList captures;
|
|
|
|
|
|
|
|
MatchResults matches;
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
if ((found = find_match_in_buffer<direction>(buffer, utf8::next(begin), matches, regex)))
|
|
|
|
{
|
|
|
|
begin = matches[0].first;
|
|
|
|
end = matches[0].second;
|
|
|
|
for (auto& match : matches)
|
|
|
|
captures.emplace_back(match.first, match.second);
|
|
|
|
}
|
|
|
|
if (not found or begin == buffer.end())
|
|
|
|
throw runtime_error("'" + regex.str() + "': no matches found");
|
|
|
|
|
|
|
|
end = (begin == end) ? end : utf8::previous(end);
|
|
|
|
if (direction == Backward)
|
|
|
|
std::swap(begin, end);
|
|
|
|
|
|
|
|
Selection res{begin.coord(), end.coord(), std::move(captures)};
|
|
|
|
if (mode == SelectMode::Replace)
|
2013-12-13 10:11:48 +01:00
|
|
|
selections = SelectionList{ std::move(res) };
|
2013-12-13 00:56:53 +01:00
|
|
|
else if (mode == SelectMode::ReplaceMain)
|
|
|
|
sel = std::move(res);
|
|
|
|
else if (mode == SelectMode::Append)
|
|
|
|
{
|
|
|
|
selections.push_back(std::move(res));
|
|
|
|
selections.set_main_index(selections.size() - 1);
|
|
|
|
}
|
|
|
|
selections.sort_and_merge_overlapping();
|
|
|
|
}
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2013-12-14 14:21:07 +01:00
|
|
|
void select_all_matches(const Buffer& buffer, SelectionList& selections,
|
2013-12-14 15:08:26 +01:00
|
|
|
const Regex& regex);
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2013-12-14 14:21:07 +01:00
|
|
|
void split_selection(const Buffer& buffer, SelectionList& selections,
|
2013-04-05 19:28:08 +02:00
|
|
|
const Regex& separator_regex);
|
2011-11-21 20:30:44 +01:00
|
|
|
|
2013-01-07 18:53:27 +01:00
|
|
|
using CodepointPair = std::pair<Codepoint, Codepoint>;
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_surrounding(const Buffer& buffer, const Selection& selection,
|
2013-10-08 20:24:56 +02:00
|
|
|
CodepointPair matching, int level, ObjectFlags flags);
|
2013-01-07 18:53:27 +01:00
|
|
|
|
2011-09-21 16:37:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selectors_hh_INCLUDED
|