Refactor InputMode::Insert::prepare

This commit is contained in:
Maxime Coste 2014-06-09 23:23:49 +01:00
parent f54f8818c6
commit 5245f0073c

View File

@ -801,49 +801,47 @@ private:
cursor = buffer.char_next(cursor); cursor = buffer.char_next(cursor);
} }
break; break;
case InsertMode::OpenLineBelow:
case InsertMode::AppendAtLineEnd: case InsertMode::AppendAtLineEnd:
for (auto& sel : selections) for (auto& sel : selections)
sel = ByteCoord{sel.max().line, buffer[sel.max().line].length() - 1}; sel = ByteCoord{sel.max().line, buffer[sel.max().line].length() - 1};
break; break;
case InsertMode::OpenLineBelow:
for (auto& sel : selections)
sel = ByteCoord{sel.max().line, buffer[sel.max().line].length() - 1};
insert('\n');
break;
case InsertMode::OpenLineAbove: case InsertMode::OpenLineAbove:
for (auto& sel : selections)
{
auto line = sel.min().line;
sel = line > 0 ? ByteCoord{line - 1, buffer[line-1].length() - 1}
: ByteCoord{0, 0};
}
insert('\n');
// fix case where we inserted at begining
for (auto& sel : selections)
{
if (sel.anchor() == buffer.char_next({0,0}))
sel = Selection{{0,0}};
}
break;
case InsertMode::InsertAtLineBegin: case InsertMode::InsertAtLineBegin:
for (auto& sel : selections) for (auto& sel : selections)
{ {
ByteCoord pos = sel.min().line; ByteCoord pos = sel.min().line;
if (mode == InsertMode::OpenLineAbove)
pos = buffer.char_prev(pos);
else
{
auto pos_non_blank = buffer.iterator_at(pos); auto pos_non_blank = buffer.iterator_at(pos);
while (*pos_non_blank == ' ' or *pos_non_blank == '\t') while (*pos_non_blank == ' ' or *pos_non_blank == '\t')
++pos_non_blank; ++pos_non_blank;
if (*pos_non_blank != '\n') if (*pos_non_blank != '\n')
pos = pos_non_blank.coord(); pos = pos_non_blank.coord();
}
sel = pos; sel = pos;
} }
break; break;
case InsertMode::InsertAtNextLineBegin: case InsertMode::InsertAtNextLineBegin:
case InsertMode::InsertCursor: case InsertMode::InsertCursor:
kak_assert(false); // not implemented kak_assert(false); // invalid for interactive insert
break; break;
} }
if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove)
{
insert('\n');
if (mode == InsertMode::OpenLineAbove)
{
for (auto& sel : selections)
{
// special case, the --first line above did nothing, so we need to compensate now
if (sel.anchor() == buffer.char_next({0,0}))
sel = Selection{{0,0}};
}
}
}
selections.sort_and_merge_overlapping(); selections.sort_and_merge_overlapping();
selections.check_invariant(); selections.check_invariant();
buffer.check_invariant(); buffer.check_invariant();