Charles Lu
Charles Lu

Reputation: 511

What's the mechanism to ensure the autoload functions in emacs autoloaded

I understand all the functions marked with a line ";;;###Autoload" will be an autoloaded function, but the question is what's the mechanism underlying to excute this 'autoloaded function?'. And also why when one installs a package from elpa, there's a compiled file called XXX-autoload.elc?

Upvotes: 3

Views: 800

Answers (3)

phils
phils

Reputation: 73274

As Drew commented, you can find the details in the manual at:

  • C-hig (elisp) Autoload RET
  • C-hig (elisp) Packaging Basics RET

This may be the aspect you most wanted to understand:

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'.

In other words, those autoload cookies are only used when generating the loaddefs.el files, and the ELPA package *-autoload.el files, and Emacs must then load those files in order to register the autoload definitions.

Emacs loads some of these files automatically, and elisp package managers generally ensure that package autoloads are loaded.

You could also use these mechanisms to generate your own custom autoload files, and tell Emacs to load them via your init file. More commonly, people often write (autoload ...) forms directly in their init file -- this is the simplest way to provide autoloading for functions which have no autoload cookies in their source.

Upvotes: 1

Brian Burns
Brian Burns

Reputation: 22032

When you install an Elpa package, Emacs scans through all the .el files in the package for autoload tokens and adds stubs for those functions/variables to a [package]-autoloads.el file. The stubs just attach information to the function/variable symbols that tell Emacs where to find the actual definitions, and what the docstring says. Then it compiles all the .el files to .elc files, including the autoloads file.

Then (and every time you start Emacs) it loads and evaluates the [package]-autoloads.elc file, rather than the main .elc files. That way it's much faster to load packages, as the main .elc files will only be loaded when one of the autoloaded symbols is invoked. The other functions/variables in the main .el files are not known to Emacs until then.

Upvotes: 5

abo-abo
abo-abo

Reputation: 20342

Your own (non-ELPA) autoload requires two things from you:

  • to generate a loaddefs.el file
  • to load the loaddefs.el file on startup

To generate it, you can use a function similar to mine:

(defun update-all-autoloads ()
  (interactive)
  (cd emacs-d)
  (let ((generated-autoload-file
         (expand-file-name "loaddefs.el")))
    (when (not (file-exists-p generated-autoload-file))
      (with-current-buffer (find-file-noselect
                            generated-autoload-file)
        (insert ";;")
        (save-buffer)))
    (mapc #'update-directory-autoloads
            '("" "oleh" "oleh/modes"))))

To load, simply:

(load (concat emacs-d "loaddefs.el") nil t)

How it works: the first function collects all the autoloaded functions from desired directories and defines them with only the name and location, no body.

Since the body isn't evaluated it's supposed to speed things up. And when you call this "empty" body, it knows where to load the actual one.

The code above is taken from my blog post, there may be more context there if you need it.

Upvotes: 2

Related Questions