From 2057c69585b058de1c218c9b64d47a9dd0f3094c Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 11 Jan 2017 22:18:02 -0500 Subject: [PATCH] add Buffer::{undo,redo}() --- src/core/Buffer.cc | 31 +++++++++++++++++++++++++++++++ src/core/Buffer.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/src/core/Buffer.cc b/src/core/Buffer.cc index 64a65d8..a6e18b0 100644 --- a/src/core/Buffer.cc +++ b/src/core/Buffer.cc @@ -386,3 +386,34 @@ void Buffer::apply_change_unit(const ChangeUnit & change_unit, bool forward) erase_data(change_unit.buffer_position, change_unit.length); } } + +void Buffer::undo() +{ + if (m_current_change_operation_index != INVALID_CHANGE_OPERATION_INDEX) + { + apply_change_operation(*m_change_operations[m_current_change_operation_index], false); + m_current_change_operation_index = m_change_operations[m_current_change_operation_index]->parent; + } +} + +void Buffer::redo() +{ + size_t child_index = INVALID_CHANGE_OPERATION_INDEX; + if (m_current_change_operation_index == INVALID_CHANGE_OPERATION_INDEX) + { + if (m_change_operations.size() > 0u) + { + child_index = 0u; + } + } + else + { + child_index = *m_change_operations[m_current_change_operation_index]->children.begin(); + } + + if (child_index != INVALID_CHANGE_OPERATION_INDEX) + { + apply_change_operation(*m_change_operations[child_index], true); + m_current_change_operation_index = child_index; + } +} diff --git a/src/core/Buffer.h b/src/core/Buffer.h index e58bfb0..68b41e7 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -134,6 +134,8 @@ public: Iterator end() const { return *m_eof_iterator; } void push_operation() { m_operation_level++; } void pop_operation(); + void undo(); + void redo(); protected: bool m_eol_at_eof;