2012-01-31 20:12:06 +01:00
|
|
|
#include "selection.hh"
|
|
|
|
|
2012-10-08 14:26:57 +02:00
|
|
|
#include "utf8.hh"
|
2014-05-25 21:28:32 +02:00
|
|
|
#include "buffer_utils.hh"
|
2012-10-08 14:26:57 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Selection s, size_t timestamp)
|
2014-05-13 00:25:15 +02:00
|
|
|
: 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
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Selection s)
|
2014-05-13 00:25:15 +02:00
|
|
|
: SelectionList(buffer, s, buffer.timestamp())
|
|
|
|
{}
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, std::vector<Selection> s, size_t timestamp)
|
2014-05-13 00:25:15 +02:00
|
|
|
: 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
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, std::vector<Selection> s)
|
2014-05-13 00:25:15 +02:00
|
|
|
: SelectionList(buffer, std::move(s), buffer.timestamp())
|
|
|
|
{}
|
|
|
|
|
2014-05-26 21:57:10 +02:00
|
|
|
namespace
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
2014-05-26 21:57:10 +02:00
|
|
|
|
|
|
|
ByteCoord update_insert(ByteCoord coord, ByteCoord begin, ByteCoord end)
|
|
|
|
{
|
|
|
|
if (coord < begin)
|
|
|
|
return coord;
|
|
|
|
if (begin.line == coord.line)
|
|
|
|
coord.column += end.column - begin.column;
|
|
|
|
coord.line += end.line - begin.line;
|
|
|
|
kak_assert(coord.line >= 0 and coord.column >= 0);
|
|
|
|
return coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteCoord update_erase(ByteCoord coord, ByteCoord begin, ByteCoord end)
|
|
|
|
{
|
|
|
|
if (coord < begin)
|
|
|
|
return coord;
|
|
|
|
if (coord <= end)
|
|
|
|
return begin;
|
|
|
|
if (end.line == coord.line)
|
|
|
|
coord.column -= end.column - begin.column;
|
|
|
|
coord.line -= end.line - begin.line;
|
|
|
|
kak_assert(coord.line >= 0 and coord.column >= 0);
|
|
|
|
return coord;
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
|
2014-05-26 21:57:10 +02:00
|
|
|
ByteCoord update_pos(ByteCoord coord, const Buffer::Change& change)
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
2014-05-26 21:57:10 +02:00
|
|
|
if (change.type == Buffer::Change::Insert)
|
|
|
|
return update_insert(coord, change.begin, change.end);
|
2014-05-29 06:48:40 +02:00
|
|
|
else
|
2014-05-26 21:57:10 +02:00
|
|
|
return update_erase(coord, change.begin, change.end);
|
|
|
|
}
|
|
|
|
|
2014-05-29 06:48:40 +02:00
|
|
|
// This tracks position changes for changes that are done
|
|
|
|
// in a forward way (each change takes place at a position)
|
|
|
|
// *after* the previous one.
|
|
|
|
struct PositionChangesTracker
|
|
|
|
{
|
|
|
|
ByteCoord last_pos;
|
|
|
|
ByteCoord pos_change;
|
|
|
|
|
|
|
|
void update(const Buffer::Change& change)
|
|
|
|
{
|
|
|
|
if (change.type == Buffer::Change::Insert)
|
|
|
|
{
|
|
|
|
pos_change.line += change.end.line - change.begin.line;
|
|
|
|
if (change.begin.line != last_pos.line)
|
|
|
|
pos_change.column = 0;
|
|
|
|
pos_change.column += change.end.column - change.begin.column;
|
|
|
|
last_pos = change.end;
|
|
|
|
}
|
|
|
|
else if (change.type == Buffer::Change::Erase)
|
|
|
|
{
|
|
|
|
pos_change.line -= change.end.line - change.begin.line;
|
|
|
|
if (last_pos.line != change.end.line)
|
|
|
|
pos_change.column = 0;
|
|
|
|
pos_change.column -= change.end.column - change.begin.column;
|
|
|
|
last_pos = change.begin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(const Buffer& buffer, size_t& timestamp)
|
|
|
|
{
|
|
|
|
for (auto& change : buffer.changes_since(timestamp))
|
|
|
|
update(change);
|
|
|
|
timestamp = buffer.timestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteCoord get_new_coord(ByteCoord coord)
|
|
|
|
{
|
|
|
|
if (last_pos.line - pos_change.line == coord.line)
|
|
|
|
coord.column += pos_change.column;
|
|
|
|
coord.line += pos_change.line;
|
|
|
|
return coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_sel(Selection& sel)
|
|
|
|
{
|
|
|
|
sel.anchor() = get_new_coord(sel.anchor());
|
|
|
|
sel.cursor() = get_new_coord(sel.cursor());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-29 06:48:40 +02:00
|
|
|
auto compare = [](const Buffer::Change& lhs, const Buffer::Change& rhs)
|
|
|
|
{ return lhs.begin < rhs.begin; };
|
|
|
|
auto relevant = [](const Buffer::Change& change, const ByteCoord& coord)
|
|
|
|
{ return change.type == Buffer::Change::Insert ? change.begin <= coord
|
|
|
|
: change.begin < coord; };
|
|
|
|
|
|
|
|
auto changes = m_buffer->changes_since(m_timestamp);
|
|
|
|
auto change_it = changes.begin();
|
|
|
|
while (change_it != changes.end())
|
2014-05-26 21:57:10 +02:00
|
|
|
{
|
2014-05-29 06:48:40 +02:00
|
|
|
auto change_end = std::is_sorted_until(change_it, changes.end(), compare);
|
|
|
|
PositionChangesTracker changes_tracker;
|
|
|
|
|
|
|
|
auto advance_while_relevant = [&](const ByteCoord& pos) mutable {
|
|
|
|
while (relevant(*change_it, pos) and change_it != change_end)
|
|
|
|
changes_tracker.update(*change_it++);
|
|
|
|
while (change_it != change_end and
|
|
|
|
change_it->begin == changes_tracker.last_pos)
|
|
|
|
changes_tracker.update(*change_it++);
|
|
|
|
};
|
|
|
|
|
2014-05-26 21:57:10 +02:00
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
2014-05-29 06:48:40 +02:00
|
|
|
auto& sel_min = sel.min();
|
|
|
|
auto& sel_max = sel.max();
|
|
|
|
advance_while_relevant(sel_min);
|
|
|
|
sel_min = changes_tracker.get_new_coord(sel_min);
|
|
|
|
|
|
|
|
advance_while_relevant(sel_max);
|
|
|
|
sel_max = changes_tracker.get_new_coord(sel_max);
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
2014-05-29 06:48:40 +02:00
|
|
|
change_it = change_end;
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
2014-05-20 21:33:38 +02:00
|
|
|
for (auto& sel : m_selections)
|
2014-05-13 00:25:15 +02:00
|
|
|
{
|
2014-05-26 21:57:10 +02:00
|
|
|
sel.anchor() = m_buffer->clamp(sel.anchor());
|
|
|
|
sel.cursor() = m_buffer->clamp(sel.cursor());
|
2014-05-13 00:25:15 +02:00
|
|
|
}
|
2014-05-22 10:52:22 +02:00
|
|
|
merge_overlapping(overlaps);
|
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
|
|
|
}
|
2014-05-14 21:56:27 +02: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 13:38:48 +02:00
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
BufferIterator prepare_insert(Buffer& buffer, const Selection& sel, InsertMode mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case InsertMode::Insert:
|
|
|
|
return buffer.iterator_at(sel.min());
|
2014-05-25 23:59:29 +02:00
|
|
|
case InsertMode::InsertCursor:
|
|
|
|
return buffer.iterator_at(sel.cursor());
|
2014-05-25 21:28:32 +02:00
|
|
|
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();
|
2014-05-26 21:57:10 +02:00
|
|
|
PositionChangesTracker changes_tracker;
|
|
|
|
for (size_t index = 0; index < m_selections.size(); ++index)
|
2014-05-25 21:28:32 +02:00
|
|
|
{
|
|
|
|
auto& sel = m_selections[index];
|
2014-05-26 21:57:10 +02:00
|
|
|
|
|
|
|
changes_tracker.update_sel(sel);
|
|
|
|
kak_assert(m_buffer->is_valid(sel.anchor()));
|
|
|
|
kak_assert(m_buffer->is_valid(sel.cursor()));
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
auto pos = prepare_insert(*m_buffer, sel, mode);
|
2014-05-26 21:57:10 +02:00
|
|
|
changes_tracker.update(*m_buffer, m_timestamp);
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
const String& str = strings[std::min(index, strings.size()-1)];
|
|
|
|
pos = m_buffer->insert(pos, str);
|
2014-05-26 21:57:10 +02:00
|
|
|
|
|
|
|
auto& change = m_buffer->changes_since(m_timestamp).back();
|
|
|
|
changes_tracker.update(change);
|
|
|
|
m_timestamp = m_buffer->timestamp();
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
if (mode == InsertMode::Replace)
|
|
|
|
{
|
2014-05-26 21:57:10 +02:00
|
|
|
sel.anchor() = change.begin;
|
|
|
|
sel.cursor() = m_buffer->char_prev(change.end);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sel.anchor() = m_buffer->clamp(update_insert(sel.anchor(), change.begin, change.end));
|
|
|
|
sel.cursor() = m_buffer->clamp(update_insert(sel.cursor(), change.begin, change.end));
|
2014-05-25 21:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
check_invariant();
|
|
|
|
m_buffer->check_invariant();
|
|
|
|
}
|
|
|
|
|
2014-05-26 21:57:10 +02:00
|
|
|
void SelectionList::erase()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
PositionChangesTracker changes_tracker;
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
|
|
|
changes_tracker.update_sel(sel);
|
|
|
|
auto pos = Kakoune::erase(*m_buffer, sel);
|
|
|
|
sel.anchor() = sel.cursor() = m_buffer->clamp(pos.coord());
|
|
|
|
changes_tracker.update(*m_buffer, m_timestamp);
|
|
|
|
}
|
|
|
|
m_buffer->check_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_insert(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end)
|
|
|
|
{
|
|
|
|
for (auto& sel : sels)
|
|
|
|
{
|
|
|
|
sel.anchor() = update_insert(sel.anchor(), begin, end);
|
|
|
|
sel.cursor() = update_insert(sel.cursor(), begin, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_erase(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end)
|
|
|
|
{
|
|
|
|
for (auto& sel : sels)
|
|
|
|
{
|
|
|
|
sel.anchor() = update_erase(sel.anchor(), begin, end);
|
|
|
|
sel.cursor() = update_erase(sel.cursor(), begin, end);
|
|
|
|
}
|
|
|
|
}
|
2014-05-25 21:28:32 +02:00
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|