2 * info backend for Halibut
4 * Possible future work:
6 * - configurable choice of how to allocate node names?
7 * + possibly a template-like approach, choosing node names to
8 * be the full section title or perhaps the internal keyword?
9 * + neither of those seems quite right. Perhaps instead a
10 * Windows Help-like mechanism, where a magic config
11 * directive allows user choice of name for every node.
12 * + Only trouble with that is, now what happens to the section
13 * numbers? Do they become completely vestigial and just sit
14 * in the title text of each node? Or do we keep them in the
15 * menus somehow? I think people might occasionally want to
16 * go to a section by number, if only because all the _other_
17 * formats of the same document will reference the numbers
18 * all the time. So our menu lines could look like one of
20 * * Nodename: Section 1.2. Title of section.
21 * * Section 1.2: Nodename. Title of section.
23 * - might be helpful to diagnose duplicate node names!
25 * - more flexibility in heading underlines, like text backend.
26 * + Given info.el's fontifier, we'd want the following defaults:
27 * \cfg{info-title-underline}{*}
28 * \cfg{info-chapter-underline}{=}
29 * \cfg{info-section-underline}{0}{-}
30 * \cfg{info-section-underline}{1}{.}
32 * - Indices generated by makeinfo use a menu rather than a bunch of
33 * cross-references, which reduces visual clutter rather. For
34 * singly-referenced items, it looks like:
35 * * toner cartridge, replacing: Toner.
36 * It does a horrid job on multiply-referenced entries, though,
37 * perhaps because the name before the colon is meant to be unique.
39 * - The string "*note" is matched case-insensitively, so we could
40 * make things slightly less ugly by using the lower-case version
41 * when the user asks for \k. Unfortunately, standalone Info seems
42 * to match node names case-sensitively, so we can't downcase that.
54 int listindentbefore
, listindentafter
;
55 int indent_code
, width
, index_width
;
56 wchar_t *bullet
, *listsuffix
;
57 wchar_t *startemph
, *endemph
;
58 wchar_t *lquote
, *rquote
;
59 wchar_t *sectsuffix
, *underline
;
70 #define EMPTY_INFO_DATA { { 0, 0, NULL }, 0, CHARSET_INIT_STATE, FALSE }
71 static const info_data empty_info_data
= EMPTY_INFO_DATA
;
73 typedef struct node_tag node
;
76 node
*up
, *prev
, *next
, *lastchild
;
77 int pos
, started_menu
, filenum
;
89 static int info_rdadd(info_data
*, wchar_t);
90 static int info_rdadds(info_data
*, wchar_t const *);
91 static int info_rdaddc(info_data
*, char);
92 static int info_rdaddsc(info_data
*, char const *);
94 static void info_heading(info_data
*, word
*, word
*, int, infoconfig
*);
95 static void info_rule(info_data
*, int, int, infoconfig
*);
96 static void info_para(info_data
*, word
*, wchar_t *, word
*, keywordlist
*,
97 int, int, int, infoconfig
*);
98 static void info_codepara(info_data
*, word
*, int, int);
99 static void info_versionid(info_data
*, word
*, infoconfig
*);
100 static void info_menu_item(info_data
*, node
*, paragraph
*, infoconfig
*);
101 static word
*info_transform_wordlist(word
*, keywordlist
*);
102 static int info_check_index(word
*, node
*, indexdata
*);
104 static int info_rdaddwc(info_data
*, word
*, word
*, int, infoconfig
*);
106 static node
*info_node_new(char *name
, int charset
);
107 static char *info_node_name_for_para(paragraph
*p
, infoconfig
*);
108 static char *info_node_name_for_text(wchar_t *text
, infoconfig
*);
110 static infoconfig
info_configure(paragraph
*source
) {
117 ret
.filename
= dupstr("output.info");
118 ret
.maxfilesize
= 64 << 10;
119 ret
.charset
= CS_ASCII
;
121 ret
.listindentbefore
= 1;
122 ret
.listindentafter
= 3;
124 ret
.index_width
= 40;
125 ret
.listsuffix
= L
".";
126 ret
.bullet
= L
"\x2022\0-\0\0";
127 ret
.rule
= L
"\x2500\0-\0\0";
128 ret
.startemph
= L
"_\0_\0\0";
129 ret
.endemph
= uadv(ret
.startemph
);
130 ret
.lquote
= L
"\x2018\0\x2019\0`\0'\0\0";
131 ret
.rquote
= uadv(ret
.lquote
);
132 ret
.sectsuffix
= L
": ";
133 ret
.underline
= L
"\x203E\0-\0\0";
134 ret
.index_text
= L
"Index";
137 * Two-pass configuration so that we can pick up global config
138 * (e.g. `quotes') before having it overridden by specific
139 * config (`info-quotes'), irrespective of the order in which
142 for (p
= source
; p
; p
= p
->next
) {
143 if (p
->type
== para_Config
) {
144 if (!ustricmp(p
->keyword
, L
"quotes")) {
145 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
146 ret
.lquote
= uadv(p
->keyword
);
147 ret
.rquote
= uadv(ret
.lquote
);
149 } else if (!ustricmp(p
->keyword
, L
"index")) {
150 ret
.index_text
= uadv(p
->keyword
);
155 for (p
= source
; p
; p
= p
->next
) {
156 if (p
->type
== para_Config
) {
157 if (!ustricmp(p
->keyword
, L
"info-filename")) {
159 ret
.filename
= dupstr(adv(p
->origkeyword
));
160 } else if (!ustricmp(p
->keyword
, L
"info-charset")) {
161 ret
.charset
= charset_from_ustr(&p
->fpos
, uadv(p
->keyword
));
162 } else if (!ustricmp(p
->keyword
, L
"info-max-file-size")) {
163 ret
.maxfilesize
= utoi(uadv(p
->keyword
));
164 } else if (!ustricmp(p
->keyword
, L
"info-width")) {
165 ret
.width
= utoi(uadv(p
->keyword
));
166 } else if (!ustricmp(p
->keyword
, L
"info-indent-code")) {
167 ret
.indent_code
= utoi(uadv(p
->keyword
));
168 } else if (!ustricmp(p
->keyword
, L
"info-index-width")) {
169 ret
.index_width
= utoi(uadv(p
->keyword
));
170 } else if (!ustricmp(p
->keyword
, L
"info-list-indent")) {
171 ret
.listindentbefore
= utoi(uadv(p
->keyword
));
172 } else if (!ustricmp(p
->keyword
, L
"info-listitem-indent")) {
173 ret
.listindentafter
= utoi(uadv(p
->keyword
));
174 } else if (!ustricmp(p
->keyword
, L
"info-section-suffix")) {
175 ret
.sectsuffix
= uadv(p
->keyword
);
176 } else if (!ustricmp(p
->keyword
, L
"info-underline")) {
177 ret
.underline
= uadv(p
->keyword
);
178 } else if (!ustricmp(p
->keyword
, L
"info-bullet")) {
179 ret
.bullet
= uadv(p
->keyword
);
180 } else if (!ustricmp(p
->keyword
, L
"info-rule")) {
181 ret
.rule
= uadv(p
->keyword
);
182 } else if (!ustricmp(p
->keyword
, L
"info-list-suffix")) {
183 ret
.listsuffix
= uadv(p
->keyword
);
184 } else if (!ustricmp(p
->keyword
, L
"info-emphasis")) {
185 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
186 ret
.startemph
= uadv(p
->keyword
);
187 ret
.endemph
= uadv(ret
.startemph
);
189 } else if (!ustricmp(p
->keyword
, L
"info-quotes")) {
190 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
191 ret
.lquote
= uadv(p
->keyword
);
192 ret
.rquote
= uadv(ret
.lquote
);
199 * Now process fallbacks on quote characters, underlines, the
200 * rule character, the emphasis characters, and bullets.
202 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
203 (!cvt_ok(ret
.charset
, ret
.lquote
) ||
204 !cvt_ok(ret
.charset
, ret
.rquote
))) {
205 ret
.lquote
= uadv(ret
.rquote
);
206 ret
.rquote
= uadv(ret
.lquote
);
209 while (*uadv(ret
.endemph
) && *uadv(uadv(ret
.endemph
)) &&
210 (!cvt_ok(ret
.charset
, ret
.startemph
) ||
211 !cvt_ok(ret
.charset
, ret
.endemph
))) {
212 ret
.startemph
= uadv(ret
.endemph
);
213 ret
.endemph
= uadv(ret
.startemph
);
216 while (*ret
.underline
&& *uadv(ret
.underline
) &&
217 !cvt_ok(ret
.charset
, ret
.underline
))
218 ret
.underline
= uadv(ret
.underline
);
220 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
221 !cvt_ok(ret
.charset
, ret
.bullet
))
222 ret
.bullet
= uadv(ret
.bullet
);
224 while (*ret
.rule
&& *uadv(ret
.rule
) &&
225 !cvt_ok(ret
.charset
, ret
.rule
))
226 ret
.rule
= uadv(ret
.rule
);
231 paragraph
*info_config_filename(char *filename
)
233 return cmdline_cfg_simple("info-filename", filename
, NULL
);
236 void info_backend(paragraph
*sourceform
, keywordlist
*keywords
,
237 indexdata
*idx
, void *unused
) {
240 word
*prefix
, *body
, *wp
;
242 wchar_t *prefixextra
;
243 int nesting
, nestindent
;
244 int indentb
, indenta
;
247 info_data intro_text
= EMPTY_INFO_DATA
;
248 node
*topnode
, *currnode
;
254 conf
= info_configure(sourceform
);
257 * Go through and create a node for each section.
259 topnode
= info_node_new("Top", conf
.charset
);
261 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
267 case para_UnnumberedChapter
:
271 node
*newnode
, *upnode
;
274 nodename
= info_node_name_for_para(p
, &conf
);
275 newnode
= info_node_new(nodename
, conf
.charset
);
278 p
->private_data
= newnode
;
281 upnode
= (node
*)p
->parent
->private_data
;
285 newnode
->up
= upnode
;
287 currnode
->next
= newnode
;
288 newnode
->prev
= currnode
;
290 currnode
->listnext
= newnode
;
295 p
->private_data
= NULL
;
300 * Set up the display form of each index entry.
306 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
307 info_idx
*ii
= snew(info_idx
);
308 info_data id
= EMPTY_INFO_DATA
;
310 id
.charset
= conf
.charset
;
312 ii
->nnodes
= ii
->nodesize
= 0;
315 ii
->length
= info_rdaddwc(&id
, entry
->text
, NULL
, FALSE
, &conf
);
317 ii
->text
= id
.output
.text
;
319 entry
->backend_data
= ii
;
324 * An Info file begins with a piece of introductory text which
325 * is apparently never shown anywhere. This seems to me to be a
326 * good place to put the copyright notice and the version IDs.
327 * Also, Info directory entries are expected to go here.
329 intro_text
.charset
= conf
.charset
;
331 info_rdaddsc(&intro_text
,
332 "This Info file generated by Halibut, ");
333 info_rdaddsc(&intro_text
, version
);
334 info_rdaddsc(&intro_text
, "\n\n");
336 for (p
= sourceform
; p
; p
= p
->next
)
337 if (p
->type
== para_Config
&&
338 !ustricmp(p
->keyword
, L
"info-dir-entry")) {
339 wchar_t *section
, *shortname
, *longname
, *kw
;
342 section
= uadv(p
->keyword
);
343 shortname
= *section ?
uadv(section
) : L
"";
344 longname
= *shortname ?
uadv(shortname
) : L
"";
345 kw
= *longname ?
uadv(longname
) : L
"";
348 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 3);
352 info_rdaddsc(&intro_text
, "INFO-DIR-SECTION ");
353 info_rdadds(&intro_text
, section
);
354 info_rdaddsc(&intro_text
, "\nSTART-INFO-DIR-ENTRY\n* ");
355 info_rdadds(&intro_text
, shortname
);
356 info_rdaddsc(&intro_text
, ": (");
357 s
= dupstr(conf
.filename
);
358 if (strlen(s
) > 5 && !strcmp(s
+strlen(s
)-5, ".info"))
359 s
[strlen(s
)-5] = '\0';
360 info_rdaddsc(&intro_text
, s
);
362 info_rdaddsc(&intro_text
, ")");
364 keyword
*kwl
= kw_lookup(keywords
, kw
);
365 if (kwl
&& kwl
->para
->private_data
) {
366 node
*n
= (node
*)kwl
->para
->private_data
;
367 info_rdaddsc(&intro_text
, n
->name
);
370 info_rdaddsc(&intro_text
, ". ");
371 info_rdadds(&intro_text
, longname
);
372 info_rdaddsc(&intro_text
, "\nEND-INFO-DIR-ENTRY\n\n");
375 for (p
= sourceform
; p
; p
= p
->next
)
376 if (p
->type
== para_Copyright
)
377 info_para(&intro_text
, NULL
, NULL
, p
->words
, keywords
,
378 0, 0, conf
.width
, &conf
);
380 for (p
= sourceform
; p
; p
= p
->next
)
381 if (p
->type
== para_VersionID
)
382 info_versionid(&intro_text
, p
->words
, &conf
);
384 if (intro_text
.output
.text
[intro_text
.output
.pos
-1] != '\n')
385 info_rdaddc(&intro_text
, '\n');
388 for (p
= sourceform
; p
; p
= p
->next
)
389 if (p
->type
== para_Title
)
390 info_heading(&topnode
->text
, NULL
, p
->words
, conf
.width
, &conf
);
392 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
397 /* Do the main document */
398 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
405 assert(nesting
>= 0);
409 nesting
+= nestindent
;
412 nesting
-= nestindent
;
413 assert(nesting
>= 0);
417 * Things we ignore because we've already processed them or
418 * aren't going to touch them in this pass.
422 case para_Biblio
: /* only touch BiblioCited */
433 case para_UnnumberedChapter
:
436 currnode
= p
->private_data
;
438 assert(currnode
->up
);
440 if (!currnode
->up
->started_menu
) {
441 info_rdaddsc(&currnode
->up
->text
, "* Menu:\n\n");
442 currnode
->up
->started_menu
= TRUE
;
444 info_menu_item(&currnode
->up
->text
, currnode
, p
, &conf
);
446 has_index
|= info_check_index(p
->words
, currnode
, idx
);
447 info_heading(&currnode
->text
, p
->kwtext
, p
->words
, conf
.width
, &conf
);
452 info_rule(&currnode
->text
, nesting
, conf
.width
- nesting
, &conf
);
457 case para_DescribedThing
:
458 case para_Description
:
459 case para_BiblioCited
:
461 case para_NumberedList
:
462 has_index
|= info_check_index(p
->words
, currnode
, idx
);
463 if (p
->type
== para_Bullet
) {
466 bullet
.type
= word_Normal
;
467 bullet
.text
= conf
.bullet
;
470 indentb
= conf
.listindentbefore
;
471 indenta
= conf
.listindentafter
;
472 } else if (p
->type
== para_NumberedList
) {
474 prefixextra
= conf
.listsuffix
;
475 indentb
= conf
.listindentbefore
;
476 indenta
= conf
.listindentafter
;
477 } else if (p
->type
== para_Description
) {
480 indentb
= conf
.listindentbefore
;
481 indenta
= conf
.listindentafter
;
485 indentb
= indenta
= 0;
487 if (p
->type
== para_BiblioCited
) {
488 body
= dup_word_list(p
->kwtext
);
489 for (wp
= body
; wp
->next
; wp
= wp
->next
);
490 wp
->next
= &spaceword
;
491 spaceword
.next
= p
->words
;
492 spaceword
.alt
= NULL
;
493 spaceword
.type
= word_WhiteSpace
;
494 spaceword
.text
= NULL
;
499 info_para(&currnode
->text
, prefix
, prefixextra
, body
, keywords
,
500 nesting
+ indentb
, indenta
,
501 conf
.width
- nesting
- indentb
- indenta
, &conf
);
504 free_word_list(body
);
509 info_codepara(&currnode
->text
, p
->words
,
510 nesting
+ conf
.indent_code
,
511 conf
.width
- nesting
- 2 * conf
.indent_code
);
516 * Create an index node if required.
524 nodename
= info_node_name_for_text(conf
.index_text
, &conf
);
525 newnode
= info_node_new(nodename
, conf
.charset
);
528 newnode
->up
= topnode
;
530 currnode
->next
= newnode
;
531 newnode
->prev
= currnode
;
532 currnode
->listnext
= newnode
;
534 k
= info_rdadds(&newnode
->text
, conf
.index_text
);
535 info_rdaddsc(&newnode
->text
, "\n");
537 info_rdadds(&newnode
->text
, conf
.underline
);
538 k
-= ustrwid(conf
.underline
, conf
.charset
);
540 info_rdaddsc(&newnode
->text
, "\n\n");
542 info_menu_item(&topnode
->text
, newnode
, NULL
, &conf
);
544 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
545 info_idx
*ii
= (info_idx
*)entry
->backend_data
;
547 for (j
= 0; j
< ii
->nnodes
; j
++) {
549 * When we have multiple references for a single
550 * index term, we only display the actual term on
551 * the first line, to make it clear that the terms
552 * really are the same.
555 info_rdaddsc(&newnode
->text
, ii
->text
);
556 for (k
= (j ?
0 : ii
->length
); k
< conf
.index_width
-2; k
++)
557 info_rdaddc(&newnode
->text
, ' ');
558 info_rdaddsc(&newnode
->text
, " *Note ");
559 info_rdaddsc(&newnode
->text
, ii
->nodes
[j
]->name
);
560 info_rdaddsc(&newnode
->text
, "::\n");
566 * Finalise the text of each node, by adding the ^_ delimiter
567 * and the node line at the top.
569 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
570 char *origtext
= currnode
->text
.output
.text
;
571 currnode
->text
= empty_info_data
;
572 currnode
->text
.charset
= conf
.charset
;
573 info_rdaddsc(&currnode
->text
, "\037\nFile: ");
574 info_rdaddsc(&currnode
->text
, conf
.filename
);
575 info_rdaddsc(&currnode
->text
, ", Node: ");
576 info_rdaddsc(&currnode
->text
, currnode
->name
);
577 if (currnode
->prev
) {
578 info_rdaddsc(&currnode
->text
, ", Prev: ");
579 info_rdaddsc(&currnode
->text
, currnode
->prev
->name
);
581 info_rdaddsc(&currnode
->text
, ", Up: ");
582 info_rdaddsc(&currnode
->text
, (currnode
->up ?
583 currnode
->up
->name
: "(dir)"));
584 if (currnode
->next
) {
585 info_rdaddsc(&currnode
->text
, ", Next: ");
586 info_rdaddsc(&currnode
->text
, currnode
->next
->name
);
588 info_rdaddsc(&currnode
->text
, "\n\n");
589 info_rdaddsc(&currnode
->text
, origtext
);
591 * Just make _absolutely_ sure we end with a newline.
593 if (currnode
->text
.output
.text
[currnode
->text
.output
.pos
-1] != '\n')
594 info_rdaddc(&currnode
->text
, '\n');
600 * Compute the offsets for the tag table.
602 filepos
= intro_text
.output
.pos
;
603 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
604 currnode
->pos
= filepos
;
605 filepos
+= currnode
->text
.output
.pos
;
609 * Split into sub-files.
611 if (conf
.maxfilesize
> 0) {
612 int currfilesize
= intro_text
.output
.pos
, currfilenum
= 1;
613 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
614 if (currfilesize
> intro_text
.output
.pos
&&
615 currfilesize
+ currnode
->text
.output
.pos
> conf
.maxfilesize
) {
617 currfilesize
= intro_text
.output
.pos
;
619 currnode
->filenum
= currfilenum
;
620 currfilesize
+= currnode
->text
.output
.pos
;
625 * Write the primary output file.
627 fp
= fopen(conf
.filename
, "w");
629 error(err_cantopenw
, conf
.filename
);
632 fputs(intro_text
.output
.text
, fp
);
633 if (conf
.maxfilesize
== 0) {
634 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
)
635 fputs(currnode
->text
.output
.text
, fp
);
638 fprintf(fp
, "\037\nIndirect:\n");
639 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
)
640 if (filenum
!= currnode
->filenum
) {
641 filenum
= currnode
->filenum
;
642 fprintf(fp
, "%s-%d: %d\n", conf
.filename
, filenum
,
646 fprintf(fp
, "\037\nTag Table:\n");
647 if (conf
.maxfilesize
> 0)
648 fprintf(fp
, "(Indirect)\n");
649 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
)
650 fprintf(fp
, "Node: %s\177%d\n", currnode
->name
, currnode
->pos
);
651 fprintf(fp
, "\037\nEnd Tag Table\n");
655 * Write the subfiles.
657 if (conf
.maxfilesize
> 0) {
661 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
662 if (filenum
!= currnode
->filenum
) {
665 filenum
= currnode
->filenum
;
669 fname
= snewn(strlen(conf
.filename
) + 40, char);
670 sprintf(fname
, "%s-%d", conf
.filename
, filenum
);
671 fp
= fopen(fname
, "w");
673 error(err_cantopenw
, fname
);
677 fputs(intro_text
.output
.text
, fp
);
679 fputs(currnode
->text
.output
.text
, fp
);
687 static int info_check_index(word
*w
, node
*n
, indexdata
*idx
)
691 for (; w
; w
= w
->next
) {
692 if (w
->type
== word_IndexRef
) {
696 tag
= index_findtag(idx
, w
->text
);
700 for (i
= 0; i
< tag
->nrefs
; i
++) {
701 indexentry
*entry
= tag
->refs
[i
];
702 info_idx
*ii
= (info_idx
*)entry
->backend_data
;
704 if (ii
->nnodes
> 0 && ii
->nodes
[ii
->nnodes
-1] == n
) {
706 * If the same index term is indexed twice
707 * within the same section, we only want to
708 * mention it once in the index. So do nothing
714 if (ii
->nnodes
>= ii
->nodesize
) {
716 ii
->nodes
= sresize(ii
->nodes
, ii
->nodesize
, node
*);
719 ii
->nodes
[ii
->nnodes
++] = n
;
729 static word
*info_transform_wordlist(word
*words
, keywordlist
*keywords
)
731 word
*ret
= dup_word_list(words
);
735 for (w
= ret
; w
; w
= w
->next
) {
736 w
->private_data
= NULL
;
737 if (w
->type
== word_UpperXref
|| w
->type
== word_LowerXref
) {
738 kwl
= kw_lookup(keywords
, w
->text
);
740 if (kwl
->para
->type
== para_NumberedList
||
741 kwl
->para
->type
== para_BiblioCited
) {
743 * In Info, we do nothing special for xrefs to
744 * numbered list items or bibliography entries.
749 * An xref to a different section has its text
750 * completely replaced.
756 if (w2
->type
== word_XrefEnd
) {
766 * Now w is the UpperXref / LowerXref we
767 * started with, and w4 is the next word after
768 * the corresponding XrefEnd (if any). The
769 * simplest thing is just to stick a pointer to
770 * the target node structure in the private
771 * data field of the xref word, and let
772 * info_rdaddwc and friends read the node name
776 w
->private_data
= kwl
->para
->private_data
;
777 assert(w
->private_data
);
786 static int info_rdaddwc(info_data
*id
, word
*words
, word
*end
, int xrefs
,
790 for (; words
&& words
!= end
; words
= words
->next
) switch (words
->type
) {
801 case word_WhiteSpace
:
804 case word_WkCodeSpace
:
808 case word_WkCodeQuote
:
809 assert(words
->type
!= word_CodeQuote
&&
810 words
->type
!= word_WkCodeQuote
);
811 if (towordstyle(words
->type
) == word_Emph
&&
812 (attraux(words
->aux
) == attr_First
||
813 attraux(words
->aux
) == attr_Only
))
814 ret
+= info_rdadds(id
, cfg
->startemph
);
815 else if (towordstyle(words
->type
) == word_Code
&&
816 (attraux(words
->aux
) == attr_First
||
817 attraux(words
->aux
) == attr_Only
))
818 ret
+= info_rdadds(id
, cfg
->lquote
);
819 if (removeattr(words
->type
) == word_Normal
) {
820 if (cvt_ok(id
->charset
, words
->text
) || !words
->alt
)
821 ret
+= info_rdadds(id
, words
->text
);
823 ret
+= info_rdaddwc(id
, words
->alt
, NULL
, FALSE
, cfg
);
824 } else if (removeattr(words
->type
) == word_WhiteSpace
) {
825 ret
+= info_rdadd(id
, L
' ');
826 } else if (removeattr(words
->type
) == word_Quote
) {
827 ret
+= info_rdadds(id
, quoteaux(words
->aux
) == quote_Open ?
828 cfg
->lquote
: cfg
->rquote
);
830 if (towordstyle(words
->type
) == word_Emph
&&
831 (attraux(words
->aux
) == attr_Last
||
832 attraux(words
->aux
) == attr_Only
))
833 ret
+= info_rdadds(id
, cfg
->endemph
);
834 else if (towordstyle(words
->type
) == word_Code
&&
835 (attraux(words
->aux
) == attr_Last
||
836 attraux(words
->aux
) == attr_Only
))
837 ret
+= info_rdadds(id
, cfg
->rquote
);
842 if (xrefs
&& words
->private_data
) {
844 * This bit is structural and so must be done in char
845 * rather than wchar_t.
847 ret
+= info_rdaddsc(id
, "*Note ");
848 ret
+= info_rdaddsc(id
, ((node
*)words
->private_data
)->name
);
849 ret
+= info_rdaddsc(id
, "::");
857 static int info_width_internal(word
*words
, int xrefs
, infoconfig
*cfg
);
859 static int info_width_internal_list(word
*words
, int xrefs
, infoconfig
*cfg
) {
862 w
+= info_width_internal(words
, xrefs
, cfg
);
868 static int info_width_internal(word
*words
, int xrefs
, infoconfig
*cfg
) {
872 switch (words
->type
) {
881 if (xrefs
&& words
->private_data
) {
882 /* "*Note " plus "::" comes to 8 characters */
883 return 8 + strwid(((node
*)words
->private_data
)->name
,
889 assert(words
->type
< word_internal_endattrs
);
892 attr
= towordstyle(words
->type
);
894 if (attr
== word_Emph
|| attr
== word_Code
) {
895 if (attraux(words
->aux
) == attr_Only
||
896 attraux(words
->aux
) == attr_First
)
897 wid
+= ustrwid(attr
== word_Emph ? cfg
->startemph
: cfg
->lquote
,
900 if (attr
== word_Emph
|| attr
== word_Code
) {
901 if (attraux(words
->aux
) == attr_Only
||
902 attraux(words
->aux
) == attr_Last
)
903 wid
+= ustrwid(attr
== word_Emph ? cfg
->startemph
: cfg
->lquote
,
907 switch (words
->type
) {
912 if (cvt_ok(cfg
->charset
, words
->text
) || !words
->alt
)
913 wid
+= ustrwid(words
->text
, cfg
->charset
);
915 wid
+= info_width_internal_list(words
->alt
, xrefs
, cfg
);
918 case word_WhiteSpace
:
921 case word_WkCodeSpace
:
925 case word_WkCodeQuote
:
926 assert(words
->type
!= word_CodeQuote
&&
927 words
->type
!= word_WkCodeQuote
);
928 if (removeattr(words
->type
) == word_Quote
) {
929 if (quoteaux(words
->aux
) == quote_Open
)
930 wid
+= ustrwid(cfg
->lquote
, cfg
->charset
);
932 wid
+= ustrwid(cfg
->rquote
, cfg
->charset
);
939 static int info_width_noxrefs(void *ctx
, word
*words
)
941 return info_width_internal(words
, FALSE
, (infoconfig
*)ctx
);
943 static int info_width_xrefs(void *ctx
, word
*words
)
945 return info_width_internal(words
, TRUE
, (infoconfig
*)ctx
);
948 static void info_heading(info_data
*text
, word
*tprefix
,
949 word
*words
, int width
, infoconfig
*cfg
) {
951 int firstlinewidth
, wrapwidth
;
952 wrappedline
*wrapping
, *p
;
956 length
+= info_rdaddwc(text
, tprefix
, NULL
, FALSE
, cfg
);
957 length
+= info_rdadds(text
, cfg
->sectsuffix
);
961 firstlinewidth
= width
- length
;
963 wrapping
= wrap_para(words
, firstlinewidth
, wrapwidth
,
964 info_width_noxrefs
, cfg
, 0);
965 for (p
= wrapping
; p
; p
= p
->next
) {
966 length
+= info_rdaddwc(text
, p
->begin
, p
->end
, FALSE
, cfg
);
967 info_rdadd(text
, L
'\n');
969 info_rdadds(text
, cfg
->underline
);
970 length
-= ustrwid(cfg
->underline
, cfg
->charset
);
972 info_rdadd(text
, L
'\n');
976 info_rdadd(text
, L
'\n');
979 static void info_rule(info_data
*text
, int indent
, int width
, infoconfig
*cfg
)
981 while (indent
--) info_rdadd(text
, L
' ');
983 info_rdadds(text
, cfg
->rule
);
984 width
-= ustrwid(cfg
->rule
, cfg
->charset
);
986 info_rdadd(text
, L
'\n');
987 info_rdadd(text
, L
'\n');
990 static void info_para(info_data
*text
, word
*prefix
, wchar_t *prefixextra
,
991 word
*input
, keywordlist
*keywords
, int indent
,
992 int extraindent
, int width
, infoconfig
*cfg
) {
993 wrappedline
*wrapping
, *p
;
997 int firstlinewidth
= width
;
999 words
= info_transform_wordlist(input
, keywords
);
1002 for (i
= 0; i
< indent
; i
++)
1003 info_rdadd(text
, L
' ');
1004 e
= info_rdaddwc(text
, prefix
, NULL
, FALSE
, cfg
);
1006 e
+= info_rdadds(text
, prefixextra
);
1007 /* If the prefix is too long, shorten the first line to fit. */
1008 e
= extraindent
- e
;
1010 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
1011 if (firstlinewidth
< 0) {
1012 e
= indent
+ extraindent
;
1013 firstlinewidth
= width
;
1014 info_rdadd(text
, L
'\n');
1019 e
= indent
+ extraindent
;
1021 wrapping
= wrap_para(words
, firstlinewidth
, width
, info_width_xrefs
,
1023 for (p
= wrapping
; p
; p
= p
->next
) {
1024 for (i
= 0; i
< e
; i
++)
1025 info_rdadd(text
, L
' ');
1026 info_rdaddwc(text
, p
->begin
, p
->end
, TRUE
, cfg
);
1027 info_rdadd(text
, L
'\n');
1028 e
= indent
+ extraindent
;
1030 wrap_free(wrapping
);
1031 info_rdadd(text
, L
'\n');
1033 free_word_list(words
);
1036 static void info_codepara(info_data
*text
, word
*words
,
1037 int indent
, int width
) {
1040 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1041 for (i
= 0; i
< indent
; i
++)
1042 info_rdadd(text
, L
' ');
1043 if (info_rdadds(text
, words
->text
) > width
) {
1046 info_rdadd(text
, L
'\n');
1049 info_rdadd(text
, L
'\n');
1052 static void info_versionid(info_data
*text
, word
*words
, infoconfig
*cfg
) {
1053 info_rdadd(text
, L
'[');
1054 info_rdaddwc(text
, words
, NULL
, FALSE
, cfg
);
1055 info_rdadds(text
, L
"]\n");
1058 static node
*info_node_new(char *name
, int charset
)
1063 n
->text
= empty_info_data
;
1064 n
->text
.charset
= charset
;
1065 n
->up
= n
->next
= n
->prev
= n
->lastchild
= n
->listnext
= NULL
;
1066 n
->name
= dupstr(name
);
1067 n
->started_menu
= FALSE
;
1072 static char *info_node_name_core(info_data
*id
, filepos
*fpos
)
1077 * We cannot have commas, colons or parentheses in a node name.
1078 * Remove any that we find, with a warning.
1080 p
= q
= id
->output
.text
;
1082 if (*p
== ':' || *p
== ',' || *p
== '(' || *p
== ')') {
1083 error(err_infonodechar
, fpos
, *p
);
1091 return id
->output
.text
;
1094 static char *info_node_name_for_para(paragraph
*par
, infoconfig
*cfg
)
1096 info_data id
= EMPTY_INFO_DATA
;
1098 id
.charset
= cfg
->charset
;
1099 info_rdaddwc(&id
, par
->kwtext ? par
->kwtext
: par
->words
,
1101 info_rdaddsc(&id
, NULL
);
1103 return info_node_name_core(&id
, &par
->fpos
);
1106 static char *info_node_name_for_text(wchar_t *text
, infoconfig
*cfg
)
1108 info_data id
= EMPTY_INFO_DATA
;
1110 id
.charset
= cfg
->charset
;
1111 info_rdadds(&id
, text
);
1112 info_rdaddsc(&id
, NULL
);
1114 return info_node_name_core(&id
, NULL
);
1117 static void info_menu_item(info_data
*text
, node
*n
, paragraph
*p
,
1121 * FIXME: Depending on how we're doing node names in this info
1122 * file, we might want to do
1124 * * Node name:: Chapter title
1128 * * Chapter number: Node name.
1130 * This function mostly works in char rather than wchar_t,
1131 * because a menu item is a structural component.
1133 info_rdaddsc(text
, "* ");
1134 info_rdaddsc(text
, n
->name
);
1135 info_rdaddsc(text
, "::");
1137 info_rdaddc(text
, ' ');
1138 info_rdaddwc(text
, p
->words
, NULL
, FALSE
, cfg
);
1140 info_rdaddc(text
, '\n');
1144 * These functions implement my wrapper on the rdadd* calls which
1145 * allows me to switch arbitrarily between literal octet-string
1146 * text and charset-translated Unicode. (Because no matter what
1147 * character set I write the actual text in, I expect info readers
1148 * to treat node names and file names literally and to expect
1149 * keywords like `*Note' in their canonical form, so I have to take
1150 * steps to ensure that those structural elements of the file
1151 * aren't messed with.)
1153 static int info_rdadds(info_data
*d
, wchar_t const *wcs
)
1156 d
->state
= charset_init_state
;
1162 int len
, width
, ret
;
1164 width
= ustrwid(wcs
, d
->charset
);
1170 ret
= charset_from_unicode(&wcs
, &len
, buf
, lenof(buf
),
1171 d
->charset
, &d
->state
, NULL
);
1173 assert(len
< prevlen
);
1177 rdaddsc(&d
->output
, buf
);
1186 static int info_rdaddsc(info_data
*d
, char const *cs
)
1192 ret
= charset_from_unicode(NULL
, 0, buf
, lenof(buf
),
1193 d
->charset
, &d
->state
, NULL
);
1196 rdaddsc(&d
->output
, buf
);
1203 rdaddsc(&d
->output
, cs
);
1204 return strwid(cs
, d
->charset
);
1209 static int info_rdadd(info_data
*d
, wchar_t wc
)
1214 return info_rdadds(d
, wcs
);
1217 static int info_rdaddc(info_data
*d
, char c
)
1222 return info_rdaddsc(d
, cs
);