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 | |
| parent | e15edaaa06b9a08aa4131588da6aa9adca610ea6 (diff) | |
| download | vis-c6031d7f1f3278979c9f5c9f4802b4e1f1cbd683.tar.gz vis-c6031d7f1f3278979c9f5c9f4802b4e1f1cbd683.tar.xz | |
text-motion: add functions to iterate over lines of a range
| -rw-r--r-- | text-motions.c | 33 | ||||
| -rw-r--r-- | text-motions.h | 5 |
2 files changed, 38 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); diff --git a/text-motions.h b/text-motions.h index 438a42f..72878cd 100644 --- a/text-motions.h +++ b/text-motions.h @@ -42,6 +42,11 @@ size_t text_line_char_prev(Text*, size_t pos); /* move to same offset in previous/next line */ size_t text_line_up(Text*, size_t pos); size_t text_line_down(Text*, size_t pos); +/* functions to iterate over all line beginnings in a given range */ +size_t text_range_line_first(Text*, Filerange*); +size_t text_range_line_last(Text*, Filerange*); +size_t text_range_line_next(Text*, Filerange*, size_t pos); +size_t text_range_line_prev(Text*, Filerange*, 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. |
