16.9 Loading Files

Many people in the GNU Emacs community have written extensions to Emacs. As time goes by, these extensions are often included in new releases. For example, the Calendar and Diary packages are now part of the standard GNU Emacs, as is Calc.

You can use a load command to evaluate a complete file and thereby install all the functions and variables in the file into Emacs. For example:

(load "~/emacs/slowsplit")

This evaluates, i.e., loads, the slowsplit.el file or if it exists, the faster, byte compiled slowsplit.elc file from the emacs sub-directory of your home directory. The file contains the function split-window-quietly, which John Robinson wrote in 1989.

The split-window-quietly function splits a window with the minimum of redisplay. I installed it in 1989 because it worked well with the slow 1200 baud terminals I was then using. Nowadays, I only occasionally come across such a slow connection, but I continue to use the function because I like the way it leaves the bottom half of a buffer in the lower of the new windows and the top half in the upper window.

To replace the key binding for the default split-window-vertically, you must also unset that key and bind the keys to split-window-quietly, like this:

(global-unset-key "\C-x2")
(global-set-key "\C-x2" 'split-window-quietly)

If you load many extensions, as I do, then instead of specifying the exact location of the extension file, as shown above, you can specify that directory as part of Emacs’s load-path. Then, when Emacs loads a file, it will search that directory as well as its default list of directories. (The default list is specified in paths.h when Emacs is built.)

The following command adds your ~/emacs directory to the existing load path:

;;; Emacs Load Path
(setq load-path (cons "~/emacs" load-path))

Incidentally, load-library is an interactive interface to the load function. The complete function looks like this:

(defun load-library (library)
  "Load the Emacs Lisp library named LIBRARY.
This is an interface to the function `load'.  LIBRARY is searched
for in `load-path', both with and without `load-suffixes' (as
well as `load-file-rep-suffixes').

See Info node `(emacs)Lisp Libraries' for more details.
See `load-file' for a different interface to `load'."
   (list (completing-read "Load library: "
                          (apply-partially 'locate-file-completion-table
  (load library))

The name of the function, load-library, comes from the use of “library” as a conventional synonym for “file”. The source for the load-library command is in the files.el library.

Another interactive command that does a slightly different job is load-file. See Libraries of Lisp Code for Emacs in The GNU Emacs Manual, for information on the distinction between load-library and this command.