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