Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.27 MB, 620 trang )
Appendix A: Installing Functions and Packages from the CD: bbdb.el
Sections in this Hour:
sams-lib.el
pager.el
igrep.el
template.el
cust-stub.el
flyspell.el
folding.el
bbdb.el
Previous Section Next Section
© Copyright Macmillan USA. All rights reserved.
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs225.htm (2 of 2) [4/06/2004 11:20:29 PM]
Hour 24: Installing Emacs Add-Ons: Controlling Options
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
Controlling Options
Many add-ons have options that can be set or changed. Newer add-on packages often have a Customize interface,
which is the easiest method. Otherwise, you set the user-configurable options by including statements in your .emacs
file. The Commentary section of the add-on Lisp file contains examples of the .emacs lines that are needed to change
these options. In this section you can also find the mode's location in the Customize menu.
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
© Copyright Macmillan USA. All rights reserved.
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs210.htm [4/06/2004 11:20:30 PM]
Previous Section Next Section
Hour 24: Installing Emacs Add-Ons: Summary
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
Summary
This hour showed you how to install many of the add-on Emacs modes. Procedures for customizing modes were
outlined. Several add-ons were described, and Internet links to the sources listed. Emacs by itself is a powerful editor,
but using these add-ons enables you to tailor it to your needs (even though you never need all the available extensions).
Try several of them, and eventually you will find a few that suit your editing style--and you will end up with a
customized editor that, in effect, you have designed yourself.
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
© Copyright Macmillan USA. All rights reserved.
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs214.htm [4/06/2004 11:20:30 PM]
Previous Section Next Section
Hour 24: Installing Emacs Add-Ons: Installing an Add-On
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
Installing an Add-On
Location and Paths
A Sample Installation
Emacs is designed in such a way that new add-ons can be easily installed. Further steps are needed for you to make use
of them. Various types of lines are added to the .emacs startup file, which either load an add-on for the duration of the
session or make it available when it is needed.
Location and Paths
When Emacs is first installed, an empty directory is created just for add-on modes. It's called the site-lisp directory, and
its location varies depending on which operating system you use. If you install many Emacs extensions, this directory
can become crowded, making it difficult to keep track of what add-ons have been installed. This is especially true when
large multifile packages, such as the W3 Web browser or AucTeX, are installed. One method of organizing the files is
to keep them in separate directories and tell Emacs where they are. A statement in your .emacs file such as
(setq load-path (cons directory load-path))
causes Emacs to look for files to load in your new directory before searching the standard Emacs Lisp directories. If
you prefer that Emacs search its native directories first, use the following statement instead:
(setq load-path (append load-path (list directory)))
The first of these examples is used when a newer version of a default Emacs mode has been installed and you want it to
be used instead the default mode.
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs207.htm (1 of 2) [4/06/2004 11:20:31 PM]
Hour 24: Installing Emacs Add-Ons: Installing an Add-On
A Sample Installation
The easiest way to understand how this works is to copy one of the add-on files that is on the CD-ROM to your sitelisp directory. Cyclebuffer.el is a short file that is used here as an example (see Figure 24.1). Open the file with Emacs,
and then look for the commented-out section near the top of the file.
Figure 24.1
Cyclebuffer.el commentary.
You can skip the very first part because in most cases it contains licensing information. The information that you need
is normally in a section that begins with Commentary:. A brief description of the mode's purpose is usually followed by
lines that are to be inserted into your .emacs file; in this case, you see the following four lines:
(autoload 'cyclebuffer-forward "cyclebuffer" "cycle forward" t)
(autoload 'cyclebuffer-backward "cyclebuffer" "cycle backward" t)
(global-set-key "\M-N" 'cyclebuffer-forward)
(global-set-key "\M-P" 'cyclebuffer-backward)
If you copy these lines directly from the file on the CD-ROM, be sure to delete the semicolons at the beginning of each
line; otherwise Emacs ignores them when it is loading the .emacs file.
After you have copied the file into a directory on your load-path and added the required lines to your .emacs file, you
are ready to try the new mode. Save your .emacs file, and then type M-x load-file. When you are prompted in the
minibuffer, type ~/.emacs; this causes Emacs to reread the file. Next, load several files. When you type either M-P or
M-N, cyclebuffer is automatically loaded and you can use the keybindings to quickly cycle through the available
buffers.
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
© Copyright Macmillan USA. All rights reserved.
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs207.htm (2 of 2) [4/06/2004 11:20:31 PM]
Hour 24: Installing Emacs Add-Ons: Hooks
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
Hooks
Binding Keys With Hooks
Hooks are often provided for add-on Lisp files. Hooks are aptly named--they are functions that an Emacs process can
"hook" into in order for a user to change the default behavior. An example might make this clearer:
(add-hook 'text-mode-hook 'turn-on-auto-fill)
By default, Emacs doesn't automatically wrap lines in text- mode. Adding the preceding lines to your .emacs file causes
the function turn-on-auto-fill to be automatically loaded along with text-mode. When this hook is active, lines are
automatically wrapped as you type. To try out a hook without including it in your .emacs file, type the line (complete
with parentheses) into the Scratch buffer and then type C-x C-e, which is the keybinding for Evaluate LastSExpression. The hook is then active until you exit Emacs.
Hooks are also used when you want to load two modes simultaneously. For example, if you want outline-minor-mode
to be loaded along with AucTeX when you are editing TeX files, the following line is needed in your .emacs file:
(add-hook 'LaTeX-mode-hook '(lambda () (outline-minor-mode1)))
Using the lambda statement is a method of creating a nameless function (see Hour 22, "Learning Lisp Basics"); in this
case, it causes outline-minor-mode to be loaded along with AucTeX's LaTeX mode. You didn't need to use lambda in
the earlier examples because turn-on-auto-fill is already a function, whereas outline-minor-mode is a mode. In order for
outline-minor-mode to be used in an add-hook statement, it is necessary to wrap it in a function definition.
Hooks can also be used to choose between several available options. CC-mode is a major mode that is used for editing
C and C++ source-code files. The mode can be set to use one of several different coding styles with the following
statement:
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs211.htm (1 of 3) [4/06/2004 11:20:32 PM]
Hour 24: Installing Emacs Add-Ons: Hooks
(add-hook 'c-mode-hook
' (lambda ()
(c-set-style "gnu")))
Many modes have their own built-in hooks, which you normally don't want to disable. The add-hook statement leaves
existing hooks active while adding an additional hook.
Binding Keys With Hooks
Hook statements are often used to change the keybindings of a mode, most of which have predefined keymaps.
Everyone has different editing habits and needs, so the most convenient and comfortable keys differ from user to user.
Using mode-hooks ensures that the new keybindings only take effect when the particular mode is in use. Following are
some examples:
(add-hook 'notes-mode-hook
' (lambda ()
(define-key notes-mode-map [f5] 'notes-follow-prev-link)
(define-key notes-mode-map [f6] 'notes-follow-next-link)))
This example changes two multiple-key bindings to an adjacent pair of unused function keys. This speeds up two oftenused commands in notes-mode. This mode is briefly described at the end of the hour:
(setq mail-mode-hook
' (lambda ()
(define-key mail-mode-map "\C-cm" 'mail-send-and-exit)
(define-key mail-mode-map "\C-cc" 'mail-cc)
('turn-on-auto-fill)
(setq fill-column 65)))
In this example, two keybindings have been changed to shorter key sequences, auto-fill has been turned on, and the fill
column has been narrowed to 65. Mail-mode doesn't have a predefined hook, so the setq statement is used to create
one. Any mail-mode hook statements that follow this one in the .emacs file can use the add-hook function rather than
setq.
Some add-ons have special provisions for changing the keybindings. For example, Folding-mode includes a function
called fold-default-keys-function, which simplifies the alteration of keybindings. A function such as this in your .emacs
file changes just one of the keybindings:
defun my-fold-bind-keys ()
"My favorite fold key settings"
;; - I want the default ones, but I want fold-whole-buffer and
;;
fold-open-buffer to be close together, instead of the
;;
default
;; - read them all.
;;
(fold-default-keys)
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs211.htm (2 of 3) [4/06/2004 11:20:32 PM]
Hour 24: Installing Emacs Add-Ons: Hooks
;; - first disable old bind and define new one.
;;
(define-key folding-mode-map "\C-c\C-w" nil)
(define-key folding-mode-map "\C-c\C-p" 'fold-whole-buffer)
)
This function first sets the keymap to the default with the (fold-default-keys) statement. Next, the default keybinding
for fold-whole-buffer is disabled and a new binding (C-c C-p) is substituted.
The following line assigns the new my-fold-bind-keys function as the new default:
setq fold-default-keys-function 'my-fold-bind-keys)
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
© Copyright Macmillan USA. All rights reserved.
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs211.htm (3 of 3) [4/06/2004 11:20:32 PM]
Hour 24: Installing Emacs Add-Ons: Q&A
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
Q&A
Q What function is used to create a mode-hook when the hook isn't provided by the mode?
A The setq function.
Q Can more than one add-hook statement for the same mode be used in the .emacs file?
A Yes, new hooks can be added at any time.
Q What effect does byte-compiling a Lisp file have?
A A byte-compiled Lisp file is more compact and loads faster, but in this form it is no longer readable as a text
file.
Q What is the difference between autoloading a file and loading it with load-library?
A An autoloaded file isn't actually loaded until it is needed, whereas a file loaded with load-library is loaded
into memory from the start.
Q What happens when a file is loaded with require?
A The require function only loads a file if it hasn't already been loaded.
Sams Teach Yourself Emacs in 24 Hours
Hour 24: Installing Emacs Add-Ons
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs215.htm (1 of 2) [4/06/2004 11:20:33 PM]
Contents Index
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons: Q&A
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
© Copyright Macmillan USA. All rights reserved.
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs215.htm (2 of 2) [4/06/2004 11:20:33 PM]
Hour 24: Installing Emacs Add-Ons: Loading Techniques
Contents Index
Sams Teach Yourself Emacs in 24 Hours
Previous Hour Next Hour
Hour 24: Installing Emacs Add-Ons
Sections in this Hour:
Simple and Complex Add-Ons
Controlling Options
Summary
Installing an Add-On
Hooks
Q&A
Loading Techniques
Adapting Functions
Exercises
Filenames and Interpreters
A Sampling of Emacs Packages
Previous Section Next Section
Loading Techniques
Autoloading is the most common method of activating an add-on mode. This method is very efficient because the
actual Lisp code is not loaded until the functions in the mode are actually needed. When Emacs starts up and reads
your .emacs file, it deals with autoload statements (such as the two in the preceding example) not by loading the Lisp
file, but by remembering the name of the file in case it is needed during the editing session.
There are some Emacs add-ons that are intended to be activated when Emacs starts and remain active throughout the
session. The load-library function loads a Lisp file, and a similar function called require loads a file only if it hasn't
already been loaded. These two statements are used as follows:
(load-library 'name)
(require 'name)
In the preceding examples, name is the filename of the Lisp file without the .el or .elc extension.
Another function that you will occasionally encounter is provide. provide is used when you need to make a specific
feature in a Lisp file available. The statement looks similar to the following:
(provide 'name)
Sams Teach Yourself Emacs in 24 Hours
Hour 24: Installing Emacs Add-Ons
file:///C|/Documents%20and%20Settings/win1/Desktop/b/emacs208.htm (1 of 2) [4/06/2004 11:20:34 PM]
Contents Index
Previous Hour Next Hour