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,15 +41,20 @@ struct InsertSequence
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)
{
while (true)
{
Key key = get_key();
switch (key.modifiers)
{
case Key::Modifiers::None:
switch (key.key)
{
case 27:
return false;
return;
default:
inserter.insert(String() + key.key);
}
@ -60,7 +65,6 @@ bool insert_char(IncrementalInserter& inserter, const Key& key)
case 'r':
{
Key next_key = get_key();
last_insert_sequence.keys.push_back(next_key);
if (next_key.modifiers == Key::Modifiers::None)
inserter.insert(RegisterManager::instance()[next_key.key]);
break;
@ -82,10 +86,9 @@ bool insert_char(IncrementalInserter& inserter, const Key& key)
break;
}
break;
default:
break;
}
return true;
redraw();
}
}
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();
IncrementalInserter inserter(editor, mode);
draw_editor_ifn(editor);
while(true)
{
Key key = get_key();
if (not insert_char(inserter, key))
break;
insert_sequence(inserter,
[&]() { Key key = get_key();
last_insert_sequence.keys.push_back(key);
draw_editor_ifn(editor);
}
return key; },
[&]() { draw_editor_ifn(editor); });
}
void do_repeat_insert(Editor& editor, int count)
{
IncrementalInserter inserter(editor, last_insert_sequence.mode);
for (const Key& key : last_insert_sequence.keys)
{
insert_char(inserter, key);
}
draw_editor_ifn(editor);
}
if (last_insert_sequence.keys.empty())
return;
IncrementalInserter inserter(editor, last_insert_sequence.mode);
size_t index = 0;
insert_sequence(inserter,
[&]() { return last_insert_sequence.keys[index++]; },
[](){});
}
template<bool append>
void do_go(Editor& editor, int count)