vis

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

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

commit 502340a5f7d27b71126cd84293ccdb0381b5ff24
parent 66fd017b22d1feced8403a9a6dc9e4d9e3b40405
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed,  9 Nov 2016 12:56:25 +0100

vis: add replace operator VIS_OP_REPLACE

Delete the given range and insert the same number of replacement characters.

Diffstat:
Mvis-operators.c | 21+++++++++++++++++++++
Mvis.h | 2++
2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/vis-operators.c b/vis-operators.c @@ -218,6 +218,18 @@ static size_t op_modeswitch(Vis *vis, Text *txt, OperatorContext *c) { return c->newpos != EPOS ? c->newpos : c->pos; } +static size_t op_replace(Vis *vis, Text *txt, OperatorContext *c) { + size_t count = 0; + Iterator it = text_iterator_get(txt, c->range.start); + while (it. pos < c->range.end && text_iterator_char_next(&it, NULL)) + count++; + op_delete(vis, txt, c); + size_t pos = c->range.start; + for (size_t len = strlen(c->arg->s); count > 0; pos += len, count--) + text_insert(txt, pos, c->arg->s, len); + return pos; +} + static size_t op_filter(Vis *vis, Text *txt, OperatorContext *c) { return text_size(txt) + 1; /* do not change cursor position, would destroy selection */ } @@ -258,6 +270,14 @@ bool vis_operator(Vis *vis, enum VisOperator id, ...) { case VIS_OP_SHIFT_RIGHT: vis_motion_type(vis, VIS_MOTIONTYPE_LINEWISE); break; + case VIS_OP_REPLACE: + { + Macro *macro = &vis->registers[VIS_MACRO_OPERATOR].buf; + macro_reset(macro); + macro_append(macro, va_arg(ap, char*)); + vis->action.arg.s = macro->data; + break; + } default: break; } @@ -304,6 +324,7 @@ const Operator vis_operators[] = { [VIS_OP_CASE_SWAP] = { op_case_change }, [VIS_OP_JOIN] = { op_join }, [VIS_OP_MODESWITCH] = { op_modeswitch }, + [VIS_OP_REPLACE] = { op_replace }, [VIS_OP_CURSOR_SOL] = { op_cursor }, [VIS_OP_FILTER] = { op_filter }, }; diff --git a/vis.h b/vis.h @@ -183,6 +183,7 @@ enum VisOperator { VIS_OP_SHIFT_LEFT, VIS_OP_JOIN, VIS_OP_MODESWITCH, + VIS_OP_REPLACE, VIS_OP_CURSOR_SOL, VIS_OP_CASE_SWAP, VIS_OP_FILTER, @@ -208,6 +209,7 @@ enum VisOperator { * - VIS_OP_FILTER a char pointer referring to the command to run * - VIS_OP_JOIN a char pointer referring to the text to insert between lines * - VIS_OP_MODESWITCH a enum VisMode constant indicating the mode to switch to + * - VIS_OP_REPLACE a char pointer reffering to the replacement character */ bool vis_operator(Vis*, enum VisOperator, ...);