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.
This commit is contained in:
Maxime Coste 2018-06-03 14:30:24 +10:00
parent 8aba0b3cb4
commit d6c6ed9bbf
3 changed files with 16 additions and 24 deletions

View File

@ -1727,23 +1727,18 @@ SelectionList read_selections_from_register(char reg, Context& context)
auto content = RegisterManager::instance()[reg].get(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)); throw runtime_error(format("register '{}' does not contain a selections desc", reg));
auto splitted = content[0] | split<StringView>(' '); struct error : runtime_error { error(size_t) : runtime_error{"expected <buffer>@<timestamp>@main_index"} {} };
if (splitted.begin() == splitted.end()) const auto desc = content[0] | split<StringView>('@') | static_gather<error, 3>();
throw runtime_error(format("register '{}' does not contain a selections desc", reg)); 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]);
struct error : runtime_error { error(size_t) : runtime_error{"expected <buffer>@<timestamp>"} {} }; auto sels = content | skip(1) | transform(selection_from_string) | gather<Vector<Selection>>();
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]);
auto sels = splitted | skip(1) | transform(selection_from_string) | gather<Vector<Selection>>(); return {SelectionList::UnsortedTag{}, buffer, std::move(sels), timestamp, main};
if (sels.empty())
throw runtime_error(format("register '{}' contains an empty selection list", reg));
return {SelectionList::UnsortedTag{}, buffer, std::move(sels), timestamp};
} }
enum class CombineOp enum class CombineOp
@ -1857,10 +1852,11 @@ void save_selections(Context& context, NormalParams params)
const bool empty = content.size() == 1 and content[0].empty(); const bool empty = content.size() == 1 and content[0].empty();
auto save_to_reg = [reg](Context& context, const SelectionList& sels) { auto save_to_reg = [reg](Context& context, const SelectionList& sels) {
String desc = format("{}@{} {}", context.buffer().name(), auto& buffer = context.buffer();
context.buffer().timestamp(), auto descs = concatenated(ConstArrayView<String>{format("{}@{}@{}", buffer.name(), buffer.timestamp(), sels.main_index())},
selection_list_to_string(sels)); sels | transform(selection_to_string)) | gather<Vector<String>>();
RegisterManager::instance()[reg].set(context, desc); RegisterManager::instance()[reg].set(context, descs);
context.print_status({format("{} {} selections to register '{}'", context.print_status({format("{} {} selections to register '{}'",
combine ? "Combined" : "Saved", sels.size(), reg), combine ? "Combined" : "Saved", sels.size(), reg),
context.faces()["Information"]}); context.faces()["Information"]});

View File

@ -27,10 +27,7 @@ SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list, size_t time
SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list) SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list)
: SelectionList(buffer, std::move(list), buffer.timestamp()) {} : SelectionList(buffer, std::move(list), buffer.timestamp()) {}
SelectionList::SelectionList(SelectionList::UnsortedTag, Buffer& buffer, Vector<Selection> list) SelectionList::SelectionList(SelectionList::UnsortedTag, Buffer& buffer, Vector<Selection> list, size_t timestamp, size_t main)
: SelectionList(UnsortedTag{}, buffer, std::move(list), buffer.timestamp()) {}
SelectionList::SelectionList(SelectionList::UnsortedTag, Buffer& buffer, Vector<Selection> list, size_t timestamp)
: m_buffer(&buffer), m_selections(std::move(list)), m_timestamp(timestamp) : m_buffer(&buffer), m_selections(std::move(list)), m_timestamp(timestamp)
{ {
sort_and_merge_overlapping(); 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; }) auto sels = descs | transform([&](auto&& d) { auto s = selection_from_string(d); clamp(s, buffer); return s; })
| gather<Vector<Selection>>(); | gather<Vector<Selection>>();
return {SelectionList::UnsortedTag{}, buffer, std::move(sels)}; return {SelectionList::UnsortedTag{}, buffer, std::move(sels), buffer.timestamp(), 0};
} }
} }

View File

@ -88,8 +88,7 @@ struct SelectionList
SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp); SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp);
struct UnsortedTag {}; struct UnsortedTag {};
SelectionList(UnsortedTag, Buffer& buffer, Vector<Selection> s); SelectionList(UnsortedTag, Buffer& buffer, Vector<Selection> s, size_t timestamp, size_t main);
SelectionList(UnsortedTag, Buffer& buffer, Vector<Selection> s, size_t timestamp);
void update(); void update();