vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 1fe6ea75722fbcc03d2f625760a1c5551fd22d33
parent c42bb666229dfcb5e8ae0f136f86316eb64271d8
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Thu, 25 Sep 2014 12:22:07 +0200

Add text_object_word_raw which does not include any whitespaces

Diffstat:
Mtext-objects.c | 32++++++++++++++++++++++++++++++++
Mtext-objects.h | 4+++-
2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/text-objects.c b/text-objects.c @@ -51,6 +51,38 @@ Filerange text_object_word(Text *txt, size_t pos) { return r; } +Filerange text_object_word_raw(Text *txt, size_t pos) { + char c, prev = '0', next = '0'; + Filerange r = text_range_empty(); + Iterator it = text_iterator_get(txt, pos); + if (!text_iterator_byte_get(&it, &c)) + return r; + if (text_iterator_byte_prev(&it, &prev)) + text_iterator_byte_next(&it, NULL); + text_iterator_byte_next(&it, &next); + if (isspace(c)) { + return r; + } else if (isspace(prev) && isspace(next)) { + /* on a single character */ + r.start = pos; + r.end = text_char_next(txt, pos); + } else if (isspace(prev)) { + /* at start of a word */ + r.start = pos; + r.end = text_char_next(txt, text_word_end_next(txt, pos)); + } else if (isspace(next)) { + /* at end of a word */ + r.start = text_word_start_prev(txt, pos); + r.end = text_char_next(txt, pos); + } else { + /* in the middle of a word */ + r.start = text_word_start_prev(txt, pos); + r.end = text_char_next(txt, text_word_end_next(txt, pos)); + } + + return r; +} + Filerange text_object_line(Text *txt, size_t pos) { Filerange r; r.start = text_line_begin(txt, pos); diff --git a/text-objects.h b/text-objects.h @@ -13,7 +13,9 @@ * happens to be a whitespace include all neighbouring leading whitespaces * and the following word. */ Filerange text_object_word(Text*, size_t pos); -Filerange text_object_word_boundry(Text*, size_t pos, int (*isboundry)(int)); +/* word which happens to be at pos, _not_ including any white spaces. if pos + * is not part of a word, an empty range is returned */ +Filerange text_object_word_raw(Text*, size_t pos); Filerange text_object_line(Text*, size_t pos); Filerange text_object_sentence(Text*, size_t pos); Filerange text_object_paragraph(Text*, size_t pos);