comparison lisp/emacs-lisp/advice.el @ 26622:019c7ea59fd9

Many doc fixes.
author Richard M. Stallman <rms@gnu.org>
date Fri, 26 Nov 1999 17:46:05 +0000
parents f96b2bc4ef08
children 173f907a5812
comparison
equal deleted inserted replaced
26621:5df1b3f7ed3a 26622:019c7ea59fd9
1 ;;; advice.el --- an overloading mechanism for Emacs Lisp functions 1 ;;; advice.el --- an overloading mechanism for Emacs Lisp functions
2 2
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc. 3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
4 4
5 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu> 5 ;; Author: Hans Chalupsky <hans@cs.buffalo.edu>
6 ;; Maintainer: FSF
6 ;; Created: 12 Dec 1992 7 ;; Created: 12 Dec 1992
7 ;; Version: advice.el,v 2.14 1994/08/05 03:42:04 hans Exp
8 ;; Keywords: extensions, lisp, tools 8 ;; Keywords: extensions, lisp, tools
9 9
10 ;; This file is part of GNU Emacs. 10 ;; This file is part of GNU Emacs.
11 11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify 12 ;; GNU Emacs is free software; you can redistribute it and/or modify
3536 (ad-set-cache function (symbol-function function) nil) 3536 (ad-set-cache function (symbol-function function) nil)
3537 (ad-set-cache 3537 (ad-set-cache
3538 function (symbol-function function) (ad-make-cache-id function))))) 3538 function (symbol-function function) (ad-make-cache-id function)))))
3539 3539
3540 (defun ad-handle-definition (function) 3540 (defun ad-handle-definition (function)
3541 "Handles re/definition of an advised FUNCTION during de/activation. 3541 "Handle re/definition of an advised FUNCTION during de/activation.
3542 If FUNCTION does not have an original definition associated with it and 3542 If FUNCTION does not have an original definition associated with it and
3543 the current definition is usable, then it will be stored as FUNCTION's 3543 the current definition is usable, then it will be stored as FUNCTION's
3544 original definition. If no current definition is available (even in the 3544 original definition. If no current definition is available (even in the
3545 case of undefinition) nothing will be done. In the case of redefinition 3545 case of undefinition) nothing will be done. In the case of redefinition
3546 the action taken depends on the value of `ad-redefinition-action' (which 3546 the action taken depends on the value of `ad-redefinition-action' (which
3580 3580
3581 ;; @@ The top-level advice interface: 3581 ;; @@ The top-level advice interface:
3582 ;; ================================== 3582 ;; ==================================
3583 3583
3584 (defun ad-activate (function &optional compile) 3584 (defun ad-activate (function &optional compile)
3585 "Activates all the advice information of an advised FUNCTION. 3585 "Activate all the advice information of an advised FUNCTION.
3586 If FUNCTION has a proper original definition then an advised 3586 If FUNCTION has a proper original definition then an advised
3587 definition will be generated from FUNCTION's advice info and the 3587 definition will be generated from FUNCTION's advice info and the
3588 definition of FUNCTION will be replaced with it. If a previously 3588 definition of FUNCTION will be replaced with it. If a previously
3589 cached advised definition was available, it will be used. 3589 cached advised definition was available, it will be used.
3590 The optional COMPILE argument determines whether the resulting function 3590 The optional COMPILE argument determines whether the resulting function
3620 (t (ad-deactivate function))))))))) 3620 (t (ad-deactivate function)))))))))
3621 3621
3622 (defalias 'ad-activate-on 'ad-activate) 3622 (defalias 'ad-activate-on 'ad-activate)
3623 3623
3624 (defun ad-deactivate (function) 3624 (defun ad-deactivate (function)
3625 "Deactivates the advice of an actively advised FUNCTION. 3625 "Deactivate the advice of an actively advised FUNCTION.
3626 If FUNCTION has a proper original definition, then the current 3626 If FUNCTION has a proper original definition, then the current
3627 definition of FUNCTION will be replaced with it. All the advice 3627 definition of FUNCTION will be replaced with it. All the advice
3628 information will still be available so it can be activated again with 3628 information will still be available so it can be activated again with
3629 a call to `ad-activate'." 3629 a call to `ad-activate'."
3630 (interactive 3630 (interactive
3649 "Update advised definition of: " 'ad-is-active))) 3649 "Update advised definition of: " 'ad-is-active)))
3650 (if (ad-is-active function) 3650 (if (ad-is-active function)
3651 (ad-activate function compile))) 3651 (ad-activate function compile)))
3652 3652
3653 (defun ad-unadvise (function) 3653 (defun ad-unadvise (function)
3654 "Deactivates FUNCTION and then remove all its advice information. 3654 "Deactivate FUNCTION and then remove all its advice information.
3655 If FUNCTION was not advised this will be a noop." 3655 If FUNCTION was not advised this will be a noop."
3656 (interactive 3656 (interactive
3657 (list (ad-read-advised-function "Unadvise function: "))) 3657 (list (ad-read-advised-function "Unadvise function: ")))
3658 (cond ((ad-is-advised function) 3658 (cond ((ad-is-advised function)
3659 (if (ad-is-active function) 3659 (if (ad-is-active function)
3661 (ad-clear-orig-definition function) 3661 (ad-clear-orig-definition function)
3662 (ad-set-advice-info function nil) 3662 (ad-set-advice-info function nil)
3663 (ad-pop-advised-function function)))) 3663 (ad-pop-advised-function function))))
3664 3664
3665 (defun ad-recover (function) 3665 (defun ad-recover (function)
3666 "Try to recover FUNCTION's original definition and unadvises it. 3666 "Try to recover FUNCTION's original definition, and unadvise it.
3667 This is more low-level than `ad-unadvise' because it does not do any 3667 This is more low-level than `ad-unadvise' in that it does not do
3668 deactivation which might run hooks and get into other trouble. 3668 deactivation, which might run hooks and get into other trouble."
3669 Use in emergencies." 3669 Use in emergencies."
3670 ;; Use more primitive interactive behavior here: Accept any symbol that's 3670 ;; Use more primitive interactive behavior here: Accept any symbol that's
3671 ;; currently defined in obarray, not necessarily with a function definition: 3671 ;; currently defined in obarray, not necessarily with a function definition:
3672 (interactive 3672 (interactive
3673 (list (intern 3673 (list (intern
3678 (ad-clear-orig-definition function))) 3678 (ad-clear-orig-definition function)))
3679 (ad-set-advice-info function nil) 3679 (ad-set-advice-info function nil)
3680 (ad-pop-advised-function function)))) 3680 (ad-pop-advised-function function))))
3681 3681
3682 (defun ad-activate-regexp (regexp &optional compile) 3682 (defun ad-activate-regexp (regexp &optional compile)
3683 "Activates functions with an advice name containing a REGEXP match. 3683 "Activate functions with an advice name containing a REGEXP match.
3684 This activates the advice for each function
3685 that has at least one piece of advice whose name includes a match for REGEXP.
3684 See `ad-activate' for documentation on the optional COMPILE argument." 3686 See `ad-activate' for documentation on the optional COMPILE argument."
3685 (interactive 3687 (interactive
3686 (list (ad-read-regexp "Activate via advice regexp: ") 3688 (list (ad-read-regexp "Activate via advice regexp: ")
3687 current-prefix-arg)) 3689 current-prefix-arg))
3688 (ad-do-advised-functions (function) 3690 (ad-do-advised-functions (function)
3689 (if (ad-find-some-advice function 'any regexp) 3691 (if (ad-find-some-advice function 'any regexp)
3690 (ad-activate function compile)))) 3692 (ad-activate function compile))))
3691 3693
3692 (defun ad-deactivate-regexp (regexp) 3694 (defun ad-deactivate-regexp (regexp)
3693 "Deactivates functions with an advice name containing REGEXP match." 3695 "Deactivate functions with an advice name containing REGEXP match.
3696 This deactivates the advice for each function
3697 that has at least one piece of advice whose name includes a match for REGEXP."
3694 (interactive 3698 (interactive
3695 (list (ad-read-regexp "Deactivate via advice regexp: "))) 3699 (list (ad-read-regexp "Deactivate via advice regexp: ")))
3696 (ad-do-advised-functions (function) 3700 (ad-do-advised-functions (function)
3697 (if (ad-find-some-advice function 'any regexp) 3701 (if (ad-find-some-advice function 'any regexp)
3698 (ad-deactivate function)))) 3702 (ad-deactivate function))))
3699 3703
3700 (defun ad-update-regexp (regexp &optional compile) 3704 (defun ad-update-regexp (regexp &optional compile)
3701 "Update functions with an advice name containing a REGEXP match. 3705 "Update functions with an advice name containing a REGEXP match.
3706 This reactivates the advice for each function
3707 that has at least one piece of advice whose name includes a match for REGEXP.
3702 See `ad-activate' for documentation on the optional COMPILE argument." 3708 See `ad-activate' for documentation on the optional COMPILE argument."
3703 (interactive 3709 (interactive
3704 (list (ad-read-regexp "Update via advice regexp: ") 3710 (list (ad-read-regexp "Update via advice regexp: ")
3705 current-prefix-arg)) 3711 current-prefix-arg))
3706 (ad-do-advised-functions (function) 3712 (ad-do-advised-functions (function)
3707 (if (ad-find-some-advice function 'any regexp) 3713 (if (ad-find-some-advice function 'any regexp)
3708 (ad-update function compile)))) 3714 (ad-update function compile))))
3709 3715
3710 (defun ad-activate-all (&optional compile) 3716 (defun ad-activate-all (&optional compile)
3711 "Activates all currently advised functions. 3717 "Activate all currently advised functions.
3712 See `ad-activate' for documentation on the optional COMPILE argument." 3718 See `ad-activate' for documentation on the optional COMPILE argument."
3713 (interactive "P") 3719 (interactive "P")
3714 (ad-do-advised-functions (function) 3720 (ad-do-advised-functions (function)
3715 (ad-activate function compile))) 3721 (ad-activate function compile)))
3716 3722
3717 (defun ad-deactivate-all () 3723 (defun ad-deactivate-all ()
3718 "Deactivates all currently advised functions." 3724 "Deactivate all currently advised functions."
3719 (interactive) 3725 (interactive)
3720 (ad-do-advised-functions (function) 3726 (ad-do-advised-functions (function)
3721 (ad-deactivate function))) 3727 (ad-deactivate function)))
3722 3728
3723 (defun ad-update-all (&optional compile) 3729 (defun ad-update-all (&optional compile)
3726 (interactive "P") 3732 (interactive "P")
3727 (ad-do-advised-functions (function) 3733 (ad-do-advised-functions (function)
3728 (ad-update function compile))) 3734 (ad-update function compile)))
3729 3735
3730 (defun ad-unadvise-all () 3736 (defun ad-unadvise-all ()
3731 "Unadvises all currently advised functions." 3737 "Unadvise all currently advised functions."
3732 (interactive) 3738 (interactive)
3733 (ad-do-advised-functions (function) 3739 (ad-do-advised-functions (function)
3734 (ad-unadvise function))) 3740 (ad-unadvise function)))
3735 3741
3736 (defun ad-recover-all () 3742 (defun ad-recover-all ()
3737 "Recovers all currently advised functions. Use in emergencies." 3743 "Recover all currently advised functions. Use in emergencies.
3744 To recover a function means to try to find its original (pre-advice)
3745 definition, and delete all advice.
3746 This is more low-level than `ad-unadvise' in that it does not do
3747 deactivation, which might run hooks and get into other trouble."
3738 (interactive) 3748 (interactive)
3739 (ad-do-advised-functions (function) 3749 (ad-do-advised-functions (function)
3740 (condition-case nil 3750 (condition-case nil
3741 (ad-recover function) 3751 (ad-recover function)
3742 (error nil)))) 3752 (error nil))))
3943 (ad-safe-fset 'ad-activate-internal 'ad-activate) 3953 (ad-safe-fset 'ad-activate-internal 'ad-activate)
3944 (ad-enable-advice 'documentation 'after 'ad-advised-docstring) 3954 (ad-enable-advice 'documentation 'after 'ad-advised-docstring)
3945 (ad-activate 'documentation 'compile)) 3955 (ad-activate 'documentation 'compile))
3946 3956
3947 (defun ad-stop-advice () 3957 (defun ad-stop-advice ()
3948 "Stops the automatic advice handling magic. 3958 "Stop the automatic advice handling magic.
3949 You should only need this in case of Advice-related emergencies." 3959 You should only need this in case of Advice-related emergencies."
3950 (interactive) 3960 (interactive)
3951 ;; Advising `ad-activate-internal' means death!! 3961 ;; Advising `ad-activate-internal' means death!!
3952 (ad-set-advice-info 'ad-activate-internal nil) 3962 (ad-set-advice-info 'ad-activate-internal nil)
3953 (ad-disable-advice 'documentation 'after 'ad-advised-docstring) 3963 (ad-disable-advice 'documentation 'after 'ad-advised-docstring)