The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

Поиск:  Каталог документации

Next Previous Contents

4. Customizing Emacs

Virtually all Emacs customization is done via Lisp code. You can modify variables which influence the way Emacs operates or you can add new functions to Emacs (or override existing functions--replacing them with your own).

4.1 Temporary Customization

While experimenting with Emacs customization, you'll probably want to do it in a way that is temporary. If you do something horribly wrong, you can just C-x C-c to exit emacs and run it again. Once you've figured out what changes you'd like to make permanent, you can add them to your very own .emacs file so that they take effect every time you start Emacs. This is discussed in the next section.

Variable Assignments

The easiest customizations are accomplished by changing the value of a variable in Emacs. The list code to do this looks like this:

(setq variable-name new-value)

Where variable-name is the name of the variable and new-value is the value you'd like to give the variable. (In Lisp-speak, you're binding a variable to a value.) The setq function in lisp is analogous to the assignment operators (usually =) in other programming languages.

NOTE: I'm glossing over many details here for the sake of simplicity. You may also see me or others use the Lisp functions set and even setq-default. If you're really curious, feel free to look them up in an Emacs Lisp reference.

Let's look at a line from my .emacs file

(setq-default transient-mark-mode t)

The variable transient-mark-mode controls whether or not a region becomes highlighted when I mark it. In many GUI applications, if you click and drag the mouse to select a range of text it becomes hi-lighted in reverse video or some other color. Emacs will do the same thing it the transient-mark-mode variable is set (to a non-nil value).

A WHAT value?

Okay. Brief digression. Most programming languages have some notion of true/false values. In C/C++ a value is considered true if it is a non-zero value. In Perl, a non-null or non-zero value is true. In Lisp, the same idea applies but the names and symbols are different.

True is usually written as t and false (or null) is written as nil. Like in other languages, though, any non-nill value is considered true.

To get the full description of what transient-mark-mode does, you can use the on-line help. Type C-h v or M-x describe-variable and then transient-mark-mode. If you're lazy like me, you can take advantage of variable name completion using the Tab key. Just type part of the variable name and hit the Tab key. If you've typed enough of it that Emacs can already uniquely identify it, you'll see the whole name completed for you.

Another variable that folks often set is fill-column. It tells Emacs how wide the screen should be for the purposes of word-wrapping (and auto-fill-mode respects this value). To set the value to something absurd, you could type:

(setq fill-column 20)

But that won't actually do anything. You need to tell Emacs to evaluate the expression you typed. To do so, put the point (cursor) at the end of the expression end then type C-x C-e, which calls the function eval-last-sexp in case you care. When you do that, notice that 20 (or whatever value you used) is echoed back to you in the mini-buffer at the bottom of the screen. That's just the return value from the expression you evaluated.

Just to prove that it works, type a sentence or two. If you happen to have auto-fill-mode enabled (you probably don't), you'll notice the text wrapping at the 20 column mark. Otherwise, after you've typed some stuff, type M-q which calls the function fill-paragraph. It will then perform the word wrapping.

File Associations

You can configure Emacs to automatically do something when you open a file of a particular type (just like some GUIs will automatically launch a specific application if you click on the icon for a particular file). For example, I may want Emacs to automatically switch to text-mode every time I open a file with a .txt extension. Well, that already happens. :-) So let's tell Emacs to always enter text-mode when you open a file named ``README''.

(setq auto-mode-alist (cons '("README" . text-mode) auto-mode-alist))

Huh?

Without diving into lots of Lisp programming that you really don't need to know (but it wouldn't hurt you to learn), let just say that the variable auto-mode-alist contains a list of pairs. Each pair contains a regular expression and an Emacs mode name. If a file you open matches the regular expression (in this case, the string README) Emacs starts the mode you specified.

The funny syntax above is because we're actually adding another pair to that mode list. You wouldn't want to just assign to auto-mode-alist without making sure the values that it already contains aren't lost.

And if I wanted Emacs to automatically switch to html-helper-mode every time that I opened a file that ended with .html or .htm, I would add this to my .emacs file:

(setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.htm$" . html-helper-mode) auto-mode-alist))

The possibilities are truly endless.

4.2 Using a .emacs File

After you've spent some time with Emacs and have a basic idea of what customization can do for you, you'll probably want to customize a few things permanently (or at least until you change your mind). If you find yourself using Emacs on a daily basis, you'll also notice that your .emacs file get bigger as time goes on. That's a Good Thing because it means you've figured out how to make Emacs work the way you want it do work. It's a shame that more software products don't let you do that.

In case you haven't already guessed, every time you start Emacs, it looks for a file named .emacs in your home directory. Your .emacs file is where you should put any Lisp code that you want run automatically and that includes the sort of customization we've been dealing with here.

Another example from my .emacs file:

(setq inhibit-startup-message t)

The inhibit-startup-message variable controls whether or not Emacs displays that welcome message when it starts. After a while, I got sick of looking at it (because I knew how to find the help and whatnot), so I went in search of a way to turn it off.

As an exercise, try creating a .emacs file of your own and add that line to it. Then exit and start Emacs again. You should not see the welcome message.

Often times when your read about an Emacs mode (or a package), the documentation will suggest some code to add to your .emacs file in order to make the mode or package work in a particular way.

The GNU Emacs FAQ (C-h F) contains some items related to .emacs files that you might find useful.

4.3 The Customize Package

As Emacs has grown in popularity and continued to evolved, someone eventually said ``there has to be a better way to let novice users customize their Emacs.'' And customize was born.

Customize provides a more intuitive method of customizing parts of Emacs. To try it out, either visit the Customize sub-menu in your Help menu, or type M-x customize.

Customize groups customization into logical groups like ``Editing'', ``Programming'', ``Files'', and so on. Some groups contain sub-groups.

If you make changes using the customize interface, Emacs will save the changes to your .emacs file. That's rather handy, because you can easily inspect (and change) the changes it made for you.

I don't use the Customize interface, so I can't say much more about it..

4.4 X Windows Display

Like any well behaved X application, Emacs respects your X resources. That means you can control the initial colors, geometry, and other X specific things just as you could with an xterm, nxterm, or whatever.

Here's the relevant bit of my ~/.Xdefaults file:

emacs*Background: DarkSlateGray
emacs*Foreground: Wheat
emacs*pointerColor: Orchid
emacs*cursorColor: Orchid
emacs*bitmapIcon: on
emacs*font: fixed
emacs.geometry: 80x25

See your X manual page for more details about X resources.

Chris Gray ( cgray4@po-box.mcgill.ca) also notes:

In Debian, the ~/.Xdefaults doesn't seem to be used. However, Debian people can put what you have given in /etc/X11/Xresources/emacs and they can have the pretty colors that they had when they were using RedHat.


Next Previous Contents


Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру