From 7f17644a6bfcaee2fd6c5a013da70d7c018dced3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 23 May 2013 14:17:25 +0200 Subject: [PATCH] Buffer insert and erase takes coord rather than iterators --- src/buffer.cc | 21 +++++++++++---------- src/buffer.hh | 8 ++------ src/editor.cc | 17 +++++++---------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 5e958cb8..7a654a1d 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -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 diff --git a/src/buffer.hh b/src/buffer.hh index 612bf299..4b8a3f43 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -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; diff --git a/src/editor.cc b/src/editor.cc index 74fab23f..5e7115be 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -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{};