From 968e573d803df664b4280ed30e22846a9b08963c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 1 Jan 2017 12:58:04 +0000 Subject: [PATCH] Slight code refactor for paste handling --- src/normal.cc | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index bb408a4d..1386567d 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -550,14 +550,15 @@ void change(Context& context, NormalParams params) enter_insert_mode(context, params); } -constexpr InsertMode adapt_for_linewise(InsertMode mode) +InsertMode adapt_for_linewise(InsertMode mode) { - return ((mode == InsertMode::Append) ? - InsertMode::InsertAtNextLineBegin : - ((mode == InsertMode::Insert) ? - InsertMode::InsertAtLineBegin : - ((mode == InsertMode::Replace) ? - InsertMode::Replace : InsertMode::Insert))); + switch (mode) + { + case InsertMode::Append: return InsertMode::InsertAtNextLineBegin; + case InsertMode::Insert: return InsertMode::InsertAtLineBegin; + case InsertMode::Replace: return InsertMode::Replace; + default: return InsertMode::Insert; + } } template @@ -565,15 +566,11 @@ void paste(Context& context, NormalParams params) { const char reg = params.reg ? params.reg : '"'; auto strings = RegisterManager::instance()[reg].values(context); - InsertMode effective_mode = mode; - for (auto& str : strings) - { - if (not str.empty() and str.back() == '\n') - { - effective_mode = adapt_for_linewise(mode); - break; - } - } + const bool linewise = contains_that(strings, [](StringView str) { + return not str.empty() and str.back() == '\n'; + }); + const auto effective_mode = linewise ? adapt_for_linewise(mode) : mode; + ScopedEdition edition(context); context.selections().insert(strings, effective_mode); }