From 6aae2a61d59cf63aa66c1f5745222d05011508a5 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Tue, 15 Mar 2016 10:49:48 -0400 Subject: [PATCH] Better tabs (still some issues) --- src/util.d | 10 ++++++++++ src/view.d | 11 +++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/util.d b/src/util.d index 0f82fbbc..d1754d6d 100644 --- a/src/util.d +++ b/src/util.d @@ -5,3 +5,13 @@ string emptyString(int size) { } return str; } + +int numOccurences(string str, char c) { + int n; + foreach (letter; str) { + if (letter == c) { + n++; + } + } + return n; +} diff --git a/src/view.d b/src/view.d index f4493717..25fb3c5e 100644 --- a/src/view.d +++ b/src/view.d @@ -90,7 +90,6 @@ class View { if (cursor.x < buf.lines[cursor.y].length) { if (buf.lines[cursor.y][cursor.x] == '\t') { cursor.x++; - cursor.offset += tabSize-1; } else { cursor.x++; } @@ -102,7 +101,6 @@ class View { if (cursor.x > 0) { if (buf.lines[cursor.y][cursor.x-1] == '\t') { cursor.x--; - cursor.offset -= tabSize-1; } else { cursor.x--; } @@ -128,8 +126,10 @@ class View { } else if (e.key == Key.arrowLeft) { cursorLeft(); } else if (e.key == Key.mouseLeft) { - cursor.x = e.x - xOffset; + auto eventX = e.x; cursor.y = e.y + topline; + eventX -= buf.lines[cursor.y][0 .. cursor.x].numOccurences('\t') * (tabSize-1); + cursor.x = eventX - xOffset; if (cursor.y - topline > height-1) { cursor.y = height + topline-1; } @@ -165,9 +165,6 @@ class View { cursorRight(); } else if (e.key == Key.backspace2) { if (cloc > 0) { - if (buf.lines[cursor.y][cursor.x-1] == '\t') { - cursor.offset -= tabSize-1; - } buf.remove(cloc-1, cloc); setCursorLoc(cloc - 1); cursor.lastX = cursor.x; @@ -182,6 +179,8 @@ class View { if (cursor.y > topline + height-1) { topline = cursor.y - height+1; } + + cursor.offset = buf.lines[cursor.y][0 .. cursor.x].numOccurences('\t') * (tabSize-1); } }