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