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