1 /* footnote.c -- footnotes for Texinfo.
2 $Id: footnote.c,v 1.13 2002/03/02 15:05:21 karl Exp $
4 Copyright (C) 1998, 99, 2002 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 Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 /* Nonzero means that the footnote style for this document was set on
27 the command line, which overrides any other settings. */
28 int footnote_style_preset = 0;
30 /* The current footnote number in this node. Each time a new node is
31 started this is reset to 1. */
32 int current_footnote_number = 1;
34 /* Nonzero means we automatically number footnotes with no specified marker. */
35 int number_footnotes = 1;
37 /* Nonzero means we are currently outputting footnotes. */
38 int already_outputting_pending_notes = 0;
41 /* Footnotes can be handled in one of two ways:
44 Make them look like followed references, with the reference
45 destinations in a makeinfo manufactured node or,
47 Make them appear at the bottom of the node that they originally
50 #define separate_node 0
53 int footnote_style = end_node;
54 int first_footnote_this_node = 1;
55 int footnote_count = 0;
57 /* Set the footnote style based on the style identifier in STRING. */
59 set_footnote_style (string)
62 if (strcasecmp (string, "separate") == 0)
63 footnote_style = separate_node;
64 else if (strcasecmp (string, "end") == 0)
65 footnote_style = end_node;
77 get_rest_of_line (1, &arg);
79 /* If set on command line, do not change the footnote style. */
80 if (!footnote_style_preset && set_footnote_style (arg) != 0)
81 line_error (_("Bad argument to %c%s"), COMMAND_PREFIX, command);
94 FN *pending_notes = NULL;
96 /* A method for remembering footnotes. Note that this list gets output
97 at the end of the current node. */
99 remember_note (marker, note)
102 FN *temp = xmalloc (sizeof (FN));
104 temp->marker = xstrdup (marker);
105 temp->note = xstrdup (note);
106 temp->next = pending_notes;
107 temp->number = current_footnote_number;
108 pending_notes = temp;
112 /* How to get rid of existing footnotes. */
114 free_pending_notes ()
118 while ((temp = pending_notes))
122 pending_notes = pending_notes->next;
125 first_footnote_this_node = 1;
127 current_footnote_number = 1; /* for html */
130 /* What to do when you see a @footnote construct. */
132 /* Handle a "footnote".
133 footnote *{this is a footnote}
134 where "*" is the (optional) marker character for this note. */
141 get_until ("{", &marker);
142 canon_white (marker);
144 if (macro_expansion_output_stream && !executing_string)
145 append_to_expansion_output (input_text_offset + 1); /* include the { */
147 /* Read the argument in braces. */
148 if (curchar () != '{')
150 line_error (_("`%c%s' needs an argument `{...}', not just `%s'"),
151 COMMAND_PREFIX, command, marker);
159 int loc = ++input_text_offset;
163 if (loc == input_text_length)
165 line_error (_("No closing brace for footnote `%s'"), marker);
169 if (input_text[loc] == '{')
171 else if (input_text[loc] == '}')
173 else if (input_text[loc] == '\n')
179 len = (loc - input_text_offset) - 1;
180 note = xmalloc (len + 1);
181 memcpy (note, &input_text[input_text_offset], len);
183 input_text_offset = loc;
186 /* Must write the macro-expanded argument to the macro expansion
187 output stream. This is like the case in index_add_arg. */
188 if (macro_expansion_output_stream && !executing_string)
190 /* Calling me_execute_string on a lone } provokes an error, since
191 as far as the reader knows there is no matching {. We wrote
192 the { above in the call to append_to_expansion_output. */
193 me_execute_string_keep_state (note, "}");
196 if (!current_node || !*current_node)
198 line_error (_("Footnote defined without parent node"));
204 /* output_pending_notes is non-reentrant (it uses a global data
205 structure pending_notes, which it frees before it returns), and
206 TeX doesn't grok footnotes inside footnotes anyway. Disallow
208 if (already_outputting_pending_notes)
210 line_error (_("Footnotes inside footnotes are not allowed"));
220 if (number_footnotes)
222 marker = xmalloc (10);
223 sprintf (marker, "%d", current_footnote_number);
226 marker = xstrdup ("*");
230 xml_insert_footnote (note);
233 remember_note (marker, note);
235 /* fixme: html: footnote processing needs work; we currently ignore
236 the style requested; we could clash with a node name of the form
237 `fn-<n>', though that's unlikely. */
240 add_html_elt ("<a rel=footnote href=");
241 add_word_args ("\"#fn-%d\"><sup>%s</sup></a>",
242 current_footnote_number, marker);
245 /* Your method should at least insert MARKER. */
246 switch (footnote_style)
249 add_word_args ("(%s)", marker);
250 execute_string (" (*note %s-Footnote-%d::)",
251 current_node, current_footnote_number);
252 if (first_footnote_this_node)
254 char *temp_string, *expanded_ref;
256 temp_string = xmalloc (strlen (current_node)
257 + strlen ("-Footnotes") + 1);
259 strcpy (temp_string, current_node);
260 strcat (temp_string, "-Footnotes");
261 expanded_ref = expansion (temp_string, 0);
262 remember_node_reference (expanded_ref, line_number,
266 first_footnote_this_node = 0;
271 add_word_args ("(%s)", marker);
277 current_footnote_number++;
283 /* Output the footnotes. We are at the end of the current node. */
285 output_pending_notes ()
287 FN *footnote = pending_notes;
293 { /* The type= attribute is used just in case some weirdo browser
294 out there doesn't use numbers by default. Since we rely on the
295 browser to produce the footnote numbers, we need to make sure
296 they ARE indeed numbers. Pre-HTML4 browsers seem to not care. */
297 add_word ("<hr><h4>");
298 add_word (_("Footnotes"));
299 add_word ("</h4>\n<ol type=\"1\">\n");
302 switch (footnote_style)
306 char *old_current_node = current_node;
307 char *old_command = xstrdup (command);
309 already_outputting_pending_notes++;
310 execute_string ("%cnode %s-Footnotes,,,%s\n",
311 COMMAND_PREFIX, current_node, current_node);
312 already_outputting_pending_notes--;
313 current_node = old_current_node;
315 command = old_command;
321 in_fixed_width_font++;
322 /* This string should be translated according to the
323 @documentlanguage, not the current LANG. We can't do that
324 yet, so leave it in English. */
325 execute_string ("---------- Footnotes ----------\n\n");
326 in_fixed_width_font--;
330 /* Handle the footnotes in reverse order. */
332 FN **array = xmalloc ((footnote_count + 1) * sizeof (FN *));
333 array[footnote_count] = NULL;
335 while (--footnote_count > -1)
337 array[footnote_count] = footnote;
338 footnote = footnote->next;
344 while ((footnote = array[++footnote_count]))
348 /* Make the text of every footnote begin a separate paragraph. */
349 add_word_args ("<li><a name=\"fn-%d\"></a>\n<p>",
351 already_outputting_pending_notes++;
352 execute_string ("%s", footnote->note);
353 already_outputting_pending_notes--;
358 char *old_current_node = current_node;
359 char *old_command = xstrdup (command);
361 already_outputting_pending_notes++;
362 execute_string ("%canchor{%s-Footnote-%d}(%s) %s",
363 COMMAND_PREFIX, current_node, footnote->number,
364 footnote->marker, footnote->note);
365 already_outputting_pending_notes--;
366 current_node = old_current_node;
368 command = old_command;
375 add_word ("</ol><hr>");
380 free_pending_notes ();