From cdb2c766a5480cf2352e7db9b57cc9fcb9a81df2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 1 Jan 2017 17:31:47 +0000 Subject: [PATCH] Refactor SelectionList::insert a bit --- src/normal.cc | 9 +++++---- src/selection.cc | 44 ++++++++++++++++++++------------------------ src/selection.hh | 2 +- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index 1386567d..835a9d32 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -594,21 +594,22 @@ void paste_all(Context& context, NormalParams params) offsets.push_back(all.length()); } + Vector insert_pos; auto& selections = context.selections(); { ScopedEdition edition(context); - selections.insert(all, effective_mode, true); + selections.insert(all, effective_mode, &insert_pos); } const Buffer& buffer = context.buffer(); Vector result; - for (auto& selection : selections) + for (auto& ins_pos : insert_pos) { ByteCount pos = 0; for (auto offset : offsets) { - result.push_back({ buffer.advance(selection.min(), pos), - buffer.advance(selection.min(), offset-1) }); + result.push_back({ buffer.advance(ins_pos, pos), + buffer.advance(ins_pos, offset-1) }); pos = offset; } } diff --git a/src/selection.cc b/src/selection.cc index 6b74a6ab..48446253 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -513,7 +513,7 @@ BufferCoord prepare_insert(Buffer& buffer, const Selection& sel, InsertMode mode } void SelectionList::insert(ConstArrayView strings, InsertMode mode, - bool select_inserted) + Vector* out_insert_pos) { if (strings.empty()) return; @@ -525,43 +525,39 @@ void SelectionList::insert(ConstArrayView strings, InsertMode mode, auto& sel = m_selections[index]; sel.anchor() = changes_tracker.get_new_coord(sel.anchor()); - kak_assert(m_buffer->is_valid(sel.anchor())); sel.cursor() = changes_tracker.get_new_coord(sel.cursor()); - kak_assert(m_buffer->is_valid(sel.cursor())); - - auto pos = prepare_insert(*m_buffer, sel, mode); - changes_tracker.update(*m_buffer, m_timestamp); + kak_assert(m_buffer->is_valid(sel.anchor()) and + m_buffer->is_valid(sel.cursor())); const String& str = strings[std::min(index, strings.size()-1)]; - if (mode == InsertMode::Replace) - pos = replace(*m_buffer, sel, str); - else - pos = m_buffer->insert(pos, str); + const auto pos = (mode == InsertMode::Replace) ? + replace(*m_buffer, sel, str) + : m_buffer->insert(prepare_insert(*m_buffer, sel, mode), str); + + if (out_insert_pos) + out_insert_pos->push_back(pos); - auto& change = m_buffer->changes_since(m_timestamp).back(); changes_tracker.update(*m_buffer, m_timestamp); m_timestamp = m_buffer->timestamp(); - if (select_inserted or mode == InsertMode::Replace) + if (mode == InsertMode::Replace) { if (str.empty()) - { sel.anchor() = sel.cursor() = m_buffer->clamp(pos); - continue; + else + { + // we want min and max from *before* we do any change + auto& min = sel.min(); + auto& max = sel.max(); + auto& change = m_buffer->changes_since(0).back(); + min = change.begin; + max = m_buffer->char_prev(change.end); } - - // we want min and max from *before* we do any change - auto& min = sel.min(); - auto& max = sel.max(); - min = change.begin; - max = m_buffer->char_prev(change.end); } - else + else if (not str.empty()) { - if (str.empty()) - continue; - + auto& change = m_buffer->changes_since(0).back(); sel.anchor() = m_buffer->clamp(update_insert(sel.anchor(), change.begin, change.end)); sel.cursor() = m_buffer->clamp(update_insert(sel.cursor(), change.begin, change.end)); } diff --git a/src/selection.hh b/src/selection.hh index 7428fb39..92eecea6 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -135,7 +135,7 @@ struct SelectionList void update_timestamp() { m_timestamp = m_buffer->timestamp(); } void insert(ConstArrayView strings, InsertMode mode, - bool select_inserted = false); + Vector* out_insert_pos = nullptr); void erase(); private: