From 771c54bc04c60501eb17b3f9edc9328b55b4d731 Mon Sep 17 00:00:00 2001 From: schwarze Date: Sun, 4 Jun 2017 18:48:09 +0000 Subject: [PATCH] Make term_flushln() simpler and more robust: Eliminate the "overstep" state variable. The information is already contained in "viscol". Minus 60 lines of code, no functional change intended. --- usr.bin/mandoc/man_term.c | 3 +- usr.bin/mandoc/mdoc_term.c | 53 ++++++----------------- usr.bin/mandoc/term.c | 87 +++++++++++--------------------------- usr.bin/mandoc/term.h | 8 ++-- 4 files changed, 42 insertions(+), 109 deletions(-) diff --git a/usr.bin/mandoc/man_term.c b/usr.bin/mandoc/man_term.c index fe21148ea0e..fa7d5159bd0 100644 --- a/usr.bin/mandoc/man_term.c +++ b/usr.bin/mandoc/man_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: man_term.c,v 1.153 2017/05/09 14:09:37 schwarze Exp $ */ +/* $OpenBSD: man_term.c,v 1.154 2017/06/04 18:48:09 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2015, 2017 Ingo Schwarze @@ -139,7 +139,6 @@ terminal_man(void *arg, const struct roff_man *man) size_t save_defindent; p = (struct termp *)arg; - p->overstep = 0; p->rmargin = p->maxrmargin = p->defrmargin; term_tab_set(p, NULL); term_tab_set(p, "T"); diff --git a/usr.bin/mandoc/mdoc_term.c b/usr.bin/mandoc/mdoc_term.c index 122179a2fa9..38b2a59f2e4 100644 --- a/usr.bin/mandoc/mdoc_term.c +++ b/usr.bin/mandoc/mdoc_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc_term.c,v 1.258 2017/06/01 19:05:15 schwarze Exp $ */ +/* $OpenBSD: mdoc_term.c,v 1.259 2017/06/04 18:48:09 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2017 Ingo Schwarze @@ -257,7 +257,6 @@ terminal_mdoc(void *arg, const struct roff_man *mdoc) size_t save_defindent; p = (struct termp *)arg; - p->overstep = 0; p->rmargin = p->maxrmargin = p->defrmargin; term_tab_set(p, NULL); term_tab_set(p, "T"); @@ -763,33 +762,15 @@ termp_it_pre(DECL_ARGS) case LIST_bullet: case LIST_dash: case LIST_hyphen: - /* - * Weird special case. - * Some very narrow lists actually hang. - */ - if (width <= (int)term_len(p, 2)) - p->flags |= TERMP_HANG; - if (n->type != ROFFT_HEAD) - break; - p->flags |= TERMP_NOBREAK; - p->trailspace = 1; + if (n->type == ROFFT_HEAD) { + p->flags |= TERMP_NOBREAK | TERMP_HANG; + p->trailspace = 1; + } else if (width <= (int)term_len(p, 2)) + p->flags |= TERMP_NOPAD; break; case LIST_hang: if (n->type != ROFFT_HEAD) break; - - /* - * This is ugly. If `-hang' is specified and the body - * is a `Bl' or `Bd', then we want basically to nullify - * the "overstep" effect in term_flushln() and treat - * this as a `-ohang' list instead. - */ - if (NULL != n->next && - NULL != n->next->child && - (MDOC_Bl == n->next->child->tok || - MDOC_Bd == n->next->child->tok)) - break; - p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG; p->trailspace = 1; break; @@ -801,7 +782,7 @@ termp_it_pre(DECL_ARGS) p->trailspace = 2; if (NULL == n->next || NULL == n->next->child) - p->flags |= TERMP_DANGLE; + p->flags |= TERMP_HANG; break; case LIST_column: if (n->type == ROFFT_HEAD) @@ -835,23 +816,11 @@ termp_it_pre(DECL_ARGS) p->offset += offset; switch (type) { - case LIST_hang: - /* - * Same stipulation as above, regarding `-hang'. We - * don't want to recalculate rmargin and offsets when - * using `Bd' or `Bl' within `-hang' overstep lists. - */ - if (n->type == ROFFT_HEAD && - NULL != n->next && - NULL != n->next->child && - (MDOC_Bl == n->next->child->tok || - MDOC_Bd == n->next->child->tok)) - break; - /* FALLTHROUGH */ case LIST_bullet: case LIST_dash: case LIST_enum: case LIST_hyphen: + case LIST_hang: case LIST_tag: if (n->type == ROFFT_HEAD) p->rmargin = p->offset + width; @@ -918,6 +887,7 @@ termp_it_pre(DECL_ARGS) case LIST_column: if (n->type == ROFFT_HEAD) return 0; + p->minbl = 0; break; default: break; @@ -958,8 +928,7 @@ termp_it_post(DECL_ARGS) * has munged them in the meanwhile. */ - p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP | TERMP_BRIND | - TERMP_DANGLE | TERMP_HANG); + p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP | TERMP_BRIND | TERMP_HANG); p->trailspace = 0; } @@ -1379,6 +1348,7 @@ termp_fn_pre(DECL_ARGS) if (pretty) { term_flushln(p); p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG); + p->flags |= TERMP_NOPAD; p->offset = p->rmargin; p->rmargin = rmargin; } @@ -1857,6 +1827,7 @@ termp_fo_pre(DECL_ARGS) term_flushln(p); p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG); + p->flags |= TERMP_NOPAD; p->offset = p->rmargin; p->rmargin = rmargin; } diff --git a/usr.bin/mandoc/term.c b/usr.bin/mandoc/term.c index 107243c6f78..0e17f6b1fa8 100644 --- a/usr.bin/mandoc/term.c +++ b/usr.bin/mandoc/term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: term.c,v 1.122 2017/06/02 19:21:03 schwarze Exp $ */ +/* $OpenBSD: term.c,v 1.123 2017/06/04 18:48:09 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze @@ -82,11 +82,11 @@ term_end(struct termp *p) * to be broken, start the next line at the right margin instead * of at the offset. Used together with TERMP_NOBREAK for the tags * in various kinds of tagged lists. - * - TERMP_DANGLE: Do not break the output line at the right margin, + * - TERMP_HANG: Do not break the output line at the right margin, * append the next chunk after it even if this one is too long. * To be used together with TERMP_NOBREAK. - * - TERMP_HANG: Like TERMP_DANGLE, and also suppress padding before - * the next chunk if this column is not full. + * - TERMP_NOPAD: Start writing at the current position, + * do not pad with blank characters up to the offset. */ void term_flushln(struct termp *p) @@ -102,34 +102,15 @@ term_flushln(struct termp *p) size_t jhy; /* last hyph before overflow w/r/t j */ size_t maxvis; /* output position of visible boundary */ - /* - * First, establish the maximum columns of "visible" content. - * This is usually the difference between the right-margin and - * an indentation, but can be, for tagged lists or columns, a - * small set of values. - * - * The following unsigned-signed subtractions look strange, - * but they are actually correct. If the int p->overstep - * is negative, it gets sign extended. Subtracting that - * very large size_t effectively adds a small number to dv. - */ - dv = p->rmargin > p->offset ? p->rmargin - p->offset : 0; - maxvis = (int)dv > p->overstep ? dv - (size_t)p->overstep : 0; - - if (p->flags & TERMP_NOBREAK) { - dv = p->maxrmargin > p->offset ? - p->maxrmargin - p->offset : 0; - bp = (int)dv > p->overstep ? - dv - (size_t)p->overstep : 0; - } else - bp = maxvis; - - /* - * Calculate the required amount of padding. - */ - vbl = p->offset + p->overstep > p->viscol ? - p->offset + p->overstep - p->viscol : 0; - + vbl = (p->flags & TERMP_NOPAD) || p->offset < p->viscol ? 0 : + p->offset - p->viscol; + if (p->minbl && vbl < p->minbl) + vbl = p->minbl; + maxvis = p->rmargin > p->viscol + vbl ? + p->rmargin - p->viscol - vbl : 0; + bp = !(p->flags & TERMP_NOBREAK) ? maxvis : + p->maxrmargin > p->viscol + vbl ? + p->maxrmargin - p->viscol - vbl : 0; vis = vend = 0; i = 0; @@ -199,20 +180,13 @@ term_flushln(struct termp *p) /* Re-establish indentation. */ - if (p->flags & TERMP_BRIND) { + if (p->flags & TERMP_BRIND) vbl += p->rmargin; - vend += p->rmargin - p->offset; - } else + else vbl += p->offset; - - /* - * Remove the p->overstep width. - * Again, if p->overstep is negative, - * sign extension does the right thing. - */ - - bp += (size_t)p->overstep; - p->overstep = 0; + maxvis = p->rmargin > vbl ? p->rmargin - vbl : 0; + bp = !(p->flags & TERMP_NOBREAK) ? maxvis : + p->maxrmargin > vbl ? p->maxrmargin - vbl : 0; } /* Write out the [remaining] word. */ @@ -267,32 +241,19 @@ term_flushln(struct termp *p) vis = 0; p->col = 0; - p->overstep = 0; - p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE); + p->flags &= ~(TERMP_BACKAFTER | TERMP_BACKBEFORE | TERMP_NOPAD); if ( ! (TERMP_NOBREAK & p->flags)) { p->viscol = 0; + p->minbl = 0; (*p->endline)(p); return; } if (TERMP_HANG & p->flags) { - p->overstep += (int)(p->offset + vis - p->rmargin + - p->trailspace * (*p->width)(p, ' ')); - - /* - * If we have overstepped the margin, temporarily move - * it to the right and flag the rest of the line to be - * shorter. - * If there is a request to keep the columns together, - * allow negative overstep when the column is not full. - */ - if (p->trailspace && p->overstep < 0) - p->overstep = 0; - return; - - } else if (TERMP_DANGLE & p->flags) + p->minbl = p->trailspace; return; + } /* Trailing whitespace is significant in some columns. */ if (vis && vbl && (TERMP_BRTRSP & p->flags)) @@ -302,7 +263,9 @@ term_flushln(struct termp *p) if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) { (*p->endline)(p); p->viscol = 0; - } + p->minbl = 0; + } else + p->minbl = p->trailspace; } /* diff --git a/usr.bin/mandoc/term.h b/usr.bin/mandoc/term.h index 6e14411e55c..23135b930f5 100644 --- a/usr.bin/mandoc/term.h +++ b/usr.bin/mandoc/term.h @@ -1,4 +1,4 @@ -/* $OpenBSD: term.h,v 1.65 2017/05/08 15:33:43 schwarze Exp $ */ +/* $OpenBSD: term.h,v 1.66 2017/06/04 18:48:09 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011-2015, 2017 Ingo Schwarze @@ -67,7 +67,7 @@ struct termp { size_t col; /* Bytes in buf. */ size_t viscol; /* Chars on current line. */ size_t trailspace; /* See termp_flushln(). */ - int overstep; /* See termp_flushln(). */ + size_t minbl; /* Minimum blanks before next field. */ int ti; /* Temporary indent for one line. */ int skipvsp; /* Vertical space to skip. */ int flags; @@ -82,8 +82,8 @@ struct termp { #define TERMP_NOBREAK (1 << 8) /* See term_flushln(). */ #define TERMP_BRTRSP (1 << 9) /* See term_flushln(). */ #define TERMP_BRIND (1 << 10) /* See term_flushln(). */ -#define TERMP_DANGLE (1 << 11) /* See term_flushln(). */ -#define TERMP_HANG (1 << 12) /* See term_flushln(). */ +#define TERMP_HANG (1 << 11) /* See term_flushln(). */ +#define TERMP_NOPAD (1 << 12) /* See term_flushln(). */ #define TERMP_NOSPLIT (1 << 13) /* Do not break line before .An. */ #define TERMP_SPLIT (1 << 14) /* Break line before .An. */ #define TERMP_NONEWLINE (1 << 15) /* No line break in nofill mode. */ -- 2.20.1