gcc41: update README.DELETED
[dragonfly.git] / contrib / texinfo-4 / makeinfo / sectioning.c
blobce32aeb3a7e52badc5a164a2d5ba44f14673b268
1 /* sectioning.c -- for @chapter, @section, ..., @contents ...
2 $Id: sectioning.c,v 1.25 2004/07/05 22:23:23 karl Exp $
4 Copyright (C) 1999, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 Originally written by Karl Heinz Marbaise <kama@hippo.fido.de>. */
22 #include "system.h"
23 #include "cmds.h"
24 #include "macro.h"
25 #include "makeinfo.h"
26 #include "node.h"
27 #include "toc.h"
28 #include "sectioning.h"
29 #include "xml.h"
31 /* See comment in sectioning.h. */
32 section_alist_type section_alist[] = {
33 { "unnumberedsubsubsec", 5, ENUM_SECT_NO, TOC_YES },
34 { "unnumberedsubsec", 4, ENUM_SECT_NO, TOC_YES },
35 { "unnumberedsec", 3, ENUM_SECT_NO, TOC_YES },
36 { "unnumbered", 2, ENUM_SECT_NO, TOC_YES },
37 { "centerchap", 2, ENUM_SECT_NO, TOC_YES },
39 { "appendixsubsubsec", 5, ENUM_SECT_APP, TOC_YES }, /* numbered like A.X.X.X */
40 { "appendixsubsec", 4, ENUM_SECT_APP, TOC_YES },
41 { "appendixsec", 3, ENUM_SECT_APP, TOC_YES },
42 { "appendixsection", 3, ENUM_SECT_APP, TOC_YES },
43 { "appendix", 2, ENUM_SECT_APP, TOC_YES },
45 { "subsubsec", 5, ENUM_SECT_YES, TOC_YES },
46 { "subsubsection", 5, ENUM_SECT_YES, TOC_YES },
47 { "subsection", 4, ENUM_SECT_YES, TOC_YES },
48 { "section", 3, ENUM_SECT_YES, TOC_YES },
49 { "chapter", 2, ENUM_SECT_YES, TOC_YES },
51 { "subsubheading", 5, ENUM_SECT_NO, TOC_NO },
52 { "subheading", 4, ENUM_SECT_NO, TOC_NO },
53 { "heading", 3, ENUM_SECT_NO, TOC_NO },
54 { "chapheading", 2, ENUM_SECT_NO, TOC_NO },
55 { "majorheading", 2, ENUM_SECT_NO, TOC_NO },
57 { "top", 1, ENUM_SECT_NO, TOC_YES },
58 { NULL, 0, 0, 0 }
61 /* The argument of @settitle, used for HTML. */
62 char *title = NULL;
65 #define APPENDIX_MAGIC 1024
66 #define UNNUMBERED_MAGIC 2048
68 /* Number memory for every level @chapter, @section,
69 @subsection, @subsubsection. */
70 static int numbers [] = { 0, 0, 0, 0 };
72 /* enum_marker == APPENDIX_MAGIC then we are counting appendencies
73 enum_marker == UNNUMBERED_MAGIC then we are within unnumbered area.
74 Handling situations like this:
75 @unnumbered ..
76 @section ... */
77 static int enum_marker = 0;
79 /* Organized by level commands. That is, "*" == chapter, "=" == section. */
80 static char *scoring_characters = "*=-.";
82 /* Amount to offset the name of sectioning commands to levels by. */
83 static int section_alist_offset = 0;
85 /* These two variables are for @float, @cindex like commands that need to know
86 in which section they are used. */
87 /* Last value returned by get_sectioning_number. */
88 static char *last_sectioning_number = "";
89 /* Last title used by sectioning_underscore, etc. */
90 static char *last_sectioning_title = "";
92 /* num == ENUM_SECT_NO means unnumbered (should never call this)
93 num == ENUM_SECT_YES means numbered
94 num == ENUM_SECT_APP means numbered like A.1 and so on */
95 static char *
96 get_sectioning_number (int level, int num)
98 static char s[100]; /* should ever be enough for 99.99.99.99
99 Appendix A.1 */
101 char *p;
102 int i;
104 s[0] = 0;
106 /* create enumeration in front of chapter, section, subsection and so on. */
107 for (i = 0; i < level; i++)
109 p = s + strlen (s);
110 if ((i == 0) && (enum_marker == APPENDIX_MAGIC))
111 sprintf (p, "%c.", numbers[i] + 64); /* Should be changed to
112 be more portable */
113 else
114 sprintf (p, "%d.", numbers[i]);
117 /* the last number is never followed by a dot */
118 p = s + strlen (s);
119 if ((num == ENUM_SECT_APP)
120 && (i == 0)
121 && (enum_marker == APPENDIX_MAGIC))
122 sprintf (p, _("Appendix %c"), numbers[i] + 64);
123 else
124 sprintf (p, "%d", numbers[i]);
126 /* Poor man's cache :-) */
127 if (strlen (last_sectioning_number))
128 free (last_sectioning_number);
129 last_sectioning_number = xstrdup (s);
131 return s;
135 /* Set the level of @top to LEVEL. Return the old level of @top. */
137 set_top_section_level (int level)
139 int i, result = -1;
141 for (i = 0; section_alist[i].name; i++)
142 if (strcmp (section_alist[i].name, "top") == 0)
144 result = section_alist[i].level;
145 section_alist[i].level = level;
146 break;
148 return result;
152 /* return the index of the given sectioning command in section_alist */
153 static int
154 search_sectioning (char *text)
156 int i;
157 char *t;
159 /* ignore the optional command prefix */
160 if (text[0] == COMMAND_PREFIX)
161 text++;
163 for (i = 0; (t = section_alist[i].name); i++)
165 if (strcmp (t, text) == 0)
167 return i;
170 return -1;
173 /* Return an integer which identifies the type of section present in
174 TEXT -- 1 for @top, 2 for chapters, ..., 5 for subsubsections (as
175 specified in section_alist). We take into account any @lowersections
176 and @raisesections. If SECNAME is non-NULL, also return the
177 corresponding section name. */
179 what_section (char *text, char **secname)
181 int index, j;
182 char *temp;
183 int return_val;
185 find_section_command:
186 for (j = 0; text[j] && cr_or_whitespace (text[j]); j++);
187 if (text[j] != COMMAND_PREFIX)
188 return -1;
190 text = text + j + 1;
192 /* We skip @c, @comment, and @?index commands. */
193 if ((strncmp (text, "comment", strlen ("comment")) == 0) ||
194 (text[0] == 'c' && cr_or_whitespace (text[1])) ||
195 (strcmp (text + 1, "index") == 0))
197 while (*text++ != '\n');
198 goto find_section_command;
201 /* Handle italicized sectioning commands. */
202 if (*text == 'i')
203 text++;
205 for (j = 0; text[j] && !cr_or_whitespace (text[j]); j++);
207 temp = xmalloc (1 + j);
208 strncpy (temp, text, j);
209 temp[j] = 0;
211 index = search_sectioning (temp);
212 free (temp);
213 if (index >= 0)
215 return_val = section_alist[index].level + section_alist_offset;
216 if (return_val < 0)
217 return_val = 0;
218 else if (return_val > 5)
219 return_val = 5;
221 if (secname)
223 int i;
224 int alist_size = sizeof (section_alist) / sizeof(section_alist_type);
225 /* Find location of offset sectioning entry, but don't go off
226 either end of the array. */
227 int index_offset = MAX (index - section_alist_offset, 0);
228 index_offset = MIN (index_offset, alist_size - 1);
230 /* Also make sure we don't go into the next "group" of
231 sectioning changes, e.g., change from an @appendix to an
232 @heading or some such. */
233 #define SIGN(expr) ((expr) < 0 ? -1 : 1)
234 for (i = index; i != index_offset; i -= SIGN (section_alist_offset))
236 /* As it happens, each group has unique .num/.toc values. */
237 if (section_alist[i].num != section_alist[index_offset].num
238 || section_alist[i].toc != section_alist[index_offset].toc)
239 break;
241 *secname = section_alist[i].name;
243 return return_val;
245 return -1;
248 /* Returns current top level division (ie. chapter, unnumbered) number.
249 - For chapters, returns the number.
250 - For unnumbered sections, returns empty string.
251 - For appendices, returns A, B, etc. */
252 char *
253 current_chapter_number (void)
255 if (enum_marker == UNNUMBERED_MAGIC)
256 return xstrdup ("");
257 else if (enum_marker == APPENDIX_MAGIC)
259 char s[1];
260 sprintf (s, "%c", numbers[0] + 64);
261 return xstrdup (s);
263 else
265 char s[5];
266 sprintf (s, "%d", numbers[0]);
267 return xstrdup (s);
271 /* Returns number of the last sectioning command used. */
272 char *
273 current_sectioning_number (void)
275 if (enum_marker == UNNUMBERED_MAGIC || !number_sections)
276 return xstrdup ("");
277 else
278 return xstrdup (last_sectioning_number);
281 /* Returns arguments of the last sectioning command used. */
282 char *
283 current_sectioning_name (void)
285 return xstrdup (last_sectioning_title);
288 /* insert_and_underscore, sectioning_underscore and sectioning_html call this. */
290 static char *
291 handle_enum_increment (int level, int index)
293 /* Here is how TeX handles enumeration:
294 - Anything starting with @unnumbered is not enumerated.
295 - @majorheading and the like are not enumberated. */
296 int i;
298 /* First constraint above. */
299 if (enum_marker == UNNUMBERED_MAGIC && level == 0)
300 return xstrdup ("");
302 /* Second constraint. */
303 if (section_alist[index].num == ENUM_SECT_NO)
304 return xstrdup ("");
306 /* reset all counters which are one level deeper */
307 for (i = level; i < 3; i++)
308 numbers [i + 1] = 0;
310 numbers[level]++;
311 if (section_alist[index].num == ENUM_SECT_NO || enum_marker == UNNUMBERED_MAGIC
312 || !number_sections)
313 return xstrdup ("");
314 else
315 return xstrdup (get_sectioning_number (level, section_alist[index].num));
319 void
320 sectioning_underscore (char *cmd)
322 char *temp, *secname;
323 int level;
325 /* If we're not indenting the first paragraph, we shall make it behave
326 like @noindent is called directly after the section heading. */
327 if (! do_first_par_indent)
328 cm_noindent ();
330 temp = xmalloc (2 + strlen (cmd));
331 temp[0] = COMMAND_PREFIX;
332 strcpy (&temp[1], cmd);
333 level = what_section (temp, &secname);
334 level -= 2;
335 if (level < 0)
336 level = 0;
337 free (temp);
339 /* If the argument to @top is empty, we try using the one from @settitle.
340 Warn if both are unusable. */
341 if (STREQ (command, "top"))
343 int save_input_text_offset = input_text_offset;
345 get_rest_of_line (0, &temp);
347 /* Due to get_rest_of_line ... */
348 line_number--;
350 if (strlen (temp) == 0 && (!title || strlen (title) == 0))
351 warning ("Must specify a title with least one of @settitle or @top");
353 input_text_offset = save_input_text_offset;
356 if (xml)
358 /* If the section appears in the toc, it means it's a real section
359 unlike majorheading, chapheading etc. */
360 if (section_alist[search_sectioning (cmd)].toc == TOC_YES)
362 xml_close_sections (level);
363 /* Mark the beginning of the section
364 If the next command is printindex, we will remove
365 the section and put an Index instead */
366 flush_output ();
367 xml_last_section_output_position = output_paragraph_offset;
369 get_rest_of_line (0, &temp);
371 /* Use @settitle value if @top parameter is empty. */
372 if (STREQ (command, "top") && strlen(temp) == 0)
373 temp = xstrdup (title ? title : "");
375 /* Docbook does not support @unnumbered at all. So we provide numbers
376 that other formats use. @appendix seems to be fine though, so we let
377 Docbook handle that as usual. */
378 if (docbook && enum_marker != APPENDIX_MAGIC)
380 if (section_alist[search_sectioning (cmd)].num == ENUM_SECT_NO
381 && section_alist[search_sectioning (cmd)].toc == TOC_YES)
382 xml_insert_element_with_attribute (xml_element (secname),
383 START, "label=\"%s\" xreflabel=\"%s\"",
384 handle_enum_increment (level, search_sectioning (cmd)),
385 text_expansion (temp));
386 else
387 xml_insert_element_with_attribute (xml_element (secname),
388 START, "label=\"%s\"",
389 handle_enum_increment (level, search_sectioning (cmd)));
391 else
392 xml_insert_element (xml_element (secname), START);
394 xml_insert_element (TITLE, START);
395 xml_open_section (level, secname);
396 execute_string ("%s", temp);
397 xml_insert_element (TITLE, END);
399 free (temp);
401 else
403 if (docbook)
405 if (level > 0)
406 xml_insert_element_with_attribute (xml_element (secname), START,
407 "renderas=\"sect%d\"", level);
408 else
409 xml_insert_element_with_attribute (xml_element (secname), START,
410 "renderas=\"other\"");
412 else
413 xml_insert_element (xml_element (secname), START);
415 get_rest_of_line (0, &temp);
416 execute_string ("%s", temp);
417 free (temp);
419 xml_insert_element (xml_element (secname), END);
422 else if (html)
423 sectioning_html (level, secname);
424 else
425 insert_and_underscore (level, secname);
429 /* Insert the text following input_text_offset up to the end of the line
430 in a new, separate paragraph. Directly underneath it, insert a
431 line of WITH_CHAR, the same length of the inserted text. */
432 void
433 insert_and_underscore (int level, char *cmd)
435 int i, len;
436 int index;
437 int old_no_indent;
438 unsigned char *starting_pos, *ending_pos;
439 char *temp;
440 char with_char = scoring_characters[level];
442 close_paragraph ();
443 filling_enabled = indented_fill = 0;
444 old_no_indent = no_indent;
445 no_indent = 1;
447 if (macro_expansion_output_stream && !executing_string)
448 append_to_expansion_output (input_text_offset + 1);
450 get_rest_of_line (0, &temp);
452 /* Use @settitle value if @top parameter is empty. */
453 if (STREQ (command, "top") && strlen(temp) == 0)
454 temp = xstrdup (title ? title : "");
456 starting_pos = output_paragraph + output_paragraph_offset;
458 /* Poor man's cache for section title. */
459 if (strlen (last_sectioning_title))
460 free (last_sectioning_title);
461 last_sectioning_title = xstrdup (temp);
463 index = search_sectioning (cmd);
464 if (index < 0)
466 /* should never happen, but a poor guy, named Murphy ... */
467 warning (_("Internal error (search_sectioning) `%s'!"), cmd);
468 return;
471 /* This is a bit tricky: we must produce "X.Y SECTION-NAME" in the
472 Info output and in TOC, but only SECTION-NAME in the macro-expanded
473 output. */
475 /* Step 1: produce "X.Y" and add it to Info output. */
476 add_word_args ("%s ", handle_enum_increment (level, index));
478 /* Step 2: add "SECTION-NAME" to both Info and macro-expanded output. */
479 if (macro_expansion_output_stream && !executing_string)
481 char *temp1 = xmalloc (2 + strlen (temp));
482 sprintf (temp1, "%s\n", temp);
483 remember_itext (input_text, input_text_offset);
484 me_execute_string (temp1);
485 free (temp1);
487 else
488 execute_string ("%s\n", temp);
490 /* Step 3: pluck "X.Y SECTION-NAME" from the output buffer and
491 insert it into the TOC. */
492 ending_pos = output_paragraph + output_paragraph_offset;
493 if (section_alist[index].toc == TOC_YES)
494 toc_add_entry (substring (starting_pos, ending_pos - 1),
495 level, current_node, NULL);
497 free (temp);
499 len = (ending_pos - starting_pos) - 1;
500 for (i = 0; i < len; i++)
501 add_char (with_char);
502 insert ('\n');
503 close_paragraph ();
504 filling_enabled = 1;
505 no_indent = old_no_indent;
508 /* Insert the text following input_text_offset up to the end of the
509 line as an HTML heading element of the appropriate `level' and
510 tagged as an anchor for the current node.. */
512 void
513 sectioning_html (int level, char *cmd)
515 static int toc_ref_count = 0;
516 int index;
517 int old_no_indent;
518 unsigned char *starting_pos, *ending_pos;
519 char *temp, *toc_anchor = NULL;
521 close_paragraph ();
522 filling_enabled = indented_fill = 0;
523 old_no_indent = no_indent;
524 no_indent = 1;
526 /* level 0 (chapter) is <h2>, and we go down from there. */
527 add_html_block_elt_args ("<h%d class=\"%s\">", level + 2, cmd);
529 /* If we are outside of any node, produce an anchor that
530 the TOC could refer to. */
531 if (!current_node || !*current_node)
533 static const char a_name[] = "<a name=\"";
535 starting_pos = output_paragraph + output_paragraph_offset;
536 add_word_args ("%sTOC%d\">", a_name, toc_ref_count++);
537 toc_anchor = substring (starting_pos + sizeof (a_name) - 1,
538 output_paragraph + output_paragraph_offset);
539 /* This must be added after toc_anchor is extracted, since
540 toc_anchor cannot include the closing </a>. For details,
541 see toc.c:toc_add_entry and toc.c:contents_update_html.
543 Also, the anchor close must be output before the section name
544 in case the name itself contains an anchor. */
545 add_word ("</a>");
547 starting_pos = output_paragraph + output_paragraph_offset;
549 if (macro_expansion_output_stream && !executing_string)
550 append_to_expansion_output (input_text_offset + 1);
552 get_rest_of_line (0, &temp);
554 /* Use @settitle value if @top parameter is empty. */
555 if (STREQ (command, "top") && strlen(temp) == 0)
556 temp = xstrdup (title ? title : "");
558 index = search_sectioning (cmd);
559 if (index < 0)
561 /* should never happen, but a poor guy, named Murphy ... */
562 warning (_("Internal error (search_sectioning) \"%s\"!"), cmd);
563 return;
566 /* Produce "X.Y" and add it to HTML output. */
568 char *title_number = handle_enum_increment (level, index);
569 if (strlen (title_number) > 0)
570 add_word_args ("%s ", title_number);
573 /* add the section name to both HTML and macro-expanded output. */
574 if (macro_expansion_output_stream && !executing_string)
576 remember_itext (input_text, input_text_offset);
577 me_execute_string (temp);
578 write_region_to_macro_output ("\n", 0, 1);
580 else
581 execute_string ("%s", temp);
583 ending_pos = output_paragraph + output_paragraph_offset;
585 /* Pluck ``X.Y SECTION-NAME'' from the output buffer and insert it
586 into the TOC. */
587 if (section_alist[index].toc == TOC_YES)
588 toc_add_entry (substring (starting_pos, ending_pos),
589 level, current_node, toc_anchor);
591 free (temp);
593 if (outstanding_node)
594 outstanding_node = 0;
596 add_word_args ("</h%d>", level + 2);
597 close_paragraph();
598 filling_enabled = 1;
599 no_indent = old_no_indent;
603 /* Shift the meaning of @section to @chapter. */
604 void
605 cm_raisesections (void)
607 discard_until ("\n");
608 section_alist_offset--;
611 /* Shift the meaning of @chapter to @section. */
612 void
613 cm_lowersections (void)
615 discard_until ("\n");
616 section_alist_offset++;
619 /* The command still works, but prints a warning message in addition. */
620 void
621 cm_ideprecated (int arg, int start, int end)
623 warning (_("%c%s is obsolete; use %c%s instead"),
624 COMMAND_PREFIX, command, COMMAND_PREFIX, command + 1);
625 sectioning_underscore (command + 1);
629 /* Treat this just like @unnumbered. The only difference is
630 in node defaulting. */
631 void
632 cm_top (void)
634 /* It is an error to have more than one @top. */
635 if (top_node_seen && strcmp (current_node, "Top") != 0)
637 TAG_ENTRY *tag = tag_table;
639 line_error (_("Node with %ctop as a section already exists"),
640 COMMAND_PREFIX);
642 while (tag)
644 if (tag->flags & TAG_FLAG_IS_TOP)
646 file_line_error (tag->filename, tag->line_no,
647 _("Here is the %ctop node"), COMMAND_PREFIX);
648 return;
650 tag = tag->next_ent;
653 else
655 top_node_seen = 1;
657 /* It is an error to use @top before using @node. */
658 if (!tag_table)
660 char *top_name;
662 get_rest_of_line (0, &top_name);
663 line_error (_("%ctop used before %cnode, defaulting to %s"),
664 COMMAND_PREFIX, COMMAND_PREFIX, top_name);
665 execute_string ("@node Top, , (dir), (dir)\n@top %s\n", top_name);
666 free (top_name);
667 return;
670 cm_unnumbered ();
672 /* The most recently defined node is the top node. */
673 tag_table->flags |= TAG_FLAG_IS_TOP;
675 /* Now set the logical hierarchical level of the Top node. */
677 int orig_offset = input_text_offset;
679 input_text_offset = search_forward (node_search_string, orig_offset);
681 if (input_text_offset > 0)
683 int this_section;
685 /* We have encountered a non-top node, so mark that one exists. */
686 non_top_node_seen = 1;
688 /* Move to the end of this line, and find out what the
689 sectioning command is here. */
690 while (input_text[input_text_offset] != '\n')
691 input_text_offset++;
693 if (input_text_offset < input_text_length)
694 input_text_offset++;
696 this_section = what_section (input_text + input_text_offset,
697 NULL);
699 /* If we found a sectioning command, then give the top section
700 a level of this section - 1. */
701 if (this_section != -1)
702 set_top_section_level (this_section - 1);
704 input_text_offset = orig_offset;
709 /* The remainder of the text on this line is a chapter heading. */
710 void
711 cm_chapter (void)
713 enum_marker = 0;
714 sectioning_underscore ("chapter");
717 /* The remainder of the text on this line is a section heading. */
718 void
719 cm_section (void)
721 sectioning_underscore ("section");
724 /* The remainder of the text on this line is a subsection heading. */
725 void
726 cm_subsection (void)
728 sectioning_underscore ("subsection");
731 /* The remainder of the text on this line is a subsubsection heading. */
732 void
733 cm_subsubsection (void)
735 sectioning_underscore ("subsubsection");
738 /* The remainder of the text on this line is an unnumbered heading. */
739 void
740 cm_unnumbered (void)
742 enum_marker = UNNUMBERED_MAGIC;
743 sectioning_underscore ("unnumbered");
746 /* The remainder of the text on this line is an unnumbered section heading. */
747 void
748 cm_unnumberedsec (void)
750 sectioning_underscore ("unnumberedsec");
753 /* The remainder of the text on this line is an unnumbered
754 subsection heading. */
755 void
756 cm_unnumberedsubsec (void)
758 sectioning_underscore ("unnumberedsubsec");
761 /* The remainder of the text on this line is an unnumbered
762 subsubsection heading. */
763 void
764 cm_unnumberedsubsubsec (void)
766 sectioning_underscore ("unnumberedsubsubsec");
769 /* The remainder of the text on this line is an appendix heading. */
770 void
771 cm_appendix (void)
773 /* Reset top level number so we start from Appendix A */
774 if (enum_marker != APPENDIX_MAGIC)
775 numbers [0] = 0;
776 enum_marker = APPENDIX_MAGIC;
777 sectioning_underscore ("appendix");
780 /* The remainder of the text on this line is an appendix section heading. */
781 void
782 cm_appendixsec (void)
784 sectioning_underscore ("appendixsec");
787 /* The remainder of the text on this line is an appendix subsection heading. */
788 void
789 cm_appendixsubsec (void)
791 sectioning_underscore ("appendixsubsec");
794 /* The remainder of the text on this line is an appendix
795 subsubsection heading. */
796 void
797 cm_appendixsubsubsec (void)
799 sectioning_underscore ("appendixsubsubsec");
802 /* Compatibility functions substitute for chapter, section, etc. */
803 void
804 cm_majorheading (void)
806 sectioning_underscore ("majorheading");
809 void
810 cm_chapheading (void)
812 sectioning_underscore ("chapheading");
815 void
816 cm_heading (void)
818 sectioning_underscore ("heading");
821 void
822 cm_subheading (void)
824 sectioning_underscore ("subheading");
827 void
828 cm_subsubheading (void)
830 sectioning_underscore ("subsubheading");