annotate lisp/emacs-lisp/advice.el @ 5746:94535442be19

(ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we want to preload this file.
author Karl Heuer <kwzh@gnu.org>
date Wed, 02 Feb 1994 21:35:07 +0000
parents 9cde7d7fea1f
children 2f1deaa86ee2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4110
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1 ;;; advice.el --- advice mechanism for Emacs Lisp functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
5 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu>
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
6 ;; Created: 12 Dec 1992
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
7 ;; Version: advice.el,v 2.1 1993/05/26 00:07:58 hans Exp
5140
9cde7d7fea1f Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 5138
diff changeset
8 ;; Keywords: extensions, lisp, tools
4110
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
9
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
11
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
15 ;; any later version.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
16
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
20 ;; GNU General Public License for more details.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
21
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
25
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
26 ;; LCD Archive Entry:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
27 ;; advice|Hans Chalupsky|hans@cs.buffalo.edu|
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
28 ;; Advice mechanism for Emacs Lisp functions|
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
29 ;; 1993/05/26 00:07:58|2.1|~/packages/advice.el.Z|
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
30
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
31
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
32 ;;; Commentary:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
33
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
34 ;; @ Introduction:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
35 ;; ===============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
36 ;; This package implements a full-fledged Lisp-style advice mechanism
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
37 ;; for Emacs Lisp. Advice is a clean and efficient way to modify the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
38 ;; behavior of Emacs Lisp functions without having to keep personal
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
39 ;; modified copies of such functions around. A great number of such
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
40 ;; modifications can be achieved by treating the original function as a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
41 ;; black box and specifying a different execution environment for it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
42 ;; with a piece of advice. Think of a piece of advice as a kind of fancy
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
43 ;; hook that you can attach to any function/macro/subr.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
44
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
45 ;; @ Highlights:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
46 ;; =============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
47 ;; - Clean definition of multiple, named before/around/after advices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
48 ;; for functions, macros, subrs and special forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
49 ;; - Full control over the arguments an advised function will receive,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
50 ;; the binding environment in which it will be executed, as well as the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
51 ;; value it will return.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
52 ;; - Allows re/definition of interactive behavior for functions and subrs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
53 ;; - Every piece of advice can have its documentation string which will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
54 ;; combined with the original documentation of the advised function at
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
55 ;; call-time of `documentation' for proper command-key substitution.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
56 ;; - The execution of every piece of advice can be protected against error
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
57 ;; and non-local exits in preceding code or advices.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
58 ;; - Simple argument access either by name, or, more portable but as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
59 ;; efficient, via access macros
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
60 ;; - Allows the specification of a different argument list for the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
61 ;; version of a function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
62 ;; - Advised functions can be byte-compiled either at file-compile time
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
63 ;; (see preactivation) or activation time.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
64 ;; - Separation of advice definition and activation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
65 ;; - Provides generally accessible function definition (after) hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
66 ;; - Forward advice is possible (an application of definition hooks), that is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
67 ;; as yet undefined or autoload functions can be advised without having to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
68 ;; preload the file in which they are defined.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
69 ;; - Forward redefinition is possible because around advice can be used to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
70 ;; completely redefine a function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
71 ;; - A caching mechanism for advised definition provides for cheap deactivation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
72 ;; and reactivation of advised functions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
73 ;; - Preactivation allows efficient construction and compilation of advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
74 ;; definitions at file compile time without giving up the flexibility of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
75 ;; the advice mechanism.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
76 ;; - En/disablement mechanism allows the use of different "views" of advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
77 ;; functions depending on what pieces of advice are currently en/disabled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
78 ;; - Provides manipulation mechanisms for sets of advised functions via
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
79 ;; regular expressions that match advice names
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
80 ;; - Allows definition of load-hooks for arbitrary Emacs Lisp files without
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
81 ;; modification of these files
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
82
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
83 ;; @ How to get the latest advice.el:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
84 ;; ==================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
85 ;; You can get the latest version of this package either via anonymous ftp
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
86 ;; from ftp.cs.buffalo.edu (128.205.32.9) with pathname /pub/Emacs/advice.el,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
87 ;; or send email to hans@cs.buffalo.edu and I'll mail it to you.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
88
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
89 ;; @ Overview, or how to read this file:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
90 ;; =====================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
91 ;; Advice has enough features now to justify an info file, however, I
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
92 ;; didn't have the time yet to do all the necessary formatting. So,
4827
8a20e754058e Doc fix.
Brian Fox <bfox@gnu.org>
parents: 4111
diff changeset
93 ;; until I do have the time or some kind soul does it for me I crammed
4110
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
94 ;; everything into the source file. Because about 50% of this file is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
95 ;; documentation it should be in outline-mode by default, but it is not.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
96 ;; If you choose to use outline-mode set `outline-regexp' to `";; @+"'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
97 ;; and use `M-x hide-body' to see just the headings. Use the various
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
98 ;; other outline-mode functions to move around in the text. If you use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
99 ;; Lucid Emacs, you'll just have to wait until `selective-display'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
100 ;; works properly in order to be able to use outline-mode, sorry.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
101 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
102 ;; And yes, I know: Documentation is for wimps.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
103 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
104 ;; The four major sections of this file are:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
105 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
106 ;; @ This initial information ...installation, customization etc.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
107 ;; @ Advice documentation: ...general documentation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
108 ;; @ Foo games: An advice tutorial ...teaches about advice by example
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
109 ;; @ Advice implementation: ...actual code, yeah!!
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
110 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
111 ;; The latter three are actual headings which you can search for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
112 ;; directly in case outline-mode doesn't work for you.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
113
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
114 ;; @ Restrictions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
115 ;; ===============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
116 ;; - Advised functions/macros/subrs will only exhibit their advised behavior
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
117 ;; when they are invoked via their function cell. This means that advice will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
118 ;; not work for the following:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
119 ;; + advised subrs that are called directly from other subrs or C-code
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
120 ;; + advised subrs that got replaced with their byte-code during
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
121 ;; byte-compilation (e.g., car)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
122 ;; + advised macros which were expanded during byte-compilation before
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
123 ;; their advice was activated.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
124 ;; - This package was developed under GNU Emacs 18.59 and Lucid Emacs 19.6.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
125 ;; It was adapted and tested for GNU Emacs 19.8 and seems to work ok for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
126 ;; Epoch 4.2. For different Emacs environments your mileage may vary.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
127
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
128 ;; @ Credits:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
129 ;; ==========
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
130 ;; This package is an extension and generalization of packages such as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
131 ;; insert-hooks.el written by Noah S. Friedman, and advise.el written by
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
132 ;; Raul J. Acevedo. Some ideas used in here come from these packages,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
133 ;; others come from the various Lisp advice mechanisms I've come across
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
134 ;; so far, and a few are simply mine.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
135
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
136 ;; @ Comments, suggestions, bug reports:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
137 ;; =====================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
138 ;; If you find any bugs, have suggestions for new advice features, find the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
139 ;; documentation wrong, confusing, incomplete, or otherwise unsatisfactory,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
140 ;; have any questions about advice.el, or have otherwise enlightening
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
141 ;; comments feel free to send me email at <hans@cs.buffalo.edu>.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
142
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
143 ;; @ Safety Rules and Emergency Exits:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
144 ;; ===================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
145 ;; Before we begin: CAUTION!!
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
146 ;; advice.el provides you with a lot of rope to hang yourself on very
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
147 ;; easily accessible trees, so, here are a few important things you
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
148 ;; should know: Once advice has been started with `ad-start-advice' it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
149 ;; generates advised definitions of the `documentation' function, and,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
150 ;; if definition hooks are enabled (e.g., for forward advice), also of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
151 ;; `defun', `defmacro' and `fset' (if you use Jamie Zawinski's (jwz)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
152 ;; optimizing byte-compiler as standardly used in GNU Emacs-19 and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
153 ;; Lucid Emacs-19 (Lemacs), then enabling definition hooks will also
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
154 ;; redefine the `byte-code' subr). All these changes can be undone at
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
155 ;; any time with `M-x ad-stop-advice'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
156 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
157 ;; If you experience any strange behavior/errors etc. that you attribute to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
158 ;; advice.el or to some ill-advised function do one of the following:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
159
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
160 ;; - M-x ad-deactivate FUNCTION (if you have a definite suspicion what
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
161 ;; function gives you problems)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
162 ;; - M-x ad-deactivate-all (if you don't have a clue what's going wrong)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
163 ;; - M-x ad-stop-advice (if you think the problem is related to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
164 ;; advised functions used by advice.el itself)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
165 ;; - M-x ad-recover-normality (for real emergencies)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
166 ;; - If none of the above solves your advice related problem go to another
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
167 ;; terminal, kill your Emacs process and send me some hate mail.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
168
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
169 ;; The first three measures have restarts, i.e., once you've figured out
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
170 ;; the problem you can reactivate advised functions with either `ad-activate',
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
171 ;; `ad-activate-all', or `ad-start-advice'. `ad-recover-normality' unadvises
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
172 ;; everything so you won't be able to reactivate any advised functions, you'll
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
173 ;; have to stick with their standard incarnations for the rest of the session.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
174
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
175 ;; IMPORTANT: With advice.el loaded always do `M-x ad-deactivate-all' before
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
176 ;; you byte-compile a file, because advised special forms and macros can lead
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
177 ;; to unwanted compilation results. When you are done compiling use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
178 ;; `M-x ad-activate-all' to go back to the advised state of all your
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
179 ;; advised functions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
180
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
181 ;; RELAX: advice.el is pretty safe even if you are oblivious to the above.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
182 ;; I use it extensively and haven't run into any serious trouble in a long
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
183 ;; time. Just wanted you to be warned.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
184
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
185 ;; @ Installation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
186 ;; ===============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
187 ;; Put this file somewhere into your Emacs `load-path' and byte-compile it.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
188 ;; Both steps are mandatory! You cannot (and would not want to) run advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
189 ;; uncompiled, and because there is bootstrapping going on the byte-compiler
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
190 ;; needs to preload advice in order to compile it, hence, it has to find it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
191 ;; in your `load-path' (you can preload advice.el "by hand" before you compile
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
192 ;; it if you don't want to put it into your `load-path'). Once you have
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
193 ;; compiled advice put the following autoload declarations into your .emacs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
194 ;; to load it on demand
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
195 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
196 ;; (autoload 'defadvice "advice" "Define a piece of advice" nil t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
197 ;; (autoload 'ad-add-advice "advice" "Add a piece of advice")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
198 ;; (autoload 'ad-start-advice "advice" "Start advice magic" t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
199 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
200 ;; or explicitly load it with (require 'advice) or (load "advice").
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
201
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
202 ;; @@ Preloading:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
203 ;; ==============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
204 ;; If you preload the complete advice.el or its autoloads into a dumped Emacs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
205 ;; image and you use jwz's byte-compiler make sure advice gets loaded after the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
206 ;; byte-compiler runtime support is loaded so that `ad-use-jwz-byte-compiler'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
207 ;; receives the proper initial value.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
208
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
209 ;; @ Customization:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
210 ;; ================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
211 ;; Part of the advice magic does not start until you call `ad-start-advice'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
212 ;; which you can either do interactively, explicitly in your .emacs, or by
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
213 ;; putting
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
214 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
215 ;; (setq ad-start-advice-on-load t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
216 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
217 ;; into your .emacs which will automatically start advice when the file gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
218 ;; loaded.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
219
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
220 ;; If you want to be able to forward advise functions, that is to advise them
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
221 ;; when they are not yet defined or defined as autoloads, then you should put
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
222 ;; the following into your .emacs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
223 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
224 ;; (setq ad-activate-on-definition t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
225 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
226 ;; which will activate all advice at the time the function gets actually
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
227 ;; defined/loaded. The value of this variable will not have any effect until
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
228 ;; `ad-start-advice' gets executed.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
229
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
230 ;; If you use a v18 Emacs but use jwz's byte-compiler and want to use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
231 ;; forward advice make sure that `ad-use-jwz-byte-compiler' has a non-NIL
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
232 ;; value after advice.el got loaded. If it doesn't set it explicitly in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
233 ;; your .emacs with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
234 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
235 ;; (setq ad-use-jwz-byte-compiler t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
236 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
237 ;; Also make sure that you read the paragraph on forward advice below to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
238 ;; find out about the trade-offs involved for this combination of features.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
239
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
240 ;; Look at the documentation of `ad-redefinition-action' for possible values
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
241 ;; of this variable. Its default value is `warn' which will print a warning
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
242 ;; message when an already defined advised function gets redefined with a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
243 ;; new original definition and de/activated.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
244
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
245 ;; @ Motivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
246 ;; =============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
247 ;; Before I go on explaining how advice works, here are four simple examples
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
248 ;; how this package can be used. The first three are very useful, the last one
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
249 ;; is just a joke:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
250
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
251 ;;(defadvice switch-to-buffer (before existing-buffers-only activate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
252 ;; "When called interactively switch to existing buffers only, unless
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
253 ;;when called with a prefix argument."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
254 ;; (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
255 ;; (list (read-buffer "Switch to buffer: " (other-buffer)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
256 ;; (null current-prefix-arg)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
257 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
258 ;;(defadvice switch-to-buffer (around confirm-non-existing-buffers activate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
259 ;; "Switch to non-existing buffers only upon confirmation."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
260 ;; (interactive "BSwitch to buffer: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
261 ;; (if (or (get-buffer (ad-get-arg 0))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
262 ;; (y-or-n-p (format "`%s' does not exist, create? " (ad-get-arg 0))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
263 ;; ad-do-it))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
264 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
265 ;;(defadvice find-file (before existing-files-only activate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
266 ;; "Find existing files only"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
267 ;; (interactive "fFind file: "))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
268 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
269 ;;(defadvice car (around interactive activate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
270 ;; "Make `car' an interactive function."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
271 ;; (interactive "xCar of list: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
272 ;; ad-do-it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
273 ;; (if (interactive-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
274 ;; (message "%s" ad-return-value)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
275
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
276
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
277 ;; @ Advice documentation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
278 ;; =======================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
279 ;; Below is general documentation of the various features of advice. For more
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
280 ;; concrete examples check the corresponding sections in the tutorial part.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
281
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
282 ;; @@ Terminology:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
283 ;; ===============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
284 ;; - GNU Emacs-19: GNU's version of Emacs with major version 19
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
285 ;; - Lemacs: Lucid's version of Emacs with major version 19
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
286 ;; - v18: Any Emacs with major version 18 or built as an extension to that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
287 ;; (such as Epoch)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
288 ;; - v19: Any Emacs with major version 19
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
289 ;; - jwz: Jamie Zawinski - keeper of Lemacs and creator of the optimizing
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
290 ;; byte-compiler used in v19s.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
291 ;; - advices: Short for "pieces of advice".
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
292
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
293 ;; @@ Defining a piece of advice with `defadvice':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
294 ;; ===============================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
295 ;; The main means of defining a piece of advice is the macro `defadvice',
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
296 ;; there is no interactive way of specifying a piece of advice. A call to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
297 ;; `defadvice' has the following syntax which is similar to the syntax of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
298 ;; `defun/defmacro':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
299 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
300 ;; (defadvice <function> (<class> <name> [<position>] [<arglist>] {<flags>}*)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
301 ;; [ [<documentation-string>] [<interactive-form>] ]
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
302 ;; {<body-form>}* )
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
303
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
304 ;; <function> is the name of the function/macro/subr to be advised.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
305
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
306 ;; <class> is the class of the advice which has to be one of `before',
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
307 ;; `around', `after', `activation' or `deactivation' (the last two allow
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
308 ;; definition of special act/deactivation hooks).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
309
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
310 ;; <name> is the name of the advice which has to be a non-NIL symbol.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
311 ;; Names uniquely identify a piece of advice in a certain advice class,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
312 ;; hence, advices can be redefined by defining an advice with the same class
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
313 ;; and name. Advice names are global symbols, hence, the same name space
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
314 ;; conventions used for function names should be applied.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
315
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
316 ;; An optional <position> specifies where in the current list of advices of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
317 ;; the specified <class> this new advice will be placed. <position> has to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
318 ;; be either `first', `last' or a number that specifies a zero-based
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
319 ;; position (`first' is equivalent to 0). If no position is specified
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
320 ;; `first' will be used as a default. If this call to `defadvice' redefines
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
321 ;; an already existing advice (see above) then the position argument will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
322 ;; be ignored and the position of the already existing advice will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
323
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
324 ;; An optional <arglist> which has to be a list can be used to define the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
325 ;; argument list of the advised function. This argument list should of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
326 ;; course be compatible with the argument list of the original function,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
327 ;; otherwise functions that call the advised function with the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
328 ;; argument list in mind will break. If more than one advice specify an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
329 ;; argument list then the first one (the one with the smallest position)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
330 ;; found in the list of before/around/after advices will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
331
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
332 ;; <flags> is a list of symbols that specify further information about the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
333 ;; advice. All flags can be specified with unambiguous initial substrings.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
334 ;; `activate': Specifies that the advice information of the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
335 ;; function should be activated right after this advice has been
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
336 ;; defined. In forward advices `activate' will be ignored.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
337 ;; `protect': Specifies that this advice should be protected against
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
338 ;; non-local exits and errors in preceding code/advices.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
339 ;; `compile': Specifies that the advised function should be byte-compiled.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
340 ;; This flag will be ignored unless `activate' is also specified.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
341 ;; `disable': Specifies that the defined advice should be disabled, hence,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
342 ;; it will not be used in an activation until somebody enables it.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
343 ;; `preactivate': Specifies that the advised function should get preactivated
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
344 ;; at macro-expansion/compile time of this `defadvice'. This
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
345 ;; generates a compiled advised definition according to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
346 ;; current advice state which will be used during activation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
347 ;; if appropriate. Only use this if the `defadvice' gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
348 ;; actually compiled (with a v18 byte-compiler put the `defadvice'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
349 ;; into the body of a `defun' to accomplish proper compilation).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
350
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
351 ;; An optional <documentation-string> can be supplied to document the advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
352 ;; On call of the `documentation' function it will be combined with the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
353 ;; documentation strings of the original function and other advices.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
354
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
355 ;; An optional <interactive-form> form can be supplied to change/add
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
356 ;; interactive behavior of the original function. If more than one advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
357 ;; has an `(interactive ...)' specification then the first one (the one
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
358 ;; with the smallest position) found in the list of before/around/after
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
359 ;; advices will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
360
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
361 ;; A possibly empty list of <body-forms> specifies the body of the advice in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
362 ;; an implicit progn. The body of an advice can access/change arguments,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
363 ;; the return value, the binding environment, and can have all sorts of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
364 ;; other side effects.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
365
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
366 ;; @@ Assembling advised definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
367 ;; ==================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
368 ;; Suppose a function/macro/subr/special-form has N pieces of before advice,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
369 ;; M pieces of around advice and K pieces of after advice. Assuming none of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
370 ;; the advices is protected, its advised definition will look like this
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
371 ;; (body-form indices correspond to the position of the respective advice in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
372 ;; that advice class):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
373
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
374 ;; ([macro] lambda <arglist>
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
375 ;; [ [<advised-docstring>] [(interactive ...)] ]
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
376 ;; (let (ad-return-value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
377 ;; {<before-0-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
378 ;; ....
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
379 ;; {<before-N-1-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
380 ;; {<around-0-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
381 ;; {<around-1-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
382 ;; ....
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
383 ;; {<around-M-1-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
384 ;; (setq ad-return-value
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
385 ;; <apply original definition to <arglist>>)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
386 ;; {<other-around-M-1-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
387 ;; ....
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
388 ;; {<other-around-1-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
389 ;; {<other-around-0-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
390 ;; {<after-0-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
391 ;; ....
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
392 ;; {<after-K-1-body-form>}*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
393 ;; ad-return-value))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
394
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
395 ;; Macros and special forms will be redefined as macros, hence the optional
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
396 ;; [macro] in the beginning of the definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
397
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
398 ;; <arglist> is either the argument list of the original function or the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
399 ;; first argument list defined in the list of before/around/after advices.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
400 ;; The values of <arglist> variables can be accessed/changed in the body of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
401 ;; an advice by simply referring to them by their original name, however,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
402 ;; more portable argument access macros are also provided (see below). For
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
403 ;; subrs/special-forms for which neither explicit argument list definitions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
404 ;; are available, nor their documentation strings contain such definitions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
405 ;; (as they do v19s), `(&rest ad-subr-args)' will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
406
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
407 ;; <advised-docstring> is an optional, special documentation string which will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
408 ;; be expanded into a proper documentation string upon call of `documentation'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
409
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
410 ;; (interactive ...) is an optional interactive form either taken from the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
411 ;; original function or from a before/around/after advice. For advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
412 ;; interactive subrs that do not have an interactive form specified in any
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
413 ;; advice we have to use (interactive) and then call the subr interactively
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
414 ;; if the advised function was called interactively, because the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
415 ;; interactive specification of subrs is not accessible. This is the only
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
416 ;; case where changing the values of arguments will not have an affect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
417 ;; because they will be reset by the interactive specification of the subr.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
418 ;; If this is a problem one can always specify an interactive form in a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
419 ;; before/around/after advice to gain control over argument values that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
420 ;; were supplied interactively.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
421 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
422 ;; Then the body forms of the various advices in the various classes of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
423 ;; are assembled in order. The forms of around advice L are normally part of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
424 ;; one of the forms of around advice L-1. An around advice can specify where
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
425 ;; the forms of the wrapped or surrounded forms should go with the special
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
426 ;; keyword `ad-do-it', which will be substituted with a `progn' containing the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
427 ;; forms of the surrounded code.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
428
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
429 ;; The innermost part of the around advice onion is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
430 ;; <apply original definition to <arglist>>
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
431 ;; whose form depends on the type of the original function. The variable
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
432 ;; `ad-return-value' will be set to its result. This variable is visible to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
433 ;; all pieces of advice which can access and modify it before it gets returned.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
434 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
435 ;; The semantic structure of advised functions that contain protected pieces
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
436 ;; of advice is the same. The only difference is that `unwind-protect' forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
437 ;; make sure that the protected advice gets executed even if some previous
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
438 ;; piece of advice had an error or a non-local exit. If any around advice is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
439 ;; protected then the whole around advice onion will be protected.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
440
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
441 ;; @@ Argument access in advised functions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
442 ;; ========================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
443 ;; As already mentioned, the simplest way to access the arguments of an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
444 ;; advised function in the body of an advice is to refer to them by name. To
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
445 ;; do that, the advice programmer needs to know either the names of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
446 ;; argument variables of the original function, or the names used in the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
447 ;; argument list redefinition given in a piece of advice. While this simple
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
448 ;; method might be sufficient in many cases, it has the disadvantage that it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
449 ;; is not very portable because it hardcodes the argument names into the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
450 ;; advice. If the definition of the original function changes the advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
451 ;; might break even though the code might still be correct. Situations like
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
452 ;; that arise, for example, if one advises a subr like `eval-region' which
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
453 ;; gets redefined in a non-advice style into a function by the edebug
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
454 ;; package. If the advice assumes `eval-region' to be a subr it might break
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
455 ;; once edebug is loaded. Similar situations arise when one wants to use the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
456 ;; same piece of advice across different versions of Emacs. Some subrs in a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
457 ;; v18 Emacs are functions in v19 and vice versa, but for the most part the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
458 ;; semantics remain the same, hence, the same piece of advice might be usable
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
459 ;; in both Emacs versions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
460
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
461 ;; As a solution to that advice provides argument list access macros that get
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
462 ;; translated into the proper access forms at activation time, i.e., when the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
463 ;; advised definition gets constructed. Access macros access actual arguments
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
464 ;; by position regardless of how these actual argument get distributed onto
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
465 ;; the argument variables of a function. The rational behind this is that in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
466 ;; Emacs Lisp the semantics of an argument is strictly determined by its
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
467 ;; position (there are no keyword arguments).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
468
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
469 ;; Suppose the function `foo' is defined as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
470 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
471 ;; (defun foo (x y &optional z &rest r) ....)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
472 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
473 ;; and is then called with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
474 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
475 ;; (foo 0 1 2 3 4 5 6)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
476
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
477 ;; which means that X=0, Y=1, Z=2 and R=(3 4 5 6). The assumption is that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
478 ;; the semantics of an actual argument is determined by its position. It is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
479 ;; this semantics that has to be known by the advice programmer. Then s/he
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
480 ;; can access these arguments in a piece of advice with some of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
481 ;; following macros (the arrows indicate what value they will return):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
482
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
483 ;; (ad-get-arg 0) -> 0
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
484 ;; (ad-get-arg 1) -> 1
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
485 ;; (ad-get-arg 2) -> 2
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
486 ;; (ad-get-arg 3) -> 3
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
487 ;; (ad-get-args 2) -> (2 3 4 5 6)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
488 ;; (ad-get-args 4) -> (4 5 6)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
489
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
490 ;; `(ad-get-arg <position>)' will return the actual argument that was supplied
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
491 ;; at <position>, `(ad-get-args <position>)' will return the list of actual
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
492 ;; arguments supplied starting at <position>. Note that these macros can be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
493 ;; used without any knowledge about the form of the actual argument list of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
494 ;; the original function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
495
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
496 ;; Similarly, `(ad-set-arg <position> <value-form>)' can be used to set the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
497 ;; value of the actual argument at <position> to <value-form>. For example,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
498 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
499 ;; (ad-set-arg 5 "five")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
500 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
501 ;; will have the effect that R=(3 4 "five" 6) once the original function is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
502 ;; called. `(ad-set-args <position> <value-list-form>)' can be used to set
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
503 ;; the list of actual arguments starting at <position> to <value-list-form>.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
504 ;; For example,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
505 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
506 ;; (ad-set-args 0 '(5 4 3 2 1 0))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
507 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
508 ;; will have the effect that X=5, Y=4, Z=3 and R=(2 1 0) once the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
509 ;; function is called.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
510
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
511 ;; All these access macros are text macros rather than real Lisp macros. When
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
512 ;; the advised definition gets constructed they get replaced with actual access
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
513 ;; forms depending on the argument list of the advised function, i.e., after
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
514 ;; that argument access is in most cases as efficient as using the argument
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
515 ;; variable names directly.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
516
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
517 ;; @@@ Accessing argument bindings of arbitrary functions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
518 ;; =======================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
519 ;; Some functions (such as `trace-function' defined in trace.el) need a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
520 ;; method of accessing the names and bindings of the arguments of an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
521 ;; arbitrary advised function. To do that within an advice one can use the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
522 ;; special keyword `ad-arg-bindings' which is a text macro that will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
523 ;; substituted with a form that will evaluate to a list of binding
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
524 ;; specifications, one for every argument variable. These binding
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
525 ;; specifications can then be examined in the body of the advice. For
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
526 ;; example, somewhere in an advice we could do this:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
527 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
528 ;; (let* ((bindings ad-arg-bindings)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
529 ;; (firstarg (car bindings))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
530 ;; (secondarg (car (cdr bindings))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
531 ;; ;; Print info about first argument
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
532 ;; (print (format "%s=%s (%s)"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
533 ;; (ad-arg-binding-field firstarg 'name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
534 ;; (ad-arg-binding-field firstarg 'value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
535 ;; (ad-arg-binding-field firstarg 'type)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
536 ;; ....)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
537 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
538 ;; The `type' of an argument is either `required', `optional' or `rest'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
539 ;; Wherever `ad-arg-bindings' appears a form will be inserted that evaluates
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
540 ;; to the list of bindings, hence, in order to avoid multiple unnecessary
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
541 ;; evaluations one should always bind it to some variable.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
542
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
543 ;; @@@ Argument list mapping:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
544 ;; ==========================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
545 ;; Because `defadvice' allows the specification of the argument list of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
546 ;; advised function we need a mapping mechanism that maps this argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
547 ;; onto that of the original function. For example, somebody might specify
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
548 ;; `(sym newdef)' as the argument list of `fset', while advice might use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
549 ;; `(&rest ad-subr-args)' as the argument list of the original function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
550 ;; (depending on what Emacs version is used). Hence SYM and NEWDEF have to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
551 ;; be properly mapped onto the &rest variable when the original definition is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
552 ;; called. Advice automatically takes care of that mapping, hence, the advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
553 ;; programmer can specify an argument list without having to know about the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
554 ;; exact structure of the original argument list as long as the new argument
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
555 ;; list takes a compatible number/magnitude of actual arguments.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
556
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
557 ;; @@@ Definition of subr argument lists:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
558 ;; ======================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
559 ;; When advice constructs the advised definition of a function it has to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
560 ;; know the argument list of the original function. For functions and macros
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
561 ;; the argument list can be determined from the actual definition, however,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
562 ;; for subrs there is no such direct access available. In Lemacs and for some
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
563 ;; subrs in GNU Emacs-19 the argument list of a subr can be determined from
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
564 ;; its documentation string, in a v18 Emacs even that is not possible. If
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
565 ;; advice cannot at all determine the argument list of a subr it uses
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
566 ;; `(&rest ad-subr-args)' which will always work but is inefficient because
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
567 ;; it conses up arguments. The macro `ad-define-subr-args' can be used by
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
568 ;; the advice programmer to explicitly tell advice about the argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
569 ;; of a certain subr, for example,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
570 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
571 ;; (ad-define-subr-args 'fset '(sym newdef))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
572 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
573 ;; is used by advice itself to tell a v18 Emacs about the arguments of `fset'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
574 ;; The following can be used to undo such a definition:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
575 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
576 ;; (ad-undefine-subr-args 'fset)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
577 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
578 ;; The argument list definition is stored on the property list of the subr
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
579 ;; name symbol. When an argument list could be determined from the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
580 ;; documentation string it will be cached under that property. The general
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
581 ;; mechanism for looking up the argument list of a subr is the following:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
582 ;; 1) look for a definition stored on the property list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
583 ;; 2) if that failed try to infer it from the documentation string and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
584 ;; if successful cache it on the property list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
585 ;; 3) otherwise use `(&rest ad-subr-args)'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
586
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
587 ;; @@ Activation and deactivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
588 ;; ===============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
589 ;; The definition of an advised function does not change until all its advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
590 ;; gets actually activated. Activation can either happen with the `activate'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
591 ;; flag specified in the `defadvice', with an explicit call or interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
592 ;; invocation of `ad-activate', or if forward advice is enabled (i.e., the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
593 ;; value of `ad-activate-on-definition' is t) at the time an already advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
594 ;; function gets defined.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
595
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
596 ;; When a function gets first activated its original definition gets saved,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
597 ;; all defined and enabled pieces of advice will get combined with the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
598 ;; original definition, the resulting definition might get compiled depending
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
599 ;; on some conditions described below, and then the function will get
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
600 ;; redefined with the advised definition. This also means that undefined
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
601 ;; functions cannot get activated even though they might be already advised.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
602
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
603 ;; The advised definition will get compiled either if `ad-activate' was called
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
604 ;; interactively with a prefix argument, or called explicitly with its second
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
605 ;; argument as t, or, if this was a case of forward advice if the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
606 ;; definition of the function was compiled. If the advised definition was
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
607 ;; constructed during "preactivation" (see below) then that definition will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
608 ;; be already compiled because it was constructed during byte-compilation of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
609 ;; the file that contained the `defadvice' with the `preactivate' flag.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
610
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
611 ;; `ad-deactivate' can be used to back-define an advised function to its
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
612 ;; original definition. It can be called interactively or directly. Because
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
613 ;; `ad-activate' caches the advised definition the function can be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
614 ;; reactivated via `ad-activate' with only minor overhead (it is checked
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
615 ;; whether the current advice state is consistent with the cached
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
616 ;; definition, see the section on caching below).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
617
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
618 ;; `ad-activate-regexp' and `ad-deactivate-regexp' can be used to de/activate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
619 ;; all currently advised function that have a piece of advice with a name that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
620 ;; contains a match for a regular expression. These functions can be used to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
621 ;; de/activate sets of functions depending on certain advice naming
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
622 ;; conventions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
623
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
624 ;; Finally, `ad-activate-all' and `ad-deactivate-all' can be used to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
625 ;; de/activate all currently advised functions. These are useful to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
626 ;; (temporarily) return to an un/advised state.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
627
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
628 ;; @@@ Reasons for the separation of advice definition and activation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
629 ;; ===================================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
630 ;; As already mentioned, advising happens in two stages:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
631
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
632 ;; 1) definition of various pieces of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
633 ;; 2) activation of all advice currently defined and enabled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
634
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
635 ;; The advantage of this is that various pieces of advice can be defined
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
636 ;; before they get combined into an advised definition which avoids
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
637 ;; unnecessary constructions of intermediate advised definitions. The more
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
638 ;; important advantage is that it allows the implementation of forward advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
639 ;; Advice information for a certain function accumulates as the value of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
640 ;; `advice-info' property of the function symbol. This accumulation is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
641 ;; completely independent of the fact that that function might not yet be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
642 ;; defined. The special forms `defun' and `defmacro' have been advised to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
643 ;; check whether the function/macro they defined had advice information
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
644 ;; associated with it. If so and forward advice is enabled, the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
645 ;; definition will be saved, and then the advice will be activated. When a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
646 ;; file is loaded in a v18 Emacs the functions/macros it defines are also
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
647 ;; defined with calls to `defun/defmacro'. Hence, we can forward advise
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
648 ;; functions/macros which will be defined later during a load/autoload of some
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
649 ;; file (for compiled files generated by jwz's byte-compiler in a v19 Emacs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
650 ;; this is slightly more complicated but the basic idea is the same).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
651
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
652 ;; @@ Enabling/disabling pieces or sets of advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
653 ;; ===============================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
654 ;; A major motivation for the development of this advice package was to bring
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
655 ;; a little bit more structure into the function overloading chaos in Emacs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
656 ;; Lisp. Many packages achieve some of their functionality by adding a little
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
657 ;; bit (or a lot) to the standard functionality of some Emacs Lisp function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
658 ;; ange-ftp is a very popular package that achieves its magic by overloading
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
659 ;; most Emacs Lisp functions that deal with files. A popular function that's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
660 ;; overloaded by many packages is `expand-file-name'. The situation that one
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
661 ;; function is multiply overloaded can arise easily.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
662
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
663 ;; Once in a while it would be desirable to be able to disable some/all
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
664 ;; overloads of a particular package while keeping all the rest. Ideally -
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
665 ;; at least in my opinion - these overloads would all be done with advice,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
666 ;; I know I am dreaming right now... In that ideal case the enable/disable
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
667 ;; mechanism of advice could be used to achieve just that.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
668
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
669 ;; Every piece of advice is associated with an enablement flag. When the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
670 ;; advised definition of a particular function gets constructed (e.g., during
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
671 ;; activation) only the currently enabled pieces of advice will be considered.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
672 ;; This mechanism allows one to have different "views" of an advised function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
673 ;; dependent on what pieces of advice are currently enabled.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
674
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
675 ;; Another motivation for this mechanism is that it allows one to define a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
676 ;; piece of advice for some function yet keep it dormant until a certain
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
677 ;; condition is met. Until then activation of the function will not make use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
678 ;; of that piece of advice. Once the condition is met the advice can be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
679 ;; enabled and a reactivation of the function will add its functionality as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
680 ;; part of the new advised definition. For example, the advices of `defun'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
681 ;; etc. used by advice itself will stay disabled until `ad-start-advice' is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
682 ;; called and some variables have the proper values. Hence, if somebody
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
683 ;; else advised these functions too and activates them the advices defined
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
684 ;; by advice will get used only if they are intended to be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
685
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
686 ;; The main interface to this mechanism are the interactive functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
687 ;; `ad-enable-advice' and `ad-disable-advice'. For example, the following
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
688 ;; would disable a particular advice of the function `foo':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
689 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
690 ;; (ad-disable-advice 'foo 'before 'my-advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
691 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
692 ;; This call by itself only changes the flag, to get the proper effect in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
693 ;; the advised definition too one has to activate `foo' with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
694 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
695 ;; (ad-activate 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
696 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
697 ;; or interactively. To disable whole sets of advices one can use a regular
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
698 ;; expression mechanism. For example, let us assume that ange-ftp actually
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
699 ;; used advice to overload all its functions, and that it used the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
700 ;; "ange-ftp-" prefix for all its advice names, then we could temporarily
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
701 ;; disable all its advices with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
702 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
703 ;; (ad-disable-regexp "^ange-ftp-")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
704 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
705 ;; and the following call would put that actually into effect:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
706 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
707 ;; (ad-activate-regexp "^ange-ftp-")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
708 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
709 ;; A saver way would have been to use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
710 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
711 ;; (ad-update-regexp "^ange-ftp-")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
712 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
713 ;; instead which would have only reactivated currently actively advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
714 ;; functions, but not functions that were currently deactivated. All these
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
715 ;; functions can also be called interactively.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
716
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
717 ;; A certain piece of advice is considered a match if its name contains a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
718 ;; match for the regular expression. To enable ange-ftp again we would use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
719 ;; `ad-enable-regexp' and then activate or update again.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
720
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
721 ;; @@ Forward advice, function definition hooks:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
722 ;; =============================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
723 ;; Because most Emacs Lisp packages are loaded on demand via an autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
724 ;; mechanism it is essential to be able to "forward advise" functions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
725 ;; Otherwise, proper advice definition and activation would make it necessary
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
726 ;; to preload every file that defines a certain function before it can be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
727 ;; advised, which would partly defeat the purpose of the advice mechanism.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
728
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
729 ;; In the following, "forward advice" always implies its automatic activation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
730 ;; once a function gets defined, and not just the accumulation of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
731 ;; information for a possibly undefined function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
732
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
733 ;; Advice implements forward advice mainly via the following: 1) Separation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
734 ;; of advice definition and activation that makes it possible to accumulate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
735 ;; advice information without having the original function already defined,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
736 ;; 2) special versions of the function defining functions `defun', `defmacro'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
737 ;; and `fset' that check for advice information whenever they define a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
738 ;; function. If advice information was found and forward advice is enabled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
739 ;; then the advice will immediately get activated when the function gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
740 ;; defined.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
741
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
742 ;; @@@ Enabling forward advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
743 ;; ============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
744 ;; Forward advice is enabled by setting `ad-activate-on-definition' to t
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
745 ;; and then calling `ad-start-advice' which can either be done interactively,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
746 ;; directly with `(ad-start-advice)' in your .emacs, or by setting
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
747 ;; `ad-start-advice-on-load' to t before advice gets loaded. For example,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
748 ;; putting the following into your .emacs will enable forward advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
749 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
750 ;; (setq ad-start-advice-on-load t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
751 ;; (setq ad-activate-on-definition t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
752 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
753 ;; "Activation on definition" means, that whenever a function gets defined
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
754 ;; with either `defun', `defmacro', `fset' or by loading a byte-compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
755 ;; file, and the function has some advice-info stored with it then that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
756 ;; advice will get activated right away.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
757
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
758 ;; If jwz's byte-compiler is used then `ad-use-jwz-byte-compiler' should
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
759 ;; be t in order to make forward advice work with functions defined in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
760 ;; compiled files generated by that compiler. In v19s which use this
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
761 ;; compiler the value of this variable will be correct automatically.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
762 ;; If you use a v18 Emacs in conjunction with jwz's compiler and you want
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
763 ;; to use forward advice then you should check its value after loading
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
764 ;; advice. If it is nil set it explicitly with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
765 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
766 ;; (setq ad-use-jwz-byte-compiler t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
767 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
768 ;; along with `ad-activate-on-definition' before you start advice (see above).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
769
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
770 ;; IMPORTANT: A v18 Emacs + jwz's compiler + forward advice means performance
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
771 ;; tradeoffs which are described below.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
772
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
773 ;; @@@ Forward advice with compiled files generated by jwz's byte-compiler:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
774 ;; ========================================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
775 ;; The v18 byte-compiler only uses `defun/defmacro' to define compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
776 ;; functions, hence, providing advised versions of these functions was
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
777 ;; sufficient to achieve forward advice. With the advent of Jamie Zawinski's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
778 ;; optimizing byte-compiler which is now standardly used in GNU Emacs-19 and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
779 ;; Lemacs things became more complicated. jwz's compiler defines functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
780 ;; in hunks of byte-code without explicit usage of `defun/defmacro'. To
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
781 ;; still provide forward advice even in this scenario, advice defines an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
782 ;; advised version of the `byte-code' subr that scans its arguments for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
783 ;; function definitions during the loading of compiled files. While this is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
784 ;; no problem in a v19 Emacs, because it uses a new datatype for compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
785 ;; code objects and the `byte-code' subr is only rarely used at all, it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
786 ;; presents a major problem in a v18 Emacs because there calls to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
787 ;; `byte-code' are the only means of executing compiled code (every body of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
788 ;; a compiled function contains a call to `byte-code'). Because the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
789 ;; `byte-code' has to perform some extra checks every call to a compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
790 ;; function becomes more expensive.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
791
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
792 ;; Enabling forward advice leads to performance degradation in the following
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
793 ;; situations:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
794 ;; - A v18 Emacs is used and the value of `ad-use-jwz-byte-compiler' is t
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
795 ;; (either because jwz's byte-compiler is used instead of the standard v18
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
796 ;; compiler, or some compiled files generated by jwz's compiler are used).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
797 ;; - A v19 Emacs is used with some old-style v18 compiled files.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
798 ;; Some performance experiments I conducted showed that function call intensive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
799 ;; code (such as the highly recursive byte-compiler itself) slows down by a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
800 ;; factor of 1.8. Function call intensive code that runs while a file gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
801 ;; loaded can slow down by a factor of 6! For the v19 scenario this performance
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
802 ;; lossage would only apply to code that was loaded from old v18 compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
803 ;; files.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
804
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
805 ;; MORAL: If you use a v18 Emacs in conjunction with jwz's byte-compiler you
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
806 ;; should think twice whether you really need forward advice. There are some
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
807 ;; alternatives to forward advice described below that might give you what
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
808 ;; you need without the loss of performance (that performance loss probably
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
809 ;; outweighs by far any performance gain due to the optimizing nature of jwz's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
810 ;; compiler).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
811
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
812 ;; @@@ Alternatives to automatic activation of forward advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
813 ;; ===========================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
814 ;; If you use a v18 Emacs in conjunction with jwz's compiler, or you simply
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
815 ;; don't trust the automatic activation mechanism of forward advice, then
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
816 ;; you can use some of the following alternatives to get around that:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
817 ;; - Preload the file that contains the definition of the function that you
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
818 ;; want to advice. Inelegant and wasteful, but it works.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
819 ;; - If the package that contains the definition of the function you want to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
820 ;; advise has any mode hooks, and the advised function is only used once such
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
821 ;; a mode has been entered, then you can activate the advice in the mode
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
822 ;; hook. Just put a form like `(ad-activate 'my-advised-fn t)' into the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
823 ;; hook definition. The caching mechanism will reuse advised definitions,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
824 ;; so calling that mode hook over and over again will not construct
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
825 ;; advised definitions over and over again, so you won't loose any
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
826 ;; performance.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
827 ;; - If your Emacs comes with file load hooks (such as v19's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
828 ;; `after-load-alist' mechanism), then you can put the activation form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
829 ;; into that, for example, add `("myfile" (ad-activate 'my-advised-fn t))'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
830 ;; to it to activate the advice right ater "myfile" got loaded.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
831
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
832 ;; @@@ Function definition hooks:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
833 ;; ==============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
834 ;; Automatic activation of forward advice is implemented as an application
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
835 ;; of a more general function definition hook mechanism. After a function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
836 ;; gets re/defined with `defun/defmacro/fset' or via a hunk of byte-code
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
837 ;; during the loading of a byte-compiled file, and function definition hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
838 ;; are enabled, then all hook functions stored in `ad-definition-hooks' are
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
839 ;; run with the variable `ad-defined-function' bound to the name of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
840 ;; currently defined function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
841
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
842 ;; Function definition hooks can be enabled with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
843 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
844 ;; (setq ad-enable-definition-hooks t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
845 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
846 ;; before advice gets started with `ad-start-advice'. Setting
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
847 ;; `ad-activate-on-definition' to t automatically enables definition hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
848 ;; regardless of the value of `ad-enable-definition-hooks'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
849
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
850 ;; @@@ Wish list:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
851 ;; ==============
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
852 ;; - The implementation of definition hooks for v19 compiled files would be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
853 ;; safer if jwz's byte-compiler used something like `byte-code-tl' instead
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
854 ;; of `byte-code' to execute hunks of function defining byte-code at the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
855 ;; top level of compiled files.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
856 ;; - Definition hooks should be implemented directly as part of the C-code
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
857 ;; that implements `fset', because then advice.el wouldn't have to use all
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
858 ;; these dirty hacks to achieve this functionality.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
859
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
860 ;; @@ Caching of advised definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
861 ;; ==================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
862 ;; After an advised definition got constructed it gets cached as part of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
863 ;; advised function's advice-info so it can be reused, for example, after an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
864 ;; intermediate deactivation. Because the advice-info of a function might
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
865 ;; change between the time of caching and reuse a cached definition gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
866 ;; a cache-id associated with it so it can be verified whether the cached
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
867 ;; definition is still valid (the main application of this is preactivation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
868 ;; - see below).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
869
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
870 ;; When an advised function gets activated and a verifiable cached definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
871 ;; is available, then that definition will be used instead of creating a new
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
872 ;; advised definition from scratch. If you want to make sure that a new
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
873 ;; definition gets constructed then you should use `ad-clear-cache' before you
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
874 ;; activate the advised function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
875
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
876 ;; @@ Preactivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
877 ;; =================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
878 ;; Constructing an advised definition is moderately expensive. In a situation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
879 ;; where one package defines a lot of advised functions it might be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
880 ;; prohibitively expensive to do all the advised definition construction at
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
881 ;; runtime. Preactivation is a mechanism that allows compile-time construction
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
882 ;; of compiled advised definitions that can be activated cheaply during
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
883 ;; runtime. Preactivation uses the caching mechanism to do that. Here's how it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
884 ;; works:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
885
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
886 ;; When the byte-compiler compiles a `defadvice' that has the `preactivate'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
887 ;; flag specified, it uses the current original definition of the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
888 ;; function plus the advice specified in this `defadvice' (even if it is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
889 ;; specified as disabled) and all other currently enabled pieces of advice to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
890 ;; construct an advised definition and an identifying cache-id and makes them
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
891 ;; part of the `defadvice' expansion which will then be compiled by the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
892 ;; byte-compiler (to ensure that in a v18 emacs you have to put the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
893 ;; `defadvice' inside a `defun' to get it compiled and then you have to call
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
894 ;; that compiled `defun' in order to actually execute the `defadvice'). When
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
895 ;; the file with the compiled, preactivating `defadvice' gets loaded the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
896 ;; precompiled advised definition will be cached on the advised function's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
897 ;; advice-info. When it gets activated (can be immediately on execution of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
898 ;; `defadvice' or any time later) the cache-id gets checked against the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
899 ;; current state of advice and if it is verified the precompiled definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
900 ;; will be used directly (the verification is pretty cheap). If it couldn't get
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
901 ;; verified a new advised definition for that function will be built from
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
902 ;; scratch, hence, the efficiency added by the preactivation mechanism does
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
903 ;; not at all impair the flexibility of the advice mechanism.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
904
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
905 ;; MORAL: In order get all the efficiency out of preactivation the advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
906 ;; state of an advised function at the time the file with the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
907 ;; preactivating `defadvice' gets byte-compiled should be exactly
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
908 ;; the same as it will be when the advice of that function gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
909 ;; actually activated. If it is not there is a high chance that the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
910 ;; cache-id will not match and hence a new advised definition will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
911 ;; have to be constructed at runtime.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
912
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
913 ;; Preactivation and forward advice do not contradict each other. It is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
914 ;; perfectly ok to load a file with a preactivating `defadvice' before the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
915 ;; original definition of the advised function is available. The constructed
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
916 ;; advised definition will be used once the original function gets defined and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
917 ;; its advice gets activated. The only constraint is that at the time the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
918 ;; file with the preactivating `defadvice' got compiled the original function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
919 ;; definition was available.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
920
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
921 ;; TIPS: Here are some indications that a preactivation did not work the way
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
922 ;; you intended it to work:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
923 ;; - Activation of the advised function takes longer than usual/expected
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
924 ;; - The byte-compiler gets loaded while an advised function gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
925 ;; activated
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
926 ;; - `byte-compile' is part of the `features' variable even though you
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
927 ;; did not use the byte-compiler
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
928 ;; Right now advice does not provide an elegant way to find out whether
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
929 ;; and why a preactivation failed. What you can do is to trace the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
930 ;; function `ad-cache-id-verification-code' (with the function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
931 ;; `trace-function-background' defined in my trace.el package) before
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
932 ;; any of your advised functions get activated. After they got
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
933 ;; activated check whether all calls to `ad-cache-id-verification-code'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
934 ;; returned `verified' as a result. Other values indicate why the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
935 ;; verification failed which should give you enough information to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
936 ;; fix your preactivation/compile/load/activation sequence.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
937
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
938 ;; IMPORTANT: There is one case (that I am aware of) that can make
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
939 ;; preactivation fail, i.e., a preconstructed advised definition that does
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
940 ;; NOT match the current state of advice gets used nevertheless. That case
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
941 ;; arises if one package defines a certain piece of advice which gets used
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
942 ;; during preactivation, and another package incompatibly redefines that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
943 ;; very advice (i.e., same function/class/name), and it is the second advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
944 ;; that is available when the preconstructed definition gets activated, and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
945 ;; that was the only definition of that advice so far (`ad-add-advice'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
946 ;; catches advice redefinitions and clears the cache in such a case).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
947 ;; Catching that would make the cache verification too expensive.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
948
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
949 ;; MORAL-II: Redefining somebody else's advice is BAAAAD (to speak with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
950 ;; George Walker Bush), and why would you redefine your own advice anyway?
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
951 ;; Advice is a mechanism to facilitate function redefinition, not advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
952 ;; redefinition (wait until I write meta-advice.el :-). If you really have
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
953 ;; to undo somebody else's advice try to write a "neutralizing" advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
954
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
955 ;; @@ Advising macros and special forms and other dangerous things:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
956 ;; ================================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
957 ;; Look at the corresponding tutorial sections for more information on
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
958 ;; these topics. Here it suffices to point out that the special treatment
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
959 ;; of macros and special forms by the byte-compiler can lead to problems
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
960 ;; when they get advised. Macros can create problems because they get
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
961 ;; expanded at compile time, hence, they might not have all the necessary
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
962 ;; runtime support and such advice cannot be de/activated or changed as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
963 ;; it is possible for functions. Special forms create problems because they
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
964 ;; have to be advised "into" macros, i.e., an advised special form is a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
965 ;; implemented as a macro, hence, in most cases the byte-compiler will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
966 ;; not recognize it as a special form anymore which can lead to very strange
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
967 ;; results.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
968 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
969 ;; MORAL: - Only advise macros or special forms when you are absolutely sure
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
970 ;; what you are doing.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
971 ;; - As a safety measure, always do `ad-deactivate-all' before you
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
972 ;; byte-compile a file to make sure that even if some inconsiderate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
973 ;; person advised some special forms you'll get proper compilation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
974 ;; results. After compilation do `ad-activate-all' to get back to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
975 ;; the previous state.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
976
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
977 ;; @@ Adding a piece of advice with `ad-add-advice':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
978 ;; =================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
979 ;; The non-interactive function `ad-add-advice' can be used to add a piece of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
980 ;; advice to some function without using `defadvice'. This is useful if advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
981 ;; has to be added somewhere by a function (also look at `ad-make-advice').
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
982
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
983 ;; @@ Activation/deactivation advices, file load hooks:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
984 ;; ====================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
985 ;; There are two special classes of advice called `activation' and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
986 ;; `deactivation'. The body forms of these advices are not included into the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
987 ;; advised definition of a function, rather they are assembled into a hook
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
988 ;; form which will be evaluated whenever the advice-info of the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
989 ;; function gets activated or deactivated. One application of this mechanism
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
990 ;; is to define file load hooks for files that do not provide such hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
991 ;; (v19s already come with a general file-load-hook mechanism, v18s don't).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
992 ;; For example, suppose you want to print a message whenever `file-x' gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
993 ;; loaded, and suppose the last function defined in `file-x' is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
994 ;; `file-x-last-fn'. Then we can define the following advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
995 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
996 ;; (defadvice file-x-last-fn (activation file-x-load-hook)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
997 ;; "Executed whenever file-x is loaded"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
998 ;; (if load-in-progress (message "Loaded file-x")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
999 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1000 ;; This will constitute a forward advice for function `file-x-last-fn' which
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1001 ;; will get activated when `file-x' is loaded (only if forward advice is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1002 ;; enabled of course). Because there are no "real" pieces of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1003 ;; available for it, its definition will not be changed, but the activation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1004 ;; advice will be run during its activation which is equivalent to having a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1005 ;; file load hook for `file-x'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1006
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1007 ;; @@ Summary of main advice concepts:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1008 ;; ===================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1009 ;; - Definition:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1010 ;; A piece of advice gets defined with `defadvice' and added to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1011 ;; `advice-info' property of a function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1012 ;; - Enablement:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1013 ;; Every piece of advice has an enablement flag associated with it. Only
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1014 ;; enabled advices are considered during construction of an advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1015 ;; definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1016 ;; - Activation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1017 ;; Redefine an advised function with its advised definition. Constructs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1018 ;; an advised definition from scratch if no verifiable cached advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1019 ;; definition is available and caches it.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1020 ;; - Deactivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1021 ;; Back-define an advised function to its original definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1022 ;; - Update:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1023 ;; Reactivate an advised function but only if its advice is currently
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1024 ;; active. This can be used to bring all currently advised function up
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1025 ;; to date with the current state of advice without also activating
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1026 ;; currently deactivated functions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1027 ;; - Caching:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1028 ;; Is the saving of an advised definition and an identifying cache-id so
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1029 ;; it can be reused, for example, for activation after deactivation.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1030 ;; - Preactivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1031 ;; Is the construction of an advised definition according to the current
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1032 ;; state of advice during byte-compilation of a file with a preactivating
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1033 ;; `defadvice'. That advised definition can then rather cheaply be used
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1034 ;; during activation without having to construct an advised definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1035 ;; from scratch at runtime.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1036
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1037 ;; @@ Summary of interactive advice manipulation functions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1038 ;; ========================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1039 ;; The following interactive functions can be used to manipulate the state
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1040 ;; of advised functions (all of them support completion on function names,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1041 ;; advice classes and advice names):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1042
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1043 ;; - ad-activate to activate the advice of a FUNCTION
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1044 ;; - ad-deactivate to deactivate the advice of a FUNCTION
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1045 ;; - ad-update to activate the advice of a FUNCTION unless it was not
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1046 ;; yet activated or is currently deactivated.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1047 ;; - ad-unadvise deactivates a FUNCTION and removes all of its advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1048 ;; information, hence, it cannot be activated again
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1049 ;; - ad-recover tries to redefine a FUNCTION to its original definition and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1050 ;; discards all advice information (a low-level `ad-unadvise').
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1051 ;; Use only in emergencies.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1052
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1053 ;; - ad-remove-advice removes a particular piece of advice of a FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1054 ;; You still have to do call `ad-activate' or `ad-update' to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1055 ;; activate the new state of advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1056 ;; - ad-enable-advice enables a particular piece of advice of a FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1057 ;; - ad-disable-advice disables a particular piece of advice of a FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1058 ;; - ad-enable-regexp maps over all currently advised functions and enables
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1059 ;; every advice whose name contains a match for a regular
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1060 ;; expression.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1061 ;; - ad-disable-regexp disables matching advices.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1062
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1063 ;; - ad-activate-regexp activates all advised function with a matching advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1064 ;; - ad-deactivate-regexp deactivates all advised function with matching advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1065 ;; - ad-update-regexp updates all advised function with a matching advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1066 ;; - ad-activate-all activates all advised functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1067 ;; - ad-deactivate-all deactivates all advised functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1068 ;; - ad-update-all updates all advised functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1069 ;; - ad-unadvise-all unadvises all advised functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1070 ;; - ad-recover-all recovers all advised functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1071
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1072 ;; - ad-compile byte-compiles a function/macro if it is compilable.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1073
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1074 ;; @@ Summary of forms with special meanings when used within an advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1075 ;; =====================================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1076 ;; ad-return-value name of the return value variable (get/settable)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1077 ;; ad-subr-args name of &rest argument variable used for advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1078 ;; subrs whose actual argument list cannot be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1079 ;; determined (get/settable)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1080 ;; (ad-get-arg <pos>), (ad-get-args <pos>),
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1081 ;; (ad-set-arg <pos> <value>), (ad-set-args <pos> <value-list>)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1082 ;; argument access text macros to get/set the values of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1083 ;; actual arguments at a certain position
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1084 ;; ad-arg-bindings text macro that returns the actual names, values
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1085 ;; and types of the arguments as a list of bindings. The
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1086 ;; order of the bindings corresponds to the order of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1087 ;; arguments. The individual fields of every binding (name,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1088 ;; value and type) can be accessed with the function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1089 ;; `ad-arg-binding-field' (see example above).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1090 ;; ad-do-it text macro that identifies the place where the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1091 ;; or wrapped definition should go in an around advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1092
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1093
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1094 ;; @ Foo games: An advice tutorial
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1095 ;; ===============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1096 ;; The following tutorial was created in GNU Emacs 18.59. Left-justified
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1097 ;; s-expressions are input forms followed by one or more result forms.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1098 ;; First we have to start the advice magic:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1099 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1100 ;; (ad-start-advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1101 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1102 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1103 ;; We start by defining an innocent looking function `foo' that simply
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1104 ;; adds 1 to its argument X:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1105 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1106 ;; (defun foo (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1107 ;; "Add 1 to X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1108 ;; (1+ x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1109 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1110 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1111 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1112 ;; 4
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1113 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1114 ;; @@ Defining a simple piece of advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1115 ;; =====================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1116 ;; Now let's define the first piece of advice for `foo'. To do that we
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1117 ;; use the macro `defadvice' which takes a function name, a list of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1118 ;; specifiers and a list of body forms as arguments. The first element of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1119 ;; the advice specifiers is the class of the advice, the second is its name,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1120 ;; the third its position and the rest are some flags. The class of our
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1121 ;; first advice is `before', its name is `fg-add2', its position among the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1122 ;; currently defined before advices (none so far) is `first', and the advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1123 ;; will be `activate'ed immediately. Advice names are global symbols, hence,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1124 ;; the name space conventions used for function names should be applied. All
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1125 ;; advice names in this tutorial will be prefixed with `fg' for `Foo Games'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1126 ;; (because everybody has the right to be inconsistent all the function names
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1127 ;; used in this tutorial do NOT follow this convention).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1128 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1129 ;; In the body of an advice we can refer to the argument variables of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1130 ;; original function by name. Here we add 1 to X so the effect of calling
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1131 ;; `foo' will be to actually add 2. All of the advice definitions below only
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1132 ;; have one body form for simplicity, but there is no restriction to that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1133 ;; extent. Every piece of advice can have a documentation string which will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1134 ;; be combined with the documentation of the original function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1135 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1136 ;; (defadvice foo (before fg-add2 first activate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1137 ;; "Add 2 to X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1138 ;; (setq x (1+ x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1139 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1140 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1141 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1142 ;; 5
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1143 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1144 ;; @@ Specifying the position of an advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1145 ;; ========================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1146 ;; Now we define the second before advice which will cancel the effect of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1147 ;; the previous advice. This time we specify the position as 0 which is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1148 ;; equivalent to `first'. A number can be used to specify the zero-based
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1149 ;; position of an advice among the list of advices in the same class. This
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1150 ;; time we already have one before advice hence the position specification
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1151 ;; actually has an effect. So, after the following definition the position
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1152 ;; of the previous advice will be 1 even though we specified it with `first'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1153 ;; above, the reason for this is that the position argument is relative to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1154 ;; the currently defined pieces of advice which by now has changed.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1155 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1156 ;; (defadvice foo (before fg-cancel-add2 0 activate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1157 ;; "Again only add 1 to X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1158 ;; (setq x (1- x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1159 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1160 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1161 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1162 ;; 4
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1163 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1164 ;; @@ Redefining a piece of advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1165 ;; ================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1166 ;; Now we define an advice with the same class and same name but with a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1167 ;; different position. Defining an advice in a class in which an advice with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1168 ;; that name already exists is interpreted as a redefinition of that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1169 ;; particular advice, in which case the position argument will be ignored
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1170 ;; and the previous position of the redefined piece of advice is used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1171 ;; Advice flags can be specified with non-ambiguous initial substrings, hence,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1172 ;; from now on we'll use `act' instead of the verbose `activate'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1173 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1174 ;; (defadvice foo (before fg-cancel-add2 last act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1175 ;; "Again only add 1 to X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1176 ;; (setq x (1- x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1177 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1178 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1179 ;; @@ Assembly of advised documentation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1180 ;; =====================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1181 ;; The documentation strings of the various pieces of advice are assembled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1182 ;; in order which shows that advice `fg-cancel-add2' is still the first
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1183 ;; `before' advice even though we specified position `last' above:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1184 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1185 ;; (documentation 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1186 ;; "Add 1 to X.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1187 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1188 ;; This function is advised with the following advice(s):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1189 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1190 ;; fg-cancel-add2 (before):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1191 ;; Again only add 1 to X.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1192 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1193 ;; fg-add2 (before):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1194 ;; Add 2 to X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1195 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1196 ;; @@ Advising interactive behavior:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1197 ;; =================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1198 ;; We can make a function interactive (or change its interactive behavior)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1199 ;; by specifying an interactive form in one of the before or around
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1200 ;; advices (there could also be body forms in this advice). The particular
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1201 ;; definition always assigns 5 as an argument to X which gives us 6 as a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1202 ;; result when we call foo interactively:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1203 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1204 ;; (defadvice foo (before fg-inter last act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1205 ;; "Use 5 as argument when called interactively."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1206 ;; (interactive (list 5)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1207 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1208 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1209 ;; (call-interactively 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1210 ;; 6
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1211 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1212 ;; If more than one advice have an interactive declaration, then the one of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1213 ;; the advice with the smallest position will be used (before advices go
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1214 ;; before around and after advices), hence, the declaration below does
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1215 ;; not have any effect:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1216 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1217 ;; (defadvice foo (before fg-inter2 last act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1218 ;; (interactive (list 6)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1219 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1220 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1221 ;; (call-interactively 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1222 ;; 6
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1223 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1224 ;; Let's have a look at what the definition of `foo' looks like now
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1225 ;; (indentation added by hand for legibility):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1226 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1227 ;; (symbol-function 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1228 ;; (lambda (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1229 ;; "$ad-doc: foo$"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1230 ;; (interactive (list 5))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1231 ;; (let (ad-return-value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1232 ;; (setq x (1- x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1233 ;; (setq x (1+ x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1234 ;; (setq ad-return-value (ad-Orig-foo x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1235 ;; ad-return-value))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1236 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1237 ;; @@ Around advices:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1238 ;; ==================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1239 ;; Now we'll try some `around' advices. An around advice is a wrapper around
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1240 ;; the original definition. It can shadow or establish bindings for the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1241 ;; original definition, and it can look at and manipulate the value returned
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1242 ;; by the original function. The position of the special keyword `ad-do-it'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1243 ;; specifies where the code of the original function will be executed. The
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1244 ;; keyword can appear multiple times which will result in multiple calls of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1245 ;; the original function in the resulting advised code. Note, that if we don't
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1246 ;; specify a position argument (i.e., `first', `last' or a number), then
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1247 ;; `first' (or 0) is the default):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1248 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1249 ;; (defadvice foo (around fg-times-2 act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1250 ;; "First double X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1251 ;; (let ((x (* x 2)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1252 ;; ad-do-it))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1253 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1254 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1255 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1256 ;; 7
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1257 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1258 ;; Around advices are assembled like onion skins where the around advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1259 ;; with position 0 is the outermost skin and the advice at the last position
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1260 ;; is the innermost skin which is directly wrapped around the call of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1261 ;; original definition of the function. Hence, after the next `defadvice' we
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1262 ;; will first multiply X by 2 then add 1 and then call the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1263 ;; definition (i.e., add 1 again):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1264 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1265 ;; (defadvice foo (around fg-add-1 last act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1266 ;; "Add 1 to X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1267 ;; (let ((x (1+ x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1268 ;; ad-do-it))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1269 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1270 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1271 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1272 ;; 8
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1273 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1274 ;; Again, let's see what the definition of `foo' looks like so far:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1275 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1276 ;; (symbol-function 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1277 ;; (lambda (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1278 ;; "$ad-doc: foo$"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1279 ;; (interactive (list 5))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1280 ;; (let (ad-return-value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1281 ;; (setq x (1- x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1282 ;; (setq x (1+ x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1283 ;; (let ((x (* x 2)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1284 ;; (let ((x (1+ x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1285 ;; (setq ad-return-value (ad-Orig-foo x))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1286 ;; ad-return-value))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1287 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1288 ;; @@ Controlling advice activation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1289 ;; =================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1290 ;; In every `defadvice' so far we have used the flag `activate' to activate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1291 ;; the advice immediately after its definition, and that's what we want in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1292 ;; most cases. However, if we define multiple pieces of advice for a single
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1293 ;; function then activating every advice immediately is inefficient. A
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1294 ;; better way to do this is to only activate the last defined advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1295 ;; For example:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1296 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1297 ;; (defadvice foo (after fg-times-x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1298 ;; "Multiply the result with X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1299 ;; (setq ad-return-value (* ad-return-value x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1300 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1301 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1302 ;; This still yields the same result as before:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1303 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1304 ;; 8
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1305 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1306 ;; Now we define another advice and activate which will also activate the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1307 ;; previous advice `fg-times-x'. Note the use of the special variable
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1308 ;; `ad-return-value' in the body of the advice which is set to the result of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1309 ;; the original function. If we change its value then the value returned by
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1310 ;; the advised function will be changed accordingly:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1311 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1312 ;; (defadvice foo (after fg-times-x-again act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1313 ;; "Again multiply the result with X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1314 ;; (setq ad-return-value (* ad-return-value x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1315 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1316 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1317 ;; Now the advices have an effect:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1318 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1319 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1320 ;; 72
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1321 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1322 ;; @@ Protecting advice execution:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1323 ;; ===============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1324 ;; Once in a while we define an advice to perform some cleanup action,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1325 ;; for example:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1326 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1327 ;; (defadvice foo (after fg-cleanup last act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1328 ;; "Do some cleanup."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1329 ;; (print "Let's clean up now!"))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1330 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1331 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1332 ;; However, in case of an error the cleanup won't be performed:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1333 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1334 ;; (condition-case error
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1335 ;; (foo t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1336 ;; (error 'error-in-foo))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1337 ;; error-in-foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1338 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1339 ;; To make sure a certain piece of advice gets executed even if some error or
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1340 ;; non-local exit occurred in any preceding code, we can protect it by using
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1341 ;; the `protect' keyword. (if any of the around advices is protected then the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1342 ;; whole around advice onion will be protected):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1343 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1344 ;; (defadvice foo (after fg-cleanup prot act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1345 ;; "Do some protected cleanup."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1346 ;; (print "Let's clean up now!"))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1347 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1348 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1349 ;; Now the cleanup form will be executed even in case of an error:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1350 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1351 ;; (condition-case error
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1352 ;; (foo t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1353 ;; (error 'error-in-foo))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1354 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1355 ;; error-in-foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1356 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1357 ;; Again, let's see what `foo' looks like:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1358 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1359 ;; (symbol-function 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1360 ;; (lambda (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1361 ;; "$ad-doc: foo$"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1362 ;; (interactive (list 5))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1363 ;; (let (ad-return-value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1364 ;; (unwind-protect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1365 ;; (progn (setq x (1- x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1366 ;; (setq x (1+ x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1367 ;; (let ((x (* x 2)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1368 ;; (let ((x (1+ x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1369 ;; (setq ad-return-value (ad-Orig-foo x))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1370 ;; (setq ad-return-value (* ad-return-value x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1371 ;; (setq ad-return-value (* ad-return-value x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1372 ;; (print "Let's clean up now!"))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1373 ;; ad-return-value))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1374 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1375 ;; @@ Compilation of advised definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1376 ;; ======================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1377 ;; Finally, we can specify the `compile' keyword in a `defadvice' to say
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1378 ;; that we want the resulting advised function to be byte-compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1379 ;; (`compile' will be ignored unless we also specified `activate'):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1380 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1381 ;; (defadvice foo (after fg-cleanup prot act comp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1382 ;; "Do some protected cleanup."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1383 ;; (print "Let's clean up now!"))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1384 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1385 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1386 ;; Now `foo' is byte-compiled:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1387 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1388 ;; (symbol-function 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1389 ;; (lambda (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1390 ;; "$ad-doc: foo$"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1391 ;; (interactive (byte-code "....." [5] 1))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1392 ;; (byte-code "....." [ad-return-value x nil ((byte-code "....." [print "Let's clean up now!"] 2)) * 2 ad-Orig-foo] 6))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1393 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1394 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1395 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1396 ;; 72
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1397 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1398 ;; @@ Enabling and disabling pieces of advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1399 ;; ===========================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1400 ;; Once in a while it is desirable to temporarily disable a piece of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1401 ;; so that it won't be considered during activation, for example, if two
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1402 ;; different packages advise the same function and one wants to temporarily
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1403 ;; neutralize the effect of the advice of one of the packages.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1404 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1405 ;; The following disables the after advice `fg-times-x' in the function `foo'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1406 ;; All that does is to change a flag for this particular advice. All the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1407 ;; other information defining it will be left unchanged (e.g., its relative
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1408 ;; position in this advice class, etc.).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1409 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1410 ;; (ad-disable-advice 'foo 'after 'fg-times-x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1411 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1412 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1413 ;; For this to have an effect we have to activate `foo':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1414 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1415 ;; (ad-activate 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1416 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1417 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1418 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1419 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1420 ;; 24
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1421 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1422 ;; If we want to disable all multiplication advices in `foo' we can use a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1423 ;; regular expression that matches the names of such advices. Actually, any
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1424 ;; advice name that contains a match for the regular expression will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1425 ;; called a match. A special advice class `any' can be used to consider
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1426 ;; all advice classes:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1427 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1428 ;; (ad-disable-advice 'foo 'any "^fg-.*times")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1429 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1430 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1431 ;; (ad-activate 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1432 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1433 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1434 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1435 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1436 ;; 5
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1437 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1438 ;; To enable the disabled advice we could use either `ad-enable-advice'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1439 ;; similar to `ad-disable-advice', or as an alternative `ad-enable-regexp'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1440 ;; which will enable matching advices in ALL currently advised functions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1441 ;; Hence, this can be used to dis/enable advices made by a particular
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1442 ;; package to a set of functions as long as that package obeys standard
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1443 ;; advice name conventions. We prefixed all advice names with `fg-', hence
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1444 ;; the following will do the trick (`ad-enable-regexp' returns the number
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1445 ;; of matched advices):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1446 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1447 ;; (ad-enable-regexp "^fg-")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1448 ;; 9
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1449 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1450 ;; The following will activate all currently active advised functions that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1451 ;; contain some advice matched by the regular expression. This is a save
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1452 ;; way to update the activation of advised functions whose advice changed
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1453 ;; in some way or other without accidentally also activating currently
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1454 ;; deactivated functions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1455 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1456 ;; (ad-update-regexp "^fg-")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1457 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1458 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1459 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1460 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1461 ;; 72
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1462 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1463 ;; Another use for the dis/enablement mechanism is to define a piece of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1464 ;; and keep it "dormant" until a particular condition is satisfied, i.e., until
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1465 ;; then the advice will not be used during activation. The `disable' flag lets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1466 ;; one do that with `defadvice':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1467 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1468 ;; (defadvice foo (before fg-1-more dis)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1469 ;; "Add yet 1 more."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1470 ;; (setq x (1+ x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1471 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1472 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1473 ;; (ad-activate 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1474 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1475 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1476 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1477 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1478 ;; 72
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1479 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1480 ;; (ad-enable-advice 'foo 'before 'fg-1-more)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1481 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1482 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1483 ;; (ad-activate 'foo)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1484 ;; foo
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1485 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1486 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1487 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1488 ;; 160
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1489 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1490 ;; @@ Caching:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1491 ;; ===========
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1492 ;; Advised definitions get cached to allow efficient activation/deactivation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1493 ;; without having to reconstruct them if nothing in the advice-info of a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1494 ;; function has changed. The following idiom can be used to temporarily
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1495 ;; deactivate functions that have a piece of advice defined by a certain
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1496 ;; package (we save the old definition to check out caching):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1497 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1498 ;; (setq old-definition (symbol-function 'foo))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1499 ;; (lambda (x) ....)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1500 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1501 ;; (ad-deactivate-regexp "^fg-")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1502 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1503 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1504 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1505 ;; 4
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1506 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1507 ;; (ad-activate-regexp "^fg-")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1508 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1509 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1510 ;; (eq old-definition (symbol-function 'foo))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1511 ;; t
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1512 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1513 ;; (foo 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1514 ;; "Let's clean up now!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1515 ;; 160
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1516 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1517 ;; @@ Forward advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1518 ;; ==================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1519 ;; To enable automatic activation of forward advice we first have to set
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1520 ;; `ad-activate-on-definition' to t and restart advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1521 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1522 ;; (setq ad-activate-on-definition t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1523 ;; t
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1524 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1525 ;; (ad-start-advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1526 ;; (ad-activate-defined-function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1527 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1528 ;; Let's define a piece of advice for an undefined function:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1529 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1530 ;; (defadvice bar (before fg-sub-1-more act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1531 ;; "Subtract one more from X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1532 ;; (setq x (1- x)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1533 ;; bar
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1534 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1535 ;; `bar' is not yet defined:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1536 ;; (fboundp 'bar)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1537 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1538 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1539 ;; Now we define it and the forward advice will get activated (only because
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1540 ;; `ad-activate-on-definition' was t when we started advice above with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1541 ;; `ad-start-advice'):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1542 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1543 ;; (defun bar (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1544 ;; "Subtract 1 from X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1545 ;; (1- x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1546 ;; bar
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1547 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1548 ;; (bar 4)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1549 ;; 2
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1550 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1551 ;; Redefinition will activate any available advice if the value of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1552 ;; `ad-redefinition-action' is either `warn', `accept' or `discard':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1553 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1554 ;; (defun bar (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1555 ;; "Subtract 2 from X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1556 ;; (- x 2))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1557 ;; bar
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1558 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1559 ;; (bar 4)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1560 ;; 1
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1561 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1562 ;; @@ Preactivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1563 ;; =================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1564 ;; Constructing advised definitions is moderately expensive, hence, it is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1565 ;; desirable to have a way to construct them at byte-compile time.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1566 ;; Preactivation is a mechanism that allows one to do that.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1567 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1568 ;; (defun fie (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1569 ;; "Multiply X by 2."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1570 ;; (* x 2))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1571 ;; fie
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1572 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1573 ;; (defadvice fie (before fg-times-4 preact)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1574 ;; "Multiply X by 4."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1575 ;; (setq x (* x 2)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1576 ;; fie
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1577 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1578 ;; This advice did not affect `fie'...
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1579 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1580 ;; (fie 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1581 ;; 4
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1582 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1583 ;; ...but it constructed a cached definition that will be used once `fie' gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1584 ;; activated as long as its current advice state is the same as it was during
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1585 ;; preactivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1586 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1587 ;; (setq cached-definition (ad-get-cache-definition 'fie))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1588 ;; (lambda (x) ....)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1589 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1590 ;; (ad-activate 'fie)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1591 ;; fie
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1592 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1593 ;; (eq cached-definition (symbol-function 'fie))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1594 ;; t
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1595 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1596 ;; (fie 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1597 ;; 8
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1598 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1599 ;; If you put a preactivating `defadvice' into an elisp file that gets byte-
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1600 ;; compiled then the constructed advised definition will get compiled by
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1601 ;; the byte-compiler. For that to occur in a v18 emacs you have to put the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1602 ;; `defadvice' inside a `defun' because the v18 compiler does not compile
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1603 ;; top-level forms other than `defun' or `defmacro', for example,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1604 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1605 ;; (defun fg-defadvice-fum ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1606 ;; (defadvice fum (before fg-times-4 preact act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1607 ;; "Multiply X by 4."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1608 ;; (setq x (* x 2))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1609 ;; fg-defadvice-fum
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1610 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1611 ;; So far, no `defadvice' for `fum' got executed, but when we compile
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1612 ;; `fg-defadvice-fum' the `defadvice' will be expanded by the byte compiler.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1613 ;; In order for preactivation to be effective we have to have a proper
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1614 ;; definition of `fum' around at preactivation time, hence, we define it now:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1615 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1616 ;; (defun fum (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1617 ;; "Multiply X by 2."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1618 ;; (* x 2))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1619 ;; fum
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1620 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1621 ;; Now we compile the defining function which will construct an advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1622 ;; definition during expansion of the `defadvice', compile it and store it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1623 ;; as part of the compiled `fg-defadvice-fum':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1624 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1625 ;; (ad-compile-function 'fg-defadvice-fum)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1626 ;; (lambda nil (byte-code ...))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1627 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1628 ;; `fum' is still completely unaffected:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1629 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1630 ;; (fum 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1631 ;; 4
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1632 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1633 ;; (ad-get-advice-info 'fum)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1634 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1635 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1636 ;; (fg-defadvice-fum)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1637 ;; fum
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1638 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1639 ;; Now the advised version of `fum' is compiled because the compiled definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1640 ;; constructed during preactivation was used, even though we did not specify
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1641 ;; the `compile' flag:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1642 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1643 ;; (symbol-function 'fum)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1644 ;; (lambda (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1645 ;; "$ad-doc: fum$"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1646 ;; (byte-code "....." [ad-return-value x nil * 2 ad-Orig-fum] 4))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1647 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1648 ;; (fum 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1649 ;; 8
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1650 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1651 ;; A preactivated definition will only be used if it matches the current
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1652 ;; function definition and advice information. If it does not match it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1653 ;; will simply be discarded and a new advised definition will be constructed
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1654 ;; from scratch. For example, let's first remove all advice-info for `fum':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1655 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1656 ;; (ad-unadvise 'fum)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1657 ;; (("fie") ("bar") ("foo") ...)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1658 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1659 ;; And now define a new piece of advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1660 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1661 ;; (defadvice fum (before fg-interactive act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1662 ;; "Make fum interactive."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1663 ;; (interactive "nEnter x: "))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1664 ;; fum
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1665 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1666 ;; When we now try to use a preactivation it will not be used because the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1667 ;; current advice state is different from the one at preactivation time. This
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1668 ;; is no tragedy, everything will work as expected just not as efficient,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1669 ;; because a new advised definition has to be constructed from scratch:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1670 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1671 ;; (fg-defadvice-fum)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1672 ;; fum
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1673 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1674 ;; A new uncompiled advised definition got constructed:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1675 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1676 ;; (ad-compiled-p (symbol-function 'fum))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1677 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1678 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1679 ;; (fum 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1680 ;; 8
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1681 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1682 ;; MORAL: To get all the efficiency out of preactivation the function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1683 ;; definition and advice state at preactivation time must be the same as the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1684 ;; state at activation time. Preactivation does work with forward advice, all
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1685 ;; that's necessary is that the definition of the forward advised function is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1686 ;; available when the `defadvice' with the preactivation gets compiled.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1687 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1688 ;; @@ Portable argument access:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1689 ;; ============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1690 ;; So far, we always used the actual argument variable names to access an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1691 ;; argument in a piece of advice. For many advice applications this is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1692 ;; perfectly ok and keeps advices simple. However, it decreases portability
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1693 ;; of advices because it assumes specific argument variable names. For example,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1694 ;; if one advises a subr such as `eval-region' which then gets redefined by
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1695 ;; some package (e.g., edebug) into a function with different argument names,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1696 ;; then a piece of advice written for `eval-region' that was written with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1697 ;; the subr arguments in mind will break. Similar situations arise when one
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1698 ;; switches between major Emacs versions, e.g., certain subrs in v18 are
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1699 ;; functions in v19 and vice versa. Also, in v19s subr argument lists
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1700 ;; are available and will be used, while they are not available in v18.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1701 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1702 ;; Argument access text macros allow one to access arguments of an advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1703 ;; function in a portable way without having to worry about all these
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1704 ;; possibilities. These macros will be translated into the proper access forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1705 ;; at activation time, hence, argument access will be as efficient as if
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1706 ;; the arguments had been used directly in the definition of the advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1707 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1708 ;; (defun fuu (x y z)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1709 ;; "Add 3 numbers."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1710 ;; (+ x y z))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1711 ;; fuu
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1712 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1713 ;; (fuu 1 1 1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1714 ;; 3
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1715 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1716 ;; Argument access macros specify actual arguments at a certain position.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1717 ;; Position 0 access the first actual argument, position 1 the second etc.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1718 ;; For example, the following advice adds 1 to each of the 3 arguments:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1719 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1720 ;; (defadvice fuu (before fg-add-1-to-all act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1721 ;; "Adds 1 to all arguments."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1722 ;; (ad-set-arg 0 (1+ (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1723 ;; (ad-set-arg 1 (1+ (ad-get-arg 1)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1724 ;; (ad-set-arg 2 (1+ (ad-get-arg 2))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1725 ;; fuu
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1726 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1727 ;; (fuu 1 1 1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1728 ;; 6
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1729 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1730 ;; Now suppose somebody redefines `fuu' with a rest argument. Our advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1731 ;; will still work because we used access macros (note, that automatic
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1732 ;; advice activation is still in effect, hence, the redefinition of `fuu'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1733 ;; will automatically activate all its advice):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1734 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1735 ;; (defun fuu (&rest numbers)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1736 ;; "Add NUMBERS."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1737 ;; (apply '+ numbers))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1738 ;; fuu
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1739 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1740 ;; (fuu 1 1 1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1741 ;; 6
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1742 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1743 ;; (fuu 1 1 1 1 1 1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1744 ;; 9
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1745 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1746 ;; What's important to notice is that argument access macros access actual
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1747 ;; arguments regardless of how they got distributed onto argument variables.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1748 ;; In Emacs Lisp the semantics of an actual argument is determined purely
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1749 ;; by position, hence, as long as nobody changes the semantics of what a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1750 ;; certain actual argument at a certain position means the access macros
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1751 ;; will do the right thing.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1752 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1753 ;; Because of &rest arguments we need a second kind of access macro that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1754 ;; can access all actual arguments starting from a certain position:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1755 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1756 ;; (defadvice fuu (before fg-print-args act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1757 ;; "Print all arguments."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1758 ;; (print (ad-get-args 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1759 ;; fuu
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1760 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1761 ;; (fuu 1 2 3 4 5)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1762 ;; (1 2 3 4 5)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1763 ;; 18
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1764 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1765 ;; (defadvice fuu (before fg-set-args act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1766 ;; "Swaps 2nd and 3rd arg and discards all the rest."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1767 ;; (ad-set-args 1 (list (ad-get-arg 2) (ad-get-arg 1))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1768 ;; fuu
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1769 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1770 ;; (fuu 1 2 3 4 4 4 4 4 4)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1771 ;; (1 3 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1772 ;; 9
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1773 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1774 ;; (defun fuu (x y z)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1775 ;; "Add 3 numbers."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1776 ;; (+ x y z))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1777 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1778 ;; (fuu 1 2 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1779 ;; (1 3 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1780 ;; 9
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1781 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1782 ;; @@ Defining the argument list of an advised function:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1783 ;; =====================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1784 ;; Once in a while it might be desirable to advise a function and additionally
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1785 ;; give it an extra argument that controls the advised code, for example, one
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1786 ;; might want to make an interactive function sensitive to a prefix argument.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1787 ;; For such cases `defadvice' allows the specification of an argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1788 ;; for the advised function. Similar to the redefinition of interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1789 ;; behavior, the first argument list specification found in the list of before/
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1790 ;; around/after advices will be used. Of course, the specified argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1791 ;; should be downward compatible with the original argument list, otherwise
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1792 ;; functions that call the advised function with the original argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1793 ;; in mind will break.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1794 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1795 ;; (defun fii (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1796 ;; "Add 1 to X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1797 ;; (1+ x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1798 ;; fii
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1799 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1800 ;; Now we advise `fii' to use an optional second argument that controls the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1801 ;; amount of incrementation. A list following the (optional) position
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1802 ;; argument of the advice will be interpreted as an argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1803 ;; specification. This means you cannot specify an empty argument list, and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1804 ;; why would you want to anyway?
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1805 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1806 ;; (defadvice fii (before fg-inc-x (x &optional incr) act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1807 ;; "Increment X by INCR (default is 1)."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1808 ;; (setq x (+ x (1- (or incr 1)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1809 ;; fii
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1810 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1811 ;; (fii 3)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1812 ;; 4
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1813 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1814 ;; (fii 3 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1815 ;; 5
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1816 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1817 ;; @@ Specifying argument lists of subrs:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1818 ;; ======================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1819 ;; The argument lists of subrs cannot be determined directly from Lisp.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1820 ;; This means that advice.el has to use `(&rest ad-subr-args)' as the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1821 ;; argument list of the advised subr which is not very efficient. In Lemacs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1822 ;; subr argument lists can be determined from their documentation string, in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1823 ;; GNU Emacs-19 this is the case for some but not all subrs. To accommodate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1824 ;; for the cases where the argument lists cannot be determined (e.g., in a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1825 ;; v18 Emacs) advice.el comes with a specification mechanism that allows the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1826 ;; advice programmer to tell advice what the argument list of a certain subr
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1827 ;; really is.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1828 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1829 ;; In a v18 Emacs the following will return the &rest idiom:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1830 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1831 ;; (ad-arglist (symbol-function 'car))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1832 ;; (&rest ad-subr-args)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1833 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1834 ;; To tell advice what the argument list of `car' really is we
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1835 ;; can do the following:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1836 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1837 ;; (ad-define-subr-args 'car '(list))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1838 ;; ((list))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1839 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1840 ;; Now `ad-arglist' will return the proper argument list (this method is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1841 ;; actually used by advice itself for the advised definition of `fset'):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1842 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1843 ;; (ad-arglist (symbol-function 'car))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1844 ;; (list)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1845 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1846 ;; The defined argument list will be stored on the property list of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1847 ;; subr name symbol. When advice looks for a subr argument list it first
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1848 ;; checks for a definition on the property list, if that fails it tries
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1849 ;; to infer it from the documentation string and caches it on the property
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1850 ;; list if it was successful, otherwise `(&rest ad-subr-args)' will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1851 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1852 ;; @@ Advising interactive subrs:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1853 ;; ==============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1854 ;; For the most part there is no difference between advising functions and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1855 ;; advising subrs. There is one situation though where one might have to write
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1856 ;; slightly different advice code for subrs than for functions. This case
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1857 ;; arises when one wants to access subr arguments in a before/around advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1858 ;; when the arguments were determined by an interactive call to the subr.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1859 ;; Advice cannot determine what `interactive' form determines the interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1860 ;; behavior of the subr, hence, when it calls the original definition in an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1861 ;; interactive subr invocation it has to use `call-interactively' to generate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1862 ;; the proper interactive behavior. Thus up to that call the arguments of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1863 ;; interactive subr will be nil. For example, the following advice for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1864 ;; `kill-buffer' will not work in an interactive invocation...
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1865 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1866 ;; (defadvice kill-buffer (before fg-kill-buffer-hook first act preact comp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1867 ;; (my-before-kill-buffer-hook (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1868 ;; kill-buffer
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1869 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1870 ;; ...because the buffer argument will be nil in that case. The way out of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1871 ;; this dilemma is to provide an `interactive' specification that mirrors
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1872 ;; the interactive behavior of the unadvised subr, for example, the following
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1873 ;; will do the right thing even when `kill-buffer' is called interactively:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1874 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1875 ;; (defadvice kill-buffer (before fg-kill-buffer-hook first act preact comp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1876 ;; (interactive "bKill buffer: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1877 ;; (my-before-kill-buffer-hook (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1878 ;; kill-buffer
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1879 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1880 ;; @@ Advising macros:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1881 ;; ===================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1882 ;; Advising macros is slightly different because there are two significant
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1883 ;; time points in the invocation of a macro: Expansion and evaluation time.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1884 ;; For an advised macro instead of evaluating the original definition we
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1885 ;; use `macroexpand', that is, changing argument values and binding
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1886 ;; environments by pieces of advice has an affect during macro expansion
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1887 ;; but not necessarily during evaluation. In particular, any side effects
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1888 ;; of pieces of advice will occur during macro expansion. To also affect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1889 ;; the behavior during evaluation time one has to change the value of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1890 ;; `ad-return-value' in a piece of after advice. For example:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1891 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1892 ;; (defmacro foom (x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1893 ;; (` (list (, x))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1894 ;; foom
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1895 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1896 ;; (foom '(a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1897 ;; ((a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1898 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1899 ;; (defadvice foom (before fg-print-x act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1900 ;; "Print the value of X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1901 ;; (print x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1902 ;; foom
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1903 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1904 ;; The following works as expected because evaluation immediately follows
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1905 ;; macro expansion:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1906 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1907 ;; (foom '(a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1908 ;; (quote (a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1909 ;; ((a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1910 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1911 ;; However, the printing happens during expansion (or byte-compile) time:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1912 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1913 ;; (macroexpand '(foom '(a)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1914 ;; (quote (a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1915 ;; (list (quote (a)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1916 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1917 ;; If we want it to happen during evaluation time we have to do the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1918 ;; following (first remove the old advice):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1919 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1920 ;; (ad-remove-advice 'foom 'before 'fg-print-x)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1921 ;; nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1922 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1923 ;; (defadvice foom (after fg-print-x act)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1924 ;; "Print the value of X."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1925 ;; (setq ad-return-value
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1926 ;; (` (progn (print (, x))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1927 ;; (, ad-return-value)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1928 ;; foom
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1929 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1930 ;; (macroexpand '(foom '(a)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1931 ;; (progn (print (quote (a))) (list (quote (a))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1932 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1933 ;; (foom '(a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1934 ;; (a)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1935 ;; ((a))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1936 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1937 ;; While this method might seem somewhat cumbersome, it is very general
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1938 ;; because it allows one to influence macro expansion as well as evaluation.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1939 ;; In general, advising macros should be a rather rare activity anyway, in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1940 ;; particular, because compile-time macro expansion takes away a lot of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1941 ;; flexibility and effectiveness of the advice mechanism. Macros that were
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1942 ;; compile-time expanded before the advice was activated will of course never
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1943 ;; exhibit the advised behavior.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1944 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1945 ;; @@ Advising special forms:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1946 ;; ==========================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1947 ;; Now for something that should be even more rare than advising macros:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1948 ;; Advising special forms. Because special forms are irregular in their
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1949 ;; argument evaluation behavior (e.g., `setq' evaluates the second but not
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1950 ;; the first argument) they have to be advised into macros. A dangerous
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1951 ;; consequence of this is that the byte-compiler will not recognize them
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1952 ;; as special forms anymore (well, in most cases) and use their expansion
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1953 ;; rather than the proper byte-code. Also, because the original definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1954 ;; of a special form cannot be `funcall'ed, `eval' has to be used instead
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1955 ;; which is less efficient.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1956 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1957 ;; MORAL: Do not advise special forms unless you are completely sure about
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1958 ;; what you are doing (some of the forward advice behavior is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1959 ;; implemented via advice of the special forms `defun' and `defmacro').
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1960 ;; As a safety measure one should always do `ad-deactivate-all' before
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1961 ;; one byte-compiles a file to avoid any interference of advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1962 ;; special forms.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1963 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1964 ;; Apart from the safety concerns advising special forms is not any different
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1965 ;; from advising plain functions or subrs.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1966
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1967
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1968 ;;; Change Log:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1969
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1970 ;; advice.el,v
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1971 ;; Revision 2.1 1993/05/26 00:07:58 hans
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1972 ;; * advise `defalias' and `define-function' to properly handle forward
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1973 ;; advice in GNU Emacs-19.7 and later
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1974 ;; * fix minor bug in `ad-preactivate-advice'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1975 ;; * merge with FSF installation of version 2.0
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1976 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1977 ;; Revision 2.0 1993/05/18 01:29:02 hans
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1978 ;; * Totally revamped: Now also works with v19s, function indirection
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1979 ;; instead of body copying for original function calls, caching of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1980 ;; advised definitions, en/disable mechanism, more and better
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1981 ;; interactive functions, forward advice support for jwz's compiler,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1982 ;; definition hooks, portable argument access, argument list definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1983 ;; for advised functions, preactivation mechanism, pretty comprehensive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1984 ;; docs (still no info file)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1985 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1986 ;; Revision 1.8 1992/12/15 22:54:45 hans
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1987 ;; * Replaced non-standard `member' with `memq'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1988 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1989 ;; Revision 1.7 1992/12/14 22:41:49 hans
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1990 ;; * First publicly released version
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1991 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1992 ;; Revision 1.1 1992/12/12 05:37:33 hans
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1993 ;; * Created
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1994
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1995
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1996 ;;; Code:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1997
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1998 ;; @ Advice implementation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
1999 ;; ========================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2000
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2001 ;; @@ Compilation idiosyncrasies:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2002 ;; ==============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2003
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2004 ;; `defadvice' expansion needs quite a few advice functions and variables,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2005 ;; hence, I need to preload the file before it can be compiled. To avoid
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2006 ;; interference of bogus compiled files I always preload the source file:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2007 (provide 'advice-preload)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2008 ;; During a normal load this is a noop:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2009 (require 'advice-preload "advice.el")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2010
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2011 ;; For the odd case that ``' does not have an autoload definition in some
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2012 ;; Emacs we autoload it here. It is only needed for compilation, hence,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2013 ;; I don't want to unconditionally `require' it (re-autoloading ``' after
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2014 ;; this file got preloaded will properly redefine this autoload):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2015 (if (not (fboundp '`)) (autoload '` "backquote"))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2016
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2017
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2018 ;; @@ Variable definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2019 ;; ========================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2020
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2021 (defconst ad-version "2.1")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2022
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2023 (defconst ad-emacs19-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2024 (not (or (and (boundp 'epoch::version) epoch::version)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2025 (string-lessp emacs-version "19")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2026 "Non-NIL if we run Emacs version 19 or higher.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2027 This will be true for GNU Emacs-19 as well as Lemacs.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2028
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2029 (defconst ad-lemacs-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2030 (and ad-emacs19-p (string-match "Lucid" emacs-version))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2031 "Non-NIL if we run Lucid's version of Emacs-19.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2032
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2033 ;;;###autoload
4111
536a84edaaf7 (ad-start-advice-on-load): Initialize to t.
Roland McGrath <roland@gnu.org>
parents: 4110
diff changeset
2034 (defvar ad-start-advice-on-load t
4110
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2035 "*Non-NIL will start advice magic when this file gets loaded.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2036 Also see function `ad-start-advice'.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2037
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2038 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2039 (defvar ad-activate-on-definition nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2040 "*Non-NIL means automatic advice activation at function definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2041 Set this variable to t if you want to enable forward advice (which is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2042 automatic advice activation of a previously undefined function at the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2043 point the function gets defined/loaded/autoloaded). The value of this
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2044 variable takes effect only during the execution of `ad-start-advice'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2045 If non-NIL it will enable definition hooks regardless of the value
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2046 of `ad-enable-definition-hooks'.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2047
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2048 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2049 (defvar ad-redefinition-action 'warn
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2050 "*Defines what to do with redefinitions during de/activation.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2051 Redefinition occurs if a previously activated function that already has an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2052 original definition associated with it gets redefined and then de/activated.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2053 In such a case we can either accept the current definition as the new
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2054 original definition, discard the current definition and replace it with the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2055 old original, or keep it and raise an error. The values `accept', `discard',
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2056 `error' or `warn' govern what will be done. `warn' is just like `accept' but
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2057 it additionally prints a warning message. All other values will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2058 interpreted as `error'.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2059
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2060 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2061 (defvar ad-definition-hooks nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2062 "*List of hooks to be run after a function definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2063 The variable `ad-defined-function' will be bound to the name of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2064 the currently defined function when the hook function is run.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2065
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2066 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2067 (defvar ad-enable-definition-hooks nil
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2068 "*Non-NIL will enable hooks to be run on function definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2069 Setting this variable is a noop unless the value of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2070 `ad-activate-on-definition' (which see) is NIL.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2071
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2072 ;; The following autoload depends on proper preloading of the runtime
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2073 ;; support of jwz's byte-compiler for accurate initialization:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2074
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2075 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2076 (defvar ad-use-jwz-byte-compiler
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2077 ;; True if jwz's bytecomp-runtime is loaded:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2078 (fboundp 'eval-when-compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2079 "*Non-NIL means Jamie Zawinski's v19 byte-compiler will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2080 If you use a v18 Emacs and don't use jwz's optimizing byte-compiler (the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2081 normal case) then this variable should be NIL, because otherwise
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2082 enabling definition hooks (e.g., for forward advice) will redefine the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2083 `byte-code' subr which will lead to some performance degradation for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2084 byte-compiled code.")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2085
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2086
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2087 ;; @@ `fset/byte-code' hack for jwz's byte-compiler:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2088 ;; =================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2089 ;; Because byte-compiled files that were generated by jwz's byte-compiler
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2090 ;; (as standardly used in v19s) define compiled functions and macros via
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2091 ;; `fset' and `byte-code' instead of `defun/defmacro' we have to advise
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2092 ;; `fset' similar to `defun/defmacro' and redefine `byte-code' to allow
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2093 ;; proper forward advice; hence, we have to make sure that there are
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2094 ;; proper primitive versions around that can be used by the advice package
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2095 ;; itself.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2096 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2097 ;; Wish: A `byte-code-tl' function to be used at the top level of byte-
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2098 ;; compiled files which could be advised for the purpose of forward
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2099 ;; advice without creating all that trouble caused by redefining
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2100 ;; `byte-code'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2101
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2102 (if (not (fboundp 'ad-real-fset))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2103 (progn (fset 'ad-real-fset (symbol-function 'fset))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2104 ;; Copy byte-compiler properties:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2105 (put 'ad-real-fset 'byte-compile (get 'fset 'byte-compile))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2106 (put 'ad-real-fset 'byte-opcode (get 'fset 'byte-opcode))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2107
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2108 (if (not (fboundp 'ad-real-byte-code))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2109 (fset 'ad-real-byte-code (symbol-function 'byte-code)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2110
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2111
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2112 ;; @@ Some utilities:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2113 ;; ==================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2114
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2115 ;; We don't want the local arguments to interfere with anything
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2116 ;; referenced in the supplied functions => the cryptic casing:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2117 (defun ad-substitute-tree (sUbTrEe-TeSt fUnCtIoN tReE)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2118 ;;"Substitutes qualifying subTREEs with result of FUNCTION(subTREE).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2119 ;;Only proper subtrees are considered, for example, if TREE is (1 (2 (3)) 4)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2120 ;;then the subtrees will be 1 (2 (3)) 2 (3) 3 4, dotted structures are
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2121 ;;allowed too. Once a qualifying subtree has been found its subtrees will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2122 ;;not be considered anymore. (ad-substitute-tree 'atom 'identity tree)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2123 ;;generates a copy of TREE."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2124 (cond ((consp tReE)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2125 (cons (if (funcall sUbTrEe-TeSt (car tReE))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2126 (funcall fUnCtIoN (car tReE))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2127 (if (consp (car tReE))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2128 (ad-substitute-tree sUbTrEe-TeSt fUnCtIoN (car tReE))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2129 (car tReE)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2130 (ad-substitute-tree sUbTrEe-TeSt fUnCtIoN (cdr tReE))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2131 ((funcall sUbTrEe-TeSt tReE)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2132 (funcall fUnCtIoN tReE))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2133 (t tReE)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2134
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2135 ;; this is just faster than `ad-substitute-tree':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2136 (defun ad-copy-tree (tree)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2137 ;;"Returns a copy of the list structure of TREE."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2138 (cond ((consp tree)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2139 (cons (ad-copy-tree (car tree))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2140 (ad-copy-tree (cdr tree))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2141 (t tree)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2142
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2143 (defmacro ad-dolist (varform &rest body)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2144 "A Common-Lisp-style dolist iterator with the following syntax:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2145
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2146 (ad-dolist (<var> <init-form> [<result-form>])
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2147 {body-form}*)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2148
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2149 which will iterate over the list yielded by <init-form> binding <var> to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2150 current head at every iteration. If <result-form> is supplied its value will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2151 be returned at the end of the iteration, NIL otherwise. The iteration can be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2152 exited prematurely with (ad-do-return [<value>])."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2153 (let ((expansion
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2154 (` (let ((ad-dO-vAr (, (car (cdr varform))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2155 (, (car varform)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2156 (while ad-dO-vAr
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2157 (setq (, (car varform)) (car ad-dO-vAr))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2158 (,@ body)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2159 ;;work around a backquote bug:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2160 ;;(` ((,@ '(foo)) (bar))) => (append '(foo) '(((bar)))) wrong
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2161 ;;(` ((,@ '(foo)) (, '(bar)))) => (append '(foo) (list '(bar)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2162 (, '(setq ad-dO-vAr (cdr ad-dO-vAr))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2163 (, (car (cdr (cdr varform))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2164 ;;ok, this wastes some cons cells but only during compilation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2165 (if (catch 'contains-return
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2166 (ad-substitute-tree
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2167 (function (lambda (subtree)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2168 (cond ((eq (car-safe subtree) 'ad-dolist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2169 ((eq (car-safe subtree) 'ad-do-return)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2170 (throw 'contains-return t)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2171 'identity body)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2172 nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2173 (` (catch 'ad-dO-eXiT (, expansion)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2174 expansion)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2175
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2176 (defmacro ad-do-return (value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2177 (` (throw 'ad-dO-eXiT (, value))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2178
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2179 (if (not (get 'ad-dolist 'lisp-indent-hook))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2180 (put 'ad-dolist 'lisp-indent-hook 1))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2181
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2182
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2183 ;; @@ Advice info access fns:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2184 ;; ==========================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2185
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2186 ;; Advice information for a particular function is stored on the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2187 ;; advice-info property of the function symbol. It is stored as an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2188 ;; alist of the following format:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2189 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2190 ;; ((active . t/nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2191 ;; (before adv1 adv2 ...)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2192 ;; (around adv1 adv2 ...)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2193 ;; (after adv1 adv2 ...)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2194 ;; (activation adv1 adv2 ...)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2195 ;; (deactivation adv1 adv2 ...)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2196 ;; (origname . <symbol fbound to origdef>)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2197 ;; (cache . (<advised-definition> . <id>)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2198
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2199 ;; List of currently advised though not necessarily activated functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2200 ;; (this list is maintained as a completion table):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2201 (defvar ad-advised-functions nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2202
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2203 (defmacro ad-pushnew-advised-function (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2204 ;;"Add FUNCTION to `ad-advised-functions' unless its already there."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2205 (` (if (not (assoc (symbol-name (, function)) ad-advised-functions))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2206 (setq ad-advised-functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2207 (cons (list (symbol-name (, function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2208 ad-advised-functions)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2209
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2210 (defmacro ad-pop-advised-function (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2211 ;;"Remove FUNCTION from `ad-advised-functions'."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2212 (` (setq ad-advised-functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2213 (delq (assoc (symbol-name (, function)) ad-advised-functions)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2214 ad-advised-functions))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2215
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2216 (defmacro ad-do-advised-functions (varform &rest body)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2217 ;;"`ad-dolist'-style iterator that maps over `ad-advised-functions'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2218 ;; (ad-do-advised-functions (<var> [<result-form>])
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2219 ;; {body-form}*)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2220 ;;Also see `ad-dolist'. On each iteration <var> will be bound to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2221 ;;name of an advised function (a symbol)."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2222 (` (ad-dolist ((, (car varform))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2223 ad-advised-functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2224 (, (car (cdr varform))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2225 (setq (, (car varform)) (intern (car (, (car varform)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2226 (,@ body))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2227
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2228 (if (not (get 'ad-do-advised-functions 'lisp-indent-hook))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2229 (put 'ad-do-advised-functions 'lisp-indent-hook 1))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2230
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2231 (defmacro ad-get-advice-info (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2232 (` (get (, function) 'ad-advice-info)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2233
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2234 (defmacro ad-set-advice-info (function advice-info)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2235 (` (put (, function) 'ad-advice-info (, advice-info))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2236
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2237 (defmacro ad-copy-advice-info (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2238 (` (ad-copy-tree (get (, function) 'ad-advice-info))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2239
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2240 (defmacro ad-is-advised (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2241 ;;"Returns non-NIL if FUNCTION has any advice info associated with it.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2242 ;;This does not mean that the advice is also active."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2243 (list 'ad-get-advice-info function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2244
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2245 (defun ad-initialize-advice-info (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2246 ;;"Initializes the advice info for FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2247 ;;Assumes that FUNCTION has not yet been advised."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2248 (ad-pushnew-advised-function function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2249 (ad-set-advice-info function (list (cons 'active nil))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2250
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2251 (defmacro ad-get-advice-info-field (function field)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2252 ;;"Retrieves the value of the advice info FIELD of FUNCTION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2253 (` (cdr (assq (, field) (ad-get-advice-info (, function))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2254
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2255 (defun ad-set-advice-info-field (function field value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2256 ;;"Destructively modifies VALUE of the advice info FIELD of FUNCTION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2257 (and (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2258 (cond ((assq field (ad-get-advice-info function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2259 ;; A field with that name is already present:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2260 (rplacd (assq field (ad-get-advice-info function)) value))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2261 (t;; otherwise, create a new field with that name:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2262 (nconc (ad-get-advice-info function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2263 (list (cons field value)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2264
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2265 ;; Don't make this a macro so we can use it as a predicate:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2266 (defun ad-is-active (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2267 ;;"non-NIL if FUNCTION is advised and activated."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2268 (ad-get-advice-info-field function 'active))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2269
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2270
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2271 ;; @@ Access fns for single pieces of advice and related predicates:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2272 ;; =================================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2273
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2274 (defun ad-make-advice (name protect enable definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2275 "Constructs single piece of advice to be stored in some advice-info.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2276 NAME should be a non-NIL symbol, PROTECT and ENABLE should each be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2277 either t or nil, and DEFINITION should be a list of the form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2278 (advice lambda ({<arg>}*) [docstring] [(interactive ...)] {body-form}*)"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2279 (list name protect enable definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2280
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2281 ;; ad-find-advice uses the alist structure directly ->
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2282 ;; change if this data structure changes!!
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2283 (defmacro ad-advice-name (advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2284 (list 'car advice))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2285 (defmacro ad-advice-protected (advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2286 (list 'nth 1 advice))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2287 (defmacro ad-advice-enabled (advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2288 (list 'nth 2 advice))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2289 (defmacro ad-advice-definition (advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2290 (list 'nth 3 advice))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2291
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2292 (defun ad-advice-set-enabled (advice flag)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2293 (rplaca (cdr (cdr advice)) flag))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2294
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2295 (defun ad-class-p (thing)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2296 (memq thing ad-advice-classes))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2297 (defun ad-name-p (thing)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2298 (and thing (symbolp thing)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2299 (defun ad-position-p (thing)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2300 (or (natnump thing)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2301 (memq thing '(first last))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2302
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2303
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2304 ;; @@ Advice access functions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2305 ;; ===========================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2306
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2307 ;; List of defined advice classes:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2308 (defvar ad-advice-classes '(before around after activation deactivation))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2309
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2310 (defun ad-has-enabled-advice (function class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2311 ;;"True if at least one of FUNCTION's advices in CLASS is enabled."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2312 (ad-dolist (advice (ad-get-advice-info-field function class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2313 (if (ad-advice-enabled advice) (ad-do-return t))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2314
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2315 (defun ad-has-redefining-advice (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2316 ;;"True if FUNCTION's advice info defines at least 1 redefining advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2317 ;;Redefining advices affect the construction of an advised definition."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2318 (and (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2319 (or (ad-has-enabled-advice function 'before)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2320 (ad-has-enabled-advice function 'around)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2321 (ad-has-enabled-advice function 'after))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2322
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2323 (defun ad-has-any-advice (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2324 ;;"True if the advice info of FUNCTION defines at least one advice."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2325 (and (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2326 (ad-dolist (class ad-advice-classes nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2327 (if (ad-get-advice-info-field function class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2328 (ad-do-return t)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2329
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2330 (defun ad-get-enabled-advices (function class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2331 ;;"Returns the list of enabled advices of FUNCTION in CLASS."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2332 (let (enabled-advices)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2333 (ad-dolist (advice (ad-get-advice-info-field function class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2334 (if (ad-advice-enabled advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2335 (setq enabled-advices (cons advice enabled-advices))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2336 (reverse enabled-advices)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2337
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2338
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2339 ;; @@ Access functions for original definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2340 ;; ============================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2341 ;; The advice-info of an advised function contains its `origname' which is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2342 ;; a symbol that is fbound to the original definition available at the first
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2343 ;; proper activation of the function after a legal re/definition. If the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2344 ;; original was defined via fcell indirection then `origname' will be defined
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2345 ;; just so. Hence, to get hold of the actual original definition of a function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2346 ;; we need to use `ad-real-orig-definition'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2347
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2348 (defun ad-make-origname (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2349 ;;"Makes name to be used to call the original FUNCTION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2350 (intern (format "ad-Orig-%s" function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2351
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2352 (defmacro ad-get-orig-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2353 (` (let ((origname (ad-get-advice-info-field (, function) 'origname)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2354 (if (fboundp origname)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2355 (symbol-function origname)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2356
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2357 (defmacro ad-set-orig-definition (function definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2358 (` (ad-real-fset
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2359 (ad-get-advice-info-field function 'origname) (, definition))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2360
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2361 (defmacro ad-clear-orig-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2362 (` (fmakunbound (ad-get-advice-info-field (, function) 'origname))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2363
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2364
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2365 ;; @@ Interactive input functions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2366 ;; ===============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2367
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2368 (defun ad-read-advised-function (&optional prompt predicate default)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2369 ;;"Reads name of advised function with completion from the minibuffer.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2370 ;;An optional PROMPT will be used to prompt for the function. PREDICATE
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2371 ;;plays the same role as for `try-completion' (which see). DEFAULT will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2372 ;;be returned on empty input (defaults to the first advised function for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2373 ;;which PREDICATE returns non-NIL)."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2374 (if (null ad-advised-functions)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2375 (error "ad-read-advised-function: There are no advised functions"))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2376 (setq default
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2377 (or default
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2378 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2379 (if (or (null predicate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2380 (funcall predicate function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2381 (ad-do-return function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2382 (error "ad-read-advised-function: %s"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2383 "There are no qualifying advised functions")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2384 (let* ((ad-pReDiCaTe predicate)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2385 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2386 (completing-read
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2387 (format "%s(default %s) " (or prompt "Function: ") default)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2388 ad-advised-functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2389 (if predicate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2390 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2391 (lambda (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2392 ;; Oops, no closures - the joys of dynamic scoping:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2393 ;; `predicate' clashed with the `predicate' argument
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2394 ;; of Lemacs' `completing-read'.....
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2395 (funcall ad-pReDiCaTe (intern (car function))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2396 t)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2397 (if (equal function "")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2398 (if (ad-is-advised default)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2399 default
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2400 (error "ad-read-advised-function: `%s' is not advised" default))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2401 (intern function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2402
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2403 (defvar ad-advice-class-completion-table
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2404 (mapcar '(lambda (class) (list (symbol-name class)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2405 ad-advice-classes))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2406
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2407 (defun ad-read-advice-class (function &optional prompt default)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2408 ;;"Reads a legal advice class with completion from the minibuffer.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2409 ;;An optional PROMPT will be used to prompt for the class. DEFAULT will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2410 ;;be returned on empty input (defaults to the first non-empty advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2411 ;;class of FUNCTION)."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2412 (setq default
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2413 (or default
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2414 (ad-dolist (class ad-advice-classes)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2415 (if (ad-get-advice-info-field function class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2416 (ad-do-return class)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2417 (error "ad-read-advice-class: `%s' has no advices" function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2418 (let ((class (completing-read
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2419 (format "%s(default %s) " (or prompt "Class: ") default)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2420 ad-advice-class-completion-table nil t)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2421 (if (equal class "")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2422 default
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2423 (intern class))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2424
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2425 (defun ad-read-advice-name (function class &optional prompt)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2426 ;;"Reads name of existing advice of CLASS for FUNCTION with completion.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2427 ;;An optional PROMPT is used to prompt for the name."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2428 (let* ((name-completion-table
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2429 (mapcar (function (lambda (advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2430 (list (symbol-name (ad-advice-name advice)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2431 (ad-get-advice-info-field function class)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2432 (default
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2433 (if (null name-completion-table)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2434 (error "ad-read-advice-name: `%s' has no %s advice"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2435 function class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2436 (car (car name-completion-table))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2437 (prompt (format "%s(default %s) " (or prompt "Name: ") default))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2438 (name (completing-read prompt name-completion-table nil t)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2439 (if (equal name "")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2440 (intern default)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2441 (intern name))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2442
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2443 (defun ad-read-advice-specification (&optional prompt)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2444 ;;"Reads a complete function/class/name specification from minibuffer.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2445 ;;The list of read symbols will be returned. The optional PROMPT will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2446 ;;be used to prompt for the function."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2447 (let* ((function (ad-read-advised-function prompt))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2448 (class (ad-read-advice-class function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2449 (name (ad-read-advice-name function class)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2450 (list function class name)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2451
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2452 ;; Use previous regexp as a default:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2453 (defvar ad-last-regexp "")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2454
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2455 (defun ad-read-regexp (&optional prompt)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2456 ;;"Reads a regular expression from the minibuffer."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2457 (let ((regexp (read-from-minibuffer
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2458 (concat (or prompt "Regular expression: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2459 (if (equal ad-last-regexp "") ""
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2460 (format "(default \"%s\") " ad-last-regexp))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2461 (setq ad-last-regexp
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2462 (if (equal regexp "") ad-last-regexp regexp))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2463
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2464
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2465 ;; @@ Finding, enabling, adding and removing pieces of advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2466 ;; ===========================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2467
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2468 (defmacro ad-find-advice (function class name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2469 ;;"Finds the first advice of FUNCTION in CLASS with NAME."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2470 (` (assq (, name) (ad-get-advice-info-field (, function) (, class)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2471
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2472 (defun ad-advice-position (function class name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2473 ;;"Returns position of first advice of FUNCTION in CLASS with NAME."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2474 (let* ((found-advice (ad-find-advice function class name))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2475 (advices (ad-get-advice-info-field function class)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2476 (if found-advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2477 (- (length advices) (length (memq found-advice advices))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2478
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2479 (defun ad-find-some-advice (function class name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2480 "Finds the first of FUNCTION's advices in CLASS matching NAME.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2481 NAME can be a symbol or a regular expression matching part of an advice name.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2482 If CLASS is `any' all legal advice classes will be checked."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2483 (if (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2484 (let (found-advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2485 (ad-dolist (advice-class ad-advice-classes)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2486 (if (or (eq class 'any) (eq advice-class class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2487 (setq found-advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2488 (ad-dolist (advice (ad-get-advice-info-field
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2489 function advice-class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2490 (if (or (and (stringp name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2491 (string-match
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2492 name (symbol-name
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2493 (ad-advice-name advice))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2494 (eq name (ad-advice-name advice)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2495 (ad-do-return advice)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2496 (if found-advice (ad-do-return found-advice))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2497
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2498 (defun ad-enable-advice-internal (function class name flag)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2499 ;;"Sets enable FLAG of FUNCTION's advices in CLASS matching NAME.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2500 ;;If NAME is a string rather than a symbol then it's interpreted as a regular
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2501 ;;expression and all advices whose name contain a match for it will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2502 ;;affected. If CLASS is `any' advices in all legal advice classes will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2503 ;;considered. The number of changed advices will be returned (or NIL if
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2504 ;;FUNCTION was not advised)."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2505 (if (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2506 (let ((matched-advices 0))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2507 (ad-dolist (advice-class ad-advice-classes)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2508 (if (or (eq class 'any) (eq advice-class class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2509 (ad-dolist (advice (ad-get-advice-info-field
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2510 function advice-class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2511 (cond ((or (and (stringp name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2512 (string-match
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2513 name (symbol-name (ad-advice-name advice))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2514 (eq name (ad-advice-name advice)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2515 (setq matched-advices (1+ matched-advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2516 (ad-advice-set-enabled advice flag))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2517 matched-advices)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2518
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2519 (defun ad-enable-advice (function class name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2520 "Enables the advice of FUNCTION with CLASS and NAME."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2521 (interactive (ad-read-advice-specification "Enable advice of: "))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2522 (if (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2523 (if (eq (ad-enable-advice-internal function class name t) 0)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2524 (error "ad-enable-advice: `%s' has no %s advice matching `%s'"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2525 function class name))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2526 (error "ad-enable-advice: `%s' is not advised" function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2527
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2528 (defun ad-disable-advice (function class name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2529 "Disables the advice of FUNCTION with CLASS and NAME."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2530 (interactive (ad-read-advice-specification "Disable advice of: "))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2531 (if (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2532 (if (eq (ad-enable-advice-internal function class name nil) 0)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2533 (error "ad-disable-advice: `%s' has no %s advice matching `%s'"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2534 function class name))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2535 (error "ad-disable-advice: `%s' is not advised" function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2536
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2537 (defun ad-enable-regexp-internal (regexp class flag)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2538 ;;"Sets enable FLAGs of all CLASS advices whose name contains a REGEXP match.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2539 ;;If CLASS is `any' all legal advice classes are considered. The number of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2540 ;;affected advices will be returned."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2541 (let ((matched-advices 0))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2542 (ad-do-advised-functions (advised-function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2543 (setq matched-advices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2544 (+ matched-advices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2545 (or (ad-enable-advice-internal
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2546 advised-function class regexp flag)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2547 0))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2548 matched-advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2549
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2550 (defun ad-enable-regexp (regexp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2551 "Enables all advices with names that contain a match for REGEXP.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2552 All currently advised functions will be considered."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2553 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2554 (list (ad-read-regexp "Enable advices via regexp: ")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2555 (let ((matched-advices (ad-enable-regexp-internal regexp 'any t)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2556 (if (interactive-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2557 (message "%d matching advices enabled" matched-advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2558 matched-advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2559
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2560 (defun ad-disable-regexp (regexp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2561 "Disables all advices with names that contain a match for REGEXP.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2562 All currently advised functions will be considered."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2563 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2564 (list (ad-read-regexp "Disable advices via regexp: ")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2565 (let ((matched-advices (ad-enable-regexp-internal regexp 'any nil)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2566 (if (interactive-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2567 (message "%d matching advices disabled" matched-advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2568 matched-advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2569
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2570 (defun ad-remove-advice (function class name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2571 "Removes FUNCTION's advice with NAME from its advices in CLASS.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2572 If such an advice was found it will be removed from the list of advices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2573 in that CLASS."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2574 (interactive (ad-read-advice-specification "Remove advice of: "))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2575 (if (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2576 (let* ((advice-to-remove (ad-find-advice function class name)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2577 (if advice-to-remove
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2578 (ad-set-advice-info-field
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2579 function class
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2580 (delq advice-to-remove (ad-get-advice-info-field function class)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2581 (error "ad-remove-advice: `%s' has no %s advice `%s'"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2582 function class name)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2583 (error "ad-remove-advice: `%s' is not advised" function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2584
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2585 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2586 (defun ad-add-advice (function advice class position)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2587 "Adds a piece of ADVICE to FUNCTION's list of advices in CLASS.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2588 If FUNCTION already has one or more pieces of advice of the specified
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2589 CLASS then POSITION determines where the new piece will go. The value
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2590 of POSITION can either be `first', `last' or a number where 0 corresponds
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2591 to `first'. Numbers outside the range will be mapped to the closest
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2592 extreme position. If there was already a piece of ADVICE with the same
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2593 name, then the position argument will be ignored and the old advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2594 will be overwritten with the new one.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2595 If the FUNCTION was not advised already, then its advice info will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2596 initialized. Redefining a piece of advice whose name is part of the cache-id
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2597 will clear the cache."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2598 (cond ((not (ad-is-advised function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2599 (ad-initialize-advice-info function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2600 (ad-set-advice-info-field
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2601 function 'origname (ad-make-origname function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2602 (let* ((previous-position
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2603 (ad-advice-position function class (ad-advice-name advice)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2604 (advices (ad-get-advice-info-field function class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2605 ;; Determine a numerical position for the new advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2606 (position (cond (previous-position)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2607 ((eq position 'first) 0)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2608 ((eq position 'last) (length advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2609 ((numberp position)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2610 (max 0 (min position (length advices))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2611 (t 0))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2612 ;; Check whether we have to clear the cache:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2613 (if (memq (ad-advice-name advice) (ad-get-cache-class-id function class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2614 (ad-clear-cache function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2615 (if previous-position
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2616 (setcar (nthcdr position advices) advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2617 (if (= position 0)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2618 (ad-set-advice-info-field function class (cons advice advices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2619 (setcdr (nthcdr (1- position) advices)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2620 (cons advice (nthcdr position advices)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2621
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2622
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2623 ;; @@ Accessing and manipulating function definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2624 ;; ===================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2625
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2626 (defmacro ad-macrofy (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2627 ;;"Takes a lambda function DEFINITION and makes a macro out of it."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2628 (` (cons 'macro (, definition))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2629
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2630 (defmacro ad-lambdafy (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2631 ;;"Takes a macro function DEFINITION and makes a lambda out of it."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2632 (` (cdr (, definition))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2633
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2634 ;; There is no way to determine whether some subr is a special form or not,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2635 ;; hence we need this list (which is the same for v18s and v19s):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2636 (defvar ad-special-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2637 (mapcar 'symbol-function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2638 '(and catch cond condition-case defconst defmacro
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2639 defun defvar function if interactive let let*
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2640 or prog1 prog2 progn quote save-excursion
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2641 save-restriction save-window-excursion setq
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2642 setq-default unwind-protect while
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2643 with-output-to-temp-buffer)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2644
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2645 (defmacro ad-special-form-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2646 ;;"non-NIL if DEFINITION is a special form."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2647 (list 'memq definition 'ad-special-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2648
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2649 (defmacro ad-interactive-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2650 ;;"non-NIL if DEFINITION can be called interactively."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2651 (list 'commandp definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2652
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2653 (defmacro ad-subr-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2654 ;;"non-NIL if DEFINITION is a subr."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2655 (list 'subrp definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2656
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2657 (defmacro ad-macro-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2658 ;;"non-NIL if DEFINITION is a macro."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2659 (` (eq (car-safe (, definition)) 'macro)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2660
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2661 (defmacro ad-lambda-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2662 ;;"non-NIL if DEFINITION is a lambda expression."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2663 (` (eq (car-safe (, definition)) 'lambda)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2664
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2665 ;; see ad-make-advice for the format of advice definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2666 (defmacro ad-advice-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2667 ;;"non-NIL if DEFINITION is a piece of advice."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2668 (` (eq (car-safe (, definition)) 'advice)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2669
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2670 ;; GNU Emacs-19/Lemacs cross-compatibility
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2671 ;; (compiled-function-p is an obsolete function in GNU Emacs-19):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2672 (if (and (not (fboundp 'byte-code-function-p))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2673 (fboundp 'compiled-function-p))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2674 (ad-real-fset 'byte-code-function-p 'compiled-function-p))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2675
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2676 (defmacro ad-v19-compiled-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2677 ;;"non-NIL if DEFINITION is a compiled object of a v19 Emacs."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2678 (` (and ad-emacs19-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2679 (or (byte-code-function-p (, definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2680 (and (ad-macro-p (, definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2681 (byte-code-function-p (ad-lambdafy (, definition))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2682
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2683 (defmacro ad-v19-compiled-code (compiled-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2684 ;;"Returns the byte-code object of a v19 COMPILED-DEFINITION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2685 (` (if (ad-macro-p (, compiled-definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2686 (ad-lambdafy (, compiled-definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2687 (, compiled-definition))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2688
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2689 (defun ad-lambda-expression (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2690 ;;"Returns the lambda expression of a function/macro/advice DEFINITION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2691 (cond ((ad-lambda-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2692 definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2693 ((ad-macro-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2694 (ad-lambdafy definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2695 ((ad-advice-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2696 (cdr definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2697 (t nil)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2698
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2699 (defun ad-arglist (definition &optional name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2700 ;;"Returns the argument list of DEFINITION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2701 ;;If DEFINITION could be from a subr then its NAME should be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2702 ;;supplied to make subr arglist lookup more efficient."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2703 (cond ((ad-v19-compiled-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2704 (aref (ad-v19-compiled-code definition) 0))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2705 ((consp definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2706 (car (cdr (ad-lambda-expression definition))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2707 ((ad-subr-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2708 (if name
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2709 (ad-subr-arglist name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2710 ;; otherwise get it from its printed representation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2711 (setq name (format "%s" definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2712 (string-match "^#<subr \\([^>]+\\)>$" name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2713 (ad-subr-arglist
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2714 (intern (substring name (match-beginning 1) (match-end 1))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2715
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2716 ;; Store subr-args as `((arg1 arg2 ...))' so I can distinguish
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2717 ;; a defined empty arglist `(nil)' from an undefined arglist:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2718 (defmacro ad-define-subr-args (subr arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2719 (` (put (, subr) 'ad-subr-arglist (list (, arglist)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2720 (defmacro ad-undefine-subr-args (subr)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2721 (` (put (, subr) 'ad-subr-arglist nil)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2722 (defmacro ad-subr-args-defined-p (subr)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2723 (` (get (, subr) 'ad-subr-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2724 (defmacro ad-get-subr-args (subr)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2725 (` (car (get (, subr) 'ad-subr-arglist))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2726
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2727 (defun ad-subr-arglist (subr-name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2728 ;;"Retrieve arglist of the subr with SUBR-NAME.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2729 ;;Either use the one stored under the `ad-subr-arglist' property, or, if we
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2730 ;;have a v19 Emacs try to retrieve it from the docstring and cache it under
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2731 ;;that property, or otherwise use `(&rest ad-subr-args)'."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2732 (if (ad-subr-args-defined-p subr-name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2733 (ad-get-subr-args subr-name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2734 (let ((doc (if ad-emacs19-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2735 (documentation subr-name))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2736 (cond ((and doc
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2737 (string-match "[\n\t ]*\narguments: ?\\((.*)\\)\n?\\'" doc))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2738 (ad-define-subr-args
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2739 subr-name
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2740 (car (read-from-string doc (match-beginning 1) (match-end 1))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2741 (ad-get-subr-args subr-name))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2742 (t '(&rest ad-subr-args))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2743
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2744 (defun ad-docstring (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2745 ;;"Returns the unexpanded docstring of DEFINITION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2746 (let ((docstring
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2747 (if (ad-v19-compiled-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2748 (condition-case nodoc
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2749 (aref (ad-v19-compiled-code definition) 4)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2750 (error nil))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2751 (car (cdr (cdr (ad-lambda-expression definition)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2752 (if (or (stringp docstring)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2753 (natnump docstring))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2754 docstring)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2755
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2756 (defun ad-interactive-form (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2757 ;;"Returns the interactive form of DEFINITION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2758 (cond ((ad-v19-compiled-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2759 (and (commandp definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2760 (list 'interactive (aref (ad-v19-compiled-code definition) 5))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2761 ((or (ad-advice-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2762 (ad-lambda-p definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2763 (commandp (ad-lambda-expression definition)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2764
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2765 (defun ad-body-forms (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2766 ;;"Returns the list of body forms of DEFINITION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2767 (cond ((ad-v19-compiled-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2768 (setq definition (ad-v19-compiled-code definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2769 ;; build a standard (byte-code ...) form from the v19 code
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2770 ;; (I don't think I ever use this):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2771 (list (list 'byte-code
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2772 (aref definition 1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2773 (aref definition 2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2774 (aref definition 3))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2775 ((consp definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2776 (nthcdr (+ (if (ad-docstring definition) 1 0)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2777 (if (ad-interactive-form definition) 1 0))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2778 (cdr (cdr (ad-lambda-expression definition)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2779
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2780 (defun ad-compiled-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2781 ;;"non-NIL if DEFINITION is byte-compiled."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2782 (or (ad-v19-compiled-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2783 (memq (car-safe (car (ad-body-forms definition)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2784 '(byte-code ad-real-byte-code))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2785
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2786 ;; Matches the docstring of an advised definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2787 ;; The first group of the regexp matches the function name:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2788 (defvar ad-advised-definition-docstring-regexp "^\\$ad-doc: \\(.+\\)\\$$")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2789
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2790 (defun ad-make-advised-definition-docstring (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2791 ;; Makes an identifying docstring for the advised definition of FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2792 ;; Put function name into the documentation string so we can infer
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2793 ;; the name of the advised function from the docstring. This is needed
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2794 ;; to generate a proper advised docstring even if we are just given a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2795 ;; definition (also see the defadvice for `documentation'):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2796 (format "$ad-doc: %s$" (prin1-to-string function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2797
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2798 (defun ad-advised-definition-p (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2799 ;;"non-NIL if DEFINITION was generated from advice information."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2800 (if (or (ad-lambda-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2801 (ad-macro-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2802 (ad-compiled-p definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2803 (let ((docstring (ad-docstring definition)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2804 (and (stringp docstring)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2805 (string-match
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2806 ad-advised-definition-docstring-regexp docstring)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2807
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2808 (defun ad-definition-type (definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2809 ;;"Returns symbol that describes the type of DEFINITION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2810 (if (ad-macro-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2811 'macro
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2812 (if (ad-subr-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2813 (if (ad-special-form-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2814 'special-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2815 'subr)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2816 (if (or (ad-lambda-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2817 (ad-compiled-p definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2818 'function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2819 (if (ad-advice-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2820 'advice)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2821
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2822 (defun ad-has-proper-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2823 ;;"True if FUNCTION is a symbol with a proper definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2824 ;;For that it has to be fbound with a non-autoload definition."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2825 (and (symbolp function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2826 (fboundp function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2827 (not (eq (car-safe (symbol-function function)) 'autoload))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2828
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2829 ;; The following two are necessary for the sake of packages such as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2830 ;; ange-ftp which redefine functions via fcell indirection:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2831 (defun ad-real-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2832 ;;"Finds FUNCTION's definition at the end of function cell indirection."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2833 (if (ad-has-proper-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2834 (let ((definition (symbol-function function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2835 (if (symbolp definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2836 (ad-real-definition definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2837 definition))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2838
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2839 (defun ad-real-orig-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2840 ;;"Finds FUNCTION's real original definition starting from its `origname'."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2841 (if (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2842 (ad-real-definition (ad-get-advice-info-field function 'origname))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2843
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2844 (defun ad-is-compilable (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2845 ;;"True if FUNCTION has an interpreted definition that can be compiled."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2846 (and (ad-has-proper-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2847 (or (ad-lambda-p (symbol-function function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2848 (ad-macro-p (symbol-function function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2849 (not (ad-compiled-p (symbol-function function)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2850
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2851 ;; Need this because the v18 `byte-compile' can't compile macros:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2852 (defun ad-compile-function (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2853 "Byte-compiles FUNCTION (or macro) if it is not yet compiled."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2854 (interactive "aByte-compile function: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2855 (if (ad-is-compilable function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2856 (or (progn
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2857 (require 'byte-compile "bytecomp")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2858 (byte-compile function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2859 ;; If we get here we must have a macro and a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2860 ;; standard non-optimizing v18 byte-compiler:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2861 (and (ad-macro-p (symbol-function function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2862 (ad-real-fset
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2863 function (ad-macrofy
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2864 (byte-compile-lambda
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2865 (ad-lambda-expression
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2866 (symbol-function function)))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2867
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2868 (defun ad-real-byte-codify (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2869 ;;"Compile FUNCTION and use `ad-real-byte-code' in the compiled body.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2870 ;;This is needed when forward advice with jwz-byte-compiled files is used in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2871 ;;order to avoid infinite recursion and keep efficiency as high as possible."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2872 (ad-compile-function function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2873 (let ((definition (symbol-function function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2874 (cond ((ad-v19-compiled-p definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2875 ((ad-compiled-p definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2876 ;; Use ad-real-byte-code in the body of function:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2877 (setcar (car (ad-body-forms definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2878 'ad-real-byte-code)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2879
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2880
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2881 ;; @@ Constructing advised definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2882 ;; ====================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2883 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2884 ;; Main design decisions about the form of advised definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2885 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2886 ;; A) How will original definitions be called?
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2887 ;; B) What will argument lists of advised functions look like?
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2888 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2889 ;; Ad A)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2890 ;; I chose to use function indirection for all four types of original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2891 ;; definitions (functions, macros, subrs and special forms), i.e., create
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2892 ;; a unique symbol `ad-Orig-<name>' which is fbound to the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2893 ;; definition and call it according to type and arguments. Functions and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2894 ;; subrs that don't have any &rest arguments can be called directly in a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2895 ;; `(ad-Orig-<name> ....)' form. If they have a &rest argument we have to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2896 ;; use `apply'. Macros will be called with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2897 ;; `(macroexpand '(ad-Orig-<name> ....))', and special forms also need a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2898 ;; form like that with `eval' instead of `macroexpand'.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2899 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2900 ;; Ad B)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2901 ;; Use original arguments where possible and `(&rest ad-subr-args)'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2902 ;; otherwise, even though this seems to be more complicated and less
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2903 ;; uniform than a general `(&rest args)' approach. My reason to still
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2904 ;; do it that way is that in most cases my approach leads to the more
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2905 ;; efficient form for the advised function, and portability (e.g., to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2906 ;; make the same advice work regardless of whether something is a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2907 ;; function or a subr) can still be achieved with argument access macros.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2908
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2909
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2910 (defun ad-prognify (forms)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2911 (cond ((<= (length forms) 1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2912 (car forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2913 (t (cons 'progn forms))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2914
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2915 ;; @@@ Accessing argument lists:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2916 ;; =============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2917
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2918 (defun ad-parse-arglist (arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2919 ;;"Parses ARGLIST into its required, optional and rest parameters.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2920 ;;A three-element list is returned, where the 1st element is the list of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2921 ;;required arguments, the 2nd is the list of optional arguments, and the 3rd
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2922 ;;is the name of an optional rest parameter (or NIL)."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2923 (let* (required optional rest)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2924 (setq rest (car (cdr (memq '&rest arglist))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2925 (if rest (setq arglist (reverse (cdr (memq '&rest (reverse arglist))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2926 (setq optional (cdr (memq '&optional arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2927 (if optional
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2928 (setq required (reverse (cdr (memq '&optional (reverse arglist)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2929 (setq required arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2930 (list required optional rest)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2931
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2932 (defun ad-retrieve-args-form (arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2933 ;;"Generates a form which evaluates into names/values/types of ARGLIST.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2934 ;;When the form gets evaluated within a function with that argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2935 ;;it will result in a list with one entry for each argument, where the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2936 ;;first element of each entry is the name of the argument, the second
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2937 ;;element is its actual current value, and the third element is either
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2938 ;;`required', `optional' or `rest' depending on the type of the argument."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2939 (let* ((parsed-arglist (ad-parse-arglist arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2940 (rest (nth 2 parsed-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2941 (` (list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2942 (,@ (mapcar (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2943 (lambda (req)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2944 (` (list '(, req) (, req) 'required))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2945 (nth 0 parsed-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2946 (,@ (mapcar (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2947 (lambda (opt)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2948 (` (list '(, opt) (, opt) 'optional))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2949 (nth 1 parsed-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2950 (,@ (if rest (list (` (list '(, rest) (, rest) 'rest)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2951 ))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2952
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2953 (defun ad-arg-binding-field (binding field)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2954 (cond ((eq field 'name) (car binding))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2955 ((eq field 'value) (car (cdr binding)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2956 ((eq field 'type) (car (cdr (cdr binding))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2957
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2958 (defun ad-list-access (position list)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2959 (cond ((= position 0) list)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2960 ((= position 1) (list 'cdr list))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2961 (t (list 'nthcdr position list))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2962
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2963 (defun ad-element-access (position list)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2964 (cond ((= position 0) (list 'car list))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2965 ((= position 1) (` (car (cdr (, list)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2966 (t (list 'nth position list))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2967
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2968 (defun ad-access-argument (arglist index)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2969 ;;"Tells how to access ARGLIST's actual argument at position INDEX.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2970 ;;For a required/optional arg it simply returns it, if a rest argument has
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2971 ;;to be accessed, it returns a list with the index and name."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2972 (let* ((parsed-arglist (ad-parse-arglist arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2973 (reqopt-args (append (nth 0 parsed-arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2974 (nth 1 parsed-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2975 (rest-arg (nth 2 parsed-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2976 (cond ((< index (length reqopt-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2977 (nth index reqopt-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2978 (rest-arg
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2979 (list (- index (length reqopt-args)) rest-arg)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2980
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2981 (defun ad-get-argument (arglist index)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2982 ;;"Returns form to access ARGLIST's actual argument at position INDEX."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2983 (let ((argument-access (ad-access-argument arglist index)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2984 (cond ((consp argument-access)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2985 (ad-element-access
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2986 (car argument-access) (car (cdr argument-access))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2987 (argument-access))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2988
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2989 (defun ad-set-argument (arglist index value-form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2990 ;;"Returns form to set ARGLIST's actual arg at INDEX to VALUE-FORM."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2991 (let ((argument-access (ad-access-argument arglist index)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2992 (cond ((consp argument-access)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2993 ;; should this check whether there actually is something to set?
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2994 (` (setcar (, (ad-list-access
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2995 (car argument-access) (car (cdr argument-access))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2996 (, value-form))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2997 (argument-access
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2998 (` (setq (, argument-access) (, value-form))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
2999 (t (error "ad-set-argument: No argument at position %d of `%s'"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3000 index arglist)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3001
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3002 (defun ad-get-arguments (arglist index)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3003 ;;"Returns form to access all actual arguments starting at position INDEX."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3004 (let* ((parsed-arglist (ad-parse-arglist arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3005 (reqopt-args (append (nth 0 parsed-arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3006 (nth 1 parsed-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3007 (rest-arg (nth 2 parsed-arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3008 args-form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3009 (if (< index (length reqopt-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3010 (setq args-form (` (list (,@ (nthcdr index reqopt-args))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3011 (if rest-arg
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3012 (if args-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3013 (setq args-form (` (nconc (, args-form) (, rest-arg))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3014 (setq args-form (ad-list-access (- index (length reqopt-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3015 rest-arg))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3016 args-form))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3017
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3018 (defun ad-set-arguments (arglist index values-form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3019 ;;"Makes form to assign elements of VALUES-FORM as actual ARGLIST args.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3020 ;;The assignment starts at position INDEX."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3021 (let ((values-index 0)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3022 argument-access set-forms)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3023 (while (setq argument-access (ad-access-argument arglist index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3024 (if (symbolp argument-access)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3025 (setq set-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3026 (cons (ad-set-argument
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3027 arglist index
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3028 (ad-element-access values-index 'ad-vAlUeS))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3029 set-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3030 (setq set-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3031 (cons (if (= (car argument-access) 0)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3032 (list 'setq
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3033 (car (cdr argument-access))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3034 (ad-list-access values-index 'ad-vAlUeS))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3035 (list 'setcdr
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3036 (ad-list-access (1- (car argument-access))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3037 (car (cdr argument-access)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3038 (ad-list-access values-index 'ad-vAlUeS)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3039 set-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3040 ;; terminate loop
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3041 (setq arglist nil))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3042 (setq index (1+ index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3043 (setq values-index (1+ values-index)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3044 (if (null set-forms)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3045 (error "ad-set-arguments: No argument at position %d of `%s'"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3046 index arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3047 (if (= (length set-forms) 1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3048 ;; For exactly one set-form we can use values-form directly,...
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3049 (ad-substitute-tree
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3050 (function (lambda (form) (eq form 'ad-vAlUeS)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3051 (function (lambda (form) values-form))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3052 (car set-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3053 ;; ...if we have more we have to bind it to a variable:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3054 (` (let ((ad-vAlUeS (, values-form)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3055 (,@ (reverse set-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3056 ;; work around the old backquote bug:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3057 (, 'ad-vAlUeS)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3058
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3059 (defun ad-insert-argument-access-forms (definition arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3060 ;;"Expands arg-access text macros in DEFINITION according to ARGLIST."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3061 (ad-substitute-tree
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3062 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3063 (lambda (form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3064 (or (eq form 'ad-arg-bindings)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3065 (and (memq (car-safe form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3066 '(ad-get-arg ad-get-args ad-set-arg ad-set-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3067 (integerp (car-safe (cdr form)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3068 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3069 (lambda (form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3070 (if (eq form 'ad-arg-bindings)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3071 (ad-retrieve-args-form arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3072 (let ((accessor (car form))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3073 (index (car (cdr form)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3074 (val (car (cdr (ad-insert-argument-access-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3075 (cdr form) arglist)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3076 (cond ((eq accessor 'ad-get-arg)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3077 (ad-get-argument arglist index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3078 ((eq accessor 'ad-set-arg)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3079 (ad-set-argument arglist index val))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3080 ((eq accessor 'ad-get-args)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3081 (ad-get-arguments arglist index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3082 ((eq accessor 'ad-set-args)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3083 (ad-set-arguments arglist index val)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3084 definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3085
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3086 ;; @@@ Mapping argument lists:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3087 ;; ===========================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3088 ;; Here is the problem:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3089 ;; Suppose function foo was called with (foo 1 2 3 4 5), and foo has the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3090 ;; argument list (x y &rest z), and we want to call the function bar which
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3091 ;; has argument list (a &rest b) with a combination of x, y and z so that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3092 ;; the effect is just as if we had called (bar 1 2 3 4 5) directly.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3093 ;; The mapping should work for any two argument lists.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3094
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3095 (defun ad-map-arglists (source-arglist target-arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3096 "Makes funcall/apply form to map SOURCE-ARGLIST to TARGET-ARGLIST.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3097 The arguments supplied to TARGET-ARGLIST will be taken from SOURCE-ARGLIST just
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3098 as if they had been supplied to a function with TARGET-ARGLIST directly.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3099 Excess source arguments will be neglected, missing source arguments will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3100 supplied as NIL. Returns a funcall or apply form with the second element being
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3101 `function' which has to be replaced by an actual function argument.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3102 Example: (ad-map-arglists '(a &rest args) '(w x y z)) will return
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3103 (funcall function a (car args) (car (cdr args)) (nth 2 args))"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3104 (let* ((parsed-source-arglist (ad-parse-arglist source-arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3105 (source-reqopt-args (append (nth 0 parsed-source-arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3106 (nth 1 parsed-source-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3107 (source-rest-arg (nth 2 parsed-source-arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3108 (parsed-target-arglist (ad-parse-arglist target-arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3109 (target-reqopt-args (append (nth 0 parsed-target-arglist)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3110 (nth 1 parsed-target-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3111 (target-rest-arg (nth 2 parsed-target-arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3112 (need-apply (and source-rest-arg target-rest-arg))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3113 (target-arg-index -1))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3114 ;; This produces ``error-proof'' target function calls with the exception
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3115 ;; of a case like (&rest a) mapped onto (x &rest y) where the actual args
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3116 ;; supplied to A might not be enough to supply the required target arg X
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3117 (append (list (if need-apply 'apply 'funcall) 'function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3118 (cond (need-apply
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3119 ;; `apply' can take care of that directly:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3120 (append source-reqopt-args (list source-rest-arg)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3121 (t (mapcar (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3122 (lambda (arg)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3123 (setq target-arg-index (1+ target-arg-index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3124 (ad-get-argument
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3125 source-arglist target-arg-index)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3126 (append target-reqopt-args
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3127 (and target-rest-arg
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3128 ;; If we have a rest arg gobble up
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3129 ;; remaining source args:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3130 (nthcdr (length target-reqopt-args)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3131 source-reqopt-args)))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3132
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3133 (defun ad-make-mapped-call (source-arglist target-arglist target-function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3134 ;;"Makes form to call TARGET-FUNCTION with args from SOURCE-ARGLIST."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3135 (let* ((mapped-form (ad-map-arglists source-arglist target-arglist)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3136 (if (eq (car mapped-form) 'funcall)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3137 (cons target-function (cdr (cdr mapped-form)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3138 (prog1 mapped-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3139 (setcar (cdr mapped-form) (list 'quote target-function))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3140
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3141 ;; @@@ Making an advised documentation string:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3142 ;; ===========================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3143 ;; New policy: The documentation string for an advised function will be built
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3144 ;; at the time the advised `documentation' function is called. This has the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3145 ;; following advantages:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3146 ;; 1) command-key substitutions will automatically be correct
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3147 ;; 2) No wasted string space due to big advised docstrings in caches or
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3148 ;; compiled files that contain preactivations
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3149 ;; The overall overhead for this should be negligible because people normally
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3150 ;; don't lookup documentation for the same function over and over again.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3151
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3152 (defun ad-make-single-advice-docstring (advice class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3153 (let ((advice-docstring (ad-docstring (ad-advice-definition advice))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3154 ;; Always show advice name/class even if there is no docstring:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3155 (format "%s (%s):%s%s"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3156 (ad-advice-name advice) class
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3157 (if advice-docstring "\n" "")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3158 (or advice-docstring ""))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3159
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3160 (defun ad-make-advised-docstring (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3161 ;;"Constructs a documentation string for the advised FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3162 ;;It concatenates the original documentation with the documentation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3163 ;;strings of the individual pieces of advice. Name and class of every
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3164 ;;advice will be displayed too. The order of the advice documentation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3165 ;;strings corresponds to before/around/after and the individual ordering
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3166 ;;in any of these classes."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3167 (let* ((origdef (ad-real-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3168 (origdoc
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3169 ;; Use this wacky apply construction to avoid an Lemacs compiler
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3170 ;; warning (its `documentation' has only 1 arg as opposed to GNU
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3171 ;; Emacs-19's version which has an optional `raw' arg):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3172 (apply 'documentation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3173 origdef
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3174 (if (and ad-emacs19-p (not ad-lemacs-p))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3175 ;; If we have GNU Emacs-19 retrieve raw doc, because
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3176 ;; key substitution will be taken care of later anyway:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3177 '(t)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3178 (concat (or origdoc "")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3179 (if origdoc "\n\n" "\n")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3180 ;; Always inform about advice even if there is no origdoc:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3181 "This " (symbol-name (ad-definition-type origdef))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3182 " is advised with the following advice(s):"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3183 ;; Combine advice docstrings:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3184 (mapconcat
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3185 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3186 (lambda (class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3187 (mapconcat
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3188 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3189 (lambda (advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3190 (concat
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3191 "\n\n" (ad-make-single-advice-docstring advice class))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3192 (ad-get-enabled-advices function class) "")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3193 ad-advice-classes ""))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3194
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3195 ;; @@@ Accessing overriding arglists and interactive forms:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3196 ;; ========================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3197
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3198 (defun ad-advised-arglist (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3199 ;;"Finds first defined arglist in FUNCTION's redefining advices."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3200 (ad-dolist (advice (append (ad-get-enabled-advices function 'before)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3201 (ad-get-enabled-advices function 'around)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3202 (ad-get-enabled-advices function 'after)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3203 (let ((arglist (ad-arglist (ad-advice-definition advice))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3204 (if arglist
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3205 ;; We found the first one, use it:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3206 (ad-do-return arglist)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3207
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3208 (defun ad-advised-interactive-form (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3209 ;;"Finds first interactive form in FUNCTION's redefining advices."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3210 (ad-dolist (advice (append (ad-get-enabled-advices function 'before)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3211 (ad-get-enabled-advices function 'around)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3212 (ad-get-enabled-advices function 'after)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3213 (let ((interactive-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3214 (ad-interactive-form (ad-advice-definition advice))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3215 (if interactive-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3216 ;; We found the first one, use it:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3217 (ad-do-return interactive-form)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3218
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3219 ;; @@@ Putting it all together:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3220 ;; ============================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3221
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3222 (defun ad-make-advised-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3223 ;;"Generates an advised definition of FUNCTION from its advice info."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3224 (if (and (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3225 (ad-has-redefining-advice function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3226 (let* ((origdef (ad-real-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3227 (origname (ad-get-advice-info-field function 'origname))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3228 (orig-interactive-p (ad-interactive-p origdef))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3229 (orig-subr-p (ad-subr-p origdef))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3230 (orig-special-form-p (ad-special-form-p origdef))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3231 (orig-macro-p (ad-macro-p origdef))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3232 ;; Construct the individual pieces that we need for assembly:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3233 (orig-arglist (ad-arglist origdef function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3234 (advised-arglist (or (ad-advised-arglist function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3235 orig-arglist))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3236 (advised-interactive-form (ad-advised-interactive-form function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3237 (interactive-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3238 (cond (orig-macro-p nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3239 (advised-interactive-form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3240 ((ad-interactive-form origdef))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3241 ;; Otherwise we must have a subr: make it interactive if
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3242 ;; we have to and initialize required arguments in case
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3243 ;; it is called interactively:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3244 (orig-interactive-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3245 (let ((reqargs (car (ad-parse-arglist advised-arglist))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3246 (if reqargs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3247 (` (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3248 '(, (make-list (length reqargs) nil))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3249 '(interactive))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3250 (orig-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3251 (cond ((or orig-special-form-p orig-macro-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3252 ;; Special forms and macros will be advised into macros.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3253 ;; The trick is to construct an expansion for the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3254 ;; macro that does the correct thing when it gets eval'ed.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3255 ;; For macros we'll just use the expansion of the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3256 ;; macro and return that. This way compiled advised macros
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3257 ;; will be expanded into something useful. Note that after
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3258 ;; advices have full control over whether they want to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3259 ;; evaluate the expansion (the value of `ad-return-value')
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3260 ;; at macro expansion time or not. For special forms there
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3261 ;; is no solution that interacts reasonably with the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3262 ;; compiler, hence we just evaluate the original at macro
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3263 ;; expansion time and return the result. The moral of that
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3264 ;; is that one should always deactivate advised special
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3265 ;; forms before one byte-compiles a file.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3266 (` ((, (if orig-macro-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3267 'macroexpand
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3268 'eval))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3269 (cons '(, origname)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3270 (, (ad-get-arguments advised-arglist 0))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3271 ((and orig-subr-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3272 orig-interactive-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3273 (not advised-interactive-form))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3274 ;; Check whether we were called interactively
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3275 ;; in order to do proper prompting:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3276 (` (if (interactive-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3277 (call-interactively '(, origname))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3278 (, (ad-make-mapped-call
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3279 orig-arglist advised-arglist origname)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3280 ;; And now for normal functions and non-interactive subrs
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3281 ;; (or subrs whose interactive behavior was advised):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3282 (t (ad-make-mapped-call
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3283 advised-arglist orig-arglist origname)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3284
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3285 ;; Finally, build the sucker:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3286 (ad-assemble-advised-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3287 (cond (orig-macro-p 'macro)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3288 (orig-special-form-p 'special-form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3289 (t 'function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3290 advised-arglist
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3291 (ad-make-advised-definition-docstring function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3292 interactive-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3293 orig-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3294 (ad-get-enabled-advices function 'before)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3295 (ad-get-enabled-advices function 'around)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3296 (ad-get-enabled-advices function 'after)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3297
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3298 (defun ad-assemble-advised-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3299 (type args docstring interactive orig &optional befores arounds afters)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3300
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3301 ;;"Assembles an original and its advices into an advised function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3302 ;;It constructs a function or macro definition according to TYPE which has to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3303 ;;be either `macro', `function' or `special-form'. ARGS is the argument list
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3304 ;;that has to be used, DOCSTRING if non-NIL defines the documentation of the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3305 ;;definition, INTERACTIVE if non-NIL is the interactive form to be used,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3306 ;;ORIG is a form that calls the body of the original unadvised function,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3307 ;;and BEFORES, AROUNDS and AFTERS are the lists of advices with which ORIG
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3308 ;;should be modified. The assembled function will be returned."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3309
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3310 (let (before-forms around-form around-form-protected after-forms definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3311 (ad-dolist (advice befores)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3312 (cond ((and (ad-advice-protected advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3313 before-forms)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3314 (setq before-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3315 (` ((unwind-protect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3316 (, (ad-prognify before-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3317 (,@ (ad-body-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3318 (ad-advice-definition advice))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3319 (t (setq before-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3320 (append before-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3321 (ad-body-forms (ad-advice-definition advice)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3322
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3323 (setq around-form (` (setq ad-return-value (, orig))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3324 (ad-dolist (advice (reverse arounds))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3325 ;; If any of the around advices is protected then we
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3326 ;; protect the complete around advice onion:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3327 (if (ad-advice-protected advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3328 (setq around-form-protected t))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3329 (setq around-form
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3330 (ad-substitute-tree
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3331 (function (lambda (form) (eq form 'ad-do-it)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3332 (function (lambda (form) around-form))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3333 (ad-prognify (ad-body-forms (ad-advice-definition advice))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3334
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3335 (setq after-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3336 (if (and around-form-protected before-forms)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3337 (` ((unwind-protect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3338 (, (ad-prognify before-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3339 (, around-form))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3340 (append before-forms (list around-form))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3341 (ad-dolist (advice afters)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3342 (cond ((and (ad-advice-protected advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3343 after-forms)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3344 (setq after-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3345 (` ((unwind-protect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3346 (, (ad-prognify after-forms))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3347 (,@ (ad-body-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3348 (ad-advice-definition advice))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3349 (t (setq after-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3350 (append after-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3351 (ad-body-forms (ad-advice-definition advice)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3352
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3353 (setq definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3354 (` ((,@ (if (memq type '(macro special-form)) '(macro)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3355 lambda
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3356 (, args)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3357 (,@ (if docstring (list docstring)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3358 (,@ (if interactive (list interactive)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3359 (let (ad-return-value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3360 (,@ after-forms)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3361 (, (if (eq type 'special-form)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3362 '(list 'quote ad-return-value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3363 'ad-return-value))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3364
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3365 (ad-insert-argument-access-forms definition args)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3366
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3367 ;; This is needed for activation/deactivation hooks:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3368 (defun ad-make-hook-form (function hook-name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3369 ;;"Makes hook-form from FUNCTION's advice bodies in class HOOK-NAME."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3370 (let ((hook-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3371 (mapcar (function (lambda (advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3372 (ad-body-forms (ad-advice-definition advice))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3373 (ad-get-enabled-advices function hook-name))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3374 (if hook-forms
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3375 (ad-prognify (apply 'append hook-forms)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3376
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3377
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3378 ;; @@ Caching:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3379 ;; ===========
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3380 ;; Generating an advised definition of a function is moderately expensive,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3381 ;; hence, it makes sense to cache it so we can reuse it in appropriate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3382 ;; circumstances. Of course, it only makes sense to reuse a cached
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3383 ;; definition if the current advice and function definition state is the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3384 ;; same as it was at the time when the cached definition was generated.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3385 ;; For that purpose we associate every cache with an id so we can verify
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3386 ;; if it is still valid at a certain point in time. This id mechanism
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3387 ;; makes it possible to preactivate advised functions, write the compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3388 ;; advised definitions to a file and reuse them during the actual
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3389 ;; activation without having to risk that the resulting definition will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3390 ;; incorrect, well, almost.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3391 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3392 ;; A cache id is a list with six elements:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3393 ;; 1) the list of names of enabled before advices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3394 ;; 2) the list of names of enabled around advices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3395 ;; 3) the list of names of enabled after advices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3396 ;; 4) the type of the original function (macro, subr, etc.)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3397 ;; 5) the arglist of the original definition (or t if it was equal to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3398 ;; arglist of the cached definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3399 ;; 6) t if the interactive form of the original definition was equal to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3400 ;; interactive form of the cached definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3401 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3402 ;; Here's how a cache can get invalidated or be incorrect:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3403 ;; A) a piece of advice used in the cache gets redefined
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3404 ;; B) the current list of enabled advices is different from the ones used
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3405 ;; for the cache
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3406 ;; C) the type of the original function changed, e.g., a function became a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3407 ;; macro, or a subr became a function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3408 ;; D) the arglist of the original function changed
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3409 ;; E) the interactive form of the original function changed
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3410 ;; F) a piece of advice used in the cache got redefined before the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3411 ;; defadvice with the cached definition got loaded: This is a PROBLEM!
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3412 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3413 ;; Cases A and B are the normal ones. A is taken care of by `ad-add-advice'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3414 ;; which clears the cache in such a case, B is easily checked during
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3415 ;; verification at activation time.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3416 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3417 ;; Cases C, D and E have to be considered if one is slightly paranoid, i.e.,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3418 ;; if one considers the case that the original function could be different
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3419 ;; from the one available at caching time (e.g., for forward advice of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3420 ;; functions that get redefined by some packages - such as `eval-region' gets
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3421 ;; redefined by edebug). All these cases can be easily checked during
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3422 ;; verification. Element 4 of the id lets one check case C, element 5 takes
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3423 ;; care of case D (using t in the equality case saves some space, because the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3424 ;; arglist can be recovered at validation time from the cached definition),
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3425 ;; and element 6 takes care of case E which is only a problem if the original
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3426 ;; was actually a function whose interactive form was not overridden by a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3427 ;; piece of advice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3428 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3429 ;; Case F is the only one which will lead to an incorrect advised function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3430 ;; There is no way to avoid this without storing the complete advice definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3431 ;; in the cache-id which is not feasible.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3432 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3433 ;; The cache-id of a typical advised function with one piece of advice and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3434 ;; no arglist redefinition takes 7 conses which is a small price to pay for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3435 ;; the added efficiency. The validation itself is also pretty cheap, certainly
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3436 ;; a lot cheaper than reconstructing an advised definition.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3437
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3438 (defmacro ad-get-cache-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3439 (` (car (ad-get-advice-info-field (, function) 'cache))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3440
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3441 (defmacro ad-get-cache-id (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3442 (` (cdr (ad-get-advice-info-field (, function) 'cache))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3443
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3444 (defmacro ad-set-cache (function definition id)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3445 (` (ad-set-advice-info-field
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3446 (, function) 'cache (cons (, definition) (, id)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3447
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3448 (defun ad-clear-cache (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3449 "Clears a previously cached advised definition of FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3450 Clear the cache if you want to force `ad-activate' to construct a new
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3451 advised definition from scratch."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3452 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3453 (list (ad-read-advised-function "Clear cached definition of: ")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3454 (ad-set-advice-info-field function 'cache nil))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3455
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3456 (defun ad-make-cache-id (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3457 ;;"Generates an identifying image of the current advices of FUNCTION."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3458 (let ((original-definition (ad-real-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3459 (cached-definition (ad-get-cache-definition function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3460 (list (mapcar (function (lambda (advice) (ad-advice-name advice)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3461 (ad-get-enabled-advices function 'before))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3462 (mapcar (function (lambda (advice) (ad-advice-name advice)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3463 (ad-get-enabled-advices function 'around))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3464 (mapcar (function (lambda (advice) (ad-advice-name advice)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3465 (ad-get-enabled-advices function 'after))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3466 (ad-definition-type original-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3467 (if (equal (ad-arglist original-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3468 (ad-arglist cached-definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3469 t
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3470 (ad-arglist original-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3471 (if (eq (ad-definition-type original-definition) 'function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3472 (equal (ad-interactive-form original-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3473 (ad-interactive-form cached-definition))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3474
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3475 (defun ad-get-cache-class-id (function class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3476 ;;"Returns the part of FUNCTION's cache id that identifies CLASS."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3477 (let ((cache-id (ad-get-cache-id function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3478 (if (eq class 'before)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3479 (car cache-id)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3480 (if (eq class 'around)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3481 (nth 1 cache-id)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3482 (nth 2 cache-id)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3483
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3484 (defun ad-verify-cache-class-id (cache-class-id advices)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3485 (ad-dolist (advice advices (null cache-class-id))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3486 (if (ad-advice-enabled advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3487 (if (eq (car cache-class-id) (ad-advice-name advice))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3488 (setq cache-class-id (cdr cache-class-id))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3489 (ad-do-return nil)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3490
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3491 ;; There should be a way to monitor if and why a cache verification failed
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3492 ;; in order to determine whether a certain preactivation could be used or
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3493 ;; not. Right now the only way to find out is to trace
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3494 ;; `ad-cache-id-verification-code'. The code it returns indicates where the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3495 ;; verification failed. Tracing `ad-verify-cache-class-id' might provide
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3496 ;; some additional useful information.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3497
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3498 (defun ad-cache-id-verification-code (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3499 (let ((cache-id (ad-get-cache-id function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3500 (code 'before-advice-mismatch))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3501 (and (ad-verify-cache-class-id
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3502 (car cache-id) (ad-get-advice-info-field function 'before))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3503 (setq code 'around-advice-mismatch)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3504 (ad-verify-cache-class-id
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3505 (nth 1 cache-id) (ad-get-advice-info-field function 'around))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3506 (setq code 'after-advice-mismatch)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3507 (ad-verify-cache-class-id
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3508 (nth 2 cache-id) (ad-get-advice-info-field function 'after))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3509 (setq code 'definition-type-mismatch)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3510 (let ((original-definition (ad-real-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3511 (cached-definition (ad-get-cache-definition function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3512 (and (eq (nth 3 cache-id) (ad-definition-type original-definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3513 (setq code 'arglist-mismatch)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3514 (equal (if (eq (nth 4 cache-id) t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3515 (ad-arglist original-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3516 (nth 4 cache-id) )
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3517 (ad-arglist cached-definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3518 (setq code 'interactive-form-mismatch)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3519 (or (null (nth 5 cache-id))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3520 (equal (ad-interactive-form original-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3521 (ad-interactive-form cached-definition)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3522 (setq code 'verified))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3523 code))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3524
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3525 (defun ad-verify-cache-id (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3526 ;;"True if FUNCTION's cache-id is compatible with its current advices."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3527 (eq (ad-cache-id-verification-code function) 'verified))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3528
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3529
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3530 ;; @@ Preactivation:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3531 ;; =================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3532 ;; Preactivation can be used to generate compiled advised definitions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3533 ;; at compile time without having to give up the dynamic runtime flexibility
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3534 ;; of the advice mechanism. Preactivation is a special feature of `defadvice',
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3535 ;; it involves the following steps:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3536 ;; - remembering the function's current state (definition and advice-info)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3537 ;; - advising it with the defined piece of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3538 ;; - clearing its cache
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3539 ;; - generating an interpreted advised definition by activating it, this will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3540 ;; make use of all its current active advice and its current definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3541 ;; - saving the so generated cached definition and id
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3542 ;; - resetting the function's advice and definition state to what it was
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3543 ;; before the preactivation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3544 ;; - Returning the saved definition and its id to be used in the expansion of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3545 ;; `defadvice' to assign it as an initial cache, hence it will be compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3546 ;; at time the `defadvice' gets compiled (for v18 byte-compilers the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3547 ;; `defadvice' needs to be in the body of a `defun' for that to occur).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3548 ;; Naturally, for preactivation to be effective it has to be applied/compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3549 ;; at the right time, i.e., when the current state of advices and function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3550 ;; definition exactly reflects the state at activation time. Should that not
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3551 ;; be the case, the precompiled definition will just be discarded and a new
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3552 ;; advised definition will be generated.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3553
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3554 (defun ad-preactivate-advice (function advice class position)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3555 ;;"Preactivates FUNCTION and returns the constructed cache."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3556 (let* ((function-defined-p (fboundp function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3557 (old-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3558 (if function-defined-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3559 (symbol-function function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3560 (old-advice-info (ad-copy-advice-info function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3561 (ad-advised-functions ad-advised-functions))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3562 (unwind-protect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3563 (progn
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3564 (ad-add-advice function advice class position)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3565 (ad-enable-advice function class (ad-advice-name advice))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3566 (ad-clear-cache function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3567 (ad-activate function nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3568 (if (and (ad-is-active function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3569 (ad-get-cache-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3570 (list (ad-get-cache-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3571 (ad-get-cache-id function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3572 (ad-set-advice-info function old-advice-info)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3573 ;; Don't `fset' function to nil if it was previously unbound:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3574 (if function-defined-p
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3575 (ad-real-fset function old-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3576 (fmakunbound function)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3577
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3578 (defun ad-activate-advised-definition (function compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3579 ;;"Redefines FUNCTION with its advised definition from cache or scratch.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3580 ;;If COMPILE is true the resulting FUNCTION will be compiled. The current
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3581 ;;definition and its cache-id will be put into the cache."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3582 (let ((verified-cached-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3583 (if (ad-verify-cache-id function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3584 (ad-get-cache-definition function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3585 (ad-real-fset function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3586 (or verified-cached-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3587 (ad-make-advised-definition function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3588 (if compile (ad-compile-function function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3589 (if verified-cached-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3590 (if (not (eq verified-cached-definition (symbol-function function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3591 ;; we must have compiled, cache the compiled definition:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3592 (ad-set-cache
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3593 function (symbol-function function) (ad-get-cache-id function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3594 ;; We created a new advised definition, cache it with a proper id:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3595 (ad-clear-cache function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3596 ;; ad-make-cache-id needs the new cached definition:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3597 (ad-set-cache function (symbol-function function) nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3598 (ad-set-cache
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3599 function (symbol-function function) (ad-make-cache-id function)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3600
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3601 (defun ad-handle-definition (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3602 "Handles re/definition of an advised FUNCTION during de/activation.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3603 If FUNCTION does not have an original definition associated with it and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3604 the current definition is usable, then it will be stored as FUNCTION's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3605 original definition. If no current definition is available (even in the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3606 case of undefinition) nothing will be done. In the case of redefinition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3607 the action taken depends on the value of `ad-redefinition-action' (which
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3608 see). Redefinition occurs when FUNCTION already has an original definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3609 associated with it but got redefined with a new definition and then
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3610 de/activated. If you do not like the current redefinition action change
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3611 the value of `ad-redefinition-action' and de/activate again."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3612 (let ((original-definition (ad-get-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3613 (current-definition (if (ad-real-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3614 (symbol-function function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3615 (if original-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3616 (if current-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3617 (if (and (not (eq current-definition original-definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3618 ;; Redefinition with an advised definition from a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3619 ;; different function won't count as such:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3620 (not (ad-advised-definition-p current-definition)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3621 ;; we have a redefinition:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3622 (if (not (memq ad-redefinition-action '(accept discard warn)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3623 (error "ad-handle-definition (see its doc): `%s' %s"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3624 function "illegally redefined")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3625 (if (eq ad-redefinition-action 'discard)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3626 (ad-real-fset function original-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3627 (ad-set-orig-definition function current-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3628 (if (eq ad-redefinition-action 'warn)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3629 (message "ad-handle-definition: `%s' got redefined"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3630 function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3631 ;; either advised def or correct original is in place:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3632 nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3633 ;; we have an undefinition, ignore it:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3634 nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3635 (if current-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3636 ;; we have a first definition, save it as original:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3637 (ad-set-orig-definition function current-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3638 ;; we don't have anything noteworthy:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3639 nil))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3640
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3641
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3642 ;; @@ The top-level advice interface:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3643 ;; ==================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3644
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3645 (defun ad-activate (function &optional compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3646 "Activates all the advice information of an advised FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3647 If FUNCTION has a proper original definition then an advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3648 definition will be generated from FUNCTION's advice info and the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3649 definition of FUNCTION will be replaced with it. If a previously
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3650 cached advised definition was available, it will be used. With an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3651 argument (compile is non-NIL) the resulting function (or a compilable
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3652 cached definition) will also be compiled. Activation of an advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3653 function that has an advice info but no actual pieces of advice is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3654 equivalent to a call to `ad-unadvise'. Activation of an advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3655 function that has actual pieces of advice but none of them are enabled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3656 is equivalent to a call to `ad-deactivate'. The current advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3657 definition will always be cached for later usage."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3658 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3659 (list (ad-read-advised-function "Activate advice of: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3660 current-prefix-arg))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3661 (if (not (ad-is-advised function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3662 (error "ad-activate: `%s' is not advised" function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3663 (ad-handle-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3664 ;; Just return for forward advised and not yet defined functions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3665 (if (ad-get-orig-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3666 (if (not (ad-has-any-advice function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3667 (ad-unadvise function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3668 ;; Otherwise activate the advice:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3669 (cond ((ad-has-redefining-advice function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3670 (ad-activate-advised-definition function compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3671 (ad-set-advice-info-field function 'active t)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3672 (eval (ad-make-hook-form function 'activation))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3673 function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3674 ;; Here we are if we have all disabled advices:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3675 (t (ad-deactivate function)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3676
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3677 (defun ad-deactivate (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3678 "Deactivates the advice of an actively advised FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3679 If FUNCTION has a proper original definition, then the current
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3680 definition of FUNCTION will be replaced with it. All the advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3681 information will still be available so it can be activated again with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3682 a call to `ad-activate'."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3683 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3684 (list (ad-read-advised-function "Deactivate advice of: " 'ad-is-active)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3685 (if (not (ad-is-advised function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3686 (error "ad-deactivate: `%s' is not advised" function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3687 (cond ((ad-is-active function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3688 (ad-handle-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3689 (if (not (ad-get-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3690 (error "ad-deactivate: `%s' has no original definition"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3691 function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3692 (ad-real-fset function (ad-get-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3693 (ad-set-advice-info-field function 'active nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3694 (eval (ad-make-hook-form function 'deactivation))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3695 function)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3696
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3697 (defun ad-update (function &optional compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3698 "Update the advised definition of FUNCTION if its advice is active.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3699 With a prefix argument or if the current definition is compiled compile the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3700 resulting advised definition."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3701 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3702 (list (ad-read-advised-function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3703 "Update advised definition of: " 'ad-is-active)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3704 (if (ad-is-active function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3705 (ad-activate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3706 function (or compile (ad-compiled-p (symbol-function function))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3707
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3708 (defun ad-unadvise (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3709 "Deactivates FUNCTION and then removes all its advice information.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3710 If FUNCTION was not advised this will be a noop."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3711 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3712 (list (ad-read-advised-function "Unadvise function: ")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3713 (cond ((ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3714 (if (ad-is-active function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3715 (ad-deactivate function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3716 (ad-clear-orig-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3717 (ad-set-advice-info function nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3718 (ad-pop-advised-function function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3719
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3720 (defun ad-recover (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3721 "Tries to recover FUNCTION's original definition and unadvises it.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3722 This is more low-level than `ad-unadvise' because it does not do any
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3723 deactivation which might run hooks and get into other trouble.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3724 Use in emergencies."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3725 ;; Use more primitive interactive behavior here: Accept any symbol that's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3726 ;; currently defined in obarray, not necessarily with a function definition:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3727 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3728 (list (intern
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3729 (completing-read "Recover advised function: " obarray nil t))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3730 (cond ((ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3731 (cond ((ad-get-orig-definition function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3732 (ad-real-fset function (ad-get-orig-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3733 (ad-clear-orig-definition function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3734 (ad-set-advice-info function nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3735 (ad-pop-advised-function function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3736
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3737 (defun ad-activate-regexp (regexp &optional compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3738 "Activates functions with an advice name containing a REGEXP match.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3739 With prefix argument compiles resulting advised definitions."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3740 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3741 (list (ad-read-regexp "Activate via advice regexp: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3742 current-prefix-arg))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3743 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3744 (if (ad-find-some-advice function 'any regexp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3745 (ad-activate function compile))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3746
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3747 (defun ad-deactivate-regexp (regexp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3748 "Deactivates functions with an advice name containing REGEXP match."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3749 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3750 (list (ad-read-regexp "Deactivate via advice regexp: ")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3751 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3752 (if (ad-find-some-advice function 'any regexp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3753 (ad-deactivate function))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3754
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3755 (defun ad-update-regexp (regexp &optional compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3756 "Updates functions with an advice name containing a REGEXP match.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3757 With prefix argument compiles resulting advised definitions."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3758 (interactive
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3759 (list (ad-read-regexp "Update via advice regexp: ")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3760 current-prefix-arg))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3761 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3762 (if (ad-find-some-advice function 'any regexp)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3763 (ad-update function compile))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3764
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3765 (defun ad-activate-all (&optional compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3766 "Activates all currently advised functions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3767 With prefix argument compiles resulting advised definitions."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3768 (interactive "P")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3769 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3770 (ad-activate function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3771
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3772 (defun ad-deactivate-all ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3773 "Deactivates all currently advised functions."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3774 (interactive)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3775 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3776 (ad-deactivate function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3777
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3778 (defun ad-update-all (&optional compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3779 "Updates all currently advised functions.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3780 With prefix argument compiles resulting advised definitions."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3781 (interactive "P")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3782 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3783 (ad-update function compile)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3784
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3785 (defun ad-unadvise-all ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3786 "Unadvises all currently advised functions."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3787 (interactive)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3788 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3789 (ad-unadvise function)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3790
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3791 (defun ad-recover-all ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3792 "Recovers all currently advised functions. Use in emergencies."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3793 (interactive)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3794 (ad-do-advised-functions (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3795 (condition-case ignore-errors
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3796 (ad-recover function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3797 (error nil))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3798
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3799
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3800 ;; Completion alist of legal `defadvice' flags
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3801 (defvar ad-defadvice-flags
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3802 '(("protect") ("disable") ("activate") ("compile") ("preactivate")))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3803
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3804 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3805 (defmacro defadvice (function args &rest body)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3806 "Defines a piece of advice for FUNCTION (a symbol).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3807
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3808 (defadvice <function> (<class> <name> [<position>] [<arglist>] {<flags>}*)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3809 [ [<documentation-string>] [<interactive-form>] ]
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3810 {<body-form>}* )
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3811
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3812 <function> ::= name of the function to be advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3813 <class> ::= before | around | after | activation | deactivation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3814 <name> ::= non-NIL symbol that names this piece of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3815 <position> ::= first | last | <number> (optional, defaults to `first',
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3816 see also `ad-add-advice')
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3817 <arglist> ::= an optional argument list to be used for the advised function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3818 instead of the argument list of the original. The first one found in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3819 before/around/after advices will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3820 <flags> ::= protect | disable | activate | compile | preactivate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3821 All flags can be specified with unambiguous initial substrings.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3822 <documentation-string> ::= optional documentation for this piece of advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3823 <interactive-form> ::= optional interactive form to be used for the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3824 function. The first one found in before/around/after advices will be used.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3825 <body-form> ::= any s-expression
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3826
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3827 Semantics of the various flags:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3828 `protect': The piece of advice will be protected against non-local exits in
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3829 any code that precedes it. If any around advice of a function is protected
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3830 then automatically all around advices will be protected (the complete onion).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3831
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3832 `activate': All advice of FUNCTION will be activated immediately if
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3833 FUNCTION has been properly defined prior to the defadvice.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3834
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3835 `compile': In conjunction with `activate' specifies that the resulting
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3836 advised function should be compiled.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3837
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3838 `disable': The defined advice will be disabled, hence it will not be used
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3839 during activation until somebody enables it.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3840
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3841 `preactivate': Preactivates the advised FUNCTION at macro expansion/compile
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3842 time. This generates a compiled advised definition according to the current
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3843 advice state that will be used during activation if appropriate. Only use
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3844 this if the defadvice gets actually compiled (with a v18 byte-compiler put
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3845 the defadvice into the body of a defun).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3846
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3847 Look at the file advice.el for comprehensive documentation."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3848 (if (not (ad-name-p function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3849 (error "defadvice: Illegal function name: %s" function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3850 (let* ((class (car args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3851 (name (if (not (ad-class-p class))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3852 (error "defadvice: Illegal advice class: %s" class)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3853 (nth 1 args)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3854 (position (if (not (ad-name-p name))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3855 (error "defadvice: Illegal advice name: %s" name)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3856 (setq args (nthcdr 2 args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3857 (if (ad-position-p (car args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3858 (prog1 (car args)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3859 (setq args (cdr args))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3860 (arglist (if (listp (car args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3861 (prog1 (car args)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3862 (setq args (cdr args)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3863 (flags
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3864 (mapcar
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3865 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3866 (lambda (flag)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3867 (let ((completion
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3868 (try-completion (symbol-name flag) ad-defadvice-flags)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3869 (cond ((eq completion t) flag)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3870 ((assoc completion ad-defadvice-flags)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3871 (intern completion))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3872 (t (error "defadvice: Illegal or ambiguous flag: %s"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3873 flag))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3874 args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3875 (advice (ad-make-advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3876 name (memq 'protect flags)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3877 (not (memq 'disable flags))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3878 (` (advice lambda (, arglist) (,@ body)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3879 (preactivation (if (memq 'preactivate flags)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3880 (ad-preactivate-advice
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3881 function advice class position))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3882 ;; Now for the things to be done at evaluation time:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3883 (` (progn
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3884 (ad-add-advice '(, function) '(, advice) '(, class) '(, position))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3885 (,@ (if preactivation
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3886 (` ((ad-set-cache
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3887 '(, function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3888 ;; the function will get compiled:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3889 (, (cond ((ad-macro-p (car preactivation))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3890 (` (ad-macrofy
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3891 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3892 (, (ad-lambdafy
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3893 (car preactivation)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3894 (t (` (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3895 (, (car preactivation)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3896 '(, (car (cdr preactivation))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3897 (,@ (if (memq 'activate flags)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3898 (` ((ad-activate '(, function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3899 (, (if (memq 'compile flags) t)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3900 '(, function)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3901
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3902
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3903 ;; @@ Tools:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3904 ;; =========
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3905
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3906 (defmacro ad-with-originals (functions &rest body)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3907 "Binds FUNCTIONS to their original definitions and executes BODY.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3908 For any members of FUNCTIONS that are not currently advised the rebinding will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3909 be a noop. Any modifications done to the definitions of FUNCTIONS will be
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3910 undone on exit of this macro."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3911 (let* ((index -1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3912 ;; Make let-variables to store current definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3913 (current-bindings
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3914 (mapcar (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3915 (lambda (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3916 (setq index (1+ index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3917 (list (intern (format "ad-oRiGdEf-%d" index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3918 (` (symbol-function '(, function))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3919 functions)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3920 (` (let (, current-bindings)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3921 (unwind-protect
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3922 (progn
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3923 (,@ (progn
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3924 ;; Make forms to redefine functions to their
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3925 ;; original definitions if they are advised:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3926 (setq index -1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3927 (mapcar
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3928 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3929 (lambda (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3930 (setq index (1+ index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3931 (` (ad-real-fset
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3932 '(, function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3933 (or (ad-get-orig-definition '(, function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3934 (, (car (nth index current-bindings))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3935 functions)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3936 (,@ body))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3937 (,@ (progn
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3938 ;; Make forms to back-define functions to the definitions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3939 ;; they had outside this macro call:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3940 (setq index -1)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3941 (mapcar
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3942 (function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3943 (lambda (function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3944 (setq index (1+ index))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3945 (` (ad-real-fset
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3946 '(, function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3947 (, (car (nth index current-bindings)))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3948 functions))))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3949
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3950 (if (not (get 'ad-with-originals 'lisp-indent-hook))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3951 (put 'ad-with-originals 'lisp-indent-hook 1))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3952
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3953
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3954 ;; @@ Advising `defun', `defmacro', `fset' and `documentation'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3955 ;; ===========================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3956 ;; Use the advice mechanism to advise defun/defmacro/fset so we can forward
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3957 ;; advise functions that might be defined later during load/autoload.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3958 ;; Enabling forward advice was the original motivation for doing this, it
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3959 ;; has now been generalized to running definition hooks so other packages
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3960 ;; can make use of this sort of functionality too.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3961
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3962 (defvar ad-defined-function nil)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3963
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3964 (defun ad-activate-defined-function (&optional function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3965 "Activates the advice of an advised and defined FUNCTION.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3966 If the current definition of FUNCTION is byte-compiled then the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3967 definition will be compiled too. FUNCTION defaults to the value of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3968 `ad-defined-function'."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3969 (if (and (null function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3970 ad-defined-function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3971 (setq function ad-defined-function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3972 (if (and (ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3973 (ad-real-definition function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3974 (ad-activate function (ad-compiled-p (symbol-function function)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3975
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3976 ;; Define some subr arglists for the benefit of v18. Do this here because
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3977 ;; they have to be available at compile/preactivation time. Use the same
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3978 ;; as defined in Lemacs' DOC file:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3979 (cond ((not ad-emacs19-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3980 (ad-define-subr-args 'documentation '(fun1))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3981 (ad-define-subr-args 'fset '(sym newdef))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3982
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3983 ;; A kludge to get `defadvice's compiled with a v18 compiler:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3984 (defun ad-execute-defadvices ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3985
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3986 (defadvice defun (after ad-definition-hooks first disable preact)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3987 "Whenever a function gets re/defined with `defun' all hook functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3988 in `ad-definition-hooks' will be run after the re/definition with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3989 `ad-defined-function' bound to the name of the function."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3990 (let ((ad-defined-function (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3991 (run-hooks 'ad-definition-hooks)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3992
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3993 (defadvice defmacro (after ad-definition-hooks first disable preact)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3994 "Whenever a macro gets re/defined with `defmacro' all hook functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3995 in `ad-definition-hooks' will be run after the re/definition with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3996 `ad-defined-function' bound to the name of the function."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3997 (let ((ad-defined-function (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3998 (run-hooks 'ad-definition-hooks)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
3999
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4000 (defadvice fset (after ad-definition-hooks first disable preact)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4001 "Whenever a function gets re/defined with `fset' all hook functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4002 in `ad-definition-hooks' will be run after the re/definition with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4003 `ad-defined-function' bound to the name of the function. This advice was
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4004 mainly created to handle forward-advice for byte-compiled files created
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4005 by jwz's byte-compiler used in Lemacs.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4006 CAUTION: If you need the primitive `fset' behavior either deactivate
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4007 its advice or use `ad-real-fset' instead!"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4008 (let ((ad-defined-function (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4009 (run-hooks 'ad-definition-hooks)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4010
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4011 ;; Needed for GNU Emacs-19 (in v18s and Lemacs this is just a noop):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4012 (defadvice defalias (after ad-definition-hooks first disable preact)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4013 "Whenever a function gets re/defined with `defalias' all hook functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4014 in `ad-definition-hooks' will be run after the re/definition with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4015 `ad-defined-function' bound to the name of the function. This advice was
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4016 mainly created to handle forward-advice for byte-compiled files created
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4017 by jwz's byte-compiler used in GNU Emacs-19."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4018 (let ((ad-defined-function (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4019 ;; The new `byte-compile' uses `defalias' to set the definition which
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4020 ;; leads to infinite recursion if it gets to use the advised version
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4021 ;; (with `fset' this didn't matter because the compiled `byte-compile'
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4022 ;; called it via its byte-code). Should there be a general provision to
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4023 ;; avoid recursive application of definition hooks?
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4024 (ad-with-originals (defalias)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4025 (run-hooks 'ad-definition-hooks))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4026
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4027 ;; Needed for GNU Emacs-19 (seems to be an identical copy of `defalias',
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4028 ;; it is used by simple.el and might be used later, hence, advise it):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4029 (defadvice define-function (after ad-definition-hooks first disable preact)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4030 "Whenever a function gets re/defined with `define-function' all hook
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4031 functions in `ad-definition-hooks' will be run after the re/definition with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4032 `ad-defined-function' bound to the name of the function."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4033 (let ((ad-defined-function (ad-get-arg 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4034 (ad-with-originals (define-function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4035 (run-hooks 'ad-definition-hooks))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4036
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4037 (defadvice documentation (after ad-advised-docstring first disable preact)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4038 "Builds an advised docstring if FUNCTION is advised."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4039 ;; Because we get the function name from the advised docstring
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4040 ;; this will work for function names as well as for definitions:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4041 (if (and (stringp ad-return-value)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4042 (string-match
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4043 ad-advised-definition-docstring-regexp ad-return-value))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4044 (let ((function
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4045 (car (read-from-string
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4046 ad-return-value (match-beginning 1) (match-end 1)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4047 (cond ((ad-is-advised function)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4048 (setq ad-return-value (ad-make-advised-docstring function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4049 ;; Handle GNU Emacs-19's optional `raw' argument:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4050 (if (not (ad-get-arg 1))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4051 (setq ad-return-value
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4052 (substitute-command-keys ad-return-value))))))))
5746
94535442be19 (ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we
Karl Heuer <kwzh@gnu.org>
parents: 5140
diff changeset
4053
94535442be19 (ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we
Karl Heuer <kwzh@gnu.org>
parents: 5140
diff changeset
4054 ;; Make sure advice-infos are not allocated in pure space (right now they
94535442be19 (ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we
Karl Heuer <kwzh@gnu.org>
parents: 5140
diff changeset
4055 ;; are constants that are part of `ad-execute-defadvices's definition):
94535442be19 (ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we
Karl Heuer <kwzh@gnu.org>
parents: 5140
diff changeset
4056 (ad-dolist (advised-function '(defun defmacro fset defalias
94535442be19 (ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we
Karl Heuer <kwzh@gnu.org>
parents: 5140
diff changeset
4057 define-function documentation))
94535442be19 (ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we
Karl Heuer <kwzh@gnu.org>
parents: 5140
diff changeset
4058 (ad-set-advice-info advised-function (ad-copy-advice-info advised-function)))
4110
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4059
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4060 ) ;; end of ad-execute-defadvices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4061
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4062 ;; Only run this once we are compiled. Expanding the defadvices
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4063 ;; with only interpreted advice functions available takes forever:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4064 (if (ad-compiled-p (symbol-function 'ad-execute-defadvices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4065 (ad-execute-defadvices))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4066
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4067
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4068 ;; @@ Forward advice support for jwz's byte-compiler (M-x serious-HACK-mode-on)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4069 ;; ============================================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4070 ;; Jamie Zawinski's optimizing byte-compiler used in v19 (and by some daring
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4071 ;; folks in v18) produces compiled files that do not define functions via
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4072 ;; explicit calls to `defun/defmacro', it rather uses `fset' for functions with
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4073 ;; documentation strings, and hunks of byte-code for sets of functions without
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4074 ;; any documentation. In Jamie's byte-compiler a series of compiled functions
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4075 ;; without docstrings get hunked as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4076 ;; (progn (fset 'f1 <code1>) (fset 'f2 <code2>) ...).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4077 ;; The resulting progn will be compiled and the compiled form will be written
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4078 ;; to the compiled file as `(byte-code [progn-code] [constants] [depth])'. To
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4079 ;; handle forward advice we have to know when functions get defined so we can
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4080 ;; activate any advice there might be. For standard v18 byte-compiled files
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4081 ;; we can do this by simply advising `defun/defmacro' because these subrs are
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4082 ;; evaluated explicitly when such a file is loaded. For Jamie's v19 compiler
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4083 ;; our only choice is to additionally advise `fset' and change the subr
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4084 ;; `byte-code' such that it analyzes its byte-code string looking for fset's
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4085 ;; when we are currently loading a file. In v19 the general overhead caused
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4086 ;; by the advice of `byte-code' shouldn't be too bad, because byte-compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4087 ;; functions do not call byte-code explicitly (as done in v18). In v18 this
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4088 ;; is a problem because with the changed `byte-code' function function calls
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4089 ;; become more expensive.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4090 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4091 ;; Wish-List:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4092 ;; - special defining functions for use in byte-compiled files, e.g.,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4093 ;; `byte-compile-fset' and `byte-code-tl' which do the same as their
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4094 ;; standard brothers, but which can be advised for forward advice without
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4095 ;; the problems that advising `byte-code' generates.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4096 ;; - More generally, a symbol definition hook that could be used for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4097 ;; forward advice and related purposes.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4098 ;;
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4099 ;; Until then: For the analysis of the byte-code string we simply scan it for
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4100 ;; an `fset' opcode (M in ascii) that is preceded by two constant references,
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4101 ;; the first of which points to the function name and the second to its code.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4102 ;; A constant reference can either be a simple one-byte one, or a three-byte
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4103 ;; one if the function has more than 64 constants. The scanning can pretty
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4104 ;; efficiently be done with a regular expression. Here it goes:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4105
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4106 ;; Have to hardcode these opcodes if I don't
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4107 ;; want to require the byte-compiler:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4108 (defvar byte-constant 192)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4109 (defvar byte-constant-limit 64)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4110 (defvar byte-constant2 129)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4111 (defvar byte-fset 77)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4112
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4113 ;; Matches a byte-compiled fset operation with two constant arguments:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4114 (defvar ad-byte-code-fset-regexp
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4115 (let* ((constant-reference
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4116 (format "[%s-%s]"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4117 (char-to-string byte-constant)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4118 (char-to-string (+ byte-constant (1- byte-constant-limit)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4119 (constant2-reference
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4120 ;; \0 makes it necessary to use concat instead of format in 18.57:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4121 (concat (char-to-string byte-constant2) "[\0-\377][\0-\377]"))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4122 (fset-opcode (char-to-string byte-fset)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4123 (concat "\\(" constant-reference "\\|" constant2-reference "\\)"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4124 "\\(" constant-reference "\\|" constant2-reference "\\)"
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4125 fset-opcode)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4126
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4127 (defun ad-find-fset-in-byte-code (code constants start)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4128 ;;"Finds the first two-constant fset operation in CODE after START.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4129 ;;Returns a three element list consisting of the name of the defined
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4130 ;;function, its code (both taken from the CONSTANTS vector), and an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4131 ;;advanced start index."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4132 (let ((start
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4133 ;; The odd case that this regexp matches something that isn't an
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4134 ;; actual fset operation is handled by additional tests and a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4135 ;; condition handler in ad-scan-byte-code-for-fsets:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4136 (string-match ad-byte-code-fset-regexp code start))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4137 name-index code-index)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4138 (cond (start
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4139 (cond ((= (aref code start) byte-constant2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4140 (setq name-index
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4141 (+ (aref code (setq start (1+ start)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4142 (* (aref code (setq start (1+ start))) 256)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4143 (setq start (1+ start)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4144 (t (setq name-index (- (aref code start) byte-constant))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4145 (setq start (1+ start))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4146 (cond ((= (aref code start) byte-constant2)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4147 (setq code-index
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4148 (+ (aref code (setq start (1+ start)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4149 (* (aref code (setq start (1+ start))) 256)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4150 (setq start (1+ start)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4151 (t (setq code-index (- (aref code start) byte-constant))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4152 (setq start (1+ start))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4153 (list (aref constants name-index)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4154 (aref constants code-index)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4155 ;; start points to fset opcode:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4156 start))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4157 (t nil))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4158
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4159 (defun ad-scan-byte-code-for-fsets (ad-code ad-constants)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4160 ;; In case anything in here goes wrong we reset `byte-code' to its real
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4161 ;; identity. In particular, the handler of the condition-case uses
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4162 ;; `byte-code', so it better be the real one if we have an error:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4163 (ad-real-fset 'byte-code (symbol-function 'ad-real-byte-code))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4164 (condition-case ignore-errors
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4165 (let ((fset-args '(0 0 0)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4166 (while (setq fset-args (ad-find-fset-in-byte-code
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4167 ad-code ad-constants
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4168 (car (cdr (cdr fset-args)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4169 (if (and (symbolp (car fset-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4170 (fboundp (car fset-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4171 (eq (symbol-function (car fset-args))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4172 (car (cdr fset-args))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4173 ;; We've found an fset that was executed during this call
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4174 ;; to byte-code, and whose definition is still eq to the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4175 ;; current definition of the defined function:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4176 (let ((ad-defined-function (car fset-args)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4177 (run-hooks 'ad-definition-hooks))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4178 ;; Everything worked fine, readvise `byte-code':
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4179 (ad-real-fset 'byte-code (symbol-function 'ad-advised-byte-code)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4180 (error nil)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4181
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4182 ;; CAUTION: Don't try this at home!! Changing `byte-code' is a
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4183 ;; pretty suicidal activity.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4184 ;; To allow v19 forward advice we cannot advise `byte-code' as a subr as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4185 ;; we did for `defun' etc., because `ad-subr-args' of the advised
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4186 ;; `byte-code' would shield references to `ad-subr-args' in the body of
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4187 ;; v18 compiled advised subrs such as `defun', and, more importantly, the
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4188 ;; changed version of `byte-code' has to be as small and efficient as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4189 ;; possible because it is used in every call to a compiled function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4190 ;; Hence, we previously saved its original definition and redefine it as
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4191 ;; the following function - yuck:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4192
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4193 ;; The arguments will scope around the body of every byte-compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4194 ;; function, hence they have to be obscure enough to not be equal to any
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4195 ;; global or argument variable referenced by any compiled function:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4196 (defun ad-advised-byte-code-definition (ad-cOdE ad-cOnStAnTs ad-dEpTh)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4197 "Modified version of `byte-code' subr used by the advice package.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4198 `byte-code' has been modified to allow automatic activation of forward
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4199 advice for functions that are defined in byte-compiled files generated
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4200 by jwz's byte-compiler (as standardly used in v19s).
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4201 See `ad-real-byte-code' for original documentation."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4202 (prog1 (ad-real-byte-code ad-cOdE ad-cOnStAnTs ad-dEpTh)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4203 (if load-in-progress
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4204 (ad-scan-byte-code-for-fsets ad-cOdE ad-cOnStAnTs))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4205
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4206 (ad-real-byte-codify 'ad-advised-byte-code-definition)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4207
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4208 ;; ad-advised-byte-code cannot be defined with `defun', because that would
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4209 ;; use `byte-code' for its body --> major disaster if forward advice is
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4210 ;; enabled and this file gets loaded:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4211 (ad-real-fset
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4212 'ad-advised-byte-code (symbol-function 'ad-advised-byte-code-definition))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4213
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4214 (defun ad-recover-byte-code ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4215 "Recovers the real `byte-code' functionality."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4216 (interactive)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4217 (ad-real-fset 'byte-code (symbol-function 'ad-real-byte-code)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4218
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4219 ;; Make sure this is usable even if `byte-code' is screwed up:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4220 (ad-real-byte-codify 'ad-recover-byte-code)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4221
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4222 ;; Store original stack sizes because we might have to change them:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4223 (defvar ad-orig-max-lisp-eval-depth max-lisp-eval-depth)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4224 (defvar ad-orig-max-specpdl-size max-specpdl-size)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4225
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4226 (defun ad-adjust-stack-sizes (&optional reset)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4227 "Increases stack sizes for the advised `byte-code' function.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4228 When called with a prefix argument the stack sizes will be reset
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4229 to their original values. Calling this function should only be necessary
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4230 if you get stack overflows because you run highly recursive v18 compiled
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4231 code in a v19 Emacs with definition hooks enabled."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4232 (interactive "P")
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4233 (cond (reset
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4234 (setq max-lisp-eval-depth ad-orig-max-lisp-eval-depth)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4235 (setq max-specpdl-size ad-orig-max-specpdl-size))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4236 (t ;; The redefined `byte-code' needs more execution stack
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4237 ;; (5 cells per function invocation) and variable stack
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4238 ;; (3 vars per function invocation):
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4239 (setq max-lisp-eval-depth (* ad-orig-max-lisp-eval-depth 3))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4240 (setq max-specpdl-size
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4241 (+ ad-orig-max-specpdl-size (* (/ max-lisp-eval-depth 5) 3))))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4242
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4243 (defun ad-enable-definition-hooks ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4244 ;;"Enables definition hooks by redefining definition primitives.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4245 ;;Activates the advice of defun/defmacro/fset and possibly redefines
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4246 ;;`byte-code' if a v19 byte-compiler is used. Redefining these primitives
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4247 ;;might lead to problems. Use `ad-disable-definition-hooks' or
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4248 ;;`ad-stop-advice' in such a case to establish a safe state."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4249 (ad-dolist (definer '(defun defmacro fset defalias define-function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4250 (ad-enable-advice definer 'after 'ad-definition-hooks)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4251 (ad-activate definer 'compile))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4252 (cond (ad-use-jwz-byte-compiler
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4253 (ad-real-byte-codify 'ad-advised-byte-code)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4254 (ad-real-byte-codify 'ad-scan-byte-code-for-fsets)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4255 ;; Now redefine byte-code...
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4256 (ad-real-fset 'byte-code (symbol-function 'ad-advised-byte-code))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4257 ;; Only increase stack sizes in v18s, even though old-fashioned
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4258 ;; v18 byte-code might be run in a v19, in which case one can call
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4259 ;; `ad-adjust-stack-sizes' interactively if stacks become too small:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4260 (if (not ad-emacs19-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4261 (ad-adjust-stack-sizes)))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4262
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4263 (defun ad-disable-definition-hooks ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4264 ;;"Disables definition hooks by resetting definition primitives."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4265 (ad-recover-byte-code)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4266 (ad-dolist (definer '(defun defmacro fset defalias define-function))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4267 (ad-disable-advice definer 'after 'ad-definition-hooks)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4268 (ad-update definer))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4269 (if (not ad-emacs19-p)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4270 (ad-adjust-stack-sizes 'reset)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4271
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4272 (ad-real-byte-codify 'ad-disable-definition-hooks)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4273
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4274
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4275 ;; @@ Starting, stopping and recovering from the advice package magic:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4276 ;; ===================================================================
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4277
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4278 ;;;###autoload
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4279 (defun ad-start-advice ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4280 "Redefines some primitives to start the advice magic.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4281 If `ad-activate-on-definition' is t then advice information will
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4282 automatically get activated whenever an advised function gets defined or
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4283 redefined. This will enable goodies such as forward advice and
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4284 automatically enable function definition hooks. If its value is nil but
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4285 the value of `ad-enable-definition-hooks' is t then definition hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4286 will be enabled without having automatic advice activation, otherwise
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4287 function definition hooks will be disabled too. If definition hooks are
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4288 enabled then functions stored in `ad-definition-hooks' are run whenever
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4289 a function gets defined or redefined."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4290 (interactive)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4291 (ad-enable-advice 'documentation 'after 'ad-advised-docstring)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4292 (ad-activate 'documentation 'compile)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4293 (if (or ad-activate-on-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4294 ad-enable-definition-hooks)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4295 (ad-enable-definition-hooks)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4296 (ad-disable-definition-hooks))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4297 (setq ad-definition-hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4298 (if ad-activate-on-definition
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4299 (if (memq 'ad-activate-defined-function ad-definition-hooks)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4300 ad-definition-hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4301 (cons 'ad-activate-defined-function ad-definition-hooks))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4302 (delq 'ad-activate-defined-function ad-definition-hooks))))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4303
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4304 (defun ad-stop-advice ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4305 "Undefines some primitives to stop the advice magic.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4306 This can also be used to recover from advice related emergencies."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4307 (interactive)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4308 (ad-recover-byte-code)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4309 (ad-disable-advice 'documentation 'after 'ad-advised-docstring)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4310 (ad-update 'documentation)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4311 (ad-disable-definition-hooks)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4312 (setq ad-definition-hooks
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4313 (delq 'ad-activate-defined-function ad-definition-hooks)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4314
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4315 (ad-real-byte-codify 'ad-stop-advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4316
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4317 (defun ad-recover-normality ()
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4318 "Undoes all advice related redefinitions and unadvises everything.
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4319 Use only in REAL emergencies."
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4320 (interactive)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4321 (ad-recover-byte-code)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4322 (ad-recover-all)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4323 (setq ad-advised-functions nil))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4324
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4325 (ad-real-byte-codify 'ad-recover-normality)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4326
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4327 (if (and ad-start-advice-on-load
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4328 ;; ...but only if we are compiled:
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4329 (ad-compiled-p (symbol-function 'ad-execute-defadvices)))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4330 (ad-start-advice))
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4331
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4332 (provide 'advice)
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4333
ccdff27edd2e Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4334 ;;; advice.el ends here