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)
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>. */
30 #include "sectioning.h"
33 static FLOAT_ELT
*float_stack
= NULL
;
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
;
45 new->shorttitle
= shorttitle
;
46 new->position
= position
;
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
);
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
),
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 ();
79 new->section_name
= "";
82 new->next
= float_stack
;
87 count_floats_of_type_in_chapter (char *type
, char *chapter
)
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
))
104 current_float_title (void)
106 return float_stack
->title
;
110 current_float_shorttitle (void)
112 return float_stack
->shorttitle
;
116 current_float_type (void)
118 return float_stack
->type
;
122 current_float_position (void)
124 return float_stack
->position
;
128 current_float_number (void)
130 return float_stack
->number
;
134 current_float_id (void)
136 return float_stack
->id
;
140 get_float_ref (char *id
)
142 FLOAT_ELT
*temp
= float_stack
;
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
);
159 float_type_exists (char *check_type
)
161 /* Check if the requested float_type exists in the floats stack. */
164 for (temp
= float_stack
; temp
; temp
= temp
->next
)
165 if (STREQ (temp
->type
, check_type
) && temp
->id
&& *temp
->id
)
172 cm_listoffloats (void)
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
)
183 if (handling_delayed_writes
&& !float_type_exists (float_type
))
184 warning (_("Requested float type `%s' not previously used"), float_type
);
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. */
202 sprintf (list_command
, "@%s %s", command
, float_type
);
203 register_delayed_write (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
;
213 insert_string ("<ul class=\"listoffloats\">\n");
217 insert_string ("* Menu:\n\n");
222 if (strlen (temp
->id
) > 0 && STREQ (float_type
, temp
->type
))
226 /* A bit of space for HTML reabality. */
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);
236 if (strlen (float_type
) > 0)
237 execute_string ("%s", float_type
);
239 if (strlen (temp
->id
) > 0)
241 if (strlen (float_type
) > 0)
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
);
258 add_html_block_elt ("</li>\n");
264 char *title
= expansion (temp
->title
, 0);
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. */
272 /* Chosen widths are to match what @printindex produces. */
276 /* We have only one auxiliary character, NULL. */
277 aux_chars_len
= sizeof ("");
282 /* We'll be adding an asterisk, followed by a space
283 and then a colon after the title, to construct a
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
)
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)
320 /* Advance to the whitespace. */
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. */
329 || (len
== number_len
&& strlen (title
) > 0))
330 len
= column_width
- sizeof ("...");
335 entry
= xmalloc (len
+ aux_chars_len
);
338 strcpy (entry
, "* ");
342 strcat (entry
, raw_entry
);
343 strcat (entry
, "...");
350 entry
= xmalloc (len
+ aux_chars_len
);
353 strcpy (entry
, "* ");
357 strcat (entry
, raw_entry
);
363 insert_string (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)
378 if (strlen (temp
->section
) > 0)
379 { /* We got your number. */
380 insert_string ((char *) _("See "));
381 insert_string (temp
->section
);
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 ("''");
393 insert_string (temp
->id
);
395 insert_string (".\n");
406 inhibit_paragraph_indentation
= 1;
407 insert_string ("</ul>\n\n");
412 /* Retain the original order of float stack. */
414 float_stack
= (FLOAT_ELT
*) reverse_list ((GENERIC_LIST
*) temp
);
418 /* Re-increment the line number, because get_rest_of_line
419 left us looking at the next line after the command. */
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;