From d0ed5fef6a4098a7991a7e6ab44076a423721212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 27 Jan 2016 19:00:00 +0100 Subject: vis: clean up count handling There are cases where zero can also be a legitimate count. --- main.c | 15 ++++++++------- vis-motions.c | 8 ++++---- vis.c | 24 +++++++++++++++--------- vis.h | 8 +++++--- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/main.c b/main.c index 5ef6c94..f9fea56 100644 --- a/main.c +++ b/main.c @@ -1224,7 +1224,7 @@ static const char *replace(Vis *vis, const char *keys, const Arg *arg) { static const char *count(Vis *vis, const char *keys, const Arg *arg) { int digit = keys[-1] - '0'; - int count = vis_count_get(vis); + int count = vis_count_get_default(vis, 0); if (0 <= digit && digit <= 9) { if (digit == 0 && count == 0) vis_motion(vis, VIS_MOVE_LINE_BEGIN); @@ -1234,7 +1234,7 @@ static const char *count(Vis *vis, const char *keys, const Arg *arg) { } static const char *gotoline(Vis *vis, const char *keys, const Arg *arg) { - if (vis_count_get(vis)) + if (vis_count_get(vis) != VIS_COUNT_UNKNOWN) vis_motion(vis, VIS_MOVE_LINE); else if (arg->i < 0) vis_motion(vis, VIS_MOVE_FILE_BEGIN); @@ -1366,7 +1366,7 @@ static const char *redo(Vis *vis, const char *keys, const Arg *arg) { } static const char *earlier(Vis *vis, const char *keys, const Arg *arg) { - size_t pos = text_earlier(vis_text(vis), MAX(vis_count_get(vis), 1)); + size_t pos = text_earlier(vis_text(vis), vis_count_get_default(vis, 1)); if (pos != EPOS) { view_cursor_to(vis_view(vis), pos); /* redraw all windows in case some display the same file */ @@ -1376,7 +1376,7 @@ static const char *earlier(Vis *vis, const char *keys, const Arg *arg) { } static const char *later(Vis *vis, const char *keys, const Arg *arg) { - size_t pos = text_later(vis_text(vis), MAX(vis_count_get(vis), 1)); + size_t pos = text_later(vis_text(vis), vis_count_get_default(vis, 1)); if (pos != EPOS) { view_cursor_to(vis_view(vis), pos); /* redraw all windows in case some display the same file */ @@ -1506,6 +1506,7 @@ static const char *cmd(Vis *vis, const char *keys, const Arg *arg) { } static int argi2lines(Vis *vis, const Arg *arg) { + int count = vis_count_get(vis); switch (arg->i) { case -PAGE: case +PAGE: @@ -1514,8 +1515,8 @@ static int argi2lines(Vis *vis, const Arg *arg) { case +PAGE_HALF: return view_height_get(vis_view(vis))/2; default: - if (vis_count_get(vis) > 0) - return vis_count_get(vis); + if (count != VIS_COUNT_UNKNOWN) + return count; return arg->i < 0 ? -arg->i : arg->i; } } @@ -1559,7 +1560,7 @@ static const char *openline(Vis *vis, const char *keys, const Arg *arg) { } static const char *join(Vis *vis, const char *keys, const Arg *arg) { - int count = vis_count_get(vis); + int count = vis_count_get_default(vis, 0); if (count) vis_count_set(vis, count-1); vis_operator(vis, VIS_OP_JOIN); diff --git a/vis-motions.c b/vis-motions.c index 399d51d..3278546 100644 --- a/vis-motions.c +++ b/vis-motions.c @@ -90,15 +90,15 @@ static size_t till_left(Vis *vis, Text *txt, size_t pos) { } static size_t line(Vis *vis, Text *txt, size_t pos) { - return text_pos_by_lineno(txt, vis->action.count); + return text_pos_by_lineno(txt, vis_count_get_default(vis, 1)); } static size_t column(Vis *vis, Text *txt, size_t pos) { - return text_line_offset(txt, pos, vis->action.count); + return text_line_offset(txt, pos, vis_count_get_default(vis, 0)); } static size_t view_lines_top(Vis *vis, View *view) { - return view_screenline_goto(view, vis->action.count); + return view_screenline_goto(view, vis_count_get_default(vis, 1)); } static size_t view_lines_middle(Vis *vis, View *view) { @@ -108,7 +108,7 @@ static size_t view_lines_middle(Vis *vis, View *view) { static size_t view_lines_bottom(Vis *vis, View *view) { int h = view_height_get(vis->win->view); - return view_screenline_goto(vis->win->view, h - vis->action.count); + return view_screenline_goto(vis->win->view, h - vis_count_get_default(vis, 0)); } static size_t window_changelist_next(Vis *vis, Win *win, size_t pos) { diff --git a/vis.c b/vis.c index f4ad2e4..e312caa 100644 --- a/vis.c +++ b/vis.c @@ -306,6 +306,7 @@ Vis *vis_new(Ui *ui, VisEvent *event) { vis->ui->init(vis->ui, vis); vis->tabwidth = 8; vis->expandtab = false; + action_reset(&vis->action); if (!(vis->search_pattern = text_regex_new())) goto err; if (!(vis->command_file = file_new(vis, NULL))) @@ -413,8 +414,7 @@ void action_do(Vis *vis, Action *a) { if (a->op == &vis_operators[VIS_OP_FILTER] && !vis->mode->visual) vis_mode_switch(vis, VIS_MODE_VISUAL_LINE); - if (a->count < 1) - a->count = 1; + int count = MAX(a->count, 1); bool repeatable = a->op && !vis->macro_operator; bool multiple_cursors = view_cursors_count(view) > 1; bool linewise = !(a->type & CHARWISE) && ( @@ -430,7 +430,7 @@ void action_do(Vis *vis, Action *a) { reg = &vis->registers[win->file->internal ? VIS_REG_PROMPT : VIS_REG_DEFAULT]; OperatorContext c = { - .count = a->count, + .count = count, .pos = pos, .newpos = EPOS, .range = text_range_empty(), @@ -441,7 +441,7 @@ void action_do(Vis *vis, Action *a) { if (a->movement) { size_t start = pos; - for (int i = 0; i < a->count; i++) { + for (int i = 0; i < count; i++) { if (a->movement->txt) pos = a->movement->txt(txt, pos); else if (a->movement->cur) @@ -486,7 +486,7 @@ void action_do(Vis *vis, Action *a) { c.range = view_cursors_selection_get(cursor); else c.range.start = c.range.end = pos; - for (int i = 0; i < a->count; i++) { + for (int i = 0; i < count; i++) { Filerange r = a->textobj->range(txt, pos); if (!text_range_valid(&r)) break; @@ -497,7 +497,7 @@ void action_do(Vis *vis, Action *a) { c.range = text_range_union(&c.range, &r); - if (i < a->count - 1) + if (i < count - 1) pos = c.range.end + 1; } } else if (vis->mode->visual) { @@ -581,6 +581,7 @@ void action_do(Vis *vis, Action *a) { void action_reset(Action *a) { memset(a, 0, sizeof(*a)); + a->count = VIS_COUNT_UNKNOWN; } void vis_cancel(Vis *vis) { @@ -945,7 +946,7 @@ void vis_repeat(Vis *vis) { macro = macro_repeat; vis->action_prev.macro = macro; } - if (count) + if (count != VIS_COUNT_UNKNOWN) vis->action_prev.count = count; count = vis->action_prev.count; /* for some operators count should be applied only to the macro not the motion */ @@ -978,9 +979,14 @@ int vis_count_get(Vis *vis) { return vis->action.count; } +int vis_count_get_default(Vis *vis, int def) { + if (vis->action.count == VIS_COUNT_UNKNOWN) + return def; + return vis->action.count; +} + void vis_count_set(Vis *vis, int count) { - if (count >= 0) - vis->action.count = count; + vis->action.count = (count >= 0 ? count : VIS_COUNT_UNKNOWN); } void vis_register_set(Vis *vis, enum VisRegister reg) { diff --git a/vis.h b/vis.h index 37d91c9..64713ef 100644 --- a/vis.h +++ b/vis.h @@ -252,10 +252,12 @@ enum VisMotion { */ bool vis_motion(Vis*, enum VisMotion, ...); -/* a count of zero indicates that so far no special count was given. - * operators, motions and text object will always perform their function - * as if a minimal count of 1 was given */ +/* If no count is explicitly specified, operators, motions and + * text object will always perform their function as if a minimal + * count of 1 was given */ +#define VIS_COUNT_UNKNOWN (-1) int vis_count_get(Vis*); +int vis_count_get_default(Vis*, int def); void vis_count_set(Vis*, int count); enum VisMotionType { -- cgit v1.2.3