Navigation: Table of Contents, Index, next: SQL mode, prev: .emacs file by Alex, up: Emacs

Elisp Installation HOWTO

Overview:

If you want to install elisp files, you must do two things:

  1. Put the files in a directory Emacs can find
  2. Tell Emacs when to load the file

The directories Emacs searches through: load-path

The directories that Emacs searches through when an elisp file is to be loaded are stored in the load-path variable. This shows you the current load-path:

C-h v load-path RET.

If one the the directories in load-path seems adequate, put the elisp files in that directory. Often Emacs users have an ~/elisp directory where they put elisp files collected over the years. If you would like that, create the ~/elisp directory and add it to the load-path. You can create the directory from Emacs:

M-x make-directory RET ~/elisp RET.

Once you have the directory, you must put some code at the beginning of your ~/.emacs files that adds the directory to load-path:

(add-to-list 'load-path "~/elisp")

If you want to get started right away, you have to evaluate the command that adds the new directory to load-path. Place point at the end of the code you just inserted into your .emacs file and type C-x C-e to evaluate it right away. This will not be necessary in future sessions because Emacs automatically runs all the code in your .emacs file.

When to Load Elisp Files

Elisp files should be loaded on two occasions:

  1. When you use a function from the elisp file
  2. When you edit a file that uses a mode defined in said elisp file

Loading a file when a function is used: autoload

The first possibility is covered by the autoload function. Here is an example which tells Emacs to load the file sql-mode as soon as you use the sql-mode function:

(autoload 'sql-mode "sql" "SQL mode." t)

The first argument is the function, the second argument is the file (without .el or .elc), the third argument is rudimentary documentation, and the fourth argument tells Emacs that the function is interactive. This means that you can now type M-x sql-mode RET; this will automatically load the first sql.elc or sql.el file from your load-path.

Such autoload statements must be added to your ~/.emacs file, of course.

Installing a new mode for a file

Now for the editing of files: Emacs uses the variable auto-mode-alist to determine which mode to use for which files. Every entry in this list is composed of a regular expression matching the files affected and the function to call before editing such a file.

The entry ("\\.sql$" . sql-mode) in auto-mode-alist, for example, tells Emacs to use the function sql-mode for all files ending in ".sql". The way to add such an element to auto-mode-alist is to add the following statement to your ~/.emacs file:

(add-to-list 'auto-mode-alist '("\\.sql$" . sql-mode))

In combination with the autoload statement above, editing a file with the ".sql" extension will therefore call sql-mode, and that will load the first sql.elc or sql.el file from your load-path. Et voilą!


Navigation: Top, Table of Contents, Index, next: SQL mode, prev: .emacs file by Alex, up: Emacs


http://www.oocities.org/kensanata/emacs-pkg-install-howto.html / Alex Schroeder <kensanata@yahoo.com> / updated: 2001-02-15 / significant changes: 2000-07-29