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

Add ctrl-y to copy selected into input #202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions doc/tofi.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ the form **--key=value**.

> Delete word.

\<Ctrl\>-y

> Copy current selection into input.

\<Enter\>

> Confirm the current selection and quit.
Expand Down
3 changes: 3 additions & 0 deletions doc/tofi.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ All config file options described in *tofi*(5) are also accepted, in the form
<Ctrl>-w | <Ctrl>-<Backspace>
Delete word.

<Ctrl>-y
Copy current selection into input.

<Enter>
Confirm the current selection and quit.

Expand Down
52 changes: 52 additions & 0 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static void add_character(struct tofi *tofi, xkb_keycode_t keycode);
static void delete_character(struct tofi *tofi);
static void delete_word(struct tofi *tofi);
static void clear_input(struct tofi *tofi);
static void complete_selection(struct tofi *tofi);
static void paste(struct tofi *tofi);
static void select_previous_result(struct tofi *tofi);
static void select_next_result(struct tofi *tofi);
Expand Down Expand Up @@ -68,6 +69,8 @@ void input_handle_keypress(struct tofi *tofi, xkb_keycode_t keycode)
clear_input(tofi);
} else if (key == KEY_V && ctrl) {
paste(tofi);
} else if (key == KEY_Y && ctrl) {
complete_selection(tofi);
} else if (key == KEY_LEFT) {
previous_cursor_or_result(tofi);
} else if (key == KEY_RIGHT) {
Expand Down Expand Up @@ -304,6 +307,55 @@ void clear_input(struct tofi *tofi)
input_refresh_results(tofi);
}

void complete_selection(struct tofi *tofi)
{
struct entry *entry = &tofi->window.entry;
uint32_t selection = entry->selection + entry->first_result;
char *res = entry->results.buf[selection].string;
if (!res) {
return;
}

char buffer[5];
memset(buffer, 0, N_ELEM(buffer));
errno = 0;
bool eof = false;
size_t pos = 0;

entry->cursor_position = 0;
while (entry->cursor_position < N_ELEM(entry->input_utf32)) {
for (size_t i = 0; i < 4; i++) {
if (!res[pos]) {
eof = true;
break;
}

buffer[i] = res[pos];
pos += 1;
uint32_t unichar = utf8_to_utf32_validate(buffer);
if (unichar == (uint32_t)-2) {
/* The current character isn't complete yet. */
continue;
} else if (unichar == (uint32_t)-1) {
log_error("Invalid UTF-8 character in clipboard: %s\n", buffer);
break;
} else {
entry->input_utf32[entry->cursor_position] = unichar;
entry->cursor_position++;
break;
}
}
memset(buffer, 0, N_ELEM(buffer));
if (eof) {
break;
}
}
entry->input_utf32_length = entry->cursor_position;

input_refresh_results(tofi);
tofi->window.surface.redraw = true;
}

void paste(struct tofi *tofi)
{
if (tofi->clipboard.wl_data_offer == NULL || tofi->clipboard.mime_type == NULL) {
Expand Down