selection: combine enum selection_kind with selection_semantic

This commit is contained in:
Daniel Eklöf 2021-01-06 10:53:27 +01:00
parent fcca3d3e55
commit 3a9172342f
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
6 changed files with 47 additions and 38 deletions

View file

@ -119,9 +119,7 @@ extract_one(const struct terminal *term, const struct row *row,
if (ctx->last_row != NULL && row != ctx->last_row) { if (ctx->last_row != NULL && row != ctx->last_row) {
/* New row - determine if we should insert a newline or not */ /* New row - determine if we should insert a newline or not */
if (ctx->selection_kind == SELECTION_NONE || if (ctx->selection_kind != SELECTION_BLOCK) {
ctx->selection_kind == SELECTION_NORMAL)
{
if (ctx->last_row->linebreak || if (ctx->last_row->linebreak ||
ctx->empty_count > 0 || ctx->empty_count > 0 ||
cell->wc == 0) cell->wc == 0)
@ -134,9 +132,7 @@ extract_one(const struct terminal *term, const struct row *row,
ctx->newline_count++; ctx->newline_count++;
ctx->empty_count = 0; ctx->empty_count = 0;
} }
} } else {
else if (ctx->selection_kind == SELECTION_BLOCK) {
/* Always insert a linebreak */ /* Always insert a linebreak */
if (!ensure_size(ctx, 1)) if (!ensure_size(ctx, 1))
goto err; goto err;

16
input.c
View file

@ -275,8 +275,7 @@ execute_binding(struct seat *seat, struct terminal *term,
case BIND_ACTION_SELECT_BEGIN: case BIND_ACTION_SELECT_BEGIN:
if (selection_enabled(term, seat) && cursor_is_on_grid) { if (selection_enabled(term, seat) && cursor_is_on_grid) {
selection_start( selection_start(
term, seat->mouse.col, seat->mouse.row, term, seat->mouse.col, seat->mouse.row, SELECTION_CHAR_WISE, false);
SELECTION_NORMAL, SELECTION_SEMANTIC_NONE, false);
return true; return true;
} }
return false; return false;
@ -284,8 +283,7 @@ execute_binding(struct seat *seat, struct terminal *term,
case BIND_ACTION_SELECT_BEGIN_BLOCK: case BIND_ACTION_SELECT_BEGIN_BLOCK:
if (selection_enabled(term, seat) && cursor_is_on_grid) { if (selection_enabled(term, seat) && cursor_is_on_grid) {
selection_start( selection_start(
term, seat->mouse.col, seat->mouse.row, term, seat->mouse.col, seat->mouse.row, SELECTION_BLOCK, false);
SELECTION_BLOCK, SELECTION_SEMANTIC_NONE, false);
return true; return true;
} }
return false; return false;
@ -301,8 +299,7 @@ execute_binding(struct seat *seat, struct terminal *term,
case BIND_ACTION_SELECT_WORD: case BIND_ACTION_SELECT_WORD:
if (selection_enabled(term, seat) && cursor_is_on_grid) { if (selection_enabled(term, seat) && cursor_is_on_grid) {
selection_start( selection_start(
term, seat->mouse.col, seat->mouse.row, term, seat->mouse.col, seat->mouse.row, SELECTION_WORD_WISE, false);
SELECTION_NORMAL, SELECTION_SEMANTIC_WORD, false);
return true; return true;
} }
return false; return false;
@ -310,16 +307,15 @@ execute_binding(struct seat *seat, struct terminal *term,
case BIND_ACTION_SELECT_WORD_WS: case BIND_ACTION_SELECT_WORD_WS:
if (selection_enabled(term, seat) && cursor_is_on_grid) { if (selection_enabled(term, seat) && cursor_is_on_grid) {
selection_start( selection_start(
term, seat->mouse.col, seat->mouse.row, term, seat->mouse.col, seat->mouse.row, SELECTION_WORD_WISE, true);
SELECTION_NORMAL, SELECTION_SEMANTIC_WORD, true);
return true; return true;
} }
return false; return false;
case BIND_ACTION_SELECT_ROW: case BIND_ACTION_SELECT_ROW:
if (selection_enabled(term, seat) && cursor_is_on_grid) { if (selection_enabled(term, seat) && cursor_is_on_grid) {
selection_start(term, seat->mouse.col, seat->mouse.row, selection_start(
SELECTION_NORMAL, SELECTION_SEMANTIC_ROW, false); term, seat->mouse.col, seat->mouse.row, SELECTION_LINE_WISE, false);
return true; return true;
} }
return false; return false;

View file

@ -222,8 +222,8 @@ search_update_selection(struct terminal *term,
assert(selection_row >= 0 && assert(selection_row >= 0 &&
selection_row < term->grid->num_rows); selection_row < term->grid->num_rows);
selection_start(term, start_col, selection_row, selection_start(
SELECTION_NORMAL, SELECTION_SEMANTIC_NONE, false); term, start_col, selection_row, SELECTION_CHAR_WISE, false);
} }
/* Update selection endpoint */ /* Update selection endpoint */

View file

@ -194,7 +194,9 @@ foreach_selected(
void *data) void *data)
{ {
switch (term->selection.kind) { switch (term->selection.kind) {
case SELECTION_NORMAL: case SELECTION_CHAR_WISE:
case SELECTION_WORD_WISE:
case SELECTION_LINE_WISE:
foreach_selected_normal(term, start, end, cb, data); foreach_selected_normal(term, start, end, cb, data);
return; return;
@ -385,23 +387,24 @@ find_word_boundary_right(struct terminal *term, struct coord *pos,
void void
selection_start(struct terminal *term, int col, int row, selection_start(struct terminal *term, int col, int row,
enum selection_kind kind, enum selection_kind kind,
enum selection_semantic semantic,
bool spaces_only) bool spaces_only)
{ {
selection_cancel(term); selection_cancel(term);
LOG_DBG("%s selection started at %d,%d", LOG_DBG("%s selection started at %d,%d",
kind == SELECTION_NORMAL ? "normal" : kind == SELECTION_CHAR_WISE ? "character-wise" :
kind == SELECTION_WORD_WISE ? "word-wise" :
kind == SELECTION_LINE_WISE ? "line-wise" :
kind == SELECTION_BLOCK ? "block" : "<unknown>", kind == SELECTION_BLOCK ? "block" : "<unknown>",
row, col); row, col);
term->selection.kind = kind; term->selection.kind = kind;
term->selection.semantic = semantic;
term->selection.ongoing = true; term->selection.ongoing = true;
term->selection.spaces_only = spaces_only; term->selection.spaces_only = spaces_only;
switch (semantic) { switch (kind) {
case SELECTION_SEMANTIC_NONE: case SELECTION_CHAR_WISE:
case SELECTION_BLOCK:
term->selection.start = (struct coord){col, term->grid->view + row}; term->selection.start = (struct coord){col, term->grid->view + row};
term->selection.end = (struct coord){-1, -1}; term->selection.end = (struct coord){-1, -1};
@ -409,7 +412,7 @@ selection_start(struct terminal *term, int col, int row,
term->selection.pivot.end = term->selection.end; term->selection.pivot.end = term->selection.end;
break; break;
case SELECTION_SEMANTIC_WORD: { case SELECTION_WORD_WISE: {
struct coord start = {col, row}, end = {col, row}; struct coord start = {col, row}, end = {col, row};
find_word_boundary_left(term, &start, spaces_only); find_word_boundary_left(term, &start, spaces_only);
find_word_boundary_right(term, &end, spaces_only); find_word_boundary_right(term, &end, spaces_only);
@ -424,13 +427,17 @@ selection_start(struct terminal *term, int col, int row,
break; break;
} }
case SELECTION_SEMANTIC_ROW: case SELECTION_LINE_WISE:
term->selection.start = (struct coord){0, term->grid->view + row}; term->selection.start = (struct coord){0, term->grid->view + row};
term->selection.pivot.start = term->selection.start; term->selection.pivot.start = term->selection.start;
term->selection.pivot.end = (struct coord){term->cols - 1, term->grid->view + row}; term->selection.pivot.end = (struct coord){term->cols - 1, term->grid->view + row};
selection_update(term, term->cols - 1, row); selection_update(term, term->cols - 1, row);
break; break;
case SELECTION_NONE:
assert(false);
break;
} }
} }
@ -470,7 +477,7 @@ premark_selected(struct terminal *term, struct row *row, struct cell *cell,
ctx->empty_count = 0; ctx->empty_count = 0;
} }
if (cell->wc == 0 && term->selection.kind == SELECTION_NORMAL) { if (cell->wc == 0 && term->selection.kind != SELECTION_BLOCK) {
ctx->empty_count++; ctx->empty_count++;
return true; return true;
} }
@ -494,7 +501,7 @@ mark_selected(struct terminal *term, struct row *row, struct cell *cell,
ctx->empty_count = 0; ctx->empty_count = 0;
} }
if (cell->wc == 0 && term->selection.kind == SELECTION_NORMAL) { if (cell->wc == 0 && term->selection.kind != SELECTION_BLOCK) {
ctx->empty_count++; ctx->empty_count++;
return true; return true;
} }
@ -658,11 +665,12 @@ selection_update(struct terminal *term, int col, int row)
} }
} }
switch (term->selection.semantic) { switch (term->selection.kind) {
case SELECTION_SEMANTIC_NONE: case SELECTION_CHAR_WISE:
case SELECTION_BLOCK:
break; break;
case SELECTION_SEMANTIC_WORD: case SELECTION_WORD_WISE:
switch (term->selection.direction) { switch (term->selection.direction) {
case SELECTION_LEFT: { case SELECTION_LEFT: {
struct coord end = {col, row}; struct coord end = {col, row};
@ -683,7 +691,7 @@ selection_update(struct terminal *term, int col, int row)
} }
break; break;
case SELECTION_SEMANTIC_ROW: case SELECTION_LINE_WISE:
switch (term->selection.direction) { switch (term->selection.direction) {
case SELECTION_LEFT: case SELECTION_LEFT:
new_end.col = 0; new_end.col = 0;
@ -697,6 +705,10 @@ selection_update(struct terminal *term, int col, int row)
break; break;
} }
break; break;
case SELECTION_NONE:
assert(false);
break;
} }
size_t start_row_idx = new_start.row & (term->grid->num_rows - 1); size_t start_row_idx = new_start.row & (term->grid->num_rows - 1);
@ -885,7 +897,9 @@ selection_extend(struct seat *seat, struct terminal *term,
assert(false); assert(false);
return; return;
case SELECTION_NORMAL: case SELECTION_CHAR_WISE:
case SELECTION_WORD_WISE:
case SELECTION_LINE_WISE:
selection_extend_normal(term, col, row, serial); selection_extend_normal(term, col, row, serial);
break; break;

View file

@ -11,8 +11,7 @@ extern const struct zwp_primary_selection_device_v1_listener primary_selection_d
bool selection_enabled(const struct terminal *term, struct seat *seat); bool selection_enabled(const struct terminal *term, struct seat *seat);
void selection_start( void selection_start(
struct terminal *term, int col, int row, struct terminal *term, int col, int row,
enum selection_kind kind, enum selection_semantic semantic, enum selection_kind new_kind, bool spaces_only);
bool spaces_only);
void selection_update(struct terminal *term, int col, int row); void selection_update(struct terminal *term, int col, int row);
void selection_finalize( void selection_finalize(
struct seat *seat, struct terminal *term, uint32_t serial); struct seat *seat, struct terminal *term, uint32_t serial);

View file

@ -180,8 +180,13 @@ enum mouse_reporting {
enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR };
enum selection_kind { SELECTION_NONE, SELECTION_NORMAL, SELECTION_BLOCK }; enum selection_kind {
enum selection_semantic { SELECTION_SEMANTIC_NONE, SELECTION_SEMANTIC_WORD, SELECTION_SEMANTIC_ROW}; SELECTION_NONE,
SELECTION_CHAR_WISE,
SELECTION_WORD_WISE,
SELECTION_LINE_WISE,
SELECTION_BLOCK
};
enum selection_direction {SELECTION_UNDIR, SELECTION_LEFT, SELECTION_RIGHT}; enum selection_direction {SELECTION_UNDIR, SELECTION_LEFT, SELECTION_RIGHT};
enum selection_scroll_direction {SELECTION_SCROLL_NOT, SELECTION_SCROLL_UP, SELECTION_SCROLL_DOWN}; enum selection_scroll_direction {SELECTION_SCROLL_NOT, SELECTION_SCROLL_UP, SELECTION_SCROLL_DOWN};
@ -360,7 +365,6 @@ struct terminal {
struct { struct {
enum selection_kind kind; enum selection_kind kind;
enum selection_semantic semantic;
enum selection_direction direction; enum selection_direction direction;
struct coord start; struct coord start;
struct coord end; struct coord end;