aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-07-28 11:04:39 +0200
committerMarc André Tanner <mat@brain-dump.org>2015-07-28 13:21:50 +0200
commitab5973aaca82c064dd54c30d2fd3db58e07947cc (patch)
treec619b21ddae8aa0a8ef85ac4df43c9b2d8d2851c
parent1dfef906f43eb1d950d71cb6e44d6b83e2d158f3 (diff)
downloadvis-ab5973aaca82c064dd54c30d2fd3db58e07947cc.tar.gz
vis-ab5973aaca82c064dd54c30d2fd3db58e07947cc.tar.xz
text-motion: introduce text_line_char_{get,set}
-rw-r--r--text-motions.c33
-rw-r--r--text-motions.h4
2 files changed, 31 insertions, 6 deletions
diff --git a/text-motions.c b/text-motions.c
index f62ba22..9d40c33 100644
--- a/text-motions.c
+++ b/text-motions.c
@@ -166,6 +166,27 @@ size_t text_line_offset(Text *txt, size_t pos, size_t off) {
return it.pos;
}
+size_t text_line_char_set(Text *txt, size_t pos, int count) {
+ char c;
+ size_t bol = text_line_begin(txt, pos);
+ Iterator it = text_iterator_get(txt, bol);
+ while (count-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n')
+ text_iterator_char_next(&it, NULL);
+ return it.pos;
+}
+
+int text_line_char_get(Text *txt, size_t pos) {
+ char c;
+ int count = 0;
+ size_t bol = text_line_begin(txt, pos);
+ Iterator it = text_iterator_get(txt, bol);
+ while (text_iterator_byte_get(&it, &c) && it.pos < pos && c != '\r' && c != '\n') {
+ text_iterator_char_next(&it, NULL);
+ count++;
+ }
+ return count;
+}
+
size_t text_line_char_next(Text *txt, size_t pos) {
char c;
Iterator it = text_iterator_get(txt, pos);
@@ -185,15 +206,15 @@ size_t text_line_char_prev(Text *txt, size_t pos) {
}
size_t text_line_up(Text *txt, size_t pos) {
- size_t bol = text_line_begin(txt, pos);
- size_t prev = text_line_prev(txt, bol);
- return text_line_offset(txt, prev, pos - bol);
+ int count = text_line_char_get(txt, pos);
+ size_t prev = text_line_prev(txt, pos);
+ return text_line_char_set(txt, prev, count);
}
size_t text_line_down(Text *txt, size_t pos) {
- size_t bol = text_line_begin(txt, pos);
- size_t next = text_line_next(txt, bol);
- return text_line_offset(txt, next, pos - bol);
+ int count = text_line_char_get(txt, pos);
+ size_t next = text_line_next(txt, pos);
+ return text_line_char_set(txt, next, count);
}
size_t text_range_line_first(Text *txt, Filerange *r) {
diff --git a/text-motions.h b/text-motions.h
index 72878cd..c89c256 100644
--- a/text-motions.h
+++ b/text-motions.h
@@ -36,6 +36,10 @@ size_t text_line_lastchar(Text*, size_t pos);
size_t text_line_end(Text*, size_t pos);
size_t text_line_next(Text*, size_t pos);
size_t text_line_offset(Text*, size_t pos, size_t off);
+/* get character count of the line upto `pos' */
+int text_line_char_get(Text*, size_t pos);
+/* get position of the `count' character in the line containing `pos' */
+size_t text_line_char_set(Text*, size_t pos, int count);
/* move to the next/previous character on the same line */
size_t text_line_char_next(Text*, size_t pos);
size_t text_line_char_prev(Text*, size_t pos);