nadvice.el 25 KB

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