summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-04 14:25:41 -0500
committerJune McEnroe <june@causal.agency>2021-01-04 14:28:37 -0500
commit3b54425ec178a1ece9214cf2d862c105ad01247c (patch)
treefab2b9bf46e076a536c60f84fc6ef86bfe22fbfe
parentSplit ignore fields to avoid over-eager * matching (diff)
downloadcatgirl-3b54425ec178a1ece9214cf2d862c105ad01247c.tar.gz
catgirl-3b54425ec178a1ece9214cf2d862c105ad01247c.zip
Collapse whitespace while wrapping and discard trailing space
The latter avoids an extra blank line when a message ends with
whitespace that hits the edge of the window.
-rw-r--r--buffer.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/buffer.c b/buffer.c
index 740e598..15d89f8 100644
--- a/buffer.c
+++ b/buffer.c
@@ -160,11 +160,18 @@ static int flow(struct Lines *hard, int cols, const struct Line *soft) {
 			wrapStyle = style;
 		}
 
-		n = mbtowc(&wc, wrap, strlen(wrap));
-		if (n < 0) {
-			n = 1;
-		} else if (!iswspace(wc)) {
-			n = 0;
+		n = 0;
+		len = strlen(wrap);
+		for (int m; wrap[n] && (m = mbtowc(&wc, &wrap[n], len - n)); n += m) {
+			if (m < 0) {
+				m = 1;
+			} else if (!iswspace(wc)) {
+				break;
+			}
+		}
+		if (!wrap[n]) {
+			*wrap = '\0';
+			break;
 		}
 
 		flowed++;