]> git.cameronkatri.com Git - mandoc.git/blobdiff - mdoc_term.c
Implement the traditional -h option for man(1): show the SYNOPSIS only.
[mandoc.git] / mdoc_term.c
index 12ce2b0f24d4263a8627a64791b232b810a38a20..21aa4a7c0b3431d508df6b315a3736212e072e2a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $Id: mdoc_term.c,v 1.276 2014/08/10 23:54:41 schwarze Exp $ */
+/*     $Id: mdoc_term.c,v 1.281 2014/09/03 05:22:45 schwarze Exp $ */
 /*
  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -252,34 +252,44 @@ static    const struct termact termacts[MDOC_MAX] = {
 void
 terminal_mdoc(void *arg, const struct mdoc *mdoc)
 {
-       const struct mdoc_node  *n;
        const struct mdoc_meta  *meta;
+       struct mdoc_node        *n;
        struct termp            *p;
 
        p = (struct termp *)arg;
 
-       if (0 == p->defindent)
-               p->defindent = 5;
-
        p->overstep = 0;
-       p->maxrmargin = p->defrmargin;
+       p->rmargin = p->maxrmargin = p->defrmargin;
        p->tabwidth = term_len(p, 5);
 
        if (NULL == p->symtab)
                p->symtab = mchars_alloc();
 
-       n = mdoc_node(mdoc);
+       n = mdoc_node(mdoc)->child;
        meta = mdoc_meta(mdoc);
 
-       term_begin(p, print_mdoc_head, print_mdoc_foot, meta);
-
-       if (n->child) {
-               if (MDOC_Sh != n->child->tok)
-                       term_vspace(p);
-               print_mdoc_nodelist(p, NULL, meta, n->child);
+       if (p->synopsisonly) {
+               while (n != NULL) {
+                       if (n->tok == MDOC_Sh && n->sec == SEC_SYNOPSIS) {
+                               if (n->child->next->child != NULL)
+                                       print_mdoc_nodelist(p, NULL,
+                                           meta, n->child->next->child);
+                               term_newln(p);
+                               break;
+                       }
+                       n = n->next;
+               }
+       } else {
+               if (p->defindent == 0)
+                       p->defindent = 5;
+               term_begin(p, print_mdoc_head, print_mdoc_foot, meta);
+               if (n != NULL) {
+                       if (n->tok != MDOC_Sh)
+                               term_vspace(p);
+                       print_mdoc_nodelist(p, NULL, meta, n);
+               }
+               term_end(p);
        }
-
-       term_end(p);
 }
 
 static void
@@ -463,9 +473,6 @@ print_mdoc_head(struct termp *p, const void *arg)
         * switches on the manual section.
         */
 
-       p->offset = 0;
-       p->rmargin = p->maxrmargin;
-
        assert(meta->vol);
        if (NULL == meta->arch)
                volume = mandoc_strdup(meta->vol);
@@ -806,7 +813,8 @@ termp_it_pre(DECL_ARGS)
                 * the "overstep" effect in term_flushln() and treat
                 * this as a `-ohang' list instead.
                 */
-               if (n->next->child &&
+               if (NULL != n->next &&
+                   NULL != n->next->child &&
                    (MDOC_Bl == n->next->child->tok ||
                     MDOC_Bd == n->next->child->tok))
                        break;
@@ -862,7 +870,9 @@ termp_it_pre(DECL_ARGS)
                 * don't want to recalculate rmargin and offsets when
                 * using `Bd' or `Bl' within `-hang' overstep lists.
                 */
-               if (MDOC_HEAD == n->type && n->next->child &&
+               if (MDOC_HEAD == n->type &&
+                   NULL != n->next &&
+                   NULL != n->next->child &&
                    (MDOC_Bl == n->next->child->tok ||
                     MDOC_Bd == n->next->child->tok))
                        break;
@@ -1027,7 +1037,8 @@ termp_nm_pre(DECL_ARGS)
        if (MDOC_HEAD == n->type)
                synopsis_pre(p, n->parent);
 
-       if (MDOC_HEAD == n->type && n->next->child) {
+       if (MDOC_HEAD == n->type &&
+           NULL != n->next && NULL != n->next->child) {
                p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_BRIND;
                p->trailspace = 1;
                p->rmargin = p->offset + term_len(p, 1);
@@ -1055,7 +1066,8 @@ termp_nm_post(DECL_ARGS)
 
        if (MDOC_BLOCK == n->type) {
                p->flags &= ~(TERMP_KEEP | TERMP_PREKEEP);
-       } else if (MDOC_HEAD == n->type && n->next->child) {
+       } else if (MDOC_HEAD == n->type &&
+           NULL != n->next && NULL != n->next->child) {
                term_flushln(p);
                p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND | TERMP_HANG);
                p->trailspace = 0;
@@ -1070,9 +1082,10 @@ termp_fl_pre(DECL_ARGS)
        term_fontpush(p, TERMFONT_BOLD);
        term_word(p, "\\-");
 
-       if (n->child)
-               p->flags |= TERMP_NOSPACE;
-       else if (n->next && n->next->line == n->line)
+       if ( ! (n->nchild == 0 &&
+           (n->next == NULL ||
+            n->next->type == MDOC_TEXT ||
+            n->next->flags & MDOC_LINE)))
                p->flags |= TERMP_NOSPACE;
 
        return(1);