From d268609511dc77e1acbd3885642b82751d32bccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Thu, 10 Mar 2016 20:56:24 +0100 Subject: vis: let and in visual mode move to prev/next cursor We do currently not enforce a strict ordering among cursors. Hence these key bindings can move you to an arbitray position. In practice it somewhat works because most of the time cursors are created in "top-down" i.e from the start of the file towards the end. --- config.def.h | 8 ++++---- main.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/config.def.h b/config.def.h index e6b2a7d..27fc4eb 100644 --- a/config.def.h +++ b/config.def.h @@ -198,8 +198,8 @@ static const KeyBinding bindings_normal[] = { { "", ALIAS("k") }, { "", ALIAS("") }, { "", ALIAS("") }, - { "", ALIAS("") }, - { "", ALIAS("") }, + { "", ACTION(CURSORS_PREV) }, + { "", ACTION(CURSORS_NEXT) }, { "", ACTION(WINDOW_SLIDE_UP) }, { "", ACTION(WINDOW_SLIDE_DOWN) }, { "", ACTION(JUMPLIST_PREV) }, @@ -269,8 +269,8 @@ static const KeyBinding bindings_visual[] = { { ":", ACTION(PROMPT_SHOW_VISUAL) }, { "", ALIAS("") }, { "", ALIAS("") }, - { "", ALIAS("") }, - { "", ALIAS("") }, + { "", ACTION(CURSORS_PREV) }, + { "", ACTION(CURSORS_NEXT) }, { "x", ALIAS("d") }, { "r", ALIAS("c") }, { "s", ALIAS("c") }, diff --git a/main.c b/main.c index 8a0487b..bb6f8cf 100644 --- a/main.c +++ b/main.c @@ -48,6 +48,8 @@ static const char *cursors_align_indent(Vis*, const char *keys, const Arg *arg); static const char *cursors_clear(Vis*, const char *keys, const Arg *arg); /* remove the least recently added cursor */ static const char *cursors_remove(Vis*, const char *keys, const Arg *arg); +/* move to the previous (arg->i < 0) or next (arg->i > 0) cursor */ +static const char *cursors_navigate(Vis*, const char *keys, const Arg *arg); /* select the word the cursor is currently over */ static const char *cursors_select(Vis*, const char *keys, const Arg *arg); /* select the next region matching the current selection */ @@ -244,6 +246,8 @@ enum { VIS_ACTION_CURSORS_ALIGN_INDENT, VIS_ACTION_CURSORS_REMOVE_ALL, VIS_ACTION_CURSORS_REMOVE_LAST, + VIS_ACTION_CURSORS_PREV, + VIS_ACTION_CURSORS_NEXT, VIS_ACTION_TEXT_OBJECT_WORD_OUTER, VIS_ACTION_TEXT_OBJECT_WORD_INNER, VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER, @@ -929,6 +933,16 @@ static const KeyAction vis_action[] = { "Remove least recently created cursor", cursors_remove, }, + [VIS_ACTION_CURSORS_PREV] = { + "cursors-prev", + "Move to the previous cursor", + cursors_navigate, { .i = -PAGE_HALF } + }, + [VIS_ACTION_CURSORS_NEXT] = { + "cursors-next", + "Move to the next cursor", + cursors_navigate, { .i = +PAGE_HALF } + }, [VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = { "text-object-word-outer", "A word leading and trailing whitespace included", @@ -1301,6 +1315,28 @@ static const char *cursors_remove(Vis *vis, const char *keys, const Arg *arg) { return keys; } +static const char *cursors_navigate(Vis *vis, const char *keys, const Arg *arg) { + View *view = vis_view(vis); + bool multiple_cursors = view_cursors_next(view_cursors(view)); + if (!multiple_cursors) + return wscroll(vis, keys, arg); + Cursor *c = view_cursors_primary_get(view); + if (arg->i < 0) { + c = view_cursors_next(c); + if (!c) + c = view_cursors(view); + } else { + c = view_cursors_prev(c); + if (!c) { + c = view_cursors(view); + for (Cursor *n = c; n; n = view_cursors_next(n)) + c = n; + } + } + view_cursors_primary_set(c); + return keys; +} + static const char *replace(Vis *vis, const char *keys, const Arg *arg) { if (!keys[0]) return NULL; -- cgit v1.2.3