1 /* defun.c -- @defun and friends.
2 $Id: defun.c,v 1.11 1999/07/11 16:50:19 karl Exp $
4 Copyright (C) 1998, 99 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. */
22 #include "insertion.h"
26 #define DEFUN_SELF_DELIMITING(c) \
27 ((c) == '(' || (c) == ')' || (c) == '[' || (c) == ']')
29 struct token_accumulator
37 initialize_token_accumulator (accumulator)
38 struct token_accumulator *accumulator;
40 accumulator->length = 0;
41 accumulator->index = 0;
42 accumulator->tokens = NULL;
46 accumulate_token (accumulator, token)
47 struct token_accumulator *accumulator;
50 if (accumulator->index >= accumulator->length)
52 accumulator->length += 10;
53 accumulator->tokens = xrealloc (accumulator->tokens,
54 (accumulator->length * sizeof (char *)));
56 accumulator->tokens[accumulator->index] = token;
57 accumulator->index += 1;
60 /* Given STRING_POINTER pointing at an open brace, skip forward and return a
61 pointer to just past the matching close brace. */
63 scan_group_in_string (string_pointer)
64 char **string_pointer;
66 char *scan_string = (*string_pointer) + 1;
67 unsigned int level = 1;
74 *string_pointer = scan_string;
80 /* Tweak line_number to compensate for fact that
81 we gobbled the whole line before coming here. */
83 line_error (_("Missing `}' in @def arg"));
85 *string_pointer = scan_string - 1;
96 /* Return a list of tokens from the contents of STRING.
97 Commands and brace-delimited groups count as single tokens.
98 Contiguous whitespace characters are converted to a token
99 consisting of a single space. */
101 args_from_string (string)
104 struct token_accumulator accumulator;
105 char *token_start, *token_end;
106 char *scan_string = string;
108 initialize_token_accumulator (&accumulator);
111 { /* Replace arbitrary whitespace by a single space. */
112 if (whitespace (*scan_string))
115 while (whitespace (*scan_string))
117 accumulate_token ((&accumulator), (xstrdup (" ")));
121 /* Commands count as single tokens. */
122 if (*scan_string == COMMAND_PREFIX)
124 token_start = scan_string;
126 if (self_delimiting (*scan_string))
135 if ((c == 0) || (c == '{') || (whitespace (c)))
142 if (*scan_string == '{')
144 char *s = scan_string;
145 (void) scan_group_in_string (&s);
149 token_end = scan_string;
152 /* Parentheses and brackets are self-delimiting. */
153 else if (DEFUN_SELF_DELIMITING (*scan_string))
155 token_start = scan_string;
157 token_end = scan_string;
160 /* Open brace introduces a group that is a single token. */
161 else if (*scan_string == '{')
163 char *s = scan_string;
164 int balanced = scan_group_in_string (&s);
166 token_start = scan_string + 1;
168 token_end = balanced ? (scan_string - 1) : scan_string;
171 /* Otherwise a token is delimited by whitespace, parentheses,
172 brackets, or braces. A token is also ended by a command. */
175 token_start = scan_string;
183 /* Do not back up if we're looking at a }; since the only
184 valid }'s are those matched with {'s, we want to give
185 an error. If we back up, we go into an infinite loop. */
186 if (!c || whitespace (c) || DEFUN_SELF_DELIMITING (c)
193 /* If we encounter a command embedded within a token,
194 then end the token. */
195 if (c == COMMAND_PREFIX)
201 token_end = scan_string;
204 accumulate_token (&accumulator, substring (token_start, token_end));
206 accumulate_token (&accumulator, NULL);
207 return accumulator.tokens;
211 process_defun_args (defun_args, auto_var_p)
215 int pending_space = 0;
219 char *defun_arg = *defun_args++;
221 if (defun_arg == NULL)
224 if (defun_arg[0] == ' ')
236 if (DEFUN_SELF_DELIMITING (defun_arg[0]))
237 add_char (defun_arg[0]);
238 else if (defun_arg[0] == '&')
241 defun_arg = escape_string (xstrdup (defun_arg));
242 add_word (defun_arg);
246 add_word (defun_arg);
247 else if (defun_arg[0] == COMMAND_PREFIX)
248 execute_string ("%s", defun_arg);
252 defun_arg = escape_string (xstrdup (defun_arg));
253 add_word (defun_arg);
257 add_word (defun_arg);
259 add_word (defun_arg);
264 next_nonwhite_defun_arg (arg_pointer)
267 char **scan = (*arg_pointer);
268 char *arg = (*scan++);
270 if ((arg != 0) && (*arg == ' '))
278 return (arg == 0) ? "" : arg;
282 /* This is needed also in insertion.c. */
286 enum insertion_type type;
288 enum insertion_type base_type;
291 case defivar: base_type = defcv; break;
292 case defmac: base_type = deffn; break;
293 case defmethod: base_type = defop; break;
294 case defopt: base_type = defvr; break;
295 case defspec: base_type = deffn; break;
296 case deftypefun: base_type = deftypefn; break;
297 case deftypeivar: base_type = deftypeivar; break;
298 case deftypemethod: base_type = deftypemethod; break;
299 case deftypeop: base_type = deftypeop; break;
300 case deftypevar: base_type = deftypevr; break;
301 case defun: base_type = deffn; break;
302 case defvar: base_type = defvr; break;
311 /* Make the defun type insertion.
312 TYPE says which insertion this is.
313 X_P, if nonzero, says not to start a new insertion. */
315 defun_internal (type, x_p)
316 enum insertion_type type;
319 enum insertion_type base_type;
320 char **defun_args, **scan_args;
321 char *category, *defined_name, *type_name, *type_name2;
326 /* The @def.. line is the only place in Texinfo where you are
327 allowed to use unquoted braces that don't delimit arguments of
328 a command or a macro; in any other place it will trigger an
329 error message from the reader loop. The special handling of
330 this case inside `args_from_string' is an extra special hack
331 which allows this. The side effect is that if we try to expand
332 the rest of the line below, the recursive reader loop will
333 signal an error if there are brace-delimited arguments on that line.
335 The best solution to this would be to change the syntax of
336 @def.. commands so that it doesn't violate Texinfo's own rules.
337 But it's probably too late for this now, as it will break a lot
340 Unfortunately, this means that you can't call macros, use @value, etc.
341 inside @def.. commands, sigh. */
342 get_rest_of_line (0, &line);
343 defun_args = (args_from_string (line));
347 scan_args = defun_args;
349 /* Get base type and category string. */
350 base_type = get_base_type (type);
352 /* xx all these const strings should be determined upon
353 documentlanguage argument and NOT via gettext (kama). */
358 category = _("Function");
361 category = _("Macro");
364 category = _("Special Form");
368 category = _("Variable");
371 category = _("User Option");
375 category = _("Instance Variable");
379 category = _("Method");
382 category = next_nonwhite_defun_arg (&scan_args);
386 /* The class name. */
387 if ((base_type == deftypefn)
388 || (base_type == deftypevr)
389 || (base_type == defcv)
390 || (base_type == defop)
391 || (base_type == deftypeivar)
392 || (base_type == deftypemethod)
393 || (base_type == deftypeop)
395 type_name = next_nonwhite_defun_arg (&scan_args);
397 /* The type name for typed languages. */
398 if (base_type == deftypemethod
399 || base_type == deftypeivar
400 || base_type == deftypeop
402 type_name2 = next_nonwhite_defun_arg (&scan_args);
404 /* The function or whatever that's actually being defined. */
405 defined_name = next_nonwhite_defun_arg (&scan_args);
407 /* This hack exists solely for the purposes of formatting the Texinfo
408 manual. I couldn't think of a better way. The token might be a
409 simple @@ followed immediately by more text. If this is the case,
410 then the next defun arg is part of this one, and we should
412 if (*scan_args && **scan_args && !whitespace (**scan_args)
413 && STREQ (defined_name, "@@"))
415 char *tem = xmalloc (3 + strlen (scan_args[0]));
417 sprintf (tem, "@@%s", scan_args[0]);
426 begin_insertion (type);
428 /* Write the definition header line.
429 This should start at the normal indentation. */
430 current_indent -= default_indentation_increment;
434 /* Start the definition on new paragraph. */
443 execute_string (" -- %s: %s", category, defined_name);
447 execute_string (" -- %s: %s %s", category, type_name, defined_name);
450 execute_string (" -- %s %s %s: %s", category, _("of"), type_name,
454 execute_string (" -- %s %s %s: %s %s", category, _("of"), type_name,
455 type_name2, defined_name);
458 execute_string (" -- %s %s %s: %s", category, _("on"), type_name,
462 execute_string (" -- %s %s %s: %s %s", category, _("on"), type_name,
463 type_name2, defined_name);
466 execute_string (" -- %s %s %s: %s %s", category, _("on"), type_name,
467 type_name2, defined_name);
473 /* If this is not a @def...x version, it could only
474 be a normal version @def.... So start the table here. */
476 add_word ("<table width=\"100%\">\n");
478 /* If this is an @def...x there has to be an other @def... before
479 it, so this is only a new row within an existing table. With
480 two complete standalone tables the gap between them is too big. */
482 add_word ("<td align=\"left\">");
489 /* <i> is for the following function arguments. */
490 add_word_args ("<b>%s</b><i>", defined_name);
494 add_word_args ("%s <b>%s</b><i>", type_name, defined_name);
498 add_word_args ("<b>%s</b><i>", defined_name);
503 add_word_args ("%s <b>%s</b><i>", type_name2, defined_name);
508 current_indent += default_indentation_increment;
510 /* Now process the function arguments, if any. If these carry onto
511 the next line, they should be indented by two increments to
512 distinguish them from the body of the definition, which is indented
514 current_indent += default_indentation_increment;
520 process_defun_args (scan_args, 1);
523 /* Through Makeinfo 1.67 we processed remaining args only for deftp,
524 deftypefn, and deftypemethod. But the libc manual, for example,
526 @deftypevar {char *} tzname[2]
527 And simply allowing the extra text seems far simpler than trying
528 to invent yet more defn commands. In any case, we should either
529 output it or give an error, not silently ignore it. */
531 process_defun_args (scan_args, 0);
535 current_indent -= default_indentation_increment;
536 close_single_paragraph ();
540 /* xx The single words (on, off) used here, should depend on
541 documentlanguage and NOT on gettext --kama. */
549 add_word ("</i>"); /* close italic area for arguments */
550 /* put the rest into the second column */
551 add_word_args ("</td>\n<td align=\"right\">%s", category);
555 add_word ("</td>\n<td align=\"right\">");
556 add_word_args ("%s %s %s", category, _("of"), type_name);
563 add_word ("</td>\n<td align=\"right\">");
564 add_word_args ("%s %s %s", category, _("on"), type_name);
569 add_word ("</td>\n<td align=\"right\">");
570 add_word_args ("%s %s %s", category, _("of"), type_name);
572 } /* switch (base_type)... */
574 add_word ("</td>\n"); /* close second column */
575 add_word ("</tr>\n"); /* close row */
577 /* This is needed because I have to know if the next line is
578 normal text or another @def..x. If text follows, create a new
579 table to get the indentation for the following text.
581 This construction would fail if someone uses:
587 But we don't care. */
588 if (!looking_at ("@def"))
590 add_word ("</table>\n");
591 add_word ("<table width=\"95%\" align=\"center\">\n");
592 add_word ("<tr><td>\n");
597 /* Make an entry in the appropriate index. */
602 execute_string ("@findex %s\n", defined_name);
607 execute_string ("@vindex %s\n", defined_name);
610 execute_string ("@vindex %s %s %s\n", defined_name, _("of"), type_name);
615 execute_string ("@findex %s %s %s\n", defined_name, _("on"), type_name);
618 execute_string ("@tindex %s\n", defined_name);
622 /* Deallocate the token list. */
623 scan_args = defun_args;
626 char * arg = (*scan_args++);
634 /* Add an entry for a function, macro, special form, variable, or option.
635 If the name of the calling command ends in `x', then this is an extra
636 entry included in the body of an insertion of the same type. */
641 enum insertion_type type;
642 char *temp = xstrdup (command);
644 x_p = (command[strlen (command) - 1] == 'x');
647 temp[strlen (temp) - 1] = 0;
649 type = find_type_from_name (temp);
652 /* If we are adding to an already existing insertion, then make sure
653 that we are already in an insertion of type TYPE. */
654 if (x_p && (!insertion_level || insertion_stack->insertion != type))
656 line_error (_("Must be in `%s' insertion to use `%sx'"),
658 discard_until ("\n");
662 defun_internal (type, x_p);