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