From 495cc3d53cdb7182c98827f8aa0ad2726167daaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 19 Jan 2016 20:48:44 +0100 Subject: vis: fix # and * motions to only match words Word matching is currently implemented by using the \< and \> anchors of the regex(3) library part of libc. Another option would have been to use the text_object_word_find_{next,prev} functions from text-objects.c. The used search term is currently not added to the search history. Based on a patch by Markus Teich. --- vis-motions.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/vis-motions.c b/vis-motions.c index f95fcf1..0cb0252 100644 --- a/vis-motions.c +++ b/vis-motions.c @@ -1,3 +1,4 @@ +#include #include #include #include "vis-core.h" @@ -7,28 +8,30 @@ /** utility functions */ -static char *get_word_at(Text *txt, size_t pos) { +static bool search_word(Vis *vis, Text *txt, size_t pos) { + char expr[512]; Filerange word = text_object_word(txt, pos); if (!text_range_valid(&word)) - return NULL; - return text_bytes_alloc0(txt, word.start, word.end - word.start); + return false; + char *buf = text_bytes_alloc0(txt, word.start, text_range_size(&word)); + if (!buf) + return false; + snprintf(expr, sizeof(expr), "\\<%s\\>", buf); + free(buf); + return text_regex_compile(vis->search_pattern, expr, REG_EXTENDED) == 0; } /** motion implementations */ static size_t search_word_forward(Vis *vis, Text *txt, size_t pos) { - char *word = get_word_at(txt, pos); - if (word && !text_regex_compile(vis->search_pattern, word, REG_EXTENDED)) + if (search_word(vis, txt, pos)) pos = text_search_forward(txt, pos, vis->search_pattern); - free(word); return pos; } static size_t search_word_backward(Vis *vis, Text *txt, size_t pos) { - char *word = get_word_at(txt, pos); - if (word && !text_regex_compile(vis->search_pattern, word, REG_EXTENDED)) + if (search_word(vis, txt, pos)) pos = text_search_backward(txt, pos, vis->search_pattern); - free(word); return pos; } -- cgit v1.2.3