Insert: refactoring using IncrementalInserter

This commit is contained in:
Maxime Coste 2011-09-19 22:00:29 +00:00
parent 9da8fbe630
commit 13d14d4ff5

View File

@ -197,23 +197,53 @@ void print_status(const std::string& status)
addstr(status.c_str()); addstr(status.c_str());
} }
void do_insert(Window& window) struct scoped_status
{ {
print_status("-- INSERT --"); scoped_status(const std::string& status)
std::string inserted; {
WindowCoord pos = window.cursor_position(); print_status(status);
move(pos.line, pos.column); refresh();
refresh(); }
~scoped_status()
{
print_status("");
refresh();
}
};
void do_insert(Window& window, bool append = false)
{
scoped_status("-- INSERT --");
Kakoune::IncrementalInserter inserter(window, append);
while(true) while(true)
{ {
const WindowCoord& pos = inserter.cursors().back();
move(pos.line, pos.column);
char c = getch(); char c = getch();
if (c == 27) switch (c)
{
case 27:
return;
case 4:
inserter.move_cursor({0, -1});
break;
case 5:
inserter.move_cursor({0, 1});
break; break;
window.insert(std::string() + c); case 7:
inserter.erase();
break;
case '\r':
c = '\n';
default:
inserter.insert(std::string() + c);
}
draw_window(window); draw_window(window);
} }
print_status("");
} }
Window* current_window; Window* current_window;
@ -373,6 +403,7 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> keymap
{ 'd', [](Window& window, int count) { window.erase(); window.empty_selections(); } }, { 'd', [](Window& window, int count) { window.erase(); window.empty_selections(); } },
{ 'c', [](Window& window, int count) { window.erase(); do_insert(window); } }, { 'c', [](Window& window, int count) { window.erase(); do_insert(window); } },
{ 'i', [](Window& window, int count) { do_insert(window); } }, { 'i', [](Window& window, int count) { do_insert(window); } },
{ 'a', [](Window& window, int count) { do_insert(window, true); } },
{ ':', [](Window& window, int count) { do_command(); } }, { ':', [](Window& window, int count) { do_command(); } },
{ ' ', [](Window& window, int count) { window.empty_selections(); } }, { ' ', [](Window& window, int count) { window.empty_selections(); } },
{ 'w', [](Window& window, int count) { do { window.select(false, select_to_next_word); } while(--count > 0); } }, { 'w', [](Window& window, int count) { do { window.select(false, select_to_next_word); } while(--count > 0); } },