2012-01-31 20:12:06 +01:00
|
|
|
#include "selection.hh"
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
#include "buffer_utils.hh"
|
2017-01-13 14:45:46 +01:00
|
|
|
#include "changes.hh"
|
|
|
|
#include "utf8.hh"
|
2012-10-08 14:26:57 +02:00
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Selection s, size_t timestamp)
|
2019-02-09 05:41:09 +01:00
|
|
|
: m_selections({ std::move(s) }), m_buffer(&buffer), 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
|
|
|
|
2018-01-11 21:51:19 +01:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list, size_t timestamp)
|
2019-02-09 05:41:09 +01:00
|
|
|
: m_selections(std::move(list)), m_buffer(&buffer), 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
|
|
|
|
2018-01-11 21:51:19 +01:00
|
|
|
SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list)
|
|
|
|
: SelectionList(buffer, std::move(list), buffer.timestamp()) {}
|
|
|
|
|
2017-03-03 19:47:11 +01:00
|
|
|
void SelectionList::remove(size_t index)
|
|
|
|
{
|
|
|
|
m_selections.erase(begin() + index);
|
|
|
|
if (index < m_main or m_main == m_selections.size())
|
|
|
|
--m_main;
|
|
|
|
}
|
2017-03-15 17:51:55 +01:00
|
|
|
void SelectionList::set(Vector<Selection> list, size_t main)
|
|
|
|
{
|
|
|
|
kak_assert(main < list.size());
|
|
|
|
m_selections = std::move(list);
|
|
|
|
m_main = main;
|
2018-03-04 00:35:24 +01:00
|
|
|
m_timestamp = m_buffer->timestamp();
|
2017-03-15 17:51:55 +01:00
|
|
|
sort_and_merge_overlapping();
|
|
|
|
check_invariant();
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:48:09 +01:00
|
|
|
bool compare_selections(const Selection& lhs, const Selection& rhs)
|
|
|
|
{
|
2019-11-02 23:36:19 +01:00
|
|
|
const auto& lmin = lhs.min(), rmin = rhs.min();
|
2019-02-11 11:48:09 +01:00
|
|
|
return lmin == rmin ? lhs.max() < rhs.max() : lmin < rmin;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord update_insert(BufferCoord coord, BufferCoord begin, BufferCoord end)
|
2014-05-26 21:57:10 +02:00
|
|
|
{
|
|
|
|
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
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord update_erase(BufferCoord coord, BufferCoord begin, BufferCoord end)
|
2014-05-26 21:57:10 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2014-06-02 16:13:56 +02:00
|
|
|
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]))
|
|
|
|
{
|
2017-06-07 20:30:44 +02:00
|
|
|
begin[i].min() = std::min(begin[i].min(), begin[j].min());
|
|
|
|
begin[i].max() = std::max(begin[i].max(), begin[j].max());
|
2014-06-02 16:13:56 +02:00
|
|
|
if (i < main)
|
|
|
|
--main;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if (i != j)
|
|
|
|
begin[i] = std::move(begin[j]);
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 08:40:17 +02:00
|
|
|
kak_assert(std::is_sorted(begin, begin + i +1, compare_selections));
|
2014-06-02 16:13:56 +02:00
|
|
|
return begin + i + 1;
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-02 03:16:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:45:46 +01:00
|
|
|
BufferCoord& get_first(Selection& sel) { return sel.min(); }
|
|
|
|
BufferCoord& get_last(Selection& sel) { return sel.max(); }
|
2013-05-02 19:04:59 +02:00
|
|
|
|
2019-11-02 21:49:26 +01:00
|
|
|
Vector<Selection> compute_modified_ranges(const 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
|
|
|
|
2017-11-01 07:04:42 +01:00
|
|
|
kak_assert(std::is_sorted(ranges.begin(), ranges.end(), compare_selections));
|
|
|
|
|
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)
|
2017-01-08 23:30:15 +01:00
|
|
|
ranges.emplace_back(change_it->begin, change_it->end);
|
2014-06-02 16:13:56 +02:00
|
|
|
else
|
2017-01-08 23:30:15 +01:00
|
|
|
ranges.emplace_back(change_it->begin);
|
2014-06-02 16:13:56 +02:00
|
|
|
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)
|
2017-01-08 23:30:15 +01:00
|
|
|
ranges.emplace_back(change.begin, change.end);
|
2014-06-02 16:13:56 +02:00
|
|
|
else
|
2017-01-08 23:30:15 +01:00
|
|
|
ranges.emplace_back(change.begin);
|
2014-06-05 00:23:37 +02:00
|
|
|
changes_tracker.update(change);
|
2014-06-02 16:13:56 +02:00
|
|
|
}
|
|
|
|
change_it = backward_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-11-27 10:29:19 +01:00
|
|
|
// The newly added ranges might be overlapping pre-existing ones
|
|
|
|
ranges.erase(merge_overlapping(ranges.begin(), ranges.end(), dummy, overlaps), ranges.end());
|
2016-07-27 00:31:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto end_coord = buffer.end_coord();
|
|
|
|
for (auto& range : ranges)
|
|
|
|
{
|
|
|
|
range.anchor() = std::min(range.anchor(), end_coord);
|
2016-09-22 21:36:26 +02:00
|
|
|
range.cursor() = std::min<BufferCoord>(range.cursor(), end_coord);
|
2014-06-02 16:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto touches = [&](const Selection& lhs, const Selection& rhs) {
|
2017-06-11 13:24:45 +02:00
|
|
|
return lhs.max() == end_coord or buffer.char_next(lhs.max()) >= rhs.min();
|
2014-06-02 16:13:56 +02:00
|
|
|
};
|
|
|
|
size_t dummy = 0;
|
|
|
|
ranges.erase(merge_overlapping(ranges.begin(), ranges.end(), dummy, touches), ranges.end());
|
|
|
|
|
|
|
|
for (auto& sel : ranges)
|
|
|
|
{
|
2016-07-27 00:31:23 +02:00
|
|
|
kak_assert(buffer.is_valid(sel.anchor()));
|
|
|
|
kak_assert(buffer.is_valid(sel.cursor()));
|
|
|
|
|
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-13 20:25:32 +02:00
|
|
|
static void clamp(Selection& sel, const Buffer& buffer)
|
|
|
|
{
|
|
|
|
sel.anchor() = buffer.clamp(sel.anchor());
|
|
|
|
sel.cursor() = buffer.clamp(sel.cursor());
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:45 +02:00
|
|
|
void clamp_selections(Vector<Selection>& selections, const Buffer& buffer)
|
|
|
|
{
|
|
|
|
for (auto& sel : selections)
|
|
|
|
clamp(sel, buffer);
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:49:26 +01:00
|
|
|
void update_selections(Vector<Selection>& selections, size_t& main, const Buffer& buffer, size_t timestamp, bool merge)
|
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
|
|
|
}
|
2017-01-13 14:45:46 +01:00
|
|
|
kak_assert(std::is_sorted(selections.begin(), selections.end(),
|
|
|
|
compare_selections));
|
2019-05-29 15:12:04 +02:00
|
|
|
if (merge)
|
|
|
|
selections.erase(
|
|
|
|
merge_overlapping(selections.begin(), selections.end(),
|
|
|
|
main, overlaps), selections.end());
|
2014-05-26 21:57:10 +02:00
|
|
|
}
|
2016-07-08 10:52:10 +02:00
|
|
|
for (auto& sel : selections)
|
2016-07-13 20:25:32 +02:00
|
|
|
clamp(sel, buffer);
|
|
|
|
|
2019-05-29 15:12:04 +02:00
|
|
|
if (merge)
|
|
|
|
selections.erase(merge_overlapping(selections.begin(), selections.end(),
|
|
|
|
main, overlaps), selections.end());
|
2016-07-08 10:52:10 +02:00
|
|
|
}
|
2014-05-14 01:59:36 +02:00
|
|
|
|
2019-05-29 15:12:04 +02:00
|
|
|
void SelectionList::update(bool merge)
|
2016-07-08 10:52:10 +02:00
|
|
|
{
|
2019-05-29 15:12:04 +02:00
|
|
|
update_selections(m_selections, m_main, *m_buffer, m_timestamp, merge);
|
2016-07-08 10:52:10 +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());
|
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;
|
|
|
|
|
2016-07-27 01:04:06 +02:00
|
|
|
const auto end_coord = buffer.end_coord();
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord last_min{0,0};
|
2016-07-27 01:04:06 +02:00
|
|
|
for (auto& sel : m_selections)
|
2014-05-13 00:25:15 +02:00
|
|
|
{
|
2016-07-27 01:04:06 +02:00
|
|
|
auto& min = sel.min();
|
|
|
|
kak_assert(min >= last_min);
|
|
|
|
last_min = min;
|
|
|
|
|
|
|
|
const auto anchor = sel.anchor();
|
2016-09-22 21:36:26 +02:00
|
|
|
kak_assert(anchor >= BufferCoord{0,0} and anchor < end_coord);
|
2016-07-27 01:04:06 +02:00
|
|
|
kak_assert(anchor.column < buffer[anchor.line].length());
|
|
|
|
|
|
|
|
const auto cursor = sel.cursor();
|
2016-09-22 21:36:26 +02:00
|
|
|
kak_assert(cursor >= BufferCoord{0,0} and cursor < end_coord);
|
2016-07-27 01:04:06 +02:00
|
|
|
kak_assert(cursor.column < buffer[cursor.line].length());
|
2014-05-13 00:25:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:45 +02:00
|
|
|
void sort_selections(Vector<Selection>& selections, size_t& main_index)
|
2014-05-13 00:25:15 +02:00
|
|
|
{
|
2018-06-03 13:08:45 +02:00
|
|
|
if (selections.size() == 1)
|
2014-05-13 00:25:15 +02:00
|
|
|
return;
|
|
|
|
|
2018-06-03 13:08:45 +02:00
|
|
|
const auto& main = selections[main_index];
|
2014-05-13 00:25:15 +02:00
|
|
|
const auto main_begin = main.min();
|
2018-06-03 13:08:45 +02:00
|
|
|
main_index = std::count_if(selections.begin(), selections.end(),
|
|
|
|
[&](const Selection& sel) {
|
|
|
|
auto begin = sel.min();
|
|
|
|
if (begin == main_begin)
|
|
|
|
return &sel < &main;
|
|
|
|
else
|
|
|
|
return begin < main_begin;
|
|
|
|
});
|
|
|
|
std::stable_sort(selections.begin(), selections.end(), compare_selections);
|
2016-07-30 16:32:47 +02:00
|
|
|
}
|
|
|
|
|
2018-06-03 13:08:45 +02:00
|
|
|
void merge_overlapping_selections(Vector<Selection>& selections, size_t& main_index)
|
2016-07-30 16:32:47 +02:00
|
|
|
{
|
2018-06-03 13:08:45 +02:00
|
|
|
if (selections.size() == 1)
|
2016-07-30 16:32:47 +02:00
|
|
|
return;
|
|
|
|
|
2018-06-03 13:08:45 +02:00
|
|
|
selections.erase(Kakoune::merge_overlapping(selections.begin(), selections.end(),
|
|
|
|
main_index, overlaps), selections.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionList::sort()
|
|
|
|
{
|
|
|
|
sort_selections(m_selections, m_main);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionList::merge_overlapping()
|
|
|
|
{
|
|
|
|
merge_overlapping_selections(m_selections, m_main);
|
2016-07-30 16:32:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-27 12:19:07 +02:00
|
|
|
void SelectionList::merge_consecutive()
|
|
|
|
{
|
|
|
|
if (size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto touches = [this](const Selection& lhs, const Selection& rhs) {
|
|
|
|
return m_buffer->char_next(lhs.max()) >= rhs.min();
|
|
|
|
};
|
|
|
|
m_selections.erase(Kakoune::merge_overlapping(begin(), end(),
|
|
|
|
m_main, touches), end());
|
|
|
|
}
|
|
|
|
|
2016-07-30 16:32:47 +02:00
|
|
|
void SelectionList::sort_and_merge_overlapping()
|
|
|
|
{
|
|
|
|
sort();
|
|
|
|
merge_overlapping();
|
2013-05-03 13:38:48 +02:00
|
|
|
}
|
2014-05-14 21:56:27 +02:00
|
|
|
|
2017-01-01 19:07:57 +01:00
|
|
|
BufferCoord get_insert_pos(const 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::Append:
|
2018-07-04 11:42:32 +02:00
|
|
|
return buffer.char_next(sel.max());
|
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;
|
2017-01-01 19:07:57 +01:00
|
|
|
default:
|
|
|
|
kak_assert(false);
|
|
|
|
return {};
|
2014-05-25 21:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 06:05:52 +02:00
|
|
|
static void fix_overflowing_selections(Vector<Selection>& selections,
|
|
|
|
const Buffer& buffer)
|
|
|
|
{
|
|
|
|
const BufferCoord back_coord = buffer.back_coord();
|
|
|
|
for (auto& sel : selections)
|
|
|
|
{
|
2017-07-14 09:16:43 +02:00
|
|
|
sel.cursor() = std::min(buffer.clamp(sel.cursor()), back_coord);
|
|
|
|
sel.anchor() = std::min(buffer.clamp(sel.anchor()), back_coord);
|
2017-07-14 06:05:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
|
2017-01-01 18:31:47 +01:00
|
|
|
Vector<BufferCoord>* out_insert_pos)
|
2014-05-25 21:28:32 +02:00
|
|
|
{
|
|
|
|
if (strings.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
update();
|
2017-01-01 19:07:57 +01:00
|
|
|
|
|
|
|
Vector<BufferCoord> insert_pos;
|
|
|
|
if (mode != InsertMode::Replace)
|
|
|
|
{
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
insert_pos.push_back(get_insert_pos(*m_buffer, sel, mode));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-01-01 19:07:57 +01:00
|
|
|
sel.anchor() = changes_tracker.get_new_coord_tolerant(sel.anchor());
|
|
|
|
sel.cursor() = changes_tracker.get_new_coord_tolerant(sel.cursor());
|
2017-01-01 18:31:47 +01:00
|
|
|
kak_assert(m_buffer->is_valid(sel.anchor()) and
|
|
|
|
m_buffer->is_valid(sel.cursor()));
|
2014-05-26 21:57:10 +02:00
|
|
|
|
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
|
|
|
|
2017-01-01 18:31:47 +01:00
|
|
|
const auto pos = (mode == InsertMode::Replace) ?
|
2019-01-23 09:48:43 +01:00
|
|
|
sel.min() : changes_tracker.get_new_coord(insert_pos[index]);
|
|
|
|
|
|
|
|
if (mode == InsertMode::Replace)
|
2019-12-18 01:36:17 +01:00
|
|
|
{
|
|
|
|
auto range = replace(*m_buffer, sel, str);
|
|
|
|
// we want min and max from *before* we do any change
|
|
|
|
auto& min = sel.min();
|
|
|
|
auto& max = sel.max();
|
|
|
|
min = range.begin;
|
|
|
|
max = m_buffer->char_prev(range.end);
|
|
|
|
}
|
2019-01-23 09:48:43 +01:00
|
|
|
else
|
2019-12-18 01:36:17 +01:00
|
|
|
{
|
|
|
|
auto range = m_buffer->insert(pos, str);
|
|
|
|
sel.anchor() = m_buffer->clamp(update_insert(sel.anchor(), range.begin, range.end));
|
|
|
|
sel.cursor() = m_buffer->clamp(update_insert(sel.cursor(), range.begin, range.end));
|
|
|
|
}
|
2017-01-01 19:07:57 +01:00
|
|
|
|
|
|
|
changes_tracker.update(*m_buffer, m_timestamp);
|
2017-01-01 18:31:47 +01:00
|
|
|
if (out_insert_pos)
|
|
|
|
out_insert_pos->push_back(pos);
|
2014-05-25 21:28:32 +02:00
|
|
|
}
|
2017-07-14 06:05:52 +02:00
|
|
|
|
|
|
|
// We might just have been deleting text if strings were empty,
|
|
|
|
// in which case we could have some selections pushed out of the buffer
|
|
|
|
if (mode == InsertMode::Replace)
|
|
|
|
fix_overflowing_selections(m_selections, *m_buffer);
|
|
|
|
|
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();
|
2016-07-30 16:32:47 +02:00
|
|
|
merge_overlapping();
|
2015-06-27 18:39:09 +02:00
|
|
|
|
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);
|
2017-06-11 13:01:40 +02:00
|
|
|
sel.anchor() = sel.cursor() = pos;
|
2014-05-26 21:57:10 +02:00
|
|
|
changes_tracker.update(*m_buffer, m_timestamp);
|
|
|
|
}
|
2015-01-23 20:12:15 +01:00
|
|
|
|
2017-07-14 06:05:52 +02:00
|
|
|
fix_overflowing_selections(m_selections, *m_buffer);
|
2014-05-26 21:57:10 +02:00
|
|
|
m_buffer->check_invariant();
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:45:48 +01:00
|
|
|
String selection_to_string(ColumnType column_type, const Buffer& buffer, const Selection& selection, ColumnCount tabstop)
|
2015-04-13 16:21:26 +02:00
|
|
|
{
|
2019-11-02 21:49:26 +01:00
|
|
|
const auto& cursor = selection.cursor();
|
|
|
|
const auto& anchor = selection.anchor();
|
2019-11-12 11:56:45 +01:00
|
|
|
switch (column_type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case ColumnType::Byte:
|
|
|
|
return format("{}.{},{}.{}", anchor.line + 1, anchor.column + 1,
|
|
|
|
cursor.line + 1, cursor.column + 1);
|
|
|
|
case ColumnType::Codepoint:
|
|
|
|
return format("{}.{},{}.{}",
|
|
|
|
anchor.line + 1, buffer[anchor.line].char_count_to(anchor.column) + 1,
|
|
|
|
cursor.line + 1, buffer[cursor.line].char_count_to(cursor.column) + 1);
|
|
|
|
case ColumnType::DisplayColumn:
|
2019-11-13 01:45:48 +01:00
|
|
|
kak_assert(tabstop != -1);
|
2019-11-12 11:56:45 +01:00
|
|
|
return format("{}.{},{}.{}",
|
2019-11-13 01:45:48 +01:00
|
|
|
anchor.line + 1, get_column(buffer, tabstop, anchor) + 1,
|
|
|
|
cursor.line + 1, get_column(buffer, tabstop, cursor) + 1);
|
2019-11-12 11:56:45 +01:00
|
|
|
}
|
2015-04-13 16:21:26 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 01:45:48 +01:00
|
|
|
String selection_list_to_string(ColumnType column_type, const SelectionList& selections, ColumnCount tabstop)
|
2019-11-12 11:20:59 +01:00
|
|
|
{
|
|
|
|
auto& buffer = selections.buffer();
|
|
|
|
kak_assert(selections.timestamp() == buffer.timestamp());
|
|
|
|
|
|
|
|
auto to_string = [&](const Selection& selection) {
|
2019-11-13 01:45:48 +01:00
|
|
|
return selection_to_string(column_type, buffer, selection, tabstop);
|
2019-11-12 11:20:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
auto beg = &*selections.begin(), end = &*selections.end();
|
|
|
|
auto main = beg + selections.main_index();
|
|
|
|
using View = ConstArrayView<Selection>;
|
|
|
|
return join(concatenated(View{main, end}, View{beg, main}) |
|
|
|
|
transform(to_string), ' ', false);
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:45:48 +01:00
|
|
|
Selection selection_from_string(ColumnType column_type, const Buffer& buffer, StringView desc, ColumnCount tabstop)
|
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));
|
|
|
|
|
2019-11-12 11:56:45 +01:00
|
|
|
auto compute_coord = [&](int line, int column) -> BufferCoord {
|
|
|
|
if (line < 0 or column < 0)
|
2019-11-13 01:45:48 +01:00
|
|
|
throw runtime_error(format("coordinate {}.{} does not exist in buffer", line + 1, column + 1));
|
2019-11-12 11:56:45 +01:00
|
|
|
|
|
|
|
switch (column_type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case ColumnType::Byte: return {line, column};
|
|
|
|
case ColumnType::Codepoint:
|
|
|
|
if (buffer.line_count() <= line or buffer[line].char_length() <= column)
|
2019-11-13 01:45:48 +01:00
|
|
|
throw runtime_error(format("coordinate {}.{} does not exist in buffer", line + 1, column + 1));
|
2019-11-12 11:56:45 +01:00
|
|
|
return {line, buffer[line].byte_count_to(CharCount{column})};
|
|
|
|
case ColumnType::DisplayColumn:
|
2019-11-13 01:45:48 +01:00
|
|
|
kak_assert(tabstop != -1);
|
|
|
|
if (buffer.line_count() <= line or column_length(buffer, tabstop, line) <= column)
|
|
|
|
throw runtime_error(format("coordinate {}.{} does not exist in buffer", line + 1, column + 1));
|
|
|
|
return {line, get_byte_to_column(buffer, tabstop, DisplayCoord{line, ColumnCount{column}})};
|
2019-11-12 11:56:45 +01:00
|
|
|
}
|
|
|
|
};
|
2015-04-13 16:21:26 +02:00
|
|
|
|
2019-11-12 11:20:59 +01:00
|
|
|
auto anchor = compute_coord(str_to_int({desc.begin(), dot_anchor}) - 1,
|
|
|
|
str_to_int({dot_anchor+1, comma}) - 1);
|
|
|
|
|
|
|
|
auto cursor = compute_coord(str_to_int({comma+1, dot_cursor}) - 1,
|
|
|
|
str_to_int({dot_cursor+1, desc.end()}) - 1);
|
2017-12-12 08:08:40 +01:00
|
|
|
|
2015-06-25 15:00:50 +02:00
|
|
|
return Selection{anchor, cursor};
|
2015-04-13 16:21:26 +02:00
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|