[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Filter user macro invocation
From: |
Eric Blake |
Subject: |
Re: Filter user macro invocation |
Date: |
Sat, 03 May 2008 13:33:48 -0600 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080421 Thunderbird/2.0.0.14 Mnenhy/0.7.5.666 |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
According to Eric Blake on 4/30/2008 7:35 PM:
|
| to make define always treat new macros like this. Hmm, maybe I should
| mention that in the manual.
|
Done 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
iEYEARECAAYFAkgcvhwACgkQ84KuGfSFAYD0MQCeOX6W8PYrtppstMxJOqZRCucq
VuEAoI5H9qxrYn4lU85FCdQoThr0NdBH
=+6eD
-----END PGP SIGNATURE-----
>From 26b3c17ceb4f4155ff5dd13bcd9cc63039b3f242 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Sat, 3 May 2008 11:51:55 -0600
Subject: [PATCH] Document define_blind.
* doc/m4.texinfo (Ifelse): Add a new composite macro.
* THANKS: Update.
Suggested by Mike R.
Signed-off-by: Eric Blake <address@hidden>
---
ChangeLog | 7 ++++++
THANKS | 1 +
doc/m4.texinfo | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 65 insertions(+), 0 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 1454e02..6362809 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-05-03 Eric Blake <address@hidden>
+
+ Document define_blind.
+ * doc/m4.texinfo (Ifelse): Add a new composite macro.
+ * THANKS: Update.
+ Suggested by Mike R.
+
2008-04-24 Eric Blake <address@hidden>
Fix debugmode regression from 2008-04-14.
diff --git a/THANKS b/THANKS
index eb2a79d..19c1d92 100644
diff --git a/doc/m4.texinfo b/doc/m4.texinfo
index 0da6b00..4781567 100644
--- a/doc/m4.texinfo
+++ b/doc/m4.texinfo
@@ -2761,6 +2761,63 @@ foo(`a', `b', `c')
@result{}arguments:3
@end example
+Since m4 is a macro language, it is even possible to write a macro that
+makes defining blind macros easier:
+
address@hidden Composite define_blind (@var{name}, @ovar{value})
+Defines @var{name} as a blind macro, such that @var{name} will expand to
address@hidden only when given explicit arguments. @var{value} should not
+be the result of @code{defn} (@pxref{Defn}). This macro is only
+recognized with parameters, and results in an empty string.
address@hidden deffn
+
+Defining a macro to define another macro can be a bit tricky. We want
+to use a literal @samp{$#} in the argument to the nested @code{define}.
+However, if @samp{$} and @samp{#} are adjacent in the definition of
address@hidden, then it would be expanded as the number of
+arguments to @code{define_blind} rather than the intended number of
+arguments to @var{name}. The solution is to pass the difficult
+characters through extra arguments to a helper macro
address@hidden
+
+As for the limitation against using @code{defn}, there are two reasons.
+If a macro was previously defined with @code{define_blind}, then it can
+safely be renamed to a new blind macro using plain @code{define}; using
address@hidden to rename it just adds another layer of
address@hidden, occupying memory and slowing down execution. And if a
+macro is a builtin, then it would result in an attempt to define a macro
+consisting of both text and a builtin token; this is not supported, and
+the builtin token is flattened to an empty string.
+
+With that explanation, here's the definition, and some sample usage.
+Notice that @code{define_blind} is itself a blind macro.
+
address@hidden
+$ @kbd{m4 -d}
+define(`define_blind', `ifelse(`$#', `0', ``$0'',
+`_$0(`$1', `$2', `$'`#', `$'`0')')')
address@hidden
+define(`_define_blind', `define(`$1',
+`ifelse(`$3', `0', ``$4'', `$2')')')
address@hidden
+define_blind
address@hidden
+define_blind(`foo', `arguments were $*')
address@hidden
+foo
address@hidden
+foo(`bar')
address@hidden were bar
+define(`blah', defn(`foo'))
address@hidden
+blah
address@hidden
+blah(`a', `b')
address@hidden were a,b
+defn(`blah')
address@hidden(`$#', `0', ``$0'', `arguments were $*')
address@hidden example
+
@cindex multibranches
@cindex switch statement
@cindex case statement
--
1.5.5.1
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: Filter user macro invocation,
Eric Blake <=