Search code examples
elispautoload

How autoload works?


Assume I have 3 functions in a packages:

;;;###autoload
(defun my-funcA () ...)

;;;###autoload
(defun my-funcB () ...)

;;;###autoload
(defun my-func-init () ...)

If one of the functions is called, will all functions declaration be loaded? Put it in another way, if my-func-init must be loaded in order to use this package, does it mean all the autoloads are redundant?


Solution

  • On their own, these comments are just comments.

    A separate process is used to extract the associated definitions into a loaddefs.el file. When that file is loaded, all of those autoloads are defined. Thus all of the autoloads for a great many libraries can be bundled together into a single file, which is fast to load.

    M-x elisp-index-search RET autoload cookie RET

    A magic autoload comment (often called an "autoload cookie") consists of ‘;;;###autoload’, on a line by itself, just before the real definition of the function in its autoloadable source file. The command ‘M-x update-file-autoloads’ writes a corresponding ‘autoload’ call into ‘loaddefs.el’. (The string that serves as the autoload cookie and the name of the file generated by ‘update-file-autoloads’ can be changed from the above defaults, see below.) Building Emacs loads ‘loaddefs.el’ and thus calls ‘autoload’. ‘M-x update-directory-autoloads’ is even more powerful; it updates autoloads for all files in the current directory.

    The package manager processes the autoload cookies for any given package, so package authors can simply add those comments as appropriate.