Mercurial > emacs
annotate lisp/emacs-lisp/advice.el @ 109635:fc7a8c411aa3
Add declarations to header files.
* src/keyboard.h (xmalloc_widget_value, digest_single_submenu): Remove
declarations, menu.h has them.
(QCbutton, QCtoggle, QCradio, QClabel, extra_keyboard_modifiers)
(Vinput_method_function, Qinput_method_function)
(Qevent_symbol_element_mask, last_event_timestamp):
* src/dispextern.h (Voverflow_newline_into_fringe):
* src/font.h (QCantialias, Qp, syms_of_ftfont, syms_of_xfns)
(syms_of_ftxfont, syms_of_xftfont, syms_of_bdffont)
(syms_of_w32font, syms_of_nsfont):
* src/fontset.h (find_font_encoding, Qlatin):
* src/frame.h (Qtooltip, Qrun_hook_with_args, Vmenu_bar_mode)
(Vtool_bar_mode, set_frame_menubar):
* src/ftfont.h (ftfont_font_format, ftfont_get_fc_charset):
* src/xterm.h (Qx_gtk_map_stock):
* src/keymap.h (meta_prefix_char): Add declarations.
* src/lisp.h: Remove HAVE_SHM code, unused.
(QCmap, QCrehash_size, QCrehash_threshold, QCsize, QCtest)
(QCweakness, Qabove_handle, Qbackquote, Qbar, Qbelow_handle)
(Qborder, Qbottom, Qbox, Qcircular_list, Qcomma, Qcomma_at)
(Qcomma_dot, Qcursor, Qdefault, Qdown, Qend_scroll, Qeq, Qeql)
(Qequal, Qfile_exists_p, Qfont_param, Qfringe, Qfunction)
(Qfunction_documentation, Qhandle, Qhbar, Qheader_line, Qhollow)
(Qidentity, Qleft_margin, Qmenu, Qmenu_bar_update_hook)
(Qmode_line_inactive, Qmouse, Qoverriding_local_map)
(Qoverriding_terminal_local_map, Qratio, Qregion, Qright_margin)
(Qscroll_bar, Qtool_bar, Qtop, Qup, Qvertical_border, Qwhen)
(Qwindow_scroll_functions, Vafter_load_alist)
(Vauto_save_list_file_name, Vface_alternative_font_family_alist)
(Vface_alternative_font_registry_alist, Vface_font_rescale_alist)
(Vface_ignored_fonts, Vinhibit_redisplay, Vminibuffer_list)
(Vprint_length, Vprint_level, Vscalable_fonts_allowed)
(Vshell_file_name, Vsystem_name, Vwindow_scroll_functions)
(Vwindow_system_version, Vx_no_window_manager, initial_argc)
(initial_argv, last_nonmenu_event, load_in_progress)
(noninteractive_need_newline, scroll_margin): Add declarations.
author | Dan Nicolaescu <dann@ics.uci.edu> |
---|---|
date | Thu, 05 Aug 2010 16:15:24 -0700 |
parents | b9c2b845f2e6 |
children | 280c8ae2476d 376148b31b5e |
rev | line source |
---|---|
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1 ;;; advice.el --- an overloading mechanism for Emacs Lisp functions |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2 |
74466 | 3 ;; Copyright (C) 1993, 1994, 2000, 2001, 2002, 2003, 2004, |
106815 | 4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
4110 | 5 |
6 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu> | |
26622 | 7 ;; Maintainer: FSF |
4110 | 8 ;; Created: 12 Dec 1992 |
5140 | 9 ;; Keywords: extensions, lisp, tools |
4110 | 10 |
11 ;; This file is part of GNU Emacs. | |
12 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93759
diff
changeset
|
13 ;; GNU Emacs is free software: you can redistribute it and/or modify |
4110 | 14 ;; it under the terms of the GNU General Public License as published by |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93759
diff
changeset
|
15 ;; the Free Software Foundation, either version 3 of the License, or |
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93759
diff
changeset
|
16 ;; (at your option) any later version. |
4110 | 17 |
18 ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 ;; GNU General Public License for more details. | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93759
diff
changeset
|
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
4110 | 25 |
26 ;; LCD Archive Entry: | |
27 ;; advice|Hans Chalupsky|hans@cs.buffalo.edu| | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
28 ;; Overloading mechanism for Emacs Lisp functions| |
8458
a95ca44cec95
(ad-subr-arglist): Adapted to new DOC file format.
Richard M. Stallman <rms@gnu.org>
parents:
8445
diff
changeset
|
29 ;; 1994/08/05 03:42:04|2.14|~/packages/advice.el.Z| |
4110 | 30 |
31 | |
32 ;;; Commentary: | |
33 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
34 ;; NOTE: This documentation is slightly out of date. In particular, all the |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
35 ;; references to Emacs-18 are obsolete now, because it is not any longer |
26217 | 36 ;; supported by this version of Advice. |
37 | |
38 ;; Advice is documented in the Emacs Lisp Manual. | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
39 |
4110 | 40 ;; @ Introduction: |
41 ;; =============== | |
42 ;; This package implements a full-fledged Lisp-style advice mechanism | |
26217 | 43 ;; for Emacs Lisp. Advice is a clean and efficient way to modify the |
4110 | 44 ;; behavior of Emacs Lisp functions without having to keep personal |
26217 | 45 ;; modified copies of such functions around. A great number of such |
46 ;; modifications can be achieved by treating the original function as a | |
47 ;; black box and specifying a different execution environment for it | |
4110 | 48 ;; with a piece of advice. Think of a piece of advice as a kind of fancy |
49 ;; hook that you can attach to any function/macro/subr. | |
50 | |
51 ;; @ Highlights: | |
52 ;; ============= | |
53 ;; - Clean definition of multiple, named before/around/after advices | |
54 ;; for functions, macros, subrs and special forms | |
55 ;; - Full control over the arguments an advised function will receive, | |
56 ;; the binding environment in which it will be executed, as well as the | |
57 ;; value it will return. | |
58 ;; - Allows re/definition of interactive behavior for functions and subrs | |
26217 | 59 ;; - Every piece of advice can have its documentation string which will be |
4110 | 60 ;; combined with the original documentation of the advised function at |
61 ;; call-time of `documentation' for proper command-key substitution. | |
62 ;; - The execution of every piece of advice can be protected against error | |
63 ;; and non-local exits in preceding code or advices. | |
64 ;; - Simple argument access either by name, or, more portable but as | |
65 ;; efficient, via access macros | |
66 ;; - Allows the specification of a different argument list for the advised | |
67 ;; version of a function. | |
68 ;; - Advised functions can be byte-compiled either at file-compile time | |
69 ;; (see preactivation) or activation time. | |
70 ;; - Separation of advice definition and activation | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
71 ;; - Forward advice is possible, that is |
4110 | 72 ;; as yet undefined or autoload functions can be advised without having to |
26217 | 73 ;; preload the file in which they are defined. |
4110 | 74 ;; - Forward redefinition is possible because around advice can be used to |
75 ;; completely redefine a function. | |
76 ;; - A caching mechanism for advised definition provides for cheap deactivation | |
77 ;; and reactivation of advised functions. | |
78 ;; - Preactivation allows efficient construction and compilation of advised | |
79 ;; definitions at file compile time without giving up the flexibility of | |
80 ;; the advice mechanism. | |
81 ;; - En/disablement mechanism allows the use of different "views" of advised | |
82 ;; functions depending on what pieces of advice are currently en/disabled | |
26217 | 83 ;; - Provides manipulation mechanisms for sets of advised functions via |
4110 | 84 ;; regular expressions that match advice names |
85 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
86 ;; @ How to get Advice for Emacs-18: |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
87 ;; ================================= |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
88 ;; `advice18.el', a version of Advice that also works in Emacs-18 is available |
26217 | 89 ;; either via anonymous ftp from `ftp.cs.buffalo.edu (128.205.32.9)' with |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
90 ;; pathname `/pub/Emacs/advice18.el', or from one of the Emacs Lisp archive |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
91 ;; sites, or send email to <hans@cs.buffalo.edu> and I'll mail it to you. |
4110 | 92 |
93 ;; @ Overview, or how to read this file: | |
94 ;; ===================================== | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
95 ;; NOTE: This documentation is slightly out of date. In particular, all the |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
96 ;; references to Emacs-18 are obsolete now, because it is not any longer |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
97 ;; supported by this version of Advice. An up-to-date version will soon be |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
98 ;; available as an info file (thanks to the kind help of Jack Vinson and |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
99 ;; David M. Smith). Until then you can use `outline-mode' to help you read |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
100 ;; this documentation (set `outline-regexp' to `";; @+"'). |
4110 | 101 ;; |
102 ;; The four major sections of this file are: | |
103 ;; | |
104 ;; @ This initial information ...installation, customization etc. | |
105 ;; @ Advice documentation: ...general documentation | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
106 ;; @ Foo games: An advice tutorial ...teaches about Advice by example |
4110 | 107 ;; @ Advice implementation: ...actual code, yeah!! |
108 ;; | |
109 ;; The latter three are actual headings which you can search for | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
110 ;; directly in case `outline-mode' doesn't work for you. |
4110 | 111 |
112 ;; @ Restrictions: | |
113 ;; =============== | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
114 ;; - This version of Advice only works for Emacs 19.26 and later. It uses |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
115 ;; new versions of the built-in functions `fset/defalias' which are not |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
116 ;; yet available in Lucid Emacs, hence, it won't work there. |
4110 | 117 ;; - Advised functions/macros/subrs will only exhibit their advised behavior |
118 ;; when they are invoked via their function cell. This means that advice will | |
119 ;; not work for the following: | |
26217 | 120 ;; + advised subrs that are called directly from other subrs or C-code |
121 ;; + advised subrs that got replaced with their byte-code during | |
4110 | 122 ;; byte-compilation (e.g., car) |
123 ;; + advised macros which were expanded during byte-compilation before | |
124 ;; their advice was activated. | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
125 |
4110 | 126 ;; @ Credits: |
127 ;; ========== | |
128 ;; This package is an extension and generalization of packages such as | |
129 ;; insert-hooks.el written by Noah S. Friedman, and advise.el written by | |
130 ;; Raul J. Acevedo. Some ideas used in here come from these packages, | |
131 ;; others come from the various Lisp advice mechanisms I've come across | |
132 ;; so far, and a few are simply mine. | |
133 | |
134 ;; @ Comments, suggestions, bug reports: | |
135 ;; ===================================== | |
136 ;; If you find any bugs, have suggestions for new advice features, find the | |
137 ;; documentation wrong, confusing, incomplete, or otherwise unsatisfactory, | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
138 ;; have any questions about Advice, or have otherwise enlightening |
4110 | 139 ;; comments feel free to send me email at <hans@cs.buffalo.edu>. |
140 | |
141 ;; @ Safety Rules and Emergency Exits: | |
142 ;; =================================== | |
143 ;; Before we begin: CAUTION!! | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
144 ;; Advice provides you with a lot of rope to hang yourself on very |
4110 | 145 ;; easily accessible trees, so, here are a few important things you |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
146 ;; should know: Once Advice has been started with `ad-start-advice' |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
147 ;; (which happens automatically when you load this file), it |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
148 ;; generates an advised definition of the `documentation' function, and |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
149 ;; it will enable automatic advice activation when functions get defined. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
150 ;; All of this can be undone at any time with `M-x ad-stop-advice'. |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
151 ;; |
4110 | 152 ;; If you experience any strange behavior/errors etc. that you attribute to |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
153 ;; Advice or to some ill-advised function do one of the following: |
4110 | 154 |
155 ;; - M-x ad-deactivate FUNCTION (if you have a definite suspicion what | |
156 ;; function gives you problems) | |
157 ;; - M-x ad-deactivate-all (if you don't have a clue what's going wrong) | |
158 ;; - M-x ad-stop-advice (if you think the problem is related to the | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
159 ;; advised functions used by Advice itself) |
4110 | 160 ;; - M-x ad-recover-normality (for real emergencies) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
161 ;; - If none of the above solves your Advice-related problem go to another |
4110 | 162 ;; terminal, kill your Emacs process and send me some hate mail. |
163 | |
164 ;; The first three measures have restarts, i.e., once you've figured out | |
165 ;; the problem you can reactivate advised functions with either `ad-activate', | |
166 ;; `ad-activate-all', or `ad-start-advice'. `ad-recover-normality' unadvises | |
167 ;; everything so you won't be able to reactivate any advised functions, you'll | |
168 ;; have to stick with their standard incarnations for the rest of the session. | |
169 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
170 ;; IMPORTANT: With Advice loaded always do `M-x ad-deactivate-all' before |
4110 | 171 ;; you byte-compile a file, because advised special forms and macros can lead |
172 ;; to unwanted compilation results. When you are done compiling use | |
26217 | 173 ;; `M-x ad-activate-all' to go back to the advised state of all your |
4110 | 174 ;; advised functions. |
175 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
176 ;; RELAX: Advice is pretty safe even if you are oblivious to the above. |
4110 | 177 ;; I use it extensively and haven't run into any serious trouble in a long |
178 ;; time. Just wanted you to be warned. | |
179 | |
180 ;; @ Customization: | |
181 ;; ================ | |
182 | |
183 ;; Look at the documentation of `ad-redefinition-action' for possible values | |
184 ;; of this variable. Its default value is `warn' which will print a warning | |
185 ;; message when an already defined advised function gets redefined with a | |
186 ;; new original definition and de/activated. | |
187 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
188 ;; Look at the documentation of `ad-default-compilation-action' for possible |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
189 ;; values of this variable. Its default value is `maybe' which will compile |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
190 ;; advised definitions during activation in case the byte-compiler is already |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
191 ;; loaded. Otherwise, it will leave them uncompiled. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
192 |
4110 | 193 ;; @ Motivation: |
194 ;; ============= | |
195 ;; Before I go on explaining how advice works, here are four simple examples | |
196 ;; how this package can be used. The first three are very useful, the last one | |
197 ;; is just a joke: | |
198 | |
199 ;;(defadvice switch-to-buffer (before existing-buffers-only activate) | |
26217 | 200 ;; "When called interactively switch to existing buffers only, unless |
4110 | 201 ;;when called with a prefix argument." |
26217 | 202 ;; (interactive |
203 ;; (list (read-buffer "Switch to buffer: " (other-buffer) | |
4110 | 204 ;; (null current-prefix-arg))))) |
205 ;; | |
206 ;;(defadvice switch-to-buffer (around confirm-non-existing-buffers activate) | |
207 ;; "Switch to non-existing buffers only upon confirmation." | |
208 ;; (interactive "BSwitch to buffer: ") | |
209 ;; (if (or (get-buffer (ad-get-arg 0)) | |
210 ;; (y-or-n-p (format "`%s' does not exist, create? " (ad-get-arg 0)))) | |
211 ;; ad-do-it)) | |
212 ;; | |
213 ;;(defadvice find-file (before existing-files-only activate) | |
214 ;; "Find existing files only" | |
215 ;; (interactive "fFind file: ")) | |
216 ;; | |
217 ;;(defadvice car (around interactive activate) | |
218 ;; "Make `car' an interactive function." | |
219 ;; (interactive "xCar of list: ") | |
220 ;; ad-do-it | |
105372
bd2966850aac
Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents:
105371
diff
changeset
|
221 ;; (if (called-interactively-p 'interactive) |
4110 | 222 ;; (message "%s" ad-return-value))) |
223 | |
224 | |
225 ;; @ Advice documentation: | |
226 ;; ======================= | |
227 ;; Below is general documentation of the various features of advice. For more | |
228 ;; concrete examples check the corresponding sections in the tutorial part. | |
229 | |
230 ;; @@ Terminology: | |
231 ;; =============== | |
24875 | 232 ;; - Emacs, Emacs-19: Emacs as released by the GNU Project |
4110 | 233 ;; - Lemacs: Lucid's version of Emacs with major version 19 |
234 ;; - v18: Any Emacs with major version 18 or built as an extension to that | |
235 ;; (such as Epoch) | |
236 ;; - v19: Any Emacs with major version 19 | |
26217 | 237 ;; - jwz: Jamie Zawinski - former keeper of Lemacs and creator of the optimizing |
4110 | 238 ;; byte-compiler used in v19s. |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
239 ;; - Advice: The name of this package. |
4110 | 240 ;; - advices: Short for "pieces of advice". |
241 | |
242 ;; @@ Defining a piece of advice with `defadvice': | |
243 ;; =============================================== | |
244 ;; The main means of defining a piece of advice is the macro `defadvice', | |
245 ;; there is no interactive way of specifying a piece of advice. A call to | |
246 ;; `defadvice' has the following syntax which is similar to the syntax of | |
247 ;; `defun/defmacro': | |
248 ;; | |
249 ;; (defadvice <function> (<class> <name> [<position>] [<arglist>] {<flags>}*) | |
250 ;; [ [<documentation-string>] [<interactive-form>] ] | |
251 ;; {<body-form>}* ) | |
252 | |
253 ;; <function> is the name of the function/macro/subr to be advised. | |
254 | |
255 ;; <class> is the class of the advice which has to be one of `before', | |
256 ;; `around', `after', `activation' or `deactivation' (the last two allow | |
257 ;; definition of special act/deactivation hooks). | |
258 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
259 ;; <name> is the name of the advice which has to be a non-nil symbol. |
4110 | 260 ;; Names uniquely identify a piece of advice in a certain advice class, |
261 ;; hence, advices can be redefined by defining an advice with the same class | |
262 ;; and name. Advice names are global symbols, hence, the same name space | |
263 ;; conventions used for function names should be applied. | |
264 | |
265 ;; An optional <position> specifies where in the current list of advices of | |
266 ;; the specified <class> this new advice will be placed. <position> has to | |
267 ;; be either `first', `last' or a number that specifies a zero-based | |
268 ;; position (`first' is equivalent to 0). If no position is specified | |
269 ;; `first' will be used as a default. If this call to `defadvice' redefines | |
270 ;; an already existing advice (see above) then the position argument will | |
271 ;; be ignored and the position of the already existing advice will be used. | |
272 | |
273 ;; An optional <arglist> which has to be a list can be used to define the | |
274 ;; argument list of the advised function. This argument list should of | |
275 ;; course be compatible with the argument list of the original function, | |
276 ;; otherwise functions that call the advised function with the original | |
277 ;; argument list in mind will break. If more than one advice specify an | |
278 ;; argument list then the first one (the one with the smallest position) | |
279 ;; found in the list of before/around/after advices will be used. | |
280 | |
281 ;; <flags> is a list of symbols that specify further information about the | |
282 ;; advice. All flags can be specified with unambiguous initial substrings. | |
283 ;; `activate': Specifies that the advice information of the advised | |
284 ;; function should be activated right after this advice has been | |
26217 | 285 ;; defined. In forward advices `activate' will be ignored. |
4110 | 286 ;; `protect': Specifies that this advice should be protected against |
287 ;; non-local exits and errors in preceding code/advices. | |
288 ;; `compile': Specifies that the advised function should be byte-compiled. | |
289 ;; This flag will be ignored unless `activate' is also specified. | |
290 ;; `disable': Specifies that the defined advice should be disabled, hence, | |
291 ;; it will not be used in an activation until somebody enables it. | |
292 ;; `preactivate': Specifies that the advised function should get preactivated | |
293 ;; at macro-expansion/compile time of this `defadvice'. This | |
294 ;; generates a compiled advised definition according to the | |
295 ;; current advice state which will be used during activation | |
296 ;; if appropriate. Only use this if the `defadvice' gets | |
297 ;; actually compiled (with a v18 byte-compiler put the `defadvice' | |
298 ;; into the body of a `defun' to accomplish proper compilation). | |
299 | |
300 ;; An optional <documentation-string> can be supplied to document the advice. | |
301 ;; On call of the `documentation' function it will be combined with the | |
302 ;; documentation strings of the original function and other advices. | |
303 | |
304 ;; An optional <interactive-form> form can be supplied to change/add | |
305 ;; interactive behavior of the original function. If more than one advice | |
306 ;; has an `(interactive ...)' specification then the first one (the one | |
307 ;; with the smallest position) found in the list of before/around/after | |
308 ;; advices will be used. | |
309 | |
310 ;; A possibly empty list of <body-forms> specifies the body of the advice in | |
311 ;; an implicit progn. The body of an advice can access/change arguments, | |
26217 | 312 ;; the return value, the binding environment, and can have all sorts of |
4110 | 313 ;; other side effects. |
314 | |
315 ;; @@ Assembling advised definitions: | |
316 ;; ================================== | |
317 ;; Suppose a function/macro/subr/special-form has N pieces of before advice, | |
318 ;; M pieces of around advice and K pieces of after advice. Assuming none of | |
319 ;; the advices is protected, its advised definition will look like this | |
320 ;; (body-form indices correspond to the position of the respective advice in | |
321 ;; that advice class): | |
322 | |
323 ;; ([macro] lambda <arglist> | |
324 ;; [ [<advised-docstring>] [(interactive ...)] ] | |
325 ;; (let (ad-return-value) | |
326 ;; {<before-0-body-form>}* | |
327 ;; .... | |
328 ;; {<before-N-1-body-form>}* | |
329 ;; {<around-0-body-form>}* | |
330 ;; {<around-1-body-form>}* | |
331 ;; .... | |
332 ;; {<around-M-1-body-form>}* | |
333 ;; (setq ad-return-value | |
334 ;; <apply original definition to <arglist>>) | |
335 ;; {<other-around-M-1-body-form>}* | |
336 ;; .... | |
337 ;; {<other-around-1-body-form>}* | |
338 ;; {<other-around-0-body-form>}* | |
339 ;; {<after-0-body-form>}* | |
340 ;; .... | |
341 ;; {<after-K-1-body-form>}* | |
342 ;; ad-return-value)) | |
343 | |
344 ;; Macros and special forms will be redefined as macros, hence the optional | |
345 ;; [macro] in the beginning of the definition. | |
346 | |
347 ;; <arglist> is either the argument list of the original function or the | |
348 ;; first argument list defined in the list of before/around/after advices. | |
349 ;; The values of <arglist> variables can be accessed/changed in the body of | |
350 ;; an advice by simply referring to them by their original name, however, | |
351 ;; more portable argument access macros are also provided (see below). For | |
352 ;; subrs/special-forms for which neither explicit argument list definitions | |
353 ;; are available, nor their documentation strings contain such definitions | |
354 ;; (as they do v19s), `(&rest ad-subr-args)' will be used. | |
355 | |
356 ;; <advised-docstring> is an optional, special documentation string which will | |
357 ;; be expanded into a proper documentation string upon call of `documentation'. | |
358 | |
359 ;; (interactive ...) is an optional interactive form either taken from the | |
360 ;; original function or from a before/around/after advice. For advised | |
361 ;; interactive subrs that do not have an interactive form specified in any | |
362 ;; advice we have to use (interactive) and then call the subr interactively | |
363 ;; if the advised function was called interactively, because the | |
364 ;; interactive specification of subrs is not accessible. This is the only | |
365 ;; case where changing the values of arguments will not have an affect | |
366 ;; because they will be reset by the interactive specification of the subr. | |
367 ;; If this is a problem one can always specify an interactive form in a | |
368 ;; before/around/after advice to gain control over argument values that | |
369 ;; were supplied interactively. | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
370 ;; |
4110 | 371 ;; Then the body forms of the various advices in the various classes of advice |
372 ;; are assembled in order. The forms of around advice L are normally part of | |
373 ;; one of the forms of around advice L-1. An around advice can specify where | |
374 ;; the forms of the wrapped or surrounded forms should go with the special | |
375 ;; keyword `ad-do-it', which will be substituted with a `progn' containing the | |
376 ;; forms of the surrounded code. | |
377 | |
26217 | 378 ;; The innermost part of the around advice onion is |
4110 | 379 ;; <apply original definition to <arglist>> |
380 ;; whose form depends on the type of the original function. The variable | |
381 ;; `ad-return-value' will be set to its result. This variable is visible to | |
382 ;; all pieces of advice which can access and modify it before it gets returned. | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
383 ;; |
4110 | 384 ;; The semantic structure of advised functions that contain protected pieces |
385 ;; of advice is the same. The only difference is that `unwind-protect' forms | |
386 ;; make sure that the protected advice gets executed even if some previous | |
387 ;; piece of advice had an error or a non-local exit. If any around advice is | |
388 ;; protected then the whole around advice onion will be protected. | |
389 | |
390 ;; @@ Argument access in advised functions: | |
391 ;; ======================================== | |
392 ;; As already mentioned, the simplest way to access the arguments of an | |
393 ;; advised function in the body of an advice is to refer to them by name. To | |
394 ;; do that, the advice programmer needs to know either the names of the | |
395 ;; argument variables of the original function, or the names used in the | |
396 ;; argument list redefinition given in a piece of advice. While this simple | |
397 ;; method might be sufficient in many cases, it has the disadvantage that it | |
398 ;; is not very portable because it hardcodes the argument names into the | |
399 ;; advice. If the definition of the original function changes the advice | |
400 ;; might break even though the code might still be correct. Situations like | |
401 ;; that arise, for example, if one advises a subr like `eval-region' which | |
402 ;; gets redefined in a non-advice style into a function by the edebug | |
403 ;; package. If the advice assumes `eval-region' to be a subr it might break | |
404 ;; once edebug is loaded. Similar situations arise when one wants to use the | |
405 ;; same piece of advice across different versions of Emacs. Some subrs in a | |
406 ;; v18 Emacs are functions in v19 and vice versa, but for the most part the | |
407 ;; semantics remain the same, hence, the same piece of advice might be usable | |
408 ;; in both Emacs versions. | |
409 | |
410 ;; As a solution to that advice provides argument list access macros that get | |
411 ;; translated into the proper access forms at activation time, i.e., when the | |
412 ;; advised definition gets constructed. Access macros access actual arguments | |
413 ;; by position regardless of how these actual argument get distributed onto | |
414 ;; the argument variables of a function. The rational behind this is that in | |
415 ;; Emacs Lisp the semantics of an argument is strictly determined by its | |
416 ;; position (there are no keyword arguments). | |
417 | |
418 ;; Suppose the function `foo' is defined as | |
419 ;; | |
420 ;; (defun foo (x y &optional z &rest r) ....) | |
421 ;; | |
422 ;; and is then called with | |
423 ;; | |
424 ;; (foo 0 1 2 3 4 5 6) | |
425 | |
426 ;; which means that X=0, Y=1, Z=2 and R=(3 4 5 6). The assumption is that | |
427 ;; the semantics of an actual argument is determined by its position. It is | |
428 ;; this semantics that has to be known by the advice programmer. Then s/he | |
429 ;; can access these arguments in a piece of advice with some of the | |
430 ;; following macros (the arrows indicate what value they will return): | |
431 | |
432 ;; (ad-get-arg 0) -> 0 | |
433 ;; (ad-get-arg 1) -> 1 | |
434 ;; (ad-get-arg 2) -> 2 | |
435 ;; (ad-get-arg 3) -> 3 | |
436 ;; (ad-get-args 2) -> (2 3 4 5 6) | |
437 ;; (ad-get-args 4) -> (4 5 6) | |
438 | |
439 ;; `(ad-get-arg <position>)' will return the actual argument that was supplied | |
440 ;; at <position>, `(ad-get-args <position>)' will return the list of actual | |
441 ;; arguments supplied starting at <position>. Note that these macros can be | |
442 ;; used without any knowledge about the form of the actual argument list of | |
443 ;; the original function. | |
444 | |
445 ;; Similarly, `(ad-set-arg <position> <value-form>)' can be used to set the | |
446 ;; value of the actual argument at <position> to <value-form>. For example, | |
447 ;; | |
448 ;; (ad-set-arg 5 "five") | |
449 ;; | |
450 ;; will have the effect that R=(3 4 "five" 6) once the original function is | |
451 ;; called. `(ad-set-args <position> <value-list-form>)' can be used to set | |
452 ;; the list of actual arguments starting at <position> to <value-list-form>. | |
453 ;; For example, | |
454 ;; | |
455 ;; (ad-set-args 0 '(5 4 3 2 1 0)) | |
456 ;; | |
457 ;; will have the effect that X=5, Y=4, Z=3 and R=(2 1 0) once the original | |
458 ;; function is called. | |
459 | |
460 ;; All these access macros are text macros rather than real Lisp macros. When | |
461 ;; the advised definition gets constructed they get replaced with actual access | |
462 ;; forms depending on the argument list of the advised function, i.e., after | |
463 ;; that argument access is in most cases as efficient as using the argument | |
464 ;; variable names directly. | |
465 | |
466 ;; @@@ Accessing argument bindings of arbitrary functions: | |
467 ;; ======================================================= | |
468 ;; Some functions (such as `trace-function' defined in trace.el) need a | |
469 ;; method of accessing the names and bindings of the arguments of an | |
470 ;; arbitrary advised function. To do that within an advice one can use the | |
471 ;; special keyword `ad-arg-bindings' which is a text macro that will be | |
472 ;; substituted with a form that will evaluate to a list of binding | |
473 ;; specifications, one for every argument variable. These binding | |
474 ;; specifications can then be examined in the body of the advice. For | |
475 ;; example, somewhere in an advice we could do this: | |
476 ;; | |
477 ;; (let* ((bindings ad-arg-bindings) | |
478 ;; (firstarg (car bindings)) | |
479 ;; (secondarg (car (cdr bindings)))) | |
480 ;; ;; Print info about first argument | |
481 ;; (print (format "%s=%s (%s)" | |
482 ;; (ad-arg-binding-field firstarg 'name) | |
483 ;; (ad-arg-binding-field firstarg 'value) | |
484 ;; (ad-arg-binding-field firstarg 'type))) | |
485 ;; ....) | |
486 ;; | |
487 ;; The `type' of an argument is either `required', `optional' or `rest'. | |
488 ;; Wherever `ad-arg-bindings' appears a form will be inserted that evaluates | |
489 ;; to the list of bindings, hence, in order to avoid multiple unnecessary | |
490 ;; evaluations one should always bind it to some variable. | |
491 | |
492 ;; @@@ Argument list mapping: | |
493 ;; ========================== | |
494 ;; Because `defadvice' allows the specification of the argument list of the | |
495 ;; advised function we need a mapping mechanism that maps this argument list | |
496 ;; onto that of the original function. For example, somebody might specify | |
497 ;; `(sym newdef)' as the argument list of `fset', while advice might use | |
498 ;; `(&rest ad-subr-args)' as the argument list of the original function | |
499 ;; (depending on what Emacs version is used). Hence SYM and NEWDEF have to | |
500 ;; be properly mapped onto the &rest variable when the original definition is | |
26217 | 501 ;; called. Advice automatically takes care of that mapping, hence, the advice |
4110 | 502 ;; programmer can specify an argument list without having to know about the |
503 ;; exact structure of the original argument list as long as the new argument | |
504 ;; list takes a compatible number/magnitude of actual arguments. | |
505 | |
506 ;; @@@ Definition of subr argument lists: | |
507 ;; ====================================== | |
508 ;; When advice constructs the advised definition of a function it has to | |
509 ;; know the argument list of the original function. For functions and macros | |
510 ;; the argument list can be determined from the actual definition, however, | |
511 ;; for subrs there is no such direct access available. In Lemacs and for some | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
512 ;; subrs in Emacs-19 the argument list of a subr can be determined from |
4110 | 513 ;; its documentation string, in a v18 Emacs even that is not possible. If |
514 ;; advice cannot at all determine the argument list of a subr it uses | |
515 ;; `(&rest ad-subr-args)' which will always work but is inefficient because | |
516 ;; it conses up arguments. The macro `ad-define-subr-args' can be used by | |
517 ;; the advice programmer to explicitly tell advice about the argument list | |
518 ;; of a certain subr, for example, | |
519 ;; | |
520 ;; (ad-define-subr-args 'fset '(sym newdef)) | |
521 ;; | |
522 ;; is used by advice itself to tell a v18 Emacs about the arguments of `fset'. | |
523 ;; The following can be used to undo such a definition: | |
524 ;; | |
525 ;; (ad-undefine-subr-args 'fset) | |
526 ;; | |
527 ;; The argument list definition is stored on the property list of the subr | |
528 ;; name symbol. When an argument list could be determined from the | |
529 ;; documentation string it will be cached under that property. The general | |
530 ;; mechanism for looking up the argument list of a subr is the following: | |
531 ;; 1) look for a definition stored on the property list | |
532 ;; 2) if that failed try to infer it from the documentation string and | |
533 ;; if successful cache it on the property list | |
534 ;; 3) otherwise use `(&rest ad-subr-args)' | |
535 | |
536 ;; @@ Activation and deactivation: | |
537 ;; =============================== | |
538 ;; The definition of an advised function does not change until all its advice | |
539 ;; gets actually activated. Activation can either happen with the `activate' | |
540 ;; flag specified in the `defadvice', with an explicit call or interactive | |
541 ;; invocation of `ad-activate', or if forward advice is enabled (i.e., the | |
542 ;; value of `ad-activate-on-definition' is t) at the time an already advised | |
543 ;; function gets defined. | |
544 | |
545 ;; When a function gets first activated its original definition gets saved, | |
546 ;; all defined and enabled pieces of advice will get combined with the | |
547 ;; original definition, the resulting definition might get compiled depending | |
548 ;; on some conditions described below, and then the function will get | |
549 ;; redefined with the advised definition. This also means that undefined | |
550 ;; functions cannot get activated even though they might be already advised. | |
551 | |
552 ;; The advised definition will get compiled either if `ad-activate' was called | |
553 ;; interactively with a prefix argument, or called explicitly with its second | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
554 ;; argument as t, or, if `ad-default-compilation-action' justifies it according |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
555 ;; to the current system state. If the advised definition was |
4110 | 556 ;; constructed during "preactivation" (see below) then that definition will |
557 ;; be already compiled because it was constructed during byte-compilation of | |
558 ;; the file that contained the `defadvice' with the `preactivate' flag. | |
559 | |
560 ;; `ad-deactivate' can be used to back-define an advised function to its | |
561 ;; original definition. It can be called interactively or directly. Because | |
562 ;; `ad-activate' caches the advised definition the function can be | |
563 ;; reactivated via `ad-activate' with only minor overhead (it is checked | |
564 ;; whether the current advice state is consistent with the cached | |
565 ;; definition, see the section on caching below). | |
566 | |
567 ;; `ad-activate-regexp' and `ad-deactivate-regexp' can be used to de/activate | |
568 ;; all currently advised function that have a piece of advice with a name that | |
569 ;; contains a match for a regular expression. These functions can be used to | |
570 ;; de/activate sets of functions depending on certain advice naming | |
571 ;; conventions. | |
572 | |
573 ;; Finally, `ad-activate-all' and `ad-deactivate-all' can be used to | |
574 ;; de/activate all currently advised functions. These are useful to | |
575 ;; (temporarily) return to an un/advised state. | |
576 | |
577 ;; @@@ Reasons for the separation of advice definition and activation: | |
578 ;; =================================================================== | |
579 ;; As already mentioned, advising happens in two stages: | |
580 | |
581 ;; 1) definition of various pieces of advice | |
582 ;; 2) activation of all advice currently defined and enabled | |
583 | |
584 ;; The advantage of this is that various pieces of advice can be defined | |
585 ;; before they get combined into an advised definition which avoids | |
586 ;; unnecessary constructions of intermediate advised definitions. The more | |
587 ;; important advantage is that it allows the implementation of forward advice. | |
588 ;; Advice information for a certain function accumulates as the value of the | |
589 ;; `advice-info' property of the function symbol. This accumulation is | |
590 ;; completely independent of the fact that that function might not yet be | |
591 ;; defined. The special forms `defun' and `defmacro' have been advised to | |
592 ;; check whether the function/macro they defined had advice information | |
593 ;; associated with it. If so and forward advice is enabled, the original | |
594 ;; definition will be saved, and then the advice will be activated. When a | |
595 ;; file is loaded in a v18 Emacs the functions/macros it defines are also | |
596 ;; defined with calls to `defun/defmacro'. Hence, we can forward advise | |
597 ;; functions/macros which will be defined later during a load/autoload of some | |
598 ;; file (for compiled files generated by jwz's byte-compiler in a v19 Emacs | |
599 ;; this is slightly more complicated but the basic idea is the same). | |
600 | |
601 ;; @@ Enabling/disabling pieces or sets of advice: | |
602 ;; =============================================== | |
603 ;; A major motivation for the development of this advice package was to bring | |
604 ;; a little bit more structure into the function overloading chaos in Emacs | |
605 ;; Lisp. Many packages achieve some of their functionality by adding a little | |
606 ;; bit (or a lot) to the standard functionality of some Emacs Lisp function. | |
607 ;; ange-ftp is a very popular package that achieves its magic by overloading | |
608 ;; most Emacs Lisp functions that deal with files. A popular function that's | |
609 ;; overloaded by many packages is `expand-file-name'. The situation that one | |
610 ;; function is multiply overloaded can arise easily. | |
611 | |
612 ;; Once in a while it would be desirable to be able to disable some/all | |
613 ;; overloads of a particular package while keeping all the rest. Ideally - | |
614 ;; at least in my opinion - these overloads would all be done with advice, | |
615 ;; I know I am dreaming right now... In that ideal case the enable/disable | |
616 ;; mechanism of advice could be used to achieve just that. | |
617 | |
618 ;; Every piece of advice is associated with an enablement flag. When the | |
619 ;; advised definition of a particular function gets constructed (e.g., during | |
620 ;; activation) only the currently enabled pieces of advice will be considered. | |
621 ;; This mechanism allows one to have different "views" of an advised function | |
622 ;; dependent on what pieces of advice are currently enabled. | |
623 | |
624 ;; Another motivation for this mechanism is that it allows one to define a | |
625 ;; piece of advice for some function yet keep it dormant until a certain | |
626 ;; condition is met. Until then activation of the function will not make use | |
627 ;; of that piece of advice. Once the condition is met the advice can be | |
628 ;; enabled and a reactivation of the function will add its functionality as | |
629 ;; part of the new advised definition. For example, the advices of `defun' | |
630 ;; etc. used by advice itself will stay disabled until `ad-start-advice' is | |
631 ;; called and some variables have the proper values. Hence, if somebody | |
632 ;; else advised these functions too and activates them the advices defined | |
633 ;; by advice will get used only if they are intended to be used. | |
634 | |
635 ;; The main interface to this mechanism are the interactive functions | |
636 ;; `ad-enable-advice' and `ad-disable-advice'. For example, the following | |
637 ;; would disable a particular advice of the function `foo': | |
638 ;; | |
639 ;; (ad-disable-advice 'foo 'before 'my-advice) | |
640 ;; | |
641 ;; This call by itself only changes the flag, to get the proper effect in | |
642 ;; the advised definition too one has to activate `foo' with | |
643 ;; | |
644 ;; (ad-activate 'foo) | |
645 ;; | |
646 ;; or interactively. To disable whole sets of advices one can use a regular | |
647 ;; expression mechanism. For example, let us assume that ange-ftp actually | |
648 ;; used advice to overload all its functions, and that it used the | |
649 ;; "ange-ftp-" prefix for all its advice names, then we could temporarily | |
650 ;; disable all its advices with | |
651 ;; | |
652 ;; (ad-disable-regexp "^ange-ftp-") | |
653 ;; | |
654 ;; and the following call would put that actually into effect: | |
655 ;; | |
656 ;; (ad-activate-regexp "^ange-ftp-") | |
657 ;; | |
658 ;; A saver way would have been to use | |
659 ;; | |
660 ;; (ad-update-regexp "^ange-ftp-") | |
661 ;; | |
662 ;; instead which would have only reactivated currently actively advised | |
663 ;; functions, but not functions that were currently deactivated. All these | |
664 ;; functions can also be called interactively. | |
665 | |
666 ;; A certain piece of advice is considered a match if its name contains a | |
667 ;; match for the regular expression. To enable ange-ftp again we would use | |
668 ;; `ad-enable-regexp' and then activate or update again. | |
669 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
670 ;; @@ Forward advice, automatic advice activation: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
671 ;; =============================================== |
4110 | 672 ;; Because most Emacs Lisp packages are loaded on demand via an autoload |
673 ;; mechanism it is essential to be able to "forward advise" functions. | |
674 ;; Otherwise, proper advice definition and activation would make it necessary | |
675 ;; to preload every file that defines a certain function before it can be | |
676 ;; advised, which would partly defeat the purpose of the advice mechanism. | |
677 | |
678 ;; In the following, "forward advice" always implies its automatic activation | |
679 ;; once a function gets defined, and not just the accumulation of advice | |
680 ;; information for a possibly undefined function. | |
681 | |
682 ;; Advice implements forward advice mainly via the following: 1) Separation | |
683 ;; of advice definition and activation that makes it possible to accumulate | |
684 ;; advice information without having the original function already defined, | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
685 ;; 2) special versions of the built-in functions `fset/defalias' which check |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
686 ;; for advice information whenever they define a function. If advice |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
687 ;; information was found then the advice will immediately get activated when |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
688 ;; the function gets defined. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
689 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
690 ;; Automatic advice activation means, that whenever a function gets defined |
4110 | 691 ;; with either `defun', `defmacro', `fset' or by loading a byte-compiled |
692 ;; file, and the function has some advice-info stored with it then that | |
693 ;; advice will get activated right away. | |
694 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
695 ;; @@@ Enabling automatic advice activation: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
696 ;; ========================================= |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
697 ;; Automatic advice activation is enabled by default. It can be disabled by |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
698 ;; doint `M-x ad-stop-advice' and enabled again with `M-x ad-start-advice'. |
4110 | 699 |
700 ;; @@ Caching of advised definitions: | |
701 ;; ================================== | |
702 ;; After an advised definition got constructed it gets cached as part of the | |
703 ;; advised function's advice-info so it can be reused, for example, after an | |
704 ;; intermediate deactivation. Because the advice-info of a function might | |
705 ;; change between the time of caching and reuse a cached definition gets | |
706 ;; a cache-id associated with it so it can be verified whether the cached | |
707 ;; definition is still valid (the main application of this is preactivation | |
708 ;; - see below). | |
709 | |
710 ;; When an advised function gets activated and a verifiable cached definition | |
711 ;; is available, then that definition will be used instead of creating a new | |
712 ;; advised definition from scratch. If you want to make sure that a new | |
713 ;; definition gets constructed then you should use `ad-clear-cache' before you | |
714 ;; activate the advised function. | |
715 | |
716 ;; @@ Preactivation: | |
717 ;; ================= | |
718 ;; Constructing an advised definition is moderately expensive. In a situation | |
719 ;; where one package defines a lot of advised functions it might be | |
720 ;; prohibitively expensive to do all the advised definition construction at | |
721 ;; runtime. Preactivation is a mechanism that allows compile-time construction | |
722 ;; of compiled advised definitions that can be activated cheaply during | |
723 ;; runtime. Preactivation uses the caching mechanism to do that. Here's how it | |
724 ;; works: | |
725 | |
726 ;; When the byte-compiler compiles a `defadvice' that has the `preactivate' | |
727 ;; flag specified, it uses the current original definition of the advised | |
728 ;; function plus the advice specified in this `defadvice' (even if it is | |
729 ;; specified as disabled) and all other currently enabled pieces of advice to | |
730 ;; construct an advised definition and an identifying cache-id and makes them | |
731 ;; part of the `defadvice' expansion which will then be compiled by the | |
732 ;; byte-compiler (to ensure that in a v18 emacs you have to put the | |
733 ;; `defadvice' inside a `defun' to get it compiled and then you have to call | |
734 ;; that compiled `defun' in order to actually execute the `defadvice'). When | |
735 ;; the file with the compiled, preactivating `defadvice' gets loaded the | |
736 ;; precompiled advised definition will be cached on the advised function's | |
737 ;; advice-info. When it gets activated (can be immediately on execution of the | |
738 ;; `defadvice' or any time later) the cache-id gets checked against the | |
739 ;; current state of advice and if it is verified the precompiled definition | |
740 ;; will be used directly (the verification is pretty cheap). If it couldn't get | |
741 ;; verified a new advised definition for that function will be built from | |
742 ;; scratch, hence, the efficiency added by the preactivation mechanism does | |
743 ;; not at all impair the flexibility of the advice mechanism. | |
744 | |
745 ;; MORAL: In order get all the efficiency out of preactivation the advice | |
746 ;; state of an advised function at the time the file with the | |
747 ;; preactivating `defadvice' gets byte-compiled should be exactly | |
748 ;; the same as it will be when the advice of that function gets | |
749 ;; actually activated. If it is not there is a high chance that the | |
750 ;; cache-id will not match and hence a new advised definition will | |
751 ;; have to be constructed at runtime. | |
752 | |
753 ;; Preactivation and forward advice do not contradict each other. It is | |
754 ;; perfectly ok to load a file with a preactivating `defadvice' before the | |
755 ;; original definition of the advised function is available. The constructed | |
756 ;; advised definition will be used once the original function gets defined and | |
757 ;; its advice gets activated. The only constraint is that at the time the | |
758 ;; file with the preactivating `defadvice' got compiled the original function | |
759 ;; definition was available. | |
760 | |
761 ;; TIPS: Here are some indications that a preactivation did not work the way | |
762 ;; you intended it to work: | |
763 ;; - Activation of the advised function takes longer than usual/expected | |
764 ;; - The byte-compiler gets loaded while an advised function gets | |
765 ;; activated | |
766 ;; - `byte-compile' is part of the `features' variable even though you | |
767 ;; did not use the byte-compiler | |
768 ;; Right now advice does not provide an elegant way to find out whether | |
769 ;; and why a preactivation failed. What you can do is to trace the | |
770 ;; function `ad-cache-id-verification-code' (with the function | |
771 ;; `trace-function-background' defined in my trace.el package) before | |
772 ;; any of your advised functions get activated. After they got | |
773 ;; activated check whether all calls to `ad-cache-id-verification-code' | |
774 ;; returned `verified' as a result. Other values indicate why the | |
775 ;; verification failed which should give you enough information to | |
776 ;; fix your preactivation/compile/load/activation sequence. | |
777 | |
26217 | 778 ;; IMPORTANT: There is one case (that I am aware of) that can make |
4110 | 779 ;; preactivation fail, i.e., a preconstructed advised definition that does |
780 ;; NOT match the current state of advice gets used nevertheless. That case | |
781 ;; arises if one package defines a certain piece of advice which gets used | |
26217 | 782 ;; during preactivation, and another package incompatibly redefines that |
4110 | 783 ;; very advice (i.e., same function/class/name), and it is the second advice |
784 ;; that is available when the preconstructed definition gets activated, and | |
26217 | 785 ;; that was the only definition of that advice so far (`ad-add-advice' |
786 ;; catches advice redefinitions and clears the cache in such a case). | |
4110 | 787 ;; Catching that would make the cache verification too expensive. |
788 | |
789 ;; MORAL-II: Redefining somebody else's advice is BAAAAD (to speak with | |
790 ;; George Walker Bush), and why would you redefine your own advice anyway? | |
791 ;; Advice is a mechanism to facilitate function redefinition, not advice | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
792 ;; redefinition (wait until I write Meta-Advice :-). If you really have |
4110 | 793 ;; to undo somebody else's advice try to write a "neutralizing" advice. |
794 | |
795 ;; @@ Advising macros and special forms and other dangerous things: | |
796 ;; ================================================================ | |
797 ;; Look at the corresponding tutorial sections for more information on | |
798 ;; these topics. Here it suffices to point out that the special treatment | |
799 ;; of macros and special forms by the byte-compiler can lead to problems | |
800 ;; when they get advised. Macros can create problems because they get | |
801 ;; expanded at compile time, hence, they might not have all the necessary | |
802 ;; runtime support and such advice cannot be de/activated or changed as | |
803 ;; it is possible for functions. Special forms create problems because they | |
804 ;; have to be advised "into" macros, i.e., an advised special form is a | |
805 ;; implemented as a macro, hence, in most cases the byte-compiler will | |
806 ;; not recognize it as a special form anymore which can lead to very strange | |
807 ;; results. | |
808 ;; | |
809 ;; MORAL: - Only advise macros or special forms when you are absolutely sure | |
810 ;; what you are doing. | |
811 ;; - As a safety measure, always do `ad-deactivate-all' before you | |
812 ;; byte-compile a file to make sure that even if some inconsiderate | |
813 ;; person advised some special forms you'll get proper compilation | |
814 ;; results. After compilation do `ad-activate-all' to get back to | |
815 ;; the previous state. | |
816 | |
817 ;; @@ Adding a piece of advice with `ad-add-advice': | |
818 ;; ================================================= | |
819 ;; The non-interactive function `ad-add-advice' can be used to add a piece of | |
820 ;; advice to some function without using `defadvice'. This is useful if advice | |
821 ;; has to be added somewhere by a function (also look at `ad-make-advice'). | |
822 | |
823 ;; @@ Activation/deactivation advices, file load hooks: | |
824 ;; ==================================================== | |
825 ;; There are two special classes of advice called `activation' and | |
826 ;; `deactivation'. The body forms of these advices are not included into the | |
827 ;; advised definition of a function, rather they are assembled into a hook | |
828 ;; form which will be evaluated whenever the advice-info of the advised | |
829 ;; function gets activated or deactivated. One application of this mechanism | |
830 ;; is to define file load hooks for files that do not provide such hooks | |
831 ;; (v19s already come with a general file-load-hook mechanism, v18s don't). | |
832 ;; For example, suppose you want to print a message whenever `file-x' gets | |
833 ;; loaded, and suppose the last function defined in `file-x' is | |
834 ;; `file-x-last-fn'. Then we can define the following advice: | |
835 ;; | |
836 ;; (defadvice file-x-last-fn (activation file-x-load-hook) | |
837 ;; "Executed whenever file-x is loaded" | |
838 ;; (if load-in-progress (message "Loaded file-x"))) | |
839 ;; | |
840 ;; This will constitute a forward advice for function `file-x-last-fn' which | |
841 ;; will get activated when `file-x' is loaded (only if forward advice is | |
842 ;; enabled of course). Because there are no "real" pieces of advice | |
843 ;; available for it, its definition will not be changed, but the activation | |
844 ;; advice will be run during its activation which is equivalent to having a | |
845 ;; file load hook for `file-x'. | |
846 | |
847 ;; @@ Summary of main advice concepts: | |
848 ;; =================================== | |
849 ;; - Definition: | |
850 ;; A piece of advice gets defined with `defadvice' and added to the | |
851 ;; `advice-info' property of a function. | |
852 ;; - Enablement: | |
853 ;; Every piece of advice has an enablement flag associated with it. Only | |
854 ;; enabled advices are considered during construction of an advised | |
855 ;; definition. | |
856 ;; - Activation: | |
857 ;; Redefine an advised function with its advised definition. Constructs | |
858 ;; an advised definition from scratch if no verifiable cached advised | |
859 ;; definition is available and caches it. | |
860 ;; - Deactivation: | |
861 ;; Back-define an advised function to its original definition. | |
862 ;; - Update: | |
26217 | 863 ;; Reactivate an advised function but only if its advice is currently |
4110 | 864 ;; active. This can be used to bring all currently advised function up |
865 ;; to date with the current state of advice without also activating | |
866 ;; currently deactivated functions. | |
867 ;; - Caching: | |
868 ;; Is the saving of an advised definition and an identifying cache-id so | |
869 ;; it can be reused, for example, for activation after deactivation. | |
870 ;; - Preactivation: | |
871 ;; Is the construction of an advised definition according to the current | |
872 ;; state of advice during byte-compilation of a file with a preactivating | |
873 ;; `defadvice'. That advised definition can then rather cheaply be used | |
874 ;; during activation without having to construct an advised definition | |
875 ;; from scratch at runtime. | |
876 | |
877 ;; @@ Summary of interactive advice manipulation functions: | |
878 ;; ======================================================== | |
879 ;; The following interactive functions can be used to manipulate the state | |
880 ;; of advised functions (all of them support completion on function names, | |
881 ;; advice classes and advice names): | |
882 | |
883 ;; - ad-activate to activate the advice of a FUNCTION | |
884 ;; - ad-deactivate to deactivate the advice of a FUNCTION | |
885 ;; - ad-update to activate the advice of a FUNCTION unless it was not | |
886 ;; yet activated or is currently deactivated. | |
26217 | 887 ;; - ad-unadvise deactivates a FUNCTION and removes all of its advice |
4110 | 888 ;; information, hence, it cannot be activated again |
889 ;; - ad-recover tries to redefine a FUNCTION to its original definition and | |
890 ;; discards all advice information (a low-level `ad-unadvise'). | |
891 ;; Use only in emergencies. | |
892 | |
893 ;; - ad-remove-advice removes a particular piece of advice of a FUNCTION. | |
894 ;; You still have to do call `ad-activate' or `ad-update' to | |
895 ;; activate the new state of advice. | |
896 ;; - ad-enable-advice enables a particular piece of advice of a FUNCTION. | |
897 ;; - ad-disable-advice disables a particular piece of advice of a FUNCTION. | |
898 ;; - ad-enable-regexp maps over all currently advised functions and enables | |
899 ;; every advice whose name contains a match for a regular | |
900 ;; expression. | |
901 ;; - ad-disable-regexp disables matching advices. | |
902 | |
903 ;; - ad-activate-regexp activates all advised function with a matching advice | |
904 ;; - ad-deactivate-regexp deactivates all advised function with matching advice | |
905 ;; - ad-update-regexp updates all advised function with a matching advice | |
906 ;; - ad-activate-all activates all advised functions | |
907 ;; - ad-deactivate-all deactivates all advised functions | |
908 ;; - ad-update-all updates all advised functions | |
909 ;; - ad-unadvise-all unadvises all advised functions | |
910 ;; - ad-recover-all recovers all advised functions | |
911 | |
912 ;; - ad-compile byte-compiles a function/macro if it is compilable. | |
913 | |
914 ;; @@ Summary of forms with special meanings when used within an advice: | |
915 ;; ===================================================================== | |
916 ;; ad-return-value name of the return value variable (get/settable) | |
917 ;; ad-subr-args name of &rest argument variable used for advised | |
918 ;; subrs whose actual argument list cannot be | |
919 ;; determined (get/settable) | |
920 ;; (ad-get-arg <pos>), (ad-get-args <pos>), | |
921 ;; (ad-set-arg <pos> <value>), (ad-set-args <pos> <value-list>) | |
922 ;; argument access text macros to get/set the values of | |
923 ;; actual arguments at a certain position | |
924 ;; ad-arg-bindings text macro that returns the actual names, values | |
925 ;; and types of the arguments as a list of bindings. The | |
926 ;; order of the bindings corresponds to the order of the | |
927 ;; arguments. The individual fields of every binding (name, | |
928 ;; value and type) can be accessed with the function | |
929 ;; `ad-arg-binding-field' (see example above). | |
930 ;; ad-do-it text macro that identifies the place where the original | |
931 ;; or wrapped definition should go in an around advice | |
932 | |
933 | |
934 ;; @ Foo games: An advice tutorial | |
935 ;; =============================== | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
936 ;; The following tutorial was created in Emacs 18.59. Left-justified |
4110 | 937 ;; s-expressions are input forms followed by one or more result forms. |
938 ;; First we have to start the advice magic: | |
939 ;; | |
940 ;; (ad-start-advice) | |
941 ;; nil | |
942 ;; | |
943 ;; We start by defining an innocent looking function `foo' that simply | |
944 ;; adds 1 to its argument X: | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
945 ;; |
4110 | 946 ;; (defun foo (x) |
947 ;; "Add 1 to X." | |
948 ;; (1+ x)) | |
949 ;; foo | |
950 ;; | |
951 ;; (foo 3) | |
952 ;; 4 | |
953 ;; | |
954 ;; @@ Defining a simple piece of advice: | |
955 ;; ===================================== | |
956 ;; Now let's define the first piece of advice for `foo'. To do that we | |
957 ;; use the macro `defadvice' which takes a function name, a list of advice | |
958 ;; specifiers and a list of body forms as arguments. The first element of | |
959 ;; the advice specifiers is the class of the advice, the second is its name, | |
960 ;; the third its position and the rest are some flags. The class of our | |
961 ;; first advice is `before', its name is `fg-add2', its position among the | |
962 ;; currently defined before advices (none so far) is `first', and the advice | |
963 ;; will be `activate'ed immediately. Advice names are global symbols, hence, | |
964 ;; the name space conventions used for function names should be applied. All | |
965 ;; advice names in this tutorial will be prefixed with `fg' for `Foo Games' | |
966 ;; (because everybody has the right to be inconsistent all the function names | |
967 ;; used in this tutorial do NOT follow this convention). | |
968 ;; | |
969 ;; In the body of an advice we can refer to the argument variables of the | |
970 ;; original function by name. Here we add 1 to X so the effect of calling | |
971 ;; `foo' will be to actually add 2. All of the advice definitions below only | |
972 ;; have one body form for simplicity, but there is no restriction to that | |
973 ;; extent. Every piece of advice can have a documentation string which will | |
974 ;; be combined with the documentation of the original function. | |
975 ;; | |
976 ;; (defadvice foo (before fg-add2 first activate) | |
977 ;; "Add 2 to X." | |
978 ;; (setq x (1+ x))) | |
979 ;; foo | |
980 ;; | |
981 ;; (foo 3) | |
982 ;; 5 | |
983 ;; | |
984 ;; @@ Specifying the position of an advice: | |
985 ;; ======================================== | |
986 ;; Now we define the second before advice which will cancel the effect of | |
987 ;; the previous advice. This time we specify the position as 0 which is | |
988 ;; equivalent to `first'. A number can be used to specify the zero-based | |
989 ;; position of an advice among the list of advices in the same class. This | |
990 ;; time we already have one before advice hence the position specification | |
991 ;; actually has an effect. So, after the following definition the position | |
992 ;; of the previous advice will be 1 even though we specified it with `first' | |
993 ;; above, the reason for this is that the position argument is relative to | |
994 ;; the currently defined pieces of advice which by now has changed. | |
995 ;; | |
996 ;; (defadvice foo (before fg-cancel-add2 0 activate) | |
997 ;; "Again only add 1 to X." | |
998 ;; (setq x (1- x))) | |
999 ;; foo | |
1000 ;; | |
1001 ;; (foo 3) | |
1002 ;; 4 | |
1003 ;; | |
1004 ;; @@ Redefining a piece of advice: | |
1005 ;; ================================ | |
1006 ;; Now we define an advice with the same class and same name but with a | |
1007 ;; different position. Defining an advice in a class in which an advice with | |
1008 ;; that name already exists is interpreted as a redefinition of that | |
1009 ;; particular advice, in which case the position argument will be ignored | |
1010 ;; and the previous position of the redefined piece of advice is used. | |
1011 ;; Advice flags can be specified with non-ambiguous initial substrings, hence, | |
1012 ;; from now on we'll use `act' instead of the verbose `activate'. | |
1013 ;; | |
1014 ;; (defadvice foo (before fg-cancel-add2 last act) | |
1015 ;; "Again only add 1 to X." | |
1016 ;; (setq x (1- x))) | |
1017 ;; foo | |
1018 ;; | |
1019 ;; @@ Assembly of advised documentation: | |
1020 ;; ===================================== | |
1021 ;; The documentation strings of the various pieces of advice are assembled | |
1022 ;; in order which shows that advice `fg-cancel-add2' is still the first | |
1023 ;; `before' advice even though we specified position `last' above: | |
1024 ;; | |
1025 ;; (documentation 'foo) | |
1026 ;; "Add 1 to X. | |
1027 ;; | |
1028 ;; This function is advised with the following advice(s): | |
1029 ;; | |
1030 ;; fg-cancel-add2 (before): | |
1031 ;; Again only add 1 to X. | |
1032 ;; | |
1033 ;; fg-add2 (before): | |
1034 ;; Add 2 to X." | |
1035 ;; | |
1036 ;; @@ Advising interactive behavior: | |
1037 ;; ================================= | |
1038 ;; We can make a function interactive (or change its interactive behavior) | |
1039 ;; by specifying an interactive form in one of the before or around | |
1040 ;; advices (there could also be body forms in this advice). The particular | |
1041 ;; definition always assigns 5 as an argument to X which gives us 6 as a | |
1042 ;; result when we call foo interactively: | |
1043 ;; | |
1044 ;; (defadvice foo (before fg-inter last act) | |
1045 ;; "Use 5 as argument when called interactively." | |
1046 ;; (interactive (list 5))) | |
1047 ;; foo | |
1048 ;; | |
1049 ;; (call-interactively 'foo) | |
1050 ;; 6 | |
1051 ;; | |
1052 ;; If more than one advice have an interactive declaration, then the one of | |
1053 ;; the advice with the smallest position will be used (before advices go | |
1054 ;; before around and after advices), hence, the declaration below does | |
1055 ;; not have any effect: | |
1056 ;; | |
1057 ;; (defadvice foo (before fg-inter2 last act) | |
1058 ;; (interactive (list 6))) | |
1059 ;; foo | |
1060 ;; | |
1061 ;; (call-interactively 'foo) | |
1062 ;; 6 | |
1063 ;; | |
26217 | 1064 ;; Let's have a look at what the definition of `foo' looks like now |
4110 | 1065 ;; (indentation added by hand for legibility): |
1066 ;; | |
1067 ;; (symbol-function 'foo) | |
1068 ;; (lambda (x) | |
1069 ;; "$ad-doc: foo$" | |
1070 ;; (interactive (list 5)) | |
26217 | 1071 ;; (let (ad-return-value) |
1072 ;; (setq x (1- x)) | |
1073 ;; (setq x (1+ x)) | |
1074 ;; (setq ad-return-value (ad-Orig-foo x)) | |
4110 | 1075 ;; ad-return-value)) |
1076 ;; | |
1077 ;; @@ Around advices: | |
1078 ;; ================== | |
1079 ;; Now we'll try some `around' advices. An around advice is a wrapper around | |
1080 ;; the original definition. It can shadow or establish bindings for the | |
1081 ;; original definition, and it can look at and manipulate the value returned | |
1082 ;; by the original function. The position of the special keyword `ad-do-it' | |
1083 ;; specifies where the code of the original function will be executed. The | |
1084 ;; keyword can appear multiple times which will result in multiple calls of | |
1085 ;; the original function in the resulting advised code. Note, that if we don't | |
26217 | 1086 ;; specify a position argument (i.e., `first', `last' or a number), then |
4110 | 1087 ;; `first' (or 0) is the default): |
1088 ;; | |
1089 ;; (defadvice foo (around fg-times-2 act) | |
1090 ;; "First double X." | |
1091 ;; (let ((x (* x 2))) | |
1092 ;; ad-do-it)) | |
1093 ;; foo | |
1094 ;; | |
1095 ;; (foo 3) | |
1096 ;; 7 | |
1097 ;; | |
1098 ;; Around advices are assembled like onion skins where the around advice | |
1099 ;; with position 0 is the outermost skin and the advice at the last position | |
1100 ;; is the innermost skin which is directly wrapped around the call of the | |
1101 ;; original definition of the function. Hence, after the next `defadvice' we | |
1102 ;; will first multiply X by 2 then add 1 and then call the original | |
1103 ;; definition (i.e., add 1 again): | |
1104 ;; | |
1105 ;; (defadvice foo (around fg-add-1 last act) | |
1106 ;; "Add 1 to X." | |
1107 ;; (let ((x (1+ x))) | |
1108 ;; ad-do-it)) | |
1109 ;; foo | |
1110 ;; | |
1111 ;; (foo 3) | |
1112 ;; 8 | |
1113 ;; | |
1114 ;; Again, let's see what the definition of `foo' looks like so far: | |
1115 ;; | |
1116 ;; (symbol-function 'foo) | |
26217 | 1117 ;; (lambda (x) |
4110 | 1118 ;; "$ad-doc: foo$" |
26217 | 1119 ;; (interactive (list 5)) |
1120 ;; (let (ad-return-value) | |
1121 ;; (setq x (1- x)) | |
1122 ;; (setq x (1+ x)) | |
1123 ;; (let ((x (* x 2))) | |
1124 ;; (let ((x (1+ x))) | |
1125 ;; (setq ad-return-value (ad-Orig-foo x)))) | |
4110 | 1126 ;; ad-return-value)) |
1127 ;; | |
1128 ;; @@ Controlling advice activation: | |
1129 ;; ================================= | |
1130 ;; In every `defadvice' so far we have used the flag `activate' to activate | |
1131 ;; the advice immediately after its definition, and that's what we want in | |
1132 ;; most cases. However, if we define multiple pieces of advice for a single | |
1133 ;; function then activating every advice immediately is inefficient. A | |
1134 ;; better way to do this is to only activate the last defined advice. | |
1135 ;; For example: | |
1136 ;; | |
1137 ;; (defadvice foo (after fg-times-x) | |
1138 ;; "Multiply the result with X." | |
1139 ;; (setq ad-return-value (* ad-return-value x))) | |
1140 ;; foo | |
1141 ;; | |
1142 ;; This still yields the same result as before: | |
1143 ;; (foo 3) | |
1144 ;; 8 | |
1145 ;; | |
1146 ;; Now we define another advice and activate which will also activate the | |
1147 ;; previous advice `fg-times-x'. Note the use of the special variable | |
1148 ;; `ad-return-value' in the body of the advice which is set to the result of | |
1149 ;; the original function. If we change its value then the value returned by | |
1150 ;; the advised function will be changed accordingly: | |
1151 ;; | |
1152 ;; (defadvice foo (after fg-times-x-again act) | |
1153 ;; "Again multiply the result with X." | |
1154 ;; (setq ad-return-value (* ad-return-value x))) | |
1155 ;; foo | |
1156 ;; | |
1157 ;; Now the advices have an effect: | |
1158 ;; | |
1159 ;; (foo 3) | |
1160 ;; 72 | |
1161 ;; | |
1162 ;; @@ Protecting advice execution: | |
1163 ;; =============================== | |
26217 | 1164 ;; Once in a while we define an advice to perform some cleanup action, |
4110 | 1165 ;; for example: |
1166 ;; | |
1167 ;; (defadvice foo (after fg-cleanup last act) | |
1168 ;; "Do some cleanup." | |
1169 ;; (print "Let's clean up now!")) | |
1170 ;; foo | |
1171 ;; | |
1172 ;; However, in case of an error the cleanup won't be performed: | |
1173 ;; | |
1174 ;; (condition-case error | |
1175 ;; (foo t) | |
1176 ;; (error 'error-in-foo)) | |
1177 ;; error-in-foo | |
1178 ;; | |
1179 ;; To make sure a certain piece of advice gets executed even if some error or | |
1180 ;; non-local exit occurred in any preceding code, we can protect it by using | |
1181 ;; the `protect' keyword. (if any of the around advices is protected then the | |
1182 ;; whole around advice onion will be protected): | |
1183 ;; | |
1184 ;; (defadvice foo (after fg-cleanup prot act) | |
1185 ;; "Do some protected cleanup." | |
1186 ;; (print "Let's clean up now!")) | |
1187 ;; foo | |
1188 ;; | |
1189 ;; Now the cleanup form will be executed even in case of an error: | |
1190 ;; | |
1191 ;; (condition-case error | |
1192 ;; (foo t) | |
1193 ;; (error 'error-in-foo)) | |
1194 ;; "Let's clean up now!" | |
1195 ;; error-in-foo | |
1196 ;; | |
1197 ;; Again, let's see what `foo' looks like: | |
1198 ;; | |
1199 ;; (symbol-function 'foo) | |
26217 | 1200 ;; (lambda (x) |
4110 | 1201 ;; "$ad-doc: foo$" |
26217 | 1202 ;; (interactive (list 5)) |
1203 ;; (let (ad-return-value) | |
1204 ;; (unwind-protect | |
1205 ;; (progn (setq x (1- x)) | |
1206 ;; (setq x (1+ x)) | |
1207 ;; (let ((x (* x 2))) | |
1208 ;; (let ((x (1+ x))) | |
1209 ;; (setq ad-return-value (ad-Orig-foo x)))) | |
1210 ;; (setq ad-return-value (* ad-return-value x)) | |
1211 ;; (setq ad-return-value (* ad-return-value x))) | |
1212 ;; (print "Let's clean up now!")) | |
4110 | 1213 ;; ad-return-value)) |
1214 ;; | |
1215 ;; @@ Compilation of advised definitions: | |
1216 ;; ====================================== | |
1217 ;; Finally, we can specify the `compile' keyword in a `defadvice' to say | |
1218 ;; that we want the resulting advised function to be byte-compiled | |
1219 ;; (`compile' will be ignored unless we also specified `activate'): | |
1220 ;; | |
1221 ;; (defadvice foo (after fg-cleanup prot act comp) | |
1222 ;; "Do some protected cleanup." | |
1223 ;; (print "Let's clean up now!")) | |
1224 ;; foo | |
1225 ;; | |
1226 ;; Now `foo' is byte-compiled: | |
1227 ;; | |
1228 ;; (symbol-function 'foo) | |
26217 | 1229 ;; (lambda (x) |
4110 | 1230 ;; "$ad-doc: foo$" |
26217 | 1231 ;; (interactive (byte-code "....." [5] 1)) |
4110 | 1232 ;; (byte-code "....." [ad-return-value x nil ((byte-code "....." [print "Let's clean up now!"] 2)) * 2 ad-Orig-foo] 6)) |
1233 ;; | |
1234 ;; (foo 3) | |
1235 ;; "Let's clean up now!" | |
1236 ;; 72 | |
1237 ;; | |
1238 ;; @@ Enabling and disabling pieces of advice: | |
1239 ;; =========================================== | |
1240 ;; Once in a while it is desirable to temporarily disable a piece of advice | |
1241 ;; so that it won't be considered during activation, for example, if two | |
1242 ;; different packages advise the same function and one wants to temporarily | |
1243 ;; neutralize the effect of the advice of one of the packages. | |
1244 ;; | |
1245 ;; The following disables the after advice `fg-times-x' in the function `foo'. | |
1246 ;; All that does is to change a flag for this particular advice. All the | |
1247 ;; other information defining it will be left unchanged (e.g., its relative | |
1248 ;; position in this advice class, etc.). | |
1249 ;; | |
1250 ;; (ad-disable-advice 'foo 'after 'fg-times-x) | |
1251 ;; nil | |
1252 ;; | |
1253 ;; For this to have an effect we have to activate `foo': | |
1254 ;; | |
1255 ;; (ad-activate 'foo) | |
1256 ;; foo | |
1257 ;; | |
1258 ;; (foo 3) | |
1259 ;; "Let's clean up now!" | |
1260 ;; 24 | |
1261 ;; | |
1262 ;; If we want to disable all multiplication advices in `foo' we can use a | |
1263 ;; regular expression that matches the names of such advices. Actually, any | |
1264 ;; advice name that contains a match for the regular expression will be | |
1265 ;; called a match. A special advice class `any' can be used to consider | |
1266 ;; all advice classes: | |
1267 ;; | |
1268 ;; (ad-disable-advice 'foo 'any "^fg-.*times") | |
1269 ;; nil | |
1270 ;; | |
1271 ;; (ad-activate 'foo) | |
1272 ;; foo | |
1273 ;; | |
1274 ;; (foo 3) | |
1275 ;; "Let's clean up now!" | |
1276 ;; 5 | |
1277 ;; | |
1278 ;; To enable the disabled advice we could use either `ad-enable-advice' | |
1279 ;; similar to `ad-disable-advice', or as an alternative `ad-enable-regexp' | |
1280 ;; which will enable matching advices in ALL currently advised functions. | |
1281 ;; Hence, this can be used to dis/enable advices made by a particular | |
1282 ;; package to a set of functions as long as that package obeys standard | |
1283 ;; advice name conventions. We prefixed all advice names with `fg-', hence | |
1284 ;; the following will do the trick (`ad-enable-regexp' returns the number | |
1285 ;; of matched advices): | |
1286 ;; | |
1287 ;; (ad-enable-regexp "^fg-") | |
1288 ;; 9 | |
1289 ;; | |
1290 ;; The following will activate all currently active advised functions that | |
1291 ;; contain some advice matched by the regular expression. This is a save | |
1292 ;; way to update the activation of advised functions whose advice changed | |
1293 ;; in some way or other without accidentally also activating currently | |
1294 ;; deactivated functions: | |
1295 ;; | |
1296 ;; (ad-update-regexp "^fg-") | |
1297 ;; nil | |
1298 ;; | |
1299 ;; (foo 3) | |
1300 ;; "Let's clean up now!" | |
1301 ;; 72 | |
1302 ;; | |
1303 ;; Another use for the dis/enablement mechanism is to define a piece of advice | |
1304 ;; and keep it "dormant" until a particular condition is satisfied, i.e., until | |
1305 ;; then the advice will not be used during activation. The `disable' flag lets | |
1306 ;; one do that with `defadvice': | |
1307 ;; | |
1308 ;; (defadvice foo (before fg-1-more dis) | |
1309 ;; "Add yet 1 more." | |
1310 ;; (setq x (1+ x))) | |
1311 ;; foo | |
1312 ;; | |
1313 ;; (ad-activate 'foo) | |
1314 ;; foo | |
1315 ;; | |
1316 ;; (foo 3) | |
1317 ;; "Let's clean up now!" | |
1318 ;; 72 | |
1319 ;; | |
1320 ;; (ad-enable-advice 'foo 'before 'fg-1-more) | |
1321 ;; nil | |
1322 ;; | |
1323 ;; (ad-activate 'foo) | |
1324 ;; foo | |
1325 ;; | |
1326 ;; (foo 3) | |
1327 ;; "Let's clean up now!" | |
1328 ;; 160 | |
1329 ;; | |
1330 ;; @@ Caching: | |
1331 ;; =========== | |
1332 ;; Advised definitions get cached to allow efficient activation/deactivation | |
1333 ;; without having to reconstruct them if nothing in the advice-info of a | |
1334 ;; function has changed. The following idiom can be used to temporarily | |
1335 ;; deactivate functions that have a piece of advice defined by a certain | |
1336 ;; package (we save the old definition to check out caching): | |
1337 ;; | |
1338 ;; (setq old-definition (symbol-function 'foo)) | |
1339 ;; (lambda (x) ....) | |
1340 ;; | |
1341 ;; (ad-deactivate-regexp "^fg-") | |
1342 ;; nil | |
1343 ;; | |
1344 ;; (foo 3) | |
1345 ;; 4 | |
1346 ;; | |
1347 ;; (ad-activate-regexp "^fg-") | |
1348 ;; nil | |
1349 ;; | |
1350 ;; (eq old-definition (symbol-function 'foo)) | |
1351 ;; t | |
1352 ;; | |
1353 ;; (foo 3) | |
1354 ;; "Let's clean up now!" | |
1355 ;; 160 | |
1356 ;; | |
1357 ;; @@ Forward advice: | |
1358 ;; ================== | |
1359 ;; To enable automatic activation of forward advice we first have to set | |
1360 ;; `ad-activate-on-definition' to t and restart advice: | |
1361 ;; | |
1362 ;; (setq ad-activate-on-definition t) | |
1363 ;; t | |
1364 ;; | |
1365 ;; (ad-start-advice) | |
1366 ;; (ad-activate-defined-function) | |
1367 ;; | |
1368 ;; Let's define a piece of advice for an undefined function: | |
1369 ;; | |
1370 ;; (defadvice bar (before fg-sub-1-more act) | |
1371 ;; "Subtract one more from X." | |
1372 ;; (setq x (1- x))) | |
1373 ;; bar | |
1374 ;; | |
1375 ;; `bar' is not yet defined: | |
1376 ;; (fboundp 'bar) | |
1377 ;; nil | |
1378 ;; | |
1379 ;; Now we define it and the forward advice will get activated (only because | |
1380 ;; `ad-activate-on-definition' was t when we started advice above with | |
1381 ;; `ad-start-advice'): | |
1382 ;; | |
1383 ;; (defun bar (x) | |
1384 ;; "Subtract 1 from X." | |
1385 ;; (1- x)) | |
1386 ;; bar | |
1387 ;; | |
1388 ;; (bar 4) | |
1389 ;; 2 | |
1390 ;; | |
1391 ;; Redefinition will activate any available advice if the value of | |
1392 ;; `ad-redefinition-action' is either `warn', `accept' or `discard': | |
1393 ;; | |
1394 ;; (defun bar (x) | |
1395 ;; "Subtract 2 from X." | |
1396 ;; (- x 2)) | |
1397 ;; bar | |
1398 ;; | |
1399 ;; (bar 4) | |
1400 ;; 1 | |
1401 ;; | |
1402 ;; @@ Preactivation: | |
1403 ;; ================= | |
1404 ;; Constructing advised definitions is moderately expensive, hence, it is | |
1405 ;; desirable to have a way to construct them at byte-compile time. | |
1406 ;; Preactivation is a mechanism that allows one to do that. | |
1407 ;; | |
1408 ;; (defun fie (x) | |
1409 ;; "Multiply X by 2." | |
1410 ;; (* x 2)) | |
1411 ;; fie | |
1412 ;; | |
1413 ;; (defadvice fie (before fg-times-4 preact) | |
1414 ;; "Multiply X by 4." | |
1415 ;; (setq x (* x 2))) | |
1416 ;; fie | |
1417 ;; | |
1418 ;; This advice did not affect `fie'... | |
1419 ;; | |
1420 ;; (fie 2) | |
1421 ;; 4 | |
1422 ;; | |
1423 ;; ...but it constructed a cached definition that will be used once `fie' gets | |
1424 ;; activated as long as its current advice state is the same as it was during | |
1425 ;; preactivation: | |
1426 ;; | |
1427 ;; (setq cached-definition (ad-get-cache-definition 'fie)) | |
1428 ;; (lambda (x) ....) | |
1429 ;; | |
1430 ;; (ad-activate 'fie) | |
1431 ;; fie | |
1432 ;; | |
1433 ;; (eq cached-definition (symbol-function 'fie)) | |
1434 ;; t | |
1435 ;; | |
1436 ;; (fie 2) | |
1437 ;; 8 | |
1438 ;; | |
11035 | 1439 ;; If you put a preactivating `defadvice' into a Lisp file that gets byte- |
4110 | 1440 ;; compiled then the constructed advised definition will get compiled by |
1441 ;; the byte-compiler. For that to occur in a v18 emacs you have to put the | |
1442 ;; `defadvice' inside a `defun' because the v18 compiler does not compile | |
1443 ;; top-level forms other than `defun' or `defmacro', for example, | |
1444 ;; | |
1445 ;; (defun fg-defadvice-fum () | |
1446 ;; (defadvice fum (before fg-times-4 preact act) | |
1447 ;; "Multiply X by 4." | |
1448 ;; (setq x (* x 2)))) | |
1449 ;; fg-defadvice-fum | |
1450 ;; | |
1451 ;; So far, no `defadvice' for `fum' got executed, but when we compile | |
1452 ;; `fg-defadvice-fum' the `defadvice' will be expanded by the byte compiler. | |
1453 ;; In order for preactivation to be effective we have to have a proper | |
1454 ;; definition of `fum' around at preactivation time, hence, we define it now: | |
1455 ;; | |
1456 ;; (defun fum (x) | |
1457 ;; "Multiply X by 2." | |
1458 ;; (* x 2)) | |
1459 ;; fum | |
1460 ;; | |
1461 ;; Now we compile the defining function which will construct an advised | |
1462 ;; definition during expansion of the `defadvice', compile it and store it | |
1463 ;; as part of the compiled `fg-defadvice-fum': | |
1464 ;; | |
1465 ;; (ad-compile-function 'fg-defadvice-fum) | |
1466 ;; (lambda nil (byte-code ...)) | |
1467 ;; | |
1468 ;; `fum' is still completely unaffected: | |
1469 ;; | |
1470 ;; (fum 2) | |
1471 ;; 4 | |
1472 ;; | |
1473 ;; (ad-get-advice-info 'fum) | |
1474 ;; nil | |
1475 ;; | |
1476 ;; (fg-defadvice-fum) | |
1477 ;; fum | |
1478 ;; | |
1479 ;; Now the advised version of `fum' is compiled because the compiled definition | |
1480 ;; constructed during preactivation was used, even though we did not specify | |
1481 ;; the `compile' flag: | |
1482 ;; | |
1483 ;; (symbol-function 'fum) | |
26217 | 1484 ;; (lambda (x) |
4110 | 1485 ;; "$ad-doc: fum$" |
1486 ;; (byte-code "....." [ad-return-value x nil * 2 ad-Orig-fum] 4)) | |
1487 ;; | |
1488 ;; (fum 2) | |
1489 ;; 8 | |
1490 ;; | |
1491 ;; A preactivated definition will only be used if it matches the current | |
1492 ;; function definition and advice information. If it does not match it | |
1493 ;; will simply be discarded and a new advised definition will be constructed | |
1494 ;; from scratch. For example, let's first remove all advice-info for `fum': | |
1495 ;; | |
1496 ;; (ad-unadvise 'fum) | |
1497 ;; (("fie") ("bar") ("foo") ...) | |
1498 ;; | |
1499 ;; And now define a new piece of advice: | |
1500 ;; | |
1501 ;; (defadvice fum (before fg-interactive act) | |
1502 ;; "Make fum interactive." | |
1503 ;; (interactive "nEnter x: ")) | |
1504 ;; fum | |
1505 ;; | |
1506 ;; When we now try to use a preactivation it will not be used because the | |
1507 ;; current advice state is different from the one at preactivation time. This | |
1508 ;; is no tragedy, everything will work as expected just not as efficient, | |
1509 ;; because a new advised definition has to be constructed from scratch: | |
1510 ;; | |
1511 ;; (fg-defadvice-fum) | |
1512 ;; fum | |
1513 ;; | |
1514 ;; A new uncompiled advised definition got constructed: | |
1515 ;; | |
1516 ;; (ad-compiled-p (symbol-function 'fum)) | |
1517 ;; nil | |
1518 ;; | |
1519 ;; (fum 2) | |
1520 ;; 8 | |
1521 ;; | |
1522 ;; MORAL: To get all the efficiency out of preactivation the function | |
1523 ;; definition and advice state at preactivation time must be the same as the | |
1524 ;; state at activation time. Preactivation does work with forward advice, all | |
1525 ;; that's necessary is that the definition of the forward advised function is | |
1526 ;; available when the `defadvice' with the preactivation gets compiled. | |
1527 ;; | |
1528 ;; @@ Portable argument access: | |
1529 ;; ============================ | |
1530 ;; So far, we always used the actual argument variable names to access an | |
1531 ;; argument in a piece of advice. For many advice applications this is | |
1532 ;; perfectly ok and keeps advices simple. However, it decreases portability | |
1533 ;; of advices because it assumes specific argument variable names. For example, | |
1534 ;; if one advises a subr such as `eval-region' which then gets redefined by | |
1535 ;; some package (e.g., edebug) into a function with different argument names, | |
1536 ;; then a piece of advice written for `eval-region' that was written with | |
1537 ;; the subr arguments in mind will break. Similar situations arise when one | |
1538 ;; switches between major Emacs versions, e.g., certain subrs in v18 are | |
1539 ;; functions in v19 and vice versa. Also, in v19s subr argument lists | |
1540 ;; are available and will be used, while they are not available in v18. | |
1541 ;; | |
1542 ;; Argument access text macros allow one to access arguments of an advised | |
1543 ;; function in a portable way without having to worry about all these | |
1544 ;; possibilities. These macros will be translated into the proper access forms | |
1545 ;; at activation time, hence, argument access will be as efficient as if | |
1546 ;; the arguments had been used directly in the definition of the advice. | |
1547 ;; | |
1548 ;; (defun fuu (x y z) | |
1549 ;; "Add 3 numbers." | |
1550 ;; (+ x y z)) | |
1551 ;; fuu | |
1552 ;; | |
1553 ;; (fuu 1 1 1) | |
1554 ;; 3 | |
1555 ;; | |
1556 ;; Argument access macros specify actual arguments at a certain position. | |
1557 ;; Position 0 access the first actual argument, position 1 the second etc. | |
1558 ;; For example, the following advice adds 1 to each of the 3 arguments: | |
1559 ;; | |
1560 ;; (defadvice fuu (before fg-add-1-to-all act) | |
1561 ;; "Adds 1 to all arguments." | |
1562 ;; (ad-set-arg 0 (1+ (ad-get-arg 0))) | |
1563 ;; (ad-set-arg 1 (1+ (ad-get-arg 1))) | |
1564 ;; (ad-set-arg 2 (1+ (ad-get-arg 2)))) | |
1565 ;; fuu | |
1566 ;; | |
1567 ;; (fuu 1 1 1) | |
1568 ;; 6 | |
1569 ;; | |
1570 ;; Now suppose somebody redefines `fuu' with a rest argument. Our advice | |
1571 ;; will still work because we used access macros (note, that automatic | |
1572 ;; advice activation is still in effect, hence, the redefinition of `fuu' | |
1573 ;; will automatically activate all its advice): | |
1574 ;; | |
1575 ;; (defun fuu (&rest numbers) | |
1576 ;; "Add NUMBERS." | |
1577 ;; (apply '+ numbers)) | |
1578 ;; fuu | |
1579 ;; | |
1580 ;; (fuu 1 1 1) | |
1581 ;; 6 | |
1582 ;; | |
1583 ;; (fuu 1 1 1 1 1 1) | |
1584 ;; 9 | |
1585 ;; | |
1586 ;; What's important to notice is that argument access macros access actual | |
1587 ;; arguments regardless of how they got distributed onto argument variables. | |
1588 ;; In Emacs Lisp the semantics of an actual argument is determined purely | |
1589 ;; by position, hence, as long as nobody changes the semantics of what a | |
1590 ;; certain actual argument at a certain position means the access macros | |
1591 ;; will do the right thing. | |
1592 ;; | |
1593 ;; Because of &rest arguments we need a second kind of access macro that | |
1594 ;; can access all actual arguments starting from a certain position: | |
1595 ;; | |
1596 ;; (defadvice fuu (before fg-print-args act) | |
1597 ;; "Print all arguments." | |
1598 ;; (print (ad-get-args 0))) | |
1599 ;; fuu | |
1600 ;; | |
1601 ;; (fuu 1 2 3 4 5) | |
1602 ;; (1 2 3 4 5) | |
1603 ;; 18 | |
1604 ;; | |
1605 ;; (defadvice fuu (before fg-set-args act) | |
1606 ;; "Swaps 2nd and 3rd arg and discards all the rest." | |
1607 ;; (ad-set-args 1 (list (ad-get-arg 2) (ad-get-arg 1)))) | |
1608 ;; fuu | |
1609 ;; | |
1610 ;; (fuu 1 2 3 4 4 4 4 4 4) | |
1611 ;; (1 3 2) | |
1612 ;; 9 | |
1613 ;; | |
1614 ;; (defun fuu (x y z) | |
1615 ;; "Add 3 numbers." | |
1616 ;; (+ x y z)) | |
1617 ;; | |
1618 ;; (fuu 1 2 3) | |
1619 ;; (1 3 2) | |
1620 ;; 9 | |
1621 ;; | |
1622 ;; @@ Defining the argument list of an advised function: | |
1623 ;; ===================================================== | |
1624 ;; Once in a while it might be desirable to advise a function and additionally | |
1625 ;; give it an extra argument that controls the advised code, for example, one | |
1626 ;; might want to make an interactive function sensitive to a prefix argument. | |
1627 ;; For such cases `defadvice' allows the specification of an argument list | |
26217 | 1628 ;; for the advised function. Similar to the redefinition of interactive |
4110 | 1629 ;; behavior, the first argument list specification found in the list of before/ |
1630 ;; around/after advices will be used. Of course, the specified argument list | |
1631 ;; should be downward compatible with the original argument list, otherwise | |
1632 ;; functions that call the advised function with the original argument list | |
1633 ;; in mind will break. | |
1634 ;; | |
1635 ;; (defun fii (x) | |
1636 ;; "Add 1 to X." | |
1637 ;; (1+ x)) | |
1638 ;; fii | |
1639 ;; | |
1640 ;; Now we advise `fii' to use an optional second argument that controls the | |
1641 ;; amount of incrementation. A list following the (optional) position | |
1642 ;; argument of the advice will be interpreted as an argument list | |
1643 ;; specification. This means you cannot specify an empty argument list, and | |
1644 ;; why would you want to anyway? | |
1645 ;; | |
1646 ;; (defadvice fii (before fg-inc-x (x &optional incr) act) | |
1647 ;; "Increment X by INCR (default is 1)." | |
1648 ;; (setq x (+ x (1- (or incr 1))))) | |
1649 ;; fii | |
1650 ;; | |
1651 ;; (fii 3) | |
1652 ;; 4 | |
1653 ;; | |
1654 ;; (fii 3 2) | |
1655 ;; 5 | |
1656 ;; | |
1657 ;; @@ Specifying argument lists of subrs: | |
1658 ;; ====================================== | |
1659 ;; The argument lists of subrs cannot be determined directly from Lisp. | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1660 ;; This means that Advice has to use `(&rest ad-subr-args)' as the |
4110 | 1661 ;; argument list of the advised subr which is not very efficient. In Lemacs |
1662 ;; subr argument lists can be determined from their documentation string, in | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1663 ;; Emacs-19 this is the case for some but not all subrs. To accommodate |
4110 | 1664 ;; for the cases where the argument lists cannot be determined (e.g., in a |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1665 ;; v18 Emacs) Advice comes with a specification mechanism that allows the |
4110 | 1666 ;; advice programmer to tell advice what the argument list of a certain subr |
1667 ;; really is. | |
1668 ;; | |
1669 ;; In a v18 Emacs the following will return the &rest idiom: | |
1670 ;; | |
1671 ;; (ad-arglist (symbol-function 'car)) | |
1672 ;; (&rest ad-subr-args) | |
1673 ;; | |
1674 ;; To tell advice what the argument list of `car' really is we | |
1675 ;; can do the following: | |
1676 ;; | |
1677 ;; (ad-define-subr-args 'car '(list)) | |
1678 ;; ((list)) | |
1679 ;; | |
1680 ;; Now `ad-arglist' will return the proper argument list (this method is | |
1681 ;; actually used by advice itself for the advised definition of `fset'): | |
1682 ;; | |
1683 ;; (ad-arglist (symbol-function 'car)) | |
1684 ;; (list) | |
1685 ;; | |
1686 ;; The defined argument list will be stored on the property list of the | |
1687 ;; subr name symbol. When advice looks for a subr argument list it first | |
1688 ;; checks for a definition on the property list, if that fails it tries | |
1689 ;; to infer it from the documentation string and caches it on the property | |
1690 ;; list if it was successful, otherwise `(&rest ad-subr-args)' will be used. | |
1691 ;; | |
1692 ;; @@ Advising interactive subrs: | |
1693 ;; ============================== | |
1694 ;; For the most part there is no difference between advising functions and | |
1695 ;; advising subrs. There is one situation though where one might have to write | |
1696 ;; slightly different advice code for subrs than for functions. This case | |
1697 ;; arises when one wants to access subr arguments in a before/around advice | |
1698 ;; when the arguments were determined by an interactive call to the subr. | |
1699 ;; Advice cannot determine what `interactive' form determines the interactive | |
1700 ;; behavior of the subr, hence, when it calls the original definition in an | |
1701 ;; interactive subr invocation it has to use `call-interactively' to generate | |
1702 ;; the proper interactive behavior. Thus up to that call the arguments of the | |
1703 ;; interactive subr will be nil. For example, the following advice for | |
1704 ;; `kill-buffer' will not work in an interactive invocation... | |
1705 ;; | |
1706 ;; (defadvice kill-buffer (before fg-kill-buffer-hook first act preact comp) | |
1707 ;; (my-before-kill-buffer-hook (ad-get-arg 0))) | |
1708 ;; kill-buffer | |
1709 ;; | |
1710 ;; ...because the buffer argument will be nil in that case. The way out of | |
1711 ;; this dilemma is to provide an `interactive' specification that mirrors | |
1712 ;; the interactive behavior of the unadvised subr, for example, the following | |
1713 ;; will do the right thing even when `kill-buffer' is called interactively: | |
1714 ;; | |
1715 ;; (defadvice kill-buffer (before fg-kill-buffer-hook first act preact comp) | |
1716 ;; (interactive "bKill buffer: ") | |
1717 ;; (my-before-kill-buffer-hook (ad-get-arg 0))) | |
1718 ;; kill-buffer | |
1719 ;; | |
1720 ;; @@ Advising macros: | |
1721 ;; =================== | |
1722 ;; Advising macros is slightly different because there are two significant | |
1723 ;; time points in the invocation of a macro: Expansion and evaluation time. | |
1724 ;; For an advised macro instead of evaluating the original definition we | |
1725 ;; use `macroexpand', that is, changing argument values and binding | |
1726 ;; environments by pieces of advice has an affect during macro expansion | |
1727 ;; but not necessarily during evaluation. In particular, any side effects | |
1728 ;; of pieces of advice will occur during macro expansion. To also affect | |
1729 ;; the behavior during evaluation time one has to change the value of | |
1730 ;; `ad-return-value' in a piece of after advice. For example: | |
1731 ;; | |
1732 ;; (defmacro foom (x) | |
1733 ;; (` (list (, x)))) | |
1734 ;; foom | |
1735 ;; | |
1736 ;; (foom '(a)) | |
1737 ;; ((a)) | |
1738 ;; | |
1739 ;; (defadvice foom (before fg-print-x act) | |
1740 ;; "Print the value of X." | |
1741 ;; (print x)) | |
1742 ;; foom | |
1743 ;; | |
1744 ;; The following works as expected because evaluation immediately follows | |
1745 ;; macro expansion: | |
1746 ;; | |
1747 ;; (foom '(a)) | |
1748 ;; (quote (a)) | |
1749 ;; ((a)) | |
1750 ;; | |
1751 ;; However, the printing happens during expansion (or byte-compile) time: | |
1752 ;; | |
1753 ;; (macroexpand '(foom '(a))) | |
1754 ;; (quote (a)) | |
1755 ;; (list (quote (a))) | |
1756 ;; | |
26217 | 1757 ;; If we want it to happen during evaluation time we have to do the |
4110 | 1758 ;; following (first remove the old advice): |
1759 ;; | |
1760 ;; (ad-remove-advice 'foom 'before 'fg-print-x) | |
1761 ;; nil | |
1762 ;; | |
1763 ;; (defadvice foom (after fg-print-x act) | |
1764 ;; "Print the value of X." | |
1765 ;; (setq ad-return-value | |
1766 ;; (` (progn (print (, x)) | |
1767 ;; (, ad-return-value))))) | |
1768 ;; foom | |
1769 ;; | |
1770 ;; (macroexpand '(foom '(a))) | |
1771 ;; (progn (print (quote (a))) (list (quote (a)))) | |
1772 ;; | |
1773 ;; (foom '(a)) | |
1774 ;; (a) | |
1775 ;; ((a)) | |
1776 ;; | |
1777 ;; While this method might seem somewhat cumbersome, it is very general | |
1778 ;; because it allows one to influence macro expansion as well as evaluation. | |
1779 ;; In general, advising macros should be a rather rare activity anyway, in | |
1780 ;; particular, because compile-time macro expansion takes away a lot of the | |
1781 ;; flexibility and effectiveness of the advice mechanism. Macros that were | |
1782 ;; compile-time expanded before the advice was activated will of course never | |
1783 ;; exhibit the advised behavior. | |
1784 ;; | |
1785 ;; @@ Advising special forms: | |
1786 ;; ========================== | |
1787 ;; Now for something that should be even more rare than advising macros: | |
1788 ;; Advising special forms. Because special forms are irregular in their | |
1789 ;; argument evaluation behavior (e.g., `setq' evaluates the second but not | |
1790 ;; the first argument) they have to be advised into macros. A dangerous | |
1791 ;; consequence of this is that the byte-compiler will not recognize them | |
1792 ;; as special forms anymore (well, in most cases) and use their expansion | |
1793 ;; rather than the proper byte-code. Also, because the original definition | |
1794 ;; of a special form cannot be `funcall'ed, `eval' has to be used instead | |
1795 ;; which is less efficient. | |
1796 ;; | |
1797 ;; MORAL: Do not advise special forms unless you are completely sure about | |
1798 ;; what you are doing (some of the forward advice behavior is | |
1799 ;; implemented via advice of the special forms `defun' and `defmacro'). | |
1800 ;; As a safety measure one should always do `ad-deactivate-all' before | |
1801 ;; one byte-compiles a file to avoid any interference of advised | |
1802 ;; special forms. | |
1803 ;; | |
1804 ;; Apart from the safety concerns advising special forms is not any different | |
1805 ;; from advising plain functions or subrs. | |
1806 | |
1807 | |
1808 ;;; Code: | |
1809 | |
1810 ;; @ Advice implementation: | |
1811 ;; ======================== | |
1812 | |
1813 ;; @@ Compilation idiosyncrasies: | |
1814 ;; ============================== | |
1815 | |
1816 ;; `defadvice' expansion needs quite a few advice functions and variables, | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1817 ;; hence, I need to preload the file before it can be compiled. To avoid |
4110 | 1818 ;; interference of bogus compiled files I always preload the source file: |
1819 (provide 'advice-preload) | |
1820 ;; During a normal load this is a noop: | |
1821 (require 'advice-preload "advice.el") | |
1822 | |
1823 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
1824 ;; @@ Variable definitions: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
1825 ;; ======================== |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
1826 |
21365 | 1827 (defgroup advice nil |
1828 "An overloading mechanism for Emacs Lisp functions." | |
1829 :prefix "ad-" | |
26217 | 1830 :link '(custom-manual "(elisp)Advising Functions") |
21365 | 1831 :group 'lisp) |
1832 | |
8458
a95ca44cec95
(ad-subr-arglist): Adapted to new DOC file format.
Richard M. Stallman <rms@gnu.org>
parents:
8445
diff
changeset
|
1833 (defconst ad-version "2.14") |
4110 | 1834 |
1835 ;;;###autoload | |
21365 | 1836 (defcustom ad-redefinition-action 'warn |
104024
cce8d50c4566
Remove leading * from defcustom docs.
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
1837 "Defines what to do with redefinitions during Advice de/activation. |
4110 | 1838 Redefinition occurs if a previously activated function that already has an |
1839 original definition associated with it gets redefined and then de/activated. | |
1840 In such a case we can either accept the current definition as the new | |
1841 original definition, discard the current definition and replace it with the | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1842 old original, or keep it and raise an error. The values `accept', `discard', |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1843 `error' or `warn' govern what will be done. `warn' is just like `accept' but |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1844 it additionally prints a warning message. All other values will be |
21365 | 1845 interpreted as `error'." |
22577 | 1846 :type '(choice (const accept) (const discard) (const warn) |
1847 (other :tag "error" error)) | |
21365 | 1848 :group 'advice) |
4110 | 1849 |
1850 ;;;###autoload | |
21365 | 1851 (defcustom ad-default-compilation-action 'maybe |
104024
cce8d50c4566
Remove leading * from defcustom docs.
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
1852 "Defines whether to compile advised definitions during activation. |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
1853 A value of `always' will result in unconditional compilation, `never' will |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
1854 always avoid compilation, `maybe' will compile if the byte-compiler is already |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
1855 loaded, and `like-original' will compile if the original definition of the |
26217 | 1856 advised function is compiled or a built-in function. Every other value will |
1857 be interpreted as `maybe'. This variable will only be considered if the | |
21365 | 1858 COMPILE argument of `ad-activate' was supplied as nil." |
22577 | 1859 :type '(choice (const always) (const never) (const like-original) |
1860 (other :tag "maybe" maybe)) | |
21365 | 1861 :group 'advice) |
1862 | |
4110 | 1863 |
1864 | |
1865 ;; @@ Some utilities: | |
1866 ;; ================== | |
1867 | |
1868 ;; We don't want the local arguments to interfere with anything | |
1869 ;; referenced in the supplied functions => the cryptic casing: | |
1870 (defun ad-substitute-tree (sUbTrEe-TeSt fUnCtIoN tReE) | |
26217 | 1871 "Substitute qualifying subTREEs with result of FUNCTION(subTREE). |
1872 Only proper subtrees are considered, for example, if TREE is (1 (2 (3)) 4) | |
1873 then the subtrees will be 1 (2 (3)) 2 (3) 3 4, dotted structures are | |
1874 allowed too. Once a qualifying subtree has been found its subtrees will | |
1875 not be considered anymore. (ad-substitute-tree 'atom 'identity tree) | |
1876 generates a copy of TREE." | |
4110 | 1877 (cond ((consp tReE) |
1878 (cons (if (funcall sUbTrEe-TeSt (car tReE)) | |
1879 (funcall fUnCtIoN (car tReE)) | |
1880 (if (consp (car tReE)) | |
1881 (ad-substitute-tree sUbTrEe-TeSt fUnCtIoN (car tReE)) | |
1882 (car tReE))) | |
1883 (ad-substitute-tree sUbTrEe-TeSt fUnCtIoN (cdr tReE)))) | |
1884 ((funcall sUbTrEe-TeSt tReE) | |
1885 (funcall fUnCtIoN tReE)) | |
1886 (t tReE))) | |
1887 | |
1888 ;; this is just faster than `ad-substitute-tree': | |
1889 (defun ad-copy-tree (tree) | |
26217 | 1890 "Return a copy of the list structure of TREE." |
4110 | 1891 (cond ((consp tree) |
1892 (cons (ad-copy-tree (car tree)) | |
1893 (ad-copy-tree (cdr tree)))) | |
1894 (t tree))) | |
1895 | |
1896 (defmacro ad-dolist (varform &rest body) | |
1897 "A Common-Lisp-style dolist iterator with the following syntax: | |
1898 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1899 (ad-dolist (VAR INIT-FORM [RESULT-FORM]) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1900 BODY-FORM...) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1901 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1902 which will iterate over the list yielded by INIT-FORM binding VAR to the |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1903 current head at every iteration. If RESULT-FORM is supplied its value will |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1904 be returned at the end of the iteration, nil otherwise. The iteration can be |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1905 exited prematurely with `(ad-do-return [VALUE])'." |
4110 | 1906 (let ((expansion |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1907 `(let ((ad-dO-vAr ,(car (cdr varform))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1908 ,(car varform)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1909 (while ad-dO-vAr |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1910 (setq ,(car varform) (car ad-dO-vAr)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1911 ,@body |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1912 ;;work around a backquote bug: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1913 ;;(` ((,@ '(foo)) (bar))) => (append '(foo) '(((bar)))) wrong |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1914 ;;(` ((,@ '(foo)) (, '(bar)))) => (append '(foo) (list '(bar))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1915 ,'(setq ad-dO-vAr (cdr ad-dO-vAr))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1916 ,(car (cdr (cdr varform)))))) |
4110 | 1917 ;;ok, this wastes some cons cells but only during compilation: |
1918 (if (catch 'contains-return | |
1919 (ad-substitute-tree | |
1920 (function (lambda (subtree) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1921 (cond ((eq (car-safe subtree) 'ad-dolist)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1922 ((eq (car-safe subtree) 'ad-do-return) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1923 (throw 'contains-return t))))) |
4110 | 1924 'identity body) |
1925 nil) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1926 `(catch 'ad-dO-eXiT ,expansion) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1927 expansion))) |
4110 | 1928 |
1929 (defmacro ad-do-return (value) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1930 `(throw 'ad-dO-eXiT ,value)) |
4110 | 1931 |
1932 (if (not (get 'ad-dolist 'lisp-indent-hook)) | |
1933 (put 'ad-dolist 'lisp-indent-hook 1)) | |
1934 | |
1935 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1936 ;; @@ Save real definitions of subrs used by Advice: |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1937 ;; ================================================= |
26217 | 1938 ;; Advice depends on the real, unmodified functionality of various subrs, |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1939 ;; we save them here so advised versions will not interfere (eventually, |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1940 ;; we will save all subrs used in code generated by Advice): |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1941 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1942 (defmacro ad-save-real-definition (function) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1943 (let ((saved-function (intern (format "ad-real-%s" function)))) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1944 ;; Make sure the compiler is loaded during macro expansion: |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1945 (require 'byte-compile "bytecomp") |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1946 `(if (not (fboundp ',saved-function)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1947 (progn (fset ',saved-function (symbol-function ',function)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1948 ;; Copy byte-compiler properties: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1949 ,@(if (get function 'byte-compile) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1950 `((put ',saved-function 'byte-compile |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1951 ',(get function 'byte-compile)))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1952 ,@(if (get function 'byte-opcode) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1953 `((put ',saved-function 'byte-opcode |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1954 ',(get function 'byte-opcode)))))))) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1955 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1956 (defun ad-save-real-definitions () |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1957 ;; Macro expansion will hardcode the values of the various byte-compiler |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1958 ;; properties into the compiled version of this function such that the |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1959 ;; proper values will be available at runtime without loading the compiler: |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1960 (ad-save-real-definition fset) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
1961 (ad-save-real-definition documentation)) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1962 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1963 (ad-save-real-definitions) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1964 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1965 |
4110 | 1966 ;; @@ Advice info access fns: |
1967 ;; ========================== | |
1968 | |
1969 ;; Advice information for a particular function is stored on the | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
1970 ;; advice-info property of the function symbol. It is stored as an |
4110 | 1971 ;; alist of the following format: |
1972 ;; | |
1973 ;; ((active . t/nil) | |
1974 ;; (before adv1 adv2 ...) | |
1975 ;; (around adv1 adv2 ...) | |
1976 ;; (after adv1 adv2 ...) | |
1977 ;; (activation adv1 adv2 ...) | |
1978 ;; (deactivation adv1 adv2 ...) | |
1979 ;; (origname . <symbol fbound to origdef>) | |
1980 ;; (cache . (<advised-definition> . <id>))) | |
1981 | |
1982 ;; List of currently advised though not necessarily activated functions | |
1983 ;; (this list is maintained as a completion table): | |
1984 (defvar ad-advised-functions nil) | |
1985 | |
1986 (defmacro ad-pushnew-advised-function (function) | |
26217 | 1987 "Add FUNCTION to `ad-advised-functions' unless its already there." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1988 `(if (not (assoc (symbol-name ,function) ad-advised-functions)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1989 (setq ad-advised-functions |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1990 (cons (list (symbol-name ,function)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1991 ad-advised-functions)))) |
4110 | 1992 |
1993 (defmacro ad-pop-advised-function (function) | |
26217 | 1994 "Remove FUNCTION from `ad-advised-functions'." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1995 `(setq ad-advised-functions |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1996 (delq (assoc (symbol-name ,function) ad-advised-functions) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
1997 ad-advised-functions))) |
4110 | 1998 |
1999 (defmacro ad-do-advised-functions (varform &rest body) | |
26217 | 2000 "`ad-dolist'-style iterator that maps over `ad-advised-functions'. |
2001 \(ad-do-advised-functions (VAR [RESULT-FORM]) | |
2002 BODY-FORM...) | |
2003 On each iteration VAR will be bound to the name of an advised function | |
2004 \(a symbol)." | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2005 `(ad-dolist (,(car varform) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2006 ad-advised-functions |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2007 ,(car (cdr varform))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2008 (setq ,(car varform) (intern (car ,(car varform)))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2009 ,@body)) |
4110 | 2010 |
2011 (if (not (get 'ad-do-advised-functions 'lisp-indent-hook)) | |
2012 (put 'ad-do-advised-functions 'lisp-indent-hook 1)) | |
2013 | |
79099
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2014 (defun ad-get-advice-info (function) |
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2015 (get function 'ad-advice-info)) |
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2016 |
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2017 (defmacro ad-get-advice-info-macro (function) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2018 `(get ,function 'ad-advice-info)) |
4110 | 2019 |
2020 (defmacro ad-set-advice-info (function advice-info) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2021 `(put ,function 'ad-advice-info ,advice-info)) |
4110 | 2022 |
2023 (defmacro ad-copy-advice-info (function) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2024 `(ad-copy-tree (get ,function 'ad-advice-info))) |
4110 | 2025 |
2026 (defmacro ad-is-advised (function) | |
26217 | 2027 "Return non-nil if FUNCTION has any advice info associated with it. |
2028 This does not mean that the advice is also active." | |
79099
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2029 (list 'ad-get-advice-info-macro function)) |
4110 | 2030 |
2031 (defun ad-initialize-advice-info (function) | |
26217 | 2032 "Initialize the advice info for FUNCTION. |
2033 Assumes that FUNCTION has not yet been advised." | |
4110 | 2034 (ad-pushnew-advised-function function) |
2035 (ad-set-advice-info function (list (cons 'active nil)))) | |
2036 | |
2037 (defmacro ad-get-advice-info-field (function field) | |
26217 | 2038 "Retrieve the value of the advice info FIELD of FUNCTION." |
79099
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2039 `(cdr (assq ,field (ad-get-advice-info-macro ,function)))) |
4110 | 2040 |
2041 (defun ad-set-advice-info-field (function field value) | |
26217 | 2042 "Destructively modify VALUE of the advice info FIELD of FUNCTION." |
4110 | 2043 (and (ad-is-advised function) |
79099
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2044 (cond ((assq field (ad-get-advice-info-macro function)) |
4110 | 2045 ;; A field with that name is already present: |
79099
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2046 (rplacd (assq field (ad-get-advice-info-macro function)) value)) |
4110 | 2047 (t;; otherwise, create a new field with that name: |
79099
8632fa2db924
(ad-get-advice-info): Change to a function.
Richard M. Stallman <rms@gnu.org>
parents:
78217
diff
changeset
|
2048 (nconc (ad-get-advice-info-macro function) |
4110 | 2049 (list (cons field value))))))) |
2050 | |
2051 ;; Don't make this a macro so we can use it as a predicate: | |
2052 (defun ad-is-active (function) | |
26217 | 2053 "Return non-nil if FUNCTION is advised and activated." |
4110 | 2054 (ad-get-advice-info-field function 'active)) |
2055 | |
2056 | |
2057 ;; @@ Access fns for single pieces of advice and related predicates: | |
2058 ;; ================================================================= | |
2059 | |
2060 (defun ad-make-advice (name protect enable definition) | |
2061 "Constructs single piece of advice to be stored in some advice-info. | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2062 NAME should be a non-nil symbol, PROTECT and ENABLE should each be |
4110 | 2063 either t or nil, and DEFINITION should be a list of the form |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2064 `(advice lambda ARGLIST [DOCSTRING] [INTERACTIVE-FORM] BODY...)'." |
4110 | 2065 (list name protect enable definition)) |
2066 | |
2067 ;; ad-find-advice uses the alist structure directly -> | |
2068 ;; change if this data structure changes!! | |
2069 (defmacro ad-advice-name (advice) | |
2070 (list 'car advice)) | |
2071 (defmacro ad-advice-protected (advice) | |
2072 (list 'nth 1 advice)) | |
2073 (defmacro ad-advice-enabled (advice) | |
2074 (list 'nth 2 advice)) | |
2075 (defmacro ad-advice-definition (advice) | |
2076 (list 'nth 3 advice)) | |
2077 | |
2078 (defun ad-advice-set-enabled (advice flag) | |
2079 (rplaca (cdr (cdr advice)) flag)) | |
2080 | |
2081 (defun ad-class-p (thing) | |
2082 (memq thing ad-advice-classes)) | |
2083 (defun ad-name-p (thing) | |
2084 (and thing (symbolp thing))) | |
2085 (defun ad-position-p (thing) | |
2086 (or (natnump thing) | |
2087 (memq thing '(first last)))) | |
2088 | |
2089 | |
2090 ;; @@ Advice access functions: | |
2091 ;; =========================== | |
2092 | |
2093 ;; List of defined advice classes: | |
2094 (defvar ad-advice-classes '(before around after activation deactivation)) | |
2095 | |
2096 (defun ad-has-enabled-advice (function class) | |
26217 | 2097 "True if at least one of FUNCTION's advices in CLASS is enabled." |
4110 | 2098 (ad-dolist (advice (ad-get-advice-info-field function class)) |
2099 (if (ad-advice-enabled advice) (ad-do-return t)))) | |
2100 | |
2101 (defun ad-has-redefining-advice (function) | |
26217 | 2102 "True if FUNCTION's advice info defines at least 1 redefining advice. |
2103 Redefining advices affect the construction of an advised definition." | |
4110 | 2104 (and (ad-is-advised function) |
2105 (or (ad-has-enabled-advice function 'before) | |
2106 (ad-has-enabled-advice function 'around) | |
2107 (ad-has-enabled-advice function 'after)))) | |
2108 | |
2109 (defun ad-has-any-advice (function) | |
26217 | 2110 "True if the advice info of FUNCTION defines at least one advice." |
4110 | 2111 (and (ad-is-advised function) |
2112 (ad-dolist (class ad-advice-classes nil) | |
2113 (if (ad-get-advice-info-field function class) | |
2114 (ad-do-return t))))) | |
2115 | |
2116 (defun ad-get-enabled-advices (function class) | |
26217 | 2117 "Return the list of enabled advices of FUNCTION in CLASS." |
4110 | 2118 (let (enabled-advices) |
2119 (ad-dolist (advice (ad-get-advice-info-field function class)) | |
2120 (if (ad-advice-enabled advice) | |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2121 (push advice enabled-advices))) |
4110 | 2122 (reverse enabled-advices))) |
2123 | |
2124 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2125 ;; @@ Dealing with automatic advice activation via `fset/defalias': |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2126 ;; ================================================================ |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2127 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2128 ;; Since Emacs 19.26 the built-in versions of `fset' and `defalias' |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2129 ;; take care of automatic advice activation, hence, we don't have to |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2130 ;; hack it anymore by advising `fset/defun/defmacro/byte-code/etc'. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2131 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2132 ;; The functionality of the new `fset' is as follows: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2133 ;; |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2134 ;; fset(sym,newdef) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2135 ;; assign NEWDEF to SYM |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2136 ;; if (get SYM 'ad-advice-info) |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2137 ;; ad-activate-internal(SYM, nil) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2138 ;; return (symbol-function SYM) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2139 ;; |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2140 ;; Whether advised definitions created by automatic activations will be |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2141 ;; compiled depends on the value of `ad-default-compilation-action'. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2142 |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2143 ;; Since calling `ad-activate-internal' in the built-in definition of `fset' can |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2144 ;; create major disasters we have to be a bit careful. One precaution is |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2145 ;; to provide a dummy definition for `ad-activate-internal' which can be used to |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2146 ;; turn off automatic advice activation (e.g., when `ad-stop-advice' or |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2147 ;; `ad-recover-normality' are called). Another is to avoid recursive calls |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2148 ;; to `ad-activate' by using `ad-with-auto-activation-disabled' where |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2149 ;; appropriate, especially in a safe version of `fset'. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2150 |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2151 ;; For now define `ad-activate-internal' to the dummy definition: |
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2152 (defun ad-activate-internal (function &optional compile) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2153 "Automatic advice activation is disabled. `ad-start-advice' enables it." |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2154 nil) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2155 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2156 ;; This is just a copy of the above: |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2157 (defun ad-activate-internal-off (function &optional compile) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2158 "Automatic advice activation is disabled. `ad-start-advice' enables it." |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2159 nil) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2160 |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
2161 ;; This will be t for top-level calls to `ad-activate-internal-on': |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2162 (defvar ad-activate-on-top-level t) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2163 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2164 (defmacro ad-with-auto-activation-disabled (&rest body) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2165 `(let ((ad-activate-on-top-level nil)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2166 ,@body)) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2167 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2168 (defun ad-safe-fset (symbol definition) |
26217 | 2169 "A safe `fset' which will never call `ad-activate-internal' recursively." |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2170 (ad-with-auto-activation-disabled |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2171 (ad-real-fset symbol definition))) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2172 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2173 |
4110 | 2174 ;; @@ Access functions for original definitions: |
2175 ;; ============================================ | |
2176 ;; The advice-info of an advised function contains its `origname' which is | |
2177 ;; a symbol that is fbound to the original definition available at the first | |
60923
08f8c7042636
* emacs-lisp/advice.el: Replace `legal' with `valid'.
Werner LEMBERG <wl@gnu.org>
parents:
57879
diff
changeset
|
2178 ;; proper activation of the function after a valid re/definition. If the |
4110 | 2179 ;; original was defined via fcell indirection then `origname' will be defined |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2180 ;; just so. Hence, to get hold of the actual original definition of a function |
4110 | 2181 ;; we need to use `ad-real-orig-definition'. |
2182 | |
2183 (defun ad-make-origname (function) | |
26217 | 2184 "Make name to be used to call the original FUNCTION." |
4110 | 2185 (intern (format "ad-Orig-%s" function))) |
2186 | |
2187 (defmacro ad-get-orig-definition (function) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2188 `(let ((origname (ad-get-advice-info-field ,function 'origname))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2189 (if (fboundp origname) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2190 (symbol-function origname)))) |
4110 | 2191 |
2192 (defmacro ad-set-orig-definition (function definition) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2193 `(ad-safe-fset |
106966
9e04f0532ba4
* emacs-lisp/advice.el (ad-set-orig-definition): Fix typo (Bug#3541).
Chong Yidong <cyd@stupidchicken.com>
parents:
106870
diff
changeset
|
2194 (ad-get-advice-info-field ,function 'origname) ,definition)) |
4110 | 2195 |
2196 (defmacro ad-clear-orig-definition (function) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2197 `(fmakunbound (ad-get-advice-info-field ,function 'origname))) |
4110 | 2198 |
2199 | |
2200 ;; @@ Interactive input functions: | |
2201 ;; =============================== | |
2202 | |
2203 (defun ad-read-advised-function (&optional prompt predicate default) | |
26217 | 2204 "Read name of advised function with completion from the minibuffer. |
2205 An optional PROMPT will be used to prompt for the function. PREDICATE | |
2206 plays the same role as for `try-completion' (which see). DEFAULT will | |
2207 be returned on empty input (defaults to the first advised function for | |
2208 which PREDICATE returns non-nil)." | |
4110 | 2209 (if (null ad-advised-functions) |
2210 (error "ad-read-advised-function: There are no advised functions")) | |
2211 (setq default | |
2212 (or default | |
2213 (ad-do-advised-functions (function) | |
2214 (if (or (null predicate) | |
2215 (funcall predicate function)) | |
2216 (ad-do-return function))) | |
2217 (error "ad-read-advised-function: %s" | |
2218 "There are no qualifying advised functions"))) | |
2219 (let* ((ad-pReDiCaTe predicate) | |
2220 (function | |
2221 (completing-read | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2222 (format "%s (default %s): " (or prompt "Function") default) |
4110 | 2223 ad-advised-functions |
2224 (if predicate | |
2225 (function | |
2226 (lambda (function) | |
2227 ;; Oops, no closures - the joys of dynamic scoping: | |
2228 ;; `predicate' clashed with the `predicate' argument | |
2229 ;; of Lemacs' `completing-read'..... | |
2230 (funcall ad-pReDiCaTe (intern (car function)))))) | |
2231 t))) | |
2232 (if (equal function "") | |
2233 (if (ad-is-advised default) | |
2234 default | |
2235 (error "ad-read-advised-function: `%s' is not advised" default)) | |
2236 (intern function)))) | |
2237 | |
2238 (defvar ad-advice-class-completion-table | |
29579
05016ef95d0f
(ad-advice-class-completion-table)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26627
diff
changeset
|
2239 (mapcar (lambda (class) (list (symbol-name class))) |
4110 | 2240 ad-advice-classes)) |
2241 | |
2242 (defun ad-read-advice-class (function &optional prompt default) | |
60923
08f8c7042636
* emacs-lisp/advice.el: Replace `legal' with `valid'.
Werner LEMBERG <wl@gnu.org>
parents:
57879
diff
changeset
|
2243 "Read a valid advice class with completion from the minibuffer. |
26217 | 2244 An optional PROMPT will be used to prompt for the class. DEFAULT will |
2245 be returned on empty input (defaults to the first non-empty advice | |
2246 class of FUNCTION)." | |
4110 | 2247 (setq default |
2248 (or default | |
2249 (ad-dolist (class ad-advice-classes) | |
2250 (if (ad-get-advice-info-field function class) | |
2251 (ad-do-return class))) | |
2252 (error "ad-read-advice-class: `%s' has no advices" function))) | |
2253 (let ((class (completing-read | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2254 (format "%s (default %s): " (or prompt "Class") default) |
4110 | 2255 ad-advice-class-completion-table nil t))) |
2256 (if (equal class "") | |
2257 default | |
2258 (intern class)))) | |
2259 | |
2260 (defun ad-read-advice-name (function class &optional prompt) | |
26217 | 2261 "Read name of existing advice of CLASS for FUNCTION with completion. |
2262 An optional PROMPT is used to prompt for the name." | |
4110 | 2263 (let* ((name-completion-table |
2264 (mapcar (function (lambda (advice) | |
2265 (list (symbol-name (ad-advice-name advice))))) | |
2266 (ad-get-advice-info-field function class))) | |
2267 (default | |
2268 (if (null name-completion-table) | |
2269 (error "ad-read-advice-name: `%s' has no %s advice" | |
2270 function class) | |
2271 (car (car name-completion-table)))) | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2272 (prompt (format "%s (default %s): " (or prompt "Name") default)) |
4110 | 2273 (name (completing-read prompt name-completion-table nil t))) |
2274 (if (equal name "") | |
2275 (intern default) | |
2276 (intern name)))) | |
2277 | |
2278 (defun ad-read-advice-specification (&optional prompt) | |
26217 | 2279 "Read a complete function/class/name specification from minibuffer. |
2280 The list of read symbols will be returned. The optional PROMPT will | |
2281 be used to prompt for the function." | |
4110 | 2282 (let* ((function (ad-read-advised-function prompt)) |
2283 (class (ad-read-advice-class function)) | |
2284 (name (ad-read-advice-name function class))) | |
2285 (list function class name))) | |
2286 | |
2287 ;; Use previous regexp as a default: | |
2288 (defvar ad-last-regexp "") | |
2289 | |
2290 (defun ad-read-regexp (&optional prompt) | |
26217 | 2291 "Read a regular expression from the minibuffer." |
4110 | 2292 (let ((regexp (read-from-minibuffer |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2293 (concat (or prompt "Regular expression") |
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2294 (if (equal ad-last-regexp "") ": " |
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2295 (format " (default %s): " ad-last-regexp)))))) |
4110 | 2296 (setq ad-last-regexp |
2297 (if (equal regexp "") ad-last-regexp regexp)))) | |
2298 | |
2299 | |
2300 ;; @@ Finding, enabling, adding and removing pieces of advice: | |
2301 ;; =========================================================== | |
2302 | |
2303 (defmacro ad-find-advice (function class name) | |
26217 | 2304 "Find the first advice of FUNCTION in CLASS with NAME." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2305 `(assq ,name (ad-get-advice-info-field ,function ,class))) |
4110 | 2306 |
2307 (defun ad-advice-position (function class name) | |
26217 | 2308 "Return position of first advice of FUNCTION in CLASS with NAME." |
4110 | 2309 (let* ((found-advice (ad-find-advice function class name)) |
2310 (advices (ad-get-advice-info-field function class))) | |
2311 (if found-advice | |
2312 (- (length advices) (length (memq found-advice advices)))))) | |
2313 | |
2314 (defun ad-find-some-advice (function class name) | |
26217 | 2315 "Find the first of FUNCTION's advices in CLASS matching NAME. |
4110 | 2316 NAME can be a symbol or a regular expression matching part of an advice name. |
60923
08f8c7042636
* emacs-lisp/advice.el: Replace `legal' with `valid'.
Werner LEMBERG <wl@gnu.org>
parents:
57879
diff
changeset
|
2317 If CLASS is `any' all valid advice classes will be checked." |
4110 | 2318 (if (ad-is-advised function) |
2319 (let (found-advice) | |
2320 (ad-dolist (advice-class ad-advice-classes) | |
2321 (if (or (eq class 'any) (eq advice-class class)) | |
2322 (setq found-advice | |
2323 (ad-dolist (advice (ad-get-advice-info-field | |
2324 function advice-class)) | |
2325 (if (or (and (stringp name) | |
2326 (string-match | |
2327 name (symbol-name | |
2328 (ad-advice-name advice)))) | |
2329 (eq name (ad-advice-name advice))) | |
2330 (ad-do-return advice))))) | |
2331 (if found-advice (ad-do-return found-advice)))))) | |
2332 | |
2333 (defun ad-enable-advice-internal (function class name flag) | |
26217 | 2334 "Set enable FLAG of FUNCTION's advices in CLASS matching NAME. |
2335 If NAME is a string rather than a symbol then it's interpreted as a regular | |
2336 expression and all advices whose name contain a match for it will be | |
60923
08f8c7042636
* emacs-lisp/advice.el: Replace `legal' with `valid'.
Werner LEMBERG <wl@gnu.org>
parents:
57879
diff
changeset
|
2337 affected. If CLASS is `any' advices in all valid advice classes will be |
26217 | 2338 considered. The number of changed advices will be returned (or nil if |
2339 FUNCTION was not advised)." | |
4110 | 2340 (if (ad-is-advised function) |
2341 (let ((matched-advices 0)) | |
2342 (ad-dolist (advice-class ad-advice-classes) | |
2343 (if (or (eq class 'any) (eq advice-class class)) | |
2344 (ad-dolist (advice (ad-get-advice-info-field | |
2345 function advice-class)) | |
2346 (cond ((or (and (stringp name) | |
2347 (string-match | |
2348 name (symbol-name (ad-advice-name advice)))) | |
2349 (eq name (ad-advice-name advice))) | |
2350 (setq matched-advices (1+ matched-advices)) | |
2351 (ad-advice-set-enabled advice flag)))))) | |
2352 matched-advices))) | |
2353 | |
70899
b920ab84fba8
(ad-enable-advice, ad-activate, ad-disable-advice): Add autoloads.
Richard M. Stallman <rms@gnu.org>
parents:
68648
diff
changeset
|
2354 ;;;###autoload |
4110 | 2355 (defun ad-enable-advice (function class name) |
2356 "Enables the advice of FUNCTION with CLASS and NAME." | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2357 (interactive (ad-read-advice-specification "Enable advice of")) |
4110 | 2358 (if (ad-is-advised function) |
2359 (if (eq (ad-enable-advice-internal function class name t) 0) | |
2360 (error "ad-enable-advice: `%s' has no %s advice matching `%s'" | |
2361 function class name)) | |
2362 (error "ad-enable-advice: `%s' is not advised" function))) | |
2363 | |
70899
b920ab84fba8
(ad-enable-advice, ad-activate, ad-disable-advice): Add autoloads.
Richard M. Stallman <rms@gnu.org>
parents:
68648
diff
changeset
|
2364 ;;;###autoload |
4110 | 2365 (defun ad-disable-advice (function class name) |
26217 | 2366 "Disable the advice of FUNCTION with CLASS and NAME." |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2367 (interactive (ad-read-advice-specification "Disable advice of")) |
4110 | 2368 (if (ad-is-advised function) |
2369 (if (eq (ad-enable-advice-internal function class name nil) 0) | |
2370 (error "ad-disable-advice: `%s' has no %s advice matching `%s'" | |
2371 function class name)) | |
2372 (error "ad-disable-advice: `%s' is not advised" function))) | |
2373 | |
2374 (defun ad-enable-regexp-internal (regexp class flag) | |
26217 | 2375 "Set enable FLAGs of all CLASS advices whose name contains a REGEXP match. |
60923
08f8c7042636
* emacs-lisp/advice.el: Replace `legal' with `valid'.
Werner LEMBERG <wl@gnu.org>
parents:
57879
diff
changeset
|
2376 If CLASS is `any' all valid advice classes are considered. The number of |
26217 | 2377 affected advices will be returned." |
4110 | 2378 (let ((matched-advices 0)) |
2379 (ad-do-advised-functions (advised-function) | |
2380 (setq matched-advices | |
2381 (+ matched-advices | |
2382 (or (ad-enable-advice-internal | |
2383 advised-function class regexp flag) | |
2384 0)))) | |
2385 matched-advices)) | |
2386 | |
2387 (defun ad-enable-regexp (regexp) | |
2388 "Enables all advices with names that contain a match for REGEXP. | |
2389 All currently advised functions will be considered." | |
2390 (interactive | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2391 (list (ad-read-regexp "Enable advices via regexp"))) |
4110 | 2392 (let ((matched-advices (ad-enable-regexp-internal regexp 'any t))) |
105372
bd2966850aac
Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents:
105371
diff
changeset
|
2393 (if (called-interactively-p 'interactive) |
4110 | 2394 (message "%d matching advices enabled" matched-advices)) |
2395 matched-advices)) | |
2396 | |
2397 (defun ad-disable-regexp (regexp) | |
26217 | 2398 "Disable all advices with names that contain a match for REGEXP. |
4110 | 2399 All currently advised functions will be considered." |
2400 (interactive | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2401 (list (ad-read-regexp "Disable advices via regexp"))) |
4110 | 2402 (let ((matched-advices (ad-enable-regexp-internal regexp 'any nil))) |
105372
bd2966850aac
Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents:
105371
diff
changeset
|
2403 (if (called-interactively-p 'interactive) |
4110 | 2404 (message "%d matching advices disabled" matched-advices)) |
2405 matched-advices)) | |
2406 | |
2407 (defun ad-remove-advice (function class name) | |
26217 | 2408 "Remove FUNCTION's advice with NAME from its advices in CLASS. |
4110 | 2409 If such an advice was found it will be removed from the list of advices |
2410 in that CLASS." | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
2411 (interactive (ad-read-advice-specification "Remove advice of")) |
4110 | 2412 (if (ad-is-advised function) |
73251
25e1db3fd0ed
(ad-remove-advice, ad-parse-arglist, ad-make-mapped-call):
Juanma Barranquero <lekktu@gmail.com>
parents:
70899
diff
changeset
|
2413 (let ((advice-to-remove (ad-find-advice function class name))) |
4110 | 2414 (if advice-to-remove |
2415 (ad-set-advice-info-field | |
2416 function class | |
2417 (delq advice-to-remove (ad-get-advice-info-field function class))) | |
2418 (error "ad-remove-advice: `%s' has no %s advice `%s'" | |
2419 function class name))) | |
2420 (error "ad-remove-advice: `%s' is not advised" function))) | |
2421 | |
2422 ;;;###autoload | |
2423 (defun ad-add-advice (function advice class position) | |
26217 | 2424 "Add a piece of ADVICE to FUNCTION's list of advices in CLASS. |
106870
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2425 |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2426 ADVICE has the form (NAME PROTECTED ENABLED DEFINITION), where |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2427 NAME is the advice name; PROTECTED is a flag specifying whether |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2428 to protect against non-local exits; ENABLED is a flag specifying |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2429 whether to initially enable the advice; and DEFINITION has the |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2430 form (advice . LAMBDA), where LAMBDA is a lambda expression. |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2431 |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2432 If FUNCTION already has a piece of advice with the same name, |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2433 then POSITION is ignored, and the old advice is overwritten with |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2434 the new one. |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2435 |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2436 If FUNCTION already has one or more pieces of advice of the |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2437 specified CLASS, then POSITION determines where the new piece |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2438 goes. POSITION can either be `first', `last' or a number (where |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2439 0 corresponds to `first', and numbers outside the valid range are |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2440 mapped to the closest extremal position). |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2441 |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2442 If FUNCTION was not advised already, its advice info will be |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2443 initialized. Redefining a piece of advice whose name is part of |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2444 the cache-id will clear the cache. |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2445 |
3604fe47137d
* emacs-lisp/advice.el (ad-add-advice): Doc fix (Bug#5274)
Chong Yidong <cyd@stupidchicken.com>
parents:
106815
diff
changeset
|
2446 See Info node `(elisp)Computed Advice' for detailed documentation." |
4110 | 2447 (cond ((not (ad-is-advised function)) |
2448 (ad-initialize-advice-info function) | |
2449 (ad-set-advice-info-field | |
2450 function 'origname (ad-make-origname function)))) | |
2451 (let* ((previous-position | |
2452 (ad-advice-position function class (ad-advice-name advice))) | |
2453 (advices (ad-get-advice-info-field function class)) | |
2454 ;; Determine a numerical position for the new advice: | |
2455 (position (cond (previous-position) | |
2456 ((eq position 'first) 0) | |
2457 ((eq position 'last) (length advices)) | |
2458 ((numberp position) | |
2459 (max 0 (min position (length advices)))) | |
2460 (t 0)))) | |
2461 ;; Check whether we have to clear the cache: | |
2462 (if (memq (ad-advice-name advice) (ad-get-cache-class-id function class)) | |
2463 (ad-clear-cache function)) | |
2464 (if previous-position | |
2465 (setcar (nthcdr position advices) advice) | |
2466 (if (= position 0) | |
2467 (ad-set-advice-info-field function class (cons advice advices)) | |
2468 (setcdr (nthcdr (1- position) advices) | |
2469 (cons advice (nthcdr position advices))))))) | |
2470 | |
2471 | |
2472 ;; @@ Accessing and manipulating function definitions: | |
2473 ;; =================================================== | |
2474 | |
2475 (defmacro ad-macrofy (definition) | |
26217 | 2476 "Take a lambda function DEFINITION and make a macro out of it." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2477 `(cons 'macro ,definition)) |
4110 | 2478 |
2479 (defmacro ad-lambdafy (definition) | |
26217 | 2480 "Take a macro function DEFINITION and make a lambda out of it." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2481 `(cdr ,definition)) |
4110 | 2482 |
80879
cdee0fbb5943
(ad-special-forms): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
75346
diff
changeset
|
2483 (defun ad-special-form-p (definition) |
94737
f94711def01d
(ad-special-form-p): Don't use `iff' in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
94655
diff
changeset
|
2484 "Non-nil if and only if DEFINITION is a special form." |
80879
cdee0fbb5943
(ad-special-forms): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
75346
diff
changeset
|
2485 (if (and (symbolp definition) (fboundp definition)) |
cdee0fbb5943
(ad-special-forms): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
75346
diff
changeset
|
2486 (setq definition (indirect-function definition))) |
cdee0fbb5943
(ad-special-forms): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
75346
diff
changeset
|
2487 (and (subrp definition) (eq (cdr (subr-arity definition)) 'unevalled))) |
4110 | 2488 |
2489 (defmacro ad-subr-p (definition) | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2490 ;;"non-nil if DEFINITION is a subr." |
4110 | 2491 (list 'subrp definition)) |
2492 | |
2493 (defmacro ad-macro-p (definition) | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2494 ;;"non-nil if DEFINITION is a macro." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2495 `(eq (car-safe ,definition) 'macro)) |
4110 | 2496 |
2497 (defmacro ad-lambda-p (definition) | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2498 ;;"non-nil if DEFINITION is a lambda expression." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2499 `(eq (car-safe ,definition) 'lambda)) |
4110 | 2500 |
2501 ;; see ad-make-advice for the format of advice definitions: | |
2502 (defmacro ad-advice-p (definition) | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2503 ;;"non-nil if DEFINITION is a piece of advice." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2504 `(eq (car-safe ,definition) 'advice)) |
4110 | 2505 |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2506 ;; Emacs/Lemacs cross-compatibility |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2507 ;; (compiled-function-p is an obsolete function in Emacs): |
4110 | 2508 (if (and (not (fboundp 'byte-code-function-p)) |
2509 (fboundp 'compiled-function-p)) | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2510 (ad-safe-fset 'byte-code-function-p 'compiled-function-p)) |
4110 | 2511 |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2512 (defmacro ad-compiled-p (definition) |
26217 | 2513 "Return non-nil if DEFINITION is a compiled byte-code object." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2514 `(or (byte-code-function-p ,definition) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2515 (and (ad-macro-p ,definition) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2516 (byte-code-function-p (ad-lambdafy ,definition))))) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2517 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2518 (defmacro ad-compiled-code (compiled-definition) |
26217 | 2519 "Return the byte-code object of a COMPILED-DEFINITION." |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2520 `(if (ad-macro-p ,compiled-definition) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2521 (ad-lambdafy ,compiled-definition) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2522 ,compiled-definition)) |
4110 | 2523 |
2524 (defun ad-lambda-expression (definition) | |
26217 | 2525 "Return the lambda expression of a function/macro/advice DEFINITION." |
4110 | 2526 (cond ((ad-lambda-p definition) |
2527 definition) | |
2528 ((ad-macro-p definition) | |
2529 (ad-lambdafy definition)) | |
2530 ((ad-advice-p definition) | |
2531 (cdr definition)) | |
2532 (t nil))) | |
2533 | |
2534 (defun ad-arglist (definition &optional name) | |
26217 | 2535 "Return the argument list of DEFINITION. |
2536 If DEFINITION could be from a subr then its NAME should be | |
2537 supplied to make subr arglist lookup more efficient." | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2538 (cond ((ad-compiled-p definition) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2539 (aref (ad-compiled-code definition) 0)) |
4110 | 2540 ((consp definition) |
2541 (car (cdr (ad-lambda-expression definition)))) | |
2542 ((ad-subr-p definition) | |
2543 (if name | |
2544 (ad-subr-arglist name) | |
2545 ;; otherwise get it from its printed representation: | |
2546 (setq name (format "%s" definition)) | |
2547 (string-match "^#<subr \\([^>]+\\)>$" name) | |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2548 (ad-subr-arglist (intern (match-string 1 name))))))) |
4110 | 2549 |
2550 ;; Store subr-args as `((arg1 arg2 ...))' so I can distinguish | |
2551 ;; a defined empty arglist `(nil)' from an undefined arglist: | |
2552 (defmacro ad-define-subr-args (subr arglist) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2553 `(put ,subr 'ad-subr-arglist (list ,arglist))) |
4110 | 2554 (defmacro ad-undefine-subr-args (subr) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2555 `(put ,subr 'ad-subr-arglist nil)) |
4110 | 2556 (defmacro ad-subr-args-defined-p (subr) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2557 `(get ,subr 'ad-subr-arglist)) |
4110 | 2558 (defmacro ad-get-subr-args (subr) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2559 `(car (get ,subr 'ad-subr-arglist))) |
4110 | 2560 |
2561 (defun ad-subr-arglist (subr-name) | |
26217 | 2562 "Retrieve arglist of the subr with SUBR-NAME. |
2563 Either use the one stored under the `ad-subr-arglist' property, | |
2564 or try to retrieve it from the docstring and cache it under | |
2565 that property, or otherwise use `(&rest ad-subr-args)'." | |
54493
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2566 (if (ad-subr-args-defined-p subr-name) |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2567 (ad-get-subr-args subr-name) |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2568 ;; says jwz: Should use this for Lemacs 19.8 and above: |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2569 ;;((fboundp 'subr-min-args) |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2570 ;; ...) |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2571 ;; says hans: I guess what Jamie means is that I should use the values |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2572 ;; of `subr-min-args' and `subr-max-args' to construct the subr arglist |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2573 ;; without having to look it up via parsing the docstring, e.g., |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2574 ;; values 1 and 2 would suggest `(arg1 &optional arg2)' as an |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2575 ;; argument list. However, that won't work because there is no |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2576 ;; way to distinguish a subr with args `(a &optional b &rest c)' from |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2577 ;; one with args `(a &rest c)' using that mechanism. Also, the argument |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2578 ;; names from the docstring are more meaningful. Hence, I'll stick with |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2579 ;; the old way of doing things. |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2580 (let ((doc (or (ad-real-documentation subr-name t) ""))) |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2581 (if (not (string-match "\n\n\\((.+)\\)\\'" doc)) |
54509
e20bae3e3270
(ad-subr-arglist): Undo part of last patch.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54493
diff
changeset
|
2582 ;; Signalling an error leads to bugs during bootstrapping because |
e20bae3e3270
(ad-subr-arglist): Undo part of last patch.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54493
diff
changeset
|
2583 ;; the DOC file is not yet built (which is an error, BTW). |
e20bae3e3270
(ad-subr-arglist): Undo part of last patch.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54493
diff
changeset
|
2584 ;; (error "The usage info is missing from the subr %s" subr-name) |
e20bae3e3270
(ad-subr-arglist): Undo part of last patch.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54493
diff
changeset
|
2585 '(&rest ad-subr-args) |
54493
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2586 (ad-define-subr-args |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2587 subr-name |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2588 (cdr (car (read-from-string |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2589 (downcase (match-string 1 doc)))))) |
d8586f19729a
(ad-subr-arglist): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
2590 (ad-get-subr-args subr-name))))) |
4110 | 2591 |
2592 (defun ad-docstring (definition) | |
26217 | 2593 "Return the unexpanded docstring of DEFINITION." |
4110 | 2594 (let ((docstring |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2595 (if (ad-compiled-p definition) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2596 (ad-real-documentation definition t) |
4110 | 2597 (car (cdr (cdr (ad-lambda-expression definition))))))) |
2598 (if (or (stringp docstring) | |
2599 (natnump docstring)) | |
2600 docstring))) | |
2601 | |
2602 (defun ad-interactive-form (definition) | |
82192
5800574abbcb
(ad-interactive-form): Re-introduce.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82146
diff
changeset
|
2603 "Return the interactive form of DEFINITION. |
5800574abbcb
(ad-interactive-form): Re-introduce.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82146
diff
changeset
|
2604 Like `interactive-form', but also works on pieces of advice." |
5800574abbcb
(ad-interactive-form): Re-introduce.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82146
diff
changeset
|
2605 (interactive-form |
5800574abbcb
(ad-interactive-form): Re-introduce.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82146
diff
changeset
|
2606 (if (ad-advice-p definition) |
5800574abbcb
(ad-interactive-form): Re-introduce.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82146
diff
changeset
|
2607 (ad-lambda-expression definition) |
5800574abbcb
(ad-interactive-form): Re-introduce.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82146
diff
changeset
|
2608 definition))) |
4110 | 2609 |
2610 (defun ad-body-forms (definition) | |
26217 | 2611 "Return the list of body forms of DEFINITION." |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2612 (cond ((ad-compiled-p definition) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2613 nil) |
4110 | 2614 ((consp definition) |
2615 (nthcdr (+ (if (ad-docstring definition) 1 0) | |
2616 (if (ad-interactive-form definition) 1 0)) | |
2617 (cdr (cdr (ad-lambda-expression definition))))))) | |
2618 | |
2619 (defun ad-make-advised-definition-docstring (function) | |
26217 | 2620 "Make an identifying docstring for the advised definition of FUNCTION. |
2621 Put function name into the documentation string so we can infer | |
2622 the name of the advised function from the docstring. This is needed | |
2623 to generate a proper advised docstring even if we are just given a | |
85302
bc8b741390b0
(documentation): Advice deleted. Doc for advised functions
Richard M. Stallman <rms@gnu.org>
parents:
83896
diff
changeset
|
2624 definition (see the code for `documentation')." |
bc8b741390b0
(documentation): Advice deleted. Doc for advised functions
Richard M. Stallman <rms@gnu.org>
parents:
83896
diff
changeset
|
2625 (propertize "Advice doc string" 'ad-advice-info function)) |
4110 | 2626 |
2627 (defun ad-advised-definition-p (definition) | |
26217 | 2628 "Return non-nil if DEFINITION was generated from advice information." |
4110 | 2629 (if (or (ad-lambda-p definition) |
2630 (ad-macro-p definition) | |
2631 (ad-compiled-p definition)) | |
2632 (let ((docstring (ad-docstring definition))) | |
2633 (and (stringp docstring) | |
85302
bc8b741390b0
(documentation): Advice deleted. Doc for advised functions
Richard M. Stallman <rms@gnu.org>
parents:
83896
diff
changeset
|
2634 (get-text-property 0 'ad-advice-info docstring))))) |
4110 | 2635 |
2636 (defun ad-definition-type (definition) | |
26217 | 2637 "Return symbol that describes the type of DEFINITION." |
4110 | 2638 (if (ad-macro-p definition) |
2639 'macro | |
2640 (if (ad-subr-p definition) | |
2641 (if (ad-special-form-p definition) | |
2642 'special-form | |
2643 'subr) | |
2644 (if (or (ad-lambda-p definition) | |
2645 (ad-compiled-p definition)) | |
2646 'function | |
2647 (if (ad-advice-p definition) | |
2648 'advice))))) | |
2649 | |
2650 (defun ad-has-proper-definition (function) | |
26217 | 2651 "True if FUNCTION is a symbol with a proper definition. |
2652 For that it has to be fbound with a non-autoload definition." | |
4110 | 2653 (and (symbolp function) |
2654 (fboundp function) | |
2655 (not (eq (car-safe (symbol-function function)) 'autoload)))) | |
2656 | |
2657 ;; The following two are necessary for the sake of packages such as | |
2658 ;; ange-ftp which redefine functions via fcell indirection: | |
2659 (defun ad-real-definition (function) | |
26217 | 2660 "Find FUNCTION's definition at the end of function cell indirection." |
4110 | 2661 (if (ad-has-proper-definition function) |
2662 (let ((definition (symbol-function function))) | |
2663 (if (symbolp definition) | |
2664 (ad-real-definition definition) | |
2665 definition)))) | |
2666 | |
2667 (defun ad-real-orig-definition (function) | |
26217 | 2668 "Find FUNCTION's real original definition starting from its `origname'." |
4110 | 2669 (if (ad-is-advised function) |
2670 (ad-real-definition (ad-get-advice-info-field function 'origname)))) | |
2671 | |
2672 (defun ad-is-compilable (function) | |
26217 | 2673 "True if FUNCTION has an interpreted definition that can be compiled." |
4110 | 2674 (and (ad-has-proper-definition function) |
2675 (or (ad-lambda-p (symbol-function function)) | |
2676 (ad-macro-p (symbol-function function))) | |
2677 (not (ad-compiled-p (symbol-function function))))) | |
2678 | |
2679 (defun ad-compile-function (function) | |
2680 "Byte-compiles FUNCTION (or macro) if it is not yet compiled." | |
2681 (interactive "aByte-compile function: ") | |
2682 (if (ad-is-compilable function) | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2683 ;; Need to turn off auto-activation |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2684 ;; because `byte-compile' uses `fset': |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
2685 (ad-with-auto-activation-disabled |
46196
73cafacdf14f
(ad-compile-function): Disable cl-function warnings if cl is loaded.
Richard M. Stallman <rms@gnu.org>
parents:
45019
diff
changeset
|
2686 (require 'bytecomp) |
108944
b9c2b845f2e6
* lisp/emacs-lisp/advice.el (ad-compile-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
107153
diff
changeset
|
2687 (require 'warnings) ;To define warning-suppress-types |
b9c2b845f2e6
* lisp/emacs-lisp/advice.el (ad-compile-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
107153
diff
changeset
|
2688 ;before we let-bind it. |
46196
73cafacdf14f
(ad-compile-function): Disable cl-function warnings if cl is loaded.
Richard M. Stallman <rms@gnu.org>
parents:
45019
diff
changeset
|
2689 (let ((symbol (make-symbol "advice-compilation")) |
107152
34e2c3832e08
(ad-compile-function): Suppress byte-compiler warnings, since it is annoying
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
106966
diff
changeset
|
2690 (byte-compile-warnings byte-compile-warnings) |
34e2c3832e08
(ad-compile-function): Suppress byte-compiler warnings, since it is annoying
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
106966
diff
changeset
|
2691 ;; Don't pop up windows showing byte-compiler warnings. |
107153
2c62b10c201b
(ad-compile-function): Fix last change.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
107152
diff
changeset
|
2692 (warning-suppress-types '((bytecomp)))) |
46196
73cafacdf14f
(ad-compile-function): Disable cl-function warnings if cl is loaded.
Richard M. Stallman <rms@gnu.org>
parents:
45019
diff
changeset
|
2693 (if (featurep 'cl) |
85729
bd2837f89a3b
(ad-compile-function): Use byte-compile-disable-warning.
Glenn Morris <rgm@gnu.org>
parents:
85584
diff
changeset
|
2694 (byte-compile-disable-warning 'cl-functions)) |
41936 | 2695 (fset symbol (symbol-function function)) |
2696 (byte-compile symbol) | |
2697 (fset function (symbol-function symbol)))))) | |
4110 | 2698 |
2699 | |
2700 ;; @@ Constructing advised definitions: | |
2701 ;; ==================================== | |
2702 ;; | |
2703 ;; Main design decisions about the form of advised definitions: | |
2704 ;; | |
2705 ;; A) How will original definitions be called? | |
2706 ;; B) What will argument lists of advised functions look like? | |
2707 ;; | |
2708 ;; Ad A) | |
2709 ;; I chose to use function indirection for all four types of original | |
2710 ;; definitions (functions, macros, subrs and special forms), i.e., create | |
2711 ;; a unique symbol `ad-Orig-<name>' which is fbound to the original | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2712 ;; definition and call it according to type and arguments. Functions and |
4110 | 2713 ;; subrs that don't have any &rest arguments can be called directly in a |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2714 ;; `(ad-Orig-<name> ....)' form. If they have a &rest argument we have to |
26217 | 2715 ;; use `apply'. Macros will be called with |
4110 | 2716 ;; `(macroexpand '(ad-Orig-<name> ....))', and special forms also need a |
2717 ;; form like that with `eval' instead of `macroexpand'. | |
2718 ;; | |
2719 ;; Ad B) | |
2720 ;; Use original arguments where possible and `(&rest ad-subr-args)' | |
2721 ;; otherwise, even though this seems to be more complicated and less | |
2722 ;; uniform than a general `(&rest args)' approach. My reason to still | |
2723 ;; do it that way is that in most cases my approach leads to the more | |
2724 ;; efficient form for the advised function, and portability (e.g., to | |
2725 ;; make the same advice work regardless of whether something is a | |
2726 ;; function or a subr) can still be achieved with argument access macros. | |
2727 | |
2728 | |
2729 (defun ad-prognify (forms) | |
2730 (cond ((<= (length forms) 1) | |
2731 (car forms)) | |
2732 (t (cons 'progn forms)))) | |
2733 | |
2734 ;; @@@ Accessing argument lists: | |
2735 ;; ============================= | |
2736 | |
2737 (defun ad-parse-arglist (arglist) | |
26217 | 2738 "Parse ARGLIST into its required, optional and rest parameters. |
2739 A three-element list is returned, where the 1st element is the list of | |
2740 required arguments, the 2nd is the list of optional arguments, and the 3rd | |
2741 is the name of an optional rest parameter (or nil)." | |
73251
25e1db3fd0ed
(ad-remove-advice, ad-parse-arglist, ad-make-mapped-call):
Juanma Barranquero <lekktu@gmail.com>
parents:
70899
diff
changeset
|
2742 (let (required optional rest) |
4110 | 2743 (setq rest (car (cdr (memq '&rest arglist)))) |
2744 (if rest (setq arglist (reverse (cdr (memq '&rest (reverse arglist)))))) | |
2745 (setq optional (cdr (memq '&optional arglist))) | |
2746 (if optional | |
2747 (setq required (reverse (cdr (memq '&optional (reverse arglist))))) | |
2748 (setq required arglist)) | |
2749 (list required optional rest))) | |
2750 | |
2751 (defun ad-retrieve-args-form (arglist) | |
26217 | 2752 "Generate a form which evaluates into names/values/types of ARGLIST. |
2753 When the form gets evaluated within a function with that argument list | |
2754 it will result in a list with one entry for each argument, where the | |
2755 first element of each entry is the name of the argument, the second | |
2756 element is its actual current value, and the third element is either | |
2757 `required', `optional' or `rest' depending on the type of the argument." | |
4110 | 2758 (let* ((parsed-arglist (ad-parse-arglist arglist)) |
2759 (rest (nth 2 parsed-arglist))) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2760 `(list |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2761 ,@(mapcar (function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2762 (lambda (req) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2763 `(list ',req ,req 'required))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2764 (nth 0 parsed-arglist)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2765 ,@(mapcar (function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2766 (lambda (opt) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2767 `(list ',opt ,opt 'optional))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2768 (nth 1 parsed-arglist)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2769 ,@(if rest (list `(list ',rest ,rest 'rest)))))) |
4110 | 2770 |
2771 (defun ad-arg-binding-field (binding field) | |
2772 (cond ((eq field 'name) (car binding)) | |
2773 ((eq field 'value) (car (cdr binding))) | |
2774 ((eq field 'type) (car (cdr (cdr binding)))))) | |
2775 | |
2776 (defun ad-list-access (position list) | |
2777 (cond ((= position 0) list) | |
2778 ((= position 1) (list 'cdr list)) | |
2779 (t (list 'nthcdr position list)))) | |
2780 | |
2781 (defun ad-element-access (position list) | |
2782 (cond ((= position 0) (list 'car list)) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2783 ((= position 1) `(car (cdr ,list))) |
4110 | 2784 (t (list 'nth position list)))) |
2785 | |
2786 (defun ad-access-argument (arglist index) | |
26217 | 2787 "Tell how to access ARGLIST's actual argument at position INDEX. |
2788 For a required/optional arg it simply returns it, if a rest argument has | |
2789 to be accessed, it returns a list with the index and name." | |
4110 | 2790 (let* ((parsed-arglist (ad-parse-arglist arglist)) |
2791 (reqopt-args (append (nth 0 parsed-arglist) | |
2792 (nth 1 parsed-arglist))) | |
2793 (rest-arg (nth 2 parsed-arglist))) | |
2794 (cond ((< index (length reqopt-args)) | |
2795 (nth index reqopt-args)) | |
2796 (rest-arg | |
2797 (list (- index (length reqopt-args)) rest-arg))))) | |
2798 | |
2799 (defun ad-get-argument (arglist index) | |
105113
1a0de48a918c
* emacs-lisp/advice.el (ad-get-argument, ad-set-argument): Doc
Chong Yidong <cyd@stupidchicken.com>
parents:
104024
diff
changeset
|
2800 "Return form to access ARGLIST's actual argument at position INDEX. |
1a0de48a918c
* emacs-lisp/advice.el (ad-get-argument, ad-set-argument): Doc
Chong Yidong <cyd@stupidchicken.com>
parents:
104024
diff
changeset
|
2801 INDEX counts from zero." |
4110 | 2802 (let ((argument-access (ad-access-argument arglist index))) |
2803 (cond ((consp argument-access) | |
2804 (ad-element-access | |
2805 (car argument-access) (car (cdr argument-access)))) | |
2806 (argument-access)))) | |
2807 | |
2808 (defun ad-set-argument (arglist index value-form) | |
105113
1a0de48a918c
* emacs-lisp/advice.el (ad-get-argument, ad-set-argument): Doc
Chong Yidong <cyd@stupidchicken.com>
parents:
104024
diff
changeset
|
2809 "Return form to set ARGLIST's actual arg at INDEX to VALUE-FORM. |
1a0de48a918c
* emacs-lisp/advice.el (ad-get-argument, ad-set-argument): Doc
Chong Yidong <cyd@stupidchicken.com>
parents:
104024
diff
changeset
|
2810 INDEX counts from zero." |
4110 | 2811 (let ((argument-access (ad-access-argument arglist index))) |
2812 (cond ((consp argument-access) | |
2813 ;; should this check whether there actually is something to set? | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2814 `(setcar ,(ad-list-access |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2815 (car argument-access) (car (cdr argument-access))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2816 ,value-form)) |
4110 | 2817 (argument-access |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2818 `(setq ,argument-access ,value-form)) |
4110 | 2819 (t (error "ad-set-argument: No argument at position %d of `%s'" |
2820 index arglist))))) | |
2821 | |
2822 (defun ad-get-arguments (arglist index) | |
26217 | 2823 "Return form to access all actual arguments starting at position INDEX." |
4110 | 2824 (let* ((parsed-arglist (ad-parse-arglist arglist)) |
2825 (reqopt-args (append (nth 0 parsed-arglist) | |
2826 (nth 1 parsed-arglist))) | |
2827 (rest-arg (nth 2 parsed-arglist)) | |
2828 args-form) | |
2829 (if (< index (length reqopt-args)) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2830 (setq args-form `(list ,@(nthcdr index reqopt-args)))) |
4110 | 2831 (if rest-arg |
2832 (if args-form | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2833 (setq args-form `(nconc ,args-form ,rest-arg)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2834 (setq args-form (ad-list-access (- index (length reqopt-args)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2835 rest-arg)))) |
4110 | 2836 args-form)) |
2837 | |
2838 (defun ad-set-arguments (arglist index values-form) | |
26217 | 2839 "Make form to assign elements of VALUES-FORM as actual ARGLIST args. |
2840 The assignment starts at position INDEX." | |
4110 | 2841 (let ((values-index 0) |
2842 argument-access set-forms) | |
2843 (while (setq argument-access (ad-access-argument arglist index)) | |
2844 (if (symbolp argument-access) | |
2845 (setq set-forms | |
2846 (cons (ad-set-argument | |
2847 arglist index | |
2848 (ad-element-access values-index 'ad-vAlUeS)) | |
2849 set-forms)) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2850 (setq set-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2851 (cons (if (= (car argument-access) 0) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2852 (list 'setq |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2853 (car (cdr argument-access)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2854 (ad-list-access values-index 'ad-vAlUeS)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2855 (list 'setcdr |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2856 (ad-list-access (1- (car argument-access)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2857 (car (cdr argument-access))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2858 (ad-list-access values-index 'ad-vAlUeS))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2859 set-forms)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2860 ;; terminate loop |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2861 (setq arglist nil)) |
4110 | 2862 (setq index (1+ index)) |
2863 (setq values-index (1+ values-index))) | |
2864 (if (null set-forms) | |
2865 (error "ad-set-arguments: No argument at position %d of `%s'" | |
2866 index arglist) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2867 (if (= (length set-forms) 1) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2868 ;; For exactly one set-form we can use values-form directly,... |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2869 (ad-substitute-tree |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2870 (function (lambda (form) (eq form 'ad-vAlUeS))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2871 (function (lambda (form) values-form)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2872 (car set-forms)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2873 ;; ...if we have more we have to bind it to a variable: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2874 `(let ((ad-vAlUeS ,values-form)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2875 ,@(reverse set-forms) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2876 ;; work around the old backquote bug: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
2877 ,'ad-vAlUeS))))) |
4110 | 2878 |
2879 (defun ad-insert-argument-access-forms (definition arglist) | |
26217 | 2880 "Expands arg-access text macros in DEFINITION according to ARGLIST." |
4110 | 2881 (ad-substitute-tree |
2882 (function | |
2883 (lambda (form) | |
2884 (or (eq form 'ad-arg-bindings) | |
2885 (and (memq (car-safe form) | |
2886 '(ad-get-arg ad-get-args ad-set-arg ad-set-args)) | |
2887 (integerp (car-safe (cdr form))))))) | |
2888 (function | |
2889 (lambda (form) | |
2890 (if (eq form 'ad-arg-bindings) | |
2891 (ad-retrieve-args-form arglist) | |
2892 (let ((accessor (car form)) | |
2893 (index (car (cdr form))) | |
2894 (val (car (cdr (ad-insert-argument-access-forms | |
2895 (cdr form) arglist))))) | |
2896 (cond ((eq accessor 'ad-get-arg) | |
2897 (ad-get-argument arglist index)) | |
2898 ((eq accessor 'ad-set-arg) | |
2899 (ad-set-argument arglist index val)) | |
2900 ((eq accessor 'ad-get-args) | |
2901 (ad-get-arguments arglist index)) | |
2902 ((eq accessor 'ad-set-args) | |
2903 (ad-set-arguments arglist index val))))))) | |
2904 definition)) | |
2905 | |
2906 ;; @@@ Mapping argument lists: | |
2907 ;; =========================== | |
2908 ;; Here is the problem: | |
2909 ;; Suppose function foo was called with (foo 1 2 3 4 5), and foo has the | |
2910 ;; argument list (x y &rest z), and we want to call the function bar which | |
2911 ;; has argument list (a &rest b) with a combination of x, y and z so that | |
26217 | 2912 ;; the effect is just as if we had called (bar 1 2 3 4 5) directly. |
4110 | 2913 ;; The mapping should work for any two argument lists. |
2914 | |
2915 (defun ad-map-arglists (source-arglist target-arglist) | |
26217 | 2916 "Make `funcall/apply' form to map SOURCE-ARGLIST to TARGET-ARGLIST. |
4110 | 2917 The arguments supplied to TARGET-ARGLIST will be taken from SOURCE-ARGLIST just |
26217 | 2918 as if they had been supplied to a function with TARGET-ARGLIST directly. |
2919 Excess source arguments will be neglected, missing source arguments will be | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2920 supplied as nil. Returns a `funcall' or `apply' form with the second element |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2921 being `function' which has to be replaced by an actual function argument. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2922 Example: `(ad-map-arglists '(a &rest args) '(w x y z))' will return |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2923 `(funcall function a (car args) (car (cdr args)) (nth 2 args))'." |
4110 | 2924 (let* ((parsed-source-arglist (ad-parse-arglist source-arglist)) |
2925 (source-reqopt-args (append (nth 0 parsed-source-arglist) | |
2926 (nth 1 parsed-source-arglist))) | |
2927 (source-rest-arg (nth 2 parsed-source-arglist)) | |
2928 (parsed-target-arglist (ad-parse-arglist target-arglist)) | |
2929 (target-reqopt-args (append (nth 0 parsed-target-arglist) | |
2930 (nth 1 parsed-target-arglist))) | |
2931 (target-rest-arg (nth 2 parsed-target-arglist)) | |
2932 (need-apply (and source-rest-arg target-rest-arg)) | |
2933 (target-arg-index -1)) | |
2934 ;; This produces ``error-proof'' target function calls with the exception | |
2935 ;; of a case like (&rest a) mapped onto (x &rest y) where the actual args | |
2936 ;; supplied to A might not be enough to supply the required target arg X | |
2937 (append (list (if need-apply 'apply 'funcall) 'function) | |
2938 (cond (need-apply | |
2939 ;; `apply' can take care of that directly: | |
2940 (append source-reqopt-args (list source-rest-arg))) | |
2941 (t (mapcar (function | |
2942 (lambda (arg) | |
2943 (setq target-arg-index (1+ target-arg-index)) | |
2944 (ad-get-argument | |
2945 source-arglist target-arg-index))) | |
2946 (append target-reqopt-args | |
2947 (and target-rest-arg | |
2948 ;; If we have a rest arg gobble up | |
2949 ;; remaining source args: | |
2950 (nthcdr (length target-reqopt-args) | |
2951 source-reqopt-args))))))))) | |
2952 | |
2953 (defun ad-make-mapped-call (source-arglist target-arglist target-function) | |
26217 | 2954 "Make form to call TARGET-FUNCTION with args from SOURCE-ARGLIST." |
73251
25e1db3fd0ed
(ad-remove-advice, ad-parse-arglist, ad-make-mapped-call):
Juanma Barranquero <lekktu@gmail.com>
parents:
70899
diff
changeset
|
2955 (let ((mapped-form (ad-map-arglists source-arglist target-arglist))) |
4110 | 2956 (if (eq (car mapped-form) 'funcall) |
2957 (cons target-function (cdr (cdr mapped-form))) | |
2958 (prog1 mapped-form | |
2959 (setcar (cdr mapped-form) (list 'quote target-function)))))) | |
2960 | |
2961 ;; @@@ Making an advised documentation string: | |
2962 ;; =========================================== | |
2963 ;; New policy: The documentation string for an advised function will be built | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2964 ;; at the time the advised `documentation' function is called. This has the |
4110 | 2965 ;; following advantages: |
2966 ;; 1) command-key substitutions will automatically be correct | |
2967 ;; 2) No wasted string space due to big advised docstrings in caches or | |
2968 ;; compiled files that contain preactivations | |
2969 ;; The overall overhead for this should be negligible because people normally | |
2970 ;; don't lookup documentation for the same function over and over again. | |
2971 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2972 (defun ad-make-single-advice-docstring (advice class &optional style) |
4110 | 2973 (let ((advice-docstring (ad-docstring (ad-advice-definition advice)))) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2974 (cond ((eq style 'plain) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2975 advice-docstring) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2976 ((eq style 'freeze) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2977 (format "Permanent %s-advice `%s':%s%s" |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2978 class (ad-advice-name advice) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2979 (if advice-docstring "\n" "") |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2980 (or advice-docstring ""))) |
25208
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2981 (t (if advice-docstring |
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2982 (format "%s-advice `%s':\n%s" |
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2983 (capitalize (symbol-name class)) |
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2984 (ad-advice-name advice) |
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2985 advice-docstring) |
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2986 (format "%s-advice `%s'." |
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2987 (capitalize (symbol-name class)) |
76f5f50e7742
(ad-make-single-advice-docstring): Treat case with no doctring specially.
Dave Love <fx@gnu.org>
parents:
24875
diff
changeset
|
2988 (ad-advice-name advice))))))) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2989 |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2990 (require 'help-fns) ;For help-split-fundoc and help-add-fundoc-usage. |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2991 |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
2992 (defun ad-make-advised-docstring (function &optional style) |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2993 "Construct a documentation string for the advised FUNCTION. |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2994 It concatenates the original documentation with the documentation |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2995 strings of the individual pieces of advice which will be formatted |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2996 according to STYLE. STYLE can be `plain' or `freeze', everything else |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2997 will be interpreted as `default'. The order of the advice documentation |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2998 strings corresponds to before/around/after and the individual ordering |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
2999 in any of these classes." |
4110 | 3000 (let* ((origdef (ad-real-orig-definition function)) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3001 (origtype (symbol-name (ad-definition-type origdef))) |
4110 | 3002 (origdoc |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3003 ;; Retrieve raw doc, key substitution will be taken care of later: |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3004 (ad-real-documentation origdef t)) |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
3005 (usage (help-split-fundoc origdoc function)) |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
3006 paragraphs advice-docstring ad-usage) |
50842
b64d29a02737
(ad-make-advised-docstring): Adjust usage for new help-add-fundoc-usage.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
50800
diff
changeset
|
3007 (setq usage (if (null usage) t (setq origdoc (cdr usage)) (car usage))) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3008 (if origdoc (setq paragraphs (list origdoc))) |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
3009 (unless (eq style 'plain) |
83896
0fc42eec71c0
Johan Bockg? <bojohan at dd.chalmers.se>
Glenn Morris <rgm@gnu.org>
parents:
82192
diff
changeset
|
3010 (push (propertize (concat "This " origtype " is advised.") |
0fc42eec71c0
Johan Bockg? <bojohan at dd.chalmers.se>
Glenn Morris <rgm@gnu.org>
parents:
82192
diff
changeset
|
3011 'face 'font-lock-warning-face) |
0fc42eec71c0
Johan Bockg? <bojohan at dd.chalmers.se>
Glenn Morris <rgm@gnu.org>
parents:
82192
diff
changeset
|
3012 paragraphs)) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3013 (ad-dolist (class ad-advice-classes) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3014 (ad-dolist (advice (ad-get-enabled-advices function class)) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3015 (setq advice-docstring |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3016 (ad-make-single-advice-docstring advice class style)) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3017 (if advice-docstring |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
3018 (push advice-docstring paragraphs)))) |
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
3019 (setq origdoc (if paragraphs |
85584
91aa73da6526
(ad-make-advised-docstring): Add ad-advice-info text property to doc string.
Juanma Barranquero <lekktu@gmail.com>
parents:
85320
diff
changeset
|
3020 (propertize |
91aa73da6526
(ad-make-advised-docstring): Add ad-advice-info text property to doc string.
Juanma Barranquero <lekktu@gmail.com>
parents:
85320
diff
changeset
|
3021 ;; separate paragraphs with blank lines: |
91aa73da6526
(ad-make-advised-docstring): Add ad-advice-info text property to doc string.
Juanma Barranquero <lekktu@gmail.com>
parents:
85320
diff
changeset
|
3022 (mapconcat 'identity (nreverse paragraphs) "\n\n") |
91aa73da6526
(ad-make-advised-docstring): Add ad-advice-info text property to doc string.
Juanma Barranquero <lekktu@gmail.com>
parents:
85320
diff
changeset
|
3023 'ad-advice-info function))) |
50800
7fe53d25e220
(ad-get-enabled-advices, ad-special-forms)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46196
diff
changeset
|
3024 (help-add-fundoc-usage origdoc usage))) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3025 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3026 (defun ad-make-plain-docstring (function) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3027 (ad-make-advised-docstring function 'plain)) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3028 (defun ad-make-freeze-docstring (function) |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3029 (ad-make-advised-docstring function 'freeze)) |
4110 | 3030 |
3031 ;; @@@ Accessing overriding arglists and interactive forms: | |
3032 ;; ======================================================== | |
3033 | |
3034 (defun ad-advised-arglist (function) | |
26217 | 3035 "Find first defined arglist in FUNCTION's redefining advices." |
4110 | 3036 (ad-dolist (advice (append (ad-get-enabled-advices function 'before) |
3037 (ad-get-enabled-advices function 'around) | |
3038 (ad-get-enabled-advices function 'after))) | |
3039 (let ((arglist (ad-arglist (ad-advice-definition advice)))) | |
3040 (if arglist | |
3041 ;; We found the first one, use it: | |
3042 (ad-do-return arglist))))) | |
3043 | |
3044 (defun ad-advised-interactive-form (function) | |
26217 | 3045 "Find first interactive form in FUNCTION's redefining advices." |
4110 | 3046 (ad-dolist (advice (append (ad-get-enabled-advices function 'before) |
3047 (ad-get-enabled-advices function 'around) | |
3048 (ad-get-enabled-advices function 'after))) | |
3049 (let ((interactive-form | |
3050 (ad-interactive-form (ad-advice-definition advice)))) | |
3051 (if interactive-form | |
3052 ;; We found the first one, use it: | |
3053 (ad-do-return interactive-form))))) | |
3054 | |
3055 ;; @@@ Putting it all together: | |
3056 ;; ============================ | |
3057 | |
3058 (defun ad-make-advised-definition (function) | |
26217 | 3059 "Generate an advised definition of FUNCTION from its advice info." |
4110 | 3060 (if (and (ad-is-advised function) |
3061 (ad-has-redefining-advice function)) | |
3062 (let* ((origdef (ad-real-orig-definition function)) | |
3063 (origname (ad-get-advice-info-field function 'origname)) | |
82146
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3064 (orig-interactive-p (commandp origdef)) |
4110 | 3065 (orig-subr-p (ad-subr-p origdef)) |
3066 (orig-special-form-p (ad-special-form-p origdef)) | |
3067 (orig-macro-p (ad-macro-p origdef)) | |
3068 ;; Construct the individual pieces that we need for assembly: | |
3069 (orig-arglist (ad-arglist origdef function)) | |
3070 (advised-arglist (or (ad-advised-arglist function) | |
3071 orig-arglist)) | |
3072 (advised-interactive-form (ad-advised-interactive-form function)) | |
3073 (interactive-form | |
3074 (cond (orig-macro-p nil) | |
3075 (advised-interactive-form) | |
82146
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3076 ((interactive-form origdef) |
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3077 (interactive-form |
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3078 (if (and (symbolp function) (get function 'elp-info)) |
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3079 (aref (get function 'elp-info) 2) |
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3080 origdef))))) |
4110 | 3081 (orig-form |
3082 (cond ((or orig-special-form-p orig-macro-p) | |
3083 ;; Special forms and macros will be advised into macros. | |
3084 ;; The trick is to construct an expansion for the advised | |
3085 ;; macro that does the correct thing when it gets eval'ed. | |
3086 ;; For macros we'll just use the expansion of the original | |
3087 ;; macro and return that. This way compiled advised macros | |
3088 ;; will be expanded into something useful. Note that after | |
3089 ;; advices have full control over whether they want to | |
3090 ;; evaluate the expansion (the value of `ad-return-value') | |
3091 ;; at macro expansion time or not. For special forms there | |
3092 ;; is no solution that interacts reasonably with the | |
3093 ;; compiler, hence we just evaluate the original at macro | |
3094 ;; expansion time and return the result. The moral of that | |
3095 ;; is that one should always deactivate advised special | |
3096 ;; forms before one byte-compiles a file. | |
37304
9ca19dfc32fb
(ad-make-advised-definition): Construct
Gerd Moellmann <gerd@gnu.org>
parents:
37056
diff
changeset
|
3097 `(,(if orig-macro-p 'macroexpand 'eval) |
9ca19dfc32fb
(ad-make-advised-definition): Construct
Gerd Moellmann <gerd@gnu.org>
parents:
37056
diff
changeset
|
3098 (cons ',origname |
9ca19dfc32fb
(ad-make-advised-definition): Construct
Gerd Moellmann <gerd@gnu.org>
parents:
37056
diff
changeset
|
3099 ,(ad-get-arguments advised-arglist 0)))) |
4110 | 3100 ((and orig-subr-p |
3101 orig-interactive-p | |
37304
9ca19dfc32fb
(ad-make-advised-definition): Construct
Gerd Moellmann <gerd@gnu.org>
parents:
37056
diff
changeset
|
3102 (not interactive-form) |
4110 | 3103 (not advised-interactive-form)) |
3104 ;; Check whether we were called interactively | |
3105 ;; in order to do proper prompting: | |
105371
0769a73f1d18
* image-mode.el (image-toggle-display):
Juanma Barranquero <lekktu@gmail.com>
parents:
105113
diff
changeset
|
3106 `(if (called-interactively-p 'any) |
37304
9ca19dfc32fb
(ad-make-advised-definition): Construct
Gerd Moellmann <gerd@gnu.org>
parents:
37056
diff
changeset
|
3107 (call-interactively ',origname) |
66398
df04170ba46b
(ad-make-advised-definition): Fix arg-order.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
65680
diff
changeset
|
3108 ,(ad-make-mapped-call advised-arglist |
df04170ba46b
(ad-make-advised-definition): Fix arg-order.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
65680
diff
changeset
|
3109 orig-arglist |
37304
9ca19dfc32fb
(ad-make-advised-definition): Construct
Gerd Moellmann <gerd@gnu.org>
parents:
37056
diff
changeset
|
3110 origname))) |
4110 | 3111 ;; And now for normal functions and non-interactive subrs |
3112 ;; (or subrs whose interactive behavior was advised): | |
3113 (t (ad-make-mapped-call | |
3114 advised-arglist orig-arglist origname))))) | |
3115 | |
3116 ;; Finally, build the sucker: | |
3117 (ad-assemble-advised-definition | |
3118 (cond (orig-macro-p 'macro) | |
3119 (orig-special-form-p 'special-form) | |
3120 (t 'function)) | |
3121 advised-arglist | |
3122 (ad-make-advised-definition-docstring function) | |
3123 interactive-form | |
3124 orig-form | |
3125 (ad-get-enabled-advices function 'before) | |
3126 (ad-get-enabled-advices function 'around) | |
3127 (ad-get-enabled-advices function 'after))))) | |
3128 | |
3129 (defun ad-assemble-advised-definition | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3130 (type args docstring interactive orig &optional befores arounds afters) |
4110 | 3131 |
26217 | 3132 "Assembles an original and its advices into an advised function. |
3133 It constructs a function or macro definition according to TYPE which has to | |
3134 be either `macro', `function' or `special-form'. ARGS is the argument list | |
3135 that has to be used, DOCSTRING if non-nil defines the documentation of the | |
3136 definition, INTERACTIVE if non-nil is the interactive form to be used, | |
3137 ORIG is a form that calls the body of the original unadvised function, | |
3138 and BEFORES, AROUNDS and AFTERS are the lists of advices with which ORIG | |
3139 should be modified. The assembled function will be returned." | |
4110 | 3140 |
3141 (let (before-forms around-form around-form-protected after-forms definition) | |
3142 (ad-dolist (advice befores) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3143 (cond ((and (ad-advice-protected advice) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3144 before-forms) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3145 (setq before-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3146 `((unwind-protect |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3147 ,(ad-prognify before-forms) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3148 ,@(ad-body-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3149 (ad-advice-definition advice)))))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3150 (t (setq before-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3151 (append before-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3152 (ad-body-forms (ad-advice-definition advice))))))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3153 |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3154 (setq around-form `(setq ad-return-value ,orig)) |
4110 | 3155 (ad-dolist (advice (reverse arounds)) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3156 ;; If any of the around advices is protected then we |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3157 ;; protect the complete around advice onion: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3158 (if (ad-advice-protected advice) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3159 (setq around-form-protected t)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3160 (setq around-form |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3161 (ad-substitute-tree |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3162 (function (lambda (form) (eq form 'ad-do-it))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3163 (function (lambda (form) around-form)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3164 (ad-prognify (ad-body-forms (ad-advice-definition advice)))))) |
4110 | 3165 |
3166 (setq after-forms | |
3167 (if (and around-form-protected before-forms) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3168 `((unwind-protect |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3169 ,(ad-prognify before-forms) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3170 ,around-form)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3171 (append before-forms (list around-form)))) |
4110 | 3172 (ad-dolist (advice afters) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3173 (cond ((and (ad-advice-protected advice) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3174 after-forms) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3175 (setq after-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3176 `((unwind-protect |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3177 ,(ad-prognify after-forms) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3178 ,@(ad-body-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3179 (ad-advice-definition advice)))))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3180 (t (setq after-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3181 (append after-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3182 (ad-body-forms (ad-advice-definition advice))))))) |
4110 | 3183 |
3184 (setq definition | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3185 `(,@(if (memq type '(macro special-form)) '(macro)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3186 lambda |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3187 ,args |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3188 ,@(if docstring (list docstring)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3189 ,@(if interactive (list interactive)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3190 (let (ad-return-value) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3191 ,@after-forms |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3192 ,(if (eq type 'special-form) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3193 '(list 'quote ad-return-value) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3194 'ad-return-value)))) |
4110 | 3195 |
3196 (ad-insert-argument-access-forms definition args))) | |
3197 | |
3198 ;; This is needed for activation/deactivation hooks: | |
3199 (defun ad-make-hook-form (function hook-name) | |
26217 | 3200 "Make hook-form from FUNCTION's advice bodies in class HOOK-NAME." |
4110 | 3201 (let ((hook-forms |
3202 (mapcar (function (lambda (advice) | |
3203 (ad-body-forms (ad-advice-definition advice)))) | |
3204 (ad-get-enabled-advices function hook-name)))) | |
3205 (if hook-forms | |
3206 (ad-prognify (apply 'append hook-forms))))) | |
3207 | |
3208 | |
3209 ;; @@ Caching: | |
3210 ;; =========== | |
3211 ;; Generating an advised definition of a function is moderately expensive, | |
3212 ;; hence, it makes sense to cache it so we can reuse it in appropriate | |
3213 ;; circumstances. Of course, it only makes sense to reuse a cached | |
3214 ;; definition if the current advice and function definition state is the | |
3215 ;; same as it was at the time when the cached definition was generated. | |
3216 ;; For that purpose we associate every cache with an id so we can verify | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3217 ;; if it is still valid at a certain point in time. This id mechanism |
4110 | 3218 ;; makes it possible to preactivate advised functions, write the compiled |
3219 ;; advised definitions to a file and reuse them during the actual | |
3220 ;; activation without having to risk that the resulting definition will be | |
3221 ;; incorrect, well, almost. | |
3222 ;; | |
3223 ;; A cache id is a list with six elements: | |
3224 ;; 1) the list of names of enabled before advices | |
3225 ;; 2) the list of names of enabled around advices | |
3226 ;; 3) the list of names of enabled after advices | |
3227 ;; 4) the type of the original function (macro, subr, etc.) | |
3228 ;; 5) the arglist of the original definition (or t if it was equal to the | |
3229 ;; arglist of the cached definition) | |
3230 ;; 6) t if the interactive form of the original definition was equal to the | |
3231 ;; interactive form of the cached definition | |
3232 ;; | |
3233 ;; Here's how a cache can get invalidated or be incorrect: | |
3234 ;; A) a piece of advice used in the cache gets redefined | |
3235 ;; B) the current list of enabled advices is different from the ones used | |
3236 ;; for the cache | |
3237 ;; C) the type of the original function changed, e.g., a function became a | |
3238 ;; macro, or a subr became a function | |
3239 ;; D) the arglist of the original function changed | |
3240 ;; E) the interactive form of the original function changed | |
3241 ;; F) a piece of advice used in the cache got redefined before the | |
3242 ;; defadvice with the cached definition got loaded: This is a PROBLEM! | |
3243 ;; | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3244 ;; Cases A and B are the normal ones. A is taken care of by `ad-add-advice' |
4110 | 3245 ;; which clears the cache in such a case, B is easily checked during |
3246 ;; verification at activation time. | |
3247 ;; | |
3248 ;; Cases C, D and E have to be considered if one is slightly paranoid, i.e., | |
3249 ;; if one considers the case that the original function could be different | |
3250 ;; from the one available at caching time (e.g., for forward advice of | |
3251 ;; functions that get redefined by some packages - such as `eval-region' gets | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3252 ;; redefined by edebug). All these cases can be easily checked during |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3253 ;; verification. Element 4 of the id lets one check case C, element 5 takes |
4110 | 3254 ;; care of case D (using t in the equality case saves some space, because the |
3255 ;; arglist can be recovered at validation time from the cached definition), | |
3256 ;; and element 6 takes care of case E which is only a problem if the original | |
3257 ;; was actually a function whose interactive form was not overridden by a | |
3258 ;; piece of advice. | |
3259 ;; | |
3260 ;; Case F is the only one which will lead to an incorrect advised function. | |
3261 ;; There is no way to avoid this without storing the complete advice definition | |
3262 ;; in the cache-id which is not feasible. | |
3263 ;; | |
3264 ;; The cache-id of a typical advised function with one piece of advice and | |
3265 ;; no arglist redefinition takes 7 conses which is a small price to pay for | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3266 ;; the added efficiency. The validation itself is also pretty cheap, certainly |
4110 | 3267 ;; a lot cheaper than reconstructing an advised definition. |
3268 | |
3269 (defmacro ad-get-cache-definition (function) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3270 `(car (ad-get-advice-info-field ,function 'cache))) |
4110 | 3271 |
3272 (defmacro ad-get-cache-id (function) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3273 `(cdr (ad-get-advice-info-field ,function 'cache))) |
4110 | 3274 |
3275 (defmacro ad-set-cache (function definition id) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3276 `(ad-set-advice-info-field |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3277 ,function 'cache (cons ,definition ,id))) |
4110 | 3278 |
3279 (defun ad-clear-cache (function) | |
3280 "Clears a previously cached advised definition of FUNCTION. | |
3281 Clear the cache if you want to force `ad-activate' to construct a new | |
3282 advised definition from scratch." | |
3283 (interactive | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3284 (list (ad-read-advised-function "Clear cached definition of"))) |
4110 | 3285 (ad-set-advice-info-field function 'cache nil)) |
3286 | |
3287 (defun ad-make-cache-id (function) | |
26217 | 3288 "Generate an identifying image of the current advices of FUNCTION." |
4110 | 3289 (let ((original-definition (ad-real-orig-definition function)) |
3290 (cached-definition (ad-get-cache-definition function))) | |
3291 (list (mapcar (function (lambda (advice) (ad-advice-name advice))) | |
3292 (ad-get-enabled-advices function 'before)) | |
3293 (mapcar (function (lambda (advice) (ad-advice-name advice))) | |
3294 (ad-get-enabled-advices function 'around)) | |
3295 (mapcar (function (lambda (advice) (ad-advice-name advice))) | |
3296 (ad-get-enabled-advices function 'after)) | |
3297 (ad-definition-type original-definition) | |
3298 (if (equal (ad-arglist original-definition function) | |
3299 (ad-arglist cached-definition)) | |
3300 t | |
3301 (ad-arglist original-definition function)) | |
3302 (if (eq (ad-definition-type original-definition) 'function) | |
82146
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3303 (equal (interactive-form original-definition) |
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3304 (interactive-form cached-definition)))))) |
4110 | 3305 |
3306 (defun ad-get-cache-class-id (function class) | |
26217 | 3307 "Return the part of FUNCTION's cache id that identifies CLASS." |
4110 | 3308 (let ((cache-id (ad-get-cache-id function))) |
3309 (if (eq class 'before) | |
3310 (car cache-id) | |
3311 (if (eq class 'around) | |
3312 (nth 1 cache-id) | |
3313 (nth 2 cache-id))))) | |
3314 | |
3315 (defun ad-verify-cache-class-id (cache-class-id advices) | |
3316 (ad-dolist (advice advices (null cache-class-id)) | |
3317 (if (ad-advice-enabled advice) | |
3318 (if (eq (car cache-class-id) (ad-advice-name advice)) | |
3319 (setq cache-class-id (cdr cache-class-id)) | |
3320 (ad-do-return nil))))) | |
3321 | |
3322 ;; There should be a way to monitor if and why a cache verification failed | |
3323 ;; in order to determine whether a certain preactivation could be used or | |
26217 | 3324 ;; not. Right now the only way to find out is to trace |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3325 ;; `ad-cache-id-verification-code'. The code it returns indicates where the |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3326 ;; verification failed. Tracing `ad-verify-cache-class-id' might provide |
4110 | 3327 ;; some additional useful information. |
3328 | |
3329 (defun ad-cache-id-verification-code (function) | |
3330 (let ((cache-id (ad-get-cache-id function)) | |
3331 (code 'before-advice-mismatch)) | |
3332 (and (ad-verify-cache-class-id | |
3333 (car cache-id) (ad-get-advice-info-field function 'before)) | |
3334 (setq code 'around-advice-mismatch) | |
3335 (ad-verify-cache-class-id | |
3336 (nth 1 cache-id) (ad-get-advice-info-field function 'around)) | |
3337 (setq code 'after-advice-mismatch) | |
3338 (ad-verify-cache-class-id | |
3339 (nth 2 cache-id) (ad-get-advice-info-field function 'after)) | |
3340 (setq code 'definition-type-mismatch) | |
3341 (let ((original-definition (ad-real-orig-definition function)) | |
3342 (cached-definition (ad-get-cache-definition function))) | |
3343 (and (eq (nth 3 cache-id) (ad-definition-type original-definition)) | |
3344 (setq code 'arglist-mismatch) | |
3345 (equal (if (eq (nth 4 cache-id) t) | |
3346 (ad-arglist original-definition function) | |
3347 (nth 4 cache-id) ) | |
3348 (ad-arglist cached-definition)) | |
3349 (setq code 'interactive-form-mismatch) | |
3350 (or (null (nth 5 cache-id)) | |
82146
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3351 (equal (interactive-form original-definition) |
5868631b2e87
(ad-interactive-p, ad-interactive-form): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
82140
diff
changeset
|
3352 (interactive-form cached-definition))) |
4110 | 3353 (setq code 'verified)))) |
3354 code)) | |
3355 | |
3356 (defun ad-verify-cache-id (function) | |
26217 | 3357 "True if FUNCTION's cache-id is compatible with its current advices." |
4110 | 3358 (eq (ad-cache-id-verification-code function) 'verified)) |
3359 | |
3360 | |
3361 ;; @@ Preactivation: | |
3362 ;; ================= | |
3363 ;; Preactivation can be used to generate compiled advised definitions | |
3364 ;; at compile time without having to give up the dynamic runtime flexibility | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3365 ;; of the advice mechanism. Preactivation is a special feature of `defadvice', |
4110 | 3366 ;; it involves the following steps: |
3367 ;; - remembering the function's current state (definition and advice-info) | |
3368 ;; - advising it with the defined piece of advice | |
3369 ;; - clearing its cache | |
3370 ;; - generating an interpreted advised definition by activating it, this will | |
3371 ;; make use of all its current active advice and its current definition | |
3372 ;; - saving the so generated cached definition and id | |
3373 ;; - resetting the function's advice and definition state to what it was | |
3374 ;; before the preactivation | |
3375 ;; - Returning the saved definition and its id to be used in the expansion of | |
3376 ;; `defadvice' to assign it as an initial cache, hence it will be compiled | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3377 ;; at time the `defadvice' gets compiled. |
4110 | 3378 ;; Naturally, for preactivation to be effective it has to be applied/compiled |
3379 ;; at the right time, i.e., when the current state of advices and function | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3380 ;; definition exactly reflects the state at activation time. Should that not |
4110 | 3381 ;; be the case, the precompiled definition will just be discarded and a new |
3382 ;; advised definition will be generated. | |
3383 | |
3384 (defun ad-preactivate-advice (function advice class position) | |
26217 | 3385 "Preactivate FUNCTION and returns the constructed cache." |
4110 | 3386 (let* ((function-defined-p (fboundp function)) |
3387 (old-definition | |
3388 (if function-defined-p | |
3389 (symbol-function function))) | |
3390 (old-advice-info (ad-copy-advice-info function)) | |
3391 (ad-advised-functions ad-advised-functions)) | |
3392 (unwind-protect | |
3393 (progn | |
3394 (ad-add-advice function advice class position) | |
3395 (ad-enable-advice function class (ad-advice-name advice)) | |
3396 (ad-clear-cache function) | |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3397 (ad-activate function -1) |
4110 | 3398 (if (and (ad-is-active function) |
3399 (ad-get-cache-definition function)) | |
3400 (list (ad-get-cache-definition function) | |
3401 (ad-get-cache-id function)))) | |
3402 (ad-set-advice-info function old-advice-info) | |
3403 ;; Don't `fset' function to nil if it was previously unbound: | |
3404 (if function-defined-p | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3405 (ad-safe-fset function old-definition) |
4110 | 3406 (fmakunbound function))))) |
3407 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3408 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3409 ;; @@ Freezing: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3410 ;; ============ |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3411 ;; Freezing transforms a `defadvice' into a redefining `defun/defmacro' |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3412 ;; for the advised function without keeping any advice information. This |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3413 ;; feature was jwz's idea: It generates a dumpable function definition |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3414 ;; whose documentation can be written to the DOC file, and the generated |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3415 ;; code does not need any Advice runtime support. Of course, frozen advices |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3416 ;; cannot be undone. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3417 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3418 ;; Freezing only considers the advice of the particular `defadvice', other |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3419 ;; already existing advices for the same function will be ignored. To ensure |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3420 ;; proper interaction when an already advised function gets redefined with |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3421 ;; a frozen advice, frozen advices always use the actual original definition |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3422 ;; of the function, i.e., they are always at the core of the onion. E.g., if |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3423 ;; an already advised function gets redefined with a frozen advice and then |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3424 ;; unadvised, the frozen advice remains as the new definition of the function. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3425 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3426 ;; While multiple freeze advices for a single function or freeze-advising |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3427 ;; of an already advised function are possible, they are better avoided, |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3428 ;; because definition/compile/load ordering is relevant, and it becomes |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3429 ;; incomprehensible pretty quickly. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3430 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3431 (defun ad-make-freeze-definition (function advice class position) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3432 (if (not (ad-has-proper-definition function)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3433 (error |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3434 "ad-make-freeze-definition: `%s' is not yet defined" |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3435 function)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3436 (let* ((name (ad-advice-name advice)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3437 ;; With a unique origname we can have multiple freeze advices |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3438 ;; for the same function, each overloading the previous one: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3439 (unique-origname |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3440 (intern (format "%s-%s-%s" (ad-make-origname function) class name))) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3441 (orig-definition |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3442 ;; If FUNCTION is already advised, we'll use its current origdef |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3443 ;; as the original definition of the frozen advice: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3444 (or (ad-get-orig-definition function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3445 (symbol-function function))) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3446 (old-advice-info |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3447 (if (ad-is-advised function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3448 (ad-copy-advice-info function))) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3449 (real-docstring-fn |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3450 (symbol-function 'ad-make-advised-definition-docstring)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3451 (real-origname-fn |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3452 (symbol-function 'ad-make-origname)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3453 (frozen-definition |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3454 (unwind-protect |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3455 (progn |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3456 ;; Make sure we construct a proper docstring: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3457 (ad-safe-fset 'ad-make-advised-definition-docstring |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3458 'ad-make-freeze-docstring) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3459 ;; Make sure `unique-origname' is used as the origname: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3460 (ad-safe-fset 'ad-make-origname (lambda (x) unique-origname)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3461 ;; No we reset all current advice information to nil and |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3462 ;; generate an advised definition that's solely determined |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3463 ;; by ADVICE and the current origdef of FUNCTION: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3464 (ad-set-advice-info function nil) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3465 (ad-add-advice function advice class position) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3466 ;; The following will provide proper real docstrings as |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3467 ;; well as a definition that will make the compiler happy: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3468 (ad-set-orig-definition function orig-definition) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3469 (ad-make-advised-definition function)) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3470 ;; Restore the old advice state: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3471 (ad-set-advice-info function old-advice-info) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3472 ;; Restore functions: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3473 (ad-safe-fset |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3474 'ad-make-advised-definition-docstring real-docstring-fn) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3475 (ad-safe-fset 'ad-make-origname real-origname-fn)))) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3476 (if frozen-definition |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3477 (let* ((macro-p (ad-macro-p frozen-definition)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3478 (body (cdr (if macro-p |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3479 (ad-lambdafy frozen-definition) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3480 frozen-definition)))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3481 `(progn |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3482 (if (not (fboundp ',unique-origname)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3483 (fset ',unique-origname |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3484 ;; avoid infinite recursion in case the function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3485 ;; we want to freeze is already advised: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3486 (or (ad-get-orig-definition ',function) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3487 (symbol-function ',function)))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3488 (,(if macro-p 'defmacro 'defun) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3489 ,function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3490 ,@body)))))) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3491 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3492 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3493 ;; @@ Activation and definition handling: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3494 ;; ====================================== |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3495 |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3496 (defun ad-should-compile (function compile) |
26217 | 3497 "Return non-nil if the advised FUNCTION should be compiled. |
3498 If COMPILE is non-nil and not a negative number then it returns t. | |
3499 If COMPILE is a negative number then it returns nil. | |
3500 If COMPILE is nil then the result depends on the value of | |
3501 `ad-default-compilation-action' (which see)." | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3502 (if (integerp compile) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3503 (>= compile 0) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3504 (if compile |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3505 compile |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3506 (cond ((eq ad-default-compilation-action 'never) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3507 nil) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3508 ((eq ad-default-compilation-action 'always) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3509 t) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3510 ((eq ad-default-compilation-action 'like-original) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3511 (or (ad-subr-p (ad-get-orig-definition function)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3512 (ad-compiled-p (ad-get-orig-definition function)))) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3513 ;; everything else means `maybe': |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3514 (t (featurep 'byte-compile)))))) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3515 |
4110 | 3516 (defun ad-activate-advised-definition (function compile) |
26217 | 3517 "Redefine FUNCTION with its advised definition from cache or scratch. |
3518 The resulting FUNCTION will be compiled if `ad-should-compile' returns t. | |
3519 The current definition and its cache-id will be put into the cache." | |
4110 | 3520 (let ((verified-cached-definition |
3521 (if (ad-verify-cache-id function) | |
3522 (ad-get-cache-definition function)))) | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3523 (ad-safe-fset function |
4110 | 3524 (or verified-cached-definition |
3525 (ad-make-advised-definition function))) | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3526 (if (ad-should-compile function compile) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3527 (ad-compile-function function)) |
4110 | 3528 (if verified-cached-definition |
3529 (if (not (eq verified-cached-definition (symbol-function function))) | |
3530 ;; we must have compiled, cache the compiled definition: | |
3531 (ad-set-cache | |
3532 function (symbol-function function) (ad-get-cache-id function))) | |
3533 ;; We created a new advised definition, cache it with a proper id: | |
3534 (ad-clear-cache function) | |
3535 ;; ad-make-cache-id needs the new cached definition: | |
3536 (ad-set-cache function (symbol-function function) nil) | |
3537 (ad-set-cache | |
3538 function (symbol-function function) (ad-make-cache-id function))))) | |
3539 | |
3540 (defun ad-handle-definition (function) | |
26622 | 3541 "Handle re/definition of an advised FUNCTION during de/activation. |
4110 | 3542 If FUNCTION does not have an original definition associated with it and |
3543 the current definition is usable, then it will be stored as FUNCTION's | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3544 original definition. If no current definition is available (even in the |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3545 case of undefinition) nothing will be done. In the case of redefinition |
4110 | 3546 the action taken depends on the value of `ad-redefinition-action' (which |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3547 see). Redefinition occurs when FUNCTION already has an original definition |
4110 | 3548 associated with it but got redefined with a new definition and then |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3549 de/activated. If you do not like the current redefinition action change |
4110 | 3550 the value of `ad-redefinition-action' and de/activate again." |
3551 (let ((original-definition (ad-get-orig-definition function)) | |
3552 (current-definition (if (ad-real-definition function) | |
3553 (symbol-function function)))) | |
3554 (if original-definition | |
3555 (if current-definition | |
3556 (if (and (not (eq current-definition original-definition)) | |
3557 ;; Redefinition with an advised definition from a | |
3558 ;; different function won't count as such: | |
3559 (not (ad-advised-definition-p current-definition))) | |
3560 ;; we have a redefinition: | |
3561 (if (not (memq ad-redefinition-action '(accept discard warn))) | |
3562 (error "ad-handle-definition (see its doc): `%s' %s" | |
22061
eed26995bfad
(ad-handle-definition, defadvice): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
21365
diff
changeset
|
3563 function "invalidly redefined") |
4110 | 3564 (if (eq ad-redefinition-action 'discard) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3565 (ad-safe-fset function original-definition) |
4110 | 3566 (ad-set-orig-definition function current-definition) |
3567 (if (eq ad-redefinition-action 'warn) | |
3568 (message "ad-handle-definition: `%s' got redefined" | |
3569 function)))) | |
3570 ;; either advised def or correct original is in place: | |
3571 nil) | |
3572 ;; we have an undefinition, ignore it: | |
3573 nil) | |
3574 (if current-definition | |
3575 ;; we have a first definition, save it as original: | |
3576 (ad-set-orig-definition function current-definition) | |
3577 ;; we don't have anything noteworthy: | |
3578 nil)))) | |
3579 | |
3580 | |
3581 ;; @@ The top-level advice interface: | |
3582 ;; ================================== | |
3583 | |
70899
b920ab84fba8
(ad-enable-advice, ad-activate, ad-disable-advice): Add autoloads.
Richard M. Stallman <rms@gnu.org>
parents:
68648
diff
changeset
|
3584 ;;;###autoload |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3585 (defun ad-activate (function &optional compile) |
26622 | 3586 "Activate all the advice information of an advised FUNCTION. |
4110 | 3587 If FUNCTION has a proper original definition then an advised |
3588 definition will be generated from FUNCTION's advice info and the | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3589 definition of FUNCTION will be replaced with it. If a previously |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3590 cached advised definition was available, it will be used. |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3591 The optional COMPILE argument determines whether the resulting function |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3592 or a compilable cached definition will be compiled. If it is negative |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3593 no compilation will be performed, if it is positive or otherwise non-nil |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3594 the resulting function will be compiled, if it is nil the behavior depends |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3595 on the value of `ad-default-compilation-action' (which see). |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3596 Activation of an advised function that has an advice info but no actual |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3597 pieces of advice is equivalent to a call to `ad-unadvise'. Activation of |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3598 an advised function that has actual pieces of advice but none of them are |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3599 enabled is equivalent to a call to `ad-deactivate'. The current advised |
4110 | 3600 definition will always be cached for later usage." |
3601 (interactive | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3602 (list (ad-read-advised-function "Activate advice of") |
4110 | 3603 current-prefix-arg)) |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3604 (if ad-activate-on-top-level |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3605 ;; avoid recursive calls to `ad-activate': |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3606 (ad-with-auto-activation-disabled |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3607 (if (not (ad-is-advised function)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3608 (error "ad-activate: `%s' is not advised" function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3609 (ad-handle-definition function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3610 ;; Just return for forward advised and not yet defined functions: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3611 (if (ad-get-orig-definition function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3612 (if (not (ad-has-any-advice function)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3613 (ad-unadvise function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3614 ;; Otherwise activate the advice: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3615 (cond ((ad-has-redefining-advice function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3616 (ad-activate-advised-definition function compile) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3617 (ad-set-advice-info-field function 'active t) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3618 (eval (ad-make-hook-form function 'activation)) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3619 function) |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3620 ;; Here we are if we have all disabled advices: |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3621 (t (ad-deactivate function))))))))) |
4110 | 3622 |
26247
f96b2bc4ef08
(ad-activate-on): Make it an alias for ad-activate.
Gerd Moellmann <gerd@gnu.org>
parents:
26217
diff
changeset
|
3623 (defalias 'ad-activate-on 'ad-activate) |
f96b2bc4ef08
(ad-activate-on): Make it an alias for ad-activate.
Gerd Moellmann <gerd@gnu.org>
parents:
26217
diff
changeset
|
3624 |
4110 | 3625 (defun ad-deactivate (function) |
26622 | 3626 "Deactivate the advice of an actively advised FUNCTION. |
4110 | 3627 If FUNCTION has a proper original definition, then the current |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3628 definition of FUNCTION will be replaced with it. All the advice |
4110 | 3629 information will still be available so it can be activated again with |
3630 a call to `ad-activate'." | |
3631 (interactive | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3632 (list (ad-read-advised-function "Deactivate advice of" 'ad-is-active))) |
4110 | 3633 (if (not (ad-is-advised function)) |
3634 (error "ad-deactivate: `%s' is not advised" function) | |
3635 (cond ((ad-is-active function) | |
3636 (ad-handle-definition function) | |
3637 (if (not (ad-get-orig-definition function)) | |
3638 (error "ad-deactivate: `%s' has no original definition" | |
3639 function) | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3640 (ad-safe-fset function (ad-get-orig-definition function)) |
4110 | 3641 (ad-set-advice-info-field function 'active nil) |
3642 (eval (ad-make-hook-form function 'deactivation)) | |
3643 function))))) | |
3644 | |
3645 (defun ad-update (function &optional compile) | |
3646 "Update the advised definition of FUNCTION if its advice is active. | |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3647 See `ad-activate' for documentation on the optional COMPILE argument." |
4110 | 3648 (interactive |
3649 (list (ad-read-advised-function | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3650 "Update advised definition of" 'ad-is-active))) |
4110 | 3651 (if (ad-is-active function) |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3652 (ad-activate function compile))) |
4110 | 3653 |
3654 (defun ad-unadvise (function) | |
26622 | 3655 "Deactivate FUNCTION and then remove all its advice information. |
4110 | 3656 If FUNCTION was not advised this will be a noop." |
3657 (interactive | |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3658 (list (ad-read-advised-function "Unadvise function"))) |
4110 | 3659 (cond ((ad-is-advised function) |
3660 (if (ad-is-active function) | |
3661 (ad-deactivate function)) | |
3662 (ad-clear-orig-definition function) | |
3663 (ad-set-advice-info function nil) | |
3664 (ad-pop-advised-function function)))) | |
3665 | |
3666 (defun ad-recover (function) | |
26622 | 3667 "Try to recover FUNCTION's original definition, and unadvise it. |
3668 This is more low-level than `ad-unadvise' in that it does not do | |
26627 | 3669 deactivation, which might run hooks and get into other trouble. |
4110 | 3670 Use in emergencies." |
3671 ;; Use more primitive interactive behavior here: Accept any symbol that's | |
3672 ;; currently defined in obarray, not necessarily with a function definition: | |
3673 (interactive | |
3674 (list (intern | |
3675 (completing-read "Recover advised function: " obarray nil t)))) | |
3676 (cond ((ad-is-advised function) | |
3677 (cond ((ad-get-orig-definition function) | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3678 (ad-safe-fset function (ad-get-orig-definition function)) |
4110 | 3679 (ad-clear-orig-definition function))) |
3680 (ad-set-advice-info function nil) | |
3681 (ad-pop-advised-function function)))) | |
3682 | |
3683 (defun ad-activate-regexp (regexp &optional compile) | |
26622 | 3684 "Activate functions with an advice name containing a REGEXP match. |
3685 This activates the advice for each function | |
3686 that has at least one piece of advice whose name includes a match for REGEXP. | |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3687 See `ad-activate' for documentation on the optional COMPILE argument." |
4110 | 3688 (interactive |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3689 (list (ad-read-regexp "Activate via advice regexp") |
4110 | 3690 current-prefix-arg)) |
3691 (ad-do-advised-functions (function) | |
3692 (if (ad-find-some-advice function 'any regexp) | |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3693 (ad-activate function compile)))) |
4110 | 3694 |
3695 (defun ad-deactivate-regexp (regexp) | |
26622 | 3696 "Deactivate functions with an advice name containing REGEXP match. |
3697 This deactivates the advice for each function | |
3698 that has at least one piece of advice whose name includes a match for REGEXP." | |
4110 | 3699 (interactive |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3700 (list (ad-read-regexp "Deactivate via advice regexp"))) |
4110 | 3701 (ad-do-advised-functions (function) |
3702 (if (ad-find-some-advice function 'any regexp) | |
3703 (ad-deactivate function)))) | |
3704 | |
3705 (defun ad-update-regexp (regexp &optional compile) | |
26217 | 3706 "Update functions with an advice name containing a REGEXP match. |
26622 | 3707 This reactivates the advice for each function |
3708 that has at least one piece of advice whose name includes a match for REGEXP. | |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3709 See `ad-activate' for documentation on the optional COMPILE argument." |
4110 | 3710 (interactive |
65680
ed770a0a7846
2005-09-24 Emilio C. Lopes <eclig@gmx.net>
Romain Francoise <romain@orebokech.com>
parents:
64751
diff
changeset
|
3711 (list (ad-read-regexp "Update via advice regexp") |
4110 | 3712 current-prefix-arg)) |
3713 (ad-do-advised-functions (function) | |
3714 (if (ad-find-some-advice function 'any regexp) | |
3715 (ad-update function compile)))) | |
3716 | |
3717 (defun ad-activate-all (&optional compile) | |
26622 | 3718 "Activate all currently advised functions. |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3719 See `ad-activate' for documentation on the optional COMPILE argument." |
4110 | 3720 (interactive "P") |
3721 (ad-do-advised-functions (function) | |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3722 (ad-activate function compile))) |
4110 | 3723 |
3724 (defun ad-deactivate-all () | |
26622 | 3725 "Deactivate all currently advised functions." |
4110 | 3726 (interactive) |
3727 (ad-do-advised-functions (function) | |
3728 (ad-deactivate function))) | |
3729 | |
3730 (defun ad-update-all (&optional compile) | |
26217 | 3731 "Update all currently advised functions. |
3732 With prefix argument, COMPILE resulting advised definitions." | |
4110 | 3733 (interactive "P") |
3734 (ad-do-advised-functions (function) | |
3735 (ad-update function compile))) | |
3736 | |
3737 (defun ad-unadvise-all () | |
26622 | 3738 "Unadvise all currently advised functions." |
4110 | 3739 (interactive) |
3740 (ad-do-advised-functions (function) | |
3741 (ad-unadvise function))) | |
3742 | |
3743 (defun ad-recover-all () | |
26622 | 3744 "Recover all currently advised functions. Use in emergencies. |
3745 To recover a function means to try to find its original (pre-advice) | |
3746 definition, and delete all advice. | |
3747 This is more low-level than `ad-unadvise' in that it does not do | |
3748 deactivation, which might run hooks and get into other trouble." | |
4110 | 3749 (interactive) |
3750 (ad-do-advised-functions (function) | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3751 (condition-case nil |
4110 | 3752 (ad-recover function) |
3753 (error nil)))) | |
3754 | |
3755 | |
60923
08f8c7042636
* emacs-lisp/advice.el: Replace `legal' with `valid'.
Werner LEMBERG <wl@gnu.org>
parents:
57879
diff
changeset
|
3756 ;; Completion alist of valid `defadvice' flags |
4110 | 3757 (defvar ad-defadvice-flags |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3758 '(("protect") ("disable") ("activate") |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3759 ("compile") ("preactivate") ("freeze"))) |
4110 | 3760 |
3761 ;;;###autoload | |
3762 (defmacro defadvice (function args &rest body) | |
26217 | 3763 "Define a piece of advice for FUNCTION (a symbol). |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3764 The syntax of `defadvice' is as follows: |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3765 |
26217 | 3766 \(defadvice FUNCTION (CLASS NAME [POSITION] [ARGLIST] FLAG...) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3767 [DOCSTRING] [INTERACTIVE-FORM] |
78150
3fcbdd7761c3
(defadvice): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75346
diff
changeset
|
3768 BODY...) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3769 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3770 FUNCTION ::= Name of the function to be advised. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3771 CLASS ::= `before' | `around' | `after' | `activation' | `deactivation'. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3772 NAME ::= Non-nil symbol that names this piece of advice. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3773 POSITION ::= `first' | `last' | NUMBER. Optional, defaults to `first', |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3774 see also `ad-add-advice'. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3775 ARGLIST ::= An optional argument list to be used for the advised function |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3776 instead of the argument list of the original. The first one found in |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3777 before/around/after-advices will be used. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3778 FLAG ::= `protect'|`disable'|`activate'|`compile'|`preactivate'|`freeze'. |
4110 | 3779 All flags can be specified with unambiguous initial substrings. |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3780 DOCSTRING ::= Optional documentation for this piece of advice. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3781 INTERACTIVE-FORM ::= Optional interactive form to be used for the advised |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3782 function. The first one found in before/around/after-advices will be used. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3783 BODY ::= Any s-expression. |
4110 | 3784 |
3785 Semantics of the various flags: | |
3786 `protect': The piece of advice will be protected against non-local exits in | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3787 any code that precedes it. If any around-advice of a function is protected |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3788 then automatically all around-advices will be protected (the complete onion). |
4110 | 3789 |
3790 `activate': All advice of FUNCTION will be activated immediately if | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3791 FUNCTION has been properly defined prior to this application of `defadvice'. |
4110 | 3792 |
3793 `compile': In conjunction with `activate' specifies that the resulting | |
3794 advised function should be compiled. | |
3795 | |
26217 | 3796 `disable': The defined advice will be disabled, hence, it will not be used |
4110 | 3797 during activation until somebody enables it. |
3798 | |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3799 `preactivate': Preactivates the advised FUNCTION at macro-expansion/compile |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3800 time. This generates a compiled advised definition according to the current |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3801 advice state that will be used during activation if appropriate. Only use |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3802 this if the `defadvice' gets actually compiled. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3803 |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3804 `freeze': Expands the `defadvice' into a redefining `defun/defmacro' according |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3805 to this particular single advice. No other advice information will be saved. |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3806 Frozen advices cannot be undone, they behave like a hard redefinition of |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3807 the advised function. `freeze' implies `activate' and `preactivate'. The |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3808 documentation of the advised function can be dumped onto the `DOC' file |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3809 during preloading. |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3810 |
80328
349f51baa515
(defadvice): Add usage pattern.
Richard M. Stallman <rms@gnu.org>
parents:
79704
diff
changeset
|
3811 See Info node `(elisp)Advising Functions' for comprehensive documentation. |
349f51baa515
(defadvice): Add usage pattern.
Richard M. Stallman <rms@gnu.org>
parents:
79704
diff
changeset
|
3812 usage: (defadvice FUNCTION (CLASS NAME [POSITION] [ARGLIST] FLAG...) |
349f51baa515
(defadvice): Add usage pattern.
Richard M. Stallman <rms@gnu.org>
parents:
79704
diff
changeset
|
3813 [DOCSTRING] [INTERACTIVE-FORM] |
349f51baa515
(defadvice): Add usage pattern.
Richard M. Stallman <rms@gnu.org>
parents:
79704
diff
changeset
|
3814 BODY...)" |
66398
df04170ba46b
(ad-make-advised-definition): Fix arg-order.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
65680
diff
changeset
|
3815 (declare (doc-string 3)) |
4110 | 3816 (if (not (ad-name-p function)) |
22061
eed26995bfad
(ad-handle-definition, defadvice): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
21365
diff
changeset
|
3817 (error "defadvice: Invalid function name: %s" function)) |
4110 | 3818 (let* ((class (car args)) |
3819 (name (if (not (ad-class-p class)) | |
22061
eed26995bfad
(ad-handle-definition, defadvice): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
21365
diff
changeset
|
3820 (error "defadvice: Invalid advice class: %s" class) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3821 (nth 1 args))) |
4110 | 3822 (position (if (not (ad-name-p name)) |
22061
eed26995bfad
(ad-handle-definition, defadvice): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
21365
diff
changeset
|
3823 (error "defadvice: Invalid advice name: %s" name) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3824 (setq args (nthcdr 2 args)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3825 (if (ad-position-p (car args)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3826 (prog1 (car args) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3827 (setq args (cdr args)))))) |
4110 | 3828 (arglist (if (listp (car args)) |
3829 (prog1 (car args) | |
3830 (setq args (cdr args))))) | |
3831 (flags | |
3832 (mapcar | |
3833 (function | |
3834 (lambda (flag) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3835 (let ((completion |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3836 (try-completion (symbol-name flag) ad-defadvice-flags))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3837 (cond ((eq completion t) flag) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3838 ((assoc completion ad-defadvice-flags) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3839 (intern completion)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3840 (t (error "defadvice: Invalid or ambiguous flag: %s" |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3841 flag)))))) |
4110 | 3842 args)) |
3843 (advice (ad-make-advice | |
3844 name (memq 'protect flags) | |
3845 (not (memq 'disable flags)) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3846 `(advice lambda ,arglist ,@body))) |
4110 | 3847 (preactivation (if (memq 'preactivate flags) |
3848 (ad-preactivate-advice | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3849 function advice class position)))) |
4110 | 3850 ;; Now for the things to be done at evaluation time: |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3851 (if (memq 'freeze flags) |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3852 ;; jwz's idea: Freeze the advised definition into a dumpable |
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3853 ;; defun/defmacro whose docs can be written to the DOC file: |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3854 (ad-make-freeze-definition function advice class position) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3855 ;; the normal case: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3856 `(progn |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3857 (ad-add-advice ',function ',advice ',class ',position) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3858 ,@(if preactivation |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3859 `((ad-set-cache |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3860 ',function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3861 ;; the function will get compiled: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3862 ,(cond ((ad-macro-p (car preactivation)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3863 `(ad-macrofy |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3864 (function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3865 ,(ad-lambdafy |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3866 (car preactivation))))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3867 (t `(function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3868 ,(car preactivation)))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3869 ',(car (cdr preactivation))))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3870 ,@(if (memq 'activate flags) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3871 `((ad-activate ',function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3872 ,(if (memq 'compile flags) t)))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3873 ',function)))) |
4110 | 3874 |
3875 | |
3876 ;; @@ Tools: | |
3877 ;; ========= | |
3878 | |
3879 (defmacro ad-with-originals (functions &rest body) | |
26217 | 3880 "Binds FUNCTIONS to their original definitions and execute BODY. |
4110 | 3881 For any members of FUNCTIONS that are not currently advised the rebinding will |
6038
2f1deaa86ee2
Removed all support for Emacs-18:
Richard M. Stallman <rms@gnu.org>
parents:
5746
diff
changeset
|
3882 be a noop. Any modifications done to the definitions of FUNCTIONS will be |
4110 | 3883 undone on exit of this macro." |
3884 (let* ((index -1) | |
3885 ;; Make let-variables to store current definitions: | |
3886 (current-bindings | |
3887 (mapcar (function | |
3888 (lambda (function) | |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3889 (setq index (1+ index)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3890 (list (intern (format "ad-oRiGdEf-%d" index)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3891 `(symbol-function ',function)))) |
4110 | 3892 functions))) |
41608
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3893 `(let ,current-bindings |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3894 (unwind-protect |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3895 (progn |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3896 ,@(progn |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3897 ;; Make forms to redefine functions to their |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3898 ;; original definitions if they are advised: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3899 (setq index -1) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3900 (mapcar |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3901 (function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3902 (lambda (function) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3903 (setq index (1+ index)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3904 `(ad-safe-fset |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3905 ',function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3906 (or (ad-get-orig-definition ',function) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3907 ,(car (nth index current-bindings)))))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3908 functions)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3909 ,@body) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3910 ,@(progn |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3911 ;; Make forms to back-define functions to the definitions |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3912 ;; they had outside this macro call: |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3913 (setq index -1) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3914 (mapcar |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3915 (function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3916 (lambda (function) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3917 (setq index (1+ index)) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3918 `(ad-safe-fset |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3919 ',function |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3920 ,(car (nth index current-bindings))))) |
45db352a0971
Converted backquote to the new style.
Sam Steingold <sds@gnu.org>
parents:
38336
diff
changeset
|
3921 functions)))))) |
4110 | 3922 |
3923 (if (not (get 'ad-with-originals 'lisp-indent-hook)) | |
3924 (put 'ad-with-originals 'lisp-indent-hook 1)) | |
3925 | |
3926 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3927 ;; @@ Advising `documentation': |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3928 ;; ============================ |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3929 ;; Use the advice mechanism to advise `documentation' to make it |
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3930 ;; generate proper documentation strings for advised definitions: |
4110 | 3931 |
45019
c726a3eeeb0f
(documentation): Add ad-define-subr-args call.
Richard M. Stallman <rms@gnu.org>
parents:
41936
diff
changeset
|
3932 ;; This makes sure we get the right arglist for `documentation' |
c726a3eeeb0f
(documentation): Add ad-define-subr-args call.
Richard M. Stallman <rms@gnu.org>
parents:
41936
diff
changeset
|
3933 ;; during bootstrapping. |
c726a3eeeb0f
(documentation): Add ad-define-subr-args call.
Richard M. Stallman <rms@gnu.org>
parents:
41936
diff
changeset
|
3934 (ad-define-subr-args 'documentation '(function &optional raw)) |
c726a3eeeb0f
(documentation): Add ad-define-subr-args call.
Richard M. Stallman <rms@gnu.org>
parents:
41936
diff
changeset
|
3935 |
4110 | 3936 ;; @@ Starting, stopping and recovering from the advice package magic: |
3937 ;; =================================================================== | |
3938 | |
3939 (defun ad-start-advice () | |
26217 | 3940 "Start the automatic advice handling magic." |
4110 | 3941 (interactive) |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3942 ;; Advising `ad-activate-internal' means death!! |
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3943 (ad-set-advice-info 'ad-activate-internal nil) |
85302
bc8b741390b0
(documentation): Advice deleted. Doc for advised functions
Richard M. Stallman <rms@gnu.org>
parents:
83896
diff
changeset
|
3944 (ad-safe-fset 'ad-activate-internal 'ad-activate)) |
4110 | 3945 |
3946 (defun ad-stop-advice () | |
26622 | 3947 "Stop the automatic advice handling magic. |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3948 You should only need this in case of Advice-related emergencies." |
4110 | 3949 (interactive) |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3950 ;; Advising `ad-activate-internal' means death!! |
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3951 (ad-set-advice-info 'ad-activate-internal nil) |
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3952 (ad-safe-fset 'ad-activate-internal 'ad-activate-internal-off)) |
4110 | 3953 |
3954 (defun ad-recover-normality () | |
26217 | 3955 "Undo all advice related redefinitions and unadvises everything. |
4110 | 3956 Use only in REAL emergencies." |
3957 (interactive) | |
26206
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3958 ;; Advising `ad-activate-internal' means death!! |
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3959 (ad-set-advice-info 'ad-activate-internal nil) |
3d9818475597
(ad-activate-internal): Renamed from
Gerd Moellmann <gerd@gnu.org>
parents:
25260
diff
changeset
|
3960 (ad-safe-fset 'ad-activate-internal 'ad-activate-internal-off) |
4110 | 3961 (ad-recover-all) |
3962 (setq ad-advised-functions nil)) | |
3963 | |
8445
81f7b5d9b990
New handling of automatic advice activation that
Richard M. Stallman <rms@gnu.org>
parents:
6082
diff
changeset
|
3964 (ad-start-advice) |
4110 | 3965 |
3966 (provide 'advice) | |
3967 | |
66398
df04170ba46b
(ad-make-advised-definition): Fix arg-order.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
65680
diff
changeset
|
3968 ;; arch-tag: 29f8c9a1-8c88-471f-95d7-e28541c6b7c0 |
4110 | 3969 ;;; advice.el ends here |