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-05-13 02:25:15 +04:00
|
|
|
Selection(ByteCoord pos) : Selection(pos,pos) {}
|
2014-05-07 22:51:01 +04:00
|
|
|
Selection(ByteCoord anchor, ByteCoord cursor,
|
2014-03-29 12:55:45 +04:00
|
|
|
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-05-07 22:51:01 +04:00
|
|
|
ByteCoord& anchor() { return m_anchor; }
|
|
|
|
ByteCoord& cursor() { return m_cursor; }
|
2012-11-30 21:32:49 +04:00
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
const ByteCoord& anchor() const { return m_anchor; }
|
|
|
|
const ByteCoord& 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-05-29 08:48:40 +04:00
|
|
|
const ByteCoord& min() const { return m_anchor < m_cursor ? m_anchor : m_cursor; }
|
|
|
|
const ByteCoord& max() const { return m_anchor < m_cursor ? m_cursor : m_anchor; }
|
|
|
|
|
|
|
|
ByteCoord& min() { return m_anchor < m_cursor ? m_anchor : m_cursor; }
|
|
|
|
ByteCoord& max() { return m_anchor < m_cursor ? m_cursor : m_anchor; }
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-11-30 21:32:49 +04:00
|
|
|
private:
|
2014-05-07 22:51:01 +04:00
|
|
|
ByteCoord m_anchor;
|
|
|
|
ByteCoord 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
|
|
|
}
|
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
enum class InsertMode : unsigned
|
|
|
|
{
|
|
|
|
Insert,
|
2014-05-26 01:59:29 +04:00
|
|
|
InsertCursor,
|
2014-05-25 23:28:32 +04:00
|
|
|
Append,
|
|
|
|
Replace,
|
|
|
|
InsertAtLineBegin,
|
|
|
|
InsertAtNextLineBegin,
|
|
|
|
AppendAtLineEnd,
|
|
|
|
OpenLineBelow,
|
|
|
|
OpenLineAbove
|
|
|
|
};
|
|
|
|
|
2014-05-11 22:44:19 +04:00
|
|
|
struct SelectionList
|
2013-05-02 21:04:59 +04:00
|
|
|
{
|
2014-05-25 23:28:32 +04:00
|
|
|
SelectionList(Buffer& buffer, Selection s);
|
|
|
|
SelectionList(Buffer& buffer, Selection s, size_t timestamp);
|
|
|
|
SelectionList(Buffer& buffer, std::vector<Selection> s);
|
|
|
|
SelectionList(Buffer& buffer, std::vector<Selection> s, size_t timestamp);
|
2013-05-02 21:04:59 +04:00
|
|
|
|
2014-05-13 02:25:15 +04:00
|
|
|
void update();
|
|
|
|
|
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(); }
|
|
|
|
|
2014-05-14 23:56:27 +04:00
|
|
|
void avoid_eol();
|
|
|
|
|
2014-05-11 22:44:19 +04: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 02:22:54 +04:00
|
|
|
SelectionList& operator=(std::vector<Selection> list)
|
|
|
|
{
|
|
|
|
m_selections = std::move(list);
|
|
|
|
m_main = size()-1;
|
|
|
|
sort_and_merge_overlapping();
|
2014-05-17 15:13:49 +04:00
|
|
|
update_timestamp();
|
2014-05-14 02:22:54 +04:00
|
|
|
check_invariant();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-11 22:44:19 +04:00
|
|
|
using iterator = std::vector<Selection>::iterator;
|
|
|
|
iterator begin() { return m_selections.begin(); }
|
|
|
|
iterator end() { return m_selections.end(); }
|
|
|
|
|
|
|
|
using const_iterator = std::vector<Selection>::const_iterator;
|
|
|
|
const_iterator begin() const { return m_selections.begin(); }
|
|
|
|
const_iterator end() const { return m_selections.end(); }
|
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
void remove(size_t index) { m_selections.erase(begin() + index); }
|
2014-05-11 22:44:19 +04:00
|
|
|
|
|
|
|
size_t size() const { return m_selections.size(); }
|
|
|
|
|
2014-05-13 02:25:15 +04: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 22:44:19 +04:00
|
|
|
|
2014-05-13 02:25:15 +04:00
|
|
|
void sort_and_merge_overlapping();
|
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
Buffer& buffer() const { return *m_buffer; }
|
2014-05-13 02:25:15 +04:00
|
|
|
|
|
|
|
size_t timestamp() const { return m_timestamp; }
|
2014-05-17 15:13:49 +04:00
|
|
|
void update_timestamp() { m_timestamp = m_buffer->timestamp(); }
|
2013-12-13 03:17:06 +04:00
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
void insert(memoryview<String> strings, InsertMode mode);
|
|
|
|
void erase();
|
|
|
|
|
2013-12-13 03:17:06 +04:00
|
|
|
private:
|
|
|
|
size_t m_main = 0;
|
2014-05-11 22:44:19 +04:00
|
|
|
std::vector<Selection> m_selections;
|
2014-05-13 02:25:15 +04:00
|
|
|
|
2014-05-25 23:28:32 +04:00
|
|
|
safe_ptr<Buffer> m_buffer;
|
2014-05-13 02:25:15 +04:00
|
|
|
size_t m_timestamp;
|
2013-05-02 21:04:59 +04:00
|
|
|
};
|
2012-02-10 03:47:55 +04:00
|
|
|
|
2014-06-02 18:13:56 +04:00
|
|
|
std::vector<Selection> compute_modified_ranges(Buffer& buffer, size_t timestamp);
|
2014-05-14 02:22:54 +04:00
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selection_hh_INCLUDED
|