vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit f383ef7ab070757ab65b035f84239b6c6702e4ca parent 54acd98ef6512e67cc324f24c8e113a59e866a3b Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 13 Apr 2016 17:05:56 +0200 view: add view_cursors_new_force function To create a cursor even if there already exists one at the same position. Should only be used if all but one of the cursors will later be removed. Diffstat:
| M | view.c | | | 14 | +++++++++++--- |
| M | view.h | | | 7 | ++++++- |
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/view.c b/view.c @@ -1059,7 +1059,7 @@ size_t view_screenline_goto(View *view, int n) { return pos; } -Cursor *view_cursors_new(View *view, size_t pos) { +static Cursor *cursors_new(View *view, size_t pos, bool force) { Cursor *c = calloc(1, sizeof(*c)); if (!c) return NULL; @@ -1074,7 +1074,7 @@ Cursor *view_cursors_new(View *view, size_t pos) { Cursor *prev = NULL, *next = NULL; size_t cur = view_cursors_pos(view->cursor); - if (pos > cur) { + if (pos >= cur) { prev = view->cursor; for (next = prev->next; next; prev = next, next = next->next) { cur = view_cursors_pos(next); @@ -1090,7 +1090,7 @@ Cursor *view_cursors_new(View *view, size_t pos) { } } - if (pos == cur) + if (pos == cur && !force) goto err; for (Cursor *after = next; after; after = after->next) @@ -1115,6 +1115,14 @@ err: return NULL; } +Cursor *view_cursors_new(View *view, size_t pos) { + return cursors_new(view, pos, false); +} + +Cursor *view_cursors_new_force(View *view, size_t pos) { + return cursors_new(view, pos, true); +} + int view_cursors_count(View *view) { return view->cursor_count; } diff --git a/view.h b/view.h @@ -119,8 +119,13 @@ 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, at given position */ +/* create a new cursor at given position, fails if there already + * exists a cursor at the same position */ Cursor *view_cursors_new(View*, size_t pos); +/* create a new cursor even if there already is one located at the + * same position, this should only be used if the one of the two + * cursors will later be disposed */ +Cursor *view_cursors_new_force(View*, size_t pos); /* get number of active cursors */ int view_cursors_count(View*); /* get index/relative order at time of creation of a cursor [0,count-1] */