vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit f6f615c267b9e4c7bc1fc3abec920ba1cfeef2cb parent 084d4c57bbe2d0ca97d82310f8be38777deac4d4 Author: Marc André Tanner <mat@brain-dump.org> Date: Thu, 5 May 2016 11:08:15 +0200 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. Diffstat:
| M | view.c | | | 5 | ++++- |
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git 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) {