2012-01-31 23:12:06 +04:00
|
|
|
#include "selection.hh"
|
|
|
|
|
2012-10-08 16:26:57 +04:00
|
|
|
#include "utf8.hh"
|
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-11-30 21:32:49 +04:00
|
|
|
void Range::merge_with(const Range& range)
|
|
|
|
{
|
2013-05-14 15:58:35 +04:00
|
|
|
m_last = range.m_last;
|
2012-11-30 21:32:49 +04:00
|
|
|
if (m_first < m_last)
|
|
|
|
m_first = std::min(m_first, range.m_first);
|
|
|
|
if (m_first > m_last)
|
|
|
|
m_first = std::max(m_first, range.m_first);
|
|
|
|
}
|
|
|
|
|
2013-05-02 21:04:59 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
template<template <bool, bool> class UpdateFunc>
|
2013-06-01 02:48:46 +04:00
|
|
|
void on_buffer_change(const Buffer& buffer, SelectionList& sels,
|
2013-07-26 02:44:00 +04:00
|
|
|
BufferCoord begin, BufferCoord end, LineCount end_line)
|
2013-05-02 21:04:59 +04:00
|
|
|
{
|
|
|
|
auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin,
|
2013-07-26 02:44:00 +04:00
|
|
|
[](const Selection& s, BufferCoord c) { return std::max(s.first(), s.last()) < c; });
|
2013-05-02 21:04:59 +04:00
|
|
|
auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line,
|
2013-06-03 20:58:09 +04:00
|
|
|
[](LineCount l, const Selection& s) { return l < std::min(s.first(), s.last()).line; });
|
2013-05-02 21:04:59 +04:00
|
|
|
|
|
|
|
if (update_beg != update_only_line_beg)
|
|
|
|
{
|
|
|
|
// for the first one, we are not sure if min < begin
|
2013-06-01 02:48:46 +04:00
|
|
|
UpdateFunc<false, false>{}(buffer, update_beg->first(), begin, end);
|
|
|
|
UpdateFunc<false, false>{}(buffer, update_beg->last(), begin, end);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
for (auto it = update_beg+1; it < update_only_line_beg; ++it)
|
|
|
|
{
|
2013-06-01 02:48:46 +04:00
|
|
|
UpdateFunc<false, true>{}(buffer, it->first(), begin, end);
|
|
|
|
UpdateFunc<false, true>{}(buffer, it->last(), begin, end);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
if (end.line > begin.line)
|
|
|
|
{
|
|
|
|
for (auto it = update_only_line_beg; it != sels.end(); ++it)
|
|
|
|
{
|
2013-06-01 02:48:46 +04:00
|
|
|
UpdateFunc<true, true>{}(buffer, it->first(), begin, end);
|
|
|
|
UpdateFunc<true, true>{}(buffer, it->last(), begin, end);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateInsert
|
|
|
|
{
|
2013-06-03 20:58:09 +04:00
|
|
|
void operator()(const Buffer& buffer, BufferCoord& coord,
|
2013-07-26 02:44:00 +04:00
|
|
|
BufferCoord begin, BufferCoord end) const
|
2013-05-02 21:04:59 +04: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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateErase
|
|
|
|
{
|
2013-06-03 20:58:09 +04:00
|
|
|
void operator()(const Buffer& buffer, BufferCoord& coord,
|
2013-07-26 02:44:00 +04:00
|
|
|
BufferCoord begin, BufferCoord end) const
|
2013-05-02 21:04:59 +04: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)
|
2013-06-01 02:48:46 +04:00
|
|
|
coord = buffer.clamp(begin);
|
2013-05-02 21:04:59 +04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-26 02:44:00 +04:00
|
|
|
void SelectionList::update_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end)
|
2013-05-02 21:04:59 +04:00
|
|
|
{
|
2013-06-01 02:48:46 +04:00
|
|
|
on_buffer_change<UpdateInsert>(buffer, *this, begin, end, begin.line);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
|
2013-07-26 02:44:00 +04:00
|
|
|
void SelectionList::update_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end)
|
2013-05-02 21:04:59 +04:00
|
|
|
{
|
2013-06-01 02:48:46 +04:00
|
|
|
on_buffer_change<UpdateErase>(buffer, *this, begin, end, end.line);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
|
2013-05-03 15:38:48 +04:00
|
|
|
void SelectionList::check_invariant() const
|
|
|
|
{
|
2013-05-27 16:22:45 +04:00
|
|
|
for (size_t i = 0; i+1 < size(); ++ i)
|
|
|
|
kak_assert((*this)[i].min() <= (*this)[i+1].min());
|
2013-05-03 15:38:48 +04:00
|
|
|
}
|
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|