3

I maintain a somewhat popular Emacs package (ido-ubiquitous), and I would like to add the ;;;###autoload comments to my package so that it does not need to be loaded until its mode is activated. I attempted this a while ago with the same package, and it resulted in a number of cryptic errors that only occurred when the package was autoloaded, so I removed all the autoload stuff and the problems went away. I'd like to try again, but only if I can find definitive documentation on how to do it so I don't end up introducing the same bugs again.

So is there a definitive guide to adding autoload cookies to an Emacs Lisp package?

EDIT: Looking at my Git logs for my package, I see a couple of commits referring to autoloads:

  • 283f9e9 Remove unnecessary autoloads
  • 66b782f Autoload default values of override variables
  • f6086e5 Autoload fix
  • 0eed206 Remove unnecessary autoload cookie

These commits show that I was really not sure what to add autoloads to. In particular, f6086e5 and 66b782f shows that I thought I should autoload custom variables, and concluded that I also needed to autoload the defconst forms that defined their default values. If I recall correctly, this was in response to a void-variable error because the autoloaded defcustom didn't have access to the non-autoloaded default value (issue link). Finally, in 283f9e9 I had a vision of the future and took Stefan's advice by removing all autoloads except for the one on the minor mode definition.

Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159

2 Answers2

4

The ;;;###autoload cookies simply mark code which needs to be lifted into a <pkg>-autoloads.el file. This file is then loaded eagerly on startup, but that can be done quickly because it's a much smaller file. So you typically only need such a cookie on the few main entry points. E.g. I'd start by only putting one such cookie on ido-ubiquitous-mode.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • Is is necessary to put autoload cookies for things like `defcustom`s and other interactive functions (only one of those in this case)? – Ryan C. Thompson Nov 21 '13 at 11:53
  • 1
    It is generally undesirable to put autoload cookies on variables. It is useful/necessary on interactive functions which might be called before any other (i.e. might be called when org-ubiquitous.el is not yet loaded). – Stefan Nov 21 '13 at 12:19
  • What about `defadvice` ? – Adam Spiers Nov 27 '13 at 14:24
  • 1
    An autoload cookie is normally not needed for defadvice, because the advice normally only needs to be installed/activated once the package is loaded. – Stefan Nov 27 '13 at 16:58
0

The documentation for autoloading is here:

C-hig (elisp) Autoload RET

Are you able to give any details on the errors you encountered?

phils
  • 71,335
  • 11
  • 153
  • 198
  • I guess what I mean is that I know how to mark things as autoloaded, but I'm not sure *which* things I'm supposed to mark as autoloaded. The info page that you reference really only addresses the first issue. In any case, when I have time I'll dig out an example error. – Ryan C. Thompson Nov 20 '13 at 06:02