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