vis

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

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

commit e9464bf39dc67dfbd3a5af1d1b84b82cbd4e282e
parent e627df41fb121f1db75902406d05d91074718fab
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed,  9 Nov 2016 14:50:43 +0100

vis: improve `r` in normal and replace mode

In normal mode `r<key>` was previously implemented as `R<key><Escape>`.
However this does not work when the replacement key is `<Enter>` to insert
a new line, because in replace mode new lines are not overwritten.

The count is now also respected.

Also properly support `r` in visual mode where before it was aliased to `c`.

Fix #190

Diffstat:
Mconfig.def.h | 2+-
Mmain.c | 23++++++++++++++++++++---
2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -288,7 +288,7 @@ static const KeyBinding bindings_visual[] = { { "J", ACTION(JOIN_LINES) }, { "gJ", ACTION(JOIN_LINES_TRIM) }, { "o", ACTION(SELECTION_FLIP) }, - { "r", ALIAS("c") }, + { "r", ACTION(REPLACE_CHAR) }, { "s", ALIAS("c") }, { "<S-Tab>", ACTION(CURSORS_ALIGN_INDENT_RIGHT) }, { "<Tab>", ACTION(CURSORS_ALIGN_INDENT_LEFT) }, diff --git a/main.c b/main.c @@ -1634,12 +1634,29 @@ static const char *replace(Vis *vis, const char *keys, const Arg *arg) { vis_keymap_disable(vis); return NULL; } + const char *next = vis_keys_next(vis, keys); if (!next) return NULL; - vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_REPLACE); - vis_motion(vis, VIS_MOVE_NOP); - vis_keys_feed(vis, keys); + + char replacement[64]; + size_t len = next - keys; + if (len >= sizeof(replacement)) + return next; + + memcpy(replacement, keys, len); + replacement[len] = '\0'; + + if (vis_mode_get(vis) == VIS_MODE_NORMAL) { + int count = vis_count_get_default(vis, 1); + vis_operator(vis, VIS_OP_CHANGE); + vis_motion(vis, VIS_MOVE_CHAR_NEXT); + for (; count > 0; count--) + vis_keys_feed(vis, replacement); + } else { + vis_operator(vis, VIS_OP_REPLACE, replacement); + } + vis_keys_feed(vis, "<Escape>"); return next; }