From 30de262d29d703b0cc31799a4abf84239f3caf22 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 2 Jan 2021 21:20:41 +0100 Subject: selection: wip: update selection word-wise when initial selection was by word --- terminal.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index 0774aaa1..8d032d20 100644 --- a/terminal.h +++ b/terminal.h @@ -181,6 +181,7 @@ enum mouse_reporting { enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; enum selection_kind { SELECTION_NONE, SELECTION_NORMAL, SELECTION_BLOCK }; +enum selection_semantic { SELECTION_SEMANTIC_NONE, SELECTION_SEMANTIC_WORD}; enum selection_direction {SELECTION_UNDIR, SELECTION_LEFT, SELECTION_RIGHT}; enum selection_scroll_direction {SELECTION_SCROLL_NOT, SELECTION_SCROLL_UP, SELECTION_SCROLL_DOWN}; @@ -359,6 +360,7 @@ struct terminal { struct { enum selection_kind kind; + enum selection_semantic semantic; enum selection_direction direction; struct coord start; struct coord end; -- cgit v1.3 From 55ecf29a36ac404ad41eb0050ff01caa35c3cb39 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 2 Jan 2021 21:26:28 +0100 Subject: selection: wip: update selection row-wise when initial selection was by row --- selection.c | 25 +++++++++++++++++++++++-- terminal.h | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'terminal.h') diff --git a/selection.c b/selection.c index 3653fa1b..615088d8 100644 --- a/selection.c +++ b/selection.c @@ -497,7 +497,11 @@ selection_update(struct terminal *term, int col, int row) } } - if (term->selection.semantic == SELECTION_SEMANTIC_WORD) { + switch (term->selection.semantic) { + case SELECTION_SEMANTIC_NONE: + break; + + case SELECTION_SEMANTIC_WORD: switch (term->selection.direction) { case SELECTION_LEFT: find_word_boundary_left(term, &new_end, false); @@ -511,6 +515,23 @@ selection_update(struct terminal *term, int col, int row) assert(false); break; } + break; + + case SELECTION_SEMANTIC_ROW: + switch (term->selection.direction) { + case SELECTION_LEFT: + new_end.col = 0; + break; + + case SELECTION_RIGHT: + new_end.col = term->cols - 1; + break; + + case SELECTION_UNDIR: + assert(false); + break; + } + break; } /* If an end point is in the middle of a multi-column character, @@ -834,7 +855,7 @@ void selection_mark_row( struct seat *seat, struct terminal *term, int row, uint32_t serial) { - selection_start(term, 0, row, SELECTION_NORMAL, SELECTION_SEMANTIC_NONE); + selection_start(term, 0, row, SELECTION_NORMAL, SELECTION_SEMANTIC_ROW); selection_update(term, term->cols - 1, row); selection_finalize(seat, term, serial); } diff --git a/terminal.h b/terminal.h index 8d032d20..c158d214 100644 --- a/terminal.h +++ b/terminal.h @@ -181,7 +181,7 @@ enum mouse_reporting { enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; enum selection_kind { SELECTION_NONE, SELECTION_NORMAL, SELECTION_BLOCK }; -enum selection_semantic { SELECTION_SEMANTIC_NONE, SELECTION_SEMANTIC_WORD}; +enum selection_semantic { SELECTION_SEMANTIC_NONE, SELECTION_SEMANTIC_WORD, SELECTION_SEMANTIC_ROW}; enum selection_direction {SELECTION_UNDIR, SELECTION_LEFT, SELECTION_RIGHT}; enum selection_scroll_direction {SELECTION_SCROLL_NOT, SELECTION_SCROLL_UP, SELECTION_SCROLL_DOWN}; -- cgit v1.3 From bef69cb9610fc296c001974fa85e0289d21f55c2 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 2 Jan 2021 21:38:41 +0100 Subject: selection: remember whether word-wise selection uses spaces only for delimiters --- selection.c | 6 ++++-- terminal.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'terminal.h') diff --git a/selection.c b/selection.c index 615088d8..7e21ecdb 100644 --- a/selection.c +++ b/selection.c @@ -504,11 +504,11 @@ selection_update(struct terminal *term, int col, int row) case SELECTION_SEMANTIC_WORD: switch (term->selection.direction) { case SELECTION_LEFT: - find_word_boundary_left(term, &new_end, false); + find_word_boundary_left(term, &new_end, term->selection.spaces_only); break; case SELECTION_RIGHT: - find_word_boundary_right(term, &new_end, false); + find_word_boundary_right(term, &new_end, term->selection.spaces_only); break; case SELECTION_UNDIR: @@ -847,6 +847,8 @@ selection_mark_word(struct seat *seat, struct terminal *term, int col, int row, selection_start(term, start.col, start.row, SELECTION_NORMAL, SELECTION_SEMANTIC_WORD); + term->selection.spaces_only = spaces_only; + selection_update(term, end.col, end.row); selection_finalize(seat, term, serial); } diff --git a/terminal.h b/terminal.h index c158d214..52eb0a84 100644 --- a/terminal.h +++ b/terminal.h @@ -365,6 +365,7 @@ struct terminal { struct coord start; struct coord end; bool ongoing; + bool spaces_only; /* SELECTION_SEMANTIC_WORD */ struct { int fd; -- cgit v1.3 From 2fd7b2fbd46d773ba43ba5acde365d23de4dda90 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 3 Jan 2021 13:11:46 +0100 Subject: selection: allow selections to pivot around a range instead of a point Extend selection pivoting to allow selections to pivot around a range. Use this in word- and row-based selections to pivot around the initial word/row that was selected. This mimics the behavior of at least urxvt and xterm. --- selection.c | 104 +++++++++++++++++++++++++++++++++++++++++++++--------------- terminal.h | 5 +++ 2 files changed, 83 insertions(+), 26 deletions(-) (limited to 'terminal.h') diff --git a/selection.c b/selection.c index 0f856c78..37bf5af8 100644 --- a/selection.c +++ b/selection.c @@ -352,11 +352,13 @@ selection_start(struct terminal *term, int col, int row, term->selection.ongoing = true; term->selection.spaces_only = spaces_only; - switch (semantic) { case SELECTION_SEMANTIC_NONE: term->selection.start = (struct coord){col, term->grid->view + row}; term->selection.end = (struct coord){-1, -1}; + + term->selection.pivot.start = term->selection.start; + term->selection.pivot.end = term->selection.end; break; case SELECTION_SEMANTIC_WORD: { @@ -367,15 +369,22 @@ selection_start(struct terminal *term, int col, int row, term->selection.start = (struct coord){ start.col, term->grid->view + start.row}; + term->selection.pivot.start = term->selection.start; + term->selection.pivot.end = (struct coord){end.col, term->grid->view + end.row}; + selection_update(term, end.col, end.row); break; } case SELECTION_SEMANTIC_ROW: term->selection.start = (struct coord){0, term->grid->view + row}; + term->selection.pivot.start = term->selection.start; + term->selection.pivot.end = (struct coord){term->cols - 1, term->grid->view + row}; + selection_update(term, term->cols - 1, row); break; } + } /* Context used while (un)marking selected cells, to be able to @@ -511,50 +520,95 @@ selection_update(struct terminal *term, int col, int row) /* Adjust start point if the selection has changed 'direction' */ if (!(new_end.row == new_start.row && new_end.col == new_start.col)) { - enum selection_direction new_direction; + enum selection_direction new_direction = term->selection.direction; + + struct coord *pivot_start = &term->selection.pivot.start; + struct coord *pivot_end = &term->selection.pivot.end; - if (new_end.row > new_start.row || - (new_end.row == new_start.row && new_end.col > new_start.col)) + if (new_end.row < pivot_start->row || + (new_end.row == pivot_start->row && new_end.col < pivot_start->col)) { - /* New end point is after the start point */ - new_direction = SELECTION_RIGHT; - } else { - /* The new end point is before the start point */ + /* New end point is before the start point */ new_direction = SELECTION_LEFT; + } else { + /* The new end point is after the start point */ + new_direction = SELECTION_RIGHT; } if (term->selection.direction != new_direction) { - if (term->selection.direction != SELECTION_UNDIR) { - if (new_direction == SELECTION_LEFT) { + if (term->selection.direction == SELECTION_UNDIR && + pivot_end->row < 0) + { + /* First, make sure ‘start’ isn’t in the middle of a + * multi-column character */ + while (true) { + const struct row *row = term->grid->rows[pivot_start->row]; + const struct cell *cell = &row->cells[pivot_start->col]; + + if (cell->wc != CELL_MULT_COL_SPACER) + break; + + /* Multi-column chars don’t cross rows */ + assert(pivot_start->col > 0); + if (pivot_start->col == 0) + break; + + pivot_start->col--; + } + + /* + * Setup pivot end to be one character *before* start + * Which one we move, the end or start point, depends + * on the initial selection direction. + */ + + *pivot_end = *pivot_start; + + if (new_direction == SELECTION_RIGHT) { bool keep_going = true; while (keep_going) { - const wchar_t wc = row_start->cells[new_start.col].wc; + const struct row *row = term->grid->rows[pivot_end->row]; + const wchar_t wc = row->cells[pivot_end->col].wc; + keep_going = wc == CELL_MULT_COL_SPACER; - new_start.col--; - if (new_start.col < 0) { - new_start.col = term->cols - 1; - new_start.row--; - } + if (pivot_end->col == 0) { + if (pivot_end->row > 0) { + pivot_end->col = term->cols - 1; + pivot_end->row--; + } + } else + pivot_end->col--; } } else { bool keep_going = true; while (keep_going) { - const wchar_t wc = new_start.col < term->cols - 1 - ? row_start->cells[new_start.col + 1].wc - : 0; + const struct row *row = term->grid->rows[pivot_start->row]; + const wchar_t wc = pivot_start->col < term->cols - 1 + ? row->cells[pivot_start->col + 1].wc : 0; keep_going = wc == CELL_MULT_COL_SPACER; - new_start.col++; - if (new_start.col >= term->cols) { - new_start.col = 0; - new_start.row++; - } + if (pivot_start->col >= term->cols - 1) { + if (pivot_start->row < term->rows - 1) { + pivot_start->col = 0; + pivot_start->row++; + } + } else + pivot_start->col++; } } + + assert(term->grid->rows[pivot_start->row]->cells[pivot_start->col].wc != CELL_MULT_COL_SPACER); + assert(term->grid->rows[pivot_end->row]->cells[pivot_end->col].wc != CELL_MULT_COL_SPACER); } + if (new_direction == SELECTION_LEFT) { + assert(pivot_end->row >= 0); + new_start = *pivot_end; + } else + new_start = *pivot_start; + term->selection.direction = new_direction; } } @@ -574,7 +628,6 @@ selection_update(struct terminal *term, int col, int row) break; case SELECTION_UNDIR: - assert(false); break; } break; @@ -590,7 +643,6 @@ selection_update(struct terminal *term, int col, int row) break; case SELECTION_UNDIR: - assert(false); break; } break; diff --git a/terminal.h b/terminal.h index 52eb0a84..a8bd5b0b 100644 --- a/terminal.h +++ b/terminal.h @@ -367,6 +367,11 @@ struct terminal { bool ongoing; bool spaces_only; /* SELECTION_SEMANTIC_WORD */ + struct { + struct coord start; + struct coord end; + } pivot; + struct { int fd; int col; -- cgit v1.3