Elisp: load, load-file, autoload
load, load-file, autoload
load
-
(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)
Read and eval a elisp 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 〔see Elisp: 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:
filename.elc
filename.el
filename
(may include compressed files e.g. ".dll" ".dll.gz" ".elc" ".elc.gz" ".el" ".el.gz" ) see •
get-load-suffixes
• load-suffixes • load-file-rep-suffixesThis 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 meaning 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.
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:
- 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-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 package system
Emacs lisp, writing a major mode. Essentials
- Elisp: Write a Major Mode for Syntax Coloring
- Elisp: Font Lock Mode
- Elisp: Syntax Color Comments
- Elisp: Write Comment/Uncomment Command
- Elisp: Keyword Completion
- Elisp: Create Keymap (keybinding)
- Elisp: Create Function Templates
- Elisp: Command to Lookup Doc
- Elisp: Create a Hook
- Elisp: Major Mode Names
- Elisp: provide, require, features
- Elisp: load, load-file, autoload
- Elisp: Syntax Table