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