From 411e5a948621ea12d64495c419885f933335e9ec Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 23 Dec 2015 01:56:54 +0000 Subject: [PATCH] Extract jump list handling in a JumpList struct --- src/client_manager.cc | 2 +- src/context.cc | 67 +++++++++++++++++++++---------------------- src/context.hh | 27 +++++++++++------ src/normal.cc | 5 ++-- 4 files changed, 55 insertions(+), 46 deletions(-) diff --git a/src/client_manager.cc b/src/client_manager.cc index b71dfc75..c4b286d5 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -111,7 +111,7 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) { for (auto& client : m_clients) { - client->context().forget_jumps_to_buffer(buffer); + client->context().jump_list().forget_buffer(buffer); if (client->last_buffer() == &buffer) client->set_last_buffer(nullptr); diff --git a/src/context.cc b/src/context.cc index d2113a55..f72652b5 100644 --- a/src/context.cc +++ b/src/context.cc @@ -83,74 +83,73 @@ void Context::print_status(DisplayLine status) const client().print_status(std::move(status)); } -void Context::push_jump() +void JumpList::push(SelectionList jump) { - const SelectionList& jump = selections(); - if (m_current_jump != m_jump_list.end()) - m_jump_list.erase(m_current_jump+1, m_jump_list.end()); - m_jump_list.erase(std::remove(begin(m_jump_list), end(m_jump_list), jump), - end(m_jump_list)); - m_jump_list.push_back(jump); - m_current_jump = m_jump_list.end(); + if (m_current != m_jumps.end()) + m_jumps.erase(m_current+1, m_jumps.end()); + m_jumps.erase(std::remove(begin(m_jumps), end(m_jumps), jump), + end(m_jumps)); + m_jumps.push_back(jump); + m_current = m_jumps.end(); } -void Context::drop_jump() +void JumpList::drop() { - if (not m_jump_list.empty()) - m_jump_list.pop_back(); - m_current_jump = m_jump_list.end(); + if (not m_jumps.empty()) + m_jumps.pop_back(); + m_current = m_jumps.end(); } -const SelectionList& Context::jump_forward() +const SelectionList& JumpList::forward() { - if (m_current_jump != m_jump_list.end() and - m_current_jump + 1 != m_jump_list.end()) + if (m_current != m_jumps.end() and + m_current + 1 != m_jumps.end()) { - SelectionList& res = *++m_current_jump; + SelectionList& res = *++m_current; res.update(); return res; } throw runtime_error("no next jump"); } -const SelectionList& Context::jump_backward() +const SelectionList& JumpList::backward(const SelectionList& current) { - if (m_current_jump != m_jump_list.end() and - *m_current_jump != selections()) + if (m_current != m_jumps.end() and + *m_current != current) { - push_jump(); - SelectionList& res = *--m_current_jump; + push(current); + SelectionList& res = *--m_current; res.update(); return res; } - if (m_current_jump != m_jump_list.begin()) + if (m_current != m_jumps.begin()) { - if (m_current_jump == m_jump_list.end()) + if (m_current == m_jumps.end()) { - push_jump(); - --m_current_jump; - if (m_current_jump == m_jump_list.begin()) + push(current); + --m_current; + if (m_current == m_jumps.begin()) throw runtime_error("no previous jump"); } - SelectionList& res = *--m_current_jump; + SelectionList& res = *--m_current; res.update(); return res; } throw runtime_error("no previous jump"); } -void Context::forget_jumps_to_buffer(Buffer& buffer) +void JumpList::forget_buffer(Buffer& buffer) { - for (auto it = m_jump_list.begin(); it != m_jump_list.end();) + for (auto it = m_jumps.begin(); it != m_jumps.end();) { if (&it->buffer() == &buffer) { - if (it < m_current_jump) - --m_current_jump; - else if (it == m_current_jump) - m_current_jump = m_jump_list.end()-1; + if (it < m_current) + --m_current; + else if (it == m_current) + m_current = m_jumps.end()-1; - it = m_jump_list.erase(it); + it = m_jumps.erase(it); } else ++it; diff --git a/src/context.hh b/src/context.hh index af184663..7ed1442c 100644 --- a/src/context.hh +++ b/src/context.hh @@ -50,6 +50,20 @@ private: bool m_condition; }; +struct JumpList +{ + void push(SelectionList jump); + void drop(); + const SelectionList& forward(); + const SelectionList& backward(const SelectionList& current); + void forget_buffer(Buffer& buffer); + +private: + using Contents = Vector; + Contents m_jumps; + Contents::iterator m_current = m_jumps.begin(); +}; + // A Context is used to access non singleton objects for various services // in commands. // @@ -113,12 +127,6 @@ public: StringView main_sel_register_value(StringView reg) const; - void push_jump(); - void drop_jump(); - const SelectionList& jump_forward(); - const SelectionList& jump_backward(); - void forget_jumps_to_buffer(Buffer& buffer); - const String& name() const { return m_name; } void set_name(String name) { m_name = std::move(name); } @@ -136,6 +144,9 @@ public: Flags flags() const { return m_flags; } + JumpList& jump_list() { return m_jump_list; } + void push_jump() { m_jump_list.push(selections()); } + private: void begin_edition(); void end_edition(); @@ -153,9 +164,7 @@ private: String m_name; - using JumpList = Vector; - JumpList m_jump_list; - JumpList::iterator m_current_jump = m_jump_list.begin(); + JumpList m_jump_list; NestedBool m_user_hooks_disabled; NestedBool m_keymaps_disabled; diff --git a/src/normal.cc b/src/normal.cc index 839093ac..84956e08 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1135,7 +1135,8 @@ template void jump(Context& context, NormalParams) { auto jump = (direction == Forward) ? - context.jump_forward() : context.jump_backward(); + context.jump_list().forward() : + context.jump_list().backward(context.selections()); Buffer* oldbuf = &context.buffer(); Buffer& buffer = const_cast(jump.buffer()); @@ -1153,7 +1154,7 @@ void push_selections(Context& context, NormalParams) void drop_jump(Context& context, NormalParams) { - context.drop_jump(); + context.jump_list().drop(); context.print_status({ "dropped last jump", get_face("Information") }); }