comparison lispref/advice.texi @ 22252:40089afa2b1d

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Tue, 26 May 1998 18:56:56 +0000
parents d4ac295a98b3
children e41ee9a517aa
comparison
equal deleted inserted replaced
22251:5989fa41cda6 22252:40089afa2b1d
10 The @dfn{advice} feature lets you add to the existing definition of a 10 The @dfn{advice} feature lets you add to the existing definition of a
11 function, by @dfn{advising the function}. This is a clean method for a 11 function, by @dfn{advising the function}. This is a clean method for a
12 library to customize functions defined by other parts of Emacs---cleaner 12 library to customize functions defined by other parts of Emacs---cleaner
13 than redefining the whole function. 13 than redefining the whole function.
14 14
15 Each piece of advice can be enabled or disabled explicitly. The 15 @cindex piece of advice
16 enabled pieces of advice for any given function actually take effect 16 Each function can have multiple @dfn{pieces of advice}, separately
17 when you activate advice for that function, or when that function is 17 defined. Each defined piece of advice can be enabled or disabled
18 subsequently defined or redefined. 18 explicitly. The enabled pieces of advice for any given function
19 actually take effect when you @dfn{activate} advice for that function, or when
20 that function is subsequently defined or redefined.
19 21
20 @strong{Usage Note:} Advice is useful for altering the behavior of 22 @strong{Usage Note:} Advice is useful for altering the behavior of
21 existing calls to an existing function. If you want the new behavior 23 existing calls to an existing function. If you want the new behavior
22 for new calls, or for key bindings, it is cleaner to define a new 24 for new calls, or for key bindings, it is cleaner to define a new
23 function (or a new command) which uses the existing function. 25 function (or a new command) which uses the existing function.
24 26
25 @menu 27 @menu
26 * Simple Advice:: A simple example to explain the basics of advice. 28 * Simple Advice:: A simple example to explain the basics of advice.
27 * Defining Advice:: Detailed description of @code{defadvice}. 29 * Defining Advice:: Detailed description of @code{defadvice}.
30 * Around-Advice:: Wrapping advice around a function's definition.
28 * Computed Advice:: ...is to @code{defadvice} as @code{fset} is to @code{defun}. 31 * Computed Advice:: ...is to @code{defadvice} as @code{fset} is to @code{defun}.
29 * Activation of Advice:: Advice doesn't do anything until you activate it. 32 * Activation of Advice:: Advice doesn't do anything until you activate it.
30 * Enabling Advice:: You can enable or disable each piece of advice. 33 * Enabling Advice:: You can enable or disable each piece of advice.
31 * Preactivation:: Preactivation is a way of speeding up the 34 * Preactivation:: Preactivation is a way of speeding up the
32 loading of compiled advice. 35 loading of compiled advice.
33 * Argument Access:: How advice can access the function's arguments. 36 * Argument Access in Advice:: How advice can access the function's arguments.
34 * Subr Arguments:: Accessing arguments when advising a primitive. 37 * Subr Arguments:: Accessing arguments when advising a primitive.
35 * Combined Definition:: How advice is implemented. 38 * Combined Definition:: How advice is implemented.
36 @end menu 39 @end menu
37 40
38 @node Simple Advice 41 @node Simple Advice
61 (progn 64 (progn
62 (beginning-of-line) 65 (beginning-of-line)
63 (newline)))) 66 (newline))))
64 @end example 67 @end example
65 68
66 @cindex piece of advice
67 This expression defines a @dfn{piece of advice} for the function 69 This expression defines a @dfn{piece of advice} for the function
68 @code{previous-line}. This piece of advice is named 70 @code{previous-line}. This piece of advice is named
69 @code{next-line-at-end}, and the symbol @code{before} says that it is 71 @code{next-line-at-end}, and the symbol @code{before} says that it is
70 @dfn{before-advice} which should run before the regular definition of 72 @dfn{before-advice} which should run before the regular definition of
71 @code{previous-line}. @code{(arg)} specifies how the advice code can 73 @code{previous-line}. @code{(arg)} specifies how the advice code can
98 definition, and @dfn{around-advice}, which lets you specify an 100 definition, and @dfn{around-advice}, which lets you specify an
99 expression to wrap around the invocation of the base definition. 101 expression to wrap around the invocation of the base definition.
100 102
101 @node Defining Advice 103 @node Defining Advice
102 @section Defining Advice 104 @section Defining Advice
105 @cindex defining advice
106 @cindex advice, defining
103 107
104 To define a piece of advice, use the macro @code{defadvice}. A call 108 To define a piece of advice, use the macro @code{defadvice}. A call
105 to @code{defadvice} has the following syntax, which is based on the 109 to @code{defadvice} has the following syntax, which is based on the
106 syntax of @code{defun}/@code{defmacro} but adds more: 110 syntax of @code{defun} and @code{defmacro}, but adds more:
107 111
108 @findex defadvice 112 @findex defadvice
109 @example 113 @example
110 (defadvice @var{function} (@var{class} @var{name} 114 (defadvice @var{function} (@var{class} @var{name}
111 @r{[}@var{position}@r{]} @r{[}@var{arglist}@r{]} 115 @r{[}@var{position}@r{]} @r{[}@var{arglist}@r{]}
130 itself; after-advice runs after the function itself; around-advice is 134 itself; after-advice runs after the function itself; around-advice is
131 wrapped around the execution of the function itself. After-advice and 135 wrapped around the execution of the function itself. After-advice and
132 around-advice can override the return value by setting 136 around-advice can override the return value by setting
133 @code{ad-return-value}. 137 @code{ad-return-value}.
134 138
135 Around-advice specifies where the ``original'' function definition 139 @defvar ad-return-value
136 should go by means of the special symbol @code{ad-do-it}. Where this 140 While advice is executing, after the function's original definition has
137 symbol occurs inside the around-advice body, it is replaced with a 141 been executed, this variable holds its return value, which will
138 @code{progn} containing the forms of the surrounded code. If the 142 ultimately be returned to the caller after finishing all the advice.
139 around-advice does not use @code{ad-do-it}, then the original function 143 After-advice and around-advice can arrange to return some other value
140 definition is never run. This provides a way to override the original 144 by storing it in this variable.
141 definition completely. (It also overrides lower-positioned pieces of 145 @end defvar
142 around-advice).
143 146
144 The argument @var{name} is the name of the advice, a non-@code{nil} 147 The argument @var{name} is the name of the advice, a non-@code{nil}
145 symbol. The advice name uniquely identifies one piece of advice, within all 148 symbol. The advice name uniquely identifies one piece of advice, within all
146 the pieces of advice in a particular class for a particular 149 the pieces of advice in a particular class for a particular
147 @var{function}. The name allows you to refer to the piece of 150 @var{function}. The name allows you to refer to the piece of
150 In place of the argument list in an ordinary definition, an advice 153 In place of the argument list in an ordinary definition, an advice
151 definition calls for several different pieces of information. 154 definition calls for several different pieces of information.
152 155
153 The optional @var{position} specifies where, in the current list of 156 The optional @var{position} specifies where, in the current list of
154 advice of the specified @var{class}, this new advice should be placed. 157 advice of the specified @var{class}, this new advice should be placed.
155 It should be either @code{first}, @code{last} or a number that 158 It should be either @code{first}, @code{last} or a number that specifies
156 specifies a zero-based position (@code{first} is equivalent to 0). If 159 a zero-based position (@code{first} is equivalent to 0). If no position
157 no position is specified, the default is @code{first}. The 160 is specified, the default is @code{first}. Position values outside the
158 @var{position} value is ignored when redefining an existing piece of 161 range of existing positions in this class are mapped to the beginning or
159 advice. 162 the end of the range, whichever is closer. The @var{position} value is
163 ignored when redefining an existing piece of advice.
160 164
161 The optional @var{arglist} can be used to define the argument list for 165 The optional @var{arglist} can be used to define the argument list for
162 the sake of advice. This becomes the argument list of the combined 166 the sake of advice. This becomes the argument list of the combined
163 definition that is generated in order to run the advice (@pxref{Combined 167 definition that is generated in order to run the advice (@pxref{Combined
164 Definition}). Therefore, the advice expressions can use the argument 168 Definition}). Therefore, the advice expressions can use the argument
166 170
167 This argument list must be compatible with the argument list of the 171 This argument list must be compatible with the argument list of the
168 original function, so that it can handle the ways the function is 172 original function, so that it can handle the ways the function is
169 actually called. If more than one piece of advice specifies an argument 173 actually called. If more than one piece of advice specifies an argument
170 list, then the first one (the one with the smallest position) found in 174 list, then the first one (the one with the smallest position) found in
171 the list of all classes of advice is used. Numbers outside the range 175 the list of all classes of advice is used.
172 are mapped to the beginning or the end, whichever is closer. 176
173 177 The remaining elements, @var{flags}, are symbols that specify further
174 The remaining elements, @var{flags}, is a list of symbols that specify 178 information about how to use this piece of advice. Here are the valid
175 further information about how to use this piece of advice. Here are the 179 symbols and their meanings:
176 valid symbols and their meanings:
177 180
178 @table @code 181 @table @code
179 @item activate 182 @item activate
180 Activate the advice for @var{function} now. Changes in a function's 183 Activate the advice for @var{function} now. Changes in a function's
181 advice always take effect the next time you activate advice for the 184 advice always take effect the next time you activate advice for the
188 activate an undefined function's advice. However, defining 191 activate an undefined function's advice. However, defining
189 @var{function} will automatically activate its advice. 192 @var{function} will automatically activate its advice.
190 193
191 @item protect 194 @item protect
192 Protect this piece of advice against non-local exits and errors in 195 Protect this piece of advice against non-local exits and errors in
193 preceding code and advice. Protecting advice makes it a cleanup in an 196 preceding code and advice. Protecting advice places it as a cleanup in
194 @code{unwind-protect} form, so that it will execute even if the 197 an @code{unwind-protect} form, so that it will execute even if the
195 previous code gets an error or uses @code{throw}. @xref{Cleanups}. 198 previous code gets an error or uses @code{throw}. @xref{Cleanups}.
196 199
197 @item compile 200 @item compile
198 Compile the combined definition that is used to run the advice. This 201 Compile the combined definition that is used to run the advice. This
199 flag is ignored unless @code{activate} is also specified. 202 flag is ignored unless @code{activate} is also specified.
230 233
231 @strong{Warning:} When you advise a macro, keep in mind that macros are 234 @strong{Warning:} When you advise a macro, keep in mind that macros are
232 expanded when a program is compiled, not when a compiled program is run. 235 expanded when a program is compiled, not when a compiled program is run.
233 All subroutines used by the advice need to be available when the byte 236 All subroutines used by the advice need to be available when the byte
234 compiler expands the macro. 237 compiler expands the macro.
238
239 @node Around-Advice
240 @section Around-Advice
241
242 Around-advice lets you ``wrap'' a Lisp expression ``around'' the
243 original function definition. You specify where the original function
244 definition should go by means of the special symbol @code{ad-do-it}.
245 Where this symbol occurs inside the around-advice body, it is replaced
246 with a @code{progn} containing the forms of the surrounded code. Here
247 is an example:
248
249 @example
250 (defadvice foo (around foo-around)
251 "Ignore case in `foo'."
252 (let ((case-fold-search t))
253 ad-do-it))
254 @end example
255
256 @noindent
257 Its effect is to make sure that case is ignored in
258 searches when the original definition of @code{foo} is run.
259
260 @defvar ad-do-it
261 This is not really a variable, but it is somewhat used like one
262 in around-advice. It specifies the place to run the function's
263 original definition and other ``earlier'' around-advice.
264 @end defvar
265
266 If the around-advice does not use @code{ad-do-it}, then it does not run
267 the original function definition. This provides a way to override the
268 original definition completely. (It also overrides lower-positioned
269 pieces of around-advice).
235 270
236 @node Computed Advice 271 @node Computed Advice
237 @section Computed Advice 272 @section Computed Advice
238 273
239 The macro @code{defadvice} resembles @code{defun} in that the code for 274 The macro @code{defadvice} resembles @code{defun} in that the code for
268 @end defun 303 @end defun
269 304
270 @node Activation of Advice 305 @node Activation of Advice
271 @section Activation of Advice 306 @section Activation of Advice
272 @cindex activating advice 307 @cindex activating advice
308 @cindex advice, activating
273 309
274 By default, advice does not take effect when you define it---only when 310 By default, advice does not take effect when you define it---only when
275 you @dfn{activate} advice for the function that was advised. You can 311 you @dfn{activate} advice for the function that was advised. You can
276 request the activation of advice for a function when you define the 312 request the activation of advice for a function when you define the
277 advice, by specifying the @code{activate} flag in the @code{defadvice}. 313 advice, by specifying the @code{activate} flag in the @code{defadvice}.
300 This command activates the advice for @var{function}. 336 This command activates the advice for @var{function}.
301 @end deffn 337 @end deffn
302 338
303 To activate advice for a function whose advice is already active is not 339 To activate advice for a function whose advice is already active is not
304 a no-op. It is a useful operation which puts into effect any changes in 340 a no-op. It is a useful operation which puts into effect any changes in
305 advice since the previous activation of that function's advice. 341 that function's advice since the previous activation of advice for that
342 function.
306 343
307 @deffn Command ad-deactivate function 344 @deffn Command ad-deactivate function
308 This command deactivates the advice for @var{function}. 345 This command deactivates the advice for @var{function}.
346 @cindex deactivating advice
347 @cindex advice, deactivating
309 @end deffn 348 @end deffn
310 349
311 @deffn Command ad-activate-all &optional compile 350 @deffn Command ad-activate-all &optional compile
312 This command activates the advice for all functions. 351 This command activates the advice for all functions.
313 @end deffn 352 @end deffn
321 @var{regexp}. More precisely, it activates all advice for any function 360 @var{regexp}. More precisely, it activates all advice for any function
322 which has at least one piece of advice that matches @var{regexp}. 361 which has at least one piece of advice that matches @var{regexp}.
323 @end deffn 362 @end deffn
324 363
325 @deffn Command ad-deactivate-regexp regexp 364 @deffn Command ad-deactivate-regexp regexp
326 This command deactivates the advice for all functions whose names match 365 This command deactivates all pieces of advice whose names match
327 @var{regexp}. More precisely, it deactivates all advice for any 366 @var{regexp}. More precisely, it deactivates all advice for any
328 function which has at least one piece of advice that matches 367 function which has at least one piece of advice that matches
329 @var{regexp}. 368 @var{regexp}.
330 @end deffn 369 @end deffn
331 370
332 @deffn Command ad-update-regexp regexp &optional compile 371 @deffn Command ad-update-regexp regexp &optional compile
333 This command activates pieces of advice whose names match @var{regexp}, 372 This command activates pieces of advice whose names match @var{regexp},
334 but only those for functions whose advice is already activated. 373 but only those for functions whose advice is already activated.
374 @cindex reactivating advice
335 375
336 Reactivating a function's advice is useful for putting into effect all 376 Reactivating a function's advice is useful for putting into effect all
337 the changes that have been made in its advice (including enabling and 377 the changes that have been made in its advice (including enabling and
338 disabling specific pieces of advice; @pxref{Enabling Advice}) since the 378 disabling specific pieces of advice; @pxref{Enabling Advice}) since the
339 last time it was activated. 379 last time it was activated.
353 @defopt ad-default-compilation-action 393 @defopt ad-default-compilation-action
354 This variable controls whether to compile the combined definition 394 This variable controls whether to compile the combined definition
355 that results from activating advice for a function. 395 that results from activating advice for a function.
356 @end defopt 396 @end defopt
357 397
358 If the advised definition was constructed during ``preactivation'' (see 398 If the advised definition was constructed during ``preactivation''
359 below), then that definition must already be compiled, because it was 399 (@pxref{Preactivation}), then that definition must already be compiled,
360 constructed during byte-compilation of the file that contained the 400 because it was constructed during byte-compilation of the file that
361 @code{defadvice} with the @code{preactivate} flag. 401 contained the @code{defadvice} with the @code{preactivate} flag.
362 402
363 @node Enabling Advice 403 @node Enabling Advice
364 @section Enabling and Disabling Advice 404 @section Enabling and Disabling Advice
405 @cindex enabling advice
406 @cindex advice, enabling and disabling
407 @cindex disabling advice
365 408
366 Each piece of advice has a flag that says whether it is enabled or 409 Each piece of advice has a flag that says whether it is enabled or
367 not. By enabling or disabling a piece of advice, you can turn it off 410 not. By enabling or disabling a piece of advice, you can turn it on
368 and on without having to undefine and redefine it. For example, here is 411 and off without having to undefine and redefine it. For example, here is
369 how to disable a particular piece of advice named @code{my-advice} for 412 how to disable a particular piece of advice named @code{my-advice} for
370 the function @code{foo}: 413 the function @code{foo}:
371 414
372 @example 415 @example
373 (ad-disable-advice 'foo 'before 'my-advice) 416 (ad-disable-advice 'foo 'before 'my-advice)
374 @end example 417 @end example
375 418
376 This function by itself only changes the enable flag for a piece of 419 This function by itself only changes the enable flag for a piece of
377 advice. To make the change take effect in the advised definition, you 420 advice. To make the change take effect in the advised definition, you
378 must activate the advice for @code{foo} again: 421 must activate the advice for @code{foo} again:
379 422
380 @example 423 @example
381 (ad-activate 'foo) 424 (ad-activate 'foo)
406 @var{regexp}, in all classes, on all functions. 449 @var{regexp}, in all classes, on all functions.
407 @end deffn 450 @end deffn
408 451
409 @node Preactivation 452 @node Preactivation
410 @section Preactivation 453 @section Preactivation
454 @cindex preactivating advice
455 @cindex advice, preactivating
411 456
412 Constructing a combined definition to execute advice is moderately 457 Constructing a combined definition to execute advice is moderately
413 expensive. When a library advises many functions, this can make loading 458 expensive. When a library advises many functions, this can make loading
414 the library slow. In that case, you can use @dfn{preactivation} to 459 the library slow. In that case, you can use @dfn{preactivation} to
415 construct suitable combined definitions in advance. 460 construct suitable combined definitions in advance.
484 for that function. 529 for that function.
485 530
486 A more robust method is to use macros that are translated into the 531 A more robust method is to use macros that are translated into the
487 proper access forms at activation time, i.e., when constructing the 532 proper access forms at activation time, i.e., when constructing the
488 advised definition. Access macros access actual arguments by position 533 advised definition. Access macros access actual arguments by position
489 regardless of how these actual argument get distributed onto the 534 regardless of how these actual arguments get distributed onto the
490 argument variables of a function. This is robust because in Emacs Lisp 535 argument variables of a function. This is robust because in Emacs Lisp
491 the meaning of an argument is strictly determined by its position in the 536 the meaning of an argument is strictly determined by its position in the
492 argument list. 537 argument list.
493 538
494 @defmac ad-get-arg position 539 @defmac ad-get-arg position