aboutsummaryrefslogtreecommitdiff
path: root/selection.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2022-04-25 17:59:23 +0000
committerDaniel Eklöf <daniel@ekloef.se>2022-04-25 17:59:23 +0000
commit6316a5eb0cf2b1fe25ca0e9d60c4e11fce9c22d1 (patch)
tree2319fb62c409a67e3391f7502e47127f50411f81 /selection.c
parentgrid: add abs-to-sb and sb-to-abs utility function (diff)
selection: add start/end coordinate getters
Internally, selection coordinates are *unbounded* (that is, the row numbers may be larger than grid->num_rows) while a selection is ongoing. Only after it has been finalized are the coordinates bounded. This means it isn’t safe to use term->selection.coords.* directly.
Diffstat (limited to 'selection.c')
-rw-r--r--selection.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/selection.c b/selection.c
index 8a18c6bb..091d3ed0 100644
--- a/selection.c
+++ b/selection.c
@@ -38,6 +38,29 @@ static const char *const mime_type_map[] = {
[DATA_OFFER_MIME_TEXT_UTF8_STRING] = "UTF8_STRING",
};
+static inline struct coord
+bounded(const struct grid *grid, struct coord coord)
+{
+ coord.row &= grid->num_rows - 1;
+ return coord;
+}
+
+struct coord
+selection_get_start(const struct terminal *term)
+{
+ if (term->selection.coords.start.row < 0)
+ return term->selection.coords.start;
+ return bounded(term->grid, term->selection.coords.start);
+}
+
+struct coord
+selection_get_end(const struct terminal *term)
+{
+ if (term->selection.coords.end.row < 0)
+ return term->selection.coords.end;
+ return bounded(term->grid, term->selection.coords.end);
+}
+
bool
selection_on_rows(const struct terminal *term, int row_start, int row_end)
{
@@ -2461,3 +2484,4 @@ const struct zwp_primary_selection_device_v1_listener primary_selection_device_l
.data_offer = &primary_data_offer,
.selection = &primary_selection,
};
+