2012-01-31 23:12:06 +04:00
|
|
|
#ifndef selection_hh_INCLUDED
|
|
|
|
#define selection_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-03-29 12:55:45 +04:00
|
|
|
using CaptureList = std::vector<String>;
|
|
|
|
|
|
|
|
// A selection is a Selection, associated with a CaptureList
|
|
|
|
struct Selection
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2014-04-03 01:33:52 +04:00
|
|
|
Selection() = default;
|
2014-03-29 12:55:45 +04:00
|
|
|
explicit Selection(BufferCoord pos) : Selection(pos,pos) {}
|
|
|
|
Selection(BufferCoord anchor, BufferCoord cursor,
|
|
|
|
CaptureList captures = {})
|
|
|
|
: m_anchor{anchor}, m_cursor{cursor},
|
|
|
|
m_captures(std::move(captures)) {}
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2014-03-29 12:55:45 +04:00
|
|
|
void merge_with(const Selection& range);
|
2012-11-30 21:32:49 +04:00
|
|
|
|
2014-01-28 23:05:49 +04:00
|
|
|
BufferCoord& anchor() { return m_anchor; }
|
|
|
|
BufferCoord& cursor() { return m_cursor; }
|
2012-11-30 21:32:49 +04:00
|
|
|
|
2014-01-28 23:05:49 +04:00
|
|
|
const BufferCoord& anchor() const { return m_anchor; }
|
|
|
|
const BufferCoord& cursor() const { return m_cursor; }
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2014-03-29 12:55:45 +04:00
|
|
|
CaptureList& captures() { return m_captures; }
|
|
|
|
const CaptureList& captures() const { return m_captures; }
|
|
|
|
|
|
|
|
bool operator== (const Selection& other) const
|
2012-12-11 22:51:59 +04:00
|
|
|
{
|
2014-01-28 23:05:49 +04:00
|
|
|
return m_anchor == other.m_anchor and m_cursor == other.m_cursor;
|
2012-12-11 22:51:59 +04:00
|
|
|
}
|
|
|
|
|
2014-01-28 23:05:49 +04:00
|
|
|
const BufferCoord& min() const { return std::min(m_anchor, m_cursor); }
|
|
|
|
const BufferCoord& max() const { return std::max(m_anchor, m_cursor); }
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-11-30 21:32:49 +04:00
|
|
|
private:
|
2014-01-28 23:05:49 +04:00
|
|
|
BufferCoord m_anchor;
|
|
|
|
BufferCoord m_cursor;
|
2014-03-29 12:55:45 +04:00
|
|
|
|
|
|
|
CaptureList m_captures;
|
2012-11-30 21:32:49 +04:00
|
|
|
};
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2014-03-29 12:55:45 +04:00
|
|
|
inline bool overlaps(const Selection& lhs, const Selection& rhs)
|
2012-12-11 17:11:33 +04:00
|
|
|
{
|
2013-05-24 16:25:50 +04:00
|
|
|
return lhs.min() <= rhs.min() ? lhs.max() >= rhs.min()
|
|
|
|
: lhs.min() <= rhs.max();
|
2013-05-03 15:53:23 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 03:17:06 +04:00
|
|
|
static bool compare_selections(const Selection& lhs, const Selection& rhs)
|
|
|
|
{
|
|
|
|
return lhs.min() < rhs.min();
|
|
|
|
}
|
|
|
|
|
2013-05-02 21:04:59 +04:00
|
|
|
struct SelectionList : std::vector<Selection>
|
|
|
|
{
|
2013-06-18 23:43:44 +04:00
|
|
|
SelectionList() = default;
|
2013-12-14 22:38:14 +04:00
|
|
|
SelectionList(BufferCoord c) : std::vector<Selection>{Selection{c,c}} {}
|
2013-06-18 23:43:44 +04:00
|
|
|
SelectionList(Selection s) : std::vector<Selection>{s} {}
|
2013-05-02 21:04:59 +04:00
|
|
|
|
2013-07-26 02:44:00 +04:00
|
|
|
void update_insert(const Buffer& buffer, BufferCoord begin, BufferCoord end);
|
|
|
|
void update_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end);
|
2013-05-03 15:38:48 +04:00
|
|
|
|
|
|
|
void check_invariant() const;
|
2013-12-13 03:17:06 +04:00
|
|
|
|
|
|
|
const Selection& main() const { return (*this)[m_main]; }
|
|
|
|
Selection& main() { return (*this)[m_main]; }
|
|
|
|
size_t main_index() const { return m_main; }
|
|
|
|
void set_main_index(size_t main) { kak_assert(main < size()); m_main = main; }
|
|
|
|
|
|
|
|
void rotate_main(int count) { m_main = (m_main + count) % size(); }
|
|
|
|
|
|
|
|
template<typename OverlapsFunc>
|
|
|
|
void merge_overlapping(OverlapsFunc overlaps)
|
|
|
|
{
|
|
|
|
kak_assert(std::is_sorted(begin(), end(), compare_selections));
|
2014-01-14 02:23:40 +04:00
|
|
|
size_t i = 0;
|
|
|
|
for (size_t j = 1; j < size(); ++j)
|
2013-12-13 03:17:06 +04:00
|
|
|
{
|
2014-01-14 02:23:40 +04:00
|
|
|
if (overlaps((*this)[i], (*this)[j]))
|
2013-12-13 03:17:06 +04:00
|
|
|
{
|
2014-01-14 02:23:40 +04:00
|
|
|
(*this)[i].merge_with((*this)[j]);
|
|
|
|
if (i < m_main)
|
2013-12-13 03:17:06 +04:00
|
|
|
--m_main;
|
|
|
|
}
|
|
|
|
else
|
2014-01-14 02:23:40 +04:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if (i != j)
|
|
|
|
(*this)[i] = std::move((*this)[j]);
|
|
|
|
}
|
2013-12-13 03:17:06 +04:00
|
|
|
}
|
2014-01-14 02:23:40 +04:00
|
|
|
erase(begin() + i + 1, end());
|
|
|
|
kak_assert(std::is_sorted(begin(), end(), compare_selections));
|
2013-12-13 03:17:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t m_main = 0;
|
2013-05-02 21:04:59 +04:00
|
|
|
};
|
2012-02-10 03:47:55 +04:00
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selection_hh_INCLUDED
|