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