vis

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

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

commit f08959a8a54f069ec5c69ebd47517452efdd6906
parent fd1d2dc30eef7114a49094fcd4d888802d4533ea
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed, 21 Dec 2016 12:53:40 +0100

vis: implement `gh` and `gl` to move by relative byte offsets

Diffstat:
Mconfig.def.h | 2++
Mmain.c | 12++++++++++++
Mvis-motions.c | 20++++++++++++++++++++
Mvis.h | 2++
4 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -66,6 +66,8 @@ static const KeyBinding bindings_motions[] = { { "F", ACTION(TO_LEFT) }, { "f", ACTION(TO_RIGHT) }, { "go", ACTION(CURSOR_BYTE) }, + { "gh", ACTION(CURSOR_BYTE_LEFT) }, + { "gl", ACTION(CURSOR_BYTE_RIGHT) }, { "g0", ACTION(CURSOR_SCREEN_LINE_BEGIN) }, { "g_", ACTION(CURSOR_LINE_FINISH) }, { "G", ACTION(CURSOR_LINE_LAST) }, diff --git a/main.c b/main.c @@ -161,6 +161,8 @@ enum { VIS_ACTION_CURSOR_SCREEN_LINE_END, VIS_ACTION_CURSOR_PERCENT, VIS_ACTION_CURSOR_BYTE, + VIS_ACTION_CURSOR_BYTE_LEFT, + VIS_ACTION_CURSOR_BYTE_RIGHT, VIS_ACTION_CURSOR_PARAGRAPH_PREV, VIS_ACTION_CURSOR_PARAGRAPH_NEXT, VIS_ACTION_CURSOR_SENTENCE_PREV, @@ -457,6 +459,16 @@ static const KeyAction vis_action[] = { "Move to absolute byte position", movement, { .i = VIS_MOVE_BYTE } }, + [VIS_ACTION_CURSOR_BYTE_LEFT] = { + "cursor-byte-left", + "Move count bytes to the left", + movement, { .i = VIS_MOVE_BYTE_LEFT } + }, + [VIS_ACTION_CURSOR_BYTE_RIGHT] = { + "cursor-byte-right", + "Move count bytes to the right", + movement, { .i = VIS_MOVE_BYTE_RIGHT } + }, [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = { "cursor-paragraph-prev", "Move cursor paragraph backward", diff --git a/vis-motions.c b/vis-motions.c @@ -227,6 +227,18 @@ static size_t byte(Vis *vis, Text *txt, size_t pos) { return pos <= max ? pos : max; } +static size_t byte_left(Vis *vis, Text *txt, size_t pos) { + size_t off = vis_count_get_default(vis, 1); + return off <= pos ? pos-off : 0; +} + +static size_t byte_right(Vis *vis, Text *txt, size_t pos) { + size_t off = vis_count_get_default(vis, 1); + size_t new = pos + off; + size_t max = text_size(txt); + return new <= max && new > pos ? new : max; +} + void vis_motion_type(Vis *vis, enum VisMotionType type) { vis->action.type = type; } @@ -587,4 +599,12 @@ const Movement vis_motions[] = { .vis = byte, .type = IDEMPOTENT, }, + [VIS_MOVE_BYTE_LEFT] = { + .vis = byte_left, + .type = IDEMPOTENT, + }, + [VIS_MOVE_BYTE_RIGHT] = { + .vis = byte_right, + .type = IDEMPOTENT, + }, }; diff --git a/vis.h b/vis.h @@ -284,6 +284,8 @@ enum VisMotion { VIS_MOVE_NOP, VIS_MOVE_PERCENT, VIS_MOVE_BYTE, + VIS_MOVE_BYTE_LEFT, + VIS_MOVE_BYTE_RIGHT, VIS_MOVE_INVALID, /* denotes the end of the "real" motions */ /* pseudo motions: keep them at the end to save space in array definition */ VIS_MOVE_TOTILL_REPEAT,