Add a repeat last insert command, bound to .

This commit is contained in:
Maxime Coste 2011-12-05 14:28:45 +00:00
parent d1cc5da8a6
commit 23eafd4504

View File

@ -272,47 +272,77 @@ void print_status(const std::string& status)
addstr(status.c_str()); addstr(status.c_str());
} }
struct InsertSequence
{
IncrementalInserter::Mode mode;
std::string keys;
InsertSequence() : mode(IncrementalInserter::Mode::Insert) {}
};
InsertSequence last_insert_sequence;
bool insert_char(IncrementalInserter& inserter, char c)
{
switch (c)
{
case 27:
return false;
case 2:
c = getch();
if (c >= '0' and c <= '9')
inserter.insert_capture(c - '0');
break;
case 4:
inserter.move_cursor({0, -1});
break;
case 5:
inserter.move_cursor({0, 1});
break;
case 7:
inserter.erase();
break;
case '\r':
c = '\n';
default:
inserter.insert(std::string() + c);
}
return true;
}
void do_insert(Window& window, IncrementalInserter::Mode mode) void do_insert(Window& window, IncrementalInserter::Mode mode)
{ {
Kakoune::IncrementalInserter inserter(window, mode); last_insert_sequence.mode = mode;
last_insert_sequence.keys.clear();
IncrementalInserter inserter(window, mode);
draw_window(window); draw_window(window);
while(true) while(true)
{ {
const DisplayCoord& pos = window.cursor_position();
move(pos.line, pos.column);
char c = getch(); char c = getch();
switch (c)
{ if (not insert_char(inserter, c))
case 27:
return; return;
case 2: last_insert_sequence.keys += c;
c = getch();
if (c >= '0' and c <= '9')
inserter.insert_capture(c - '0');
break;
case 4:
inserter.move_cursor({0, -1});
break;
case 5:
inserter.move_cursor({0, 1});
break;
case 7:
inserter.erase();
break;
case '\r':
c = '\n';
default:
inserter.insert(std::string() + c);
}
draw_window(window); draw_window(window);
} }
} }
void do_repeat_insert(Window& window, int count)
{
IncrementalInserter inserter(window, last_insert_sequence.mode);
for (char c : last_insert_sequence.keys)
{
insert_char(inserter, c);
}
draw_window(window);
}
template<bool append> template<bool append>
void do_go(Window& window, int count) void do_go(Window& window, int count)
{ {
@ -651,6 +681,9 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> keymap
{ 's', do_select_regex }, { 's', do_select_regex },
{ '.', do_repeat_insert },
{ '%', [](Window& window, int count) { window.select([](const BufferIterator& cursor) { '%', [](Window& window, int count) { window.select([](const BufferIterator& cursor)
{ return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } }, { return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } },