vis

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

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

commit d7340ac030d3ed7f0d8045e569363d0acd58fdf0
parent a529ce240bbf114ec80525e8746059128e88e90e
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Tue, 19 Jan 2016 13:18:20 +0100

vis: improve selection restore `gv`

Diffstat:
Mmain.c | 10++++++++--
Mvis.c | 11+++++++++++
2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c @@ -1280,9 +1280,15 @@ static const char *selection_end(Vis *vis, const char *keys, const Arg *arg) { } static const char *selection_restore(Vis *vis, const char *keys, const Arg *arg) { - for (Cursor *c = view_cursors(vis_view(vis)); c; c = view_cursors_next(c)) + Text *txt = vis_text(vis); + View *view = vis_view(vis); + for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) view_cursors_selection_restore(c); - vis_mode_switch(vis, VIS_MODE_VISUAL); + Filerange sel = view_selection_get(view); + if (text_range_is_linewise(txt, &sel)) + vis_mode_switch(vis, VIS_MODE_VISUAL_LINE); + else + vis_mode_switch(vis, VIS_MODE_VISUAL); return keys; } diff --git a/vis.c b/vis.c @@ -519,7 +519,18 @@ void action_do(Vis *vis, Action *a) { if (pos == EPOS) { view_cursors_dispose(cursor); } else if (pos <= text_size(txt)) { + /* moving the cursor will affect the selection. + * because we want to be able to later restore + * the old selection we update it again before + * leaving visual mode. + */ + Filerange sel = view_cursors_selection_get(cursor); view_cursors_to(cursor, pos); + if (vis->mode->visual) { + if (vis->mode == &vis_modes[VIS_MODE_VISUAL_LINE]) + sel = text_range_linewise(txt, &sel); + view_cursors_selection_set(cursor, &sel); + } } } }