vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 8dfe6b587b007cbed7bcc5d7c4760e5fd3712598 parent 4c9c8a71f701b292980e27afcaae516217edc486 Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 5 Jul 2017 10:28:13 +0200 vis: make sure all selections have same anchored state With the current model the differences between normal and visual mode is that in the latter selections are anchored (meaning one endpoint remains fixed), while in normal mode both endpoints can in principle be updated simultaneously (currently they are always colapsed to a singleton selection, giving the impression of cursors). Diffstat:
| M | main.c | | | 24 | +++++++++++++++++------- |
| M | view.c | | | 4 | +++- |
| M | view.h | | | 5 | +++-- |
3 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/main.c b/main.c @@ -1326,6 +1326,7 @@ static const char *repeat(Vis *vis, const char *keys, const Arg *arg) { static const char *cursors_new(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); + bool anchored = view_selections_anchored(view_selections_primary_get(view)); VisCountIterator it = vis_count_iterator_get(vis, 1); while (vis_count_iterator_next(&it)) { Selection *sel = NULL; @@ -1358,8 +1359,11 @@ static const char *cursors_new(Vis *vis, const char *keys, const Arg *arg) { else if (arg->i == +1) sel_new = view_selections_next(sel); } - if (sel_new) + if (sel_new) { view_selections_primary_set(sel_new); + if (anchored) + view_selections_anchor(sel_new); + } } vis_count_set(vis, VIS_COUNT_UNKNOWN); return keys; @@ -1672,9 +1676,10 @@ static const char *selections_save(Vis *vis, const char *keys, const Arg *arg) { static const char *selections_restore(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); + bool anchored = view_selections_anchored(view_selections_primary_get(view)); enum VisRegister reg = vis_register_used(vis); Array sel = vis_register_selections_get(vis, reg); - view_selections_set_all(view, &sel); + view_selections_set_all(view, &sel, anchored); array_release(&sel); vis_cancel(vis); return keys; @@ -1707,6 +1712,7 @@ static void normalize(Array *a) { static const char *selections_union(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); + bool anchored = view_selections_anchored(view_selections_primary_get(view)); enum VisRegister reg = vis_register_used(vis); Array a = vis_register_selections_get(vis, reg); Array b = view_selections_get_all(view); @@ -1746,7 +1752,7 @@ static const char *selections_union(Vis *vis, const char *keys, const Arg *arg) if (text_range_valid(&cur)) array_add(&sel, &cur); - view_selections_set_all(view, &sel); + view_selections_set_all(view, &sel, anchored); vis_cancel(vis); array_release(&a); @@ -1773,6 +1779,7 @@ static void intersect(Array *ret, Array *a, Array *b) { static const char *selections_intersect(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); + bool anchored = view_selections_anchored(view_selections_primary_get(view)); enum VisRegister reg = vis_register_used(vis); Array a = vis_register_selections_get(vis, reg); Array b = view_selections_get_all(view); @@ -1780,7 +1787,7 @@ static const char *selections_intersect(Vis *vis, const char *keys, const Arg *a array_init_from(&sel, &a); intersect(&sel, &a, &b); - view_selections_set_all(view, &sel); + view_selections_set_all(view, &sel, anchored); vis_cancel(vis); array_release(&a); @@ -1809,6 +1816,7 @@ static void complement(Array *ret, Array *a, Filerange *universe) { static const char *selections_complement(Vis *vis, const char *keys, const Arg *arg) { Text *txt = vis_text(vis); View *view = vis_view(vis); + bool anchored = view_selections_anchored(view_selections_primary_get(view)); Filerange universe = text_object_entire(txt, 0); Array a = view_selections_get_all(view); Array sel; @@ -1816,7 +1824,7 @@ static const char *selections_complement(Vis *vis, const char *keys, const Arg * complement(&sel, &a, &universe); - view_selections_set_all(view, &sel); + view_selections_set_all(view, &sel, anchored); array_release(&a); array_release(&sel); return keys; @@ -1824,6 +1832,7 @@ static const char *selections_complement(Vis *vis, const char *keys, const Arg * static const char *selections_minus(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); + bool anchored = view_selections_anchored(view_selections_primary_get(view)); enum VisRegister reg = vis_register_used(vis); Array a = view_selections_get_all(view); Array b = vis_register_selections_get(vis, reg); @@ -1839,7 +1848,7 @@ static const char *selections_minus(Vis *vis, const char *keys, const Arg *arg) complement(&b_complement, &b, &universe); intersect(&sel, &a, &b_complement); - view_selections_set_all(view, &sel); + view_selections_set_all(view, &sel, anchored); vis_cancel(vis); array_release(&a); @@ -1902,6 +1911,7 @@ static Filerange combine_rightmost(const Filerange *r1, const Filerange *r2) { static const char *selections_combine(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); + bool anchored = view_selections_anchored(view_selections_primary_get(view)); enum VisRegister reg = vis_register_used(vis); Array a = view_selections_get_all(view); Array b = vis_register_selections_get(vis, reg); @@ -1916,7 +1926,7 @@ static const char *selections_combine(Vis *vis, const char *keys, const Arg *arg } normalize(&sel); - view_selections_set_all(view, &sel); + view_selections_set_all(view, &sel, anchored); vis_cancel(vis); array_release(&a); diff --git a/view.c b/view.c @@ -1252,7 +1252,7 @@ bool view_regions_save(View *view, Filerange *r, SelectionRegion *s) { return true; } -void view_selections_set_all(View *view, Array *arr) { +void view_selections_set_all(View *view, Array *arr, bool anchored) { Selection *s; Filerange *r; size_t i = 0; @@ -1264,11 +1264,13 @@ void view_selections_set_all(View *view, Array *arr) { } break; } + s->anchored = anchored; } while ((r = array_get(arr, i++))) { s = view_selections_new_force(view, r->start); if (!s || !view_selections_set(s, r)) break; + s->anchored = anchored; } view_selections_primary_set(view->selections); } diff --git a/view.h b/view.h @@ -147,9 +147,10 @@ void view_selections_dispose_all(View*); void view_selections_normalize(View*); /** * Replace currently active selections. - * @param The array of ``Filerange``s. + * @param array The Array of ``Filerange``s. + * @param anchored Whether *all* selection should be anchored. */ -void view_selections_set_all(View*, Array*); +void view_selections_set_all(View*, Array*, bool anchored); /** Get array containing a ``Fileranges`` for each selection. */ Array view_selections_get_all(View*); /**