From 1954d5ef9219b5c3c8ba757b0936118fe265d13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 27 Nov 2015 13:57:01 +0100 Subject: vis: improve cursor alignment command --- main.c | 13 +++++++------ view.c | 11 +++++++++++ view.h | 4 ++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 185aa94..35d2f41 100644 --- a/main.c +++ b/main.c @@ -1127,15 +1127,16 @@ static const char *cursors_align(Vis *vis, const char *keys, const Arg *arg) { Text *txt = vis_text(vis); int mincol = INT_MAX; for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) { - size_t pos = view_cursors_pos(c); - int col = text_line_char_get(txt, pos); - if (col < mincol) + int col = view_cursors_cell_get(c); + if (col >= 0 && col < mincol) mincol = col; } for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) { - size_t pos = view_cursors_pos(c); - size_t col = text_line_char_set(txt, pos, mincol); - view_cursors_to(c, col); + if (view_cursors_cell_set(c, mincol) == -1) { + size_t pos = view_cursors_pos(c); + size_t col = text_line_char_set(txt, pos, mincol); + view_cursors_to(c, col); + } } return keys; } diff --git a/view.c b/view.c index a340293..2d82e2d 100644 --- a/view.c +++ b/view.c @@ -1091,6 +1091,17 @@ size_t view_cursors_pos(Cursor *c) { return text_mark_get(c->view->text, c->mark); } +int view_cursors_cell_get(Cursor *c) { + return c->line ? c->col : -1; +} + +int view_cursors_cell_set(Cursor *c, int cell) { + if (!c->line || cell < 0) + return -1; + cursor_set(c, c->line, cell); + return c->col; +} + Register *view_cursors_register(Cursor *c) { return &c->reg; } diff --git a/view.h b/view.h index d650ec6..cbed355 100644 --- a/view.h +++ b/view.h @@ -136,6 +136,10 @@ Cursor *view_cursors_prev(Cursor*); Cursor *view_cursors_next(Cursor*); /* get current position of cursor in bytes from the start of the file */ size_t view_cursors_pos(Cursor*); +/* get/set zero based index of cell on which cursor currently resides, + * -1 if cursor is currently not visible */ +int view_cursors_cell_get(Cursor*); +int view_cursors_cell_set(Cursor*, int cell); /* place cursor at `pos' which should be in the interval [0, text-size] */ void view_cursors_to(Cursor*, size_t pos); void view_cursors_scroll_to(Cursor*, size_t pos); -- cgit v1.2.3