diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-07-27 15:09:28 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-07-28 13:21:50 +0200 |
| commit | c6031d7f1f3278979c9f5c9f4802b4e1f1cbd683 (patch) | |
| tree | 8997214f3e8a5249452b41c4bebd8aed03dce4bf /text-motions.c | |
| parent | e15edaaa06b9a08aa4131588da6aa9adca610ea6 (diff) | |
| download | vis-c6031d7f1f3278979c9f5c9f4802b4e1f1cbd683.tar.gz vis-c6031d7f1f3278979c9f5c9f4802b4e1f1cbd683.tar.xz | |
text-motion: add functions to iterate over lines of a range
Diffstat (limited to 'text-motions.c')
| -rw-r--r-- | text-motions.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/text-motions.c b/text-motions.c index 43077e9..f62ba22 100644 --- a/text-motions.c +++ b/text-motions.c @@ -16,6 +16,7 @@ #include <ctype.h> #include <string.h> #include "text-motions.h" +#include "text-util.h" #include "util.h" // TODO: specify this per file type? @@ -195,6 +196,38 @@ size_t text_line_down(Text *txt, size_t pos) { return text_line_offset(txt, next, pos - bol); } +size_t text_range_line_first(Text *txt, Filerange *r) { + if (!text_range_valid(r)) + return EPOS; + return r->start; +} + +size_t text_range_line_last(Text *txt, Filerange *r) { + if (!text_range_valid(r)) + return EPOS; + size_t pos = text_line_begin(txt, r->end); + if (pos == r->end) { + /* range ends at a begin of a line, skip last line ending */ + pos = text_line_prev(txt, pos); + pos = text_line_begin(txt, pos); + } + return r->start <= pos ? pos : r->start; +} + +size_t text_range_line_next(Text *txt, Filerange *r, size_t pos) { + if (!text_range_contains(r, pos)) + return EPOS; + size_t newpos = text_line_next(txt, pos); + return newpos != pos && newpos < r->end ? newpos : EPOS; +} + +size_t text_range_line_prev(Text *txt, Filerange *r, size_t pos) { + if (!text_range_contains(r, pos)) + return EPOS; + size_t newpos = text_line_begin(txt, text_line_prev(txt, pos)); + return newpos != pos && r->start <= newpos ? newpos : EPOS; +} + static size_t text_customword_start_next(Text *txt, size_t pos, int (*isboundry)(int)) { char c; Iterator it = text_iterator_get(txt, pos); |
