Function: ad-preactivate-advice

ad-preactivate-advice is a byte-compiled function defined in advice.el.gz.

Signature

(ad-preactivate-advice FUNCTION ADVICE CLASS POSITION)

Documentation

Preactivate FUNCTION and return the constructed cache.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/advice.el.gz
;; @@ Preactivation:
;; =================
;; Preactivation can be used to generate compiled advised definitions
;; at compile time without having to give up the dynamic runtime flexibility
;; of the advice mechanism.  Preactivation is a special feature of `defadvice',
;; it involves the following steps:
;;  - remembering the function's current state (definition and advice-info)
;;  - advising it with the defined piece of advice
;;  - clearing its cache
;;  - generating an interpreted advised definition by activating it, this will
;;    make use of all its current active advice and its current definition
;;  - saving the so generated cached definition and id
;;  - resetting the function's advice and definition state to what it was
;;    before the preactivation
;;  - Returning the saved definition and its id to be used in the expansion of
;;    `defadvice' to assign it as an initial cache, hence it will be compiled
;;    at time the `defadvice' gets compiled.
;; Naturally, for preactivation to be effective it has to be applied/compiled
;; at the right time, i.e., when the current state of advices and function
;; definition exactly reflects the state at activation time.  Should that not
;; be the case, the precompiled definition will just be discarded and a new
;; advised definition will be generated.

(defun ad-preactivate-advice (function advice class position)
  "Preactivate FUNCTION and return the constructed cache."
  (let* ((advicefunname (ad-get-advice-info-field function 'advicefunname))
         (old-advice (symbol-function advicefunname))
	 (old-advice-info (ad-copy-advice-info function))
	 (ad-advised-functions ad-advised-functions))
    (unwind-protect
	(progn
	  (ad-add-advice function advice class position)
	  (ad-enable-advice function class (ad-advice-name advice))
	  (ad-clear-cache function)
	  (ad-activate function -1)
	  (if (and (ad-is-active function)
                   (ad-get-cache-definition function))
	      (list (ad-get-cache-definition function)
		    (ad-get-cache-id function))))
      (ad-set-advice-info function old-advice-info)
      (advice-remove function advicefunname)
      (if advicefunname (fset advicefunname old-advice))
      (if old-advice (advice-add function :around advicefunname)))))