diff --git a/src/buffer.cc b/src/buffer.cc index 52621f3d4..3a0fe78eb 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -367,16 +367,17 @@ void Buffer::apply_modification(const Modification& modification) } } -void Buffer::insert(BufferIterator pos, const String& content) +void Buffer::insert(BufferIterator pos, String content) { if (content.empty()) return; - if (pos.is_end()) - --pos; + if (pos.is_end() and content.back() != '\n') + content += '\n'; - m_current_undo_group.emplace_back(Modification::Insert, pos, content); - do_insert(pos, content); + m_current_undo_group.emplace_back(Modification::Insert, pos, + std::move(content)); + do_insert(pos, m_current_undo_group.back().content); } void Buffer::erase(BufferIterator begin, BufferIterator end) diff --git a/src/buffer.hh b/src/buffer.hh index 9a5aea1f6..d75a37d53 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -111,7 +111,7 @@ public: Type type() const { return m_type; } - void insert(BufferIterator pos, const String& content); + void insert(BufferIterator pos, String content); void erase(BufferIterator begin, BufferIterator end); size_t timestamp() const { return m_timestamp; } diff --git a/src/commands.cc b/src/commands.cc index 07a62c693..8ab2810a8 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -251,7 +251,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context) ssize_t count = read(fd, data, 512); if (count > 0) { - buffer->insert(buffer->end(), String(data, data + count)); + buffer->insert(buffer->end()-1, String(data, data + count)); buffer->reset_undo_data(); context.draw_ifn(); } diff --git a/src/editor.cc b/src/editor.cc index 8775a0132..1dbd35580 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -353,8 +353,11 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode) last = first; break; } + if (first.is_end()) + --first; + if (last.is_end()) + --last; sel = Selection(first, last); - } if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove) { diff --git a/src/file.cc b/src/file.cc index 936b6fb31..69a7f5307 100644 --- a/src/file.cc +++ b/src/file.cc @@ -89,7 +89,6 @@ Buffer* create_buffer_from_file(const String& filename) Buffer* buffer = new Buffer(filename, Buffer::Type::File, ""); - String content; char buf[256]; bool crlf = false; @@ -118,7 +117,7 @@ Buffer* create_buffer_from_file(const String& filename) if (buf[pos] == '\r') crlf = true; - buffer->insert(buffer->end(), String(buf+start, buf+pos)); + buffer->insert(buffer->end()-1, String(buf+start, buf+pos)); start = pos+1; } ++pos; diff --git a/src/unit_tests.cc b/src/unit_tests.cc index b6b1a35ec..797f09465 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -27,6 +27,15 @@ void test_buffer() BufferIterator end = buffer.iterator_at({ 4, 5 }) + 1; String str = buffer.string(begin, end); assert(str == "youpi"); + + // check insert at end behaviour: auto add end of line if necessary + begin = buffer.end() - 1; + buffer.insert(buffer.end(), "tchou"); + assert(buffer.string(begin+1, buffer.end()) == "tchou\n"); + + begin = buffer.end() - 1; + buffer.insert(buffer.end(), "kanaky\n"); + assert(buffer.string(begin+1, buffer.end()) == "kanaky\n"); } void test_editor()