From 124a5d49059c1eb8ade653ef35ca9595041fc249 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 3 Jun 2018 21:08:45 +1000 Subject: Tolerate restoring invalid coordinates from register Clamp those selection after updating them to the current timestamp Fixes #2078 --- src/normal.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/normal.cc') diff --git a/src/normal.cc b/src/normal.cc index e66045a3..fb295f4d 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1734,11 +1734,22 @@ SelectionList read_selections_from_register(char reg, Context& context) const auto desc = content[0] | split('@') | static_gather(); Buffer& buffer = BufferManager::instance().get_buffer(desc[0]); const size_t timestamp = str_to_int(desc[1]); - const size_t main = str_to_int(desc[2]); + size_t main = str_to_int(desc[2]); + + if (timestamp > buffer.timestamp()) + throw runtime_error{"register '{}' refers to an invalid timestamp"}; auto sels = content | skip(1) | transform(selection_from_string) | gather>(); + sort_selections(sels, main); + merge_overlapping_selections(sels, main); + if (timestamp < buffer.timestamp()) + update_selections(sels, main, buffer, timestamp); + else + clamp_selections(sels, buffer); - return {SelectionList::UnsortedTag{}, buffer, std::move(sels), timestamp, main}; + SelectionList res{buffer, std::move(sels)}; + res.set_main_index(main); + return res; } enum class CombineOp @@ -2012,9 +2023,7 @@ void move_cursor(Context& context, NormalParams params) sel.anchor() = mode == SelectMode::Extend ? sel.anchor() : cursor; sel.cursor() = cursor; } - selections.sort(); - - selections.merge_overlapping(); + selections.sort_and_merge_overlapping(); } void select_whole_buffer(Context& context, NormalParams) -- cgit v1.2.3