=================================================================== RCS file: /cvs/mandoc/term.c,v retrieving revision 1.20 retrieving revision 1.27 diff -u -p -r1.20 -r1.27 --- mandoc/term.c 2009/02/25 13:30:53 1.20 +++ mandoc/term.c 2009/02/27 09:39:40 1.27 @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.20 2009/02/25 13:30:53 kristaps Exp $ */ +/* $Id: term.c,v 1.27 2009/02/27 09:39:40 kristaps Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons * @@ -17,12 +17,15 @@ * PERFORMANCE OF THIS SOFTWARE. */ #include +#include +#include +#include #include #include #include "term.h" -#define INDENT 4 +#define INDENT 6 /* * Performs actions on nodes of the abstract syntax tree. Both pre- and @@ -30,7 +33,7 @@ */ /* FIXME: indent/tab. */ -/* FIXME: handle nested lists. */ +/* FIXME: macro arguments can be escaped. */ #define TTYPE_PROG 0 #define TTYPE_CMD_FLAG 1 @@ -148,6 +151,7 @@ DECL_PRE(termp_ox); DECL_PRE(termp_pa); DECL_PRE(termp_pp); DECL_PRE(termp_rv); +DECL_PRE(termp_sm); DECL_PRE(termp_st); DECL_PRE(termp_sx); DECL_PRE(termp_sy); @@ -248,7 +252,7 @@ const struct termact __termacts[MDOC_MAX] = { { NULL, NULL }, /* Sc */ { termp_sq_pre, termp_sq_post }, /* So */ { termp_sq_pre, termp_sq_post }, /* Sq */ - { NULL, NULL }, /* Sm */ + { termp_sm_pre, NULL }, /* Sm */ { termp_sx_pre, NULL }, /* Sx */ { termp_sy_pre, NULL }, /* Sy */ { NULL, NULL }, /* Tn */ @@ -273,9 +277,29 @@ const struct termact *termacts = __termacts; static size_t arg_width(const struct mdoc_arg *arg) { + size_t v; + int i, len; - /* TODO */ assert(*arg->value); + if (0 == strcmp(*arg->value, "indent")) + return(INDENT); + if (0 == strcmp(*arg->value, "indent-two")) + return(INDENT * 2); + + len = (int)strlen(*arg->value); + assert(len > 0); + + for (i = 0; i < len - 1; i++) + if ( ! isdigit((int)(*arg->value)[i])) + break; + + if (i == len - 1) { + if ('n' == (*arg->value)[len - 1]) { + v = (size_t)atoi(*arg->value); + return(v); + } + + } return(strlen(*arg->value)); } @@ -290,7 +314,6 @@ arg_offset(const struct mdoc_arg *arg) return(INDENT); if (0 == strcmp(*arg->value, "indent-two")) return(INDENT * 2); - return(strlen(*arg->value)); } @@ -348,7 +371,8 @@ termp_it_pre(DECL_ARGS) { const struct mdoc_node *n, *it; const struct mdoc_block *bl; - int i; + char buf[7], *tp; + int i, type; size_t width, offset; switch (node->type) { @@ -364,17 +388,9 @@ termp_it_pre(DECL_ARGS) return(1); } - assert(MDOC_BLOCK == it->type); - assert(MDOC_It == it->tok); - - n = it->parent; - assert(MDOC_BODY == n->type); - assert(MDOC_Bl == n->tok); - n = n->parent; + n = it->parent->parent; bl = &n->data.block; - /* If `-compact', don't assert vertical space. */ - if (MDOC_BLOCK == node->type) { if (arg_hasattr(MDOC_Compact, bl->argc, bl->argv)) newln(p); @@ -383,37 +399,138 @@ termp_it_pre(DECL_ARGS) return(1); } + /* Get our list type. */ + + for (type = -1, 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_Tag): + /* FALLTHROUGH */ + case (MDOC_Ohang): + type = bl->argv[i].arg; + i = (int)bl->argc; + break; + default: + errx(1, "list type not supported"); + /* NOTREACHED */ + } + + assert(-1 != type); + + /* Save our existing (inherited) margin and offset. */ + pair->offset = p->offset; pair->rmargin = p->rmargin; - /* FIXME: auto-size. */ + /* Get list width and offset. */ + 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; + 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): + /* FIXME: auto-size. */ + if (0 == width) + errx(1, "need non-zero -width"); + break; + default: + break; + } - if (MDOC_HEAD == node->type) { + /* Word-wrap control. */ + + p->flags |= TERMP_NOSPACE; + + 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->offset += offset; - p->rmargin = p->offset + width; - } else { + else if (MDOC_BODY == node->type) p->flags |= TERMP_NOLPAD; - p->offset += width; + break; + default: + 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); + tp = buf; } + if (arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) + tp = "\\-"; + } - } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; - p->offset += offset; + /* Margin control. */ + + p->offset += offset; + + 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->rmargin = p->offset + width; + else if (MDOC_BODY == node->type) + p->offset += width; + break; + default: + break; } - return(1); + if (NULL == tp) + return(1); + + word(p, tp); + return(0); } @@ -421,46 +538,20 @@ termp_it_pre(DECL_ARGS) static void termp_it_post(DECL_ARGS) { - const struct mdoc_node *n, *it; - const struct mdoc_block *bl; - switch (node->type) { - case (MDOC_BODY): - /* FALLTHROUGH */ - case (MDOC_HEAD): - break; - default: + if (MDOC_BODY != node->type && MDOC_HEAD != node->type) return; - } - it = node->parent; - assert(MDOC_BLOCK == it->type); - assert(MDOC_It == it->tok); + flushln(p); - n = it->parent; - assert(MDOC_BODY == n->type); - assert(MDOC_Bl == n->tok); - n = n->parent; - bl = &n->data.block; + p->offset = pair->offset; + p->rmargin = pair->rmargin; - /* If `-tag', adjust our margins accordingly. */ - - if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv)) { - flushln(p); - - if (MDOC_HEAD == node->type) { - p->rmargin = pair->rmargin; - p->offset = pair->offset; - p->flags &= ~TERMP_NOBREAK; - } else { - p->offset = pair->offset; - p->flags &= ~TERMP_NOLPAD; - } - - } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) { - flushln(p); - p->offset = pair->offset; - } + if (MDOC_HEAD == node->type) { + p->flags &= ~TERMP_NOBREAK; + p->flags &= ~TERMP_NORPAD; + } else if (MDOC_BODY == node->type) + p->flags &= ~TERMP_NOLPAD; } @@ -1239,7 +1330,7 @@ termp_bq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "["); + word(p, "\\["); p->flags |= TERMP_NOSPACE; return(1); } @@ -1263,7 +1354,7 @@ termp_pq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "("); + word(p, "\\&("); p->flags |= TERMP_NOSPACE; return(1); } @@ -1374,3 +1465,23 @@ termp_ms_pre(DECL_ARGS) return(1); } + + +/* ARGSUSED */ +static int +termp_sm_pre(DECL_ARGS) +{ + +#if notyet + assert(node->child); + if (0 == strcmp("off", node->child->data.text.string)) { + p->flags &= ~TERMP_NONOSPACE; + p->flags &= ~TERMP_NOSPACE; + } else { + p->flags |= TERMP_NONOSPACE; + p->flags |= TERMP_NOSPACE; + } +#endif + + return(0); +}