vis

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

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

commit b306d1b7bbb4dc6c8ff20b8a1314c67b54f02083
parent e43e534c14c8d8aa5dbb500548d8effc8bc3a1d4
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 14 Jan 2017 11:02:52 +0100

view: enforce invariant that cursor is within selection

A cursor does not necessarily have to be at a selection boundary
(e.g. in visual line mode) but it has to be within the selection.

Diffstat:
Mview.c | 11++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/view.c b/view.c @@ -1276,8 +1276,8 @@ void view_cursors_selection_sync(Cursor *c) { if (!c->sel) return; Text *txt = c->view->text; - size_t cursor = text_mark_get(txt, c->sel->cursor); - view_cursors_to(c, cursor); + size_t pos = text_mark_get(txt, c->sel->cursor); + view_cursors_to(c, pos); } Filerange view_cursors_selection_get(Cursor *c) { @@ -1287,12 +1287,13 @@ Filerange view_cursors_selection_get(Cursor *c) { void view_cursors_selection_set(Cursor *c, const Filerange *r) { if (!text_range_valid(r)) return; - if (!c->sel) - c->sel = view_selections_new(c->view); - if (!c->sel) + if (!c->sel && !(c->sel = view_selections_new(c->view))) return; view_selections_set(c->sel, r); + size_t pos = view_cursors_pos(c); + if (!text_range_contains(r, pos)) + view_cursors_selection_sync(c); } Selection *view_selections_new(View *view) {