M4sugar makes heavy use of diversions, because it is often the case that text that must appear early in the output is not discovered until late in the input. Additionally, some of the topological sorting algorithms used in resolving macro dependencies use diversions. However, most macros should not need to change diversions directly, but rather rely on higher-level M4sugar macros to manage diversions transparently.
In the rare case that it is necessary to write a macro that explicitly
outputs text to a different diversion, it is important to be aware of an
M4 limitation regarding diversions: text only goes to a diversion if it
is not part of argument collection. Therefore, any macro that changes
the current diversion cannot be used as an unquoted argument to another
macro, but must be expanded at the top level. The macro
m4_expand
will diagnose any attempt to change diversions, since
it is generally useful only as an argument to another macro. The
following example shows what happens when diversion manipulation is
attempted within macro arguments:
m4_do([normal text] m4_divert_push([KILL])unwanted[]m4_divert_pop([KILL]) [m4_divert_push([KILL])discarded[]m4_divert_pop([KILL])])dnl ⇒normal text ⇒unwanted
Notice that the unquoted text unwanted
is output, even though it
was processed while the current diversion was KILL
, because it
was collected as part of the argument to m4_do
. However, the
text discarded
disappeared as desired, because the diversion
changes were single-quoted, and were not expanded until the top-level
rescan of the output of m4_do
.
To make diversion management easier, M4sugar uses the concept of named
diversions. Rather than using diversion numbers directly, it is nicer
to associate a name with each diversion; the diversion number associated
with a particular diversion name is an implementation detail, so you
should only use diversion names. In general, you should not output text
to a named diversion until after calling the appropriate initialization
routine for your language (m4_init
, AS_INIT
,
AT_INIT
, ...), although there are some exceptions documented
below.
M4sugar defines two named diversions.
KILL
GROW
AC_REQUIRE
.
M4sh adds several more named diversions.
BINSH
HEADER-REVISION
AC_REVISION
.
HEADER-COMMENT
HEADER-COPYRIGHT
AC_COPYRIGHT
.
M4SH-SANITIZE
M4SH-INIT
BODY
Autotest inherits diversions from M4sh, and changes the default
diversion from BODY
back to KILL
. It also adds several
more named diversions, with the following subset designed for developer
use.
PREPARE_TESTS
AT_INIT
.
For now, the named diversions of Autoconf and Autoheader, and the remaining diversions of Autotest, are not documented. In other words, intentionally outputting text into an undocumented diversion is subject to breakage in a future release of Autoconf.
Permanently discard any text that has been diverted into diversion.
Similar to
m4_divert_text
, except that content is only output to diversion if this is the first time thatm4_divert_once
has been called with its particular arguments.
If provided, check that the current diversion is indeed diversion. Then change to the diversion located earlier on the stack, giving an error if an attempt is made to pop beyond the initial m4sugar diversion of
KILL
.
Remember the former diversion on the diversion stack, and output subsequent text into diversion. M4sugar maintains a diversion stack, and issues an error if there is not a matching pop for every push.