diff options
| author | Maxime Coste <mawww@kakoune.org> | 2018-06-03 14:30:24 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2018-07-05 07:54:28 +1000 |
| commit | d6c6ed9bbf9185ad80f640078202c80cf1b2ae4e (patch) | |
| tree | 405dcc2b1d9130f27d063486b02042722abb99c9 /src | |
| parent | 8aba0b3cb4e5fe758328eba9eeba0401b4e77e52 (diff) | |
Store each selection as a separate element in a register
It makes more sense to use the list nature of the register to store
the selections instead of storing them as a single string separated
by spaces.
Diffstat (limited to 'src')
| -rw-r--r-- | src/normal.cc | 30 | ||||
| -rw-r--r-- | src/selection.cc | 7 | ||||
| -rw-r--r-- | src/selection.hh | 3 |
3 files changed, 16 insertions, 24 deletions
diff --git a/src/normal.cc b/src/normal.cc index eeac76f2..e66045a3 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1727,23 +1727,18 @@ SelectionList read_selections_from_register(char reg, Context& context) auto content = RegisterManager::instance()[reg].get(context); - if (content.size() != 1) + if (content.size() < 2) throw runtime_error(format("register '{}' does not contain a selections desc", reg)); - auto splitted = content[0] | split<StringView>(' '); - if (splitted.begin() == splitted.end()) - throw runtime_error(format("register '{}' does not contain a selections desc", reg)); - - struct error : runtime_error { error(size_t) : runtime_error{"expected <buffer>@<timestamp>"} {} }; - const auto buffer_desc = *splitted.begin() | split<StringView>('@') | static_gather<error, 2>(); - Buffer& buffer = BufferManager::instance().get_buffer(buffer_desc[0]); - const size_t timestamp = str_to_int(buffer_desc[1]); + struct error : runtime_error { error(size_t) : runtime_error{"expected <buffer>@<timestamp>@main_index"} {} }; + const auto desc = content[0] | split<StringView>('@') | static_gather<error, 3>(); + 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]); - auto sels = splitted | skip(1) | transform(selection_from_string) | gather<Vector<Selection>>(); - if (sels.empty()) - throw runtime_error(format("register '{}' contains an empty selection list", reg)); + auto sels = content | skip(1) | transform(selection_from_string) | gather<Vector<Selection>>(); - return {SelectionList::UnsortedTag{}, buffer, std::move(sels), timestamp}; + return {SelectionList::UnsortedTag{}, buffer, std::move(sels), timestamp, main}; } enum class CombineOp @@ -1857,10 +1852,11 @@ void save_selections(Context& context, NormalParams params) const bool empty = content.size() == 1 and content[0].empty(); auto save_to_reg = [reg](Context& context, const SelectionList& sels) { - String desc = format("{}@{} {}", context.buffer().name(), - context.buffer().timestamp(), - selection_list_to_string(sels)); - RegisterManager::instance()[reg].set(context, desc); + auto& buffer = context.buffer(); + auto descs = concatenated(ConstArrayView<String>{format("{}@{}@{}", buffer.name(), buffer.timestamp(), sels.main_index())}, + sels | transform(selection_to_string)) | gather<Vector<String>>(); + RegisterManager::instance()[reg].set(context, descs); + context.print_status({format("{} {} selections to register '{}'", combine ? "Combined" : "Saved", sels.size(), reg), context.faces()["Information"]}); diff --git a/src/selection.cc b/src/selection.cc index 1cd8641c..02358fea 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -27,10 +27,7 @@ SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list, size_t time SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list) : SelectionList(buffer, std::move(list), buffer.timestamp()) {} -SelectionList::SelectionList(SelectionList::UnsortedTag, Buffer& buffer, Vector<Selection> list) - : SelectionList(UnsortedTag{}, buffer, std::move(list), buffer.timestamp()) {} - -SelectionList::SelectionList(SelectionList::UnsortedTag, Buffer& buffer, Vector<Selection> list, size_t timestamp) +SelectionList::SelectionList(SelectionList::UnsortedTag, Buffer& buffer, Vector<Selection> list, size_t timestamp, size_t main) : m_buffer(&buffer), m_selections(std::move(list)), m_timestamp(timestamp) { sort_and_merge_overlapping(); @@ -509,7 +506,7 @@ SelectionList selection_list_from_string(Buffer& buffer, ConstArrayView<String> auto sels = descs | transform([&](auto&& d) { auto s = selection_from_string(d); clamp(s, buffer); return s; }) | gather<Vector<Selection>>(); - return {SelectionList::UnsortedTag{}, buffer, std::move(sels)}; + return {SelectionList::UnsortedTag{}, buffer, std::move(sels), buffer.timestamp(), 0}; } } diff --git a/src/selection.hh b/src/selection.hh index 33df78ce..1ab93aa5 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -88,8 +88,7 @@ struct SelectionList SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp); struct UnsortedTag {}; - SelectionList(UnsortedTag, Buffer& buffer, Vector<Selection> s); - SelectionList(UnsortedTag, Buffer& buffer, Vector<Selection> s, size_t timestamp); + SelectionList(UnsortedTag, Buffer& buffer, Vector<Selection> s, size_t timestamp, size_t main); void update(); |
