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