Lazy Loading Emacs Functionality
Quick, how big is your .emacs
file? How long does it take emacs to
load? A few days ago I found that my .emacs
file had
slowly grown to the point where it was taking emacs a significant
amount of time to load. Something needed to be done.
A quick glance at the file told me I was loading a lot of modules that I seldom use. For instance, I occasionally write some code in Common Lisp or Haskell, so naturally I was loading slime and the haskell environment. I commented those out.
However, after commenting them out, I quickly realized that not
loading them was problematic. When I wanted to use them, I had to open
my .emacs
file, uncomment the relevant portion, and
M‑x eval-region
the code.
Then it occurred to me that I had a clear entry point for some of
these modules. When I write Common Lisp, I usually start by loading
SLIME’s REPL by running M‑x slime
. I came up with the following
code:
(defun slime ()
(interactive)
(add-to-list 'load-path "~/emacs/slime")
(setq inferior-lisp-program "/usr/local/bin/sbcl")
(require 'slime)
(slime-setup)
(slime))
Which basically sets up my slime environment and then loads
slime. Note that the slime
function itself is redefined as part of
the evaluation of (require 'slime)
, so although this looks like a
recursive call, it’s not.
I’ve used this trick in several other cases and I think it’s kind of neat. It helps me keep Emacs loading fast, but I still have all the bells and whistles available.