Buffer insert and erase takes coord rather than iterators

This commit is contained in:
Maxime Coste 2013-05-23 14:17:25 +02:00
parent 2902cc3275
commit 7f17644a6b
3 changed files with 20 additions and 26 deletions

View File

@ -571,7 +571,7 @@ void Buffer::apply_modification(const Modification& modification)
{
ByteCount count = content.length();
BufferCoord end = advance(coord, count);
kak_assert(string({*this, coord}, {*this, end}) == content);
kak_assert(string(coord, end) == content);
do_erase(coord, end);
break;
}
@ -580,31 +580,32 @@ void Buffer::apply_modification(const Modification& modification)
}
}
void Buffer::insert(BufferIterator pos, String content)
void Buffer::insert(BufferCoord pos, String content)
{
kak_assert(is_valid(pos));
if (content.empty())
return;
if (pos.is_end() and content.back() != '\n')
if (is_end(pos) and content.back() != '\n')
content += '\n';
if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Insert, pos.coord(), content);
do_insert(pos.coord(), content);
m_current_undo_group.emplace_back(Modification::Insert, pos, content);
do_insert(pos, content);
}
void Buffer::erase(BufferIterator begin, BufferIterator end)
void Buffer::erase(BufferCoord begin, BufferCoord end)
{
if (end.is_end() and (begin.column() != 0 or begin.is_begin()))
--end;
if (is_end(end) and (begin.column != 0 or begin == BufferCoord{0,0}))
end = { line_count() - 1, m_lines.back().length() - 1};
if (begin == end)
return;
if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Erase, begin.coord(),
m_current_undo_group.emplace_back(Modification::Erase, begin,
string(begin, end));
do_erase(begin.coord(), end.coord());
do_erase(begin, end);
}
bool Buffer::is_modified() const

View File

@ -112,8 +112,8 @@ public:
bool set_name(String name);
void insert(BufferIterator pos, String content);
void erase(BufferIterator begin, BufferIterator end);
void insert(BufferCoord pos, String content);
void erase(BufferCoord begin, BufferCoord end);
size_t timestamp() const { return m_timestamp; }
@ -123,10 +123,6 @@ public:
String string(const BufferCoord& begin,
const BufferCoord& end) const;
String string(const BufferIterator& begin,
const BufferIterator& end) const
{ return string(begin.coord(), end.coord()); }
ByteCount offset(const BufferCoord& c) const;
ByteCount distance(const BufferCoord& begin, const BufferCoord& end) const;
BufferCoord advance(BufferCoord coord, ByteCount count) const;

View File

@ -38,7 +38,7 @@ void Editor::erase()
scoped_edition edition(*this);
for (auto& sel : m_selections)
{
m_buffer->erase(sel.begin(), sel.end());
m_buffer->erase(sel.begin().coord(), sel.end().coord());
avoid_eol(sel);
}
}
@ -53,7 +53,7 @@ static BufferIterator prepare_insert(Buffer& buffer, const Selection& sel,
case InsertMode::Replace:
{
BufferIterator pos = sel.begin();
buffer.erase(sel.begin(), sel.end());
buffer.erase(sel.begin().coord(), sel.end().coord());
return pos;
}
case InsertMode::Append:
@ -72,17 +72,14 @@ static BufferIterator prepare_insert(Buffer& buffer, const Selection& sel,
case InsertMode::InsertAtNextLineBegin:
return buffer.iterator_at_line_end(sel.end()-1);
case InsertMode::OpenLineBelow:
{
LineCount line = (sel.end() - 1).line();
buffer.insert(buffer.iterator_at_line_end(line), "\n");
return buffer.iterator_at_line_begin(line + 1);
}
case InsertMode::OpenLineAbove:
{
auto pos = buffer.iterator_at_line_begin(sel.begin());
buffer.insert(pos, "\n");
return pos;
auto line = mode == InsertMode::OpenLineAbove ?
sel.begin().line() : (sel.end() - 1).line() + 1;
buffer.insert(line, "\n");
return {buffer, line};
}
}
kak_assert(false);
return BufferIterator{};