vis

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

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

commit 340d950c1e3f0297dbfabc97641752393c619802
parent fba1289400b5c1e69e2560e625bf166600ef202a
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Fri,  3 Jul 2015 18:10:05 +0200

Add movements to next/previous character within same line

These movements always keep the cursor on the same line and do
not move over newlines.

Diffstat:
Mtext-motions.c | 18++++++++++++++++++
Mtext-motions.h | 3+++
Mvis.c | 4++++
3 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/text-motions.c b/text-motions.c @@ -165,6 +165,24 @@ size_t text_line_offset(Text *txt, size_t pos, size_t off) { return it.pos; } +size_t text_line_char_next(Text *txt, size_t pos) { + char c; + Iterator it = text_iterator_get(txt, pos); + if (!text_iterator_byte_get(&it, &c) || c == '\r' || c == '\n') + return pos; + if (!text_iterator_char_next(&it, &c) || c == '\r' || c == '\n') + return pos; + return it.pos; +} + +size_t text_line_char_prev(Text *txt, size_t pos) { + char c; + Iterator it = text_iterator_get(txt, pos); + if (!text_iterator_char_prev(&it, &c) || c == '\n') + return pos; + return it.pos; +} + static size_t text_customword_start_next(Text *txt, size_t pos, int (*isboundry)(int)) { char c; Iterator it = text_iterator_get(txt, pos); diff --git a/text-motions.h b/text-motions.h @@ -35,6 +35,9 @@ 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); +/* 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); /* * A longword consists of a sequence of non-blank characters, separated with * white space. TODO?: An empty line is also considered to be a word. diff --git a/vis.c b/vis.c @@ -105,6 +105,8 @@ enum { MOVE_COLUMN, MOVE_CHAR_PREV, MOVE_CHAR_NEXT, + MOVE_LINE_CHAR_PREV, + MOVE_LINE_CHAR_NEXT, MOVE_WORD_START_NEXT, MOVE_WORD_END_PREV, MOVE_WORD_END_NEXT, @@ -186,6 +188,8 @@ static Movement moves[] = { [MOVE_COLUMN] = { .txt = column, .type = CHARWISE|IDEMPOTENT}, [MOVE_CHAR_PREV] = { .txt = text_char_prev }, [MOVE_CHAR_NEXT] = { .txt = text_char_next }, + [MOVE_LINE_CHAR_PREV] = { .txt = text_line_char_prev, .type = CHARWISE }, + [MOVE_LINE_CHAR_NEXT] = { .txt = text_line_char_next, .type = CHARWISE }, [MOVE_WORD_START_PREV] = { .txt = text_word_start_prev, .type = CHARWISE }, [MOVE_WORD_START_NEXT] = { .txt = text_word_start_next, .type = CHARWISE }, [MOVE_WORD_END_PREV] = { .txt = text_word_end_prev, .type = CHARWISE|INCLUSIVE },