ELisp: load, load-file, autoload

By Xah Lee. Date: . Last updated: .
load
(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)

Read and eval a file FILE.

  • FILE can be a full path, or relative path, name, and with or without file name extension suffix.
  • it tries to find the FILE by searching for it in a list of directory stored in variable load-path

If no file name extension suffix is given in FILE, it'll try finding it by appending several file name extension.

The order is roughly:

  1. filename.elc
  2. filename.el
  3. filename

(may include compressed files e.g. ".dll" ".dll.gz" ".elc" ".elc.gz" ".el" ".el.gz" ) see • get-load-suffixesload-suffixesload-file-rep-suffixes

This is the most primitive and general function for loading a file. All other loading file functions are calling this.

load-file
(load-file FILE)

Load one specific file.

The FILE should be a full path, including file name extension, such as .el or .elc.

(load-file filename) just calls (load (expand-file-name filename) nil nil t).
autoload
(autoload FUNCTION FILE &optional DOCSTRING INTERACTIVE TYPE)

Associates a function name with a file path, and load the file and execute the function when a function is called.

Tip: If you are writing a major mode, you should setup autoload for your package's functions. It saves startup time.

What is the difference between a Package and Library?

These words are used losely, and do NOT have TECHNICAL definitions in elisp.

package
Usually refers to elisp files useful for emacs users. E.g. Command js-mode in js.el.
library
Usually means elisp files useful to emacs lisp coders. e.g. the function seq-drop is defined in seq.el, which is a library of functions.

The term “module” is not used by emacs.

Warning: No Namespace

Note: There is no structured mapping of “package/module/library” namespace and dir/file names. Emacs lisp does not have namespace, everything is global.

Warning: Package/Library/Feature are not Managed

There is no absolute relation between any concept of package/library/feature/autoload facilities and the file name.

By convention, if a elisp file name is xx-mode.el, it OFTEN provides a lisp symbol “xx-mode” as its feature name (if it does at all), and the command to invoke the mode is OFTEN named “xx-mode”. Sometimes the “-mode” part is omitted in any of {file name, feature symbol name, command name}.

This is only a lose convention. There are a lot exceptions. For example:

All the above means, you could have a file named Joe-xx-mode_v2.1.el, which provides a feature named “abc”, while the command name to activate it may be “opq”, and it might be displayed in mode line as “OPQ helper”. And, this file can be considered as a package or library.

Reference

emacs library/package system