This page explains emacs library system. For example, what's the difference between library, package, features? And what's the difference between {load-file, load, require, autoload}?
Emacs lisp's module system is a primitive system, centered on loading file, with some slightly high level things such as its “features”, autoload, require. However, nothing is strict or enforced by elisp.
| Function Name | Purpose | Tech Detail | Comment |
|---|---|---|---|
load-file | Load a file. | Load one specific file. | Use this if you have one SPECIFIC file at one particular file path. |
load | Load a file. | Load a file by searching thru var load-path. Also, tries to load a compiled version (.elc) if exists. | Use this if the path for the file is not known in advance, and you are using a file name without full path, such as undo or “undo.el”, and you want to load the compiled version if it exists (undo.elc). |
require | Load a package if it has not already been loaded. | Checks the var features, if symbol is not there, then call load to load it. | Best used in elisp source code, similar to other lang's require or “import”. |
autoload | Load a file only when a function is called. | Associate 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, it's good to have your package installation go by autoload (if possible). It saves startup time. |
See also: How to Name Your Emacs Major Mode.
Emacs lisp the language does not have namespace. Everything is global, with dynamic scope, with some shadowing mechanism. So, don't expect library or module to be language defined namespace constructs like Perl or Python's module system or Java's Package system.
(note: emacs 24 added lexical scoping for variables, using a directive. It must be specified in each file, and applies to that file's let only.)
These words are used losely, and do NOT have TECHNICAL definitions in elisp.
library → elisp file(s) containing a collection of useful stuff. For example, the command comment-dwim is defined in 〔newcomment.el〕, which is a library of functions.
package → any useful elisp library for emacs users. ⁖ major mode or minor mode.
The term “module” is not used by emacs.
(provide '‹symbol name›). When this code is evaluated, emacs will add the symbol name to the features list.(require '‹symbol name›) is called, emacs check if that symbol name is already in the features list. If not, load it. (emacs will guess a file name based on the symbol name. Or, the require function may specify a file name in the 2nd argument.)For example, type 【Alt+x describe-variable Enter ↵ features Enter ↵】. You'll see something like this:
ibuffer etags ring cc-mode cc-fonts cc-menus cc-cmds cc-styles cc-align cc-engine cc-vars cc-defs xlsl-mode encoded-kb speck sgml-mode dired info newcomment desktop recentf tree-widget wid-edit advice help-fns …
A elisp file can call (provide '‹symbol name›) near the end. When that code is executed, emacs will add that symbol to the features list.
The purpose of features is for emacs to know if a package is already loaded. The features variable and the functions {provide, require}, is the mechanism. The require function checks the features to see if a symbol is already there, if not, then load the file.
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:
lisp-mode as feature, and is invoked by a command named emacs-lisp-mode.cua-base and cua as features, and is invoked by a command named cua-mode.text-mode.desktop as feature, and the command name to invoke it is desktop-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.
Isn't all this “no namespace”, “not managed” module system very bad?
Yes it is.
Though, it's just the state of software. Many most popular langs, such as C, C++, PHP, do worse. They don't have a module system neither, and they load a file by “include”. Note that even Scheme Lisp didn't have module system, until R6RS, released in 2007, and the new module system defined in it is widely criticized, and R6RS caused Scheme community to split.