use SelectionsAndCaptures to store jumps

This commit is contained in:
Maxime Coste 2012-11-12 20:41:03 +01:00
parent 14f980e4e0
commit 9c368c85d6
2 changed files with 26 additions and 34 deletions

View File

@ -100,23 +100,25 @@ struct Context
using Insertion = std::pair<InsertMode, std::vector<Key>>; using Insertion = std::pair<InsertMode, std::vector<Key>>;
Insertion& last_insert() { return m_last_insert; } Insertion& last_insert() { return m_last_insert; }
using Jump = std::pair<safe_ptr<Buffer>, BufferCoord>;
void push_jump() void push_jump()
{ {
Jump jump{safe_ptr<Buffer>{&buffer()}, const SelectionAndCaptures& jump = editor().selections().back();
editor().selections().back().last().coord()};
if (m_current_jump != m_jump_list.end()) if (m_current_jump != m_jump_list.end())
{ {
m_jump_list.erase(m_current_jump+1, m_jump_list.end()); auto begin = m_current_jump;
if (*m_current_jump == jump) // when jump overlaps with m_current_jump, we replace m_current_jump
return; // 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_jump_list.push_back(jump);
m_current_jump = m_jump_list.end(); m_current_jump = m_jump_list.end();
} }
Jump jump_forward() SelectionAndCaptures jump_forward()
{ {
if (m_current_jump != m_jump_list.end() and if (m_current_jump != m_jump_list.end() and
m_current_jump + 1 != m_jump_list.end()) m_current_jump + 1 != m_jump_list.end())
@ -124,7 +126,7 @@ struct Context
throw runtime_error("no next jump"); throw runtime_error("no next jump");
} }
Jump jump_backward() SelectionAndCaptures jump_backward()
{ {
if (m_current_jump != m_jump_list.begin()) 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();) 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) if (it < m_current_jump)
--m_current_jump; --m_current_jump;
else if (it == 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); it = m_jump_list.erase(it);
} }
@ -165,9 +167,8 @@ private:
Insertion m_last_insert = {InsertMode::Insert, {}}; Insertion m_last_insert = {InsertMode::Insert, {}};
int m_numeric_param = 0; int m_numeric_param = 0;
using JumpList = std::vector<Jump>; SelectionAndCapturesList m_jump_list;
JumpList m_jump_list; SelectionAndCapturesList::iterator m_current_jump = m_jump_list.begin();
JumpList::iterator m_current_jump = m_jump_list.begin();
}; };
} }

View File

@ -332,30 +332,21 @@ void select_to_next_char(Context& context)
}); });
} }
void jump_forward(Context& context) enum class JumpDirection { Forward, Backward };
template<JumpDirection direction>
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); Buffer& buffer = const_cast<Buffer&>(jump.first().buffer());
if (jump.first != &context.buffer()) BufferManager::instance().set_last_used_buffer(buffer);
if (&buffer != &context.buffer())
{ {
auto& manager = ClientManager::instance(); 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)); context.editor().select(SelectionAndCapturesList{ jump });
}
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));
} }
String runtime_directory() String runtime_directory()
@ -478,8 +469,8 @@ std::unordered_map<Key, std::function<void (Context& context)>> keymap =
{ { Key::Modifiers::None, Key::PageUp }, do_scroll<Key::PageUp> }, { { Key::Modifiers::None, Key::PageUp }, do_scroll<Key::PageUp> },
{ { Key::Modifiers::None, Key::PageDown }, do_scroll<Key::PageDown> }, { { Key::Modifiers::None, Key::PageDown }, do_scroll<Key::PageDown> },
{ { Key::Modifiers::Control, 'i' }, jump_forward }, { { Key::Modifiers::Control, 'i' }, jump<JumpDirection::Forward> },
{ { Key::Modifiers::Control, 'o' }, jump_backward }, { { Key::Modifiers::Control, 'o' }, jump<JumpDirection::Backward> },
}; };
} }