Elisp: load, load-file, autoload

By Xah Lee. Date: . Last updated: .

load, load-file, autoload

load

(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)

Eval a file of emacs lisp code.

  • FILE can be a full path or file name, and with or without file name extension suffix.
  • If FILE is a file name, emacs search it in a list of directory stored in variable load-path 〔see Elisp: Load Path
  • If FILE has no file name extension, and NOSUFFIX is nil, emacs try to find the file by trying different file name extensions.

The order is roughly:

  1. .elc
  2. .el

(may include compressed files e.g. .dll .dll.gz .elc .elc.gz .el .el.gz )

see

  • get-load-suffixes
  • load-suffixes
  • load-file-rep-suffixes

💡 TIP: best to give full path for FILE (with filename extension of .el or .elc if you want to load Byte Compile Elisp File. ), because that removes ambiguity and does not have dependency issues of implicit load path. For loading emacs lisp library, it is better to use require, see Elisp: provide, require, features.

(load "~/git/xahemacs/xahemacs_init.el" nil t t)
load-file

(load-file FILE)

like load, but does not try to auto add filename extension.

💡 TIP: this designed to load a file at fullpath FILE.

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?

The terms “Package” and “Library” do not have technical meaning in elisp.

The term “module” is not used by emacs.

Library Filename is Used as Identifing Name

in emacs lisp, for human communication, a library is usually identified by its file name. e.g. we say, the command js-mode is defined in the library “js.el”.

How to Find a Library's File Location

Alt+x find-library to locate the file path.

No Namespace

Emacs lisp does not have namespace, everything is global.

Library and Feature Names Are Not Required to Sync

There is no structured mapping of library namespace and dir name or file names. (unlike java, python)

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

Elisp, Package System

Elisp, writing a major mode. Essentials