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