unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Tianxiang Xiong <tianxiang.xiong@gmail.com>
To: Emacs developers <emacs-devel@gnu.org>
Subject: [PATCH] Add `advice-remove-all`
Date: Wed, 26 Apr 2017 20:23:31 -0700	[thread overview]
Message-ID: <CACMkxixrhVME0ReBOaGo3PdcXo6Zy93aPvs4scL34ycr4y=ADw@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 451 bytes --]

I recently had the need to remove all advice from a function I was playing
around with, and was surprised to find that `advice-remove-all` did not
exist.

Others have had need of it
<https://emacs.stackexchange.com/questions/24657/unadvice-a-function/24658#24658>
as well, so it seems like a good candidate for inclusion in `nadvice.el`.

The attached patch adds `advice-remove-all` to `nadvice.el`, using the
implementation suggested by xuchungyang.

[-- Attachment #1.2: Type: text/html, Size: 533 bytes --]

[-- Attachment #2: 0001-Add-advice-remove-all.patch --]
[-- Type: text/x-patch, Size: 3429 bytes --]

From cd0378a2493e391ba05889d9261317d3cef0db50 Mon Sep 17 00:00:00 2001
From: Tianxiang Xiong <tianxiang.xiong@gmail.com>
Date: Wed, 26 Apr 2017 20:13:05 -0700
Subject: [PATCH] Add `advice-remove-all`

Removes all advice from a function.
---
 doc/lispref/functions.texi |  4 ++++
 etc/NEWS                   |  4 ++++
 lisp/emacs-lisp/nadvice.el | 18 ++++++++++++------
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 283f74f..3f3bee3 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -1734,6 +1734,10 @@ Remove the advice @var{function} from the named function @var{symbol}.
 @var{function} can also be the @code{name} of a piece of advice.
 @end defun
 
+@defun advice-remove-all symbol
+Remove all advice from named function @var{symbol}.
+@end defun
+
 @defun advice-member-p function symbol
 Return non-@code{nil} if the advice @var{function} is already in the named
 function @var{symbol}.  @var{function} can also be the @code{name} of
diff --git a/etc/NEWS b/etc/NEWS
index 9d4c72d..729c379 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -24,6 +24,10 @@ When you add a new item, use the appropriate mark if you are sure it applies,
 \f
 * Installation Changes in Emacs 26.1
 
++++
+** The function 'advice-remove-all' is added to 'nadvice.el'
+This allows all advice to be removed from a function.
+
 ** By default libgnutls is now required when building Emacs.
 Use 'configure --with-gnutls=no' to build even when GnuTLS is missing.
 
diff --git a/lisp/emacs-lisp/nadvice.el b/lisp/emacs-lisp/nadvice.el
index fd1cd2c..957fd85 100644
--- a/lisp/emacs-lisp/nadvice.el
+++ b/lisp/emacs-lisp/nadvice.el
@@ -335,6 +335,12 @@ properties alist that was specified when it was added."
     (funcall f (advice--car function-def) (advice--props function-def))
     (setq function-def (advice--cdr function-def))))
 
+(defun advice-mapc (fun symbol)
+  "Apply FUN to every advice function in SYMBOL.
+FUN is called with a two arguments: the function that was added, and the
+properties alist that was specified when it was added."
+  (advice-function-mapc fun (advice--symbol-function symbol)))
+
 (defun advice-function-member-p (advice function-def)
   "Return non-nil if ADVICE is already in FUNCTION-DEF.
 Instead of ADVICE being the actual function, it can also be the `name'
@@ -453,6 +459,12 @@ of the piece of advice."
   nil)
 
 ;;;###autoload
+(defun advice-remove-all (symbol)
+  "Remove all advice from function named SYMBOL."
+  (advice-mapc (lambda (advice _props) (advice-remove symbol advice))
+               symbol))
+
+;;;###autoload
 (defmacro define-advice (symbol args &rest body)
   "Define an advice and add it to function named SYMBOL.
 See `advice-add' and `add-function' for explanation on the
@@ -476,12 +488,6 @@ otherwise it is named `SYMBOL@NAME'.
     `(prog1 ,@(and (symbolp advice) `((defun ,advice ,lambda-list ,@body)))
        (advice-add ',symbol ,where #',advice ,@(and props `(',props))))))
 
-(defun advice-mapc (fun symbol)
-  "Apply FUN to every advice function in SYMBOL.
-FUN is called with a two arguments: the function that was added, and the
-properties alist that was specified when it was added."
-  (advice-function-mapc fun (advice--symbol-function symbol)))
-
 ;;;###autoload
 (defun advice-member-p (advice symbol)
   "Return non-nil if ADVICE has been added to SYMBOL.
-- 
2.7.4


             reply	other threads:[~2017-04-27  3:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-27  3:23 Tianxiang Xiong [this message]
2017-04-27 12:52 ` [PATCH] Add `advice-remove-all` Stefan Monnier
2017-05-01 13:26   ` Marcin Borkowski
2017-05-01 17:21     ` Stefan Monnier
2017-05-02  5:31       ` Marcin Borkowski
2017-05-02 13:47         ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2019-09-04 16:59 Matthew Newton
2019-09-04 18:30 ` Stefan Monnier
2019-09-05  0:14   ` T.V Raman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACMkxixrhVME0ReBOaGo3PdcXo6Zy93aPvs4scL34ycr4y=ADw@mail.gmail.com' \
    --to=tianxiang.xiong@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).