2012-01-31 20:12:06 +01:00
|
|
|
#ifndef selection_hh_INCLUDED
|
|
|
|
#define selection_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
using CaptureList = std::vector<String>;
|
|
|
|
|
|
|
|
// A selection is a Selection, associated with a CaptureList
|
|
|
|
struct Selection
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2014-04-02 23:33:52 +02:00
|
|
|
Selection() = default;
|
2014-05-13 00:25:15 +02:00
|
|
|
Selection(ByteCoord pos) : Selection(pos,pos) {}
|
2014-05-07 20:51:01 +02:00
|
|
|
Selection(ByteCoord anchor, ByteCoord cursor,
|
2014-03-29 09:55:45 +01:00
|
|
|
CaptureList captures = {})
|
|
|
|
: m_anchor{anchor}, m_cursor{cursor},
|
|
|
|
m_captures(std::move(captures)) {}
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
void merge_with(const Selection& range);
|
2012-11-30 18:32:49 +01:00
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
ByteCoord& anchor() { return m_anchor; }
|
|
|
|
ByteCoord& cursor() { return m_cursor; }
|
2012-11-30 18:32:49 +01:00
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
const ByteCoord& anchor() const { return m_anchor; }
|
|
|
|
const ByteCoord& cursor() const { return m_cursor; }
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
CaptureList& captures() { return m_captures; }
|
|
|
|
const CaptureList& captures() const { return m_captures; }
|
|
|
|
|
|
|
|
bool operator== (const Selection& other) const
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2014-01-28 20:05:49 +01:00
|
|
|
return m_anchor == other.m_anchor and m_cursor == other.m_cursor;
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
const ByteCoord& min() const { return std::min(m_anchor, m_cursor); }
|
|
|
|
const ByteCoord& max() const { return std::max(m_anchor, m_cursor); }
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
private:
|
2014-05-07 20:51:01 +02:00
|
|
|
ByteCoord m_anchor;
|
|
|
|
ByteCoord m_cursor;
|
2014-03-29 09:55:45 +01:00
|
|
|
|
|
|
|
CaptureList m_captures;
|
2012-11-30 18:32:49 +01:00
|
|
|
};
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
inline bool overlaps(const Selection& lhs, const Selection& rhs)
|
2012-12-11 14:11:33 +01:00
|
|
|
{
|
2013-05-24 14:25:50 +02:00
|
|
|
return lhs.min() <= rhs.min() ? lhs.max() >= rhs.min()
|
|
|
|
: lhs.min() <= rhs.max();
|
2013-05-03 13:53:23 +02:00
|
|
|
}
|
|
|
|
|
2013-12-13 00:17:06 +01:00
|
|
|
static bool compare_selections(const Selection& lhs, const Selection& rhs)
|
|
|
|
{
|
|
|
|
return lhs.min() < rhs.min();
|
|
|
|
}
|
|
|
|
|
2014-05-11 20:44:19 +02:00
|
|
|
struct SelectionList
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
2014-05-13 00:25:15 +02:00
|
|
|
SelectionList(const Buffer& buffer, Selection s);
|
|
|
|
SelectionList(const Buffer& buffer, Selection s, size_t timestamp);
|
|
|
|
SelectionList(const Buffer& buffer, std::vector<Selection> s);
|
|
|
|
SelectionList(const Buffer& buffer, std::vector<Selection> s, size_t timestamp);
|
2013-05-02 19:04:59 +02:00
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
void update();
|
|
|
|
|
2013-05-03 13:38:48 +02:00
|
|
|
void check_invariant() const;
|
2013-12-13 00:17:06 +01: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(); }
|
|
|
|
|
2014-05-14 21:56:27 +02:00
|
|
|
void avoid_eol();
|
|
|
|
|
2014-05-11 20:44:19 +02:00
|
|
|
void push_back(const Selection& sel) { m_selections.push_back(sel); }
|
|
|
|
void push_back(Selection&& sel) { m_selections.push_back(std::move(sel)); }
|
|
|
|
|
|
|
|
Selection& operator[](size_t i) { return m_selections[i]; }
|
|
|
|
const Selection& operator[](size_t i) const { return m_selections[i]; }
|
|
|
|
|
2014-05-14 00:22:54 +02:00
|
|
|
SelectionList& operator=(std::vector<Selection> list)
|
|
|
|
{
|
|
|
|
m_selections = std::move(list);
|
|
|
|
m_main = size()-1;
|
|
|
|
sort_and_merge_overlapping();
|
|
|
|
check_invariant();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-11 20:44:19 +02:00
|
|
|
using iterator = std::vector<Selection>::iterator;
|
|
|
|
iterator begin() { return m_selections.begin(); }
|
|
|
|
iterator end() { return m_selections.end(); }
|
|
|
|
|
2014-05-14 21:56:27 +02:00
|
|
|
using reverse_iterator = std::vector<Selection>::reverse_iterator;
|
|
|
|
reverse_iterator rbegin() { return m_selections.rbegin(); }
|
|
|
|
reverse_iterator rend() { return m_selections.rend(); }
|
|
|
|
|
2014-05-11 20:44:19 +02:00
|
|
|
using const_iterator = std::vector<Selection>::const_iterator;
|
|
|
|
const_iterator begin() const { return m_selections.begin(); }
|
|
|
|
const_iterator end() const { return m_selections.end(); }
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
iterator insert(Args... args)
|
|
|
|
{
|
|
|
|
return m_selections.insert(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
iterator erase(Args... args)
|
|
|
|
{
|
|
|
|
return m_selections.erase(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const { return m_selections.size(); }
|
|
|
|
bool empty() const { return m_selections.empty(); }
|
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
bool operator==(const SelectionList& other) const { return m_buffer == other.m_buffer and m_selections == other.m_selections; }
|
|
|
|
bool operator!=(const SelectionList& other) const { return !((*this) == other); }
|
2014-05-11 20:44:19 +02:00
|
|
|
|
2013-12-13 00:17:06 +01:00
|
|
|
template<typename OverlapsFunc>
|
|
|
|
void merge_overlapping(OverlapsFunc overlaps)
|
|
|
|
{
|
|
|
|
kak_assert(std::is_sorted(begin(), end(), compare_selections));
|
2014-01-13 23:23:40 +01:00
|
|
|
size_t i = 0;
|
|
|
|
for (size_t j = 1; j < size(); ++j)
|
2013-12-13 00:17:06 +01:00
|
|
|
{
|
2014-01-13 23:23:40 +01:00
|
|
|
if (overlaps((*this)[i], (*this)[j]))
|
2013-12-13 00:17:06 +01:00
|
|
|
{
|
2014-01-13 23:23:40 +01:00
|
|
|
(*this)[i].merge_with((*this)[j]);
|
|
|
|
if (i < m_main)
|
2013-12-13 00:17:06 +01:00
|
|
|
--m_main;
|
|
|
|
}
|
|
|
|
else
|
2014-01-13 23:23:40 +01:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if (i != j)
|
|
|
|
(*this)[i] = std::move((*this)[j]);
|
|
|
|
}
|
2013-12-13 00:17:06 +01:00
|
|
|
}
|
2014-01-13 23:23:40 +01:00
|
|
|
erase(begin() + i + 1, end());
|
|
|
|
kak_assert(std::is_sorted(begin(), end(), compare_selections));
|
2013-12-13 00:17:06 +01:00
|
|
|
}
|
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
void sort_and_merge_overlapping();
|
|
|
|
|
|
|
|
const Buffer& buffer() const { return *m_buffer; }
|
|
|
|
|
|
|
|
size_t timestamp() const { return m_timestamp; }
|
|
|
|
void set_timestamp(size_t timestamp) { m_timestamp = timestamp; }
|
2013-12-13 00:17:06 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
size_t m_main = 0;
|
2014-05-11 20:44:19 +02:00
|
|
|
std::vector<Selection> m_selections;
|
2014-05-13 00:25:15 +02:00
|
|
|
|
|
|
|
safe_ptr<const Buffer> m_buffer;
|
|
|
|
size_t m_timestamp;
|
2013-05-02 19:04:59 +02:00
|
|
|
};
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2014-05-14 00:22:54 +02:00
|
|
|
void update_insert(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end, bool at_end);
|
|
|
|
void update_erase(std::vector<Selection>& sels, ByteCoord begin, ByteCoord end, bool at_end);
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selection_hh_INCLUDED
|