[BACK]Return to mdoc_term.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mdoc_term.c, Revision 1.322

1.322   ! schwarze    1: /*     $Id: mdoc_term.c,v 1.321 2015/07/25 14:02:06 schwarze Exp $ */
1.1       kristaps    2: /*
1.217     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.303     schwarze    4:  * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
1.256     schwarze    5:  * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de>
1.1       kristaps    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
1.6       kristaps    8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps   10:  *
1.314     schwarze   11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.6       kristaps   12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.314     schwarze   13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.6       kristaps   14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   18:  */
1.107     kristaps   19: #include "config.h"
                     20:
1.1       kristaps   21: #include <sys/types.h>
                     22:
                     23: #include <assert.h>
                     24: #include <ctype.h>
1.302     schwarze   25: #include <limits.h>
1.118     kristaps   26: #include <stdint.h>
1.1       kristaps   27: #include <stdio.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30:
1.314     schwarze   31: #include "mandoc_aux.h"
1.125     kristaps   32: #include "mandoc.h"
1.314     schwarze   33: #include "roff.h"
                     34: #include "mdoc.h"
1.92      kristaps   35: #include "out.h"
1.1       kristaps   36: #include "term.h"
1.320     schwarze   37: #include "tag.h"
1.89      kristaps   38: #include "main.h"
1.1       kristaps   39:
                     40: struct termpair {
                     41:        struct termpair  *ppair;
1.31      kristaps   42:        int               count;
1.1       kristaps   43: };
                     44:
1.31      kristaps   45: #define        DECL_ARGS struct termp *p, \
                     46:                  struct termpair *pair, \
1.316     schwarze   47:                  const struct roff_meta *meta, \
1.315     schwarze   48:                  struct roff_node *n
1.1       kristaps   49:
                     50: struct termact {
                     51:        int     (*pre)(DECL_ARGS);
                     52:        void    (*post)(DECL_ARGS);
                     53: };
                     54:
1.302     schwarze   55: static int       a2width(const struct termp *, const char *);
1.96      kristaps   56:
                     57: static void      print_bvspace(struct termp *,
1.315     schwarze   58:                        const struct roff_node *,
                     59:                        const struct roff_node *);
1.264     schwarze   60: static void      print_mdoc_node(DECL_ARGS);
1.145     kristaps   61: static void      print_mdoc_nodelist(DECL_ARGS);
1.316     schwarze   62: static void      print_mdoc_head(struct termp *, const struct roff_meta *);
                     63: static void      print_mdoc_foot(struct termp *, const struct roff_meta *);
1.264     schwarze   64: static void      synopsis_pre(struct termp *,
1.315     schwarze   65:                        const struct roff_node *);
1.96      kristaps   66:
1.31      kristaps   67: static void      termp____post(DECL_ARGS);
1.203     kristaps   68: static void      termp__t_post(DECL_ARGS);
1.31      kristaps   69: static void      termp_bd_post(DECL_ARGS);
1.159     schwarze   70: static void      termp_bk_post(DECL_ARGS);
1.31      kristaps   71: static void      termp_bl_post(DECL_ARGS);
1.306     schwarze   72: static void      termp_eo_post(DECL_ARGS);
1.241     schwarze   73: static void      termp_fd_post(DECL_ARGS);
1.31      kristaps   74: static void      termp_fo_post(DECL_ARGS);
                     75: static void      termp_in_post(DECL_ARGS);
                     76: static void      termp_it_post(DECL_ARGS);
                     77: static void      termp_lb_post(DECL_ARGS);
1.164     schwarze   78: static void      termp_nm_post(DECL_ARGS);
1.31      kristaps   79: static void      termp_pf_post(DECL_ARGS);
1.188     kristaps   80: static void      termp_quote_post(DECL_ARGS);
1.31      kristaps   81: static void      termp_sh_post(DECL_ARGS);
                     82: static void      termp_ss_post(DECL_ARGS);
                     83:
1.184     kristaps   84: static int       termp__a_pre(DECL_ARGS);
1.203     kristaps   85: static int       termp__t_pre(DECL_ARGS);
1.61      kristaps   86: static int       termp_an_pre(DECL_ARGS);
1.31      kristaps   87: static int       termp_ap_pre(DECL_ARGS);
                     88: static int       termp_bd_pre(DECL_ARGS);
                     89: static int       termp_bf_pre(DECL_ARGS);
1.159     schwarze   90: static int       termp_bk_pre(DECL_ARGS);
1.115     kristaps   91: static int       termp_bl_pre(DECL_ARGS);
1.69      kristaps   92: static int       termp_bold_pre(DECL_ARGS);
1.31      kristaps   93: static int       termp_bt_pre(DECL_ARGS);
1.211     kristaps   94: static int       termp_bx_pre(DECL_ARGS);
1.31      kristaps   95: static int       termp_cd_pre(DECL_ARGS);
                     96: static int       termp_d1_pre(DECL_ARGS);
1.306     schwarze   97: static int       termp_eo_pre(DECL_ARGS);
1.31      kristaps   98: static int       termp_ex_pre(DECL_ARGS);
                     99: static int       termp_fa_pre(DECL_ARGS);
1.188     kristaps  100: static int       termp_fd_pre(DECL_ARGS);
1.31      kristaps  101: static int       termp_fl_pre(DECL_ARGS);
                    102: static int       termp_fn_pre(DECL_ARGS);
                    103: static int       termp_fo_pre(DECL_ARGS);
                    104: static int       termp_ft_pre(DECL_ARGS);
                    105: static int       termp_in_pre(DECL_ARGS);
                    106: static int       termp_it_pre(DECL_ARGS);
1.102     kristaps  107: static int       termp_li_pre(DECL_ARGS);
1.260     schwarze  108: static int       termp_ll_pre(DECL_ARGS);
1.31      kristaps  109: static int       termp_lk_pre(DECL_ARGS);
                    110: static int       termp_nd_pre(DECL_ARGS);
                    111: static int       termp_nm_pre(DECL_ARGS);
                    112: static int       termp_ns_pre(DECL_ARGS);
1.188     kristaps  113: static int       termp_quote_pre(DECL_ARGS);
1.31      kristaps  114: static int       termp_rs_pre(DECL_ARGS);
                    115: static int       termp_rv_pre(DECL_ARGS);
                    116: static int       termp_sh_pre(DECL_ARGS);
1.294     schwarze  117: static int       termp_skip_pre(DECL_ARGS);
1.31      kristaps  118: static int       termp_sm_pre(DECL_ARGS);
1.45      kristaps  119: static int       termp_sp_pre(DECL_ARGS);
1.31      kristaps  120: static int       termp_ss_pre(DECL_ARGS);
1.320     schwarze  121: static int       termp_tag_pre(DECL_ARGS);
1.69      kristaps  122: static int       termp_under_pre(DECL_ARGS);
1.31      kristaps  123: static int       termp_ud_pre(DECL_ARGS);
1.110     kristaps  124: static int       termp_vt_pre(DECL_ARGS);
1.31      kristaps  125: static int       termp_xr_pre(DECL_ARGS);
                    126: static int       termp_xx_pre(DECL_ARGS);
                    127:
1.70      kristaps  128: static const struct termact termacts[MDOC_MAX] = {
1.14      kristaps  129:        { termp_ap_pre, NULL }, /* Ap */
1.1       kristaps  130:        { NULL, NULL }, /* Dd */
                    131:        { NULL, NULL }, /* Dt */
                    132:        { NULL, NULL }, /* Os */
                    133:        { termp_sh_pre, termp_sh_post }, /* Sh */
1.264     schwarze  134:        { termp_ss_pre, termp_ss_post }, /* Ss */
                    135:        { termp_sp_pre, NULL }, /* Pp */
1.241     schwarze  136:        { termp_d1_pre, termp_bl_post }, /* D1 */
                    137:        { termp_d1_pre, termp_bl_post }, /* Dl */
1.1       kristaps  138:        { termp_bd_pre, termp_bd_post }, /* Bd */
                    139:        { NULL, NULL }, /* Ed */
1.115     kristaps  140:        { termp_bl_pre, termp_bl_post }, /* Bl */
1.1       kristaps  141:        { NULL, NULL }, /* El */
                    142:        { termp_it_pre, termp_it_post }, /* It */
1.264     schwarze  143:        { termp_under_pre, NULL }, /* Ad */
1.282     schwarze  144:        { termp_an_pre, NULL }, /* An */
1.69      kristaps  145:        { termp_under_pre, NULL }, /* Ar */
1.1       kristaps  146:        { termp_cd_pre, NULL }, /* Cd */
1.69      kristaps  147:        { termp_bold_pre, NULL }, /* Cm */
1.264     schwarze  148:        { NULL, NULL }, /* Dv */
                    149:        { NULL, NULL }, /* Er */
1.320     schwarze  150:        { termp_tag_pre, NULL }, /* Ev */
1.1       kristaps  151:        { termp_ex_pre, NULL }, /* Ex */
1.264     schwarze  152:        { termp_fa_pre, NULL }, /* Fa */
                    153:        { termp_fd_pre, termp_fd_post }, /* Fd */
1.1       kristaps  154:        { termp_fl_pre, NULL }, /* Fl */
1.264     schwarze  155:        { termp_fn_pre, NULL }, /* Fn */
                    156:        { termp_ft_pre, NULL }, /* Ft */
                    157:        { termp_bold_pre, NULL }, /* Ic */
                    158:        { termp_in_pre, termp_in_post }, /* In */
1.102     kristaps  159:        { termp_li_pre, NULL }, /* Li */
1.264     schwarze  160:        { termp_nd_pre, NULL }, /* Nd */
                    161:        { termp_nm_pre, termp_nm_post }, /* Nm */
1.188     kristaps  162:        { termp_quote_pre, termp_quote_post }, /* Op */
1.268     schwarze  163:        { termp_ft_pre, NULL }, /* Ot */
1.69      kristaps  164:        { termp_under_pre, NULL }, /* Pa */
1.1       kristaps  165:        { termp_rv_pre, NULL }, /* Rv */
1.264     schwarze  166:        { NULL, NULL }, /* St */
1.69      kristaps  167:        { termp_under_pre, NULL }, /* Va */
1.143     kristaps  168:        { termp_vt_pre, NULL }, /* Vt */
1.1       kristaps  169:        { termp_xr_pre, NULL }, /* Xr */
1.184     kristaps  170:        { termp__a_pre, termp____post }, /* %A */
1.84      kristaps  171:        { termp_under_pre, termp____post }, /* %B */
1.1       kristaps  172:        { NULL, termp____post }, /* %D */
1.84      kristaps  173:        { termp_under_pre, termp____post }, /* %I */
1.69      kristaps  174:        { termp_under_pre, termp____post }, /* %J */
1.1       kristaps  175:        { NULL, termp____post }, /* %N */
                    176:        { NULL, termp____post }, /* %O */
                    177:        { NULL, termp____post }, /* %P */
                    178:        { NULL, termp____post }, /* %R */
1.203     kristaps  179:        { termp__t_pre, termp__t_post }, /* %T */
1.1       kristaps  180:        { NULL, termp____post }, /* %V */
                    181:        { NULL, NULL }, /* Ac */
1.188     kristaps  182:        { termp_quote_pre, termp_quote_post }, /* Ao */
                    183:        { termp_quote_pre, termp_quote_post }, /* Aq */
1.35      kristaps  184:        { NULL, NULL }, /* At */
1.1       kristaps  185:        { NULL, NULL }, /* Bc */
1.264     schwarze  186:        { termp_bf_pre, NULL }, /* Bf */
1.188     kristaps  187:        { termp_quote_pre, termp_quote_post }, /* Bo */
                    188:        { termp_quote_pre, termp_quote_post }, /* Bq */
1.26      kristaps  189:        { termp_xx_pre, NULL }, /* Bsx */
1.211     kristaps  190:        { termp_bx_pre, NULL }, /* Bx */
1.294     schwarze  191:        { termp_skip_pre, NULL }, /* Db */
1.1       kristaps  192:        { NULL, NULL }, /* Dc */
1.188     kristaps  193:        { termp_quote_pre, termp_quote_post }, /* Do */
                    194:        { termp_quote_pre, termp_quote_post }, /* Dq */
1.112     kristaps  195:        { NULL, NULL }, /* Ec */ /* FIXME: no space */
1.1       kristaps  196:        { NULL, NULL }, /* Ef */
1.264     schwarze  197:        { termp_under_pre, NULL }, /* Em */
1.306     schwarze  198:        { termp_eo_pre, termp_eo_post }, /* Eo */
1.26      kristaps  199:        { termp_xx_pre, NULL }, /* Fx */
1.176     kristaps  200:        { termp_bold_pre, NULL }, /* Ms */
1.290     schwarze  201:        { termp_li_pre, NULL }, /* No */
1.1       kristaps  202:        { termp_ns_pre, NULL }, /* Ns */
1.26      kristaps  203:        { termp_xx_pre, NULL }, /* Nx */
                    204:        { termp_xx_pre, NULL }, /* Ox */
1.1       kristaps  205:        { NULL, NULL }, /* Pc */
1.254     schwarze  206:        { NULL, termp_pf_post }, /* Pf */
1.188     kristaps  207:        { termp_quote_pre, termp_quote_post }, /* Po */
                    208:        { termp_quote_pre, termp_quote_post }, /* Pq */
1.1       kristaps  209:        { NULL, NULL }, /* Qc */
1.188     kristaps  210:        { termp_quote_pre, termp_quote_post }, /* Ql */
                    211:        { termp_quote_pre, termp_quote_post }, /* Qo */
                    212:        { termp_quote_pre, termp_quote_post }, /* Qq */
1.1       kristaps  213:        { NULL, NULL }, /* Re */
                    214:        { termp_rs_pre, NULL }, /* Rs */
                    215:        { NULL, NULL }, /* Sc */
1.188     kristaps  216:        { termp_quote_pre, termp_quote_post }, /* So */
                    217:        { termp_quote_pre, termp_quote_post }, /* Sq */
1.1       kristaps  218:        { termp_sm_pre, NULL }, /* Sm */
1.69      kristaps  219:        { termp_under_pre, NULL }, /* Sx */
                    220:        { termp_bold_pre, NULL }, /* Sy */
1.1       kristaps  221:        { NULL, NULL }, /* Tn */
1.26      kristaps  222:        { termp_xx_pre, NULL }, /* Ux */
1.1       kristaps  223:        { NULL, NULL }, /* Xc */
                    224:        { NULL, NULL }, /* Xo */
1.264     schwarze  225:        { termp_fo_pre, termp_fo_post }, /* Fo */
                    226:        { NULL, NULL }, /* Fc */
1.188     kristaps  227:        { termp_quote_pre, termp_quote_post }, /* Oo */
1.1       kristaps  228:        { NULL, NULL }, /* Oc */
1.159     schwarze  229:        { termp_bk_pre, termp_bk_post }, /* Bk */
1.1       kristaps  230:        { NULL, NULL }, /* Ek */
                    231:        { termp_bt_pre, NULL }, /* Bt */
                    232:        { NULL, NULL }, /* Hf */
1.268     schwarze  233:        { termp_under_pre, NULL }, /* Fr */
1.1       kristaps  234:        { termp_ud_pre, NULL }, /* Ud */
1.37      kristaps  235:        { NULL, termp_lb_post }, /* Lb */
1.264     schwarze  236:        { termp_sp_pre, NULL }, /* Lp */
                    237:        { termp_lk_pre, NULL }, /* Lk */
                    238:        { termp_under_pre, NULL }, /* Mt */
                    239:        { termp_quote_pre, termp_quote_post }, /* Brq */
                    240:        { termp_quote_pre, termp_quote_post }, /* Bro */
                    241:        { NULL, NULL }, /* Brc */
                    242:        { NULL, termp____post }, /* %C */
1.294     schwarze  243:        { termp_skip_pre, NULL }, /* Es */
1.268     schwarze  244:        { termp_quote_pre, termp_quote_post }, /* En */
1.264     schwarze  245:        { termp_xx_pre, NULL }, /* Dx */
                    246:        { NULL, termp____post }, /* %Q */
1.76      kristaps  247:        { termp_sp_pre, NULL }, /* br */
1.264     schwarze  248:        { termp_sp_pre, NULL }, /* sp */
                    249:        { NULL, termp____post }, /* %U */
                    250:        { NULL, NULL }, /* Ta */
1.260     schwarze  251:        { termp_ll_pre, NULL }, /* ll */
1.1       kristaps  252: };
                    253:
1.322   ! schwarze  254: static int      fn_prio;
1.1       kristaps  255:
1.70      kristaps  256: void
1.318     schwarze  257: terminal_mdoc(void *arg, const struct roff_man *mdoc)
1.1       kristaps  258: {
1.315     schwarze  259:        struct roff_node        *n;
1.89      kristaps  260:        struct termp            *p;
                    261:
                    262:        p = (struct termp *)arg;
1.111     kristaps  263:        p->overstep = 0;
1.281     schwarze  264:        p->rmargin = p->maxrmargin = p->defrmargin;
1.157     kristaps  265:        p->tabwidth = term_len(p, 5);
1.70      kristaps  266:
1.319     schwarze  267:        n = mdoc->first->child;
1.281     schwarze  268:        if (p->synopsisonly) {
                    269:                while (n != NULL) {
                    270:                        if (n->tok == MDOC_Sh && n->sec == SEC_SYNOPSIS) {
                    271:                                if (n->child->next->child != NULL)
                    272:                                        print_mdoc_nodelist(p, NULL,
1.319     schwarze  273:                                            &mdoc->meta,
                    274:                                            n->child->next->child);
1.281     schwarze  275:                                term_newln(p);
                    276:                                break;
                    277:                        }
                    278:                        n = n->next;
                    279:                }
                    280:        } else {
                    281:                if (p->defindent == 0)
                    282:                        p->defindent = 5;
1.319     schwarze  283:                term_begin(p, print_mdoc_head, print_mdoc_foot,
                    284:                    &mdoc->meta);
1.281     schwarze  285:                if (n != NULL) {
                    286:                        if (n->tok != MDOC_Sh)
                    287:                                term_vspace(p);
1.319     schwarze  288:                        print_mdoc_nodelist(p, NULL, &mdoc->meta, n);
1.281     schwarze  289:                }
                    290:                term_end(p);
1.271     schwarze  291:        }
1.1       kristaps  292: }
                    293:
1.3       kristaps  294: static void
1.102     kristaps  295: print_mdoc_nodelist(DECL_ARGS)
1.1       kristaps  296: {
                    297:
1.304     schwarze  298:        while (n != NULL) {
                    299:                print_mdoc_node(p, pair, meta, n);
                    300:                n = n->next;
                    301:        }
1.1       kristaps  302: }
                    303:
1.3       kristaps  304: static void
1.102     kristaps  305: print_mdoc_node(DECL_ARGS)
1.1       kristaps  306: {
1.102     kristaps  307:        int              chld;
1.1       kristaps  308:        struct termpair  npair;
1.30      kristaps  309:        size_t           offset, rmargin;
1.1       kristaps  310:
1.70      kristaps  311:        chld = 1;
1.29      kristaps  312:        offset = p->offset;
1.30      kristaps  313:        rmargin = p->rmargin;
1.310     schwarze  314:        n->flags &= ~MDOC_ENDED;
1.305     schwarze  315:        n->prev_font = p->fonti;
1.29      kristaps  316:
1.97      kristaps  317:        memset(&npair, 0, sizeof(struct termpair));
1.1       kristaps  318:        npair.ppair = pair;
1.172     kristaps  319:
                    320:        /*
                    321:         * Keeps only work until the end of a line.  If a keep was
                    322:         * invoked in a prior line, revert it to PREKEEP.
                    323:         */
                    324:
1.307     schwarze  325:        if (p->flags & TERMP_KEEP && n->flags & MDOC_LINE) {
                    326:                p->flags &= ~TERMP_KEEP;
                    327:                p->flags |= TERMP_PREKEEP;
1.172     kristaps  328:        }
1.197     schwarze  329:
                    330:        /*
1.217     schwarze  331:         * After the keep flags have been set up, we may now
                    332:         * produce output.  Note that some pre-handlers do so.
                    333:         */
                    334:
                    335:        switch (n->type) {
1.314     schwarze  336:        case ROFFT_TEXT:
1.217     schwarze  337:                if (' ' == *n->string && MDOC_LINE & n->flags)
                    338:                        term_newln(p);
1.222     kristaps  339:                if (MDOC_DELIMC & n->flags)
                    340:                        p->flags |= TERMP_NOSPACE;
1.217     schwarze  341:                term_word(p, n->string);
1.222     kristaps  342:                if (MDOC_DELIMO & n->flags)
                    343:                        p->flags |= TERMP_NOSPACE;
1.217     schwarze  344:                break;
1.314     schwarze  345:        case ROFFT_EQN:
1.284     schwarze  346:                if ( ! (n->flags & MDOC_LINE))
                    347:                        p->flags |= TERMP_NOSPACE;
1.233     kristaps  348:                term_eqn(p, n->eqn);
1.286     schwarze  349:                if (n->next != NULL && ! (n->next->flags & MDOC_LINE))
1.285     schwarze  350:                        p->flags |= TERMP_NOSPACE;
1.217     schwarze  351:                break;
1.314     schwarze  352:        case ROFFT_TBL:
1.313     schwarze  353:                if (p->tbl.cols == NULL)
                    354:                        term_newln(p);
1.217     schwarze  355:                term_tbl(p, n->span);
                    356:                break;
                    357:        default:
1.295     schwarze  358:                if (termacts[n->tok].pre &&
                    359:                    (n->end == ENDBODY_NOT || n->nchild))
1.217     schwarze  360:                        chld = (*termacts[n->tok].pre)
1.245     schwarze  361:                                (p, &npair, meta, n);
1.217     schwarze  362:                break;
                    363:        }
                    364:
1.86      kristaps  365:        if (chld && n->child)
1.245     schwarze  366:                print_mdoc_nodelist(p, &npair, meta, n->child);
1.1       kristaps  367:
1.244     schwarze  368:        term_fontpopq(p,
1.310     schwarze  369:            (ENDBODY_NOT == n->end ? n : n->body)->prev_font);
1.46      kristaps  370:
1.206     kristaps  371:        switch (n->type) {
1.314     schwarze  372:        case ROFFT_TEXT:
1.206     kristaps  373:                break;
1.314     schwarze  374:        case ROFFT_TBL:
1.216     kristaps  375:                break;
1.314     schwarze  376:        case ROFFT_EQN:
1.206     kristaps  377:                break;
                    378:        default:
                    379:                if ( ! termacts[n->tok].post || MDOC_ENDED & n->flags)
                    380:                        break;
1.245     schwarze  381:                (void)(*termacts[n->tok].post)(p, &npair, meta, n);
1.162     schwarze  382:
                    383:                /*
                    384:                 * Explicit end tokens not only call the post
                    385:                 * handler, but also tell the respective block
                    386:                 * that it must not call the post handler again.
                    387:                 */
1.165     kristaps  388:                if (ENDBODY_NOT != n->end)
1.310     schwarze  389:                        n->body->flags |= MDOC_ENDED;
1.162     schwarze  390:
                    391:                /*
                    392:                 * End of line terminating an implicit block
                    393:                 * while an explicit block is still open.
                    394:                 * Continue the explicit block without spacing.
                    395:                 */
                    396:                if (ENDBODY_NOSPACE == n->end)
                    397:                        p->flags |= TERMP_NOSPACE;
1.206     kristaps  398:                break;
1.162     schwarze  399:        }
1.121     kristaps  400:
                    401:        if (MDOC_EOS & n->flags)
                    402:                p->flags |= TERMP_SENTENCE;
1.29      kristaps  403:
1.260     schwarze  404:        if (MDOC_ll != n->tok) {
                    405:                p->offset = offset;
                    406:                p->rmargin = rmargin;
                    407:        }
1.1       kristaps  408: }
                    409:
1.3       kristaps  410: static void
1.316     schwarze  411: print_mdoc_foot(struct termp *p, const struct roff_meta *meta)
1.1       kristaps  412: {
1.292     schwarze  413:        size_t sz;
1.144     kristaps  414:
1.102     kristaps  415:        term_fontrepl(p, TERMFONT_NONE);
1.101     kristaps  416:
1.264     schwarze  417:        /*
1.9       kristaps  418:         * Output the footer in new-groff style, that is, three columns
                    419:         * with the middle being the manual date and flanking columns
                    420:         * being the operating system:
                    421:         *
                    422:         * SYSTEM                  DATE                    SYSTEM
                    423:         */
                    424:
1.1       kristaps  425:        term_vspace(p);
                    426:
1.9       kristaps  427:        p->offset = 0;
1.292     schwarze  428:        sz = term_strlen(p, meta->date);
                    429:        p->rmargin = p->maxrmargin > sz ?
                    430:            (p->maxrmargin + term_len(p, 1) - sz) / 2 : 0;
1.250     schwarze  431:        p->trailspace = 1;
1.1       kristaps  432:        p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                    433:
1.245     schwarze  434:        term_word(p, meta->os);
1.1       kristaps  435:        term_flushln(p);
                    436:
1.9       kristaps  437:        p->offset = p->rmargin;
1.292     schwarze  438:        sz = term_strlen(p, meta->os);
                    439:        p->rmargin = p->maxrmargin > sz ? p->maxrmargin - sz : 0;
1.234     schwarze  440:        p->flags |= TERMP_NOSPACE;
1.9       kristaps  441:
1.245     schwarze  442:        term_word(p, meta->date);
1.9       kristaps  443:        term_flushln(p);
                    444:
1.1       kristaps  445:        p->offset = p->rmargin;
                    446:        p->rmargin = p->maxrmargin;
1.250     schwarze  447:        p->trailspace = 0;
1.1       kristaps  448:        p->flags &= ~TERMP_NOBREAK;
1.234     schwarze  449:        p->flags |= TERMP_NOSPACE;
1.1       kristaps  450:
1.245     schwarze  451:        term_word(p, meta->os);
1.1       kristaps  452:        term_flushln(p);
                    453:
1.9       kristaps  454:        p->offset = 0;
                    455:        p->rmargin = p->maxrmargin;
                    456:        p->flags = 0;
1.1       kristaps  457: }
                    458:
1.3       kristaps  459: static void
1.316     schwarze  460: print_mdoc_head(struct termp *p, const struct roff_meta *meta)
1.1       kristaps  461: {
1.267     schwarze  462:        char                    *volume, *title;
                    463:        size_t                   vollen, titlen;
1.1       kristaps  464:
                    465:        /*
                    466:         * The header is strange.  It has three components, which are
                    467:         * really two with the first duplicated.  It goes like this:
                    468:         *
                    469:         * IDENTIFIER              TITLE                   IDENTIFIER
                    470:         *
                    471:         * The IDENTIFIER is NAME(SECTION), which is the command-name
                    472:         * (if given, or "unknown" if not) followed by the manual page
                    473:         * section.  These are given in `Dt'.  The TITLE is a free-form
                    474:         * string depending on the manual volume.  If not specified, it
                    475:         * switches on the manual section.
                    476:         */
1.235     schwarze  477:
1.245     schwarze  478:        assert(meta->vol);
1.267     schwarze  479:        if (NULL == meta->arch)
                    480:                volume = mandoc_strdup(meta->vol);
                    481:        else
                    482:                mandoc_asprintf(&volume, "%s (%s)",
                    483:                    meta->vol, meta->arch);
                    484:        vollen = term_strlen(p, volume);
1.1       kristaps  485:
1.275     schwarze  486:        if (NULL == meta->msec)
                    487:                title = mandoc_strdup(meta->title);
                    488:        else
                    489:                mandoc_asprintf(&title, "%s(%s)",
                    490:                    meta->title, meta->msec);
1.235     schwarze  491:        titlen = term_strlen(p, title);
1.1       kristaps  492:
1.235     schwarze  493:        p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.250     schwarze  494:        p->trailspace = 1;
1.1       kristaps  495:        p->offset = 0;
1.267     schwarze  496:        p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
                    497:            (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.292     schwarze  498:            vollen < p->maxrmargin ?  p->maxrmargin - vollen : 0;
1.1       kristaps  499:
                    500:        term_word(p, title);
                    501:        term_flushln(p);
                    502:
1.235     schwarze  503:        p->flags |= TERMP_NOSPACE;
1.1       kristaps  504:        p->offset = p->rmargin;
1.267     schwarze  505:        p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1.235     schwarze  506:            p->maxrmargin - titlen : p->maxrmargin;
1.1       kristaps  507:
1.267     schwarze  508:        term_word(p, volume);
1.1       kristaps  509:        term_flushln(p);
                    510:
                    511:        p->flags &= ~TERMP_NOBREAK;
1.250     schwarze  512:        p->trailspace = 0;
1.235     schwarze  513:        if (p->rmargin + titlen <= p->maxrmargin) {
                    514:                p->flags |= TERMP_NOSPACE;
                    515:                p->offset = p->rmargin;
                    516:                p->rmargin = p->maxrmargin;
                    517:                term_word(p, title);
                    518:                term_flushln(p);
                    519:        }
1.1       kristaps  520:
1.235     schwarze  521:        p->flags &= ~TERMP_NOSPACE;
1.9       kristaps  522:        p->offset = 0;
1.1       kristaps  523:        p->rmargin = p->maxrmargin;
1.266     schwarze  524:        free(title);
1.267     schwarze  525:        free(volume);
1.1       kristaps  526: }
                    527:
1.302     schwarze  528: static int
1.157     kristaps  529: a2width(const struct termp *p, const char *v)
1.91      kristaps  530: {
1.92      kristaps  531:        struct roffsu    su;
1.1       kristaps  532:
1.300     schwarze  533:        if (a2roffsu(v, &su, SCALE_MAX) < 2) {
1.157     kristaps  534:                SCALE_HS_INIT(&su, term_strlen(p, v));
1.283     schwarze  535:                su.scale /= term_strlen(p, "0");
1.302     schwarze  536:        }
1.317     schwarze  537:        return(term_hspan(p, &su) / 24);
1.1       kristaps  538: }
                    539:
1.108     kristaps  540: /*
                    541:  * Determine how much space to print out before block elements of `It'
                    542:  * (and thus `Bl') and `Bd'.  And then go ahead and print that space,
                    543:  * too.
                    544:  */
1.54      kristaps  545: static void
1.264     schwarze  546: print_bvspace(struct termp *p,
1.315     schwarze  547:        const struct roff_node *bl,
                    548:        const struct roff_node *n)
1.1       kristaps  549: {
1.315     schwarze  550:        const struct roff_node  *nn;
1.236     schwarze  551:
                    552:        assert(n);
1.1       kristaps  553:
                    554:        term_newln(p);
1.150     kristaps  555:
1.202     kristaps  556:        if (MDOC_Bd == bl->tok && bl->norm->Bd.comp)
1.150     kristaps  557:                return;
1.202     kristaps  558:        if (MDOC_Bl == bl->tok && bl->norm->Bl.comp)
1.54      kristaps  559:                return;
                    560:
1.85      kristaps  561:        /* Do not vspace directly after Ss/Sh. */
1.1       kristaps  562:
1.289     schwarze  563:        nn = n;
                    564:        while (nn->prev == NULL) {
                    565:                do {
                    566:                        nn = nn->parent;
1.314     schwarze  567:                        if (nn->type == ROFFT_ROOT)
1.289     schwarze  568:                                return;
1.314     schwarze  569:                } while (nn->type != ROFFT_BLOCK);
1.289     schwarze  570:                if (nn->tok == MDOC_Sh || nn->tok == MDOC_Ss)
1.54      kristaps  571:                        return;
1.289     schwarze  572:                if (nn->tok == MDOC_It &&
                    573:                    nn->parent->parent->norm->Bl.type != LIST_item)
                    574:                        break;
1.1       kristaps  575:        }
                    576:
1.85      kristaps  577:        /* A `-column' does not assert vspace within the list. */
1.54      kristaps  578:
1.202     kristaps  579:        if (MDOC_Bl == bl->tok && LIST_column == bl->norm->Bl.type)
1.86      kristaps  580:                if (n->prev && MDOC_It == n->prev->tok)
1.54      kristaps  581:                        return;
                    582:
1.85      kristaps  583:        /* A `-diag' without body does not vspace. */
                    584:
1.202     kristaps  585:        if (MDOC_Bl == bl->tok && LIST_diag == bl->norm->Bl.type)
1.86      kristaps  586:                if (n->prev && MDOC_It == n->prev->tok) {
                    587:                        assert(n->prev->body);
                    588:                        if (NULL == n->prev->body->child)
1.55      kristaps  589:                                return;
                    590:                }
                    591:
1.54      kristaps  592:        term_vspace(p);
1.260     schwarze  593: }
                    594:
                    595:
                    596: static int
                    597: termp_ll_pre(DECL_ARGS)
                    598: {
                    599:
1.261     schwarze  600:        term_setwidth(p, n->nchild ? n->child->string : NULL);
1.260     schwarze  601:        return(0);
1.1       kristaps  602: }
                    603:
                    604: static int
                    605: termp_it_pre(DECL_ARGS)
                    606: {
1.302     schwarze  607:        char                    buf[24];
1.315     schwarze  608:        const struct roff_node *bl, *nn;
1.302     schwarze  609:        size_t                  ncols, dcol;
                    610:        int                     i, offset, width;
1.126     kristaps  611:        enum mdoc_list          type;
1.1       kristaps  612:
1.314     schwarze  613:        if (n->type == ROFFT_BLOCK) {
1.151     kristaps  614:                print_bvspace(p, n->parent->parent, n);
1.54      kristaps  615:                return(1);
                    616:        }
1.1       kristaps  617:
1.86      kristaps  618:        bl = n->parent->parent->parent;
1.202     kristaps  619:        type = bl->norm->Bl.type;
1.1       kristaps  620:
1.264     schwarze  621:        /*
1.302     schwarze  622:         * Defaults for specific list types.
                    623:         */
                    624:
                    625:        switch (type) {
                    626:        case LIST_bullet:
                    627:                /* FALLTHROUGH */
                    628:        case LIST_dash:
                    629:                /* FALLTHROUGH */
                    630:        case LIST_hyphen:
                    631:                /* FALLTHROUGH */
                    632:        case LIST_enum:
                    633:                width = term_len(p, 2);
                    634:                break;
                    635:        case LIST_hang:
                    636:                width = term_len(p, 8);
                    637:                break;
                    638:        case LIST_column:
                    639:                /* FALLTHROUGH */
                    640:        case LIST_tag:
                    641:                width = term_len(p, 10);
                    642:                break;
                    643:        default:
                    644:                width = 0;
                    645:                break;
                    646:        }
                    647:        offset = 0;
                    648:
                    649:        /*
1.108     kristaps  650:         * First calculate width and offset.  This is pretty easy unless
                    651:         * we're a -column list, in which case all prior columns must
                    652:         * be accounted for.
                    653:         */
                    654:
1.302     schwarze  655:        if (bl->norm->Bl.offs != NULL) {
1.288     schwarze  656:                offset = a2width(p, bl->norm->Bl.offs);
1.302     schwarze  657:                if (offset < 0 && (size_t)(-offset) > p->offset)
                    658:                        offset = -p->offset;
                    659:                else if (offset > SHRT_MAX)
                    660:                        offset = 0;
                    661:        }
1.152     kristaps  662:
1.1       kristaps  663:        switch (type) {
1.264     schwarze  664:        case LIST_column:
1.314     schwarze  665:                if (n->type == ROFFT_HEAD)
1.1       kristaps  666:                        break;
1.155     kristaps  667:
1.105     kristaps  668:                /*
1.108     kristaps  669:                 * Imitate groff's column handling:
                    670:                 * - For each earlier column, add its width.
                    671:                 * - For less than 5 columns, add four more blanks per
                    672:                 *   column.
                    673:                 * - For exactly 5 columns, add three more blank per
                    674:                 *   column.
                    675:                 * - For more than 5 columns, add only one column.
1.105     kristaps  676:                 */
1.202     kristaps  677:                ncols = bl->norm->Bl.ncols;
1.264     schwarze  678:                dcol = ncols < 5 ? term_len(p, 4) :
                    679:                    ncols == 5 ? term_len(p, 3) : term_len(p, 1);
1.108     kristaps  680:
1.134     kristaps  681:                /*
1.314     schwarze  682:                 * Calculate the offset by applying all prior ROFFT_BODY,
                    683:                 * so we stop at the ROFFT_HEAD (nn->prev == NULL).
1.134     kristaps  684:                 */
                    685:
1.264     schwarze  686:                for (i = 0, nn = n->prev;
                    687:                    nn->prev && i < (int)ncols;
                    688:                    nn = nn->prev, i++)
                    689:                        offset += dcol + a2width(p,
                    690:                            bl->norm->Bl.cols[i]);
1.105     kristaps  691:
                    692:                /*
1.108     kristaps  693:                 * When exceeding the declared number of columns, leave
                    694:                 * the remaining widths at 0.  This will later be
                    695:                 * adjusted to the default width of 10, or, for the last
                    696:                 * column, stretched to the right margin.
1.58      kristaps  697:                 */
1.108     kristaps  698:                if (i >= (int)ncols)
                    699:                        break;
1.58      kristaps  700:
1.105     kristaps  701:                /*
1.108     kristaps  702:                 * Use the declared column widths, extended as explained
                    703:                 * in the preceding paragraph.
1.105     kristaps  704:                 */
1.202     kristaps  705:                width = a2width(p, bl->norm->Bl.cols[i]) + dcol;
1.1       kristaps  706:                break;
                    707:        default:
1.202     kristaps  708:                if (NULL == bl->norm->Bl.width)
1.108     kristaps  709:                        break;
                    710:
1.264     schwarze  711:                /*
1.108     kristaps  712:                 * Note: buffer the width by 2, which is groff's magic
                    713:                 * number for buffering single arguments.  See the above
                    714:                 * handling for column for how this changes.
                    715:                 */
1.202     kristaps  716:                width = a2width(p, bl->norm->Bl.width) + term_len(p, 2);
1.302     schwarze  717:                if (width < 0 && (size_t)(-width) > p->offset)
                    718:                        width = -p->offset;
                    719:                else if (width > SHRT_MAX)
                    720:                        width = 0;
1.1       kristaps  721:                break;
                    722:        }
                    723:
1.264     schwarze  724:        /*
1.17      kristaps  725:         * Whitespace control.  Inset bodies need an initial space,
                    726:         * while diagonal bodies need two.
1.1       kristaps  727:         */
                    728:
1.51      kristaps  729:        p->flags |= TERMP_NOSPACE;
                    730:
1.1       kristaps  731:        switch (type) {
1.264     schwarze  732:        case LIST_diag:
1.314     schwarze  733:                if (n->type == ROFFT_BODY)
1.62      kristaps  734:                        term_word(p, "\\ \\ ");
1.51      kristaps  735:                break;
1.264     schwarze  736:        case LIST_inset:
1.314     schwarze  737:                if (n->type == ROFFT_BODY && n->parent->head->nchild)
1.51      kristaps  738:                        term_word(p, "\\ ");
1.1       kristaps  739:                break;
                    740:        default:
                    741:                break;
                    742:        }
                    743:
1.51      kristaps  744:        p->flags |= TERMP_NOSPACE;
                    745:
1.1       kristaps  746:        switch (type) {
1.264     schwarze  747:        case LIST_diag:
1.314     schwarze  748:                if (n->type == ROFFT_HEAD)
1.102     kristaps  749:                        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps  750:                break;
                    751:        default:
                    752:                break;
                    753:        }
                    754:
                    755:        /*
1.109     kristaps  756:         * Pad and break control.  This is the tricky part.  These flags
                    757:         * are documented in term_flushln() in term.c.  Note that we're
                    758:         * going to unset all of these flags in termp_it_post() when we
                    759:         * exit.
1.1       kristaps  760:         */
                    761:
                    762:        switch (type) {
1.264     schwarze  763:        case LIST_enum:
1.242     schwarze  764:                /*
                    765:                 * Weird special case.
1.302     schwarze  766:                 * Some very narrow lists actually hang.
1.242     schwarze  767:                 */
                    768:                /* FALLTHROUGH */
1.264     schwarze  769:        case LIST_bullet:
1.1       kristaps  770:                /* FALLTHROUGH */
1.264     schwarze  771:        case LIST_dash:
1.1       kristaps  772:                /* FALLTHROUGH */
1.264     schwarze  773:        case LIST_hyphen:
1.302     schwarze  774:                if (width <= (int)term_len(p, 2))
                    775:                        p->flags |= TERMP_HANG;
1.314     schwarze  776:                if (n->type != ROFFT_HEAD)
1.250     schwarze  777:                        break;
                    778:                p->flags |= TERMP_NOBREAK;
                    779:                p->trailspace = 1;
1.39      kristaps  780:                break;
1.264     schwarze  781:        case LIST_hang:
1.314     schwarze  782:                if (n->type != ROFFT_HEAD)
1.59      kristaps  783:                        break;
                    784:
                    785:                /*
                    786:                 * This is ugly.  If `-hang' is specified and the body
                    787:                 * is a `Bl' or `Bd', then we want basically to nullify
                    788:                 * the "overstep" effect in term_flushln() and treat
                    789:                 * this as a `-ohang' list instead.
                    790:                 */
1.279     schwarze  791:                if (NULL != n->next &&
1.278     schwarze  792:                    NULL != n->next->child &&
                    793:                    (MDOC_Bl == n->next->child->tok ||
                    794:                     MDOC_Bd == n->next->child->tok))
1.250     schwarze  795:                        break;
                    796:
1.263     schwarze  797:                p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
1.250     schwarze  798:                p->trailspace = 1;
1.39      kristaps  799:                break;
1.264     schwarze  800:        case LIST_tag:
1.314     schwarze  801:                if (n->type != ROFFT_HEAD)
1.39      kristaps  802:                        break;
1.250     schwarze  803:
1.263     schwarze  804:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.250     schwarze  805:                p->trailspace = 2;
                    806:
1.86      kristaps  807:                if (NULL == n->next || NULL == n->next->child)
1.39      kristaps  808:                        p->flags |= TERMP_DANGLE;
1.1       kristaps  809:                break;
1.264     schwarze  810:        case LIST_column:
1.314     schwarze  811:                if (n->type == ROFFT_HEAD)
1.134     kristaps  812:                        break;
                    813:
1.250     schwarze  814:                if (NULL == n->next) {
1.134     kristaps  815:                        p->flags &= ~TERMP_NOBREAK;
1.250     schwarze  816:                        p->trailspace = 0;
                    817:                } else {
1.134     kristaps  818:                        p->flags |= TERMP_NOBREAK;
1.250     schwarze  819:                        p->trailspace = 1;
                    820:                }
1.134     kristaps  821:
1.1       kristaps  822:                break;
1.264     schwarze  823:        case LIST_diag:
1.314     schwarze  824:                if (n->type != ROFFT_HEAD)
1.250     schwarze  825:                        break;
1.263     schwarze  826:                p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.250     schwarze  827:                p->trailspace = 1;
1.1       kristaps  828:                break;
                    829:        default:
                    830:                break;
                    831:        }
                    832:
1.264     schwarze  833:        /*
1.1       kristaps  834:         * Margin control.  Set-head-width lists have their right
                    835:         * margins shortened.  The body for these lists has the offset
                    836:         * necessarily lengthened.  Everybody gets the offset.
                    837:         */
                    838:
                    839:        p->offset += offset;
                    840:
                    841:        switch (type) {
1.264     schwarze  842:        case LIST_hang:
1.59      kristaps  843:                /*
                    844:                 * Same stipulation as above, regarding `-hang'.  We
                    845:                 * don't want to recalculate rmargin and offsets when
                    846:                 * using `Bd' or `Bl' within `-hang' overstep lists.
                    847:                 */
1.314     schwarze  848:                if (n->type == ROFFT_HEAD &&
1.279     schwarze  849:                    NULL != n->next &&
1.278     schwarze  850:                    NULL != n->next->child &&
                    851:                    (MDOC_Bl == n->next->child->tok ||
                    852:                     MDOC_Bd == n->next->child->tok))
1.59      kristaps  853:                        break;
                    854:                /* FALLTHROUGH */
1.264     schwarze  855:        case LIST_bullet:
1.1       kristaps  856:                /* FALLTHROUGH */
1.264     schwarze  857:        case LIST_dash:
1.1       kristaps  858:                /* FALLTHROUGH */
1.264     schwarze  859:        case LIST_enum:
1.1       kristaps  860:                /* FALLTHROUGH */
1.264     schwarze  861:        case LIST_hyphen:
1.1       kristaps  862:                /* FALLTHROUGH */
1.264     schwarze  863:        case LIST_tag:
1.314     schwarze  864:                if (n->type == ROFFT_HEAD)
1.1       kristaps  865:                        p->rmargin = p->offset + width;
1.292     schwarze  866:                else
1.1       kristaps  867:                        p->offset += width;
                    868:                break;
1.264     schwarze  869:        case LIST_column:
1.49      kristaps  870:                assert(width);
1.1       kristaps  871:                p->rmargin = p->offset + width;
1.264     schwarze  872:                /*
1.50      kristaps  873:                 * XXX - this behaviour is not documented: the
                    874:                 * right-most column is filled to the right margin.
                    875:                 */
1.314     schwarze  876:                if (n->type == ROFFT_HEAD)
1.134     kristaps  877:                        break;
                    878:                if (NULL == n->next && p->rmargin < p->maxrmargin)
1.50      kristaps  879:                        p->rmargin = p->maxrmargin;
1.1       kristaps  880:                break;
                    881:        default:
                    882:                break;
                    883:        }
                    884:
1.264     schwarze  885:        /*
1.1       kristaps  886:         * The dash, hyphen, bullet and enum lists all have a special
1.264     schwarze  887:         * HEAD character (temporarily bold, in some cases).
1.1       kristaps  888:         */
                    889:
1.314     schwarze  890:        if (n->type == ROFFT_HEAD)
1.1       kristaps  891:                switch (type) {
1.264     schwarze  892:                case LIST_bullet:
1.102     kristaps  893:                        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps  894:                        term_word(p, "\\[bu]");
1.102     kristaps  895:                        term_fontpop(p);
1.1       kristaps  896:                        break;
1.264     schwarze  897:                case LIST_dash:
1.1       kristaps  898:                        /* FALLTHROUGH */
1.264     schwarze  899:                case LIST_hyphen:
1.102     kristaps  900:                        term_fontpush(p, TERMFONT_BOLD);
1.27      kristaps  901:                        term_word(p, "\\(hy");
1.102     kristaps  902:                        term_fontpop(p);
1.1       kristaps  903:                        break;
1.264     schwarze  904:                case LIST_enum:
1.1       kristaps  905:                        (pair->ppair->ppair->count)++;
1.265     schwarze  906:                        (void)snprintf(buf, sizeof(buf), "%d.",
1.264     schwarze  907:                            pair->ppair->ppair->count);
1.1       kristaps  908:                        term_word(p, buf);
                    909:                        break;
                    910:                default:
                    911:                        break;
                    912:                }
                    913:
1.264     schwarze  914:        /*
1.1       kristaps  915:         * If we're not going to process our children, indicate so here.
                    916:         */
                    917:
                    918:        switch (type) {
1.264     schwarze  919:        case LIST_bullet:
1.1       kristaps  920:                /* FALLTHROUGH */
1.264     schwarze  921:        case LIST_item:
1.1       kristaps  922:                /* FALLTHROUGH */
1.264     schwarze  923:        case LIST_dash:
1.1       kristaps  924:                /* FALLTHROUGH */
1.264     schwarze  925:        case LIST_hyphen:
1.1       kristaps  926:                /* FALLTHROUGH */
1.264     schwarze  927:        case LIST_enum:
1.314     schwarze  928:                if (n->type == ROFFT_HEAD)
1.1       kristaps  929:                        return(0);
                    930:                break;
1.264     schwarze  931:        case LIST_column:
1.314     schwarze  932:                if (n->type == ROFFT_HEAD)
1.1       kristaps  933:                        return(0);
                    934:                break;
                    935:        default:
                    936:                break;
                    937:        }
                    938:
                    939:        return(1);
                    940: }
                    941:
                    942: static void
                    943: termp_it_post(DECL_ARGS)
                    944: {
1.126     kristaps  945:        enum mdoc_list     type;
1.1       kristaps  946:
1.314     schwarze  947:        if (n->type == ROFFT_BLOCK)
1.1       kristaps  948:                return;
                    949:
1.202     kristaps  950:        type = n->parent->parent->parent->norm->Bl.type;
1.1       kristaps  951:
                    952:        switch (type) {
1.264     schwarze  953:        case LIST_item:
1.1       kristaps  954:                /* FALLTHROUGH */
1.264     schwarze  955:        case LIST_diag:
1.53      kristaps  956:                /* FALLTHROUGH */
1.264     schwarze  957:        case LIST_inset:
1.314     schwarze  958:                if (n->type == ROFFT_BODY)
1.130     schwarze  959:                        term_newln(p);
1.1       kristaps  960:                break;
1.264     schwarze  961:        case LIST_column:
1.314     schwarze  962:                if (n->type == ROFFT_BODY)
1.1       kristaps  963:                        term_flushln(p);
                    964:                break;
                    965:        default:
1.130     schwarze  966:                term_newln(p);
1.1       kristaps  967:                break;
                    968:        }
                    969:
1.264     schwarze  970:        /*
1.109     kristaps  971:         * Now that our output is flushed, we can reset our tags.  Since
                    972:         * only `It' sets these flags, we're free to assume that nobody
                    973:         * has munged them in the meanwhile.
                    974:         */
                    975:
1.263     schwarze  976:        p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
                    977:                        TERMP_DANGLE | TERMP_HANG);
1.250     schwarze  978:        p->trailspace = 0;
1.1       kristaps  979: }
                    980:
                    981: static int
                    982: termp_nm_pre(DECL_ARGS)
                    983: {
1.296     schwarze  984:        const char      *cp;
1.1       kristaps  985:
1.314     schwarze  986:        if (n->type == ROFFT_BLOCK) {
1.253     schwarze  987:                p->flags |= TERMP_PREKEEP;
1.164     schwarze  988:                return(1);
1.253     schwarze  989:        }
1.164     schwarze  990:
1.314     schwarze  991:        if (n->type == ROFFT_BODY) {
1.164     schwarze  992:                if (NULL == n->child)
                    993:                        return(0);
1.234     schwarze  994:                p->flags |= TERMP_NOSPACE;
1.296     schwarze  995:                cp = NULL;
                    996:                if (n->prev->child != NULL)
                    997:                    cp = n->prev->child->string;
                    998:                if (cp == NULL)
                    999:                        cp = meta->name;
                   1000:                if (cp == NULL)
                   1001:                        p->offset += term_len(p, 6);
                   1002:                else
                   1003:                        p->offset += term_len(p, 1) + term_strlen(p, cp);
1.164     schwarze 1004:                return(1);
                   1005:        }
                   1006:
1.245     schwarze 1007:        if (NULL == n->child && NULL == meta->name)
1.164     schwarze 1008:                return(0);
1.125     kristaps 1009:
1.314     schwarze 1010:        if (n->type == ROFFT_HEAD)
1.177     schwarze 1011:                synopsis_pre(p, n->parent);
1.102     kristaps 1012:
1.314     schwarze 1013:        if (n->type == ROFFT_HEAD &&
1.278     schwarze 1014:            NULL != n->next && NULL != n->next->child) {
1.263     schwarze 1015:                p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_BRIND;
1.250     schwarze 1016:                p->trailspace = 1;
1.185     schwarze 1017:                p->rmargin = p->offset + term_len(p, 1);
                   1018:                if (NULL == n->child) {
1.245     schwarze 1019:                        p->rmargin += term_strlen(p, meta->name);
1.314     schwarze 1020:                } else if (n->child->type == ROFFT_TEXT) {
1.185     schwarze 1021:                        p->rmargin += term_strlen(p, n->child->string);
                   1022:                        if (n->child->next)
                   1023:                                p->flags |= TERMP_HANG;
                   1024:                } else {
                   1025:                        p->rmargin += term_len(p, 5);
                   1026:                        p->flags |= TERMP_HANG;
                   1027:                }
1.164     schwarze 1028:        }
                   1029:
1.102     kristaps 1030:        term_fontpush(p, TERMFONT_BOLD);
1.86      kristaps 1031:        if (NULL == n->child)
1.245     schwarze 1032:                term_word(p, meta->name);
1.1       kristaps 1033:        return(1);
                   1034: }
                   1035:
1.164     schwarze 1036: static void
                   1037: termp_nm_post(DECL_ARGS)
                   1038: {
                   1039:
1.314     schwarze 1040:        if (n->type == ROFFT_BLOCK) {
1.253     schwarze 1041:                p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1.314     schwarze 1042:        } else if (n->type == ROFFT_HEAD &&
1.278     schwarze 1043:            NULL != n->next && NULL != n->next->child) {
1.164     schwarze 1044:                term_flushln(p);
1.263     schwarze 1045:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1.250     schwarze 1046:                p->trailspace = 0;
1.314     schwarze 1047:        } else if (n->type == ROFFT_BODY && n->child != NULL)
1.164     schwarze 1048:                term_flushln(p);
                   1049: }
                   1050:
1.1       kristaps 1051: static int
                   1052: termp_fl_pre(DECL_ARGS)
                   1053: {
                   1054:
1.320     schwarze 1055:        termp_tag_pre(p, pair, meta, n);
1.102     kristaps 1056:        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1057:        term_word(p, "\\-");
1.103     kristaps 1058:
1.280     schwarze 1059:        if ( ! (n->nchild == 0 &&
                   1060:            (n->next == NULL ||
1.314     schwarze 1061:             n->next->type == ROFFT_TEXT ||
1.280     schwarze 1062:             n->next->flags & MDOC_LINE)))
1.103     kristaps 1063:                p->flags |= TERMP_NOSPACE;
                   1064:
1.1       kristaps 1065:        return(1);
                   1066: }
                   1067:
                   1068: static int
1.184     kristaps 1069: termp__a_pre(DECL_ARGS)
                   1070: {
                   1071:
                   1072:        if (n->prev && MDOC__A == n->prev->tok)
                   1073:                if (NULL == n->next || MDOC__A != n->next->tok)
                   1074:                        term_word(p, "and");
                   1075:
                   1076:        return(1);
                   1077: }
                   1078:
                   1079: static int
1.61      kristaps 1080: termp_an_pre(DECL_ARGS)
                   1081: {
                   1082:
1.282     schwarze 1083:        if (n->norm->An.auth == AUTH_split) {
                   1084:                p->flags &= ~TERMP_NOSPLIT;
                   1085:                p->flags |= TERMP_SPLIT;
                   1086:                return(0);
                   1087:        }
                   1088:        if (n->norm->An.auth == AUTH_nosplit) {
                   1089:                p->flags &= ~TERMP_SPLIT;
                   1090:                p->flags |= TERMP_NOSPLIT;
                   1091:                return(0);
                   1092:        }
1.264     schwarze 1093:
1.282     schwarze 1094:        if (p->flags & TERMP_SPLIT)
1.61      kristaps 1095:                term_newln(p);
                   1096:
1.282     schwarze 1097:        if (n->sec == SEC_AUTHORS && ! (p->flags & TERMP_NOSPLIT))
                   1098:                p->flags |= TERMP_SPLIT;
1.61      kristaps 1099:
                   1100:        return(1);
                   1101: }
                   1102:
                   1103: static int
1.1       kristaps 1104: termp_ns_pre(DECL_ARGS)
                   1105: {
                   1106:
1.215     kristaps 1107:        if ( ! (MDOC_LINE & n->flags))
                   1108:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 1109:        return(1);
                   1110: }
                   1111:
                   1112: static int
                   1113: termp_rs_pre(DECL_ARGS)
                   1114: {
                   1115:
1.86      kristaps 1116:        if (SEC_SEE_ALSO != n->sec)
1.80      kristaps 1117:                return(1);
1.314     schwarze 1118:        if (n->type == ROFFT_BLOCK && n->prev != NULL)
1.1       kristaps 1119:                term_vspace(p);
                   1120:        return(1);
                   1121: }
                   1122:
                   1123: static int
                   1124: termp_rv_pre(DECL_ARGS)
                   1125: {
1.223     kristaps 1126:        int              nchild;
1.1       kristaps 1127:
                   1128:        term_newln(p);
                   1129:
1.223     kristaps 1130:        nchild = n->nchild;
1.273     schwarze 1131:        if (nchild > 0) {
                   1132:                term_word(p, "The");
1.224     kristaps 1133:
1.273     schwarze 1134:                for (n = n->child; n; n = n->next) {
                   1135:                        term_fontpush(p, TERMFONT_BOLD);
                   1136:                        term_word(p, n->string);
                   1137:                        term_fontpop(p);
1.223     kristaps 1138:
                   1139:                        p->flags |= TERMP_NOSPACE;
1.273     schwarze 1140:                        term_word(p, "()");
                   1141:
                   1142:                        if (n->next == NULL)
                   1143:                                continue;
                   1144:
                   1145:                        if (nchild > 2) {
                   1146:                                p->flags |= TERMP_NOSPACE;
                   1147:                                term_word(p, ",");
                   1148:                        }
                   1149:                        if (n->next->next == NULL)
                   1150:                                term_word(p, "and");
1.223     kristaps 1151:                }
                   1152:
1.273     schwarze 1153:                if (nchild > 1)
                   1154:                        term_word(p, "functions return");
                   1155:                else
                   1156:                        term_word(p, "function returns");
                   1157:
                   1158:                term_word(p, "the value\\~0 if successful;");
                   1159:        } else
                   1160:                term_word(p, "Upon successful completion,"
                   1161:                    " the value\\~0 is returned;");
1.1       kristaps 1162:
1.273     schwarze 1163:        term_word(p, "otherwise the value\\~\\-1 is returned"
                   1164:            " and the global variable");
1.1       kristaps 1165:
1.102     kristaps 1166:        term_fontpush(p, TERMFONT_UNDER);
1.1       kristaps 1167:        term_word(p, "errno");
1.102     kristaps 1168:        term_fontpop(p);
1.1       kristaps 1169:
1.264     schwarze 1170:        term_word(p, "is set to indicate the error.");
1.131     kristaps 1171:        p->flags |= TERMP_SENTENCE;
1.1       kristaps 1172:
1.68      kristaps 1173:        return(0);
1.1       kristaps 1174: }
                   1175:
                   1176: static int
                   1177: termp_ex_pre(DECL_ARGS)
                   1178: {
1.224     kristaps 1179:        int              nchild;
1.1       kristaps 1180:
1.224     kristaps 1181:        term_newln(p);
1.68      kristaps 1182:        term_word(p, "The");
1.1       kristaps 1183:
1.224     kristaps 1184:        nchild = n->nchild;
                   1185:        for (n = n->child; n; n = n->next) {
1.102     kristaps 1186:                term_fontpush(p, TERMFONT_BOLD);
1.224     kristaps 1187:                term_word(p, n->string);
1.102     kristaps 1188:                term_fontpop(p);
1.224     kristaps 1189:
                   1190:                if (nchild > 2 && n->next) {
                   1191:                        p->flags |= TERMP_NOSPACE;
1.68      kristaps 1192:                        term_word(p, ",");
1.224     kristaps 1193:                }
                   1194:
                   1195:                if (n->next && NULL == n->next->next)
                   1196:                        term_word(p, "and");
1.68      kristaps 1197:        }
                   1198:
1.224     kristaps 1199:        if (nchild > 1)
1.273     schwarze 1200:                term_word(p, "utilities exit\\~0");
1.68      kristaps 1201:        else
1.273     schwarze 1202:                term_word(p, "utility exits\\~0");
1.68      kristaps 1203:
1.273     schwarze 1204:        term_word(p, "on success, and\\~>0 if an error occurs.");
1.224     kristaps 1205:
1.131     kristaps 1206:        p->flags |= TERMP_SENTENCE;
1.68      kristaps 1207:        return(0);
1.1       kristaps 1208: }
                   1209:
                   1210: static int
                   1211: termp_nd_pre(DECL_ARGS)
                   1212: {
1.25      kristaps 1213:
1.314     schwarze 1214:        if (n->type == ROFFT_BODY)
1.297     schwarze 1215:                term_word(p, "\\(en");
1.1       kristaps 1216:        return(1);
1.115     kristaps 1217: }
                   1218:
                   1219: static int
                   1220: termp_bl_pre(DECL_ARGS)
                   1221: {
                   1222:
1.314     schwarze 1223:        return(n->type != ROFFT_HEAD);
1.1       kristaps 1224: }
                   1225:
                   1226: static void
                   1227: termp_bl_post(DECL_ARGS)
                   1228: {
                   1229:
1.314     schwarze 1230:        if (n->type == ROFFT_BLOCK)
1.1       kristaps 1231:                term_newln(p);
                   1232: }
                   1233:
                   1234: static int
                   1235: termp_xr_pre(DECL_ARGS)
                   1236: {
                   1237:
1.225     kristaps 1238:        if (NULL == (n = n->child))
1.113     kristaps 1239:                return(0);
                   1240:
1.314     schwarze 1241:        assert(n->type == ROFFT_TEXT);
1.225     kristaps 1242:        term_word(p, n->string);
1.11      kristaps 1243:
1.264     schwarze 1244:        if (NULL == (n = n->next))
1.1       kristaps 1245:                return(0);
1.225     kristaps 1246:
1.1       kristaps 1247:        p->flags |= TERMP_NOSPACE;
                   1248:        term_word(p, "(");
1.222     kristaps 1249:        p->flags |= TERMP_NOSPACE;
1.225     kristaps 1250:
1.314     schwarze 1251:        assert(n->type == ROFFT_TEXT);
1.225     kristaps 1252:        term_word(p, n->string);
                   1253:
1.222     kristaps 1254:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1255:        term_word(p, ")");
1.86      kristaps 1256:
1.1       kristaps 1257:        return(0);
                   1258: }
                   1259:
1.143     kristaps 1260: /*
                   1261:  * This decides how to assert whitespace before any of the SYNOPSIS set
                   1262:  * of macros (which, as in the case of Ft/Fo and Ft/Fn, may contain
                   1263:  * macro combos).
                   1264:  */
                   1265: static void
1.315     schwarze 1266: synopsis_pre(struct termp *p, const struct roff_node *n)
1.143     kristaps 1267: {
1.264     schwarze 1268:        /*
1.143     kristaps 1269:         * Obviously, if we're not in a SYNOPSIS or no prior macros
                   1270:         * exist, do nothing.
                   1271:         */
1.160     kristaps 1272:        if (NULL == n->prev || ! (MDOC_SYNPRETTY & n->flags))
1.143     kristaps 1273:                return;
                   1274:
                   1275:        /*
                   1276:         * If we're the second in a pair of like elements, emit our
                   1277:         * newline and return.  UNLESS we're `Fo', `Fn', `Fn', in which
                   1278:         * case we soldier on.
                   1279:         */
1.264     schwarze 1280:        if (n->prev->tok == n->tok &&
                   1281:            MDOC_Ft != n->tok &&
                   1282:            MDOC_Fo != n->tok &&
                   1283:            MDOC_Fn != n->tok) {
1.143     kristaps 1284:                term_newln(p);
                   1285:                return;
                   1286:        }
                   1287:
                   1288:        /*
                   1289:         * If we're one of the SYNOPSIS set and non-like pair-wise after
                   1290:         * another (or Fn/Fo, which we've let slip through) then assert
                   1291:         * vertical space, else only newline and move on.
                   1292:         */
                   1293:        switch (n->prev->tok) {
1.264     schwarze 1294:        case MDOC_Fd:
1.143     kristaps 1295:                /* FALLTHROUGH */
1.264     schwarze 1296:        case MDOC_Fn:
1.143     kristaps 1297:                /* FALLTHROUGH */
1.264     schwarze 1298:        case MDOC_Fo:
1.143     kristaps 1299:                /* FALLTHROUGH */
1.264     schwarze 1300:        case MDOC_In:
1.143     kristaps 1301:                /* FALLTHROUGH */
1.264     schwarze 1302:        case MDOC_Vt:
1.143     kristaps 1303:                term_vspace(p);
                   1304:                break;
1.264     schwarze 1305:        case MDOC_Ft:
1.143     kristaps 1306:                if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
                   1307:                        term_vspace(p);
                   1308:                        break;
                   1309:                }
                   1310:                /* FALLTHROUGH */
                   1311:        default:
                   1312:                term_newln(p);
                   1313:                break;
                   1314:        }
                   1315: }
                   1316:
1.110     kristaps 1317: static int
                   1318: termp_vt_pre(DECL_ARGS)
                   1319: {
                   1320:
1.314     schwarze 1321:        if (n->type == ROFFT_ELEM) {
1.143     kristaps 1322:                synopsis_pre(p, n);
1.245     schwarze 1323:                return(termp_under_pre(p, pair, meta, n));
1.314     schwarze 1324:        } else if (n->type == ROFFT_BLOCK) {
1.143     kristaps 1325:                synopsis_pre(p, n);
                   1326:                return(1);
1.314     schwarze 1327:        } else if (n->type == ROFFT_HEAD)
1.110     kristaps 1328:                return(0);
                   1329:
1.245     schwarze 1330:        return(termp_under_pre(p, pair, meta, n));
1.110     kristaps 1331: }
                   1332:
1.1       kristaps 1333: static int
1.69      kristaps 1334: termp_bold_pre(DECL_ARGS)
1.1       kristaps 1335: {
                   1336:
1.320     schwarze 1337:        termp_tag_pre(p, pair, meta, n);
1.102     kristaps 1338:        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1339:        return(1);
                   1340: }
                   1341:
1.143     kristaps 1342: static int
                   1343: termp_fd_pre(DECL_ARGS)
1.1       kristaps 1344: {
                   1345:
1.143     kristaps 1346:        synopsis_pre(p, n);
1.245     schwarze 1347:        return(termp_bold_pre(p, pair, meta, n));
1.1       kristaps 1348: }
                   1349:
1.241     schwarze 1350: static void
                   1351: termp_fd_post(DECL_ARGS)
                   1352: {
                   1353:
                   1354:        term_newln(p);
                   1355: }
                   1356:
1.1       kristaps 1357: static int
                   1358: termp_sh_pre(DECL_ARGS)
                   1359: {
1.85      kristaps 1360:
1.86      kristaps 1361:        switch (n->type) {
1.314     schwarze 1362:        case ROFFT_BLOCK:
1.293     schwarze 1363:                /*
                   1364:                 * Vertical space before sections, except
                   1365:                 * when the previous section was empty.
                   1366:                 */
                   1367:                if (n->prev == NULL ||
1.322   ! schwarze 1368:                    n->prev->tok != MDOC_Sh ||
1.293     schwarze 1369:                    (n->prev->body != NULL &&
                   1370:                     n->prev->body->child != NULL))
                   1371:                        term_vspace(p);
1.65      kristaps 1372:                break;
1.314     schwarze 1373:        case ROFFT_HEAD:
1.102     kristaps 1374:                term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1375:                break;
1.314     schwarze 1376:        case ROFFT_BODY:
1.238     schwarze 1377:                p->offset = term_len(p, p->defindent);
1.322   ! schwarze 1378:                switch (n->sec) {
        !          1379:                case SEC_DESCRIPTION:
        !          1380:                        fn_prio = 0;
        !          1381:                        break;
        !          1382:                case SEC_AUTHORS:
1.239     schwarze 1383:                        p->flags &= ~(TERMP_SPLIT|TERMP_NOSPLIT);
1.322   ! schwarze 1384:                        break;
        !          1385:                default:
        !          1386:                        break;
        !          1387:                }
1.1       kristaps 1388:                break;
                   1389:        default:
                   1390:                break;
                   1391:        }
                   1392:        return(1);
                   1393: }
                   1394:
                   1395: static void
                   1396: termp_sh_post(DECL_ARGS)
                   1397: {
                   1398:
1.86      kristaps 1399:        switch (n->type) {
1.314     schwarze 1400:        case ROFFT_HEAD:
1.1       kristaps 1401:                term_newln(p);
                   1402:                break;
1.314     schwarze 1403:        case ROFFT_BODY:
1.1       kristaps 1404:                term_newln(p);
                   1405:                p->offset = 0;
                   1406:                break;
                   1407:        default:
                   1408:                break;
                   1409:        }
                   1410: }
                   1411:
                   1412: static int
                   1413: termp_bt_pre(DECL_ARGS)
                   1414: {
                   1415:
                   1416:        term_word(p, "is currently in beta test.");
1.131     kristaps 1417:        p->flags |= TERMP_SENTENCE;
1.81      kristaps 1418:        return(0);
1.1       kristaps 1419: }
                   1420:
                   1421: static void
                   1422: termp_lb_post(DECL_ARGS)
                   1423: {
                   1424:
1.122     kristaps 1425:        if (SEC_LIBRARY == n->sec && MDOC_LINE & n->flags)
1.81      kristaps 1426:                term_newln(p);
1.1       kristaps 1427: }
                   1428:
                   1429: static int
                   1430: termp_ud_pre(DECL_ARGS)
                   1431: {
                   1432:
                   1433:        term_word(p, "currently under development.");
1.131     kristaps 1434:        p->flags |= TERMP_SENTENCE;
1.86      kristaps 1435:        return(0);
1.1       kristaps 1436: }
                   1437:
                   1438: static int
                   1439: termp_d1_pre(DECL_ARGS)
                   1440: {
                   1441:
1.314     schwarze 1442:        if (n->type != ROFFT_BLOCK)
1.1       kristaps 1443:                return(1);
                   1444:        term_newln(p);
1.238     schwarze 1445:        p->offset += term_len(p, p->defindent + 1);
1.1       kristaps 1446:        return(1);
                   1447: }
                   1448:
                   1449: static int
                   1450: termp_ft_pre(DECL_ARGS)
                   1451: {
                   1452:
1.141     kristaps 1453:        /* NB: MDOC_LINE does not effect this! */
1.143     kristaps 1454:        synopsis_pre(p, n);
1.102     kristaps 1455:        term_fontpush(p, TERMFONT_UNDER);
1.1       kristaps 1456:        return(1);
                   1457: }
                   1458:
                   1459: static int
                   1460: termp_fn_pre(DECL_ARGS)
                   1461: {
1.257     schwarze 1462:        size_t           rmargin = 0;
1.226     kristaps 1463:        int              pretty;
                   1464:
                   1465:        pretty = MDOC_SYNPRETTY & n->flags;
1.1       kristaps 1466:
1.143     kristaps 1467:        synopsis_pre(p, n);
1.139     kristaps 1468:
1.226     kristaps 1469:        if (NULL == (n = n->child))
                   1470:                return(0);
                   1471:
1.251     schwarze 1472:        if (pretty) {
                   1473:                rmargin = p->rmargin;
1.257     schwarze 1474:                p->rmargin = p->offset + term_len(p, 4);
1.263     schwarze 1475:                p->flags |= TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG;
1.251     schwarze 1476:        }
                   1477:
1.314     schwarze 1478:        assert(n->type == ROFFT_TEXT);
1.102     kristaps 1479:        term_fontpush(p, TERMFONT_BOLD);
1.226     kristaps 1480:        term_word(p, n->string);
1.102     kristaps 1481:        term_fontpop(p);
1.1       kristaps 1482:
1.322   ! schwarze 1483:        if (n->sec == SEC_DESCRIPTION) {
        !          1484:                if ( ! tag_get(n->string, 0, ++fn_prio))
        !          1485:                        tag_put(n->string, 0, fn_prio, p->line);
        !          1486:        }
        !          1487:
1.251     schwarze 1488:        if (pretty) {
                   1489:                term_flushln(p);
1.263     schwarze 1490:                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
1.251     schwarze 1491:                p->offset = p->rmargin;
                   1492:                p->rmargin = rmargin;
                   1493:        }
                   1494:
1.1       kristaps 1495:        p->flags |= TERMP_NOSPACE;
                   1496:        term_word(p, "(");
1.222     kristaps 1497:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1498:
1.226     kristaps 1499:        for (n = n->next; n; n = n->next) {
1.314     schwarze 1500:                assert(n->type == ROFFT_TEXT);
1.102     kristaps 1501:                term_fontpush(p, TERMFONT_UNDER);
1.255     schwarze 1502:                if (pretty)
                   1503:                        p->flags |= TERMP_NBRWORD;
1.226     kristaps 1504:                term_word(p, n->string);
1.102     kristaps 1505:                term_fontpop(p);
                   1506:
1.226     kristaps 1507:                if (n->next) {
1.222     kristaps 1508:                        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1509:                        term_word(p, ",");
1.222     kristaps 1510:                }
1.1       kristaps 1511:        }
                   1512:
1.222     kristaps 1513:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1514:        term_word(p, ")");
                   1515:
1.226     kristaps 1516:        if (pretty) {
1.222     kristaps 1517:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 1518:                term_word(p, ";");
1.251     schwarze 1519:                term_flushln(p);
1.222     kristaps 1520:        }
1.1       kristaps 1521:
                   1522:        return(0);
                   1523: }
                   1524:
                   1525: static int
                   1526: termp_fa_pre(DECL_ARGS)
                   1527: {
1.315     schwarze 1528:        const struct roff_node  *nn;
1.1       kristaps 1529:
1.86      kristaps 1530:        if (n->parent->tok != MDOC_Fo) {
1.102     kristaps 1531:                term_fontpush(p, TERMFONT_UNDER);
1.1       kristaps 1532:                return(1);
                   1533:        }
                   1534:
1.86      kristaps 1535:        for (nn = n->child; nn; nn = nn->next) {
1.102     kristaps 1536:                term_fontpush(p, TERMFONT_UNDER);
1.258     schwarze 1537:                p->flags |= TERMP_NBRWORD;
1.86      kristaps 1538:                term_word(p, nn->string);
1.102     kristaps 1539:                term_fontpop(p);
                   1540:
1.256     schwarze 1541:                if (nn->next || (n->next && n->next->tok == MDOC_Fa)) {
1.222     kristaps 1542:                        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1543:                        term_word(p, ",");
1.222     kristaps 1544:                }
1.1       kristaps 1545:        }
                   1546:
                   1547:        return(0);
                   1548: }
                   1549:
                   1550: static int
                   1551: termp_bd_pre(DECL_ARGS)
                   1552: {
1.272     schwarze 1553:        size_t                   tabwidth, lm, len, rm, rmax;
1.315     schwarze 1554:        struct roff_node        *nn;
1.302     schwarze 1555:        int                      offset;
1.1       kristaps 1556:
1.314     schwarze 1557:        if (n->type == ROFFT_BLOCK) {
1.151     kristaps 1558:                print_bvspace(p, n, n);
1.54      kristaps 1559:                return(1);
1.314     schwarze 1560:        } else if (n->type == ROFFT_HEAD)
1.116     kristaps 1561:                return(0);
1.1       kristaps 1562:
1.288     schwarze 1563:        /* Handle the -offset argument. */
                   1564:
                   1565:        if (n->norm->Bd.offs == NULL ||
                   1566:            ! strcmp(n->norm->Bd.offs, "left"))
                   1567:                /* nothing */;
                   1568:        else if ( ! strcmp(n->norm->Bd.offs, "indent"))
                   1569:                p->offset += term_len(p, p->defindent + 1);
                   1570:        else if ( ! strcmp(n->norm->Bd.offs, "indent-two"))
                   1571:                p->offset += term_len(p, (p->defindent + 1) * 2);
1.302     schwarze 1572:        else {
                   1573:                offset = a2width(p, n->norm->Bd.offs);
                   1574:                if (offset < 0 && (size_t)(-offset) > p->offset)
                   1575:                        p->offset = 0;
                   1576:                else if (offset < SHRT_MAX)
                   1577:                        p->offset += offset;
                   1578:        }
1.85      kristaps 1579:
                   1580:        /*
                   1581:         * If -ragged or -filled are specified, the block does nothing
                   1582:         * but change the indentation.  If -unfilled or -literal are
                   1583:         * specified, text is printed exactly as entered in the display:
                   1584:         * for macro lines, a newline is appended to the line.  Blank
                   1585:         * lines are allowed.
                   1586:         */
1.264     schwarze 1587:
                   1588:        if (DISP_literal != n->norm->Bd.type &&
1.272     schwarze 1589:            DISP_unfilled != n->norm->Bd.type &&
                   1590:            DISP_centered != n->norm->Bd.type)
1.1       kristaps 1591:                return(1);
                   1592:
1.129     schwarze 1593:        tabwidth = p->tabwidth;
1.208     kristaps 1594:        if (DISP_literal == n->norm->Bd.type)
                   1595:                p->tabwidth = term_len(p, 8);
                   1596:
1.272     schwarze 1597:        lm = p->offset;
1.119     kristaps 1598:        rm = p->rmargin;
                   1599:        rmax = p->maxrmargin;
1.120     kristaps 1600:        p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1.118     kristaps 1601:
1.86      kristaps 1602:        for (nn = n->child; nn; nn = nn->next) {
1.272     schwarze 1603:                if (DISP_centered == n->norm->Bd.type) {
1.314     schwarze 1604:                        if (nn->type == ROFFT_TEXT) {
1.272     schwarze 1605:                                len = term_strlen(p, nn->string);
                   1606:                                p->offset = len >= rm ? 0 :
                   1607:                                    lm + len >= rm ? rm - len :
                   1608:                                    (lm + rm - len) / 2;
                   1609:                        } else
                   1610:                                p->offset = lm;
                   1611:                }
1.245     schwarze 1612:                print_mdoc_node(p, pair, meta, nn);
1.190     kristaps 1613:                /*
                   1614:                 * If the printed node flushes its own line, then we
                   1615:                 * needn't do it here as well.  This is hacky, but the
                   1616:                 * notion of selective eoln whitespace is pretty dumb
                   1617:                 * anyway, so don't sweat it.
                   1618:                 */
                   1619:                switch (nn->tok) {
1.264     schwarze 1620:                case MDOC_Sm:
1.193     kristaps 1621:                        /* FALLTHROUGH */
1.264     schwarze 1622:                case MDOC_br:
1.190     kristaps 1623:                        /* FALLTHROUGH */
1.264     schwarze 1624:                case MDOC_sp:
1.190     kristaps 1625:                        /* FALLTHROUGH */
1.264     schwarze 1626:                case MDOC_Bl:
1.196     kristaps 1627:                        /* FALLTHROUGH */
1.264     schwarze 1628:                case MDOC_D1:
1.196     kristaps 1629:                        /* FALLTHROUGH */
1.264     schwarze 1630:                case MDOC_Dl:
1.190     kristaps 1631:                        /* FALLTHROUGH */
1.264     schwarze 1632:                case MDOC_Lp:
1.190     kristaps 1633:                        /* FALLTHROUGH */
1.264     schwarze 1634:                case MDOC_Pp:
1.190     kristaps 1635:                        continue;
                   1636:                default:
                   1637:                        break;
                   1638:                }
1.299     schwarze 1639:                if (p->flags & TERMP_NONEWLINE ||
                   1640:                    (nn->next && ! (nn->next->flags & MDOC_LINE)))
1.180     schwarze 1641:                        continue;
                   1642:                term_flushln(p);
                   1643:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 1644:        }
1.147     kristaps 1645:
1.129     schwarze 1646:        p->tabwidth = tabwidth;
1.119     kristaps 1647:        p->rmargin = rm;
                   1648:        p->maxrmargin = rmax;
1.1       kristaps 1649:        return(0);
                   1650: }
                   1651:
                   1652: static void
                   1653: termp_bd_post(DECL_ARGS)
                   1654: {
1.119     kristaps 1655:        size_t           rm, rmax;
1.1       kristaps 1656:
1.314     schwarze 1657:        if (n->type != ROFFT_BODY)
1.1       kristaps 1658:                return;
1.118     kristaps 1659:
1.119     kristaps 1660:        rm = p->rmargin;
                   1661:        rmax = p->maxrmargin;
1.118     kristaps 1662:
1.264     schwarze 1663:        if (DISP_literal == n->norm->Bd.type ||
                   1664:            DISP_unfilled == n->norm->Bd.type)
1.120     kristaps 1665:                p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1.118     kristaps 1666:
1.60      kristaps 1667:        p->flags |= TERMP_NOSPACE;
1.130     schwarze 1668:        term_newln(p);
1.118     kristaps 1669:
1.119     kristaps 1670:        p->rmargin = rm;
                   1671:        p->maxrmargin = rmax;
1.1       kristaps 1672: }
                   1673:
1.211     kristaps 1674: static int
                   1675: termp_bx_pre(DECL_ARGS)
1.1       kristaps 1676: {
                   1677:
1.211     kristaps 1678:        if (NULL != (n = n->child)) {
                   1679:                term_word(p, n->string);
                   1680:                p->flags |= TERMP_NOSPACE;
                   1681:                term_word(p, "BSD");
                   1682:        } else {
                   1683:                term_word(p, "BSD");
                   1684:                return(0);
                   1685:        }
                   1686:
                   1687:        if (NULL != (n = n->next)) {
                   1688:                p->flags |= TERMP_NOSPACE;
1.213     kristaps 1689:                term_word(p, "-");
1.1       kristaps 1690:                p->flags |= TERMP_NOSPACE;
1.213     kristaps 1691:                term_word(p, n->string);
1.211     kristaps 1692:        }
                   1693:
                   1694:        return(0);
1.1       kristaps 1695: }
                   1696:
                   1697: static int
1.26      kristaps 1698: termp_xx_pre(DECL_ARGS)
1.1       kristaps 1699: {
1.26      kristaps 1700:        const char      *pp;
1.218     schwarze 1701:        int              flags;
1.1       kristaps 1702:
1.26      kristaps 1703:        pp = NULL;
1.86      kristaps 1704:        switch (n->tok) {
1.264     schwarze 1705:        case MDOC_Bsx:
1.194     schwarze 1706:                pp = "BSD/OS";
1.26      kristaps 1707:                break;
1.264     schwarze 1708:        case MDOC_Dx:
1.100     kristaps 1709:                pp = "DragonFly";
1.26      kristaps 1710:                break;
1.264     schwarze 1711:        case MDOC_Fx:
1.26      kristaps 1712:                pp = "FreeBSD";
                   1713:                break;
1.264     schwarze 1714:        case MDOC_Nx:
1.26      kristaps 1715:                pp = "NetBSD";
                   1716:                break;
1.264     schwarze 1717:        case MDOC_Ox:
1.26      kristaps 1718:                pp = "OpenBSD";
                   1719:                break;
1.264     schwarze 1720:        case MDOC_Ux:
1.26      kristaps 1721:                pp = "UNIX";
                   1722:                break;
                   1723:        default:
1.246     schwarze 1724:                abort();
                   1725:                /* NOTREACHED */
1.26      kristaps 1726:        }
1.1       kristaps 1727:
1.26      kristaps 1728:        term_word(p, pp);
1.214     kristaps 1729:        if (n->child) {
1.218     schwarze 1730:                flags = p->flags;
1.214     kristaps 1731:                p->flags |= TERMP_KEEP;
                   1732:                term_word(p, n->child->string);
1.218     schwarze 1733:                p->flags = flags;
1.214     kristaps 1734:        }
                   1735:        return(0);
1.1       kristaps 1736: }
                   1737:
                   1738: static void
                   1739: termp_pf_post(DECL_ARGS)
                   1740: {
                   1741:
1.298     schwarze 1742:        if ( ! (n->next == NULL || n->next->flags & MDOC_LINE))
                   1743:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 1744: }
                   1745:
                   1746: static int
                   1747: termp_ss_pre(DECL_ARGS)
                   1748: {
                   1749:
1.86      kristaps 1750:        switch (n->type) {
1.314     schwarze 1751:        case ROFFT_BLOCK:
1.1       kristaps 1752:                term_newln(p);
1.86      kristaps 1753:                if (n->prev)
1.1       kristaps 1754:                        term_vspace(p);
                   1755:                break;
1.314     schwarze 1756:        case ROFFT_HEAD:
1.102     kristaps 1757:                term_fontpush(p, TERMFONT_BOLD);
1.238     schwarze 1758:                p->offset = term_len(p, (p->defindent+1)/2);
1.1       kristaps 1759:                break;
1.314     schwarze 1760:        case ROFFT_BODY:
1.274     schwarze 1761:                p->offset = term_len(p, p->defindent);
                   1762:                break;
1.1       kristaps 1763:        default:
                   1764:                break;
                   1765:        }
                   1766:
                   1767:        return(1);
                   1768: }
                   1769:
                   1770: static void
                   1771: termp_ss_post(DECL_ARGS)
                   1772: {
                   1773:
1.314     schwarze 1774:        if (n->type == ROFFT_HEAD || n->type == ROFFT_BODY)
1.1       kristaps 1775:                term_newln(p);
                   1776: }
                   1777:
                   1778: static int
                   1779: termp_cd_pre(DECL_ARGS)
                   1780: {
                   1781:
1.143     kristaps 1782:        synopsis_pre(p, n);
1.102     kristaps 1783:        term_fontpush(p, TERMFONT_BOLD);
1.1       kristaps 1784:        return(1);
                   1785: }
                   1786:
                   1787: static int
                   1788: termp_in_pre(DECL_ARGS)
                   1789: {
1.142     schwarze 1790:
1.143     kristaps 1791:        synopsis_pre(p, n);
1.1       kristaps 1792:
1.160     kristaps 1793:        if (MDOC_SYNPRETTY & n->flags && MDOC_LINE & n->flags) {
1.138     kristaps 1794:                term_fontpush(p, TERMFONT_BOLD);
1.22      kristaps 1795:                term_word(p, "#include");
1.138     kristaps 1796:                term_word(p, "<");
                   1797:        } else {
                   1798:                term_word(p, "<");
                   1799:                term_fontpush(p, TERMFONT_UNDER);
                   1800:        }
1.22      kristaps 1801:
1.1       kristaps 1802:        p->flags |= TERMP_NOSPACE;
                   1803:        return(1);
                   1804: }
                   1805:
                   1806: static void
                   1807: termp_in_post(DECL_ARGS)
                   1808: {
                   1809:
1.160     kristaps 1810:        if (MDOC_SYNPRETTY & n->flags)
1.138     kristaps 1811:                term_fontpush(p, TERMFONT_BOLD);
                   1812:
1.79      kristaps 1813:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1814:        term_word(p, ">");
                   1815:
1.160     kristaps 1816:        if (MDOC_SYNPRETTY & n->flags)
1.138     kristaps 1817:                term_fontpop(p);
1.1       kristaps 1818: }
                   1819:
                   1820: static int
1.45      kristaps 1821: termp_sp_pre(DECL_ARGS)
                   1822: {
1.300     schwarze 1823:        struct roffsu    su;
1.312     schwarze 1824:        int              i, len;
1.45      kristaps 1825:
1.86      kristaps 1826:        switch (n->tok) {
1.264     schwarze 1827:        case MDOC_sp:
1.300     schwarze 1828:                if (n->child) {
                   1829:                        if ( ! a2roffsu(n->child->string, &su, SCALE_VS))
                   1830:                                su.scale = 1.0;
                   1831:                        len = term_vspan(p, &su);
                   1832:                } else
                   1833:                        len = 1;
1.76      kristaps 1834:                break;
1.264     schwarze 1835:        case MDOC_br:
1.76      kristaps 1836:                len = 0;
                   1837:                break;
                   1838:        default:
                   1839:                len = 1;
1.322   ! schwarze 1840:                fn_prio = 0;
1.76      kristaps 1841:                break;
1.45      kristaps 1842:        }
                   1843:
                   1844:        if (0 == len)
                   1845:                term_newln(p);
1.312     schwarze 1846:        else if (len < 0)
                   1847:                p->skipvsp -= len;
                   1848:        else
                   1849:                for (i = 0; i < len; i++)
                   1850:                        term_vspace(p);
1.45      kristaps 1851:
                   1852:        return(0);
                   1853: }
                   1854:
                   1855: static int
1.294     schwarze 1856: termp_skip_pre(DECL_ARGS)
1.268     schwarze 1857: {
                   1858:
                   1859:        return(0);
                   1860: }
                   1861:
                   1862: static int
1.188     kristaps 1863: termp_quote_pre(DECL_ARGS)
1.1       kristaps 1864: {
                   1865:
1.314     schwarze 1866:        if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1.1       kristaps 1867:                return(1);
                   1868:
1.188     kristaps 1869:        switch (n->tok) {
1.264     schwarze 1870:        case MDOC_Ao:
1.188     kristaps 1871:                /* FALLTHROUGH */
1.264     schwarze 1872:        case MDOC_Aq:
1.303     schwarze 1873:                term_word(p, n->nchild == 1 &&
                   1874:                    n->child->tok == MDOC_Mt ? "<" : "\\(la");
1.188     kristaps 1875:                break;
1.264     schwarze 1876:        case MDOC_Bro:
1.188     kristaps 1877:                /* FALLTHROUGH */
1.264     schwarze 1878:        case MDOC_Brq:
1.188     kristaps 1879:                term_word(p, "{");
                   1880:                break;
1.264     schwarze 1881:        case MDOC_Oo:
1.188     kristaps 1882:                /* FALLTHROUGH */
1.264     schwarze 1883:        case MDOC_Op:
1.188     kristaps 1884:                /* FALLTHROUGH */
1.264     schwarze 1885:        case MDOC_Bo:
1.188     kristaps 1886:                /* FALLTHROUGH */
1.264     schwarze 1887:        case MDOC_Bq:
1.188     kristaps 1888:                term_word(p, "[");
                   1889:                break;
1.264     schwarze 1890:        case MDOC_Do:
1.188     kristaps 1891:                /* FALLTHROUGH */
1.264     schwarze 1892:        case MDOC_Dq:
1.311     schwarze 1893:                term_word(p, "\\(Lq");
1.188     kristaps 1894:                break;
1.268     schwarze 1895:        case MDOC_En:
                   1896:                if (NULL == n->norm->Es ||
                   1897:                    NULL == n->norm->Es->child)
                   1898:                        return(1);
                   1899:                term_word(p, n->norm->Es->child->string);
                   1900:                break;
1.264     schwarze 1901:        case MDOC_Po:
1.188     kristaps 1902:                /* FALLTHROUGH */
1.264     schwarze 1903:        case MDOC_Pq:
1.188     kristaps 1904:                term_word(p, "(");
                   1905:                break;
1.264     schwarze 1906:        case MDOC__T:
1.205     kristaps 1907:                /* FALLTHROUGH */
1.264     schwarze 1908:        case MDOC_Qo:
1.188     kristaps 1909:                /* FALLTHROUGH */
1.264     schwarze 1910:        case MDOC_Qq:
1.188     kristaps 1911:                term_word(p, "\"");
                   1912:                break;
1.264     schwarze 1913:        case MDOC_Ql:
1.188     kristaps 1914:                /* FALLTHROUGH */
1.264     schwarze 1915:        case MDOC_So:
1.188     kristaps 1916:                /* FALLTHROUGH */
1.264     schwarze 1917:        case MDOC_Sq:
1.249     schwarze 1918:                term_word(p, "\\(oq");
1.188     kristaps 1919:                break;
                   1920:        default:
                   1921:                abort();
                   1922:                /* NOTREACHED */
                   1923:        }
1.1       kristaps 1924:
                   1925:        p->flags |= TERMP_NOSPACE;
                   1926:        return(1);
                   1927: }
                   1928:
                   1929: static void
1.188     kristaps 1930: termp_quote_post(DECL_ARGS)
1.1       kristaps 1931: {
                   1932:
1.314     schwarze 1933:        if (n->type != ROFFT_BODY && n->type != ROFFT_ELEM)
1.1       kristaps 1934:                return;
                   1935:
1.306     schwarze 1936:        p->flags |= TERMP_NOSPACE;
1.1       kristaps 1937:
1.188     kristaps 1938:        switch (n->tok) {
1.264     schwarze 1939:        case MDOC_Ao:
1.188     kristaps 1940:                /* FALLTHROUGH */
1.264     schwarze 1941:        case MDOC_Aq:
1.303     schwarze 1942:                term_word(p, n->nchild == 1 &&
                   1943:                    n->child->tok == MDOC_Mt ? ">" : "\\(ra");
1.188     kristaps 1944:                break;
1.264     schwarze 1945:        case MDOC_Bro:
1.188     kristaps 1946:                /* FALLTHROUGH */
1.264     schwarze 1947:        case MDOC_Brq:
1.188     kristaps 1948:                term_word(p, "}");
                   1949:                break;
1.264     schwarze 1950:        case MDOC_Oo:
1.188     kristaps 1951:                /* FALLTHROUGH */
1.264     schwarze 1952:        case MDOC_Op:
1.188     kristaps 1953:                /* FALLTHROUGH */
1.264     schwarze 1954:        case MDOC_Bo:
1.188     kristaps 1955:                /* FALLTHROUGH */
1.264     schwarze 1956:        case MDOC_Bq:
1.188     kristaps 1957:                term_word(p, "]");
                   1958:                break;
1.264     schwarze 1959:        case MDOC_Do:
1.188     kristaps 1960:                /* FALLTHROUGH */
1.264     schwarze 1961:        case MDOC_Dq:
1.311     schwarze 1962:                term_word(p, "\\(Rq");
1.268     schwarze 1963:                break;
                   1964:        case MDOC_En:
1.306     schwarze 1965:                if (n->norm->Es == NULL ||
                   1966:                    n->norm->Es->child == NULL ||
                   1967:                    n->norm->Es->child->next == NULL)
                   1968:                        p->flags &= ~TERMP_NOSPACE;
                   1969:                else
1.268     schwarze 1970:                        term_word(p, n->norm->Es->child->next->string);
1.188     kristaps 1971:                break;
1.264     schwarze 1972:        case MDOC_Po:
1.188     kristaps 1973:                /* FALLTHROUGH */
1.264     schwarze 1974:        case MDOC_Pq:
1.188     kristaps 1975:                term_word(p, ")");
                   1976:                break;
1.264     schwarze 1977:        case MDOC__T:
1.205     kristaps 1978:                /* FALLTHROUGH */
1.264     schwarze 1979:        case MDOC_Qo:
1.188     kristaps 1980:                /* FALLTHROUGH */
1.264     schwarze 1981:        case MDOC_Qq:
1.188     kristaps 1982:                term_word(p, "\"");
                   1983:                break;
1.264     schwarze 1984:        case MDOC_Ql:
1.188     kristaps 1985:                /* FALLTHROUGH */
1.264     schwarze 1986:        case MDOC_So:
1.188     kristaps 1987:                /* FALLTHROUGH */
1.264     schwarze 1988:        case MDOC_Sq:
1.249     schwarze 1989:                term_word(p, "\\(cq");
1.188     kristaps 1990:                break;
                   1991:        default:
                   1992:                abort();
                   1993:                /* NOTREACHED */
                   1994:        }
1.306     schwarze 1995: }
                   1996:
                   1997: static int
                   1998: termp_eo_pre(DECL_ARGS)
                   1999: {
                   2000:
1.314     schwarze 2001:        if (n->type != ROFFT_BODY)
1.306     schwarze 2002:                return(1);
                   2003:
                   2004:        if (n->end == ENDBODY_NOT &&
                   2005:            n->parent->head->child == NULL &&
                   2006:            n->child != NULL &&
                   2007:            n->child->end != ENDBODY_NOT)
                   2008:                term_word(p, "\\&");
                   2009:        else if (n->end != ENDBODY_NOT ? n->child != NULL :
1.309     schwarze 2010:             n->parent->head->child != NULL && (n->child != NULL ||
                   2011:             (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1.306     schwarze 2012:                p->flags |= TERMP_NOSPACE;
                   2013:
                   2014:        return(1);
                   2015: }
                   2016:
                   2017: static void
                   2018: termp_eo_post(DECL_ARGS)
                   2019: {
                   2020:        int      body, tail;
                   2021:
1.314     schwarze 2022:        if (n->type != ROFFT_BODY)
1.306     schwarze 2023:                return;
                   2024:
                   2025:        if (n->end != ENDBODY_NOT) {
                   2026:                p->flags &= ~TERMP_NOSPACE;
                   2027:                return;
                   2028:        }
                   2029:
                   2030:        body = n->child != NULL || n->parent->head->child != NULL;
                   2031:        tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
                   2032:
                   2033:        if (body && tail)
                   2034:                p->flags |= TERMP_NOSPACE;
                   2035:        else if ( ! (body || tail))
                   2036:                term_word(p, "\\&");
                   2037:        else if ( ! tail)
                   2038:                p->flags &= ~TERMP_NOSPACE;
1.1       kristaps 2039: }
                   2040:
                   2041: static int
                   2042: termp_fo_pre(DECL_ARGS)
                   2043: {
1.257     schwarze 2044:        size_t           rmargin = 0;
1.256     schwarze 2045:        int              pretty;
                   2046:
                   2047:        pretty = MDOC_SYNPRETTY & n->flags;
1.1       kristaps 2048:
1.314     schwarze 2049:        if (n->type == ROFFT_BLOCK) {
1.143     kristaps 2050:                synopsis_pre(p, n);
1.141     kristaps 2051:                return(1);
1.314     schwarze 2052:        } else if (n->type == ROFFT_BODY) {
1.256     schwarze 2053:                if (pretty) {
                   2054:                        rmargin = p->rmargin;
1.257     schwarze 2055:                        p->rmargin = p->offset + term_len(p, 4);
1.263     schwarze 2056:                        p->flags |= TERMP_NOBREAK | TERMP_BRIND |
                   2057:                                        TERMP_HANG;
1.256     schwarze 2058:                }
1.33      kristaps 2059:                p->flags |= TERMP_NOSPACE;
1.1       kristaps 2060:                term_word(p, "(");
1.222     kristaps 2061:                p->flags |= TERMP_NOSPACE;
1.256     schwarze 2062:                if (pretty) {
                   2063:                        term_flushln(p);
1.263     schwarze 2064:                        p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND |
                   2065:                                        TERMP_HANG);
1.256     schwarze 2066:                        p->offset = p->rmargin;
                   2067:                        p->rmargin = rmargin;
                   2068:                }
1.1       kristaps 2069:                return(1);
1.256     schwarze 2070:        }
1.141     kristaps 2071:
1.169     kristaps 2072:        if (NULL == n->child)
                   2073:                return(0);
                   2074:
1.141     kristaps 2075:        /* XXX: we drop non-initial arguments as per groff. */
1.1       kristaps 2076:
1.141     kristaps 2077:        assert(n->child->string);
1.102     kristaps 2078:        term_fontpush(p, TERMFONT_BOLD);
1.141     kristaps 2079:        term_word(p, n->child->string);
1.1       kristaps 2080:        return(0);
                   2081: }
                   2082:
                   2083: static void
                   2084: termp_fo_post(DECL_ARGS)
                   2085: {
                   2086:
1.314     schwarze 2087:        if (n->type != ROFFT_BODY)
1.143     kristaps 2088:                return;
                   2089:
1.222     kristaps 2090:        p->flags |= TERMP_NOSPACE;
1.143     kristaps 2091:        term_word(p, ")");
                   2092:
1.222     kristaps 2093:        if (MDOC_SYNPRETTY & n->flags) {
                   2094:                p->flags |= TERMP_NOSPACE;
1.143     kristaps 2095:                term_word(p, ";");
1.256     schwarze 2096:                term_flushln(p);
1.222     kristaps 2097:        }
1.1       kristaps 2098: }
                   2099:
                   2100: static int
                   2101: termp_bf_pre(DECL_ARGS)
                   2102: {
                   2103:
1.314     schwarze 2104:        if (n->type == ROFFT_HEAD)
1.1       kristaps 2105:                return(0);
1.314     schwarze 2106:        else if (n->type != ROFFT_BODY)
1.1       kristaps 2107:                return(1);
                   2108:
1.264     schwarze 2109:        if (FONT_Em == n->norm->Bf.font)
1.102     kristaps 2110:                term_fontpush(p, TERMFONT_UNDER);
1.264     schwarze 2111:        else if (FONT_Sy == n->norm->Bf.font)
1.102     kristaps 2112:                term_fontpush(p, TERMFONT_BOLD);
1.264     schwarze 2113:        else
1.102     kristaps 2114:                term_fontpush(p, TERMFONT_NONE);
1.1       kristaps 2115:
                   2116:        return(1);
                   2117: }
                   2118:
                   2119: static int
                   2120: termp_sm_pre(DECL_ARGS)
                   2121: {
                   2122:
1.269     schwarze 2123:        if (NULL == n->child)
                   2124:                p->flags ^= TERMP_NONOSPACE;
                   2125:        else if (0 == strcmp("on", n->child->string))
1.1       kristaps 2126:                p->flags &= ~TERMP_NONOSPACE;
1.269     schwarze 2127:        else
1.1       kristaps 2128:                p->flags |= TERMP_NONOSPACE;
1.269     schwarze 2129:
                   2130:        if (p->col && ! (TERMP_NONOSPACE & p->flags))
                   2131:                p->flags &= ~TERMP_NOSPACE;
1.1       kristaps 2132:
                   2133:        return(0);
                   2134: }
                   2135:
                   2136: static int
                   2137: termp_ap_pre(DECL_ARGS)
                   2138: {
                   2139:
                   2140:        p->flags |= TERMP_NOSPACE;
1.188     kristaps 2141:        term_word(p, "'");
1.1       kristaps 2142:        p->flags |= TERMP_NOSPACE;
                   2143:        return(1);
                   2144: }
                   2145:
                   2146: static void
                   2147: termp____post(DECL_ARGS)
                   2148: {
1.184     kristaps 2149:
                   2150:        /*
                   2151:         * Handle lists of authors.  In general, print each followed by
                   2152:         * a comma.  Don't print the comma if there are only two
                   2153:         * authors.
                   2154:         */
                   2155:        if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok)
                   2156:                if (NULL == n->next->next || MDOC__A != n->next->next->tok)
                   2157:                        if (NULL == n->prev || MDOC__A != n->prev->tok)
                   2158:                                return;
1.1       kristaps 2159:
1.95      kristaps 2160:        /* TODO: %U. */
                   2161:
1.187     kristaps 2162:        if (NULL == n->parent || MDOC_Rs != n->parent->tok)
                   2163:                return;
                   2164:
1.222     kristaps 2165:        p->flags |= TERMP_NOSPACE;
1.186     kristaps 2166:        if (NULL == n->next) {
                   2167:                term_word(p, ".");
                   2168:                p->flags |= TERMP_SENTENCE;
                   2169:        } else
                   2170:                term_word(p, ",");
1.1       kristaps 2171: }
                   2172:
                   2173: static int
1.102     kristaps 2174: termp_li_pre(DECL_ARGS)
                   2175: {
                   2176:
                   2177:        term_fontpush(p, TERMFONT_NONE);
                   2178:        return(1);
                   2179: }
                   2180:
                   2181: static int
1.1       kristaps 2182: termp_lk_pre(DECL_ARGS)
                   2183: {
1.315     schwarze 2184:        const struct roff_node *link, *descr;
1.1       kristaps 2185:
1.240     schwarze 2186:        if (NULL == (link = n->child))
                   2187:                return(0);
1.181     kristaps 2188:
1.240     schwarze 2189:        if (NULL != (descr = link->next)) {
                   2190:                term_fontpush(p, TERMFONT_UNDER);
                   2191:                while (NULL != descr) {
                   2192:                        term_word(p, descr->string);
                   2193:                        descr = descr->next;
                   2194:                }
                   2195:                p->flags |= TERMP_NOSPACE;
                   2196:                term_word(p, ":");
                   2197:                term_fontpop(p);
                   2198:        }
1.1       kristaps 2199:
1.102     kristaps 2200:        term_fontpush(p, TERMFONT_BOLD);
1.240     schwarze 2201:        term_word(p, link->string);
1.102     kristaps 2202:        term_fontpop(p);
1.12      kristaps 2203:
1.1       kristaps 2204:        return(0);
                   2205: }
                   2206:
1.159     schwarze 2207: static int
                   2208: termp_bk_pre(DECL_ARGS)
                   2209: {
                   2210:
1.161     schwarze 2211:        switch (n->type) {
1.314     schwarze 2212:        case ROFFT_BLOCK:
1.166     kristaps 2213:                break;
1.314     schwarze 2214:        case ROFFT_HEAD:
1.161     schwarze 2215:                return(0);
1.314     schwarze 2216:        case ROFFT_BODY:
1.200     schwarze 2217:                if (n->parent->args || 0 == n->prev->nchild)
                   2218:                        p->flags |= TERMP_PREKEEP;
1.166     kristaps 2219:                break;
1.161     schwarze 2220:        default:
                   2221:                abort();
1.166     kristaps 2222:                /* NOTREACHED */
1.161     schwarze 2223:        }
1.166     kristaps 2224:
                   2225:        return(1);
1.159     schwarze 2226: }
                   2227:
                   2228: static void
                   2229: termp_bk_post(DECL_ARGS)
                   2230: {
                   2231:
1.314     schwarze 2232:        if (n->type == ROFFT_BODY)
1.161     schwarze 2233:                p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
1.203     kristaps 2234: }
                   2235:
                   2236: static void
                   2237: termp__t_post(DECL_ARGS)
                   2238: {
                   2239:
                   2240:        /*
                   2241:         * If we're in an `Rs' and there's a journal present, then quote
                   2242:         * us instead of underlining us (for disambiguation).
                   2243:         */
1.217     schwarze 2244:        if (n->parent && MDOC_Rs == n->parent->tok &&
1.264     schwarze 2245:            n->parent->norm->Rs.quote_T)
1.245     schwarze 2246:                termp_quote_post(p, pair, meta, n);
1.203     kristaps 2247:
1.245     schwarze 2248:        termp____post(p, pair, meta, n);
1.203     kristaps 2249: }
                   2250:
                   2251: static int
                   2252: termp__t_pre(DECL_ARGS)
                   2253: {
                   2254:
                   2255:        /*
                   2256:         * If we're in an `Rs' and there's a journal present, then quote
                   2257:         * us instead of underlining us (for disambiguation).
                   2258:         */
                   2259:        if (n->parent && MDOC_Rs == n->parent->tok &&
1.264     schwarze 2260:            n->parent->norm->Rs.quote_T)
1.245     schwarze 2261:                return(termp_quote_pre(p, pair, meta, n));
1.203     kristaps 2262:
                   2263:        term_fontpush(p, TERMFONT_UNDER);
                   2264:        return(1);
1.159     schwarze 2265: }
1.1       kristaps 2266:
                   2267: static int
1.69      kristaps 2268: termp_under_pre(DECL_ARGS)
1.1       kristaps 2269: {
                   2270:
1.102     kristaps 2271:        term_fontpush(p, TERMFONT_UNDER);
1.320     schwarze 2272:        return(1);
                   2273: }
                   2274:
                   2275: static int
                   2276: termp_tag_pre(DECL_ARGS)
                   2277: {
                   2278:
                   2279:        if (n->child != NULL &&
                   2280:            n->child->type == ROFFT_TEXT &&
                   2281:            n->prev == NULL &&
                   2282:            (n->parent->tok == MDOC_It ||
                   2283:             (n->parent->tok == MDOC_Xo &&
                   2284:              n->parent->parent->prev == NULL &&
                   2285:              n->parent->parent->parent->tok == MDOC_It)) &&
1.321     schwarze 2286:            ! tag_get(n->child->string, 0, 1))
                   2287:                tag_put(n->child->string, 0, 1, p->line);
1.84      kristaps 2288:        return(1);
                   2289: }

CVSweb