2012-01-31 20:12:06 +01:00
|
|
|
#ifndef editor_hh_INCLUDED
|
|
|
|
#define editor_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
2012-12-11 19:51:59 +01:00
|
|
|
#include "dynamic_selection_list.hh"
|
2012-02-10 00:47:55 +01:00
|
|
|
#include "memoryview.hh"
|
2012-01-31 20:12:06 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-10-30 09:46:15 +01:00
|
|
|
namespace InputModes { class Insert; }
|
|
|
|
|
2012-02-10 00:47:55 +01:00
|
|
|
class Register;
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-09-07 14:29:29 +02:00
|
|
|
enum class SelectMode
|
|
|
|
{
|
|
|
|
Replace,
|
|
|
|
Extend,
|
|
|
|
Append,
|
2013-03-15 18:20:35 +01:00
|
|
|
ReplaceMain,
|
2012-09-07 14:29:29 +02:00
|
|
|
};
|
|
|
|
|
2012-10-09 19:25:20 +02:00
|
|
|
enum class InsertMode : unsigned
|
|
|
|
{
|
|
|
|
Insert,
|
|
|
|
Append,
|
|
|
|
Replace,
|
|
|
|
InsertAtLineBegin,
|
2012-11-26 19:23:50 +01:00
|
|
|
InsertAtNextLineBegin,
|
2012-10-09 19:25:20 +02:00
|
|
|
AppendAtLineEnd,
|
|
|
|
OpenLineBelow,
|
|
|
|
OpenLineAbove
|
|
|
|
};
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
// An Editor is a buffer mutator
|
|
|
|
//
|
|
|
|
// The Editor class provides methods to manipulate a set of selections
|
|
|
|
// and to use these selections to mutate it's buffer.
|
2012-08-05 18:23:09 +02:00
|
|
|
class Editor : public SafeCountable
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
|
|
|
public:
|
2013-06-01 14:22:57 +02:00
|
|
|
typedef std::function<Selection (const Buffer&, const Selection&)> Selector;
|
2013-12-13 10:11:48 +01:00
|
|
|
typedef std::function<void (const Buffer&, SelectionList&)> MultiSelector;
|
2012-01-31 20:12:06 +01:00
|
|
|
|
|
|
|
Editor(Buffer& buffer);
|
|
|
|
virtual ~Editor() {}
|
|
|
|
|
2012-11-04 23:39:25 +01:00
|
|
|
Buffer& buffer() const { return *m_buffer; }
|
2012-01-31 20:12:06 +01:00
|
|
|
|
|
|
|
void erase();
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2012-10-09 19:25:20 +02:00
|
|
|
void insert(const String& string,
|
|
|
|
InsertMode mode = InsertMode::Insert);
|
2013-07-26 01:17:12 +02:00
|
|
|
void insert(memoryview<String> strings,
|
2012-10-09 19:25:20 +02:00
|
|
|
InsertMode mode = InsertMode::Insert);
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-10-02 14:10:00 +02:00
|
|
|
void move_selections(LineCount move,
|
|
|
|
SelectMode mode = SelectMode::Replace);
|
|
|
|
void move_selections(CharCount move,
|
2012-09-07 14:29:29 +02:00
|
|
|
SelectMode mode = SelectMode::Replace);
|
2012-01-31 20:12:06 +01:00
|
|
|
void clear_selections();
|
2012-11-19 19:03:56 +01:00
|
|
|
void flip_selections();
|
2012-01-31 20:12:06 +01:00
|
|
|
void keep_selection(int index);
|
2012-05-29 13:15:43 +02:00
|
|
|
void remove_selection(int index);
|
2013-07-26 00:44:00 +02:00
|
|
|
void select(BufferCoord c, SelectMode mode = SelectMode::Replace)
|
2013-07-12 14:15:31 +02:00
|
|
|
{ select(Selection{ buffer().clamp(c) }, mode); }
|
2012-12-31 14:06:20 +01:00
|
|
|
void select(const Selection& sel,
|
2012-12-18 19:12:15 +01:00
|
|
|
SelectMode mode = SelectMode::Replace);
|
2012-09-07 14:29:29 +02:00
|
|
|
void select(const Selector& selector,
|
|
|
|
SelectMode mode = SelectMode::Replace);
|
2012-11-30 18:32:49 +01:00
|
|
|
void select(SelectionList selections);
|
2012-01-31 20:12:06 +01:00
|
|
|
void multi_select(const MultiSelector& selector);
|
|
|
|
|
2013-12-13 00:17:06 +01:00
|
|
|
void rotate_selections(int count) { m_selections.rotate_main(count); }
|
2013-03-15 18:48:59 +01:00
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
const SelectionList& selections() const { return m_selections; }
|
2013-12-13 00:17:06 +01:00
|
|
|
const Selection& main_selection() const { return m_selections.main(); }
|
|
|
|
size_t main_selection_index() const { return m_selections.main_index(); }
|
2012-02-10 00:47:55 +01:00
|
|
|
std::vector<String> selections_content() const;
|
2012-01-31 20:12:06 +01:00
|
|
|
|
|
|
|
bool undo();
|
|
|
|
bool redo();
|
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
bool is_editing() const { return m_edition_level!= 0; }
|
2012-01-31 20:12:06 +01:00
|
|
|
private:
|
2013-02-18 14:00:43 +01:00
|
|
|
friend struct scoped_edition;
|
2013-10-30 09:46:15 +01:00
|
|
|
friend class InputModes::Insert;
|
2012-02-08 00:41:10 +01:00
|
|
|
void begin_edition();
|
|
|
|
void end_edition();
|
|
|
|
|
2013-07-26 00:44:00 +02:00
|
|
|
virtual BufferCoord offset_coord(BufferCoord coord, LineCount move);
|
|
|
|
virtual BufferCoord offset_coord(BufferCoord coord, CharCount move);
|
2013-07-17 21:17:32 +02:00
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
int m_edition_level;
|
2012-01-31 20:12:06 +01:00
|
|
|
|
|
|
|
void check_invariant() const;
|
|
|
|
|
2012-11-04 23:39:25 +01:00
|
|
|
safe_ptr<Buffer> m_buffer;
|
2012-12-11 19:51:59 +01:00
|
|
|
DynamicSelectionList m_selections;
|
2012-01-31 20:12:06 +01:00
|
|
|
};
|
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
struct scoped_edition
|
|
|
|
{
|
|
|
|
scoped_edition(Editor& editor)
|
|
|
|
: m_editor(editor)
|
|
|
|
{ m_editor.begin_edition(); }
|
|
|
|
|
|
|
|
~scoped_edition()
|
|
|
|
{ m_editor.end_edition(); }
|
|
|
|
|
2012-10-01 14:23:04 +02:00
|
|
|
Editor& editor() const { return m_editor; }
|
2012-01-31 20:12:06 +01:00
|
|
|
private:
|
2013-10-30 09:46:15 +01:00
|
|
|
Editor& m_editor;
|
2012-01-31 20:12:06 +01:00
|
|
|
};
|
|
|
|
|
2013-10-30 09:46:15 +01:00
|
|
|
void avoid_eol(const Buffer& buffer, BufferCoord& coord);
|
|
|
|
void avoid_eol(const Buffer& buffer, Range& sel);
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // editor_hh_INCLUDED
|
|
|
|
|