aboutsummaryrefslogtreecommitdiff
path: root/selection.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-01-02 20:20:41 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-01-04 18:48:26 +0000
commit30de262d29d703b0cc31799a4abf84239f3caf22 (patch)
treebd71d133f96e031c311b2d171769f4f3922dc429 /selection.c
parentrender: draw combining characters on top of colored bitmap glyphs (emoji) (diff)
selection: wip: update selection word-wise when initial selection was by word
Diffstat (limited to 'selection.c')
-rw-r--r--selection.c139
1 files changed, 85 insertions, 54 deletions
diff --git a/selection.c b/selection.c
index db5a0b9f..3653fa1b 100644
--- a/selection.c
+++ b/selection.c
@@ -238,7 +238,7 @@ selection_to_text(const struct terminal *term)
void
selection_start(struct terminal *term, int col, int row,
- enum selection_kind kind)
+ enum selection_kind kind, enum selection_semantic semantic)
{
selection_cancel(term);
@@ -248,6 +248,7 @@ selection_start(struct terminal *term, int col, int row,
row, col);
term->selection.kind = kind;
+ term->selection.semantic = semantic;
term->selection.start = (struct coord){col, term->grid->view + row};
term->selection.end = (struct coord){-1, -1};
term->selection.ongoing = true;
@@ -360,6 +361,68 @@ selection_modify(struct terminal *term, struct coord start, struct coord end)
render_refresh(term);
}
+static void
+find_word_boundary_left(struct terminal *term, struct coord *pos,
+ bool spaces_only)
+{
+ const struct row *r = grid_row_in_view(term->grid, pos->row);
+ wchar_t c = r->cells[pos->col].wc;
+
+ if (!(c == 0 || !isword(c, spaces_only, term->conf->word_delimiters))) {
+ while (true) {
+ int next_col = pos->col - 1;
+ int next_row = pos->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);
+
+ c = row->cells[next_col].wc;
+ if (c == 0 || !isword(c, spaces_only, term->conf->word_delimiters))
+ break;
+
+ pos->col = next_col;
+ pos->row = next_row;
+ }
+ }
+}
+
+static void
+find_word_boundary_right(struct terminal *term, struct coord *pos,
+ bool spaces_only)
+{
+ const struct row *r = grid_row_in_view(term->grid, pos->row);
+ wchar_t c = r->cells[pos->col].wc;
+
+ if (!(c == 0 || !isword(c, spaces_only, term->conf->word_delimiters))) {
+ while (true) {
+ int next_col = pos->col + 1;
+ int next_row = pos->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);
+
+ c = row->cells[next_col].wc;
+ if (c == '\0' || !isword(c, spaces_only, term->conf->word_delimiters))
+ break;
+
+ pos->col = next_col;
+ pos->row = next_row;
+ }
+ }
+}
+
void
selection_update(struct terminal *term, int col, int row)
{
@@ -434,6 +497,22 @@ selection_update(struct terminal *term, int col, int row)
}
}
+ if (term->selection.semantic == SELECTION_SEMANTIC_WORD) {
+ switch (term->selection.direction) {
+ case SELECTION_LEFT:
+ find_word_boundary_left(term, &new_end, false);
+ break;
+
+ case SELECTION_RIGHT:
+ find_word_boundary_right(term, &new_end, false);
+ break;
+
+ case SELECTION_UNDIR:
+ assert(false);
+ break;
+ }
+ }
+
/* If an end point is in the middle of a multi-column character,
* expand the selection to cover the entire character */
if (new_start.row < new_end.row ||
@@ -742,59 +821,11 @@ selection_mark_word(struct seat *seat, struct terminal *term, int col, int row,
struct coord start = {col, row};
struct coord end = {col, row};
- const struct row *r = grid_row_in_view(term->grid, start.row);
- wchar_t c = r->cells[start.col].wc;
-
- if (!(c == 0 || !isword(c, spaces_only, term->conf->word_delimiters))) {
- 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);
-
- c = row->cells[next_col].wc;
- if (c == 0 || !isword(c, spaces_only, term->conf->word_delimiters))
- break;
-
- start.col = next_col;
- start.row = next_row;
- }
- }
-
- r = grid_row_in_view(term->grid, end.row);
- c = r->cells[end.col].wc;
-
- if (!(c == 0 || !isword(c, spaces_only, term->conf->word_delimiters))) {
- 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);
-
- c = row->cells[next_col].wc;
- if (c == '\0' || !isword(c, spaces_only, term->conf->word_delimiters))
- break;
-
- end.col = next_col;
- end.row = next_row;
- }
- }
+ find_word_boundary_left(term, &start, spaces_only);
+ find_word_boundary_right(term, &end, spaces_only);
- selection_start(term, start.col, start.row, SELECTION_NORMAL);
+ selection_start(term, start.col, start.row,
+ SELECTION_NORMAL, SELECTION_SEMANTIC_WORD);
selection_update(term, end.col, end.row);
selection_finalize(seat, term, serial);
}
@@ -803,7 +834,7 @@ void
selection_mark_row(
struct seat *seat, struct terminal *term, int row, uint32_t serial)
{
- selection_start(term, 0, row, SELECTION_NORMAL);
+ selection_start(term, 0, row, SELECTION_NORMAL, SELECTION_SEMANTIC_NONE);
selection_update(term, term->cols - 1, row);
selection_finalize(seat, term, serial);
}