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

Annotation of mandoc/mdoc_term.c, Revision 1.292

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

CVSweb