summaryrefslogtreecommitdiff
path: root/src/buffer.hh
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2011-09-06 18:49:32 +0000
committerMaxime Coste <frrrwww@gmail.com>2011-09-06 18:49:32 +0000
commit1e87fe17c6941ffc257a6afc22e6bf1f15342d19 (patch)
tree4572bcceae30022da09efee9cff2e22b3a0a918b /src/buffer.hh
parentf58cbf0b98f6fc36e1746107a5095d55eb1ffc94 (diff)
Buffer: Undo/Redo implementation
Currently only a linear undo, i.e. if you undo and then make some new changes, previous undoed changes are lost. Final undo system should support an undo tree, with timestamped modifications.
Diffstat (limited to 'src/buffer.hh')
-rw-r--r--src/buffer.hh37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/buffer.hh b/src/buffer.hh
index f1441747..60306647 100644
--- a/src/buffer.hh
+++ b/src/buffer.hh
@@ -68,6 +68,12 @@ public:
Buffer(const std::string& name,
const BufferString& initial_content = "");
+ void begin_undo_group();
+ void end_undo_group();
+
+ bool undo();
+ bool redo();
+
void erase(const BufferIterator& begin,
const BufferIterator& end);
@@ -92,6 +98,13 @@ public:
private:
BufferChar at(BufferPos position) const;
+
+ void do_erase(const BufferIterator& begin,
+ const BufferIterator& end);
+
+ void do_insert(const BufferIterator& position,
+ const BufferString& string);
+
friend class BufferIterator;
std::vector<BufferPos> m_lines;
@@ -103,6 +116,30 @@ private:
BufferString m_content;
std::string m_name;
+
+ struct Modification
+ {
+ enum Type { Insert, Erase };
+
+ Type type;
+ BufferIterator position;
+ BufferString content;
+
+ Modification(Type type, BufferIterator position, BufferString content)
+ : type(type), position(position), content(content) {}
+
+ Modification inverse() const;
+ };
+ typedef std::vector<Modification> UndoGroup;
+
+ std::vector<UndoGroup> m_history;
+ std::vector<UndoGroup>::iterator m_history_cursor;
+ UndoGroup m_current_undo_group;
+
+ void replay_modification(const Modification& modification);
+ void revert_modification(const Modification& modification);
+
+ void append_modification(Modification&& modification);
};
}