diff options
| -rw-r--r-- | text-motions.c | 24 | ||||
| -rw-r--r-- | text-motions.h | 3 | ||||
| -rw-r--r-- | vis.c | 4 |
3 files changed, 27 insertions, 4 deletions
diff --git a/text-motions.c b/text-motions.c index 97012bd..aa779b2 100644 --- 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 index ce3c5d5..d65bdf2 100644 --- 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 @@ -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; |
