diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2010-05-17 02:25:43 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2010-05-17 02:25:43 +0000 |
commit | 002b2f7dd6205aa46827d3448a56fd697e269411 (patch) | |
tree | f9491e30c9627b10b505ece65096e41b80d478c4 /usr.bin | |
parent | fb57ef1fc5c2056cbfeafdf0e941e6b5687d7e9e (diff) |
The function term_flushln() had effectively forked wrt to bsd.lv.
This is the OpenBSD part of the main step to bring it back in sync.
At the same time, this prevents trailing whitespace in the output:
We delay writing blanks until we are sure printable characters follow.
This is achieved by
* not using vbl any longer for the control of line breaking
* such that vbl can sum up all kinds of white space
* before writing a word, printing all the blanks collected in vbl
* within the word, adding NBSP chars to vbl, then continuing with the word
* after the word, adding blanks to vbl, then starting the next word
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/mandoc/term.c | 86 |
1 files changed, 40 insertions, 46 deletions
diff --git a/usr.bin/mandoc/term.c b/usr.bin/mandoc/term.c index 84fd27e0ddf..874e519a65b 100644 --- a/usr.bin/mandoc/term.c +++ b/usr.bin/mandoc/term.c @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.32 2010/05/15 21:09:53 schwarze Exp $ */ +/* $Id: term.c,v 1.33 2010/05/17 02:25:42 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -130,7 +130,7 @@ term_flushln(struct termp *p) int i; /* current input position in p->buf */ size_t vis; /* current visual position on output */ size_t vbl; /* number of blanks to prepend to output */ - size_t vend; /* end of word visual position on output */ + size_t vend; /* end of word visual position on output */ size_t bp; /* visual right border position */ int j; /* temporary loop index */ int jhy; /* last hyphen before line overflow */ @@ -154,33 +154,26 @@ term_flushln(struct termp *p) bp = TERMP_NOBREAK & p->flags ? mmax : maxvis; + /* + * Indent the first line of a paragraph. + */ + vbl = p->flags & TERMP_NOLPAD ? 0 : p->offset; + /* * FIXME: if bp is zero, we still output the first word before * breaking the line. */ - vis = i = 0; + vis = vend = i = 0; while (i < (int)p->col) { /* - * Choose the number of blanks to prepend: no blank at the - * beginning of a line, one between words -- but do not - * actually write them yet. - */ - vbl = (size_t)(0 == vis ? 0 : 1); - vis += vbl; - vend = vis; - - /* * Handle literal tab characters. */ for (j = i; j < (int)p->col; j++) { if ('\t' != p->buf[j]) break; - /* Collapse tab with inter-word spacing. */ - if (vis > 0 && j == i) - vend = vis - 1; - vend = (vend/p->tabwidth+1)*p->tabwidth; + vend = (vis/p->tabwidth+1)*p->tabwidth; vbl += vend - vis; vis = vend; } @@ -196,7 +189,7 @@ term_flushln(struct termp *p) for (jhy = 0; j < (int)p->col; j++) { if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j]) break; - else if (8 == p->buf[j]) + if (8 == p->buf[j]) vend--; else { if (vend > vis && vend < bp && @@ -207,21 +200,10 @@ term_flushln(struct termp *p) } /* - * Usually, indent the first line of each paragraph. - */ - if (0 == i && ! (p->flags & TERMP_NOLPAD)) { - p->viscol += p->offset; - /* LINTED */ - for (j = 0; j < (int)p->offset; j++) - putchar(' '); - } - - /* * Find out whether we would exceed the right margin. - * If so, break to the next line. (TODO: hyphenate) - * Otherwise, write the chosen number of blanks now. + * If so, break to the next line. */ - if (vend > bp && 0 == jhy && vis > vbl) { + if (vend > bp && 0 == jhy && vis > 0) { vend -= vis; putchar('\n'); if (TERMP_NOBREAK & p->flags) { @@ -230,18 +212,15 @@ term_flushln(struct termp *p) putchar(' '); vend += p->rmargin - p->offset; } else { - p->viscol = p->offset; - for (j = 0; j < (int)p->offset; j++) - putchar(' '); + p->viscol = 0; + vbl = p->offset; } + /* Remove the p->overstep width. */ + bp += (int)/* LINTED */ p->overstep; p->overstep = 0; - } else { - p->viscol += vbl; - for (j = 0; j < (int)vbl; j++) - putchar(' '); } /* @@ -250,24 +229,39 @@ term_flushln(struct termp *p) while (i < (int)p->col && '\t' == p->buf[i]) i++; - /* - * Finally, write out the word. - */ + /* Write out the [remaining] word. */ for ( ; i < (int)p->col; i++) { if (vend > bp && jhy > 0 && i > jhy) break; if ('\t' == p->buf[i]) break; if (' ' == p->buf[i]) { - i++; + while (' ' == p->buf[i]) { + vbl++; + i++; + } break; } - if (ASCII_NBRSP == p->buf[i]) - putchar(' '); - else - putchar(p->buf[i]); + if (ASCII_NBRSP == p->buf[i]) { + vbl++; + continue; + } + + /* + * Now we definitely know there will be + * printable characters to output, + * so write preceding white space now. + */ + if (vbl) { + for (j = 0; j < (int)vbl; j++) + putchar(' '); + p->viscol += vbl; + vbl = 0; + } + putchar(p->buf[i]); + p->viscol += 1; } - p->viscol += vend - vis; + vend += vbl; vis = vend; } |