From 175ff92f9fec55d89b99cc03ad7261f6b5877a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 11 Dec 2017 10:33:38 +0100 Subject: vis: improve `cw` behavior Correctly handle single letter words surrounded by special symbols, e.g. [c]. Fix #643 --- vis-motions.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'vis-motions.c') diff --git a/vis-motions.c b/vis-motions.c index 5aa0e87..b72f280 100644 --- a/vis-motions.c +++ b/vis-motions.c @@ -1,5 +1,6 @@ #include #include +#include #include "vis-core.h" #include "text-motions.h" #include "text-objects.h" @@ -60,16 +61,18 @@ static size_t search_backward(Vis *vis, Text *txt, size_t pos) { return pos; } -static size_t common_word_next(Vis *vis, Text *txt, size_t pos, enum VisMotion end_next) { +static size_t common_word_next(Vis *vis, Text *txt, size_t pos, + enum VisMotion start_next, enum VisMotion end_next, + int (*isboundary)(int)) { char c; Iterator it = text_iterator_get(txt, pos); if (!text_iterator_byte_get(&it, &c)) return pos; const Movement *motion = NULL; int count = vis_count_get_default(vis, 1); - if (c == ' ' || c == '\t') { - motion = &vis_motions[VIS_MOVE_WORD_START_NEXT]; - } else if (text_iterator_char_next(&it, &c) && (c == ' ' || c == '\t')) { + if (isspace((unsigned char)c)) { + motion = &vis_motions[start_next]; + } else if (!isboundary((unsigned char)c) && text_iterator_char_next(&it, &c) && isboundary((unsigned char)c)) { /* we are on the last character of a word */ if (count == 1) { /* map `cw` to `cl` */ @@ -100,11 +103,13 @@ static size_t common_word_next(Vis *vis, Text *txt, size_t pos, enum VisMotion e } static size_t word_next(Vis *vis, Text *txt, size_t pos) { - return common_word_next(vis, txt, pos, VIS_MOVE_WORD_END_NEXT); + return common_word_next(vis, txt, pos, VIS_MOVE_WORD_START_NEXT, + VIS_MOVE_WORD_END_NEXT, is_word_boundary); } static size_t longword_next(Vis *vis, Text *txt, size_t pos) { - return common_word_next(vis, txt, pos, VIS_MOVE_LONGWORD_END_NEXT); + return common_word_next(vis, txt, pos, VIS_MOVE_LONGWORD_START_NEXT, + VIS_MOVE_LONGWORD_END_NEXT, isspace); } static size_t to(Vis *vis, Text *txt, size_t pos) { -- cgit v1.2.3