vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit e1e5582b936361848f4bd07ebfdb201805a80011
parent f6afcc32612e2eb4d555f9f4058aee5175738547
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed, 17 Sep 2014 14:28:45 +0200

Implement right shift operator

Diffstat:
Mconfig.def.h | 1+
Mvis.c | 25+++++++++++++++++++++++++
2 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -198,6 +198,7 @@ static KeyBinding vis_operators[] = { { { NONE('c') }, operator, { .i = OP_CHANGE } }, { { NONE('y') }, operator, { .i = OP_YANK } }, { { NONE('p') }, operator, { .i = OP_PUT } }, + { { NONE('>') }, operator, { .i = OP_SHIFT_RIGHT } }, { /* empty last element, array terminator */ }, }; diff --git a/vis.c b/vis.c @@ -151,6 +151,7 @@ static void op_change(OperatorContext *c); static void op_yank(OperatorContext *c); static void op_put(OperatorContext *c); static void op_delete(OperatorContext *c); +static void op_shift_right(OperatorContext *c); /* these can be passed as int argument to operator(&(const Arg){ .i = OP_*}) */ enum { @@ -158,6 +159,7 @@ enum { OP_CHANGE, OP_YANK, OP_PUT, + OP_SHIFT_RIGHT, }; static Operator ops[] = { @@ -165,6 +167,7 @@ static Operator ops[] = { [OP_CHANGE] = { op_change, false }, [OP_YANK] = { op_yank, false }, [OP_PUT] = { op_put, true }, + [OP_SHIFT_RIGHT] = { op_shift_right, false }, }; #define PAGE INT_MAX @@ -465,6 +468,28 @@ static void op_put(OperatorContext *c) { window_cursor_to(vis->win->win, pos + c->reg->len); } +static const char *expand_tab(void) { + return "\t"; +} + +static void op_shift_right(OperatorContext *c) { + Text *txt = vis->win->text; + size_t pos = text_line_begin(txt, c->range.end), prev_pos; + const char *tab = expand_tab(); + size_t tablen = strlen(tab); + + /* if range ends at the begin of a line, skip line break */ + if (pos == c->range.end) + pos = text_line_prev(txt, pos); + + do { + prev_pos = pos = text_line_begin(txt, pos); + text_insert(txt, pos, tab, tablen); + pos = text_line_prev(txt, pos); + } while (pos >= c->range.start && pos != prev_pos); + editor_draw(vis); +} + /** movement implementations of type: size_t (*move)(const Arg*) */ static size_t search_forward(const Arg *arg) {