Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 24: Installing Emacs Add-Ons |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

## Adapting Functions

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.

Tip - Any time you edit your .emacs file, it is possible to accidentally leave out a parenthesis or to make a typographical error. When you start up Emacs it tries to load the .emacs file; if there are any errors, you see the following message in the minibuffer: Error in init file. Emacs won't load the remainder of the file, so whatever customizations you have made following the error in the file are ignored. One way to avoid this is to select any new lines that you have added and run Evaluate Region, which is a command that can be executed from the Emacs-Lisp menu. This enables you to spot the error early and correct it. It's also a good idea to keep a copy of a functional .emacs file in another directory as a backup.

Tip - If you want a new add-on to run as quickly as possible, don't forget to byte-compile the file or files. Just open the file in an Emacs session, and then select Byte-compile This File in the Emacs-Lisp menu. A new file with the extension .elc is created. Emacs uses these byte-compiled files rather than the .el files, if they exist.

Tip - If you have installed several Emacs add-ons, there are probably some that you find more useful than others. If you decide that you don't need an add-on, it's advisable to disable it in your .emacs file. Just comment out the lines that load the package. Modes that don't get used waste memory and cause Emacs to start up slowly, especially if they are loaded with load-library rather than autoload.

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 24: Installing Emacs Add-Ons |  | Previous HourNext Hour
Sections in this Hour: |  |  |  |

© Copyright Macmillan USA. All rights reserved.