From 64e204b205f09fdbac728c8910bf1867c286ebaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 28 Mar 2016 12:10:03 +0200 Subject: vis: support column based alignment That is when multiple cursors are on the same line, the first cursor on every line is aligned, then the second one and so on. --- main.c | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index bbb8f10..652ee11 100644 --- a/main.c +++ b/main.c @@ -1224,34 +1224,39 @@ static const char *cursors_align(Vis *vis, const char *keys, const Arg *arg) { static const char *cursors_align_indent(Vis *vis, const char *keys, const Arg *arg) { View *view = vis_view(vis); Text *txt = vis_text(vis); - int mincol = INT_MAX, maxcol = 0; - - for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) { - int col = text_line_width_get(txt, view_cursors_pos(c)); - if (col < mincol) - mincol = col; - if (col > maxcol) - maxcol = col; - } + bool left_align = arg->i < 0; + int columns = view_cursors_column_count(view); + + for (int i = 0; i < columns; i++) { + int mincol = INT_MAX, maxcol = 0; + for (Cursor *c = view_cursors_column(view, i); c; c = view_cursors_column_next(c, i)) { + int col = text_line_width_get(txt, view_cursors_pos(c)); + if (col < mincol) + mincol = col; + if (col > maxcol) + maxcol = col; + } - size_t len = maxcol - mincol; - char *buf = malloc(len+1); - if (!buf) - return keys; - memset(buf, ' ', len); + size_t len = maxcol - mincol; + char *buf = malloc(len+1); + if (!buf) + return keys; + memset(buf, ' ', len); - for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) { - size_t pos = view_cursors_pos(c); - int col = text_line_width_get(txt, pos); - if (col < maxcol) { - size_t off = maxcol - col; - if (off <= len) - text_insert(txt, pos, buf, off); + for (Cursor *c = view_cursors_column(view, i); c; c = view_cursors_column_next(c, i)) { + size_t pos = view_cursors_pos(c); + int col = text_line_width_get(txt, pos); + if (col < maxcol) { + size_t off = maxcol - col; + if (off <= len) + text_insert(txt, pos, buf, off); + } } + + free(buf); } view_draw(view); - free(buf); return keys; } -- cgit v1.2.3