aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-06-15 13:15:57 +0200
committerMarc André Tanner <mat@brain-dump.org>2017-06-15 15:51:43 +0200
commit97f3bb57b1e613aedd97cb941ed436001692a1c1 (patch)
tree7e46ef4b7d1c0a9aaacfe93e69cc4a88d7bc8097 /main.c
parent6aac9391ee4f38459d5fa53e82dc11647d755126 (diff)
downloadvis-97f3bb57b1e613aedd97cb941ed436001692a1c1.tar.gz
vis-97f3bb57b1e613aedd97cb941ed436001692a1c1.tar.xz
vis: implement pairwise selection combinator: union
Diffstat (limited to 'main.c')
-rw-r--r--main.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/main.c b/main.c
index 3253a22..e9df2c3 100644
--- a/main.c
+++ b/main.c
@@ -86,6 +86,9 @@ static const char *selections_intersect(Vis*, const char *keys, const Arg *arg);
static const char *selections_complement(Vis*, const char *keys, const Arg *arg);
/* subtract selections from register */
static const char *selections_minus(Vis*, const char *keys, const Arg *arg);
+/* pariwise combine selections */
+static const char *selections_combine(Vis*, const char *keys, const Arg *arg);
+static Filerange combine_union(const Filerange*, const Filerange*);
/* adjust current used count according to keys */
static const char *count(Vis*, const char *keys, const Arg *arg);
/* move to the count-th line or if not given either to the first (arg->i < 0)
@@ -294,6 +297,7 @@ enum {
VIS_ACTION_SELECTIONS_INTERSECT,
VIS_ACTION_SELECTIONS_COMPLEMENT,
VIS_ACTION_SELECTIONS_MINUS,
+ VIS_ACTION_SELECTIONS_COMBINE_UNION,
VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
VIS_ACTION_TEXT_OBJECT_WORD_INNER,
VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
@@ -1084,6 +1088,11 @@ static const KeyAction vis_action[] = {
VIS_HELP("Subtract selections from register")
selections_minus
},
+ [VIS_ACTION_SELECTIONS_COMBINE_UNION] = {
+ "vis-selections-combine-union",
+ VIS_HELP("Pairwise union with selection from register")
+ selections_combine, { .combine = combine_union }
+ },
[VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
"vis-textobject-word-outer",
VIS_HELP("A word leading and trailing whitespace included")
@@ -1789,6 +1798,39 @@ static const char *selections_minus(Vis *vis, const char *keys, const Arg *arg)
return keys;
}
+static Filerange combine_union(const Filerange *r1, const Filerange *r2) {
+ if (!r1)
+ return *r2;
+ if (!r2)
+ return *r1;
+ return text_range_union(r1, r2);
+}
+
+static const char *selections_combine(Vis *vis, const char *keys, const Arg *arg) {
+ View *view = vis_view(vis);
+ enum VisRegister reg = vis_register_used(vis);
+ Array a = view_selections_get_all(view);
+ Array b = vis_register_selections_get(vis, reg);
+ Array sel;
+ array_init_from(&sel, &a);
+
+ Filerange *r1 = array_get(&a, 0), *r2 = array_get(&b, 0);
+ for (size_t i = 0, j = 0; r1 || r2; r1 = array_get(&a, ++i), r2 = array_get(&b, ++j)) {
+ Filerange new = arg->combine(r1, r2);
+ if (text_range_valid(&new))
+ array_add(&sel, &new);
+ }
+
+ view_selections_set_all(view, &sel);
+ vis_cancel(vis);
+
+ array_release(&a);
+ array_release(&b);
+ array_release(&sel);
+
+ return keys;
+}
+
static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
if (!keys[0]) {
vis_keymap_disable(vis);