Update copyright year to 2014 by running admin/update-copyright.
[bpt/emacs.git] / doc / lispref / advice.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
ba318903 3@c Copyright (C) 1998-1999, 2001-2014 Free Software Foundation, Inc.
b8d4c8d0 4@c See the file elisp.texi for copying conditions.
ecc6530d 5@node Advising Functions
b8d4c8d0
GM
6@chapter Advising Emacs Lisp Functions
7@cindex advising functions
8
b8d4c8d0 9@cindex piece of advice
2200a8c9
CY
10 The @dfn{advice} feature lets you add to the existing definition of
11a function, by @dfn{advising the function}. A function can have
12multiple @dfn{pieces of advice}, each of which can be separately
13defined, and separately enabled or disabled (@pxref{Activation of
14Advice}). Each piece of advice can alter almost anything about the
15function, including its argument list, what the function does when it
16runs, and the value it returns.
17
18 Advice can be useful for altering the behavior of an existing
19function without having to redefine the whole function. However, it
20can be a source of bugs, since existing callers to the function may
21assume the old behavior, and work incorrectly when the behavior is
22changed by advice. Advice can also cause confusion in debugging, if
23the person doing the debugging does not notice or remember that the
24function has been modified by advice.
25
26 For these reasons, advice should be reserved for the cases where you
27cannot modify a function's behavior in any other way. If it is
28possible to do the same thing via a hook, that is preferable
29(@pxref{Hooks}). If you simply want to change what a particular key
30does, it may be better to write a new command, and remap the old
31command's key bindings to the new one (@pxref{Remapping Commands}).
32In particular, Emacs's own source files should not put advice on
33functions in Emacs. (There are currently a few exceptions to this
34convention, but we aim to correct them.)
35
36 Macros can also be advised, in much the same way as functions.
37However, special forms (@pxref{Special Forms}) cannot be advised.
38
39 It is possible to advise a primitive (@pxref{What Is a Function}),
40but one should typically @emph{not} do so, for two reasons. Firstly,
41some primitives are used by the advice mechanism, and advising them
42could cause an infinite recursion. Secondly, many primitives are
43called directly from C, and such calls ignore advice; hence, one ends
44up in a confusing situation where some calls (occurring from Lisp
45code) obey the advice and other calls (from C code) do not.
b8d4c8d0
GM
46
47@menu
48* Simple Advice:: A simple example to explain the basics of advice.
49* Defining Advice:: Detailed description of @code{defadvice}.
50* Around-Advice:: Wrapping advice around a function's definition.
51* Computed Advice:: ...is to @code{defadvice} as @code{fset} is to @code{defun}.
52* Activation of Advice:: Advice doesn't do anything until you activate it.
53* Enabling Advice:: You can enable or disable each piece of advice.
54* Preactivation:: Preactivation is a way of speeding up the
55 loading of compiled advice.
56* Argument Access in Advice:: How advice can access the function's arguments.
b8d4c8d0
GM
57* Combined Definition:: How advice is implemented.
58@end menu
59
60@node Simple Advice
61@section A Simple Advice Example
62
63 The command @code{next-line} moves point down vertically one or more
64lines; it is the standard binding of @kbd{C-n}. When used on the last
65line of the buffer, this command inserts a newline to create a line to
66move to if @code{next-line-add-newlines} is non-@code{nil} (its default
67is @code{nil}.)
68
69 Suppose you wanted to add a similar feature to @code{previous-line},
70which would insert a new line at the beginning of the buffer for the
71command to move to (when @code{next-line-add-newlines} is
72non-@code{nil}). How could you do this?
73
74 You could do it by redefining the whole function, but that is not
75modular. The advice feature provides a cleaner alternative: you can
76effectively add your code to the existing function definition, without
77actually changing or even seeing that definition. Here is how to do
78this:
79
80@example
81(defadvice previous-line (before next-line-at-end
82 (&optional arg try-vscroll))
83 "Insert an empty line when moving up from the top line."
84 (if (and next-line-add-newlines (= arg 1)
85 (save-excursion (beginning-of-line) (bobp)))
86 (progn
87 (beginning-of-line)
88 (newline))))
89@end example
90
91 This expression defines a @dfn{piece of advice} for the function
92@code{previous-line}. This piece of advice is named
93@code{next-line-at-end}, and the symbol @code{before} says that it is
94@dfn{before-advice} which should run before the regular definition of
95@code{previous-line}. @code{(&optional arg try-vscroll)} specifies
96how the advice code can refer to the function's arguments.
97
98 When this piece of advice runs, it creates an additional line, in the
99situation where that is appropriate, but does not move point to that
100line. This is the correct way to write the advice, because the normal
101definition will run afterward and will move back to the newly inserted
102line.
103
104 Defining the advice doesn't immediately change the function
105@code{previous-line}. That happens when you @dfn{activate} the advice,
106like this:
107
108@example
109(ad-activate 'previous-line)
110@end example
111
112@noindent
113This is what actually begins to use the advice that has been defined so
114far for the function @code{previous-line}. Henceforth, whenever that
115function is run, whether invoked by the user with @kbd{C-p} or
116@kbd{M-x}, or called from Lisp, it runs the advice first, and its
117regular definition second.
118
119 This example illustrates before-advice, which is one @dfn{class} of
120advice: it runs before the function's base definition. There are two
121other advice classes: @dfn{after-advice}, which runs after the base
122definition, and @dfn{around-advice}, which lets you specify an
123expression to wrap around the invocation of the base definition.
124
125@node Defining Advice
126@section Defining Advice
127@cindex defining advice
128@cindex advice, defining
129
130 To define a piece of advice, use the macro @code{defadvice}. A call
131to @code{defadvice} has the following syntax, which is based on the
132syntax of @code{defun} and @code{defmacro}, but adds more:
133
134@findex defadvice
135@example
136(defadvice @var{function} (@var{class} @var{name}
137 @r{[}@var{position}@r{]} @r{[}@var{arglist}@r{]}
138 @var{flags}...)
139 @r{[}@var{documentation-string}@r{]}
140 @r{[}@var{interactive-form}@r{]}
141 @var{body-forms}...)
142@end example
143
144@noindent
2200a8c9
CY
145Here, @var{function} is the name of the function (or macro) to be
146advised. From now on, we will write just ``function'' when describing
147the entity being advised, but this always includes macros.
b8d4c8d0
GM
148
149 In place of the argument list in an ordinary definition, an advice
150definition calls for several different pieces of information.
151
152@cindex class of advice
153@cindex before-advice
154@cindex after-advice
155@cindex around-advice
156@var{class} specifies the @dfn{class} of the advice---one of @code{before},
157@code{after}, or @code{around}. Before-advice runs before the function
158itself; after-advice runs after the function itself; around-advice is
159wrapped around the execution of the function itself. After-advice and
160around-advice can override the return value by setting
161@code{ad-return-value}.
162
163@defvar ad-return-value
164While advice is executing, after the function's original definition has
165been executed, this variable holds its return value, which will
166ultimately be returned to the caller after finishing all the advice.
167After-advice and around-advice can arrange to return some other value
168by storing it in this variable.
169@end defvar
170
171The argument @var{name} is the name of the advice, a non-@code{nil}
172symbol. The advice name uniquely identifies one piece of advice, within all
173the pieces of advice in a particular class for a particular
174@var{function}. The name allows you to refer to the piece of
175advice---to redefine it, or to enable or disable it.
176
177The optional @var{position} specifies where, in the current list of
178advice of the specified @var{class}, this new advice should be placed.
179It should be either @code{first}, @code{last} or a number that specifies
180a zero-based position (@code{first} is equivalent to 0). If no position
181is specified, the default is @code{first}. Position values outside the
182range of existing positions in this class are mapped to the beginning or
183the end of the range, whichever is closer. The @var{position} value is
184ignored when redefining an existing piece of advice.
185
186The optional @var{arglist} can be used to define the argument list for
187the sake of advice. This becomes the argument list of the combined
188definition that is generated in order to run the advice (@pxref{Combined
189Definition}). Therefore, the advice expressions can use the argument
190variables in this list to access argument values.
191
192The argument list used in advice need not be the same as the argument
193list used in the original function, but must be compatible with it, so
194that it can handle the ways the function is actually called. If two
195pieces of advice for a function both specify an argument list, they must
196specify the same argument list.
197
198@xref{Argument Access in Advice}, for more information about argument
199lists and advice, and a more flexible way for advice to access the
200arguments.
201
202The remaining elements, @var{flags}, are symbols that specify further
203information about how to use this piece of advice. Here are the valid
204symbols and their meanings:
205
206@table @code
207@item activate
208Activate the advice for @var{function} now. Changes in a function's
209advice always take effect the next time you activate advice for the
210function; this flag says to do so, for @var{function}, immediately after
211defining this piece of advice.
212
213@cindex forward advice
214This flag has no immediate effect if @var{function} itself is not defined yet (a
215situation known as @dfn{forward advice}), because it is impossible to
216activate an undefined function's advice. However, defining
217@var{function} will automatically activate its advice.
218
219@item protect
220Protect this piece of advice against non-local exits and errors in
221preceding code and advice. Protecting advice places it as a cleanup in
222an @code{unwind-protect} form, so that it will execute even if the
223previous code gets an error or uses @code{throw}. @xref{Cleanups}.
224
225@item compile
226Compile the combined definition that is used to run the advice. This
227flag is ignored unless @code{activate} is also specified.
228@xref{Combined Definition}.
229
230@item disable
231Initially disable this piece of advice, so that it will not be used
232unless subsequently explicitly enabled. @xref{Enabling Advice}.
233
234@item preactivate
235Activate advice for @var{function} when this @code{defadvice} is
236compiled or macroexpanded. This generates a compiled advised definition
237according to the current advice state, which will be used during
238activation if appropriate. @xref{Preactivation}.
239
240This is useful only if this @code{defadvice} is byte-compiled.
241@end table
242
243The optional @var{documentation-string} serves to document this piece of
244advice. When advice is active for @var{function}, the documentation for
245@var{function} (as returned by @code{documentation}) combines the
246documentation strings of all the advice for @var{function} with the
247documentation string of its original function definition.
248
249The optional @var{interactive-form} form can be supplied to change the
250interactive behavior of the original function. If more than one piece
251of advice has an @var{interactive-form}, then the first one (the one
252with the smallest position) found among all the advice takes precedence.
253
254The possibly empty list of @var{body-forms} specifies the body of the
255advice. The body of an advice can access or change the arguments, the
256return value, the binding environment, and perform any other kind of
257side effect.
258
259@strong{Warning:} When you advise a macro, keep in mind that macros are
260expanded when a program is compiled, not when a compiled program is run.
261All subroutines used by the advice need to be available when the byte
262compiler expands the macro.
263
264@deffn Command ad-unadvise function
25dec365 265This command deletes all pieces of advice from @var{function}.
b8d4c8d0
GM
266@end deffn
267
268@deffn Command ad-unadvise-all
269This command deletes all pieces of advice from all functions.
270@end deffn
271
272@node Around-Advice
273@section Around-Advice
274
275 Around-advice lets you ``wrap'' a Lisp expression ``around'' the
276original function definition. You specify where the original function
277definition should go by means of the special symbol @code{ad-do-it}.
278Where this symbol occurs inside the around-advice body, it is replaced
279with a @code{progn} containing the forms of the surrounded code. Here
280is an example:
281
282@example
283(defadvice foo (around foo-around)
284 "Ignore case in `foo'."
285 (let ((case-fold-search t))
286 ad-do-it))
287@end example
288
289@noindent
290Its effect is to make sure that case is ignored in
291searches when the original definition of @code{foo} is run.
292
293@defvar ad-do-it
294This is not really a variable, rather a place-holder that looks like a
295variable. You use it in around-advice to specify the place to run the
296function's original definition and other ``earlier'' around-advice.
297@end defvar
298
299If the around-advice does not use @code{ad-do-it}, then it does not run
300the original function definition. This provides a way to override the
301original definition completely. (It also overrides lower-positioned
302pieces of around-advice).
303
304If the around-advice uses @code{ad-do-it} more than once, the original
305definition is run at each place. In this way, around-advice can execute
306the original definition (and lower-positioned pieces of around-advice)
307several times. Another way to do that is by using @code{ad-do-it}
308inside of a loop.
309
310@node Computed Advice
311@section Computed Advice
312
313The macro @code{defadvice} resembles @code{defun} in that the code for
314the advice, and all other information about it, are explicitly stated in
315the source code. You can also create advice whose details are computed,
316using the function @code{ad-add-advice}.
317
318@defun ad-add-advice function advice class position
319Calling @code{ad-add-advice} adds @var{advice} as a piece of advice to
794f204b 320@var{function} in class @var{class}. The argument @var{advice} has
b8d4c8d0
GM
321this form:
322
323@example
324(@var{name} @var{protected} @var{enabled} @var{definition})
325@end example
326
794f204b
CY
327@noindent
328Here, @var{protected} and @var{enabled} are flags; if @var{protected}
329is non-@code{nil}, the advice is protected against non-local exits
330(@pxref{Defining Advice}), and if @var{enabled} is @code{nil} the
331advice is initially disabled (@pxref{Enabling Advice}).
332@var{definition} should have the form
333
334@example
8b4ef1fc 335(advice . @var{lambda})
794f204b
CY
336@end example
337
338@noindent
8b4ef1fc
CY
339where @var{lambda} is a lambda expression; this lambda expression is
340called in order to perform the advice. @xref{Lambda Expressions}.
794f204b
CY
341
342If the @var{function} argument to @code{ad-add-advice} already has one
343or more pieces of advice in the specified @var{class}, then
344@var{position} specifies where in the list to put the new piece of
345advice. The value of @var{position} can either be @code{first},
346@code{last}, or a number (counting from 0 at the beginning of the
347list). Numbers outside the range are mapped to the beginning or the
348end of the range, whichever is closer. The @var{position} value is
349ignored when redefining an existing piece of advice.
b8d4c8d0
GM
350
351If @var{function} already has a piece of @var{advice} with the same
352name, then the position argument is ignored and the old advice is
353replaced with the new one.
354@end defun
355
356@node Activation of Advice
357@section Activation of Advice
358@cindex activating advice
359@cindex advice, activating
360
361By default, advice does not take effect when you define it---only when
25dec365
CY
362you @dfn{activate} advice for the function. However, the advice will
363be activated automatically if you define or redefine the function
364later. You can request the activation of advice for a function when
365you define the advice, by specifying the @code{activate} flag in the
366@code{defadvice}; or you can activate the advice separately by calling
367the function @code{ad-activate} or one of the other activation
368commands listed below.
b8d4c8d0
GM
369
370Separating the activation of advice from the act of defining it permits
371you to add several pieces of advice to one function efficiently, without
372redefining the function over and over as each advice is added. More
373importantly, it permits defining advice for a function before that
374function is actually defined.
375
376When a function's advice is first activated, the function's original
377definition is saved, and all enabled pieces of advice for that function
378are combined with the original definition to make a new definition.
379(Pieces of advice that are currently disabled are not used; see
380@ref{Enabling Advice}.) This definition is installed, and optionally
381byte-compiled as well, depending on conditions described below.
382
383In all of the commands to activate advice, if @var{compile} is
384@code{t} (or anything but @code{nil} or a negative number), the
385command also compiles the combined definition which implements the
386advice. If it is @code{nil} or a negative number, what happens
387depends on @code{ad-default-compilation-action} as described below.
388
389@deffn Command ad-activate function &optional compile
390This command activates all the advice defined for @var{function}.
391@end deffn
392
393 Activating advice does nothing if @var{function}'s advice is already
394active. But if there is new advice, added since the previous time you
395activated advice for @var{function}, it activates the new advice.
396
397@deffn Command ad-deactivate function
398This command deactivates the advice for @var{function}.
399@cindex deactivating advice
400@c @cindex advice, deactivating "advice, activating" is just above
401@end deffn
402
403@deffn Command ad-update function &optional compile
404This command activates the advice for @var{function}
405if its advice is already activated. This is useful
406if you change the advice.
407@end deffn
408
409@deffn Command ad-activate-all &optional compile
410This command activates the advice for all functions.
411@end deffn
412
413@deffn Command ad-deactivate-all
414This command deactivates the advice for all functions.
415@end deffn
416
417@deffn Command ad-update-all &optional compile
418This command activates the advice for all functions
419whose advice is already activated. This is useful
420if you change the advice of some functions.
421@end deffn
422
423@deffn Command ad-activate-regexp regexp &optional compile
424This command activates all pieces of advice whose names match
425@var{regexp}. More precisely, it activates all advice for any function
426which has at least one piece of advice that matches @var{regexp}.
427@end deffn
428
429@deffn Command ad-deactivate-regexp regexp
430This command deactivates all pieces of advice whose names match
431@var{regexp}. More precisely, it deactivates all advice for any
432function which has at least one piece of advice that matches
433@var{regexp}.
434@end deffn
435
436@deffn Command ad-update-regexp regexp &optional compile
437This command activates pieces of advice whose names match @var{regexp},
438but only those for functions whose advice is already activated.
439@cindex reactivating advice
440
441Reactivating a function's advice is useful for putting into effect all
442the changes that have been made in its advice (including enabling and
443disabling specific pieces of advice; @pxref{Enabling Advice}) since the
444last time it was activated.
445@end deffn
446
447@deffn Command ad-start-advice
448Turn on automatic advice activation when a function is defined or
449redefined. This is the default mode.
450@end deffn
451
452@deffn Command ad-stop-advice
453Turn off automatic advice activation when a function is defined or
454redefined.
455@end deffn
456
457@defopt ad-default-compilation-action
458This variable controls whether to compile the combined definition
459that results from activating advice for a function.
460
461A value of @code{always} specifies to compile unconditionally.
462A value of @code{never} specifies never compile the advice.
463
b1baed0b 464A value of @code{maybe} specifies to compile if the byte compiler is
b8d4c8d0
GM
465already loaded. A value of @code{like-original} specifies to compile
466the advice if the original definition of the advised function is
467compiled or a built-in function.
468
469This variable takes effect only if the @var{compile} argument of
470@code{ad-activate} (or any of the above functions) did not force
471compilation.
472@end defopt
473
474 If the advised definition was constructed during ``preactivation''
475(@pxref{Preactivation}), then that definition must already be compiled,
476because it was constructed during byte-compilation of the file that
477contained the @code{defadvice} with the @code{preactivate} flag.
478
479@node Enabling Advice
480@section Enabling and Disabling Advice
481@cindex enabling advice
482@cindex advice, enabling and disabling
483@cindex disabling advice
484
485 Each piece of advice has a flag that says whether it is enabled or
486not. By enabling or disabling a piece of advice, you can turn it on
487and off without having to undefine and redefine it. For example, here is
488how to disable a particular piece of advice named @code{my-advice} for
489the function @code{foo}:
490
491@example
492(ad-disable-advice 'foo 'before 'my-advice)
493@end example
494
495 This function by itself only changes the enable flag for a piece of
496advice. To make the change take effect in the advised definition, you
497must activate the advice for @code{foo} again:
498
499@example
500(ad-activate 'foo)
501@end example
502
503@deffn Command ad-disable-advice function class name
504This command disables the piece of advice named @var{name} in class
505@var{class} on @var{function}.
506@end deffn
507
508@deffn Command ad-enable-advice function class name
509This command enables the piece of advice named @var{name} in class
510@var{class} on @var{function}.
511@end deffn
512
513 You can also disable many pieces of advice at once, for various
514functions, using a regular expression. As always, the changes take real
515effect only when you next reactivate advice for the functions in
516question.
517
518@deffn Command ad-disable-regexp regexp
519This command disables all pieces of advice whose names match
520@var{regexp}, in all classes, on all functions.
521@end deffn
522
523@deffn Command ad-enable-regexp regexp
524This command enables all pieces of advice whose names match
525@var{regexp}, in all classes, on all functions.
526@end deffn
527
528@node Preactivation
529@section Preactivation
530@cindex preactivating advice
531@cindex advice, preactivating
532
533 Constructing a combined definition to execute advice is moderately
534expensive. When a library advises many functions, this can make loading
535the library slow. In that case, you can use @dfn{preactivation} to
536construct suitable combined definitions in advance.
537
538 To use preactivation, specify the @code{preactivate} flag when you
539define the advice with @code{defadvice}. This @code{defadvice} call
540creates a combined definition which embodies this piece of advice
541(whether enabled or not) plus any other currently enabled advice for the
542same function, and the function's own definition. If the
543@code{defadvice} is compiled, that compiles the combined definition
544also.
545
546 When the function's advice is subsequently activated, if the enabled
547advice for the function matches what was used to make this combined
548definition, then the existing combined definition is used, thus avoiding
549the need to construct one. Thus, preactivation never causes wrong
550results---but it may fail to do any good, if the enabled advice at the
551time of activation doesn't match what was used for preactivation.
552
553 Here are some symptoms that can indicate that a preactivation did not
554work properly, because of a mismatch.
555
556@itemize @bullet
557@item
558Activation of the advised
559function takes longer than usual.
560@item
b1baed0b 561The byte compiler gets
b8d4c8d0
GM
562loaded while an advised function gets activated.
563@item
564@code{byte-compile} is included in the value of @code{features} even
b1baed0b 565though you did not ever explicitly use the byte compiler.
b8d4c8d0
GM
566@end itemize
567
568Compiled preactivated advice works properly even if the function itself
569is not defined until later; however, the function needs to be defined
570when you @emph{compile} the preactivated advice.
571
572There is no elegant way to find out why preactivated advice is not being
573used. What you can do is to trace the function
574@code{ad-cache-id-verification-code} (with the function
575@code{trace-function-background}) before the advised function's advice
576is activated. After activation, check the value returned by
577@code{ad-cache-id-verification-code} for that function: @code{verified}
578means that the preactivated advice was used, while other values give
579some information about why they were considered inappropriate.
580
581 @strong{Warning:} There is one known case that can make preactivation
582fail, in that a preconstructed combined definition is used even though
583it fails to match the current state of advice. This can happen when two
584packages define different pieces of advice with the same name, in the
585same class, for the same function. But you should avoid that anyway.
586
587@node Argument Access in Advice
588@section Argument Access in Advice
589
590 The simplest way to access the arguments of an advised function in the
591body of a piece of advice is to use the same names that the function
592definition uses. To do this, you need to know the names of the argument
593variables of the original function.
594
595 While this simple method is sufficient in many cases, it has a
596disadvantage: it is not robust, because it hard-codes the argument names
597into the advice. If the definition of the original function changes,
598the advice might break.
599
600 Another method is to specify an argument list in the advice itself.
601This avoids the need to know the original function definition's argument
602names, but it has a limitation: all the advice on any particular
603function must use the same argument list, because the argument list
604actually used for all the advice comes from the first piece of advice
605for that function.
606
607 A more robust method is to use macros that are translated into the
608proper access forms at activation time, i.e., when constructing the
4b1ed1bb
CY
609advised definition. Access macros access actual arguments by their
610(zero-based) position, regardless of how these actual arguments get
611distributed onto the argument variables of a function. This is robust
612because in Emacs Lisp the meaning of an argument is strictly
613determined by its position in the argument list.
b8d4c8d0
GM
614
615@defmac ad-get-arg position
616This returns the actual argument that was supplied at @var{position}.
617@end defmac
618
619@defmac ad-get-args position
620This returns the list of actual arguments supplied starting at
621@var{position}.
622@end defmac
623
624@defmac ad-set-arg position value
625This sets the value of the actual argument at @var{position} to
626@var{value}
627@end defmac
628
629@defmac ad-set-args position value-list
630This sets the list of actual arguments starting at @var{position} to
631@var{value-list}.
632@end defmac
633
634 Now an example. Suppose the function @code{foo} is defined as
635
636@example
637(defun foo (x y &optional z &rest r) ...)
638@end example
639
640@noindent
641and is then called with
642
643@example
644(foo 0 1 2 3 4 5 6)
645@end example
646
647@noindent
648which means that @var{x} is 0, @var{y} is 1, @var{z} is 2 and @var{r} is
649@code{(3 4 5 6)} within the body of @code{foo}. Here is what
650@code{ad-get-arg} and @code{ad-get-args} return in this case:
651
652@example
653(ad-get-arg 0) @result{} 0
654(ad-get-arg 1) @result{} 1
655(ad-get-arg 2) @result{} 2
656(ad-get-arg 3) @result{} 3
657(ad-get-args 2) @result{} (2 3 4 5 6)
658(ad-get-args 4) @result{} (4 5 6)
659@end example
660
661 Setting arguments also makes sense in this example:
662
663@example
664(ad-set-arg 5 "five")
665@end example
666
667@noindent
668has the effect of changing the sixth argument to @code{"five"}. If this
669happens in advice executed before the body of @code{foo} is run, then
670@var{r} will be @code{(3 4 "five" 6)} within that body.
671
672 Here is an example of setting a tail of the argument list:
673
674@example
675(ad-set-args 0 '(5 4 3 2 1 0))
676@end example
677
678@noindent
679If this happens in advice executed before the body of @code{foo} is run,
680then within that body, @var{x} will be 5, @var{y} will be 4, @var{z}
681will be 3, and @var{r} will be @code{(2 1 0)} inside the body of
682@code{foo}.
683
684 These argument constructs are not really implemented as Lisp macros.
685Instead they are implemented specially by the advice mechanism.
686
b8d4c8d0
GM
687@node Combined Definition
688@section The Combined Definition
689
690 Suppose that a function has @var{n} pieces of before-advice
691(numbered from 0 through @var{n}@minus{}1), @var{m} pieces of
692around-advice and @var{k} pieces of after-advice. Assuming no piece
693of advice is protected, the combined definition produced to implement
694the advice for a function looks like this:
695
696@example
697(lambda @var{arglist}
698 @r{[} @r{[}@var{advised-docstring}@r{]} @r{[}(interactive ...)@r{]} @r{]}
699 (let (ad-return-value)
700 @r{before-0-body-form}...
701 ....
702 @r{before-@var{n}@minus{}1-body-form}...
703 @r{around-0-body-form}...
704 @r{around-1-body-form}...
705 ....
706 @r{around-@var{m}@minus{}1-body-form}...
707 (setq ad-return-value
708 @r{apply original definition to @var{arglist}})
709 @r{end-of-around-@var{m}@minus{}1-body-form}...
710 ....
711 @r{end-of-around-1-body-form}...
712 @r{end-of-around-0-body-form}...
713 @r{after-0-body-form}...
714 ....
715 @r{after-@var{k}@minus{}1-body-form}...
716 ad-return-value))
717@end example
718
719Macros are redefined as macros, which means adding @code{macro} to
720the beginning of the combined definition.
721
722The interactive form is present if the original function or some piece
723of advice specifies one. When an interactive primitive function is
724advised, advice uses a special method: it calls the primitive with
725@code{call-interactively} so that it will read its own arguments.
726In this case, the advice cannot access the arguments.
727
728The body forms of the various advice in each class are assembled
729according to their specified order. The forms of around-advice @var{l}
730are included in one of the forms of around-advice @var{l} @minus{} 1.
731
732The innermost part of the around advice onion is
733
734@display
735apply original definition to @var{arglist}
736@end display
737
738@noindent
739whose form depends on the type of the original function. The variable
740@code{ad-return-value} is set to whatever this returns. The variable is
741visible to all pieces of advice, which can access and modify it before
742it is actually returned from the advised function.
743
744The semantic structure of advised functions that contain protected
745pieces of advice is the same. The only difference is that
746@code{unwind-protect} forms ensure that the protected advice gets
747executed even if some previous piece of advice had an error or a
748non-local exit. If any around-advice is protected, then the whole
749around-advice onion is protected as a result.