From 9c368c85d6c75f4d81551ff8870e75ca840f0c9a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 12 Nov 2012 20:41:03 +0100 Subject: [PATCH] use SelectionsAndCaptures to store jumps --- src/context.hh | 27 ++++++++++++++------------- src/main.cc | 33 ++++++++++++--------------------- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/context.hh b/src/context.hh index 844b4d04..a59e41eb 100644 --- a/src/context.hh +++ b/src/context.hh @@ -100,23 +100,25 @@ struct Context using Insertion = std::pair>; Insertion& last_insert() { return m_last_insert; } - using Jump = std::pair, BufferCoord>; void push_jump() { - Jump jump{safe_ptr{&buffer()}, - editor().selections().back().last().coord()}; + const SelectionAndCaptures& jump = editor().selections().back(); if (m_current_jump != m_jump_list.end()) { - m_jump_list.erase(m_current_jump+1, m_jump_list.end()); - if (*m_current_jump == jump) - return; + auto begin = m_current_jump; + // when jump overlaps with m_current_jump, we replace m_current_jump + // instead of pushing after it. + if (&begin->first().buffer() != &jump.first().buffer() or + not overlaps(*begin, jump)) + ++begin; + m_jump_list.erase(begin, m_jump_list.end()); } m_jump_list.push_back(jump); m_current_jump = m_jump_list.end(); } - Jump jump_forward() + SelectionAndCaptures jump_forward() { if (m_current_jump != m_jump_list.end() and m_current_jump + 1 != m_jump_list.end()) @@ -124,7 +126,7 @@ struct Context throw runtime_error("no next jump"); } - Jump jump_backward() + SelectionAndCaptures jump_backward() { if (m_current_jump != m_jump_list.begin()) { @@ -142,12 +144,12 @@ struct Context { for (auto it = m_jump_list.begin(); it != m_jump_list.end();) { - if (it->first == &buffer) + if (&it->first().buffer() == &buffer) { if (it < m_current_jump) --m_current_jump; else if (it == m_current_jump) - m_current_jump = m_jump_list.end(); + m_current_jump = m_jump_list.end()-1; it = m_jump_list.erase(it); } @@ -165,9 +167,8 @@ private: Insertion m_last_insert = {InsertMode::Insert, {}}; int m_numeric_param = 0; - using JumpList = std::vector; - JumpList m_jump_list; - JumpList::iterator m_current_jump = m_jump_list.begin(); + SelectionAndCapturesList m_jump_list; + SelectionAndCapturesList::iterator m_current_jump = m_jump_list.begin(); }; } diff --git a/src/main.cc b/src/main.cc index 9f2136dc..804fd7fd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -332,30 +332,21 @@ void select_to_next_char(Context& context) }); } -void jump_forward(Context& context) +enum class JumpDirection { Forward, Backward }; +template +void jump(Context& context) { - auto jump = context.jump_forward(); + auto jump = (direction == JumpDirection::Forward) ? + context.jump_forward() : context.jump_backward(); - BufferManager::instance().set_last_used_buffer(*jump.first); - if (jump.first != &context.buffer()) + Buffer& buffer = const_cast(jump.first().buffer()); + BufferManager::instance().set_last_used_buffer(buffer); + if (&buffer != &context.buffer()) { auto& manager = ClientManager::instance(); - context.change_editor(manager.get_unused_window_for_buffer(*jump.first)); + context.change_editor(manager.get_unused_window_for_buffer(buffer)); } - context.editor().select(jump.first->iterator_at(jump.second)); -} - -void jump_backward(Context& context) -{ - auto jump = context.jump_backward(); - - BufferManager::instance().set_last_used_buffer(*jump.first); - if (jump.first != &context.buffer()) - { - auto& manager = ClientManager::instance(); - context.change_editor(manager.get_unused_window_for_buffer(*jump.first)); - } - context.editor().select(jump.first->iterator_at(jump.second)); + context.editor().select(SelectionAndCapturesList{ jump }); } String runtime_directory() @@ -478,8 +469,8 @@ std::unordered_map> keymap = { { Key::Modifiers::None, Key::PageUp }, do_scroll }, { { Key::Modifiers::None, Key::PageDown }, do_scroll }, - { { Key::Modifiers::Control, 'i' }, jump_forward }, - { { Key::Modifiers::Control, 'o' }, jump_backward }, + { { Key::Modifiers::Control, 'i' }, jump }, + { { Key::Modifiers::Control, 'o' }, jump }, }; }