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
|
|
|
|
{
|
|
|
|
|
2012-02-10 00:47:55 +01:00
|
|
|
Selection::Selection(const BufferIterator& first, const BufferIterator& last)
|
|
|
|
: m_first(first), m_last(last)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-10-08 14:26:57 +02:00
|
|
|
check_invariant();
|
|
|
|
register_with_buffer();
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Selection::Selection(const Selection& other)
|
2012-02-10 00:47:55 +01:00
|
|
|
: m_first(other.m_first), m_last(other.m_last)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
|
|
|
register_with_buffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
Selection::~Selection()
|
|
|
|
{
|
|
|
|
unregister_with_buffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
Selection& Selection::operator=(const Selection& other)
|
|
|
|
{
|
|
|
|
const bool new_buffer = &m_first.buffer() != &other.m_first.buffer();
|
|
|
|
if (new_buffer)
|
|
|
|
unregister_with_buffer();
|
|
|
|
|
|
|
|
m_first = other.m_first;
|
|
|
|
m_last = other.m_last;
|
|
|
|
|
|
|
|
if (new_buffer)
|
|
|
|
register_with_buffer();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferIterator Selection::begin() const
|
|
|
|
{
|
|
|
|
return std::min(m_first, m_last);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferIterator Selection::end() const
|
|
|
|
{
|
2012-10-08 14:26:57 +02:00
|
|
|
return utf8::next(std::max(m_first, m_last));
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::merge_with(const Selection& selection)
|
|
|
|
{
|
2012-09-05 14:27:48 +02:00
|
|
|
if (m_first < m_last)
|
2012-01-31 20:12:06 +01:00
|
|
|
m_first = std::min(m_first, selection.m_first);
|
2012-09-05 14:27:48 +02:00
|
|
|
if (m_first > m_last)
|
2012-01-31 20:12:06 +01:00
|
|
|
m_first = std::max(m_first, selection.m_first);
|
|
|
|
m_last = selection.m_last;
|
|
|
|
}
|
|
|
|
|
2012-10-08 14:26:57 +02:00
|
|
|
static void avoid_eol(BufferIterator& it)
|
|
|
|
{
|
|
|
|
const auto column = it.column();
|
|
|
|
if (column != 0 and column == it.buffer().line_length(it.line()) - 1)
|
|
|
|
it = utf8::previous(it);
|
|
|
|
}
|
|
|
|
|
2012-08-15 18:20:02 +02:00
|
|
|
void Selection::avoid_eol()
|
|
|
|
{
|
2012-10-08 14:26:57 +02:00
|
|
|
Kakoune::avoid_eol(m_first);
|
|
|
|
Kakoune::avoid_eol(m_last);
|
2012-08-15 18:20:02 +02:00
|
|
|
}
|
|
|
|
|
2012-07-16 21:51:37 +02:00
|
|
|
void Selection::on_insert(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
|
|
|
m_first.on_insert(begin.coord(), end.coord());
|
|
|
|
m_last.on_insert(begin.coord(), end.coord());
|
2012-10-08 14:26:57 +02:00
|
|
|
check_invariant();
|
2012-07-16 21:51:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::on_erase(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
|
|
|
m_first.on_erase(begin.coord(), end.coord());
|
|
|
|
m_last.on_erase(begin.coord(), end.coord());
|
2012-10-08 14:26:57 +02:00
|
|
|
check_invariant();
|
2012-07-16 21:51:37 +02:00
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
void Selection::register_with_buffer()
|
|
|
|
{
|
2012-11-12 20:11:27 +01:00
|
|
|
m_first.buffer().add_change_listener(*this);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::unregister_with_buffer()
|
|
|
|
{
|
2012-11-12 20:11:27 +01:00
|
|
|
m_first.buffer().remove_change_listener(*this);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-10-08 14:26:57 +02:00
|
|
|
void Selection::check_invariant() const
|
|
|
|
{
|
|
|
|
assert(utf8::is_character_start(m_first));
|
|
|
|
assert(utf8::is_character_start(m_last));
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|