refactor insertion and insertion replay functions

This commit is contained in:
Maxime Coste 2012-07-27 10:22:33 +02:00
parent 0e9b0c8b3a
commit 6bd67c01cc

View File

@ -41,51 +41,54 @@ struct InsertSequence
InsertSequence last_insert_sequence; InsertSequence last_insert_sequence;
bool insert_char(IncrementalInserter& inserter, const Key& key) template<typename GetKey, typename Redraw>
void insert_sequence(IncrementalInserter& inserter,
GetKey get_key, Redraw redraw)
{ {
switch (key.modifiers) while (true)
{ {
case Key::Modifiers::None: Key key = get_key();
switch (key.key) switch (key.modifiers)
{ {
case 27: case Key::Modifiers::None:
return false; switch (key.key)
default: {
inserter.insert(String() + key.key); case 27:
} return;
break; default:
case Key::Modifiers::Control: inserter.insert(String() + key.key);
switch (key.key) }
{ break;
case 'r': case Key::Modifiers::Control:
{ switch (key.key)
Key next_key = get_key(); {
last_insert_sequence.keys.push_back(next_key); case 'r':
if (next_key.modifiers == Key::Modifiers::None) {
inserter.insert(RegisterManager::instance()[next_key.key]); Key next_key = get_key();
if (next_key.modifiers == Key::Modifiers::None)
inserter.insert(RegisterManager::instance()[next_key.key]);
break;
}
case 'm':
inserter.insert(String() + '\n');
break;
case 'i':
inserter.insert(String() + '\t');
break;
case 'd':
inserter.move_cursors({0, -1});
break;
case 'e':
inserter.move_cursors({0, 1});
break;
case 'g':
inserter.erase();
break;
}
break; break;
} }
case 'm': redraw();
inserter.insert(String() + '\n');
break;
case 'i':
inserter.insert(String() + '\t');
break;
case 'd':
inserter.move_cursors({0, -1});
break;
case 'e':
inserter.move_cursors({0, 1});
break;
case 'g':
inserter.erase();
break;
}
break;
default:
break;
} }
return true;
} }
void do_insert(Editor& editor, IncrementalInserter::Mode mode) void do_insert(Editor& editor, IncrementalInserter::Mode mode)
@ -94,28 +97,24 @@ void do_insert(Editor& editor, IncrementalInserter::Mode mode)
last_insert_sequence.keys.clear(); last_insert_sequence.keys.clear();
IncrementalInserter inserter(editor, mode); IncrementalInserter inserter(editor, mode);
draw_editor_ifn(editor); draw_editor_ifn(editor);
while(true) insert_sequence(inserter,
{ [&]() { Key key = get_key();
Key key = get_key(); last_insert_sequence.keys.push_back(key);
return key; },
if (not insert_char(inserter, key)) [&]() { draw_editor_ifn(editor); });
break;
last_insert_sequence.keys.push_back(key);
draw_editor_ifn(editor);
}
} }
void do_repeat_insert(Editor& editor, int count) void do_repeat_insert(Editor& editor, int count)
{ {
IncrementalInserter inserter(editor, last_insert_sequence.mode); if (last_insert_sequence.keys.empty())
for (const Key& key : last_insert_sequence.keys) return;
{
insert_char(inserter, key);
}
draw_editor_ifn(editor);
}
IncrementalInserter inserter(editor, last_insert_sequence.mode);
size_t index = 0;
insert_sequence(inserter,
[&]() { return last_insert_sequence.keys[index++]; },
[](){});
}
template<bool append> template<bool append>
void do_go(Editor& editor, int count) void do_go(Editor& editor, int count)