Sams Teach Yourself Emacs in 24 Hours |
|||||||||||||||||||||||||||
Hour 24: Installing Emacs Add-Ons |
|||||||||||||||||||||||||||
|
There is another way to modify existing functions without using hooks, and without modifying the function itself. The advice tool is a part of Emacs that enables the user to insert a short statement in the .emacs file, which in effect gives advice to a function in order to change its behavior. This is similar to using hooks, but the advice utility is more flexible and open-ended.
Following is an example in which the default action of the switch-to-buffer function (normally called with the keybinding C-x b) is changed. If this advice is activated, the switch-to-buffer function no longer creates a new and empty buffer if the filename in the minibuffer refers to a nonexistent file; instead, the question filename does not exist, create? appears, giving the user a choice of whether to create a new buffer:
(defadvice switch-to-buffer (around confirm-non-existing-buffers activate) "Switch to non-existing buffers only upon confirmation." (interactive "BSwitch to buffer: ") (if (or (get-buffer (ad-get-arg 0)) (y-or-n-p (format "´%s' does not exist, create? "(ad-get-arg 0)))) ad-do-it))
Included in sams-lib.el is a utility that uses advice to set up an ongoing monitor of the hooks that are active in an Emacs session. To use this tracing utility, first make sure that sam-libs.el is being loaded from your .emacs file, and that the library is in your site-lisp directory or elsewhere on your load-path. The following line in the .emacs file loads the library:
(require 'sams-lib)
To activate the trace function, type M-x sams-trace-hooks. This code causes every instance of hook activation to generate a message in the minibuffer. This can be useful when you are trying new add-hook statements. You probably don't want this running all the time; to disable the monitoring, type the following command: M-x sams-stop-trace.
Exhaustive documentation and tutorials for the advice utility can be found at the beginning of the advice.el file, which is a part of every recent Emacs distribution.
Sams Teach Yourself Emacs in 24 Hours |
|||||||||||||||||||||||||||
Hour 24: Installing Emacs Add-Ons |
|||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.