vis

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

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

commit 8d15ff90addd2fc8fbfb9ed843cfe2c99020952b
parent 74669fdfa33333b918b4df1447ef45cb9f653f32
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 13 Sep 2014 18:39:01 +0200

Add movements 'H', 'M', 'L'

H moves to the n-th window line from top
M moves to the middle window line
L moves to the n-th window line from bottom

Diffstat:
Mconfig.def.h | 3+++
Mvis.c | 24++++++++++++++++++++++++
Mwindow.c | 7+++++++
Mwindow.h | 2++
4 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -129,6 +129,9 @@ static KeyBinding vis_movements[] = { { { NONE('|') }, movement, { .i = MOVE_COLUMN } }, { { NONE('n') }, movement, { .i = MOVE_SEARCH_FORWARD } }, { { NONE('N') }, movement, { .i = MOVE_SEARCH_BACKWARD } }, + { { NONE('H') }, movement, { .i = MOVE_WINDOW_LINE_TOP } }, + { { NONE('M') }, movement, { .i = MOVE_WINDOW_LINE_MIDDLE} }, + { { NONE('L') }, movement, { .i = MOVE_WINDOW_LINE_BOTTOM} }, { { NONE('f') }, movement_key, { .i = MOVE_RIGHT_TO } }, { { NONE('F') }, movement_key, { .i = MOVE_LEFT_TO } }, { { NONE('t') }, movement_key, { .i = MOVE_RIGHT_TILL } }, diff --git a/vis.c b/vis.c @@ -196,6 +196,9 @@ enum { MOVE_MARK_LINE, MOVE_SEARCH_FORWARD, MOVE_SEARCH_BACKWARD, + MOVE_WINDOW_LINE_TOP, + MOVE_WINDOW_LINE_MIDDLE, + MOVE_WINDOW_LINE_BOTTOM, }; /** movements which can be used besides the one in text-motions.h and window.h */ @@ -218,6 +221,12 @@ static size_t till_left(const Arg *arg); static size_t line(const Arg *arg); /* goto to byte action.count on current line */ static size_t column(const Arg *arg); +/* goto the action.count-th line from top of the focused window */ +static size_t window_line_top(const Arg *arg); +/* goto the start of middle line of the focused window */ +static size_t window_line_middle(const Arg *arg); +/* goto the action.count-th line from bottom of the focused window */ +static size_t window_line_bottom(const Arg *arg); static Movement moves[] = { [MOVE_LINE_UP] = { .win = window_line_up }, @@ -251,6 +260,9 @@ static Movement moves[] = { [MOVE_MARK_LINE] = { .cmd = mark_line_goto, .type = LINEWISE }, [MOVE_SEARCH_FORWARD] = { .cmd = search_forward, .type = LINEWISE }, [MOVE_SEARCH_BACKWARD] = { .cmd = search_backward, .type = LINEWISE }, + [MOVE_WINDOW_LINE_TOP] = { .cmd = window_line_top, .type = LINEWISE }, + [MOVE_WINDOW_LINE_MIDDLE] = { .cmd = window_line_middle,.type = LINEWISE }, + [MOVE_WINDOW_LINE_BOTTOM] = { .cmd = window_line_bottom,.type = LINEWISE }, }; /* these can be passed as int argument to textobj(&(const Arg){ .i = TEXT_OBJ_* }) */ @@ -504,6 +516,18 @@ static size_t column(const Arg *arg) { return it.pos; } +static size_t window_line_top(const Arg *arg) { + return window_line_goto(vis->win->win, action.count); +} + +static size_t window_line_middle(const Arg *arg) { + return window_line_goto(vis->win->win, vis->win->height / 2); +} + +static size_t window_line_bottom(const Arg *arg) { + return window_line_goto(vis->win->win, vis->win->height - action.count); +} + /** key bindings functions of type: void (*func)(const Arg*) */ static void repeat(const Arg *arg) { diff --git a/window.c b/window.c @@ -749,3 +749,10 @@ void window_cursor_watch(Win *win, void (*cursor_moved)(Win*, void *), void *dat win->cursor_moved = cursor_moved; win->cursor_moved_data = data; } + +size_t window_line_goto(Win *win, int n) { + size_t pos = win->start; + for (Line *line = win->topline; --n > 0 && line != win->lastline; line = line->next) + pos += line->len; + return pos; +} diff --git a/window.h b/window.h @@ -33,6 +33,8 @@ size_t window_char_next(Win*); size_t window_char_prev(Win*); size_t window_line_down(Win*); size_t window_line_up(Win*); +/* place the cursor at the start ot the n-th window line, counting from 1 */ +size_t window_line_goto(Win*, int n); /* get cursor position in bytes from start of the file */ size_t window_cursor_get(Win*);