diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-01-19 20:48:44 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-01-19 20:48:44 +0100 |
| commit | 495cc3d53cdb7182c98827f8aa0ad2726167daaf (patch) | |
| tree | 5444016289d2e8f72a37af70baa6d81ef8b9300e /vis-motions.c | |
| parent | 7a97daac539cb03416cb7b95a811c2aef1348402 (diff) | |
| download | vis-495cc3d53cdb7182c98827f8aa0ad2726167daaf.tar.gz vis-495cc3d53cdb7182c98827f8aa0ad2726167daaf.tar.xz | |
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.
Diffstat (limited to 'vis-motions.c')
| -rw-r--r-- | vis-motions.c | 21 |
1 files 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 <stdio.h> #include <string.h> #include <regex.h> #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; } |
