vis

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

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

commit a6d58ef5f5050b2e48c1ec638e946c53a92ec36a
parent bc8d4ba7551c6d95d9e5d20a86ea6c24d1d2ba70
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Tue, 28 Jul 2015 11:04:39 +0200

text-motion: introduce text_line_char_{get,set}

Diffstat:
Mtext-motions.c | 33+++++++++++++++++++++++++++------
Mtext-motions.h | 4++++
2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/text-motions.c b/text-motions.c @@ -166,6 +166,27 @@ size_t text_line_offset(Text *txt, size_t pos, size_t off) { return it.pos; } +size_t text_line_char_set(Text *txt, size_t pos, int count) { + char c; + size_t bol = text_line_begin(txt, pos); + Iterator it = text_iterator_get(txt, bol); + while (count-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n') + text_iterator_char_next(&it, NULL); + return it.pos; +} + +int text_line_char_get(Text *txt, size_t pos) { + char c; + int count = 0; + size_t bol = text_line_begin(txt, pos); + Iterator it = text_iterator_get(txt, bol); + while (text_iterator_byte_get(&it, &c) && it.pos < pos && c != '\r' && c != '\n') { + text_iterator_char_next(&it, NULL); + count++; + } + return count; +} + size_t text_line_char_next(Text *txt, size_t pos) { char c; Iterator it = text_iterator_get(txt, pos); @@ -185,15 +206,15 @@ size_t text_line_char_prev(Text *txt, size_t pos) { } size_t text_line_up(Text *txt, size_t pos) { - size_t bol = text_line_begin(txt, pos); - size_t prev = text_line_prev(txt, bol); - return text_line_offset(txt, prev, pos - bol); + int count = text_line_char_get(txt, pos); + size_t prev = text_line_prev(txt, pos); + return text_line_char_set(txt, prev, count); } size_t text_line_down(Text *txt, size_t pos) { - size_t bol = text_line_begin(txt, pos); - size_t next = text_line_next(txt, bol); - return text_line_offset(txt, next, pos - bol); + int count = text_line_char_get(txt, pos); + size_t next = text_line_next(txt, pos); + return text_line_char_set(txt, next, count); } size_t text_range_line_first(Text *txt, Filerange *r) { diff --git a/text-motions.h b/text-motions.h @@ -36,6 +36,10 @@ size_t text_line_lastchar(Text*, size_t pos); size_t text_line_end(Text*, size_t pos); size_t text_line_next(Text*, size_t pos); size_t text_line_offset(Text*, size_t pos, size_t off); +/* get character count of the line upto `pos' */ +int text_line_char_get(Text*, size_t pos); +/* get position of the `count' character in the line containing `pos' */ +size_t text_line_char_set(Text*, size_t pos, int count); /* move to the next/previous character on the same line */ size_t text_line_char_next(Text*, size_t pos); size_t text_line_char_prev(Text*, size_t pos);