From c915e189500f6f6e986826e2e09a06009119ca9a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 16 Jul 2012 21:53:00 +0200 Subject: [PATCH] Move selection to last modification position in editor::{undo,redo} --- src/editor.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 1c300a61..f1b30cae 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -226,14 +226,63 @@ void Editor::multi_select(const MultiSelector& selector) } } +class LastModifiedRangeListener : public BufferChangeListener +{ +public: + LastModifiedRangeListener(Buffer& buffer) + : m_buffer(buffer) + { m_buffer.add_change_listener(*this); } + + ~LastModifiedRangeListener() + { m_buffer.remove_change_listener(*this); } + + void on_insert(const BufferIterator& begin, const BufferIterator& end) + { + m_first = begin; + m_last = end-1; + } + + void on_erase(const BufferIterator& begin, const BufferIterator& end) + { + m_first = begin; + if (m_first >= m_buffer.end()) + m_first = m_buffer.end() - 1; + m_last = m_first; + } + + const BufferIterator& first() const { return m_first; } + const BufferIterator& last() const { return m_last; } + +private: + BufferIterator m_first; + BufferIterator m_last; + Buffer& m_buffer; +}; + bool Editor::undo() { - return m_buffer.undo(); + LastModifiedRangeListener listener(buffer()); + bool res = m_buffer.undo(); + if (res) + { + m_selections.back().clear(); + m_selections.back().push_back(Selection(listener.first(), + listener.last())); + } + return res; } bool Editor::redo() { - return m_buffer.redo(); + LastModifiedRangeListener listener(buffer()); + bool res = m_buffer.redo(); + if (res) + { + m_selections.back().clear(); + m_selections.back().push_back(Selection(listener.first(), + listener.last())); + } + return res; } void Editor::check_invariant() const