=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mandoc/term.c,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- src/usr.bin/mandoc/term.c 2010/05/15 21:09:53 1.32 +++ src/usr.bin/mandoc/term.c 2010/05/17 02:25:42 1.33 @@ -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 * @@ -130,7 +130,7 @@ 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 @@ 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 @@ 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 @@ } /* - * 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 @@ 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 @@ 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; }