From 8ab73e6d89429ef733ec388b5ab0aa6d37fb8bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 13 Jan 2016 11:59:05 +0100 Subject: vis: fix tab expansion if enabled Closes #144 --- vis.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'vis.c') diff --git a/vis.c b/vis.c index f23c42e..6ccd584 100644 --- a/vis.c +++ b/vis.c @@ -1218,8 +1218,23 @@ const char *vis_mode_status(Vis *vis) { } void vis_insert_tab(Vis *vis) { - const char *tab = expandtab(vis); - vis_insert_key(vis, tab, strlen(tab)); + if (!vis->expandtab) { + vis_insert_key(vis, "\t", 1); + return; + } + char spaces[9]; + int tabwidth = MIN(vis->tabwidth, LENGTH(spaces) - 1); + for (Cursor *c = view_cursors(vis->win->view); c; c = view_cursors_next(c)) { + size_t pos = view_cursors_pos(c); + /* FIXME: this does not take double width characters into account */ + int chars = text_line_char_get(vis->win->file->text, pos); + int count = tabwidth - (chars % tabwidth); + for (int i = 0; i < count; i++) + spaces[i] = ' '; + spaces[count] = '\0'; + vis_insert(vis, pos, spaces, count); + view_cursors_scroll_to(c, pos + count); + } } static void copy_indent_from_previous_line(Win *win) { -- cgit v1.2.3