selection: implement double-click-to-select-word

This commit is contained in:
Daniel Eklöf 2019-07-17 21:30:57 +02:00
parent f669cce5b9
commit 8bd969262b
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
4 changed files with 97 additions and 4 deletions

28
input.c
View file

@ -6,6 +6,7 @@
#include <threads.h>
#include <locale.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <linux/input-event-codes.h>
@ -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)

View file

@ -1,5 +1,6 @@
#include "selection.h"
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
@ -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)
{

View file

@ -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);

View file

@ -253,6 +253,9 @@ struct terminal {
int col;
int row;
int button;
int last_button;
struct timeval last_time;
} mouse;
struct coord cursor;