2012-01-31 23:12:06 +04:00
|
|
|
#include "selection.hh"
|
|
|
|
|
2012-10-08 16:26:57 +04:00
|
|
|
#include "utf8.hh"
|
2014-05-20 23:33:38 +04:00
|
|
|
#include "modification.hh"
|
2014-05-25 23:28:32 +04:00
|
|
|
#include "buffer_utils.hh"
|
2012-10-08 16:26:57 +04:00
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-03-29 12:55:45 +04:00
|
|
|
void Selection::merge_with(const Selection& range)
|
2012-11-30 21:32:49 +04:00
|
|
|
{
|
2014-01-28 23:05:49 +04: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 21:32:49 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 21:04:59 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
template<template <bool, bool> class UpdateFunc>
|
2014-05-14 02:22:54 +04:00
|
|
|
void on_buffer_change(std::vector<Selection>& sels,
|
2014-05-12 16:59:21 +04:00
|
|
|
ByteCoord begin, ByteCoord end, bool at_end, LineCount end_line)
|
2013-05-02 21:04:59 +04:00
|
|
|
{
|
|
|
|
auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin,
|
2014-05-07 22:51:01 +04:00
|
|
|
[](const Selection& s, ByteCoord c)
|
2014-04-08 23:09:54 +04:00
|
|
|
{ return s.max() < c; });
|
2013-05-02 21:04:59 +04:00
|
|
|
auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line,
|
2014-04-08 23:09:54 +04:00
|
|
|
[](LineCount l, const Selection& s)
|
|
|
|
{ return l < s.min().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
|
2014-05-12 16:59:21 +04:00
|
|
|
UpdateFunc<false, false>{}(update_beg->anchor(), begin, end, at_end);
|
|
|
|
UpdateFunc<false, false>{}(update_beg->cursor(), begin, end, at_end);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
for (auto it = update_beg+1; it < update_only_line_beg; ++it)
|
|
|
|
{
|
2014-05-12 16:59:21 +04:00
|
|
|
UpdateFunc<false, true>{}(it->anchor(), begin, end, at_end);
|
|
|
|
UpdateFunc<false, true>{}(it->cursor(), begin, end, at_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)
|
|
|
|
{
|
2014-05-12 16:59:21 +04:00
|
|
|
UpdateFunc<true, true>{}(it->anchor(), begin, end, at_end);
|
|
|
|
UpdateFunc<true, true>{}(it->cursor(), begin, end, at_end);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateInsert
|
|
|
|
{
|
2014-05-12 16:59:21 +04:00
|
|
|
void operator()(ByteCoord& coord, ByteCoord begin, ByteCoord end,
|
|
|
|
bool at_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;
|
2014-05-13 02:25:15 +04:00
|
|
|
kak_assert(coord.line >= 0 and coord.column >= 0);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateErase
|
|
|
|
{
|
2014-05-12 16:59:21 +04:00
|
|
|
void operator()(ByteCoord& coord, ByteCoord begin, ByteCoord end,
|
|
|
|
bool at_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)
|
2014-05-12 16:59:21 +04:00
|
|
|
{
|
2014-05-13 02:25:15 +04:00
|
|
|
if (not at_end or begin == ByteCoord{0,0})
|
2014-05-12 16:59:21 +04:00
|
|
|
coord = begin;
|
|
|
|
else
|
|
|
|
coord = begin.column ? ByteCoord{begin.line, begin.column-1}
|
|
|
|
: ByteCoord{begin.line - 1};
|
|
|
|
}
|
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;
|
|
|
|
}
|
2014-05-13 02:25:15 +04:00
|
|
|
kak_assert(coord.line >= 0 and coord.column >= 0);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Selection s, size_t timestamp)
|
2014-05-13 02:25:15 +04:00
|
|
|
: m_buffer(&buffer), m_selections({ s }), m_timestamp(timestamp)
|
2014-05-14 02:22:54 +04:00
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
}
|
2014-05-13 02:25:15 +04:00
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Selection s)
|
2014-05-13 02:25:15 +04:00
|
|
|
: SelectionList(buffer, s, buffer.timestamp())
|
|
|
|
{}
|
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, std::vector<Selection> s, size_t timestamp)
|
2014-05-13 02:25:15 +04:00
|
|
|
: m_buffer(&buffer), m_selections(std::move(s)), m_timestamp(timestamp)
|
2014-05-14 02:22:54 +04:00
|
|
|
{
|
|
|
|
kak_assert(size() > 0);
|
|
|
|
check_invariant();
|
|
|
|
}
|
2014-05-13 02:25:15 +04:00
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, std::vector<Selection> s)
|
2014-05-13 02:25:15 +04:00
|
|
|
: SelectionList(buffer, std::move(s), buffer.timestamp())
|
|
|
|
{}
|
|
|
|
|
2014-05-14 02:22:54 +04:00
|
|
|
void update_insert(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end, bool at_end)
|
2013-05-02 21:04:59 +04:00
|
|
|
{
|
2014-05-14 02:22:54 +04:00
|
|
|
on_buffer_change<UpdateInsert>(sels, begin, end, at_end, begin.line);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
|
2014-05-14 02:22:54 +04:00
|
|
|
void update_erase(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end, bool at_end)
|
2013-05-02 21:04:59 +04:00
|
|
|
{
|
2014-05-14 02:22:54 +04:00
|
|
|
on_buffer_change<UpdateErase>(sels, begin, end, at_end, end.line);
|
2013-05-02 21:04:59 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 02:25:15 +04:00
|
|
|
void SelectionList::update()
|
|
|
|
{
|
2014-05-14 03:59:36 +04:00
|
|
|
if (m_timestamp == m_buffer->timestamp())
|
|
|
|
return;
|
|
|
|
|
2014-05-20 23:33:38 +04:00
|
|
|
auto modifs = compute_modifications(*m_buffer, m_timestamp);
|
|
|
|
for (auto& sel : m_selections)
|
2014-05-13 02:25:15 +04:00
|
|
|
{
|
2014-05-20 23:33:38 +04:00
|
|
|
auto anchor = update_pos(modifs, sel.anchor());
|
|
|
|
kak_assert(m_buffer->is_valid(anchor));
|
2014-05-22 12:52:52 +04:00
|
|
|
sel.anchor() = m_buffer->clamp(anchor);
|
2014-05-20 23:33:38 +04:00
|
|
|
|
|
|
|
auto cursor = update_pos(modifs, sel.cursor());
|
|
|
|
kak_assert(m_buffer->is_valid(cursor));
|
2014-05-22 12:52:52 +04:00
|
|
|
sel.cursor() = m_buffer->clamp(cursor);
|
2014-05-13 02:25:15 +04:00
|
|
|
}
|
|
|
|
|
2014-05-22 12:52:22 +04:00
|
|
|
merge_overlapping(overlaps);
|
2014-05-13 02:25:15 +04:00
|
|
|
check_invariant();
|
2014-05-14 03:59:36 +04:00
|
|
|
|
|
|
|
m_timestamp = m_buffer->timestamp();
|
2014-05-13 02:25:15 +04:00
|
|
|
}
|
|
|
|
|
2013-05-03 15:38:48 +04:00
|
|
|
void SelectionList::check_invariant() const
|
|
|
|
{
|
2014-05-13 02:25:15 +04:00
|
|
|
#ifdef KAK_DEBUG
|
|
|
|
auto& buffer = this->buffer();
|
2013-12-14 18:11:14 +04:00
|
|
|
kak_assert(size() > 0);
|
2013-12-13 03:17:06 +04:00
|
|
|
kak_assert(m_main < size());
|
2014-05-13 02:25:15 +04:00
|
|
|
for (size_t i = 0; i < size(); ++i)
|
|
|
|
{
|
|
|
|
auto& sel = (*this)[i];
|
2014-05-14 03:59:36 +04:00
|
|
|
if (i+1 < size())
|
|
|
|
kak_assert((*this)[i].min() <= (*this)[i+1].min());
|
2014-05-13 02:25:15 +04: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 22:21:19 +04: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 02:25:15 +04: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 15:38:48 +04:00
|
|
|
}
|
2014-05-14 23:56:27 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
inline void _avoid_eol(const Buffer& buffer, ByteCoord& coord)
|
|
|
|
{
|
|
|
|
const auto column = coord.column;
|
|
|
|
const auto& line = buffer[coord.line];
|
|
|
|
if (column != 0 and column == line.length() - 1)
|
|
|
|
coord.column = line.byte_count_to(line.char_length() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void _avoid_eol(const Buffer& buffer, Selection& sel)
|
|
|
|
{
|
|
|
|
_avoid_eol(buffer, sel.anchor());
|
|
|
|
_avoid_eol(buffer, sel.cursor());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionList::avoid_eol()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
_avoid_eol(buffer(), sel);
|
|
|
|
}
|
2013-05-03 15:38:48 +04:00
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
void SelectionList::erase()
|
|
|
|
{
|
|
|
|
for (auto& sel : reversed(m_selections))
|
|
|
|
{
|
|
|
|
Kakoune::erase(*m_buffer, sel);
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
avoid_eol();
|
|
|
|
m_buffer->check_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferIterator prepare_insert(Buffer& buffer, const Selection& sel, InsertMode mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case InsertMode::Insert:
|
|
|
|
return buffer.iterator_at(sel.min());
|
|
|
|
case InsertMode::Replace:
|
|
|
|
return erase(buffer, sel);
|
|
|
|
case InsertMode::Append:
|
|
|
|
{
|
|
|
|
// special case for end of lines, append to current line instead
|
|
|
|
auto pos = buffer.iterator_at(sel.max());
|
|
|
|
return *pos == '\n' ? pos : utf8::next(pos);
|
|
|
|
}
|
|
|
|
case InsertMode::InsertAtLineBegin:
|
|
|
|
return buffer.iterator_at(sel.min().line);
|
|
|
|
case InsertMode::AppendAtLineEnd:
|
|
|
|
return buffer.iterator_at({sel.max().line, buffer[sel.max().line].length() - 1});
|
|
|
|
case InsertMode::InsertAtNextLineBegin:
|
|
|
|
return buffer.iterator_at(sel.max().line+1);
|
|
|
|
case InsertMode::OpenLineBelow:
|
|
|
|
return buffer.insert(buffer.iterator_at(sel.max().line + 1), "\n");
|
|
|
|
case InsertMode::OpenLineAbove:
|
|
|
|
return buffer.insert(buffer.iterator_at(sel.min().line), "\n");
|
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionList::insert(memoryview<String> strings, InsertMode mode)
|
|
|
|
{
|
|
|
|
if (strings.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
update();
|
|
|
|
for (size_t i = 0; i < m_selections.size(); ++i)
|
|
|
|
{
|
|
|
|
size_t index = m_selections.size() - 1 - i;
|
|
|
|
auto& sel = m_selections[index];
|
|
|
|
auto pos = prepare_insert(*m_buffer, sel, mode);
|
|
|
|
const String& str = strings[std::min(index, strings.size()-1)];
|
|
|
|
pos = m_buffer->insert(pos, str);
|
|
|
|
if (mode == InsertMode::Replace)
|
|
|
|
{
|
|
|
|
if (pos == m_buffer->end())
|
|
|
|
--pos;
|
|
|
|
sel.anchor() = pos.coord();
|
|
|
|
sel.cursor() = (str.empty() ?
|
|
|
|
pos : pos + str.byte_count_to(str.char_length() - 1)).coord();
|
|
|
|
|
|
|
|
// update following selections
|
|
|
|
auto changes = compute_modifications(*m_buffer, timestamp());
|
|
|
|
for (size_t j = index+1; j < m_selections.size(); ++j)
|
|
|
|
{
|
|
|
|
auto& sel = m_selections[j];
|
|
|
|
sel.anchor() = update_pos(changes, sel.anchor());
|
|
|
|
sel.cursor() = update_pos(changes, sel.cursor());
|
|
|
|
}
|
|
|
|
update_timestamp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
avoid_eol();
|
|
|
|
check_invariant();
|
|
|
|
m_buffer->check_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|