From f9a147a50f33f0e27858bddead52d60ce8b46591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sat, 14 Jan 2017 11:02:52 +0100 Subject: 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. --- view.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'view.c') diff --git a/view.c b/view.c index 5352a0f..8b7238e 100644 --- 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) { -- cgit v1.2.3