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

Annotation of mandoc/mdoc_term.c, Revision 1.339

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

CVSweb