From a69369cc9ca61c63f4370f29cd9c24bd07ba6e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 14 Mar 2016 13:35:51 +0100 Subject: sam: change print command behavior when given an empty range Only create a selection (and switch to visual mode) if at least one print command was given a non-empty range. Also reject invalid ranges. This allows cursor movements with thinks like #10 --- sam.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/sam.c b/sam.c index d734a77..a3b746a 100644 --- a/sam.c +++ b/sam.c @@ -597,15 +597,27 @@ enum SamError sam_cmd(Vis *vis, const char *s) { enum SamError err = SAM_ERR_OK; if (!s) return err; + Command *cmd = sam_parse(vis, s, &err); if (!cmd) { if (err == SAM_ERR_OK) err = SAM_ERR_MEMORY; return err; } + Filerange range = text_range_empty(); - bool status = sam_execute(vis, vis->win, cmd, &range); - vis_mode_switch(vis, status ? VIS_MODE_NORMAL : VIS_MODE_VISUAL); + sam_execute(vis, vis->win, cmd, &range); + + bool completed = true; + for (Cursor *c = view_cursors(vis->win->view); c; c = view_cursors_next(c)) { + Filerange sel = view_cursors_selection_get(c); + if (text_range_valid(&sel)) { + completed = false; + break; + } + } + + vis_mode_switch(vis, completed ? VIS_MODE_NORMAL : VIS_MODE_VISUAL); command_free(cmd); return err; } @@ -734,15 +746,20 @@ static bool cmd_select(Vis *vis, Win *win, Command *cmd, Filerange *range) { } static bool cmd_print(Vis *vis, Win *win, Command *cmd, Filerange *range) { + if (!text_range_valid(range)) + return false; View *view = win->view; Text *txt = win->file->text; Cursor *cursor = view_cursors_new(view); if (cursor) { - view_cursors_selection_set(cursor, range); - view_cursors_to(cursor, text_char_prev(txt, range->end)); + if (range->start != range->end) { + view_cursors_selection_set(cursor, range); + view_cursors_to(cursor, text_char_prev(txt, range->end)); + } else { + view_cursors_to(cursor, range->end); + } } - /* indicate "failure"/incomplete command to keep visual mode */ - return false; + return cursor != NULL; } static bool cmd_files(Vis *vis, Win *win, Command *cmd, Filerange *range) { -- cgit v1.2.3