comparison lispref/advice.texi @ 60261:dd9ce828300e

(Advising Functions): Don't imply one part of Emacs should advise another part. Markup changes. (Defining Advice): Move transitional para. (Activation of Advice): Cleanup. Explain if COMPILE is nil or negative.
author Richard M. Stallman <rms@gnu.org>
date Sat, 26 Feb 2005 23:49:49 +0000
parents b73dae8c28d0
children a489093cf10b e4694597cbf4
comparison
equal deleted inserted replaced
60260:a8e0abd18501 60261:dd9ce828300e
5 @setfilename ../info/advising 5 @setfilename ../info/advising
6 @node Advising Functions, Debugging, Byte Compilation, Top 6 @node Advising Functions, Debugging, Byte Compilation, Top
7 @chapter Advising Emacs Lisp Functions 7 @chapter Advising Emacs Lisp Functions
8 @cindex advising functions 8 @cindex advising functions
9 9
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
11 function, by @dfn{advising the function}. This is a clean method for a 11 a function, by @dfn{advising the function}. This is a clean method
12 library to customize functions defined by other parts of Emacs---cleaner 12 for a library to customize functions defined within Emacs---cleaner
13 than redefining the whole function. 13 than redefining the whole function.
14 14
15 @cindex piece of advice 15 @cindex piece of advice
16 Each function can have multiple @dfn{pieces of advice}, separately 16 Each function can have multiple @dfn{pieces of advice}, separately
17 defined. Each defined piece of advice can be @dfn{enabled} or 17 defined. Each defined piece of advice can be @dfn{enabled} or
18 disabled explicitly. All the enabled pieces of advice for any given 18 @dfn{disabled} explicitly. All the enabled pieces of advice for any given
19 function actually take effect when you @dfn{activate} advice for that 19 function actually take effect when you @dfn{activate} advice for that
20 function, or when you define or redefine the function. Note that 20 function, or when you define or redefine the function. Note that
21 enabling a piece of advice and activating advice for a function 21 enabling a piece of advice and activating advice for a function
22 are not the same thing. 22 are not the same thing.
23 23
127 Here, @var{function} is the name of the function (or macro or special 127 Here, @var{function} is the name of the function (or macro or special
128 form) to be advised. From now on, we will write just ``function'' when 128 form) to be advised. From now on, we will write just ``function'' when
129 describing the entity being advised, but this always includes macros and 129 describing the entity being advised, but this always includes macros and
130 special forms. 130 special forms.
131 131
132 In place of the argument list in an ordinary definition, an advice
133 definition calls for several different pieces of information.
134
132 @cindex class of advice 135 @cindex class of advice
133 @cindex before-advice 136 @cindex before-advice
134 @cindex after-advice 137 @cindex after-advice
135 @cindex around-advice 138 @cindex around-advice
136 @var{class} specifies the @dfn{class} of the advice---one of @code{before}, 139 @var{class} specifies the @dfn{class} of the advice---one of @code{before},
152 symbol. The advice name uniquely identifies one piece of advice, within all 155 symbol. The advice name uniquely identifies one piece of advice, within all
153 the pieces of advice in a particular class for a particular 156 the pieces of advice in a particular class for a particular
154 @var{function}. The name allows you to refer to the piece of 157 @var{function}. The name allows you to refer to the piece of
155 advice---to redefine it, or to enable or disable it. 158 advice---to redefine it, or to enable or disable it.
156 159
157 In place of the argument list in an ordinary definition, an advice
158 definition calls for several different pieces of information.
159
160 The optional @var{position} specifies where, in the current list of 160 The optional @var{position} specifies where, in the current list of
161 advice of the specified @var{class}, this new advice should be placed. 161 advice of the specified @var{class}, this new advice should be placed.
162 It should be either @code{first}, @code{last} or a number that specifies 162 It should be either @code{first}, @code{last} or a number that specifies
163 a zero-based position (@code{first} is equivalent to 0). If no position 163 a zero-based position (@code{first} is equivalent to 0). If no position
164 is specified, the default is @code{first}. Position values outside the 164 is specified, the default is @code{first}. Position values outside the
330 @section Activation of Advice 330 @section Activation of Advice
331 @cindex activating advice 331 @cindex activating advice
332 @cindex advice, activating 332 @cindex advice, activating
333 333
334 By default, advice does not take effect when you define it---only when 334 By default, advice does not take effect when you define it---only when
335 you @dfn{activate} advice for the function that was advised. However 335 you @dfn{activate} advice for the function that was advised. However,
336 the advice will be automatically activated if the function is defined 336 the advice will be activated automatically if you define or redefine
337 or redefined later. You can request the activation of advice for a 337 the function later. You can request the activation of advice for a
338 function when you define the advice, by specifying the @code{activate} 338 function when you define the advice, by specifying the @code{activate}
339 flag in the @code{defadvice}. But normally you activate the advice 339 flag in the @code{defadvice}. But normally you activate the advice
340 for a function by calling the function @code{ad-activate} or one of 340 for a function by calling the function @code{ad-activate} or one of
341 the other activation commands listed below. 341 the other activation commands listed below.
342 342
351 are combined with the original definition to make a new definition. 351 are combined with the original definition to make a new definition.
352 (Pieces of advice that are currently disabled are not used; see 352 (Pieces of advice that are currently disabled are not used; see
353 @ref{Enabling Advice}.) This definition is installed, and optionally 353 @ref{Enabling Advice}.) This definition is installed, and optionally
354 byte-compiled as well, depending on conditions described below. 354 byte-compiled as well, depending on conditions described below.
355 355
356 In all of the commands to activate advice, if @var{compile} is @code{t}, 356 In all of the commands to activate advice, if @var{compile} is
357 the command also compiles the combined definition which implements the 357 @code{t} (or anything but @code{nil} or a negative number), the
358 advice. 358 command also compiles the combined definition which implements the
359 advice. If it is @code{nil} or a negative number, what happens
360 depends on @code{ad-default-compilation-action} as described below.
359 361
360 @deffn Command ad-activate function &optional compile 362 @deffn Command ad-activate function &optional compile
361 This command activates all the advice defined for @var{function}. 363 This command activates all the advice defined for @var{function}.
362 @end deffn 364 @end deffn
363 365
436 already loaded. A value of @code{like-original} specifies to compile 438 already loaded. A value of @code{like-original} specifies to compile
437 the advice if the original definition of the advised function is 439 the advice if the original definition of the advised function is
438 compiled or a built-in function. 440 compiled or a built-in function.
439 441
440 This variable takes effect only if the @var{compile} argument of 442 This variable takes effect only if the @var{compile} argument of
441 @code{ad-activate} (or any of the above functions) was supplied as 443 @code{ad-activate} (or any of the above functions) did not force
442 @code{nil}. If that argument is non-@code{nil}, that means 444 compilation.
443 to compile the advice regardless.
444 @end defopt 445 @end defopt
445 446
446 If the advised definition was constructed during ``preactivation'' 447 If the advised definition was constructed during ``preactivation''
447 (@pxref{Preactivation}), then that definition must already be compiled, 448 (@pxref{Preactivation}), then that definition must already be compiled,
448 because it was constructed during byte-compilation of the file that 449 because it was constructed during byte-compilation of the file that