summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2013-12-22 23:34:13 +0000
committerIngo Schwarze <schwarze@openbsd.org>2013-12-22 23:34:13 +0000
commit3b2ca955f8a2aa20d55aed7f0b8fb63533fddb64 (patch)
treee82445f6f67dcbe0c89395f44bf802693714be39
parentcac6ca6afd5184c9e640382fcad986fa5922cec9 (diff)
downloadmandoc-3b2ca955f8a2aa20d55aed7f0b8fb63533fddb64.tar.gz
Polishing the worms in my favourite can, term_flushln().
The TERMP_TWOSPACE flag i introduced in August 2009 was idiosyncratic and served only a very narrow purpose. Replace it by a more intuitive and more general termp attribute "trailspace", to be used together with TERMP_NOBREAK, to request a minimum amount of whitespace at the end of the current column. Adapt all code to the new interface. No functional change intended; code reviews to confirm that are welcome *eg*.
-rw-r--r--man_term.c16
-rw-r--r--mdoc_term.c47
-rw-r--r--term.c12
-rw-r--r--term.h3
4 files changed, 49 insertions, 29 deletions
diff --git a/man_term.c b/man_term.c
index 3c76f4cb..22668f02 100644
--- a/man_term.c
+++ b/man_term.c
@@ -265,7 +265,8 @@ pre_literal(DECL_ARGS)
if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
p->offset = p->rmargin;
p->rmargin = p->maxrmargin;
- p->flags &= ~(TERMP_NOBREAK | TERMP_TWOSPACE);
+ p->trailspace = 0;
+ p->flags &= ~TERMP_NOBREAK;
p->flags |= TERMP_NOSPACE;
}
@@ -535,7 +536,7 @@ pre_HP(DECL_ARGS)
if ( ! (MANT_LITERAL & mt->fl)) {
p->flags |= TERMP_NOBREAK;
- p->flags |= TERMP_TWOSPACE;
+ p->trailspace = 2;
}
len = mt->lmargin[mt->lmargincur];
@@ -570,7 +571,7 @@ post_HP(DECL_ARGS)
case (MAN_BODY):
term_newln(p);
p->flags &= ~TERMP_NOBREAK;
- p->flags &= ~TERMP_TWOSPACE;
+ p->trailspace = 0;
p->offset = mt->offset;
p->rmargin = p->maxrmargin;
break;
@@ -613,6 +614,7 @@ pre_IP(DECL_ARGS)
break;
case (MAN_HEAD):
p->flags |= TERMP_NOBREAK;
+ p->trailspace = 1;
break;
case (MAN_BLOCK):
print_bvspace(p, n, mt->pardist);
@@ -675,6 +677,7 @@ post_IP(DECL_ARGS)
case (MAN_HEAD):
term_flushln(p);
p->flags &= ~TERMP_NOBREAK;
+ p->trailspace = 0;
p->rmargin = p->maxrmargin;
break;
case (MAN_BODY):
@@ -698,6 +701,7 @@ pre_TP(DECL_ARGS)
switch (n->type) {
case (MAN_HEAD):
p->flags |= TERMP_NOBREAK;
+ p->trailspace = 1;
break;
case (MAN_BODY):
p->flags |= TERMP_NOSPACE;
@@ -745,8 +749,8 @@ pre_TP(DECL_ARGS)
case (MAN_BODY):
p->offset = mt->offset + len;
p->rmargin = p->maxrmargin;
+ p->trailspace = 0;
p->flags &= ~TERMP_NOBREAK;
- p->flags &= ~TERMP_TWOSPACE;
break;
default:
break;
@@ -1101,6 +1105,7 @@ print_man_foot(struct termp *p, const void *arg)
/* Bottom left corner: manual source. */
p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
+ p->trailspace = 1;
p->offset = 0;
p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
@@ -1123,6 +1128,7 @@ print_man_foot(struct termp *p, const void *arg)
p->flags &= ~TERMP_NOBREAK;
p->flags |= TERMP_NOSPACE;
+ p->trailspace = 0;
p->offset = p->rmargin;
p->rmargin = p->maxrmargin;
@@ -1154,6 +1160,7 @@ print_man_head(struct termp *p, const void *arg)
titlen = term_strlen(p, title);
p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
+ p->trailspace = 1;
p->offset = 0;
p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
(p->maxrmargin -
@@ -1176,6 +1183,7 @@ print_man_head(struct termp *p, const void *arg)
/* Top right corner: title and section, again. */
p->flags &= ~TERMP_NOBREAK;
+ p->trailspace = 0;
if (p->rmargin + titlen <= p->maxrmargin) {
p->flags |= TERMP_NOSPACE;
p->offset = p->rmargin;
diff --git a/mdoc_term.c b/mdoc_term.c
index a9b41f15..e10ded9b 100644
--- a/mdoc_term.c
+++ b/mdoc_term.c
@@ -1,7 +1,7 @@
/* $Id$ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2010, 2012 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2010, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -424,6 +424,7 @@ print_mdoc_foot(struct termp *p, const void *arg)
p->offset = 0;
p->rmargin = (p->maxrmargin -
term_strlen(p, meta->date) + term_len(p, 1)) / 2;
+ p->trailspace = 1;
p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
term_word(p, meta->os);
@@ -438,6 +439,7 @@ print_mdoc_foot(struct termp *p, const void *arg)
p->offset = p->rmargin;
p->rmargin = p->maxrmargin;
+ p->trailspace = 0;
p->flags &= ~TERMP_NOBREAK;
p->flags |= TERMP_NOSPACE;
@@ -489,6 +491,7 @@ print_mdoc_head(struct termp *p, const void *arg)
titlen = term_strlen(p, title);
p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
+ p->trailspace = 1;
p->offset = 0;
p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
(p->maxrmargin -
@@ -507,6 +510,7 @@ print_mdoc_head(struct termp *p, const void *arg)
term_flushln(p);
p->flags &= ~TERMP_NOBREAK;
+ p->trailspace = 0;
if (p->rmargin + titlen <= p->maxrmargin) {
p->flags |= TERMP_NOSPACE;
p->offset = p->rmargin;
@@ -794,13 +798,13 @@ termp_it_pre(DECL_ARGS)
case (LIST_dash):
/* FALLTHROUGH */
case (LIST_hyphen):
- if (MDOC_HEAD == n->type)
- p->flags |= TERMP_NOBREAK;
+ if (MDOC_HEAD != n->type)
+ break;
+ p->flags |= TERMP_NOBREAK;
+ p->trailspace = 1;
break;
case (LIST_hang):
- if (MDOC_HEAD == n->type)
- p->flags |= TERMP_NOBREAK;
- else
+ if (MDOC_HEAD != n->type)
break;
/*
@@ -812,16 +816,18 @@ termp_it_pre(DECL_ARGS)
if (n->next->child &&
(MDOC_Bl == n->next->child->tok ||
MDOC_Bd == n->next->child->tok))
- p->flags &= ~TERMP_NOBREAK;
- else
- p->flags |= TERMP_HANG;
+ break;
+
+ p->flags |= TERMP_NOBREAK | TERMP_HANG;
+ p->trailspace = 1;
break;
case (LIST_tag):
- if (MDOC_HEAD == n->type)
- p->flags |= TERMP_NOBREAK | TERMP_TWOSPACE;
-
if (MDOC_HEAD != n->type)
break;
+
+ p->flags |= TERMP_NOBREAK;
+ p->trailspace = 2;
+
if (NULL == n->next || NULL == n->next->child)
p->flags |= TERMP_DANGLE;
break;
@@ -829,15 +835,20 @@ termp_it_pre(DECL_ARGS)
if (MDOC_HEAD == n->type)
break;
- if (NULL == n->next)
+ if (NULL == n->next) {
p->flags &= ~TERMP_NOBREAK;
- else
+ p->trailspace = 0;
+ } else {
p->flags |= TERMP_NOBREAK;
+ p->trailspace = 1;
+ }
break;
case (LIST_diag):
- if (MDOC_HEAD == n->type)
- p->flags |= TERMP_NOBREAK;
+ if (MDOC_HEAD != n->type)
+ break;
+ p->flags |= TERMP_NOBREAK;
+ p->trailspace = 1;
break;
default:
break;
@@ -989,8 +1000,8 @@ termp_it_post(DECL_ARGS)
p->flags &= ~TERMP_DANGLE;
p->flags &= ~TERMP_NOBREAK;
- p->flags &= ~TERMP_TWOSPACE;
p->flags &= ~TERMP_HANG;
+ p->trailspace = 0;
}
@@ -1023,6 +1034,7 @@ termp_nm_pre(DECL_ARGS)
if (MDOC_HEAD == n->type && n->next->child) {
p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
+ p->trailspace = 1;
p->rmargin = p->offset + term_len(p, 1);
if (NULL == n->child) {
p->rmargin += term_strlen(p, meta->name);
@@ -1051,6 +1063,7 @@ termp_nm_post(DECL_ARGS)
if (MDOC_HEAD == n->type && n->next->child) {
term_flushln(p);
p->flags &= ~(TERMP_NOBREAK | TERMP_HANG);
+ p->trailspace = 0;
} else if (MDOC_BODY == n->type && n->child)
term_flushln(p);
}
diff --git a/term.c b/term.c
index c19b097d..f5f1882d 100644
--- a/term.c
+++ b/term.c
@@ -83,9 +83,8 @@ term_end(struct termp *p)
* - TERMP_NOBREAK: this is the most important and is used when making
* columns. In short: don't print a newline and instead expect the
* next call to do the padding up to the start of the next column.
- *
- * - TERMP_TWOSPACE: make sure there is room for at least two space
- * characters of padding. Otherwise, rather break the line.
+ * p->trailspace may be set to 0, 1, or 2, depending on how many
+ * space characters are required at the end of the column.
*
* - TERMP_DANGLE: don't newline when TERMP_NOBREAK is specified and
* the line is overrun, and don't pad-right if it's underrun.
@@ -269,8 +268,8 @@ term_flushln(struct termp *p)
}
if (TERMP_HANG & p->flags) {
- /* We need one blank after the tag. */
- p->overstep = (int)(vis - maxvis + (*p->width)(p, ' '));
+ p->overstep = (int)(vis - maxvis +
+ p->trailspace * (*p->width)(p, ' '));
/*
* If we have overstepped the margin, temporarily move
@@ -285,8 +284,7 @@ term_flushln(struct termp *p)
return;
/* If the column was overrun, break the line. */
- if (maxvis <= vis +
- ((TERMP_TWOSPACE & p->flags) ? (*p->width)(p, ' ') : 0)) {
+ if (maxvis < vis + p->trailspace * (*p->width)(p, ' ')) {
(*p->endline)(p);
p->viscol = 0;
}
diff --git a/term.h b/term.h
index 2c6ece97..880fefb1 100644
--- a/term.h
+++ b/term.h
@@ -1,6 +1,7 @@
/* $Id$ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -63,6 +64,7 @@ struct termp {
size_t tabwidth; /* Distance of tab positions. */
size_t col; /* Bytes in buf. */
size_t viscol; /* Chars on current line. */
+ size_t trailspace; /* See termp_flushln(). */
int overstep; /* See termp_flushln(). */
int skipvsp; /* Vertical space to skip. */
int flags;
@@ -73,7 +75,6 @@ struct termp {
#define TERMP_NONOSPACE (1 << 7) /* No space (no autounset). */
#define TERMP_DANGLE (1 << 8) /* See term_flushln(). */
#define TERMP_HANG (1 << 9) /* See term_flushln(). */
-#define TERMP_TWOSPACE (1 << 10) /* See term_flushln(). */
#define TERMP_NOSPLIT (1 << 11) /* See termp_an_pre/post(). */
#define TERMP_SPLIT (1 << 12) /* See termp_an_pre/post(). */
#define TERMP_ANPREC (1 << 13) /* See termp_an_pre(). */