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