aboutsummaryrefslogtreecommitdiff
path: root/view.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-03-28 12:01:49 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-03-28 12:25:05 +0200
commit647dc4c030c0ee876d85de0447f4d706d600d3f6 (patch)
tree3de0921d374d75928e70d0aecb226db3b7dfdffd /view.c
parent99512776c0f35f3f518c995642d1707368f80598 (diff)
downloadvis-647dc4c030c0ee876d85de0447f4d706d600d3f6.tar.gz
vis-647dc4c030c0ee876d85de0447f4d706d600d3f6.tar.xz
view: add infrastructure to iterate through cursor columns
The number of columns i.e. maximal number of cursors located on the same line can be obtained by view_cursors_column_count. Column addressing is zero based, valid indexes are [0, max-1]. Assuming there is a cursor on every letter: a b c d e f g h i max column would be 3, and the following would iterate over the cursors forming the second column [c, e, h]: for (Cursor *c = view_cursors_column(view, 1); c; c = view_cursors_column_next(c, 1)) ...
Diffstat (limited to 'view.c')
-rw-r--r--view.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/view.c b/view.c
index 46e576d..ebf2732 100644
--- a/view.c
+++ b/view.c
@@ -4,6 +4,7 @@
#include <ctype.h>
#include <errno.h>
#include <regex.h>
+#include <limits.h>
#include "vis-lua.h"
#include "view.h"
#include "text.h"
@@ -1103,6 +1104,59 @@ int view_cursors_count(View *view) {
return i;
}
+int view_cursors_column_count(View *view) {
+ Text *txt = view->text;
+ int cpl_max = 0, cpl = 0; /* cursors per line */
+ size_t line_prev = 0;
+ for (Cursor *c = view->cursors; c; c = c->next) {
+ size_t pos = view_cursors_pos(c);
+ size_t line = text_lineno_by_pos(txt, pos);
+ if (line == line_prev)
+ cpl++;
+ else
+ cpl = 1;
+ line_prev = line;
+ if (cpl > cpl_max)
+ cpl_max = cpl;
+ }
+ return cpl_max;
+}
+
+static Cursor *cursors_column_next(View *view, Cursor *c, int column) {
+ size_t line_cur = 0;
+ int column_cur = 0;
+ Text *txt = view->text;
+ if (c) {
+ size_t pos = view_cursors_pos(c);
+ line_cur = text_lineno_by_pos(txt, pos);
+ column_cur = INT_MIN;
+ } else {
+ c = view->cursors;
+ }
+
+ for (; c; c = c->next) {
+ size_t pos = view_cursors_pos(c);
+ size_t line = text_lineno_by_pos(txt, pos);
+ if (line != line_cur) {
+ line_cur = line;
+ column_cur = 0;
+ } else {
+ column_cur++;
+ }
+ if (column == column_cur)
+ return c;
+ }
+ return NULL;
+}
+
+Cursor *view_cursors_column(View *view, int column) {
+ return cursors_column_next(view, NULL, column);
+}
+
+Cursor *view_cursors_column_next(Cursor *c, int column) {
+ return cursors_column_next(c->view, c, column);
+}
+
bool view_cursors_multiple(View *view) {
return view->cursors && view->cursors->next;
}