What is "with-eval-after-load" in Emacs Lisp

Talespin_Kit picture Talespin_Kit · Feb 19, 2014 · Viewed 7.4k times · Source

I came across the macro with-eval-after-load when trying to install persp-mode from here. But I am unable to find the macro inside Emacs and/or on Google. Where is it defined? Is it part of standard Emacs Lisp?

Answer

legoscia picture legoscia · Feb 19, 2014

From etc/NEWS:

* Lisp Changes in Emacs 24.4
...
** New macro `with-eval-after-load'.
This is like the old `eval-after-load', but better behaved.

Emacs 24.4 was released on 20th October 2014.

eval-after-load is considered ill-behaved because it is a function, not a macro, and thus requires the code inside it to be quoted, which means that it cannot be byte-compiled. It also accepts only one form, so if you have more than one, you need to use progn. For example:

(eval-after-load "foo"
  '(progn
     (setq foo 42)
     (setq bar 17)))

The equivalent version with with-eval-after-load would be:

(with-eval-after-load "foo"
  (setq foo 42)
  (setq bar 17))

As noted by Clément in a comment, one disadvantage of with-eval-after-load is that you cannot rely on macros defined in the module in question, while with eval-after-load you can be sure that such macros are defined and available to use. This was discussed on emacs-devel.