From 8d960c6dc536efcbe5dc2fae330a95015e0aae86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 10 Sep 2014 21:00:47 +0200 Subject: text-motions: add text_line_prev function --- text-motions.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'text-motions.c') diff --git a/text-motions.c b/text-motions.c index 25ff87c..15fdd2d 100644 --- a/text-motions.c +++ b/text-motions.c @@ -73,6 +73,20 @@ size_t text_find_char_prev(Text *txt, size_t pos, const char *s, size_t len) { return matched == 0 ? it.pos : pos; } +size_t text_line_prev(Text *txt, size_t pos) { + char c; + Iterator it = text_iterator_get(txt, pos); + if (!text_iterator_byte_get(&it, &c)) + return pos; + if (c == '\r') + text_iterator_byte_prev(&it, &c); + if (c == '\n') + text_iterator_byte_prev(&it, &c); + while (text_iterator_byte_get(&it, &c) && c != '\n') + text_iterator_byte_prev(&it, NULL); + return it.pos; +} + size_t text_line_begin(Text *txt, size_t pos) { char c; Iterator it = text_iterator_get(txt, pos); -- cgit v1.2.3