m4-patches
[Top][All Lists]
Advanced

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

divert extension


From: Eric Blake
Subject: divert extension
Date: Fri, 28 Nov 2008 07:56:45 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.18) Gecko/20081105 Thunderbird/2.0.0.18 Mnenhy/0.7.5.666

-----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             address@hidden
-----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 <address@hidden>
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 <address@hidden>
(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  <address@hidden>
+
+       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  <address@hidden>
 
        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         address@hidden
 Chris McGuire          address@hidden
 Damian Menscher                address@hidden
 Dan Jacobson           address@hidden
+Daniel Richard G.      address@hidden
 David J. MacKenzie     address@hidden
 David Perlin           address@hidden
 Elbert Pol             address@hidden
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}:
 
address@hidden Builtin divert (@dvar{number, 0})
address@hidden 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.
 
address@hidden @acronym{GNU} extensions
+As a @acronym{GNU} extension, if optional @var{text} is supplied and
address@hidden 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
address@hidden 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.
+
address@hidden
+define(`echo', `$1')
address@hidden
+echo(divert(`1')`one'divert(`2'))`'dnl
+echo(`divert(`3')three`'divert(`4')')`'dnl
+echo(divert(`5', `five')divert(`6'))`'dnl
+divert
address@hidden
+undivert(`1')
address@hidden
+undivert(`2')
address@hidden
+undivert(`3')
address@hidden
+undivert(`4')
address@hidden
+undivert(`5')
address@hidden
+undivert(`6')
address@hidden
address@hidden example
+
address@hidden
address@hidden additional tests, not worth documenting in manual
+
address@hidden options: -s
address@hidden
+$ @kbd{m4 -s}
+define(`echo', `$1')dnl
+divert(`-1', `discarded without warning')
+divert`'dnl
+echo(` world'divert(divnum, `hello'))
address@hidden 4 "stdin"
address@hidden world
address@hidden example
address@hidden 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


reply via email to

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