vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 66fd017b22d1feced8403a9a6dc9e4d9e3b40405
parent 71c1c1be3a42797a3b2a813f4515abcd455f390b
Author: Marc André Tanner <mat@brain-dump.org>
Date: Wed, 9 Nov 2016 11:58:29 +0100
vis: unify VIS_OP_{INSERT,REPLACE} implementation
They both perform a motion before changing mode.
Diffstat:
| M | main.c | | | 6 | +++--- |
| M | vis-core.h | | | 1 | + |
| M | vis-modes.c | | | 6 | ++++-- |
| M | vis-operators.c | | | 12 | +++++------- |
| M | vis.c | | | 8 | ++++---- |
| M | vis.h | | | 4 | ++-- |
6 files changed, 19 insertions(+), 18 deletions(-)
diff --git a/main.c b/main.c @@ -1637,7 +1637,7 @@ static const char *replace(Vis *vis, const char *keys, const Arg *arg) { const char *next = vis_keys_next(vis, keys); if (!next) return NULL; - vis_operator(vis, VIS_OP_REPLACE); + vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_REPLACE); vis_motion(vis, VIS_MOVE_NOP); vis_keys_feed(vis, keys); vis_keys_feed(vis, "<Escape>"); @@ -1967,7 +1967,7 @@ static const char *window(Vis *vis, const char *keys, const Arg *arg) { } static const char *openline(Vis *vis, const char *keys, const Arg *arg) { - vis_operator(vis, VIS_OP_INSERT); + vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT); if (arg->i > 0) { vis_motion(vis, VIS_MOVE_LINE_END); vis_keys_feed(vis, "<insert-newline>"); @@ -2000,7 +2000,7 @@ static const char *switchmode(Vis *vis, const char *keys, const Arg *arg) { } static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) { - vis_operator(vis, VIS_OP_INSERT); + vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT); vis_motion(vis, arg->i); return keys; } diff --git a/vis-core.h b/vis-core.h @@ -95,6 +95,7 @@ typedef Buffer Macro; typedef struct { /** collects all information until an operator is executed */ int count; + enum VisMode mode; enum VisMotionType type; const Operator *op; const Movement *movement; diff --git a/vis-modes.c b/vis-modes.c @@ -122,7 +122,8 @@ static void vis_mode_insert_enter(Vis *vis, Mode *old) { return; if (!vis->action.op) { action_reset(&vis->action_prev); - vis->action_prev.op = &vis_operators[VIS_OP_INSERT]; + vis->action_prev.op = &vis_operators[VIS_OP_MODESWITCH]; + vis->action_prev.mode = VIS_MODE_INSERT; } if (!vis->macro_operator) { macro_operator_record(vis); @@ -150,7 +151,8 @@ static void vis_mode_replace_enter(Vis *vis, Mode *old) { return; if (!vis->action.op) { action_reset(&vis->action_prev); - vis->action_prev.op = &vis_operators[VIS_OP_REPLACE]; + vis->action_prev.op = &vis_operators[VIS_OP_MODESWITCH]; + vis->action_prev.mode = VIS_MODE_REPLACE; } if (!vis->macro_operator) { macro_operator_record(vis); diff --git a/vis-operators.c b/vis-operators.c @@ -214,11 +214,7 @@ static size_t op_join(Vis *vis, Text *txt, OperatorContext *c) { return newpos != EPOS ? newpos : c->range.start; } -static size_t op_insert(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) { +static size_t op_modeswitch(Vis *vis, Text *txt, OperatorContext *c) { return c->newpos != EPOS ? c->newpos : c->pos; } @@ -231,6 +227,9 @@ bool vis_operator(Vis *vis, enum VisOperator id, ...) { va_start(ap, id); switch (id) { + case VIS_OP_MODESWITCH: + vis->action.mode = va_arg(ap, int); + break; case VIS_OP_CASE_LOWER: case VIS_OP_CASE_UPPER: case VIS_OP_CASE_SWAP: @@ -304,8 +303,7 @@ const Operator vis_operators[] = { [VIS_OP_SHIFT_LEFT] = { op_shift_left }, [VIS_OP_CASE_SWAP] = { op_case_change }, [VIS_OP_JOIN] = { op_join }, - [VIS_OP_INSERT] = { op_insert }, - [VIS_OP_REPLACE] = { op_replace }, + [VIS_OP_MODESWITCH] = { op_modeswitch }, [VIS_OP_CURSOR_SOL] = { op_cursor }, [VIS_OP_FILTER] = { op_filter }, }; diff --git a/vis.c b/vis.c @@ -704,10 +704,10 @@ void vis_do(Vis *vis) { /* operator implementations must not change the mode, * they might get called multiple times (once for every cursor) */ - if (a->op == &vis_operators[VIS_OP_INSERT] || a->op == &vis_operators[VIS_OP_CHANGE]) { + if (a->op == &vis_operators[VIS_OP_CHANGE]) { vis_mode_switch(vis, VIS_MODE_INSERT); - } else if (a->op == &vis_operators[VIS_OP_REPLACE]) { - vis_mode_switch(vis, VIS_MODE_REPLACE); + } else if (a->op == &vis_operators[VIS_OP_MODESWITCH]) { + vis_mode_switch(vis, a->mode); } else if (a->op == &vis_operators[VIS_OP_FILTER]) { if (a->arg.s) vis_cmd(vis, a->arg.s); @@ -1127,7 +1127,7 @@ void vis_repeat(Vis *vis) { vis->action_prev.count = count; count = vis->action_prev.count; /* for some operators count should be applied only to the macro not the motion */ - if (vis->action_prev.op == &vis_operators[VIS_OP_INSERT] || vis->action_prev.op == &vis_operators[VIS_OP_REPLACE]) + if (vis->action_prev.op == &vis_operators[VIS_OP_MODESWITCH]) vis->action_prev.count = 1; vis->action = vis->action_prev; vis_do(vis); diff --git a/vis.h b/vis.h @@ -182,8 +182,7 @@ enum VisOperator { VIS_OP_SHIFT_RIGHT, VIS_OP_SHIFT_LEFT, VIS_OP_JOIN, - VIS_OP_INSERT, - VIS_OP_REPLACE, + VIS_OP_MODESWITCH, VIS_OP_CURSOR_SOL, VIS_OP_CASE_SWAP, VIS_OP_FILTER, @@ -208,6 +207,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 */ bool vis_operator(Vis*, enum VisOperator, ...);