Emacs Lisp: load, load-file, autoload, require
here's a summary of emacs lisp file loading system.
load
is the core function to read a emacs lisp file. It takes a file name. And search load-path if the file is not full path.load-file
is a wrapper toload
. It provides some syntax shortcut.require
callsload
when the required symbol is not already loaded. [see Emacs Lisp: provide, require, features]autoload
associates a symbol name (a function) with a file name. When the function is called and it's not already found in symbol table, it callsload
to load the associated file and run the function.
load
(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)
The general function for loading a file.
If no optional argument is given, it'll try to load the FILE by searching for it in a list of directory stored in variable load-path, by first appending.elc
to the argument FILE, and if none found, it'll try appending.el
, and others, and finally try just FILE as given.
For example, if you(load "x")
, it'll try
x.elc
x.el
x
load-file
- Load one specific file.
Use this when you have a specific full path of a file in mind.
The file name argument should contain file name extension, such as
.el
or.elc
.
(load-file file_name)
just calls(load (expand-file-name file_name) nil nil t)
. autoload
(autoload FUNCTION FILE &optional DOCSTRING INTERACTIVE TYPE)
Load a file only when a function is called.autoload
associates a function name with a file path. When the function is called, load the file, and execute the function. If you are writing a major mode, have your package installation go by autoload if possible. 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
injs.el
. - library
-
Usually means elisp files useful to emacs lisp coders. e.g. the function
seq-drop
is defined inseq.el
, which is a library of functions.
The term “module” is not used by emacs.
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.
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 xyz-mode.el
, it OFTEN provides a lisp symbol “xyz-mode” as its feature name (if it does at all), and the command to invoke the mode is OFTEN named “xyz-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:
- The file
lisp-mode.el
provides the symbollisp-mode
as feature, and is invoked by a command namedemacs-lisp-mode
. - The
cua-base.el
file provides symbolscua-base
andcua
as features, and is invoked by a command namedcua-mode
. - The
text-mode.el
file does not provide any symbol for feature. It is invoked by a command namedtext-mode
. - The file
desktop.el
provides the symboldesktop
as feature, and the command name to invoke it isdesktop-save-mode
.
All the above means, you could have a file named Joe-xyz-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.