vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit f5952874cc5f18ccb3d573a70167040a24f69e8a
parent 5110f4e238b67b24cdf16686a9f35c908f3c9191
Author: Marc André Tanner <mat@brain-dump.org>
Date: Tue, 19 Jan 2016 20:48:44 +0100
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:
| M | vis-motions.c | | | 21 | ++++++++++++--------- |
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git 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; }