From 8bd969262b091b910560d183c57df5ee09805fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 17 Jul 2019 21:30:57 +0200 Subject: [PATCH] selection: implement double-click-to-select-word --- input.c | 28 ++++++++++++++++++---- selection.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ selection.h | 1 + terminal.h | 3 +++ 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/input.c b/input.c index a50f76cf..6c5353ca 100644 --- a/input.c +++ b/input.c @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -354,19 +355,38 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, struct terminal *term = data; switch (state) { - case WL_POINTER_BUTTON_STATE_PRESSED: - if (button == BTN_LEFT) - selection_start(term, term->mouse.col, term->mouse.row); - else { + case WL_POINTER_BUTTON_STATE_PRESSED: { + bool double_click = false; + + struct timeval now; + gettimeofday(&now, NULL); + + struct timeval since_last; + timersub(&now, &term->mouse.last_time, &since_last); + if (button == term->mouse.last_button && + since_last.tv_sec == 0 && since_last.tv_usec <= 300 * 1000) + { + double_click = true; + } + + if (button == BTN_LEFT) { + if (double_click) + selection_mark_word(term, term->mouse.col, term->mouse.row, serial); + else + selection_start(term, term->mouse.col, term->mouse.row); + } else { if (button == BTN_MIDDLE) selection_from_primary(term); selection_cancel(term); } term->mouse.button = button; /* For motion events */ + term->mouse.last_button = button; + term->mouse.last_time = now; term_mouse_down(term, button, term->mouse.row, term->mouse.col, term->kbd.shift, term->kbd.alt, term->kbd.ctrl); break; + } case WL_POINTER_BUTTON_STATE_RELEASED: if (button != BTN_LEFT || term->selection.end.col == -1) diff --git a/selection.c b/selection.c index 76b4d3dc..73a20b40 100644 --- a/selection.c +++ b/selection.c @@ -1,5 +1,6 @@ #include "selection.h" +#include #include #include #include @@ -214,6 +215,74 @@ selection_cancel(struct terminal *term) } } +void +selection_mark_word(struct terminal *term, int col, int row, uint32_t serial) +{ + if (!selection_enabled(term)) + return; + + selection_cancel(term); + + struct coord start = {col, row}; + struct coord end = {col, row}; + + const struct row *r = grid_row_in_view(term->grid, start.row); + unsigned char c = r->cells[start.col].c[0]; + + if (!(c == '\0' || isspace(c))) { + while (true) { + int next_col = start.col - 1; + int next_row = start.row; + + /* Linewrap */ + if (next_col < 0) { + next_col = term->cols - 1; + if (--next_row < 0) + break; + } + + const struct row *row = grid_row_in_view(term->grid, next_row); + + unsigned char c = row->cells[next_col].c[0]; + if (c == '\0' || isspace(c)) + break; + + start.col = next_col; + start.row = next_row; + } + } + + r = grid_row_in_view(term->grid, end.row); + c = r->cells[end.col].c[0]; + + if (!(c == '\0' || isspace(c))) { + while (true) { + int next_col = end.col + 1; + int next_row = end.row; + + /* Linewrap */ + if (next_col >= term->cols) { + next_col = 0; + if (++next_row >= term->rows) + break; + } + + const struct row *row = grid_row_in_view(term->grid, next_row); + + unsigned char c = row->cells[next_col].c[0]; + if (c == '\0' || isspace(c)) + break; + + end.col = next_col; + end.row = next_row; + } + } + + selection_start(term, start.col, start.row); + selection_update(term, end.col, end.row); + selection_finalize(term, serial); +} + static void target(void *data, struct wl_data_source *wl_data_source, const char *mime_type) { diff --git a/selection.h b/selection.h index f6edfd5f..772f5a73 100644 --- a/selection.h +++ b/selection.h @@ -11,6 +11,7 @@ void selection_start(struct terminal *term, int col, int row); void selection_update(struct terminal *term, int col, int row); void selection_finalize(struct terminal *term, uint32_t serial); void selection_cancel(struct terminal *term); +void selection_mark_word(struct terminal *term, int col, int row, uint32_t serial); void selection_to_clipboard(struct terminal *term, uint32_t serial); void selection_from_clipboard(struct terminal *term, uint32_t serial); diff --git a/terminal.h b/terminal.h index 71a77ddb..d17d2128 100644 --- a/terminal.h +++ b/terminal.h @@ -253,6 +253,9 @@ struct terminal { int col; int row; int button; + + int last_button; + struct timeval last_time; } mouse; struct coord cursor;