From 85e2a31b315a1308f0a40e50d85992500f7e2953 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 17 Sep 2011 14:57:35 +0000 Subject: [PATCH] Window: code cleanup and more intelligent Selection class --- src/window.cc | 39 +++++++++++++++++++++------------------ src/window.hh | 22 +++++++++++++++++++--- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/window.cc b/src/window.cc index feb379d8..4cf5d60b 100644 --- a/src/window.cc +++ b/src/window.cc @@ -22,10 +22,10 @@ void Window::erase() m_buffer.erase(cursor, cursor+1); } - for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) + for (auto& sel : m_selections) { - m_buffer.erase(sel->begin, sel->end); - sel->end = sel->begin; + m_buffer.erase(sel.begin(), sel.end()); + sel = Selection(sel.begin(), sel.begin()); } m_buffer.end_undo_group(); } @@ -55,11 +55,10 @@ void Window::insert(const String& string) move_cursor(measure_string(string)); } - for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) + for (auto& sel : m_selections) { - m_buffer.insert(sel->begin, string); - sel->begin += string.length(); - sel->end += string.length(); + m_buffer.insert(sel.begin(), string); + sel.offset(string.length()); } m_buffer.end_undo_group(); } @@ -73,9 +72,9 @@ void Window::append(const String& string) insert(string); } - for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) + for (auto& sel : m_selections) { - m_buffer.insert(sel->end, string); + m_buffer.insert(sel.end(), string); } m_buffer.end_undo_group(); } @@ -126,12 +125,12 @@ void Window::select(bool append, const Selector& selector) } else { - for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) + for (auto& sel : m_selections) { - sel->end = selector(sel->end).end; + sel = Selection(sel.begin(), selector(sel.end()).end()); } } - m_cursor = line_and_column_at(m_selections.back().end); + m_cursor = line_and_column_at(m_selections.back().end()); scroll_to_keep_cursor_visible_ifn(); } @@ -149,27 +148,31 @@ void Window::update_display_buffer() m_display_buffer.clear(); SelectionList sorted_selections = m_selections; + + for (auto& sel : sorted_selections) + sel.canonicalize(); + std::sort(sorted_selections.begin(), sorted_selections.end(), - [](const Selection& lhs, const Selection& rhs) { return lhs.begin < rhs.begin; }); + [](const Selection& lhs, const Selection& rhs) { return lhs.begin() < rhs.begin(); }); BufferIterator current_position = m_buffer.iterator_at(m_position); for (Selection& sel : sorted_selections) { - if (current_position != sel.begin) + if (current_position != sel.begin()) { DisplayAtom atom; - atom.content = m_buffer.string(current_position, sel.begin); + atom.content = m_buffer.string(current_position, sel.begin()); m_display_buffer.append(atom); } - if (sel.begin != sel.end) + if (sel.begin() != sel.end()) { DisplayAtom atom; - atom.content = m_buffer.string(sel.begin, sel.end); + atom.content = m_buffer.string(sel.begin(), sel.end()); atom.attribute = UNDERLINE; m_display_buffer.append(atom); } - current_position = sel.end; + current_position = sel.end(); } if (current_position != m_buffer.end()) { diff --git a/src/window.hh b/src/window.hh index f134cb71..e4afbe2b 100644 --- a/src/window.hh +++ b/src/window.hh @@ -20,10 +20,26 @@ struct WindowCoord : LineAndColumn struct Selection { Selection(const BufferIterator& begin, const BufferIterator& end) - : begin(begin), end(end) {} + : m_begin(begin), m_end(end) {} - BufferIterator begin; - BufferIterator end; + const BufferIterator& begin() const { return m_begin; } + const BufferIterator& end() const { return m_end; } + + void canonicalize() + { + if (m_end < m_begin) + std::swap(m_begin, m_end); + } + + void offset(int offset) + { + m_begin += offset; + m_end += offset; + } + +private: + BufferIterator m_begin; + BufferIterator m_end; }; typedef std::vector SelectionList;