aboutsummaryrefslogtreecommitdiff
path: root/vis.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-07-28 12:10:22 +0200
committerMarc André Tanner <mat@brain-dump.org>2015-07-28 13:21:50 +0200
commitb366f55f29af3775c055115572dd84873b1921e0 (patch)
treeb9c6a710997272d8b84238965e6f992abfbed42c /vis.c
parenta19159c506577a168655afcd961381dd1f995610 (diff)
downloadvis-b366f55f29af3775c055115572dd84873b1921e0.tar.gz
vis-b366f55f29af3775c055115572dd84873b1921e0.tar.xz
vis: use multiple cursor/selection infrastructure
This commits introduces the following keybindings, in normal mode: CTRL-N select word the cursor is currently over, switch to visual mode CTRL-P remove least recently added cursor ESC if a selection is active, clear it. Otherwise dispose all but the primary cursor. In visual mode: CTRL-N create new cursor and select next word matching current selection CTRL-X clear (skip) current selection, but select next matching word CTRL-P remove least recently added cursor
Diffstat (limited to 'vis.c')
-rw-r--r--vis.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/vis.c b/vis.c
index b10f798..b385135 100644
--- a/vis.c
+++ b/vis.c
@@ -326,6 +326,14 @@ static void cursors_new(const Arg *arg);
static void cursors_align(const Arg *arg);
/* remove all but the primary cursor and their selections */
static void cursors_clear(const Arg *arg);
+/* remove the least recently added cursor */
+static void cursors_remove(const Arg *arg);
+/* select the word the cursor is currently over */
+static void cursors_select(const Arg *arg);
+/* select the next region matching the current selection */
+static void cursors_select_next(const Arg *arg);
+/* clear current selection but select next match */
+static void cursors_select_skip(const Arg *arg);
/* adjust action.count by arg->i */
static void count(const Arg *arg);
/* move to the action.count-th line or if not given either to the first (arg->i < 0)
@@ -869,6 +877,59 @@ static void cursors_clear(const Arg *arg) {
view_cursors_selection_clear(view_cursor(view));
}
+static void cursors_select(const Arg *arg) {
+ Text *txt = vis->win->file->text;
+ View *view = vis->win->view;
+ for (Cursor *cursor = view_cursors(view); cursor; cursor = view_cursors_next(cursor)) {
+ Filerange sel = view_cursors_selection_get(cursor);
+ Filerange word = text_object_word(txt, view_cursors_pos(cursor));
+ if (!text_range_valid(&sel) && text_range_valid(&word)) {
+ view_cursors_selection_set(cursor, &word);
+ view_cursors_to(cursor, text_char_prev(txt, word.end));
+ }
+ }
+ switchmode(&(const Arg){ .i = VIS_MODE_VISUAL });
+}
+
+static void cursors_select_next(const Arg *arg) {
+ Text *txt = vis->win->file->text;
+ View *view = vis->win->view;
+ Cursor *cursor = view_cursor(view);
+ Filerange sel = view_cursors_selection_get(cursor);
+ if (!text_range_valid(&sel))
+ return;
+
+ size_t len = text_range_size(&sel);
+ char *buf = malloc(len+1);
+ if (!buf)
+ return;
+ len = text_bytes_get(txt, sel.start, len, buf);
+ buf[len] = '\0';
+ Filerange word = text_object_word_find_next(txt, sel.end, buf);
+ free(buf);
+
+ if (text_range_valid(&word)) {
+ cursor = view_cursors_new(view);
+ if (!cursor)
+ return;
+ view_cursors_selection_set(cursor, &word);
+ view_cursors_to(cursor, text_char_prev(txt, word.end));
+ }
+}
+
+static void cursors_select_skip(const Arg *arg) {
+ View *view = vis->win->view;
+ Cursor *cursor = view_cursor(view);
+ cursors_select_next(arg);
+ if (cursor != view_cursor(view))
+ view_cursors_dispose(cursor);
+}
+
+static void cursors_remove(const Arg *arg) {
+ View *view = vis->win->view;
+ view_cursors_dispose(view_cursor(view));
+}
+
static void replace(const Arg *arg) {
Key k = getkey();
if (!k.str[0])