1
1
/* macro.c -- user-defined macros for Texinfo.
2
$Id: macro.c,v 1.12 2002/03/02 15:05:21 karl Exp $
2
$Id: macro.c,v 1.6 2004/04/11 17:56:47 karl Exp $
4
Copyright (C) 1998, 99, 2002 Free Software Foundation, Inc.
4
Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
6
6
This program is free software; you can redistribute it and/or modify
7
7
it under the terms of the GNU General Public License as published by
101
99
and SOURCE_LINENO is the line number within that file. If a macro already
102
100
exists with NAME, then a warning is produced, and that previous
103
101
definition is overwritten. */
105
add_macro (name, arglist, body, source_file, source_lineno, flags)
110
int source_lineno, flags;
103
add_macro (char *name, char **arglist, char *body, char *source_file,
104
int source_lineno, int flags)
298
290
/* Substitute actual parameters for named parameters in body.
299
291
The named parameters which appear in BODY must by surrounded
300
292
reverse slashes, as in \foo\. */
302
apply (named, actuals, body)
303
char **named, **actuals, *body;
294
apply (char **named, char **actuals, char *body)
306
297
int new_body_index, new_body_size;
323
314
{ /* Snarf parameter name, check against named parameters. */
325
int param_start, which, len;
316
int param_start, len;
327
318
param_start = ++i;
328
319
while (body[i] && body[i] != '\\')
336
327
if (body[i]) /* move past \ */
339
/* Now check against named parameters. */
340
for (which = 0; named && named[which]; which++)
341
if (STREQ (named[which], param))
344
if (named && named[which])
346
text = which < length_of_actuals ? actuals[which] : NULL;
352
{ /* not a parameter, either it's \\ (if len==0) or an
353
error. In either case, restore one \ at least. */
355
warning (_("\\ in macro expansion followed by `%s' instead of \\ or parameter name"),
331
{ /* \\ always means \, even if macro has no args. */
359
333
text = xmalloc (1 + len);
360
334
sprintf (text, "\\%s", param);
340
/* Check against named parameters. */
341
for (which = 0; named && named[which]; which++)
342
if (STREQ (named[which], param))
345
if (named && named[which])
347
text = which < length_of_actuals ? actuals[which] : NULL;
351
text = xstrdup (text); /* so we can free it */
354
{ /* not a parameter, so it's an error. */
355
warning (_("\\ in macro expansion followed by `%s' instead of parameter name"),
358
text = xmalloc (1 + len);
359
sprintf (text, "\\%s", param);
363
363
if (strlen (param) + 2 < len)
436
433
end_line = line_number;
437
434
line_number = start_line;
439
if (macro_expansion_output_stream && !executing_string && !me_inhibit_expansion)
436
if (macro_expansion_output_stream
437
&& !executing_string && !me_inhibit_expansion)
441
439
remember_itext (input_text, input_text_offset);
442
440
me_execute_string (execution_string);
454
452
set the ME_RECURSE flag. MACTYPE is either "macro" or "rmacro", and
455
453
tells us what the matching @end should be. */
457
define_macro (mactype, recursive)
455
define_macro (char *mactype, int recursive)
462
char *name, **arglist, *body, *line, *last_end;
463
int body_size, body_index;
459
char *last_end = NULL;
461
char **arglist = NULL;
462
int body_size = 0, body_index = 0;
465
465
int defining_line = line_number;
473
467
if (macro_expansion_output_stream && !executing_string)
474
468
me_append_before_this_command ();
478
472
/* Get the name of the macro. This is the set of characters which are
479
473
not whitespace and are not `{' immediately following the @macro. */
474
start = input_text_offset;
481
int start = input_text_offset;
485
(i < input_text_length) &&
486
(input_text[i] != '{') &&
487
(!cr_or_whitespace (input_text[i]));
478
for (i = start; i < input_text_length && input_text[i] != '{'
479
&& !cr_or_whitespace (input_text[i]);
491
483
name = xmalloc (1 + len);
689
681
add_macro (name, arglist, body, input_filename, defining_line, flags);
691
683
if (macro_expansion_output_stream && !executing_string)
692
remember_itext (input_text, input_text_offset);
685
/* Remember text for future expansions. */
686
remember_itext (input_text, input_text_offset);
688
/* Bizarrely, output the @macro itself. This is so texinfo.tex
689
will have a chance to read it when texi2dvi calls makeinfo -E.
690
The problem is that we don't really expand macros in all
691
contexts; a @table's @item is one. And a fix is not obvious to
692
me, since it appears virtually identical to any other internal
693
expansion. Just setting a variable in cm_item caused other
694
strange expansion problems. */
695
write_region_to_macro_output ("@", 0, 1);
696
write_region_to_macro_output (mactype, 0, strlen (mactype));
697
write_region_to_macro_output (" ", 0, 1);
698
write_region_to_macro_output (input_text, start, input_text_offset);
698
705
define_macro ("macro", 0);
704
711
define_macro ("rmacro", 1);
850
853
/* Append the text which appeared in input_text from the last offset to
851
854
the character just before the command that we are currently executing. */
853
me_append_before_this_command ()
856
me_append_before_this_command (void)
862
865
/* Similar to execute_string, but only takes a single string argument,
863
866
and remembers the input text location, etc. */
865
me_execute_string (execution_string)
866
char *execution_string;
868
me_execute_string (char *execution_string)
868
870
int saved_escape_html = escape_html;
869
871
int saved_in_paragraph = in_paragraph;
895
897
when we need to produce macro-expanded output for input which
896
898
leaves no traces in the Info output. */
898
me_execute_string_keep_state (execution_string, append_string)
899
char *execution_string, *append_string;
900
me_execute_string_keep_state (char *execution_string, char *append_string)
901
902
int op_orig, opcol_orig, popen_orig;
902
903
int fill_orig, newline_orig, indent_orig, meta_pos_orig;
976
write_region_to_macro_output (string, start, end)
974
write_region_to_macro_output (char *string, int start, int end)
980
976
if (macro_expansion_output_stream)
981
977
fwrite (string + start, 1, end - start, macro_expansion_output_stream);
993
989
static alias_type *aliases;
991
/* @alias aname = cmdname */
999
996
alias_type *a = xmalloc (sizeof (alias_type));
1001
998
skip_whitespace ();
1002
get_until_in_line (1, "=", &(a->alias));
999
get_until_in_line (0, "=", &(a->alias));
1003
1000
canon_white (a->alias);
1005
1002
discard_until ("=");
1097
1092
/* actually perform the enclosure expansion */
1099
enclosure_expand (arg, start, end)
1100
int arg, start, end;
1094
enclosure_expand (int arg, int start, int end)
1102
1096
if (arg == START)
1103
1097
add_word (enclosure_stack->current->before);