2012-01-31 20:12:06 +01:00
|
|
|
#include "selection.hh"
|
|
|
|
|
2012-10-08 14:26:57 +02:00
|
|
|
#include "utf8.hh"
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
void Selection::merge_with(const Selection& range)
|
2012-11-30 18:32:49 +01:00
|
|
|
{
|
2014-01-28 20:05:49 +01:00
|
|
|
m_cursor = range.m_cursor;
|
|
|
|
if (m_anchor < m_cursor)
|
|
|
|
m_anchor = std::min(m_anchor, range.m_anchor);
|
|
|
|
if (m_anchor > m_cursor)
|
|
|
|
m_anchor = std::max(m_anchor, range.m_anchor);
|
2012-11-30 18:32:49 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 19:04:59 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
template<template <bool, bool> class UpdateFunc>
|
2014-05-14 00:22:54 +02:00
|
|
|
void on_buffer_change(std::vector<Selection>& sels,
|
2014-05-12 14:59:21 +02:00
|
|
|
ByteCoord begin, ByteCoord end, bool at_end, LineCount end_line)
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
|
|
|
auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin,
|
2014-05-07 20:51:01 +02:00
|
|
|
[](const Selection& s, ByteCoord c)
|
2014-04-08 21:09:54 +02:00
|
|
|
{ return s.max() < c; });
|
2013-05-02 19:04:59 +02:00
|
|
|
auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line,
|
2014-04-08 21:09:54 +02:00
|
|
|
[](LineCount l, const Selection& s)
|
|
|
|
{ return l < s.min().line; });
|
2013-05-02 19:04:59 +02:00
|
|
|
|
|
|
|
if (update_beg != update_only_line_beg)
|
|
|
|
{
|
|
|
|
// for the first one, we are not sure if min < begin
|
2014-05-12 14:59:21 +02:00
|
|
|
UpdateFunc<false, false>{}(update_beg->anchor(), begin, end, at_end);
|
|
|
|
UpdateFunc<false, false>{}(update_beg->cursor(), begin, end, at_end);
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
for (auto it = update_beg+1; it < update_only_line_beg; ++it)
|
|
|
|
{
|
2014-05-12 14:59:21 +02:00
|
|
|
UpdateFunc<false, true>{}(it->anchor(), begin, end, at_end);
|
|
|
|
UpdateFunc<false, true>{}(it->cursor(), begin, end, at_end);
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
if (end.line > begin.line)
|
|
|
|
{
|
|
|
|
for (auto it = update_only_line_beg; it != sels.end(); ++it)
|
|
|
|
{
|
2014-05-12 14:59:21 +02:00
|
|
|
UpdateFunc<true, true>{}(it->anchor(), begin, end, at_end);
|
|
|
|
UpdateFunc<true, true>{}(it->cursor(), begin, end, at_end);
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateInsert
|
|
|
|
{
|
2014-05-12 14:59:21 +02:00
|
|
|
void operator()(ByteCoord& coord, ByteCoord begin, ByteCoord end,
|
|
|
|
bool at_end) const
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
|
|
|
if (assume_different_line)
|
|
|
|
kak_assert(begin.line < coord.line);
|
|
|
|
if (not assume_greater_than_begin and coord < begin)
|
|
|
|
return;
|
|
|
|
if (not assume_different_line and begin.line == coord.line)
|
|
|
|
coord.column = end.column + coord.column - begin.column;
|
|
|
|
|
|
|
|
coord.line += end.line - begin.line;
|
2014-05-13 00:25:15 +02:00
|
|
|
kak_assert(coord.line >= 0 and coord.column >= 0);
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateErase
|
|
|
|
{
|
2014-05-12 14:59:21 +02:00
|
|
|
void operator()(ByteCoord& coord, ByteCoord begin, ByteCoord end,
|
|
|
|
bool at_end) const
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
|
|
|
if (not assume_greater_than_begin and coord < begin)
|
|
|
|
return;
|
|
|
|
if (assume_different_line)
|
|
|
|
kak_assert(end.line < coord.line);
|
|
|
|
if (not assume_different_line and coord <= end)
|
2014-05-12 14:59:21 +02:00
|
|
|
{
|
2014-05-13 00:25:15 +02:00
|
|
|
if (not at_end or begin == ByteCoord{0,0})
|
2014-05-12 14:59:21 +02:00
|
|
|
coord = begin;
|
|
|
|
else
|
|
|
|
coord = begin.column ? ByteCoord{begin.line, begin.column-1}
|
|
|
|
: ByteCoord{begin.line - 1};
|
|
|
|
}
|
2013-05-02 19:04:59 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (not assume_different_line and end.line == coord.line)
|
|
|
|
{
|
|
|
|
coord.line = begin.line;
|
|
|
|
coord.column = begin.column + coord.column - end.column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
coord.line -= end.line - begin.line;
|
|
|
|
}
|
2014-05-13 00:25:15 +02:00
|
|
|
kak_assert(coord.line >= 0 and coord.column >= 0);
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
SelectionList::SelectionList(const Buffer& buffer, Selection s, size_t timestamp)
|
|
|
|
: m_buffer(&buffer), m_selections({ s }), m_timestamp(timestamp)
|
2014-05-14 00:22:54 +02:00
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
}
|
2014-05-13 00:25:15 +02:00
|
|
|
|
|
|
|
SelectionList::SelectionList(const Buffer& buffer, Selection s)
|
|
|
|
: SelectionList(buffer, s, buffer.timestamp())
|
|
|
|
{}
|
|
|
|
|
|
|
|
SelectionList::SelectionList(const Buffer& buffer, std::vector<Selection> s, size_t timestamp)
|
|
|
|
: m_buffer(&buffer), m_selections(std::move(s)), m_timestamp(timestamp)
|
2014-05-14 00:22:54 +02:00
|
|
|
{
|
|
|
|
kak_assert(size() > 0);
|
|
|
|
check_invariant();
|
|
|
|
}
|
2014-05-13 00:25:15 +02:00
|
|
|
|
|
|
|
SelectionList::SelectionList(const Buffer& buffer, std::vector<Selection> s)
|
|
|
|
: SelectionList(buffer, std::move(s), buffer.timestamp())
|
|
|
|
{}
|
|
|
|
|
2014-05-14 00:22:54 +02:00
|
|
|
void update_insert(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end, bool at_end)
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
2014-05-14 00:22:54 +02:00
|
|
|
on_buffer_change<UpdateInsert>(sels, begin, end, at_end, begin.line);
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
|
2014-05-14 00:22:54 +02:00
|
|
|
void update_erase(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end, bool at_end)
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
2014-05-14 00:22:54 +02:00
|
|
|
on_buffer_change<UpdateErase>(sels, begin, end, at_end, end.line);
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
void SelectionList::update()
|
|
|
|
{
|
2014-05-14 01:59:36 +02:00
|
|
|
if (m_timestamp == m_buffer->timestamp())
|
|
|
|
return;
|
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
for (auto& change : m_buffer->changes_since(m_timestamp))
|
|
|
|
{
|
|
|
|
if (change.type == Buffer::Change::Insert)
|
2014-05-14 00:22:54 +02:00
|
|
|
update_insert(m_selections, change.begin, change.end, change.at_end);
|
2014-05-13 00:25:15 +02:00
|
|
|
else
|
2014-05-14 00:22:54 +02:00
|
|
|
update_erase(m_selections, change.begin, change.end, change.at_end);
|
2014-05-13 00:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
check_invariant();
|
2014-05-14 01:59:36 +02:00
|
|
|
|
|
|
|
m_timestamp = m_buffer->timestamp();
|
2014-05-13 00:25:15 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 13:38:48 +02:00
|
|
|
void SelectionList::check_invariant() const
|
|
|
|
{
|
2014-05-13 00:25:15 +02:00
|
|
|
#ifdef KAK_DEBUG
|
|
|
|
auto& buffer = this->buffer();
|
2013-12-14 15:11:14 +01:00
|
|
|
kak_assert(size() > 0);
|
2013-12-13 00:17:06 +01:00
|
|
|
kak_assert(m_main < size());
|
2014-05-13 00:25:15 +02:00
|
|
|
for (size_t i = 0; i < size(); ++i)
|
|
|
|
{
|
|
|
|
auto& sel = (*this)[i];
|
2014-05-14 01:59:36 +02:00
|
|
|
if (i+1 < size())
|
|
|
|
kak_assert((*this)[i].min() <= (*this)[i+1].min());
|
2014-05-13 00:25:15 +02:00
|
|
|
kak_assert(buffer.is_valid(sel.anchor()));
|
|
|
|
kak_assert(buffer.is_valid(sel.cursor()));
|
|
|
|
kak_assert(not buffer.is_end(sel.anchor()));
|
|
|
|
kak_assert(not buffer.is_end(sel.cursor()));
|
2014-05-14 20:21:19 +02:00
|
|
|
kak_assert(utf8::is_character_start(buffer.byte_at(sel.anchor())));
|
|
|
|
kak_assert(utf8::is_character_start(buffer.byte_at(sel.cursor())));
|
2014-05-13 00:25:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionList::sort_and_merge_overlapping()
|
|
|
|
{
|
|
|
|
if (size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto& main = this->main();
|
|
|
|
const auto main_begin = main.min();
|
|
|
|
m_main = std::count_if(begin(), end(), [&](const Selection& sel) {
|
|
|
|
auto begin = sel.min();
|
|
|
|
if (begin == main_begin)
|
|
|
|
return &sel < &main;
|
|
|
|
else
|
|
|
|
return begin < main_begin;
|
|
|
|
});
|
|
|
|
std::stable_sort(begin(), end(), compare_selections);
|
|
|
|
merge_overlapping(overlaps);
|
2013-05-03 13:38:48 +02:00
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|