[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: protect my comma

From: Stepan Kasal
Subject: Re: protect my comma
Date: Tue, 17 Jan 2006 11:31:48 +0100
User-agent: Mutt/1.4.1i


On Mon, Jan 16, 2006 at 05:52:14PM +0100, marc chantreux wrote:
> I feel dumb: I was trying and trying again to get a macro that really 
> protect the comma under deeper recursion level but I failed. Peraps 
> there is no easy way to do rescursive fonctions with m4?

recursive functions are possible, as in the example I posted in my
previous mail.

You just have to manage the quotes correctly.  To do that, you
have to remember:

When macro with its parameter list is scanned, one level of quotes
is stripped; then the $1 and such in the body are replaced with
the parameters without the quotes.  When the text is expanded in the
"outermost" level, another pair of quotes is stripped.

So to call another macro with the same $1, you have to add one pair
of quotes:

define(`FOO', `BAR(`$1')')

this calls BAR with the same parameter as the parameter for FOO.

If we had also
define(`BAR', `This is $1.')
define(`SOME', `something')

Then FOO(`SOME') or BAR(`SOME') calls BAR with the parameter SOME,
which is expanded at the end.  If you meant literal "SOME", you'd
have to do  FOO(``SOME'') or BAR(``SOME'')--the outer pair of quotes
denotes the parameter, while the inner pair of quotes get's passed
and at the end if prevents the expansion fo SOME.

Then we have $@ and shift().  The expansion will be a list of quoted
parameters, so you don't write `$@' or `shift(....)'; instead, $@ and
shift() add a pair of quotes to each of the parameters.
So to pass the whole list of parameters, you do:

        define(`FOO', `BAR($@)')

or, (almost) equivalently:

        define(`FOO', `BAR(`$1', shift($@))')

Hope this helps. Try to read the m4 manual, too.

Have a nioce day,
        Stepan Kasal

reply via email to

[Prev in Thread] Current Thread [Next in Thread]