vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit da19f08f528c07e9680e137e0aad358aaaf66b79 parent cb3d37a98ca093d7fb32361bbd1241d5bc35fb28 Author: Marc André Tanner <mat@brain-dump.org> Date: Fri, 7 Aug 2015 22:33:43 +0200 vis: limit to/till movements to current line Diffstat:
| M | text-motions.c | | | 24 | ++++++++++++++++++++++-- |
| M | text-motions.h | | | 3 | +++ |
| M | vis.c | | | 4 | ++-- |
3 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/text-motions.c b/text-motions.c @@ -46,7 +46,7 @@ size_t text_char_prev(Text *txt, size_t pos) { return it.pos; } -size_t text_find_next(Text *txt, size_t pos, const char *s) { +static size_t find_next(Text *txt, size_t pos, const char *s, bool line) { if (!s) return pos; size_t len = strlen(s), matched = 0; @@ -61,11 +61,21 @@ size_t text_find_next(Text *txt, size_t pos, const char *s) { matched = 0; } text_iterator_byte_next(&it, NULL); + if (line && c == '\n') + break; } return matched == len ? it.pos - len : pos; } -size_t text_find_prev(Text *txt, size_t pos, const char *s) { +size_t text_find_next(Text *txt, size_t pos, const char *s) { + return find_next(txt, pos, s, false); +} + +size_t text_line_find_next(Text *txt, size_t pos, const char *s) { + return find_next(txt, pos, s, true); +} + +static size_t find_prev(Text *txt, size_t pos, const char *s, bool line) { if (!s) return pos; size_t len = strlen(s), matched = len - 1; @@ -84,10 +94,20 @@ size_t text_find_prev(Text *txt, size_t pos, const char *s) { matched = len - 1; } text_iterator_byte_prev(&it, NULL); + if (line && c == '\n') + break; } return matched == 0 ? it.pos : pos; } +size_t text_find_prev(Text *txt, size_t pos, const char *s) { + return find_prev(txt, pos, s, false); +} + +size_t text_line_find_prev(Text *txt, size_t pos, const char *s) { + return find_prev(txt, pos, s, true); +} + size_t text_line_prev(Text *txt, size_t pos) { char c; Iterator it = text_iterator_get(txt, pos); diff --git a/text-motions.h b/text-motions.h @@ -21,6 +21,9 @@ size_t text_char_prev(Text*, size_t pos); * original position */ size_t text_find_next(Text*, size_t pos, const char *s); size_t text_find_prev(Text*, size_t pos, const char *s); +/* same as above but limit searched range to the line containing pos */ +size_t text_line_find_next(Text*, size_t pos, const char *s); +size_t text_line_find_prev(Text*, size_t pos, const char *s); /* begin finish end next * v v v v diff --git a/vis.c b/vis.c @@ -740,7 +740,7 @@ static size_t mark_line_goto(File *txt, size_t pos) { static size_t to(Text *txt, size_t pos) { char c; - size_t hit = text_find_next(txt, pos+1, vis->search_char); + size_t hit = text_line_find_next(txt, pos+1, vis->search_char); if (!text_byte_get(txt, hit, &c) || c != vis->search_char[0]) return pos; return hit; @@ -757,7 +757,7 @@ static size_t to_left(Text *txt, size_t pos) { char c; if (pos == 0) return pos; - size_t hit = text_find_prev(txt, pos-1, vis->search_char); + size_t hit = text_line_find_prev(txt, pos-1, vis->search_char); if (!text_byte_get(txt, hit, &c) || c != vis->search_char[0]) return pos; return hit;