diff options
-rw-r--r-- | mdocterm.c | 11 | ||||
-rw-r--r-- | term.c | 216 | ||||
-rw-r--r-- | term.h | 1 |
3 files changed, 141 insertions, 87 deletions
@@ -91,7 +91,7 @@ main(int argc, char *argv[]) if (NULL == (termp.buf = malloc(termp.maxcols))) err(1, "malloc"); - header(&termp, mdoc_meta(mdoc)); + /*header(&termp, mdoc_meta(mdoc));*/ body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc)); footer(&termp, mdoc_meta(mdoc)); @@ -168,7 +168,7 @@ flushln(struct termp *p) * the line with TERMP_NOBREAK). */ - if (vis && vis + vsz >= maxvis) { + if (vis && vis + vsz > maxvis) { /* FIXME */ if (p->flags & TERMP_NOBREAK) errx(1, "word breaks right margin"); @@ -176,7 +176,7 @@ flushln(struct termp *p) for (j = 0; j < p->offset; j++) putchar(' '); vis = 0; - } else if (vis + vsz >= maxvis) + } else if (vis + vsz > maxvis) /* FIXME */ errx(1, "word breaks right margin"); @@ -203,8 +203,9 @@ flushln(struct termp *p) */ if (p->flags & TERMP_NOBREAK) { - for ( ; vis < maxvis; vis++) - putchar(' '); + if ( ! (p->flags & TERMP_NORPAD)) + for ( ; vis < maxvis; vis++) + putchar(' '); } else putchar('\n'); @@ -17,6 +17,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> +#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -350,8 +351,8 @@ termp_it_pre(DECL_ARGS) { const struct mdoc_node *n, *it; const struct mdoc_block *bl; - char buf[7]; - int i; + char buf[7], *tp; + int i, type; size_t width, offset; switch (node->type) { @@ -378,79 +379,151 @@ termp_it_pre(DECL_ARGS) return(1); } + /* Get our list type. */ + + for (i = 0; i < (int)bl->argc; i++) + switch (bl->argv[i].arg) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + /* FALLTHROUGH */ + case (MDOC_Item): + /* FALLTHROUGH */ + case (MDOC_Tag): + /* FALLTHROUGH */ + case (MDOC_Ohang): + type = bl->argv[i].arg; + i = (int)bl->argc; + break; + case (MDOC_Inset): + /* FALLTHROUGH */ + case (MDOC_Hang): + /* FALLTHROUGH */ + case (MDOC_Diag): + /* FALLTHROUGH */ + case (MDOC_Column): + errx(1, "list type not supported"); + /* NOTREACHED */ + default: + break; + } + + /* Save our existing (inherited) margin and offset. */ + pair->offset = p->offset; pair->rmargin = p->rmargin; + /* Get list width and offset. */ + /* FIXME: auto-size. */ i = arg_getattr(MDOC_Width, bl->argc, bl->argv); - width = i >= 0 ? arg_width(&bl->argv[i]) : 10; + width = i >= 0 ? arg_width(&bl->argv[i]) : 0; i = arg_getattr(MDOC_Offset, bl->argc, bl->argv); offset = i >= 0 ? arg_offset(&bl->argv[i]) : 0; - assert(MDOC_HEAD == node->type || - MDOC_BODY == node->type); + /* Override the width. */ - if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; - if (MDOC_BODY == node->type) { - p->flags |= TERMP_NOLPAD; - p->offset += width; - } else { - p->flags |= TERMP_NOBREAK; - p->rmargin = p->offset + offset + width; - } - - } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; + switch (type) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + width = width > 6 ? width : 6; + break; + case (MDOC_Tag): + if (0 == width) + errx(1, "need non-zero -width"); + break; + default: + break; + } - } else if (arg_hasattr(MDOC_Diag, bl->argc, bl->argv)) { - /* TODO. */ + /* Word-wrap control. */ - } else if (arg_hasattr(MDOC_Hang, bl->argc, bl->argv)) { - /* TODO. */ + p->flags |= TERMP_NOSPACE; - } else if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; - if (MDOC_BODY == node->type) { - p->flags |= TERMP_NOLPAD; - p->offset += 6; - } else { - word(p, "\\[bu]"); + switch (type) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + /* FALLTHROUGH */ + case (MDOC_Tag): + if (MDOC_HEAD == node->type) p->flags |= TERMP_NOBREAK; - p->rmargin = p->offset + offset + 6; - } - - } else if (arg_hasattr(MDOC_Enum, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; - if (MDOC_BODY == node->type) { + else if (MDOC_BODY == node->type) p->flags |= TERMP_NOLPAD; - p->offset += 6; - } else { + break; + case (MDOC_Ohang): + break; + } + + /* + * Get a token to use as the HEAD lead-in. If NULL, we use the + * HEAD child. + */ + + tp = NULL; + if (MDOC_HEAD == node->type) { + if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv)) + tp = "\\[bu]"; + if (arg_hasattr(MDOC_Dash, bl->argc, bl->argv)) + tp = "\\-"; + if (arg_hasattr(MDOC_Enum, bl->argc, bl->argv)) { (pair->ppair->ppair->count)++; (void)snprintf(buf, sizeof(buf), "%d.", pair->ppair->ppair->count); - word(p, buf); - p->flags |= TERMP_NOBREAK; - p->rmargin = p->offset + offset + 6; + tp = buf; } + if (arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) + tp = "\\-"; + if (arg_hasattr(MDOC_Item, bl->argc, bl->argv)) + tp = ""; + } - } else if (arg_hasattr(MDOC_Dash, bl->argc, bl->argv) || - arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; - if (MDOC_BODY == node->type) { - p->flags |= TERMP_NOLPAD; - p->offset += 6; - return(1); - } else { - word(p, "\\-"); - p->flags |= TERMP_NOBREAK; - p->rmargin = p->offset + offset + 6; - } - } + /* Margin control. */ p->offset += offset; - return(1); + + switch (type) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + /* FALLTHROUGH */ + case (MDOC_Item): + /* FALLTHROUGH */ + case (MDOC_Tag): + if (MDOC_HEAD == node->type) + p->rmargin = p->offset + width; + else if (MDOC_BODY == node->type) + p->offset += width; + break; + case (MDOC_Ohang): + break; + } + + if (NULL == tp) + return(1); + + /* XXX - ignoring children. */ + + word(p, tp); + return(0); } @@ -458,41 +531,20 @@ termp_it_pre(DECL_ARGS) static void termp_it_post(DECL_ARGS) { - const struct mdoc_node *n, *it; - const struct mdoc_block *bl; if (MDOC_BODY != node->type && MDOC_HEAD != node->type) return; - it = node->parent; - n = it->parent->parent; - bl = &n->data.block; - - if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv) || - arg_hasattr(MDOC_Bullet, bl->argc, bl->argv) || - arg_hasattr(MDOC_Dash, bl->argc, bl->argv) || - arg_hasattr(MDOC_Enum, bl->argc, bl->argv) || - arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) { - flushln(p); - if (MDOC_HEAD == node->type) { - p->rmargin = pair->rmargin; - p->flags &= ~TERMP_NOBREAK; - } else - p->flags &= ~TERMP_NOLPAD; - - } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) { - flushln(p); - - } else if (arg_hasattr(MDOC_Inset, bl->argc, bl->argv)) { - if (MDOC_BODY == node->type) - flushln(p); - - } else if (arg_hasattr(MDOC_Item, bl->argc, bl->argv)) { - if (MDOC_BODY == node->type) - flushln(p); - } + flushln(p); p->offset = pair->offset; + p->rmargin = pair->rmargin; + + if (MDOC_HEAD == node->type) { + p->flags &= ~TERMP_NOBREAK; + p->flags &= ~TERMP_NORPAD; + } else if (MDOC_BODY == node->type) + p->flags &= ~TERMP_NOLPAD; } @@ -38,6 +38,7 @@ struct termp { #define TERMP_LITERAL (1 << 5) /* Literal words. */ #define TERMP_IGNDELIM (1 << 6) /* Delims like regulars. */ #define TERMP_NONOSPACE (1 << 7) /* No space (no autounset). */ +#define TERMP_NORPAD (1 << 8) /* No rightpad. */ char *buf; }; |