Next: , Previous: Undefine, Up: Definitions


4.5 Renaming macros

It is possible to rename an already defined macro. To do this, you need the builtin defn:

— Builtin: defn (name)

Expands to the quoted definition of name. If the argument is not a defined macro, the expansion is void.

If name is a user-defined macro, the quoted definition is simply the quoted expansion text. If, instead, name is a builtin, the expansion is a special token, which points to the builtin's internal definition. This token is only meaningful as the second argument to define (and pushdef), and is silently converted to an empty string in most other contexts.

The macro defn is recognized only with parameters.

Its normal use is best understood through an example, which shows how to rename undefine to zap:

     define(`zap', defn(`undefine'))
     =>
     zap(`undefine')
     =>
     undefine(`zap')
     =>undefine(zap)

In this way, defn can be used to copy macro definitions, and also definitions of builtin macros. Even if the original macro is removed, the other name can still be used to access the definition.

The fact that macro definitions can be transferred also explains why you should use $0, rather than retyping a macro's name in its definition:

     define(`foo', `This is `$0'')
     =>
     define(`bar', defn(`foo'))
     =>
     bar
     =>This is bar

Macros used as string variables should be referred through defn, to avoid unwanted expansion of the text:

     define(`string', `The macro dnl is very useful
     ')
     =>
     string
     =>The macro defn(`string')
     =>The macro dnl is very useful
     =>

However, it is important to remember that m4 rescanning is purely textual. If an unbalanced end-quote string occurs in a macro definition, the rescan will see that embedded quote as the termination of the quoted string, and the remainder of the macro's definition will be rescanned unquoted. Thus it is a good idea to avoid unbalanced end-quotes in macro definitions or arguments to macros.

     define(`foo', a'a)
     =>
     define(`a', `A')
     =>
     define(`echo', `$@')
     =>
     foo
     =>A'A
     defn(`foo')
     =>aA'
     echo(foo)
     =>AA'

Using defn to generate special tokens for builtin macros outside of expected contexts can sometimes trigger warnings. But most of the time, such tokens are silently converted to the empty string.

     defn(`defn')
     =>
     define(defn(`divnum'), `cannot redefine a builtin token')
     error-->m4:stdin:2: Warning: define: invalid macro name ignored
     =>
     divnum
     =>0