diff options
author | Andrea Corallo <akrl@sdf.org> | 2020-10-02 18:13:28 +0200 |
---|---|---|
committer | Andrea Corallo <akrl@sdf.org> | 2020-10-02 21:20:50 +0200 |
commit | d07d7ab1a0e321ced62ebe5dd9db27eb7e93430e (patch) | |
tree | 5649441b909d42e121644180e79c1e4789c9b614 /lisp/emacs-lisp | |
parent | 36e0c3fb07db9805e97fbd2650aa28ac2c169dba (diff) | |
download | emacs-d07d7ab1a0e321ced62ebe5dd9db27eb7e93430e.tar.gz emacs-d07d7ab1a0e321ced62ebe5dd9db27eb7e93430e.tar.bz2 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.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r-- | lisp/emacs-lisp/nadvice.el | 26 |
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." (macroexp-let2 nil new `(advice--remove-function ,getter ,function) `(unless (eq ,new ,getter) ,(funcall setter new))))) +;;;###autoload +(defmacro advice-flet (bindings &rest body) + ;; FIXME add doc. + (declare (indent 1)) + (let ((let-binds ()) + (ad-add ()) + (ad-del ())) + (dolist (bind bindings) + (let* ((fun-name (car bind)) + (fun (cadr bind)) + (tmp-sym (gensym (symbol-name fun-name)))) + (push `(,tmp-sym ,fun) let-binds) + (push `(advice-add #',fun-name + ,(if (= (length bind) 3) + (nth 2 bind) + :override) + ,tmp-sym) + ad-add) + (push `(advice-remove #',fun-name ,tmp-sym) ad-del))) + `(let ,(reverse let-binds) + (unwind-protect + (progn + ,@(reverse ad-add) + ,@body) + ,@(reverse ad-del))))) + (defun advice-function-mapc (f function-def) "Apply F to every advice function in FUNCTION-DEF. F is called with two arguments: the function that was added, and the |