source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Make term_flushln() simpler and more robust: Eliminate the
@ 2017-06-04 18:51 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2017-06-04 18:51 UTC (permalink / raw)
  To: source

Log Message:
-----------
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.

Modified Files:
--------------
    mdocml:
        man_term.c
        mdoc_term.c
        term.c
        term.h

Revision Data
-------------
Index: man_term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/man_term.c,v
retrieving revision 1.200
retrieving revision 1.201
diff -Lman_term.c -Lman_term.c -u -p -r1.200 -r1.201
--- man_term.c
+++ man_term.c
@@ -141,7 +141,6 @@ terminal_man(void *arg, const struct rof
 	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");
Index: term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/term.c,v
retrieving revision 1.262
retrieving revision 1.263
diff -Lterm.c -Lterm.c -u -p -r1.262 -r1.263
--- term.c
+++ term.c
@@ -84,11 +84,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)
@@ -104,34 +104,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;
 
@@ -201,20 +182,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. */
@@ -269,32 +243,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))
@@ -304,7 +265,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;
 }
 
 /*
Index: term.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/term.h,v
retrieving revision 1.121
retrieving revision 1.122
diff -Lterm.h -Lterm.h -u -p -r1.121 -r1.122
--- term.h
+++ term.h
@@ -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. */
Index: mdoc_term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mdoc_term.c,v
retrieving revision 1.359
retrieving revision 1.360
diff -Lmdoc_term.c -Lmdoc_term.c -u -p -r1.359 -r1.360
--- mdoc_term.c
+++ mdoc_term.c
@@ -259,7 +259,6 @@ terminal_mdoc(void *arg, const struct ro
 	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");
@@ -765,33 +764,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;
@@ -803,7 +784,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)
@@ -837,23 +818,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;
@@ -920,6 +889,7 @@ termp_it_pre(DECL_ARGS)
 	case LIST_column:
 		if (n->type == ROFFT_HEAD)
 			return 0;
+		p->minbl = 0;
 		break;
 	default:
 		break;
@@ -960,8 +930,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;
 }
 
@@ -1381,6 +1350,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;
 	}
@@ -1859,6 +1829,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;
 		}
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-06-04 18:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-04 18:51 mdocml: Make term_flushln() simpler and more robust: Eliminate the schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).