Function: advice-add

advice-add is a byte-compiled function defined in nadvice.el.gz.

Signature

(advice-add SYMBOL WHERE FUNCTION &optional PROPS)

Documentation

Like add-function but for the function named SYMBOL.

Contrary to add-function, this will properly handle the cases where SYMBOL is defined as a macro, alias, command, ...

Probably introduced at or before Emacs version 24.4.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/nadvice.el.gz
;;;###autoload
(defun advice-add (symbol where function &optional props)
  "Like `add-function' but for the function named SYMBOL.
Contrary to `add-function', this will properly handle the cases where SYMBOL
is defined as a macro, alias, command, ..."
  ;; TODO:
  ;; - record the advice location, to display in describe-function.
  ;; - change all defadvice in lisp/**/*.el.
  ;; - obsolete advice.el.
  (let* ((f (symbol-function symbol))
	 (nf (advice--normalize symbol f)))
    (unless (eq f nf) (fset symbol nf))
    (add-function where (cond
                         ((eq (car-safe nf) 'macro) (cdr nf))
                         ;; Reasons to delay installation of the advice:
                         ;; - If the function is not yet defined, installing
                         ;;   the advice would affect `fboundp'ness.
                         ;; - the symbol-function slot of an autoloaded
                         ;;   function is not itself a function value.
                         ;; - `autoload' does nothing if the function is
                         ;;   not an autoload or undefined.
                         ((or (not nf) (autoloadp nf))
                          (get symbol 'advice--pending))
                         (t (symbol-function symbol)))
                  function props)
    (put symbol 'function-documentation `(advice--make-docstring ',symbol))
    (add-function :around (get symbol 'defalias-fset-function)
                  #'advice--defalias-fset))
  nil)