From c6031d7f1f3278979c9f5c9f4802b4e1f1cbd683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 27 Jul 2015 15:09:28 +0200 Subject: text-motion: add functions to iterate over lines of a range --- text-motions.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'text-motions.c') 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 #include #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); -- cgit v1.2.3