vis

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

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

commit a3cee0d120b3794f53e696bda8e50ddaaa2115f7
parent 3b2ceff09853f7db640890f235f9865f6cab3ea9
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed,  3 Feb 2016 12:58:55 +0100

text-motion: hide ugly casts for isboundary behind a #define

Diffstat:
Mtext-motions.c | 33+++++++++++++++++----------------
Mtext-motions.h | 8++++----
2 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/text-motions.c b/text-motions.c @@ -20,6 +20,7 @@ #include "util.h" #define space(c) (isspace((unsigned char)c)) +#define boundary(c) (isboundary((unsigned char)c)) // TODO: specify this per file type? int is_word_boundry(int c) { @@ -279,50 +280,50 @@ size_t text_range_line_prev(Text *txt, Filerange *r, size_t pos) { return newpos != pos && r->start <= newpos ? newpos : EPOS; } -size_t text_customword_start_next(Text *txt, size_t pos, int (*isboundry)(int)) { +size_t text_customword_start_next(Text *txt, size_t pos, int (*isboundary)(int)) { char c; Iterator it = text_iterator_get(txt, pos); if (!text_iterator_byte_get(&it, &c)) return pos; - if (isboundry((unsigned char)c)) - while (isboundry((unsigned char)c) && !space(c) && text_iterator_char_next(&it, &c)); + if (boundary(c)) + while (boundary(c) && !space(c) && text_iterator_char_next(&it, &c)); else - while (!isboundry((unsigned char)c) && text_iterator_char_next(&it, &c)); + while (!boundary(c) && text_iterator_char_next(&it, &c)); while (space(c) && text_iterator_char_next(&it, &c)); return it.pos; } -size_t text_customword_start_prev(Text *txt, size_t pos, int (*isboundry)(int)) { +size_t text_customword_start_prev(Text *txt, size_t pos, int (*isboundary)(int)) { char c; Iterator it = text_iterator_get(txt, pos); while (text_iterator_char_prev(&it, &c) && space(c)); - if (isboundry((unsigned char)c)) - do pos = it.pos; while (text_iterator_char_prev(&it, &c) && isboundry((unsigned char)c) && !space(c)); + if (boundary(c)) + do pos = it.pos; while (text_iterator_char_prev(&it, &c) && boundary(c) && !space(c)); else - do pos = it.pos; while (text_iterator_char_prev(&it, &c) && !isboundry((unsigned char)c)); + do pos = it.pos; while (text_iterator_char_prev(&it, &c) && !boundary(c)); return pos; } -size_t text_customword_end_next(Text *txt, size_t pos, int (*isboundry)(int)) { +size_t text_customword_end_next(Text *txt, size_t pos, int (*isboundary)(int)) { char c; Iterator it = text_iterator_get(txt, pos); while (text_iterator_char_next(&it, &c) && space(c)); - if (isboundry((unsigned char)c)) - do pos = it.pos; while (text_iterator_char_next(&it, &c) && isboundry((unsigned char)c) && !space(c)); + if (boundary(c)) + do pos = it.pos; while (text_iterator_char_next(&it, &c) && boundary(c) && !space(c)); else - do pos = it.pos; while (text_iterator_char_next(&it, &c) && !isboundry((unsigned char)c)); + do pos = it.pos; while (text_iterator_char_next(&it, &c) && !isboundary(c)); return pos; } -size_t text_customword_end_prev(Text *txt, size_t pos, int (*isboundry)(int)) { +size_t text_customword_end_prev(Text *txt, size_t pos, int (*isboundary)(int)) { char c; Iterator it = text_iterator_get(txt, pos); if (!text_iterator_byte_get(&it, &c)) return pos; - if (isboundry((unsigned char)c)) - while (isboundry((unsigned char)c) && !space(c) && text_iterator_char_prev(&it, &c)); + if (boundary(c)) + while (boundary(c) && !space(c) && text_iterator_char_prev(&it, &c)); else - while (!isboundry((unsigned char)c) && text_iterator_char_prev(&it, &c)); + while (!boundary(c) && text_iterator_char_prev(&it, &c)); while (space(c) && text_iterator_char_prev(&it, &c)); return it.pos; } diff --git a/text-motions.h b/text-motions.h @@ -79,10 +79,10 @@ size_t text_word_start_prev(Text*, size_t pos); /* * More general versions of the above, define your own word boundaries. */ -size_t text_customword_start_next(Text*, size_t pos, int (*isboundry)(int)); -size_t text_customword_start_prev(Text*, size_t pos, int (*isboundry)(int)); -size_t text_customword_end_next(Text*, size_t pos, int (*isboundry)(int)); -size_t text_customword_end_prev(Text*, size_t pos, int (*isboundry)(int)); +size_t text_customword_start_next(Text*, size_t pos, int (*isboundary)(int)); +size_t text_customword_start_prev(Text*, size_t pos, int (*isboundary)(int)); +size_t text_customword_end_next(Text*, size_t pos, int (*isboundary)(int)); +size_t text_customword_end_prev(Text*, size_t pos, int (*isboundary)(int)); /* TODO: implement the following semantics * A sentence is defined as ending at a '.', '!' or '?' followed by either the * end of a line, or by a space or tab. Any number of closing ')', ']', '"'