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-09-22 14:55:39 +02:00
|
|
|
: m_buffer(&buffer), m_selections({ std::move(s) }), m_timestamp(timestamp)
|
2015-08-03 12:23:40 +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)
|
2015-11-04 20:53:47 +01:00
|
|
|
: SelectionList(buffer, std::move(s), buffer.timestamp()) {}
|
2014-05-13 00:25:15 +02:00
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, 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);
|
2015-11-04 20:53:47 +01:00
|
|
|
m_main = size() - 1;
|
2015-08-03 12:23:40 +02:00
|
|
|
check_invariant();
|
2014-05-14 00:22:54 +02:00
|
|
|
}
|
2014-05-13 00:25:15 +02:00
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Vector<Selection> s)
|
2015-11-04 20:53:47 +01:00
|
|
|
: SelectionList(buffer, std::move(s), buffer.timestamp()) {}
|
2014-05-13 00:25:15 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-21 14:50:49 +02:00
|
|
|
/* For reference
|
2014-05-26 21:57:10 +02:00
|
|
|
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;
|
2015-04-21 14:50:49 +02:00
|
|
|
} */
|
2013-05-02 19:04:59 +02:00
|
|
|
|
2015-04-21 14:50:49 +02:00
|
|
|
bool compare_selections(const Selection& lhs, const Selection& rhs)
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
2014-06-02 16:13:56 +02:00
|
|
|
return lhs.min() < rhs.min();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Iterator, typename OverlapsFunc>
|
|
|
|
Iterator merge_overlapping(Iterator begin, Iterator end, size_t& main, OverlapsFunc overlaps)
|
|
|
|
{
|
2014-06-02 18:36:46 +02:00
|
|
|
if (begin == end)
|
|
|
|
return begin;
|
|
|
|
|
2014-06-02 16:13:56 +02:00
|
|
|
kak_assert(std::is_sorted(begin, end, compare_selections));
|
|
|
|
size_t size = end - begin;
|
|
|
|
size_t i = 0;
|
|
|
|
for (size_t j = 1; j < size; ++j)
|
|
|
|
{
|
|
|
|
if (overlaps(begin[i], begin[j]))
|
|
|
|
{
|
|
|
|
begin[i].merge_with(begin[j]);
|
|
|
|
if (i < main)
|
|
|
|
--main;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if (i != j)
|
|
|
|
begin[i] = std::move(begin[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return begin + i + 1;
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2014-06-05 00:23:37 +02:00
|
|
|
struct ForwardChangesTracker
|
2014-05-29 06:48:40 +02:00
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
ByteCoord cur_pos; // last change position at current modification
|
|
|
|
ByteCoord old_pos; // last change position at start
|
2014-05-29 06:48:40 +02:00
|
|
|
|
|
|
|
void update(const Buffer::Change& change)
|
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
kak_assert(change.begin >= cur_pos);
|
|
|
|
|
2014-05-29 06:48:40 +02:00
|
|
|
if (change.type == Buffer::Change::Insert)
|
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
old_pos = get_old_coord(change.begin);
|
|
|
|
cur_pos = change.end;
|
2014-05-29 06:48:40 +02:00
|
|
|
}
|
|
|
|
else if (change.type == Buffer::Change::Erase)
|
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
old_pos = get_old_coord(change.end);
|
|
|
|
cur_pos = change.begin;
|
2014-05-29 06:48:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(const Buffer& buffer, size_t& timestamp)
|
|
|
|
{
|
|
|
|
for (auto& change : buffer.changes_since(timestamp))
|
|
|
|
update(change);
|
|
|
|
timestamp = buffer.timestamp();
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:23:37 +02:00
|
|
|
ByteCoord get_old_coord(ByteCoord coord) const
|
|
|
|
{
|
|
|
|
kak_assert(cur_pos <= coord);
|
|
|
|
auto pos_change = cur_pos - old_pos;
|
|
|
|
if (cur_pos.line == coord.line)
|
|
|
|
{
|
|
|
|
kak_assert(pos_change.column <= coord.column);
|
|
|
|
coord.column -= pos_change.column;
|
|
|
|
}
|
|
|
|
coord.line -= pos_change.line;
|
|
|
|
kak_assert(old_pos <= coord);
|
|
|
|
return coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteCoord get_new_coord(ByteCoord coord) const
|
2014-05-29 06:48:40 +02:00
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
kak_assert(old_pos <= coord);
|
|
|
|
auto pos_change = cur_pos - old_pos;
|
|
|
|
if (old_pos.line == coord.line)
|
|
|
|
{
|
|
|
|
kak_assert(-pos_change.column <= coord.column);
|
|
|
|
coord.column += pos_change.column;
|
|
|
|
}
|
2014-05-29 06:48:40 +02:00
|
|
|
coord.line += pos_change.line;
|
2014-06-05 00:23:37 +02:00
|
|
|
kak_assert(cur_pos <= coord);
|
2014-05-29 06:48:40 +02:00
|
|
|
return coord;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:23:37 +02:00
|
|
|
ByteCoord get_new_coord_tolerant(ByteCoord coord) const
|
2014-05-29 06:48:40 +02:00
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
if (coord < old_pos)
|
|
|
|
return cur_pos;
|
|
|
|
return get_new_coord(coord);
|
2014-05-29 06:48:40 +02:00
|
|
|
}
|
|
|
|
|
2014-06-05 00:23:37 +02:00
|
|
|
bool relevant(const Buffer::Change& change, ByteCoord old_coord) const
|
|
|
|
{
|
2014-06-12 22:23:49 +02:00
|
|
|
auto new_coord = get_new_coord_tolerant(old_coord);
|
2014-06-05 00:23:37 +02:00
|
|
|
return change.type == Buffer::Change::Insert ? change.begin <= new_coord
|
|
|
|
: change.begin < new_coord;
|
|
|
|
}
|
|
|
|
};
|
2014-06-02 03:16:19 +02:00
|
|
|
|
2014-06-02 18:36:46 +02:00
|
|
|
const Buffer::Change* forward_sorted_until(const Buffer::Change* first, const Buffer::Change* last)
|
2014-06-02 16:13:56 +02:00
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
if (first != last) {
|
|
|
|
const Buffer::Change* next = first;
|
|
|
|
while (++next != last) {
|
|
|
|
const auto& ref = first->type == Buffer::Change::Insert ? first->end : first->begin;
|
|
|
|
if (next->begin <= ref)
|
|
|
|
return next;
|
|
|
|
first = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last;
|
2014-06-02 16:13:56 +02:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:36:46 +02:00
|
|
|
const Buffer::Change* backward_sorted_until(const Buffer::Change* first, const Buffer::Change* last)
|
2014-06-02 16:13:56 +02:00
|
|
|
{
|
2014-06-02 18:36:46 +02:00
|
|
|
if (first != last) {
|
|
|
|
const Buffer::Change* next = first;
|
|
|
|
while (++next != last) {
|
2014-06-05 00:23:37 +02:00
|
|
|
if (first->begin <= next->end)
|
2014-06-02 18:36:46 +02:00
|
|
|
return next;
|
|
|
|
first = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last;
|
2014-06-02 16:13:56 +02:00
|
|
|
}
|
|
|
|
|
2015-12-17 05:56:44 +01:00
|
|
|
void update_forward(ConstArrayView<Buffer::Change> changes, Vector<Selection>& selections)
|
2014-06-02 03:16:19 +02:00
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
ForwardChangesTracker changes_tracker;
|
2014-06-02 03:16:19 +02:00
|
|
|
|
|
|
|
auto change_it = changes.begin();
|
|
|
|
auto advance_while_relevant = [&](const ByteCoord& pos) mutable {
|
2014-06-05 00:23:37 +02:00
|
|
|
while (change_it != changes.end() and changes_tracker.relevant(*change_it, pos))
|
2014-06-02 03:16:19 +02:00
|
|
|
changes_tracker.update(*change_it++);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& sel : selections)
|
|
|
|
{
|
|
|
|
auto& sel_min = sel.min();
|
|
|
|
auto& sel_max = sel.max();
|
|
|
|
advance_while_relevant(sel_min);
|
2014-06-05 00:23:37 +02:00
|
|
|
sel_min = changes_tracker.get_new_coord_tolerant(sel_min);
|
2014-06-02 03:16:19 +02:00
|
|
|
|
|
|
|
advance_while_relevant(sel_max);
|
2014-06-05 00:23:37 +02:00
|
|
|
sel_max = changes_tracker.get_new_coord_tolerant(sel_max);
|
2014-06-02 03:16:19 +02:00
|
|
|
}
|
2014-06-02 18:36:46 +02:00
|
|
|
kak_assert(std::is_sorted(selections.begin(), selections.end(), compare_selections));
|
2014-06-02 03:16:19 +02:00
|
|
|
}
|
|
|
|
|
2015-12-17 05:56:44 +01:00
|
|
|
void update_backward(ConstArrayView<Buffer::Change> changes, Vector<Selection>& selections)
|
2014-06-02 03:16:19 +02:00
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
ForwardChangesTracker changes_tracker;
|
2014-06-02 03:16:19 +02:00
|
|
|
|
|
|
|
using ReverseIt = std::reverse_iterator<const Buffer::Change*>;
|
|
|
|
auto change_it = ReverseIt(changes.end());
|
|
|
|
auto change_end = ReverseIt(changes.begin());
|
|
|
|
auto advance_while_relevant = [&](const ByteCoord& pos) mutable {
|
|
|
|
while (change_it != change_end)
|
|
|
|
{
|
|
|
|
auto change = *change_it;
|
|
|
|
change.begin = changes_tracker.get_new_coord(change.begin);
|
|
|
|
change.end = changes_tracker.get_new_coord(change.end);
|
2014-06-05 00:23:37 +02:00
|
|
|
if (not changes_tracker.relevant(change, pos))
|
2014-06-02 03:16:19 +02:00
|
|
|
break;
|
|
|
|
changes_tracker.update(change);
|
|
|
|
++change_it;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& sel : selections)
|
|
|
|
{
|
|
|
|
auto& sel_min = sel.min();
|
|
|
|
auto& sel_max = sel.max();
|
|
|
|
advance_while_relevant(sel_min);
|
2014-06-05 00:23:37 +02:00
|
|
|
sel_min = changes_tracker.get_new_coord_tolerant(sel_min);
|
2014-06-02 03:16:19 +02:00
|
|
|
|
|
|
|
advance_while_relevant(sel_max);
|
2014-06-05 00:23:37 +02:00
|
|
|
sel_max = changes_tracker.get_new_coord_tolerant(sel_max);
|
2014-06-02 03:16:19 +02:00
|
|
|
}
|
2014-06-02 18:36:46 +02:00
|
|
|
kak_assert(std::is_sorted(selections.begin(), selections.end(), compare_selections));
|
2014-06-02 03:16:19 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 19:04:59 +02:00
|
|
|
}
|
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<Selection> compute_modified_ranges(Buffer& buffer, size_t timestamp)
|
2014-06-02 16:13:56 +02:00
|
|
|
{
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<Selection> ranges;
|
2014-06-02 16:13:56 +02:00
|
|
|
auto changes = buffer.changes_since(timestamp);
|
|
|
|
auto change_it = changes.begin();
|
|
|
|
while (change_it != changes.end())
|
|
|
|
{
|
2014-06-02 18:36:46 +02:00
|
|
|
auto forward_end = forward_sorted_until(change_it, changes.end());
|
|
|
|
auto backward_end = backward_sorted_until(change_it, changes.end());
|
2014-06-02 16:13:56 +02:00
|
|
|
|
2014-06-02 18:36:46 +02:00
|
|
|
size_t prev_size;
|
|
|
|
size_t dummy = 0;
|
2014-06-02 16:13:56 +02:00
|
|
|
if (forward_end >= backward_end)
|
|
|
|
{
|
2015-12-17 05:56:44 +01:00
|
|
|
update_forward({ change_it, forward_end }, ranges);
|
|
|
|
ranges.erase(merge_overlapping(ranges.begin(), ranges.end(), dummy, overlaps), ranges.end());
|
2014-06-02 18:36:46 +02:00
|
|
|
prev_size = ranges.size();
|
2014-06-02 16:13:56 +02:00
|
|
|
|
2014-06-05 00:23:37 +02:00
|
|
|
ForwardChangesTracker changes_tracker;
|
2014-06-02 16:13:56 +02:00
|
|
|
for (; change_it != forward_end; ++change_it)
|
|
|
|
{
|
|
|
|
if (change_it->type == Buffer::Change::Insert)
|
|
|
|
ranges.push_back({ change_it->begin, change_it->end });
|
|
|
|
else
|
|
|
|
ranges.push_back({ change_it->begin });
|
|
|
|
changes_tracker.update(*change_it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-17 05:56:44 +01:00
|
|
|
update_backward({ change_it, backward_end }, ranges);
|
|
|
|
ranges.erase(merge_overlapping(ranges.begin(), ranges.end(), dummy, overlaps), ranges.end());
|
2014-06-02 18:36:46 +02:00
|
|
|
prev_size = ranges.size();
|
2014-06-02 16:13:56 +02:00
|
|
|
|
|
|
|
using ReverseIt = std::reverse_iterator<const Buffer::Change*>;
|
2014-06-05 00:23:37 +02:00
|
|
|
ForwardChangesTracker changes_tracker;
|
2014-06-02 16:13:56 +02:00
|
|
|
for (ReverseIt it{backward_end}, end{change_it}; it != end; ++it)
|
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
auto change = *it;
|
|
|
|
change.begin = changes_tracker.get_new_coord(change.begin);
|
|
|
|
change.end = changes_tracker.get_new_coord(change.end);
|
|
|
|
|
|
|
|
if (change.type == Buffer::Change::Insert)
|
|
|
|
ranges.push_back({ change.begin, change.end });
|
2014-06-02 16:13:56 +02:00
|
|
|
else
|
2014-06-05 00:23:37 +02:00
|
|
|
ranges.push_back({ change.begin });
|
|
|
|
changes_tracker.update(change);
|
2014-06-02 16:13:56 +02:00
|
|
|
}
|
|
|
|
change_it = backward_end;
|
|
|
|
}
|
|
|
|
|
2016-04-13 21:48:34 +02:00
|
|
|
const auto end_coord = buffer.end_coord();
|
|
|
|
for (auto it = ranges.begin() + prev_size; it != ranges.end(); ++it)
|
|
|
|
{
|
|
|
|
it->anchor() = std::min(it->anchor(), end_coord);
|
|
|
|
it->cursor() = std::min<ByteCoord>(it->cursor(), end_coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-02 16:13:56 +02:00
|
|
|
kak_assert(std::is_sorted(ranges.begin() + prev_size, ranges.end(), compare_selections));
|
|
|
|
std::inplace_merge(ranges.begin(), ranges.begin() + prev_size, ranges.end(), compare_selections);
|
2014-06-05 00:23:37 +02:00
|
|
|
ranges.erase(merge_overlapping(ranges.begin(), ranges.end(), dummy, overlaps), ranges.end());
|
2014-06-02 16:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto touches = [&](const Selection& lhs, const Selection& rhs) {
|
|
|
|
return buffer.char_next(lhs.max()) >= rhs.min();
|
|
|
|
};
|
|
|
|
size_t dummy = 0;
|
|
|
|
ranges.erase(merge_overlapping(ranges.begin(), ranges.end(), dummy, touches), ranges.end());
|
|
|
|
|
|
|
|
for (auto& sel : ranges)
|
|
|
|
{
|
2015-06-05 14:10:30 +02:00
|
|
|
if (buffer.is_end(sel.anchor()))
|
|
|
|
sel.anchor() = buffer.back_coord();
|
|
|
|
if (buffer.is_end(sel.cursor()))
|
|
|
|
sel.cursor() = buffer.back_coord();
|
|
|
|
|
2014-06-02 16:13:56 +02:00
|
|
|
if (sel.anchor() != sel.cursor())
|
|
|
|
sel.cursor() = buffer.char_prev(sel.cursor());
|
|
|
|
}
|
|
|
|
return ranges;
|
|
|
|
}
|
|
|
|
|
2016-07-08 10:52:10 +02:00
|
|
|
void update_selections(Vector<Selection>& selections, size_t& main, Buffer& buffer, size_t timestamp)
|
2014-05-13 00:25:15 +02:00
|
|
|
{
|
2016-07-08 10:52:10 +02:00
|
|
|
if (timestamp == buffer.timestamp())
|
2014-05-14 01:59:36 +02:00
|
|
|
return;
|
|
|
|
|
2016-07-08 10:52:10 +02:00
|
|
|
auto changes = buffer.changes_since(timestamp);
|
2014-05-29 06:48:40 +02:00
|
|
|
auto change_it = changes.begin();
|
|
|
|
while (change_it != changes.end())
|
2014-05-26 21:57:10 +02:00
|
|
|
{
|
2014-06-02 18:36:46 +02:00
|
|
|
auto forward_end = forward_sorted_until(change_it, changes.end());
|
|
|
|
auto backward_end = backward_sorted_until(change_it, changes.end());
|
2014-05-29 06:48:40 +02:00
|
|
|
|
2014-06-02 03:16:19 +02:00
|
|
|
if (forward_end >= backward_end)
|
|
|
|
{
|
2016-07-08 10:52:10 +02:00
|
|
|
update_forward({ change_it, forward_end }, selections);
|
2014-06-02 03:16:19 +02:00
|
|
|
change_it = forward_end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-08 10:52:10 +02:00
|
|
|
update_backward({ change_it, backward_end }, selections);
|
2014-06-02 03:16:19 +02:00
|
|
|
change_it = backward_end;
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
2016-07-08 10:52:10 +02:00
|
|
|
selections.erase(
|
|
|
|
merge_overlapping(selections.begin(), selections.end(),
|
|
|
|
main, overlaps), selections.end());
|
|
|
|
kak_assert(std::is_sorted(selections.begin(), selections.end(),
|
2014-06-02 03:16:19 +02:00
|
|
|
compare_selections));
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
2016-07-08 10:52:10 +02:00
|
|
|
for (auto& sel : selections)
|
2014-05-13 00:25:15 +02:00
|
|
|
{
|
2016-07-08 10:52:10 +02:00
|
|
|
sel.anchor() = buffer.clamp(sel.anchor());
|
|
|
|
sel.cursor() = buffer.clamp(sel.cursor());
|
2014-05-13 00:25:15 +02:00
|
|
|
}
|
2016-07-08 10:52:10 +02:00
|
|
|
selections.erase(merge_overlapping(selections.begin(), selections.end(),
|
|
|
|
main, overlaps), selections.end());
|
|
|
|
}
|
2014-05-14 01:59:36 +02:00
|
|
|
|
2016-07-08 10:52:10 +02:00
|
|
|
void SelectionList::update()
|
|
|
|
{
|
|
|
|
update_selections(m_selections, m_main, *m_buffer, m_timestamp);
|
|
|
|
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());
|
2015-08-03 12:23:40 +02:00
|
|
|
const size_t timestamp = buffer.timestamp();
|
|
|
|
kak_assert(timestamp >= m_timestamp);
|
|
|
|
|
|
|
|
// cannot check further in that case
|
|
|
|
if (timestamp != m_timestamp)
|
|
|
|
return;
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
#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);
|
2014-06-01 16:57:12 +02:00
|
|
|
m_selections.erase(merge_overlapping(begin(), end(), m_main, overlaps), end());
|
2013-05-03 13:38:48 +02:00
|
|
|
}
|
2014-05-14 21:56:27 +02:00
|
|
|
|
2015-05-13 22:31:39 +02:00
|
|
|
static inline void _avoid_eol(const Buffer& buffer, ByteCoord& coord)
|
2014-05-14 21:56:27 +02:00
|
|
|
{
|
2014-10-03 14:39:13 +02:00
|
|
|
auto column = coord.column;
|
|
|
|
auto line = buffer[coord.line];
|
2014-05-14 21:56:27 +02:00
|
|
|
if (column != 0 and column == line.length() - 1)
|
|
|
|
coord.column = line.byte_count_to(line.char_length() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionList::avoid_eol()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
for (auto& sel : m_selections)
|
2015-05-13 22:31:39 +02:00
|
|
|
{
|
|
|
|
_avoid_eol(buffer(), sel.anchor());
|
|
|
|
_avoid_eol(buffer(), sel.cursor());
|
|
|
|
}
|
2014-05-14 21:56:27 +02:00
|
|
|
}
|
2013-05-03 13:38:48 +02:00
|
|
|
|
2016-03-16 14:59:30 +01:00
|
|
|
ByteCoord prepare_insert(Buffer& buffer, const Selection& sel, InsertMode mode)
|
2014-05-25 21:28:32 +02:00
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case InsertMode::Insert:
|
2016-03-16 14:59:30 +01:00
|
|
|
return sel.min();
|
2014-05-25 23:59:29 +02:00
|
|
|
case InsertMode::InsertCursor:
|
2016-03-16 14:59:30 +01:00
|
|
|
return sel.cursor();
|
2014-05-25 21:28:32 +02:00
|
|
|
case InsertMode::Replace:
|
2016-03-16 14:48:11 +01:00
|
|
|
return {}; // replace is handled specially, by calling Buffer::replace
|
2014-05-25 21:28:32 +02:00
|
|
|
case InsertMode::Append:
|
2014-12-19 14:45:38 +01:00
|
|
|
{
|
|
|
|
// special case for end of lines, append to current line instead
|
2016-03-16 14:59:30 +01:00
|
|
|
auto pos = sel.max();
|
|
|
|
return buffer.byte_at(pos) == '\n' ? pos : buffer.char_next(pos);
|
2014-12-19 14:45:38 +01:00
|
|
|
}
|
2014-05-25 21:28:32 +02:00
|
|
|
case InsertMode::InsertAtLineBegin:
|
2016-03-16 14:59:30 +01:00
|
|
|
return sel.min().line;
|
2014-05-25 21:28:32 +02:00
|
|
|
case InsertMode::AppendAtLineEnd:
|
2016-03-16 14:59:30 +01:00
|
|
|
return {sel.max().line, buffer[sel.max().line].length() - 1};
|
2014-05-25 21:28:32 +02:00
|
|
|
case InsertMode::InsertAtNextLineBegin:
|
2016-03-16 14:59:30 +01:00
|
|
|
return sel.max().line+1;
|
2014-05-25 21:28:32 +02:00
|
|
|
case InsertMode::OpenLineBelow:
|
2016-03-16 14:59:30 +01:00
|
|
|
return buffer.insert(sel.max().line + 1, "\n");
|
2014-05-25 21:28:32 +02:00
|
|
|
case InsertMode::OpenLineAbove:
|
2016-03-16 14:59:30 +01:00
|
|
|
return buffer.insert(sel.min().line, "\n");
|
2014-05-25 21:28:32 +02:00
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
|
2014-07-03 01:25:39 +02:00
|
|
|
bool select_inserted)
|
2014-05-25 21:28:32 +02:00
|
|
|
{
|
|
|
|
if (strings.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
update();
|
2014-06-05 00:23:37 +02:00
|
|
|
ForwardChangesTracker changes_tracker;
|
2014-05-26 21:57:10 +02:00
|
|
|
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
|
|
|
|
2014-06-05 00:23:37 +02:00
|
|
|
sel.anchor() = changes_tracker.get_new_coord(sel.anchor());
|
2014-05-26 21:57:10 +02:00
|
|
|
kak_assert(m_buffer->is_valid(sel.anchor()));
|
2014-06-05 00:23:37 +02:00
|
|
|
sel.cursor() = changes_tracker.get_new_coord(sel.cursor());
|
2014-05-26 21:57:10 +02:00
|
|
|
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)];
|
2014-06-05 20:41:31 +02:00
|
|
|
|
2016-03-16 14:48:11 +01:00
|
|
|
if (mode == InsertMode::Replace)
|
|
|
|
pos = replace(*m_buffer, sel, str);
|
|
|
|
else
|
|
|
|
pos = m_buffer->insert(pos, str);
|
2014-05-26 21:57:10 +02:00
|
|
|
|
|
|
|
auto& change = m_buffer->changes_since(m_timestamp).back();
|
2016-03-16 14:48:11 +01:00
|
|
|
changes_tracker.update(*m_buffer, m_timestamp);
|
2014-05-26 21:57:10 +02:00
|
|
|
m_timestamp = m_buffer->timestamp();
|
|
|
|
|
2014-07-03 01:25:39 +02:00
|
|
|
if (select_inserted or mode == InsertMode::Replace)
|
2014-05-25 21:28:32 +02:00
|
|
|
{
|
2016-03-16 14:48:11 +01:00
|
|
|
if (str.empty())
|
|
|
|
{
|
2016-03-16 14:59:30 +01:00
|
|
|
sel.anchor() = sel.cursor() = m_buffer->clamp(pos);
|
2016-03-16 14:48:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-09 22:09:17 +01:00
|
|
|
// we want min and max from *before* we do any change
|
|
|
|
auto& min = sel.min();
|
|
|
|
auto& max = sel.max();
|
|
|
|
min = change.begin;
|
|
|
|
max = m_buffer->char_prev(change.end);
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-16 14:48:11 +01:00
|
|
|
if (str.empty())
|
|
|
|
continue;
|
|
|
|
|
2014-05-26 21:57:10 +02:00
|
|
|
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();
|
2015-06-27 18:39:09 +02:00
|
|
|
|
|
|
|
m_selections.erase(merge_overlapping(begin(), end(), m_main, overlaps),
|
|
|
|
end());
|
|
|
|
|
2014-06-05 00:23:37 +02:00
|
|
|
ForwardChangesTracker changes_tracker;
|
2014-05-26 21:57:10 +02:00
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
2014-06-05 00:23:37 +02:00
|
|
|
sel.anchor() = changes_tracker.get_new_coord(sel.anchor());
|
|
|
|
kak_assert(m_buffer->is_valid(sel.anchor()));
|
|
|
|
sel.cursor() = changes_tracker.get_new_coord(sel.cursor());
|
|
|
|
kak_assert(m_buffer->is_valid(sel.cursor()));
|
|
|
|
|
2014-05-26 21:57:10 +02:00
|
|
|
auto pos = Kakoune::erase(*m_buffer, sel);
|
2016-03-16 14:59:30 +01:00
|
|
|
sel.anchor() = sel.cursor() = m_buffer->clamp(pos);
|
2014-05-26 21:57:10 +02:00
|
|
|
changes_tracker.update(*m_buffer, m_timestamp);
|
|
|
|
}
|
2015-01-23 20:12:15 +01:00
|
|
|
|
|
|
|
ByteCoord back_coord = m_buffer->back_coord();
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
|
|
|
if (sel.anchor() > back_coord)
|
|
|
|
sel.anchor() = back_coord;
|
|
|
|
if (sel.cursor() > back_coord)
|
|
|
|
sel.cursor() = back_coord;
|
|
|
|
}
|
2015-06-27 18:39:09 +02:00
|
|
|
|
2014-05-26 21:57:10 +02:00
|
|
|
m_buffer->check_invariant();
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:00:50 +02:00
|
|
|
String selection_to_string(const Selection& selection)
|
2015-04-13 16:21:26 +02:00
|
|
|
{
|
|
|
|
auto& cursor = selection.cursor();
|
|
|
|
auto& anchor = selection.anchor();
|
2015-06-25 15:00:50 +02:00
|
|
|
return format("{}.{},{}.{}", anchor.line + 1, anchor.column + 1,
|
|
|
|
cursor.line + 1, cursor.column + 1);
|
2015-04-13 16:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String selection_list_to_string(const SelectionList& selections)
|
|
|
|
{
|
2016-03-08 22:35:56 +01:00
|
|
|
return join(selections | transform([](const Selection& s)
|
|
|
|
{ return selection_to_string(s); }),
|
2015-04-13 16:21:26 +02:00
|
|
|
':', false);
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:00:50 +02:00
|
|
|
Selection selection_from_string(StringView desc)
|
2015-04-13 16:21:26 +02:00
|
|
|
{
|
2015-06-25 15:00:50 +02:00
|
|
|
auto comma = find(desc, ',');
|
|
|
|
auto dot_anchor = find(StringView{desc.begin(), comma}, '.');
|
|
|
|
auto dot_cursor = find(StringView{comma, desc.end()}, '.');
|
|
|
|
|
|
|
|
if (comma == desc.end() or dot_anchor == comma or dot_cursor == desc.end())
|
|
|
|
throw runtime_error(format("'{}' does not follow <line>.<column>,<line>.<column> format", desc));
|
|
|
|
|
|
|
|
ByteCoord anchor{str_to_int({desc.begin(), dot_anchor}) - 1,
|
|
|
|
str_to_int({dot_anchor+1, comma}) - 1};
|
2015-04-13 16:21:26 +02:00
|
|
|
|
2015-06-25 15:00:50 +02:00
|
|
|
ByteCoord cursor{str_to_int({comma+1, dot_cursor}) - 1,
|
|
|
|
str_to_int({dot_cursor+1, desc.end()}) - 1};
|
2015-04-13 16:21:26 +02:00
|
|
|
|
2015-06-25 15:00:50 +02:00
|
|
|
return Selection{anchor, cursor};
|
2015-04-13 16:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SelectionList selection_list_from_string(Buffer& buffer, StringView desc)
|
|
|
|
{
|
2016-06-06 18:17:18 +02:00
|
|
|
if (desc.empty())
|
|
|
|
throw runtime_error{"empty selection description"};
|
|
|
|
|
2015-04-13 16:21:26 +02:00
|
|
|
Vector<Selection> sels;
|
2016-03-25 01:14:56 +01:00
|
|
|
for (auto sel_desc : desc | split<StringView>(':'))
|
2015-06-25 15:00:50 +02:00
|
|
|
sels.push_back(selection_from_string(sel_desc));
|
2015-04-13 16:21:26 +02:00
|
|
|
return {buffer, std::move(sels)};
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|