2011-09-21 16:37:09 +02:00
|
|
|
#ifndef selectors_hh_INCLUDED
|
|
|
|
#define selectors_hh_INCLUDED
|
|
|
|
|
2014-10-23 19:55:45 +02:00
|
|
|
#include "flags.hh"
|
2012-02-07 15:26:51 +01:00
|
|
|
#include "selection.hh"
|
2014-04-28 20:48:23 +02:00
|
|
|
#include "buffer_utils.hh"
|
2013-01-07 18:53:27 +01:00
|
|
|
#include "unicode.hh"
|
2013-12-14 15:49:10 +01:00
|
|
|
#include "utf8_iterator.hh"
|
2014-10-13 14:12:33 +02:00
|
|
|
#include "regex.hh"
|
2011-09-21 16:37:09 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-09-18 01:34:23 +02:00
|
|
|
inline Selection keep_direction(Selection res, const Selection& ref)
|
|
|
|
{
|
|
|
|
if ((res.cursor() < res.anchor()) != (ref.cursor() < ref.anchor()))
|
|
|
|
std::swap<ByteCoord>(res.cursor(), res.anchor());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:51:06 +02:00
|
|
|
inline Selection target_eol(Selection sel)
|
|
|
|
{
|
|
|
|
sel.cursor().target = INT_MAX;
|
|
|
|
return sel;
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:56:08 +02:00
|
|
|
using Utf8Iterator = utf8::iterator<BufferIterator>;
|
2013-12-14 15:49:10 +01:00
|
|
|
|
2016-01-26 08:23:18 +01:00
|
|
|
inline Selection utf8_range(const BufferIterator& first, const BufferIterator& last)
|
|
|
|
{
|
|
|
|
return {first.coord(), last.coord()};
|
|
|
|
}
|
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
inline Selection utf8_range(const Utf8Iterator& first, const Utf8Iterator& last)
|
2013-12-14 15:49:10 +01:00
|
|
|
{
|
|
|
|
return {first.base().coord(), last.base().coord()};
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-09-23 20:39:21 +02:00
|
|
|
Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer};
|
2013-12-14 15:49:10 +01:00
|
|
|
if (begin+1 == buffer.end())
|
|
|
|
return selection;
|
|
|
|
if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
|
|
|
|
++begin;
|
|
|
|
|
2016-03-06 01:07:06 +01:00
|
|
|
if (not skip_while(begin, buffer.end(),
|
|
|
|
[](Codepoint c) { return is_eol(c); }))
|
2013-12-14 15:49:10 +01:00
|
|
|
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>);
|
|
|
|
|
2015-04-15 01:34:00 +02:00
|
|
|
skip_while(end, buffer.end(), is_horizontal_blank);
|
2013-12-14 15:49:10 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-09-23 20:39:21 +02:00
|
|
|
Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer};
|
2013-12-14 15:49:10 +01:00
|
|
|
if (begin+1 == buffer.end())
|
|
|
|
return selection;
|
|
|
|
if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
|
|
|
|
++begin;
|
|
|
|
|
2016-03-06 01:07:06 +01:00
|
|
|
if (not skip_while(begin, buffer.end(),
|
|
|
|
[](Codepoint c) { return is_eol(c); }))
|
2013-12-14 15:49:10 +01:00
|
|
|
return selection;
|
|
|
|
Utf8Iterator end = begin;
|
2015-04-15 01:34:00 +02:00
|
|
|
skip_while(end, buffer.end(), is_horizontal_blank);
|
2013-12-14 15:49:10 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-09-23 20:39:21 +02:00
|
|
|
Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer};
|
2013-12-14 15:49:10 +01:00
|
|
|
if (begin == buffer.begin())
|
|
|
|
return selection;
|
|
|
|
if (categorize<word_type>(*begin) != categorize<word_type>(*(begin-1)))
|
|
|
|
--begin;
|
|
|
|
|
2015-12-23 22:43:07 +01:00
|
|
|
skip_while_reverse(begin, buffer.begin(), [](Codepoint c){ return is_eol(c); });
|
2013-12-14 15:49:10 +01:00
|
|
|
Utf8Iterator end = begin;
|
2015-04-15 01:34:00 +02:00
|
|
|
skip_while_reverse(end, buffer.begin(), is_horizontal_blank);
|
2013-12-14 15:49:10 +01:00
|
|
|
|
|
|
|
bool with_end = false;
|
|
|
|
if (word_type == Word and is_punctuation(*end))
|
2016-03-06 01:07:06 +01:00
|
|
|
with_end = skip_while_reverse(end, buffer.begin(), is_punctuation);
|
|
|
|
|
2013-12-14 15:49:10 +01:00
|
|
|
else if (is_word<word_type>(*end))
|
2016-03-06 01:07:06 +01:00
|
|
|
with_end = skip_while_reverse(end, buffer.begin(), is_word<word_type>);
|
2013-12-14 15:49:10 +01:00
|
|
|
|
|
|
|
return utf8_range(begin, with_end ? end : end+1);
|
|
|
|
}
|
2012-10-08 19:12:09 +02:00
|
|
|
|
2014-05-27 10:50:12 +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
|
|
|
|
2015-06-08 14:51:06 +02:00
|
|
|
template<bool only_move>
|
|
|
|
Selection select_to_line_end(const Buffer& buffer, const Selection& selection)
|
|
|
|
{
|
|
|
|
ByteCoord begin = selection.cursor();
|
|
|
|
LineCount line = begin.line;
|
|
|
|
ByteCoord end = utf8::previous(buffer.iterator_at({line, buffer[line].length() - 1}),
|
|
|
|
buffer.iterator_at(line)).coord();
|
|
|
|
return target_eol({only_move ? end : begin, end});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool only_move>
|
|
|
|
Selection select_to_line_begin(const Buffer& buffer, const Selection& selection)
|
|
|
|
{
|
|
|
|
ByteCoord begin = selection.cursor();
|
|
|
|
ByteCoord end = begin.line;
|
|
|
|
return {only_move ? end : begin, end};
|
|
|
|
}
|
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
|
|
|
|
};
|
2014-10-23 19:55:45 +02:00
|
|
|
|
|
|
|
template<> struct WithBitOps<ObjectFlags> : std::true_type {};
|
2013-05-15 14:24:09 +02:00
|
|
|
|
2013-10-07 19:44:22 +02:00
|
|
|
template<WordType word_type>
|
2014-05-27 10:50:12 +02:00
|
|
|
Selection select_word(const Buffer& buffer,
|
|
|
|
const Selection& selection,
|
|
|
|
ObjectFlags flags)
|
2013-12-14 15:49:10 +01:00
|
|
|
{
|
2015-09-23 20:39:21 +02:00
|
|
|
Utf8Iterator first{buffer.iterator_at(selection.cursor()), buffer};
|
2013-12-14 15:49:10 +01:00
|
|
|
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))
|
2015-04-15 01:34:00 +02:00
|
|
|
skip_while(last, buffer.end(), is_horizontal_blank);
|
2013-12-14 15:49:10 +01:00
|
|
|
--last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (not (flags & ObjectFlags::Inner))
|
|
|
|
{
|
|
|
|
if (flags & ObjectFlags::ToBegin)
|
|
|
|
{
|
2015-04-15 01:34:00 +02:00
|
|
|
skip_while_reverse(first, buffer.begin(), is_horizontal_blank);
|
2013-12-14 15:49:10 +01:00
|
|
|
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)
|
|
|
|
{
|
2015-04-15 01:34:00 +02:00
|
|
|
skip_while(last, buffer.end(), is_horizontal_blank);
|
2013-12-14 15:49:10 +01:00
|
|
|
--last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (flags & ObjectFlags::ToEnd) ? utf8_range(first, last)
|
|
|
|
: utf8_range(last, first);
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:50:12 +02:00
|
|
|
Selection select_number(const Buffer& buffer,
|
|
|
|
const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
|
|
|
|
2014-05-26 22:44:57 +02:00
|
|
|
Selection select_sentence(const Buffer& buffer,
|
|
|
|
const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
|
|
|
|
|
|
|
Selection select_paragraph(const Buffer& buffer,
|
|
|
|
const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
|
|
|
|
2014-06-11 15:00:45 +02:00
|
|
|
Selection select_whitespaces(const Buffer& buffer,
|
|
|
|
const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
|
|
|
|
2014-05-26 22:44:57 +02:00
|
|
|
Selection select_indent(const Buffer& buffer,
|
|
|
|
const Selection& selection,
|
|
|
|
ObjectFlags flags);
|
|
|
|
|
2015-06-25 15:44:43 +02:00
|
|
|
Selection select_argument(const Buffer& buffer,
|
|
|
|
const Selection& selection,
|
|
|
|
int level, ObjectFlags flags);
|
|
|
|
|
2014-05-26 22:44:57 +02:00
|
|
|
Selection select_lines(const Buffer& buffer, const Selection& selection);
|
|
|
|
|
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
|
|
|
|
2014-05-27 01:35:12 +02:00
|
|
|
void select_buffer(SelectionList& selections);
|
2014-05-26 22:44:57 +02:00
|
|
|
|
2013-07-02 14:55:34 +02:00
|
|
|
enum Direction { Forward, Backward };
|
|
|
|
|
2015-12-23 22:43:07 +01:00
|
|
|
inline bool find_last_match(const Buffer& buffer, const BufferIterator& pos,
|
2014-10-13 14:12:33 +02:00
|
|
|
MatchResults<BufferIterator>& res,
|
|
|
|
const Regex& regex)
|
2013-12-13 00:56:53 +01:00
|
|
|
{
|
2014-10-13 14:12:33 +02:00
|
|
|
MatchResults<BufferIterator> matches;
|
2015-12-23 22:43:07 +01:00
|
|
|
const bool is_pos_eol = is_eol(buffer, pos.coord());
|
|
|
|
const bool is_pos_eow = is_eow(buffer, pos.coord());
|
|
|
|
auto begin = buffer.begin();
|
2015-12-27 16:09:37 +01:00
|
|
|
while (begin != pos and regex_search(begin, pos, matches, regex,
|
|
|
|
match_flags(is_bol(begin.coord()), is_pos_eol, is_pos_eow)))
|
2013-12-13 00:56:53 +01:00
|
|
|
{
|
2015-12-27 16:09:37 +01:00
|
|
|
begin = utf8::next(matches[0].first, pos);
|
|
|
|
if (res.empty() or matches[0].second > res[0].second)
|
|
|
|
res.swap(matches);
|
2013-12-13 00:56:53 +01:00
|
|
|
}
|
|
|
|
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,
|
2014-10-13 14:12:33 +02:00
|
|
|
MatchResults<BufferIterator>& matches,
|
2016-02-29 14:50:18 +01:00
|
|
|
const Regex& ex, bool& wrapped)
|
2013-12-13 00:56:53 +01:00
|
|
|
{
|
2016-02-29 14:50:18 +01:00
|
|
|
wrapped = false;
|
2013-12-13 00:56:53 +01:00
|
|
|
if (direction == Forward)
|
2016-02-29 14:50:18 +01:00
|
|
|
{
|
|
|
|
if (regex_search(pos, buffer.end(), matches, ex,
|
|
|
|
match_flags(is_bol(pos.coord()), true, true)))
|
|
|
|
return true;
|
|
|
|
wrapped = true;
|
|
|
|
return regex_search(buffer.begin(), buffer.end(), matches, ex);
|
|
|
|
}
|
2013-12-13 00:56:53 +01:00
|
|
|
else
|
2016-02-29 14:50:18 +01:00
|
|
|
{
|
|
|
|
if (find_last_match(buffer, pos, matches, ex))
|
|
|
|
return true;
|
|
|
|
wrapped = true;
|
|
|
|
return find_last_match(buffer, buffer.end(), matches, ex);
|
|
|
|
}
|
2013-12-13 00:56:53 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 23:48:54 +01:00
|
|
|
inline BufferIterator ensure_char_start(const Buffer& buffer, const BufferIterator& it)
|
|
|
|
{
|
|
|
|
return it != buffer.end() ?
|
|
|
|
utf8::character_start(it, buffer.iterator_at(it.coord().line)) : it;
|
|
|
|
}
|
|
|
|
|
2013-12-14 19:39:03 +01:00
|
|
|
template<Direction direction>
|
2016-02-29 14:50:18 +01:00
|
|
|
Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex, bool& wrapped)
|
2013-12-13 00:56:53 +01:00
|
|
|
{
|
2014-09-26 01:27:23 +02:00
|
|
|
auto begin = buffer.iterator_at(direction == Backward ? sel.min() : sel.max());
|
2013-12-13 00:56:53 +01:00
|
|
|
auto end = begin;
|
|
|
|
|
2013-12-14 19:39:03 +01:00
|
|
|
CaptureList captures;
|
2014-10-13 14:12:33 +02:00
|
|
|
MatchResults<BufferIterator> matches;
|
2013-12-13 00:56:53 +01:00
|
|
|
bool found = false;
|
2015-09-14 15:28:45 +02:00
|
|
|
auto pos = direction == Forward ? utf8::next(begin, buffer.end()) : begin;
|
2016-02-29 14:50:18 +01:00
|
|
|
if ((found = find_match_in_buffer<direction>(buffer, pos, matches, regex, wrapped)))
|
2013-12-13 00:56:53 +01:00
|
|
|
{
|
2015-02-02 23:48:54 +01:00
|
|
|
begin = ensure_char_start(buffer, matches[0].first);
|
|
|
|
end = ensure_char_start(buffer, matches[0].second);
|
2013-12-13 00:56:53 +01:00
|
|
|
for (auto& match : matches)
|
2016-02-05 00:52:06 +01:00
|
|
|
captures.push_back(buffer.string(match.first.coord(),
|
|
|
|
match.second.coord()));
|
2013-12-13 00:56:53 +01:00
|
|
|
}
|
|
|
|
if (not found or begin == buffer.end())
|
2015-06-01 22:15:59 +02:00
|
|
|
throw runtime_error(format("'{}': no matches found", regex.str()));
|
2013-12-13 00:56:53 +01:00
|
|
|
|
2014-07-02 22:14:01 +02:00
|
|
|
end = (begin == end) ? end : utf8::previous(end, begin);
|
2013-12-13 00:56:53 +01:00
|
|
|
if (direction == Backward)
|
|
|
|
std::swap(begin, end);
|
|
|
|
|
2014-09-25 14:29:53 +02:00
|
|
|
return {begin.coord(), end.coord(), std::move(captures)};
|
2013-12-14 19:39:03 +01:00
|
|
|
}
|
|
|
|
|
2015-11-26 14:36:26 +01:00
|
|
|
void select_all_matches(SelectionList& selections, const Regex& regex, unsigned capture = 0);
|
|
|
|
void split_selections(SelectionList& selections, const Regex& separator_regex, unsigned capture = 0);
|
2011-11-21 20:30:44 +01:00
|
|
|
|
2013-06-01 14:22:57 +02:00
|
|
|
Selection select_surrounding(const Buffer& buffer, const Selection& selection,
|
2016-01-27 09:28:25 +01:00
|
|
|
StringView opening, StringView closing, int level,
|
|
|
|
ObjectFlags flags);
|
2013-01-07 18:53:27 +01:00
|
|
|
|
2011-09-21 16:37:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selectors_hh_INCLUDED
|