From 1990a764e3d2ffa77931068f876cd49f76bd43f9 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Thu, 9 Mar 2023 21:21:29 +0100 Subject: [PATCH] Make linewise bracketed paste match P behavior This is experimental. Testing will reveal if this is the desired behavior. --- src/input_handler.cc | 12 ++++++++---- src/normal.cc | 7 ------- src/normal.hh | 10 ++++++++++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/input_handler.cc b/src/input_handler.cc index a24ddfa2..877517e2 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -77,13 +77,17 @@ void InputMode::paste(StringView content) try { Buffer& buffer = context().buffer(); + const bool linewise = not content.empty() and content.back() == '\n'; ScopedEdition edition{context()}; ScopedSelectionEdition selection_edition{context()}; - context().selections().for_each([&buffer, content=std::move(content)] + context().selections().for_each([&buffer, content=std::move(content), linewise] (size_t index, Selection& sel) { - BufferRange range = buffer.insert(sel.min(), content); - sel.min() = range.begin; - sel.max() = range.end > range.begin ? buffer.char_prev(range.end) : range.begin; + auto& min = sel.min(); + auto& max = sel.max(); + BufferRange range = + buffer.insert(paste_pos(buffer, min, max, PasteMode::Insert, linewise), content); + min = range.begin; + max = range.end > range.begin ? buffer.char_prev(range.end) : range.begin; }, false); } catch (Kakoune::runtime_error& error) diff --git a/src/normal.cc b/src/normal.cc index 7dc14382..e3fdee16 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -657,13 +657,6 @@ void change(Context& context, NormalParams params) enter_insert_mode(context, params); } -enum class PasteMode -{ - Append, - Insert, - Replace -}; - BufferCoord paste_pos(Buffer& buffer, BufferCoord min, BufferCoord max, PasteMode mode, bool linewise) { switch (mode) diff --git a/src/normal.hh b/src/normal.hh index ad0f81be..080c3d01 100644 --- a/src/normal.hh +++ b/src/normal.hh @@ -10,6 +10,7 @@ namespace Kakoune { +class Buffer; class Context; struct no_selections_remaining : runtime_error @@ -40,6 +41,15 @@ struct KeyInfo String build_autoinfo_for_mapping(const Context& context, KeymapMode mode, ConstArrayView built_ins); +enum class PasteMode +{ + Append, + Insert, + Replace +}; + +BufferCoord paste_pos(Buffer& buffer, BufferCoord min, BufferCoord max, PasteMode mode, bool linewise); + } #endif // normal_hh_INCLUDED