comparison lispref/macros.texi @ 89910:548375b6b1f8

Update unicode branch
author Miles Bader <miles@gnu.org>
date Mon, 19 Apr 2004 07:01:43 +0000
parents 375f2633d815
children 59dcbfe97385
comparison
equal deleted inserted replaced
89909:68c22ea6027c 89910:548375b6b1f8
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, 1995, 1998 Free Software Foundation, Inc. 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 2004 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, Customization, Functions, Top 6 @node Macros, Customization, Functions, Top
7 @chapter Macros 7 @chapter Macros
8 @cindex macros 8 @cindex macros
28 * Compiling Macros:: How macros are expanded by the compiler. 28 * Compiling Macros:: How macros are expanded by the compiler.
29 * Defining Macros:: How to write a macro definition. 29 * Defining Macros:: How to write a macro definition.
30 * Backquote:: Easier construction of list structure. 30 * Backquote:: Easier construction of list structure.
31 * Problems with Macros:: Don't evaluate the macro arguments too many times. 31 * Problems with Macros:: Don't evaluate the macro arguments too many times.
32 Don't hide the user's variables. 32 Don't hide the user's variables.
33 * Indenting Macros:: Specifying how to indent macro calls.
33 @end menu 34 @end menu
34 35
35 @node Simple Macro 36 @node Simple Macro
36 @section A Simple Example of a Macro 37 @section A Simple Example of a Macro
37 38
132 @group 133 @group
133 (macroexpand '(inc2 r s)) 134 (macroexpand '(inc2 r s))
134 @result{} (progn (inc r) (inc s)) ; @r{@code{inc} not expanded here.} 135 @result{} (progn (inc r) (inc s)) ; @r{@code{inc} not expanded here.}
135 @end group 136 @end group
136 @end smallexample 137 @end smallexample
138 @end defun
139
140
141 @defun macroexpand-all form &optional environment
142 @cindex macro expansion in entire form
143
144 @code{macroexpand-all} expands macros like @code{macroexpand}, but
145 will look for and expand all macros in @var{form}, not just at the
146 top-level.
147
148 In emacs-lisp, @code{macroexpand-all} guarantees that if no macros
149 are expanded, the return value will be @code{eq} to @var{form}.
150
151 Repeating the example used for @code{macroexpand} above with
152 @code{macroexpand-all}, we see that @code{macroexpand-all} @emph{does}
153 expand the embedded calls to @code{inc}:
154
155 @smallexample
156 (macroexpand-all '(inc2 r s))
157 @result{} (progn (setq r (1+ r)) (setq s (1+ s)))
158 @end smallexample
159
137 @end defun 160 @end defun
138 161
139 @node Compiling Macros 162 @node Compiling Macros
140 @section Macros and Byte Compilation 163 @section Macros and Byte Compilation
141 @cindex byte-compiling macros 164 @cindex byte-compiling macros
203 (@pxref{Argument List}). Macros may have a documentation string, but 226 (@pxref{Argument List}). Macros may have a documentation string, but
204 any @code{interactive} declaration is ignored since macros cannot be 227 any @code{interactive} declaration is ignored since macros cannot be
205 called interactively. 228 called interactively.
206 @end defspec 229 @end defspec
207 230
231 The body of the macro definition can include a @code{declare} form,
232 which can specify how @key{TAB} should indent macro calls, and how to
233 step through them for Edebug.
234
235 @anchor{Definition of declare}
236 @defmac declare @var{specs}@dots{}
237 A @code{declare} form is used in a macro definition to specify various
238 additional information about it. Two kinds of specification are
239 currently supported:
240
241 @table @code
242 @item (edebug @var{edebug-form-spec})
243 Specify how to step through macro calls for Edebug.
244 @xref{Instrumenting Macro Calls}, for more details.
245
246 @item (indent @var{indent-spec})
247 Specify how to indent calls to this macro. @xref{Indenting Macros},
248 for more details.
249 @end table
250
251 A @code{declare} form only has its special effect in the body of a
252 @code{defmacro} form if it immediately follows the documentation
253 string, if present, or the argument list otherwise. (Strictly
254 speaking, @emph{several} @code{declare} forms can follow the
255 documentation string or argument list, but since a @code{declare} form
256 can have several @var{specs}, they can always be combined into a
257 single form.) When used at other places in a @code{defmacro} form, or
258 outside a @code{defmacro} form, @code{declare} just returns @code{nil}
259 without evaluating any @var{specs}.
260 @end defmac
261
262 No macro absolutely needs a @code{declare} form, because that form
263 has no effect on how the macro expands, on what the macro means in the
264 program. It only affects secondary features: indentation and Edebug.
265
208 @node Backquote 266 @node Backquote
209 @section Backquote 267 @section Backquote
210 @cindex backquote (list substitution) 268 @cindex backquote (list substitution)
211 @cindex ` (list substitution) 269 @cindex ` (list substitution)
212 @findex ` 270 @findex `
329 @end menu 387 @end menu
330 388
331 @node Wrong Time 389 @node Wrong Time
332 @subsection Wrong Time 390 @subsection Wrong Time
333 391
334 The most common problem in writing macros is doing too some of the 392 The most common problem in writing macros is doing some of the
335 real work prematurely---while expanding the macro, rather than in the 393 real work prematurely---while expanding the macro, rather than in the
336 expansion itself. For instance, one real package had this nmacro 394 expansion itself. For instance, one real package had this macro
337 definition: 395 definition:
338 396
339 @example 397 @example
340 (defmacro my-set-buffer-multibyte (arg) 398 (defmacro my-set-buffer-multibyte (arg)
341 (if (fboundp 'set-buffer-multibyte) 399 (if (fboundp 'set-buffer-multibyte)
634 One way to avoid pathological cases like this is to think of 692 One way to avoid pathological cases like this is to think of
635 @code{empty-object} as a funny kind of constant, not as a memory 693 @code{empty-object} as a funny kind of constant, not as a memory
636 allocation construct. You wouldn't use @code{setcar} on a constant such 694 allocation construct. You wouldn't use @code{setcar} on a constant such
637 as @code{'(nil)}, so naturally you won't use it on @code{(empty-object)} 695 as @code{'(nil)}, so naturally you won't use it on @code{(empty-object)}
638 either. 696 either.
697
698 @node Indenting Macros
699 @section Indenting Macros
700
701 You can use the @code{declare} form in the macro definition to
702 specify how to @key{TAB} should indent indent calls to the macro. You
703 write it like this:
704
705 @example
706 (declare (indent @var{indent-spec}))
707 @end example
708
709 @noindent
710 Here are the possibilities for @var{indent-spec}:
711
712 @table @asis
713 @item @code{nil}
714 This is the same as no property---use the standard indentation pattern.
715 @item @code{defun}
716 Handle this function like a @samp{def} construct: treat the second
717 line as the start of a @dfn{body}.
718 @item a number, @var{number}
719 The first @var{number} arguments of the function are
720 @dfn{distinguished} arguments; the rest are considered the body
721 of the expression. A line in the expression is indented according to
722 whether the first argument on it is distinguished or not. If the
723 argument is part of the body, the line is indented @code{lisp-body-indent}
724 more columns than the open-parenthesis starting the containing
725 expression. If the argument is distinguished and is either the first
726 or second argument, it is indented @emph{twice} that many extra columns.
727 If the argument is distinguished and not the first or second argument,
728 the line uses the standard pattern.
729 @item a symbol, @var{symbol}
730 @var{symbol} should be a function name; that function is called to
731 calculate the indentation of a line within this expression. The
732 function receives two arguments:
733 @table @asis
734 @item @var{state}
735 The value returned by @code{parse-partial-sexp} (a Lisp primitive for
736 indentation and nesting computation) when it parses up to the
737 beginning of this line.
738 @item @var{pos}
739 The position at which the line being indented begins.
740 @end table
741 @noindent
742 It should return either a number, which is the number of columns of
743 indentation for that line, or a list whose car is such a number. The
744 difference between returning a number and returning a list is that a
745 number says that all following lines at the same nesting level should
746 be indented just like this one; a list says that following lines might
747 call for different indentations. This makes a difference when the
748 indentation is being computed by @kbd{C-M-q}; if the value is a
749 number, @kbd{C-M-q} need not recalculate indentation for the following
750 lines until the end of the list.
751 @end table
752
753 @ignore
754 arch-tag: d4cce66d-1047-45c3-bfde-db6719d6e82b
755 @end ignore