From 1f0b06a85520faa7d8fdfa5a6a177261fe0fcaa9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 24 Sep 2011 12:39:40 +0000 Subject: [PATCH] Window: undo group cleanup and fix of append method --- src/window.cc | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/window.cc b/src/window.cc index d3cb630b..54d618f8 100644 --- a/src/window.cc +++ b/src/window.cc @@ -23,6 +23,16 @@ void Selection::offset(int offset) m_last += offset; } +struct scoped_undo_group +{ + scoped_undo_group(Buffer& buffer) + : m_buffer(buffer) { m_buffer.begin_undo_group(); } + + ~scoped_undo_group() { m_buffer.end_undo_group(); } +private: + Buffer& m_buffer; +}; + Window::Window(Buffer& buffer) : m_buffer(buffer), m_position(0, 0), @@ -34,7 +44,7 @@ Window::Window(Buffer& buffer) void Window::erase() { - m_buffer.begin_undo_group(); + scoped_undo_group undo_group(m_buffer); if (m_selections.empty()) { BufferIterator cursor = iterator_at(m_cursor); @@ -48,7 +58,6 @@ void Window::erase() } if (not m_selections.empty()) m_cursor = line_and_column_at(m_selections.back().last()); - m_buffer.end_undo_group(); } static WindowCoord measure_string(const Window::String& string) @@ -69,7 +78,8 @@ static WindowCoord measure_string(const Window::String& string) void Window::insert(const String& string) { - m_buffer.begin_undo_group(); + scoped_undo_group undo_group(m_buffer); + if (m_selections.empty()) { m_buffer.insert(iterator_at(m_cursor), string); @@ -81,23 +91,23 @@ void Window::insert(const String& string) m_buffer.insert(sel.begin(), string); sel.offset(string.length()); } - m_buffer.end_undo_group(); } void Window::append(const String& string) { - m_buffer.begin_undo_group(); if (m_selections.empty()) { move_cursor(WindowCoord(0 , 1)); insert(string); } - - for (auto& sel : m_selections) + else { - m_buffer.insert(sel.end(), string); + scoped_undo_group undo_group(m_buffer); + for (auto& sel : m_selections) + { + m_buffer.insert(sel.end(), string); + } } - m_buffer.end_undo_group(); } bool Window::undo()