From aa993027ac8694ad7646c5364d78942e275b2699 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 26 Sep 2018 16:15:05 -0400 Subject: [PATCH] handle reverse selections properly when trimming whitespace --- lib/buf.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/buf.c b/lib/buf.c index e6d95d5..667dc5e 100644 --- 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; } -- 2.52.0