;;; insert-hooks using v19 advice.el
;;; Roland McGrath 6/8/93

(require 'advice)

;;;###autoload
(defun insert-hooks (function head-hook &optional tail-hook)
  "Rewrite the function specified by the first argument FUNCTION to
run hooks. 

If second argument HEAD-HOOK, a symbol referring to a named hook, is
non-nil, insert a form in FUNCTION that will run that hook before
anything else is done. 

If optional third argument TAIL-HOOK is non-nil, insert a form in
FUNCTION that will run the hook as the last thing done.

Return value is new definition of FUNCTION.

FUNCTION may be an autoloaded function, subr, or lambda-expression, but may
not be a macro."  
  (if head-hook
      (ad-add-advice function 
		     (ad-make-advice head-hook nil t
				     (` (advice lambda ()
						(, (format "\
Runs hook %s before all else."
							   head-hook))
						(run-hooks '(, head-hook)))))
		     'before 'first))
  (if tail-hook
      (ad-add-advice function 
		     (ad-make-advice tail-hook nil t
				     (` (advice lambda ()
						(, (format "\
Runs hook %s after all else."
							   tail-hook))
						(run-hooks '(, tail-hook)))))
		     'after 'last))
  (ad-activate function (byte-code-function-p (indirect-function function))))

(provide 'hook-advice)
(provide 'insert-hooks)

;; eof
