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.
38 * Info's 'i' command requires the use of a menu -- it fails to
39 * find any index entries at all with Halibut's current index format.
41 * - The string "*note" is matched case-insensitively, so we could
42 * make things slightly less ugly by using the lower-case version
43 * when the user asks for \k. Unfortunately, standalone Info seems
44 * to match node names case-sensitively, so we can't downcase that.
56 int listindentbefore
, listindentafter
;
57 int indent_code
, width
, index_width
;
58 wchar_t *bullet
, *listsuffix
;
59 wchar_t *startemph
, *endemph
;
60 wchar_t *lquote
, *rquote
;
61 wchar_t *sectsuffix
, *underline
;
72 #define EMPTY_INFO_DATA { { 0, 0, NULL }, 0, CHARSET_INIT_STATE, FALSE }
73 static const info_data empty_info_data
= EMPTY_INFO_DATA
;
75 typedef struct node_tag node
;
78 node
*up
, *prev
, *next
, *lastchild
;
79 int pos
, started_menu
, filenum
;
91 static int info_rdadd(info_data
*, wchar_t);
92 static int info_rdadds(info_data
*, wchar_t const *);
93 static int info_rdaddc(info_data
*, char);
94 static int info_rdaddsc(info_data
*, char const *);
96 static void info_heading(info_data
*, word
*, word
*, int, infoconfig
*);
97 static void info_rule(info_data
*, int, int, infoconfig
*);
98 static void info_para(info_data
*, word
*, wchar_t *, word
*, keywordlist
*,
99 int, int, int, infoconfig
*);
100 static void info_codepara(info_data
*, word
*, int, int);
101 static void info_versionid(info_data
*, word
*, infoconfig
*);
102 static void info_menu_item(info_data
*, node
*, paragraph
*, infoconfig
*);
103 static word
*info_transform_wordlist(word
*, keywordlist
*);
104 static int info_check_index(word
*, node
*, indexdata
*);
106 static int info_rdaddwc(info_data
*, word
*, word
*, int, infoconfig
*);
108 static node
*info_node_new(char *name
, int charset
);
109 static char *info_node_name_for_para(paragraph
*p
, infoconfig
*);
110 static char *info_node_name_for_text(wchar_t *text
, infoconfig
*);
112 static infoconfig
info_configure(paragraph
*source
) {
119 ret
.filename
= dupstr("output.info");
120 ret
.maxfilesize
= 64 << 10;
121 ret
.charset
= CS_ASCII
;
123 ret
.listindentbefore
= 1;
124 ret
.listindentafter
= 3;
126 ret
.index_width
= 40;
127 ret
.listsuffix
= L
".";
128 ret
.bullet
= L
"\x2022\0-\0\0";
129 ret
.rule
= L
"\x2500\0-\0\0";
130 ret
.startemph
= L
"_\0_\0\0";
131 ret
.endemph
= uadv(ret
.startemph
);
132 ret
.lquote
= L
"\x2018\0\x2019\0`\0'\0\0";
133 ret
.rquote
= uadv(ret
.lquote
);
134 ret
.sectsuffix
= L
": ";
135 ret
.underline
= L
"\x203E\0-\0\0";
136 ret
.index_text
= L
"Index";
139 * Two-pass configuration so that we can pick up global config
140 * (e.g. `quotes') before having it overridden by specific
141 * config (`info-quotes'), irrespective of the order in which
144 for (p
= source
; p
; p
= p
->next
) {
145 if (p
->type
== para_Config
) {
146 if (!ustricmp(p
->keyword
, L
"quotes")) {
147 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
148 ret
.lquote
= uadv(p
->keyword
);
149 ret
.rquote
= uadv(ret
.lquote
);
151 } else if (!ustricmp(p
->keyword
, L
"index")) {
152 ret
.index_text
= uadv(p
->keyword
);
157 for (p
= source
; p
; p
= p
->next
) {
158 if (p
->type
== para_Config
) {
159 if (!ustricmp(p
->keyword
, L
"info-filename")) {
161 ret
.filename
= dupstr(adv(p
->origkeyword
));
162 } else if (!ustricmp(p
->keyword
, L
"info-charset")) {
163 ret
.charset
= charset_from_ustr(&p
->fpos
, uadv(p
->keyword
));
164 } else if (!ustricmp(p
->keyword
, L
"info-max-file-size")) {
165 ret
.maxfilesize
= utoi(uadv(p
->keyword
));
166 } else if (!ustricmp(p
->keyword
, L
"info-width")) {
167 ret
.width
= utoi(uadv(p
->keyword
));
168 } else if (!ustricmp(p
->keyword
, L
"info-indent-code")) {
169 ret
.indent_code
= utoi(uadv(p
->keyword
));
170 } else if (!ustricmp(p
->keyword
, L
"info-index-width")) {
171 ret
.index_width
= utoi(uadv(p
->keyword
));
172 } else if (!ustricmp(p
->keyword
, L
"info-list-indent")) {
173 ret
.listindentbefore
= utoi(uadv(p
->keyword
));
174 } else if (!ustricmp(p
->keyword
, L
"info-listitem-indent")) {
175 ret
.listindentafter
= utoi(uadv(p
->keyword
));
176 } else if (!ustricmp(p
->keyword
, L
"info-section-suffix")) {
177 ret
.sectsuffix
= uadv(p
->keyword
);
178 } else if (!ustricmp(p
->keyword
, L
"info-underline")) {
179 ret
.underline
= uadv(p
->keyword
);
180 } else if (!ustricmp(p
->keyword
, L
"info-bullet")) {
181 ret
.bullet
= uadv(p
->keyword
);
182 } else if (!ustricmp(p
->keyword
, L
"info-rule")) {
183 ret
.rule
= uadv(p
->keyword
);
184 } else if (!ustricmp(p
->keyword
, L
"info-list-suffix")) {
185 ret
.listsuffix
= uadv(p
->keyword
);
186 } else if (!ustricmp(p
->keyword
, L
"info-emphasis")) {
187 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
188 ret
.startemph
= uadv(p
->keyword
);
189 ret
.endemph
= uadv(ret
.startemph
);
191 } else if (!ustricmp(p
->keyword
, L
"info-quotes")) {
192 if (*uadv(p
->keyword
) && *uadv(uadv(p
->keyword
))) {
193 ret
.lquote
= uadv(p
->keyword
);
194 ret
.rquote
= uadv(ret
.lquote
);
201 * Now process fallbacks on quote characters, underlines, the
202 * rule character, the emphasis characters, and bullets.
204 while (*uadv(ret
.rquote
) && *uadv(uadv(ret
.rquote
)) &&
205 (!cvt_ok(ret
.charset
, ret
.lquote
) ||
206 !cvt_ok(ret
.charset
, ret
.rquote
))) {
207 ret
.lquote
= uadv(ret
.rquote
);
208 ret
.rquote
= uadv(ret
.lquote
);
211 while (*uadv(ret
.endemph
) && *uadv(uadv(ret
.endemph
)) &&
212 (!cvt_ok(ret
.charset
, ret
.startemph
) ||
213 !cvt_ok(ret
.charset
, ret
.endemph
))) {
214 ret
.startemph
= uadv(ret
.endemph
);
215 ret
.endemph
= uadv(ret
.startemph
);
218 while (*ret
.underline
&& *uadv(ret
.underline
) &&
219 !cvt_ok(ret
.charset
, ret
.underline
))
220 ret
.underline
= uadv(ret
.underline
);
222 while (*ret
.bullet
&& *uadv(ret
.bullet
) &&
223 !cvt_ok(ret
.charset
, ret
.bullet
))
224 ret
.bullet
= uadv(ret
.bullet
);
226 while (*ret
.rule
&& *uadv(ret
.rule
) &&
227 !cvt_ok(ret
.charset
, ret
.rule
))
228 ret
.rule
= uadv(ret
.rule
);
233 paragraph
*info_config_filename(char *filename
)
235 return cmdline_cfg_simple("info-filename", filename
, NULL
);
238 void info_backend(paragraph
*sourceform
, keywordlist
*keywords
,
239 indexdata
*idx
, void *unused
) {
242 word
*prefix
, *body
, *wp
;
244 wchar_t *prefixextra
;
245 int nesting
, nestindent
;
246 int indentb
, indenta
;
249 info_data intro_text
= EMPTY_INFO_DATA
;
250 node
*topnode
, *currnode
;
256 conf
= info_configure(sourceform
);
259 * Go through and create a node for each section.
261 topnode
= info_node_new("Top", conf
.charset
);
263 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
269 case para_UnnumberedChapter
:
273 node
*newnode
, *upnode
;
276 nodename
= info_node_name_for_para(p
, &conf
);
277 newnode
= info_node_new(nodename
, conf
.charset
);
280 p
->private_data
= newnode
;
283 upnode
= (node
*)p
->parent
->private_data
;
287 newnode
->up
= upnode
;
289 currnode
->next
= newnode
;
290 newnode
->prev
= currnode
;
292 currnode
->listnext
= newnode
;
297 p
->private_data
= NULL
;
302 * Set up the display form of each index entry.
308 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
309 info_idx
*ii
= snew(info_idx
);
310 info_data id
= EMPTY_INFO_DATA
;
312 id
.charset
= conf
.charset
;
314 ii
->nnodes
= ii
->nodesize
= 0;
317 ii
->length
= info_rdaddwc(&id
, entry
->text
, NULL
, FALSE
, &conf
);
319 ii
->text
= id
.output
.text
;
321 entry
->backend_data
= ii
;
326 * An Info file begins with a piece of introductory text which
327 * is apparently never shown anywhere. This seems to me to be a
328 * good place to put the copyright notice and the version IDs.
329 * Also, Info directory entries are expected to go here.
331 intro_text
.charset
= conf
.charset
;
333 info_rdaddsc(&intro_text
,
334 "This Info file generated by Halibut, ");
335 info_rdaddsc(&intro_text
, version
);
336 info_rdaddsc(&intro_text
, "\n\n");
338 for (p
= sourceform
; p
; p
= p
->next
)
339 if (p
->type
== para_Config
&&
340 !ustricmp(p
->keyword
, L
"info-dir-entry")) {
341 wchar_t *section
, *shortname
, *longname
, *kw
;
344 section
= uadv(p
->keyword
);
345 shortname
= *section ?
uadv(section
) : L
"";
346 longname
= *shortname ?
uadv(shortname
) : L
"";
347 kw
= *longname ?
uadv(longname
) : L
"";
350 error(err_cfginsufarg
, &p
->fpos
, p
->origkeyword
, 3);
354 info_rdaddsc(&intro_text
, "INFO-DIR-SECTION ");
355 info_rdadds(&intro_text
, section
);
356 info_rdaddsc(&intro_text
, "\nSTART-INFO-DIR-ENTRY\n* ");
357 info_rdadds(&intro_text
, shortname
);
358 info_rdaddsc(&intro_text
, ": (");
359 s
= dupstr(conf
.filename
);
360 if (strlen(s
) > 5 && !strcmp(s
+strlen(s
)-5, ".info"))
361 s
[strlen(s
)-5] = '\0';
362 info_rdaddsc(&intro_text
, s
);
364 info_rdaddsc(&intro_text
, ")");
366 keyword
*kwl
= kw_lookup(keywords
, kw
);
367 if (kwl
&& kwl
->para
->private_data
) {
368 node
*n
= (node
*)kwl
->para
->private_data
;
369 info_rdaddsc(&intro_text
, n
->name
);
372 info_rdaddsc(&intro_text
, ". ");
373 info_rdadds(&intro_text
, longname
);
374 info_rdaddsc(&intro_text
, "\nEND-INFO-DIR-ENTRY\n\n");
377 for (p
= sourceform
; p
; p
= p
->next
)
378 if (p
->type
== para_Copyright
)
379 info_para(&intro_text
, NULL
, NULL
, p
->words
, keywords
,
380 0, 0, conf
.width
, &conf
);
382 for (p
= sourceform
; p
; p
= p
->next
)
383 if (p
->type
== para_VersionID
)
384 info_versionid(&intro_text
, p
->words
, &conf
);
386 if (intro_text
.output
.text
[intro_text
.output
.pos
-1] != '\n')
387 info_rdaddc(&intro_text
, '\n');
390 for (p
= sourceform
; p
; p
= p
->next
)
391 if (p
->type
== para_Title
)
392 info_heading(&topnode
->text
, NULL
, p
->words
, conf
.width
, &conf
);
394 nestindent
= conf
.listindentbefore
+ conf
.listindentafter
;
399 /* Do the main document */
400 for (p
= sourceform
; p
; p
= p
->next
) switch (p
->type
) {
407 assert(nesting
>= 0);
411 nesting
+= nestindent
;
414 nesting
-= nestindent
;
415 assert(nesting
>= 0);
419 * Things we ignore because we've already processed them or
420 * aren't going to touch them in this pass.
424 case para_Biblio
: /* only touch BiblioCited */
435 case para_UnnumberedChapter
:
438 currnode
= p
->private_data
;
440 assert(currnode
->up
);
442 if (!currnode
->up
->started_menu
) {
443 info_rdaddsc(&currnode
->up
->text
, "* Menu:\n\n");
444 currnode
->up
->started_menu
= TRUE
;
446 info_menu_item(&currnode
->up
->text
, currnode
, p
, &conf
);
448 has_index
|= info_check_index(p
->words
, currnode
, idx
);
449 info_heading(&currnode
->text
, p
->kwtext
, p
->words
, conf
.width
, &conf
);
454 info_rule(&currnode
->text
, nesting
, conf
.width
- nesting
, &conf
);
459 case para_DescribedThing
:
460 case para_Description
:
461 case para_BiblioCited
:
463 case para_NumberedList
:
464 has_index
|= info_check_index(p
->words
, currnode
, idx
);
465 if (p
->type
== para_Bullet
) {
468 bullet
.type
= word_Normal
;
469 bullet
.text
= conf
.bullet
;
472 indentb
= conf
.listindentbefore
;
473 indenta
= conf
.listindentafter
;
474 } else if (p
->type
== para_NumberedList
) {
476 prefixextra
= conf
.listsuffix
;
477 indentb
= conf
.listindentbefore
;
478 indenta
= conf
.listindentafter
;
479 } else if (p
->type
== para_Description
) {
482 indentb
= conf
.listindentbefore
;
483 indenta
= conf
.listindentafter
;
487 indentb
= indenta
= 0;
489 if (p
->type
== para_BiblioCited
) {
490 body
= dup_word_list(p
->kwtext
);
491 for (wp
= body
; wp
->next
; wp
= wp
->next
);
492 wp
->next
= &spaceword
;
493 spaceword
.next
= p
->words
;
494 spaceword
.alt
= NULL
;
495 spaceword
.type
= word_WhiteSpace
;
496 spaceword
.text
= NULL
;
501 info_para(&currnode
->text
, prefix
, prefixextra
, body
, keywords
,
502 nesting
+ indentb
, indenta
,
503 conf
.width
- nesting
- indentb
- indenta
, &conf
);
506 free_word_list(body
);
511 info_codepara(&currnode
->text
, p
->words
,
512 nesting
+ conf
.indent_code
,
513 conf
.width
- nesting
- 2 * conf
.indent_code
);
518 * Create an index node if required.
526 nodename
= info_node_name_for_text(conf
.index_text
, &conf
);
527 newnode
= info_node_new(nodename
, conf
.charset
);
530 newnode
->up
= topnode
;
532 currnode
->next
= newnode
;
533 newnode
->prev
= currnode
;
534 currnode
->listnext
= newnode
;
536 k
= info_rdadds(&newnode
->text
, conf
.index_text
);
537 info_rdaddsc(&newnode
->text
, "\n");
539 info_rdadds(&newnode
->text
, conf
.underline
);
540 k
-= ustrwid(conf
.underline
, conf
.charset
);
542 info_rdaddsc(&newnode
->text
, "\n\n");
544 info_menu_item(&topnode
->text
, newnode
, NULL
, &conf
);
546 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++) {
547 info_idx
*ii
= (info_idx
*)entry
->backend_data
;
549 for (j
= 0; j
< ii
->nnodes
; j
++) {
551 * When we have multiple references for a single
552 * index term, we only display the actual term on
553 * the first line, to make it clear that the terms
554 * really are the same.
557 info_rdaddsc(&newnode
->text
, ii
->text
);
558 for (k
= (j ?
0 : ii
->length
); k
< conf
.index_width
-2; k
++)
559 info_rdaddc(&newnode
->text
, ' ');
560 info_rdaddsc(&newnode
->text
, " *Note ");
561 info_rdaddsc(&newnode
->text
, ii
->nodes
[j
]->name
);
562 info_rdaddsc(&newnode
->text
, "::\n");
568 * Finalise the text of each node, by adding the ^_ delimiter
569 * and the node line at the top.
571 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
572 char *origtext
= currnode
->text
.output
.text
;
573 currnode
->text
= empty_info_data
;
574 currnode
->text
.charset
= conf
.charset
;
575 info_rdaddsc(&currnode
->text
, "\037\nFile: ");
576 info_rdaddsc(&currnode
->text
, conf
.filename
);
577 info_rdaddsc(&currnode
->text
, ", Node: ");
578 info_rdaddsc(&currnode
->text
, currnode
->name
);
579 if (currnode
->prev
) {
580 info_rdaddsc(&currnode
->text
, ", Prev: ");
581 info_rdaddsc(&currnode
->text
, currnode
->prev
->name
);
583 info_rdaddsc(&currnode
->text
, ", Up: ");
584 info_rdaddsc(&currnode
->text
, (currnode
->up ?
585 currnode
->up
->name
: "(dir)"));
586 if (currnode
->next
) {
587 info_rdaddsc(&currnode
->text
, ", Next: ");
588 info_rdaddsc(&currnode
->text
, currnode
->next
->name
);
590 info_rdaddsc(&currnode
->text
, "\n\n");
591 info_rdaddsc(&currnode
->text
, origtext
);
593 * Just make _absolutely_ sure we end with a newline.
595 if (currnode
->text
.output
.text
[currnode
->text
.output
.pos
-1] != '\n')
596 info_rdaddc(&currnode
->text
, '\n');
602 * Compute the offsets for the tag table.
604 filepos
= intro_text
.output
.pos
;
605 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
606 currnode
->pos
= filepos
;
607 filepos
+= currnode
->text
.output
.pos
;
611 * Split into sub-files.
613 if (conf
.maxfilesize
> 0) {
614 int currfilesize
= intro_text
.output
.pos
, currfilenum
= 1;
615 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
616 if (currfilesize
> intro_text
.output
.pos
&&
617 currfilesize
+ currnode
->text
.output
.pos
> conf
.maxfilesize
) {
619 currfilesize
= intro_text
.output
.pos
;
621 currnode
->filenum
= currfilenum
;
622 currfilesize
+= currnode
->text
.output
.pos
;
627 * Write the primary output file.
629 fp
= fopen(conf
.filename
, "w");
631 error(err_cantopenw
, conf
.filename
);
634 fputs(intro_text
.output
.text
, fp
);
635 if (conf
.maxfilesize
== 0) {
636 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
)
637 fputs(currnode
->text
.output
.text
, fp
);
640 fprintf(fp
, "\037\nIndirect:\n");
641 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
)
642 if (filenum
!= currnode
->filenum
) {
643 filenum
= currnode
->filenum
;
644 fprintf(fp
, "%s-%d: %d\n", conf
.filename
, filenum
,
648 fprintf(fp
, "\037\nTag Table:\n");
649 if (conf
.maxfilesize
> 0)
650 fprintf(fp
, "(Indirect)\n");
651 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
)
652 fprintf(fp
, "Node: %s\177%d\n", currnode
->name
, currnode
->pos
);
653 fprintf(fp
, "\037\nEnd Tag Table\n");
657 * Write the subfiles.
659 if (conf
.maxfilesize
> 0) {
663 for (currnode
= topnode
; currnode
; currnode
= currnode
->listnext
) {
664 if (filenum
!= currnode
->filenum
) {
667 filenum
= currnode
->filenum
;
671 fname
= snewn(strlen(conf
.filename
) + 40, char);
672 sprintf(fname
, "%s-%d", conf
.filename
, filenum
);
673 fp
= fopen(fname
, "w");
675 error(err_cantopenw
, fname
);
679 fputs(intro_text
.output
.text
, fp
);
681 fputs(currnode
->text
.output
.text
, fp
);
689 static int info_check_index(word
*w
, node
*n
, indexdata
*idx
)
693 for (; w
; w
= w
->next
) {
694 if (w
->type
== word_IndexRef
) {
698 tag
= index_findtag(idx
, w
->text
);
702 for (i
= 0; i
< tag
->nrefs
; i
++) {
703 indexentry
*entry
= tag
->refs
[i
];
704 info_idx
*ii
= (info_idx
*)entry
->backend_data
;
706 if (ii
->nnodes
> 0 && ii
->nodes
[ii
->nnodes
-1] == n
) {
708 * If the same index term is indexed twice
709 * within the same section, we only want to
710 * mention it once in the index. So do nothing
716 if (ii
->nnodes
>= ii
->nodesize
) {
718 ii
->nodes
= sresize(ii
->nodes
, ii
->nodesize
, node
*);
721 ii
->nodes
[ii
->nnodes
++] = n
;
731 static word
*info_transform_wordlist(word
*words
, keywordlist
*keywords
)
733 word
*ret
= dup_word_list(words
);
737 for (w
= ret
; w
; w
= w
->next
) {
738 w
->private_data
= NULL
;
739 if (w
->type
== word_UpperXref
|| w
->type
== word_LowerXref
) {
740 kwl
= kw_lookup(keywords
, w
->text
);
742 if (kwl
->para
->type
== para_NumberedList
||
743 kwl
->para
->type
== para_BiblioCited
) {
745 * In Info, we do nothing special for xrefs to
746 * numbered list items or bibliography entries.
751 * An xref to a different section has its text
752 * completely replaced.
758 if (w2
->type
== word_XrefEnd
) {
768 * Now w is the UpperXref / LowerXref we
769 * started with, and w4 is the next word after
770 * the corresponding XrefEnd (if any). The
771 * simplest thing is just to stick a pointer to
772 * the target node structure in the private
773 * data field of the xref word, and let
774 * info_rdaddwc and friends read the node name
778 w
->private_data
= kwl
->para
->private_data
;
779 assert(w
->private_data
);
788 static int info_rdaddwc(info_data
*id
, word
*words
, word
*end
, int xrefs
,
792 for (; words
&& words
!= end
; words
= words
->next
) switch (words
->type
) {
803 case word_WhiteSpace
:
806 case word_WkCodeSpace
:
810 case word_WkCodeQuote
:
811 assert(words
->type
!= word_CodeQuote
&&
812 words
->type
!= word_WkCodeQuote
);
813 if (towordstyle(words
->type
) == word_Emph
&&
814 (attraux(words
->aux
) == attr_First
||
815 attraux(words
->aux
) == attr_Only
))
816 ret
+= info_rdadds(id
, cfg
->startemph
);
817 else if (towordstyle(words
->type
) == word_Code
&&
818 (attraux(words
->aux
) == attr_First
||
819 attraux(words
->aux
) == attr_Only
))
820 ret
+= info_rdadds(id
, cfg
->lquote
);
821 if (removeattr(words
->type
) == word_Normal
) {
822 if (cvt_ok(id
->charset
, words
->text
) || !words
->alt
)
823 ret
+= info_rdadds(id
, words
->text
);
825 ret
+= info_rdaddwc(id
, words
->alt
, NULL
, FALSE
, cfg
);
826 } else if (removeattr(words
->type
) == word_WhiteSpace
) {
827 ret
+= info_rdadd(id
, L
' ');
828 } else if (removeattr(words
->type
) == word_Quote
) {
829 ret
+= info_rdadds(id
, quoteaux(words
->aux
) == quote_Open ?
830 cfg
->lquote
: cfg
->rquote
);
832 if (towordstyle(words
->type
) == word_Emph
&&
833 (attraux(words
->aux
) == attr_Last
||
834 attraux(words
->aux
) == attr_Only
))
835 ret
+= info_rdadds(id
, cfg
->endemph
);
836 else if (towordstyle(words
->type
) == word_Code
&&
837 (attraux(words
->aux
) == attr_Last
||
838 attraux(words
->aux
) == attr_Only
))
839 ret
+= info_rdadds(id
, cfg
->rquote
);
844 if (xrefs
&& words
->private_data
) {
846 * This bit is structural and so must be done in char
847 * rather than wchar_t.
849 ret
+= info_rdaddsc(id
, "*Note ");
850 ret
+= info_rdaddsc(id
, ((node
*)words
->private_data
)->name
);
851 ret
+= info_rdaddsc(id
, "::");
859 static int info_width_internal(word
*words
, int xrefs
, infoconfig
*cfg
);
861 static int info_width_internal_list(word
*words
, int xrefs
, infoconfig
*cfg
) {
864 w
+= info_width_internal(words
, xrefs
, cfg
);
870 static int info_width_internal(word
*words
, int xrefs
, infoconfig
*cfg
) {
874 switch (words
->type
) {
883 if (xrefs
&& words
->private_data
) {
884 /* "*Note " plus "::" comes to 8 characters */
885 return 8 + strwid(((node
*)words
->private_data
)->name
,
891 assert(words
->type
< word_internal_endattrs
);
894 attr
= towordstyle(words
->type
);
896 if (attr
== word_Emph
|| attr
== word_Code
) {
897 if (attraux(words
->aux
) == attr_Only
||
898 attraux(words
->aux
) == attr_First
)
899 wid
+= ustrwid(attr
== word_Emph ? cfg
->startemph
: cfg
->lquote
,
902 if (attr
== word_Emph
|| attr
== word_Code
) {
903 if (attraux(words
->aux
) == attr_Only
||
904 attraux(words
->aux
) == attr_Last
)
905 wid
+= ustrwid(attr
== word_Emph ? cfg
->startemph
: cfg
->lquote
,
909 switch (words
->type
) {
914 if (cvt_ok(cfg
->charset
, words
->text
) || !words
->alt
)
915 wid
+= ustrwid(words
->text
, cfg
->charset
);
917 wid
+= info_width_internal_list(words
->alt
, xrefs
, cfg
);
920 case word_WhiteSpace
:
923 case word_WkCodeSpace
:
927 case word_WkCodeQuote
:
928 assert(words
->type
!= word_CodeQuote
&&
929 words
->type
!= word_WkCodeQuote
);
930 if (removeattr(words
->type
) == word_Quote
) {
931 if (quoteaux(words
->aux
) == quote_Open
)
932 wid
+= ustrwid(cfg
->lquote
, cfg
->charset
);
934 wid
+= ustrwid(cfg
->rquote
, cfg
->charset
);
941 static int info_width_noxrefs(void *ctx
, word
*words
)
943 return info_width_internal(words
, FALSE
, (infoconfig
*)ctx
);
945 static int info_width_xrefs(void *ctx
, word
*words
)
947 return info_width_internal(words
, TRUE
, (infoconfig
*)ctx
);
950 static void info_heading(info_data
*text
, word
*tprefix
,
951 word
*words
, int width
, infoconfig
*cfg
) {
953 int firstlinewidth
, wrapwidth
;
954 wrappedline
*wrapping
, *p
;
958 length
+= info_rdaddwc(text
, tprefix
, NULL
, FALSE
, cfg
);
959 length
+= info_rdadds(text
, cfg
->sectsuffix
);
963 firstlinewidth
= width
- length
;
965 wrapping
= wrap_para(words
, firstlinewidth
, wrapwidth
,
966 info_width_noxrefs
, cfg
, 0);
967 for (p
= wrapping
; p
; p
= p
->next
) {
968 length
+= info_rdaddwc(text
, p
->begin
, p
->end
, FALSE
, cfg
);
969 info_rdadd(text
, L
'\n');
971 info_rdadds(text
, cfg
->underline
);
972 length
-= ustrwid(cfg
->underline
, cfg
->charset
);
974 info_rdadd(text
, L
'\n');
978 info_rdadd(text
, L
'\n');
981 static void info_rule(info_data
*text
, int indent
, int width
, infoconfig
*cfg
)
983 while (indent
--) info_rdadd(text
, L
' ');
985 info_rdadds(text
, cfg
->rule
);
986 width
-= ustrwid(cfg
->rule
, cfg
->charset
);
988 info_rdadd(text
, L
'\n');
989 info_rdadd(text
, L
'\n');
992 static void info_para(info_data
*text
, word
*prefix
, wchar_t *prefixextra
,
993 word
*input
, keywordlist
*keywords
, int indent
,
994 int extraindent
, int width
, infoconfig
*cfg
) {
995 wrappedline
*wrapping
, *p
;
999 int firstlinewidth
= width
;
1001 words
= info_transform_wordlist(input
, keywords
);
1004 for (i
= 0; i
< indent
; i
++)
1005 info_rdadd(text
, L
' ');
1006 e
= info_rdaddwc(text
, prefix
, NULL
, FALSE
, cfg
);
1008 e
+= info_rdadds(text
, prefixextra
);
1009 /* If the prefix is too long, shorten the first line to fit. */
1010 e
= extraindent
- e
;
1012 firstlinewidth
+= e
; /* this decreases it, since e < 0 */
1013 if (firstlinewidth
< 0) {
1014 e
= indent
+ extraindent
;
1015 firstlinewidth
= width
;
1016 info_rdadd(text
, L
'\n');
1021 e
= indent
+ extraindent
;
1023 wrapping
= wrap_para(words
, firstlinewidth
, width
, info_width_xrefs
,
1025 for (p
= wrapping
; p
; p
= p
->next
) {
1026 for (i
= 0; i
< e
; i
++)
1027 info_rdadd(text
, L
' ');
1028 info_rdaddwc(text
, p
->begin
, p
->end
, TRUE
, cfg
);
1029 info_rdadd(text
, L
'\n');
1030 e
= indent
+ extraindent
;
1032 wrap_free(wrapping
);
1033 info_rdadd(text
, L
'\n');
1035 free_word_list(words
);
1038 static void info_codepara(info_data
*text
, word
*words
,
1039 int indent
, int width
) {
1042 for (; words
; words
= words
->next
) if (words
->type
== word_WeakCode
) {
1043 for (i
= 0; i
< indent
; i
++)
1044 info_rdadd(text
, L
' ');
1045 if (info_rdadds(text
, words
->text
) > width
) {
1048 info_rdadd(text
, L
'\n');
1051 info_rdadd(text
, L
'\n');
1054 static void info_versionid(info_data
*text
, word
*words
, infoconfig
*cfg
) {
1055 info_rdadd(text
, L
'[');
1056 info_rdaddwc(text
, words
, NULL
, FALSE
, cfg
);
1057 info_rdadds(text
, L
"]\n");
1060 static node
*info_node_new(char *name
, int charset
)
1065 n
->text
= empty_info_data
;
1066 n
->text
.charset
= charset
;
1067 n
->up
= n
->next
= n
->prev
= n
->lastchild
= n
->listnext
= NULL
;
1068 n
->name
= dupstr(name
);
1069 n
->started_menu
= FALSE
;
1074 static char *info_node_name_core(info_data
*id
, filepos
*fpos
)
1079 * We cannot have commas, colons or parentheses in a node name.
1080 * Remove any that we find, with a warning.
1082 p
= q
= id
->output
.text
;
1084 if (*p
== ':' || *p
== ',' || *p
== '(' || *p
== ')') {
1085 error(err_infonodechar
, fpos
, *p
);
1093 return id
->output
.text
;
1096 static char *info_node_name_for_para(paragraph
*par
, infoconfig
*cfg
)
1098 info_data id
= EMPTY_INFO_DATA
;
1100 id
.charset
= cfg
->charset
;
1101 info_rdaddwc(&id
, par
->kwtext ? par
->kwtext
: par
->words
,
1103 info_rdaddsc(&id
, NULL
);
1105 return info_node_name_core(&id
, &par
->fpos
);
1108 static char *info_node_name_for_text(wchar_t *text
, infoconfig
*cfg
)
1110 info_data id
= EMPTY_INFO_DATA
;
1112 id
.charset
= cfg
->charset
;
1113 info_rdadds(&id
, text
);
1114 info_rdaddsc(&id
, NULL
);
1116 return info_node_name_core(&id
, NULL
);
1119 static void info_menu_item(info_data
*text
, node
*n
, paragraph
*p
,
1123 * FIXME: Depending on how we're doing node names in this info
1124 * file, we might want to do
1126 * * Node name:: Chapter title
1130 * * Chapter number: Node name.
1132 * This function mostly works in char rather than wchar_t,
1133 * because a menu item is a structural component.
1135 info_rdaddsc(text
, "* ");
1136 info_rdaddsc(text
, n
->name
);
1137 info_rdaddsc(text
, "::");
1139 info_rdaddc(text
, ' ');
1140 info_rdaddwc(text
, p
->words
, NULL
, FALSE
, cfg
);
1142 info_rdaddc(text
, '\n');
1146 * These functions implement my wrapper on the rdadd* calls which
1147 * allows me to switch arbitrarily between literal octet-string
1148 * text and charset-translated Unicode. (Because no matter what
1149 * character set I write the actual text in, I expect info readers
1150 * to treat node names and file names literally and to expect
1151 * keywords like `*Note' in their canonical form, so I have to take
1152 * steps to ensure that those structural elements of the file
1153 * aren't messed with.)
1155 static int info_rdadds(info_data
*d
, wchar_t const *wcs
)
1158 d
->state
= charset_init_state
;
1164 int len
, width
, ret
;
1166 width
= ustrwid(wcs
, d
->charset
);
1172 ret
= charset_from_unicode(&wcs
, &len
, buf
, lenof(buf
),
1173 d
->charset
, &d
->state
, NULL
);
1175 assert(len
< prevlen
);
1179 rdaddsc(&d
->output
, buf
);
1188 static int info_rdaddsc(info_data
*d
, char const *cs
)
1194 ret
= charset_from_unicode(NULL
, 0, buf
, lenof(buf
),
1195 d
->charset
, &d
->state
, NULL
);
1198 rdaddsc(&d
->output
, buf
);
1205 rdaddsc(&d
->output
, cs
);
1206 return strwid(cs
, d
->charset
);
1211 static int info_rdadd(info_data
*d
, wchar_t wc
)
1216 return info_rdadds(d
, wcs
);
1219 static int info_rdaddc(info_data
*d
, char c
)
1224 return info_rdaddsc(d
, cs
);