]> git.mdlowis.com Git - projs/tide.git/commitdiff
handle reverse selections properly when trimming whitespace
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 26 Sep 2018 20:15:05 +0000 (16:15 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 26 Sep 2018 20:15:05 +0000 (16:15 -0400)
lib/buf.c

index e6d95d5055488feccfa30f8e53fe14f7c502a741..667dc5e9e43ecdc396e9572f3e62d263a3c05fec 100644 (file)
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -143,9 +143,17 @@ void buf_reload(Buf* buf) {
     buf_load(buf, path);
 }
 
+static Sel selswap(Sel sel) {
+    size_t off = sel.beg;
+    sel.beg = sel.end, sel.end = off;
+    return sel;
+}
+
 static void trim_whitespace(Buf* buf) {
     if (!TrimOnSave || !buf_end(buf)) return;
     Sel sel = buf->selection;
+    bool swapped = (sel.beg > sel.end);
+    if (swapped) sel = selswap(sel);    
     unsigned prev = 1;
     buf->selection.beg = buf->selection.end = 0;
     while (buf->selection.end < buf_end(buf) && prev != buf->selection.end) {
@@ -166,7 +174,8 @@ static void trim_whitespace(Buf* buf) {
         /* move to the next character */
         prev = buf->selection.end;
         buf->selection.end = buf_byrune(buf, buf->selection.end, +1);
-    }    
+    }
+    if (swapped) sel = selswap(sel);
     buf->selection = sel;
 }