aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-08-05 18:15:18 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-08-05 18:15:18 +0000
commit1e08d93528dd8c8feacbb2f58106588903f60a22 (patch)
tree8be5a3ce9ab100b4a90f3c2e96f8190aa4d31744
parentinput: use a timer fd to handle keyboard key repeat (diff)
selection: ensure start < end in finalize
When the selection is finalized, swap start/end if necessary, to make sure start <= end.
-rw-r--r--render.c40
-rw-r--r--selection.c17
2 files changed, 27 insertions, 30 deletions
diff --git a/render.c b/render.c
index 5dcd7035..950638ac 100644
--- a/render.c
+++ b/render.c
@@ -92,34 +92,26 @@ draw_strikeout(const struct terminal *term, cairo_t *cr, const struct font *font
static bool
coord_is_selected(const struct terminal *term, int col, int row)
{
- if (term->selection.start.col != -1 && term->selection.end.col != -1) {
- const struct coord *start = &term->selection.start;
- const struct coord *end = &term->selection.end;
+ if (term->selection.start.col == -1 || term->selection.end.col == -1)
+ return false;
- if (end->row < start->row || (end->row == start->row && end->col < start->col)) {
- const struct coord *tmp = start;
- start = end;
- end = tmp;
- }
+ const struct coord *start = &term->selection.start;
+ const struct coord *end = &term->selection.end;
- assert(start->row <= end->row);
+ assert(start->row <= end->row);
- if (start->row == end->row) {
- return (term->grid->view + row == start->row &&
- col >= start->col &&
- col <= end->col);
- } else {
- if (term->grid->view + row == start->row)
- return col >= start->col;
- else if (term->grid->view + row == end->row)
- return col <= end->col;
- else
- return (term->grid->view + row >= start->row &&
- term->grid->view + row <= end->row);
- }
- }
+ row += term->grid->view;
- return false;
+ if (start->row == end->row) {
+ return row == start->row && col >= start->col && col <= end->col;
+ } else {
+ if (row == start->row)
+ return col >= start->col;
+ else if (row == end->row)
+ return col <= end->col;
+ else
+ return row >= start->row && row <= end->row;
+ }
}
static void
diff --git a/selection.c b/selection.c
index 92beafe5..fe4f4297 100644
--- a/selection.c
+++ b/selection.c
@@ -30,12 +30,6 @@ extract_selection(const struct terminal *term)
const struct coord *start = &term->selection.start;
const struct coord *end = &term->selection.end;
- if (start->row > end->row || (start->row == end->row && start->col > end->col)) {
- const struct coord *tmp = start;
- start = end;
- end = tmp;
- }
-
assert(start->row <= end->row);
size_t max_cells = 0;
@@ -159,6 +153,17 @@ selection_finalize(struct terminal *term, uint32_t serial)
assert(term->selection.start.row != -1);
assert(term->selection.end.row != -1);
+ if (term->selection.start.row > term->selection.end.row ||
+ (term->selection.start.row == term->selection.end.row &&
+ term->selection.start.col > term->selection.end.col))
+ {
+ struct coord tmp = term->selection.start;
+ term->selection.start = term->selection.end;
+ term->selection.end = tmp;
+ }
+
+ assert(term->selection.start.row <= term->selection.end.row);
+
/* TODO: somehow share code with the clipboard equivalent */
if (term->selection.primary.data_source != NULL) {
/* Kill previous data source */