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