]> git.cameronkatri.com Git - mandoc.git/blobdiff - mdoc_term.c
Add -Owidth=width option to mandoc -Tascii. Asked for by joerg@ about a
[mandoc.git] / mdoc_term.c
index c030e3373148e4fe54783e50c3279c91fc2e2af0..e6be6214a9a6e713e5c4f6dd0464493202774e75 100644 (file)
@@ -1,4 +1,4 @@
-/*     $Id: mdoc_term.c,v 1.128 2010/05/24 14:35:59 schwarze Exp $ */
+/*     $Id: mdoc_term.c,v 1.142 2010/06/06 22:08:15 schwarze Exp $ */
 /*
  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
  *
@@ -82,7 +82,6 @@ static        void      termp_dq_post(DECL_ARGS);
 static void      termp_fd_post(DECL_ARGS);
 static void      termp_fn_post(DECL_ARGS);
 static void      termp_fo_post(DECL_ARGS);
-static void      termp_ft_post(DECL_ARGS);
 static void      termp_in_post(DECL_ARGS);
 static void      termp_it_post(DECL_ARGS);
 static void      termp_lb_post(DECL_ARGS);
@@ -166,7 +165,7 @@ static      const struct termact termacts[MDOC_MAX] = {
        { termp_bold_pre, termp_fd_post }, /* Fd */ 
        { termp_fl_pre, NULL }, /* Fl */
        { termp_fn_pre, termp_fn_post }, /* Fn */ 
-       { termp_ft_pre, termp_ft_post }, /* Ft */ 
+       { termp_ft_pre, NULL }, /* Ft */ 
        { termp_bold_pre, NULL }, /* Ic */ 
        { termp_in_pre, termp_in_post }, /* In */ 
        { termp_li_pre, NULL }, /* Li */
@@ -260,6 +259,7 @@ static      const struct termact termacts[MDOC_MAX] = {
        { termp_sp_pre, NULL }, /* br */
        { termp_sp_pre, NULL }, /* sp */ 
        { termp_under_pre, termp____post }, /* %U */ 
+       { NULL, NULL }, /* Ta */ 
 };
 
 
@@ -274,6 +274,7 @@ terminal_mdoc(void *arg, const struct mdoc *mdoc)
 
        p->overstep = 0;
        p->maxrmargin = p->defrmargin;
+       p->tabwidth = 5;
 
        if (NULL == p->symtab)
                switch (p->enc) {
@@ -699,7 +700,7 @@ termp_it_pre(DECL_ARGS)
 
        switch (type) {
        case (LIST_column):
-               if (MDOC_BODY == n->type)
+               if (MDOC_HEAD == n->type)
                        break;
                /*
                 * Imitate groff's column handling:
@@ -714,8 +715,13 @@ termp_it_pre(DECL_ARGS)
                /* LINTED */
                dcol = ncols < 5 ? 4 : ncols == 5 ? 3 : 1;
 
+               /*
+                * Calculate the offset by applying all prior MDOC_BODY,
+                * so we stop at the MDOC_HEAD (NULL == nn->prev).
+                */
+
                for (i = 0, nn = n->prev; 
-                               nn && i < (int)ncols; 
+                               nn->prev && i < (int)ncols; 
                                nn = nn->prev, i++)
                        offset += dcol + a2width
                                (&bl->args->argv[vals[2]], i);
@@ -868,15 +874,18 @@ termp_it_pre(DECL_ARGS)
                        p->flags |= TERMP_DANGLE;
                break;
        case (LIST_column):
-               if (MDOC_HEAD == n->type) {
-                       assert(n->next);
-                       if (MDOC_BODY == n->next->type)
-                               p->flags &= ~TERMP_NOBREAK;
-                       else
-                               p->flags |= TERMP_NOBREAK;
-                       if (n->prev) 
-                               p->flags |= TERMP_NOLPAD;
-               }
+               if (MDOC_HEAD == n->type)
+                       break;
+
+               if (NULL == n->next)
+                       p->flags &= ~TERMP_NOBREAK;
+               else
+                       p->flags |= TERMP_NOBREAK;
+
+               assert(n->prev);
+               if (MDOC_BODY == n->prev->type) 
+                       p->flags |= TERMP_NOLPAD;
+
                break;
        case (LIST_diag):
                if (MDOC_HEAD == n->type)
@@ -928,9 +937,9 @@ termp_it_pre(DECL_ARGS)
                 * XXX - this behaviour is not documented: the
                 * right-most column is filled to the right margin.
                 */
-               if (MDOC_HEAD == n->type &&
-                               MDOC_BODY == n->next->type &&
-                               p->rmargin < p->maxrmargin)
+               if (MDOC_HEAD == n->type)
+                       break;
+               if (NULL == n->next && p->rmargin < p->maxrmargin)
                        p->rmargin = p->maxrmargin;
                break;
        default:
@@ -984,7 +993,7 @@ termp_it_pre(DECL_ARGS)
                        return(0);
                break;
        case (LIST_column):
-               if (MDOC_BODY == n->type)
+               if (MDOC_HEAD == n->type)
                        return(0);
                break;
        default:
@@ -1013,14 +1022,14 @@ termp_it_post(DECL_ARGS)
                /* FALLTHROUGH */
        case (LIST_inset):
                if (MDOC_BODY == n->type)
-                       term_flushln(p);
+                       term_newln(p);
                break;
        case (LIST_column):
-               if (MDOC_HEAD == n->type)
+               if (MDOC_BODY == n->type)
                        term_flushln(p);
                break;
        default:
-               term_flushln(p);
+               term_newln(p);
                break;
        }
 
@@ -1191,6 +1200,7 @@ termp_rv_pre(DECL_ARGS)
        term_fontpop(p);
 
                term_word(p, "is set to indicate the error.");
+       p->flags |= TERMP_SENTENCE;
 
        return(0);
 }
@@ -1223,6 +1233,7 @@ termp_ex_pre(DECL_ARGS)
                term_word(p, "utility exits");
 
                term_word(p, "0 on success, and >0 if an error occurs.");
+       p->flags |= TERMP_SENTENCE;
 
        return(0);
 }
@@ -1346,12 +1357,7 @@ static void
 termp_fd_post(DECL_ARGS)
 {
 
-       if (n->sec != SEC_SYNOPSIS || ! (MDOC_LINE & n->flags))
-               return;
-
        term_newln(p);
-       if (n->next && MDOC_Fd != n->next->tok)
-               term_vspace(p);
 }
 
 
@@ -1424,6 +1430,7 @@ termp_bt_pre(DECL_ARGS)
 {
 
        term_word(p, "is currently in beta test.");
+       p->flags |= TERMP_SENTENCE;
        return(0);
 }
 
@@ -1444,6 +1451,7 @@ termp_ud_pre(DECL_ARGS)
 {
 
        term_word(p, "currently under development.");
+       p->flags |= TERMP_SENTENCE;
        return(0);
 }
 
@@ -1502,31 +1510,29 @@ static int
 termp_ft_pre(DECL_ARGS)
 {
 
-       if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags)
-               if (n->prev && MDOC_Fo == n->prev->tok)
-                       term_vspace(p);
+       /* NB: MDOC_LINE does not effect this! */
+       if (SEC_SYNOPSIS == n->sec && n->prev)
+               term_vspace(p);
 
        term_fontpush(p, TERMFONT_UNDER);
        return(1);
 }
 
 
-/* ARGSUSED */
-static void
-termp_ft_post(DECL_ARGS)
-{
-
-       if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags)
-               term_newln(p);
-}
-
-
 /* ARGSUSED */
 static int
 termp_fn_pre(DECL_ARGS)
 {
        const struct mdoc_node  *nn;
 
+       /* NB: MDOC_LINE has no effect on this macro! */
+       if (SEC_SYNOPSIS == n->sec) {
+               if (n->prev && MDOC_Ft == n->prev->tok)
+                       term_newln(p);
+               else if (n->prev)
+                       term_vspace(p);
+       }
+
        term_fontpush(p, TERMFONT_BOLD);
        term_word(p, n->child->string);
        term_fontpop(p);
@@ -1557,8 +1563,9 @@ static void
 termp_fn_post(DECL_ARGS)
 {
 
-       if (n->sec == SEC_SYNOPSIS && n->next && MDOC_LINE & n->flags)
-               term_vspace(p);
+       /* NB: MDOC_LINE has no effect on this macro! */
+       if (SEC_SYNOPSIS == n->sec)
+               term_newln(p);
 }
 
 
@@ -1593,6 +1600,7 @@ termp_fa_pre(DECL_ARGS)
 static int
 termp_bd_pre(DECL_ARGS)
 {
+       size_t                   tabwidth;
        int                      i, type;
        size_t                   rm, rmax;
        const struct mdoc_node  *nn;
@@ -1622,6 +1630,8 @@ termp_bd_pre(DECL_ARGS)
        if (MDOC_Literal != type && MDOC_Unfilled != type)
                return(1);
 
+       tabwidth = p->tabwidth;
+       p->tabwidth = 8;
        rm = p->rmargin;
        rmax = p->maxrmargin;
        p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
@@ -1629,13 +1639,12 @@ termp_bd_pre(DECL_ARGS)
        for (nn = n->child; nn; nn = nn->next) {
                p->flags |= TERMP_NOSPACE;
                print_mdoc_node(p, pair, m, nn);
-               if (NULL == nn->next)
-                       continue;
-               if (nn->prev && nn->prev->line < nn->line)
-                       term_flushln(p);
-               else if (NULL == nn->prev)
+               if (NULL == nn->prev ||
+                   nn->prev->line < nn->line ||
+                   NULL == nn->next)
                        term_flushln(p);
        }
+       p->tabwidth = tabwidth;
 
        p->rmargin = rm;
        p->maxrmargin = rmax;
@@ -1663,7 +1672,7 @@ termp_bd_post(DECL_ARGS)
                p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
 
        p->flags |= TERMP_NOSPACE;
-       term_flushln(p);
+       term_newln(p);
 
        p->rmargin = rm;
        p->maxrmargin = rmax;
@@ -1836,11 +1845,18 @@ static int
 termp_in_pre(DECL_ARGS)
 {
 
-       term_fontpush(p, TERMFONT_BOLD);
-       if (SEC_SYNOPSIS == n->sec)
+       if (SEC_SYNOPSIS == n->sec && n->prev && MDOC_In != n->prev->tok)
+               term_vspace(p);
+
+       if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags) {
+               term_fontpush(p, TERMFONT_BOLD);
                term_word(p, "#include");
+               term_word(p, "<");
+       } else {
+               term_word(p, "<");
+               term_fontpush(p, TERMFONT_UNDER);
+       }
 
-       term_word(p, "<");
        p->flags |= TERMP_NOSPACE;
        return(1);
 }
@@ -1851,23 +1867,16 @@ static void
 termp_in_post(DECL_ARGS)
 {
 
-       term_fontpush(p, TERMFONT_BOLD);
+       if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags)
+               term_fontpush(p, TERMFONT_BOLD);
+
        p->flags |= TERMP_NOSPACE;
        term_word(p, ">");
-       term_fontpop(p);
-
-       if (SEC_SYNOPSIS != n->sec && ! (MDOC_LINE & n->flags))
-               return;
 
-       term_newln(p);
-       /* 
-        * XXX Not entirely correct.  If `.In foo bar' is specified in
-        * the SYNOPSIS section, then it produces a single break after
-        * the <foo>; mandoc asserts a vertical space.  Since this
-        * construction is rarely used, I think it's fine.
-        */
-       if (n->next && MDOC_In != n->next->tok)
-               term_vspace(p);
+       if (SEC_SYNOPSIS == n->sec && MDOC_LINE & n->flags) {
+               term_fontpop(p);
+               term_newln(p);
+       }
 }
 
 
@@ -1976,23 +1985,29 @@ termp_pq_post(DECL_ARGS)
 static int
 termp_fo_pre(DECL_ARGS)
 {
-       const struct mdoc_node *nn;
 
-       if (MDOC_BODY == n->type) {
+       if (MDOC_BLOCK == n->type) {
+               /* NB: MDOC_LINE has no effect on this macro! */
+               if (SEC_SYNOPSIS != n->sec)
+                       return(1);
+               if (n->prev && MDOC_Ft == n->prev->tok)
+                       term_newln(p);
+               else if (n->prev)
+                       term_vspace(p);
+               return(1);
+       } else if (MDOC_BODY == n->type) {
                p->flags |= TERMP_NOSPACE;
                term_word(p, "(");
                p->flags |= TERMP_NOSPACE;
                return(1);
-       } else if (MDOC_HEAD != n->type) 
-               return(1);
+       } 
 
-       term_fontpush(p, TERMFONT_BOLD);
-       for (nn = n->child; nn; nn = nn->next) {
-               assert(MDOC_TEXT == nn->type);
-               term_word(p, nn->string);
-       }
-       term_fontpop(p);
+       /* XXX: we drop non-initial arguments as per groff. */
 
+       assert(n->child);
+       assert(n->child->string);
+       term_fontpush(p, TERMFONT_BOLD);
+       term_word(p, n->child->string);
        return(0);
 }
 
@@ -2002,13 +2017,18 @@ static void
 termp_fo_post(DECL_ARGS)
 {
 
-       if (MDOC_BODY != n->type)
-               return;
-       p->flags |= TERMP_NOSPACE;
-       term_word(p, ")");
-       p->flags |= TERMP_NOSPACE;
-       term_word(p, ";");
-       term_newln(p);
+       if (MDOC_BLOCK == n->type) {
+               /* NB: MDOC_LINE has no effect on this macro! */
+               if (SEC_SYNOPSIS == n->sec)
+                       term_newln(p);
+       } else if (MDOC_BODY == n->type) {
+               p->flags |= TERMP_NOSPACE;
+               term_word(p, ")");
+               if (SEC_SYNOPSIS == n->sec) {
+                       p->flags |= TERMP_NOSPACE;
+                       term_word(p, ";");
+               }
+       }
 }