7

I am trying to figure out how to add autoload features to a private library. Let's say I have developed a private Lisp library "my-lib.el". It consist of a set of functions, some can be used from any mode, others are only used for a particular mode. According to the manual I could put a autoload cookie above each function definition, and then call update-file-autoloads. Is this correct? But when do I call update-file-autoloads? In the beginning of the Emacs init file?

For example, in my ~/.emacs init file, I could write:

(update-file-autoloads "my-lib.el")
(require 'my-lib)

Where in "my-lib.el"

;;;###autoload
(defun my-func()
  (interactive)
  (message "Hello")) 

;;; more functions, omitted here...

(provide 'my-lib) 

But this gives error from update-file-autoloads if the file is not in the current directory. Or if "my-lib.el" is in the current directory, I get the error: Wrong type argument: stringp, nil.

My aim is to be able to type M-x my-func (as an example), and my-func should autoload at that point if this was the first time I called it.

Håkon Hægland
  • 3,608
  • 1
  • 20
  • 51

2 Answers2

5

That's not how I would do it. I would:

  1. update-file-autoloads for "my-lib.el" to "my-lib-autoloads.el".
  2. (require 'my-lib-autoloads) in "init.el".

That's it. Now, each time you add or remove an autoload cookie, you should update "my-lib-autoloads.el". Don't parse "my-lib.el" at startup: it's probably slower than just loading it.

abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • Thanks @abo-abo. But why do I get error: `Wrong type argument: stringp, nil` from `update-file-autoloads` ? According to `C-h f update-file-autoloads` the syntax is: `(update-file-autoloads FILE &optional SAVE-AFTER OUTFILE)` .. and what is the purpose of the `SAVE-AFTER` argument? – Håkon Hægland Feb 06 '15 at 14:18
  • I did the thing interactively. The following code also works: `(update-file-autoloads "lispy.el" t (expand-file-name "lispy-autoloads.el"))` – abo-abo Feb 06 '15 at 14:27
  • `SAVE-AFTER` will save the "-autoloads.el" file if it's already open, I guess. – abo-abo Feb 06 '15 at 14:28
  • Ok, I see.. But now this produces a file `"my-lib-autoloads.el"` with only one statement `"(provide 'my-lib-autoloads)`".. Shouldn't there be some `autoload` statements in there too? – Håkon Hægland Feb 06 '15 at 14:35
  • Do you have `;;;###autoload` in the file? – abo-abo Feb 06 '15 at 16:28
  • Yes, I have. To debug, I used a file that is identical to the one shown in the Question above.. – Håkon Hægland Feb 06 '15 at 17:33
  • I think I found the problem. It seems like `update-file-autoloads` creates a new buffer with `"my-lib-autoloads.el"`, but it does not save the buffer to the file with the same name. That was what confused me, since I looked at the file, and not the buffer :).. Wonder, why it does not save the buffer by default? – Håkon Hægland Feb 06 '15 at 17:44
3

If you are using package.el, after changing my-lib.el every time, use package-install-from-buffer to install your private library as a package. You don't need to issue (require 'my-lib) in your init file for invoking autoload commands because package.el has already generated "my-lib-autoloads.el" and loaded it for you.

xuchunyang
  • 14,302
  • 1
  • 18
  • 39