Function: advice-remove

advice-remove is an interactive and byte-compiled function defined in nadvice.el.gz.

Signature

(advice-remove SYMBOL FUNCTION)

Documentation

Like remove-function but for the function named SYMBOL.

Contrary to remove-function, this also works when SYMBOL is a macro or an autoload and it preserves fboundp. Instead of the actual function to remove, FUNCTION can also be the name of the piece of advice.

View in manual

Probably introduced at or before Emacs version 24.4.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/nadvice.el.gz
;;;###autoload
(defun advice-remove (symbol function)
  "Like `remove-function' but for the function named SYMBOL.
Contrary to `remove-function', this also works when SYMBOL is a macro
or an autoload and it preserves `fboundp'.
Instead of the actual function to remove, FUNCTION can also be the `name'
of the piece of advice."
  (interactive
   (let* ((pred (lambda (sym) (advice--p (advice--symbol-function sym))))
          (default (when-let* ((f (function-called-at-point))
                               ((funcall pred f)))
                     (symbol-name f)))
          (prompt (format-prompt "Remove advice from function" default))
          (symbol (intern (completing-read prompt obarray pred t nil nil default)))
          advices)
     (advice-mapc (lambda (f p)
                    (let ((k (or (alist-get 'name p) f)))
                      (push (cons
                             ;; "name" (string) and 'name (symbol) are
                             ;; considered different names so we use
                             ;; `prin1-to-string' even if the name is
                             ;; a string to distinguish between these
                             ;; two cases.
                             (prin1-to-string k)
                             ;; We use `k' here instead of `f' because
                             ;; the same advice can have multiple
                             ;; names.
                             k)
                            advices)))
                  symbol)
     (list symbol (cdr (assoc-string
                        (completing-read "Advice to remove: " advices nil t)
                        advices)))))
  (let ((f (symbol-function symbol)))
    (remove-function (cond ;This is `advice--symbol-function' but as a "place".
                      ((get symbol 'advice--pending)
                       (get symbol 'advice--pending))
                      ((eq (car-safe f) 'macro) (cdr f))
                      (t (symbol-function symbol)))
                     function)
    (unless (advice--p (advice--symbol-function symbol))
      (remove-function (get symbol 'defalias-fset-function)
                       #'advice--defalias-fset)
      (let ((asr (get symbol 'advice--saved-rewrite)))
        (and asr (eq (cdr-safe (symbol-function symbol))
                     (cdr asr))
             (fset symbol (car (get symbol 'advice--saved-rewrite)))))))
  nil)