3 /* defun.c -- @defun and friends.
4 Id: defun.c,v 1.11 2004/04/11 17:56:46 karl Exp
6 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "insertion.h"
32 #define DEFUN_SELF_DELIMITING(c) \
33 ((c) == '(' || (c) == ')' || (c) == '[' || (c) == ']')
35 struct token_accumulator
43 initialize_token_accumulator (struct token_accumulator
*accumulator
)
45 accumulator
->length
= 0;
46 accumulator
->index
= 0;
47 accumulator
->tokens
= NULL
;
51 accumulate_token (struct token_accumulator
*accumulator
, char *token
)
53 if (accumulator
->index
>= accumulator
->length
)
55 accumulator
->length
+= 10;
56 accumulator
->tokens
= xrealloc (accumulator
->tokens
,
57 (accumulator
->length
* sizeof (char *)));
59 accumulator
->tokens
[accumulator
->index
] = token
;
60 accumulator
->index
+= 1;
63 /* Given STRING_POINTER pointing at an open brace, skip forward and return a
64 pointer to just past the matching close brace. */
66 scan_group_in_string (char **string_pointer
)
68 char *scan_string
= (*string_pointer
) + 1;
69 unsigned int level
= 1;
70 int started_command
= 0;
77 *string_pointer
= scan_string
;
83 /* Tweak line_number to compensate for fact that
84 we gobbled the whole line before coming here. */
86 line_error (_("Missing `}' in @def arg"));
88 *string_pointer
= scan_string
- 1;
92 if (c
== '{' && !started_command
)
94 if (c
== '}' && !started_command
)
97 /* remember if at @. */
98 started_command
= (c
== '@' && !started_command
);
102 /* Return a list of tokens from the contents of STRING.
103 Commands and brace-delimited groups count as single tokens.
104 Contiguous whitespace characters are converted to a token
105 consisting of a single space. */
107 args_from_string (char *string
)
109 struct token_accumulator accumulator
;
110 char *token_start
, *token_end
;
111 char *scan_string
= string
;
113 initialize_token_accumulator (&accumulator
);
116 { /* Replace arbitrary whitespace by a single space. */
117 if (whitespace (*scan_string
))
120 while (whitespace (*scan_string
))
122 accumulate_token ((&accumulator
), (xstrdup (" ")));
126 /* Commands count as single tokens. */
127 if (*scan_string
== COMMAND_PREFIX
)
129 token_start
= scan_string
;
131 if (self_delimiting (*scan_string
))
140 if ((c
== 0) || (c
== '{') || (whitespace (c
)))
147 if (*scan_string
== '{')
149 char *s
= scan_string
;
150 (void) scan_group_in_string (&s
);
154 token_end
= scan_string
;
157 /* Parentheses and brackets are self-delimiting. */
158 else if (DEFUN_SELF_DELIMITING (*scan_string
))
160 token_start
= scan_string
;
162 token_end
= scan_string
;
165 /* Open brace introduces a group that is a single token. */
166 else if (*scan_string
== '{')
168 char *s
= scan_string
;
169 int balanced
= scan_group_in_string (&s
);
171 token_start
= scan_string
+ 1;
173 token_end
= balanced
? (scan_string
- 1) : scan_string
;
176 /* Make commas separate tokens so to differentiate them from
177 parameter types in XML output. */
178 else if (*scan_string
== ',')
180 token_start
= scan_string
;
182 token_end
= scan_string
;
185 /* Otherwise a token is delimited by whitespace, parentheses,
186 brackets, or braces. A token is also ended by a command. */
189 token_start
= scan_string
;
197 /* Do not back up if we're looking at a }; since the only
198 valid }'s are those matched with {'s, we want to give
199 an error. If we back up, we go into an infinite loop. */
200 if (!c
|| whitespace (c
) || DEFUN_SELF_DELIMITING (c
)
207 /* End token if we are looking at a comma, as commas are
215 /* If we encounter a command embedded within a token,
216 then end the token. */
217 if (c
== COMMAND_PREFIX
)
223 token_end
= scan_string
;
226 accumulate_token (&accumulator
, substring (token_start
, token_end
));
228 accumulate_token (&accumulator
, NULL
);
229 return accumulator
.tokens
;
233 process_defun_args (char **defun_args
, int auto_var_p
)
235 int pending_space
= 0;
239 xml_process_defun_args (defun_args
, auto_var_p
);
245 char *defun_arg
= *defun_args
++;
247 if (defun_arg
== NULL
)
250 if (defun_arg
[0] == ' ')
262 if (DEFUN_SELF_DELIMITING (defun_arg
[0]))
264 /* Within @deffn and friends, texinfo.tex makes parentheses
265 sans serif and brackets bold. We use roman instead. */
267 insert_html_tag (START
, "");
269 add_char (defun_arg
[0]);
272 insert_html_tag (END
, "");
274 /* else if (defun_arg[0] == '&' || defun_arg[0] == COMMAND_PREFIX) */
275 /* execute_string ("%s", defun_arg); */
276 /* else if (auto_var_p) */
277 /* execute_string ("%s", defun_arg); */
279 execute_string ("%s", defun_arg
);
284 next_nonwhite_defun_arg (char ***arg_pointer
)
286 char **scan
= (*arg_pointer
);
287 char *arg
= (*scan
++);
289 if ((arg
!= 0) && (*arg
== ' '))
297 return (arg
== 0) ? "" : arg
;
301 /* This is needed also in insertion.c. */
304 get_base_type (int type
)
309 case defivar
: base_type
= defcv
; break;
310 case defmac
: base_type
= deffn
; break;
311 case defmethod
: base_type
= defop
; break;
312 case defopt
: base_type
= defvr
; break;
313 case defspec
: base_type
= deffn
; break;
314 case deftypecv
: base_type
= deftypecv
; break;
315 case deftypefun
: base_type
= deftypefn
; break;
316 case deftypeivar
: base_type
= deftypeivar
; break;
317 case deftypemethod
: base_type
= deftypemethod
; break;
318 case deftypeop
: base_type
= deftypeop
; break;
319 case deftypevar
: base_type
= deftypevr
; break;
320 case defun
: base_type
= deffn
; break;
321 case defvar
: base_type
= defvr
; break;
330 /* Make the defun type insertion.
331 TYPE says which insertion this is.
332 X_P, if nonzero, says not to start a new insertion. */
334 defun_internal (int type
, int x_p
)
337 char **defun_args
, **scan_args
;
338 const char *category
;
340 char *type_name
= NULL
;
341 char *type_name2
= NULL
;
346 /* The @def.. line is the only place in Texinfo where you are
347 allowed to use unquoted braces that don't delimit arguments of
348 a command or a macro; in any other place it will trigger an
349 error message from the reader loop. The special handling of
350 this case inside `args_from_string' is an extra special hack
351 which allows this. The side effect is that if we try to expand
352 the rest of the line below, the recursive reader loop will
353 signal an error if there are brace-delimited arguments on that line.
355 The best solution to this would be to change the syntax of
356 @def.. commands so that it doesn't violate Texinfo's own rules.
357 But it's probably too late for this now, as it will break a lot
360 Unfortunately, this means that you can't call macros, use @value, etc.
361 inside @def.. commands, sigh. */
362 get_rest_of_line (0, &line
);
364 /* Basic line continuation. If a line ends with \s*@\s* concatanate
367 char *next_line
, *new_line
;
371 i
= strlen (line
) - 1;
373 if (line
[i
] == '@' && line
[i
-1] != '@')
375 get_rest_of_line (0, &next_line
);
376 new_line
= (char *) xmalloc (i
+ strlen (next_line
) + 2);
377 strncpy (new_line
, line
, i
);
380 strcat (new_line
, " ");
381 strcat (new_line
, next_line
);
382 line
= xstrdup (new_line
);
386 goto line_continuation
;
390 defun_args
= (args_from_string (line
));
394 scan_args
= defun_args
;
396 /* Get base type and category string. */
397 base_type
= get_base_type (type
);
399 /* xx all these const strings should be determined upon
400 documentlanguage argument and NOT via gettext (kama). */
405 category
= _("Function");
408 category
= _("Macro");
411 category
= _("Special Form");
415 category
= _("Variable");
418 category
= _("User Option");
422 category
= _("Instance Variable");
426 category
= _("Method");
429 category
= next_nonwhite_defun_arg (&scan_args
);
433 /* The class name. */
434 if ((base_type
== deftypecv
)
435 || (base_type
== deftypefn
)
436 || (base_type
== deftypevr
)
437 || (base_type
== defcv
)
438 || (base_type
== defop
)
439 || (base_type
== deftypeivar
)
440 || (base_type
== deftypemethod
)
441 || (base_type
== deftypeop
)
443 type_name
= next_nonwhite_defun_arg (&scan_args
);
445 /* The type name for typed languages. */
446 if ((base_type
== deftypecv
)
447 || (base_type
== deftypeivar
)
448 || (base_type
== deftypemethod
)
449 || (base_type
== deftypeop
)
451 type_name2
= next_nonwhite_defun_arg (&scan_args
);
453 /* The function or whatever that's actually being defined. */
454 defined_name
= next_nonwhite_defun_arg (&scan_args
);
456 /* This hack exists solely for the purposes of formatting the Texinfo
457 manual. I couldn't think of a better way. The token might be a
458 simple @@ followed immediately by more text. If this is the case,
459 then the next defun arg is part of this one, and we should
461 if (*scan_args
&& **scan_args
&& !whitespace (**scan_args
)
462 && STREQ (defined_name
, "@@"))
464 char *tem
= xmalloc (3 + strlen (scan_args
[0]));
466 sprintf (tem
, "@@%s", scan_args
[0]);
474 /* It's easy to write @defun foo(arg1 arg2), but a following ( is
475 misparsed by texinfo.tex and this is next to impossible to fix.
477 if (*scan_args
&& **scan_args
&& **scan_args
== '(')
478 warning ("`%c' follows defined name `%s' instead of whitespace",
479 **scan_args
, defined_name
);
482 begin_insertion (type
);
484 /* Write the definition header line.
485 This should start at the normal indentation. */
486 current_indent
-= default_indentation_increment
;
495 execute_string (" --- %s: %s", category
, defined_name
);
499 execute_string (" --- %s: %s %s", category
, type_name
, defined_name
);
502 execute_string (" --- %s %s %s: %s", category
, _("of"), type_name
,
507 execute_string (" --- %s %s %s: %s %s", category
, _("of"), type_name
,
508 type_name2
, defined_name
);
511 execute_string (" --- %s %s %s: %s", category
, _("on"), type_name
,
515 execute_string (" --- %s %s %s: %s %s", category
, _("on"), type_name
,
516 type_name2
, defined_name
);
519 execute_string (" --- %s %s %s: %s %s", category
, _("on"), type_name
,
520 type_name2
, defined_name
);
525 /* If this is not a @def...x version, it could only
526 be a normal version @def.... So start the table here. */
528 insert_string ("<div class=\"defun\">\n");
530 rollback_empty_tag ("blockquote");
532 /* xx The single words (on, off) used here, should depend on
533 documentlanguage and NOT on gettext --kama. */
541 execute_string ("--- %s: ", category
);
547 execute_string ("--- %s %s %s: ", category
, _("of"), type_name
);
553 execute_string ("--- %s %s %s: ", category
, _("on"), type_name
);
555 } /* switch (base_type)... */
562 /* <var> is for the following function arguments. */
563 insert_html_tag (START
, "b");
564 execute_string ("%s", defined_name
);
565 insert_html_tag (END
, "b");
566 insert_html_tag (START
, "var");
570 execute_string ("%s ", type_name
);
571 insert_html_tag (START
, "b");
572 execute_string ("%s", defined_name
);
573 insert_html_tag (END
, "b");
574 insert_html_tag (START
, "var");
578 insert_html_tag (START
, "b");
579 execute_string ("%s", defined_name
);
580 insert_html_tag (END
, "b");
581 insert_html_tag (START
, "var");
587 execute_string ("%s ", type_name2
);
588 insert_html_tag (START
, "b");
589 execute_string ("%s", defined_name
);
590 insert_html_tag (END
, "b");
591 insert_html_tag (START
, "var");
596 xml_begin_def_term (base_type
, category
, defined_name
, type_name
,
599 current_indent
+= default_indentation_increment
;
601 /* Now process the function arguments, if any. If these carry onto
602 the next line, they should be indented by two increments to
603 distinguish them from the body of the definition, which is indented
605 current_indent
+= default_indentation_increment
;
611 process_defun_args (scan_args
, 1);
614 /* Through Makeinfo 1.67 we processed remaining args only for deftp,
615 deftypefn, and deftypemethod. But the libc manual, for example,
617 @deftypevar {char *} tzname[2]
618 And simply allowing the extra text seems far simpler than trying
619 to invent yet more defn commands. In any case, we should either
620 output it or give an error, not silently ignore it. */
622 process_defun_args (scan_args
, 0);
626 current_indent
-= default_indentation_increment
;
628 close_single_paragraph ();
630 /* Make an entry in the appropriate index. (XML and
631 Docbook already got their entries, so skip them.) */
637 execute_string ("@findex %s\n", defined_name
);
643 execute_string ("@vindex %s\n", defined_name
);
646 execute_string ("@vindex %s %s %s\n", defined_name
, _("of"),
652 execute_string ("@findex %s %s %s\n", defined_name
, _("on"),
656 execute_string ("@tindex %s\n", defined_name
);
664 inhibit_paragraph_indentation
= 1;
666 insert_html_tag (END
, "var");
667 insert_string ("<br>\n");
668 /* Indent the definition a bit. */
669 add_html_block_elt ("<blockquote>");
671 inhibit_paragraph_indentation
= 0;
672 paragraph_is_open
= 0;
675 /* Deallocate the token list. */
676 scan_args
= defun_args
;
679 char * arg
= (*scan_args
++);
687 /* Add an entry for a function, macro, special form, variable, or option.
688 If the name of the calling command ends in `x', then this is an extra
689 entry included in the body of an insertion of the same type. */
694 char *base_command
= xstrdup (command
); /* command with any `x' removed */
695 int x_p
= (command
[strlen (command
) - 1] == 'x');
698 base_command
[strlen (base_command
) - 1] = 0;
700 type
= find_type_from_name (base_command
);
702 /* If we are adding to an already existing insertion, then make sure
703 that we are already in an insertion of type TYPE. */
706 INSERTION_ELT
*i
= insertion_stack
;
707 /* Skip over ifclear and ifset conditionals. */
708 while (i
&& (i
->insertion
== ifset
|| i
->insertion
== ifclear
))
711 if (!i
|| i
->insertion
!= type
)
713 line_error (_("Must be in `@%s' environment to use `@%s'"),
714 base_command
, command
);
715 discard_until ("\n");
720 defun_internal (type
, x_p
);