1 /* sectioning.c -- all related stuff @chapter, @section... @contents
2 $Id: sectioning.c,v 1.12 1999/08/17 21:06:50 karl Exp $
4 Copyright (C) 1999 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)
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 Written by Karl Heinz Marbaise <kama@hippo.fido.de>. */
28 #include "sectioning.h"
30 /* See comment in sectioning.h. */
31 section_alist_type section_alist[] = {
32 { "unnumberedsubsubsec", 5, ENUM_SECT_NO, TOC_YES },
33 { "unnumberedsubsec", 4, ENUM_SECT_NO, TOC_YES },
34 { "unnumberedsec", 3, ENUM_SECT_NO, TOC_YES },
35 { "unnumbered", 2, ENUM_SECT_NO, TOC_YES },
37 { "appendixsubsubsec", 5, ENUM_SECT_APP, TOC_YES }, /* numbered like A.X.X.X */
38 { "appendixsubsec", 4, ENUM_SECT_APP, TOC_YES },
39 { "appendixsec", 3, ENUM_SECT_APP, TOC_YES },
40 { "appendixsection", 3, ENUM_SECT_APP, TOC_YES },
41 { "appendix", 2, ENUM_SECT_APP, TOC_YES },
43 { "subsubsec", 5, ENUM_SECT_YES, TOC_YES },
44 { "subsubsection", 5, ENUM_SECT_YES, TOC_YES },
45 { "subsection", 4, ENUM_SECT_YES, TOC_YES },
46 { "section", 3, ENUM_SECT_YES, TOC_YES },
47 { "chapter", 2, ENUM_SECT_YES, TOC_YES },
49 { "subsubheading", 5, ENUM_SECT_NO, TOC_NO },
50 { "subheading", 4, ENUM_SECT_NO, TOC_NO },
51 { "heading", 3, ENUM_SECT_NO, TOC_NO },
52 { "chapheading", 2, ENUM_SECT_NO, TOC_NO },
53 { "majorheading", 2, ENUM_SECT_NO, TOC_NO },
55 { "top", 1, ENUM_SECT_NO, TOC_YES },
59 /* The argument of @settitle, used for HTML. */
63 #define APPENDIX_MAGIC 1024
64 #define UNNUMBERED_MAGIC 2048
66 /* Number memory for every level @chapter, @section,
67 @subsection, @subsubsection. */
68 static int numbers [] = { 0, 0, 0, 0 };
70 /* enum_marker == APPENDIX_MAGIC then we are counting appendencies
71 enum_marker == UNNUMBERED_MAGIC then we are within unnumbered area.
72 Handling situations like this:
75 static int enum_marker = 0;
77 /* Organized by level commands. That is, "*" == chapter, "=" == section. */
78 static char *scoring_characters = "*=-.";
80 /* Amount to offset the name of sectioning commands to levels by. */
81 static int section_alist_offset = 0;
84 /* num == ENUM_SECT_NO means unnumbered (should never call this)
85 num == ENUM_SECT_YES means numbered
86 num == ENUM_SECT_APP means numbered like A.1 and so on */
88 get_sectioning_number (level, num)
92 static char s[100]; /* should ever be enough for 99.99.99.99
100 /* create enumeration in front of chapter, section, subsection and so on. */
101 for (i = 0; i < level; i++)
104 if ((i == 0) && (enum_marker == APPENDIX_MAGIC))
105 sprintf (p, "%c.", numbers[i] + 64); /* Should be changed to
108 sprintf (p, "%d.", numbers[i]);
111 /* the last number is never followed by a dot */
113 if ((num == ENUM_SECT_APP)
115 && (enum_marker == APPENDIX_MAGIC))
116 sprintf (p, _("Appendix %c "), numbers[i] + 64);
118 sprintf (p, "%d ", numbers[i]);
124 /* Set the level of @top to LEVEL. Return the old level of @top. */
126 set_top_section_level (level)
131 for (i = 0; section_alist[i].name; i++)
132 if (strcmp (section_alist[i].name, "top") == 0)
134 result = section_alist[i].level;
135 section_alist[i].level = level;
142 /* return the index of the given sectioning command in section_alist */
144 search_sectioning (text)
150 /* ignore the optional command prefix */
151 if (text[0] == COMMAND_PREFIX)
154 for (i = 0; (t = section_alist[i].name); i++)
156 if (strcmp (t, text) == 0)
164 /* Return an integer which identifies the type section present in TEXT. */
173 find_section_command:
174 for (j = 0; text[j] && cr_or_whitespace (text[j]); j++);
175 if (text[j] != COMMAND_PREFIX)
180 /* We skip @c, @comment, and @?index commands. */
181 if ((strncmp (text, "comment", strlen ("comment")) == 0) ||
182 (text[0] == 'c' && cr_or_whitespace (text[1])) ||
183 (strcmp (text + 1, "index") == 0))
185 while (*text++ != '\n');
186 goto find_section_command;
189 /* Handle italicized sectioning commands. */
193 for (j = 0; text[j] && !cr_or_whitespace (text[j]); j++);
195 temp = xmalloc (1 + j);
196 strncpy (temp, text, j);
199 index = search_sectioning (temp);
203 return_val = section_alist[index].level + section_alist_offset;
206 else if (return_val > 5)
215 sectioning_underscore (cmd)
222 temp = xmalloc (2 + strlen (cmd));
223 temp[0] = COMMAND_PREFIX;
224 strcpy (&temp[1], cmd);
225 level = what_section (temp);
233 sectioning_html (level, cmd);
236 character = scoring_characters[level];
237 insert_and_underscore (level, character, cmd);
241 /* insert_and_underscore and sectioning_html are the
242 only functions which call this.
243 I have created this, because it was exactly the same
244 code in both functions. */
246 handle_enum_increment (level, index)
250 /* special for unnumbered */
251 if (number_sections && section_alist[index].num == ENUM_SECT_NO)
254 && enum_marker != UNNUMBERED_MAGIC)
255 enum_marker = UNNUMBERED_MAGIC;
257 /* enumerate only things which are allowed */
258 if (number_sections && section_alist[index].num)
260 /* reset the marker if we get into enumerated areas */
261 if (section_alist[index].num == ENUM_SECT_YES
263 && enum_marker == UNNUMBERED_MAGIC)
265 /* This is special for appendix; if we got the first
266 time an appendix command then we are entering appendix.
267 Thats the point we have to start countint with A, B and so on. */
268 if (section_alist[index].num == ENUM_SECT_APP
270 && enum_marker != APPENDIX_MAGIC)
272 enum_marker = APPENDIX_MAGIC;
273 numbers [0] = 0; /* this means we start with Appendix A */
276 /* only increment counters if we are not in unnumbered
277 area. This handles situations like this:
278 @unnumbered .... This sets enum_marker to UNNUMBERED_MAGIC
280 if (enum_marker != UNNUMBERED_MAGIC)
284 /* reset all counters which are one level deeper */
285 for (i = level; i < 3; i++)
290 (get_sectioning_number (level, section_alist[index].num));
292 } /* if (number_sections)... */
298 /* Insert the text following input_text_offset up to the end of the line
299 in a new, separate paragraph. Directly underneath it, insert a
300 line of WITH_CHAR, the same length of the inserted text. */
302 insert_and_underscore (level, with_char, cmd)
310 unsigned char *starting_pos, *ending_pos;
314 filling_enabled = indented_fill = 0;
315 old_no_indent = no_indent;
318 if (macro_expansion_output_stream && !executing_string)
319 append_to_expansion_output (input_text_offset + 1);
321 get_rest_of_line (0, &temp);
322 starting_pos = output_paragraph + output_paragraph_offset;
324 index = search_sectioning (cmd);
327 /* should never happen, but a poor guy, named Murphy ... */
328 warning (_("Internal error (search_sectioning) \"%s\"!"), cmd);
332 /* This is a bit tricky: we must produce "X.Y SECTION-NAME" in the
333 Info output and in TOC, but only SECTION-NAME in the macro-expanded
336 /* Step 1: produce "X.Y" and add it to Info output. */
337 add_word (handle_enum_increment (level, index));
339 /* Step 2: add "SECTION-NAME" to both Info and macro-expanded output. */
340 if (macro_expansion_output_stream && !executing_string)
342 char *temp1 = xmalloc (2 + strlen (temp));
343 sprintf (temp1, "%s\n", temp);
344 remember_itext (input_text, input_text_offset);
345 me_execute_string (temp1);
349 execute_string ("%s\n", temp);
351 /* Step 3: pluck "X.Y SECTION-NAME" from the output buffer and
352 insert it into the TOC. */
353 ending_pos = output_paragraph + output_paragraph_offset;
354 if (section_alist[index].toc == TOC_YES)
355 toc_add_entry (substring (starting_pos, ending_pos - 1),
356 level, current_node, NULL);
360 len = (ending_pos - starting_pos) - 1;
361 for (i = 0; i < len; i++)
362 add_char (with_char);
366 no_indent = old_no_indent;
369 /* Insert the text following input_text_offset up to the end of the
370 line as an HTML heading element of the appropriate `level' and
371 tagged as an anchor for the current node.. */
373 sectioning_html (level, cmd)
377 static int toc_ref_count = 0;
380 unsigned char *starting_pos, *ending_pos;
381 char *temp, *toc_anchor = NULL;
384 filling_enabled = indented_fill = 0;
385 old_no_indent = no_indent;
388 add_word_args ("<h%d>", level + 1); /* level 0 is <h1> */
390 /* If we are outside of any node, produce an anchor that
391 the TOC could refer to. */
392 if (!current_node || !*current_node)
394 starting_pos = output_paragraph + output_paragraph_offset;
395 add_word_args ("<a name=\"TOC%d\">", toc_ref_count++);
396 toc_anchor = substring (starting_pos + 9,
397 output_paragraph + output_paragraph_offset);
399 starting_pos = output_paragraph + output_paragraph_offset;
401 if (macro_expansion_output_stream && !executing_string)
402 append_to_expansion_output (input_text_offset + 1);
404 get_rest_of_line (0, &temp);
406 index = search_sectioning (cmd);
409 /* should never happen, but a poor guy, named Murphy ... */
410 warning (_("Internal error (search_sectioning) \"%s\"!"), cmd);
414 /* Produce "X.Y" and add it to HTML output. */
415 add_word (handle_enum_increment (level, index));
417 /* add the section name to both HTML and macro-expanded output. */
418 if (macro_expansion_output_stream && !executing_string)
420 remember_itext (input_text, input_text_offset);
421 me_execute_string (temp);
422 write_region_to_macro_output ("\n", 0, 1);
425 execute_string ("%s", temp);
427 ending_pos = output_paragraph + output_paragraph_offset;
429 /* Pluck ``X.Y SECTION-NAME'' from the output buffer and insert it
431 if (section_alist[index].toc == TOC_YES)
432 toc_add_entry (substring (starting_pos, ending_pos),
433 level, current_node, toc_anchor);
437 if (outstanding_node)
438 outstanding_node = 0;
440 add_word_args ("</h%d>", level+1);
443 no_indent = old_no_indent;
447 /* Shift the meaning of @section to @chapter. */
451 discard_until ("\n");
452 section_alist_offset--;
455 /* Shift the meaning of @chapter to @section. */
459 discard_until ("\n");
460 section_alist_offset++;
463 /* The command still works, but prints a warning message in addition. */
465 cm_ideprecated (arg, start, end)
468 warning (_("%c%s is obsolete; use %c%s instead"),
469 COMMAND_PREFIX, command, COMMAND_PREFIX, command + 1);
470 sectioning_underscore (command + 1);
474 /* Treat this just like @unnumbered. The only difference is
475 in node defaulting. */
479 /* It is an error to have more than one @top. */
480 if (top_node_seen && strcmp (current_node, "Top") != 0)
482 TAG_ENTRY *tag = tag_table;
484 line_error (_("Node with %ctop as a section already exists"),
489 if (tag->flags & TAG_FLAG_IS_TOP)
491 int old_line_number = line_number;
492 char *old_input_filename = input_filename;
494 line_number = tag->line_no;
495 input_filename = tag->filename;
496 line_error (_("Here is the %ctop node"), COMMAND_PREFIX);
497 input_filename = old_input_filename;
498 line_number = old_line_number;
506 TAG_ENTRY *top_node = find_node ("Top");
509 /* It is an error to use @top before you have used @node. */
514 get_rest_of_line (0, &top_name);
515 line_error (_("%ctop used before %cnode, defaulting to %s"),
516 COMMAND_PREFIX, COMMAND_PREFIX, top_name);
517 execute_string ("@node Top, , (dir), (dir)\n@top %s\n", top_name);
521 else if (html && splitting)
523 char *next = top_node ? top_node->next : NULL;
528 add_word (_("Next:"));
529 add_word ("<a rel=next href=\"");
530 add_anchor_name (next, 1);
532 execute_string (next);
539 /* The most recently defined node is the top node. */
540 tag_table->flags |= TAG_FLAG_IS_TOP;
542 /* Now set the logical hierarchical level of the Top node. */
544 int orig_offset = input_text_offset;
546 input_text_offset = search_forward (node_search_string, orig_offset);
548 if (input_text_offset > 0)
552 /* We have encountered a non-top node, so mark that one exists. */
553 non_top_node_seen = 1;
555 /* Move to the end of this line, and find out what the
556 sectioning command is here. */
557 while (input_text[input_text_offset] != '\n')
560 if (input_text_offset < input_text_length)
563 this_section = what_section (input_text + input_text_offset);
565 /* If we found a sectioning command, then give the top section
566 a level of this section - 1. */
567 if (this_section != -1)
568 set_top_section_level (this_section - 1);
570 input_text_offset = orig_offset;
575 /* The remainder of the text on this line is a chapter heading. */
579 sectioning_underscore ("chapter");
582 /* The remainder of the text on this line is a section heading. */
586 sectioning_underscore ("section");
589 /* The remainder of the text on this line is a subsection heading. */
593 sectioning_underscore ("subsection");
596 /* The remainder of the text on this line is a subsubsection heading. */
600 sectioning_underscore ("subsubsection");
603 /* The remainder of the text on this line is an unnumbered heading. */
607 sectioning_underscore ("unnumbered");
610 /* The remainder of the text on this line is an unnumbered section heading. */
614 sectioning_underscore ("unnumberedsec");
617 /* The remainder of the text on this line is an unnumbered
618 subsection heading. */
620 cm_unnumberedsubsec ()
622 sectioning_underscore ("unnumberedsubsec");
625 /* The remainder of the text on this line is an unnumbered
626 subsubsection heading. */
628 cm_unnumberedsubsubsec ()
630 sectioning_underscore ("unnumberedsubsubsec");
633 /* The remainder of the text on this line is an appendix heading. */
637 sectioning_underscore ("appendix");
640 /* The remainder of the text on this line is an appendix section heading. */
644 sectioning_underscore ("appendixsec");
647 /* The remainder of the text on this line is an appendix subsection heading. */
651 sectioning_underscore ("appendixsubsec");
654 /* The remainder of the text on this line is an appendix
655 subsubsection heading. */
657 cm_appendixsubsubsec ()
659 sectioning_underscore ("appendixsubsubsec");
662 /* Compatibility functions substitute for chapter, section, etc. */
666 sectioning_underscore ("majorheading");
672 sectioning_underscore ("chapheading");
678 sectioning_underscore ("heading");
684 sectioning_underscore ("subheading");
690 sectioning_underscore ("subsubheading");