-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm backporting the feature on the master branch of allowing a second
parameter to divert to branch-1.6, as follows.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             [EMAIL PROTECTED]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkkwBq0ACgkQ84KuGfSFAYD7bwCeNyA1d3WtNX7Kl+2AhvawAit3
5LcAn2We9Whnbxd0xl8s2TjGr3jDHg7f
=Ne3C
-----END PGP SIGNATURE-----
>From 9f5e389c810aacbd70b04f2530aa52a897cd0ad9 Mon Sep 17 00:00:00 2001
From: Eric Blake <[EMAIL PROTECTED]>
Date: Fri, 28 Nov 2008 07:46:57 -0700
Subject: [PATCH] Add extension to divert builtin.

* src/builtin.c (m4_divert): Support optional second parameter.
* src/output.c (divert_text): Optimize empty output.
* doc/m4.texinfo (Divert): Document the extension.
* NEWS: Likewise.
* THANKS: Update.
Reported by Daniel Richard G.

Signed-off-by: Eric Blake <[EMAIL PROTECTED]>
(cherry picked from commit 4f82b1e37666722c57e9e6fd2495d9c1cd694db8)
---
 ChangeLog      |   10 +++++++++
 NEWS           |    5 ++++
 THANKS         |    1 +
 doc/m4.texinfo |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/builtin.c  |    3 +-
 src/output.c   |    2 +-
 6 files changed, 79 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d100a59..2085dea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-11-28  Eric Blake  <[EMAIL PROTECTED]>
+
+       Add extension to divert builtin.
+       * src/builtin.c (m4_divert): Support optional second parameter.
+       * src/output.c (divert_text): Optimize empty output.
+       * doc/m4.texinfo (Divert): Document the extension.
+       * NEWS: Likewise.
+       * THANKS: Update.
+       Reported by Daniel Richard G.
+
 2008-11-26  Eric Blake  <[EMAIL PROTECTED]>
 
        Keep COPYING in repository.
diff --git a/NEWS b/NEWS
index bb61b72..3fb4d90 100644
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,11 @@ Foundation, Inc.
    then apply this patch:
      http://git.sv.gnu.org/gitweb/?p=autoconf.git;a=commitdiff;h=56d42fa71
 
+** The `divert' builtin now accepts an optional second argument of text
+   that is immediately placed in the new diversion, regardless of whether
+   the current expansion is nested within argument collection of another
+   macro.
+
 ** The `-d'/`--debug' command-line option now understands `-' and `+'
    modifiers, the way the builtin `debugmode' has always done; this allows
    `-d-V' to disable prior debug settings from the command line, similar to
diff --git a/THANKS b/THANKS
index 6a048b9..bb8a651 100644
--- a/THANKS
+++ b/THANKS
@@ -28,6 +28,7 @@ Cesar Strauss         [EMAIL PROTECTED]
 Chris McGuire          [EMAIL PROTECTED]
 Damian Menscher                [EMAIL PROTECTED]
 Dan Jacobson           [EMAIL PROTECTED]
+Daniel Richard G.      [EMAIL PROTECTED]
 David J. MacKenzie     [EMAIL PROTECTED]
 David Perlin           [EMAIL PROTECTED]
 Elbert Pol             [EMAIL PROTECTED]
diff --git a/doc/m4.texinfo b/doc/m4.texinfo
index f77a6b5..8301bb7 100644
--- a/doc/m4.texinfo
+++ b/doc/m4.texinfo
@@ -5603,11 +5603,17 @@ Divert
 @cindex files, diverting output to
 Output is diverted using @code{divert}:
 
[EMAIL PROTECTED] Builtin divert (@dvar{number, 0})
[EMAIL PROTECTED] Builtin divert (@dvar{number, 0}, @ovar{text})
 The current diversion is changed to @var{number}.  If @var{number} is left
 out or empty, it is assumed to be zero.  If @var{number} cannot be
 parsed, the diversion is unchanged.
 
[EMAIL PROTECTED] @acronym{GNU} extensions
+As a @acronym{GNU} extension, if optional @var{text} is supplied and
[EMAIL PROTECTED] was valid, then @var{text} is immediately output to the new
+diversion, regardless of whether the expansion of @code{divert} occurred
+while collecting arguments for another macro.
+
 The expansion of @code{divert} is void.
 @end deffn
 
@@ -5671,6 +5677,59 @@ Divert
 @result{}world
 @end example
 
+The ability to immediately output extra text is a @acronym{GNU}
+extension, but it can prove useful for ensuring that text goes to a
+particular diversion no matter how many pending macro expansions are in
+progress.  For a demonstration of why this is useful, it is important to
+understand in the example below why @samp{one} is output in diversion 2,
+not diversion 1, while @samp{three} and @samp{five} both end up in the
+correctly numbered diversion.  The key point is that when @code{divert}
+is executed unquoted as part of the argument collection of another
+macro, the side effect takes place immediately, but the text @samp{one}
+is not passed to any diversion until after the @samp{divert(`2')} and
+the enclosing @code{echo} have also taken place.  The example with
[EMAIL PROTECTED] shows how following the quoting rule of thumb delays the
+invocation of @code{divert} until it is not nested in any argument
+collection context, while the example with @samp{five} shows the use of
+the optional argument to speed up the output process.
+
[EMAIL PROTECTED]
+define(`echo', `$1')
[EMAIL PROTECTED]
+echo(divert(`1')`one'divert(`2'))`'dnl
+echo(`divert(`3')three`'divert(`4')')`'dnl
+echo(divert(`5', `five')divert(`6'))`'dnl
+divert
[EMAIL PROTECTED]
+undivert(`1')
[EMAIL PROTECTED]
+undivert(`2')
[EMAIL PROTECTED]
+undivert(`3')
[EMAIL PROTECTED]
+undivert(`4')
[EMAIL PROTECTED]
+undivert(`5')
[EMAIL PROTECTED]
+undivert(`6')
[EMAIL PROTECTED]
[EMAIL PROTECTED] example
+
[EMAIL PROTECTED]
[EMAIL PROTECTED] additional tests, not worth documenting in manual
+
[EMAIL PROTECTED] options: -s
[EMAIL PROTECTED]
+$ @kbd{m4 -s}
+define(`echo', `$1')dnl
+divert(`-1', `discarded without warning')
+divert`'dnl
+echo(` world'divert(divnum, `hello'))
[EMAIL PROTECTED] 4 "stdin"
[EMAIL PROTECTED] world
[EMAIL PROTECTED] example
[EMAIL PROTECTED] ignore
+
 Note that @code{divert} is an English word, but also an active macro
 without arguments.  When processing plain text, the word might appear in
 normal text and be unintentionally swallowed as a macro invocation.  One
diff --git a/src/builtin.c b/src/builtin.c
index 267292c..24f2df6 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -1304,11 +1304,12 @@ m4_divert (struct obstack *obs, int argc, 
macro_arguments *argv)
   const call_info *me = arg_info (argv);
   int i = 0;
 
-  bad_argc (me, argc, 0, 1);
+  bad_argc (me, argc, 0, 2);
   if (argc >= 2 && !numeric_arg (me, ARG (1), &i))
     return;
 
   make_diversion (i);
+  divert_text (NULL, ARG (2), ARG_LEN (2), current_line);
 }
 
 /*-----------------------------------------------------.
diff --git a/src/output.c b/src/output.c
index 6d74ecd..6a02b80 100644
--- a/src/output.c
+++ b/src/output.c
@@ -479,7 +479,7 @@ divert_text (struct obstack *obs, const char *text, int 
length, int line)
 
   /* Do nothing if TEXT should be discarded.  */
 
-  if (output_diversion == NULL)
+  if (output_diversion == NULL || !length)
     return;
 
   /* Output TEXT to a file, or in-memory diversion buffer.  */
-- 
1.6.0.4

_______________________________________________
M4-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/m4-patches

Reply via email to