vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 61eae1ffb23d5c4874e9d4da41d0e28071f1ed0b parent 1bf2903739e75e7414f187f11313e3d8fea60d54 Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 14 Jun 2017 15:09:36 +0200 view: add functions to get/set all selections Diffstat:
| M | view.c | | | 39 | ++++++++++++++++++++++++++++++++++----- |
| M | view.h | | | 13 | +++++++++++++ |
2 files changed, 47 insertions(+), 5 deletions(-)
diff --git a/view.c b/view.c @@ -38,11 +38,6 @@ enum { * the necessary offset for the last character. */ -typedef struct { - Mark anchor; - Mark cursor; -} SelectionRegion; - struct Selection { Mark cursor; /* other selection endpoint where it changes */ Mark anchor; /* position where the selection was created */ @@ -1247,6 +1242,40 @@ bool view_selections_restore(Selection *s) { return true; } +void view_selections_set_all(View *view, Array *arr) { + Selection *s; + Filerange *r; + size_t i = 0; + for (s = view->selections; s; s = s->next) { + if (!(r = array_get(arr, i++)) || !view_selections_set(s, r)) { + for (Selection *next; s; s = next) { + next = view_selections_next(s); + view_selections_dispose(s); + } + break; + } + } + while ((r = array_get(arr, i++))) { + s = view_selections_new_force(view, r->start); + if (!s || !view_selections_set(s, r)) + break; + } + view_selections_primary_set(view->selections); +} + +Array view_selections_get_all(View *view) { + Array arr; + array_init_sized(&arr, sizeof(Filerange)); + if (!array_reserve(&arr, view_selections_count(view))) + return arr; + for (Selection *s = view->selections; s; s = s->next) { + Filerange r = view_selections_get(s); + if (text_range_valid(&r)) + array_add(&arr, &r); + } + return arr; +} + void view_selections_normalize(View *view) { Selection *prev = NULL; Filerange range_prev = text_range_empty(); diff --git a/view.h b/view.h @@ -9,6 +9,12 @@ typedef struct Selection Selection; #include "text.h" #include "ui.h" +#include "array.h" + +typedef struct { + Mark anchor; + Mark cursor; +} SelectionRegion; typedef struct { char data[16]; /* utf8 encoded character displayed in this cell (might be more than @@ -140,6 +146,13 @@ void view_selections_dispose_all(View*); /** Dispose all invalid and merge all overlapping selections. */ void view_selections_normalize(View*); /** + * Replace currently active selections. + * @param The array of ``Filerange``s. + */ +void view_selections_set_all(View*, Array*); +/** Get array containing a ``Fileranges`` for each selection. */ +Array view_selections_get_all(View*); +/** * @} * @defgroup view_navigate * @{