diff options
| author | Jason Felice <jason.m.felice@gmail.com> | 2019-11-11 21:06:37 -0500 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2019-11-12 20:54:32 +1100 |
| commit | 5fae16faefe65464a37322390be3bb291cc3bafd (patch) | |
| tree | 996f8dcdd271cea453aeceb2dea9e4985e323195 /src | |
| parent | 6eb820dc54c96100909a179f2a58bbb9b00b51cd (diff) | |
Implement %val{selections_char_desc}
Fixes #3194
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cc | 8 | ||||
| -rw-r--r-- | src/selection.cc | 12 | ||||
| -rw-r--r-- | src/selection.hh | 23 |
3 files changed, 32 insertions, 11 deletions
diff --git a/src/main.cc b/src/main.cc index 5fc49b59..8ebd6f76 100644 --- a/src/main.cc +++ b/src/main.cc @@ -252,7 +252,11 @@ static const EnvVarDesc builtin_env_vars[] = { { }, { "selections_desc", false, [](StringView name, const Context& context, Quoting quoting) - { return selection_list_to_string(context.selections()); } + { return selection_list_to_string<false>(context.selections()); } + }, { + "selections_char_desc", false, + [](StringView name, const Context& context, Quoting quoting) + { return selection_list_to_string<true>(context.selections()); } }, { "selection_length", false, [](StringView name, const Context& context, Quoting quoting) -> String @@ -793,7 +797,7 @@ int run_server(StringView session, StringView server_init, kak_assert(local_client); const String client_name = local_client->context().name(); const String buffer_name = local_client->context().buffer().name(); - const String selections = selection_list_to_string(local_client->context().selections()); + const String selections = selection_list_to_string<false>(local_client->context().selections()); ClientManager::instance().remove_client(*local_client, true, 0); client_manager.clear_client_trash(); diff --git a/src/selection.cc b/src/selection.cc index a5b2c7b1..412d4f2d 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -480,13 +480,13 @@ String selection_to_string(const Selection& selection) cursor.line + 1, cursor.column + 1); } -String selection_list_to_string(const SelectionList& selections) +String selection_to_string_char(const Buffer& buffer, const Selection& selection) { - auto beg = &*selections.begin(), end = &*selections.end(); - auto main = beg + selections.main_index(); - using View = ConstArrayView<Selection>; - return join(concatenated(View{main, end}, View{beg, main}) | - transform(selection_to_string), ' ', false); + const auto& cursor = selection.cursor(); + const auto& anchor = selection.anchor(); + return format("{}.{},{}.{}", + anchor.line + 1, buffer[anchor.line].char_count_to(anchor.column) + 1, + cursor.line + 1, buffer[cursor.line].char_count_to(cursor.column) + 1); } Selection selection_from_string(StringView desc) diff --git a/src/selection.hh b/src/selection.hh index 5c47f878..568b438a 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -95,7 +95,6 @@ struct SelectionList SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp); void update(bool merge = true); - void check_invariant() const; const Selection& main() const { return (*this)[m_main]; } @@ -157,9 +156,27 @@ private: Vector<Selection> compute_modified_ranges(const Buffer& buffer, size_t timestamp); -String selection_to_string(const Selection& selection); -String selection_list_to_string(const SelectionList& selection); Selection selection_from_string(StringView desc); +String selection_to_string(const Selection& selection); +String selection_to_string_char(const Buffer& buffer, const Selection& selection); + +template<bool char_columns> +String selection_list_to_string(const SelectionList& selections) +{ + auto& buffer = selections.buffer(); + kak_assert(selections.timestamp() == buffer.timestamp()); + + auto to_string = [&](const Selection& selection) { + return char_columns ? selection_to_string_char(buffer, selection) + : selection_to_string(selection); + }; + + auto beg = &*selections.begin(), end = &*selections.end(); + auto main = beg + selections.main_index(); + using View = ConstArrayView<Selection>; + return join(concatenated(View{main, end}, View{beg, main}) | + transform(to_string), ' ', false); +} template<class StringArray> SelectionList selection_list_from_strings(Buffer& buffer, StringArray&& descs, size_t timestamp, size_t main) |
