print_wrapped_text(): allow hard newlines
print_wrapped_text() will insert its own newlines. Up until now, if the text passed to it contained newlines, they would not be handled properly (the wrapping got confused after that). The strategy is to replace a single new-line with a space, but keep double new-lines so that already-wrapped text with empty lines between paragraphs will be handled properly. However, single new-line characters are only handled this way if the character after it is an alphanumeric character, as per Linus' suggestion. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Johannes Schindelin committed
Nov 11, 2007 at 14:14 UTC
ae0b27023018416c0bfe54823466dee67c20705a
1 file changed
+16
-2
utf8.c
+16
-2
@@ -310,6 +310,8 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
310
if (!c || isspace(c)) {
311
if (w < width || !space) {
312
const char *start = bol;
313
+ if (!c && text == start)
314
+ return w;
315
if (space)
316
start = space;
317
else
@@ -317,13 +319,25 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
319
fwrite(start, text - start, 1, stdout);
320
if (!c)
321
return w;
320
- else if (c == '\t')
321
- w |= 0x07;
322
space = text;
323
+ if (c == '\t')
324
+ w |= 0x07;
325
+ else if (c == '\n') {
326
+ space++;
327
+ if (*space == '\n') {
328
+ putchar('\n');
329
+ goto new_line;
330
+ }
331
+ else if (!isalnum(*space))
332
+ goto new_line;
333
+ else
334
+ putchar(' ');
335
+ }
336
w++;
337
text++;
338
}
339
else {
340
+new_line:
341
putchar('\n');
342
text = bol = space + isspace(*space);
343
space = NULL;