Mercurial > emacs
comparison lispref/macros.texi @ 21007:66d807bdc5b4
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 28 Feb 1998 01:53:53 +0000 |
parents | a6eb5f12b0f3 |
children | 90da2489c498 |
comparison
equal
deleted
inserted
replaced
21006:00022857f529 | 21007:66d807bdc5b4 |
---|---|
1 @c -*-texinfo-*- | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | 2 @c This is part of the GNU Emacs Lisp Reference Manual. |
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. | 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. |
4 @c See the file elisp.texi for copying conditions. | 4 @c See the file elisp.texi for copying conditions. |
5 @setfilename ../info/macros | 5 @setfilename ../info/macros |
6 @node Macros, Loading, Functions, Top | 6 @node Macros, Customization, Functions, Top |
7 @chapter Macros | 7 @chapter Macros |
8 @cindex macros | 8 @cindex macros |
9 | 9 |
10 @dfn{Macros} enable you to define new control constructs and other | 10 @dfn{Macros} enable you to define new control constructs and other |
11 language features. A macro is defined much like a function, but instead | 11 language features. A macro is defined much like a function, but instead |
151 As a result, the compiled code produces the value and side effects | 151 As a result, the compiled code produces the value and side effects |
152 intended for the macro, but executes at full compiled speed. This would | 152 intended for the macro, but executes at full compiled speed. This would |
153 not work if the macro body computed the value and side effects | 153 not work if the macro body computed the value and side effects |
154 itself---they would be computed at compile time, which is not useful. | 154 itself---they would be computed at compile time, which is not useful. |
155 | 155 |
156 In order for compilation of macro calls to work, the macros must be | 156 In order for compilation of macro calls to work, the macros must |
157 defined in Lisp when the calls to them are compiled. The compiler has a | 157 already be defined in Lisp when the calls to them are compiled. The |
158 special feature to help you do this: if a file being compiled contains a | 158 compiler has a special feature to help you do this: if a file being |
159 @code{defmacro} form, the macro is defined temporarily for the rest of | 159 compiled contains a @code{defmacro} form, the macro is defined |
160 the compilation of that file. To use this feature, you must define the | 160 temporarily for the rest of the compilation of that file. To make this |
161 macro in the same file where it is used and before its first use. | 161 feature work, you must put the @code{defmacro} in the same file where it |
162 is used, and before its first use. | |
162 | 163 |
163 Byte-compiling a file executes any @code{require} calls at top-level | 164 Byte-compiling a file executes any @code{require} calls at top-level |
164 in the file. This is in case the file needs the required packages for | 165 in the file. This is in case the file needs the required packages for |
165 proper compilation. One way to ensure that necessary macro definitions | 166 proper compilation. One way to ensure that necessary macro definitions |
166 are available during compilation is to require the files that define | 167 are available during compilation is to require the files that define |
242 `(a list of ,(+ 2 3) elements) | 243 `(a list of ,(+ 2 3) elements) |
243 @result{} (a list of 5 elements) | 244 @result{} (a list of 5 elements) |
244 @end group | 245 @end group |
245 @end example | 246 @end example |
246 | 247 |
248 Substitution with @samp{,} is allowed at deeper levels of the list | |
249 structure also. For example: | |
250 | |
251 @example | |
252 @group | |
253 (defmacro t-becomes-nil (variable) | |
254 `(if (eq ,variable t) | |
255 (setq ,variable nil))) | |
256 @end group | |
257 | |
258 @group | |
259 (t-becomes-nil foo) | |
260 @equiv{} (if (eq foo t) (setq foo nil)) | |
261 @end group | |
262 @end example | |
263 | |
247 @findex ,@@ @r{(with Backquote)} | 264 @findex ,@@ @r{(with Backquote)} |
248 @cindex splicing (with backquote) | 265 @cindex splicing (with backquote) |
249 You can also @dfn{splice} an evaluated value into the resulting list, | 266 You can also @dfn{splice} an evaluated value into the resulting list, |
250 using the special marker @samp{,@@}. The elements of the spliced list | 267 using the special marker @samp{,@@}. The elements of the spliced list |
251 become elements at the same level as the other elements of the resulting | 268 become elements at the same level as the other elements of the resulting |
252 list. The equivalent code without using @samp{`} is often unreadable. | 269 list. The equivalent code without using @samp{`} is often unreadable. |
253 Here are some examples: | 270 Here are some examples: |
254 | 271 |
289 extra level of parentheses surrounding both the @samp{,} or @samp{,@@} | 306 extra level of parentheses surrounding both the @samp{,} or @samp{,@@} |
290 and the following expression. The old syntax required whitespace | 307 and the following expression. The old syntax required whitespace |
291 between the @samp{`}, @samp{,} or @samp{,@@} and the following | 308 between the @samp{`}, @samp{,} or @samp{,@@} and the following |
292 expression. | 309 expression. |
293 | 310 |
294 This syntax is still accepted, but no longer recommended except for | 311 This syntax is still accepted, for compatibility with old Emacs |
295 compatibility with old Emacs versions. | 312 versions, but we recommend not using it in new programs. |
296 @end quotation | 313 @end quotation |
297 | 314 |
298 @node Problems with Macros | 315 @node Problems with Macros |
299 @section Common Problems Using Macros | 316 @section Common Problems Using Macros |
300 | 317 |
486 in expressions ordinarily. | 503 in expressions ordinarily. |
487 | 504 |
488 @node Eval During Expansion | 505 @node Eval During Expansion |
489 @subsection Evaluating Macro Arguments in Expansion | 506 @subsection Evaluating Macro Arguments in Expansion |
490 | 507 |
491 Another problem can happen if you evaluate any of the macro argument | 508 Another problem can happen if you the macro definition itself |
492 expressions during the computation of the expansion, such as by calling | 509 evaluates any of the macro argument expressions, such as by calling |
493 @code{eval} (@pxref{Eval}). If the argument is supposed to refer to the | 510 @code{eval} (@pxref{Eval}). If the argument is supposed to refer to the |
494 user's variables, you may have trouble if the user happens to use a | 511 user's variables, you may have trouble if the user happens to use a |
495 variable with the same name as one of the macro arguments. Inside the | 512 variable with the same name as one of the macro arguments. Inside the |
496 macro body, the macro argument binding is the most local binding of this | 513 macro body, the macro argument binding is the most local binding of this |
497 variable, so any references inside the form being evaluated do refer | 514 variable, so any references inside the form being evaluated do refer to |
498 to it. Here is an example: | 515 it. Here is an example: |
499 | 516 |
500 @example | 517 @example |
501 @group | 518 @group |
502 (defmacro foo (a) | 519 (defmacro foo (a) |
503 (list 'setq (eval a) t)) | 520 (list 'setq (eval a) t)) |
526 with @code{eval}) don't occur and its local variable bindings don't | 543 with @code{eval}) don't occur and its local variable bindings don't |
527 exist. | 544 exist. |
528 | 545 |
529 The safe way to work with the run-time value of an expression is to | 546 The safe way to work with the run-time value of an expression is to |
530 put the expression into the macro expansion, so that its value is | 547 put the expression into the macro expansion, so that its value is |
531 computed as part of executing the expansion. | 548 computed as part of executing the expansion. This is what the other |
549 examples in this chapter do. | |
532 | 550 |
533 @node Repeated Expansion | 551 @node Repeated Expansion |
534 @subsection How Many Times is the Macro Expanded? | 552 @subsection How Many Times is the Macro Expanded? |
535 | 553 |
536 Occasionally problems result from the fact that a macro call is | 554 Occasionally problems result from the fact that a macro call is |