annotate lisp/utils/advice.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; advice.el --- an overloading mechanism for Emacs Lisp functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Created: 12 Dec 1992
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Version: advice.el,v 2.14 1994/08/05 03:42:04 hans Exp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; Keywords: extensions, lisp, tools
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;; LCD Archive Entry:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; advice|Hans Chalupsky|hans@cs.buffalo.edu|
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; Overloading mechanism for Emacs Lisp functions|
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; 1994/08/05 03:42:04|2.14|~/packages/advice.el.Z|
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; Synched up with: FSF 19.30.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; NOTE: This documentation is slightly out of date. In particular, all the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; references to Emacs-18 are obsolete now, because it is not any longer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;; supported by this version of Advice. An up-to-date version will soon be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; available as an info file (thanks to the kind help of Jack Vinson and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;; David M. Smith).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; @ Introduction:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;; ===============
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;; This package implements a full-fledged Lisp-style advice mechanism
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;; for Emacs Lisp. Advice is a clean and efficient way to modify the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; behavior of Emacs Lisp functions without having to keep personal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;; modified copies of such functions around. A great number of such
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;; modifications can be achieved by treating the original function as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; black box and specifying a different execution environment for it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;; with a piece of advice. Think of a piece of advice as a kind of fancy
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; hook that you can attach to any function/macro/subr.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; @ Highlights:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; =============
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;; - Clean definition of multiple, named before/around/after advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; for functions, macros, subrs and special forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;; - Full control over the arguments an advised function will receive,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;; the binding environment in which it will be executed, as well as the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;; value it will return.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; - Allows re/definition of interactive behavior for functions and subrs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;; - Every piece of advice can have its documentation string which will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ;; combined with the original documentation of the advised function at
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ;; call-time of `documentation' for proper command-key substitution.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;; - The execution of every piece of advice can be protected against error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;; and non-local exits in preceding code or advices.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;; - Simple argument access either by name, or, more portable but as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;; efficient, via access macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;; - Allows the specification of a different argument list for the advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;; version of a function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;; - Advised functions can be byte-compiled either at file-compile time
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;; (see preactivation) or activation time.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 ;; - Separation of advice definition and activation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ;; - Forward advice is possible, that is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ;; as yet undefined or autoload functions can be advised without having to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;; preload the file in which they are defined.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ;; - Forward redefinition is possible because around advice can be used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;; completely redefine a function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;; - A caching mechanism for advised definition provides for cheap deactivation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ;; and reactivation of advised functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ;; - Preactivation allows efficient construction and compilation of advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ;; definitions at file compile time without giving up the flexibility of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;; the advice mechanism.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;; - En/disablement mechanism allows the use of different "views" of advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;; functions depending on what pieces of advice are currently en/disabled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;; - Provides manipulation mechanisms for sets of advised functions via
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ;; regular expressions that match advice names
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;; @ How to get Advice for Emacs-18:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 ;; =================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 ;; `advice18.el', a version of Advice that also works in Emacs-18 is available
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 ;; either via anonymous ftp from `ftp.cs.buffalo.edu (128.205.32.9)' with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 ;; pathname `/pub/Emacs/advice18.el', or from one of the Emacs Lisp archive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 ;; sites, or send email to <hans@cs.buffalo.edu> and I'll mail it to you.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 ;; @ Overview, or how to read this file:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 ;; =====================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ;; NOTE: This documentation is slightly out of date. In particular, all the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ;; references to Emacs-18 are obsolete now, because it is not any longer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 ;; supported by this version of Advice. An up-to-date version will soon be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;; available as an info file (thanks to the kind help of Jack Vinson and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;; David M. Smith). Until then you can use `outline-mode' to help you read
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;; this documentation (set `outline-regexp' to `";; @+"').
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;; The four major sections of this file are:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 ;; @ This initial information ...installation, customization etc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ;; @ Advice documentation: ...general documentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ;; @ Foo games: An advice tutorial ...teaches about Advice by example
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ;; @ Advice implementation: ...actual code, yeah!!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 ;; The latter three are actual headings which you can search for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 ;; directly in case `outline-mode' doesn't work for you.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ;; @ Restrictions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 ;; ===============
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 ;; - Only works with Emacs 19.26 or later and XEmacs 19.12 or later.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 ;; - Advised functions/macros/subrs will only exhibit their advised behavior
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;; when they are invoked via their function cell. This means that advice will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ;; not work for the following:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 ;; + advised subrs that are called directly from other subrs or C-code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;; + advised subrs that got replaced with their byte-code during
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;; byte-compilation (e.g., car)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 ;; + advised macros which were expanded during byte-compilation before
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 ;; their advice was activated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 ;; @ Credits:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 ;; ==========
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 ;; This package is an extension and generalization of packages such as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 ;; insert-hooks.el written by Noah S. Friedman, and advise.el written by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;; Raul J. Acevedo. Some ideas used in here come from these packages,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ;; others come from the various Lisp advice mechanisms I've come across
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ;; so far, and a few are simply mine.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 ;; @ Comments, suggestions, bug reports:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 ;; =====================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 ;; If you find any bugs, have suggestions for new advice features, find the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 ;; documentation wrong, confusing, incomplete, or otherwise unsatisfactory,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ;; have any questions about Advice, or have otherwise enlightening
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 ;; comments feel free to send me email at <hans@cs.buffalo.edu>.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 ;; @ Safety Rules and Emergency Exits:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ;; ===================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ;; Before we begin: CAUTION!!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 ;; Advice provides you with a lot of rope to hang yourself on very
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 ;; easily accessible trees, so, here are a few important things you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 ;; should know: Once Advice has been started with `ad-start-advice'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 ;; (which happens automatically when you load this file), it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 ;; generates an advised definition of the `documentation' function, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 ;; it will enable automatic advice activation when functions get defined.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 ;; All of this can be undone at any time with `M-x ad-stop-advice'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ;; If you experience any strange behavior/errors etc. that you attribute to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 ;; Advice or to some ill-advised function do one of the following:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 ;; - M-x ad-deactivate FUNCTION (if you have a definite suspicion what
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 ;; function gives you problems)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 ;; - M-x ad-deactivate-all (if you don't have a clue what's going wrong)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 ;; - M-x ad-stop-advice (if you think the problem is related to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 ;; advised functions used by Advice itself)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 ;; - M-x ad-recover-normality (for real emergencies)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 ;; - If none of the above solves your Advice-related problem go to another
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ;; terminal, kill your Emacs process and send me some hate mail.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 ;; The first three measures have restarts, i.e., once you've figured out
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 ;; the problem you can reactivate advised functions with either `ad-activate',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 ;; `ad-activate-all', or `ad-start-advice'. `ad-recover-normality' unadvises
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 ;; everything so you won't be able to reactivate any advised functions, you'll
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 ;; have to stick with their standard incarnations for the rest of the session.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 ;; IMPORTANT: With Advice loaded always do `M-x ad-deactivate-all' before
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 ;; you byte-compile a file, because advised special forms and macros can lead
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ;; to unwanted compilation results. When you are done compiling use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 ;; `M-x ad-activate-all' to go back to the advised state of all your
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 ;; advised functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 ;; RELAX: Advice is pretty safe even if you are oblivious to the above.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 ;; I use it extensively and haven't run into any serious trouble in a long
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 ;; time. Just wanted you to be warned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 ;; @ Customization:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 ;; ================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ;; Look at the documentation of `ad-redefinition-action' for possible values
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ;; of this variable. Its default value is `warn' which will print a warning
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ;; message when an already defined advised function gets redefined with a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 ;; new original definition and de/activated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 ;; Look at the documentation of `ad-default-compilation-action' for possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 ;; values of this variable. Its default value is `maybe' which will compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 ;; advised definitions during activation in case the byte-compiler is already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 ;; loaded. Otherwise, it will leave them uncompiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 ;; @ Motivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 ;; =============
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 ;; Before I go on explaining how advice works, here are four simple examples
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 ;; how this package can be used. The first three are very useful, the last one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 ;; is just a joke:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 ;;(defadvice switch-to-buffer (before existing-buffers-only activate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 ;; "When called interactively switch to existing buffers only, unless
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 ;;when called with a prefix argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 ;; (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 ;; (list (read-buffer "Switch to buffer: " (other-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 ;; (null current-prefix-arg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 ;;(defadvice switch-to-buffer (around confirm-non-existing-buffers activate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 ;; "Switch to non-existing buffers only upon confirmation."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 ;; (interactive "BSwitch to buffer: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 ;; (if (or (get-buffer (ad-get-arg 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 ;; (y-or-n-p (format "`%s' does not exist, create? " (ad-get-arg 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 ;; ad-do-it))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 ;;(defadvice find-file (before existing-files-only activate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 ;; "Find existing files only"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 ;; (interactive "fFind file: "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 ;;(defadvice car (around interactive activate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 ;; "Make `car' an interactive function."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 ;; (interactive "xCar of list: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 ;; ad-do-it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 ;; (if (interactive-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 ;; (message "%s" ad-return-value)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 ;; @ Advice documentation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 ;; =======================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 ;; Below is general documentation of the various features of advice. For more
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 ;; concrete examples check the corresponding sections in the tutorial part.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 ;; @@ Terminology:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 ;; ===============
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 ;; - Emacs, Emacs-19: FSF's version of Emacs with major version 19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 ;; - Lemacs: Lucid's version of Emacs with major version 19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 ;; - XEmacs: New name of Lucid Emacs starting with 19.11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 ;; - v18: Any Emacs with major version 18 or built as an extension to that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 ;; (such as Epoch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 ;; - v19: Any Emacs with major version 19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 ;; - jwz: Jamie Zawinski - former keeper of Lemacs and creator of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 ;; optimizing byte-compiler used in v19s.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 ;; - Advice: The name of this package.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ;; - advices: Short for "pieces of advice".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 ;; @@ Defining a piece of advice with `defadvice':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 ;; ===============================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 ;; The main means of defining a piece of advice is the macro `defadvice',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 ;; there is no interactive way of specifying a piece of advice. A call to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 ;; `defadvice' has the following syntax which is similar to the syntax of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 ;; `defun/defmacro':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 ;; (defadvice <function> (<class> <name> [<position>] [<arglist>] {<flags>}*)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 ;; [ [<documentation-string>] [<interactive-form>] ]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 ;; {<body-form>}* )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 ;; <function> is the name of the function/macro/subr to be advised.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 ;; <class> is the class of the advice which has to be one of `before',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 ;; `around', `after', `activation' or `deactivation' (the last two allow
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 ;; definition of special act/deactivation hooks).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 ;; <name> is the name of the advice which has to be a non-nil symbol.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 ;; Names uniquely identify a piece of advice in a certain advice class,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 ;; hence, advices can be redefined by defining an advice with the same class
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 ;; and name. Advice names are global symbols, hence, the same name space
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 ;; conventions used for function names should be applied.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 ;; An optional <position> specifies where in the current list of advices of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 ;; the specified <class> this new advice will be placed. <position> has to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 ;; be either `first', `last' or a number that specifies a zero-based
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 ;; position (`first' is equivalent to 0). If no position is specified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 ;; `first' will be used as a default. If this call to `defadvice' redefines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 ;; an already existing advice (see above) then the position argument will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 ;; be ignored and the position of the already existing advice will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 ;; An optional <arglist> which has to be a list can be used to define the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 ;; argument list of the advised function. This argument list should of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 ;; course be compatible with the argument list of the original function,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 ;; otherwise functions that call the advised function with the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 ;; argument list in mind will break. If more than one advice specify an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 ;; argument list then the first one (the one with the smallest position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 ;; found in the list of before/around/after advices will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 ;; <flags> is a list of symbols that specify further information about the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 ;; advice. All flags can be specified with unambiguous initial substrings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 ;; `activate': Specifies that the advice information of the advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 ;; function should be activated right after this advice has been
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 ;; defined. In forward advices `activate' will be ignored.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 ;; `protect': Specifies that this advice should be protected against
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 ;; non-local exits and errors in preceding code/advices.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 ;; `compile': Specifies that the advised function should be byte-compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 ;; This flag will be ignored unless `activate' is also specified.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 ;; `disable': Specifies that the defined advice should be disabled, hence,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 ;; it will not be used in an activation until somebody enables it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 ;; `preactivate': Specifies that the advised function should get preactivated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 ;; at macro-expansion/compile time of this `defadvice'. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 ;; generates a compiled advised definition according to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 ;; current advice state which will be used during activation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 ;; if appropriate. Only use this if the `defadvice' gets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 ;; actually compiled (with a v18 byte-compiler put the `defadvice'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 ;; into the body of a `defun' to accomplish proper compilation).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 ;; An optional <documentation-string> can be supplied to document the advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 ;; On call of the `documentation' function it will be combined with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 ;; documentation strings of the original function and other advices.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 ;; An optional <interactive-form> form can be supplied to change/add
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 ;; interactive behavior of the original function. If more than one advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 ;; has an `(interactive ...)' specification then the first one (the one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 ;; with the smallest position) found in the list of before/around/after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 ;; advices will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 ;; A possibly empty list of <body-forms> specifies the body of the advice in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 ;; an implicit progn. The body of an advice can access/change arguments,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 ;; the return value, the binding environment, and can have all sorts of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 ;; other side effects.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 ;; @@ Assembling advised definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 ;; ==================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 ;; Suppose a function/macro/subr/special-form has N pieces of before advice,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 ;; M pieces of around advice and K pieces of after advice. Assuming none of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 ;; the advices is protected, its advised definition will look like this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 ;; (body-form indices correspond to the position of the respective advice in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 ;; that advice class):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 ;; ([macro] lambda <arglist>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 ;; [ [<advised-docstring>] [(interactive ...)] ]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 ;; (let (ad-return-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 ;; {<before-0-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 ;; ....
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 ;; {<before-N-1-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 ;; {<around-0-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 ;; {<around-1-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 ;; ....
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 ;; {<around-M-1-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 ;; (setq ad-return-value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 ;; <apply original definition to <arglist>>)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 ;; {<other-around-M-1-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 ;; ....
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 ;; {<other-around-1-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 ;; {<other-around-0-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 ;; {<after-0-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 ;; ....
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 ;; {<after-K-1-body-form>}*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 ;; ad-return-value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 ;; Macros and special forms will be redefined as macros, hence the optional
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 ;; [macro] in the beginning of the definition.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 ;; <arglist> is either the argument list of the original function or the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 ;; first argument list defined in the list of before/around/after advices.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 ;; The values of <arglist> variables can be accessed/changed in the body of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 ;; an advice by simply referring to them by their original name, however,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 ;; more portable argument access macros are also provided (see below). For
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 ;; subrs/special-forms for which neither explicit argument list definitions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 ;; are available, nor their documentation strings contain such definitions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 ;; (as they do v19s), `(&rest ad-subr-args)' will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 ;; <advised-docstring> is an optional, special documentation string which will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 ;; be expanded into a proper documentation string upon call of `documentation'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 ;; (interactive ...) is an optional interactive form either taken from the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 ;; original function or from a before/around/after advice. For advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 ;; interactive subrs that do not have an interactive form specified in any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 ;; advice we have to use (interactive) and then call the subr interactively
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 ;; if the advised function was called interactively, because the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 ;; interactive specification of subrs is not accessible. This is the only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 ;; case where changing the values of arguments will not have an affect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 ;; because they will be reset by the interactive specification of the subr.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 ;; If this is a problem one can always specify an interactive form in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 ;; before/around/after advice to gain control over argument values that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 ;; were supplied interactively.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 ;; Then the body forms of the various advices in the various classes of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 ;; are assembled in order. The forms of around advice L are normally part of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 ;; one of the forms of around advice L-1. An around advice can specify where
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 ;; the forms of the wrapped or surrounded forms should go with the special
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 ;; keyword `ad-do-it', which will be substituted with a `progn' containing the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 ;; forms of the surrounded code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 ;; The innermost part of the around advice onion is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 ;; <apply original definition to <arglist>>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 ;; whose form depends on the type of the original function. The variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 ;; `ad-return-value' will be set to its result. This variable is visible to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 ;; all pieces of advice which can access and modify it before it gets returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 ;; The semantic structure of advised functions that contain protected pieces
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 ;; of advice is the same. The only difference is that `unwind-protect' forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 ;; make sure that the protected advice gets executed even if some previous
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 ;; piece of advice had an error or a non-local exit. If any around advice is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 ;; protected then the whole around advice onion will be protected.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 ;; @@ Argument access in advised functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 ;; ========================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 ;; As already mentioned, the simplest way to access the arguments of an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 ;; advised function in the body of an advice is to refer to them by name. To
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 ;; do that, the advice programmer needs to know either the names of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 ;; argument variables of the original function, or the names used in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 ;; argument list redefinition given in a piece of advice. While this simple
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 ;; method might be sufficient in many cases, it has the disadvantage that it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 ;; is not very portable because it hardcodes the argument names into the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 ;; advice. If the definition of the original function changes the advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 ;; might break even though the code might still be correct. Situations like
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 ;; that arise, for example, if one advises a subr like `eval-region' which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 ;; gets redefined in a non-advice style into a function by the edebug
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 ;; package. If the advice assumes `eval-region' to be a subr it might break
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 ;; once edebug is loaded. Similar situations arise when one wants to use the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 ;; same piece of advice across different versions of Emacs. Some subrs in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 ;; v18 Emacs are functions in v19 and vice versa, but for the most part the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 ;; semantics remain the same, hence, the same piece of advice might be usable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 ;; in both Emacs versions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 ;; As a solution to that advice provides argument list access macros that get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 ;; translated into the proper access forms at activation time, i.e., when the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 ;; advised definition gets constructed. Access macros access actual arguments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 ;; by position regardless of how these actual argument get distributed onto
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 ;; the argument variables of a function. The rational behind this is that in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 ;; Emacs Lisp the semantics of an argument is strictly determined by its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 ;; position (there are no keyword arguments).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 ;; Suppose the function `foo' is defined as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 ;; (defun foo (x y &optional z &rest r) ....)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 ;; and is then called with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 ;; (foo 0 1 2 3 4 5 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 ;; which means that X=0, Y=1, Z=2 and R=(3 4 5 6). The assumption is that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 ;; the semantics of an actual argument is determined by its position. It is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 ;; this semantics that has to be known by the advice programmer. Then s/he
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 ;; can access these arguments in a piece of advice with some of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 ;; following macros (the arrows indicate what value they will return):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 ;; (ad-get-arg 0) -> 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 ;; (ad-get-arg 1) -> 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 ;; (ad-get-arg 2) -> 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 ;; (ad-get-arg 3) -> 3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 ;; (ad-get-args 2) -> (2 3 4 5 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 ;; (ad-get-args 4) -> (4 5 6)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 ;; `(ad-get-arg <position>)' will return the actual argument that was supplied
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 ;; at <position>, `(ad-get-args <position>)' will return the list of actual
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 ;; arguments supplied starting at <position>. Note that these macros can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 ;; used without any knowledge about the form of the actual argument list of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 ;; the original function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 ;; Similarly, `(ad-set-arg <position> <value-form>)' can be used to set the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 ;; value of the actual argument at <position> to <value-form>. For example,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 ;; (ad-set-arg 5 "five")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 ;; will have the effect that R=(3 4 "five" 6) once the original function is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 ;; called. `(ad-set-args <position> <value-list-form>)' can be used to set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 ;; the list of actual arguments starting at <position> to <value-list-form>.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 ;; For example,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 ;; (ad-set-args 0 '(5 4 3 2 1 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 ;; will have the effect that X=5, Y=4, Z=3 and R=(2 1 0) once the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 ;; function is called.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 ;; All these access macros are text macros rather than real Lisp macros. When
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 ;; the advised definition gets constructed they get replaced with actual access
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 ;; forms depending on the argument list of the advised function, i.e., after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 ;; that argument access is in most cases as efficient as using the argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 ;; variable names directly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 ;; @@@ Accessing argument bindings of arbitrary functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 ;; =======================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 ;; Some functions (such as `trace-function' defined in trace.el) need a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 ;; method of accessing the names and bindings of the arguments of an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 ;; arbitrary advised function. To do that within an advice one can use the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 ;; special keyword `ad-arg-bindings' which is a text macro that will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 ;; substituted with a form that will evaluate to a list of binding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 ;; specifications, one for every argument variable. These binding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 ;; specifications can then be examined in the body of the advice. For
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 ;; example, somewhere in an advice we could do this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 ;; (let* ((bindings ad-arg-bindings)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 ;; (firstarg (car bindings))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 ;; (secondarg (car (cdr bindings))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 ;; ;; Print info about first argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 ;; (print (format "%s=%s (%s)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 ;; (ad-arg-binding-field firstarg 'name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 ;; (ad-arg-binding-field firstarg 'value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 ;; (ad-arg-binding-field firstarg 'type)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 ;; ....)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 ;; The `type' of an argument is either `required', `optional' or `rest'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 ;; Wherever `ad-arg-bindings' appears a form will be inserted that evaluates
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 ;; to the list of bindings, hence, in order to avoid multiple unnecessary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 ;; evaluations one should always bind it to some variable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 ;; @@@ Argument list mapping:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 ;; ==========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 ;; Because `defadvice' allows the specification of the argument list of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 ;; advised function we need a mapping mechanism that maps this argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 ;; onto that of the original function. For example, somebody might specify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 ;; `(sym newdef)' as the argument list of `fset', while advice might use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 ;; `(&rest ad-subr-args)' as the argument list of the original function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 ;; (depending on what Emacs version is used). Hence SYM and NEWDEF have to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 ;; be properly mapped onto the &rest variable when the original definition is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 ;; called. Advice automatically takes care of that mapping, hence, the advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 ;; programmer can specify an argument list without having to know about the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 ;; exact structure of the original argument list as long as the new argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 ;; list takes a compatible number/magnitude of actual arguments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 ;; @@@ Definition of subr argument lists:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 ;; ======================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 ;; When advice constructs the advised definition of a function it has to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 ;; know the argument list of the original function. For functions and macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 ;; the argument list can be determined from the actual definition, however,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 ;; for subrs there is no such direct access available. In XEmacs and for some
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 ;; subrs in Emacs-19 the argument list of a subr can be determined from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 ;; its documentation string, in a v18 Emacs even that is not possible. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 ;; advice cannot at all determine the argument list of a subr it uses
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 ;; `(&rest ad-subr-args)' which will always work but is inefficient because
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 ;; it conses up arguments. The macro `ad-define-subr-args' can be used by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 ;; the advice programmer to explicitly tell advice about the argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 ;; of a certain subr, for example,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 ;; (ad-define-subr-args 'fset '(sym newdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 ;; is used by advice itself to tell a v18 Emacs about the arguments of `fset'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 ;; The following can be used to undo such a definition:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 ;; (ad-undefine-subr-args 'fset)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 ;; The argument list definition is stored on the property list of the subr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 ;; name symbol. When an argument list could be determined from the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 ;; documentation string it will be cached under that property. The general
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 ;; mechanism for looking up the argument list of a subr is the following:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 ;; 1) look for a definition stored on the property list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 ;; 2) if that failed try to infer it from the documentation string and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 ;; if successful cache it on the property list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 ;; 3) otherwise use `(&rest ad-subr-args)'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 ;; @@ Activation and deactivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 ;; ===============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 ;; The definition of an advised function does not change until all its advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 ;; gets actually activated. Activation can either happen with the `activate'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 ;; flag specified in the `defadvice', with an explicit call or interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 ;; invocation of `ad-activate', or if forward advice is enabled (i.e., the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 ;; value of `ad-activate-on-definition' is t) at the time an already advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 ;; function gets defined.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 ;; When a function gets first activated its original definition gets saved,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 ;; all defined and enabled pieces of advice will get combined with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 ;; original definition, the resulting definition might get compiled depending
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 ;; on some conditions described below, and then the function will get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 ;; redefined with the advised definition. This also means that undefined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 ;; functions cannot get activated even though they might be already advised.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 ;; The advised definition will get compiled either if `ad-activate' was called
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 ;; interactively with a prefix argument, or called explicitly with its second
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 ;; argument as t, or, if `ad-default-compilation-action' justifies it according
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 ;; to the current system state. If the advised definition was
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 ;; constructed during "preactivation" (see below) then that definition will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 ;; be already compiled because it was constructed during byte-compilation of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 ;; the file that contained the `defadvice' with the `preactivate' flag.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 ;; `ad-deactivate' can be used to back-define an advised function to its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 ;; original definition. It can be called interactively or directly. Because
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 ;; `ad-activate' caches the advised definition the function can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 ;; reactivated via `ad-activate' with only minor overhead (it is checked
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 ;; whether the current advice state is consistent with the cached
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 ;; definition, see the section on caching below).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 ;; `ad-activate-regexp' and `ad-deactivate-regexp' can be used to de/activate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 ;; all currently advised function that have a piece of advice with a name that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 ;; contains a match for a regular expression. These functions can be used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 ;; de/activate sets of functions depending on certain advice naming
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 ;; conventions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 ;; Finally, `ad-activate-all' and `ad-deactivate-all' can be used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 ;; de/activate all currently advised functions. These are useful to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 ;; (temporarily) return to an un/advised state.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 ;; @@@ Reasons for the separation of advice definition and activation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 ;; ===================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 ;; As already mentioned, advising happens in two stages:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 ;; 1) definition of various pieces of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 ;; 2) activation of all advice currently defined and enabled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 ;; The advantage of this is that various pieces of advice can be defined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 ;; before they get combined into an advised definition which avoids
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 ;; unnecessary constructions of intermediate advised definitions. The more
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 ;; important advantage is that it allows the implementation of forward advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 ;; Advice information for a certain function accumulates as the value of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 ;; `advice-info' property of the function symbol. This accumulation is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 ;; completely independent of the fact that that function might not yet be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 ;; defined. The special forms `defun' and `defmacro' have been advised to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 ;; check whether the function/macro they defined had advice information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 ;; associated with it. If so and forward advice is enabled, the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 ;; definition will be saved, and then the advice will be activated. When a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 ;; file is loaded in a v18 Emacs the functions/macros it defines are also
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 ;; defined with calls to `defun/defmacro'. Hence, we can forward advise
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 ;; functions/macros which will be defined later during a load/autoload of some
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 ;; file (for compiled files generated by jwz's byte-compiler in a v19 Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 ;; this is slightly more complicated but the basic idea is the same).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 ;; @@ Enabling/disabling pieces or sets of advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 ;; ===============================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 ;; A major motivation for the development of this advice package was to bring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 ;; a little bit more structure into the function overloading chaos in Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 ;; Lisp. Many packages achieve some of their functionality by adding a little
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 ;; bit (or a lot) to the standard functionality of some Emacs Lisp function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 ;; ange-ftp is a very popular package that achieves its magic by overloading
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 ;; most Emacs Lisp functions that deal with files. A popular function that's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 ;; overloaded by many packages is `expand-file-name'. The situation that one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 ;; function is multiply overloaded can arise easily.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 ;; Once in a while it would be desirable to be able to disable some/all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 ;; overloads of a particular package while keeping all the rest. Ideally -
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 ;; at least in my opinion - these overloads would all be done with advice,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
615 ;; I know I am dreaming right now... In that ideal case the enable/disable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616 ;; mechanism of advice could be used to achieve just that.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 ;; Every piece of advice is associated with an enablement flag. When the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 ;; advised definition of a particular function gets constructed (e.g., during
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 ;; activation) only the currently enabled pieces of advice will be considered.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 ;; This mechanism allows one to have different "views" of an advised function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 ;; dependent on what pieces of advice are currently enabled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 ;; Another motivation for this mechanism is that it allows one to define a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 ;; piece of advice for some function yet keep it dormant until a certain
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 ;; condition is met. Until then activation of the function will not make use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 ;; of that piece of advice. Once the condition is met the advice can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 ;; enabled and a reactivation of the function will add its functionality as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 ;; part of the new advised definition. For example, the advices of `defun'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 ;; etc. used by advice itself will stay disabled until `ad-start-advice' is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 ;; called and some variables have the proper values. Hence, if somebody
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 ;; else advised these functions too and activates them the advices defined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 ;; by advice will get used only if they are intended to be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 ;; The main interface to this mechanism are the interactive functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 ;; `ad-enable-advice' and `ad-disable-advice'. For example, the following
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 ;; would disable a particular advice of the function `foo':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 ;; (ad-disable-advice 'foo 'before 'my-advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 ;; This call by itself only changes the flag, to get the proper effect in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 ;; the advised definition too one has to activate `foo' with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 ;; (ad-activate 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 ;; or interactively. To disable whole sets of advices one can use a regular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 ;; expression mechanism. For example, let us assume that ange-ftp actually
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 ;; used advice to overload all its functions, and that it used the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 ;; "ange-ftp-" prefix for all its advice names, then we could temporarily
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 ;; disable all its advices with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 ;; (ad-disable-regexp "^ange-ftp-")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 ;; and the following call would put that actually into effect:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 ;; (ad-activate-regexp "^ange-ftp-")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 ;; A saver way would have been to use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 ;; (ad-update-regexp "^ange-ftp-")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 ;; instead which would have only reactivated currently actively advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 ;; functions, but not functions that were currently deactivated. All these
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
664 ;; functions can also be called interactively.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 ;; A certain piece of advice is considered a match if its name contains a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 ;; match for the regular expression. To enable ange-ftp again we would use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 ;; `ad-enable-regexp' and then activate or update again.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 ;; @@ Forward advice, automatic advice activation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 ;; ===============================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 ;; Because most Emacs Lisp packages are loaded on demand via an autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 ;; mechanism it is essential to be able to "forward advise" functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 ;; Otherwise, proper advice definition and activation would make it necessary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 ;; to preload every file that defines a certain function before it can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 ;; advised, which would partly defeat the purpose of the advice mechanism.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
677
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678 ;; In the following, "forward advice" always implies its automatic activation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 ;; once a function gets defined, and not just the accumulation of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 ;; information for a possibly undefined function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 ;; Advice implements forward advice mainly via the following: 1) Separation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 ;; of advice definition and activation that makes it possible to accumulate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 ;; advice information without having the original function already defined,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 ;; 2) special versions of the built-in functions `fset/defalias' which check
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 ;; for advice information whenever they define a function. If advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 ;; information was found then the advice will immediately get activated when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 ;; the function gets defined.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 ;; Automatic advice activation means, that whenever a function gets defined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 ;; with either `defun', `defmacro', `fset' or by loading a byte-compiled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 ;; file, and the function has some advice-info stored with it then that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 ;; advice will get activated right away.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 ;; @@@ Enabling automatic advice activation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 ;; =========================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 ;; Automatic advice activation is enabled by default. It can be disabled by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 ;; doint `M-x ad-stop-advice' and enabled again with `M-x ad-start-advice'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 ;; @@ Caching of advised definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 ;; ==================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 ;; After an advised definition got constructed it gets cached as part of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 ;; advised function's advice-info so it can be reused, for example, after an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 ;; intermediate deactivation. Because the advice-info of a function might
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 ;; change between the time of caching and reuse a cached definition gets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706 ;; a cache-id associated with it so it can be verified whether the cached
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 ;; definition is still valid (the main application of this is preactivation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 ;; - see below).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 ;; When an advised function gets activated and a verifiable cached definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711 ;; is available, then that definition will be used instead of creating a new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 ;; advised definition from scratch. If you want to make sure that a new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 ;; definition gets constructed then you should use `ad-clear-cache' before you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714 ;; activate the advised function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 ;; @@ Preactivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 ;; =================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 ;; Constructing an advised definition is moderately expensive. In a situation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 ;; where one package defines a lot of advised functions it might be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 ;; prohibitively expensive to do all the advised definition construction at
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 ;; runtime. Preactivation is a mechanism that allows compile-time construction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 ;; of compiled advised definitions that can be activated cheaply during
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 ;; runtime. Preactivation uses the caching mechanism to do that. Here's how it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 ;; works:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 ;; When the byte-compiler compiles a `defadvice' that has the `preactivate'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 ;; flag specified, it uses the current original definition of the advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 ;; function plus the advice specified in this `defadvice' (even if it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 ;; specified as disabled) and all other currently enabled pieces of advice to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 ;; construct an advised definition and an identifying cache-id and makes them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 ;; part of the `defadvice' expansion which will then be compiled by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 ;; byte-compiler (to ensure that in a v18 emacs you have to put the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 ;; `defadvice' inside a `defun' to get it compiled and then you have to call
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 ;; that compiled `defun' in order to actually execute the `defadvice'). When
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 ;; the file with the compiled, preactivating `defadvice' gets loaded the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 ;; precompiled advised definition will be cached on the advised function's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 ;; advice-info. When it gets activated (can be immediately on execution of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 ;; `defadvice' or any time later) the cache-id gets checked against the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 ;; current state of advice and if it is verified the precompiled definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 ;; will be used directly (the verification is pretty cheap). If it couldn't get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 ;; verified a new advised definition for that function will be built from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 ;; scratch, hence, the efficiency added by the preactivation mechanism does
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 ;; not at all impair the flexibility of the advice mechanism.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 ;; MORAL: In order get all the efficiency out of preactivation the advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 ;; state of an advised function at the time the file with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 ;; preactivating `defadvice' gets byte-compiled should be exactly
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 ;; the same as it will be when the advice of that function gets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 ;; actually activated. If it is not there is a high chance that the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 ;; cache-id will not match and hence a new advised definition will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 ;; have to be constructed at runtime.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 ;; Preactivation and forward advice do not contradict each other. It is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 ;; perfectly ok to load a file with a preactivating `defadvice' before the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 ;; original definition of the advised function is available. The constructed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 ;; advised definition will be used once the original function gets defined and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 ;; its advice gets activated. The only constraint is that at the time the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 ;; file with the preactivating `defadvice' got compiled the original function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 ;; definition was available.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 ;; TIPS: Here are some indications that a preactivation did not work the way
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 ;; you intended it to work:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 ;; - Activation of the advised function takes longer than usual/expected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 ;; - The byte-compiler gets loaded while an advised function gets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 ;; activated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 ;; - `byte-compile' is part of the `features' variable even though you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 ;; did not use the byte-compiler
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 ;; Right now advice does not provide an elegant way to find out whether
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 ;; and why a preactivation failed. What you can do is to trace the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 ;; function `ad-cache-id-verification-code' (with the function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 ;; `trace-function-background' defined in my trace.el package) before
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 ;; any of your advised functions get activated. After they got
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 ;; activated check whether all calls to `ad-cache-id-verification-code'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 ;; returned `verified' as a result. Other values indicate why the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 ;; verification failed which should give you enough information to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 ;; fix your preactivation/compile/load/activation sequence.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 ;; IMPORTANT: There is one case (that I am aware of) that can make
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 ;; preactivation fail, i.e., a preconstructed advised definition that does
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 ;; NOT match the current state of advice gets used nevertheless. That case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 ;; arises if one package defines a certain piece of advice which gets used
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 ;; during preactivation, and another package incompatibly redefines that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 ;; very advice (i.e., same function/class/name), and it is the second advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 ;; that is available when the preconstructed definition gets activated, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 ;; that was the only definition of that advice so far (`ad-add-advice'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 ;; catches advice redefinitions and clears the cache in such a case).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 ;; Catching that would make the cache verification too expensive.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 ;; MORAL-II: Redefining somebody else's advice is BAAAAD (to speak with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 ;; George Walker Bush), and why would you redefine your own advice anyway?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 ;; Advice is a mechanism to facilitate function redefinition, not advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 ;; redefinition (wait until I write Meta-Advice :-). If you really have
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 ;; to undo somebody else's advice try to write a "neutralizing" advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 ;; @@ Advising macros and special forms and other dangerous things:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 ;; ================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 ;; Look at the corresponding tutorial sections for more information on
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 ;; these topics. Here it suffices to point out that the special treatment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 ;; of macros and special forms by the byte-compiler can lead to problems
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 ;; when they get advised. Macros can create problems because they get
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 ;; expanded at compile time, hence, they might not have all the necessary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 ;; runtime support and such advice cannot be de/activated or changed as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 ;; it is possible for functions. Special forms create problems because they
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 ;; have to be advised "into" macros, i.e., an advised special form is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 ;; implemented as a macro, hence, in most cases the byte-compiler will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 ;; not recognize it as a special form anymore which can lead to very strange
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 ;; results.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 ;; MORAL: - Only advise macros or special forms when you are absolutely sure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 ;; what you are doing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 ;; - As a safety measure, always do `ad-deactivate-all' before you
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 ;; byte-compile a file to make sure that even if some inconsiderate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 ;; person advised some special forms you'll get proper compilation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 ;; results. After compilation do `ad-activate-all' to get back to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 ;; the previous state.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 ;; @@ Adding a piece of advice with `ad-add-advice':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 ;; =================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 ;; The non-interactive function `ad-add-advice' can be used to add a piece of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 ;; advice to some function without using `defadvice'. This is useful if advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 ;; has to be added somewhere by a function (also look at `ad-make-advice').
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 ;; @@ Activation/deactivation advices, file load hooks:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 ;; ====================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 ;; There are two special classes of advice called `activation' and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826 ;; `deactivation'. The body forms of these advices are not included into the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 ;; advised definition of a function, rather they are assembled into a hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 ;; form which will be evaluated whenever the advice-info of the advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
829 ;; function gets activated or deactivated. One application of this mechanism
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 ;; is to define file load hooks for files that do not provide such hooks
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 ;; (v19s already come with a general file-load-hook mechanism, v18s don't).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 ;; For example, suppose you want to print a message whenever `file-x' gets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833 ;; loaded, and suppose the last function defined in `file-x' is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834 ;; `file-x-last-fn'. Then we can define the following advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 ;; (defadvice file-x-last-fn (activation file-x-load-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 ;; "Executed whenever file-x is loaded"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 ;; (if load-in-progress (message "Loaded file-x")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 ;; This will constitute a forward advice for function `file-x-last-fn' which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 ;; will get activated when `file-x' is loaded (only if forward advice is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 ;; enabled of course). Because there are no "real" pieces of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 ;; available for it, its definition will not be changed, but the activation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 ;; advice will be run during its activation which is equivalent to having a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 ;; file load hook for `file-x'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 ;; @@ Summary of main advice concepts:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 ;; ===================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 ;; - Definition:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 ;; A piece of advice gets defined with `defadvice' and added to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 ;; `advice-info' property of a function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 ;; - Enablement:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 ;; Every piece of advice has an enablement flag associated with it. Only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 ;; enabled advices are considered during construction of an advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 ;; definition.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 ;; - Activation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 ;; Redefine an advised function with its advised definition. Constructs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 ;; an advised definition from scratch if no verifiable cached advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 ;; definition is available and caches it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 ;; - Deactivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 ;; Back-define an advised function to its original definition.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862 ;; - Update:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 ;; Reactivate an advised function but only if its advice is currently
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 ;; active. This can be used to bring all currently advised function up
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865 ;; to date with the current state of advice without also activating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 ;; currently deactivated functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 ;; - Caching:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 ;; Is the saving of an advised definition and an identifying cache-id so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 ;; it can be reused, for example, for activation after deactivation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 ;; - Preactivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 ;; Is the construction of an advised definition according to the current
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 ;; state of advice during byte-compilation of a file with a preactivating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 ;; `defadvice'. That advised definition can then rather cheaply be used
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 ;; during activation without having to construct an advised definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 ;; from scratch at runtime.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 ;; @@ Summary of interactive advice manipulation functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 ;; ========================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 ;; The following interactive functions can be used to manipulate the state
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 ;; of advised functions (all of them support completion on function names,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 ;; advice classes and advice names):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 ;; - ad-activate to activate the advice of a FUNCTION
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 ;; - ad-deactivate to deactivate the advice of a FUNCTION
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 ;; - ad-update to activate the advice of a FUNCTION unless it was not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 ;; yet activated or is currently deactivated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 ;; - ad-unadvise deactivates a FUNCTION and removes all of its advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 ;; information, hence, it cannot be activated again
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 ;; - ad-recover tries to redefine a FUNCTION to its original definition and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 ;; discards all advice information (a low-level `ad-unadvise').
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 ;; Use only in emergencies.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 ;; - ad-remove-advice removes a particular piece of advice of a FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 ;; You still have to do call `ad-activate' or `ad-update' to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 ;; activate the new state of advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 ;; - ad-enable-advice enables a particular piece of advice of a FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 ;; - ad-disable-advice disables a particular piece of advice of a FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 ;; - ad-enable-regexp maps over all currently advised functions and enables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 ;; every advice whose name contains a match for a regular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 ;; expression.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 ;; - ad-disable-regexp disables matching advices.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 ;; - ad-activate-regexp activates all advised function with a matching advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 ;; - ad-deactivate-regexp deactivates all advised function with matching advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 ;; - ad-update-regexp updates all advised function with a matching advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 ;; - ad-activate-all activates all advised functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 ;; - ad-deactivate-all deactivates all advised functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 ;; - ad-update-all updates all advised functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 ;; - ad-unadvise-all unadvises all advised functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910 ;; - ad-recover-all recovers all advised functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912 ;; - ad-compile byte-compiles a function/macro if it is compilable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914 ;; @@ Summary of forms with special meanings when used within an advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915 ;; =====================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 ;; ad-return-value name of the return value variable (get/settable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 ;; ad-subr-args name of &rest argument variable used for advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 ;; subrs whose actual argument list cannot be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 ;; determined (get/settable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920 ;; (ad-get-arg <pos>), (ad-get-args <pos>),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921 ;; (ad-set-arg <pos> <value>), (ad-set-args <pos> <value-list>)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922 ;; argument access text macros to get/set the values of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923 ;; actual arguments at a certain position
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 ;; ad-arg-bindings text macro that returns the actual names, values
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 ;; and types of the arguments as a list of bindings. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 ;; order of the bindings corresponds to the order of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 ;; arguments. The individual fields of every binding (name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928 ;; value and type) can be accessed with the function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929 ;; `ad-arg-binding-field' (see example above).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 ;; ad-do-it text macro that identifies the place where the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931 ;; or wrapped definition should go in an around advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 ;; @ Foo games: An advice tutorial
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 ;; ===============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 ;; The following tutorial was created in Emacs 18.59. Left-justified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 ;; s-expressions are input forms followed by one or more result forms.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938 ;; First we have to start the advice magic:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 ;; (ad-start-advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 ;; We start by defining an innocent looking function `foo' that simply
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 ;; adds 1 to its argument X:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 ;; (defun foo (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 ;; "Add 1 to X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 ;; (1+ x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 ;; 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 ;; @@ Defining a simple piece of advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 ;; =====================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 ;; Now let's define the first piece of advice for `foo'. To do that we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 ;; use the macro `defadvice' which takes a function name, a list of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 ;; specifiers and a list of body forms as arguments. The first element of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 ;; the advice specifiers is the class of the advice, the second is its name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960 ;; the third its position and the rest are some flags. The class of our
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961 ;; first advice is `before', its name is `fg-add2', its position among the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962 ;; currently defined before advices (none so far) is `first', and the advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 ;; will be `activate'ed immediately. Advice names are global symbols, hence,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964 ;; the name space conventions used for function names should be applied. All
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 ;; advice names in this tutorial will be prefixed with `fg' for `Foo Games'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 ;; (because everybody has the right to be inconsistent all the function names
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 ;; used in this tutorial do NOT follow this convention).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 ;; In the body of an advice we can refer to the argument variables of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 ;; original function by name. Here we add 1 to X so the effect of calling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 ;; `foo' will be to actually add 2. All of the advice definitions below only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972 ;; have one body form for simplicity, but there is no restriction to that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973 ;; extent. Every piece of advice can have a documentation string which will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
974 ;; be combined with the documentation of the original function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 ;; (defadvice foo (before fg-add2 first activate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 ;; "Add 2 to X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 ;; (setq x (1+ x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
982 ;; 5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984 ;; @@ Specifying the position of an advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
985 ;; ========================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986 ;; Now we define the second before advice which will cancel the effect of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
987 ;; the previous advice. This time we specify the position as 0 which is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988 ;; equivalent to `first'. A number can be used to specify the zero-based
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 ;; position of an advice among the list of advices in the same class. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990 ;; time we already have one before advice hence the position specification
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991 ;; actually has an effect. So, after the following definition the position
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 ;; of the previous advice will be 1 even though we specified it with `first'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993 ;; above, the reason for this is that the position argument is relative to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994 ;; the currently defined pieces of advice which by now has changed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996 ;; (defadvice foo (before fg-cancel-add2 0 activate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 ;; "Again only add 1 to X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 ;; (setq x (1- x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 ;; 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 ;; @@ Redefining a piece of advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1005 ;; ================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006 ;; Now we define an advice with the same class and same name but with a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007 ;; different position. Defining an advice in a class in which an advice with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008 ;; that name already exists is interpreted as a redefinition of that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 ;; particular advice, in which case the position argument will be ignored
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 ;; and the previous position of the redefined piece of advice is used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 ;; Advice flags can be specified with non-ambiguous initial substrings, hence,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012 ;; from now on we'll use `act' instead of the verbose `activate'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1013 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014 ;; (defadvice foo (before fg-cancel-add2 last act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015 ;; "Again only add 1 to X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1016 ;; (setq x (1- x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 ;; @@ Assembly of advised documentation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020 ;; =====================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021 ;; The documentation strings of the various pieces of advice are assembled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022 ;; in order which shows that advice `fg-cancel-add2' is still the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1023 ;; `before' advice even though we specified position `last' above:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1025 ;; (documentation 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 ;; "Add 1 to X.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1028 ;; This function is advised with the following advice(s):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1029 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1030 ;; fg-cancel-add2 (before):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1031 ;; Again only add 1 to X.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1032 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1033 ;; fg-add2 (before):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1034 ;; Add 2 to X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1035 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1036 ;; @@ Advising interactive behavior:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1037 ;; =================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1038 ;; We can make a function interactive (or change its interactive behavior)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1039 ;; by specifying an interactive form in one of the before or around
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1040 ;; advices (there could also be body forms in this advice). The particular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1041 ;; definition always assigns 5 as an argument to X which gives us 6 as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1042 ;; result when we call foo interactively:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1043 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1044 ;; (defadvice foo (before fg-inter last act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1045 ;; "Use 5 as argument when called interactively."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1046 ;; (interactive (list 5)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1047 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1048 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1049 ;; (call-interactively 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1050 ;; 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1051 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1052 ;; If more than one advice have an interactive declaration, then the one of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1053 ;; the advice with the smallest position will be used (before advices go
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1054 ;; before around and after advices), hence, the declaration below does
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1055 ;; not have any effect:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1056 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1057 ;; (defadvice foo (before fg-inter2 last act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1058 ;; (interactive (list 6)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1059 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1060 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1061 ;; (call-interactively 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1062 ;; 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1063 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1064 ;; Let's have a look at what the definition of `foo' looks like now
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1065 ;; (indentation added by hand for legibility):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1066 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1067 ;; (symbol-function 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1068 ;; (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1069 ;; "$ad-doc: foo$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1070 ;; (interactive (list 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1071 ;; (let (ad-return-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1072 ;; (setq x (1- x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1073 ;; (setq x (1+ x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1074 ;; (setq ad-return-value (ad-Orig-foo x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1075 ;; ad-return-value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1076 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1077 ;; @@ Around advices:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1078 ;; ==================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1079 ;; Now we'll try some `around' advices. An around advice is a wrapper around
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1080 ;; the original definition. It can shadow or establish bindings for the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1081 ;; original definition, and it can look at and manipulate the value returned
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1082 ;; by the original function. The position of the special keyword `ad-do-it'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1083 ;; specifies where the code of the original function will be executed. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1084 ;; keyword can appear multiple times which will result in multiple calls of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1085 ;; the original function in the resulting advised code. Note, that if we don't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1086 ;; specify a position argument (i.e., `first', `last' or a number), then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1087 ;; `first' (or 0) is the default):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1088 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1089 ;; (defadvice foo (around fg-times-2 act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1090 ;; "First double X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1091 ;; (let ((x (* x 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1092 ;; ad-do-it))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1093 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1094 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1095 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1096 ;; 7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1097 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1098 ;; Around advices are assembled like onion skins where the around advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1099 ;; with position 0 is the outermost skin and the advice at the last position
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1100 ;; is the innermost skin which is directly wrapped around the call of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1101 ;; original definition of the function. Hence, after the next `defadvice' we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1102 ;; will first multiply X by 2 then add 1 and then call the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1103 ;; definition (i.e., add 1 again):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1104 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1105 ;; (defadvice foo (around fg-add-1 last act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1106 ;; "Add 1 to X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1107 ;; (let ((x (1+ x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1108 ;; ad-do-it))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1109 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1110 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1111 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1112 ;; 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1113 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1114 ;; Again, let's see what the definition of `foo' looks like so far:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1115 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1116 ;; (symbol-function 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1117 ;; (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1118 ;; "$ad-doc: foo$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1119 ;; (interactive (list 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1120 ;; (let (ad-return-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1121 ;; (setq x (1- x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1122 ;; (setq x (1+ x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1123 ;; (let ((x (* x 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1124 ;; (let ((x (1+ x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1125 ;; (setq ad-return-value (ad-Orig-foo x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1126 ;; ad-return-value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1127 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1128 ;; @@ Controlling advice activation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1129 ;; =================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1130 ;; In every `defadvice' so far we have used the flag `activate' to activate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1131 ;; the advice immediately after its definition, and that's what we want in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1132 ;; most cases. However, if we define multiple pieces of advice for a single
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1133 ;; function then activating every advice immediately is inefficient. A
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1134 ;; better way to do this is to only activate the last defined advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1135 ;; For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1136 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1137 ;; (defadvice foo (after fg-times-x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1138 ;; "Multiply the result with X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1139 ;; (setq ad-return-value (* ad-return-value x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1140 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1141 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1142 ;; This still yields the same result as before:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1143 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1144 ;; 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1145 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1146 ;; Now we define another advice and activate which will also activate the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1147 ;; previous advice `fg-times-x'. Note the use of the special variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1148 ;; `ad-return-value' in the body of the advice which is set to the result of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1149 ;; the original function. If we change its value then the value returned by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1150 ;; the advised function will be changed accordingly:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1151 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1152 ;; (defadvice foo (after fg-times-x-again act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1153 ;; "Again multiply the result with X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1154 ;; (setq ad-return-value (* ad-return-value x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1155 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1156 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1157 ;; Now the advices have an effect:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1158 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1159 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1160 ;; 72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1161 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1162 ;; @@ Protecting advice execution:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1163 ;; ===============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1164 ;; Once in a while we define an advice to perform some cleanup action,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1165 ;; for example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1166 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1167 ;; (defadvice foo (after fg-cleanup last act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1168 ;; "Do some cleanup."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1169 ;; (print "Let's clean up now!"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1170 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1171 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1172 ;; However, in case of an error the cleanup won't be performed:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1173 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1174 ;; (condition-case error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1175 ;; (foo t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1176 ;; (error 'error-in-foo))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1177 ;; error-in-foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1178 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1179 ;; To make sure a certain piece of advice gets executed even if some error or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1180 ;; non-local exit occurred in any preceding code, we can protect it by using
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1181 ;; the `protect' keyword. (if any of the around advices is protected then the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1182 ;; whole around advice onion will be protected):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1183 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1184 ;; (defadvice foo (after fg-cleanup prot act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1185 ;; "Do some protected cleanup."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1186 ;; (print "Let's clean up now!"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1187 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1188 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1189 ;; Now the cleanup form will be executed even in case of an error:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1190 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1191 ;; (condition-case error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1192 ;; (foo t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1193 ;; (error 'error-in-foo))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1194 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1195 ;; error-in-foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1196 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1197 ;; Again, let's see what `foo' looks like:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1198 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1199 ;; (symbol-function 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1200 ;; (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1201 ;; "$ad-doc: foo$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1202 ;; (interactive (list 5))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1203 ;; (let (ad-return-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1204 ;; (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1205 ;; (progn (setq x (1- x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1206 ;; (setq x (1+ x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1207 ;; (let ((x (* x 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1208 ;; (let ((x (1+ x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1209 ;; (setq ad-return-value (ad-Orig-foo x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1210 ;; (setq ad-return-value (* ad-return-value x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1211 ;; (setq ad-return-value (* ad-return-value x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1212 ;; (print "Let's clean up now!"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1213 ;; ad-return-value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1214 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1215 ;; @@ Compilation of advised definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1216 ;; ======================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1217 ;; Finally, we can specify the `compile' keyword in a `defadvice' to say
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1218 ;; that we want the resulting advised function to be byte-compiled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1219 ;; (`compile' will be ignored unless we also specified `activate'):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1220 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1221 ;; (defadvice foo (after fg-cleanup prot act comp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1222 ;; "Do some protected cleanup."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1223 ;; (print "Let's clean up now!"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1224 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1225 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1226 ;; Now `foo' is byte-compiled:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1227 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1228 ;; (symbol-function 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1229 ;; (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1230 ;; "$ad-doc: foo$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1231 ;; (interactive (byte-code "....." [5] 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1232 ;; (byte-code "....." [ad-return-value x nil ((byte-code "....." [print "Let's clean up now!"] 2)) * 2 ad-Orig-foo] 6))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1233 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1234 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1235 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1236 ;; 72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1237 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1238 ;; @@ Enabling and disabling pieces of advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1239 ;; ===========================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1240 ;; Once in a while it is desirable to temporarily disable a piece of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1241 ;; so that it won't be considered during activation, for example, if two
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1242 ;; different packages advise the same function and one wants to temporarily
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1243 ;; neutralize the effect of the advice of one of the packages.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1244 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1245 ;; The following disables the after advice `fg-times-x' in the function `foo'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1246 ;; All that does is to change a flag for this particular advice. All the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1247 ;; other information defining it will be left unchanged (e.g., its relative
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1248 ;; position in this advice class, etc.).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1249 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1250 ;; (ad-disable-advice 'foo 'after 'fg-times-x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1251 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1252 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1253 ;; For this to have an effect we have to activate `foo':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1254 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1255 ;; (ad-activate 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1256 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1257 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1258 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1259 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1260 ;; 24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1261 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1262 ;; If we want to disable all multiplication advices in `foo' we can use a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1263 ;; regular expression that matches the names of such advices. Actually, any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1264 ;; advice name that contains a match for the regular expression will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1265 ;; called a match. A special advice class `any' can be used to consider
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1266 ;; all advice classes:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1267 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1268 ;; (ad-disable-advice 'foo 'any "^fg-.*times")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1269 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1270 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1271 ;; (ad-activate 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1272 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1273 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1274 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1275 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1276 ;; 5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1277 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1278 ;; To enable the disabled advice we could use either `ad-enable-advice'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1279 ;; similar to `ad-disable-advice', or as an alternative `ad-enable-regexp'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1280 ;; which will enable matching advices in ALL currently advised functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1281 ;; Hence, this can be used to dis/enable advices made by a particular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1282 ;; package to a set of functions as long as that package obeys standard
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1283 ;; advice name conventions. We prefixed all advice names with `fg-', hence
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1284 ;; the following will do the trick (`ad-enable-regexp' returns the number
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1285 ;; of matched advices):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1286 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1287 ;; (ad-enable-regexp "^fg-")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1288 ;; 9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1289 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1290 ;; The following will activate all currently active advised functions that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1291 ;; contain some advice matched by the regular expression. This is a save
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1292 ;; way to update the activation of advised functions whose advice changed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1293 ;; in some way or other without accidentally also activating currently
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1294 ;; deactivated functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1295 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1296 ;; (ad-update-regexp "^fg-")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1297 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1298 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1299 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1300 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1301 ;; 72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1302 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1303 ;; Another use for the dis/enablement mechanism is to define a piece of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1304 ;; and keep it "dormant" until a particular condition is satisfied, i.e., until
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1305 ;; then the advice will not be used during activation. The `disable' flag lets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1306 ;; one do that with `defadvice':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1307 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1308 ;; (defadvice foo (before fg-1-more dis)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1309 ;; "Add yet 1 more."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1310 ;; (setq x (1+ x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1311 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1312 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1313 ;; (ad-activate 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1314 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1315 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1316 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1317 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1318 ;; 72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1319 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1320 ;; (ad-enable-advice 'foo 'before 'fg-1-more)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1321 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1322 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1323 ;; (ad-activate 'foo)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1324 ;; foo
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1325 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1326 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1327 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1328 ;; 160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1329 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1330 ;; @@ Caching:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1331 ;; ===========
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1332 ;; Advised definitions get cached to allow efficient activation/deactivation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1333 ;; without having to reconstruct them if nothing in the advice-info of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1334 ;; function has changed. The following idiom can be used to temporarily
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1335 ;; deactivate functions that have a piece of advice defined by a certain
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1336 ;; package (we save the old definition to check out caching):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1337 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1338 ;; (setq old-definition (symbol-function 'foo))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1339 ;; (lambda (x) ....)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1340 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1341 ;; (ad-deactivate-regexp "^fg-")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1342 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1343 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1344 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1345 ;; 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1346 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1347 ;; (ad-activate-regexp "^fg-")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1348 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1349 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1350 ;; (eq old-definition (symbol-function 'foo))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1351 ;; t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1352 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1353 ;; (foo 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1354 ;; "Let's clean up now!"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1355 ;; 160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1356 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1357 ;; @@ Forward advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1358 ;; ==================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1359 ;; To enable automatic activation of forward advice we first have to set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1360 ;; `ad-activate-on-definition' to t and restart advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1361 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1362 ;; (setq ad-activate-on-definition t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1363 ;; t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1364 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1365 ;; (ad-start-advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1366 ;; (ad-activate-defined-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1367 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1368 ;; Let's define a piece of advice for an undefined function:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1369 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1370 ;; (defadvice bar (before fg-sub-1-more act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1371 ;; "Subtract one more from X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1372 ;; (setq x (1- x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1373 ;; bar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1374 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1375 ;; `bar' is not yet defined:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1376 ;; (fboundp 'bar)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1377 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1378 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1379 ;; Now we define it and the forward advice will get activated (only because
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1380 ;; `ad-activate-on-definition' was t when we started advice above with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1381 ;; `ad-start-advice'):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1382 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1383 ;; (defun bar (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1384 ;; "Subtract 1 from X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1385 ;; (1- x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1386 ;; bar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1387 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1388 ;; (bar 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1389 ;; 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1390 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1391 ;; Redefinition will activate any available advice if the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1392 ;; `ad-redefinition-action' is either `warn', `accept' or `discard':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1393 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1394 ;; (defun bar (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1395 ;; "Subtract 2 from X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1396 ;; (- x 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1397 ;; bar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1398 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1399 ;; (bar 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1400 ;; 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1401 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1402 ;; @@ Preactivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1403 ;; =================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1404 ;; Constructing advised definitions is moderately expensive, hence, it is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1405 ;; desirable to have a way to construct them at byte-compile time.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1406 ;; Preactivation is a mechanism that allows one to do that.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1407 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1408 ;; (defun fie (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1409 ;; "Multiply X by 2."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1410 ;; (* x 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1411 ;; fie
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1412 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1413 ;; (defadvice fie (before fg-times-4 preact)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1414 ;; "Multiply X by 4."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1415 ;; (setq x (* x 2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1416 ;; fie
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1417 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1418 ;; This advice did not affect `fie'...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1419 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1420 ;; (fie 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1421 ;; 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1422 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1423 ;; ...but it constructed a cached definition that will be used once `fie' gets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1424 ;; activated as long as its current advice state is the same as it was during
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1425 ;; preactivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1426 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1427 ;; (setq cached-definition (ad-get-cache-definition 'fie))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1428 ;; (lambda (x) ....)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1429 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1430 ;; (ad-activate 'fie)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1431 ;; fie
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1432 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1433 ;; (eq cached-definition (symbol-function 'fie))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1434 ;; t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1435 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1436 ;; (fie 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1437 ;; 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1438 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1439 ;; If you put a preactivating `defadvice' into a Lisp file that gets byte-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1440 ;; compiled then the constructed advised definition will get compiled by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1441 ;; the byte-compiler. For that to occur in a v18 emacs you have to put the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1442 ;; `defadvice' inside a `defun' because the v18 compiler does not compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1443 ;; top-level forms other than `defun' or `defmacro', for example,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1444 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1445 ;; (defun fg-defadvice-fum ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1446 ;; (defadvice fum (before fg-times-4 preact act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1447 ;; "Multiply X by 4."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1448 ;; (setq x (* x 2))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1449 ;; fg-defadvice-fum
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1450 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1451 ;; So far, no `defadvice' for `fum' got executed, but when we compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1452 ;; `fg-defadvice-fum' the `defadvice' will be expanded by the byte compiler.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1453 ;; In order for preactivation to be effective we have to have a proper
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1454 ;; definition of `fum' around at preactivation time, hence, we define it now:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1455 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1456 ;; (defun fum (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1457 ;; "Multiply X by 2."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1458 ;; (* x 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1459 ;; fum
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1460 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1461 ;; Now we compile the defining function which will construct an advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1462 ;; definition during expansion of the `defadvice', compile it and store it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1463 ;; as part of the compiled `fg-defadvice-fum':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1464 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1465 ;; (ad-compile-function 'fg-defadvice-fum)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1466 ;; (lambda nil (byte-code ...))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1467 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1468 ;; `fum' is still completely unaffected:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1469 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1470 ;; (fum 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1471 ;; 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1472 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1473 ;; (ad-get-advice-info 'fum)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1474 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1475 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1476 ;; (fg-defadvice-fum)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1477 ;; fum
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1478 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1479 ;; Now the advised version of `fum' is compiled because the compiled definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1480 ;; constructed during preactivation was used, even though we did not specify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1481 ;; the `compile' flag:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1482 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1483 ;; (symbol-function 'fum)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1484 ;; (lambda (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1485 ;; "$ad-doc: fum$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1486 ;; (byte-code "....." [ad-return-value x nil * 2 ad-Orig-fum] 4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1487 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1488 ;; (fum 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1489 ;; 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1490 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1491 ;; A preactivated definition will only be used if it matches the current
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1492 ;; function definition and advice information. If it does not match it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1493 ;; will simply be discarded and a new advised definition will be constructed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1494 ;; from scratch. For example, let's first remove all advice-info for `fum':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1495 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1496 ;; (ad-unadvise 'fum)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1497 ;; (("fie") ("bar") ("foo") ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1498 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1499 ;; And now define a new piece of advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1500 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1501 ;; (defadvice fum (before fg-interactive act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1502 ;; "Make fum interactive."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1503 ;; (interactive "nEnter x: "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1504 ;; fum
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1505 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1506 ;; When we now try to use a preactivation it will not be used because the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1507 ;; current advice state is different from the one at preactivation time. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1508 ;; is no tragedy, everything will work as expected just not as efficient,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1509 ;; because a new advised definition has to be constructed from scratch:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1510 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1511 ;; (fg-defadvice-fum)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1512 ;; fum
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1513 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1514 ;; A new uncompiled advised definition got constructed:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1515 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1516 ;; (ad-compiled-p (symbol-function 'fum))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1517 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1518 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1519 ;; (fum 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1520 ;; 8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1521 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1522 ;; MORAL: To get all the efficiency out of preactivation the function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1523 ;; definition and advice state at preactivation time must be the same as the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1524 ;; state at activation time. Preactivation does work with forward advice, all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1525 ;; that's necessary is that the definition of the forward advised function is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1526 ;; available when the `defadvice' with the preactivation gets compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1527 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1528 ;; @@ Portable argument access:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1529 ;; ============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1530 ;; So far, we always used the actual argument variable names to access an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1531 ;; argument in a piece of advice. For many advice applications this is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1532 ;; perfectly ok and keeps advices simple. However, it decreases portability
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1533 ;; of advices because it assumes specific argument variable names. For example,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1534 ;; if one advises a subr such as `eval-region' which then gets redefined by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1535 ;; some package (e.g., edebug) into a function with different argument names,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1536 ;; then a piece of advice written for `eval-region' that was written with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1537 ;; the subr arguments in mind will break. Similar situations arise when one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1538 ;; switches between major Emacs versions, e.g., certain subrs in v18 are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1539 ;; functions in v19 and vice versa. Also, in v19s subr argument lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1540 ;; are available and will be used, while they are not available in v18.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1541 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1542 ;; Argument access text macros allow one to access arguments of an advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1543 ;; function in a portable way without having to worry about all these
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1544 ;; possibilities. These macros will be translated into the proper access forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1545 ;; at activation time, hence, argument access will be as efficient as if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1546 ;; the arguments had been used directly in the definition of the advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1547 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1548 ;; (defun fuu (x y z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1549 ;; "Add 3 numbers."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1550 ;; (+ x y z))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1551 ;; fuu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1552 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1553 ;; (fuu 1 1 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1554 ;; 3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1555 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1556 ;; Argument access macros specify actual arguments at a certain position.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1557 ;; Position 0 access the first actual argument, position 1 the second etc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1558 ;; For example, the following advice adds 1 to each of the 3 arguments:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1559 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1560 ;; (defadvice fuu (before fg-add-1-to-all act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1561 ;; "Adds 1 to all arguments."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1562 ;; (ad-set-arg 0 (1+ (ad-get-arg 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1563 ;; (ad-set-arg 1 (1+ (ad-get-arg 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1564 ;; (ad-set-arg 2 (1+ (ad-get-arg 2))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1565 ;; fuu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1566 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1567 ;; (fuu 1 1 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1568 ;; 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1569 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1570 ;; Now suppose somebody redefines `fuu' with a rest argument. Our advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1571 ;; will still work because we used access macros (note, that automatic
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1572 ;; advice activation is still in effect, hence, the redefinition of `fuu'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1573 ;; will automatically activate all its advice):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1574 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1575 ;; (defun fuu (&rest numbers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1576 ;; "Add NUMBERS."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1577 ;; (apply '+ numbers))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1578 ;; fuu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1579 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1580 ;; (fuu 1 1 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1581 ;; 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1582 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1583 ;; (fuu 1 1 1 1 1 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1584 ;; 9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1585 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1586 ;; What's important to notice is that argument access macros access actual
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1587 ;; arguments regardless of how they got distributed onto argument variables.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1588 ;; In Emacs Lisp the semantics of an actual argument is determined purely
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1589 ;; by position, hence, as long as nobody changes the semantics of what a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1590 ;; certain actual argument at a certain position means the access macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1591 ;; will do the right thing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1592 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1593 ;; Because of &rest arguments we need a second kind of access macro that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1594 ;; can access all actual arguments starting from a certain position:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1595 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1596 ;; (defadvice fuu (before fg-print-args act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1597 ;; "Print all arguments."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1598 ;; (print (ad-get-args 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1599 ;; fuu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1600 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1601 ;; (fuu 1 2 3 4 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1602 ;; (1 2 3 4 5)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1603 ;; 18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1604 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1605 ;; (defadvice fuu (before fg-set-args act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1606 ;; "Swaps 2nd and 3rd arg and discards all the rest."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1607 ;; (ad-set-args 1 (list (ad-get-arg 2) (ad-get-arg 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1608 ;; fuu
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1609 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1610 ;; (fuu 1 2 3 4 4 4 4 4 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1611 ;; (1 3 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1612 ;; 9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1613 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1614 ;; (defun fuu (x y z)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1615 ;; "Add 3 numbers."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1616 ;; (+ x y z))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1617 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1618 ;; (fuu 1 2 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1619 ;; (1 3 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1620 ;; 9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1621 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1622 ;; @@ Defining the argument list of an advised function:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1623 ;; =====================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1624 ;; Once in a while it might be desirable to advise a function and additionally
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1625 ;; give it an extra argument that controls the advised code, for example, one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1626 ;; might want to make an interactive function sensitive to a prefix argument.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1627 ;; For such cases `defadvice' allows the specification of an argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1628 ;; for the advised function. Similar to the redefinition of interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1629 ;; behavior, the first argument list specification found in the list of before/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1630 ;; around/after advices will be used. Of course, the specified argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1631 ;; should be downward compatible with the original argument list, otherwise
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1632 ;; functions that call the advised function with the original argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1633 ;; in mind will break.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1634 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1635 ;; (defun fii (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1636 ;; "Add 1 to X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1637 ;; (1+ x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1638 ;; fii
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1639 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1640 ;; Now we advise `fii' to use an optional second argument that controls the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1641 ;; amount of incrementation. A list following the (optional) position
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1642 ;; argument of the advice will be interpreted as an argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1643 ;; specification. This means you cannot specify an empty argument list, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1644 ;; why would you want to anyway?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1645 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1646 ;; (defadvice fii (before fg-inc-x (x &optional incr) act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1647 ;; "Increment X by INCR (default is 1)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1648 ;; (setq x (+ x (1- (or incr 1)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1649 ;; fii
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1650 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1651 ;; (fii 3)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1652 ;; 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1653 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1654 ;; (fii 3 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1655 ;; 5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1656 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1657 ;; @@ Specifying argument lists of subrs:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1658 ;; ======================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1659 ;; The argument lists of subrs cannot be determined directly from Lisp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1660 ;; This means that Advice has to use `(&rest ad-subr-args)' as the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1661 ;; argument list of the advised subr which is not very efficient. In XEmacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1662 ;; subr argument lists can be determined from their documentation string, in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1663 ;; Emacs-19 this is the case for some but not all subrs. To accommodate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1664 ;; for the cases where the argument lists cannot be determined (e.g., in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1665 ;; v18 Emacs) Advice comes with a specification mechanism that allows the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1666 ;; advice programmer to tell advice what the argument list of a certain subr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1667 ;; really is.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1668 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1669 ;; In a v18 Emacs the following will return the &rest idiom:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1670 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1671 ;; (ad-arglist (symbol-function 'car))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1672 ;; (&rest ad-subr-args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1673 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1674 ;; To tell advice what the argument list of `car' really is we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1675 ;; can do the following:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1676 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1677 ;; (ad-define-subr-args 'car '(list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1678 ;; ((list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1679 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1680 ;; Now `ad-arglist' will return the proper argument list (this method is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1681 ;; actually used by advice itself for the advised definition of `fset'):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1682 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1683 ;; (ad-arglist (symbol-function 'car))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1684 ;; (list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1685 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1686 ;; The defined argument list will be stored on the property list of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1687 ;; subr name symbol. When advice looks for a subr argument list it first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1688 ;; checks for a definition on the property list, if that fails it tries
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1689 ;; to infer it from the documentation string and caches it on the property
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1690 ;; list if it was successful, otherwise `(&rest ad-subr-args)' will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1691 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1692 ;; @@ Advising interactive subrs:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1693 ;; ==============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1694 ;; For the most part there is no difference between advising functions and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1695 ;; advising subrs. There is one situation though where one might have to write
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1696 ;; slightly different advice code for subrs than for functions. This case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1697 ;; arises when one wants to access subr arguments in a before/around advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1698 ;; when the arguments were determined by an interactive call to the subr.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1699 ;; Advice cannot determine what `interactive' form determines the interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1700 ;; behavior of the subr, hence, when it calls the original definition in an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1701 ;; interactive subr invocation it has to use `call-interactively' to generate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1702 ;; the proper interactive behavior. Thus up to that call the arguments of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1703 ;; interactive subr will be nil. For example, the following advice for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1704 ;; `kill-buffer' will not work in an interactive invocation...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1705 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1706 ;; (defadvice kill-buffer (before fg-kill-buffer-hook first act preact comp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1707 ;; (my-before-kill-buffer-hook (ad-get-arg 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1708 ;; kill-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1709 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1710 ;; ...because the buffer argument will be nil in that case. The way out of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1711 ;; this dilemma is to provide an `interactive' specification that mirrors
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1712 ;; the interactive behavior of the unadvised subr, for example, the following
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1713 ;; will do the right thing even when `kill-buffer' is called interactively:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1714 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1715 ;; (defadvice kill-buffer (before fg-kill-buffer-hook first act preact comp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1716 ;; (interactive "bKill buffer: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1717 ;; (my-before-kill-buffer-hook (ad-get-arg 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1718 ;; kill-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1719 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1720 ;; @@ Advising macros:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1721 ;; ===================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1722 ;; Advising macros is slightly different because there are two significant
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1723 ;; time points in the invocation of a macro: Expansion and evaluation time.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1724 ;; For an advised macro instead of evaluating the original definition we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1725 ;; use `macroexpand', that is, changing argument values and binding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1726 ;; environments by pieces of advice has an affect during macro expansion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1727 ;; but not necessarily during evaluation. In particular, any side effects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1728 ;; of pieces of advice will occur during macro expansion. To also affect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1729 ;; the behavior during evaluation time one has to change the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1730 ;; `ad-return-value' in a piece of after advice. For example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1731 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1732 ;; (defmacro foom (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1733 ;; (` (list (, x))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1734 ;; foom
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1735 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1736 ;; (foom '(a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1737 ;; ((a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1738 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1739 ;; (defadvice foom (before fg-print-x act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1740 ;; "Print the value of X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1741 ;; (print x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1742 ;; foom
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1743 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1744 ;; The following works as expected because evaluation immediately follows
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1745 ;; macro expansion:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1746 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1747 ;; (foom '(a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1748 ;; (quote (a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1749 ;; ((a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1750 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1751 ;; However, the printing happens during expansion (or byte-compile) time:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1752 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1753 ;; (macroexpand '(foom '(a)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1754 ;; (quote (a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1755 ;; (list (quote (a)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1756 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1757 ;; If we want it to happen during evaluation time we have to do the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1758 ;; following (first remove the old advice):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1759 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1760 ;; (ad-remove-advice 'foom 'before 'fg-print-x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1761 ;; nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1762 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1763 ;; (defadvice foom (after fg-print-x act)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1764 ;; "Print the value of X."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1765 ;; (setq ad-return-value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1766 ;; (` (progn (print (, x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1767 ;; (, ad-return-value)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1768 ;; foom
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1769 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1770 ;; (macroexpand '(foom '(a)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1771 ;; (progn (print (quote (a))) (list (quote (a))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1772 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1773 ;; (foom '(a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1774 ;; (a)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1775 ;; ((a))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1776 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1777 ;; While this method might seem somewhat cumbersome, it is very general
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1778 ;; because it allows one to influence macro expansion as well as evaluation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1779 ;; In general, advising macros should be a rather rare activity anyway, in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1780 ;; particular, because compile-time macro expansion takes away a lot of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1781 ;; flexibility and effectiveness of the advice mechanism. Macros that were
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1782 ;; compile-time expanded before the advice was activated will of course never
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1783 ;; exhibit the advised behavior.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1784 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1785 ;; @@ Advising special forms:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1786 ;; ==========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1787 ;; Now for something that should be even more rare than advising macros:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1788 ;; Advising special forms. Because special forms are irregular in their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1789 ;; argument evaluation behavior (e.g., `setq' evaluates the second but not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1790 ;; the first argument) they have to be advised into macros. A dangerous
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1791 ;; consequence of this is that the byte-compiler will not recognize them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1792 ;; as special forms anymore (well, in most cases) and use their expansion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1793 ;; rather than the proper byte-code. Also, because the original definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1794 ;; of a special form cannot be `funcall'ed, `eval' has to be used instead
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1795 ;; which is less efficient.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1796 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1797 ;; MORAL: Do not advise special forms unless you are completely sure about
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1798 ;; what you are doing (some of the forward advice behavior is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1799 ;; implemented via advice of the special forms `defun' and `defmacro').
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1800 ;; As a safety measure one should always do `ad-deactivate-all' before
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1801 ;; one byte-compiles a file to avoid any interference of advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1802 ;; special forms.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1803 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1804 ;; Apart from the safety concerns advising special forms is not any different
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1805 ;; from advising plain functions or subrs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1806
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1807
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1808 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1809
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1810 ;; @ Advice implementation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1811 ;; ========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1812
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1813 ;; @@ Compilation idiosyncrasies:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1814 ;; ==============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1815
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1816 ;; `defadvice' expansion needs quite a few advice functions and variables,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1817 ;; hence, I need to preload the file before it can be compiled. To avoid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1818 ;; interference of bogus compiled files I always preload the source file:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1819 (provide 'advice-preload)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1820 ;; During a normal load this is a noop:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1821 (require 'advice-preload "advice.el")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1822
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1823
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1824 (defmacro ad-xemacs-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1825 ;;Expands into Non-nil constant if we run XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1826 ;;Unselected conditional code will be optimized away during compilation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1827 (string-match "XEmacs" emacs-version))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1828
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1829
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1830 ;; @@ Variable definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1831 ;; ========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1832
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1833 (defconst ad-version "2.14")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1834
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1835 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1836 (defvar ad-redefinition-action 'warn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1837 "*Defines what to do with redefinitions during Advice de/activation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1838 Redefinition occurs if a previously activated function that already has an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1839 original definition associated with it gets redefined and then de/activated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1840 In such a case we can either accept the current definition as the new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1841 original definition, discard the current definition and replace it with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1842 old original, or keep it and raise an error. The values `accept', `discard',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1843 `error' or `warn' govern what will be done. `warn' is just like `accept' but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1844 it additionally prints a warning message. All other values will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1845 interpreted as `error'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1846
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1847 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1848 (defvar ad-default-compilation-action 'maybe
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1849 "*Defines whether to compile advised definitions during activation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1850 A value of `always' will result in unconditional compilation, `never' will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1851 always avoid compilation, `maybe' will compile if the byte-compiler is already
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1852 loaded, and `like-original' will compile if the original definition of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1853 advised function is compiled or a built-in function. Every other value will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1854 be interpreted as `maybe'. This variable will only be considered if the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1855 COMPILE argument of `ad-activate' was supplied as nil.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1856
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1857
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1858 ;; @@ Some utilities:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1859 ;; ==================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1860
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1861 ;; We don't want the local arguments to interfere with anything
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1862 ;; referenced in the supplied functions => the cryptic casing:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1863 (defun ad-substitute-tree (sUbTrEe-TeSt fUnCtIoN tReE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1864 ;;"Substitutes qualifying subTREEs with result of FUNCTION(subTREE).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1865 ;;Only proper subtrees are considered, for example, if TREE is (1 (2 (3)) 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1866 ;;then the subtrees will be 1 (2 (3)) 2 (3) 3 4, dotted structures are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1867 ;;allowed too. Once a qualifying subtree has been found its subtrees will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1868 ;;not be considered anymore. (ad-substitute-tree 'atom 'identity tree)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1869 ;;generates a copy of TREE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1870 (cond ((consp tReE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1871 (cons (if (funcall sUbTrEe-TeSt (car tReE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1872 (funcall fUnCtIoN (car tReE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1873 (if (consp (car tReE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1874 (ad-substitute-tree sUbTrEe-TeSt fUnCtIoN (car tReE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1875 (car tReE)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1876 (ad-substitute-tree sUbTrEe-TeSt fUnCtIoN (cdr tReE))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1877 ((funcall sUbTrEe-TeSt tReE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1878 (funcall fUnCtIoN tReE))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1879 (t tReE)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1880
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1881 ;; this is just faster than `ad-substitute-tree':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1882 (defun ad-copy-tree (tree)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1883 ;;"Returns a copy of the list structure of TREE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1884 (cond ((consp tree)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1885 (cons (ad-copy-tree (car tree))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1886 (ad-copy-tree (cdr tree))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1887 (t tree)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1888
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1889 (defmacro ad-dolist (varform &rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1890 "A Common-Lisp-style dolist iterator with the following syntax:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1891
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1892 (ad-dolist (VAR INIT-FORM [RESULT-FORM])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1893 BODY-FORM...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1894
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1895 which will iterate over the list yielded by INIT-FORM binding VAR to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1896 current head at every iteration. If RESULT-FORM is supplied its value will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1897 be returned at the end of the iteration, nil otherwise. The iteration can be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1898 exited prematurely with `(ad-do-return [VALUE])'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1899 (let ((expansion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1900 (` (let ((ad-dO-vAr (, (car (cdr varform))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1901 (, (car varform)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1902 (while ad-dO-vAr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1903 (setq (, (car varform)) (car ad-dO-vAr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1904 (,@ body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1905 ;;work around a backquote bug:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1906 ;;(` ((,@ '(foo)) (bar))) => (append '(foo) '(((bar)))) wrong
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1907 ;;(` ((,@ '(foo)) (, '(bar)))) => (append '(foo) (list '(bar)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1908 (, '(setq ad-dO-vAr (cdr ad-dO-vAr))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1909 (, (car (cdr (cdr varform))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1910 ;;ok, this wastes some cons cells but only during compilation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1911 (if (catch 'contains-return
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1912 (ad-substitute-tree
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1913 (function (lambda (subtree)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1914 (cond ((eq (car-safe subtree) 'ad-dolist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1915 ((eq (car-safe subtree) 'ad-do-return)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1916 (throw 'contains-return t)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1917 'identity body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1918 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1919 (` (catch 'ad-dO-eXiT (, expansion)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1920 expansion)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1921
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1922 (defmacro ad-do-return (value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1923 (` (throw 'ad-dO-eXiT (, value))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1924
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1925 (if (not (get 'ad-dolist 'lisp-indent-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1926 (put 'ad-dolist 'lisp-indent-hook 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1927
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1928
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1929 ;; @@ Save real definitions of subrs used by Advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1930 ;; =================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1931 ;; Advice depends on the real, unmodified functionality of various subrs,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1932 ;; we save them here so advised versions will not interfere (eventually,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1933 ;; we will save all subrs used in code generated by Advice):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1934
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1935 (defmacro ad-save-real-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1936 (let ((saved-function (intern (format "ad-real-%s" function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1937 ;; Make sure the compiler is loaded during macro expansion:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1938 (require 'byte-compile "bytecomp")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1939 (` (if (not (fboundp '(, saved-function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1940 (progn (fset '(, saved-function) (symbol-function '(, function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1941 ;; Copy byte-compiler properties:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1942 (,@ (if (get function 'byte-compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1943 (` ((put '(, saved-function) 'byte-compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1944 '(, (get function 'byte-compile)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1945 (,@ (if (get function 'byte-opcode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1946 (` ((put '(, saved-function) 'byte-opcode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1947 '(, (get function 'byte-opcode))))))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1948
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1949 (defun ad-save-real-definitions ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1950 ;; Macro expansion will hardcode the values of the various byte-compiler
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1951 ;; properties into the compiled version of this function such that the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1952 ;; proper values will be available at runtime without loading the compiler:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1953 (ad-save-real-definition fset)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1954 (ad-save-real-definition documentation))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1955
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1956 (ad-save-real-definitions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1957
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1958
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1959 ;; @@ Advice info access fns:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1960 ;; ==========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1961
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1962 ;; Advice information for a particular function is stored on the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1963 ;; advice-info property of the function symbol. It is stored as an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1964 ;; alist of the following format:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1965 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1966 ;; ((active . t/nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1967 ;; (before adv1 adv2 ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1968 ;; (around adv1 adv2 ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1969 ;; (after adv1 adv2 ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1970 ;; (activation adv1 adv2 ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1971 ;; (deactivation adv1 adv2 ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1972 ;; (origname . <symbol fbound to origdef>)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1973 ;; (cache . (<advised-definition> . <id>)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1974
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1975 ;; List of currently advised though not necessarily activated functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1976 ;; (this list is maintained as a completion table):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1977 (defvar ad-advised-functions nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1978
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1979 (defmacro ad-pushnew-advised-function (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1980 ;;"Add FUNCTION to `ad-advised-functions' unless its already there."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1981 (` (if (not (assoc (symbol-name (, function)) ad-advised-functions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1982 (setq ad-advised-functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1983 (cons (list (symbol-name (, function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1984 ad-advised-functions)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1985
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1986 (defmacro ad-pop-advised-function (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1987 ;;"Remove FUNCTION from `ad-advised-functions'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1988 (` (setq ad-advised-functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1989 (delq (assoc (symbol-name (, function)) ad-advised-functions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1990 ad-advised-functions))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1991
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1992 (defmacro ad-do-advised-functions (varform &rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1993 ;;"`ad-dolist'-style iterator that maps over `ad-advised-functions'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1994 ;; (ad-do-advised-functions (VAR [RESULT-FORM])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1995 ;; BODY-FORM...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1996 ;;Also see `ad-dolist'. On each iteration VAR will be bound to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1997 ;;name of an advised function (a symbol)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1998 (` (ad-dolist ((, (car varform))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1999 ad-advised-functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2000 (, (car (cdr varform))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2001 (setq (, (car varform)) (intern (car (, (car varform)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2002 (,@ body))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2003
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2004 (if (not (get 'ad-do-advised-functions 'lisp-indent-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2005 (put 'ad-do-advised-functions 'lisp-indent-hook 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2006
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2007 (defmacro ad-get-advice-info (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2008 (` (get (, function) 'ad-advice-info)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2009
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2010 (defmacro ad-set-advice-info (function advice-info)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2011 (` (put (, function) 'ad-advice-info (, advice-info))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2012
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2013 (defmacro ad-copy-advice-info (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2014 (` (ad-copy-tree (get (, function) 'ad-advice-info))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2015
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2016 (defmacro ad-is-advised (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2017 ;;"Returns non-nil if FUNCTION has any advice info associated with it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2018 ;;This does not mean that the advice is also active."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2019 (list 'ad-get-advice-info function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2020
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2021 (defun ad-initialize-advice-info (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2022 ;;"Initializes the advice info for FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2023 ;;Assumes that FUNCTION has not yet been advised."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2024 (ad-pushnew-advised-function function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2025 (ad-set-advice-info function (list (cons 'active nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2026
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2027 (defmacro ad-get-advice-info-field (function field)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2028 ;;"Retrieves the value of the advice info FIELD of FUNCTION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2029 (` (cdr (assq (, field) (ad-get-advice-info (, function))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2030
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2031 (defun ad-set-advice-info-field (function field value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2032 ;;"Destructively modifies VALUE of the advice info FIELD of FUNCTION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2033 (and (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2034 (cond ((assq field (ad-get-advice-info function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2035 ;; A field with that name is already present:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2036 (rplacd (assq field (ad-get-advice-info function)) value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2037 (t;; otherwise, create a new field with that name:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2038 (nconc (ad-get-advice-info function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2039 (list (cons field value)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2040
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2041 ;; Don't make this a macro so we can use it as a predicate:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2042 (defun ad-is-active (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2043 ;;"non-nil if FUNCTION is advised and activated."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2044 (ad-get-advice-info-field function 'active))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2045
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2046
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2047 ;; @@ Access fns for single pieces of advice and related predicates:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2048 ;; =================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2049
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2050 (defun ad-make-advice (name protect enable definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2051 "Constructs single piece of advice to be stored in some advice-info.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2052 NAME should be a non-nil symbol, PROTECT and ENABLE should each be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2053 either t or nil, and DEFINITION should be a list of the form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2054 `(advice lambda ARGLIST [DOCSTRING] [INTERACTIVE-FORM] BODY...)'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2055 (list name protect enable definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2056
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2057 ;; ad-find-advice uses the alist structure directly ->
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2058 ;; change if this data structure changes!!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2059 (defmacro ad-advice-name (advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2060 (list 'car advice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2061 (defmacro ad-advice-protected (advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2062 (list 'nth 1 advice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2063 (defmacro ad-advice-enabled (advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2064 (list 'nth 2 advice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2065 (defmacro ad-advice-definition (advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2066 (list 'nth 3 advice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2067
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2068 (defun ad-advice-set-enabled (advice flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2069 (rplaca (cdr (cdr advice)) flag))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2070
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2071 (defun ad-class-p (thing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2072 (memq thing ad-advice-classes))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2073 (defun ad-name-p (thing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2074 (and thing (symbolp thing)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2075 (defun ad-position-p (thing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2076 (or (natnump thing)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2077 (memq thing '(first last))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2078
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2079
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2080 ;; @@ Advice access functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2081 ;; ===========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2082
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2083 ;; List of defined advice classes:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2084 (defvar ad-advice-classes '(before around after activation deactivation))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2085
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2086 (defun ad-has-enabled-advice (function class)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2087 ;;"True if at least one of FUNCTION's advices in CLASS is enabled."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2088 (ad-dolist (advice (ad-get-advice-info-field function class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2089 (if (ad-advice-enabled advice) (ad-do-return t))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2090
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2091 (defun ad-has-redefining-advice (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2092 ;;"True if FUNCTION's advice info defines at least 1 redefining advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2093 ;;Redefining advices affect the construction of an advised definition."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2094 (and (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2095 (or (ad-has-enabled-advice function 'before)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2096 (ad-has-enabled-advice function 'around)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2097 (ad-has-enabled-advice function 'after))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2098
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2099 (defun ad-has-any-advice (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2100 ;;"True if the advice info of FUNCTION defines at least one advice."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2101 (and (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2102 (ad-dolist (class ad-advice-classes nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2103 (if (ad-get-advice-info-field function class)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2104 (ad-do-return t)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2106 (defun ad-get-enabled-advices (function class)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2107 ;;"Returns the list of enabled advices of FUNCTION in CLASS."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2108 (let (enabled-advices)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2109 (ad-dolist (advice (ad-get-advice-info-field function class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2110 (if (ad-advice-enabled advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2111 (setq enabled-advices (cons advice enabled-advices))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2112 (reverse enabled-advices)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2115 ;; @@ Dealing with automatic advice activation via `fset/defalias':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2116 ;; ================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2118 ;; Since Emacs 19.26 the built-in versions of `fset' and `defalias'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2119 ;; take care of automatic advice activation, hence, we don't have to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2120 ;; hack it anymore by advising `fset/defun/defmacro/byte-code/etc'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2121
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2122 ;; The functionality of the new `fset' is as follows:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2123 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2124 ;; fset(sym,newdef)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2125 ;; assign NEWDEF to SYM
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2126 ;; if (get SYM 'ad-advice-info)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2127 ;; ad-activate(SYM, nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2128 ;; return (symbol-function SYM)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2129 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2130 ;; Whether advised definitions created by automatic activations will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2131 ;; compiled depends on the value of `ad-default-compilation-action'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2133 ;; Since calling `ad-activate' in the built-in definition of `fset' can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2134 ;; create major disasters we have to be a bit careful. One precaution is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2135 ;; to provide a dummy definition for `ad-activate' which can be used to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2136 ;; turn off automatic advice activation (e.g., when `ad-stop-advice' or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2137 ;; `ad-recover-normality' are called). Another is to avoid recursive calls
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2138 ;; to `ad-activate-on' by using `ad-with-auto-activation-disabled' where
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2139 ;; appropriate, especially in a safe version of `fset'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2140
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2141 ;; For now define `ad-activate' to the dummy definition:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2142 (defun ad-activate (function &optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2143 "Automatic advice activation is disabled. `ad-start-advice' enables it."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2144 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2146 ;; This is just a copy of the above:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2147 (defun ad-activate-off (function &optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2148 "Automatic advice activation is disabled. `ad-start-advice' enables it."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2149 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2151 ;; This will be t for top-level calls to `ad-activate-on':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2152 (defvar ad-activate-on-top-level t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2153
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2154 (defmacro ad-with-auto-activation-disabled (&rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2155 (` (let ((ad-activate-on-top-level nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2156 (,@ body))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2158 (defun ad-safe-fset (symbol definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2159 ;; A safe `fset' which will never call `ad-activate' recursively.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2160 (ad-with-auto-activation-disabled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2161 (ad-real-fset symbol definition)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2164 ;; @@ Access functions for original definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2165 ;; ============================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2166 ;; The advice-info of an advised function contains its `origname' which is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2167 ;; a symbol that is fbound to the original definition available at the first
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2168 ;; proper activation of the function after a legal re/definition. If the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2169 ;; original was defined via fcell indirection then `origname' will be defined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2170 ;; just so. Hence, to get hold of the actual original definition of a function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2171 ;; we need to use `ad-real-orig-definition'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2172
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2173 (defun ad-make-origname (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2174 ;;"Makes name to be used to call the original FUNCTION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2175 (intern (format "ad-Orig-%s" function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2176
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2177 (defmacro ad-get-orig-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2178 (` (let ((origname (ad-get-advice-info-field (, function) 'origname)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2179 (if (fboundp origname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2180 (symbol-function origname)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2182 (defmacro ad-set-orig-definition (function definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2183 (` (ad-safe-fset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2184 (ad-get-advice-info-field function 'origname) (, definition))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2185
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2186 (defmacro ad-clear-orig-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2187 (` (fmakunbound (ad-get-advice-info-field (, function) 'origname))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2190 ;; @@ Interactive input functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2191 ;; ===============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2192
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2193 (defun ad-read-advised-function (&optional prompt predicate default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2194 ;;"Reads name of advised function with completion from the minibuffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2195 ;;An optional PROMPT will be used to prompt for the function. PREDICATE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2196 ;;plays the same role as for `try-completion' (which see). DEFAULT will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2197 ;;be returned on empty input (defaults to the first advised function for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2198 ;;which PREDICATE returns non-nil)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2199 (if (null ad-advised-functions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2200 (error "ad-read-advised-function: There are no advised functions"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2201 (setq default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2202 (or default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2203 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2204 (if (or (null predicate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2205 (funcall predicate function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2206 (ad-do-return function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2207 (error "ad-read-advised-function: %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2208 "There are no qualifying advised functions")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2209 (let* ((ad-pReDiCaTe predicate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2210 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2211 (completing-read
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2212 (format "%s(default %s) " (or prompt "Function: ") default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2213 ad-advised-functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2214 (if predicate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2215 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2216 (lambda (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2217 ;; Oops, no closures - the joys of dynamic scoping:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2218 ;; `predicate' clashed with the `predicate' argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2219 ;; of XEmacs' `completing-read'.....
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2220 (funcall ad-pReDiCaTe (intern (car function))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2221 t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2222 (if (equal function "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2223 (if (ad-is-advised default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2224 default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2225 (error "ad-read-advised-function: `%s' is not advised" default))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2226 (intern function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2228 (defvar ad-advice-class-completion-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2229 (mapcar '(lambda (class) (list (symbol-name class)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2230 ad-advice-classes))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2232 (defun ad-read-advice-class (function &optional prompt default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2233 ;;"Reads a legal advice class with completion from the minibuffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2234 ;;An optional PROMPT will be used to prompt for the class. DEFAULT will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2235 ;;be returned on empty input (defaults to the first non-empty advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2236 ;;class of FUNCTION)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2237 (setq default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2238 (or default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2239 (ad-dolist (class ad-advice-classes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2240 (if (ad-get-advice-info-field function class)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2241 (ad-do-return class)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2242 (error "ad-read-advice-class: `%s' has no advices" function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2243 (let ((class (completing-read
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2244 (format "%s(default %s) " (or prompt "Class: ") default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2245 ad-advice-class-completion-table nil t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2246 (if (equal class "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2247 default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2248 (intern class))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2250 (defun ad-read-advice-name (function class &optional prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2251 ;;"Reads name of existing advice of CLASS for FUNCTION with completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2252 ;;An optional PROMPT is used to prompt for the name."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2253 (let* ((name-completion-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2254 (mapcar (function (lambda (advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2255 (list (symbol-name (ad-advice-name advice)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2256 (ad-get-advice-info-field function class)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2257 (default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2258 (if (null name-completion-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2259 (error "ad-read-advice-name: `%s' has no %s advice"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2260 function class)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2261 (car (car name-completion-table))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2262 (prompt (format "%s(default %s) " (or prompt "Name: ") default))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2263 (name (completing-read prompt name-completion-table nil t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2264 (if (equal name "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2265 (intern default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2266 (intern name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2267
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2268 (defun ad-read-advice-specification (&optional prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2269 ;;"Reads a complete function/class/name specification from minibuffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2270 ;;The list of read symbols will be returned. The optional PROMPT will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2271 ;;be used to prompt for the function."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2272 (let* ((function (ad-read-advised-function prompt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2273 (class (ad-read-advice-class function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2274 (name (ad-read-advice-name function class)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2275 (list function class name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2277 ;; Use previous regexp as a default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2278 (defvar ad-last-regexp "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2280 (defun ad-read-regexp (&optional prompt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2281 ;;"Reads a regular expression from the minibuffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2282 (let ((regexp (read-from-minibuffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2283 (concat (or prompt "Regular expression: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2284 (if (equal ad-last-regexp "") ""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2285 (format "(default \"%s\") " ad-last-regexp))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2286 (setq ad-last-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2287 (if (equal regexp "") ad-last-regexp regexp))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2290 ;; @@ Finding, enabling, adding and removing pieces of advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2291 ;; ===========================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2293 (defmacro ad-find-advice (function class name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2294 ;;"Finds the first advice of FUNCTION in CLASS with NAME."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2295 (` (assq (, name) (ad-get-advice-info-field (, function) (, class)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2296
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2297 (defun ad-advice-position (function class name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2298 ;;"Returns position of first advice of FUNCTION in CLASS with NAME."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2299 (let* ((found-advice (ad-find-advice function class name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2300 (advices (ad-get-advice-info-field function class)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2301 (if found-advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2302 (- (length advices) (length (memq found-advice advices))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2303
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2304 (defun ad-find-some-advice (function class name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2305 "Finds the first of FUNCTION's advices in CLASS matching NAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2306 NAME can be a symbol or a regular expression matching part of an advice name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2307 If CLASS is `any' all legal advice classes will be checked."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2308 (if (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2309 (let (found-advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2310 (ad-dolist (advice-class ad-advice-classes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2311 (if (or (eq class 'any) (eq advice-class class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2312 (setq found-advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2313 (ad-dolist (advice (ad-get-advice-info-field
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2314 function advice-class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2315 (if (or (and (stringp name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2316 (string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2317 name (symbol-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2318 (ad-advice-name advice))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2319 (eq name (ad-advice-name advice)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2320 (ad-do-return advice)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2321 (if found-advice (ad-do-return found-advice))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2323 (defun ad-enable-advice-internal (function class name flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2324 ;;"Sets enable FLAG of FUNCTION's advices in CLASS matching NAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2325 ;;If NAME is a string rather than a symbol then it's interpreted as a regular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2326 ;;expression and all advices whose name contain a match for it will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2327 ;;affected. If CLASS is `any' advices in all legal advice classes will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2328 ;;considered. The number of changed advices will be returned (or nil if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2329 ;;FUNCTION was not advised)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2330 (if (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2331 (let ((matched-advices 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2332 (ad-dolist (advice-class ad-advice-classes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2333 (if (or (eq class 'any) (eq advice-class class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2334 (ad-dolist (advice (ad-get-advice-info-field
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2335 function advice-class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2336 (cond ((or (and (stringp name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2337 (string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2338 name (symbol-name (ad-advice-name advice))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2339 (eq name (ad-advice-name advice)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2340 (setq matched-advices (1+ matched-advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2341 (ad-advice-set-enabled advice flag))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2342 matched-advices)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2343
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2344 (defun ad-enable-advice (function class name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2345 "Enables the advice of FUNCTION with CLASS and NAME."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2346 (interactive (ad-read-advice-specification "Enable advice of: "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2347 (if (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2348 (if (eq (ad-enable-advice-internal function class name t) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2349 (error "ad-enable-advice: `%s' has no %s advice matching `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2350 function class name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2351 (error "ad-enable-advice: `%s' is not advised" function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2353 (defun ad-disable-advice (function class name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2354 "Disables the advice of FUNCTION with CLASS and NAME."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2355 (interactive (ad-read-advice-specification "Disable advice of: "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2356 (if (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2357 (if (eq (ad-enable-advice-internal function class name nil) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2358 (error "ad-disable-advice: `%s' has no %s advice matching `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2359 function class name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2360 (error "ad-disable-advice: `%s' is not advised" function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2362 (defun ad-enable-regexp-internal (regexp class flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2363 ;;"Sets enable FLAGs of all CLASS advices whose name contains a REGEXP match.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2364 ;;If CLASS is `any' all legal advice classes are considered. The number of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2365 ;;affected advices will be returned."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2366 (let ((matched-advices 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2367 (ad-do-advised-functions (advised-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2368 (setq matched-advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2369 (+ matched-advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2370 (or (ad-enable-advice-internal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2371 advised-function class regexp flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2372 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2373 matched-advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2374
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2375 (defun ad-enable-regexp (regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2376 "Enables all advices with names that contain a match for REGEXP.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2377 All currently advised functions will be considered."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2378 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2379 (list (ad-read-regexp "Enable advices via regexp: ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2380 (let ((matched-advices (ad-enable-regexp-internal regexp 'any t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2381 (if (interactive-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2382 (message "%d matching advices enabled" matched-advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2383 matched-advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2384
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2385 (defun ad-disable-regexp (regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2386 "Disables all advices with names that contain a match for REGEXP.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2387 All currently advised functions will be considered."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2388 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2389 (list (ad-read-regexp "Disable advices via regexp: ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2390 (let ((matched-advices (ad-enable-regexp-internal regexp 'any nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2391 (if (interactive-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2392 (message "%d matching advices disabled" matched-advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2393 matched-advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2395 (defun ad-remove-advice (function class name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2396 "Removes FUNCTION's advice with NAME from its advices in CLASS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2397 If such an advice was found it will be removed from the list of advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2398 in that CLASS."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2399 (interactive (ad-read-advice-specification "Remove advice of: "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2400 (if (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2401 (let* ((advice-to-remove (ad-find-advice function class name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2402 (if advice-to-remove
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2403 (ad-set-advice-info-field
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2404 function class
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2405 (delq advice-to-remove (ad-get-advice-info-field function class)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2406 (error "ad-remove-advice: `%s' has no %s advice `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2407 function class name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2408 (error "ad-remove-advice: `%s' is not advised" function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2409
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2410 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2411 (defun ad-add-advice (function advice class position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2412 "Adds a piece of ADVICE to FUNCTION's list of advices in CLASS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2413 If FUNCTION already has one or more pieces of advice of the specified
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2414 CLASS then POSITION determines where the new piece will go. The value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2415 of POSITION can either be `first', `last' or a number where 0 corresponds
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2416 to `first'. Numbers outside the range will be mapped to the closest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2417 extreme position. If there was already a piece of ADVICE with the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2418 name, then the position argument will be ignored and the old advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2419 will be overwritten with the new one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2420 If the FUNCTION was not advised already, then its advice info will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2421 initialized. Redefining a piece of advice whose name is part of the cache-id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2422 will clear the cache."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2423 (cond ((not (ad-is-advised function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2424 (ad-initialize-advice-info function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2425 (ad-set-advice-info-field
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2426 function 'origname (ad-make-origname function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2427 (let* ((previous-position
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2428 (ad-advice-position function class (ad-advice-name advice)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2429 (advices (ad-get-advice-info-field function class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2430 ;; Determine a numerical position for the new advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2431 (position (cond (previous-position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2432 ((eq position 'first) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2433 ((eq position 'last) (length advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2434 ((numberp position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2435 (max 0 (min position (length advices))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2436 (t 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2437 ;; Check whether we have to clear the cache:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2438 (if (memq (ad-advice-name advice) (ad-get-cache-class-id function class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2439 (ad-clear-cache function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2440 (if previous-position
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2441 (setcar (nthcdr position advices) advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2442 (if (= position 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2443 (ad-set-advice-info-field function class (cons advice advices))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2444 (setcdr (nthcdr (1- position) advices)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2445 (cons advice (nthcdr position advices)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2446
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2447
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2448 ;; @@ Accessing and manipulating function definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2449 ;; ===================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2450
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2451 (defmacro ad-macrofy (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2452 ;;"Takes a lambda function DEFINITION and makes a macro out of it."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2453 (` (cons 'macro (, definition))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2454
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2455 (defmacro ad-lambdafy (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2456 ;;"Takes a macro function DEFINITION and makes a lambda out of it."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2457 (` (cdr (, definition))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2458
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2459 ;; There is no way to determine whether some subr is a special form or not,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2460 ;; hence we need this list (which is probably out of date):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2461 (defvar ad-special-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2462 (mapcar 'symbol-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2463 '(and catch cond condition-case defconst defmacro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2464 defun defvar function if interactive let let*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2465 or prog1 prog2 progn quote save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2466 save-restriction save-window-excursion setq
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2467 setq-default unwind-protect while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2468 with-output-to-temp-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2469
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2470 (defmacro ad-special-form-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2471 ;;"non-nil if DEFINITION is a special form."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2472 (list 'memq definition 'ad-special-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2473
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2474 (defmacro ad-interactive-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2475 ;;"non-nil if DEFINITION can be called interactively."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2476 (list 'commandp definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2477
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2478 (defmacro ad-subr-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2479 ;;"non-nil if DEFINITION is a subr."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2480 (list 'subrp definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2481
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2482 (defmacro ad-macro-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2483 ;;"non-nil if DEFINITION is a macro."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2484 (` (eq (car-safe (, definition)) 'macro)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2485
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2486 (defmacro ad-lambda-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2487 ;;"non-nil if DEFINITION is a lambda expression."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2488 (` (eq (car-safe (, definition)) 'lambda)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2489
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2490 ;; see ad-make-advice for the format of advice definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2491 (defmacro ad-advice-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2492 ;;"non-nil if DEFINITION is a piece of advice."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2493 (` (eq (car-safe (, definition)) 'advice)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2494
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2495 ;; Emacs/XEmacs cross-compatibility
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2496 ;; (compiled-function-p is an obsolete function in Emacs):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2497 (if (and (not (fboundp 'byte-code-function-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2498 (fboundp 'compiled-function-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2499 (ad-safe-fset 'byte-code-function-p 'compiled-function-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2500
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2501 (defmacro ad-compiled-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2502 ;;"non-nil if DEFINITION is a compiled byte-code object."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2503 (` (or (byte-code-function-p (, definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2504 (and (ad-macro-p (, definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2505 (byte-code-function-p (ad-lambdafy (, definition)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2507 (defmacro ad-compiled-code (compiled-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2508 ;;"Returns the byte-code object of a COMPILED-DEFINITION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2509 (` (if (ad-macro-p (, compiled-definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2510 (ad-lambdafy (, compiled-definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2511 (, compiled-definition))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2512
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2513 (defun ad-lambda-expression (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2514 ;;"Returns the lambda expression of a function/macro/advice DEFINITION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2515 (cond ((ad-lambda-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2516 definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2517 ((ad-macro-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2518 (ad-lambdafy definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2519 ((ad-advice-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2520 (cdr definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2521 (t nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2522
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2523 (defun ad-arglist (definition &optional name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2524 ;;"Returns the argument list of DEFINITION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2525 ;;If DEFINITION could be from a subr then its NAME should be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2526 ;;supplied to make subr arglist lookup more efficient."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2527 (cond ((ad-compiled-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2528 ;; XEmacs fix:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2529 (if (fboundp 'compiled-function-arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2530 (compiled-function-arglist (ad-compiled-code definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2531 (aref (ad-compiled-code definition) 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2532 ((consp definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2533 (car (cdr (ad-lambda-expression definition))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2534 ((ad-subr-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2535 (if name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2536 (ad-subr-arglist name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2537 ;; otherwise get it from its printed representation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2538 (setq name (format "%s" definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2539 (string-match "^#<subr \\([^>]+\\)>$" name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2540 (ad-subr-arglist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2541 (intern (substring name (match-beginning 1) (match-end 1))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2542
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2543 ;; Store subr-args as `((arg1 arg2 ...))' so I can distinguish
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2544 ;; a defined empty arglist `(nil)' from an undefined arglist:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2545 (defmacro ad-define-subr-args (subr arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2546 (` (put (, subr) 'ad-subr-arglist (list (, arglist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2547 (defmacro ad-undefine-subr-args (subr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2548 (` (put (, subr) 'ad-subr-arglist nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2549 (defmacro ad-subr-args-defined-p (subr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2550 (` (get (, subr) 'ad-subr-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2551 (defmacro ad-get-subr-args (subr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2552 (` (car (get (, subr) 'ad-subr-arglist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2553
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2554 (defun ad-subr-arglist (subr-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2555 ;;"Retrieve arglist of the subr with SUBR-NAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2556 ;;Either use the one stored under the `ad-subr-arglist' property,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2557 ;;or try to retrieve it from the docstring and cache it under
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2558 ;;that property, or otherwise use `(&rest ad-subr-args)'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2559 (cond ((ad-subr-args-defined-p subr-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2560 (ad-get-subr-args subr-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2561 ;; says jwz: Should use this for Lemacs 19.8 and above:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2562 ;;((fboundp 'subr-min-args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2563 ;; ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2564 ;; says hans: I guess what Jamie means is that I should use the values
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2565 ;; of `subr-min-args' and `subr-max-args' to construct the subr arglist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2566 ;; without having to look it up via parsing the docstring, e.g.,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2567 ;; values 1 and 2 would suggest `(arg1 &optional arg2)' as an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2568 ;; argument list. However, that won't work because there is no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2569 ;; way to distinguish a subr with args `(a &optional b &rest c)' from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2570 ;; one with args `(a &rest c)' using that mechanism. Also, the argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2571 ;; names from the docstring are more meaningful. Hence, I'll stick with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2572 ;; the old way of doing things.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2573 (t (let ((doc (or (ad-real-documentation subr-name t) "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2574 (cond ((string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2575 "[\n\t ]*\narguments: ?\\((.*)\\)\n?\\'" doc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2576 ;; this is the format used in XEmacs and in FSFmacs pre-19.24:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2577 ;; XEmacs 19.12+ uppercases the args like FSF...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2578 (ad-define-subr-args
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2579 subr-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2580 (car (read-from-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2581 (downcase (substring doc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2582 (match-beginning 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2583 (match-end 1))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2584 (ad-get-subr-args subr-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2585 ((string-match "^\\(([^\)]+)\\)\n?\\'" doc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2586 (ad-define-subr-args
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2587 subr-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2588 (cdr (car (read-from-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2589 (downcase
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2590 (substring doc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2591 (match-beginning 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2592 (match-end 1)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2593 (ad-get-subr-args subr-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2594 (t '(&rest ad-subr-args)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2595
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2596 (defun ad-docstring (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2597 ;;"Returns the unexpanded docstring of DEFINITION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2598 (let ((docstring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2599 (if (ad-compiled-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2600 (ad-real-documentation definition t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2601 (car (cdr (cdr (ad-lambda-expression definition)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2602 (if (or (stringp docstring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2603 (natnump docstring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2604 docstring)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2605
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2606 (defun ad-interactive-form (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2607 ;;"Returns the interactive form of DEFINITION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2608 (cond ((ad-compiled-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2609 (and (commandp definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2610 (list 'interactive (aref (ad-compiled-code definition) 5))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2611 ((or (ad-advice-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2612 (ad-lambda-p definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2613 (commandp (ad-lambda-expression definition)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2614
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2615 (defun ad-body-forms (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2616 ;;"Returns the list of body forms of DEFINITION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2617 (cond ((ad-compiled-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2618 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2619 ((consp definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2620 (nthcdr (+ (if (ad-docstring definition) 1 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2621 (if (ad-interactive-form definition) 1 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2622 (cdr (cdr (ad-lambda-expression definition)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2623
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2624 ;; Matches the docstring of an advised definition.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2625 ;; The first group of the regexp matches the function name:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2626 (defvar ad-advised-definition-docstring-regexp "^\\$ad-doc: \\(.+\\)\\$$")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2627
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2628 (defun ad-make-advised-definition-docstring (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2629 ;; Makes an identifying docstring for the advised definition of FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2630 ;; Put function name into the documentation string so we can infer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2631 ;; the name of the advised function from the docstring. This is needed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2632 ;; to generate a proper advised docstring even if we are just given a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2633 ;; definition (also see the defadvice for `documentation'):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2634 (format "$ad-doc: %s$" (prin1-to-string function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2635
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2636 (defun ad-advised-definition-p (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2637 ;;"non-nil if DEFINITION was generated from advice information."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2638 (if (or (ad-lambda-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2639 (ad-macro-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2640 (ad-compiled-p definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2641 (let ((docstring (ad-docstring definition)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2642 (and (stringp docstring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2643 (string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2644 ad-advised-definition-docstring-regexp docstring)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2645
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2646 (defun ad-definition-type (definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2647 ;;"Returns symbol that describes the type of DEFINITION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2648 (if (ad-macro-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2649 'macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2650 (if (ad-subr-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2651 (if (ad-special-form-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2652 'special-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2653 'subr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2654 (if (or (ad-lambda-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2655 (ad-compiled-p definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2656 'function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2657 (if (ad-advice-p definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2658 'advice)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2659
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2660 (defun ad-has-proper-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2661 ;;"True if FUNCTION is a symbol with a proper definition.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2662 ;;For that it has to be fbound with a non-autoload definition."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2663 (and (symbolp function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2664 (fboundp function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2665 (not (eq (car-safe (symbol-function function)) 'autoload))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2666
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2667 ;; The following two are necessary for the sake of packages such as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2668 ;; ange-ftp which redefine functions via fcell indirection:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2669 (defun ad-real-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2670 ;;"Finds FUNCTION's definition at the end of function cell indirection."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2671 (if (ad-has-proper-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2672 (let ((definition (symbol-function function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2673 (if (symbolp definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2674 (ad-real-definition definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2675 definition))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2676
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2677 (defun ad-real-orig-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2678 ;;"Finds FUNCTION's real original definition starting from its `origname'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2679 (if (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2680 (ad-real-definition (ad-get-advice-info-field function 'origname))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2681
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2682 (defun ad-is-compilable (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2683 ;;"True if FUNCTION has an interpreted definition that can be compiled."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2684 (and (ad-has-proper-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2685 (or (ad-lambda-p (symbol-function function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2686 (ad-macro-p (symbol-function function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2687 (not (ad-compiled-p (symbol-function function)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2688
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2689 (defun ad-compile-function (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2690 "Byte-compiles FUNCTION (or macro) if it is not yet compiled."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2691 (interactive "aByte-compile function: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2692 (if (ad-is-compilable function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2693 ;; Need to turn off auto-activation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2694 ;; because `byte-compile' uses `fset':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2695 (ad-with-auto-activation-disabled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2696 (byte-compile function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2697
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2698
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2699 ;; @@ Constructing advised definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2700 ;; ====================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2701 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2702 ;; Main design decisions about the form of advised definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2703 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2704 ;; A) How will original definitions be called?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2705 ;; B) What will argument lists of advised functions look like?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2706 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2707 ;; Ad A)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2708 ;; I chose to use function indirection for all four types of original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2709 ;; definitions (functions, macros, subrs and special forms), i.e., create
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2710 ;; a unique symbol `ad-Orig-<name>' which is fbound to the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2711 ;; definition and call it according to type and arguments. Functions and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2712 ;; subrs that don't have any &rest arguments can be called directly in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2713 ;; `(ad-Orig-<name> ....)' form. If they have a &rest argument we have to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2714 ;; use `apply'. Macros will be called with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2715 ;; `(macroexpand '(ad-Orig-<name> ....))', and special forms also need a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2716 ;; form like that with `eval' instead of `macroexpand'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2717 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2718 ;; Ad B)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2719 ;; Use original arguments where possible and `(&rest ad-subr-args)'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2720 ;; otherwise, even though this seems to be more complicated and less
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2721 ;; uniform than a general `(&rest args)' approach. My reason to still
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2722 ;; do it that way is that in most cases my approach leads to the more
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2723 ;; efficient form for the advised function, and portability (e.g., to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2724 ;; make the same advice work regardless of whether something is a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2725 ;; function or a subr) can still be achieved with argument access macros.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2726
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2727
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2728 (defun ad-prognify (forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2729 (cond ((<= (length forms) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2730 (car forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2731 (t (cons 'progn forms))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2732
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2733 ;; @@@ Accessing argument lists:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2734 ;; =============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2735
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2736 (defun ad-parse-arglist (arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2737 ;;"Parses ARGLIST into its required, optional and rest parameters.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2738 ;;A three-element list is returned, where the 1st element is the list of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2739 ;;required arguments, the 2nd is the list of optional arguments, and the 3rd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2740 ;;is the name of an optional rest parameter (or nil)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2741 (let* (required optional rest)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2742 (setq rest (car (cdr (memq '&rest arglist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2743 (if rest (setq arglist (reverse (cdr (memq '&rest (reverse arglist))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2744 (setq optional (cdr (memq '&optional arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2745 (if optional
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2746 (setq required (reverse (cdr (memq '&optional (reverse arglist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2747 (setq required arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2748 (list required optional rest)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2749
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2750 (defun ad-retrieve-args-form (arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2751 ;;"Generates a form which evaluates into names/values/types of ARGLIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2752 ;;When the form gets evaluated within a function with that argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2753 ;;it will result in a list with one entry for each argument, where the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2754 ;;first element of each entry is the name of the argument, the second
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2755 ;;element is its actual current value, and the third element is either
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2756 ;;`required', `optional' or `rest' depending on the type of the argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2757 (let* ((parsed-arglist (ad-parse-arglist arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2758 (rest (nth 2 parsed-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2759 (` (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2760 (,@ (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2761 (lambda (req)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2762 (` (list '(, req) (, req) 'required))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2763 (nth 0 parsed-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2764 (,@ (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2765 (lambda (opt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2766 (` (list '(, opt) (, opt) 'optional))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2767 (nth 1 parsed-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2768 (,@ (if rest (list (` (list '(, rest) (, rest) 'rest)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2769 ))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2770
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2771 (defun ad-arg-binding-field (binding field)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2772 (cond ((eq field 'name) (car binding))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2773 ((eq field 'value) (car (cdr binding)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2774 ((eq field 'type) (car (cdr (cdr binding))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2775
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2776 (defun ad-list-access (position list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2777 (cond ((= position 0) list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2778 ((= position 1) (list 'cdr list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2779 (t (list 'nthcdr position list))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2780
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2781 (defun ad-element-access (position list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2782 (cond ((= position 0) (list 'car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2783 ((= position 1) (` (car (cdr (, list)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2784 (t (list 'nth position list))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2785
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2786 (defun ad-access-argument (arglist index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2787 ;;"Tells how to access ARGLIST's actual argument at position INDEX.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2788 ;;For a required/optional arg it simply returns it, if a rest argument has
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2789 ;;to be accessed, it returns a list with the index and name."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2790 (let* ((parsed-arglist (ad-parse-arglist arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2791 (reqopt-args (append (nth 0 parsed-arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2792 (nth 1 parsed-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2793 (rest-arg (nth 2 parsed-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2794 (cond ((< index (length reqopt-args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2795 (nth index reqopt-args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2796 (rest-arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2797 (list (- index (length reqopt-args)) rest-arg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2798
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2799 (defun ad-get-argument (arglist index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2800 ;;"Returns form to access ARGLIST's actual argument at position INDEX."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2801 (let ((argument-access (ad-access-argument arglist index)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2802 (cond ((consp argument-access)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2803 (ad-element-access
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2804 (car argument-access) (car (cdr argument-access))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2805 (argument-access))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2806
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2807 (defun ad-set-argument (arglist index value-form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2808 ;;"Returns form to set ARGLIST's actual arg at INDEX to VALUE-FORM."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2809 (let ((argument-access (ad-access-argument arglist index)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2810 (cond ((consp argument-access)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2811 ;; should this check whether there actually is something to set?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2812 (` (setcar (, (ad-list-access
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2813 (car argument-access) (car (cdr argument-access))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2814 (, value-form))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2815 (argument-access
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2816 (` (setq (, argument-access) (, value-form))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2817 (t (error "ad-set-argument: No argument at position %d of `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2818 index arglist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2819
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2820 (defun ad-get-arguments (arglist index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2821 ;;"Returns form to access all actual arguments starting at position INDEX."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2822 (let* ((parsed-arglist (ad-parse-arglist arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2823 (reqopt-args (append (nth 0 parsed-arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2824 (nth 1 parsed-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2825 (rest-arg (nth 2 parsed-arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2826 args-form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2827 (if (< index (length reqopt-args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2828 (setq args-form (` (list (,@ (nthcdr index reqopt-args))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2829 (if rest-arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2830 (if args-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2831 (setq args-form (` (nconc (, args-form) (, rest-arg))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2832 (setq args-form (ad-list-access (- index (length reqopt-args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2833 rest-arg))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2834 args-form))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2835
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2836 (defun ad-set-arguments (arglist index values-form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2837 ;;"Makes form to assign elements of VALUES-FORM as actual ARGLIST args.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2838 ;;The assignment starts at position INDEX."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2839 (let ((values-index 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2840 argument-access set-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2841 (while (setq argument-access (ad-access-argument arglist index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2842 (if (symbolp argument-access)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2843 (setq set-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2844 (cons (ad-set-argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2845 arglist index
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2846 (ad-element-access values-index 'ad-vAlUeS))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2847 set-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2848 (setq set-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2849 (cons (if (= (car argument-access) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2850 (list 'setq
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2851 (car (cdr argument-access))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2852 (ad-list-access values-index 'ad-vAlUeS))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2853 (list 'setcdr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2854 (ad-list-access (1- (car argument-access))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2855 (car (cdr argument-access)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2856 (ad-list-access values-index 'ad-vAlUeS)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2857 set-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2858 ;; terminate loop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2859 (setq arglist nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2860 (setq index (1+ index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2861 (setq values-index (1+ values-index)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2862 (if (null set-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2863 (error "ad-set-arguments: No argument at position %d of `%s'"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2864 index arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2865 (if (= (length set-forms) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2866 ;; For exactly one set-form we can use values-form directly,...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2867 (ad-substitute-tree
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2868 (function (lambda (form) (eq form 'ad-vAlUeS)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2869 (function (lambda (form) values-form))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2870 (car set-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2871 ;; ...if we have more we have to bind it to a variable:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2872 (` (let ((ad-vAlUeS (, values-form)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2873 (,@ (reverse set-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2874 ;; work around the old backquote bug:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2875 (, 'ad-vAlUeS)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2876
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2877 (defun ad-insert-argument-access-forms (definition arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2878 ;;"Expands arg-access text macros in DEFINITION according to ARGLIST."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2879 (ad-substitute-tree
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2880 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2881 (lambda (form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2882 (or (eq form 'ad-arg-bindings)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2883 (and (memq (car-safe form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2884 '(ad-get-arg ad-get-args ad-set-arg ad-set-args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2885 (integerp (car-safe (cdr form)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2886 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2887 (lambda (form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2888 (if (eq form 'ad-arg-bindings)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2889 (ad-retrieve-args-form arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2890 (let ((accessor (car form))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2891 (index (car (cdr form)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2892 (val (car (cdr (ad-insert-argument-access-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2893 (cdr form) arglist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2894 (cond ((eq accessor 'ad-get-arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2895 (ad-get-argument arglist index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2896 ((eq accessor 'ad-set-arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2897 (ad-set-argument arglist index val))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2898 ((eq accessor 'ad-get-args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2899 (ad-get-arguments arglist index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2900 ((eq accessor 'ad-set-args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2901 (ad-set-arguments arglist index val)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2902 definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2903
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2904 ;; @@@ Mapping argument lists:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2905 ;; ===========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2906 ;; Here is the problem:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2907 ;; Suppose function foo was called with (foo 1 2 3 4 5), and foo has the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2908 ;; argument list (x y &rest z), and we want to call the function bar which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2909 ;; has argument list (a &rest b) with a combination of x, y and z so that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2910 ;; the effect is just as if we had called (bar 1 2 3 4 5) directly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2911 ;; The mapping should work for any two argument lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2912
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2913 (defun ad-map-arglists (source-arglist target-arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2914 "Makes `funcall/apply' form to map SOURCE-ARGLIST to TARGET-ARGLIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2915 The arguments supplied to TARGET-ARGLIST will be taken from SOURCE-ARGLIST just
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2916 as if they had been supplied to a function with TARGET-ARGLIST directly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2917 Excess source arguments will be neglected, missing source arguments will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2918 supplied as nil. Returns a `funcall' or `apply' form with the second element
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2919 being `function' which has to be replaced by an actual function argument.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2920 Example: `(ad-map-arglists '(a &rest args) '(w x y z))' will return
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2921 `(funcall function a (car args) (car (cdr args)) (nth 2 args))'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2922 (let* ((parsed-source-arglist (ad-parse-arglist source-arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2923 (source-reqopt-args (append (nth 0 parsed-source-arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2924 (nth 1 parsed-source-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2925 (source-rest-arg (nth 2 parsed-source-arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2926 (parsed-target-arglist (ad-parse-arglist target-arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2927 (target-reqopt-args (append (nth 0 parsed-target-arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2928 (nth 1 parsed-target-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2929 (target-rest-arg (nth 2 parsed-target-arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2930 (need-apply (and source-rest-arg target-rest-arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2931 (target-arg-index -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2932 ;; This produces ``error-proof'' target function calls with the exception
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2933 ;; of a case like (&rest a) mapped onto (x &rest y) where the actual args
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2934 ;; supplied to A might not be enough to supply the required target arg X
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2935 (append (list (if need-apply 'apply 'funcall) 'function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2936 (cond (need-apply
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2937 ;; `apply' can take care of that directly:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2938 (append source-reqopt-args (list source-rest-arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2939 (t (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2940 (lambda (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2941 (setq target-arg-index (1+ target-arg-index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2942 (ad-get-argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2943 source-arglist target-arg-index)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2944 (append target-reqopt-args
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2945 (and target-rest-arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2946 ;; If we have a rest arg gobble up
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2947 ;; remaining source args:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2948 (nthcdr (length target-reqopt-args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2949 source-reqopt-args)))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2950
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2951 (defun ad-make-mapped-call (source-arglist target-arglist target-function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2952 ;;"Makes form to call TARGET-FUNCTION with args from SOURCE-ARGLIST."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2953 (let* ((mapped-form (ad-map-arglists source-arglist target-arglist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2954 (if (eq (car mapped-form) 'funcall)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2955 (cons target-function (cdr (cdr mapped-form)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2956 (prog1 mapped-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2957 (setcar (cdr mapped-form) (list 'quote target-function))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2958
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2959 ;; @@@ Making an advised documentation string:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2960 ;; ===========================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2961 ;; New policy: The documentation string for an advised function will be built
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2962 ;; at the time the advised `documentation' function is called. This has the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2963 ;; following advantages:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2964 ;; 1) command-key substitutions will automatically be correct
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2965 ;; 2) No wasted string space due to big advised docstrings in caches or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2966 ;; compiled files that contain preactivations
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2967 ;; The overall overhead for this should be negligible because people normally
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2968 ;; don't lookup documentation for the same function over and over again.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2969
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2970 (defun ad-make-single-advice-docstring (advice class &optional style)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2971 (let ((advice-docstring (ad-docstring (ad-advice-definition advice))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2972 (cond ((eq style 'plain)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2973 advice-docstring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2974 ((eq style 'freeze)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2975 (format "Permanent %s-advice `%s':%s%s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2976 class (ad-advice-name advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2977 (if advice-docstring "\n" "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2978 (or advice-docstring "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2979 (t (format "%s-advice `%s':%s%s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2980 (capitalize (symbol-name class)) (ad-advice-name advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2981 (if advice-docstring "\n" "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2982 (or advice-docstring ""))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2983
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2984 (defun ad-make-advised-docstring (function &optional style)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2985 ;;"Constructs a documentation string for the advised FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2986 ;;It concatenates the original documentation with the documentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2987 ;;strings of the individual pieces of advice which will be formatted
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2988 ;;according to STYLE. STYLE can be `plain' or `freeze', everything else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2989 ;;will be interpreted as `default'. The order of the advice documentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2990 ;;strings corresponds to before/around/after and the individual ordering
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2991 ;;in any of these classes."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2992 (let* ((origdef (ad-real-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2993 (origtype (symbol-name (ad-definition-type origdef)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2994 (origdoc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2995 ;; Retrieve raw doc, key substitution will be taken care of later:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2996 (ad-real-documentation origdef t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2997 paragraphs advice-docstring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2998 (if origdoc (setq paragraphs (list origdoc)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2999 (if (not (eq style 'plain))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3000 (setq paragraphs (cons (concat "This " origtype " is advised.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3001 paragraphs)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3002 (ad-dolist (class ad-advice-classes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3003 (ad-dolist (advice (ad-get-enabled-advices function class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3004 (setq advice-docstring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3005 (ad-make-single-advice-docstring advice class style))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3006 (if advice-docstring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3007 (setq paragraphs (cons advice-docstring paragraphs)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3008 (if paragraphs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3009 ;; separate paragraphs with blank lines:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3010 (mapconcat 'identity (nreverse paragraphs) "\n\n"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3011
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3012 (defun ad-make-plain-docstring (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3013 (ad-make-advised-docstring function 'plain))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3014 (defun ad-make-freeze-docstring (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3015 (ad-make-advised-docstring function 'freeze))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3016
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3017 ;; @@@ Accessing overriding arglists and interactive forms:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3018 ;; ========================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3019
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3020 (defun ad-advised-arglist (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3021 ;;"Finds first defined arglist in FUNCTION's redefining advices."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3022 (ad-dolist (advice (append (ad-get-enabled-advices function 'before)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3023 (ad-get-enabled-advices function 'around)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3024 (ad-get-enabled-advices function 'after)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3025 (let ((arglist (ad-arglist (ad-advice-definition advice))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3026 (if arglist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3027 ;; We found the first one, use it:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3028 (ad-do-return arglist)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3029
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3030 (defun ad-advised-interactive-form (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3031 ;;"Finds first interactive form in FUNCTION's redefining advices."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3032 (ad-dolist (advice (append (ad-get-enabled-advices function 'before)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3033 (ad-get-enabled-advices function 'around)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3034 (ad-get-enabled-advices function 'after)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3035 (let ((interactive-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3036 (ad-interactive-form (ad-advice-definition advice))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3037 (if interactive-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3038 ;; We found the first one, use it:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3039 (ad-do-return interactive-form)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3040
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3041 ;; @@@ Putting it all together:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3042 ;; ============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3043
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3044 (defun ad-make-advised-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3045 ;;"Generates an advised definition of FUNCTION from its advice info."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3046 (if (and (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3047 (ad-has-redefining-advice function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3048 (let* ((origdef (ad-real-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3049 (origname (ad-get-advice-info-field function 'origname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3050 (orig-interactive-p (ad-interactive-p origdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3051 (orig-subr-p (ad-subr-p origdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3052 (orig-special-form-p (ad-special-form-p origdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3053 (orig-macro-p (ad-macro-p origdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3054 ;; Construct the individual pieces that we need for assembly:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3055 (orig-arglist (ad-arglist origdef function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3056 (advised-arglist (or (ad-advised-arglist function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3057 orig-arglist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3058 (advised-interactive-form (ad-advised-interactive-form function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3059 (interactive-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3060 (cond (orig-macro-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3061 (advised-interactive-form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3062 ((ad-interactive-form origdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3063 ;; Otherwise we must have a subr: make it interactive if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3064 ;; we have to and initialize required arguments in case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3065 ;; it is called interactively:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3066 (orig-interactive-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3067 (let ((reqargs (car (ad-parse-arglist advised-arglist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3068 (if reqargs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3069 (` (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3070 '(, (make-list (length reqargs) nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3071 '(interactive))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3072 (orig-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3073 (cond ((or orig-special-form-p orig-macro-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3074 ;; Special forms and macros will be advised into macros.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3075 ;; The trick is to construct an expansion for the advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3076 ;; macro that does the correct thing when it gets eval'ed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3077 ;; For macros we'll just use the expansion of the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3078 ;; macro and return that. This way compiled advised macros
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3079 ;; will be expanded into something useful. Note that after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3080 ;; advices have full control over whether they want to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3081 ;; evaluate the expansion (the value of `ad-return-value')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3082 ;; at macro expansion time or not. For special forms there
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3083 ;; is no solution that interacts reasonably with the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3084 ;; compiler, hence we just evaluate the original at macro
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3085 ;; expansion time and return the result. The moral of that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3086 ;; is that one should always deactivate advised special
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3087 ;; forms before one byte-compiles a file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3088 (` ((, (if orig-macro-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3089 'macroexpand
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3090 'eval))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3091 (cons '(, origname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3092 (, (ad-get-arguments advised-arglist 0))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3093 ((and orig-subr-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3094 orig-interactive-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3095 (not advised-interactive-form))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3096 ;; Check whether we were called interactively
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3097 ;; in order to do proper prompting:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3098 (` (if (interactive-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3099 (call-interactively '(, origname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3100 (, (ad-make-mapped-call
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3101 orig-arglist advised-arglist origname)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3102 ;; And now for normal functions and non-interactive subrs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3103 ;; (or subrs whose interactive behavior was advised):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3104 (t (ad-make-mapped-call
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3105 advised-arglist orig-arglist origname)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3107 ;; Finally, build the sucker:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3108 (ad-assemble-advised-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3109 (cond (orig-macro-p 'macro)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3110 (orig-special-form-p 'special-form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3111 (t 'function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3112 advised-arglist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3113 (ad-make-advised-definition-docstring function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3114 interactive-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3115 orig-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3116 (ad-get-enabled-advices function 'before)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3117 (ad-get-enabled-advices function 'around)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3118 (ad-get-enabled-advices function 'after)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3120 (defun ad-assemble-advised-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3121 (type args docstring interactive orig &optional befores arounds afters)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3123 ;;"Assembles an original and its advices into an advised function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3124 ;;It constructs a function or macro definition according to TYPE which has to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3125 ;;be either `macro', `function' or `special-form'. ARGS is the argument list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3126 ;;that has to be used, DOCSTRING if non-nil defines the documentation of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3127 ;;definition, INTERACTIVE if non-nil is the interactive form to be used,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3128 ;;ORIG is a form that calls the body of the original unadvised function,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3129 ;;and BEFORES, AROUNDS and AFTERS are the lists of advices with which ORIG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3130 ;;should be modified. The assembled function will be returned."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3131
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3132 (let (before-forms around-form around-form-protected after-forms definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3133 (ad-dolist (advice befores)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3134 (cond ((and (ad-advice-protected advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3135 before-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3136 (setq before-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3137 (` ((unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3138 (, (ad-prognify before-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3139 (,@ (ad-body-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3140 (ad-advice-definition advice))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3141 (t (setq before-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3142 (append before-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3143 (ad-body-forms (ad-advice-definition advice)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3145 (setq around-form (` (setq ad-return-value (, orig))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3146 (ad-dolist (advice (reverse arounds))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3147 ;; If any of the around advices is protected then we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3148 ;; protect the complete around advice onion:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3149 (if (ad-advice-protected advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3150 (setq around-form-protected t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3151 (setq around-form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3152 (ad-substitute-tree
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3153 (function (lambda (form) (eq form 'ad-do-it)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3154 (function (lambda (form) around-form))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3155 (ad-prognify (ad-body-forms (ad-advice-definition advice))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3157 (setq after-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3158 (if (and around-form-protected before-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3159 (` ((unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3160 (, (ad-prognify before-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3161 (, around-form))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3162 (append before-forms (list around-form))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3163 (ad-dolist (advice afters)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3164 (cond ((and (ad-advice-protected advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3165 after-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3166 (setq after-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3167 (` ((unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3168 (, (ad-prognify after-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3169 (,@ (ad-body-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3170 (ad-advice-definition advice))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3171 (t (setq after-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3172 (append after-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3173 (ad-body-forms (ad-advice-definition advice)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3175 (setq definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3176 (` ((,@ (if (memq type '(macro special-form)) '(macro)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3177 lambda
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3178 (, args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3179 (,@ (if docstring (list docstring)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3180 (,@ (if interactive (list interactive)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3181 (let (ad-return-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3182 (,@ after-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3183 (, (if (eq type 'special-form)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3184 '(list 'quote ad-return-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3185 'ad-return-value))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3187 (ad-insert-argument-access-forms definition args)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3189 ;; This is needed for activation/deactivation hooks:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3190 (defun ad-make-hook-form (function hook-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3191 ;;"Makes hook-form from FUNCTION's advice bodies in class HOOK-NAME."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3192 (let ((hook-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3193 (mapcar (function (lambda (advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3194 (ad-body-forms (ad-advice-definition advice))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3195 (ad-get-enabled-advices function hook-name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3196 (if hook-forms
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3197 (ad-prognify (apply 'append hook-forms)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3198
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3200 ;; @@ Caching:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3201 ;; ===========
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3202 ;; Generating an advised definition of a function is moderately expensive,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3203 ;; hence, it makes sense to cache it so we can reuse it in appropriate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3204 ;; circumstances. Of course, it only makes sense to reuse a cached
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3205 ;; definition if the current advice and function definition state is the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3206 ;; same as it was at the time when the cached definition was generated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3207 ;; For that purpose we associate every cache with an id so we can verify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3208 ;; if it is still valid at a certain point in time. This id mechanism
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3209 ;; makes it possible to preactivate advised functions, write the compiled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3210 ;; advised definitions to a file and reuse them during the actual
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3211 ;; activation without having to risk that the resulting definition will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3212 ;; incorrect, well, almost.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3213 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3214 ;; A cache id is a list with six elements:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3215 ;; 1) the list of names of enabled before advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3216 ;; 2) the list of names of enabled around advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3217 ;; 3) the list of names of enabled after advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3218 ;; 4) the type of the original function (macro, subr, etc.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3219 ;; 5) the arglist of the original definition (or t if it was equal to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3220 ;; arglist of the cached definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3221 ;; 6) t if the interactive form of the original definition was equal to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3222 ;; interactive form of the cached definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3223 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3224 ;; Here's how a cache can get invalidated or be incorrect:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3225 ;; A) a piece of advice used in the cache gets redefined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3226 ;; B) the current list of enabled advices is different from the ones used
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3227 ;; for the cache
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3228 ;; C) the type of the original function changed, e.g., a function became a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3229 ;; macro, or a subr became a function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3230 ;; D) the arglist of the original function changed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3231 ;; E) the interactive form of the original function changed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3232 ;; F) a piece of advice used in the cache got redefined before the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3233 ;; defadvice with the cached definition got loaded: This is a PROBLEM!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3234 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3235 ;; Cases A and B are the normal ones. A is taken care of by `ad-add-advice'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3236 ;; which clears the cache in such a case, B is easily checked during
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3237 ;; verification at activation time.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3238 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3239 ;; Cases C, D and E have to be considered if one is slightly paranoid, i.e.,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3240 ;; if one considers the case that the original function could be different
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3241 ;; from the one available at caching time (e.g., for forward advice of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3242 ;; functions that get redefined by some packages - such as `eval-region' gets
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3243 ;; redefined by edebug). All these cases can be easily checked during
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3244 ;; verification. Element 4 of the id lets one check case C, element 5 takes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3245 ;; care of case D (using t in the equality case saves some space, because the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3246 ;; arglist can be recovered at validation time from the cached definition),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3247 ;; and element 6 takes care of case E which is only a problem if the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3248 ;; was actually a function whose interactive form was not overridden by a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3249 ;; piece of advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3250 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3251 ;; Case F is the only one which will lead to an incorrect advised function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3252 ;; There is no way to avoid this without storing the complete advice definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3253 ;; in the cache-id which is not feasible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3254 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3255 ;; The cache-id of a typical advised function with one piece of advice and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3256 ;; no arglist redefinition takes 7 conses which is a small price to pay for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3257 ;; the added efficiency. The validation itself is also pretty cheap, certainly
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3258 ;; a lot cheaper than reconstructing an advised definition.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3259
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3260 (defmacro ad-get-cache-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3261 (` (car (ad-get-advice-info-field (, function) 'cache))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3263 (defmacro ad-get-cache-id (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3264 (` (cdr (ad-get-advice-info-field (, function) 'cache))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3266 (defmacro ad-set-cache (function definition id)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3267 (` (ad-set-advice-info-field
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3268 (, function) 'cache (cons (, definition) (, id)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3269
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3270 (defun ad-clear-cache (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3271 "Clears a previously cached advised definition of FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3272 Clear the cache if you want to force `ad-activate' to construct a new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3273 advised definition from scratch."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3274 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3275 (list (ad-read-advised-function "Clear cached definition of: ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3276 (ad-set-advice-info-field function 'cache nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3278 (defun ad-make-cache-id (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3279 ;;"Generates an identifying image of the current advices of FUNCTION."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3280 (let ((original-definition (ad-real-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3281 (cached-definition (ad-get-cache-definition function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3282 (list (mapcar (function (lambda (advice) (ad-advice-name advice)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3283 (ad-get-enabled-advices function 'before))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3284 (mapcar (function (lambda (advice) (ad-advice-name advice)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3285 (ad-get-enabled-advices function 'around))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3286 (mapcar (function (lambda (advice) (ad-advice-name advice)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3287 (ad-get-enabled-advices function 'after))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3288 (ad-definition-type original-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3289 (if (equal (ad-arglist original-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3290 (ad-arglist cached-definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3291 t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3292 (ad-arglist original-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3293 (if (eq (ad-definition-type original-definition) 'function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3294 (equal (ad-interactive-form original-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3295 (ad-interactive-form cached-definition))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3296
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3297 (defun ad-get-cache-class-id (function class)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3298 ;;"Returns the part of FUNCTION's cache id that identifies CLASS."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3299 (let ((cache-id (ad-get-cache-id function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3300 (if (eq class 'before)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3301 (car cache-id)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3302 (if (eq class 'around)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3303 (nth 1 cache-id)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3304 (nth 2 cache-id)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3305
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3306 (defun ad-verify-cache-class-id (cache-class-id advices)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3307 (ad-dolist (advice advices (null cache-class-id))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3308 (if (ad-advice-enabled advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3309 (if (eq (car cache-class-id) (ad-advice-name advice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3310 (setq cache-class-id (cdr cache-class-id))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3311 (ad-do-return nil)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3312
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3313 ;; There should be a way to monitor if and why a cache verification failed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3314 ;; in order to determine whether a certain preactivation could be used or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3315 ;; not. Right now the only way to find out is to trace
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3316 ;; `ad-cache-id-verification-code'. The code it returns indicates where the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3317 ;; verification failed. Tracing `ad-verify-cache-class-id' might provide
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3318 ;; some additional useful information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3320 (defun ad-cache-id-verification-code (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3321 (let ((cache-id (ad-get-cache-id function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3322 (code 'before-advice-mismatch))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3323 (and (ad-verify-cache-class-id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3324 (car cache-id) (ad-get-advice-info-field function 'before))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3325 (setq code 'around-advice-mismatch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3326 (ad-verify-cache-class-id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3327 (nth 1 cache-id) (ad-get-advice-info-field function 'around))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3328 (setq code 'after-advice-mismatch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3329 (ad-verify-cache-class-id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3330 (nth 2 cache-id) (ad-get-advice-info-field function 'after))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3331 (setq code 'definition-type-mismatch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3332 (let ((original-definition (ad-real-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3333 (cached-definition (ad-get-cache-definition function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3334 (and (eq (nth 3 cache-id) (ad-definition-type original-definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3335 (setq code 'arglist-mismatch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3336 (equal (if (eq (nth 4 cache-id) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3337 (ad-arglist original-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3338 (nth 4 cache-id) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3339 (ad-arglist cached-definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3340 (setq code 'interactive-form-mismatch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3341 (or (null (nth 5 cache-id))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3342 (equal (ad-interactive-form original-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3343 (ad-interactive-form cached-definition)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3344 (setq code 'verified))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3345 code))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3347 (defun ad-verify-cache-id (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3348 ;;"True if FUNCTION's cache-id is compatible with its current advices."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3349 (eq (ad-cache-id-verification-code function) 'verified))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3350
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3351
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3352 ;; @@ Preactivation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3353 ;; =================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3354 ;; Preactivation can be used to generate compiled advised definitions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3355 ;; at compile time without having to give up the dynamic runtime flexibility
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3356 ;; of the advice mechanism. Preactivation is a special feature of `defadvice',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3357 ;; it involves the following steps:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3358 ;; - remembering the function's current state (definition and advice-info)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3359 ;; - advising it with the defined piece of advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3360 ;; - clearing its cache
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3361 ;; - generating an interpreted advised definition by activating it, this will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3362 ;; make use of all its current active advice and its current definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3363 ;; - saving the so generated cached definition and id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3364 ;; - resetting the function's advice and definition state to what it was
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3365 ;; before the preactivation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3366 ;; - Returning the saved definition and its id to be used in the expansion of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3367 ;; `defadvice' to assign it as an initial cache, hence it will be compiled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3368 ;; at time the `defadvice' gets compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3369 ;; Naturally, for preactivation to be effective it has to be applied/compiled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3370 ;; at the right time, i.e., when the current state of advices and function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3371 ;; definition exactly reflects the state at activation time. Should that not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3372 ;; be the case, the precompiled definition will just be discarded and a new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3373 ;; advised definition will be generated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3374
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3375 (defun ad-preactivate-advice (function advice class position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3376 ;;"Preactivates FUNCTION and returns the constructed cache."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3377 (let* ((function-defined-p (fboundp function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3378 (old-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3379 (if function-defined-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3380 (symbol-function function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3381 (old-advice-info (ad-copy-advice-info function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3382 (ad-advised-functions ad-advised-functions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3383 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3384 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3385 (ad-add-advice function advice class position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3386 (ad-enable-advice function class (ad-advice-name advice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3387 (ad-clear-cache function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3388 (ad-activate-on function -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3389 (if (and (ad-is-active function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3390 (ad-get-cache-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3391 (list (ad-get-cache-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3392 (ad-get-cache-id function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3393 (ad-set-advice-info function old-advice-info)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3394 ;; Don't `fset' function to nil if it was previously unbound:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3395 (if function-defined-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3396 (ad-safe-fset function old-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3397 (fmakunbound function)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3398
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3400 ;; @@ Freezing:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3401 ;; ============
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3402 ;; Freezing transforms a `defadvice' into a redefining `defun/defmacro'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3403 ;; for the advised function without keeping any advice information. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3404 ;; feature was jwz's idea: It generates a dumpable function definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3405 ;; whose documentation can be written to the DOC file, and the generated
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3406 ;; code does not need any Advice runtime support. Of course, frozen advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3407 ;; cannot be undone.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3408
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3409 ;; Freezing only considers the advice of the particular `defadvice', other
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3410 ;; already existing advices for the same function will be ignored. To ensure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3411 ;; proper interaction when an already advised function gets redefined with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3412 ;; a frozen advice, frozen advices always use the actual original definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3413 ;; of the function, i.e., they are always at the core of the onion. E.g., if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3414 ;; an already advised function gets redefined with a frozen advice and then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3415 ;; unadvised, the frozen advice remains as the new definition of the function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3416
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3417 ;; While multiple freeze advices for a single function or freeze-advising
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3418 ;; of an already advised function are possible, they are better avoided,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3419 ;; because definition/compile/load ordering is relevant, and it becomes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3420 ;; incomprehensible pretty quickly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3421
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3422 (defun ad-make-freeze-definition (function advice class position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3423 (if (not (ad-has-proper-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3424 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3425 "ad-make-freeze-definition: `%s' is not yet defined"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3426 function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3427 (let* ((name (ad-advice-name advice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3428 ;; With a unique origname we can have multiple freeze advices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3429 ;; for the same function, each overloading the previous one:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3430 (unique-origname
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3431 (intern (format "%s-%s-%s" (ad-make-origname function) class name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3432 (orig-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3433 ;; If FUNCTION is already advised, we'll use its current origdef
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3434 ;; as the original definition of the frozen advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3435 (or (ad-get-orig-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3436 (symbol-function function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3437 (old-advice-info
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3438 (if (ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3439 (ad-copy-advice-info function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3440 (real-docstring-fn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3441 (symbol-function 'ad-make-advised-definition-docstring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3442 (real-origname-fn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3443 (symbol-function 'ad-make-origname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3444 (frozen-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3445 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3446 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3447 ;; Make sure we construct a proper docstring:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3448 (ad-safe-fset 'ad-make-advised-definition-docstring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3449 'ad-make-freeze-docstring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3450 ;; Make sure `unique-origname' is used as the origname:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3451 (ad-safe-fset 'ad-make-origname '(lambda (x) unique-origname))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3452 ;; No we reset all current advice information to nil and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3453 ;; generate an advised definition that's solely determined
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3454 ;; by ADVICE and the current origdef of FUNCTION:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3455 (ad-set-advice-info function nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3456 (ad-add-advice function advice class position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3457 ;; The following will provide proper real docstrings as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3458 ;; well as a definition that will make the compiler happy:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3459 (ad-set-orig-definition function orig-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3460 (ad-make-advised-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3461 ;; Restore the old advice state:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3462 (ad-set-advice-info function old-advice-info)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3463 ;; Restore functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3464 (ad-safe-fset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3465 'ad-make-advised-definition-docstring real-docstring-fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3466 (ad-safe-fset 'ad-make-origname real-origname-fn))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3467 (if frozen-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3468 (let* ((macro-p (ad-macro-p frozen-definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3469 (body (cdr (if macro-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3470 (ad-lambdafy frozen-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3471 frozen-definition))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3472 (` (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3473 (if (not (fboundp '(, unique-origname)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3474 (fset '(, unique-origname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3475 ;; avoid infinite recursion in case the function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3476 ;; we want to freeze is already advised:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3477 (or (ad-get-orig-definition '(, function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3478 (symbol-function '(, function)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3479 ((, (if macro-p 'defmacro 'defun))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3480 (, function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3481 (,@ body))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3482
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3483
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3484 ;; @@ Activation and definition handling:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3485 ;; ======================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3486
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3487 (defun ad-should-compile (function compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3488 ;;"Returns non-nil if the advised FUNCTION should be compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3489 ;;If COMPILE is non-nil and not a negative number then it returns t.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3490 ;;If COMPILE is a negative number then it returns nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3491 ;;If COMPILE is nil then the result depends on the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3492 ;;`ad-default-compilation-action' (which see)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3493 (if (integerp compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3494 (>= compile 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3495 (if compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3496 compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3497 (cond ((eq ad-default-compilation-action 'never)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3498 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3499 ((eq ad-default-compilation-action 'always)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3500 t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3501 ((eq ad-default-compilation-action 'like-original)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3502 (or (ad-subr-p (ad-get-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3503 (ad-compiled-p (ad-get-orig-definition function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3504 ;; everything else means `maybe':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3505 (t (featurep 'byte-compile))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3507 (defun ad-activate-advised-definition (function compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3508 ;;"Redefines FUNCTION with its advised definition from cache or scratch.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3509 ;;The resulting FUNCTION will be compiled if `ad-should-compile' returns t.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3510 ;;The current definition and its cache-id will be put into the cache."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3511 (let ((verified-cached-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3512 (if (ad-verify-cache-id function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3513 (ad-get-cache-definition function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3514 (ad-safe-fset function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3515 (or verified-cached-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3516 (ad-make-advised-definition function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3517 (if (ad-should-compile function compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3518 (ad-compile-function function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3519 (if verified-cached-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3520 (if (not (eq verified-cached-definition (symbol-function function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3521 ;; we must have compiled, cache the compiled definition:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3522 (ad-set-cache
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3523 function (symbol-function function) (ad-get-cache-id function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3524 ;; We created a new advised definition, cache it with a proper id:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3525 (ad-clear-cache function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3526 ;; ad-make-cache-id needs the new cached definition:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3527 (ad-set-cache function (symbol-function function) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3528 (ad-set-cache
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3529 function (symbol-function function) (ad-make-cache-id function)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3530
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3531 (defun ad-handle-definition (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3532 "Handles re/definition of an advised FUNCTION during de/activation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3533 If FUNCTION does not have an original definition associated with it and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3534 the current definition is usable, then it will be stored as FUNCTION's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3535 original definition. If no current definition is available (even in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3536 case of undefinition) nothing will be done. In the case of redefinition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3537 the action taken depends on the value of `ad-redefinition-action' (which
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3538 see). Redefinition occurs when FUNCTION already has an original definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3539 associated with it but got redefined with a new definition and then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3540 de/activated. If you do not like the current redefinition action change
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3541 the value of `ad-redefinition-action' and de/activate again."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3542 (let ((original-definition (ad-get-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3543 (current-definition (if (ad-real-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3544 (symbol-function function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3545 (if original-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3546 (if current-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3547 (if (and (not (eq current-definition original-definition))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3548 ;; Redefinition with an advised definition from a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3549 ;; different function won't count as such:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3550 (not (ad-advised-definition-p current-definition)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3551 ;; we have a redefinition:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3552 (if (not (memq ad-redefinition-action '(accept discard warn)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3553 (error "ad-handle-definition (see its doc): `%s' %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3554 function "illegally redefined")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3555 (if (eq ad-redefinition-action 'discard)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3556 (ad-safe-fset function original-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3557 (ad-set-orig-definition function current-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3558 (if (eq ad-redefinition-action 'warn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3559 (message "ad-handle-definition: `%s' got redefined"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3560 function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3561 ;; either advised def or correct original is in place:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3562 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3563 ;; we have an undefinition, ignore it:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3564 nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3565 (if current-definition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3566 ;; we have a first definition, save it as original:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3567 (ad-set-orig-definition function current-definition)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3568 ;; we don't have anything noteworthy:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3569 nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3570
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3571
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3572 ;; @@ The top-level advice interface:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3573 ;; ==================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3574
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3575 (defun ad-activate-on (function &optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3576 "Activates all the advice information of an advised FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3577 If FUNCTION has a proper original definition then an advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3578 definition will be generated from FUNCTION's advice info and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3579 definition of FUNCTION will be replaced with it. If a previously
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3580 cached advised definition was available, it will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3581 The optional COMPILE argument determines whether the resulting function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3582 or a compilable cached definition will be compiled. If it is negative
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3583 no compilation will be performed, if it is positive or otherwise non-nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3584 the resulting function will be compiled, if it is nil the behavior depends
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3585 on the value of `ad-default-compilation-action' (which see).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3586 Activation of an advised function that has an advice info but no actual
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3587 pieces of advice is equivalent to a call to `ad-unadvise'. Activation of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3588 an advised function that has actual pieces of advice but none of them are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3589 enabled is equivalent to a call to `ad-deactivate'. The current advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3590 definition will always be cached for later usage."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3591 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3592 (list (ad-read-advised-function "Activate advice of: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3593 current-prefix-arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3594 (if ad-activate-on-top-level
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3595 ;; avoid recursive calls to `ad-activate-on':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3596 (ad-with-auto-activation-disabled
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3597 (if (not (ad-is-advised function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3598 (error "ad-activate: `%s' is not advised" function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3599 (ad-handle-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3600 ;; Just return for forward advised and not yet defined functions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3601 (if (ad-get-orig-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3602 (if (not (ad-has-any-advice function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3603 (ad-unadvise function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3604 ;; Otherwise activate the advice:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3605 (cond ((ad-has-redefining-advice function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3606 (ad-activate-advised-definition function compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3607 (ad-set-advice-info-field function 'active t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3608 (eval (ad-make-hook-form function 'activation))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3609 function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3610 ;; Here we are if we have all disabled advices:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3611 (t (ad-deactivate function)))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3612
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3613 (defun ad-deactivate (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3614 "Deactivates the advice of an actively advised FUNCTION.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3615 If FUNCTION has a proper original definition, then the current
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3616 definition of FUNCTION will be replaced with it. All the advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3617 information will still be available so it can be activated again with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3618 a call to `ad-activate'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3619 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3620 (list (ad-read-advised-function "Deactivate advice of: " 'ad-is-active)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3621 (if (not (ad-is-advised function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3622 (error "ad-deactivate: `%s' is not advised" function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3623 (cond ((ad-is-active function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3624 (ad-handle-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3625 (if (not (ad-get-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3626 (error "ad-deactivate: `%s' has no original definition"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3627 function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3628 (ad-safe-fset function (ad-get-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3629 (ad-set-advice-info-field function 'active nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3630 (eval (ad-make-hook-form function 'deactivation))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3631 function)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3632
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3633 (defun ad-update (function &optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3634 "Update the advised definition of FUNCTION if its advice is active.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3635 See `ad-activate-on' for documentation on the optional COMPILE argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3636 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3637 (list (ad-read-advised-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3638 "Update advised definition of: " 'ad-is-active)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3639 (if (ad-is-active function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3640 (ad-activate-on function compile)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3641
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3642 (defun ad-unadvise (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3643 "Deactivates FUNCTION and then removes all its advice information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3644 If FUNCTION was not advised this will be a noop."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3645 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3646 (list (ad-read-advised-function "Unadvise function: ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3647 (cond ((ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3648 (if (ad-is-active function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3649 (ad-deactivate function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3650 (ad-clear-orig-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3651 (ad-set-advice-info function nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3652 (ad-pop-advised-function function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3653
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3654 (defun ad-recover (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3655 "Tries to recover FUNCTION's original definition and unadvises it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3656 This is more low-level than `ad-unadvise' because it does not do any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3657 deactivation which might run hooks and get into other trouble.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3658 Use in emergencies."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3659 ;; Use more primitive interactive behavior here: Accept any symbol that's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3660 ;; currently defined in obarray, not necessarily with a function definition:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3661 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3662 (list (intern
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3663 (completing-read "Recover advised function: " obarray nil t))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3664 (cond ((ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3665 (cond ((ad-get-orig-definition function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3666 (ad-safe-fset function (ad-get-orig-definition function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3667 (ad-clear-orig-definition function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3668 (ad-set-advice-info function nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3669 (ad-pop-advised-function function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3670
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3671 (defun ad-activate-regexp (regexp &optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3672 "Activates functions with an advice name containing a REGEXP match.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3673 See `ad-activate-on' for documentation on the optional COMPILE argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3674 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3675 (list (ad-read-regexp "Activate via advice regexp: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3676 current-prefix-arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3677 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3678 (if (ad-find-some-advice function 'any regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3679 (ad-activate-on function compile))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3680
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3681 (defun ad-deactivate-regexp (regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3682 "Deactivates functions with an advice name containing REGEXP match."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3683 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3684 (list (ad-read-regexp "Deactivate via advice regexp: ")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3685 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3686 (if (ad-find-some-advice function 'any regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3687 (ad-deactivate function))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3688
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3689 (defun ad-update-regexp (regexp &optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3690 "Updates functions with an advice name containing a REGEXP match.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3691 See `ad-activate-on' for documentation on the optional COMPILE argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3692 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3693 (list (ad-read-regexp "Update via advice regexp: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3694 current-prefix-arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3695 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3696 (if (ad-find-some-advice function 'any regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3697 (ad-update function compile))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3698
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3699 (defun ad-activate-all (&optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3700 "Activates all currently advised functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3701 See `ad-activate-on' for documentation on the optional COMPILE argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3702 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3703 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3704 (ad-activate-on function compile)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3705
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3706 (defun ad-deactivate-all ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3707 "Deactivates all currently advised functions."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3708 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3709 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3710 (ad-deactivate function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3711
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3712 (defun ad-update-all (&optional compile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3713 "Updates all currently advised functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3714 With prefix argument compiles resulting advised definitions."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3715 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3716 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3717 (ad-update function compile)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3718
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3719 (defun ad-unadvise-all ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3720 "Unadvises all currently advised functions."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3721 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3722 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3723 (ad-unadvise function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3724
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3725 (defun ad-recover-all ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3726 "Recovers all currently advised functions. Use in emergencies."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3727 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3728 (ad-do-advised-functions (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3729 (condition-case nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3730 (ad-recover function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3731 (error nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3732
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3733
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3734 ;; Completion alist of legal `defadvice' flags
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3735 (defvar ad-defadvice-flags
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3736 '(("protect") ("disable") ("activate")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3737 ("compile") ("preactivate") ("freeze")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3738
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3739 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3740 (defmacro defadvice (function args &rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3741 "Defines a piece of advice for FUNCTION (a symbol).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3742 The syntax of `defadvice' is as follows:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3743
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3744 (defadvice FUNCTION (CLASS NAME [POSITION] [ARGLIST] FLAG...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3745 [DOCSTRING] [INTERACTIVE-FORM]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3746 BODY... )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3747
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3748 FUNCTION ::= Name of the function to be advised.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3749 CLASS ::= `before' | `around' | `after' | `activation' | `deactivation'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3750 NAME ::= Non-nil symbol that names this piece of advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3751 POSITION ::= `first' | `last' | NUMBER. Optional, defaults to `first',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3752 see also `ad-add-advice'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3753 ARGLIST ::= An optional argument list to be used for the advised function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3754 instead of the argument list of the original. The first one found in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3755 before/around/after-advices will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3756 FLAG ::= `protect'|`disable'|`activate'|`compile'|`preactivate'|`freeze'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3757 All flags can be specified with unambiguous initial substrings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3758 DOCSTRING ::= Optional documentation for this piece of advice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3759 INTERACTIVE-FORM ::= Optional interactive form to be used for the advised
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3760 function. The first one found in before/around/after-advices will be used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3761 BODY ::= Any s-expression.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3762
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3763 Semantics of the various flags:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3764 `protect': The piece of advice will be protected against non-local exits in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3765 any code that precedes it. If any around-advice of a function is protected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3766 then automatically all around-advices will be protected (the complete onion).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3767
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3768 `activate': All advice of FUNCTION will be activated immediately if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3769 FUNCTION has been properly defined prior to this application of `defadvice'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3770
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3771 `compile': In conjunction with `activate' specifies that the resulting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3772 advised function should be compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3773
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3774 `disable': The defined advice will be disabled, hence, it will not be used
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3775 during activation until somebody enables it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3776
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3777 `preactivate': Preactivates the advised FUNCTION at macro-expansion/compile
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3778 time. This generates a compiled advised definition according to the current
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3779 advice state that will be used during activation if appropriate. Only use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3780 this if the `defadvice' gets actually compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3781
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3782 `freeze': Expands the `defadvice' into a redefining `defun/defmacro' according
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3783 to this particular single advice. No other advice information will be saved.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3784 Frozen advices cannot be undone, they behave like a hard redefinition of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3785 the advised function. `freeze' implies `activate' and `preactivate'. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3786 documentation of the advised function can be dumped onto the `DOC' file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3787 during preloading.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3788
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3789 Look at the file `advice.el' for comprehensive documentation."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3790 (if (not (ad-name-p function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3791 (error "defadvice: Illegal function name: %s" function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3792 (let* ((class (car args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3793 (name (if (not (ad-class-p class))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3794 (error "defadvice: Illegal advice class: %s" class)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3795 (nth 1 args)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3796 (position (if (not (ad-name-p name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3797 (error "defadvice: Illegal advice name: %s" name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3798 (setq args (nthcdr 2 args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3799 (if (ad-position-p (car args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3800 (prog1 (car args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3801 (setq args (cdr args))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3802 (arglist (if (listp (car args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3803 (prog1 (car args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3804 (setq args (cdr args)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3805 (flags
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3806 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3807 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3808 (lambda (flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3809 (let ((completion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3810 (try-completion (symbol-name flag) ad-defadvice-flags)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3811 (cond ((eq completion t) flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3812 ((assoc completion ad-defadvice-flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3813 (intern completion))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3814 (t (error "defadvice: Illegal or ambiguous flag: %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3815 flag))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3816 args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3817 (advice (ad-make-advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3818 name (memq 'protect flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3819 (not (memq 'disable flags))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3820 (` (advice lambda (, arglist) (,@ body)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3821 (preactivation (if (memq 'preactivate flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3822 (ad-preactivate-advice
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3823 function advice class position))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3824 ;; Now for the things to be done at evaluation time:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3825 (if (memq 'freeze flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3826 ;; jwz's idea: Freeze the advised definition into a dumpable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3827 ;; defun/defmacro whose docs can be written to the DOC file:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3828 (ad-make-freeze-definition function advice class position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3829 ;; the normal case:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3830 (` (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3831 (ad-add-advice '(, function) '(, advice) '(, class) '(, position))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3832 (,@ (if preactivation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3833 (` ((ad-set-cache
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3834 '(, function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3835 ;; the function will get compiled:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3836 (, (cond ((ad-macro-p (car preactivation))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3837 (` (ad-macrofy
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3838 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3839 (, (ad-lambdafy
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3840 (car preactivation)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3841 (t (` (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3842 (, (car preactivation)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3843 '(, (car (cdr preactivation))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3844 (,@ (if (memq 'activate flags)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3845 (` ((ad-activate-on '(, function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3846 (, (if (memq 'compile flags) t)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3847 '(, function))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3848
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3849
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3850 ;; @@ Tools:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3851 ;; =========
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3852
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3853 (defmacro ad-with-originals (functions &rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3854 "Binds FUNCTIONS to their original definitions and executes BODY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3855 For any members of FUNCTIONS that are not currently advised the rebinding will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3856 be a noop. Any modifications done to the definitions of FUNCTIONS will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3857 undone on exit of this macro."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3858 (let* ((index -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3859 ;; Make let-variables to store current definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3860 (current-bindings
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3861 (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3862 (lambda (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3863 (setq index (1+ index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3864 (list (intern (format "ad-oRiGdEf-%d" index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3865 (` (symbol-function '(, function))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3866 functions)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3867 (` (let (, current-bindings)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3868 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3869 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3870 (,@ (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3871 ;; Make forms to redefine functions to their
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3872 ;; original definitions if they are advised:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3873 (setq index -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3874 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3875 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3876 (lambda (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3877 (setq index (1+ index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3878 (` (ad-safe-fset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3879 '(, function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3880 (or (ad-get-orig-definition '(, function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3881 (, (car (nth index current-bindings))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3882 functions)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3883 (,@ body))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3884 (,@ (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3885 ;; Make forms to back-define functions to the definitions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3886 ;; they had outside this macro call:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3887 (setq index -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3888 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3889 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3890 (lambda (function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3891 (setq index (1+ index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3892 (` (ad-safe-fset
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3893 '(, function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3894 (, (car (nth index current-bindings)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3895 functions))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3896
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3897 (if (not (get 'ad-with-originals 'lisp-indent-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3898 (put 'ad-with-originals 'lisp-indent-hook 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3899
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3900
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3901 ;; @@ Advising `documentation':
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3902 ;; ============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3903 ;; Use the advice mechanism to advise `documentation' to make it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3904 ;; generate proper documentation strings for advised definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3905
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3906 (defadvice documentation (after ad-advised-docstring first disable preact)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3907 "Builds an advised docstring if FUNCTION is advised."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3908 ;; Because we get the function name from the advised docstring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3909 ;; this will work for function names as well as for definitions:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3910 (if (and (stringp ad-return-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3911 (string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3912 ad-advised-definition-docstring-regexp ad-return-value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3913 (let ((function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3914 (car (read-from-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3915 ad-return-value (match-beginning 1) (match-end 1)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3916 (cond ((ad-is-advised function)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3917 (setq ad-return-value (ad-make-advised-docstring function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3918 ;; Handle optional `raw' argument:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3919 (if (not (ad-get-arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3920 (setq ad-return-value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3921 (substitute-command-keys ad-return-value))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3922
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3923
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3924 ;; @@ Starting, stopping and recovering from the advice package magic:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3925 ;; ===================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3926
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3927 (defun ad-start-advice ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3928 "Starts the automatic advice handling magic."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3929 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3930 ;; Advising `ad-activate' means death!!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3931 (ad-set-advice-info 'ad-activate nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3932 (ad-safe-fset 'ad-activate 'ad-activate-on)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3933 (ad-enable-advice 'documentation 'after 'ad-advised-docstring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3934 (ad-activate-on 'documentation 'compile))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3935
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3936 (defun ad-stop-advice ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3937 "Stops the automatic advice handling magic.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3938 You should only need this in case of Advice-related emergencies."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3939 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3940 ;; Advising `ad-activate' means death!!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3941 (ad-set-advice-info 'ad-activate nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3942 (ad-disable-advice 'documentation 'after 'ad-advised-docstring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3943 (ad-update 'documentation)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3944 (ad-safe-fset 'ad-activate 'ad-activate-off))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3945
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3946 (defun ad-recover-normality ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3947 "Undoes all advice related redefinitions and unadvises everything.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3948 Use only in REAL emergencies."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3949 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3950 ;; Advising `ad-activate' means death!!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3951 (ad-set-advice-info 'ad-activate nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3952 (ad-safe-fset 'ad-activate 'ad-activate-off)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3953 (ad-recover-all)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3954 (setq ad-advised-functions nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3955
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3956 (ad-start-advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3957
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3958 (provide 'advice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3959
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3960 ;;; advice.el ends here