From df79d0c2453991c4762c6c6ffbccd714312c87f9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 8 Jun 2022 19:43:03 +1000 Subject: [PATCH] Distinguish between non-eol max column target and plain max column --- src/buffer.cc | 5 +++-- src/buffer.hh | 4 ++-- src/input_handler.cc | 2 +- src/normal.cc | 2 +- src/selection.hh | 1 + src/selectors.cc | 2 +- 6 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index b2552085..4d7d432d 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -155,14 +155,15 @@ BufferCoord Buffer::clamp(BufferCoord coord) const return coord; } -BufferCoord Buffer::offset_coord(BufferCoord coord, CharCount offset, ColumnCount, bool) const +BufferCoord Buffer::offset_coord(BufferCoord coord, CharCount offset, ColumnCount) const { return utf8::advance(iterator_at(coord), offset < 0 ? begin() : end()-1, offset).coord(); } -BufferCoordAndTarget Buffer::offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop, bool avoid_eol) const +BufferCoordAndTarget Buffer::offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop) const { const auto column = coord.target == -1 ? get_column(*this, tabstop, coord) : coord.target; + const bool avoid_eol = coord.target < max_column; const auto line = Kakoune::clamp(coord.line + offset, 0_line, line_count()-1); const auto max_column = get_column(*this, tabstop, {line, m_lines[line].length()-1}); const auto final_column = std::max(0_col, std::min(column, max_column - (avoid_eol ? 1 : 0))); diff --git a/src/buffer.hh b/src/buffer.hh index 7dd4bdad..ce336e96 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -188,8 +188,8 @@ public: // returns nearest valid coordinates from given ones BufferCoord clamp(BufferCoord coord) const; - BufferCoord offset_coord(BufferCoord coord, CharCount offset, ColumnCount, bool) const; - BufferCoordAndTarget offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop, bool avoid_eol) const; + BufferCoord offset_coord(BufferCoord coord, CharCount offset, ColumnCount) const; + BufferCoordAndTarget offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop) const; const String& name() const { return m_name; } const String& display_name() const { return m_display_name; } diff --git a/src/input_handler.cc b/src/input_handler.cc index 6cf175c2..9a622559 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -1445,7 +1445,7 @@ private: const ColumnCount tabstop = context().options()["tabstop"].get(); for (auto& sel : selections) { - auto cursor = context().buffer().offset_coord(sel.cursor(), offset, tabstop, false); + auto cursor = context().buffer().offset_coord(sel.cursor(), offset, tabstop); sel.anchor() = sel.cursor() = cursor; } selections.sort_and_merge_overlapping(); diff --git a/src/normal.cc b/src/normal.cc index 17fd4833..7694f513 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -2076,7 +2076,7 @@ void move_cursor(Context& context, NormalParams params) auto& selections = context.selections(); for (auto& sel : selections) { - auto cursor = context.buffer().offset_coord(sel.cursor(), offset, tabstop, true); + auto cursor = context.buffer().offset_coord(sel.cursor(), offset, tabstop); sel.anchor() = mode == SelectMode::Extend ? sel.anchor() : cursor; sel.cursor() = cursor; } diff --git a/src/selection.hh b/src/selection.hh index 43ea412b..299406be 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -11,6 +11,7 @@ namespace Kakoune using CaptureList = Vector; constexpr ColumnCount max_column{std::numeric_limits::max()}; +constexpr ColumnCount max_non_eol_column{max_column-1}; // A selection is a Selection, associated with a CaptureList struct Selection diff --git a/src/selectors.cc b/src/selectors.cc index 43819034..b4e57e36 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -184,7 +184,7 @@ select_to_line_end(const Context& context, const Selection& selection) buffer.iterator_at(line)).coord(); if (end < begin) // Do not go backward when cursor is on eol end = begin; - return Selection{only_move ? end : begin, {end, max_column}}; + return Selection{only_move ? end : begin, {end, max_non_eol_column}}; } template Optional select_to_line_end(const Context&, const Selection&); template Optional select_to_line_end(const Context&, const Selection&);