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

Annotation of mandoc/mdoc_term.c, Revision 1.309

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

CVSweb