From 559ab349a05d5201b666eddb22d921c40b131bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 21 Dec 2016 12:25:58 +0100 Subject: vis: implement `go` to move to absolute byte position --- config.def.h | 1 + main.c | 6 ++++++ vis-motions.c | 10 ++++++++++ vis.h | 1 + 4 files changed, 18 insertions(+) diff --git a/config.def.h b/config.def.h index cc68a84..eb01ac9 100644 --- a/config.def.h +++ b/config.def.h @@ -65,6 +65,7 @@ static const KeyBinding bindings_motions[] = { { "", ALIAS("j") }, { "F", ACTION(TO_LEFT) }, { "f", ACTION(TO_RIGHT) }, + { "go", ACTION(CURSOR_BYTE) }, { "g0", ACTION(CURSOR_SCREEN_LINE_BEGIN) }, { "g_", ACTION(CURSOR_LINE_FINISH) }, { "G", ACTION(CURSOR_LINE_LAST) }, diff --git a/main.c b/main.c index c59261b..7462e1b 100644 --- a/main.c +++ b/main.c @@ -160,6 +160,7 @@ enum { VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE, VIS_ACTION_CURSOR_SCREEN_LINE_END, VIS_ACTION_CURSOR_PERCENT, + VIS_ACTION_CURSOR_BYTE, VIS_ACTION_CURSOR_PARAGRAPH_PREV, VIS_ACTION_CURSOR_PARAGRAPH_NEXT, VIS_ACTION_CURSOR_SENTENCE_PREV, @@ -451,6 +452,11 @@ static const KeyAction vis_action[] = { "Move to count % of file or matching item", percent }, + [VIS_ACTION_CURSOR_BYTE] = { + "cursor-byte", + "Move to absolute byte position", + movement, { .i = VIS_MOVE_BYTE } + }, [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = { "cursor-paragraph-prev", "Move cursor paragraph backward", diff --git a/vis-motions.c b/vis-motions.c index a9c354c..1e9d9c5 100644 --- a/vis-motions.c +++ b/vis-motions.c @@ -221,6 +221,12 @@ static size_t percent(Vis *vis, Text *txt, size_t pos) { return text_size(txt) * ratio / 100; } +static size_t byte(Vis *vis, Text *txt, size_t pos) { + pos = vis_count_get_default(vis, 0); + size_t max = text_size(txt); + return pos <= max ? pos : max; +} + void vis_motion_type(Vis *vis, enum VisMotionType type) { vis->action.type = type; } @@ -577,4 +583,8 @@ const Movement vis_motions[] = { .vis = percent, .type = IDEMPOTENT, }, + [VIS_MOVE_BYTE] = { + .vis = byte, + .type = IDEMPOTENT, + }, }; diff --git a/vis.h b/vis.h index 69bef3b..081ae35 100644 --- a/vis.h +++ b/vis.h @@ -283,6 +283,7 @@ enum VisMotion { VIS_MOVE_JUMPLIST_PREV, VIS_MOVE_NOP, VIS_MOVE_PERCENT, + VIS_MOVE_BYTE, 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, -- cgit v1.2.3