Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A 'c' column suffix selects by codepoints #3188

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/selection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -489,26 +489,41 @@ String selection_list_to_string(const SelectionList& selections)
transform(selection_to_string), ' ', false);
}

Selection selection_from_string(StringView desc)
static BufferCoord buffer_coord_from_string(const Buffer& buffer, StringView desc, size_t timestamp)
{
auto comma = find(desc, ',');
auto dot_anchor = find(StringView{desc.begin(), comma}, '.');
auto dot_cursor = find(StringView{comma, desc.end()}, '.');

if (comma == desc.end() or dot_anchor == comma or dot_cursor == desc.end())
throw runtime_error(format("'{}' does not follow <line>.<column>,<line>.<column> format", desc));

BufferCoord anchor{str_to_int({desc.begin(), dot_anchor}) - 1,
str_to_int({dot_anchor+1, comma}) - 1};
auto dot = find(desc, '.');
if (dot == desc.end())
throw runtime_error(format("'{}' does not follow <line>.<column>[bc] format", desc));

BufferCoord cursor{str_to_int({comma+1, dot_cursor}) - 1,
str_to_int({dot_cursor+1, desc.end()}) - 1};
auto is_suffix = [](char ch) {
return ch == 'b' || ch == 'c';
};
auto suffix = std::find_if(dot+1, desc.end(), is_suffix);
if (suffix != desc.end() and suffix+1 != desc.end())
throw runtime_error(format("'{}' does not follow <line>.<column>[bc] format", desc));

if (anchor.line < 0 or anchor.column < 0 or
cursor.line < 0 or cursor.column < 0)
const int line = str_to_int({desc.begin(), dot}) - 1;
const int column = str_to_int({dot+1, suffix}) - 1;
if (line < 0 or column < 0)
throw runtime_error(format("coordinates must be >= 1: '{}'", desc));

return Selection{anchor, cursor};
if (suffix == desc.end() or *suffix == 'b')
return BufferCoord{line, column};
else
{
if (timestamp != buffer.timestamp())
throw runtime_error("codepoint columns are only allowed on latest timestamp");
return BufferCoord{line, buffer[line].byte_count_to(CharCount{column})};
}
}

Selection selection_from_string(const Buffer& buffer, StringView desc, size_t timestamp)
{
auto comma = find(desc, ',');
if (comma == desc.end())
throw runtime_error(format("'{}' does not follow <line>.<column>[bc],<line>.<column>[bc] format", desc));
return {buffer_coord_from_string(buffer, {desc.begin(), comma}, timestamp),
buffer_coord_from_string(buffer, {comma+1, desc.end()}, timestamp)};
}

}
7 changes: 5 additions & 2 deletions src/selection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,18 @@ Vector<Selection> compute_modified_ranges(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);
Selection selection_from_string(const Buffer& buffer, StringView desc, size_t timestamp);

template<class StringArray>
SelectionList selection_list_from_strings(Buffer& buffer, StringArray&& descs, size_t timestamp, size_t main)
{
if (timestamp > buffer.timestamp())
throw runtime_error{format("invalid timestamp '{}'", timestamp)};

auto sels = descs | transform(selection_from_string) | gather<Vector<Selection>>();
auto parse_selection = [&buffer, timestamp](StringView desc) {
return selection_from_string(buffer, desc, timestamp);
};
auto sels = descs | transform(parse_selection) | gather<Vector<Selection>>();
if (sels.empty())
throw runtime_error{"empty selection description"};
if (main >= sels.size())
Expand Down
1 change: 1 addition & 0 deletions test/compose/select-codepoints/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:select 1.1c,1.2c 1.4c,1.5c 1.7c,1.7c 2.9c,2.7c 3.2c,3.5c 3.10c,3.11c 4.2c,4.3c 5.3c,5.3c<ret>
5 changes: 5 additions & 0 deletions test/compose/select-codepoints/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lx λx.λy.y x
∃e∈C. ∀x∈C. x⊗e=e⊗x=x
λx.λy.y x̦x
-y̦͂-
-o̦͂-
1 change: 1 addition & 0 deletions test/compose/select-codepoints/kak_quoted_selections
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'lx' 'λx' 'λ' '∀x∈' 'λx.λ' 'x̦' 'y̦' '̦'