root/trunk/lisp/emacs-lisp/advice.el

Revision 4220, 168.2 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; advice.el --- an overloading mechanism for Emacs Lisp functions
2
3 ;; Copyright (C) 1993, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu>
7 ;; Maintainer: FSF
8 ;; Created: 12 Dec 1992
9 ;; Keywords: extensions, lisp, tools
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;; LCD Archive Entry:
29 ;; advice|Hans Chalupsky|hans@cs.buffalo.edu|
30 ;; Overloading mechanism for Emacs Lisp functions|
31 ;; 1994/08/05 03:42:04|2.14|~/packages/advice.el.Z|
32
33
34 ;;; Commentary:
35
36 ;; NOTE: This documentation is slightly out of date. In particular, all the
37 ;; references to Emacs-18 are obsolete now, because it is not any longer
38 ;; supported by this version of Advice.
39
40 ;; Advice is documented in the Emacs Lisp Manual.
41
42 ;; @ Introduction:
43 ;; ===============
44 ;; This package implements a full-fledged Lisp-style advice mechanism
45 ;; for Emacs Lisp. Advice is a clean and efficient way to modify the
46 ;; behavior of Emacs Lisp functions without having to keep  personal
47 ;; modified copies of such functions around. A great number of such
48 ;; modifications can be achieved by treating the original function as a
49 ;; black box and specifying a different execution environment for it
50 ;; with a piece of advice. Think of a piece of advice as a kind of fancy
51 ;; hook that you can attach to any function/macro/subr.
52
53 ;; @ Highlights:
54 ;; =============
55 ;; - Clean definition of multiple, named before/around/after advices
56 ;;   for functions, macros, subrs and special forms
57 ;; - Full control over the arguments an advised function will receive,
58 ;;   the binding environment in which it will be executed, as well as the
59 ;;   value it will return.
60 ;; - Allows re/definition of interactive behavior for functions and subrs
61 ;; - Every piece of advice can have its documentation string which will be
62 ;;   combined with the original documentation of the advised function at
63 ;;   call-time of `documentation' for proper command-key substitution.
64 ;; - The execution of every piece of advice can be protected against error
65 ;;   and non-local exits in preceding code or advices.
66 ;; - Simple argument access either by name, or, more portable but as
67 ;;   efficient, via access macros
68 ;; - Allows the specification of a different argument list for the advised
69 ;;   version of a function.
70 ;; - Advised functions can be byte-compiled either at file-compile time
71 ;;   (see preactivation) or activation time.
72 ;; - Separation of advice definition and activation
73 ;; - Forward advice is possible, that is
74 ;;   as yet undefined or autoload functions can be advised without having to
75 ;;   preload the file in which they are defined.
76 ;; - Forward redefinition is possible because around advice can be used to
77 ;;   completely redefine a function.
78 ;; - A caching mechanism for advised definition provides for cheap deactivation
79 ;;   and reactivation of advised functions.
80 ;; - Preactivation allows efficient construction and compilation of advised
81 ;;   definitions at file compile time without giving up the flexibility of
82 ;;   the advice mechanism.
83 ;; - En/disablement mechanism allows the use of  different "views" of advised
84 ;;   functions depending on what pieces of advice are currently en/disabled
85 ;; - Provides manipulation mechanisms for sets of advised functions via
86 ;;   regular expressions that match advice names
87
88 ;; @ How to get Advice for Emacs-18:
89 ;; =================================
90 ;; `advice18.el', a version of Advice that also works in Emacs-18 is available
91 ;; either via anonymous ftp from `ftp.cs.buffalo.edu (128.205.32.9)' with
92 ;; pathname `/pub/Emacs/advice18.el', or from one of the Emacs Lisp archive
93 ;; sites, or send email to <hans@cs.buffalo.edu> and I'll mail it to you.
94
95 ;; @ Overview, or how to read this file:
96 ;; =====================================
97 ;; NOTE: This documentation is slightly out of date. In particular, all the
98 ;; references to Emacs-18 are obsolete now, because it is not any longer
99 ;; supported by this version of Advice. An up-to-date version will soon be
100 ;; available as an info file (thanks to the kind help of Jack Vinson and
101 ;; David M. Smith). Until then you can use `outline-mode' to help you read
102 ;; this documentation (set `outline-regexp' to `";; @+"').
103 ;;
104 ;; The four major sections of this file are:
105 ;;
106 ;;   @ This initial information       ...installation, customization etc.
107 ;;   @ Advice documentation:          ...general documentation
108 ;;   @ Foo games: An advice tutorial  ...teaches about Advice by example
109 ;;   @ Advice implementation:         ...actual code, yeah!!
110 ;;
111 ;; The latter three are actual headings which you can search for
112 ;; directly in case `outline-mode' doesn't work for you.
113
114 ;; @ Restrictions:
115 ;; ===============
116 ;; - This version of Advice only works for Emacs 19.26 and later. It uses
117 ;;   new versions of the built-in functions `fset/defalias' which are not
118 ;;   yet available in Lucid Emacs, hence, it won't work there.
119 ;; - Advised functions/macros/subrs will only exhibit their advised behavior
120 ;;   when they are invoked via their function cell. This means that advice will
121 ;;   not work for the following:
122 ;;   + advised subrs that are called directly from other subrs or C-code
123 ;;   + advised subrs that got replaced with their byte-code during
124 ;;     byte-compilation (e.g., car)
125 ;;   + advised macros which were expanded during byte-compilation before
126 ;;     their advice was activated.
127
128 ;; @ Credits:
129 ;; ==========
130 ;; This package is an extension and generalization of packages such as
131 ;; insert-hooks.el written by Noah S. Friedman, and advise.el written by
132 ;; Raul J. Acevedo. Some ideas used in here come from these packages,
133 ;; others come from the various Lisp advice mechanisms I've come across
134 ;; so far, and a few are simply mine.
135
136 ;; @ Comments, suggestions, bug reports:
137 ;; =====================================
138 ;; If you find any bugs, have suggestions for new advice features, find the
139 ;; documentation wrong, confusing, incomplete, or otherwise unsatisfactory,
140 ;; have any questions about Advice, or have otherwise enlightening
141 ;; comments feel free to send me email at <hans@cs.buffalo.edu>.
142
143 ;; @ Safety Rules and Emergency Exits:
144 ;; ===================================
145 ;; Before we begin: CAUTION!!
146 ;; Advice provides you with a lot of rope to hang yourself on very
147 ;; easily accessible trees, so, here are a few important things you
148 ;; should know: Once Advice has been started with `ad-start-advice'
149 ;; (which happens automatically when you load this file), it
150 ;; generates an advised definition of the `documentation' function, and
151 ;; it will enable automatic advice activation when functions get defined.
152 ;; All of this can be undone at any time with `M-x ad-stop-advice'.
153 ;;
154 ;; If you experience any strange behavior/errors etc. that you attribute to
155 ;; Advice or to some ill-advised function do one of the following:
156
157 ;; - M-x ad-deactivate FUNCTION (if you have a definite suspicion what
158 ;;                               function gives you problems)
159 ;; - M-x ad-deactivate-all      (if you don't have a clue what's going wrong)
160 ;; - M-x ad-stop-advice         (if you think the problem is related to the
161 ;;                               advised functions used by Advice itself)
162 ;; - M-x ad-recover-normality   (for real emergencies)
163 ;; - If none of the above solves your Advice-related problem go to another
164 ;;   terminal, kill your Emacs process and send me some hate mail.
165
166 ;; The first three measures have restarts, i.e., once you've figured out
167 ;; the problem you can reactivate advised functions with either `ad-activate',
168 ;; `ad-activate-all', or `ad-start-advice'. `ad-recover-normality' unadvises
169 ;; everything so you won't be able to reactivate any advised functions, you'll
170 ;; have to stick with their standard incarnations for the rest of the session.
171
172 ;; IMPORTANT: With Advice loaded always do `M-x ad-deactivate-all' before
173 ;; you byte-compile a file, because advised special forms and macros can lead
174 ;; to unwanted compilation results. When you are done compiling use
175 ;; `M-x ad-activate-all' to go back to the advised state of all your
176 ;; advised functions.
177
178 ;; RELAX: Advice is pretty safe even if you are oblivious to the above.
179 ;; I use it extensively and haven't run into any serious trouble in a long
180 ;; time. Just wanted you to be warned.
181
182 ;; @ Customization:
183 ;; ================
184
185 ;; Look at the documentation of `ad-redefinition-action' for possible values
186 ;; of this variable. Its default value is `warn' which will print a warning
187 ;; message when an already defined advised function gets redefined with a
188 ;; new original definition and de/activated.
189
190 ;; Look at the documentation of `ad-default-compilation-action' for possible
191 ;; values of this variable. Its default value is `maybe' which will compile
192 ;; advised definitions during activation in case the byte-compiler is already
193 ;; loaded. Otherwise, it will leave them uncompiled.
194
195 ;; @ Motivation:
196 ;; =============
197 ;; Before I go on explaining how advice works, here are four simple examples
198 ;; how this package can be used. The first three are very useful, the last one
199 ;; is just a joke:
200
201 ;;(defadvice switch-to-buffer (before existing-buffers-only activate)
202 ;;  "When called interactively switch to existing buffers only, unless
203 ;;when called with a prefix argument."
204 ;;  (interactive
205 ;;   (list (read-buffer "Switch to buffer: " (other-buffer)
206 ;;                      (null current-prefix-arg)))))
207 ;;
208 ;;(defadvice switch-to-buffer (around confirm-non-existing-buffers activate)
209 ;;  "Switch to non-existing buffers only upon confirmation."
210 ;;  (interactive "BSwitch to buffer: ")
211 ;;  (if (or (get-buffer (ad-get-arg 0))
212 ;;          (y-or-n-p (format "`%s' does not exist, create? " (ad-get-arg 0))))
213 ;;      ad-do-it))
214 ;;
215 ;;(defadvice find-file (before existing-files-only activate)
216 ;;  "Find existing files only"
217 ;;  (interactive "fFind file: "))
218 ;;
219 ;;(defadvice car (around interactive activate)
220 ;;  "Make `car' an interactive function."
221 ;;   (interactive "xCar of list: ")
222 ;;   ad-do-it
223 ;;   (if (interactive-p)
224 ;;       (message "%s" ad-return-value)))
225
226
227 ;; @ Advice documentation:
228 ;; =======================
229 ;; Below is general documentation of the various features of advice. For more
230 ;; concrete examples check the corresponding sections in the tutorial part.
231
232 ;; @@ Terminology:
233 ;; ===============
234 ;; - Emacs, Emacs-19: Emacs as released by the GNU Project
235 ;; - Lemacs: Lucid's version of Emacs with major version 19
236 ;; - v18: Any Emacs with major version 18 or built as an extension to that
237 ;;        (such as Epoch)
238 ;; - v19: Any Emacs with major version 19
239 ;; - jwz: Jamie Zawinski - former keeper of Lemacs and creator of the optimizing
240 ;;        byte-compiler used in v19s.
241 ;; - Advice: The name of this package.
242 ;; - advices: Short for "pieces of advice".
243
244 ;; @@ Defining a piece of advice with `defadvice':
245 ;; ===============================================
246 ;; The main means of defining a piece of advice is the macro `defadvice',
247 ;; there is no interactive way of specifying a piece of advice.  A call to
248 ;; `defadvice' has the following syntax which is similar to the syntax of
249 ;; `defun/defmacro':
250 ;;
251 ;; (defadvice <function> (<class> <name> [<position>] [<arglist>] {<flags>}*)
252 ;;   [ [<documentation-string>] [<interactive-form>] ]
253 ;;   {<body-form>}* )
254
255 ;; <function> is the name of the function/macro/subr to be advised.
256
257 ;; <class> is the class of the advice which has to be one of `before',
258 ;; `around', `after', `activation' or `deactivation' (the last two allow
259 ;; definition of special act/deactivation hooks).
260
261 ;; <name> is the name of the advice which has to be a non-nil symbol.
262 ;; Names uniquely identify a piece of advice in a certain advice class,
263 ;; hence, advices can be redefined by defining an advice with the same class
264 ;; and name. Advice names are global symbols, hence, the same name space
265 ;; conventions used for function names should be applied.
266
267 ;; An optional <position> specifies where in the current list of advices of
268 ;; the specified <class> this new advice will be placed. <position> has to
269 ;; be either `first', `last' or a number that specifies a zero-based
270 ;; position (`first' is equivalent to 0). If no position is specified
271 ;; `first' will be used as a default. If this call to `defadvice' redefines
272 ;; an already existing advice (see above) then the position argument will
273 ;; be ignored and the position of the already existing advice will be used.
274
275 ;; An optional <arglist> which has to be a list can be used to define the
276 ;; argument list of the advised function. This argument list should of
277 ;; course be compatible with the argument list of the original function,
278 ;; otherwise functions that call the advised function with the original
279 ;; argument list in mind will break. If more than one advice specify an
280 ;; argument list then the first one (the one with the smallest position)
281 ;; found in the list of before/around/after advices will be used.
282
283 ;; <flags> is a list of symbols that specify further information about the
284 ;; advice. All flags can be specified with unambiguous initial substrings.
285 ;;   `activate': Specifies that the advice information of the advised
286 ;;              function should be activated right after this advice has been
287 ;;              defined. In forward advices `activate' will be ignored.
288 ;;   `protect': Specifies that this advice should be protected against
289 ;;              non-local exits and errors in preceding code/advices.
290 ;;   `compile': Specifies that the advised function should be byte-compiled.
291 ;;              This flag will be ignored unless `activate' is also specified.
292 ;;   `disable': Specifies that the defined advice should be disabled, hence,
293 ;;              it will not be used in an activation until somebody enables it.
294 ;;   `preactivate': Specifies that the advised function should get preactivated
295 ;;              at macro-expansion/compile time of this `defadvice'. This
296 ;;              generates a compiled advised definition according to the
297 ;;              current advice state which will be used during activation
298 ;;              if appropriate. Only use this if the `defadvice' gets
299 ;;              actually compiled (with a v18 byte-compiler put the `defadvice'
300 ;;              into the body of a `defun' to accomplish proper compilation).
301
302 ;; An optional <documentation-string> can be supplied to document the advice.
303 ;; On call of the `documentation' function it will be combined with the
304 ;; documentation strings of the original function and other advices.
305
306 ;; An optional <interactive-form> form can be supplied to change/add
307 ;; interactive behavior of the original function. If more than one advice
308 ;; has an `(interactive ...)' specification then the first one (the one
309 ;; with the smallest position) found in the list of before/around/after
310 ;; advices will be used.
311
312 ;; A possibly empty list of <body-forms> specifies the body of the advice in
313 ;; an implicit progn. The body of an advice can access/change arguments,
314 ;; the return value, the binding environment, and can have all sorts of
315 ;; other side effects.
316
317 ;; @@ Assembling advised definitions:
318 ;; ==================================
319 ;; Suppose a function/macro/subr/special-form has N pieces of before advice,
320 ;; M pieces of around advice and K pieces of after advice. Assuming none of
321 ;; the advices is protected, its advised definition will look like this
322 ;; (body-form indices correspond to the position of the respective advice in
323 ;; that advice class):
324
325 ;;    ([macro] lambda <arglist>
326 ;;       [ [<advised-docstring>] [(interactive ...)] ]
327 ;;       (let (ad-return-value)
328 ;;         {<before-0-body-form>}*
329 ;;               ....
330 ;;         {<before-N-1-body-form>}*
331 ;;         {<around-0-body-form>}*
332 ;;            {<around-1-body-form>}*
333 ;;                  ....
334 ;;               {<around-M-1-body-form>}*
335 ;;                  (setq ad-return-value
336 ;;                        <apply original definition to <arglist>>)
337 ;;               {<other-around-M-1-body-form>}*
338 ;;                  ....
339 ;;            {<other-around-1-body-form>}*
340 ;;         {<other-around-0-body-form>}*
341 ;;         {<after-0-body-form>}*
342 ;;               ....
343 ;;         {<after-K-1-body-form>}*
344 ;;         ad-return-value))
345
346 ;; Macros and special forms will be redefined as macros, hence the optional
347 ;; [macro] in the beginning of the definition.
348
349 ;; <arglist> is either the argument list of the original function or the
350 ;; first argument list defined in the list of before/around/after advices.
351 ;; The values of <arglist> variables can be accessed/changed in the body of
352 ;; an advice by simply referring to them by their original name, however,
353 ;; more portable argument access macros are also provided (see below).  For
354 ;; subrs/special-forms for which neither explicit argument list definitions
355 ;; are available, nor their documentation strings contain such definitions
356 ;; (as they do v19s), `(&rest ad-subr-args)' will be used.
357
358 ;; <advised-docstring> is an optional, special documentation string which will
359 ;; be expanded into a proper documentation string upon call of `documentation'.
360
361 ;; (interactive ...) is an optional interactive form either taken from the
362 ;; original function or from a before/around/after advice. For advised
363 ;; interactive subrs that do not have an interactive form specified in any
364 ;; advice we have to use (interactive) and then call the subr interactively
365 ;; if the advised function was called interactively, because the
366 ;; interactive specification of subrs is not accessible. This is the only
367 ;; case where changing the values of arguments will not have an affect
368 ;; because they will be reset by the interactive specification of the subr.
369 ;; If this is a problem one can always specify an interactive form in a
370 ;; before/around/after advice to gain control over argument values that
371 ;; were supplied interactively.
372 ;;
373 ;; Then the body forms of the various advices in the various classes of advice
374 ;; are assembled in order.  The forms of around advice L are normally part of
375 ;; one of the forms of around advice L-1. An around advice can specify where
376 ;; the forms of the wrapped or surrounded forms should go with the special
377 ;; keyword `ad-do-it', which will be substituted with a `progn' containing the
378 ;; forms of the surrounded code.
379
380 ;; The innermost part of the around advice onion is
381 ;;      <apply original definition to <arglist>>
382 ;; whose form depends on the type of the original function. The variable
383 ;; `ad-return-value' will be set to its result. This variable is visible to
384 ;; all pieces of advice which can access and modify it before it gets returned.
385 ;;
386 ;; The semantic structure of advised functions that contain protected pieces
387 ;; of advice is the same. The only difference is that `unwind-protect' forms
388 ;; make sure that the protected advice gets executed even if some previous
389 ;; piece of advice had an error or a non-local exit. If any around advice is
390 ;; protected then the whole around advice onion will be protected.
391
392 ;; @@ Argument access in advised functions:
393 ;; ========================================
394 ;; As already mentioned, the simplest way to access the arguments of an
395 ;; advised function in the body of an advice is to refer to them by name. To
396 ;; do that, the advice programmer needs to know either the names of the
397 ;; argument variables of the original function, or the names used in the
398 ;; argument list redefinition given in a piece of advice. While this simple
399 ;; method might be sufficient in many cases, it has the disadvantage that it
400 ;; is not very portable because it hardcodes the argument names into the
401 ;; advice. If the definition of the original function changes the advice
402 ;; might break even though the code might still be correct. Situations like
403 ;; that arise, for example, if one advises a subr like `eval-region' which
404 ;; gets redefined in a non-advice style into a function by the edebug
405 ;; package. If the advice assumes `eval-region' to be a subr it might break
406 ;; once edebug is loaded. Similar situations arise when one wants to use the
407 ;; same piece of advice across different versions of Emacs. Some subrs in a
408 ;; v18 Emacs are functions in v19 and vice versa, but for the most part the
409 ;; semantics remain the same, hence, the same piece of advice might be usable
410 ;; in both Emacs versions.
411
412 ;; As a solution to that advice provides argument list access macros that get
413 ;; translated into the proper access forms at activation time, i.e., when the
414 ;; advised definition gets constructed. Access macros access actual arguments
415 ;; by position regardless of how these actual argument get distributed onto
416 ;; the argument variables of a function. The rational behind this is that in
417 ;; Emacs Lisp the semantics of an argument is strictly determined by its
418 ;; position (there are no keyword arguments).
419
420 ;; Suppose the function `foo' is defined as
421 ;;
422 ;;    (defun foo (x y &optional z &rest r) ....)
423 ;;
424 ;; and is then called with
425 ;;
426 ;;    (foo 0 1 2 3 4 5 6)
427
428 ;; which means that X=0, Y=1, Z=2 and R=(3 4 5 6). The assumption is that
429 ;; the semantics of an actual argument is determined by its position. It is
430 ;; this semantics that has to be known by the advice programmer. Then s/he
431 ;; can access these arguments in a piece of advice with some of the
432 ;; following macros (the arrows indicate what value they will return):
433
434 ;;    (ad-get-arg 0) -> 0
435 ;;    (ad-get-arg 1) -> 1
436 ;;    (ad-get-arg 2) -> 2
437 ;;    (ad-get-arg 3) -> 3
438 ;;    (ad-get-args 2) -> (2 3 4 5 6)
439 ;;    (ad-get-args 4) -> (4 5 6)
440
441 ;; `(ad-get-arg <position>)' will return the actual argument that was supplied
442 ;; at <position>, `(ad-get-args <position>)' will return the list of actual
443 ;; arguments supplied starting at <position>. Note that these macros can be
444 ;; used without any knowledge about the form of the actual argument list of
445 ;; the original function.
446
447 ;; Similarly, `(ad-set-arg <position> <value-form>)' can be used to set the
448 ;; value of the actual argument at <position> to <value-form>. For example,
449 ;;
450 ;;   (ad-set-arg 5 "five")
451 ;;
452 ;; will have the effect that R=(3 4 "five" 6) once the original function is
453 ;; called. `(ad-set-args <position> <value-list-form>)' can be used to set
454 ;; the list of actual arguments starting at <position> to <value-list-form>.
455 ;; For example,
456 ;;
457 ;;   (ad-set-args 0 '(5 4 3 2 1 0))
458 ;;
459 ;; will have the effect that X=5, Y=4, Z=3 and R=(2 1 0) once the original
460 ;; function is called.
461
462 ;; All these access macros are text macros rather than real Lisp macros. When
463 ;; the advised definition gets constructed they get replaced with actual access
464 ;; forms depending on the argument list of the advised function, i.e., after
465 ;; that argument access is in most cases as efficient as using the argument
466 ;; variable names directly.
467
468 ;; @@@ Accessing argument bindings of arbitrary functions:
469 ;; =======================================================
470 ;; Some functions (such as `trace-function' defined in trace.el) need a
471 ;; method of accessing the names and bindings of the arguments of an
472 ;; arbitrary advised function. To do that within an advice one can use the
473 ;; special keyword `ad-arg-bindings' which is a text macro that will be
474 ;; substituted with a form that will evaluate to a list of binding
475 ;; specifications, one for every argument variable.  These binding
476 ;; specifications can then be examined in the body of the advice.  For
477 ;; example, somewhere in an advice we could do this:
478 ;;
479 ;;   (let* ((bindings ad-arg-bindings)
480 ;;          (firstarg (car bindings))
481 ;;          (secondarg (car (cdr bindings))))
482 ;;     ;; Print info about first argument
483 ;;     (print (format "%s=%s (%s)"
484 ;;                    (ad-arg-binding-field firstarg 'name)
485 ;;                    (ad-arg-binding-field firstarg 'value)
486 ;;                    (ad-arg-binding-field firstarg 'type)))
487 ;;     ....)
488 ;;
489 ;; The `type' of an argument is either `required', `optional' or `rest'.
490 ;; Wherever `ad-arg-bindings' appears a form will be inserted that evaluates
491 ;; to the list of bindings, hence, in order to avoid multiple unnecessary
492 ;; evaluations one should always bind it to some variable.
493
494 ;; @@@ Argument list mapping:
495 ;; ==========================
496 ;; Because `defadvice' allows the specification of the argument list of the
497 ;; advised function we need a mapping mechanism that maps this argument list
498 ;; onto that of the original function. For example, somebody might specify
499 ;; `(sym newdef)' as the argument list of `fset', while advice might use
500 ;; `(&rest ad-subr-args)' as the argument list of the original function
501 ;; (depending on what Emacs version is used). Hence SYM and NEWDEF have to
502 ;; be properly mapped onto the &rest variable when the original definition is
503 ;; called. Advice automatically takes care of that mapping, hence, the advice
504 ;; programmer can specify an argument list without having to know about the
505 ;; exact structure of the original argument list as long as the new argument
506 ;; list takes a compatible number/magnitude of actual arguments.
507
508 ;; @@@ Definition of subr argument lists:
509 ;; ======================================
510 ;; When advice constructs the advised definition of a function it has to
511 ;; know the argument list of the original function. For functions and macros
512 ;; the argument list can be determined from the actual definition, however,
513 ;; for subrs there is no such direct access available. In Lemacs and for some
514 ;; subrs in Emacs-19 the argument list of a subr can be determined from
515 ;; its documentation string, in a v18 Emacs even that is not possible. If
516 ;; advice cannot at all determine the argument list of a subr it uses
517 ;; `(&rest ad-subr-args)' which will always work but is inefficient because
518 ;; it conses up arguments. The macro `ad-define-subr-args' can be used by
519 ;; the advice programmer to explicitly tell advice about the argument list
520 ;; of a certain subr, for example,
521 ;;
522 ;;    (ad-define-subr-args 'fset '(sym newdef))
523 ;;
524 ;; is used by advice itself to tell a v18 Emacs about the arguments of `fset'.
525 ;; The following can be used to undo such a definition:
526 ;;
527 ;;    (ad-undefine-subr-args 'fset)
528 ;;
529 ;; The argument list definition is stored on the property list of the subr
530 ;; name symbol. When an argument list could be determined from the
531 ;; documentation string it will be cached under that property. The general
532 ;; mechanism for looking up the argument list of a subr is the following:
533 ;; 1) look for a definition stored on the property list
534 ;; 2) if that failed try to infer it from the documentation string and
535 ;;    if successful cache it on the property list
536 ;; 3) otherwise use `(&rest ad-subr-args)'
537
538 ;; @@ Activation and deactivation:
539 ;; ===============================
540 ;; The definition of an advised function does not change until all its advice
541 ;; gets actually activated. Activation can either happen with the `activate'
542 ;; flag specified in the `defadvice', with an explicit call or interactive
543 ;; invocation of `ad-activate', or if forward advice is enabled (i.e., the
544 ;; value of `ad-activate-on-definition' is t) at the time an already advised
545 ;; function gets defined.
546
547 ;; When a function gets first activated its original definition gets saved,
548 ;; all defined and enabled pieces of advice will get combined with the
549 ;; original definition, the resulting definition might get compiled depending
550 ;; on some conditions described below, and then the function will get
551 ;; redefined with the advised definition.  This also means that undefined
552 ;; functions cannot get activated even though they might be already advised.
553
554 ;; The advised definition will get compiled either if `ad-activate' was called
555 ;; interactively with a prefix argument, or called explicitly with its second
556 ;; argument as t, or, if `ad-default-compilation-action' justifies it according
557 ;; to the current system state. If the advised definition was
558 ;; constructed during "preactivation" (see below) then that definition will
559 ;; be already compiled because it was constructed during byte-compilation of
560 ;; the file that contained the `defadvice' with the `preactivate' flag.
561
562 ;; `ad-deactivate' can be used to back-define an advised function to its
563 ;; original definition. It can be called interactively or directly. Because
564 ;; `ad-activate' caches the advised definition the function can be
565 ;; reactivated via `ad-activate' with only minor overhead (it is checked
566 ;; whether the current advice state is consistent with the cached
567 ;; definition, see the section on caching below).
568
569 ;; `ad-activate-regexp' and `ad-deactivate-regexp' can be used to de/activate
570 ;; all currently advised function that have a piece of advice with a name that
571 ;; contains a match for a regular expression. These functions can be used to
572 ;; de/activate sets of functions depending on certain advice naming
573 ;; conventions.
574
575 ;; Finally, `ad-activate-all' and `ad-deactivate-all' can be used to
576 ;; de/activate all currently advised functions. These are useful to
577 ;; (temporarily) return to an un/advised state.
578
579 ;; @@@ Reasons for the separation of advice definition and activation:
580 ;; ===================================================================
581 ;; As already mentioned, advising happens in two stages:
582
583 ;;   1) definition of various pieces of advice
584 ;;   2) activation of all advice currently defined and enabled
585
586 ;; The advantage of this is that various pieces of advice can be defined
587 ;; before they get combined into an advised definition which avoids
588 ;; unnecessary constructions of intermediate advised definitions. The more
589 ;; important advantage is that it allows the implementation of forward advice.
590 ;; Advice information for a certain function accumulates as the value of the
591 ;; `advice-info' property of the function symbol. This accumulation is
592 ;; completely independent of the fact that that function might not yet be
593 ;; defined. The special forms `defun' and `defmacro' have been advised to
594 ;; check whether the function/macro they defined had advice information
595 ;; associated with it. If so and forward advice is enabled, the original
596 ;; definition will be saved, and then the advice will be activated. When a
597 ;; file is loaded in a v18 Emacs the functions/macros it defines are also
598 ;; defined with calls to `defun/defmacro'.  Hence, we can forward advise
599 ;; functions/macros which will be defined later during a load/autoload of some
600 ;; file (for compiled files generated by jwz's byte-compiler in a v19 Emacs
601 ;; this is slightly more complicated but the basic idea is the same).
602
603 ;; @@ Enabling/disabling pieces or sets of advice:
604 ;; ===============================================
605 ;; A major motivation for the development of this advice package was to bring
606 ;; a little bit more structure into the function overloading chaos in Emacs
607 ;; Lisp. Many packages achieve some of their functionality by adding a little
608 ;; bit (or a lot) to the standard functionality of some Emacs Lisp function.
609 ;; ange-ftp is a very popular package that achieves its magic by overloading
610 ;; most Emacs Lisp functions that deal with files. A popular function that's
611 ;; overloaded by many packages is `expand-file-name'. The situation that one
612 ;; function is multiply overloaded can arise easily.
613
614 ;; Once in a while it would be desirable to be able to disable some/all
615 ;; overloads of a particular package while keeping all the rest.  Ideally -
616 ;; at least in my opinion - these overloads would all be done with advice,
617 ;; I know I am dreaming right now... In that ideal case the enable/disable
618 ;; mechanism of advice could be used to achieve just that.
619
620 ;; Every piece of advice is associated with an enablement flag. When the
621 ;; advised definition of a particular function gets constructed (e.g., during
622 ;; activation) only the currently enabled pieces of advice will be considered.
623 ;; This mechanism allows one to have different "views" of an advised function
624 ;; dependent on what pieces of advice are currently enabled.
625
626 ;; Another motivation for this mechanism is that it allows one to define a
627 ;; piece of advice for some function yet keep it dormant until a certain
628 ;; condition is met. Until then activation of the function will not make use
629 ;; of that piece of advice. Once the condition is met the advice can be
630 ;; enabled and a reactivation of the function will add its functionality as
631 ;; part of the new advised definition. For example, the advices of `defun'
632 ;; etc. used by advice itself will stay disabled until `ad-start-advice' is
633 ;; called and some variables have the proper values.  Hence, if somebody
634 ;; else advised these functions too and activates them the advices defined
635 ;; by advice will get used only if they are intended to be used.
636
637 ;; The main interface to this mechanism are the interactive functions
638 ;; `ad-enable-advice' and `ad-disable-advice'. For example, the following
639 ;; would disable a particular advice of the function `foo':
640 ;;
641 ;;    (ad-disable-advice 'foo 'before 'my-advice)
642 ;;
643 ;; This call by itself only changes the flag, to get the proper effect in
644 ;; the advised definition too one has to activate `foo' with
645 ;;
646 ;;    (ad-activate 'foo)
647 ;;
648 ;; or interactively. To disable whole sets of advices one can use a regular
649 ;; expression mechanism. For example, let us assume that ange-ftp actually
650 ;; used advice to overload all its functions, and that it used the
651 ;; "ange-ftp-" prefix for all its advice names, then we could temporarily
652 ;; disable all its advices with
653 ;;
654