Commit | Line | Data |
---|---|---|
231d8498 SM |
1 | ;;; nadvice.el --- Light-weight advice primitives for Elisp functions -*- lexical-binding: t -*- |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2012-2014 Free Software Foundation, Inc. |
231d8498 SM |
4 | |
5 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> | |
6 | ;; Keywords: extensions, lisp, tools | |
7 | ;; Package: emacs | |
8 | ||
9 | ;; This program is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation, either version 3 of the License, or | |
12 | ;; (at your option) any later version. | |
13 | ||
14 | ;; This program is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;; This package lets you add behavior (which we call "piece of advice") to | |
25 | ;; existing functions, like the old `advice.el' package, but with much fewer | |
8725b746 | 26 | ;; bells and whistles. It comes in 2 parts: |
231d8498 SM |
27 | ;; |
28 | ;; - The first part lets you add/remove functions, similarly to | |
29 | ;; add/remove-hook, from any "place" (i.e. as accepted by `setf') that | |
30 | ;; holds a function. | |
31 | ;; This part provides mainly 2 macros: `add-function' and `remove-function'. | |
32 | ;; | |
a77b8d5e | 33 | ;; - The second part provides `advice-add' and `advice-remove' which are |
231d8498 SM |
34 | ;; refined version of the previous macros specially tailored for the case |
35 | ;; where the place that we want to modify is a `symbol-function'. | |
36 | ||
37 | ;;; Code: | |
38 | ||
39 | ;;;; Lightweight advice/hook | |
40 | (defvar advice--where-alist | |
41 | '((:around "\300\301\302\003#\207" 5) | |
42 | (:before "\300\301\002\"\210\300\302\002\"\207" 4) | |
43 | (:after "\300\302\002\"\300\301\003\"\210\207" 5) | |
bcd7a0a4 | 44 | (:override "\300\301\ 2\"\207" 4) |
231d8498 SM |
45 | (:after-until "\300\302\002\"\206\013\000\300\301\002\"\207" 4) |
46 | (:after-while "\300\302\002\"\205\013\000\300\301\002\"\207" 4) | |
47 | (:before-until "\300\301\002\"\206\013\000\300\302\002\"\207" 4) | |
d36ed1c8 SM |
48 | (:before-while "\300\301\002\"\205\013\000\300\302\002\"\207" 4) |
49 | (:filter-args "\300\302\301\ 3!\"\207" 5) | |
50 | (:filter-return "\301\300\302\ 3\"!\207" 5)) | |
231d8498 SM |
51 | "List of descriptions of how to add a function. |
52 | Each element has the form (WHERE BYTECODE STACK) where: | |
53 | WHERE is a keyword indicating where the function is added. | |
54 | BYTECODE is the corresponding byte-code that will be used. | |
55 | STACK is the amount of stack space needed by the byte-code.") | |
56 | ||
57 | (defvar advice--bytecodes (mapcar #'cadr advice--where-alist)) | |
58 | ||
59 | (defun advice--p (object) | |
60 | (and (byte-code-function-p object) | |
61 | (eq 128 (aref object 0)) | |
62 | (memq (length object) '(5 6)) | |
63 | (memq (aref object 1) advice--bytecodes) | |
64 | (eq #'apply (aref (aref object 2) 0)))) | |
65 | ||
66 | (defsubst advice--car (f) (aref (aref f 2) 1)) | |
67 | (defsubst advice--cdr (f) (aref (aref f 2) 2)) | |
68 | (defsubst advice--props (f) (aref (aref f 2) 3)) | |
69 | ||
70e5a261 SM |
70 | (defun advice--cd*r (f) |
71 | (while (advice--p f) | |
72 | (setq f (advice--cdr f))) | |
73 | f) | |
74 | ||
0d53f628 CY |
75 | (defun advice--make-docstring (function) |
76 | "Build the raw docstring for FUNCTION, presumably advised." | |
231d8498 SM |
77 | (let ((flist (indirect-function function)) |
78 | (docstring nil)) | |
79 | (if (eq 'macro (car-safe flist)) (setq flist (cdr flist))) | |
80 | (while (advice--p flist) | |
81 | (let ((bytecode (aref flist 1)) | |
82 | (where nil)) | |
83 | (dolist (elem advice--where-alist) | |
84 | (if (eq bytecode (cadr elem)) (setq where (car elem)))) | |
85 | (setq docstring | |
86 | (concat | |
87 | docstring | |
88 | (propertize (format "%s advice: " where) | |
89 | 'face 'warning) | |
90 | (let ((fun (advice--car flist))) | |
91 | (if (symbolp fun) (format "`%S'" fun) | |
92 | (let* ((name (cdr (assq 'name (advice--props flist)))) | |
93 | (doc (documentation fun t)) | |
94 | (usage (help-split-fundoc doc function))) | |
95 | (if usage (setq doc (cdr usage))) | |
96 | (if name | |
97 | (if doc | |
98 | (format "%s\n%s" name doc) | |
99 | (format "%s" name)) | |
100 | (or doc "No documentation"))))) | |
101 | "\n"))) | |
102 | (setq flist (advice--cdr flist))) | |
103 | (if docstring (setq docstring (concat docstring "\n"))) | |
104 | (let* ((origdoc (unless (eq function flist) ;Avoid inf-loops. | |
105 | (documentation flist t))) | |
106 | (usage (help-split-fundoc origdoc function))) | |
107 | (setq usage (if (null usage) | |
108 | (let ((arglist (help-function-arglist flist))) | |
109 | (format "%S" (help-make-usage function arglist))) | |
110 | (setq origdoc (cdr usage)) (car usage))) | |
111 | (help-add-fundoc-usage (concat docstring origdoc) usage)))) | |
112 | ||
1668ea90 SM |
113 | (defun advice-eval-interactive-spec (spec) |
114 | "Evaluate the interactive spec SPEC." | |
115 | (cond | |
116 | ((stringp spec) | |
117 | ;; There's no direct access to the C code (in call-interactively) that | |
118 | ;; processes those specs, but that shouldn't stop us, should it? | |
119 | ;; FIXME: Despite appearances, this is not faithful: SPEC and | |
120 | ;; (advice-eval-interactive-spec SPEC) will behave subtly differently w.r.t | |
121 | ;; command-history (and maybe a few other details). | |
122 | (call-interactively `(lambda (&rest args) (interactive ,spec) args))) | |
123 | ;; ((functionp spec) (funcall spec)) | |
124 | (t (eval spec)))) | |
125 | ||
adbfe42c SM |
126 | (defun advice--interactive-form (function) |
127 | ;; Like `interactive-form' but tries to avoid autoloading functions. | |
128 | (when (commandp function) | |
129 | (if (not (and (symbolp function) (autoloadp (symbol-function function)))) | |
130 | (interactive-form function) | |
131 | `(interactive (advice-eval-interactive-spec | |
132 | (cadr (interactive-form ',function))))))) | |
133 | ||
231d8498 | 134 | (defun advice--make-interactive-form (function main) |
231d8498 SM |
135 | ;; TODO: make it so that interactive spec can be a constant which |
136 | ;; dynamically checks the advice--car/cdr to do its job. | |
1668ea90 | 137 | ;; For that, advice-eval-interactive-spec needs to be more faithful. |
adbfe42c SM |
138 | (let* ((iff (advice--interactive-form function)) |
139 | (ifm (advice--interactive-form main)) | |
140 | (fspec (cadr iff))) | |
47f01a8a | 141 | (when (eq 'function (car-safe fspec)) ;; Macroexpanded lambda? |
1668ea90 SM |
142 | (setq fspec (nth 1 fspec))) |
143 | (if (functionp fspec) | |
adbfe42c SM |
144 | `(funcall ',fspec ',(cadr ifm)) |
145 | (cadr (or iff ifm))))) | |
231d8498 | 146 | |
adbfe42c | 147 | (defun advice--make-1 (byte-code stack-depth function main props) |
231d8498 SM |
148 | "Build a function value that adds FUNCTION to MAIN." |
149 | (let ((adv-sig (gethash main advertised-signature-table)) | |
150 | (advice | |
151 | (apply #'make-byte-code 128 byte-code | |
1ea22560 | 152 | (vector #'apply function main props) stack-depth nil |
cb3a1380 | 153 | (and (or (commandp function) (commandp main)) |
cb3a1380 SM |
154 | (list (advice--make-interactive-form |
155 | function main)))))) | |
231d8498 SM |
156 | (when adv-sig (puthash advice adv-sig advertised-signature-table)) |
157 | advice)) | |
158 | ||
159 | (defun advice--make (where function main props) | |
160 | "Build a function value that adds FUNCTION to MAIN at WHERE. | |
161 | WHERE is a symbol to select an entry in `advice--where-alist'." | |
cb3a1380 SM |
162 | (let ((fd (or (cdr (assq 'depth props)) 0)) |
163 | (md (if (advice--p main) | |
164 | (or (cdr (assq 'depth (advice--props main))) 0)))) | |
165 | (if (and md (> fd md)) | |
166 | ;; `function' should go deeper. | |
167 | (let ((rest (advice--make where function (advice--cdr main) props))) | |
168 | (advice--make-1 (aref main 1) (aref main 3) | |
169 | (advice--car main) rest (advice--props main))) | |
170 | (let ((desc (assq where advice--where-alist))) | |
171 | (unless desc (error "Unknown add-function location `%S'" where)) | |
172 | (advice--make-1 (nth 1 desc) (nth 2 desc) | |
173 | function main props))))) | |
231d8498 | 174 | |
dabefae5 | 175 | (defun advice--member-p (function name definition) |
231d8498 SM |
176 | (let ((found nil)) |
177 | (while (and (not found) (advice--p definition)) | |
178 | (if (or (equal function (advice--car definition)) | |
dabefae5 SM |
179 | (when name |
180 | (equal name (cdr (assq 'name (advice--props definition)))))) | |
c67c0839 | 181 | (setq found definition) |
231d8498 SM |
182 | (setq definition (advice--cdr definition)))) |
183 | found)) | |
184 | ||
a731fc1b | 185 | (defun advice--tweak (flist tweaker) |
231d8498 | 186 | (if (not (advice--p flist)) |
a731fc1b | 187 | (funcall tweaker nil flist nil) |
231d8498 | 188 | (let ((first (advice--car flist)) |
a731fc1b | 189 | (rest (advice--cdr flist)) |
231d8498 | 190 | (props (advice--props flist))) |
cb9c0a53 SM |
191 | (let ((val (funcall tweaker first rest props))) |
192 | (if val (car val) | |
a731fc1b SM |
193 | (let ((nrest (advice--tweak rest tweaker))) |
194 | (if (eq rest nrest) flist | |
195 | (advice--make-1 (aref flist 1) (aref flist 3) | |
cb9c0a53 | 196 | first nrest props)))))))) |
a731fc1b SM |
197 | |
198 | ;;;###autoload | |
199 | (defun advice--remove-function (flist function) | |
200 | (advice--tweak flist | |
201 | (lambda (first rest props) | |
cb9c0a53 SM |
202 | (cond ((not first) rest) |
203 | ((or (equal function first) | |
a731fc1b | 204 | (equal function (cdr (assq 'name props)))) |
cb9c0a53 | 205 | (list rest)))))) |
231d8498 | 206 | |
1d44e9dc SM |
207 | (defvar advice--buffer-local-function-sample nil |
208 | "keeps an example of the special \"run the default value\" functions. | |
209 | These functions play the same role as t in buffer-local hooks, and to recognize | |
210 | them, we keep a sample here against which to compare. Each instance is | |
211 | different, but `function-equal' will hopefully ignore those differences.") | |
a61428c4 SM |
212 | |
213 | (defun advice--set-buffer-local (var val) | |
214 | (if (function-equal val advice--buffer-local-function-sample) | |
215 | (kill-local-variable var) | |
216 | (set (make-local-variable var) val))) | |
217 | ||
218 | ;;;###autoload | |
219 | (defun advice--buffer-local (var) | |
220 | "Buffer-local value of VAR, presumed to contain a function." | |
221 | (declare (gv-setter advice--set-buffer-local)) | |
222 | (if (local-variable-p var) (symbol-value var) | |
223 | (setq advice--buffer-local-function-sample | |
1d44e9dc | 224 | ;; This function acts like the t special value in buffer-local hooks. |
a61428c4 SM |
225 | (lambda (&rest args) (apply (default-value var) args))))) |
226 | ||
231d8498 SM |
227 | ;;;###autoload |
228 | (defmacro add-function (where place function &optional props) | |
229 | ;; TODO: | |
a61428c4 | 230 | ;; - maybe let `where' specify some kind of predicate and use it |
231d8498 | 231 | ;; to implement things like mode-local or eieio-defmethod. |
a61428c4 SM |
232 | ;; Of course, that only makes sense if the predicates of all advices can |
233 | ;; be combined and made more efficient. | |
231d8498 SM |
234 | ;; :before is like a normal add-hook on a normal hook. |
235 | ;; :before-while is like add-hook on run-hook-with-args-until-failure. | |
236 | ;; :before-until is like add-hook on run-hook-with-args-until-success. | |
237 | ;; Same with :after-* but for (add-hook ... 'append). | |
238 | "Add a piece of advice on the function stored at PLACE. | |
239 | FUNCTION describes the code to add. WHERE describes where to add it. | |
240 | WHERE can be explained by showing the resulting new function, as the | |
241 | result of combining FUNCTION and the previous value of PLACE, which we | |
242 | call OLDFUN here: | |
243 | `:before' (lambda (&rest r) (apply FUNCTION r) (apply OLDFUN r)) | |
244 | `:after' (lambda (&rest r) (prog1 (apply OLDFUN r) (apply FUNCTION r))) | |
245 | `:around' (lambda (&rest r) (apply FUNCTION OLDFUN r)) | |
bcd7a0a4 | 246 | `:override' (lambda (&rest r) (apply FUNCTION r)) |
231d8498 SM |
247 | `:before-while' (lambda (&rest r) (and (apply FUNCTION r) (apply OLDFUN r))) |
248 | `:before-until' (lambda (&rest r) (or (apply FUNCTION r) (apply OLDFUN r))) | |
249 | `:after-while' (lambda (&rest r) (and (apply OLDFUN r) (apply FUNCTION r))) | |
250 | `:after-until' (lambda (&rest r) (or (apply OLDFUN r) (apply FUNCTION r))) | |
d36ed1c8 SM |
251 | `:filter-args' (lambda (&rest r) (apply OLDFUN (funcall FUNCTION r))) |
252 | `:filter-return'(lambda (&rest r) (funcall FUNCTION (apply OLDFUN r))) | |
231d8498 SM |
253 | If FUNCTION was already added, do nothing. |
254 | PROPS is an alist of additional properties, among which the following have | |
255 | a special meaning: | |
1668ea90 | 256 | - `name': a string or symbol. It can be used to refer to this piece of advice. |
cb3a1380 SM |
257 | - `depth': a number indicating a preference w.r.t ordering. |
258 | The default depth is 0. By convention, a depth of 100 means that | |
259 | the advice should be innermost (i.e. at the end of the list), | |
260 | whereas a depth of -100 means that the advice should be outermost. | |
1668ea90 | 261 | |
36c0a301 SM |
262 | If PLACE is a simple variable, only its global value will be affected. |
263 | Use (local 'VAR) if you want to apply FUNCTION to VAR buffer-locally. | |
a61428c4 | 264 | |
1668ea90 SM |
265 | If one of FUNCTION or OLDFUN is interactive, then the resulting function |
266 | is also interactive. There are 3 cases: | |
267 | - FUNCTION is not interactive: the interactive spec of OLDFUN is used. | |
268 | - The interactive spec of FUNCTION is itself a function: it should take one | |
269 | argument (the interactive spec of OLDFUN, which it can pass to | |
270 | `advice-eval-interactive-spec') and return the list of arguments to use. | |
271 | - Else, use the interactive spec of FUNCTION and ignore the one of OLDFUN." | |
231d8498 | 272 | (declare (debug t)) ;;(indent 2) |
a61428c4 SM |
273 | (cond ((eq 'local (car-safe place)) |
274 | (setq place `(advice--buffer-local ,@(cdr place)))) | |
275 | ((symbolp place) | |
36c0a301 | 276 | (setq place `(default-value ',place)))) |
231d8498 SM |
277 | `(advice--add-function ,where (gv-ref ,place) ,function ,props)) |
278 | ||
279 | ;;;###autoload | |
280 | (defun advice--add-function (where ref function props) | |
c67c0839 SM |
281 | (let ((a (advice--member-p function (cdr (assq 'name props)) |
282 | (gv-deref ref)))) | |
283 | (when a | |
284 | ;; The advice is already present. Remove the old one, first. | |
285 | (setf (gv-deref ref) | |
286 | (advice--remove-function (gv-deref ref) (advice--car a)))) | |
231d8498 SM |
287 | (setf (gv-deref ref) |
288 | (advice--make where function (gv-deref ref) props)))) | |
289 | ||
bcd7a0a4 | 290 | ;;;###autoload |
231d8498 SM |
291 | (defmacro remove-function (place function) |
292 | "Remove the FUNCTION piece of advice from PLACE. | |
293 | If FUNCTION was not added to PLACE, do nothing. | |
294 | Instead of FUNCTION being the actual function, it can also be the `name' | |
295 | of the piece of advice." | |
296 | (declare (debug t)) | |
a61428c4 SM |
297 | (cond ((eq 'local (car-safe place)) |
298 | (setq place `(advice--buffer-local ,@(cdr place)))) | |
299 | ((symbolp place) | |
986545b5 | 300 | (setq place `(default-value ',place)))) |
231d8498 SM |
301 | (gv-letplace (getter setter) place |
302 | (macroexp-let2 nil new `(advice--remove-function ,getter ,function) | |
303 | `(unless (eq ,new ,getter) ,(funcall setter new))))) | |
304 | ||
1d44e9dc SM |
305 | (defun advice-function-mapc (f function-def) |
306 | "Apply F to every advice function in FUNCTION-DEF. | |
307 | F is called with two arguments: the function that was added, and the | |
308 | properties alist that was specified when it was added." | |
309 | (while (advice--p function-def) | |
310 | (funcall f (advice--car function-def) (advice--props function-def)) | |
311 | (setq function-def (advice--cdr function-def)))) | |
312 | ||
313 | (defun advice-function-member-p (advice function-def) | |
314 | "Return non-nil if ADVICE is already in FUNCTION-DEF. | |
315 | Instead of ADVICE being the actual function, it can also be the `name' | |
316 | of the piece of advice." | |
317 | (advice--member-p advice advice function-def)) | |
318 | ||
231d8498 SM |
319 | ;;;; Specific application of add-function to `symbol-function' for advice. |
320 | ||
321 | (defun advice--subst-main (old new) | |
a731fc1b SM |
322 | (advice--tweak old |
323 | (lambda (first _rest _props) (if (not first) new)))) | |
231d8498 | 324 | |
413d4689 SM |
325 | (defun advice--normalize (symbol def) |
326 | (cond | |
327 | ((special-form-p def) | |
328 | ;; Not worth the trouble trying to handle this, I think. | |
1d44e9dc | 329 | (error "Advice impossible: %S is a special form" symbol)) |
671d5c16 SM |
330 | ((and (symbolp def) (macrop def)) |
331 | (let ((newval `(macro . ,(lambda (&rest r) (macroexpand `(,def . ,r)))))) | |
1d44e9dc | 332 | (put symbol 'advice--saved-rewrite (cons def (cdr newval))) |
413d4689 SM |
333 | newval)) |
334 | ;; `f' might be a pure (hence read-only) cons! | |
335 | ((and (eq 'macro (car-safe def)) | |
336 | (not (ignore-errors (setcdr def (cdr def)) t))) | |
337 | (cons 'macro (cdr def))) | |
338 | (t def))) | |
339 | ||
340 | (defsubst advice--strip-macro (x) | |
341 | (if (eq 'macro (car-safe x)) (cdr x) x)) | |
342 | ||
1d44e9dc SM |
343 | (defun advice--symbol-function (symbol) |
344 | ;; The value conceptually stored in `symbol-function' is split into two | |
345 | ;; parts: | |
346 | ;; - the normal function definition. | |
347 | ;; - the list of advice applied to it. | |
348 | ;; `advice--symbol-function' is intended to return the second part (i.e. the | |
349 | ;; list of advice, which includes a hole at the end which typically holds the | |
350 | ;; first part, but this function doesn't care much which value is found | |
351 | ;; there). | |
352 | ;; In the "normal" state both parts are combined into a single value stored | |
353 | ;; in the "function slot" of the symbol. But the way they are combined is | |
354 | ;; different depending on whether the definition is a function or a macro. | |
355 | ;; Also if the function definition is nil (i.e. unbound) or is an autoload, | |
356 | ;; the second part is stashed away temporarily in the `advice--pending' | |
357 | ;; symbol property. | |
358 | (or (get symbol 'advice--pending) | |
359 | (advice--strip-macro (symbol-function symbol)))) | |
360 | ||
231d8498 | 361 | (defun advice--defalias-fset (fsetfun symbol newdef) |
1d44e9dc | 362 | (unless fsetfun (setq fsetfun #'fset)) |
413d4689 SM |
363 | (when (get symbol 'advice--saved-rewrite) |
364 | (put symbol 'advice--saved-rewrite nil)) | |
365 | (setq newdef (advice--normalize symbol newdef)) | |
671d5c16 | 366 | (let ((oldadv (advice--symbol-function symbol))) |
539f75f4 SM |
367 | (if (and newdef (not (autoloadp newdef))) |
368 | (let* ((snewdef (advice--strip-macro newdef)) | |
369 | (snewadv (advice--subst-main oldadv snewdef))) | |
370 | (put symbol 'advice--pending nil) | |
1d44e9dc | 371 | (funcall fsetfun symbol |
539f75f4 SM |
372 | (if (eq snewdef newdef) snewadv (cons 'macro snewadv)))) |
373 | (unless (eq oldadv (get symbol 'advice--pending)) | |
374 | (put symbol 'advice--pending (advice--subst-main oldadv nil))) | |
1d44e9dc | 375 | (funcall fsetfun symbol newdef)))) |
231d8498 SM |
376 | |
377 | ;;;###autoload | |
378 | (defun advice-add (symbol where function &optional props) | |
379 | "Like `add-function' but for the function named SYMBOL. | |
380 | Contrary to `add-function', this will properly handle the cases where SYMBOL | |
381 | is defined as a macro, alias, command, ..." | |
382 | ;; TODO: | |
383 | ;; - record the advice location, to display in describe-function. | |
384 | ;; - change all defadvice in lisp/**/*.el. | |
231d8498 | 385 | ;; - obsolete advice.el. |
539f75f4 | 386 | (let* ((f (symbol-function symbol)) |
413d4689 | 387 | (nf (advice--normalize symbol f))) |
1d44e9dc | 388 | (unless (eq f nf) (fset symbol nf)) |
231d8498 | 389 | (add-function where (cond |
413d4689 | 390 | ((eq (car-safe nf) 'macro) (cdr nf)) |
1668ea90 SM |
391 | ;; Reasons to delay installation of the advice: |
392 | ;; - If the function is not yet defined, installing | |
393 | ;; the advice would affect `fboundp'ness. | |
adbfe42c SM |
394 | ;; - the symbol-function slot of an autoloaded |
395 | ;; function is not itself a function value. | |
1668ea90 SM |
396 | ;; - `autoload' does nothing if the function is |
397 | ;; not an autoload or undefined. | |
398 | ((or (not nf) (autoloadp nf)) | |
231d8498 SM |
399 | (get symbol 'advice--pending)) |
400 | (t (symbol-function symbol))) | |
401 | function props) | |
0d53f628 | 402 | (put symbol 'function-documentation `(advice--make-docstring ',symbol)) |
231d8498 SM |
403 | (add-function :around (get symbol 'defalias-fset-function) |
404 | #'advice--defalias-fset)) | |
405 | nil) | |
406 | ||
407 | ;;;###autoload | |
408 | (defun advice-remove (symbol function) | |
409 | "Like `remove-function' but for the function named SYMBOL. | |
539f75f4 SM |
410 | Contrary to `remove-function', this also works when SYMBOL is a macro |
411 | or an autoload and it preserves `fboundp'. | |
231d8498 SM |
412 | Instead of the actual function to remove, FUNCTION can also be the `name' |
413 | of the piece of advice." | |
539f75f4 | 414 | (let ((f (symbol-function symbol))) |
1d44e9dc SM |
415 | (remove-function (cond ;This is `advice--symbol-function' but as a "place". |
416 | ((get symbol 'advice--pending) | |
417 | (get symbol 'advice--pending)) | |
418 | ((eq (car-safe f) 'macro) (cdr f)) | |
419 | (t (symbol-function symbol))) | |
539f75f4 | 420 | function) |
671d5c16 | 421 | (unless (advice--p (advice--symbol-function symbol)) |
539f75f4 SM |
422 | (remove-function (get symbol 'defalias-fset-function) |
423 | #'advice--defalias-fset) | |
1d44e9dc SM |
424 | (let ((asr (get symbol 'advice--saved-rewrite))) |
425 | (and asr (eq (cdr-safe (symbol-function symbol)) | |
426 | (cdr asr)) | |
427 | (fset symbol (car (get symbol 'advice--saved-rewrite))))))) | |
539f75f4 SM |
428 | nil) |
429 | ||
1d44e9dc SM |
430 | (defun advice-mapc (fun symbol) |
431 | "Apply FUN to every advice function in SYMBOL. | |
539f75f4 SM |
432 | FUN is called with a two arguments: the function that was added, and the |
433 | properties alist that was specified when it was added." | |
1d44e9dc | 434 | (advice-function-mapc fun (advice--symbol-function symbol))) |
231d8498 SM |
435 | |
436 | ;;;###autoload | |
1d44e9dc SM |
437 | (defun advice-member-p (advice symbol) |
438 | "Return non-nil if ADVICE has been added to SYMBOL. | |
413d4689 | 439 | Instead of ADVICE being the actual function, it can also be the `name' |
231d8498 | 440 | of the piece of advice." |
1d44e9dc | 441 | (advice-function-member-p advice (advice--symbol-function symbol))) |
231d8498 | 442 | |
23ba2705 SM |
443 | ;; When code is advised, called-interactively-p needs to be taught to skip |
444 | ;; the advising frames. | |
445 | ;; FIXME: This Major Ugly Hack won't handle calls to called-interactively-p | |
446 | ;; done from the advised function if the deepest advice is an around advice! | |
447 | ;; In other cases (calls from an advice or calls from the advised function when | |
448 | ;; the deepest advice is not an around advice), it should hopefully get | |
449 | ;; it right. | |
450 | (add-hook 'called-interactively-p-functions | |
451 | #'advice--called-interactively-skip) | |
452 | (defun advice--called-interactively-skip (origi frame1 frame2) | |
453 | (let* ((i origi) | |
454 | (get-next-frame | |
455 | (lambda () | |
456 | (setq frame1 frame2) | |
7ced0d04 | 457 | (setq frame2 (backtrace-frame i #'called-interactively-p)) |
23ba2705 SM |
458 | ;; (message "Advice Frame %d = %S" i frame2) |
459 | (setq i (1+ i))))) | |
460 | (when (and (eq (nth 1 frame2) 'apply) | |
461 | (progn | |
462 | (funcall get-next-frame) | |
463 | (advice--p (indirect-function (nth 1 frame2))))) | |
464 | (funcall get-next-frame) | |
465 | ;; If we now have the symbol, this was the head advice and | |
466 | ;; we're done. | |
467 | (while (advice--p (nth 1 frame1)) | |
468 | ;; This was an inner advice called from some earlier advice. | |
469 | ;; The stack frames look different depending on the particular | |
470 | ;; kind of the earlier advice. | |
471 | (let ((inneradvice (nth 1 frame1))) | |
472 | (if (and (eq (nth 1 frame2) 'apply) | |
473 | (progn | |
474 | (funcall get-next-frame) | |
475 | (advice--p (indirect-function | |
476 | (nth 1 frame2))))) | |
477 | ;; The earlier advice was something like a before/after | |
478 | ;; advice where the "next" code is called directly by the | |
479 | ;; advice--p object. | |
480 | (funcall get-next-frame) | |
481 | ;; It's apparently an around advice, where the "next" is | |
482 | ;; called by the body of the advice in any way it sees fit, | |
483 | ;; so we need to skip the frames of that body. | |
484 | (while | |
485 | (progn | |
486 | (funcall get-next-frame) | |
487 | (not (and (eq (nth 1 frame2) 'apply) | |
488 | (eq (nth 3 frame2) inneradvice))))) | |
489 | (funcall get-next-frame) | |
490 | (funcall get-next-frame)))) | |
491 | (- i origi 1)))) | |
492 | ||
231d8498 SM |
493 | |
494 | (provide 'nadvice) | |
495 | ;;; nadvice.el ends here |