Thursday, February 5, 2009

Speeding up your Emacs - autoloads

Following a bit of re-structuring I was pleasantly surprised to see how easy it is to generate your own autoloads in order to speed up Emacs. Simply put, autoloads mean you do not load files or functions until you need them. Instead you read in a catalog of stubs which tell the system what to load if and when certain functions are called. And it's incredibly easy to use.

The first you do is make use of the "magic comment". All that is a special format line prior to your defun lines. e.g

  • ;;;###autoload
  • (defun rgr/anything ()
  • "default to thing at point for anything"
  • (interactive)
  • (anything nil (region-or-word-at-point)))
Here the ";;;###autoload" tells the autload generator to include the following functions. Then having set the necessary autoload file name, you can generate your autoload file which is commonly, but not necessarily, called "loaddefs.el". Here I use "my-autoload.el":

  • ;; Generate autoload for locally downloaded and developed elisp files
  • ;;;###autoload
  • (defun my-autoloads nil
  • (interactive)
  • "Generate autoloads for code in ~/.emacs.d/."
  • (interactive)
  • (let ((generated-autoload-file "~/.emacs.d/my-autoload.el"))
  • (update-directory-autoloads "~/.emacs.d")))
Then call my-autoloads and bobs your uncle. The main thing to consider is keeping your functions seperate from the key definitions (as one example) which call them since you would need to load that file to install the key binding thus slightly defeating the object!

I don't need to mention that you need to load the autoload file do I?

  • (require 'my-autoload)

No comments:

Post a Comment