vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 3188409c7bb8171f9cf2186a49ec055b50b298f7 parent 75d88a4aba08d2d1cb8df97512b319050376e628 Author: Marc André Tanner <mat@brain-dump.org> Date: Mon, 27 Jul 2015 15:09:28 +0200 text-motion: add functions to iterate over lines of a range Diffstat:
| M | text-motions.c | | | 33 | +++++++++++++++++++++++++++++++++ |
| M | text-motions.h | | | 5 | +++++ |
2 files changed, 38 insertions(+), 0 deletions(-)
diff --git 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 @@ -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.