Add configurability for section heading formats, in much the same
[sgt/halibut] / bk_text.c
index 1583cbd..b682b42 100644 (file)
--- a/bk_text.c
+++ b/bk_text.c
@@ -12,6 +12,7 @@ typedef struct {
     alignment align;
     int just_numbers;
     wchar_t underline;
+    wchar_t *number_suffix;
 } alignstruct;
 
 typedef struct {
@@ -64,11 +65,13 @@ static textconfig text_configure(paragraph *source) {
     ret.atitle.underline = L'=';
     ret.achapter.align = LEFT;
     ret.achapter.just_numbers = FALSE;
+    ret.achapter.number_suffix = ustrdup(L": ");
     ret.achapter.underline = L'-';
     ret.nasect = 1;
     ret.asect = mknewa(alignstruct, ret.nasect);
     ret.asect[0].align = LEFTPLUS;
     ret.asect[0].just_numbers = TRUE;
+    ret.asect[0].number_suffix = ustrdup(L" ");
     ret.asect[0].underline = L'\0';
     ret.include_version_id = TRUE;
     ret.indent_preambles = FALSE;
@@ -91,7 +94,9 @@ static textconfig text_configure(paragraph *source) {
            } else if (!ustricmp(source->keyword, L"text-chapter-underline")) {
                ret.achapter.underline = *uadv(source->keyword);
            } else if (!ustricmp(source->keyword, L"text-chapter-numeric")) {
-               ret.achapter.underline = utob(uadv(source->keyword));
+               ret.achapter.just_numbers = utob(uadv(source->keyword));
+           } else if (!ustricmp(source->keyword, L"text-chapter-suffix")) {
+               ret.achapter.number_suffix = uadv(source->keyword);
            } else if (!ustricmp(source->keyword, L"text-section-align")) {
                wchar_t *p = uadv(source->keyword);
                int n = 0;
@@ -137,6 +142,21 @@ static textconfig text_configure(paragraph *source) {
                    ret.nasect = n+1;
                }
                ret.asect[n].just_numbers = utob(p);
+           } else if (!ustricmp(source->keyword, L"text-section-suffix")) {
+               wchar_t *p = uadv(source->keyword);
+               int n = 0;
+               if (uisdigit(*p)) {
+                   n = utoi(p);
+                   p = uadv(p);
+               }
+               if (n >= ret.nasect) {
+                   int i;
+                   ret.asect = resize(ret.asect, n+1);
+                   for (i = ret.nasect; i <= n; i++)
+                       ret.asect[i] = ret.asect[ret.nasect-1];
+                   ret.nasect = n+1;
+               }
+               ret.asect[n].number_suffix = p;
            } else if (!ustricmp(source->keyword, L"text-title-align")) {
                ret.atitle.align = utoalign(uadv(source->keyword));
            } else if (!ustricmp(source->keyword, L"text-title-underline")) {
@@ -269,7 +289,8 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
            body = p->words;
        }
        text_para(fp, prefix, prefixextra, body,
-                 conf.indent + indentb, indenta, conf.width);
+                 conf.indent + indentb, indenta,
+                 conf.width - indentb - indenta);
        if (wp) {
            wp->next = NULL;
            free_word_list(body);
@@ -463,11 +484,19 @@ static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text,
     wrappedline *wrapping, *p;
 
     if (align.just_numbers && nprefix) {
+       char *c;
        text_rdaddwc(&t, nprefix, NULL);
-       rdaddc(&t, ' ');               /* FIXME: as below */
+       if (text_convert(align.number_suffix, &c)) {
+           rdaddsc(&t, c);
+           sfree(c);
+       }
     } else if (!align.just_numbers && tprefix) {
+       char *c;
        text_rdaddwc(&t, tprefix, NULL);
-       rdaddsc(&t, ": ");             /* FIXME: configurability */
+       if (text_convert(align.number_suffix, &c)) {
+           rdaddsc(&t, c);
+           sfree(c);
+       }
     }
     margin = length = (t.text ? strlen(t.text) : 0);
 
@@ -535,15 +564,16 @@ static void text_para(FILE *fp, word *prefix, char *prefixextra, word *text,
        if (prefixextra)
            rdaddsc(&pfx, prefixextra);
        fprintf(fp, "%*s%s", indent, "", pfx.text);
+       /* If the prefix is too long, shorten the first line to fit. */
        e = extraindent - strlen(pfx.text);
        if (e < 0) {
-           e = 0;
-           firstlinewidth -= e;
+           firstlinewidth += e;       /* this decreases it, since e < 0 */
            if (firstlinewidth < 0) {
                e = indent + extraindent;
                firstlinewidth = width;
                fprintf(fp, "\n");
-           }
+           } else
+               e = 0;
        }
        sfree(pfx.text);
     } else