aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrea Corallo2020-10-02 18:13:28 +0200
committerAndrea Corallo2020-10-02 21:20:50 +0200
commitd07d7ab1a0e321ced62ebe5dd9db27eb7e93430e (patch)
tree5649441b909d42e121644180e79c1e4789c9b614
parent36e0c3fb07db9805e97fbd2650aa28ac2c169dba (diff)
downloademacs-d07d7ab1a0e321ced62ebe5dd9db27eb7e93430e.tar.gz
emacs-d07d7ab1a0e321ced62ebe5dd9db27eb7e93430e.zip
Add `advice-flet' macro
The testsuite does large use of primitive redefinition, to avoid that we define `advice-flet' to use instead as an easy `cl-letf' replacement. * lisp/emacs-lisp/nadvice.el (advice-flet): New macro.
-rw-r--r--lisp/emacs-lisp/nadvice.el26
1 files changed, 26 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/nadvice.el b/lisp/emacs-lisp/nadvice.el
index 5b3aa708508..21da038dc1c 100644
--- a/lisp/emacs-lisp/nadvice.el
+++ b/lisp/emacs-lisp/nadvice.el
@@ -356,6 +356,32 @@ of the piece of advice."
356 (macroexp-let2 nil new `(advice--remove-function ,getter ,function) 356 (macroexp-let2 nil new `(advice--remove-function ,getter ,function)
357 `(unless (eq ,new ,getter) ,(funcall setter new))))) 357 `(unless (eq ,new ,getter) ,(funcall setter new)))))
358 358
359;;;###autoload
360(defmacro advice-flet (bindings &rest body)
361 ;; FIXME add doc.
362 (declare (indent 1))
363 (let ((let-binds ())
364 (ad-add ())
365 (ad-del ()))
366 (dolist (bind bindings)
367 (let* ((fun-name (car bind))
368 (fun (cadr bind))
369 (tmp-sym (gensym (symbol-name fun-name))))
370 (push `(,tmp-sym ,fun) let-binds)
371 (push `(advice-add #',fun-name
372 ,(if (= (length bind) 3)
373 (nth 2 bind)
374 :override)
375 ,tmp-sym)
376 ad-add)
377 (push `(advice-remove #',fun-name ,tmp-sym) ad-del)))
378 `(let ,(reverse let-binds)
379 (unwind-protect
380 (progn
381 ,@(reverse ad-add)
382 ,@body)
383 ,@(reverse ad-del)))))
384
359(defun advice-function-mapc (f function-def) 385(defun advice-function-mapc (f function-def)
360 "Apply F to every advice function in FUNCTION-DEF. 386 "Apply F to every advice function in FUNCTION-DEF.
361F is called with two arguments: the function that was added, and the 387F is called with two arguments: the function that was added, and the