diff --git a/src/buffer.cc b/src/buffer.cc index 76a2009d..6bf82020 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -55,9 +55,10 @@ Buffer::~Buffer() assert(m_change_listeners.empty()); } -BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const +BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column, + bool avoid_eol) const { - return BufferIterator(*this, clamp(line_and_column)); + return BufferIterator(*this, clamp(line_and_column, avoid_eol)); } BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const @@ -78,14 +79,15 @@ BufferSize Buffer::line_length(BufferPos line) const return end - m_lines[line].start; } -BufferCoord Buffer::clamp(const BufferCoord& line_and_column) const +BufferCoord Buffer::clamp(const BufferCoord& line_and_column, + bool avoid_eol) const { if (m_lines.empty()) return BufferCoord(); BufferCoord result(line_and_column.line, line_and_column.column); result.line = Kakoune::clamp(0, m_lines.size() - 1, result.line); - int max_col = std::max(0, line_length(result.line) - 2); + int max_col = std::max(0, line_length(result.line) - (avoid_eol ? 2 : 1)); result.column = Kakoune::clamp(0, max_col, result.column); return result; } diff --git a/src/buffer.hh b/src/buffer.hh index b048dbe5..a0d19836 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -130,11 +130,16 @@ public: BufferSize character_count() const; BufferSize line_count() const; - BufferIterator iterator_at(const BufferCoord& line_and_column) const; + // returns an iterator at given coordinates. line_and_column is + // clamped according to avoid_eol. + BufferIterator iterator_at(const BufferCoord& line_and_column, + bool avoid_eol = false) const; BufferCoord line_and_column_at(const BufferIterator& iterator) const; // returns nearest valid coordinates from given ones - BufferCoord clamp(const BufferCoord& line_and_column) const; + // if avoid_eol, clamp to character before eol if line is not empty + BufferCoord clamp(const BufferCoord& line_and_column, + bool avoid_eol = false) const; const String& name() const { return m_name; } diff --git a/src/editor.cc b/src/editor.cc index 817ce8e7..b262698b 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -113,7 +113,7 @@ void Editor::move_selections(const BufferCoord& offset, bool append) for (auto& sel : m_selections.back()) { BufferCoord pos = m_buffer.line_and_column_at(sel.last()); - BufferIterator last = m_buffer.iterator_at(pos + BufferCoord(offset)); + BufferIterator last = m_buffer.iterator_at(pos + offset, true); sel = Selection(append ? sel.first() : last, last); } }