From 54b734939eea44ae84578daaa8e67f8110da7e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Thu, 5 May 2016 11:08:15 +0200 Subject: view: try to recover from invalid cursor positions A cursor is a mark, if the text containing the mark is removed the cursor is lost. In this case we try to fall back to the previously known cursor position/mark. This should improve undo operations for filter commands. --- view.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'view.c') diff --git a/view.c b/view.c index 91df559..26821c3 100644 --- a/view.c +++ b/view.c @@ -54,6 +54,7 @@ struct Cursor { /* cursor position */ int lastcol; /* remembered column used when moving across lines */ Line *line; /* screen line on which cursor currently resides */ Mark mark; /* mark used to keep track of current cursor position */ + Mark mark_old; /* previous value of the mark, used to recover cursor position */ Selection *sel; /* selection (if any) which folows the cursor upon movement */ Mark lastsel_anchor;/* previously used selection data, */ Mark lastsel_cursor;/* used to restore it */ @@ -434,6 +435,7 @@ static bool view_addch(View *view, Cell *cell) { static void cursor_to(Cursor *c, size_t pos) { Text *txt = c->view->text; + c->mark_old = c->mark; c->mark = text_mark_set(txt, pos); if (pos != c->pos) c->lastcol = 0; @@ -1256,7 +1258,8 @@ Cursor *view_cursors_next(Cursor *c) { } size_t view_cursors_pos(Cursor *c) { - return text_mark_get(c->view->text, c->mark); + size_t pos = text_mark_get(c->view->text, c->mark); + return pos != EPOS ? pos : text_mark_get(c->view->text, c->mark_old); } size_t view_cursors_line(Cursor *c) { -- cgit v1.2.3