On 05/11/2015 04:08 AM, Werner LEMBERG wrote: > > Folks, > > > has someone ever written a `safe define' macro that makes the defined > macro expands to itself if called without any argument?
Yes, the manual even has a section on how to do it: https://www.gnu.org/software/m4/manual/m4.html#Composition > > sdefine({foo}, {bar}) > > foo => foo > foo() => bar > Looks like you have done changequote({,}) so I'll keep with that in my response. > I tried the following, and I failed (due to lack of experience > w.r.t. quoting tricks, I think) – I have difficulties to delay the > expansion of `$#'... > > Below, I've marked the problematic spots with (nonexisting) > pseudo-functions. > > define({sdefine}, > {define({$1}, > {ifelse(delay_expansion_of({$#}), > {0}, > {{$1}}, > {expand_at_definition_time(shift($@))})})}) define({sdefine}, {_$0({$1}, {$2}, {$}{#}, {$}{0})}) define({_sdefine}, {define({$1}, {ifelse({$3}, 0, {{$4}}, {$2})})}) The trick is realizing you need two functions: the outer sdefine that uses quoting tricks to pass a literal $# and $0 to the inner function, and the inner function that then does the desired define using the literal strings in the right place. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
