2012-01-31 20:12:06 +01:00
|
|
|
#include "selection.hh"
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
register_with_buffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return std::max(m_first, m_last) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-15 18:20:02 +02:00
|
|
|
void Selection::avoid_eol()
|
|
|
|
{
|
|
|
|
m_first.clamp(true);
|
|
|
|
m_last.clamp(true);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-31 20:12:06 +01:00
|
|
|
void Selection::register_with_buffer()
|
|
|
|
{
|
2012-04-04 15:56:19 +02:00
|
|
|
Buffer& buffer = const_cast<Buffer&>(m_first.buffer());
|
2012-07-16 21:51:37 +02:00
|
|
|
buffer.add_change_listener(*this);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Selection::unregister_with_buffer()
|
|
|
|
{
|
2012-04-04 15:56:19 +02:00
|
|
|
Buffer& buffer = const_cast<Buffer&>(m_first.buffer());
|
2012-07-16 21:51:37 +02:00
|
|
|
buffer.remove_change_listener(*this);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|