I have developed some private extensions to some major modes, and I would like to load those extensions only when the given major mode is actually used by the user. The extensions consists of a set of functions confined to a single file. For example, for emacs-lisp-mode, I have developed a file "~/emacs/my-emacs-lisp-mode-hook.el, here is a minimal example for illustration purposes:

(defun my-emacs-lisp-mode-hook ()

(defun my-hook-func ()
  (message-box "This is my hook function"))

And then, in my Emacs init file ~/.emacs I have:

(autoload 'my-emacs-lisp-mode-hook "~/emacs/my-emacs-lisp-mode-hook.el" "My Emacs Lisp mode hook" nil nil)
(add-hook 'emacs-lisp-mode-hook 'my-emacs-lisp-mode-hook)

Note that I do not have an autoload call for the function my-hook-func. The question is: If it is sufficient to call autoload only for the function my-emacs-lisp-mode-hook, or if I have to call autoload for each function in the file "~/emacs/my-emacs-lisp-mode-hook.el? The idea is that, the other functions in the file will never be used unless a buffer switches to major mode emacs-lisp-mode and the hook function my-emacs-lisp-mode-hook has run.

Currently, this strategy seems to work (I get the message box created in my-hook-func even if I have no explicit autoload statement for that function). But I could not find a clear statement of this behavior in the manual. So is this a viable strategy? (It would certainly allow me to simplify my code, that is: not having to add autoload cookies for each function, and not call update-file-autoloads and so on..)

  • And BTW, you don't need to (and shouldn't) autoload hooks. Just calling add-hook will work even if the hook is not defined yet.
    – Malabarba
    Feb 8, 2015 at 10:17
  • @Malabarba I guess you are talking about the case where the hook function is loaded later by the Emacs init file.. I am not considering that case, I am considering the case where the hook is defined in a another file that is not loaded yet (and not will be loaded later, unless I had used autoload to inform Emacs in which file to find the hook).. Feb 8, 2015 at 10:35
  • 1
    actually I was talking about the hook itself. But I see now I misread your question. You're autoloading the function, not the hook. Just ignore me. :-)
    – Malabarba
    Feb 8, 2015 at 10:47

2 Answers 2


Once a single autoloaded call is triggered, the whole file will be required.

The common strategy is to put ;;;###autoload on the user interface entry points: the most used interactive functions. For instance, for a minor mode, usually only the minor mode definition needs to be autoloaded.

Finding the following info

The "autoload" facility lets you register the existence of a function or macro, but put off loading the file that defines it. The first call to the function automatically loads the proper library, in order to install the real definition and other associated code, then runs the real definition as if it had been loaded all along. Autoloading can also be triggered by looking up the documentation of the function or macro

  • F1 i to launch info
  • g (elisp) to go to Elisp node
  • i autoload to search the index for the word autoload
  • that's it, the topic is the first one (out of thirteen) in the index
  • Thanks @abo-abo! Then I think my strategy will work also :) Could you find a reference to the place in the manual where this behavior is stated? Feb 7, 2015 at 14:08

Consider asking Emacs first.

C-h r i autoload takes you to node Lisp Libraries in the Emacs manual, where you see, among other things:

Some commands are "autoloaded"; when you run them, Emacs automatically loads the associated library first. For instance, the M-x compile command (*note Compilation::) is autoloaded; if you call it, Emacs automatically loads the compile library first. In contrast, the command M-x recompile is not autoloaded, so it is unavailable until you load the compile library.

Not that it says clearly that it loads the library where the command is defined, not just the command definition.

When you use i autoload a message tells you:

Found autoload in Concept Index. (2 total; use , for next)

That is, this is not the only node indexed for "autoload"; there is one more. And if you hit , then Emacs takes you to node Init Examples, where you see this:

  • Tell Emacs to find the definition for the function myfunction by loading a Lisp library named mypackage (i.e., a file mypackage.elc or mypackage.el):

     (autoload 'myfunction "mypackage" "Do what I say." t)

Again, note that it tells you that autoloading loads a library.

You will of course find much more detail about autoloading in the Elisp manual, but even this cursory glance at the Emacs manual provides the answer to your question.

Ask Emacs.

  • Thanks @Drew for the help on how to search the included Emacs manual! Good to know, but it seems like it is easier to search the online manual with Google. BTW, when I press C-h r I get the Emacs manual, is there keyboard shortcut to open the Emacs Lisp manual also? (I tried C-h C-h but could not find any...) Feb 8, 2015 at 10:59
  • 1
    There is no predefined key sequence for the Elisp manual, but you can of course define your own. And C-h i opens Info to the directory of all manuals, so C-h i m followed by a char or two is, in effect, a shortcut for any given manual. E.g., C-h i m g RET opens the Gnus manual.
    – Drew
    Feb 8, 2015 at 15:05
  • Is it really easier to use Google? The indices in the Emacs manual are pretty good and you will only find information useful for the installed version.
    – user2005
    Jun 15, 2015 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.