From 8965cd21386683cae680ba7de4b6a6ee6687e17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sun, 27 Mar 2016 16:14:48 +0200 Subject: view: change cursor creation API to take an initial position --- main.c | 22 +++++++++++----------- view.c | 7 ++++--- view.h | 4 ++-- vis-operators.c | 15 ++++++--------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index 95786be..bbb8f10 100644 --- a/main.c +++ b/main.c @@ -1190,15 +1190,15 @@ static const char *repeat(Vis *vis, const char *keys, const Arg *arg) { static const char *cursors_new(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); - size_t pos = view_cursor_get(view); - Cursor *cursor = view_cursors_new(view); - if (cursor) { - view_cursors_to(cursor, pos); - if (arg->i > 0) - view_line_down(cursor); - else if (arg->i < 0) - view_line_up(cursor); - } + Cursor *cursor = view_cursors_primary_get(view); + size_t oldpos = view_cursors_pos(cursor); + if (arg->i > 0) + view_line_down(cursor); + else if (arg->i < 0) + view_line_up(cursor); + size_t newpos = view_cursors_pos(cursor); + view_cursors_to(cursor, oldpos); + view_cursors_new(view, newpos); return keys; } @@ -1294,11 +1294,11 @@ static const char *cursors_select_next(Vis *vis, const char *keys, const Arg *ar free(buf); if (text_range_valid(&word)) { - cursor = view_cursors_new(view); + size_t pos = text_char_prev(txt, word.end); + cursor = view_cursors_new(view, pos); if (!cursor) return keys; view_cursors_selection_set(cursor, &word); - view_cursors_to(cursor, text_char_prev(txt, word.end)); } return keys; } diff --git a/view.c b/view.c index b33263e..0d625f9 100644 --- a/view.c +++ b/view.c @@ -737,7 +737,7 @@ View *view_new(Text *text, lua_State *lua) { View *view = calloc(1, sizeof(View)); if (!view) return NULL; - if (!view_cursors_new(view)) { + if (!view_cursors_new(view, EPOS)) { view_free(view); return NULL; } @@ -1051,17 +1051,18 @@ size_t view_screenline_goto(View *view, int n) { return pos; } -Cursor *view_cursors_new(View *view) { +Cursor *view_cursors_new(View *view, size_t pos) { Cursor *c = calloc(1, sizeof(*c)); if (!c) return NULL; - c->view = view; c->next = view->cursors; if (view->cursors) view->cursors->prev = c; view->cursors = c; view->cursor = c; + if (pos != EPOS) + view_cursors_to(c, pos); return c; } diff --git a/view.h b/view.h index 621543e..e408cf7 100644 --- a/view.h +++ b/view.h @@ -119,8 +119,8 @@ void view_scroll_to(View*, size_t pos); * position is visible. if the position is in the middle of a line, try to * adjust the viewport in such a way that the whole line is displayed */ void view_cursor_to(View*, size_t pos); -/* create a new cursor */ -Cursor *view_cursors_new(View*); +/* create a new cursor, at given position */ +Cursor *view_cursors_new(View*, size_t pos); /* get number of active cursors */ int view_cursors_count(View*); /* exist there more than 1 cursor */ diff --git a/vis-operators.c b/vis-operators.c index f9e2ec9..0be30ad 100644 --- a/vis-operators.c +++ b/vis-operators.c @@ -169,15 +169,12 @@ static size_t op_cursor(Vis *vis, Text *txt, OperatorContext *c) { View *view = vis->win->view; Filerange r = text_range_linewise(txt, &c->range); for (size_t line = text_range_line_first(txt, &r); line != EPOS; line = text_range_line_next(txt, &r, line)) { - Cursor *cursor = view_cursors_new(view); - if (cursor) { - size_t pos; - if (c->arg->i == VIS_OP_CURSOR_EOL) - pos = text_line_finish(txt, line); - else - pos = text_line_start(txt, line); - view_cursors_to(cursor, pos); - } + size_t pos; + if (c->arg->i == VIS_OP_CURSOR_EOL) + pos = text_line_finish(txt, line); + else + pos = text_line_start(txt, line); + view_cursors_new(view, pos); } return EPOS; } -- cgit v1.2.3