No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / texinfo / makeinfo / float.c
blob0c349b73c4514cde6c4ed4f8073df49f9df8dfbc
1 /* $NetBSD$ */
3 /* float.c -- float environment functions.
4 Id: float.c,v 1.8 2004/07/05 22:23:22 karl Exp
6 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 Originally written by Alper Ersoy <dirt@gtk.org>. */
24 #include "system.h"
25 #include "makeinfo.h"
26 #include "cmds.h"
27 #include "files.h"
28 #include "float.h"
29 #include "html.h"
30 #include "sectioning.h"
31 #include "xml.h"
33 static FLOAT_ELT *float_stack = NULL;
35 void
36 add_new_float (char *id, char *title, char *shorttitle,
37 char *type, char *position)
39 FLOAT_ELT *new = xmalloc (sizeof (FLOAT_ELT));
40 unsigned long num_len;
42 new->id = id;
43 new->type = type;
44 new->title = title;
45 new->shorttitle = shorttitle;
46 new->position = position;
47 new->title_used = 0;
48 new->defining_line = line_number - 1;
50 new->number = current_chapter_number ();
51 /* Append dot if not @unnumbered. */
52 num_len = strlen (new->number);
53 if (num_len > 0)
55 new->number = xrealloc (new->number, num_len + 1 + 1);
56 new->number[num_len] = '.';
57 new->number[num_len+1] = '\0';
60 { /* Append the current float number. */
61 unsigned len = strlen (new->number) + 21; /* that's 64 bits */
62 char *s = xmalloc (len + 1);
64 sprintf (s, "%s%d", new->number,
65 count_floats_of_type_in_chapter (text_expansion (type),
66 new->number) + 1);
67 free (new->number);
68 new->number = xstrdup (s);
71 /* Plain text output needs sectioning number and its title,
72 when listing floats. */
73 if (!html && !xml && no_headers)
75 new->section = current_sectioning_number ();
76 if (strlen (new->section) == 0)
77 new->section_name = current_sectioning_name ();
78 else
79 new->section_name = "";
82 new->next = float_stack;
83 float_stack = new;
86 int
87 count_floats_of_type_in_chapter (char *type, char *chapter)
89 int i = 0;
90 int l = strlen (chapter);
91 FLOAT_ELT *temp = float_stack;
93 while (temp && strncmp (temp->number, chapter, l) == 0)
95 if (strlen (temp->id) > 0 && STREQ (text_expansion (temp->type), type))
96 i++;
97 temp = temp->next;
100 return i;
103 char *
104 current_float_title (void)
106 return float_stack->title;
109 char *
110 current_float_shorttitle (void)
112 return float_stack->shorttitle;
115 char *
116 current_float_type (void)
118 return float_stack->type;
121 char *
122 current_float_position (void)
124 return float_stack->position;
127 char *
128 current_float_number (void)
130 return float_stack->number;
133 char *
134 current_float_id (void)
136 return float_stack->id;
139 char *
140 get_float_ref (char *id)
142 FLOAT_ELT *temp = float_stack;
144 while (temp)
146 if (STREQ (id, temp->id))
148 char *s = xmalloc (strlen (temp->type) + strlen (temp->number) + 2);
149 sprintf (s, "%s %s", temp->type, temp->number);
150 return s;
152 temp = temp->next;
155 return NULL;
158 static int
159 float_type_exists (char *check_type)
161 /* Check if the requested float_type exists in the floats stack. */
162 FLOAT_ELT *temp;
164 for (temp = float_stack; temp; temp = temp->next)
165 if (STREQ (temp->type, check_type) && temp->id && *temp->id)
166 return 1;
168 return 0;
171 void
172 cm_listoffloats (void)
174 char *float_type;
175 get_rest_of_line (1, &float_type);
177 /* get_rest_of_line increments the line number by one,
178 so to make warnings/errors point to the correct line,
179 we decrement the line_number again. */
180 if (!handling_delayed_writes)
181 line_number--;
183 if (handling_delayed_writes && !float_type_exists (float_type))
184 warning (_("Requested float type `%s' not previously used"), float_type);
186 if (xml)
188 xml_insert_element_with_attribute (LISTOFFLOATS, START,
189 "type=\"%s\"", text_expansion (float_type));
190 xml_insert_element (LISTOFFLOATS, END);
192 else if (!handling_delayed_writes)
194 int command_len = sizeof ("@ ") + strlen (command) + strlen (float_type);
195 char *list_command = xmalloc (command_len + 1);
197 /* These are for the text following @listoffloats command.
198 Handling them with delayed writes is too late. */
199 close_paragraph ();
200 cm_noindent ();
202 sprintf (list_command, "@%s %s", command, float_type);
203 register_delayed_write (list_command);
204 free (list_command);
206 else if (float_type_exists (float_type))
208 FLOAT_ELT *temp = (FLOAT_ELT *) reverse_list
209 ((GENERIC_LIST *) float_stack);
210 FLOAT_ELT *new_start = temp;
212 if (html)
213 insert_string ("<ul class=\"listoffloats\">\n");
214 else
216 if (!no_headers)
217 insert_string ("* Menu:\n\n");
220 while (temp)
222 if (strlen (temp->id) > 0 && STREQ (float_type, temp->type))
224 if (html)
226 /* A bit of space for HTML reabality. */
227 insert_string (" ");
228 add_html_block_elt ("<li>");
230 /* Simply relying on @ref command doesn't work here, because
231 commas in the caption may confuse the argument parsing. */
232 add_word ("<a href=\"");
233 add_anchor_name (temp->id, 1);
234 add_word ("\">");
236 if (strlen (float_type) > 0)
237 execute_string ("%s", float_type);
239 if (strlen (temp->id) > 0)
241 if (strlen (float_type) > 0)
242 add_char (' ');
244 add_word (temp->number);
247 if (strlen (temp->title) > 0)
249 if (strlen (float_type) > 0
250 || strlen (temp->id) > 0)
251 insert_string (": ");
253 execute_string ("%s", temp->title);
256 add_word ("</a>");
258 add_html_block_elt ("</li>\n");
260 else
262 char *entry;
263 char *raw_entry;
264 char *title = expansion (temp->title, 0);
266 int len;
267 int aux_chars_len; /* these are asterisk, colon, etc. */
268 int column_width; /* width of the first column in menus. */
269 int number_len; /* length of Figure X.Y: etc. */
270 int i = 0;
272 /* Chosen widths are to match what @printindex produces. */
273 if (no_headers)
275 column_width = 43;
276 /* We have only one auxiliary character, NULL. */
277 aux_chars_len = sizeof ("");
279 else
281 column_width = 37;
282 /* We'll be adding an asterisk, followed by a space
283 and then a colon after the title, to construct a
284 proper menu item. */
285 aux_chars_len = sizeof ("* :");
288 /* Allocate enough space for possible expansion later. */
289 raw_entry = (char *) xmalloc (strlen (float_type)
290 + strlen (temp->number) + strlen (title)
291 + sizeof (": "));
293 sprintf (raw_entry, "%s %s", float_type, temp->number);
295 if (strlen (title) > 0)
296 strcat (raw_entry, ": ");
298 number_len = strlen (raw_entry);
300 len = strlen (title) + strlen (raw_entry);
302 /* If we have a @shortcaption, try it if @caption is
303 too long to fit on a line. */
304 if (len + aux_chars_len > column_width
305 && strlen (temp->shorttitle) > 0)
306 title = expansion (temp->shorttitle, 0);
308 strcat (raw_entry, title);
309 len = strlen (raw_entry);
311 if (len + aux_chars_len > column_width)
312 { /* Shorten long titles by looking for a space before
313 column_width - strlen (" ..."). */
314 /* -1 is for NULL, which is already in aux_chars_len. */
315 aux_chars_len += sizeof ("...") - 1;
316 len = column_width - aux_chars_len;
317 while (raw_entry[len] != ' ' && len >= 0)
318 len--;
320 /* Advance to the whitespace. */
321 len++;
323 /* If we are at the end of, say, Figure X.Y:, but
324 we have a title, then this means title does not
325 contain any whitespaces. Or it may be that we
326 went as far as the beginning. Just print as much
327 as possible of the title. */
328 if (len == 0
329 || (len == number_len && strlen (title) > 0))
330 len = column_width - sizeof ("...");
332 /* Break here. */
333 raw_entry[len] = 0;
335 entry = xmalloc (len + aux_chars_len);
337 if (!no_headers)
338 strcpy (entry, "* ");
339 else
340 entry[0] = 0;
342 strcat (entry, raw_entry);
343 strcat (entry, "...");
345 if (!no_headers)
346 strcat (entry, ":");
348 else
350 entry = xmalloc (len + aux_chars_len);
352 if (!no_headers)
353 strcpy (entry, "* ");
354 else
355 entry[0] = 0;
357 strcat (entry, raw_entry);
359 if (!no_headers)
360 strcat (entry, ":");
363 insert_string (entry);
365 i = strlen (entry);
366 /* We insert space chars until ``column_width + four spaces''
367 is reached, to make the layout the same with what we produce
368 for @printindex. This is of course not obligatory, though
369 easier on the eye. -1 is for NULL. */
370 while (i < column_width + sizeof (" ") - 1)
372 insert (' ');
373 i++;
376 if (no_headers)
378 if (strlen (temp->section) > 0)
379 { /* We got your number. */
380 insert_string ((char *) _("See "));
381 insert_string (temp->section);
383 else
384 { /* Sigh, @float in an @unnumbered. :-\ */
385 insert_string ("\n ");
386 insert_string ((char *) _("See "));
387 insert_string ("``");
388 insert_string (expansion (temp->section_name, 0));
389 insert_string ("''");
392 else
393 insert_string (temp->id);
395 insert_string (".\n");
397 free (entry);
398 free (title);
401 temp = temp->next;
404 if (html)
406 inhibit_paragraph_indentation = 1;
407 insert_string ("</ul>\n\n");
409 else
410 insert ('\n');
412 /* Retain the original order of float stack. */
413 temp = new_start;
414 float_stack = (FLOAT_ELT *) reverse_list ((GENERIC_LIST *) temp);
417 free (float_type);
418 /* Re-increment the line number, because get_rest_of_line
419 left us looking at the next line after the command. */
420 line_number++;
424 current_float_used_title (void)
426 return float_stack->title_used;
429 void current_float_set_title_used (void)
431 float_stack->title_used = 1;