Fix utf8 handling in Editor::insert when replacing

This commit is contained in:
Maxime Coste 2013-02-26 14:06:25 +01:00
parent 7865223587
commit c343407465

View File

@ -75,18 +75,19 @@ static BufferIterator prepare_insert(Buffer& buffer, const Selection& sel,
return BufferIterator{}; return BufferIterator{};
} }
void Editor::insert(const String& string, InsertMode mode) void Editor::insert(const String& str, InsertMode mode)
{ {
scoped_edition edition(*this); scoped_edition edition(*this);
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
BufferIterator pos = prepare_insert(*m_buffer, sel, mode); BufferIterator pos = prepare_insert(*m_buffer, sel, mode);
m_buffer->insert(pos, string); m_buffer->insert(pos, str);
if (mode == InsertMode::Replace) if (mode == InsertMode::Replace)
{ {
sel.first() = pos; sel.first() = pos;
sel.last() = pos + (string.empty() ? 0 : string.length() - 1 ); sel.last() = str.empty() ? pos : utf8::character_start(pos + str.length() - 1);
sel.check_invariant();
} }
sel.avoid_eol(); sel.avoid_eol();
} }
@ -107,7 +108,8 @@ void Editor::insert(const memoryview<String>& strings, InsertMode mode)
if (mode == InsertMode::Replace) if (mode == InsertMode::Replace)
{ {
sel.first() = pos; sel.first() = pos;
sel.last() = pos + (str.empty() ? 0 : str.length() - 1); sel.last() = str.empty() ? pos : utf8::character_start(pos + str.length() - 1);
sel.check_invariant();
} }
sel.avoid_eol(); sel.avoid_eol();
} }