]> code.delx.au - gnu-emacs/blobdiff - lispref/macros.texi
*** empty log message ***
[gnu-emacs] / lispref / macros.texi
index 0a739bc3ba55608773dd7ac5609412a03950d342..3c91e5bb317ec04cb395e63ed982206b4ed31eb1 100644 (file)
@@ -1,6 +1,6 @@
 @c -*-texinfo-*-
 @c This is part of the GNU Emacs Lisp Reference Manual.
-@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. 
+@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
 @c See the file elisp.texi for copying conditions.
 @setfilename ../info/macros
 @node Macros, Customization, Functions, Top
@@ -320,6 +320,7 @@ This section describes some important consequences that can lead to
 trouble, and rules to follow to avoid trouble.
 
 @menu
+* Wrong Time::             Do the work in the expansion, not in the macro.
 * Argument Evaluation::    The expansion should evaluate each macro arg once.
 * Surprising Local Vars::  Local variable bindings in the expansion
                               require special care.
@@ -327,6 +328,37 @@ trouble, and rules to follow to avoid trouble.
 * Repeated Expansion::     Avoid depending on how many times expansion is done.
 @end menu
 
+@node Wrong Time
+@subsection Wrong Time
+
+  The most common problem in writing macros is doing too some of the
+real work prematurely---while expanding the macro, rather than in the
+expansion itself.  For instance, one real package had this nmacro
+definition:
+
+@example
+(defmacro my-set-buffer-multibyte (arg)
+  (if (fboundp 'set-buffer-multibyte)
+      (set-buffer-multibyte arg)))
+@end example
+
+With this erroneous macro definition, the program worked fine when
+interpreted but failed when compiled.  This macro definition called
+@code{set-buffer-multibyte} during compilation, which was wrong, and
+then did nothing when the compiled package was run.  The definition
+that the programmer really wanted was this:
+
+@example
+(defmacro my-set-buffer-multibyte (arg)
+  (if (fboundp 'set-buffer-multibyte)
+      `(set-buffer-multibyte ,arg)))
+@end example
+
+@noindent
+This macro expands, if appropriate, into a call to
+@code{set-buffer-multibyte} that will be executed when the compiled
+program is actually run.
+
 @node Argument Evaluation
 @subsection Evaluating Macro Arguments Repeatedly
 
@@ -414,7 +446,7 @@ macro.  Here is a correct expansion for the @code{for} macro:
 @end group
 @end smallexample
 
-Here is a macro definition that creates this expansion: 
+Here is a macro definition that creates this expansion:
 
 @smallexample
 @group
@@ -434,7 +466,7 @@ described in the following section.
 @node Surprising Local Vars
 @subsection Local Variables in Macro Expansions
 
-@ifinfo
+@ifnottex
   In the previous section, the definition of @code{for} was fixed as
 follows to make the expansion evaluate the macro arguments the proper
 number of times:
@@ -452,7 +484,7 @@ number of times:
        (inc ,var))))
 @end group
 @end smallexample
-@end ifinfo
+@end ifnottex
 
   The new definition of @code{for} has a new problem: it introduces a
 local variable named @code{max} which the user does not expect.  This